monitor_fdpass.c revision 1.3
1/*	$NetBSD: monitor_fdpass.c,v 1.3 2010/11/21 18:29:48 adam Exp $	*/
2/* $OpenBSD: monitor_fdpass.c,v 1.19 2010/01/12 00:58:25 djm Exp $ */
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 "includes.h"
29__RCSID("$NetBSD: monitor_fdpass.c,v 1.3 2010/11/21 18:29:48 adam Exp $");
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <sys/uio.h>
33
34#include <errno.h>
35#include <poll.h>
36#include <string.h>
37#include <stdarg.h>
38
39#include "log.h"
40#include "monitor_fdpass.h"
41
42int
43mm_send_fd(int sock, int fd)
44{
45	struct msghdr msg;
46	union {
47		struct cmsghdr hdr;
48		char buf[1024];
49	} cmsgbuf;
50	struct cmsghdr *cmsg;
51	struct iovec vec;
52	char ch = '\0';
53	ssize_t n;
54	struct pollfd pfd;
55
56	if (sizeof(cmsgbuf.buf) < CMSG_SPACE(sizeof(int))) {
57		error("%s: %zu < %zu, recompile", __func__,
58		    sizeof(cmsgbuf.buf), CMSG_SPACE(sizeof(int)));
59		return -1;
60	}
61
62	memset(&msg, 0, sizeof(msg));
63	msg.msg_control = &cmsgbuf.buf;
64	msg.msg_controllen = CMSG_SPACE(sizeof(int));
65	cmsg = CMSG_FIRSTHDR(&msg);
66	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
67	cmsg->cmsg_level = SOL_SOCKET;
68	cmsg->cmsg_type = SCM_RIGHTS;
69	*(int *)CMSG_DATA(cmsg) = fd;
70	msg.msg_controllen = cmsg->cmsg_len;
71
72	vec.iov_base = &ch;
73	vec.iov_len = 1;
74	msg.msg_iov = &vec;
75	msg.msg_iovlen = 1;
76
77	pfd.fd = sock;
78	pfd.events = POLLOUT;
79	while ((n = sendmsg(sock, &msg, 0)) == -1 &&
80	    (errno == EAGAIN || errno == EINTR)) {
81		debug3("%s: sendmsg(%d): %s", __func__, fd, strerror(errno));
82		(void)poll(&pfd, 1, -1);
83	}
84	if (n == -1) {
85		error("%s: sendmsg(%d): %s", __func__, fd,
86		    strerror(errno));
87		return -1;
88	}
89
90	if (n != 1) {
91		error("%s: sendmsg: expected sent 1 got %ld",
92		    __func__, (long)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	vec.iov_base = &ch;
121	vec.iov_len = 1;
122	msg.msg_iov = &vec;
123	msg.msg_iovlen = 1;
124	msg.msg_control = &cmsgbuf.buf;
125	msg.msg_controllen = CMSG_SPACE(sizeof(int));
126
127	pfd.fd = sock;
128	pfd.events = POLLIN;
129	while ((n = recvmsg(sock, &msg, 0)) == -1 &&
130	    (errno == EAGAIN || errno == EINTR)) {
131		debug3("%s: recvmsg: %s", __func__, strerror(errno));
132		(void)poll(&pfd, 1, -1);
133	}
134	if (n == -1) {
135		error("%s: recvmsg: %s", __func__, strerror(errno));
136		return -1;
137	}
138
139	if (n != 1) {
140		error("%s: recvmsg: expected received 1 got %ld",
141		    __func__, (long)n);
142		return -1;
143	}
144
145	cmsg = CMSG_FIRSTHDR(&msg);
146	if (cmsg == NULL) {
147		error("%s: no message header", __func__);
148		return -1;
149	}
150
151	if (cmsg->cmsg_type != SCM_RIGHTS) {
152		error("%s: expected type %d got %d", __func__,
153		    SCM_RIGHTS, cmsg->cmsg_type);
154		return -1;
155	}
156	fd = (*(int *)CMSG_DATA(cmsg));
157	return fd;
158}
159