passfd: give nicer error for recvfd at eof
[gnulib.git] / lib / passfd.c
1 /* Copyright (C) 2011-2013 Free Software Foundation, Inc.
2
3    This program is free software: you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; either version 3 of the License, or
6    (at your option) any later version.
7
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12
13    You should have received a copy of the GNU General Public License
14    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
15
16 #include <config.h>
17
18 /* Specification.  */
19 #include "passfd.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <sys/socket.h>
30
31 #include "cloexec.h"
32
33 /* The code that uses CMSG_FIRSTHDR is enabled on
34    Linux, Mac OS X, FreeBSD, OpenBSD, NetBSD, AIX, OSF/1, Cygwin.
35    The code that uses HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS is enabled on
36    HP-UX, IRIX, Solaris.  */
37
38 /* MSG_CMSG_CLOEXEC is defined only on Linux, as of 2011.  */
39 #ifndef MSG_CMSG_CLOEXEC
40 # define MSG_CMSG_CLOEXEC 0
41 #endif
42
43 #if HAVE_SENDMSG
44 /* sendfd sends the file descriptor fd along the socket
45    to a process calling recvfd on the other end.
46
47    Return 0 on success, or -1 with errno set in case of error.
48 */
49 int
50 sendfd (int sock, int fd)
51 {
52   char byte = 0;
53   struct iovec iov;
54   struct msghdr msg;
55 # ifdef CMSG_FIRSTHDR
56   struct cmsghdr *cmsg;
57   char buf[CMSG_SPACE (sizeof fd)];
58 # endif
59
60   /* send at least one char */
61   memset (&msg, 0, sizeof msg);
62   iov.iov_base = &byte;
63   iov.iov_len = 1;
64   msg.msg_iov = &iov;
65   msg.msg_iovlen = 1;
66   msg.msg_name = NULL;
67   msg.msg_namelen = 0;
68
69 # ifdef CMSG_FIRSTHDR
70   msg.msg_control = buf;
71   msg.msg_controllen = sizeof buf;
72   cmsg = CMSG_FIRSTHDR (&msg);
73   cmsg->cmsg_level = SOL_SOCKET;
74   cmsg->cmsg_type = SCM_RIGHTS;
75   cmsg->cmsg_len = CMSG_LEN (sizeof fd);
76   /* Initialize the payload: */
77   memcpy (CMSG_DATA (cmsg), &fd, sizeof fd);
78 # elif HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
79   msg.msg_accrights = &fd;
80   msg.msg_accrightslen = sizeof fd;
81 # else
82   errno = ENOSYS;
83   return -1;
84 # endif
85
86   if (sendmsg (sock, &msg, 0) != iov.iov_len)
87     return -1;
88   return 0;
89 }
90 #else
91 int
92 sendfd (int sock _GL_UNUSED, int fd _GL_UNUSED)
93 {
94   errno = ENOSYS;
95   return -1;
96 }
97 #endif
98
99
100 #if HAVE_RECVMSG
101 /* recvfd receives a file descriptor through the socket.
102    The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>).
103
104    Return the fd on success, or -1 with errno set in case of error.
105 */
106 int
107 recvfd (int sock, int flags)
108 {
109   char byte = 0;
110   struct iovec iov;
111   struct msghdr msg;
112   int fd = -1;
113   ssize_t len;
114 # ifdef CMSG_FIRSTHDR
115   struct cmsghdr *cmsg;
116   char buf[CMSG_SPACE (sizeof fd)];
117   int flags_recvmsg = flags & O_CLOEXEC ? MSG_CMSG_CLOEXEC : 0;
118 # endif
119
120   if ((flags & ~O_CLOEXEC) != 0)
121     {
122       errno = EINVAL;
123       return -1;
124     }
125
126   /* send at least one char */
127   memset (&msg, 0, sizeof msg);
128   iov.iov_base = &byte;
129   iov.iov_len = 1;
130   msg.msg_iov = &iov;
131   msg.msg_iovlen = 1;
132   msg.msg_name = NULL;
133   msg.msg_namelen = 0;
134
135 # ifdef CMSG_FIRSTHDR
136   msg.msg_control = buf;
137   msg.msg_controllen = sizeof buf;
138   cmsg = CMSG_FIRSTHDR (&msg);
139   cmsg->cmsg_level = SOL_SOCKET;
140   cmsg->cmsg_type = SCM_RIGHTS;
141   cmsg->cmsg_len = CMSG_LEN (sizeof fd);
142   /* Initialize the payload: */
143   memcpy (CMSG_DATA (cmsg), &fd, sizeof fd);
144   msg.msg_controllen = cmsg->cmsg_len;
145
146   len = recvmsg (sock, &msg, flags_recvmsg);
147   if (len < 0)
148     return -1;
149
150   cmsg = CMSG_FIRSTHDR (&msg);
151   /* be paranoiac */
152   if (len == 0 || cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof fd)
153       || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
154     {
155       /* fake errno: at end the file is not available */
156       errno = len ? EACCES : ENOTCONN;
157       return -1;
158     }
159
160   memcpy (&fd, CMSG_DATA (cmsg), sizeof fd);
161
162   /* set close-on-exec flag */
163   if (!MSG_CMSG_CLOEXEC && (flags & O_CLOEXEC))
164     {
165       if (set_cloexec_flag (fd, true) < 0)
166         {
167           int saved_errno = errno;
168           (void) close (fd);
169           errno = saved_errno;
170           return -1;
171         }
172     }
173
174 # elif HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
175   msg.msg_accrights = &fd;
176   msg.msg_accrightslen = sizeof fd;
177   if (recvmsg (sock, &msg, 0) < 0)
178     return -1;
179
180   /* set close-on-exec flag */
181   if (flags & O_CLOEXEC)
182     {
183       if (set_cloexec_flag (fd, true) < 0)
184         {
185           int saved_errno = errno;
186           close (fd);
187           errno = saved_errno;
188           return -1;
189         }
190     }
191 # else
192   errno = ENOSYS;
193 # endif
194
195   return fd;
196 }
197 #else
198 int
199 recvfd (int sock _GL_UNUSED, int flags _GL_UNUSED)
200 {
201   errno = ENOSYS;
202   return -1;
203 }
204 #endif