blob: 7bad003296d5553b902c1dbc14924550da544b99 [file] [log] [blame]
Dmitry V. Levin6124cd92016-01-25 22:20:54 +00001/*
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
41int
42main(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
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +000057 int listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
Dmitry V. Levin6124cd92016-01-25 22:20:54 +000058 if (listen_fd < 0)
59 perror_msg_and_skip("socket");
60 unsigned long listen_inode = inode_of_sockfd(listen_fd);
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +000061 printf("socket(AF_LOCAL, SOCK_STREAM, 0) = %d<UNIX:[%lu]>\n",
Dmitry V. Levin6124cd92016-01-25 22:20:54 +000062 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_LOCAL, 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_LOCAL"
88 ", sun_path=\"%s\"}, [%u]) = 0\n", listen_fd, listen_inode,
89 av[1], av[1], (unsigned) *len);
90
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +000091 int connect_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
Dmitry V. Levin6124cd92016-01-25 22:20:54 +000092 if (connect_fd < 0)
93 perror_msg_and_fail("socket");
94 unsigned long connect_inode = inode_of_sockfd(connect_fd);
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +000095 printf("socket(AF_LOCAL, SOCK_STREAM, 0) = %d<UNIX:[%lu]>\n",
Dmitry V. Levin6124cd92016-01-25 22:20:54 +000096 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_LOCAL"
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_LOCAL, NULL}"
112 ", [%u]) = %d<UNIX:[%lu->%lu,\"%s\"]>\n",
113 listen_fd, listen_inode, av[1], (unsigned) *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_LOCAL"
121 ", sun_path=\"%s\"}, [%u]) = 0\n", connect_fd, connect_inode,
122 accept_inode, av[1], (unsigned) *len);
123
124 char text[] = "text";
125 assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, 0)
126 == sizeof(text) - 1);
127 printf("sendto(%d<UNIX:[%lu->%lu]>, \"%s\", %u, MSG_DONTWAIT"
128 ", NULL, 0) = %u\n",
129 connect_fd, connect_inode, accept_inode, text,
130 (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
131
132 assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, NULL)
133 == sizeof(text) - 1);
134 printf("recvfrom(%d<UNIX:[%lu->%lu,\"%s\"]>, \"%s\", %u, MSG_DONTWAIT"
135 ", NULL, NULL) = %u\n",
136 accept_fd, accept_inode, connect_inode, av[1], text,
137 (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
138
139 assert(close(connect_fd) == 0);
140 printf("close(%d<UNIX:[%lu->%lu]>) = 0\n",
141 connect_fd, connect_inode, accept_inode);
142
143 assert(close(accept_fd) == 0);
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000144 printf("close(%d<UNIX:[%lu->%lu,\"%s\"]>) = 0\n",
145 accept_fd, accept_inode, connect_inode, av[1]);
Dmitry V. Levin6124cd92016-01-25 22:20:54 +0000146
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +0000147 connect_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
Dmitry V. Levin6124cd92016-01-25 22:20:54 +0000148 if (connect_fd < 0)
149 perror_msg_and_fail("socket");
150 connect_inode = inode_of_sockfd(connect_fd);
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +0000151 printf("socket(AF_LOCAL, SOCK_STREAM, 0) = %d<UNIX:[%lu]>\n",
Dmitry V. Levin6124cd92016-01-25 22:20:54 +0000152 connect_fd, connect_inode);
153
154 *optval = 1;
155 *len = sizeof(*optval);
156 if (setsockopt(connect_fd, SOL_SOCKET, SO_PASSCRED, optval, *len))
157 perror_msg_and_fail("setsockopt");
158 printf("setsockopt(%d<UNIX:[%lu]>, SOL_SOCKET, SO_PASSCRED"
159 ", [%u], %u) = 0\n",
160 connect_fd, connect_inode, *optval, (unsigned) *len);
161
162 memset(listen_sa, 0, sizeof(addr));
163 *len = sizeof(addr);
164 if (getsockname(listen_fd, listen_sa, len))
165 perror_msg_and_fail("getsockname");
166 printf("getsockname(%d<UNIX:[%lu,\"%s\"]>, {sa_family=AF_LOCAL"
167 ", sun_path=\"%s\"}, [%u]) = 0\n", listen_fd, listen_inode,
168 av[1], av[1], (unsigned) *len);
169
170 if (connect(connect_fd, listen_sa, *len))
171 perror_msg_and_fail("connect");
172 printf("connect(%d<UNIX:[%lu]>, {sa_family=AF_LOCAL"
173 ", sun_path=\"%s\"}, %u) = 0\n",
174 connect_fd, connect_inode, av[1], (unsigned) *len);
175
176 memset(accept_sa, 0, sizeof(addr));
177 *len = sizeof(addr);
178 accept_fd = accept(listen_fd, accept_sa, len);
179 if (accept_fd < 0)
180 perror_msg_and_fail("accept");
181 accept_inode = inode_of_sockfd(accept_fd);
182 const char * const sun_path1 =
183 ((struct sockaddr_un *) accept_sa) -> sun_path + 1;
184 printf("accept(%d<UNIX:[%lu,\"%s\"]>, {sa_family=AF_LOCAL"
185 ", sun_path=@\"%s\"}, [%u]) = %d<UNIX:[%lu->%lu,\"%s\"]>\n",
186 listen_fd, listen_inode, av[1], sun_path1, (unsigned) *len,
187 accept_fd, accept_inode, connect_inode, av[1]);
188
189 memset(listen_sa, 0, sizeof(addr));
190 *len = sizeof(addr);
191 if (getpeername(connect_fd, listen_sa, len))
192 perror_msg_and_fail("getpeername");
193 printf("getpeername(%d<UNIX:[%lu->%lu,@\"%s\"]>, {sa_family=AF_LOCAL"
194 ", sun_path=\"%s\"}, [%u]) = 0\n", connect_fd, connect_inode,
195 accept_inode, sun_path1, av[1], (unsigned) *len);
196
197 assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, 0)
198 == sizeof(text) - 1);
199 printf("sendto(%d<UNIX:[%lu->%lu,@\"%s\"]>, \"%s\", %u, MSG_DONTWAIT"
200 ", NULL, 0) = %u\n",
201 connect_fd, connect_inode, accept_inode, sun_path1, text,
202 (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
203
204 assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, NULL)
205 == sizeof(text) - 1);
206 printf("recvfrom(%d<UNIX:[%lu->%lu,\"%s\"]>, \"%s\", %u, MSG_DONTWAIT"
207 ", NULL, NULL) = %u\n",
208 accept_fd, accept_inode, connect_inode, av[1], text,
209 (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
210
211 assert(close(connect_fd) == 0);
212 printf("close(%d<UNIX:[%lu->%lu,@\"%s\"]>) = 0\n",
213 connect_fd, connect_inode, accept_inode, sun_path1);
214
215 assert(close(accept_fd) == 0);
Dmitry V. Levin3c17d1b2016-02-01 23:14:59 +0000216 printf("close(%d<UNIX:[%lu->%lu,\"%s\"]>) = 0\n",
217 accept_fd, accept_inode, connect_inode, av[1]);
Dmitry V. Levin6124cd92016-01-25 22:20:54 +0000218
219 assert(unlink(av[1]) == 0);
220
221 assert(close(listen_fd) == 0);
222 printf("close(%d<UNIX:[%lu,\"%s\"]>) = 0\n",
223 listen_fd, listen_inode, av[1]);
224
225 puts("+++ exited with 0 +++");
226 return 0;
227}