passfd: standardize coding conventions
[gnulib.git] / lib / passfd.c
1 /* Copyright (C) 2011 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 <sys/uio.h>
28 #include <unistd.h>
29
30 #include <sys/socket.h>
31 #if HAVE_SYS_UN_H
32 # include <sys/un.h>
33 #endif
34
35 #include "cloexec.h"
36
37 #ifndef MSG_CMSG_CLOEXEC
38 # define MSG_CMSG_CLOEXEC 0
39 #endif
40
41 /* sendfd sends the file descriptor fd along the socket
42    to a process calling recvfd on the other end.
43
44    Return 0 on success, or -1 with errno set in case of error.
45 */
46 int
47 sendfd (int sock, int fd)
48 {
49   char send = 0;
50   struct iovec iov;
51   struct msghdr msg;
52
53   /* send at least one char */
54   memset (&msg, 0, sizeof msg);
55   iov.iov_base = &send;
56   iov.iov_len = 1;
57   msg.msg_iov = &iov;
58   msg.msg_iovlen = 1;
59   msg.msg_name = NULL;
60   msg.msg_namelen = 0;
61
62   {
63 #if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
64     struct cmsghdr *cmsg;
65     char buf[CMSG_SPACE (sizeof fd)];
66
67     msg.msg_control = buf;
68     msg.msg_controllen = sizeof buf;
69     cmsg = CMSG_FIRSTHDR (&msg);
70     cmsg->cmsg_level = SOL_SOCKET;
71     cmsg->cmsg_type = SCM_RIGHTS;
72     cmsg->cmsg_len = CMSG_LEN (sizeof fd);
73     /* Initialize the payload: */
74     memcpy (CMSG_DATA (cmsg), &fd, sizeof fd);
75 #elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY
76     msg.msg_accrights = &fd;
77     msg.msg_accrightslen = sizeof fd;
78 #else
79     errno = ENOSYS;
80     return -1;
81 #endif
82   }
83
84   if (sendmsg (sock, &msg, 0) != iov.iov_len)
85     return -1;
86   return 0;
87 }
88
89 /* recvfd receives a file descriptor through the socket.
90    The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>).
91
92    Return 0 on success, or -1 with errno set in case of error.
93 */
94 int
95 recvfd (int sock, int flags)
96 {
97   char recv = 0;
98   struct iovec iov;
99   struct msghdr msg;
100
101   if ((flags & ~O_CLOEXEC) != 0)
102     {
103       errno = EINVAL;
104       return -1;
105     }
106
107   /* send at least one char */
108   iov.iov_base = &recv;
109   iov.iov_len = 1;
110   msg.msg_iov = &iov;
111   msg.msg_iovlen = 1;
112   msg.msg_name = NULL;
113   msg.msg_namelen = 0;
114
115   {
116 #if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
117     int fd;
118     struct cmsghdr *cmsg;
119     char buf[CMSG_SPACE (sizeof fd)];
120     const int mone = -1;
121     int flags_recvmsg = flags & O_CLOEXEC ? MSG_CMSG_CLOEXEC : 0;
122
123     msg.msg_control = buf;
124     msg.msg_controllen = sizeof buf;
125     cmsg = CMSG_FIRSTHDR (&msg);
126     cmsg->cmsg_level = SOL_SOCKET;
127     cmsg->cmsg_type = SCM_RIGHTS;
128     cmsg->cmsg_len = CMSG_LEN (sizeof mone);
129     /* Initialize the payload: */
130     memcpy (CMSG_DATA (cmsg), &mone, sizeof mone);
131     msg.msg_controllen = cmsg->cmsg_len;
132
133     if (recvmsg (sock, &msg, flags_recvmsg) < 0)
134       return -1;
135
136     cmsg = CMSG_FIRSTHDR (&msg);
137     /* be paranoiac */
138     if (cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof fd)
139         || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
140       {
141         /* fake errno: at end the file is not available */
142         errno = EACCES;
143         return -1;
144       }
145
146     memcpy (&fd, CMSG_DATA (cmsg), sizeof fd);
147
148     /* set close-on-exec flag */
149     if (!MSG_CMSG_CLOEXEC && (flags & O_CLOEXEC))
150       {
151         if (set_cloexec_flag (fd, true) < 0)
152           {
153             int saved_errno = errno;
154             (void) close (fd);
155             errno = saved_errno;
156             return -1;
157           }
158       }
159
160     return fd;
161
162 #elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY
163     int fd;
164
165     msg.msg_accrights = &fd;
166     msg.msg_accrightslen = sizeof fd;
167     if (recvmsg (sock, &msg, 0) < 0)
168       return -1;
169
170     /* set close-on-exec flag */
171     if (flags & O_CLOEXEC)
172       {
173         if (set_cloexec_flag (fd, true) < 0)
174           {
175             int saved_errno = errno;
176             close (fd);
177             errno = saved_errno;
178             return -1;
179           }
180       }
181
182     return fd;
183
184 #else
185     errno = ENOSYS;
186     return -1;
187 #endif
188   }
189 }