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