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