2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 The FreeBSD Foundation
5 * Copyright (c) 2013 Mariusz Zaborski <oshogbo@FreeBSD.org>
8 * This software was developed by Pawel Jakub Dawidek under sponsorship from
9 * the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/socket.h>
51 #include "common_impl.h"
56 #define PJDLOG_ASSERT(...) assert(__VA_ARGS__)
57 #define PJDLOG_RASSERT(expr, ...) assert(expr)
58 #define PJDLOG_ABORT(...) abort()
61 #define PKG_MAX_SIZE (MCLBYTES / CMSG_SPACE(sizeof(int)) - 1)
64 msghdr_add_fd(struct cmsghdr *cmsg, int fd)
67 PJDLOG_ASSERT(fd >= 0);
69 cmsg->cmsg_level = SOL_SOCKET;
70 cmsg->cmsg_type = SCM_RIGHTS;
71 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
72 bcopy(&fd, CMSG_DATA(cmsg), sizeof(fd));
78 msghdr_get_fd(struct cmsghdr *cmsg)
82 if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET ||
83 cmsg->cmsg_type != SCM_RIGHTS ||
84 cmsg->cmsg_len != CMSG_LEN(sizeof(fd))) {
89 bcopy(CMSG_DATA(cmsg), &fd, sizeof(fd));
90 #ifndef MSG_CMSG_CLOEXEC
92 * If the MSG_CMSG_CLOEXEC flag is not available we cannot set the
93 * close-on-exec flag atomically, but we still want to set it for
96 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
103 fd_wait(int fd, bool doread)
107 PJDLOG_ASSERT(fd >= 0);
111 (void)select(fd + 1, doread ? &fds : NULL, doread ? NULL : &fds,
116 msg_recv(int sock, struct msghdr *msg)
120 PJDLOG_ASSERT(sock >= 0);
122 #ifdef MSG_CMSG_CLOEXEC
123 flags = MSG_CMSG_CLOEXEC;
130 if (recvmsg(sock, msg, flags) == -1) {
142 msg_send(int sock, const struct msghdr *msg)
145 PJDLOG_ASSERT(sock >= 0);
148 fd_wait(sock, false);
149 if (sendmsg(sock, msg, 0) == -1) {
161 * MacOS/Linux do not define struct cmsgcred but we need to bootstrap libnv
162 * when building on non-FreeBSD systems. Since they are not used during
163 * bootstrap we can just omit these two functions there.
166 #warning "cred_send() not supported on non-FreeBSD systems"
171 unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
173 struct cmsghdr *cmsg;
177 bzero(credbuf, sizeof(credbuf));
178 bzero(&msg, sizeof(msg));
179 bzero(&iov, sizeof(iov));
182 * XXX: We send one byte along with the control message, because
183 * setting msg_iov to NULL only works if this is the first
184 * packet send over the socket. Once we send some data we
185 * won't be able to send credentials anymore. This is most
186 * likely a kernel bug.
189 iov.iov_base = &dummy;
190 iov.iov_len = sizeof(dummy);
194 msg.msg_control = credbuf;
195 msg.msg_controllen = sizeof(credbuf);
197 cmsg = CMSG_FIRSTHDR(&msg);
198 cmsg->cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
199 cmsg->cmsg_level = SOL_SOCKET;
200 cmsg->cmsg_type = SCM_CREDS;
202 if (msg_send(sock, &msg) == -1)
209 cred_recv(int sock, struct cmsgcred *cred)
211 unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
213 struct cmsghdr *cmsg;
217 bzero(credbuf, sizeof(credbuf));
218 bzero(&msg, sizeof(msg));
219 bzero(&iov, sizeof(iov));
221 iov.iov_base = &dummy;
222 iov.iov_len = sizeof(dummy);
226 msg.msg_control = credbuf;
227 msg.msg_controllen = sizeof(credbuf);
229 if (msg_recv(sock, &msg) == -1)
232 cmsg = CMSG_FIRSTHDR(&msg);
234 cmsg->cmsg_len != CMSG_LEN(sizeof(struct cmsgcred)) ||
235 cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_CREDS) {
239 bcopy(CMSG_DATA(cmsg), cred, sizeof(*cred));
246 fd_package_send(int sock, const int *fds, size_t nfds)
249 struct cmsghdr *cmsg;
255 PJDLOG_ASSERT(sock >= 0);
256 PJDLOG_ASSERT(fds != NULL);
257 PJDLOG_ASSERT(nfds > 0);
259 bzero(&msg, sizeof(msg));
262 * XXX: Look into cred_send function for more details.
265 iov.iov_base = &dummy;
266 iov.iov_len = sizeof(dummy);
270 msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
271 msg.msg_control = calloc(1, msg.msg_controllen);
272 if (msg.msg_control == NULL)
277 for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
278 i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
279 if (msghdr_add_fd(cmsg, fds[i]) == -1)
283 if (msg_send(sock, &msg) == -1)
289 free(msg.msg_control);
295 fd_package_recv(int sock, int *fds, size_t nfds)
298 struct cmsghdr *cmsg;
304 PJDLOG_ASSERT(sock >= 0);
305 PJDLOG_ASSERT(nfds > 0);
306 PJDLOG_ASSERT(fds != NULL);
308 bzero(&msg, sizeof(msg));
309 bzero(&iov, sizeof(iov));
312 * XXX: Look into cred_send function for more details.
314 iov.iov_base = &dummy;
315 iov.iov_len = sizeof(dummy);
319 msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
320 msg.msg_control = calloc(1, msg.msg_controllen);
321 if (msg.msg_control == NULL)
326 if (msg_recv(sock, &msg) == -1)
329 for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
330 i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
331 fds[i] = msghdr_get_fd(cmsg);
336 if (cmsg != NULL || i < nfds) {
340 * We need to close all received descriptors, even if we have
341 * different control message (eg. SCM_CREDS) in between.
343 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
344 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
345 fd = msghdr_get_fd(cmsg);
356 free(msg.msg_control);
362 fd_recv(int sock, int *fds, size_t nfds)
364 unsigned int i, step, j;
367 if (nfds == 0 || fds == NULL) {
374 if (PKG_MAX_SIZE < nfds - i)
378 ret = fd_package_recv(sock, fds + i, step);
380 /* Close all received descriptors. */
382 for (j = 0; j < i; j++)
394 fd_send(int sock, const int *fds, size_t nfds)
396 unsigned int i, step;
399 if (nfds == 0 || fds == NULL) {
406 if (PKG_MAX_SIZE < nfds - i)
410 ret = fd_package_send(sock, fds + i, step);
420 buf_send(int sock, void *buf, size_t size)
425 PJDLOG_ASSERT(sock >= 0);
426 PJDLOG_ASSERT(size > 0);
427 PJDLOG_ASSERT(buf != NULL);
431 fd_wait(sock, false);
432 done = send(sock, ptr, size, 0);
437 } else if (done == 0) {
449 buf_recv(int sock, void *buf, size_t size)
454 PJDLOG_ASSERT(sock >= 0);
455 PJDLOG_ASSERT(buf != NULL);
460 done = recv(sock, ptr, size, 0);
465 } else if (done == 0) {