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