1 /*
2  * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@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 "tests.h"
29 #include <assert.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <sys/socket.h>
35 
main(int ac,const char ** av)36 int main(int ac, const char **av)
37 {
38 	assert(ac > 0);
39 	int fds[ac];
40 
41 	static const char sample[] =
42 		"\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff";
43 	const unsigned int data_size = sizeof(sample) - 1;
44 	void *data = tail_alloc(data_size);
45 	memcpy(data, sample, data_size);
46 
47 	struct iovec *iov = tail_alloc(sizeof(struct iovec));
48 	iov->iov_base = data;
49 	iov->iov_len = data_size;
50 
51 	struct msghdr *mh = tail_alloc(sizeof(struct msghdr));
52 	memset(mh, 0, sizeof(*mh));
53 	mh->msg_iov = iov;
54 	mh->msg_iovlen = 1;
55 
56 	int i;
57 	while ((i = open("/dev/null", O_RDWR)) <= ac + 2)
58 		assert(i >= 0);
59 	while (i > 2)
60 		assert(close(i--) == 0);
61 	assert(close(0) == 0);
62 
63 	int sv[2];
64 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
65 		perror_msg_and_skip("socketpair");
66 	int one = 1;
67 	if (setsockopt(sv[0], SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)))
68 		perror_msg_and_skip("setsockopt");
69 
70 	assert((fds[0] = open("/dev/null", O_RDWR)) == 4);
71 	for (i = 1; i < ac; ++i)
72 		assert((fds[i] = open(av[i], O_RDONLY)) == i + 4);
73 
74 	unsigned int cmsg_size = CMSG_SPACE(sizeof(fds));
75 	struct cmsghdr *cmsg = tail_alloc(cmsg_size);
76 	memset(cmsg, 0, cmsg_size);
77 	cmsg->cmsg_level = SOL_SOCKET;
78 	cmsg->cmsg_type = SCM_RIGHTS;
79 	cmsg->cmsg_len = CMSG_LEN(sizeof(fds));
80 	memcpy(CMSG_DATA(cmsg), fds, sizeof(fds));
81 
82 	mh->msg_control = cmsg;
83 	mh->msg_controllen = cmsg_size;
84 
85 	assert(sendmsg(sv[1], mh, 0) == (int) data_size);
86 
87 	assert(close(sv[1]) == 0);
88 	assert(open("/dev/null", O_RDWR) == sv[1]);
89 
90 	for (i = 0; i < ac; ++i) {
91 		assert(close(fds[i]) == 0);
92 		fds[i] = 0;
93 	}
94 
95 	cmsg_size += CMSG_SPACE(sizeof(struct ucred));
96 	cmsg = tail_alloc(cmsg_size);
97 	memset(cmsg, 0, cmsg_size);
98 	mh->msg_control = cmsg;
99 	mh->msg_controllen = cmsg_size;
100 
101 	assert(recvmsg(0, mh, 0) == (int) data_size);
102 	assert(close(0) == 0);
103 
104 	return 0;
105 }
106