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