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