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 PATH "/tmp/123"
49 #define ITERATIONS 1000000
52 stream_server(int listenfd)
57 acceptfd = accept(listenfd, NULL, NULL);
59 warn("stream_server: accept");
70 struct sockaddr_un sun;
75 bzero(&sun, sizeof(sun));
76 sun.sun_len = sizeof(sun);
77 sun.sun_family = AF_UNIX;
78 strcpy(sun.sun_path, PATH);
79 for (i = 0; i < ITERATIONS; i++) {
80 fd = socket(PF_UNIX, SOCK_STREAM, 0);
82 warn("stream_client: socket");
85 len = sendto(fd, &c, sizeof(c), 0, (struct sockaddr *)&sun,
88 warn("stream_client: sendto");
96 struct sockaddr_un sun;
100 listenfd = socket(PF_UNIX, SOCK_STREAM, 0);
102 err(-1, "stream_test: socket");
104 bzero(&sun, sizeof(sun));
105 sun.sun_len = sizeof(sun);
106 sun.sun_family = AF_UNIX;
107 strcpy(sun.sun_path, PATH);
109 if (bind(listenfd, (struct sockaddr *)&sun, sizeof(sun)) < 0)
110 err(-1, "stream_test: bind");
112 if (listen(listenfd, -1) < 0)
113 err(-1, "stream_test: listen");
117 err(-1, "stream_test: fork");
122 kill(childpid, SIGTERM);
125 stream_server(listenfd);
131 datagram_server(int serverfd)
137 len = recv(serverfd, &c, sizeof(c), 0);
139 warn("datagram_server: recv");
144 datagram_client(void)
146 struct sockaddr_un sun;
151 bzero(&sun, sizeof(sun));
152 sun.sun_len = sizeof(sun);
153 sun.sun_family = AF_UNIX;
154 strcpy(sun.sun_path, PATH);
155 for (i = 0; i < ITERATIONS; i++) {
156 fd = socket(PF_UNIX, SOCK_DGRAM, 0);
158 warn("datagram_client: socket");
161 len = sendto(fd, &c, sizeof(c), 0, (struct sockaddr *)&sun,
164 warn("datagram_client: sendto");
172 struct sockaddr_un sun;
176 serverfd = socket(PF_UNIX, SOCK_DGRAM, 0);
178 err(-1, "datagram_test: socket");
180 bzero(&sun, sizeof(sun));
181 sun.sun_len = sizeof(sun);
182 sun.sun_family = AF_UNIX;
183 strcpy(sun.sun_path, PATH);
185 if (bind(serverfd, (struct sockaddr *)&sun, sizeof(sun)) < 0)
186 err(-1, "datagram_test: bind");
190 err(-1, "datagram_test: fork");
195 kill(childpid, SIGTERM);
198 datagram_server(serverfd);
204 main(int argc, char *argv[])