blob: cb22c4382ca56e17f6b6fa92e467d63c7e233f7a [file] [log] [blame]
Dmitry V. Levin751fcb82016-02-02 19:48:46 +00001/*
2 * This file is part of net-y-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. Levine139cbe2016-06-20 22:52:49 +000057 int listen_fd = socket(AF_UNIX, SOCK_STREAM, 0);
Dmitry V. Levin751fcb82016-02-02 19:48:46 +000058 if (listen_fd < 0)
59 perror_msg_and_skip("socket");
60 unsigned long listen_inode = inode_of_sockfd(listen_fd);
Dmitry V. Levine139cbe2016-06-20 22:52:49 +000061 printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<socket:[%lu]>\n",
Dmitry V. Levin751fcb82016-02-02 19:48:46 +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");
Dmitry V. Levine139cbe2016-06-20 22:52:49 +000067 printf("bind(%d<socket:[%lu]>, {sa_family=AF_UNIX, sun_path=\"%s\"}"
Dmitry V. Levin751fcb82016-02-02 19:48:46 +000068 ", %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<socket:[%lu]>, 1) = 0\n", listen_fd, listen_inode);
73
74 unsigned int * const optval = tail_alloc(sizeof(unsigned int));
75 *len = sizeof(*optval);
76 if (getsockopt(listen_fd, SOL_SOCKET, SO_PASSCRED, optval, len))
77 perror_msg_and_fail("getsockopt");
78 printf("getsockopt(%d<socket:[%lu]>, SOL_SOCKET, SO_PASSCRED"
79 ", [%u], [%u]) = 0\n",
80 listen_fd, listen_inode, *optval, (unsigned) *len);
81
82 memset(listen_sa, 0, sizeof(addr));
83 *len = sizeof(addr);
84 if (getsockname(listen_fd, listen_sa, len))
85 perror_msg_and_fail("getsockname");
Dmitry V. Levine139cbe2016-06-20 22:52:49 +000086 printf("getsockname(%d<socket:[%lu]>, {sa_family=AF_UNIX"
Dmitry V. Levin751fcb82016-02-02 19:48:46 +000087 ", sun_path=\"%s\"}, [%u]) = 0\n", listen_fd, listen_inode,
88 av[1], (unsigned) *len);
89
Dmitry V. Levine139cbe2016-06-20 22:52:49 +000090 int connect_fd = socket(AF_UNIX, SOCK_STREAM, 0);
Dmitry V. Levin751fcb82016-02-02 19:48:46 +000091 if (connect_fd < 0)
92 perror_msg_and_fail("socket");
93 unsigned long connect_inode = inode_of_sockfd(connect_fd);
Dmitry V. Levine139cbe2016-06-20 22:52:49 +000094 printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<socket:[%lu]>\n",
Dmitry V. Levin751fcb82016-02-02 19:48:46 +000095 connect_fd, connect_inode);
96
97 if (connect(connect_fd, listen_sa, *len))
98 perror_msg_and_fail("connect");
Dmitry V. Levine139cbe2016-06-20 22:52:49 +000099 printf("connect(%d<socket:[%lu]>, {sa_family=AF_UNIX"
Dmitry V. Levin751fcb82016-02-02 19:48:46 +0000100 ", sun_path=\"%s\"}, %u) = 0\n",
101 connect_fd, connect_inode, av[1], (unsigned) *len);
102
103 struct sockaddr * const accept_sa = tail_alloc(sizeof(addr));
104 memset(accept_sa, 0, sizeof(addr));
105 *len = sizeof(addr);
106 int accept_fd = accept(listen_fd, accept_sa, len);
107 if (accept_fd < 0)
108 perror_msg_and_fail("accept");
109 unsigned long accept_inode = inode_of_sockfd(accept_fd);
Dmitry V. Levinf52753a2016-06-22 23:40:56 +0000110 printf("accept(%d<socket:[%lu]>, {sa_family=AF_UNIX}"
Dmitry V. Levin751fcb82016-02-02 19:48:46 +0000111 ", [%u]) = %d<socket:[%lu]>\n",
112 listen_fd, listen_inode, (unsigned) *len,
113 accept_fd, accept_inode);
114
115 memset(listen_sa, 0, sizeof(addr));
116 *len = sizeof(addr);
117 if (getpeername(connect_fd, listen_sa, len))
118 perror_msg_and_fail("getpeername");
Dmitry V. Levine139cbe2016-06-20 22:52:49 +0000119 printf("getpeername(%d<socket:[%lu]>, {sa_family=AF_UNIX"
Dmitry V. Levin751fcb82016-02-02 19:48:46 +0000120 ", sun_path=\"%s\"}, [%u]) = 0\n", connect_fd, connect_inode,
121 av[1], (unsigned) *len);
122
123 char text[] = "text";
124 assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, 0)
125 == sizeof(text) - 1);
126 printf("sendto(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT"
127 ", NULL, 0) = %u\n",
128 connect_fd, connect_inode, text,
129 (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
130
131 assert(close(connect_fd) == 0);
132 printf("close(%d<socket:[%lu]>) = 0\n", connect_fd, connect_inode);
133
134 assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, NULL)
135 == sizeof(text) - 1);
136 printf("recvfrom(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT"
137 ", NULL, NULL) = %u\n",
138 accept_fd, accept_inode, text,
139 (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
140
141 assert(close(accept_fd) == 0);
142 printf("close(%d<socket:[%lu]>) = 0\n", accept_fd, accept_inode);
143
Dmitry V. Levine139cbe2016-06-20 22:52:49 +0000144 connect_fd = socket(AF_UNIX, SOCK_STREAM, 0);
Dmitry V. Levin751fcb82016-02-02 19:48:46 +0000145 if (connect_fd < 0)
146 perror_msg_and_fail("socket");
147 connect_inode = inode_of_sockfd(connect_fd);
Dmitry V. Levine139cbe2016-06-20 22:52:49 +0000148 printf("socket(AF_UNIX, SOCK_STREAM, 0) = %d<socket:[%lu]>\n",
Dmitry V. Levin751fcb82016-02-02 19:48:46 +0000149 connect_fd, connect_inode);
150
151 *optval = 1;
152 *len = sizeof(*optval);
153 if (setsockopt(connect_fd, SOL_SOCKET, SO_PASSCRED, optval, *len))
154 perror_msg_and_fail("setsockopt");
155 printf("setsockopt(%d<socket:[%lu]>, SOL_SOCKET, SO_PASSCRED"
156 ", [%u], %u) = 0\n",
157 connect_fd, connect_inode, *optval, (unsigned) *len);
158
159 memset(listen_sa, 0, sizeof(addr));
160 *len = sizeof(addr);
161 if (getsockname(listen_fd, listen_sa, len))
162 perror_msg_and_fail("getsockname");
Dmitry V. Levine139cbe2016-06-20 22:52:49 +0000163 printf("getsockname(%d<socket:[%lu]>, {sa_family=AF_UNIX"
Dmitry V. Levin751fcb82016-02-02 19:48:46 +0000164 ", sun_path=\"%s\"}, [%u]) = 0\n",
165 listen_fd, listen_inode, av[1], (unsigned) *len);
166
167 if (connect(connect_fd, listen_sa, *len))
168 perror_msg_and_fail("connect");
Dmitry V. Levine139cbe2016-06-20 22:52:49 +0000169 printf("connect(%d<socket:[%lu]>, {sa_family=AF_UNIX"
Dmitry V. Levin751fcb82016-02-02 19:48:46 +0000170 ", sun_path=\"%s\"}, %u) = 0\n",
171 connect_fd, connect_inode, av[1], (unsigned) *len);
172
173 memset(accept_sa, 0, sizeof(addr));
174 *len = sizeof(addr);
175 accept_fd = accept(listen_fd, accept_sa, len);
176 if (accept_fd < 0)
177 perror_msg_and_fail("accept");
178 accept_inode = inode_of_sockfd(accept_fd);
179 const char * const sun_path1 =
180 ((struct sockaddr_un *) accept_sa) -> sun_path + 1;
Dmitry V. Levine139cbe2016-06-20 22:52:49 +0000181 printf("accept(%d<socket:[%lu]>, {sa_family=AF_UNIX"
Dmitry V. Levin751fcb82016-02-02 19:48:46 +0000182 ", sun_path=@\"%s\"}, [%u]) = %d<socket:[%lu]>\n",
183 listen_fd, listen_inode, sun_path1, (unsigned) *len,
184 accept_fd, accept_inode);
185
186 memset(listen_sa, 0, sizeof(addr));
187 *len = sizeof(addr);
188 if (getpeername(connect_fd, listen_sa, len))
189 perror_msg_and_fail("getpeername");
Dmitry V. Levine139cbe2016-06-20 22:52:49 +0000190 printf("getpeername(%d<socket:[%lu]>, {sa_family=AF_UNIX"
Dmitry V. Levin751fcb82016-02-02 19:48:46 +0000191 ", sun_path=\"%s\"}, [%u]) = 0\n",
192 connect_fd, connect_inode, av[1], (unsigned) *len);
193
194 memset(accept_sa, 0, sizeof(addr));
195 *len = sizeof(addr);
196 if (getsockname(connect_fd, accept_sa, len))
197 perror_msg_and_fail("getsockname");
Dmitry V. Levine139cbe2016-06-20 22:52:49 +0000198 printf("getsockname(%d<socket:[%lu]>, {sa_family=AF_UNIX"
Dmitry V. Levin751fcb82016-02-02 19:48:46 +0000199 ", sun_path=@\"%s\"}, [%u]) = 0\n",
200 connect_fd, connect_inode, sun_path1, (unsigned) *len);
201
202 assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, 0)
203 == sizeof(text) - 1);
204 printf("sendto(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT"
205 ", NULL, 0) = %u\n",
206 connect_fd, connect_inode, text,
207 (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
208
209 assert(close(connect_fd) == 0);
210 printf("close(%d<socket:[%lu]>) = 0\n", connect_fd, connect_inode);
211
212 assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_DONTWAIT, NULL, NULL)
213 == sizeof(text) - 1);
214 printf("recvfrom(%d<socket:[%lu]>, \"%s\", %u, MSG_DONTWAIT"
215 ", NULL, NULL) = %u\n",
216 accept_fd, accept_inode, text,
217 (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1);
218
219 assert(close(accept_fd) == 0);
220 printf("close(%d<socket:[%lu]>) = 0\n", accept_fd, accept_inode);
221
222 assert(unlink(av[1]) == 0);
223
224 assert(close(listen_fd) == 0);
225 printf("close(%d<socket:[%lu]>) = 0\n",
226 listen_fd, listen_inode);
227
228 puts("+++ exited with 0 +++");
229 return 0;
230}