passfd module, part 2.
[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 2 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 <errno.h>
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
28 #endif
29 #ifdef HAVE_SYS_UN_H
30 #include <sys/un.h>
31 #endif
32 #ifdef HAVE_WINSOCK2_H
33 #include <winsock2.h>
34 #endif
35
36 /* Sendfd sends the file descriptor fd along the socket
37    to a process calling recvfd on the other end.
38    
39    return -1 in case of error, 0 on success
40 */
41 int
42 sendfd (int sock, int fd)
43 {
44   char send = 0;
45   struct iovec iov[1];
46   struct msghdr msg;
47
48   /* send at least one char */
49   iov[0].iov_base = &send;
50   iov[0].iov_len = 1;
51   msg.msg_iov = iov;
52   msg.msg_iovlen = 1;
53   msg.msg_name = 0;
54   msg.msg_namelen = 0;
55
56   {
57 #ifdef HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
58     struct cmsghdr *cmsg;
59     char buf[CMSG_SPACE (sizeof (fd))];
60
61     msg.msg_control = buf;
62     msg.msg_controllen = sizeof (buf);
63     cmsg = CMSG_FIRSTHDR (&msg);
64     cmsg->cmsg_level = SOL_SOCKET;
65     cmsg->cmsg_type = SCM_RIGHTS;
66     cmsg->cmsg_len = CMSG_LEN (sizeof (int));
67     /* Initialize the payload: */
68     memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd));
69     msg.msg_controllen = cmsg->cmsg_len;
70 #elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY
71     msg.msg_accrights = &fd;
72     msg.msg_accrightslen = sizeof (fd);
73 #else
74     errno = ENOSYS;
75     return -1;
76 #endif
77   }
78
79   if (sendmsg (sock, &msg, 0) != iov[0].iov_len)
80     return -1;
81   return 0;
82 }
83
84 /* Sendfd sends the file descriptor fd along the socket 
85    to a process calling recvfd on the other end.
86
87    return -1 in case of error, fd on success
88 */
89 int
90 recvfd (int sock)
91 {
92   char recv = 0;
93   const int mone = -1;
94   int fd;
95   struct iovec iov[1];
96   struct msghdr msg;
97
98   /* send at least one char */
99   iov[0].iov_base = &recv;
100   iov[0].iov_len = 1;
101   msg.msg_iov = iov;
102   msg.msg_iovlen = 1;
103   msg.msg_name = 0;
104   msg.msg_namelen = 0;
105
106   {
107 #ifdef HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
108     struct cmsghdr *cmsg;
109     char buf[CMSG_SPACE (sizeof (fd))];
110
111     msg.msg_control = buf;
112     msg.msg_controllen = sizeof (buf);
113     cmsg = CMSG_FIRSTHDR (&msg);
114     cmsg->cmsg_level = SOL_SOCKET;
115     cmsg->cmsg_type = SCM_RIGHTS;
116     cmsg->cmsg_len = CMSG_LEN (sizeof (int));
117     /* Initialize the payload: */
118     memcpy (CMSG_DATA (cmsg), &mone, sizeof (mone));
119     msg.msg_controllen = cmsg->cmsg_len;
120
121     if (recvmsg (sock, &msg, 0) < 0)
122       return -1;
123
124     cmsg = CMSG_FIRSTHDR (&msg);
125     /* be paranoiac */
126     if (cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof (int))
127         || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
128       {
129         /* fake errno: at end the file is not available */
130         errno = EACCES;
131         return -1;
132       }
133
134     memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
135     return fd;
136 #elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY
137     msg.msg_accrights = &fd;
138     msg.msg_accrightslen = sizeof (fd);
139     if (recvmsg (sock, &msg, 0) < 0)
140       return -1;
141     return fd;
142 #else
143     errno = ENOSYS;
144     return -1;
145 #endif
146   }
147 }