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