From: Eric Blake Date: Thu, 21 Apr 2011 17:48:22 +0000 (-0600) Subject: passfd: speed up configure and drop unused code X-Git-Tag: v0.1~2925 X-Git-Url: http://erislabs.net/gitweb/?a=commitdiff_plain;h=e55355319eb5409871be533d7b9713328a8283db;p=gnulib.git passfd: speed up configure and drop unused code Ultimately, it would be nice to provide a sendmsg and recvmsg module, and have those provide a replacement struct msghdr and silently convert msg_accrights into the replacement struct's msg_control, when targeting older BSD. But until that point in time, this is a nice cleanup. SCM_RIGHTS really only works on Unix sockets; however, there is nothing in passfd.c that needs to be hard-coded to this (rather, the hard-coding of the address family is done externally when the socket or socketpair is created). * m4/afunix.m4 (gl_SOCKET_AFUNIX): Rename... * m4/passfd.m4 (gl_PASSFD): ...to something more fitting of its use. Drop test for setting unused HAVE_UNIXSOCKET_SCM_RIGHTS. Instead of probing at configure for unix_scm_rights_bsd44_way, instead probe for CMSG_FIRSTHDR at compile time. Simplify BSD 4.3 check to a struct member probe. * lib/passfd.c (includes): Nothing here requires . (sendfd, recvfd): Update preprocessor checks. * modules/passfd (Files): Reflect rename, and drop unused file. (Depends-on): Drop unused dependency. Signed-off-by: Eric Blake --- diff --git a/ChangeLog b/ChangeLog index 95fee4897..be6470a8f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,17 @@ 2011-04-21 Eric Blake + passfd: speed up configure and drop unused code + * m4/afunix.m4 (gl_SOCKET_AFUNIX): Rename... + * m4/passfd.m4 (gl_PASSFD): ...to something more fitting of + its use. Drop test for setting unused HAVE_UNIXSOCKET_SCM_RIGHTS. + Instead of probing at configure for unix_scm_rights_bsd44_way, + instead probe for CMSG_FIRSTHDR at compile time. Simplify BSD 4.3 + check to a struct member probe. + * lib/passfd.c (includes): Nothing here requires . + (sendfd, recvfd): Update preprocessor checks. + * modules/passfd (Files): Reflect rename, and drop unused file. + (Depends-on): Drop unused dependency. + passfd: allow compilation on mingw * modules/sys_socket (Depends-on): Add sys_uio. * lib/sys_socket.in.h [!@HAVE_SYS_SOCKET_H@]: Use it for struct diff --git a/lib/passfd.c b/lib/passfd.c index 5d4c6a762..a4d884107 100644 --- a/lib/passfd.c +++ b/lib/passfd.c @@ -27,9 +27,6 @@ #include #include -#if HAVE_SYS_UN_H -# include -#endif #include "cloexec.h" @@ -49,7 +46,7 @@ sendfd (int sock, int fd) char send = 0; struct iovec iov; struct msghdr msg; -# if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY +# ifdef CMSG_FIRSTHDR struct cmsghdr *cmsg; char buf[CMSG_SPACE (sizeof fd)]; # endif @@ -63,7 +60,7 @@ sendfd (int sock, int fd) msg.msg_name = NULL; msg.msg_namelen = 0; -# if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY +# ifdef CMSG_FIRSTHDR msg.msg_control = buf; msg.msg_controllen = sizeof buf; cmsg = CMSG_FIRSTHDR (&msg); @@ -72,7 +69,7 @@ sendfd (int sock, int fd) cmsg->cmsg_len = CMSG_LEN (sizeof fd); /* Initialize the payload: */ memcpy (CMSG_DATA (cmsg), &fd, sizeof fd); -# elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY +# elif HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS msg.msg_accrights = &fd; msg.msg_accrightslen = sizeof fd; # else @@ -107,7 +104,7 @@ recvfd (int sock, int flags) struct iovec iov; struct msghdr msg; int fd = -1; -# if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY +# ifdef CMSG_FIRSTHDR struct cmsghdr *cmsg; char buf[CMSG_SPACE (sizeof fd)]; int flags_recvmsg = flags & O_CLOEXEC ? MSG_CMSG_CLOEXEC : 0; @@ -128,7 +125,7 @@ recvfd (int sock, int flags) msg.msg_name = NULL; msg.msg_namelen = 0; -# if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY +# ifdef CMSG_FIRSTHDR msg.msg_control = buf; msg.msg_controllen = sizeof buf; cmsg = CMSG_FIRSTHDR (&msg); @@ -166,7 +163,7 @@ recvfd (int sock, int flags) } } -# elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY +# elif HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS msg.msg_accrights = &fd; msg.msg_accrightslen = sizeof fd; if (recvmsg (sock, &msg, 0) < 0) diff --git a/m4/afunix.m4 b/m4/afunix.m4 deleted file mode 100644 index 3f5eb447d..000000000 --- a/m4/afunix.m4 +++ /dev/null @@ -1,117 +0,0 @@ -# afunix.m4 serial 7 -dnl Copyright (C) 2011 Free Software Foundation, Inc. -dnl This file is free software; the Free Software Foundation -dnl gives unlimited permission to copy and/or distribute it, -dnl with or without modifications, as long as this notice is preserved. - -AC_DEFUN([gl_SOCKET_AFUNIX], -[ - AC_REQUIRE([gl_HEADER_SYS_SOCKET]) - AC_REQUIRE([gl_SOCKET_FAMILY_UNIX]) - AC_CHECK_FUNCS_ONCE([recvmsg sendmsg]) - - AC_MSG_CHECKING([for UNIX domain sockets SCM_RIGHTS]) - AC_CACHE_VAL([gl_cv_socket_unix_scm_rights], - [AC_COMPILE_IFELSE( - [AC_LANG_PROGRAM( - [[#include - #ifdef HAVE_SYS_SOCKET_H - #include - #endif - #ifdef HAVE_SYS_UN_H - #include - #endif - #ifdef HAVE_WINSOCK2_H - #include - #endif - ]], - [[struct cmsghdr cmh; - cmh.cmsg_level = SOL_SOCKET; - cmh.cmsg_type = SCM_RIGHTS; - if (&cmh) return 0; - ]])], - [gl_cv_socket_unix_scm_rights=yes], - [gl_cv_socket_unix_scm_rights=no]) - ]) - AC_MSG_RESULT([$gl_cv_socket_unix_scm_rights]) - if test $gl_cv_socket_unix_scm_rights = yes; then - AC_DEFINE([HAVE_UNIXSOCKET_SCM_RIGHTS], [1], - [Define to 1 if defines SCM_RIGHTS.]) - fi - - dnl Persuade AIX 5.2 to declare CMSG_SPACE, CMSG_LEN. - AC_DEFINE([_LINUX_SOURCE_COMPAT], [1], - [Define in order to get some macros on AIX systems.]) - - AC_MSG_CHECKING([for UNIX domain sockets SCM_RIGHTS that behave in BSD4.4 way]) - AC_CACHE_VAL([gl_cv_socket_unix_scm_rights_bsd44_way], - [AC_COMPILE_IFELSE( - [AC_LANG_PROGRAM( - [[#include - #include - #ifdef HAVE_SYS_SOCKET_H - #include - #endif - #ifdef HAVE_SYS_UN_H - #include - #endif - #ifdef HAVE_WINSOCK2_H - #include - #endif - ]], - [[struct msghdr msg = {0}; - struct cmsghdr *cmsg; - int myfds[1] = {0}; - char buf[CMSG_SPACE (sizeof (myfds))]; - - msg.msg_control = buf; - msg.msg_controllen = sizeof buf; - cmsg = CMSG_FIRSTHDR (&msg); - cmsg->cmsg_level = SOL_SOCKET; - cmsg->cmsg_type = SCM_RIGHTS; - cmsg->cmsg_len = CMSG_LEN (sizeof (int)); - /* fake Initialize the payload: */ - (void) CMSG_DATA (cmsg); - /* Sum of the length of all control messages in the buffer: */ - msg.msg_controllen = cmsg->cmsg_len; - return 0; - ]])], - [gl_cv_socket_unix_scm_rights_bsd44_way=yes], - [gl_cv_socket_unix_scm_rights_bsd44_way=no]) - ]) - AC_MSG_RESULT([$gl_cv_socket_unix_scm_rights_bsd44_way]) - if test $gl_cv_socket_unix_scm_rights_bsd44_way = yes; then - AC_DEFINE([HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY], [1], - [Define to 1 if fd can be sent/received in the BSD4.4 way.]) - fi - - AC_MSG_CHECKING([for UNIX domain sockets SCM_RIGHTS that behave in BSD4.3 way]) - AC_CACHE_VAL([gl_cv_socket_unix_scm_rights_bsd43_way], - [AC_COMPILE_IFELSE( - [AC_LANG_PROGRAM( - [[#include - #ifdef HAVE_SYS_SOCKET_H - #include - #endif - #ifdef HAVE_SYS_UN_H - #include - #endif - #ifdef HAVE_WINSOCK2_H - #include - #endif - ]], - [[struct msghdr msg; - int fd = 0; - msg.msg_accrights = &fd; - msg.msg_accrightslen = sizeof (fd); - if (&msg) return 0; - ]])], - [gl_cv_socket_unix_scm_rights_bsd43_way=yes], - [gl_cv_socket_unix_scm_rights_bsd43_way=no]) - ]) - AC_MSG_RESULT([$gl_cv_socket_unix_scm_rights_bsd43_way]) - if test $gl_cv_socket_unix_scm_rights_bsd43_way = yes; then - AC_DEFINE([HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY], [1], - [Define to 1 if fd can be sent/received in the BSD4.3 way.]) - fi -]) diff --git a/m4/passfd.m4 b/m4/passfd.m4 new file mode 100644 index 000000000..2028f7f55 --- /dev/null +++ b/m4/passfd.m4 @@ -0,0 +1,23 @@ +# passfd.m4 serial 8 +dnl Copyright (C) 2011 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_PASSFD], +[ + AC_REQUIRE([gl_HEADER_SYS_SOCKET]) + AC_CHECK_FUNCS_ONCE([recvmsg sendmsg]) + + dnl Persuade AIX 5.2 to declare CMSG_SPACE, CMSG_LEN. + dnl CMSG_FIRSTHDR is POSIX 2008, but CMSG_SPACE is only in RFC 3542. + AC_DEFINE([_LINUX_SOURCE_COMPAT], [1], + [Define in order to get some macros on AIX systems.]) + + dnl Passfd prefers the POSIX use of msg.msg_control if the CMSG_* macros + dnl are present, but can fall back to BSD 4.3 style of msg.msg_accrights. + AC_CHECK_MEMBERS([struct msghdr.msg_accrights], [], [], [[ + #include + #include + ]]) +]) diff --git a/modules/passfd b/modules/passfd index 74ef4102c..b3d8a3c02 100644 --- a/modules/passfd +++ b/modules/passfd @@ -4,17 +4,15 @@ Pass file descriptors along Unix domain sockets Files: lib/passfd.h lib/passfd.c -m4/afunix.m4 -m4/sockpfaf.m4 +m4/passfd.m4 Depends-on: cloexec sys_socket socketlib -extensions configure.ac: -gl_SOCKET_AFUNIX +gl_PASSFD Makefile.am: lib_SOURCES += passfd.c