xref: /netbsd/tests/net/fdpass/fdpass.c (revision f896ab54)
1*f896ab54Schristos /*	$NetBSD: fdpass.c,v 1.1 2012/08/13 11:15:05 christos Exp $	*/
2*f896ab54Schristos /* $OpenBSD: monitor_fdpass.c,v 1.19 2010/01/12 00:58:25 djm Exp $ */
3*f896ab54Schristos /*
4*f896ab54Schristos  * Copyright 2001 Niels Provos <provos@citi.umich.edu>
5*f896ab54Schristos  * All rights reserved.
6*f896ab54Schristos  *
7*f896ab54Schristos  * Redistribution and use in source and binary forms, with or without
8*f896ab54Schristos  * modification, are permitted provided that the following conditions
9*f896ab54Schristos  * are met:
10*f896ab54Schristos  * 1. Redistributions of source code must retain the above copyright
11*f896ab54Schristos  *    notice, this list of conditions and the following disclaimer.
12*f896ab54Schristos  * 2. Redistributions in binary form must reproduce the above copyright
13*f896ab54Schristos  *    notice, this list of conditions and the following disclaimer in the
14*f896ab54Schristos  *    documentation and/or other materials provided with the distribution.
15*f896ab54Schristos  *
16*f896ab54Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*f896ab54Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*f896ab54Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*f896ab54Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*f896ab54Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21*f896ab54Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*f896ab54Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*f896ab54Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*f896ab54Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25*f896ab54Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*f896ab54Schristos  */
27*f896ab54Schristos 
28*f896ab54Schristos #include <sys/cdefs.h>
29*f896ab54Schristos __RCSID("$NetBSD: fdpass.c,v 1.1 2012/08/13 11:15:05 christos Exp $");
30*f896ab54Schristos #include <sys/types.h>
31*f896ab54Schristos #include <sys/socket.h>
32*f896ab54Schristos #include <sys/uio.h>
33*f896ab54Schristos #include <sys/wait.h>
34*f896ab54Schristos 
35*f896ab54Schristos #include <stdio.h>
36*f896ab54Schristos #include <errno.h>
37*f896ab54Schristos #include <err.h>
38*f896ab54Schristos #include <stdlib.h>
39*f896ab54Schristos #include <unistd.h>
40*f896ab54Schristos #include <fcntl.h>
41*f896ab54Schristos #include <poll.h>
42*f896ab54Schristos #include <string.h>
43*f896ab54Schristos 
44*f896ab54Schristos static int debug;
45*f896ab54Schristos 
46*f896ab54Schristos static int
send_fd(int sock,int fd)47*f896ab54Schristos send_fd(int sock, int fd)
48*f896ab54Schristos {
49*f896ab54Schristos 	struct msghdr msg;
50*f896ab54Schristos 	union {
51*f896ab54Schristos 		struct cmsghdr hdr;
52*f896ab54Schristos 		char buf[1024];
53*f896ab54Schristos 	} cmsgbuf;
54*f896ab54Schristos 	struct cmsghdr *cmsg;
55*f896ab54Schristos 	struct iovec vec;
56*f896ab54Schristos 	char ch = '\0';
57*f896ab54Schristos 	ssize_t n;
58*f896ab54Schristos 	struct pollfd pfd;
59*f896ab54Schristos 
60*f896ab54Schristos 	if (sizeof(cmsgbuf.buf) < CMSG_SPACE(sizeof(int)))
61*f896ab54Schristos 		errx(1, "%s: %zu < %zu, recompile", __func__,
62*f896ab54Schristos 		    sizeof(cmsgbuf.buf), CMSG_SPACE(sizeof(int)));
63*f896ab54Schristos 
64*f896ab54Schristos 	memset(&msg, 0, sizeof(msg));
65*f896ab54Schristos 	msg.msg_control = &cmsgbuf.buf;
66*f896ab54Schristos 	msg.msg_controllen = CMSG_SPACE(sizeof(int));
67*f896ab54Schristos 	cmsg = CMSG_FIRSTHDR(&msg);
68*f896ab54Schristos 	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
69*f896ab54Schristos 	cmsg->cmsg_level = SOL_SOCKET;
70*f896ab54Schristos 	cmsg->cmsg_type = SCM_RIGHTS;
71*f896ab54Schristos 	*(int *)CMSG_DATA(cmsg) = fd;
72*f896ab54Schristos 	msg.msg_controllen = cmsg->cmsg_len;
73*f896ab54Schristos 
74*f896ab54Schristos 	vec.iov_base = &ch;
75*f896ab54Schristos 	vec.iov_len = 1;
76*f896ab54Schristos 	msg.msg_iov = &vec;
77*f896ab54Schristos 	msg.msg_iovlen = 1;
78*f896ab54Schristos 
79*f896ab54Schristos 	pfd.fd = sock;
80*f896ab54Schristos 	pfd.events = POLLOUT;
81*f896ab54Schristos 	while ((n = sendmsg(sock, &msg, 0)) == -1 &&
82*f896ab54Schristos 	    (errno == EAGAIN || errno == EINTR)) {
83*f896ab54Schristos 		(void)poll(&pfd, 1, -1);
84*f896ab54Schristos 	}
85*f896ab54Schristos 	switch (n) {
86*f896ab54Schristos 	case -1:
87*f896ab54Schristos 		err(1, "%s: sendmsg(%d)", __func__, fd);
88*f896ab54Schristos 	case 1:
89*f896ab54Schristos 		if (debug)
90*f896ab54Schristos 			fprintf(stderr, "%d: send fd %d\n", getpid(), fd);
91*f896ab54Schristos 		return 0;
92*f896ab54Schristos 	default:
93*f896ab54Schristos 		errx(1, "%s: sendmsg: expected sent 1 got %ld",
94*f896ab54Schristos 		    __func__, (long)n);
95*f896ab54Schristos 	}
96*f896ab54Schristos }
97*f896ab54Schristos 
98*f896ab54Schristos static int
recv_fd(int sock)99*f896ab54Schristos recv_fd(int sock)
100*f896ab54Schristos {
101*f896ab54Schristos 	struct msghdr msg;
102*f896ab54Schristos 	union {
103*f896ab54Schristos 		struct cmsghdr hdr;
104*f896ab54Schristos 		char buf[1024];
105*f896ab54Schristos 	} cmsgbuf;
106*f896ab54Schristos 	struct cmsghdr *cmsg;
107*f896ab54Schristos 	struct iovec vec;
108*f896ab54Schristos 	ssize_t n;
109*f896ab54Schristos 	char ch;
110*f896ab54Schristos 	int fd;
111*f896ab54Schristos 	struct pollfd pfd;
112*f896ab54Schristos 
113*f896ab54Schristos 	if (sizeof(cmsgbuf.buf) < CMSG_SPACE(sizeof(int)))
114*f896ab54Schristos 		errx(1, "%s: %zu < %zu, recompile", __func__,
115*f896ab54Schristos 		    sizeof(cmsgbuf.buf), CMSG_SPACE(sizeof(int)));
116*f896ab54Schristos 
117*f896ab54Schristos 	memset(&msg, 0, sizeof(msg));
118*f896ab54Schristos 	vec.iov_base = &ch;
119*f896ab54Schristos 	vec.iov_len = 1;
120*f896ab54Schristos 	msg.msg_iov = &vec;
121*f896ab54Schristos 	msg.msg_iovlen = 1;
122*f896ab54Schristos 	msg.msg_control = &cmsgbuf.buf;
123*f896ab54Schristos 	msg.msg_controllen = CMSG_SPACE(sizeof(int));
124*f896ab54Schristos 
125*f896ab54Schristos 	pfd.fd = sock;
126*f896ab54Schristos 	pfd.events = POLLIN;
127*f896ab54Schristos 	while ((n = recvmsg(sock, &msg, 0)) == -1 &&
128*f896ab54Schristos 	    (errno == EAGAIN || errno == EINTR)) {
129*f896ab54Schristos 		(void)poll(&pfd, 1, -1);
130*f896ab54Schristos 	}
131*f896ab54Schristos 	switch (n) {
132*f896ab54Schristos 	case -1:
133*f896ab54Schristos 		err(1, "%s: recvmsg", __func__);
134*f896ab54Schristos 	case 1:
135*f896ab54Schristos 		break;
136*f896ab54Schristos 	default:
137*f896ab54Schristos 		errx(1, "%s: recvmsg: expected received 1 got %ld",
138*f896ab54Schristos 		    __func__, (long)n);
139*f896ab54Schristos 	}
140*f896ab54Schristos 
141*f896ab54Schristos 	cmsg = CMSG_FIRSTHDR(&msg);
142*f896ab54Schristos 	if (cmsg == NULL)
143*f896ab54Schristos 		errx(1, "%s: no message header", __func__);
144*f896ab54Schristos 
145*f896ab54Schristos 	if (cmsg->cmsg_type != SCM_RIGHTS)
146*f896ab54Schristos 		err(1, "%s: expected type %d got %d", __func__,
147*f896ab54Schristos 		    SCM_RIGHTS, cmsg->cmsg_type);
148*f896ab54Schristos 	fd = (*(int *)CMSG_DATA(cmsg));
149*f896ab54Schristos 	if (debug)
150*f896ab54Schristos 		fprintf(stderr, "%d: recv fd %d\n", getpid(), fd);
151*f896ab54Schristos 	return fd;
152*f896ab54Schristos }
153*f896ab54Schristos 
154*f896ab54Schristos static void usage(void) __attribute__((__noreturn__));
155*f896ab54Schristos 
156*f896ab54Schristos static void
usage(void)157*f896ab54Schristos usage(void)
158*f896ab54Schristos {
159*f896ab54Schristos 	fprintf(stderr, "Usage: %s [-vd] -i <input> -o <output>\n"
160*f896ab54Schristos 	    "\t %s [-v] -p <progname>\n", getprogname(), getprogname());
161*f896ab54Schristos 	exit(EXIT_FAILURE);
162*f896ab54Schristos }
163*f896ab54Schristos 
164*f896ab54Schristos int
main(int argc,char * argv[])165*f896ab54Schristos main(int argc, char *argv[])
166*f896ab54Schristos {
167*f896ab54Schristos 	int s[2], fd, status, c, verbose;
168*f896ab54Schristos 	char buf[1024], *prog;
169*f896ab54Schristos 
170*f896ab54Schristos 	prog = NULL;
171*f896ab54Schristos 	s[0] = s[1] = -1;
172*f896ab54Schristos 	verbose = 0;
173*f896ab54Schristos 
174*f896ab54Schristos 	while ((c = getopt(argc, argv, "di:o:p:")) != -1)
175*f896ab54Schristos 		switch (c) {
176*f896ab54Schristos 		case 'd':
177*f896ab54Schristos 			debug++;
178*f896ab54Schristos 			break;
179*f896ab54Schristos 		case 'i':
180*f896ab54Schristos 			s[0] = atoi(optarg);
181*f896ab54Schristos 			break;
182*f896ab54Schristos 		case 'o':
183*f896ab54Schristos 			s[1] = atoi(optarg);
184*f896ab54Schristos 			break;
185*f896ab54Schristos 		case 'p':
186*f896ab54Schristos 			prog = optarg;
187*f896ab54Schristos 			break;
188*f896ab54Schristos 		default:
189*f896ab54Schristos 			usage();
190*f896ab54Schristos 		}
191*f896ab54Schristos 
192*f896ab54Schristos 	if ((s[0] == -1 && s[1] != -1) || (s[0] != -1 && s[1] == -1))
193*f896ab54Schristos 		usage();
194*f896ab54Schristos 
195*f896ab54Schristos 	if (s[0] == -1) {
196*f896ab54Schristos 		if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, s) == -1)
197*f896ab54Schristos 			err(1, "socketpair");
198*f896ab54Schristos 	} else
199*f896ab54Schristos 		goto recv;
200*f896ab54Schristos 
201*f896ab54Schristos 	switch (fork()) {
202*f896ab54Schristos 	case -1:
203*f896ab54Schristos 		err(1, "fork");
204*f896ab54Schristos 	default:
205*f896ab54Schristos 		fd = open("foo", O_RDWR|O_CREAT|O_TRUNC, 0666);
206*f896ab54Schristos 		if (fd == -1)
207*f896ab54Schristos 			err(1, "open");
208*f896ab54Schristos 		send_fd(s[0], fd);
209*f896ab54Schristos 		wait(&status);
210*f896ab54Schristos 		return 0;
211*f896ab54Schristos 	case 0:
212*f896ab54Schristos 		if (prog != NULL) {
213*f896ab54Schristos 			char i[64], o[64];
214*f896ab54Schristos 			snprintf(i, sizeof(i), "%d", s[0]);
215*f896ab54Schristos 			snprintf(o, sizeof(o), "%d", s[1]);
216*f896ab54Schristos 			execlp(prog, prog, "-i", i, "-o", o, NULL);
217*f896ab54Schristos 			err(1, "execlp");
218*f896ab54Schristos 		}
219*f896ab54Schristos 	recv:
220*f896ab54Schristos 		fd = recv_fd(s[1]);
221*f896ab54Schristos 		if (verbose) {
222*f896ab54Schristos 			snprintf(buf, sizeof(buf), "ls -l /proc/%d/fd",
223*f896ab54Schristos 			    getpid());
224*f896ab54Schristos 			system(buf);
225*f896ab54Schristos 		}
226*f896ab54Schristos 		if (write(fd, "foo\n", 4) == -1)
227*f896ab54Schristos 			err(1, "write");
228*f896ab54Schristos 		close(fd);
229*f896ab54Schristos 		return 0;
230*f896ab54Schristos 	}
231*f896ab54Schristos }
232