2 * Copyright (c) 2013 The FreeBSD Foundation
3 * Copyright (c) 2013 Mariusz Zaborski <oshogbo@FreeBSD.org>
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/socket.h>
49 #include "common_impl.h"
54 #define PJDLOG_ASSERT(...) assert(__VA_ARGS__)
55 #define PJDLOG_RASSERT(expr, ...) assert(expr)
56 #define PJDLOG_ABORT(...) abort()
59 #define PKG_MAX_SIZE (MCLBYTES / CMSG_SPACE(sizeof(int)) - 1)
62 msghdr_add_fd(struct cmsghdr *cmsg, int fd)
65 PJDLOG_ASSERT(fd >= 0);
67 if (!fd_is_valid(fd)) {
72 cmsg->cmsg_level = SOL_SOCKET;
73 cmsg->cmsg_type = SCM_RIGHTS;
74 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
75 bcopy(&fd, CMSG_DATA(cmsg), sizeof(fd));
81 msghdr_get_fd(struct cmsghdr *cmsg)
85 if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET ||
86 cmsg->cmsg_type != SCM_RIGHTS ||
87 cmsg->cmsg_len != CMSG_LEN(sizeof(fd))) {
92 bcopy(CMSG_DATA(cmsg), &fd, sizeof(fd));
93 #ifndef MSG_CMSG_CLOEXEC
95 * If the MSG_CMSG_CLOEXEC flag is not available we cannot set the
96 * close-on-exec flag atomically, but we still want to set it for
99 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
106 fd_wait(int fd, bool doread)
110 PJDLOG_ASSERT(fd >= 0);
114 (void)select(fd + 1, doread ? &fds : NULL, doread ? NULL : &fds,
119 msg_recv(int sock, struct msghdr *msg)
123 PJDLOG_ASSERT(sock >= 0);
125 #ifdef MSG_CMSG_CLOEXEC
126 flags = MSG_CMSG_CLOEXEC;
133 if (recvmsg(sock, msg, flags) == -1) {
145 msg_send(int sock, const struct msghdr *msg)
148 PJDLOG_ASSERT(sock >= 0);
151 fd_wait(sock, false);
152 if (sendmsg(sock, msg, 0) == -1) {
166 unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
168 struct cmsghdr *cmsg;
172 bzero(credbuf, sizeof(credbuf));
173 bzero(&msg, sizeof(msg));
174 bzero(&iov, sizeof(iov));
177 * XXX: We send one byte along with the control message, because
178 * setting msg_iov to NULL only works if this is the first
179 * packet send over the socket. Once we send some data we
180 * won't be able to send credentials anymore. This is most
181 * likely a kernel bug.
184 iov.iov_base = &dummy;
185 iov.iov_len = sizeof(dummy);
189 msg.msg_control = credbuf;
190 msg.msg_controllen = sizeof(credbuf);
192 cmsg = CMSG_FIRSTHDR(&msg);
193 cmsg->cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
194 cmsg->cmsg_level = SOL_SOCKET;
195 cmsg->cmsg_type = SCM_CREDS;
197 if (msg_send(sock, &msg) == -1)
204 cred_recv(int sock, struct cmsgcred *cred)
206 unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
208 struct cmsghdr *cmsg;
212 bzero(credbuf, sizeof(credbuf));
213 bzero(&msg, sizeof(msg));
214 bzero(&iov, sizeof(iov));
216 iov.iov_base = &dummy;
217 iov.iov_len = sizeof(dummy);
221 msg.msg_control = credbuf;
222 msg.msg_controllen = sizeof(credbuf);
224 if (msg_recv(sock, &msg) == -1)
227 cmsg = CMSG_FIRSTHDR(&msg);
229 cmsg->cmsg_len != CMSG_LEN(sizeof(struct cmsgcred)) ||
230 cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_CREDS) {
234 bcopy(CMSG_DATA(cmsg), cred, sizeof(*cred));
240 fd_package_send(int sock, const int *fds, size_t nfds)
243 struct cmsghdr *cmsg;
249 PJDLOG_ASSERT(sock >= 0);
250 PJDLOG_ASSERT(fds != NULL);
251 PJDLOG_ASSERT(nfds > 0);
253 bzero(&msg, sizeof(msg));
256 * XXX: Look into cred_send function for more details.
259 iov.iov_base = &dummy;
260 iov.iov_len = sizeof(dummy);
264 msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
265 msg.msg_control = calloc(1, msg.msg_controllen);
266 if (msg.msg_control == NULL)
271 for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
272 i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
273 if (msghdr_add_fd(cmsg, fds[i]) == -1)
277 if (msg_send(sock, &msg) == -1)
283 free(msg.msg_control);
289 fd_package_recv(int sock, int *fds, size_t nfds)
292 struct cmsghdr *cmsg;
298 PJDLOG_ASSERT(sock >= 0);
299 PJDLOG_ASSERT(nfds > 0);
300 PJDLOG_ASSERT(fds != NULL);
303 bzero(&msg, sizeof(msg));
304 bzero(&iov, sizeof(iov));
307 * XXX: Look into cred_send function for more details.
309 iov.iov_base = &dummy;
310 iov.iov_len = sizeof(dummy);
314 msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
315 msg.msg_control = calloc(1, msg.msg_controllen);
316 if (msg.msg_control == NULL)
321 if (msg_recv(sock, &msg) == -1)
324 for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
325 i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
326 fds[i] = msghdr_get_fd(cmsg);
331 if (cmsg != NULL || i < nfds) {
335 * We need to close all received descriptors, even if we have
336 * different control message (eg. SCM_CREDS) in between.
338 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
339 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
340 fd = msghdr_get_fd(cmsg);
351 free(msg.msg_control);
357 fd_recv(int sock, int *fds, size_t nfds)
359 unsigned int i, step, j;
362 if (nfds == 0 || fds == NULL) {
369 if (PKG_MAX_SIZE < nfds - i)
373 ret = fd_package_recv(sock, fds + i, step);
375 /* Close all received descriptors. */
377 for (j = 0; j < i; j++)
389 fd_send(int sock, const int *fds, size_t nfds)
391 unsigned int i, step;
394 if (nfds == 0 || fds == NULL) {
401 if (PKG_MAX_SIZE < nfds - i)
405 ret = fd_package_send(sock, fds + i, step);
415 buf_send(int sock, void *buf, size_t size)
420 PJDLOG_ASSERT(sock >= 0);
421 PJDLOG_ASSERT(size > 0);
422 PJDLOG_ASSERT(buf != NULL);
426 fd_wait(sock, false);
427 done = send(sock, ptr, size, 0);
432 } else if (done == 0) {
444 buf_recv(int sock, void *buf, size_t size)
449 PJDLOG_ASSERT(sock >= 0);
450 PJDLOG_ASSERT(buf != NULL);
455 done = recv(sock, ptr, size, 0);
460 } else if (done == 0) {