Bump to 1.14.1
[platform/upstream/augeas.git] / lib / passfd.c
1 /* Copyright (C) 2011-2016 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   msg.msg_controllen = cmsg->cmsg_len;
79 # elif HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
80   msg.msg_accrights = &fd;
81   msg.msg_accrightslen = sizeof fd;
82 # else
83   errno = ENOSYS;
84   return -1;
85 # endif
86
87   if (sendmsg (sock, &msg, 0) != iov.iov_len)
88     return -1;
89   return 0;
90 }
91 #else
92 int
93 sendfd (int sock _GL_UNUSED, int fd _GL_UNUSED)
94 {
95   errno = ENOSYS;
96   return -1;
97 }
98 #endif
99
100
101 #if HAVE_RECVMSG
102 /* recvfd receives a file descriptor through the socket.
103    The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>).
104
105    Return the fd on success, or -1 with errno set in case of error.
106 */
107 int
108 recvfd (int sock, int flags)
109 {
110   char byte = 0;
111   struct iovec iov;
112   struct msghdr msg;
113   int fd = -1;
114   ssize_t len;
115 # ifdef CMSG_FIRSTHDR
116   struct cmsghdr *cmsg;
117   char buf[CMSG_SPACE (sizeof fd)];
118   int flags_recvmsg = flags & O_CLOEXEC ? MSG_CMSG_CLOEXEC : 0;
119 # endif
120
121   if ((flags & ~O_CLOEXEC) != 0)
122     {
123       errno = EINVAL;
124       return -1;
125     }
126
127   /* send at least one char */
128   memset (&msg, 0, sizeof msg);
129   iov.iov_base = &byte;
130   iov.iov_len = 1;
131   msg.msg_iov = &iov;
132   msg.msg_iovlen = 1;
133   msg.msg_name = NULL;
134   msg.msg_namelen = 0;
135
136 # ifdef CMSG_FIRSTHDR
137   msg.msg_control = buf;
138   msg.msg_controllen = sizeof buf;
139   cmsg = CMSG_FIRSTHDR (&msg);
140   cmsg->cmsg_level = SOL_SOCKET;
141   cmsg->cmsg_type = SCM_RIGHTS;
142   cmsg->cmsg_len = CMSG_LEN (sizeof fd);
143   /* Initialize the payload: */
144   memcpy (CMSG_DATA (cmsg), &fd, sizeof fd);
145   msg.msg_controllen = cmsg->cmsg_len;
146
147   len = recvmsg (sock, &msg, flags_recvmsg);
148   if (len < 0)
149     return -1;
150
151   cmsg = CMSG_FIRSTHDR (&msg);
152   /* be paranoiac */
153   if (len == 0 || cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof fd)
154       || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
155     {
156       /* fake errno: at end the file is not available */
157       errno = len ? EACCES : ENOTCONN;
158       return -1;
159     }
160
161   memcpy (&fd, CMSG_DATA (cmsg), sizeof fd);
162
163   /* set close-on-exec flag */
164   if (!MSG_CMSG_CLOEXEC && (flags & O_CLOEXEC))
165     {
166       if (set_cloexec_flag (fd, true) < 0)
167         {
168           int saved_errno = errno;
169           (void) close (fd);
170           errno = saved_errno;
171           return -1;
172         }
173     }
174
175 # elif HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
176   msg.msg_accrights = &fd;
177   msg.msg_accrightslen = sizeof fd;
178   if (recvmsg (sock, &msg, 0) < 0)
179     return -1;
180
181   /* set close-on-exec flag */
182   if (flags & O_CLOEXEC)
183     {
184       if (set_cloexec_flag (fd, true) < 0)
185         {
186           int saved_errno = errno;
187           close (fd);
188           errno = saved_errno;
189           return -1;
190         }
191     }
192 # else
193   errno = ENOSYS;
194 # endif
195
196   return fd;
197 }
198 #else
199 int
200 recvfd (int sock _GL_UNUSED, int flags _GL_UNUSED)
201 {
202   errno = ENOSYS;
203   return -1;
204 }
205 #endif