2 * Copyright (c) 2017 Hartmut Brandt <harti@FreeBSD.org>
3 * Copyright (c) 2017 Gleb Smirnoff <glebius@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/event.h>
33 #include <sys/filio.h>
34 #include <sys/ioctl.h>
35 #include <netinet/in.h>
42 * This regression test is against a scenario when a socket is first added
43 * to a kqueue, and only then is put into listening state.
44 * This weird scenario was made a valid one in r313043, and shouldn't be
51 struct sockaddr_in addr;
54 int kq, sock, opt, pid, nev, fd;
56 if ((kq = kqueue()) == -1)
59 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
62 EV_SET(&ev[0], sock, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, NULL);
63 EV_SET(&ev[1], sock, EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, NULL);
66 if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt)) == -1)
69 if (kevent(kq, ev, 2, NULL, 0, NULL) == -1)
72 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1)
75 memset(&addr, 0, sizeof(addr));
76 addr.sin_family = AF_INET;
77 addr.sin_port = htons(10000);
79 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
81 if (listen(sock, 0x80) == -1)
84 if (ioctl(sock, FIONBIO, &opt) == -1)
85 err(1, "ioctl(FIONBIO)");
87 if (kevent(kq, ev, 2, NULL, 0, NULL) == -1)
94 if (close(sock) == -1)
96 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
98 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
101 nev = kevent(kq, NULL, 0, ev, 2, NULL);
104 for (int i = 0; i < nev; ++i) {
105 if (ev[i].ident == (uintptr_t )sock) {
106 fd = accept(ev[i].ident,
107 (struct sockaddr *)&addr, &socklen);