Home | History | Annotate | Line # | Download | only in unfdpass
unfdpass.c revision 1.8.16.1
      1  1.8.16.1   keiichi /*	$NetBSD: unfdpass.c,v 1.8.16.1 2008/03/24 07:14:47 keiichi 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.2   thorpej  * Test passing of file descriptors and credentials 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.4   mycroft #include <sys/uio.h>
     50       1.4   mycroft 
     51       1.1   thorpej #include <err.h>
     52       1.2   thorpej #include <errno.h>
     53       1.1   thorpej #include <fcntl.h>
     54       1.2   thorpej #include <signal.h>
     55       1.1   thorpej #include <stdio.h>
     56       1.1   thorpej #include <string.h>
     57       1.6   thorpej #include <stdlib.h>
     58       1.1   thorpej #include <unistd.h>
     59       1.1   thorpej 
     60       1.2   thorpej #define	SOCK_NAME	"test-sock"
     61       1.1   thorpej 
     62       1.7     perry int	main(int, char *[]);
     63       1.7     perry void	child(void);
     64       1.7     perry void	catch_sigchld(int);
     65       1.7     perry void	usage(char *progname);
     66       1.1   thorpej 
     67       1.4   mycroft #define	FILE_SIZE	128
     68       1.4   mycroft #define	MSG_SIZE	-1
     69       1.4   mycroft #define	NFILES		24
     70       1.4   mycroft 
     71       1.6   thorpej #define	FDCM_DATASIZE	(sizeof(int) * NFILES)
     72       1.6   thorpej #define	CRCM_DATASIZE	(SOCKCREDSIZE(NGROUPS))
     73       1.6   thorpej 
     74       1.6   thorpej #define	MESSAGE_SIZE	(CMSG_SPACE(FDCM_DATASIZE) +			\
     75       1.6   thorpej 			 CMSG_SPACE(CRCM_DATASIZE))
     76       1.2   thorpej 
     77       1.5  sommerfe int chroot_rcvr = 0;
     78       1.5  sommerfe int pass_dir = 0;
     79       1.5  sommerfe int pass_root_dir = 0;
     80       1.5  sommerfe int exit_early = 0;
     81       1.5  sommerfe int exit_later = 0;
     82       1.5  sommerfe int pass_sock = 0;
     83       1.5  sommerfe int make_pretzel = 0;
     84       1.5  sommerfe 
     85       1.1   thorpej /* ARGSUSED */
     86       1.1   thorpej int
     87       1.1   thorpej main(argc, argv)
     88       1.1   thorpej 	int argc;
     89       1.1   thorpej 	char *argv[];
     90       1.1   thorpej {
     91       1.4   mycroft #if MSG_SIZE >= 0
     92       1.4   mycroft 	struct iovec iov;
     93       1.4   mycroft #endif
     94       1.5  sommerfe 	char *progname=argv[0];
     95       1.1   thorpej 	struct msghdr msg;
     96       1.6   thorpej 	int listensock, sock, fd, i;
     97       1.4   mycroft 	char fname[16], buf[FILE_SIZE];
     98       1.2   thorpej 	struct cmsghdr *cmp;
     99       1.6   thorpej 	void *message;
    100       1.2   thorpej 	int *files = NULL;
    101       1.2   thorpej 	struct sockcred *sc = NULL;
    102       1.2   thorpej 	struct sockaddr_un sun, csun;
    103       1.8       mrg 	socklen_t csunlen;
    104       1.1   thorpej 	pid_t pid;
    105       1.5  sommerfe 	int ch;
    106       1.6   thorpej 
    107       1.6   thorpej 	message = malloc(CMSG_SPACE(MESSAGE_SIZE));
    108       1.6   thorpej 	if (message == NULL)
    109       1.6   thorpej 		err(1, "unable to malloc message buffer");
    110       1.6   thorpej 	memset(message, 0, CMSG_SPACE(MESSAGE_SIZE));
    111       1.5  sommerfe 
    112       1.5  sommerfe 	while ((ch = getopt(argc, argv, "DESdepr")) != -1) {
    113       1.5  sommerfe 		switch(ch) {
    114       1.5  sommerfe 
    115       1.5  sommerfe 		case 'e':
    116       1.5  sommerfe 			exit_early++; /* test early GC */
    117       1.5  sommerfe 			break;
    118       1.5  sommerfe 
    119       1.5  sommerfe 		case 'E':
    120       1.5  sommerfe 			exit_later++; /* test later GC */
    121       1.5  sommerfe 			break;
    122       1.5  sommerfe 
    123       1.5  sommerfe 		case 'd':
    124       1.5  sommerfe 			pass_dir++;
    125       1.5  sommerfe 			break;
    126       1.5  sommerfe 
    127       1.5  sommerfe 		case 'D':
    128       1.5  sommerfe 			pass_dir++;
    129       1.5  sommerfe 			pass_root_dir++;
    130       1.5  sommerfe 			break;
    131       1.5  sommerfe 
    132       1.5  sommerfe 		case 'S':
    133       1.5  sommerfe 			pass_sock++;
    134       1.5  sommerfe 			break;
    135       1.5  sommerfe 
    136       1.5  sommerfe 		case 'r':
    137       1.5  sommerfe 			chroot_rcvr++;
    138       1.5  sommerfe 			break;
    139       1.5  sommerfe 
    140       1.5  sommerfe 		case 'p':
    141       1.5  sommerfe 			make_pretzel++;
    142       1.5  sommerfe 			break;
    143       1.5  sommerfe 
    144       1.5  sommerfe 		case '?':
    145       1.5  sommerfe 		default:
    146       1.5  sommerfe 			usage(progname);
    147       1.5  sommerfe 		}
    148       1.5  sommerfe 	}
    149       1.5  sommerfe 
    150       1.1   thorpej 
    151       1.1   thorpej 	/*
    152       1.1   thorpej 	 * Create the test files.
    153       1.1   thorpej 	 */
    154       1.4   mycroft 	for (i = 0; i < NFILES; i++) {
    155       1.1   thorpej 		(void) sprintf(fname, "file%d", i + 1);
    156       1.1   thorpej 		if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
    157       1.1   thorpej 			err(1, "open %s", fname);
    158       1.1   thorpej 		(void) sprintf(buf, "This is file %d.\n", i + 1);
    159       1.1   thorpej 		if (write(fd, buf, strlen(buf)) != strlen(buf))
    160       1.1   thorpej 			err(1, "write %s", fname);
    161       1.1   thorpej 		(void) close(fd);
    162       1.1   thorpej 	}
    163       1.1   thorpej 
    164       1.1   thorpej 	/*
    165       1.2   thorpej 	 * Create the listen socket.
    166       1.1   thorpej 	 */
    167       1.3   thorpej 	if ((listensock = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1)
    168       1.2   thorpej 		err(1, "socket");
    169       1.2   thorpej 
    170       1.2   thorpej 	(void) unlink(SOCK_NAME);
    171       1.2   thorpej 	(void) memset(&sun, 0, sizeof(sun));
    172       1.2   thorpej 	sun.sun_family = AF_LOCAL;
    173       1.2   thorpej 	(void) strcpy(sun.sun_path, SOCK_NAME);
    174       1.2   thorpej 	sun.sun_len = SUN_LEN(&sun);
    175       1.2   thorpej 
    176       1.2   thorpej 	i = 1;
    177       1.2   thorpej 	if (setsockopt(listensock, 0, LOCAL_CREDS, &i, sizeof(i)) == -1)
    178       1.2   thorpej 		err(1, "setsockopt");
    179       1.2   thorpej 
    180       1.2   thorpej 	if (bind(listensock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
    181       1.2   thorpej 		err(1, "bind");
    182       1.2   thorpej 
    183       1.2   thorpej 	if (listen(listensock, 1) == -1)
    184       1.2   thorpej 		err(1, "listen");
    185       1.1   thorpej 
    186       1.2   thorpej 	/*
    187       1.2   thorpej 	 * Create the sender.
    188       1.2   thorpej 	 */
    189       1.2   thorpej 	(void) signal(SIGCHLD, catch_sigchld);
    190       1.1   thorpej 	pid = fork();
    191       1.1   thorpej 	switch (pid) {
    192       1.1   thorpej 	case -1:
    193       1.1   thorpej 		err(1, "fork");
    194       1.1   thorpej 		/* NOTREACHED */
    195       1.1   thorpej 
    196       1.1   thorpej 	case 0:
    197       1.1   thorpej 		child();
    198       1.1   thorpej 		/* NOTREACHED */
    199       1.1   thorpej 	}
    200       1.1   thorpej 
    201       1.5  sommerfe 	if (exit_early)
    202       1.5  sommerfe 		exit(0);
    203       1.5  sommerfe 
    204       1.5  sommerfe 	if (chroot_rcvr &&
    205       1.5  sommerfe 	    ((chroot(".") < 0)))
    206       1.5  sommerfe 		err(1, "chroot");
    207       1.5  sommerfe 
    208       1.2   thorpej 	/*
    209       1.2   thorpej 	 * Wait for the sender to connect.
    210       1.2   thorpej 	 */
    211  1.8.16.1   keiichi 	csunlen = sizeof(csun);
    212       1.2   thorpej 	if ((sock = accept(listensock, (struct sockaddr *)&csun,
    213       1.2   thorpej 	    &csunlen)) == -1)
    214       1.2   thorpej 		err(1, "accept");
    215       1.1   thorpej 
    216       1.1   thorpej 	/*
    217       1.2   thorpej 	 * Give sender a chance to run.  We will get going again
    218       1.2   thorpej 	 * once the SIGCHLD arrives.
    219       1.1   thorpej 	 */
    220       1.2   thorpej 	(void) sleep(10);
    221       1.1   thorpej 
    222       1.5  sommerfe 	if (exit_later)
    223       1.5  sommerfe 		exit(0);
    224       1.5  sommerfe 
    225       1.2   thorpej 	/*
    226       1.2   thorpej 	 * Grab the descriptors and credentials passed to us.
    227       1.2   thorpej 	 */
    228       1.4   mycroft 
    229       1.6   thorpej 	/* Expect 2 messages; descriptors and creds. */
    230       1.5  sommerfe 	do {
    231       1.5  sommerfe 		(void) memset(&msg, 0, sizeof(msg));
    232       1.6   thorpej 		msg.msg_control = message;
    233       1.6   thorpej 		msg.msg_controllen = MESSAGE_SIZE;
    234       1.4   mycroft #if MSG_SIZE >= 0
    235       1.5  sommerfe 		iov.iov_base = buf;
    236       1.5  sommerfe 		iov.iov_len = MSG_SIZE;
    237       1.5  sommerfe 		msg.msg_iov = &iov;
    238       1.5  sommerfe 		msg.msg_iovlen = 1;
    239       1.4   mycroft #endif
    240       1.2   thorpej 
    241       1.5  sommerfe 		if (recvmsg(sock, &msg, 0) == -1)
    242       1.5  sommerfe 			err(1, "recvmsg");
    243       1.5  sommerfe 
    244       1.5  sommerfe 		(void) close(sock);
    245       1.5  sommerfe 		sock = -1;
    246       1.2   thorpej 
    247       1.5  sommerfe 		if (msg.msg_controllen == 0)
    248       1.5  sommerfe 			errx(1, "no control messages received");
    249       1.2   thorpej 
    250       1.5  sommerfe 		if (msg.msg_flags & MSG_CTRUNC)
    251       1.5  sommerfe 			errx(1, "lost control message data");
    252       1.2   thorpej 
    253       1.5  sommerfe 		for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL;
    254       1.5  sommerfe 		     cmp = CMSG_NXTHDR(&msg, cmp)) {
    255       1.5  sommerfe 			if (cmp->cmsg_level != SOL_SOCKET)
    256       1.5  sommerfe 				errx(1, "bad control message level %d",
    257       1.5  sommerfe 				    cmp->cmsg_level);
    258       1.1   thorpej 
    259       1.5  sommerfe 			switch (cmp->cmsg_type) {
    260       1.5  sommerfe 			case SCM_RIGHTS:
    261       1.6   thorpej 				if (cmp->cmsg_len != CMSG_LEN(FDCM_DATASIZE))
    262       1.6   thorpej 					errx(1, "bad fd control message "
    263       1.6   thorpej 					    "length %d", cmp->cmsg_len);
    264       1.2   thorpej 
    265       1.5  sommerfe 				files = (int *)CMSG_DATA(cmp);
    266       1.5  sommerfe 				break;
    267       1.2   thorpej 
    268       1.5  sommerfe 			case SCM_CREDS:
    269       1.6   thorpej 				if (cmp->cmsg_len < CMSG_LEN(SOCKCREDSIZE(1)))
    270       1.6   thorpej 					errx(1, "bad cred control message "
    271       1.6   thorpej 					    "length %d", cmp->cmsg_len);
    272       1.2   thorpej 
    273       1.5  sommerfe 				sc = (struct sockcred *)CMSG_DATA(cmp);
    274       1.5  sommerfe 				break;
    275       1.2   thorpej 
    276       1.5  sommerfe 			default:
    277       1.5  sommerfe 				errx(1, "unexpected control message");
    278       1.5  sommerfe 				/* NOTREACHED */
    279       1.5  sommerfe 			}
    280       1.2   thorpej 		}
    281       1.1   thorpej 
    282       1.5  sommerfe 		/*
    283       1.5  sommerfe 		 * Read the files and print their contents.
    284       1.5  sommerfe 		 */
    285       1.5  sommerfe 		if (files == NULL)
    286       1.5  sommerfe 			warnx("didn't get fd control message");
    287       1.5  sommerfe 		else {
    288       1.5  sommerfe 			for (i = 0; i < NFILES; i++) {
    289       1.5  sommerfe 				struct stat st;
    290       1.5  sommerfe 				(void) memset(buf, 0, sizeof(buf));
    291       1.5  sommerfe 				fstat(files[i], &st);
    292       1.5  sommerfe 				if (S_ISDIR(st.st_mode)) {
    293       1.5  sommerfe 					printf("file %d is a directory\n", i+1);
    294       1.5  sommerfe 				} else if (S_ISSOCK(st.st_mode)) {
    295       1.5  sommerfe 					printf("file %d is a socket\n", i+1);
    296       1.5  sommerfe 					sock = files[i];
    297       1.5  sommerfe 				} else {
    298       1.5  sommerfe 					int c;
    299       1.5  sommerfe 					c = read (files[i], buf, sizeof(buf));
    300       1.5  sommerfe 					if (c < 0)
    301       1.5  sommerfe 						err(1, "read file %d", i + 1);
    302       1.5  sommerfe 					else if (c == 0)
    303       1.5  sommerfe 						printf("[eof on %d]\n", i + 1);
    304       1.5  sommerfe 					else
    305       1.5  sommerfe 						printf("%s", buf);
    306       1.5  sommerfe 				}
    307       1.5  sommerfe 			}
    308       1.5  sommerfe 		}
    309       1.5  sommerfe 		/*
    310       1.5  sommerfe 		 * Double-check credentials.
    311       1.5  sommerfe 		 */
    312       1.5  sommerfe 		if (sc == NULL)
    313       1.5  sommerfe 			warnx("didn't get cred control message");
    314       1.5  sommerfe 		else {
    315       1.5  sommerfe 			if (sc->sc_uid == getuid() &&
    316       1.5  sommerfe 			    sc->sc_euid == geteuid() &&
    317       1.5  sommerfe 			    sc->sc_gid == getgid() &&
    318       1.5  sommerfe 			    sc->sc_egid == getegid())
    319       1.5  sommerfe 				printf("Credentials match.\n");
    320       1.5  sommerfe 			else
    321       1.5  sommerfe 				printf("Credentials do NOT match.\n");
    322       1.2   thorpej 		}
    323       1.5  sommerfe 	} while (sock != -1);
    324       1.1   thorpej 
    325       1.2   thorpej 	/*
    326       1.2   thorpej 	 * All done!
    327       1.2   thorpej 	 */
    328       1.1   thorpej 	exit(0);
    329       1.1   thorpej }
    330       1.1   thorpej 
    331       1.1   thorpej void
    332       1.5  sommerfe usage(progname)
    333       1.5  sommerfe 	char *progname;
    334       1.5  sommerfe {
    335       1.5  sommerfe 	fprintf(stderr, "usage: %s [-derDES]\n", progname);
    336       1.5  sommerfe 	exit(1);
    337       1.5  sommerfe }
    338       1.5  sommerfe 
    339       1.5  sommerfe void
    340       1.2   thorpej catch_sigchld(sig)
    341       1.2   thorpej 	int sig;
    342       1.2   thorpej {
    343       1.2   thorpej 	int status;
    344       1.2   thorpej 
    345       1.2   thorpej 	(void) wait(&status);
    346       1.2   thorpej }
    347       1.2   thorpej 
    348       1.2   thorpej void
    349       1.1   thorpej child()
    350       1.1   thorpej {
    351       1.4   mycroft #if MSG_SIZE >= 0
    352       1.4   mycroft 	struct iovec iov;
    353       1.4   mycroft #endif
    354       1.1   thorpej 	struct msghdr msg;
    355       1.6   thorpej 	char fname[16];
    356       1.1   thorpej 	struct cmsghdr *cmp;
    357       1.6   thorpej 	void *fdcm;
    358       1.6   thorpej 	int i, fd, sock, nfd, *files;
    359       1.2   thorpej 	struct sockaddr_un sun;
    360       1.5  sommerfe 	int spair[2];
    361       1.6   thorpej 
    362       1.6   thorpej 	fdcm = malloc(CMSG_SPACE(FDCM_DATASIZE));
    363       1.6   thorpej 	if (fdcm == NULL)
    364       1.6   thorpej 		err(1, "unable to malloc fd control message");
    365       1.6   thorpej 	memset(fdcm, 0, CMSG_SPACE(FDCM_DATASIZE));
    366       1.6   thorpej 
    367       1.6   thorpej 	cmp = fdcm;
    368       1.6   thorpej 	files = (int *)CMSG_DATA(fdcm);
    369       1.6   thorpej 
    370       1.1   thorpej 	/*
    371       1.2   thorpej 	 * Create socket and connect to the receiver.
    372       1.1   thorpej 	 */
    373       1.3   thorpej 	if ((sock = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1)
    374       1.2   thorpej 		errx(1, "child socket");
    375       1.2   thorpej 
    376       1.2   thorpej 	(void) memset(&sun, 0, sizeof(sun));
    377       1.2   thorpej 	sun.sun_family = AF_LOCAL;
    378       1.2   thorpej 	(void) strcpy(sun.sun_path, SOCK_NAME);
    379       1.2   thorpej 	sun.sun_len = SUN_LEN(&sun);
    380       1.2   thorpej 
    381       1.2   thorpej 	if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
    382       1.2   thorpej 		err(1, "child connect");
    383       1.1   thorpej 
    384       1.5  sommerfe 	nfd = NFILES;
    385       1.5  sommerfe 	i = 0;
    386       1.5  sommerfe 
    387       1.5  sommerfe 	if (pass_sock) {
    388       1.6   thorpej 		files[i++] = sock;
    389       1.5  sommerfe 	}
    390       1.5  sommerfe 
    391       1.5  sommerfe 	if (pass_dir)
    392       1.5  sommerfe 		nfd--;
    393       1.5  sommerfe 
    394       1.1   thorpej 	/*
    395       1.5  sommerfe 	 * Open the files again, and pass them to the child
    396       1.5  sommerfe 	 * over the socket.
    397       1.1   thorpej 	 */
    398       1.5  sommerfe 
    399       1.5  sommerfe 	for (; i < nfd; i++) {
    400       1.2   thorpej 		(void) sprintf(fname, "file%d", i + 1);
    401       1.2   thorpej 		if ((fd = open(fname, O_RDONLY, 0666)) == -1)
    402       1.2   thorpej 			err(1, "child open %s", fname);
    403       1.6   thorpej 		files[i] = fd;
    404       1.2   thorpej 	}
    405       1.5  sommerfe 
    406       1.5  sommerfe 	if (pass_dir) {
    407       1.5  sommerfe 		char *dirname = pass_root_dir ? "/" : ".";
    408       1.5  sommerfe 
    409       1.5  sommerfe 
    410       1.5  sommerfe 		if ((fd = open(dirname, O_RDONLY, 0)) == -1) {
    411       1.5  sommerfe 			err(1, "child open directory %s", dirname);
    412       1.5  sommerfe 		}
    413       1.6   thorpej 		files[i] = fd;
    414       1.5  sommerfe 	}
    415       1.5  sommerfe 
    416       1.1   thorpej 	(void) memset(&msg, 0, sizeof(msg));
    417       1.6   thorpej 	msg.msg_control = fdcm;
    418       1.6   thorpej 	msg.msg_controllen = CMSG_LEN(FDCM_DATASIZE);
    419       1.4   mycroft #if MSG_SIZE >= 0
    420       1.4   mycroft 	iov.iov_base = buf;
    421       1.4   mycroft 	iov.iov_len = MSG_SIZE;
    422       1.4   mycroft 	msg.msg_iov = &iov;
    423       1.4   mycroft 	msg.msg_iovlen = 1;
    424       1.4   mycroft #endif
    425       1.1   thorpej 
    426       1.1   thorpej 	cmp = CMSG_FIRSTHDR(&msg);
    427       1.6   thorpej 	cmp->cmsg_len = CMSG_LEN(FDCM_DATASIZE);
    428       1.2   thorpej 	cmp->cmsg_level = SOL_SOCKET;
    429       1.2   thorpej 	cmp->cmsg_type = SCM_RIGHTS;
    430       1.5  sommerfe 
    431       1.5  sommerfe 	while (make_pretzel > 0) {
    432       1.5  sommerfe 		if (socketpair(PF_LOCAL, SOCK_STREAM, 0, spair) < 0)
    433       1.5  sommerfe 			err(1, "socketpair");
    434       1.5  sommerfe 
    435       1.5  sommerfe 		printf("send pretzel\n");
    436       1.5  sommerfe 		if (sendmsg(spair[0], &msg, 0) < 0)
    437       1.5  sommerfe 			err(1, "child prezel sendmsg");
    438       1.5  sommerfe 
    439       1.6   thorpej 		close(files[0]);
    440       1.6   thorpej 		close(files[1]);
    441       1.6   thorpej 		files[0] = spair[0];
    442       1.6   thorpej 		files[1] = spair[1];
    443       1.5  sommerfe 		make_pretzel--;
    444       1.5  sommerfe 	}
    445       1.1   thorpej 
    446       1.4   mycroft 	if (sendmsg(sock, &msg, 0) == -1)
    447       1.2   thorpej 		err(1, "child sendmsg");
    448       1.1   thorpej 
    449       1.1   thorpej 	/*
    450       1.1   thorpej 	 * All done!
    451       1.1   thorpej 	 */
    452       1.1   thorpej 	exit(0);
    453       1.1   thorpej }
    454