2 * Copyright (c) 2006 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Attempts to exercise UNIX domain socket races relating to the non-atomic
31 * connect-and-send properties of sendto(). As the result of such a race is
32 * a kernel panic, this test simply completes or doesn't.
34 * XXX: Despite implementing support for sendto() on stream sockets with
35 * implied connect, the appropriate flag isn't set in the FreeBSD kernel so
36 * it does not work. For now, don't call the stream test.
39 #include <sys/types.h>
40 #include <sys/socket.h>
48 #define ITERATIONS 1000000
50 static char socket_path[] = "tmp.XXXXXX";
53 stream_server(int listenfd)
58 acceptfd = accept(listenfd, NULL, NULL);
60 warn("stream_server: accept");
71 struct sockaddr_un sun;
76 bzero(&sun, sizeof(sun));
77 sun.sun_len = sizeof(sun);
78 sun.sun_family = AF_UNIX;
79 strcpy(sun.sun_path, socket_path);
80 for (i = 0; i < ITERATIONS; i++) {
81 fd = socket(PF_UNIX, SOCK_STREAM, 0);
83 warn("stream_client: socket");
86 len = sendto(fd, &c, sizeof(c), 0, (struct sockaddr *)&sun,
89 warn("stream_client: sendto");
97 struct sockaddr_un sun;
101 listenfd = socket(PF_UNIX, SOCK_STREAM, 0);
103 err(-1, "stream_test: socket");
105 bzero(&sun, sizeof(sun));
106 sun.sun_len = sizeof(sun);
107 sun.sun_family = AF_UNIX;
108 strcpy(sun.sun_path, socket_path);
110 if (bind(listenfd, (struct sockaddr *)&sun, sizeof(sun)) < 0)
111 err(-1, "stream_test: bind");
113 if (listen(listenfd, -1) < 0)
114 err(-1, "stream_test: listen");
118 err(-1, "stream_test: fork");
123 kill(childpid, SIGTERM);
126 stream_server(listenfd);
128 (void)unlink(socket_path);
132 datagram_server(int serverfd)
138 len = recv(serverfd, &c, sizeof(c), 0);
140 warn("datagram_server: recv");
145 datagram_client(void)
147 struct sockaddr_un sun;
152 bzero(&sun, sizeof(sun));
153 sun.sun_len = sizeof(sun);
154 sun.sun_family = AF_UNIX;
155 strcpy(sun.sun_path, socket_path);
156 for (i = 0; i < ITERATIONS; i++) {
157 fd = socket(PF_UNIX, SOCK_DGRAM, 0);
159 warn("datagram_client: socket");
162 len = sendto(fd, &c, sizeof(c), 0, (struct sockaddr *)&sun,
165 warn("datagram_client: sendto");
173 struct sockaddr_un sun;
177 serverfd = socket(PF_UNIX, SOCK_DGRAM, 0);
179 err(-1, "datagram_test: socket");
181 bzero(&sun, sizeof(sun));
182 sun.sun_len = sizeof(sun);
183 sun.sun_family = AF_UNIX;
184 strcpy(sun.sun_path, socket_path);
186 if (bind(serverfd, (struct sockaddr *)&sun, sizeof(sun)) < 0)
187 err(-1, "datagram_test: bind");
191 err(-1, "datagram_test: fork");
196 kill(childpid, SIGTERM);
199 datagram_server(serverfd);
201 (void)unlink(socket_path);
208 if (mkstemp(socket_path) == -1)
209 err(1, "mkstemp failed");
210 (void)unlink(socket_path);