1 /*
2  * This file is part of net-yy-unix strace test.
3  *
4  * Copyright (c) 2013-2017 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016-2018 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "tests.h"
32 #include <assert.h>
33 #include <stddef.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <sys/socket.h>
39 #include <sys/un.h>
40 
41 #include "accept_compat.h"
42 
43 #define TEST_SOCKET "net-yy-unix.socket"
44 
45 int
main(void)46 main(void)
47 {
48 	skip_if_unavailable("/proc/self/fd/");
49 
50 	struct sockaddr_un addr = {
51 		.sun_family = AF_UNIX,
52 		.sun_path = TEST_SOCKET
53 	};
54 	struct sockaddr * const listen_sa = tail_memdup(&addr, sizeof(addr));
55 
56 	TAIL_ALLOC_OBJECT_CONST_PTR(socklen_t, len);
57 	*len = offsetof(struct sockaddr_un, sun_path) + strlen(TEST_SOCKET) + 1;
58 	if (*len > sizeof(addr))
59 		*len = sizeof(addr);
60 
61 	int listen_fd = socket(AF_UNIX, SOCK_STREAM, 0);
62 	if (listen_fd < 0)
63 		perror_msg_and_skip("socket");
64 	unsigned long listen_inode = inode_of_sockfd(listen_fd);
65 	printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<UNIX:[%lu]>\n",
66 	       listen_fd, listen_inode);
67 
68 	(void) unlink(TEST_SOCKET);
69 	if (bind(listen_fd, listen_sa, *len))
70 		perror_msg_and_skip("bind");
71 	printf("bind(%d<UNIX:[%lu]>, {sa_family=AF_UNIX, sun_path=\"%s\"}"
72 	       ", %u) = 0\n",
73 	       listen_fd, listen_inode, TEST_SOCKET, (unsigned) *len);
74 
75 	if (listen(listen_fd, 1))
76 		perror_msg_and_skip("listen");
77 	printf("listen(%d<UNIX:[%lu,\"%s\"]>, 1) = 0\n",
78 	       listen_fd, listen_inode, TEST_SOCKET);
79 
80 	TAIL_ALLOC_OBJECT_CONST_PTR(unsigned int, optval);
81 	*len = sizeof(*optval);
82 	if (getsockopt(listen_fd, SOL_SOCKET, SO_PASSCRED, optval, len))
83 		perror_msg_and_fail("getsockopt");
84 	printf("getsockopt(%d<UNIX:[%lu,\"%s\"]>, SOL_SOCKET, SO_PASSCRED"
85 	       ", [%u], [%u]) = 0\n",
86 	       listen_fd, listen_inode, TEST_SOCKET, *optval, (unsigned) *len);
87 
88 	memset(listen_sa, 0, sizeof(addr));
89 	*len = sizeof(addr);
90 	if (getsockname(listen_fd, listen_sa, len))
91 		perror_msg_and_fail("getsockname");
92 	printf("getsockname(%d<UNIX:[%lu,\"%s\"]>, {sa_family=AF_UNIX"
93 	       ", sun_path=\"%s\"}, [%d->%d]) = 0\n", listen_fd, listen_inode,
94 	       TEST_SOCKET, TEST_SOCKET, (int) sizeof(addr), (int) *len);
95 
96 	int connect_fd = socket(AF_UNIX, SOCK_STREAM, 0);
97 	if (connect_fd < 0)
98 		perror_msg_and_fail("socket");
99 	unsigned long connect_inode = inode_of_sockfd(connect_fd);
100 	printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<UNIX:[%lu]>\n",
101 	       connect_fd, connect_inode);
102 
103 	if (connect(connect_fd, listen_sa, *len))
104 		perror_msg_and_fail("connect");
105 	printf("connect(%d<UNIX:[%lu]>, {sa_family=AF_UNIX"
106 	       ", sun_path=\"%s\"}, %u) = 0\n",
107 	       connect_fd, connect_inode, TEST_SOCKET, (unsigned) *len);
108 
109 	struct sockaddr * const accept_sa = tail_alloc(sizeof(addr));
110 	memset(accept_sa, 0, sizeof(addr));
111 	*len = sizeof(addr);
112 	int accept_fd = do_accept(listen_fd, accept_sa, len);
113 	if (accept_fd < 0)
114 		perror_msg_and_fail("accept");
115 	unsigned long accept_inode = inode_of_sockfd(accept_fd);
116 	printf("accept(%d<UNIX:[%lu,\"%s\"]>, {sa_family=AF_UNIX}"
117 	       ", [%d->%d]) = %d<UNIX:[%lu->%lu,\"%s\"]>\n",
118 	       listen_fd, listen_inode, TEST_SOCKET,
119 	       (int) sizeof(addr), (int) *len,
120 	       accept_fd, accept_inode, connect_inode, TEST_SOCKET);
121 
122 	memset(listen_sa, 0, sizeof(addr));
123 	*len = sizeof(addr);
124 	if (getpeername(connect_fd, listen_sa, len))
125 		perror_msg_and_fail("getpeername");
126 	printf("getpeername(%d<UNIX:[%lu->%lu]>, {sa_family=AF_UNIX"
127 	       ", sun_path=\"%s\"}, [%d->%d]) = 0\n",
128 	       connect_fd, connect_inode,
129 	       accept_inode, TEST_SOCKET, (int) sizeof(addr), (int) *len);
130 
131 	char text[] = "text";
132 	assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, 0)
133 	       == sizeof(text) - 1);
134 	printf("sendto(%d<UNIX:[%lu->%lu]>, \"%s\", %u, MSG_DONTWAIT"
135 	       ", NULL, 0) = %u\n",
136 	       connect_fd, connect_inode, accept_inode, text,
137 	       (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
138 
139 	assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, NULL)
140 	       == sizeof(text) - 1);
141 	printf("recvfrom(%d<UNIX:[%lu->%lu,\"%s\"]>, \"%s\", %u, MSG_DONTWAIT"
142 	       ", NULL, NULL) = %u\n",
143 	       accept_fd, accept_inode, connect_inode, TEST_SOCKET, text,
144 	       (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
145 
146 	assert(close(connect_fd) == 0);
147 	printf("close(%d<UNIX:[%lu->%lu]>) = 0\n",
148 	       connect_fd, connect_inode, accept_inode);
149 
150 	assert(close(accept_fd) == 0);
151 	printf("close(%d<UNIX:[%lu->%lu,\"%s\"]>) = 0\n",
152 	       accept_fd, accept_inode, connect_inode, TEST_SOCKET);
153 
154 	connect_fd = socket(AF_UNIX, SOCK_STREAM, 0);
155 	if (connect_fd < 0)
156 		perror_msg_and_fail("socket");
157 	connect_inode = inode_of_sockfd(connect_fd);
158 	printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<UNIX:[%lu]>\n",
159 	       connect_fd, connect_inode);
160 
161 	*optval = 1;
162 	*len = sizeof(*optval);
163 	if (setsockopt(connect_fd, SOL_SOCKET, SO_PASSCRED, optval, *len))
164 		perror_msg_and_fail("setsockopt");
165 	printf("setsockopt(%d<UNIX:[%lu]>, SOL_SOCKET, SO_PASSCRED"
166 	       ", [%u], %u) = 0\n",
167 	       connect_fd, connect_inode, *optval, (unsigned) *len);
168 
169 	memset(listen_sa, 0, sizeof(addr));
170 	*len = sizeof(addr);
171 	if (getsockname(listen_fd, listen_sa, len))
172 		perror_msg_and_fail("getsockname");
173 	printf("getsockname(%d<UNIX:[%lu,\"%s\"]>, {sa_family=AF_UNIX"
174 	       ", sun_path=\"%s\"}, [%d->%d]) = 0\n", listen_fd, listen_inode,
175 	       TEST_SOCKET, TEST_SOCKET, (int) sizeof(addr), (int) *len);
176 
177 	if (connect(connect_fd, listen_sa, *len))
178 		perror_msg_and_fail("connect");
179 	printf("connect(%d<UNIX:[%lu]>, {sa_family=AF_UNIX"
180 	       ", sun_path=\"%s\"}, %u) = 0\n",
181 	       connect_fd, connect_inode, TEST_SOCKET, (unsigned) *len);
182 
183 	memset(accept_sa, 0, sizeof(addr));
184 	*len = sizeof(addr);
185 	accept_fd = do_accept(listen_fd, accept_sa, len);
186 	if (accept_fd < 0)
187 		perror_msg_and_fail("accept");
188 	accept_inode = inode_of_sockfd(accept_fd);
189 	const char * const sun_path1 =
190 		((struct sockaddr_un *) accept_sa)->sun_path + 1;
191 	printf("accept(%d<UNIX:[%lu,\"%s\"]>, {sa_family=AF_UNIX"
192 	       ", sun_path=@\"%s\"}, [%d->%d]) = %d<UNIX:[%lu->%lu,\"%s\"]>\n",
193 	       listen_fd, listen_inode, TEST_SOCKET, sun_path1,
194 	       (int) sizeof(addr), (int) *len,
195 	       accept_fd, accept_inode, connect_inode, TEST_SOCKET);
196 
197 	memset(listen_sa, 0, sizeof(addr));
198 	*len = sizeof(addr);
199 	if (getpeername(connect_fd, listen_sa, len))
200 		perror_msg_and_fail("getpeername");
201 	printf("getpeername(%d<UNIX:[%lu->%lu,@\"%s\"]>, {sa_family=AF_UNIX"
202 	       ", sun_path=\"%s\"}, [%d->%d]) = 0\n", connect_fd, connect_inode,
203 	       accept_inode, sun_path1, TEST_SOCKET, (int) sizeof(addr), (int) *len);
204 
205 	assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, 0)
206 	       == sizeof(text) - 1);
207 	printf("sendto(%d<UNIX:[%lu->%lu,@\"%s\"]>, \"%s\", %u, MSG_DONTWAIT"
208 	       ", NULL, 0) = %u\n",
209 	       connect_fd, connect_inode, accept_inode, sun_path1, text,
210 	       (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
211 
212 	assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, NULL)
213 	       == sizeof(text) - 1);
214 	printf("recvfrom(%d<UNIX:[%lu->%lu,\"%s\"]>, \"%s\", %u, MSG_DONTWAIT"
215 	       ", NULL, NULL) = %u\n",
216 	       accept_fd, accept_inode, connect_inode, TEST_SOCKET, text,
217 	       (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
218 
219 	assert(close(connect_fd) == 0);
220 	printf("close(%d<UNIX:[%lu->%lu,@\"%s\"]>) = 0\n",
221 	       connect_fd, connect_inode, accept_inode, sun_path1);
222 
223 	assert(close(accept_fd) == 0);
224 	printf("close(%d<UNIX:[%lu->%lu,\"%s\"]>) = 0\n",
225 	       accept_fd, accept_inode, connect_inode, TEST_SOCKET);
226 
227 	assert(unlink(TEST_SOCKET) == 0);
228 
229 	assert(close(listen_fd) == 0);
230 	printf("close(%d<UNIX:[%lu,\"%s\"]>) = 0\n",
231 	       listen_fd, listen_inode, TEST_SOCKET);
232 
233 	puts("+++ exited with 0 +++");
234 	return 0;
235 }
236