Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (c) 2014-2015 Dmitry V. Levin <ldv (at) altlinux.org>
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. The name of the author may not be used to endorse or promote products
     14  *    derived from this software without specific prior written permission.
     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 <assert.h>
     29 #include <string.h>
     30 #include <stdlib.h>
     31 #include <unistd.h>
     32 #include <errno.h>
     33 #include <fcntl.h>
     34 #include <sys/socket.h>
     35 #include <sys/wait.h>
     36 
     37 int main(int ac, const char **av)
     38 {
     39 	int i;
     40 	int data = 0;
     41 	struct iovec iov = {
     42 		.iov_base = &data,
     43 		.iov_len = sizeof(iov)
     44 	};
     45 
     46 	while ((i = open("/dev/null", O_RDWR)) < 3)
     47 		assert(i >= 0);
     48 	(void) close(3);
     49 
     50 	int sv[2];
     51 	assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
     52 	int one = 1;
     53 	assert(setsockopt(sv[0], SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) == 0);
     54 
     55 	pid_t pid = fork();
     56 	assert(pid >= 0);
     57 
     58 	if (pid) {
     59 		assert(close(sv[0]) == 0);
     60 		assert(dup2(sv[1], 1) == 1);
     61 		assert(close(sv[1]) == 0);
     62 
     63 		int fds[ac];
     64 		assert((fds[0] = open("/dev/null", O_RDWR)) == 3);
     65 		for (i = 1; i < ac; ++i)
     66 			assert((fds[i] = open(av[i], O_RDONLY)) == i + 3);
     67 
     68 		union {
     69 			struct cmsghdr cmsg;
     70 			char buf[CMSG_LEN(sizeof(fds))];
     71 		} control;
     72 
     73 		control.cmsg.cmsg_level = SOL_SOCKET;
     74 		control.cmsg.cmsg_type = SCM_RIGHTS;
     75 		control.cmsg.cmsg_len = CMSG_LEN(sizeof(fds));
     76 		memcpy(CMSG_DATA(&control.cmsg), fds, sizeof(fds));
     77 
     78 		struct msghdr mh = {
     79 			.msg_iov = &iov,
     80 			.msg_iovlen = 1,
     81 			.msg_control = &control,
     82 			.msg_controllen = sizeof(control)
     83 		};
     84 
     85 		assert(sendmsg(1, &mh, 0) == sizeof(iov));
     86 		assert(close(1) == 0);
     87 
     88                 int status;
     89 		assert(waitpid(pid, &status, 0) == pid);
     90 		assert(status == 0);
     91 	} else {
     92 		assert(close(sv[1]) == 0);
     93 		assert(dup2(sv[0], 0) == 0);
     94 		assert(close(sv[0]) == 0);
     95 
     96 		struct cmsghdr control[4 + ac * sizeof(int) / sizeof(struct cmsghdr)];
     97 
     98 		struct msghdr mh = {
     99 			.msg_iov = &iov,
    100 			.msg_iovlen = 1,
    101 			.msg_control = control,
    102 			.msg_controllen = sizeof(control)
    103 		};
    104 
    105 		assert(recvmsg(0, &mh, 0) == sizeof(iov));
    106 		assert(close(0) == 0);
    107 	}
    108 
    109 	return 0;
    110 }
    111