passfd: standardize coding conventions
[gnulib.git] / m4 / afunix.m4
1 # afunix.m4 serial 6
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   dnl Persuade AIX 5.2 <sys/socket.h> to declare CMSG_SPACE, CMSG_LEN.
42   AC_DEFINE([_LINUX_SOURCE_COMPAT], [1],
43     [Define in order to get some macros on AIX systems.])
44
45   AC_MSG_CHECKING([for UNIX domain sockets SCM_RIGHTS that behave in BSD4.4 way])
46   AC_CACHE_VAL([gl_cv_socket_unix_scm_rights_bsd44_way],
47     [AC_COMPILE_IFELSE(
48        [AC_LANG_PROGRAM(
49           [[#include <sys/types.h>
50             #include <stddef.h>
51             #ifdef HAVE_SYS_SOCKET_H
52             #include <sys/socket.h>
53             #endif
54             #ifdef HAVE_SYS_UN_H
55             #include <sys/un.h>
56             #endif
57             #ifdef HAVE_WINSOCK2_H
58             #include <winsock2.h>
59             #endif
60             ]],
61             [[struct msghdr msg = {0};
62               struct cmsghdr *cmsg;
63               int myfds[1] = {0};
64               char buf[CMSG_SPACE (sizeof (myfds))];
65
66               msg.msg_control = buf;
67               msg.msg_controllen = sizeof buf;
68               cmsg = CMSG_FIRSTHDR (&msg);
69               cmsg->cmsg_level = SOL_SOCKET;
70               cmsg->cmsg_type = SCM_RIGHTS;
71               cmsg->cmsg_len = CMSG_LEN (sizeof (int));
72               /* fake Initialize the payload: */
73               (void) CMSG_DATA (cmsg);
74               /* Sum of the length of all control messages in the buffer: */
75               msg.msg_controllen = cmsg->cmsg_len;
76               return 0;
77             ]])],
78        [gl_cv_socket_unix_scm_rights_bsd44_way=yes],
79        [gl_cv_socket_unix_scm_rights_bsd44_way=no])
80     ])
81   AC_MSG_RESULT([$gl_cv_socket_unix_scm_rights_bsd44_way])
82   if test $gl_cv_socket_unix_scm_rights_bsd44_way = yes; then
83     AC_DEFINE([HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY], [1],
84       [Define to 1 if fd can be sent/received in the BSD4.4 way.])
85   fi
86
87   AC_MSG_CHECKING([for UNIX domain sockets SCM_RIGHTS that behave in BSD4.3 way])
88   AC_CACHE_VAL([gl_cv_socket_unix_scm_rights_bsd43_way],
89     [AC_COMPILE_IFELSE(
90        [AC_LANG_PROGRAM(
91           [[#include <sys/types.h>
92             #ifdef HAVE_SYS_SOCKET_H
93             #include <sys/socket.h>
94             #endif
95             #ifdef HAVE_SYS_UN_H
96             #include <sys/un.h>
97             #endif
98             #ifdef HAVE_WINSOCK2_H
99             #include <winsock2.h>
100             #endif
101             ]],
102             [[struct msghdr msg;
103               int fd = 0;
104               msg.msg_accrights = &fd;
105               msg.msg_accrightslen = sizeof (fd);
106               if (&msg) return 0;
107             ]])],
108        [gl_cv_socket_unix_scm_rights_bsd43_way=yes],
109        [gl_cv_socket_unix_scm_rights_bsd43_way=no])
110     ])
111   AC_MSG_RESULT([$gl_cv_socket_unix_scm_rights_bsd43_way])
112   if test $gl_cv_socket_unix_scm_rights_bsd43_way = yes; then
113     AC_DEFINE([HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY], [1],
114       [Define to 1 if fd can be sent/received in the BSD4.3 way.])
115   fi
116 ])