2 * Copyright (c) 2004-2005 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
29 #include <sys/types.h>
30 #include <sys/socket.h>
32 #include <netinet/in.h>
34 #include <arpa/inet.h>
44 * The listenclose regression test is designed to catch kernel bugs that may
45 * trigger as a result of performing a close on a listen() socket with as-yet
46 * unaccepted connections in its queues. This results in the connections
47 * being aborted, which is a not-often-followed code path. To do this, we
48 * create a local TCP socket, build a non-blocking connection to it, and then
49 * close the accept socket. The connection must be non-blocking or the
50 * program will block and as such connect() will not return as accept() is
55 main(int argc, char *argv[])
57 int listen_sock, connect_sock;
58 struct sockaddr_in sin;
63 listen_sock = socket(PF_INET, SOCK_STREAM, 0);
64 if (listen_sock == -1)
66 "socket(PF_INET, SOCK_STREAM, 0) for listen socket: %s",
70 bzero(&sin, sizeof(sin));
71 sin.sin_len = sizeof(sin);
72 sin.sin_family = AF_INET;
73 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
76 if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
77 errx(-1, "bind(%s, %d) for listen socket: %s",
78 inet_ntoa(sin.sin_addr), 0, strerror(errno));
81 if (getsockname(listen_sock, (struct sockaddr *)&sin, &len) < 0)
82 errx(-1, "getsockname() for listen socket: %s",
86 if (listen(listen_sock, -1) < 0)
87 errx(-1, "listen() for listen socket: %s", strerror(errno));
89 connect_sock = socket(PF_INET, SOCK_STREAM, 0);
90 if (connect_sock == -1)
91 errx(-1, "socket(PF_INET, SOCK_STREAM, 0) for connect "
92 "socket: %s", strerror(errno));
95 if (fcntl(connect_sock, F_SETFL, &arg) < 0)
96 errx(-1, "socket(PF_INET, SOCK_STREAM, 0) for connect socket"
97 ": %s", strerror(errno));
99 bzero(&sin, sizeof(sin));
100 sin.sin_len = sizeof(sin);
101 sin.sin_family = AF_INET;
102 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
105 if (connect(connect_sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
106 errx(-1, "connect() for connect socket: %s", strerror(errno));