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/types.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()
60 msghdr_add_fd(struct cmsghdr *cmsg, int fd)
63 PJDLOG_ASSERT(fd >= 0);
65 if (!fd_is_valid(fd)) {
70 cmsg->cmsg_level = SOL_SOCKET;
71 cmsg->cmsg_type = SCM_RIGHTS;
72 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
73 bcopy(&fd, CMSG_DATA(cmsg), sizeof(fd));
79 msghdr_get_fd(struct cmsghdr *cmsg)
83 if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET ||
84 cmsg->cmsg_type != SCM_RIGHTS ||
85 cmsg->cmsg_len != CMSG_LEN(sizeof(fd))) {
90 bcopy(CMSG_DATA(cmsg), &fd, sizeof(fd));
91 #ifndef MSG_CMSG_CLOEXEC
93 * If the MSG_CMSG_CLOEXEC flag is not available we cannot set the
94 * close-on-exec flag atomically, but we still want to set it for
97 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
104 fd_wait(int fd, bool doread)
108 PJDLOG_ASSERT(fd >= 0);
112 (void)select(fd + 1, doread ? &fds : NULL, doread ? NULL : &fds,
117 msg_recv(int sock, struct msghdr *msg)
121 PJDLOG_ASSERT(sock >= 0);
123 #ifdef MSG_CMSG_CLOEXEC
124 flags = MSG_CMSG_CLOEXEC;
131 if (recvmsg(sock, msg, flags) == -1) {
143 msg_send(int sock, const struct msghdr *msg)
146 PJDLOG_ASSERT(sock >= 0);
149 fd_wait(sock, false);
150 if (sendmsg(sock, msg, 0) == -1) {
164 unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
166 struct cmsghdr *cmsg;
170 bzero(credbuf, sizeof(credbuf));
171 bzero(&msg, sizeof(msg));
172 bzero(&iov, sizeof(iov));
175 * XXX: We send one byte along with the control message, because
176 * setting msg_iov to NULL only works if this is the first
177 * packet send over the socket. Once we send some data we
178 * won't be able to send credentials anymore. This is most
179 * likely a kernel bug.
182 iov.iov_base = &dummy;
183 iov.iov_len = sizeof(dummy);
187 msg.msg_control = credbuf;
188 msg.msg_controllen = sizeof(credbuf);
190 cmsg = CMSG_FIRSTHDR(&msg);
191 cmsg->cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
192 cmsg->cmsg_level = SOL_SOCKET;
193 cmsg->cmsg_type = SCM_CREDS;
195 if (msg_send(sock, &msg) == -1)
202 cred_recv(int sock, struct cmsgcred *cred)
204 unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
206 struct cmsghdr *cmsg;
210 bzero(credbuf, sizeof(credbuf));
211 bzero(&msg, sizeof(msg));
212 bzero(&iov, sizeof(iov));
214 iov.iov_base = &dummy;
215 iov.iov_len = sizeof(dummy);
219 msg.msg_control = credbuf;
220 msg.msg_controllen = sizeof(credbuf);
222 if (msg_recv(sock, &msg) == -1)
225 cmsg = CMSG_FIRSTHDR(&msg);
227 cmsg->cmsg_len != CMSG_LEN(sizeof(struct cmsgcred)) ||
228 cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_CREDS) {
232 bcopy(CMSG_DATA(cmsg), cred, sizeof(*cred));
238 fd_send(int sock, const int *fds, size_t nfds)
241 struct cmsghdr *cmsg;
245 if (nfds == 0 || fds == NULL) {
250 bzero(&msg, sizeof(msg));
253 msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
254 msg.msg_control = calloc(1, msg.msg_controllen);
255 if (msg.msg_control == NULL)
260 for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
261 i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
262 if (msghdr_add_fd(cmsg, fds[i]) == -1)
266 if (msg_send(sock, &msg) == -1)
272 free(msg.msg_control);
278 fd_recv(int sock, int *fds, size_t nfds)
281 struct cmsghdr *cmsg;
285 if (nfds == 0 || fds == NULL) {
290 bzero(&msg, sizeof(msg));
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)
303 for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
304 i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
305 fds[i] = msghdr_get_fd(cmsg);
310 if (cmsg != NULL || i < nfds) {
314 * We need to close all received descriptors, even if we have
315 * different control message (eg. SCM_CREDS) in between.
317 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
318 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
319 fd = msghdr_get_fd(cmsg);
330 free(msg.msg_control);
336 buf_send(int sock, void *buf, size_t size)
341 PJDLOG_ASSERT(sock >= 0);
342 PJDLOG_ASSERT(size > 0);
343 PJDLOG_ASSERT(buf != NULL);
347 fd_wait(sock, false);
348 done = send(sock, ptr, size, 0);
353 } else if (done == 0) {
365 buf_recv(int sock, void *buf, size_t size)
370 PJDLOG_ASSERT(sock >= 0);
371 PJDLOG_ASSERT(buf != NULL);
376 done = recv(sock, ptr, size, 0);
381 } else if (done == 0) {