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