Mercurial > hg > octave-kai > gnulib-hg
changeset 14509:509ee86b2306
passfd: standardize coding conventions
* m4/afunix.m4 (gl_SOCKET_AFUNIX): Drop check for something that
can be learned at compile time.
* lib/passfd.c (MSG_CMSG_CLOEXEC): Reduce number of in-function
ifdefs.
(passfd, recvfd): Follow gnulib code conventions.
Signed-off-by: Eric Blake <eblake@redhat.com>
author | Eric Blake <eblake@redhat.com> |
---|---|
date | Wed, 30 Mar 2011 15:30:13 -0600 |
parents | 6bacc85ddf06 |
children | 9f461c37c0fa |
files | ChangeLog lib/passfd.c m4/afunix.m4 |
diffstat | 3 files changed, 38 insertions(+), 60 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2011-03-30 Eric Blake <eblake@redhat.com> + passfd: standardize coding conventions + * m4/afunix.m4 (gl_SOCKET_AFUNIX): Drop check for something that + can be learned at compile time. + * lib/passfd.c (MSG_CMSG_CLOEXEC): Reduce number of in-function + ifdefs. + (passfd, recvfd): Follow gnulib code conventions. + passfd: fix incorrect sendmsg arguments * lib/passfd.c (sendfd): Avoid uninitialized msg_flags field, and incorrect msg_controllen value.
--- a/lib/passfd.c +++ b/lib/passfd.c @@ -34,6 +34,10 @@ #include "cloexec.h" +#ifndef MSG_CMSG_CLOEXEC +# define MSG_CMSG_CLOEXEC 0 +#endif + /* sendfd sends the file descriptor fd along the socket to a process calling recvfd on the other end. @@ -43,41 +47,41 @@ sendfd (int sock, int fd) { char send = 0; - struct iovec iov[1]; + struct iovec iov; struct msghdr msg; /* send at least one char */ memset (&msg, 0, sizeof msg); - iov[0].iov_base = &send; - iov[0].iov_len = 1; - msg.msg_iov = iov; + iov.iov_base = &send; + iov.iov_len = 1; + msg.msg_iov = &iov; msg.msg_iovlen = 1; - msg.msg_name = 0; + msg.msg_name = NULL; msg.msg_namelen = 0; { #if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY struct cmsghdr *cmsg; - char buf[CMSG_SPACE (sizeof (fd))]; + char buf[CMSG_SPACE (sizeof fd)]; msg.msg_control = buf; - msg.msg_controllen = sizeof (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)); + cmsg->cmsg_len = CMSG_LEN (sizeof fd); /* Initialize the payload: */ - memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd)); + memcpy (CMSG_DATA (cmsg), &fd, sizeof fd); #elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY msg.msg_accrights = &fd; - msg.msg_accrightslen = sizeof (fd); + msg.msg_accrightslen = sizeof fd; #else errno = ENOSYS; return -1; #endif } - if (sendmsg (sock, &msg, 0) != iov[0].iov_len) + if (sendmsg (sock, &msg, 0) != iov.iov_len) return -1; return 0; } @@ -91,7 +95,7 @@ recvfd (int sock, int flags) { char recv = 0; - struct iovec iov[1]; + struct iovec iov; struct msghdr msg; if ((flags & ~O_CLOEXEC) != 0) @@ -101,33 +105,29 @@ } /* send at least one char */ - iov[0].iov_base = &recv; - iov[0].iov_len = 1; - msg.msg_iov = iov; + iov.iov_base = &recv; + iov.iov_len = 1; + msg.msg_iov = &iov; msg.msg_iovlen = 1; - msg.msg_name = 0; + msg.msg_name = NULL; msg.msg_namelen = 0; { #if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY int fd; struct cmsghdr *cmsg; - char buf[CMSG_SPACE (sizeof (fd))]; + char buf[CMSG_SPACE (sizeof fd)]; const int mone = -1; -# if HAVE_MSG_CMSG_CLOEXEC - int flags_recvmsg = (flags & O_CLOEXEC ? MSG_CMSG_CLOEXEC : 0); -# else - int flags_recvmsg = 0; -# endif + int flags_recvmsg = flags & O_CLOEXEC ? MSG_CMSG_CLOEXEC : 0; msg.msg_control = buf; - msg.msg_controllen = sizeof (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)); + cmsg->cmsg_len = CMSG_LEN (sizeof mone); /* Initialize the payload: */ - memcpy (CMSG_DATA (cmsg), &mone, sizeof (mone)); + memcpy (CMSG_DATA (cmsg), &mone, sizeof mone); msg.msg_controllen = cmsg->cmsg_len; if (recvmsg (sock, &msg, flags_recvmsg) < 0) @@ -135,7 +135,7 @@ cmsg = CMSG_FIRSTHDR (&msg); /* be paranoiac */ - if (cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof (int)) + if (cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof fd) || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { /* fake errno: at end the file is not available */ @@ -143,11 +143,10 @@ return -1; } - memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd)); + memcpy (&fd, CMSG_DATA (cmsg), sizeof fd); -# if !HAVE_MSG_CMSG_CLOEXEC /* set close-on-exec flag */ - if (flags & O_CLOEXEC) + if (!MSG_CMSG_CLOEXEC && (flags & O_CLOEXEC)) { if (set_cloexec_flag (fd, true) < 0) { @@ -157,7 +156,6 @@ return -1; } } -# endif return fd; @@ -165,7 +163,7 @@ int fd; msg.msg_accrights = &fd; - msg.msg_accrightslen = sizeof (fd); + msg.msg_accrightslen = sizeof fd; if (recvmsg (sock, &msg, 0) < 0) return -1; @@ -175,7 +173,7 @@ if (set_cloexec_flag (fd, true) < 0) { int saved_errno = errno; - (void) close (fd); + close (fd); errno = saved_errno; return -1; }
--- a/m4/afunix.m4 +++ b/m4/afunix.m4 @@ -1,4 +1,4 @@ -# afunix.m4 serial 5 +# afunix.m4 serial 6 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, @@ -113,31 +113,4 @@ AC_DEFINE([HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY], [1], [Define to 1 if fd can be sent/received in the BSD4.3 way.]) fi - - AC_MSG_CHECKING([for UNIX domain sockets recvmsg() MSG_CMSG_CLOEXEC flag]) - AC_CACHE_VAL([gl_cv_socket_unix_msg_cmsg_cloexec], - [AC_COMPILE_IFELSE( - [AC_LANG_PROGRAM( - [[#include <sys/types.h> - #ifdef HAVE_SYS_SOCKET_H - #include <sys/socket.h> - #endif - #ifdef HAVE_SYS_UN_H - #include <sys/un.h> - #endif - #ifdef HAVE_WINSOCK2_H - #include <winsock2.h> - #endif - ]], - [[int flags = MSG_CMSG_CLOEXEC; - if (&flags) return 0; - ]])], - [gl_cv_socket_unix_msg_cmsg_cloexec=yes], - [gl_cv_socket_unix_msg_cmsg_cloexec=no]) - ]) - AC_MSG_RESULT([$gl_cv_socket_unix_msg_cmsg_cloexec]) - if test $gl_cv_socket_unix_msg_cmsg_cloexec = yes; then - AC_DEFINE([HAVE_MSG_CMSG_CLOEXEC], [1], - [Define to 1 if recvmsg could be specified with MSG_CMSG_CLOEXEC.]) - fi ])