8095ae921d58422191fd01e25c7e571c1f4e5328
[gnulib.git] / m4 / afunix.m4
1 # afunix.m4 serial 2
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             #ifdef HAVE_SYS_SOCKET_H
47             #include <sys/socket.h>
48             #endif
49             #ifdef HAVE_SYS_UN_H
50             #include <sys/un.h>
51             #endif
52             #ifdef HAVE_WINSOCK2_H
53             #include <winsock2.h>
54             #endif
55             ]],
56             [[struct msghdr msg = {0};
57               struct cmsghdr *cmsg;
58               int myfds[1] = {0};
59               char buf[CMSG_SPACE (sizeof (myfds))];
60               int *fdptr;
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 ])