Home | History | Annotate | Line # | Download | only in fdpass
      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 $ */
      3 /*
      4  * Copyright 2001 Niels Provos <provos (at) 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 <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>
     32 #include <sys/uio.h>
     33 #include <sys/wait.h>
     34 
     35 #include <stdio.h>
     36 #include <errno.h>
     37 #include <err.h>
     38 #include <stdlib.h>
     39 #include <unistd.h>
     40 #include <fcntl.h>
     41 #include <poll.h>
     42 #include <string.h>
     43 
     44 static int debug;
     45 
     46 static int
     47 send_fd(int sock, int fd)
     48 {
     49 	struct msghdr msg;
     50 	union {
     51 		struct cmsghdr hdr;
     52 		char buf[1024];
     53 	} cmsgbuf;
     54 	struct cmsghdr *cmsg;
     55 	struct iovec vec;
     56 	char ch = '\0';
     57 	ssize_t n;
     58 	struct pollfd pfd;
     59 
     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)));
     63 
     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;
     73 
     74 	vec.iov_base = &ch;
     75 	vec.iov_len = 1;
     76 	msg.msg_iov = &vec;
     77 	msg.msg_iovlen = 1;
     78 
     79 	pfd.fd = sock;
     80 	pfd.events = POLLOUT;
     81 	while ((n = sendmsg(sock, &msg, 0)) == -1 &&
     82 	    (errno == EAGAIN || errno == EINTR)) {
     83 		(void)poll(&pfd, 1, -1);
     84 	}
     85 	switch (n) {
     86 	case -1:
     87 		err(1, "%s: sendmsg(%d)", __func__, fd);
     88 	case 1:
     89 		if (debug)
     90 			fprintf(stderr, "%d: send fd %d\n", getpid(), fd);
     91 		return 0;
     92 	default:
     93 		errx(1, "%s: sendmsg: expected sent 1 got %ld",
     94 		    __func__, (long)n);
     95 	}
     96 }
     97 
     98 static int
     99 recv_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 		errx(1, "%s: %zu < %zu, recompile", __func__,
    115 		    sizeof(cmsgbuf.buf), CMSG_SPACE(sizeof(int)));
    116 
    117 	memset(&msg, 0, sizeof(msg));
    118 	vec.iov_base = &ch;
    119 	vec.iov_len = 1;
    120 	msg.msg_iov = &vec;
    121 	msg.msg_iovlen = 1;
    122 	msg.msg_control = &cmsgbuf.buf;
    123 	msg.msg_controllen = CMSG_SPACE(sizeof(int));
    124 
    125 	pfd.fd = sock;
    126 	pfd.events = POLLIN;
    127 	while ((n = recvmsg(sock, &msg, 0)) == -1 &&
    128 	    (errno == EAGAIN || errno == EINTR)) {
    129 		(void)poll(&pfd, 1, -1);
    130 	}
    131 	switch (n) {
    132 	case -1:
    133 		err(1, "%s: recvmsg", __func__);
    134 	case 1:
    135 		break;
    136 	default:
    137 		errx(1, "%s: recvmsg: expected received 1 got %ld",
    138 		    __func__, (long)n);
    139 	}
    140 
    141 	cmsg = CMSG_FIRSTHDR(&msg);
    142 	if (cmsg == NULL)
    143 		errx(1, "%s: no message header", __func__);
    144 
    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));
    149 	if (debug)
    150 		fprintf(stderr, "%d: recv fd %d\n", getpid(), fd);
    151 	return fd;
    152 }
    153 
    154 static void usage(void) __attribute__((__noreturn__));
    155 
    156 static void
    157 usage(void)
    158 {
    159 	fprintf(stderr, "Usage: %s [-vd] -i <input> -o <output>\n"
    160 	    "\t %s [-v] -p <progname>\n", getprogname(), getprogname());
    161 	exit(EXIT_FAILURE);
    162 }
    163 
    164 int
    165 main(int argc, char *argv[])
    166 {
    167 	int s[2], fd, status, c, verbose;
    168 	char buf[1024], *prog;
    169 
    170 	prog = NULL;
    171 	s[0] = s[1] = -1;
    172 	verbose = 0;
    173 
    174 	while ((c = getopt(argc, argv, "di:o:p:")) != -1)
    175 		switch (c) {
    176 		case 'd':
    177 			debug++;
    178 			break;
    179 		case 'i':
    180 			s[0] = atoi(optarg);
    181 			break;
    182 		case 'o':
    183 			s[1] = atoi(optarg);
    184 			break;
    185 		case 'p':
    186 			prog = optarg;
    187 			break;
    188 		default:
    189 			usage();
    190 		}
    191 
    192 	if ((s[0] == -1 && s[1] != -1) || (s[0] != -1 && s[1] == -1))
    193 		usage();
    194 
    195 	if (s[0] == -1) {
    196 		if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, s) == -1)
    197 			err(1, "socketpair");
    198 	} else
    199 		goto recv;
    200 
    201 	switch (fork()) {
    202 	case -1:
    203 		err(1, "fork");
    204 	default:
    205 		fd = open("foo", O_RDWR|O_CREAT|O_TRUNC, 0666);
    206 		if (fd == -1)
    207 			err(1, "open");
    208 		send_fd(s[0], fd);
    209 		wait(&status);
    210 		return 0;
    211 	case 0:
    212 		if (prog != NULL) {
    213 			char i[64], o[64];
    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);
    217 			err(1, "execlp");
    218 		}
    219 	recv:
    220 		fd = recv_fd(s[1]);
    221 		if (verbose) {
    222 			snprintf(buf, sizeof(buf), "ls -l /proc/%d/fd",
    223 			    getpid());
    224 			system(buf);
    225 		}
    226 		if (write(fd, "foo\n", 4) == -1)
    227 			err(1, "write");
    228 		close(fd);
    229 		return 0;
    230 	}
    231 }
    232