1 /* $NetBSD: fdpass.c,v 1.1 2012/08/13 11:15:05 christos Exp $ */
2 /* $OpenBSD: monitor_fdpass.c,v 1.19 2010/01/12 00:58:25 djm Exp $ */
4 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: fdpass.c,v 1.1 2012/08/13 11:15:05 christos Exp $");
30 #include <sys/types.h>
31 #include <sys/socket.h>
47 send_fd(int sock, int fd)
60 if (sizeof(cmsgbuf.buf) < CMSG_SPACE(sizeof(int)))
61 errx(1, "%s: %zu < %zu, recompile", __func__,
62 sizeof(cmsgbuf.buf), CMSG_SPACE(sizeof(int)));
64 memset(&msg, 0, sizeof(msg));
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;
81 while ((n = sendmsg(sock, &msg, 0)) == -1 &&
82 (errno == EAGAIN || errno == EINTR)) {
83 (void)poll(&pfd, 1, -1);
87 err(1, "%s: sendmsg(%d)", __func__, fd);
90 fprintf(stderr, "%d: send fd %d\n", getpid(), fd);
93 errx(1, "%s: sendmsg: expected sent 1 got %ld",
106 struct cmsghdr *cmsg;
113 if (sizeof(cmsgbuf.buf) < CMSG_SPACE(sizeof(int)))
114 errx(1, "%s: %zu < %zu, recompile", __func__,
115 sizeof(cmsgbuf.buf), CMSG_SPACE(sizeof(int)));
117 memset(&msg, 0, sizeof(msg));
122 msg.msg_control = &cmsgbuf.buf;
123 msg.msg_controllen = CMSG_SPACE(sizeof(int));
127 while ((n = recvmsg(sock, &msg, 0)) == -1 &&
128 (errno == EAGAIN || errno == EINTR)) {
129 (void)poll(&pfd, 1, -1);
133 err(1, "%s: recvmsg", __func__);
137 errx(1, "%s: recvmsg: expected received 1 got %ld",
141 cmsg = CMSG_FIRSTHDR(&msg);
143 errx(1, "%s: no message header", __func__);
145 if (cmsg->cmsg_type != SCM_RIGHTS)
146 err(1, "%s: expected type %d got %d", __func__,
147 SCM_RIGHTS, cmsg->cmsg_type);
148 fd = (*(int *)CMSG_DATA(cmsg));
150 fprintf(stderr, "%d: recv fd %d\n", getpid(), fd);
154 static void usage(void) __attribute__((__noreturn__));
159 fprintf(stderr, "Usage: %s [-vd] -i <input> -o <output>\n"
160 "\t %s [-v] -p <progname>\n", getprogname(), getprogname());
165 main(int argc, char *argv[])
167 int s[2], fd, status, c, verbose;
168 char buf[1024], *prog;
174 while ((c = getopt(argc, argv, "di:o:p:")) != -1)
192 if ((s[0] == -1 && s[1] != -1) || (s[0] != -1 && s[1] == -1))
196 if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, s) == -1)
197 err(1, "socketpair");
205 fd = open("foo", O_RDWR|O_CREAT|O_TRUNC, 0666);
214 snprintf(i, sizeof(i), "%d", s[0]);
215 snprintf(o, sizeof(o), "%d", s[1]);
216 execlp(prog, prog, "-i", i, "-o", o, NULL);
222 snprintf(buf, sizeof(buf), "ls -l /proc/%d/fd",
226 if (write(fd, "foo\n", 4) == -1)