1 /* $OpenBSD: monitor_fdpass.c,v 1.18 2008/11/30 11:59:26 dtucker 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 <string.h> 33 #include <stdarg.h> 34 35 #include "log.h" 36 #include "monitor_fdpass.h" 37 38 int 39 mm_send_fd(int sock, int fd) 40 { 41 struct msghdr msg; 42 union { 43 struct cmsghdr hdr; 44 char buf[CMSG_SPACE(sizeof(int))]; 45 } cmsgbuf; 46 struct cmsghdr *cmsg; 47 struct iovec vec; 48 char ch = '\0'; 49 ssize_t n; 50 51 memset(&msg, 0, sizeof(msg)); 52 msg.msg_control = (caddr_t)&cmsgbuf.buf; 53 msg.msg_controllen = sizeof(cmsgbuf.buf); 54 cmsg = CMSG_FIRSTHDR(&msg); 55 cmsg->cmsg_len = CMSG_LEN(sizeof(int)); 56 cmsg->cmsg_level = SOL_SOCKET; 57 cmsg->cmsg_type = SCM_RIGHTS; 58 *(int *)CMSG_DATA(cmsg) = fd; 59 60 vec.iov_base = &ch; 61 vec.iov_len = 1; 62 msg.msg_iov = &vec; 63 msg.msg_iovlen = 1; 64 65 while ((n = sendmsg(sock, &msg, 0)) == -1 && (errno == EAGAIN || 66 errno == EINTR)) 67 debug3("%s: sendmsg(%d): %s", __func__, fd, strerror(errno)); 68 if (n == -1) { 69 error("%s: sendmsg(%d): %s", __func__, fd, 70 strerror(errno)); 71 return -1; 72 } 73 74 if (n != 1) { 75 error("%s: sendmsg: expected sent 1 got %ld", 76 __func__, (long)n); 77 return -1; 78 } 79 return 0; 80 } 81 82 int 83 mm_receive_fd(int sock) 84 { 85 struct msghdr msg; 86 union { 87 struct cmsghdr hdr; 88 char buf[CMSG_SPACE(sizeof(int))]; 89 } cmsgbuf; 90 struct cmsghdr *cmsg; 91 struct iovec vec; 92 ssize_t n; 93 char ch; 94 int fd; 95 96 memset(&msg, 0, sizeof(msg)); 97 vec.iov_base = &ch; 98 vec.iov_len = 1; 99 msg.msg_iov = &vec; 100 msg.msg_iovlen = 1; 101 msg.msg_control = &cmsgbuf.buf; 102 msg.msg_controllen = sizeof(cmsgbuf.buf); 103 104 while ((n = recvmsg(sock, &msg, 0)) == -1 && (errno == EAGAIN || 105 errno == EINTR)) 106 debug3("%s: recvmsg: %s", __func__, strerror(errno)); 107 if (n == -1) { 108 error("%s: recvmsg: %s", __func__, strerror(errno)); 109 return -1; 110 } 111 112 if (n != 1) { 113 error("%s: recvmsg: expected received 1 got %ld", 114 __func__, (long)n); 115 return -1; 116 } 117 118 cmsg = CMSG_FIRSTHDR(&msg); 119 if (cmsg == NULL) { 120 error("%s: no message header", __func__); 121 return -1; 122 } 123 124 if (cmsg->cmsg_type != SCM_RIGHTS) { 125 error("%s: expected type %d got %d", __func__, 126 SCM_RIGHTS, cmsg->cmsg_type); 127 return -1; 128 } 129 fd = (*(int *)CMSG_DATA(cmsg)); 130 return fd; 131 } 132