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