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>
38 #include <sys/select.h>
52 #include "common_impl.h"
57 #define PJDLOG_ASSERT(...) assert(__VA_ARGS__)
58 #define PJDLOG_RASSERT(expr, ...) assert(expr)
59 #define PJDLOG_ABORT(...) abort()
63 /* Linux: arbitrary size, but must be lower than SCM_MAX_FD. */
64 #define PKG_MAX_SIZE ((64U - 1) * CMSG_SPACE(sizeof(int)))
66 #define PKG_MAX_SIZE (MCLBYTES / CMSG_SPACE(sizeof(int)) - 1)
70 msghdr_add_fd(struct cmsghdr *cmsg, int fd)
73 PJDLOG_ASSERT(fd >= 0);
75 cmsg->cmsg_level = SOL_SOCKET;
76 cmsg->cmsg_type = SCM_RIGHTS;
77 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
78 bcopy(&fd, CMSG_DATA(cmsg), sizeof(fd));
84 fd_wait(int fd, bool doread)
88 PJDLOG_ASSERT(fd >= 0);
92 (void)select(fd + 1, doread ? &fds : NULL, doread ? NULL : &fds,
97 msg_recv(int sock, struct msghdr *msg)
101 PJDLOG_ASSERT(sock >= 0);
103 #ifdef MSG_CMSG_CLOEXEC
104 flags = MSG_CMSG_CLOEXEC;
111 if (recvmsg(sock, msg, flags) == -1) {
123 msg_send(int sock, const struct msghdr *msg)
126 PJDLOG_ASSERT(sock >= 0);
129 fd_wait(sock, false);
130 if (sendmsg(sock, msg, 0) == -1) {
145 unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
147 struct cmsghdr *cmsg;
151 bzero(credbuf, sizeof(credbuf));
152 bzero(&msg, sizeof(msg));
153 bzero(&iov, sizeof(iov));
156 * XXX: We send one byte along with the control message, because
157 * setting msg_iov to NULL only works if this is the first
158 * packet send over the socket. Once we send some data we
159 * won't be able to send credentials anymore. This is most
160 * likely a kernel bug.
163 iov.iov_base = &dummy;
164 iov.iov_len = sizeof(dummy);
168 msg.msg_control = credbuf;
169 msg.msg_controllen = sizeof(credbuf);
171 cmsg = CMSG_FIRSTHDR(&msg);
172 cmsg->cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
173 cmsg->cmsg_level = SOL_SOCKET;
174 cmsg->cmsg_type = SCM_CREDS;
176 if (msg_send(sock, &msg) == -1)
183 cred_recv(int sock, struct cmsgcred *cred)
185 unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
187 struct cmsghdr *cmsg;
191 bzero(credbuf, sizeof(credbuf));
192 bzero(&msg, sizeof(msg));
193 bzero(&iov, sizeof(iov));
195 iov.iov_base = &dummy;
196 iov.iov_len = sizeof(dummy);
200 msg.msg_control = credbuf;
201 msg.msg_controllen = sizeof(credbuf);
203 if (msg_recv(sock, &msg) == -1)
206 cmsg = CMSG_FIRSTHDR(&msg);
208 cmsg->cmsg_len != CMSG_LEN(sizeof(struct cmsgcred)) ||
209 cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_CREDS) {
213 bcopy(CMSG_DATA(cmsg), cred, sizeof(*cred));
220 fd_package_send(int sock, const int *fds, size_t nfds)
223 struct cmsghdr *cmsg;
229 PJDLOG_ASSERT(sock >= 0);
230 PJDLOG_ASSERT(fds != NULL);
231 PJDLOG_ASSERT(nfds > 0);
233 bzero(&msg, sizeof(msg));
236 * XXX: Look into cred_send function for more details.
239 iov.iov_base = &dummy;
240 iov.iov_len = sizeof(dummy);
244 msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
245 msg.msg_control = calloc(1, msg.msg_controllen);
246 if (msg.msg_control == NULL)
251 for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
252 i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
253 if (msghdr_add_fd(cmsg, fds[i]) == -1)
257 if (msg_send(sock, &msg) == -1)
263 free(msg.msg_control);
269 fd_package_recv(int sock, int *fds, size_t nfds)
272 struct cmsghdr *cmsg;
278 PJDLOG_ASSERT(sock >= 0);
279 PJDLOG_ASSERT(nfds > 0);
280 PJDLOG_ASSERT(fds != NULL);
282 bzero(&msg, sizeof(msg));
283 bzero(&iov, sizeof(iov));
286 * XXX: Look into cred_send function for more details.
288 iov.iov_base = &dummy;
289 iov.iov_len = sizeof(dummy);
293 msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
294 msg.msg_control = calloc(1, msg.msg_controllen);
295 if (msg.msg_control == NULL)
300 if (msg_recv(sock, &msg) == -1)
304 cmsg = CMSG_FIRSTHDR(&msg);
305 while (cmsg && i < nfds) {
308 if (cmsg->cmsg_level != SOL_SOCKET ||
309 cmsg->cmsg_type != SCM_RIGHTS) {
313 n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
318 bcopy(CMSG_DATA(cmsg), fds + i, sizeof(int) * n);
319 cmsg = CMSG_NXTHDR(&msg, cmsg);
323 if (cmsg != NULL || i < nfds) {
327 * We need to close all received descriptors, even if we have
328 * different control message (eg. SCM_CREDS) in between.
331 for (i = 0; i < last; i++) {
340 #ifndef MSG_CMSG_CLOEXEC
342 * If the MSG_CMSG_CLOEXEC flag is not available we cannot set the
343 * close-on-exec flag atomically, but we still want to set it for
346 for (i = 0; i < nfds; i++) {
347 (void) fcntl(fds[i], F_SETFD, FD_CLOEXEC);
354 free(msg.msg_control);
360 fd_recv(int sock, int *fds, size_t nfds)
362 unsigned int i, step, j;
365 if (nfds == 0 || fds == NULL) {
372 if (PKG_MAX_SIZE < nfds - i)
376 ret = fd_package_recv(sock, fds + i, step);
378 /* Close all received descriptors. */
380 for (j = 0; j < i; j++)
392 fd_send(int sock, const int *fds, size_t nfds)
394 unsigned int i, step;
397 if (nfds == 0 || fds == NULL) {
404 if (PKG_MAX_SIZE < nfds - i)
408 ret = fd_package_send(sock, fds + i, step);
418 buf_send(int sock, void *buf, size_t size)
423 PJDLOG_ASSERT(sock >= 0);
424 PJDLOG_ASSERT(size > 0);
425 PJDLOG_ASSERT(buf != NULL);
429 fd_wait(sock, false);
430 done = send(sock, ptr, size, 0);
435 } else if (done == 0) {
447 buf_recv(int sock, void *buf, size_t size)
452 PJDLOG_ASSERT(sock >= 0);
453 PJDLOG_ASSERT(buf != NULL);
458 done = recv(sock, ptr, size, 0);
463 } else if (done == 0) {