# HG changeset patch # User Bruno Haible # Date 1300027007 -3600 # Node ID 08622275f761dd6a503c2dea5122b13935bb7dda # Parent 236edbfecf1d5991890c35c58dc33bf07b15c223 passfd module, part 2, tweaks. * modules/passfd (Files): Reorder. (Depends-on): Remove errno. (Include): Remove , . * lib/passfd.h: Use a GPLv3+ header. Make C++ safe. * lib/passfd.c: Untabify. Use a GPLv3+ header. Really include the specification header. Include always. Don't include . Use "#if HAVE_..." instead of "#ifdef HAVE_...". (sendfd): Clarify that it sets errno when it fails. (recvfd): Fix specification. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2011-03-13 Bruno Haible + + passfd module, part 2, tweaks. + * modules/passfd (Files): Reorder. + (Depends-on): Remove errno. + (Include): Remove , . + * lib/passfd.h: Use a GPLv3+ header. Make C++ safe. + * lib/passfd.c: Untabify. Use a GPLv3+ header. Really include the + specification header. Include always. Don't include + . Use "#if HAVE_..." instead of "#ifdef HAVE_...". + (sendfd): Clarify that it sets errno when it fails. + (recvfd): Fix specification. + 2011-03-07 Bastien Roucariès passfd module, part 2. diff --git a/lib/passfd.c b/lib/passfd.c --- a/lib/passfd.c +++ b/lib/passfd.c @@ -2,7 +2,7 @@ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or + the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, @@ -16,6 +16,8 @@ #include /* Specification. */ +#include "passfd.h" + #include #include #include @@ -23,20 +25,15 @@ #include #include -#ifdef HAVE_SYS_SOCKET_H #include -#endif -#ifdef HAVE_SYS_UN_H -#include -#endif -#ifdef HAVE_WINSOCK2_H -#include +#if HAVE_SYS_UN_H +# include #endif -/* Sendfd sends the file descriptor fd along the socket +/* sendfd sends the file descriptor fd along the socket to a process calling recvfd on the other end. - - return -1 in case of error, 0 on success + + Return 0 on success, or -1 with errno set in case of error. */ int sendfd (int sock, int fd) @@ -54,7 +51,7 @@ msg.msg_namelen = 0; { -#ifdef HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY +#if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY struct cmsghdr *cmsg; char buf[CMSG_SPACE (sizeof (fd))]; @@ -81,17 +78,14 @@ return 0; } -/* Sendfd sends the file descriptor fd along the socket - to a process calling recvfd on the other end. +/* recvfd receives a file descriptor through the socket. - return -1 in case of error, fd on success + Return 0 on success, or -1 with errno set in case of error. */ int recvfd (int sock) { char recv = 0; - const int mone = -1; - int fd; struct iovec iov[1]; struct msghdr msg; @@ -104,9 +98,11 @@ msg.msg_namelen = 0; { -#ifdef HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY +#if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY + int fd; struct cmsghdr *cmsg; char buf[CMSG_SPACE (sizeof (fd))]; + const int mone = -1; msg.msg_control = buf; msg.msg_controllen = sizeof (buf); @@ -124,16 +120,18 @@ cmsg = CMSG_FIRSTHDR (&msg); /* be paranoiac */ if (cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof (int)) - || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) + || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { - /* fake errno: at end the file is not available */ - errno = EACCES; - return -1; + /* fake errno: at end the file is not available */ + errno = EACCES; + return -1; } memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd)); return fd; #elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY + int fd; + msg.msg_accrights = &fd; msg.msg_accrightslen = sizeof (fd); if (recvmsg (sock, &msg, 0) < 0) diff --git a/lib/passfd.h b/lib/passfd.h --- a/lib/passfd.h +++ b/lib/passfd.h @@ -4,7 +4,7 @@ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or + the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, @@ -14,8 +14,19 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ + #ifndef PASSFD_H_ #define PASSFD_H_ 1 -int sendfd (int sock, int fd); -int recvfd (int sock); + +#ifdef __cplusplus +extern "C" { #endif + +extern int sendfd (int sock, int fd); +extern int recvfd (int sock); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/modules/passfd b/modules/passfd --- a/modules/passfd +++ b/modules/passfd @@ -2,13 +2,12 @@ Passfile descriptors along Unix (socket/stream) file descriptors Files: -m4/sockpfaf.m4 +lib/passfd.h +lib/passfd.c m4/afunix.m4 -lib/passfd.c -lib/passfd.h +m4/sockpfaf.m4 Depends-on: -errno sys_socket extensions @@ -19,8 +18,6 @@ lib_SOURCES += passfd.c Include: - - "passfd.h" License: