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()
62 * To work around limitations in 32-bit emulation on 64-bit kernels, use a
63 * machine-independent limit on the number of FDs per message. Each control
64 * message contains 1 FD and requires 12 bytes for the header, 4 pad bytes,
65 * 4 bytes for the descriptor, and another 4 pad bytes.
67 #define PKG_MAX_SIZE (MCLBYTES / 24)
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 msghdr_get_fd(struct cmsghdr *cmsg)
88 if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET ||
89 cmsg->cmsg_type != SCM_RIGHTS ||
90 cmsg->cmsg_len != CMSG_LEN(sizeof(fd))) {
95 bcopy(CMSG_DATA(cmsg), &fd, sizeof(fd));
96 #ifndef MSG_CMSG_CLOEXEC
98 * If the MSG_CMSG_CLOEXEC flag is not available we cannot set the
99 * close-on-exec flag atomically, but we still want to set it for
102 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
109 fd_wait(int fd, bool doread)
113 PJDLOG_ASSERT(fd >= 0);
117 (void)select(fd + 1, doread ? &fds : NULL, doread ? NULL : &fds,
122 msg_recv(int sock, struct msghdr *msg)
126 PJDLOG_ASSERT(sock >= 0);
128 #ifdef MSG_CMSG_CLOEXEC
129 flags = MSG_CMSG_CLOEXEC;
136 if (recvmsg(sock, msg, flags) == -1) {
148 msg_send(int sock, const struct msghdr *msg)
151 PJDLOG_ASSERT(sock >= 0);
154 fd_wait(sock, false);
155 if (sendmsg(sock, msg, 0) == -1) {
169 unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
171 struct cmsghdr *cmsg;
175 bzero(credbuf, sizeof(credbuf));
176 bzero(&msg, sizeof(msg));
177 bzero(&iov, sizeof(iov));
180 * XXX: We send one byte along with the control message, because
181 * setting msg_iov to NULL only works if this is the first
182 * packet send over the socket. Once we send some data we
183 * won't be able to send credentials anymore. This is most
184 * likely a kernel bug.
187 iov.iov_base = &dummy;
188 iov.iov_len = sizeof(dummy);
192 msg.msg_control = credbuf;
193 msg.msg_controllen = sizeof(credbuf);
195 cmsg = CMSG_FIRSTHDR(&msg);
196 cmsg->cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
197 cmsg->cmsg_level = SOL_SOCKET;
198 cmsg->cmsg_type = SCM_CREDS;
200 if (msg_send(sock, &msg) == -1)
207 cred_recv(int sock, struct cmsgcred *cred)
209 unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
211 struct cmsghdr *cmsg;
215 bzero(credbuf, sizeof(credbuf));
216 bzero(&msg, sizeof(msg));
217 bzero(&iov, sizeof(iov));
219 iov.iov_base = &dummy;
220 iov.iov_len = sizeof(dummy);
224 msg.msg_control = credbuf;
225 msg.msg_controllen = sizeof(credbuf);
227 if (msg_recv(sock, &msg) == -1)
230 cmsg = CMSG_FIRSTHDR(&msg);
232 cmsg->cmsg_len != CMSG_LEN(sizeof(struct cmsgcred)) ||
233 cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_CREDS) {
237 bcopy(CMSG_DATA(cmsg), cred, sizeof(*cred));
243 fd_package_send(int sock, const int *fds, size_t nfds)
246 struct cmsghdr *cmsg;
252 PJDLOG_ASSERT(sock >= 0);
253 PJDLOG_ASSERT(fds != NULL);
254 PJDLOG_ASSERT(nfds > 0);
256 bzero(&msg, sizeof(msg));
259 * XXX: Look into cred_send function for more details.
262 iov.iov_base = &dummy;
263 iov.iov_len = sizeof(dummy);
267 msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
268 msg.msg_control = calloc(1, msg.msg_controllen);
269 if (msg.msg_control == NULL)
274 for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
275 i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
276 if (msghdr_add_fd(cmsg, fds[i]) == -1)
280 if (msg_send(sock, &msg) == -1)
286 free(msg.msg_control);
292 fd_package_recv(int sock, int *fds, size_t nfds)
295 struct cmsghdr *cmsg;
301 PJDLOG_ASSERT(sock >= 0);
302 PJDLOG_ASSERT(nfds > 0);
303 PJDLOG_ASSERT(fds != NULL);
305 bzero(&msg, sizeof(msg));
306 bzero(&iov, sizeof(iov));
309 * XXX: Look into cred_send function for more details.
311 iov.iov_base = &dummy;
312 iov.iov_len = sizeof(dummy);
316 msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
317 msg.msg_control = calloc(1, msg.msg_controllen);
318 if (msg.msg_control == NULL)
323 if (msg_recv(sock, &msg) == -1)
326 for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
327 i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
328 fds[i] = msghdr_get_fd(cmsg);
333 if (cmsg != NULL || i < nfds) {
337 * We need to close all received descriptors, even if we have
338 * different control message (eg. SCM_CREDS) in between.
340 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
341 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
342 fd = msghdr_get_fd(cmsg);
353 free(msg.msg_control);
359 fd_recv(int sock, int *fds, size_t nfds)
361 unsigned int i, step, j;
364 if (nfds == 0 || fds == NULL) {
371 if (PKG_MAX_SIZE < nfds - i)
375 ret = fd_package_recv(sock, fds + i, step);
377 /* Close all received descriptors. */
379 for (j = 0; j < i; j++)
391 fd_send(int sock, const int *fds, size_t nfds)
393 unsigned int i, step;
396 if (nfds == 0 || fds == NULL) {
403 if (PKG_MAX_SIZE < nfds - i)
407 ret = fd_package_send(sock, fds + i, step);
417 buf_send(int sock, void *buf, size_t size)
422 PJDLOG_ASSERT(sock >= 0);
423 PJDLOG_ASSERT(size > 0);
424 PJDLOG_ASSERT(buf != NULL);
428 fd_wait(sock, false);
429 done = send(sock, ptr, size, 0);
434 } else if (done == 0) {
446 buf_recv(int sock, void *buf, size_t size)
451 PJDLOG_ASSERT(sock >= 0);
452 PJDLOG_ASSERT(buf != NULL);
457 done = recv(sock, ptr, size, 0);
462 } else if (done == 0) {