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) {
163 unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
165 struct cmsghdr *cmsg;
169 bzero(credbuf, sizeof(credbuf));
170 bzero(&msg, sizeof(msg));
171 bzero(&iov, sizeof(iov));
174 * XXX: We send one byte along with the control message, because
175 * setting msg_iov to NULL only works if this is the first
176 * packet send over the socket. Once we send some data we
177 * won't be able to send credentials anymore. This is most
178 * likely a kernel bug.
181 iov.iov_base = &dummy;
182 iov.iov_len = sizeof(dummy);
186 msg.msg_control = credbuf;
187 msg.msg_controllen = sizeof(credbuf);
189 cmsg = CMSG_FIRSTHDR(&msg);
190 cmsg->cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
191 cmsg->cmsg_level = SOL_SOCKET;
192 cmsg->cmsg_type = SCM_CREDS;
194 if (msg_send(sock, &msg) == -1)
201 cred_recv(int sock, struct cmsgcred *cred)
203 unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
205 struct cmsghdr *cmsg;
209 bzero(credbuf, sizeof(credbuf));
210 bzero(&msg, sizeof(msg));
211 bzero(&iov, sizeof(iov));
213 iov.iov_base = &dummy;
214 iov.iov_len = sizeof(dummy);
218 msg.msg_control = credbuf;
219 msg.msg_controllen = sizeof(credbuf);
221 if (msg_recv(sock, &msg) == -1)
224 cmsg = CMSG_FIRSTHDR(&msg);
226 cmsg->cmsg_len != CMSG_LEN(sizeof(struct cmsgcred)) ||
227 cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_CREDS) {
231 bcopy(CMSG_DATA(cmsg), cred, sizeof(*cred));
237 fd_package_send(int sock, const int *fds, size_t nfds)
240 struct cmsghdr *cmsg;
246 PJDLOG_ASSERT(sock >= 0);
247 PJDLOG_ASSERT(fds != NULL);
248 PJDLOG_ASSERT(nfds > 0);
250 bzero(&msg, sizeof(msg));
253 * XXX: Look into cred_send function for more details.
256 iov.iov_base = &dummy;
257 iov.iov_len = sizeof(dummy);
261 msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
262 msg.msg_control = calloc(1, msg.msg_controllen);
263 if (msg.msg_control == NULL)
268 for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
269 i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
270 if (msghdr_add_fd(cmsg, fds[i]) == -1)
274 if (msg_send(sock, &msg) == -1)
280 free(msg.msg_control);
286 fd_package_recv(int sock, int *fds, size_t nfds)
289 struct cmsghdr *cmsg;
295 PJDLOG_ASSERT(sock >= 0);
296 PJDLOG_ASSERT(nfds > 0);
297 PJDLOG_ASSERT(fds != NULL);
299 bzero(&msg, sizeof(msg));
300 bzero(&iov, sizeof(iov));
303 * XXX: Look into cred_send function for more details.
305 iov.iov_base = &dummy;
306 iov.iov_len = sizeof(dummy);
310 msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
311 msg.msg_control = calloc(1, msg.msg_controllen);
312 if (msg.msg_control == NULL)
317 if (msg_recv(sock, &msg) == -1)
320 for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
321 i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
322 fds[i] = msghdr_get_fd(cmsg);
327 if (cmsg != NULL || i < nfds) {
331 * We need to close all received descriptors, even if we have
332 * different control message (eg. SCM_CREDS) in between.
334 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
335 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
336 fd = msghdr_get_fd(cmsg);
347 free(msg.msg_control);
353 fd_recv(int sock, int *fds, size_t nfds)
355 unsigned int i, step, j;
358 if (nfds == 0 || fds == NULL) {
365 if (PKG_MAX_SIZE < nfds - i)
369 ret = fd_package_recv(sock, fds + i, step);
371 /* Close all received descriptors. */
373 for (j = 0; j < i; j++)
385 fd_send(int sock, const int *fds, size_t nfds)
387 unsigned int i, step;
390 if (nfds == 0 || fds == NULL) {
397 if (PKG_MAX_SIZE < nfds - i)
401 ret = fd_package_send(sock, fds + i, step);
411 buf_send(int sock, void *buf, size_t size)
416 PJDLOG_ASSERT(sock >= 0);
417 PJDLOG_ASSERT(size > 0);
418 PJDLOG_ASSERT(buf != NULL);
422 fd_wait(sock, false);
423 done = send(sock, ptr, size, 0);
428 } else if (done == 0) {
440 buf_recv(int sock, void *buf, size_t size)
445 PJDLOG_ASSERT(sock >= 0);
446 PJDLOG_ASSERT(buf != NULL);
451 done = recv(sock, ptr, size, 0);
456 } else if (done == 0) {