Home | History | Annotate | Line # | Download | only in unfdpass
unfdpass.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: unfdpass.c,v 1.1 1998/01/07 03:53:02 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1  thorpej  * NASA Ames Research Center.
     10  1.1  thorpej  *
     11  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.1  thorpej  * modification, are permitted provided that the following conditions
     13  1.1  thorpej  * are met:
     14  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.1  thorpej  *    must display the following acknowledgement:
     21  1.1  thorpej  *	This product includes software developed by the NetBSD
     22  1.1  thorpej  *	Foundation, Inc. and its contributors.
     23  1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1  thorpej  *    contributors may be used to endorse or promote products derived
     25  1.1  thorpej  *    from this software without specific prior written permission.
     26  1.1  thorpej  *
     27  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1  thorpej  */
     39  1.1  thorpej 
     40  1.1  thorpej /*
     41  1.1  thorpej  * Test passing of file descriptors over Unix domain sockets.
     42  1.1  thorpej  */
     43  1.1  thorpej 
     44  1.1  thorpej #include <sys/param.h>
     45  1.1  thorpej #include <sys/socket.h>
     46  1.1  thorpej #include <sys/time.h>
     47  1.1  thorpej #include <sys/wait.h>
     48  1.1  thorpej #include <sys/un.h>
     49  1.1  thorpej #include <err.h>
     50  1.1  thorpej #include <fcntl.h>
     51  1.1  thorpej #include <stdio.h>
     52  1.1  thorpej #include <string.h>
     53  1.1  thorpej #include <unistd.h>
     54  1.1  thorpej 
     55  1.1  thorpej int	sock[2];
     56  1.1  thorpej 
     57  1.1  thorpej int	main __P((int, char *[]));
     58  1.1  thorpej void	child __P((void));
     59  1.1  thorpej 
     60  1.1  thorpej struct cmessage {
     61  1.1  thorpej 	struct cmsghdr cm;
     62  1.1  thorpej 	int files[2];
     63  1.1  thorpej };
     64  1.1  thorpej 
     65  1.1  thorpej /* ARGSUSED */
     66  1.1  thorpej int
     67  1.1  thorpej main(argc, argv)
     68  1.1  thorpej 	int argc;
     69  1.1  thorpej 	char *argv[];
     70  1.1  thorpej {
     71  1.1  thorpej 	struct msghdr msg;
     72  1.1  thorpej 	int fd, i, status;
     73  1.1  thorpej 	char fname[16], buf[64];
     74  1.1  thorpej 	pid_t pid;
     75  1.1  thorpej 	struct cmsghdr *cmp;
     76  1.1  thorpej 	struct cmessage cm;
     77  1.1  thorpej 
     78  1.1  thorpej 	/*
     79  1.1  thorpej 	 * Create the test files.
     80  1.1  thorpej 	 */
     81  1.1  thorpej 	for (i = 0; i < 2; i++) {
     82  1.1  thorpej 		(void) sprintf(fname, "file%d", i + 1);
     83  1.1  thorpej 		if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
     84  1.1  thorpej 			err(1, "open %s", fname);
     85  1.1  thorpej 		(void) sprintf(buf, "This is file %d.\n", i + 1);
     86  1.1  thorpej 		if (write(fd, buf, strlen(buf)) != strlen(buf))
     87  1.1  thorpej 			err(1, "write %s", fname);
     88  1.1  thorpej 		(void) close(fd);
     89  1.1  thorpej 	}
     90  1.1  thorpej 
     91  1.1  thorpej 	/*
     92  1.1  thorpej 	 * Create the socket pair to pass the descriptors over.
     93  1.1  thorpej 	 */
     94  1.1  thorpej 	if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sock) == -1)
     95  1.1  thorpej 		err(1, "socketpair");
     96  1.1  thorpej 
     97  1.1  thorpej 	pid = fork();
     98  1.1  thorpej 	switch (pid) {
     99  1.1  thorpej 	case -1:
    100  1.1  thorpej 		err(1, "fork");
    101  1.1  thorpej 		/* NOTREACHED */
    102  1.1  thorpej 
    103  1.1  thorpej 	case 0:
    104  1.1  thorpej 		child();
    105  1.1  thorpej 		/* NOTREACHED */
    106  1.1  thorpej 	}
    107  1.1  thorpej 
    108  1.1  thorpej 	(void) close(sock[1]);
    109  1.1  thorpej 
    110  1.1  thorpej 	/*
    111  1.1  thorpej 	 * Open the files again, and pass them to the child over the socket.
    112  1.1  thorpej 	 */
    113  1.1  thorpej 	for (i = 0; i < 2; i++) {
    114  1.1  thorpej 		(void) sprintf(fname, "file%d", i + 1);
    115  1.1  thorpej 		if ((fd = open(fname, O_RDONLY, 0666)) == -1)
    116  1.1  thorpej 			err(1, "open %s", fname);
    117  1.1  thorpej 		cm.files[i] = fd;
    118  1.1  thorpej 	}
    119  1.1  thorpej 
    120  1.1  thorpej 	(void) memset(&msg, 0, sizeof(msg));
    121  1.1  thorpej 	msg.msg_control = (caddr_t) &cm;
    122  1.1  thorpej 	msg.msg_controllen = sizeof(cm);
    123  1.1  thorpej 
    124  1.1  thorpej 	cmp = CMSG_FIRSTHDR(&msg);
    125  1.1  thorpej 	cmp->cmsg_len = sizeof(cm);
    126  1.1  thorpej 	cmp->cmsg_level = SOL_SOCKET;
    127  1.1  thorpej 	cmp->cmsg_type = SCM_RIGHTS;
    128  1.1  thorpej 
    129  1.1  thorpej 	if (sendmsg(sock[0], &msg, 0)) {
    130  1.1  thorpej 		warn("sendmsg");
    131  1.1  thorpej 		(void) kill(pid, SIGINT);
    132  1.1  thorpej 	}
    133  1.1  thorpej 
    134  1.1  thorpej 	/* Wait for the child to exit. */
    135  1.1  thorpej 	if (waitpid(pid, &status, 0) == -1)
    136  1.1  thorpej 		err(1, "waitpid");
    137  1.1  thorpej 
    138  1.1  thorpej 	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
    139  1.1  thorpej 		errx(1, "child exited with status of %d\n",
    140  1.1  thorpej 		    WEXITSTATUS(status));
    141  1.1  thorpej 
    142  1.1  thorpej 	exit(0);
    143  1.1  thorpej }
    144  1.1  thorpej 
    145  1.1  thorpej void
    146  1.1  thorpej child()
    147  1.1  thorpej {
    148  1.1  thorpej 	struct msghdr msg;
    149  1.1  thorpej 	char buf[64];
    150  1.1  thorpej 	struct cmsghdr *cmp;
    151  1.1  thorpej 	struct cmessage cm;
    152  1.1  thorpej 	int i;
    153  1.1  thorpej 
    154  1.1  thorpej 	/*
    155  1.1  thorpej 	 * Close the socket we're not using.
    156  1.1  thorpej 	 */
    157  1.1  thorpej 	(void) close(sock[0]);
    158  1.1  thorpej 
    159  1.1  thorpej 	/*
    160  1.1  thorpej 	 * Grab the descriptors passed to us.
    161  1.1  thorpej 	 */
    162  1.1  thorpej 	(void) memset(&msg, 0, sizeof(msg));
    163  1.1  thorpej 	msg.msg_control = (caddr_t) &cm;
    164  1.1  thorpej 	msg.msg_controllen = sizeof(cm);
    165  1.1  thorpej 
    166  1.1  thorpej 	if (recvmsg(sock[1], &msg, 0) < 0)
    167  1.1  thorpej 		err(1, "recvmsg");
    168  1.1  thorpej 
    169  1.1  thorpej 	cmp = CMSG_FIRSTHDR(&msg);
    170  1.1  thorpej 
    171  1.1  thorpej 	if (cmp->cmsg_len != sizeof(cm))
    172  1.1  thorpej 		err(1, "bad control message length");
    173  1.1  thorpej 	if (cmp->cmsg_level != SOL_SOCKET)
    174  1.1  thorpej 		err(1, "bad control message level");
    175  1.1  thorpej 	if (cmp->cmsg_type != SCM_RIGHTS)
    176  1.1  thorpej 		err(1, "bad control message type");
    177  1.1  thorpej 
    178  1.1  thorpej 	/*
    179  1.1  thorpej 	 * Read the files and print their contents.
    180  1.1  thorpej 	 */
    181  1.1  thorpej 	for (i = 0; i < 2; i++) {
    182  1.1  thorpej 		(void) memset(buf, 0, sizeof(buf));
    183  1.1  thorpej 		if (read(cm.files[i], buf, sizeof(buf)) <= 0)
    184  1.1  thorpej 			err(1, "read file %d", i + 1);
    185  1.1  thorpej 		printf("%s", buf);
    186  1.1  thorpej 	}
    187  1.1  thorpej 
    188  1.1  thorpej 	/*
    189  1.1  thorpej 	 * All done!
    190  1.1  thorpej 	 */
    191  1.1  thorpej 	exit(0);
    192  1.1  thorpej }
    193