1 /* $OpenBSD: monitor_fdpass.c,v 1.17 2016/02/29 20:22:36 jca Exp $ */ 2 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 <sys/types.h> 29 #include <sys/socket.h> 30 #include <sys/uio.h> 31 32 #include <errno.h> 33 #include <string.h> 34 35 #include "log.h" 36 #include "monitor.h" 37 38 int 39 mm_send_fd(int socket, int fd) 40 { 41 struct msghdr msg; 42 union { 43 struct cmsghdr hdr; 44 char buf[CMSG_SPACE(sizeof(int))]; 45 } cmsgbuf; 46 char ch = '\0'; 47 struct cmsghdr *cmsg; 48 struct iovec vec; 49 ssize_t n; 50 51 bzero(&msg, 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 if ((n = sendmsg(socket, &msg, 0)) == -1) { 66 log_error("mm_send_fd: sendmsg(%d)", fd); 67 return -1; 68 } 69 if (n != 1) { 70 log_error("mm_send_fd: sendmsg: expected sent 1 got %zd", n); 71 return -1; 72 } 73 return 0; 74 } 75 76 int 77 mm_receive_fd(int socket) 78 { 79 struct msghdr msg; 80 union { 81 struct cmsghdr hdr; 82 char buf[CMSG_SPACE(sizeof(int))]; 83 } cmsgbuf; 84 char ch; 85 struct cmsghdr *cmsg; 86 struct iovec vec; 87 ssize_t n; 88 int fd; 89 90 bzero(&msg, sizeof msg); 91 vec.iov_base = &ch; 92 vec.iov_len = 1; 93 msg.msg_iov = &vec; 94 msg.msg_iovlen = 1; 95 msg.msg_control = &cmsgbuf.buf; 96 msg.msg_controllen = sizeof(cmsgbuf.buf); 97 98 if ((n = recvmsg(socket, &msg, 0)) == -1) { 99 log_error("mm_receive_fd: recvmsg"); 100 return -1; 101 } 102 if (n != 1) { 103 log_error("mm_receive_fd: recvmsg: expected received 1 got %zd", 104 n); 105 return -1; 106 } 107 cmsg = CMSG_FIRSTHDR(&msg); 108 if (cmsg == NULL) { 109 log_error("mm_receive_fd: no message header"); 110 return -1; 111 } 112 if (cmsg->cmsg_type != SCM_RIGHTS) { 113 log_error("mm_receive_fd: expected type %d got %d", SCM_RIGHTS, 114 cmsg->cmsg_type); 115 return -1; 116 } 117 fd = (*(int *)CMSG_DATA(cmsg)); 118 return fd; 119 } 120