1 /* $NetBSD: monitor_fdpass.c,v 1.5 2015/04/03 23:58:19 christos Exp $ */ 2 /* $OpenBSD: monitor_fdpass.c,v 1.20 2015/02/25 23:05:47 djm Exp $ */ 3 /* 4 * Copyright 2001 Niels Provos <provos@citi.umich.edu> 5 * All rights reserved. 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "includes.h" 29 __RCSID("$NetBSD: monitor_fdpass.c,v 1.5 2015/04/03 23:58:19 christos Exp $"); 30 #include <sys/types.h> 31 #include <sys/socket.h> 32 #include <sys/uio.h> 33 34 #include <errno.h> 35 #include <poll.h> 36 #include <string.h> 37 #include <stdarg.h> 38 39 #include "log.h" 40 #include "monitor_fdpass.h" 41 42 int 43 mm_send_fd(int sock, int fd) 44 { 45 struct msghdr msg; 46 union { 47 struct cmsghdr hdr; 48 char buf[1024]; 49 } cmsgbuf; 50 struct cmsghdr *cmsg; 51 struct iovec vec; 52 char ch = '\0'; 53 ssize_t n; 54 struct pollfd pfd; 55 56 if (sizeof(cmsgbuf.buf) < CMSG_SPACE(sizeof(int))) { 57 error("%s: %zu < %zu, recompile", __func__, 58 sizeof(cmsgbuf.buf), CMSG_SPACE(sizeof(int))); 59 return -1; 60 } 61 62 memset(&msg, 0, sizeof(msg)); 63 memset(&cmsgbuf, 0, sizeof(cmsgbuf)); 64 msg.msg_control = &cmsgbuf.buf; 65 msg.msg_controllen = CMSG_SPACE(sizeof(int)); 66 cmsg = CMSG_FIRSTHDR(&msg); 67 cmsg->cmsg_len = CMSG_LEN(sizeof(int)); 68 cmsg->cmsg_level = SOL_SOCKET; 69 cmsg->cmsg_type = SCM_RIGHTS; 70 *(int *)CMSG_DATA(cmsg) = fd; 71 msg.msg_controllen = cmsg->cmsg_len; 72 73 vec.iov_base = &ch; 74 vec.iov_len = 1; 75 msg.msg_iov = &vec; 76 msg.msg_iovlen = 1; 77 78 pfd.fd = sock; 79 pfd.events = POLLOUT; 80 while ((n = sendmsg(sock, &msg, 0)) == -1 && 81 (errno == EAGAIN || errno == EINTR)) { 82 debug3("%s: sendmsg(%d): %s", __func__, fd, strerror(errno)); 83 (void)poll(&pfd, 1, -1); 84 } 85 if (n == -1) { 86 error("%s: sendmsg(%d): %s", __func__, fd, 87 strerror(errno)); 88 return -1; 89 } 90 91 if (n != 1) { 92 error("%s: sendmsg: expected sent 1 got %ld", 93 __func__, (long)n); 94 return -1; 95 } 96 return 0; 97 } 98 99 int 100 mm_receive_fd(int sock) 101 { 102 struct msghdr msg; 103 union { 104 struct cmsghdr hdr; 105 char buf[1024]; 106 } cmsgbuf; 107 struct cmsghdr *cmsg; 108 struct iovec vec; 109 ssize_t n; 110 char ch; 111 int fd; 112 struct pollfd pfd; 113 114 if (sizeof(cmsgbuf.buf) < CMSG_SPACE(sizeof(int))) { 115 error("%s: %zu < %zu, recompile", __func__, 116 sizeof(cmsgbuf.buf), CMSG_SPACE(sizeof(int))); 117 return -1; 118 } 119 120 memset(&msg, 0, sizeof(msg)); 121 memset(&cmsgbuf, 0, sizeof(cmsgbuf)); 122 vec.iov_base = &ch; 123 vec.iov_len = 1; 124 msg.msg_iov = &vec; 125 msg.msg_iovlen = 1; 126 msg.msg_control = &cmsgbuf.buf; 127 msg.msg_controllen = CMSG_SPACE(sizeof(int)); 128 129 pfd.fd = sock; 130 pfd.events = POLLIN; 131 while ((n = recvmsg(sock, &msg, 0)) == -1 && 132 (errno == EAGAIN || errno == EINTR)) { 133 debug3("%s: recvmsg: %s", __func__, strerror(errno)); 134 (void)poll(&pfd, 1, -1); 135 } 136 if (n == -1) { 137 error("%s: recvmsg: %s", __func__, strerror(errno)); 138 return -1; 139 } 140 141 if (n != 1) { 142 error("%s: recvmsg: expected received 1 got %ld", 143 __func__, (long)n); 144 return -1; 145 } 146 147 cmsg = CMSG_FIRSTHDR(&msg); 148 if (cmsg == NULL) { 149 error("%s: no message header", __func__); 150 return -1; 151 } 152 153 if (cmsg->cmsg_type != SCM_RIGHTS) { 154 error("%s: expected type %d got %d", __func__, 155 SCM_RIGHTS, cmsg->cmsg_type); 156 return -1; 157 } 158 fd = (*(int *)CMSG_DATA(cmsg)); 159 return fd; 160 } 161