1 /* $NetBSD: t_unix.c,v 1.11 2013/11/13 21:41:23 christos Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/cdefs.h>
41 __RCSID("$Id: t_unix.c,v 1.11 2013/11/13 21:41:23 christos Exp $");
43 #define getprogname() argv[0]
51 #include <sys/param.h>
52 #include <sys/socket.h>
64 #define FAIL(msg, ...) err(EXIT_FAILURE, msg, ## __VA_ARGS__)
68 #define FAIL(msg, ...) \
70 ATF_CHECK_MSG(0, msg, ## __VA_ARGS__); \
72 } while (/*CONSTCOND*/0)
76 #define OF offsetof(struct sockaddr_un, sun_path)
79 print(const char *msg, struct sockaddr_un *addr, socklen_t len)
83 printf("%s: client socket length: %zu\n", msg, (size_t)len);
84 printf("%s: client family %d\n", msg, addr->sun_family);
86 printf("%s: client len %d\n", msg, addr->sun_len);
88 printf("%s: socket name: ", msg);
89 for (i = 0; i < len - OF; i++) {
90 int ch = addr->sun_path[i];
91 if (ch < ' ' || '~' < ch)
92 printf("\\x%02x", ch);
103 struct sockaddr_un sun;
107 guard1 = guard2 = 's';
109 memset(&sun, 0, sizeof(sun));
111 if ((s = accept(s, (struct sockaddr *)&sun, &len)) == -1)
114 FAIL("guard1 = '%c'", guard1);
116 FAIL("guard2 = '%c'", guard2);
118 print("accept", &sun, len);
121 FAIL("len %d != 2", len);
122 if (sun.sun_family != AF_UNIX)
123 FAIL("sun->sun_family %d != AF_UNIX", sun.sun_family);
125 if (sun.sun_len != 2)
126 FAIL("sun->sun_len %d != 2", sun.sun_len);
128 for (size_t i = 0; i < sizeof(sun.sun_path); i++)
130 FAIL("sun.sun_path[%zu] %d != NULL", i,
140 test(bool closeit, size_t len)
144 int srvr = -1, clnt = -1, acpt = -1;
145 struct sockaddr_un *sock_addr = NULL, *sun = NULL;
146 socklen_t sock_addrlen;
148 srvr = socket(AF_UNIX, SOCK_STREAM, 0);
150 FAIL("socket(srvrer)");
154 if ((sun = calloc(1, slen)) == NULL)
157 srvr = socket(AF_UNIX, SOCK_STREAM, 0);
161 memset(sun->sun_path, 'a', len);
162 sun->sun_path[len] = '\0';
163 (void)unlink(sun->sun_path);
169 sun->sun_family = AF_UNIX;
171 if (bind(srvr, (struct sockaddr *)sun, sl) == -1) {
172 if (errno == EINVAL && sl >= 256) {
179 if (listen(srvr, SOMAXCONN) == -1)
182 clnt = socket(AF_UNIX, SOCK_STREAM, 0);
184 FAIL("socket(client)");
186 if (connect(clnt, (const struct sockaddr *)sun, sl) == -1)
190 if (close(clnt) == -1)
198 * Both linux and NetBSD return ENOTCONN, why?
201 socklen_t peer_addrlen;
202 sockaddr_un peer_addr;
204 peer_addrlen = sizeof(peer_addr);
205 memset(&peer_addr, 0, sizeof(peer_addr));
206 if (getpeername(srvr, (struct sockaddr *)&peer_addr,
207 &peer_addrlen) == -1)
209 print("peer", &peer_addr, peer_addrlen);
213 if ((sock_addr = calloc(1, slen)) == NULL)
216 if (getsockname(srvr, (struct sockaddr *)sock_addr, &sock_addrlen)
219 print("sock", sock_addr, sock_addrlen);
221 if (sock_addr->sun_family != AF_UNIX)
222 FAIL("sock_addr->sun_family %d != AF_UNIX",
223 sock_addr->sun_family);
226 if (sock_addrlen LX != len)
227 FAIL("sock_addr_len %zu != %zu", (size_t)sock_addrlen, len);
229 if (sock_addr->sun_len != sl)
230 FAIL("sock_addr.sun_len %d != %zu", sock_addr->sun_len,
233 for (size_t i = 0; i < slen - OF; i++)
234 if (sock_addr->sun_path[i] != sun->sun_path[i])
235 FAIL("sock_addr.sun_path[%zu] %d != "
236 "sun->sun_path[%zu] %d\n", i,
237 sock_addr->sun_path[i], i, sun->sun_path[i]);
243 if (clnt != -1 && !closeit)
254 if (clnt != -1 && !closeit)
263 ATF_TC(sockaddr_un_len_exceed);
264 ATF_TC_HEAD(sockaddr_un_len_exceed, tc)
267 atf_tc_set_md_var(tc, "descr", "Check that exceeding the size of "
268 "unix domain sockets does not trash memory or kernel when "
269 "exceeding the size of the fixed sun_path");
272 ATF_TC_BODY(sockaddr_un_len_exceed, tc)
274 ATF_REQUIRE_MSG(test(false, 254) == -1, "test(false, 254): %s",
278 ATF_TC(sockaddr_un_len_max);
279 ATF_TC_HEAD(sockaddr_un_len_max, tc)
282 atf_tc_set_md_var(tc, "descr", "Check that we can use the maximum "
283 "unix domain socket pathlen (253): 255 - sizeof(sun_len) - "
284 "sizeof(sun_family)");
287 ATF_TC_BODY(sockaddr_un_len_max, tc)
289 ATF_REQUIRE_MSG(test(false, 253) == 0, "test(false, 253): %s",
293 ATF_TC(sockaddr_un_closed);
294 ATF_TC_HEAD(sockaddr_un_closed, tc)
297 atf_tc_set_md_var(tc, "descr", "Check that we can use the accepted "
298 "address of unix domain socket when closed");
301 ATF_TC_BODY(sockaddr_un_closed, tc)
303 ATF_REQUIRE_MSG(test(true, 100) == 0, "test(true, 100): %s",
310 ATF_TP_ADD_TC(tp, sockaddr_un_len_exceed);
311 ATF_TP_ADD_TC(tp, sockaddr_un_len_max);
312 ATF_TP_ADD_TC(tp, sockaddr_un_closed);
313 return atf_no_error();
317 main(int argc, char *argv[])
322 fprintf(stderr, "Usage: %s <len>\n", getprogname());
325 test(false, atoi(argv[1]));
326 test(true, atoi(argv[1]));