1 /*- 2 * Copyright (c) 2012 The FreeBSD Foundation 3 * 4 * This software was developed by Pawel Jakub Dawidek under sponsorship from 5 * the FreeBSD Foundation. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/types.h> 30 #include <sys/select.h> 31 #include <sys/socket.h> 32 33 #include <assert.h> 34 #include <errno.h> 35 #include <stdlib.h> 36 #include <strings.h> 37 38 #include "misc.h" 39 40 int 41 pdwait(int pfd) 42 { 43 fd_set fdset; 44 45 FD_ZERO(&fdset); 46 FD_SET(pfd, &fdset); 47 48 return (select(pfd + 1, NULL, &fdset, NULL, NULL) == -1 ? -1 : 0); 49 } 50 51 int 52 descriptor_send(int sock, int fd) 53 { 54 unsigned char ctrl[CMSG_SPACE(sizeof(fd))]; 55 struct msghdr msg; 56 struct cmsghdr *cmsg; 57 58 assert(sock >= 0); 59 assert(fd >= 0); 60 61 bzero(&msg, sizeof(msg)); 62 bzero(&ctrl, sizeof(ctrl)); 63 64 msg.msg_iov = NULL; 65 msg.msg_iovlen = 0; 66 msg.msg_control = ctrl; 67 msg.msg_controllen = sizeof(ctrl); 68 69 cmsg = CMSG_FIRSTHDR(&msg); 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)); 74 75 if (sendmsg(sock, &msg, 0) == -1) 76 return (errno); 77 78 return (0); 79 } 80 81 int 82 descriptor_recv(int sock, int *fdp) 83 { 84 unsigned char ctrl[CMSG_SPACE(sizeof(*fdp))]; 85 struct msghdr msg; 86 struct cmsghdr *cmsg; 87 struct iovec iov; 88 int val; 89 90 assert(sock >= 0); 91 assert(fdp != NULL); 92 93 bzero(&msg, sizeof(msg)); 94 bzero(&ctrl, sizeof(ctrl)); 95 96 #if 1 97 /* 98 * This doesn't really make sense, as we don't plan to receive any 99 * data, but if no buffer is provided and recv(2) returns 0 without 100 * control message. Must be kernel bug. 101 */ 102 iov.iov_base = &val; 103 iov.iov_len = sizeof(val); 104 msg.msg_iov = &iov; 105 msg.msg_iovlen = 1; 106 #else 107 msg.msg_iov = NULL; 108 msg.msg_iovlen = 0; 109 #endif 110 msg.msg_control = ctrl; 111 msg.msg_controllen = sizeof(ctrl); 112 113 if (recvmsg(sock, &msg, 0) == -1) 114 return (errno); 115 116 cmsg = CMSG_FIRSTHDR(&msg); 117 if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET || 118 cmsg->cmsg_type != SCM_RIGHTS) { 119 return (EINVAL); 120 } 121 bcopy(CMSG_DATA(cmsg), fdp, sizeof(*fdp)); 122 123 return (0); 124 } 125