passfd: Work around bug with CMSG_FIRSTHDR on FreeBSD 6.4.
[pspp] / m4 / afunix.m4
1 # afunix.m4 serial 4
2 dnl Copyright (C) 2011 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_SOCKET_AFUNIX],
8 [
9   AC_REQUIRE([gl_HEADER_SYS_SOCKET])
10   AC_REQUIRE([gl_SOCKET_FAMILY_UNIX])
11
12   AC_MSG_CHECKING([for UNIX domain sockets SCM_RIGHTS])
13   AC_CACHE_VAL([gl_cv_socket_unix_scm_rights],
14     [AC_COMPILE_IFELSE(
15        [AC_LANG_PROGRAM(
16           [[#include <sys/types.h>
17             #ifdef HAVE_SYS_SOCKET_H
18             #include <sys/socket.h>
19             #endif
20             #ifdef HAVE_SYS_UN_H
21             #include <sys/un.h>
22             #endif
23             #ifdef HAVE_WINSOCK2_H
24             #include <winsock2.h>
25             #endif
26           ]],
27           [[struct cmsghdr cmh;
28             cmh.cmsg_level = SOL_SOCKET;
29             cmh.cmsg_type = SCM_RIGHTS;
30             if (&cmh) return 0;
31           ]])],
32        [gl_cv_socket_unix_scm_rights=yes],
33        [gl_cv_socket_unix_scm_rights=no])
34     ])
35   AC_MSG_RESULT([$gl_cv_socket_unix_scm_rights])
36   if test $gl_cv_socket_unix_scm_rights = yes; then
37     AC_DEFINE([HAVE_UNIXSOCKET_SCM_RIGHTS], [1],
38       [Define to 1 if <sys/socket.h> defines SCM_RIGHTS.])
39   fi
40
41   AC_MSG_CHECKING([for UNIX domain sockets SCM_RIGHTS that behave in BSD4.4 way])
42   AC_CACHE_VAL([gl_cv_socket_unix_scm_rights_bsd44_way],
43     [AC_COMPILE_IFELSE(
44        [AC_LANG_PROGRAM(
45           [[#include <sys/types.h>
46             #include <stddef.h>
47             #ifdef HAVE_SYS_SOCKET_H
48             #include <sys/socket.h>
49             #endif
50             #ifdef HAVE_SYS_UN_H
51             #include <sys/un.h>
52             #endif
53             #ifdef HAVE_WINSOCK2_H
54             #include <winsock2.h>
55             #endif
56             ]],
57             [[struct msghdr msg = {0};
58               struct cmsghdr *cmsg;
59               int myfds[1] = {0};
60               char buf[CMSG_SPACE (sizeof (myfds))];
61
62               msg.msg_control = buf;
63               msg.msg_controllen = sizeof buf;
64               cmsg = CMSG_FIRSTHDR (&msg);
65               cmsg->cmsg_level = SOL_SOCKET;
66               cmsg->cmsg_type = SCM_RIGHTS;
67               cmsg->cmsg_len = CMSG_LEN (sizeof (int));
68               /* fake Initialize the payload: */
69               (void) CMSG_DATA (cmsg);
70               /* Sum of the length of all control messages in the buffer: */
71               msg.msg_controllen = cmsg->cmsg_len;
72               return 0;
73             ]])],
74        [gl_cv_socket_unix_scm_rights_bsd44_way=yes],
75        [gl_cv_socket_unix_scm_rights_bsd44_way=no])
76     ])
77   AC_MSG_RESULT([$gl_cv_socket_unix_scm_rights_bsd44_way])
78   if test $gl_cv_socket_unix_scm_rights_bsd44_way = yes; then
79     AC_DEFINE([HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY], [1],
80       [Define to 1 if fd can be sent/received in the BSD4.4 way.])
81   fi
82
83   AC_MSG_CHECKING([for UNIX domain sockets SCM_RIGHTS that behave in BSD4.3 way])
84   AC_CACHE_VAL([gl_cv_socket_unix_scm_rights_bsd43_way],
85     [AC_COMPILE_IFELSE(
86        [AC_LANG_PROGRAM(
87           [[#include <sys/types.h>
88             #ifdef HAVE_SYS_SOCKET_H
89             #include <sys/socket.h>
90             #endif
91             #ifdef HAVE_SYS_UN_H
92             #include <sys/un.h>
93             #endif
94             #ifdef HAVE_WINSOCK2_H
95             #include <winsock2.h>
96             #endif
97             ]],
98             [[struct msghdr msg;
99               int fd = 0;
100               msg.msg_accrights = &fd;
101               msg.msg_accrightslen = sizeof (fd);
102               if (&msg) return 0;
103             ]])],
104        [gl_cv_socket_unix_scm_rights_bsd43_way=yes],
105        [gl_cv_socket_unix_scm_rights_bsd43_way=no])
106     ])
107   AC_MSG_RESULT([$gl_cv_socket_unix_scm_rights_bsd43_way])
108   if test $gl_cv_socket_unix_scm_rights_bsd43_way = yes; then
109     AC_DEFINE([HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY], [1],
110       [Define to 1 if fd can be sent/received in the BSD4.3 way.])
111   fi
112
113   AC_MSG_CHECKING([for UNIX domain sockets recvmsg() MSG_CMSG_CLOEXEC flag])
114   AC_CACHE_VAL([gl_cv_socket_unix_msg_cmsg_cloexec],
115     [AC_COMPILE_IFELSE(
116        [AC_LANG_PROGRAM(
117           [[#include <sys/types.h>
118             #ifdef HAVE_SYS_SOCKET_H
119             #include <sys/socket.h>
120             #endif
121             #ifdef HAVE_SYS_UN_H
122             #include <sys/un.h>
123             #endif
124             #ifdef HAVE_WINSOCK2_H
125             #include <winsock2.h>
126             #endif
127             ]],
128             [[int flags = MSG_CMSG_CLOEXEC;
129               if (&flags) return 0;
130             ]])],
131        [gl_cv_socket_unix_msg_cmsg_cloexec=yes],
132        [gl_cv_socket_unix_msg_cmsg_cloexec=no])
133     ])
134   AC_MSG_RESULT([$gl_cv_socket_unix_msg_cmsg_cloexec])
135   if test $gl_cv_socket_unix_msg_cmsg_cloexec = yes; then
136     AC_DEFINE([HAVE_MSG_CMSG_CLOEXEC], [1],
137       [Define to 1 if recvmsg could be specified with MSG_CMSG_CLOEXEC.])
138   fi
139 ])