1 /*
2  * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2014-2017 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tests.h"
30 #include <assert.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/socket.h>
36 
main(int ac,const char ** av)37 int main(int ac, const char **av)
38 {
39 	assert(ac > 0);
40 	int fds[ac];
41 
42 	static const char sample[] =
43 		"\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff";
44 	const unsigned int data_size = sizeof(sample) - 1;
45 	void *data = tail_alloc(data_size);
46 	memcpy(data, sample, data_size);
47 
48 	TAIL_ALLOC_OBJECT_CONST_PTR(struct iovec, iov);
49 	iov->iov_base = data;
50 	iov->iov_len = data_size;
51 
52 	TAIL_ALLOC_OBJECT_CONST_PTR(struct msghdr, mh);
53 	memset(mh, 0, sizeof(*mh));
54 	mh->msg_iov = iov;
55 	mh->msg_iovlen = 1;
56 
57 	int i;
58 	while ((i = open("/dev/null", O_RDWR)) <= ac + 2)
59 		assert(i >= 0);
60 	while (i > 2)
61 		assert(close(i--) == 0);
62 	assert(close(0) == 0);
63 
64 	int sv[2];
65 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
66 		perror_msg_and_skip("socketpair");
67 	int one = 1;
68 	if (setsockopt(sv[0], SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)))
69 		perror_msg_and_skip("setsockopt");
70 
71 	assert((fds[0] = open("/dev/null", O_RDWR)) == 4);
72 	for (i = 1; i < ac; ++i)
73 		assert((fds[i] = open(av[i], O_RDONLY)) == i + 4);
74 
75 	unsigned int cmsg_size = CMSG_SPACE(sizeof(fds));
76 	struct cmsghdr *cmsg = tail_alloc(cmsg_size);
77 	memset(cmsg, 0, cmsg_size);
78 	cmsg->cmsg_level = SOL_SOCKET;
79 	cmsg->cmsg_type = SCM_RIGHTS;
80 	cmsg->cmsg_len = CMSG_LEN(sizeof(fds));
81 	memcpy(CMSG_DATA(cmsg), fds, sizeof(fds));
82 
83 	mh->msg_control = cmsg;
84 	mh->msg_controllen = cmsg_size;
85 
86 	assert(sendmsg(sv[1], mh, 0) == (int) data_size);
87 
88 	assert(close(sv[1]) == 0);
89 	assert(open("/dev/null", O_RDWR) == sv[1]);
90 
91 	for (i = 0; i < ac; ++i) {
92 		assert(close(fds[i]) == 0);
93 		fds[i] = 0;
94 	}
95 
96 	cmsg_size += CMSG_SPACE(sizeof(struct ucred));
97 	cmsg = tail_alloc(cmsg_size);
98 	memset(cmsg, 0, cmsg_size);
99 	mh->msg_control = cmsg;
100 	mh->msg_controllen = cmsg_size;
101 
102 	assert(recvmsg(0, mh, 0) == (int) data_size);
103 	assert(close(0) == 0);
104 
105 	return 0;
106 }
107