msgio.c revision 1.2.2.2 1 1.2.2.2 pgoyette /* $NetBSD: msgio.c,v 1.2.2.2 2018/09/30 01:45:55 pgoyette Exp $ */
2 1.2.2.2 pgoyette
3 1.2.2.2 pgoyette /*-
4 1.2.2.2 pgoyette * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 1.2.2.2 pgoyette *
6 1.2.2.2 pgoyette * Copyright (c) 2013 The FreeBSD Foundation
7 1.2.2.2 pgoyette * Copyright (c) 2013 Mariusz Zaborski <oshogbo (at) FreeBSD.org>
8 1.2.2.2 pgoyette * All rights reserved.
9 1.2.2.2 pgoyette *
10 1.2.2.2 pgoyette * This software was developed by Pawel Jakub Dawidek under sponsorship from
11 1.2.2.2 pgoyette * the FreeBSD Foundation.
12 1.2.2.2 pgoyette *
13 1.2.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
14 1.2.2.2 pgoyette * modification, are permitted provided that the following conditions
15 1.2.2.2 pgoyette * are met:
16 1.2.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
17 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
18 1.2.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
19 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
20 1.2.2.2 pgoyette * documentation and/or other materials provided with the distribution.
21 1.2.2.2 pgoyette *
22 1.2.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
23 1.2.2.2 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.2.2.2 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.2.2.2 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
26 1.2.2.2 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.2.2.2 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.2.2.2 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.2.2.2 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.2.2.2 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.2.2.2 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.2.2.2 pgoyette * SUCH DAMAGE.
33 1.2.2.2 pgoyette */
34 1.2.2.2 pgoyette
35 1.2.2.2 pgoyette #include <sys/cdefs.h>
36 1.2.2.2 pgoyette #ifdef __FreeBSD__
37 1.2.2.2 pgoyette __FBSDID("$FreeBSD: head/lib/libnv/msgio.c 326219 2017-11-26 02:00:33Z pfg $");
38 1.2.2.2 pgoyette #else
39 1.2.2.2 pgoyette __RCSID("$NetBSD: msgio.c,v 1.2.2.2 2018/09/30 01:45:55 pgoyette Exp $");
40 1.2.2.2 pgoyette #endif
41 1.2.2.2 pgoyette
42 1.2.2.2 pgoyette #include <sys/param.h>
43 1.2.2.2 pgoyette #include <sys/socket.h>
44 1.2.2.2 pgoyette #include <sys/select.h>
45 1.2.2.2 pgoyette
46 1.2.2.2 pgoyette #include <errno.h>
47 1.2.2.2 pgoyette #include <fcntl.h>
48 1.2.2.2 pgoyette #include <stdbool.h>
49 1.2.2.2 pgoyette #include <stdint.h>
50 1.2.2.2 pgoyette #include <stdlib.h>
51 1.2.2.2 pgoyette #include <string.h>
52 1.2.2.2 pgoyette #include <unistd.h>
53 1.2.2.2 pgoyette
54 1.2.2.2 pgoyette #ifdef HAVE_PJDLOG
55 1.2.2.2 pgoyette #include <pjdlog.h>
56 1.2.2.2 pgoyette #endif
57 1.2.2.2 pgoyette
58 1.2.2.2 pgoyette #include "common_impl.h"
59 1.2.2.2 pgoyette #include "msgio.h"
60 1.2.2.2 pgoyette
61 1.2.2.2 pgoyette #ifndef HAVE_PJDLOG
62 1.2.2.2 pgoyette #include <assert.h>
63 1.2.2.2 pgoyette #define PJDLOG_ASSERT(...) assert(__VA_ARGS__)
64 1.2.2.2 pgoyette #define PJDLOG_RASSERT(expr, ...) assert(expr)
65 1.2.2.2 pgoyette #define PJDLOG_ABORT(...) abort()
66 1.2.2.2 pgoyette #endif
67 1.2.2.2 pgoyette
68 1.2.2.2 pgoyette #ifdef __linux__
69 1.2.2.2 pgoyette /* Linux: arbitrary size, but must be lower than SCM_MAX_FD. */
70 1.2.2.2 pgoyette #define PKG_MAX_SIZE ((64U - 1) * CMSG_SPACE(sizeof(int)))
71 1.2.2.2 pgoyette #else
72 1.2.2.2 pgoyette #define PKG_MAX_SIZE (MCLBYTES / CMSG_SPACE(sizeof(int)) - 1)
73 1.2.2.2 pgoyette #endif
74 1.2.2.2 pgoyette
75 1.2.2.2 pgoyette static int
76 1.2.2.2 pgoyette msghdr_add_fd(struct cmsghdr *cmsg, int fd)
77 1.2.2.2 pgoyette {
78 1.2.2.2 pgoyette
79 1.2.2.2 pgoyette PJDLOG_ASSERT(fd >= 0);
80 1.2.2.2 pgoyette
81 1.2.2.2 pgoyette if (!fd_is_valid(fd)) {
82 1.2.2.2 pgoyette errno = EBADF;
83 1.2.2.2 pgoyette return (-1);
84 1.2.2.2 pgoyette }
85 1.2.2.2 pgoyette
86 1.2.2.2 pgoyette cmsg->cmsg_level = SOL_SOCKET;
87 1.2.2.2 pgoyette cmsg->cmsg_type = SCM_RIGHTS;
88 1.2.2.2 pgoyette cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
89 1.2.2.2 pgoyette bcopy(&fd, CMSG_DATA(cmsg), sizeof(fd));
90 1.2.2.2 pgoyette
91 1.2.2.2 pgoyette return (0);
92 1.2.2.2 pgoyette }
93 1.2.2.2 pgoyette
94 1.2.2.2 pgoyette static int
95 1.2.2.2 pgoyette msghdr_get_fd(struct cmsghdr *cmsg)
96 1.2.2.2 pgoyette {
97 1.2.2.2 pgoyette int fd;
98 1.2.2.2 pgoyette
99 1.2.2.2 pgoyette if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET ||
100 1.2.2.2 pgoyette cmsg->cmsg_type != SCM_RIGHTS ||
101 1.2.2.2 pgoyette cmsg->cmsg_len != CMSG_LEN(sizeof(fd))) {
102 1.2.2.2 pgoyette errno = EINVAL;
103 1.2.2.2 pgoyette return (-1);
104 1.2.2.2 pgoyette }
105 1.2.2.2 pgoyette
106 1.2.2.2 pgoyette bcopy(CMSG_DATA(cmsg), &fd, sizeof(fd));
107 1.2.2.2 pgoyette #ifndef MSG_CMSG_CLOEXEC
108 1.2.2.2 pgoyette /*
109 1.2.2.2 pgoyette * If the MSG_CMSG_CLOEXEC flag is not available we cannot set the
110 1.2.2.2 pgoyette * close-on-exec flag atomically, but we still want to set it for
111 1.2.2.2 pgoyette * consistency.
112 1.2.2.2 pgoyette */
113 1.2.2.2 pgoyette (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
114 1.2.2.2 pgoyette #endif
115 1.2.2.2 pgoyette
116 1.2.2.2 pgoyette return (fd);
117 1.2.2.2 pgoyette }
118 1.2.2.2 pgoyette
119 1.2.2.2 pgoyette static void
120 1.2.2.2 pgoyette fd_wait(int fd, bool doread)
121 1.2.2.2 pgoyette {
122 1.2.2.2 pgoyette fd_set fds;
123 1.2.2.2 pgoyette
124 1.2.2.2 pgoyette PJDLOG_ASSERT(fd >= 0);
125 1.2.2.2 pgoyette
126 1.2.2.2 pgoyette FD_ZERO(&fds);
127 1.2.2.2 pgoyette FD_SET(fd, &fds);
128 1.2.2.2 pgoyette (void)select(fd + 1, doread ? &fds : NULL, doread ? NULL : &fds,
129 1.2.2.2 pgoyette NULL, NULL);
130 1.2.2.2 pgoyette }
131 1.2.2.2 pgoyette
132 1.2.2.2 pgoyette static int
133 1.2.2.2 pgoyette msg_recv(int sock, struct msghdr *msg)
134 1.2.2.2 pgoyette {
135 1.2.2.2 pgoyette int flags;
136 1.2.2.2 pgoyette
137 1.2.2.2 pgoyette PJDLOG_ASSERT(sock >= 0);
138 1.2.2.2 pgoyette
139 1.2.2.2 pgoyette #ifdef MSG_CMSG_CLOEXEC
140 1.2.2.2 pgoyette flags = MSG_CMSG_CLOEXEC;
141 1.2.2.2 pgoyette #else
142 1.2.2.2 pgoyette flags = 0;
143 1.2.2.2 pgoyette #endif
144 1.2.2.2 pgoyette
145 1.2.2.2 pgoyette for (;;) {
146 1.2.2.2 pgoyette fd_wait(sock, true);
147 1.2.2.2 pgoyette if (recvmsg(sock, msg, flags) == -1) {
148 1.2.2.2 pgoyette if (errno == EINTR)
149 1.2.2.2 pgoyette continue;
150 1.2.2.2 pgoyette return (-1);
151 1.2.2.2 pgoyette }
152 1.2.2.2 pgoyette break;
153 1.2.2.2 pgoyette }
154 1.2.2.2 pgoyette
155 1.2.2.2 pgoyette return (0);
156 1.2.2.2 pgoyette }
157 1.2.2.2 pgoyette
158 1.2.2.2 pgoyette static int
159 1.2.2.2 pgoyette msg_send(int sock, const struct msghdr *msg)
160 1.2.2.2 pgoyette {
161 1.2.2.2 pgoyette
162 1.2.2.2 pgoyette PJDLOG_ASSERT(sock >= 0);
163 1.2.2.2 pgoyette
164 1.2.2.2 pgoyette for (;;) {
165 1.2.2.2 pgoyette fd_wait(sock, false);
166 1.2.2.2 pgoyette if (sendmsg(sock, msg, 0) == -1) {
167 1.2.2.2 pgoyette if (errno == EINTR)
168 1.2.2.2 pgoyette continue;
169 1.2.2.2 pgoyette return (-1);
170 1.2.2.2 pgoyette }
171 1.2.2.2 pgoyette break;
172 1.2.2.2 pgoyette }
173 1.2.2.2 pgoyette
174 1.2.2.2 pgoyette return (0);
175 1.2.2.2 pgoyette }
176 1.2.2.2 pgoyette
177 1.2.2.2 pgoyette #ifdef __FreeBSD__
178 1.2.2.2 pgoyette int
179 1.2.2.2 pgoyette cred_send(int sock)
180 1.2.2.2 pgoyette {
181 1.2.2.2 pgoyette unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
182 1.2.2.2 pgoyette struct msghdr msg;
183 1.2.2.2 pgoyette struct cmsghdr *cmsg;
184 1.2.2.2 pgoyette struct iovec iov;
185 1.2.2.2 pgoyette uint8_t dummy;
186 1.2.2.2 pgoyette
187 1.2.2.2 pgoyette bzero(credbuf, sizeof(credbuf));
188 1.2.2.2 pgoyette bzero(&msg, sizeof(msg));
189 1.2.2.2 pgoyette bzero(&iov, sizeof(iov));
190 1.2.2.2 pgoyette
191 1.2.2.2 pgoyette /*
192 1.2.2.2 pgoyette * XXX: We send one byte along with the control message, because
193 1.2.2.2 pgoyette * setting msg_iov to NULL only works if this is the first
194 1.2.2.2 pgoyette * packet send over the socket. Once we send some data we
195 1.2.2.2 pgoyette * won't be able to send credentials anymore. This is most
196 1.2.2.2 pgoyette * likely a kernel bug.
197 1.2.2.2 pgoyette */
198 1.2.2.2 pgoyette dummy = 0;
199 1.2.2.2 pgoyette iov.iov_base = &dummy;
200 1.2.2.2 pgoyette iov.iov_len = sizeof(dummy);
201 1.2.2.2 pgoyette
202 1.2.2.2 pgoyette msg.msg_iov = &iov;
203 1.2.2.2 pgoyette msg.msg_iovlen = 1;
204 1.2.2.2 pgoyette msg.msg_control = credbuf;
205 1.2.2.2 pgoyette msg.msg_controllen = sizeof(credbuf);
206 1.2.2.2 pgoyette
207 1.2.2.2 pgoyette cmsg = CMSG_FIRSTHDR(&msg);
208 1.2.2.2 pgoyette cmsg->cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
209 1.2.2.2 pgoyette cmsg->cmsg_level = SOL_SOCKET;
210 1.2.2.2 pgoyette cmsg->cmsg_type = SCM_CREDS;
211 1.2.2.2 pgoyette
212 1.2.2.2 pgoyette if (msg_send(sock, &msg) == -1)
213 1.2.2.2 pgoyette return (-1);
214 1.2.2.2 pgoyette
215 1.2.2.2 pgoyette return (0);
216 1.2.2.2 pgoyette }
217 1.2.2.2 pgoyette
218 1.2.2.2 pgoyette int
219 1.2.2.2 pgoyette cred_recv(int sock, struct cmsgcred *cred)
220 1.2.2.2 pgoyette {
221 1.2.2.2 pgoyette unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
222 1.2.2.2 pgoyette struct msghdr msg;
223 1.2.2.2 pgoyette struct cmsghdr *cmsg;
224 1.2.2.2 pgoyette struct iovec iov;
225 1.2.2.2 pgoyette uint8_t dummy;
226 1.2.2.2 pgoyette
227 1.2.2.2 pgoyette bzero(credbuf, sizeof(credbuf));
228 1.2.2.2 pgoyette bzero(&msg, sizeof(msg));
229 1.2.2.2 pgoyette bzero(&iov, sizeof(iov));
230 1.2.2.2 pgoyette
231 1.2.2.2 pgoyette iov.iov_base = &dummy;
232 1.2.2.2 pgoyette iov.iov_len = sizeof(dummy);
233 1.2.2.2 pgoyette
234 1.2.2.2 pgoyette msg.msg_iov = &iov;
235 1.2.2.2 pgoyette msg.msg_iovlen = 1;
236 1.2.2.2 pgoyette msg.msg_control = credbuf;
237 1.2.2.2 pgoyette msg.msg_controllen = sizeof(credbuf);
238 1.2.2.2 pgoyette
239 1.2.2.2 pgoyette if (msg_recv(sock, &msg) == -1)
240 1.2.2.2 pgoyette return (-1);
241 1.2.2.2 pgoyette
242 1.2.2.2 pgoyette cmsg = CMSG_FIRSTHDR(&msg);
243 1.2.2.2 pgoyette if (cmsg == NULL ||
244 1.2.2.2 pgoyette cmsg->cmsg_len != CMSG_LEN(sizeof(struct cmsgcred)) ||
245 1.2.2.2 pgoyette cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_CREDS) {
246 1.2.2.2 pgoyette errno = EINVAL;
247 1.2.2.2 pgoyette return (-1);
248 1.2.2.2 pgoyette }
249 1.2.2.2 pgoyette bcopy(CMSG_DATA(cmsg), cred, sizeof(*cred));
250 1.2.2.2 pgoyette
251 1.2.2.2 pgoyette return (0);
252 1.2.2.2 pgoyette }
253 1.2.2.2 pgoyette #endif
254 1.2.2.2 pgoyette
255 1.2.2.2 pgoyette static int
256 1.2.2.2 pgoyette fd_package_send(int sock, const int *fds, size_t nfds)
257 1.2.2.2 pgoyette {
258 1.2.2.2 pgoyette struct msghdr msg;
259 1.2.2.2 pgoyette struct cmsghdr *cmsg;
260 1.2.2.2 pgoyette struct iovec iov;
261 1.2.2.2 pgoyette unsigned int i;
262 1.2.2.2 pgoyette int serrno, ret;
263 1.2.2.2 pgoyette uint8_t dummy;
264 1.2.2.2 pgoyette
265 1.2.2.2 pgoyette PJDLOG_ASSERT(sock >= 0);
266 1.2.2.2 pgoyette PJDLOG_ASSERT(fds != NULL);
267 1.2.2.2 pgoyette PJDLOG_ASSERT(nfds > 0);
268 1.2.2.2 pgoyette
269 1.2.2.2 pgoyette bzero(&msg, sizeof(msg));
270 1.2.2.2 pgoyette
271 1.2.2.2 pgoyette /*
272 1.2.2.2 pgoyette * XXX: Look into cred_send function for more details.
273 1.2.2.2 pgoyette */
274 1.2.2.2 pgoyette dummy = 0;
275 1.2.2.2 pgoyette iov.iov_base = &dummy;
276 1.2.2.2 pgoyette iov.iov_len = sizeof(dummy);
277 1.2.2.2 pgoyette
278 1.2.2.2 pgoyette msg.msg_iov = &iov;
279 1.2.2.2 pgoyette msg.msg_iovlen = 1;
280 1.2.2.2 pgoyette msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
281 1.2.2.2 pgoyette msg.msg_control = calloc(1, msg.msg_controllen);
282 1.2.2.2 pgoyette if (msg.msg_control == NULL)
283 1.2.2.2 pgoyette return (-1);
284 1.2.2.2 pgoyette
285 1.2.2.2 pgoyette ret = -1;
286 1.2.2.2 pgoyette
287 1.2.2.2 pgoyette for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
288 1.2.2.2 pgoyette i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
289 1.2.2.2 pgoyette if (msghdr_add_fd(cmsg, fds[i]) == -1)
290 1.2.2.2 pgoyette goto end;
291 1.2.2.2 pgoyette }
292 1.2.2.2 pgoyette
293 1.2.2.2 pgoyette if (msg_send(sock, &msg) == -1)
294 1.2.2.2 pgoyette goto end;
295 1.2.2.2 pgoyette
296 1.2.2.2 pgoyette ret = 0;
297 1.2.2.2 pgoyette end:
298 1.2.2.2 pgoyette serrno = errno;
299 1.2.2.2 pgoyette free(msg.msg_control);
300 1.2.2.2 pgoyette errno = serrno;
301 1.2.2.2 pgoyette return (ret);
302 1.2.2.2 pgoyette }
303 1.2.2.2 pgoyette
304 1.2.2.2 pgoyette static int
305 1.2.2.2 pgoyette fd_package_recv(int sock, int *fds, size_t nfds)
306 1.2.2.2 pgoyette {
307 1.2.2.2 pgoyette struct msghdr msg;
308 1.2.2.2 pgoyette struct cmsghdr *cmsg;
309 1.2.2.2 pgoyette unsigned int i;
310 1.2.2.2 pgoyette int serrno, ret;
311 1.2.2.2 pgoyette struct iovec iov;
312 1.2.2.2 pgoyette uint8_t dummy;
313 1.2.2.2 pgoyette
314 1.2.2.2 pgoyette PJDLOG_ASSERT(sock >= 0);
315 1.2.2.2 pgoyette PJDLOG_ASSERT(nfds > 0);
316 1.2.2.2 pgoyette PJDLOG_ASSERT(fds != NULL);
317 1.2.2.2 pgoyette
318 1.2.2.2 pgoyette bzero(&msg, sizeof(msg));
319 1.2.2.2 pgoyette bzero(&iov, sizeof(iov));
320 1.2.2.2 pgoyette
321 1.2.2.2 pgoyette /*
322 1.2.2.2 pgoyette * XXX: Look into cred_send function for more details.
323 1.2.2.2 pgoyette */
324 1.2.2.2 pgoyette iov.iov_base = &dummy;
325 1.2.2.2 pgoyette iov.iov_len = sizeof(dummy);
326 1.2.2.2 pgoyette
327 1.2.2.2 pgoyette msg.msg_iov = &iov;
328 1.2.2.2 pgoyette msg.msg_iovlen = 1;
329 1.2.2.2 pgoyette msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
330 1.2.2.2 pgoyette msg.msg_control = calloc(1, msg.msg_controllen);
331 1.2.2.2 pgoyette if (msg.msg_control == NULL)
332 1.2.2.2 pgoyette return (-1);
333 1.2.2.2 pgoyette
334 1.2.2.2 pgoyette ret = -1;
335 1.2.2.2 pgoyette
336 1.2.2.2 pgoyette if (msg_recv(sock, &msg) == -1)
337 1.2.2.2 pgoyette goto end;
338 1.2.2.2 pgoyette
339 1.2.2.2 pgoyette for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
340 1.2.2.2 pgoyette i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
341 1.2.2.2 pgoyette fds[i] = msghdr_get_fd(cmsg);
342 1.2.2.2 pgoyette if (fds[i] < 0)
343 1.2.2.2 pgoyette break;
344 1.2.2.2 pgoyette }
345 1.2.2.2 pgoyette
346 1.2.2.2 pgoyette if (cmsg != NULL || i < nfds) {
347 1.2.2.2 pgoyette int fd;
348 1.2.2.2 pgoyette
349 1.2.2.2 pgoyette /*
350 1.2.2.2 pgoyette * We need to close all received descriptors, even if we have
351 1.2.2.2 pgoyette * different control message (eg. SCM_CREDS) in between.
352 1.2.2.2 pgoyette */
353 1.2.2.2 pgoyette for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
354 1.2.2.2 pgoyette cmsg = CMSG_NXTHDR(&msg, cmsg)) {
355 1.2.2.2 pgoyette fd = msghdr_get_fd(cmsg);
356 1.2.2.2 pgoyette if (fd >= 0)
357 1.2.2.2 pgoyette close(fd);
358 1.2.2.2 pgoyette }
359 1.2.2.2 pgoyette errno = EINVAL;
360 1.2.2.2 pgoyette goto end;
361 1.2.2.2 pgoyette }
362 1.2.2.2 pgoyette
363 1.2.2.2 pgoyette ret = 0;
364 1.2.2.2 pgoyette end:
365 1.2.2.2 pgoyette serrno = errno;
366 1.2.2.2 pgoyette free(msg.msg_control);
367 1.2.2.2 pgoyette errno = serrno;
368 1.2.2.2 pgoyette return (ret);
369 1.2.2.2 pgoyette }
370 1.2.2.2 pgoyette
371 1.2.2.2 pgoyette int
372 1.2.2.2 pgoyette fd_recv(int sock, int *fds, size_t nfds)
373 1.2.2.2 pgoyette {
374 1.2.2.2 pgoyette unsigned int i, step, j;
375 1.2.2.2 pgoyette int ret, serrno;
376 1.2.2.2 pgoyette
377 1.2.2.2 pgoyette if (nfds == 0 || fds == NULL) {
378 1.2.2.2 pgoyette errno = EINVAL;
379 1.2.2.2 pgoyette return (-1);
380 1.2.2.2 pgoyette }
381 1.2.2.2 pgoyette
382 1.2.2.2 pgoyette ret = i = step = 0;
383 1.2.2.2 pgoyette while (i < nfds) {
384 1.2.2.2 pgoyette if (PKG_MAX_SIZE < nfds - i)
385 1.2.2.2 pgoyette step = PKG_MAX_SIZE;
386 1.2.2.2 pgoyette else
387 1.2.2.2 pgoyette step = nfds - i;
388 1.2.2.2 pgoyette ret = fd_package_recv(sock, fds + i, step);
389 1.2.2.2 pgoyette if (ret != 0) {
390 1.2.2.2 pgoyette /* Close all received descriptors. */
391 1.2.2.2 pgoyette serrno = errno;
392 1.2.2.2 pgoyette for (j = 0; j < i; j++)
393 1.2.2.2 pgoyette close(fds[j]);
394 1.2.2.2 pgoyette errno = serrno;
395 1.2.2.2 pgoyette break;
396 1.2.2.2 pgoyette }
397 1.2.2.2 pgoyette i += step;
398 1.2.2.2 pgoyette }
399 1.2.2.2 pgoyette
400 1.2.2.2 pgoyette return (ret);
401 1.2.2.2 pgoyette }
402 1.2.2.2 pgoyette
403 1.2.2.2 pgoyette int
404 1.2.2.2 pgoyette fd_send(int sock, const int *fds, size_t nfds)
405 1.2.2.2 pgoyette {
406 1.2.2.2 pgoyette unsigned int i, step;
407 1.2.2.2 pgoyette int ret;
408 1.2.2.2 pgoyette
409 1.2.2.2 pgoyette if (nfds == 0 || fds == NULL) {
410 1.2.2.2 pgoyette errno = EINVAL;
411 1.2.2.2 pgoyette return (-1);
412 1.2.2.2 pgoyette }
413 1.2.2.2 pgoyette
414 1.2.2.2 pgoyette ret = i = step = 0;
415 1.2.2.2 pgoyette while (i < nfds) {
416 1.2.2.2 pgoyette if (PKG_MAX_SIZE < nfds - i)
417 1.2.2.2 pgoyette step = PKG_MAX_SIZE;
418 1.2.2.2 pgoyette else
419 1.2.2.2 pgoyette step = nfds - i;
420 1.2.2.2 pgoyette ret = fd_package_send(sock, fds + i, step);
421 1.2.2.2 pgoyette if (ret != 0)
422 1.2.2.2 pgoyette break;
423 1.2.2.2 pgoyette i += step;
424 1.2.2.2 pgoyette }
425 1.2.2.2 pgoyette
426 1.2.2.2 pgoyette return (ret);
427 1.2.2.2 pgoyette }
428 1.2.2.2 pgoyette
429 1.2.2.2 pgoyette int
430 1.2.2.2 pgoyette buf_send(int sock, void *buf, size_t size)
431 1.2.2.2 pgoyette {
432 1.2.2.2 pgoyette ssize_t done;
433 1.2.2.2 pgoyette unsigned char *ptr;
434 1.2.2.2 pgoyette
435 1.2.2.2 pgoyette PJDLOG_ASSERT(sock >= 0);
436 1.2.2.2 pgoyette PJDLOG_ASSERT(size > 0);
437 1.2.2.2 pgoyette PJDLOG_ASSERT(buf != NULL);
438 1.2.2.2 pgoyette
439 1.2.2.2 pgoyette ptr = buf;
440 1.2.2.2 pgoyette do {
441 1.2.2.2 pgoyette fd_wait(sock, false);
442 1.2.2.2 pgoyette done = send(sock, ptr, size, 0);
443 1.2.2.2 pgoyette if (done == -1) {
444 1.2.2.2 pgoyette if (errno == EINTR)
445 1.2.2.2 pgoyette continue;
446 1.2.2.2 pgoyette return (-1);
447 1.2.2.2 pgoyette } else if (done == 0) {
448 1.2.2.2 pgoyette errno = ENOTCONN;
449 1.2.2.2 pgoyette return (-1);
450 1.2.2.2 pgoyette }
451 1.2.2.2 pgoyette size -= done;
452 1.2.2.2 pgoyette ptr += done;
453 1.2.2.2 pgoyette } while (size > 0);
454 1.2.2.2 pgoyette
455 1.2.2.2 pgoyette return (0);
456 1.2.2.2 pgoyette }
457 1.2.2.2 pgoyette
458 1.2.2.2 pgoyette int
459 1.2.2.2 pgoyette buf_recv(int sock, void *buf, size_t size)
460 1.2.2.2 pgoyette {
461 1.2.2.2 pgoyette ssize_t done;
462 1.2.2.2 pgoyette unsigned char *ptr;
463 1.2.2.2 pgoyette
464 1.2.2.2 pgoyette PJDLOG_ASSERT(sock >= 0);
465 1.2.2.2 pgoyette PJDLOG_ASSERT(buf != NULL);
466 1.2.2.2 pgoyette
467 1.2.2.2 pgoyette ptr = buf;
468 1.2.2.2 pgoyette while (size > 0) {
469 1.2.2.2 pgoyette fd_wait(sock, true);
470 1.2.2.2 pgoyette done = recv(sock, ptr, size, 0);
471 1.2.2.2 pgoyette if (done == -1) {
472 1.2.2.2 pgoyette if (errno == EINTR)
473 1.2.2.2 pgoyette continue;
474 1.2.2.2 pgoyette return (-1);
475 1.2.2.2 pgoyette } else if (done == 0) {
476 1.2.2.2 pgoyette errno = ENOTCONN;
477 1.2.2.2 pgoyette return (-1);
478 1.2.2.2 pgoyette }
479 1.2.2.2 pgoyette size -= done;
480 1.2.2.2 pgoyette ptr += done;
481 1.2.2.2 pgoyette }
482 1.2.2.2 pgoyette
483 1.2.2.2 pgoyette return (0);
484 1.2.2.2 pgoyette }
485