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 if (!fd_is_valid(fd)) {
74 cmsg->cmsg_level = SOL_SOCKET;
75 cmsg->cmsg_type = SCM_RIGHTS;
76 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
77 bcopy(&fd, CMSG_DATA(cmsg), sizeof(fd));
83 msghdr_get_fd(struct cmsghdr *cmsg)
87 if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET ||
88 cmsg->cmsg_type != SCM_RIGHTS ||
89 cmsg->cmsg_len != CMSG_LEN(sizeof(fd))) {
94 bcopy(CMSG_DATA(cmsg), &fd, sizeof(fd));
95 #ifndef MSG_CMSG_CLOEXEC
97 * If the MSG_CMSG_CLOEXEC flag is not available we cannot set the
98 * close-on-exec flag atomically, but we still want to set it for
101 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
108 fd_wait(int fd, bool doread)
112 PJDLOG_ASSERT(fd >= 0);
116 (void)select(fd + 1, doread ? &fds : NULL, doread ? NULL : &fds,
121 msg_recv(int sock, struct msghdr *msg)
125 PJDLOG_ASSERT(sock >= 0);
127 #ifdef MSG_CMSG_CLOEXEC
128 flags = MSG_CMSG_CLOEXEC;
135 if (recvmsg(sock, msg, flags) == -1) {
147 msg_send(int sock, const struct msghdr *msg)
150 PJDLOG_ASSERT(sock >= 0);
153 fd_wait(sock, false);
154 if (sendmsg(sock, msg, 0) == -1) {
168 unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
170 struct cmsghdr *cmsg;
174 bzero(credbuf, sizeof(credbuf));
175 bzero(&msg, sizeof(msg));
176 bzero(&iov, sizeof(iov));
179 * XXX: We send one byte along with the control message, because
180 * setting msg_iov to NULL only works if this is the first
181 * packet send over the socket. Once we send some data we
182 * won't be able to send credentials anymore. This is most
183 * likely a kernel bug.
186 iov.iov_base = &dummy;
187 iov.iov_len = sizeof(dummy);
191 msg.msg_control = credbuf;
192 msg.msg_controllen = sizeof(credbuf);
194 cmsg = CMSG_FIRSTHDR(&msg);
195 cmsg->cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
196 cmsg->cmsg_level = SOL_SOCKET;
197 cmsg->cmsg_type = SCM_CREDS;
199 if (msg_send(sock, &msg) == -1)
206 cred_recv(int sock, struct cmsgcred *cred)
208 unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
210 struct cmsghdr *cmsg;
214 bzero(credbuf, sizeof(credbuf));
215 bzero(&msg, sizeof(msg));
216 bzero(&iov, sizeof(iov));
218 iov.iov_base = &dummy;
219 iov.iov_len = sizeof(dummy);
223 msg.msg_control = credbuf;
224 msg.msg_controllen = sizeof(credbuf);
226 if (msg_recv(sock, &msg) == -1)
229 cmsg = CMSG_FIRSTHDR(&msg);
231 cmsg->cmsg_len != CMSG_LEN(sizeof(struct cmsgcred)) ||
232 cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_CREDS) {
236 bcopy(CMSG_DATA(cmsg), cred, sizeof(*cred));
242 fd_package_send(int sock, const int *fds, size_t nfds)
245 struct cmsghdr *cmsg;
251 PJDLOG_ASSERT(sock >= 0);
252 PJDLOG_ASSERT(fds != NULL);
253 PJDLOG_ASSERT(nfds > 0);
255 bzero(&msg, sizeof(msg));
258 * XXX: Look into cred_send function for more details.
261 iov.iov_base = &dummy;
262 iov.iov_len = sizeof(dummy);
266 msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
267 msg.msg_control = calloc(1, msg.msg_controllen);
268 if (msg.msg_control == NULL)
273 for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
274 i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
275 if (msghdr_add_fd(cmsg, fds[i]) == -1)
279 if (msg_send(sock, &msg) == -1)
285 free(msg.msg_control);
291 fd_package_recv(int sock, int *fds, size_t nfds)
294 struct cmsghdr *cmsg;
300 PJDLOG_ASSERT(sock >= 0);
301 PJDLOG_ASSERT(nfds > 0);
302 PJDLOG_ASSERT(fds != NULL);
304 bzero(&msg, sizeof(msg));
305 bzero(&iov, sizeof(iov));
308 * XXX: Look into cred_send function for more details.
310 iov.iov_base = &dummy;
311 iov.iov_len = sizeof(dummy);
315 msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
316 msg.msg_control = calloc(1, msg.msg_controllen);
317 if (msg.msg_control == NULL)
322 if (msg_recv(sock, &msg) == -1)
325 for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
326 i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
327 fds[i] = msghdr_get_fd(cmsg);
332 if (cmsg != NULL || i < nfds) {
336 * We need to close all received descriptors, even if we have
337 * different control message (eg. SCM_CREDS) in between.
339 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
340 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
341 fd = msghdr_get_fd(cmsg);
352 free(msg.msg_control);
358 fd_recv(int sock, int *fds, size_t nfds)
360 unsigned int i, step, j;
363 if (nfds == 0 || fds == NULL) {
370 if (PKG_MAX_SIZE < nfds - i)
374 ret = fd_package_recv(sock, fds + i, step);
376 /* Close all received descriptors. */
378 for (j = 0; j < i; j++)
390 fd_send(int sock, const int *fds, size_t nfds)
392 unsigned int i, step;
395 if (nfds == 0 || fds == NULL) {
402 if (PKG_MAX_SIZE < nfds - i)
406 ret = fd_package_send(sock, fds + i, step);
416 buf_send(int sock, void *buf, size_t size)
421 PJDLOG_ASSERT(sock >= 0);
422 PJDLOG_ASSERT(size > 0);
423 PJDLOG_ASSERT(buf != NULL);
427 fd_wait(sock, false);
428 done = send(sock, ptr, size, 0);
433 } else if (done == 0) {
445 buf_recv(int sock, void *buf, size_t size)
450 PJDLOG_ASSERT(sock >= 0);
451 PJDLOG_ASSERT(buf != NULL);
456 done = recv(sock, ptr, size, 0);
461 } else if (done == 0) {