1 /*
2  * This file is part of net-y-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-y-unix.socket"
44 
45 int
main(void)46 main(void)
47 {
48 	skip_if_unavailable("/proc/self/fd/");
49 
50 	static const 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<socket:[%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<socket:[%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<socket:[%lu]>, 1) = 0\n", listen_fd, listen_inode);
78 
79 	TAIL_ALLOC_OBJECT_CONST_PTR(unsigned int, optval);
80 	*len = sizeof(*optval);
81 	if (getsockopt(listen_fd, SOL_SOCKET, SO_PASSCRED, optval, len))
82 		perror_msg_and_fail("getsockopt");
83 	printf("getsockopt(%d<socket:[%lu]>, SOL_SOCKET, SO_PASSCRED"
84 	       ", [%u], [%u]) = 0\n",
85 	       listen_fd, listen_inode, *optval, (unsigned) *len);
86 
87 	memset(listen_sa, 0, sizeof(addr));
88 	*len = sizeof(addr);
89 	if (getsockname(listen_fd, listen_sa, len))
90 		perror_msg_and_fail("getsockname");
91 	printf("getsockname(%d<socket:[%lu]>, {sa_family=AF_UNIX"
92 	       ", sun_path=\"%s\"}, [%d->%d]) = 0\n", listen_fd, listen_inode,
93 	       TEST_SOCKET, (int) sizeof(addr), (int) *len);
94 
95 	int connect_fd = socket(AF_UNIX, SOCK_STREAM, 0);
96 	if (connect_fd < 0)
97 		perror_msg_and_fail("socket");
98 	unsigned long connect_inode = inode_of_sockfd(connect_fd);
99 	printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<socket:[%lu]>\n",
100 	       connect_fd, connect_inode);
101 
102 	if (connect(connect_fd, listen_sa, *len))
103 		perror_msg_and_fail("connect");
104 	printf("connect(%d<socket:[%lu]>, {sa_family=AF_UNIX"
105 	       ", sun_path=\"%s\"}, %u) = 0\n",
106 	       connect_fd, connect_inode, TEST_SOCKET, (unsigned) *len);
107 
108 	struct sockaddr * const accept_sa = tail_alloc(sizeof(addr));
109 	memset(accept_sa, 0, sizeof(addr));
110 	*len = sizeof(addr);
111 	int accept_fd = do_accept(listen_fd, accept_sa, len);
112 	if (accept_fd < 0)
113 		perror_msg_and_fail("accept");
114 	unsigned long accept_inode = inode_of_sockfd(accept_fd);
115 	printf("accept(%d<socket:[%lu]>, {sa_family=AF_UNIX}"
116 	       ", [%d->%d]) = %d<socket:[%lu]>\n",
117 	       listen_fd, listen_inode,
118 	       (int) sizeof(addr), (int) *len,
119 	       accept_fd, accept_inode);
120 
121 	memset(listen_sa, 0, sizeof(addr));
122 	*len = sizeof(addr);
123 	if (getpeername(connect_fd, listen_sa, len))
124 		perror_msg_and_fail("getpeername");
125 	printf("getpeername(%d<socket:[%lu]>, {sa_family=AF_UNIX"
126 	       ", sun_path=\"%s\"}, [%d->%d]) = 0\n", connect_fd, connect_inode,
127 	       TEST_SOCKET, (int) sizeof(addr), (int) *len);
128 
129 	char text[] = "text";
130 	assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, 0)
131 	       == sizeof(text) - 1);
132 	printf("sendto(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT"
133 	       ", NULL, 0) = %u\n",
134 	       connect_fd, connect_inode, text,
135 	       (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
136 
137 	assert(close(connect_fd) == 0);
138 	printf("close(%d<socket:[%lu]>) = 0\n", connect_fd, connect_inode);
139 
140 	assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, NULL)
141 	       == sizeof(text) - 1);
142 	printf("recvfrom(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT"
143 	       ", NULL, NULL) = %u\n",
144 	       accept_fd, accept_inode, text,
145 	       (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
146 
147 	assert(close(accept_fd) == 0);
148 	printf("close(%d<socket:[%lu]>) = 0\n", accept_fd, accept_inode);
149 
150 	connect_fd = socket(AF_UNIX, SOCK_STREAM, 0);
151 	if (connect_fd < 0)
152 		perror_msg_and_fail("socket");
153 	connect_inode = inode_of_sockfd(connect_fd);
154 	printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<socket:[%lu]>\n",
155 	       connect_fd, connect_inode);
156 
157 	*optval = 1;
158 	*len = sizeof(*optval);
159 	if (setsockopt(connect_fd, SOL_SOCKET, SO_PASSCRED, optval, *len))
160 		perror_msg_and_fail("setsockopt");
161 	printf("setsockopt(%d<socket:[%lu]>, SOL_SOCKET, SO_PASSCRED"
162 	       ", [%u], %u) = 0\n",
163 	       connect_fd, connect_inode, *optval, (unsigned) *len);
164 
165 	memset(listen_sa, 0, sizeof(addr));
166 	*len = sizeof(addr);
167 	if (getsockname(listen_fd, listen_sa, len))
168 		perror_msg_and_fail("getsockname");
169 	printf("getsockname(%d<socket:[%lu]>, {sa_family=AF_UNIX"
170 	       ", sun_path=\"%s\"}, [%d->%d]) = 0\n",
171 	       listen_fd, listen_inode, TEST_SOCKET,
172 	       (int) sizeof(addr), (int) *len);
173 
174 	if (connect(connect_fd, listen_sa, *len))
175 		perror_msg_and_fail("connect");
176 	printf("connect(%d<socket:[%lu]>, {sa_family=AF_UNIX"
177 	       ", sun_path=\"%s\"}, %u) = 0\n",
178 	       connect_fd, connect_inode, TEST_SOCKET, (unsigned) *len);
179 
180 	memset(accept_sa, 0, sizeof(addr));
181 	*len = sizeof(addr);
182 	accept_fd = do_accept(listen_fd, accept_sa, len);
183 	if (accept_fd < 0)
184 		perror_msg_and_fail("accept");
185 	accept_inode = inode_of_sockfd(accept_fd);
186 	const char * const sun_path1 =
187 		((struct sockaddr_un *) accept_sa)->sun_path + 1;
188 	printf("accept(%d<socket:[%lu]>, {sa_family=AF_UNIX"
189 	       ", sun_path=@\"%s\"}, [%d->%d]) = %d<socket:[%lu]>\n",
190 	       listen_fd, listen_inode, sun_path1,
191 	       (int) sizeof(addr), (int) *len,
192 	       accept_fd, accept_inode);
193 
194 	memset(listen_sa, 0, sizeof(addr));
195 	*len = sizeof(addr);
196 	if (getpeername(connect_fd, listen_sa, len))
197 		perror_msg_and_fail("getpeername");
198 	printf("getpeername(%d<socket:[%lu]>, {sa_family=AF_UNIX"
199 	       ", sun_path=\"%s\"}, [%d->%d]) = 0\n",
200 	       connect_fd, connect_inode, TEST_SOCKET,
201 	       (int) sizeof(addr), (int) *len);
202 
203 	memset(accept_sa, 0, sizeof(addr));
204 	*len = sizeof(addr);
205 	if (getsockname(connect_fd, accept_sa, len))
206 		perror_msg_and_fail("getsockname");
207 	printf("getsockname(%d<socket:[%lu]>, {sa_family=AF_UNIX"
208 	       ", sun_path=@\"%s\"}, [%d->%d]) = 0\n",
209 	       connect_fd, connect_inode, sun_path1,
210 	       (int) sizeof(addr), (int) *len);
211 
212 	assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, 0)
213 	       == sizeof(text) - 1);
214 	printf("sendto(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT"
215 	       ", NULL, 0) = %u\n",
216 	       connect_fd, connect_inode, text,
217 	       (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
218 
219 	assert(close(connect_fd) == 0);
220 	printf("close(%d<socket:[%lu]>) = 0\n", connect_fd, connect_inode);
221 
222 	assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, NULL)
223 	       == sizeof(text) - 1);
224 	printf("recvfrom(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT"
225 	       ", NULL, NULL) = %u\n",
226 	       accept_fd, accept_inode, text,
227 	       (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
228 
229 	assert(close(accept_fd) == 0);
230 	printf("close(%d<socket:[%lu]>) = 0\n", accept_fd, accept_inode);
231 
232 	assert(unlink(TEST_SOCKET) == 0);
233 
234 	assert(close(listen_fd) == 0);
235 	printf("close(%d<socket:[%lu]>) = 0\n",
236 	       listen_fd, listen_inode);
237 
238 	puts("+++ exited with 0 +++");
239 	return 0;
240 }
241