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