1 /*- 2 * Copyright (c) 2004-2005 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/types.h> 28 #include <sys/socket.h> 29 30 #include <netinet/in.h> 31 32 #include <arpa/inet.h> 33 34 #include <err.h> 35 #include <errno.h> 36 #include <fcntl.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <unistd.h> 40 41 /* 42 * The listenclose regression test is designed to catch kernel bugs that may 43 * trigger as a result of performing a close on a listen() socket with as-yet 44 * unaccepted connections in its queues. This results in the connections 45 * being aborted, which is a not-often-followed code path. To do this, we 46 * create a local TCP socket, build a non-blocking connection to it, and then 47 * close the accept socket. The connection must be non-blocking or the 48 * program will block and as such connect() will not return as accept() is 49 * never called. 50 */ 51 52 int 53 main(void) 54 { 55 int listen_sock, connect_sock; 56 struct sockaddr_in sin; 57 socklen_t len; 58 u_short port; 59 int arg; 60 61 listen_sock = socket(PF_INET, SOCK_STREAM, 0); 62 if (listen_sock == -1) 63 errx(-1, 64 "socket(PF_INET, SOCK_STREAM, 0) for listen socket: %s", 65 strerror(errno)); 66 67 68 bzero(&sin, sizeof(sin)); 69 sin.sin_len = sizeof(sin); 70 sin.sin_family = AF_INET; 71 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 72 sin.sin_port = 0; 73 74 if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) 75 errx(-1, "bind(%s, %d) for listen socket: %s", 76 inet_ntoa(sin.sin_addr), 0, strerror(errno)); 77 78 len = sizeof(sin); 79 if (getsockname(listen_sock, (struct sockaddr *)&sin, &len) < 0) 80 errx(-1, "getsockname() for listen socket: %s", 81 strerror(errno)); 82 port = sin.sin_port; 83 84 if (listen(listen_sock, -1) < 0) 85 errx(-1, "listen() for listen socket: %s", strerror(errno)); 86 87 connect_sock = socket(PF_INET, SOCK_STREAM, 0); 88 if (connect_sock == -1) 89 errx(-1, "socket(PF_INET, SOCK_STREAM, 0) for connect " 90 "socket: %s", strerror(errno)); 91 92 arg = O_NONBLOCK; 93 if (fcntl(connect_sock, F_SETFL, &arg) < 0) 94 errx(-1, "socket(PF_INET, SOCK_STREAM, 0) for connect socket" 95 ": %s", strerror(errno)); 96 97 bzero(&sin, sizeof(sin)); 98 sin.sin_len = sizeof(sin); 99 sin.sin_family = AF_INET; 100 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 101 sin.sin_port = port; 102 103 if (connect(connect_sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) 104 errx(-1, "connect() for connect socket: %s", strerror(errno)); 105 close(connect_sock); 106 close(listen_sock); 107 108 return (0); 109 } 110