]> granicus.if.org Git - strace/blob - tests/net-yy-unix.c
Remove XLAT_END
[strace] / tests / net-yy-unix.c
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  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include "tests.h"
12 #include <assert.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20
21 #include "accept_compat.h"
22
23 #define TEST_SOCKET "net-yy-unix.socket"
24
25 int
26 main(void)
27 {
28         skip_if_unavailable("/proc/self/fd/");
29
30         struct sockaddr_un addr = {
31                 .sun_family = AF_UNIX,
32                 .sun_path = TEST_SOCKET
33         };
34         struct sockaddr * const listen_sa = tail_memdup(&addr, sizeof(addr));
35
36         TAIL_ALLOC_OBJECT_CONST_PTR(socklen_t, len);
37         *len = offsetof(struct sockaddr_un, sun_path) + strlen(TEST_SOCKET) + 1;
38         if (*len > sizeof(addr))
39                 *len = sizeof(addr);
40
41         int listen_fd = socket(AF_UNIX, SOCK_STREAM, 0);
42         if (listen_fd < 0)
43                 perror_msg_and_skip("socket");
44         unsigned long listen_inode = inode_of_sockfd(listen_fd);
45         printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<UNIX:[%lu]>\n",
46                listen_fd, listen_inode);
47
48         (void) unlink(TEST_SOCKET);
49         if (bind(listen_fd, listen_sa, *len))
50                 perror_msg_and_skip("bind");
51         printf("bind(%d<UNIX:[%lu]>, {sa_family=AF_UNIX, sun_path=\"%s\"}"
52                ", %u) = 0\n",
53                listen_fd, listen_inode, TEST_SOCKET, (unsigned) *len);
54
55         if (listen(listen_fd, 1))
56                 perror_msg_and_skip("listen");
57         printf("listen(%d<UNIX:[%lu,\"%s\"]>, 1) = 0\n",
58                listen_fd, listen_inode, TEST_SOCKET);
59
60         TAIL_ALLOC_OBJECT_CONST_PTR(unsigned int, optval);
61         *len = sizeof(*optval);
62         if (getsockopt(listen_fd, SOL_SOCKET, SO_PASSCRED, optval, len))
63                 perror_msg_and_fail("getsockopt");
64         printf("getsockopt(%d<UNIX:[%lu,\"%s\"]>, SOL_SOCKET, SO_PASSCRED"
65                ", [%u], [%u]) = 0\n",
66                listen_fd, listen_inode, TEST_SOCKET, *optval, (unsigned) *len);
67
68         memset(listen_sa, 0, sizeof(addr));
69         *len = sizeof(addr);
70         if (getsockname(listen_fd, listen_sa, len))
71                 perror_msg_and_fail("getsockname");
72         printf("getsockname(%d<UNIX:[%lu,\"%s\"]>, {sa_family=AF_UNIX"
73                ", sun_path=\"%s\"}, [%d->%d]) = 0\n", listen_fd, listen_inode,
74                TEST_SOCKET, TEST_SOCKET, (int) sizeof(addr), (int) *len);
75
76         int connect_fd = socket(AF_UNIX, SOCK_STREAM, 0);
77         if (connect_fd < 0)
78                 perror_msg_and_fail("socket");
79         unsigned long connect_inode = inode_of_sockfd(connect_fd);
80         printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<UNIX:[%lu]>\n",
81                connect_fd, connect_inode);
82
83         if (connect(connect_fd, listen_sa, *len))
84                 perror_msg_and_fail("connect");
85         printf("connect(%d<UNIX:[%lu]>, {sa_family=AF_UNIX"
86                ", sun_path=\"%s\"}, %u) = 0\n",
87                connect_fd, connect_inode, TEST_SOCKET, (unsigned) *len);
88
89         struct sockaddr * const accept_sa = tail_alloc(sizeof(addr));
90         memset(accept_sa, 0, sizeof(addr));
91         *len = sizeof(addr);
92         int accept_fd = do_accept(listen_fd, accept_sa, len);
93         if (accept_fd < 0)
94                 perror_msg_and_fail("accept");
95         unsigned long accept_inode = inode_of_sockfd(accept_fd);
96         printf("accept(%d<UNIX:[%lu,\"%s\"]>, {sa_family=AF_UNIX}"
97                ", [%d->%d]) = %d<UNIX:[%lu->%lu,\"%s\"]>\n",
98                listen_fd, listen_inode, TEST_SOCKET,
99                (int) sizeof(addr), (int) *len,
100                accept_fd, accept_inode, connect_inode, TEST_SOCKET);
101
102         memset(listen_sa, 0, sizeof(addr));
103         *len = sizeof(addr);
104         if (getpeername(connect_fd, listen_sa, len))
105                 perror_msg_and_fail("getpeername");
106         printf("getpeername(%d<UNIX:[%lu->%lu]>, {sa_family=AF_UNIX"
107                ", sun_path=\"%s\"}, [%d->%d]) = 0\n",
108                connect_fd, connect_inode,
109                accept_inode, TEST_SOCKET, (int) sizeof(addr), (int) *len);
110
111         char text[] = "text";
112         assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, 0)
113                == sizeof(text) - 1);
114         printf("sendto(%d<UNIX:[%lu->%lu]>, \"%s\", %u, MSG_DONTWAIT"
115                ", NULL, 0) = %u\n",
116                connect_fd, connect_inode, accept_inode, text,
117                (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
118
119         assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, NULL)
120                == sizeof(text) - 1);
121         printf("recvfrom(%d<UNIX:[%lu->%lu,\"%s\"]>, \"%s\", %u, MSG_DONTWAIT"
122                ", NULL, NULL) = %u\n",
123                accept_fd, accept_inode, connect_inode, TEST_SOCKET, text,
124                (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
125
126         assert(close(connect_fd) == 0);
127         printf("close(%d<UNIX:[%lu->%lu]>) = 0\n",
128                connect_fd, connect_inode, accept_inode);
129
130         assert(close(accept_fd) == 0);
131         printf("close(%d<UNIX:[%lu->%lu,\"%s\"]>) = 0\n",
132                accept_fd, accept_inode, connect_inode, TEST_SOCKET);
133
134         connect_fd = socket(AF_UNIX, SOCK_STREAM, 0);
135         if (connect_fd < 0)
136                 perror_msg_and_fail("socket");
137         connect_inode = inode_of_sockfd(connect_fd);
138         printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<UNIX:[%lu]>\n",
139                connect_fd, connect_inode);
140
141         *optval = 1;
142         *len = sizeof(*optval);
143         if (setsockopt(connect_fd, SOL_SOCKET, SO_PASSCRED, optval, *len))
144                 perror_msg_and_fail("setsockopt");
145         printf("setsockopt(%d<UNIX:[%lu]>, SOL_SOCKET, SO_PASSCRED"
146                ", [%u], %u) = 0\n",
147                connect_fd, connect_inode, *optval, (unsigned) *len);
148
149         memset(listen_sa, 0, sizeof(addr));
150         *len = sizeof(addr);
151         if (getsockname(listen_fd, listen_sa, len))
152                 perror_msg_and_fail("getsockname");
153         printf("getsockname(%d<UNIX:[%lu,\"%s\"]>, {sa_family=AF_UNIX"
154                ", sun_path=\"%s\"}, [%d->%d]) = 0\n", listen_fd, listen_inode,
155                TEST_SOCKET, TEST_SOCKET, (int) sizeof(addr), (int) *len);
156
157         if (connect(connect_fd, listen_sa, *len))
158                 perror_msg_and_fail("connect");
159         printf("connect(%d<UNIX:[%lu]>, {sa_family=AF_UNIX"
160                ", sun_path=\"%s\"}, %u) = 0\n",
161                connect_fd, connect_inode, TEST_SOCKET, (unsigned) *len);
162
163         memset(accept_sa, 0, sizeof(addr));
164         *len = sizeof(addr);
165         accept_fd = do_accept(listen_fd, accept_sa, len);
166         if (accept_fd < 0)
167                 perror_msg_and_fail("accept");
168         accept_inode = inode_of_sockfd(accept_fd);
169         const char * const sun_path1 =
170                 ((struct sockaddr_un *) accept_sa)->sun_path + 1;
171         printf("accept(%d<UNIX:[%lu,\"%s\"]>, {sa_family=AF_UNIX"
172                ", sun_path=@\"%s\"}, [%d->%d]) = %d<UNIX:[%lu->%lu,\"%s\"]>\n",
173                listen_fd, listen_inode, TEST_SOCKET, sun_path1,
174                (int) sizeof(addr), (int) *len,
175                accept_fd, accept_inode, connect_inode, TEST_SOCKET);
176
177         memset(listen_sa, 0, sizeof(addr));
178         *len = sizeof(addr);
179         if (getpeername(connect_fd, listen_sa, len))
180                 perror_msg_and_fail("getpeername");
181         printf("getpeername(%d<UNIX:[%lu->%lu,@\"%s\"]>, {sa_family=AF_UNIX"
182                ", sun_path=\"%s\"}, [%d->%d]) = 0\n", connect_fd, connect_inode,
183                accept_inode, sun_path1, TEST_SOCKET, (int) sizeof(addr), (int) *len);
184
185         assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, 0)
186                == sizeof(text) - 1);
187         printf("sendto(%d<UNIX:[%lu->%lu,@\"%s\"]>, \"%s\", %u, MSG_DONTWAIT"
188                ", NULL, 0) = %u\n",
189                connect_fd, connect_inode, accept_inode, sun_path1, text,
190                (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
191
192         assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, NULL)
193                == sizeof(text) - 1);
194         printf("recvfrom(%d<UNIX:[%lu->%lu,\"%s\"]>, \"%s\", %u, MSG_DONTWAIT"
195                ", NULL, NULL) = %u\n",
196                accept_fd, accept_inode, connect_inode, TEST_SOCKET, text,
197                (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
198
199         assert(close(connect_fd) == 0);
200         printf("close(%d<UNIX:[%lu->%lu,@\"%s\"]>) = 0\n",
201                connect_fd, connect_inode, accept_inode, sun_path1);
202
203         assert(close(accept_fd) == 0);
204         printf("close(%d<UNIX:[%lu->%lu,\"%s\"]>) = 0\n",
205                accept_fd, accept_inode, connect_inode, TEST_SOCKET);
206
207         assert(unlink(TEST_SOCKET) == 0);
208
209         assert(close(listen_fd) == 0);
210         printf("close(%d<UNIX:[%lu,\"%s\"]>) = 0\n",
211                listen_fd, listen_inode, TEST_SOCKET);
212
213         puts("+++ exited with 0 +++");
214         return 0;
215 }