]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/regression/sockets/listen_kqueue/listen_kqueue.c
Re-add opencsd as a vendor import from the dist directory
[FreeBSD/FreeBSD.git] / tools / regression / sockets / listen_kqueue / listen_kqueue.c
1 /*-
2  * Copyright (c) 2017 Hartmut Brandt <harti@FreeBSD.org>
3  * Copyright (c) 2017 Gleb Smirnoff <glebius@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
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>
36 #include <err.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 /*
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
45  * broken later.
46  */
47
48 int
49 main()
50 {
51         struct sockaddr_in addr;
52         struct kevent ev[2];
53         socklen_t socklen;
54         int kq, sock, opt, pid, nev, fd;
55
56         if ((kq = kqueue()) == -1)
57                 err(1, "kqueue");
58
59         if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
60                 err(1, "socket");
61
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);
64
65         opt = 1;
66         if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt)) == -1)
67                 err(1, "setsockopt");
68
69         if (kevent(kq, ev, 2, NULL, 0, NULL) == -1)
70             err(1, "kevent");
71
72         if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1)
73                 err(1, "setsockopt");
74
75         memset(&addr, 0, sizeof(addr));
76         addr.sin_family = AF_INET;
77         addr.sin_port = htons(10000);
78
79         if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
80                 err(1, "bind");
81         if (listen(sock, 0x80) == -1)
82                 err(1, "listen");
83
84         if (ioctl(sock, FIONBIO, &opt) == -1)
85                 err(1, "ioctl(FIONBIO)");
86
87         if (kevent(kq, ev, 2, NULL, 0, NULL) == -1)
88                 err(1, "kevent");
89
90         pid = fork();
91         if (pid == -1)
92                 err(1, "fork");
93         if (pid == 0) {
94                 if (close(sock) == -1)
95                         err(1, "close");
96                 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
97                         err(1, "socket");
98                 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
99                         err(1, "connect");
100         } else {
101                 nev = kevent(kq, NULL, 0, ev, 2, NULL);
102                 if (nev < 1)
103                         err(1, "kevent");
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);
108                                 if (fd == -1)
109                                         err(1, "accept");
110                                 printf("OK\n");
111                         }
112                 }
113         }
114 }