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