aboutsummaryrefslogtreecommitdiff
path: root/tools/regression
diff options
context:
space:
mode:
authorGleb Smirnoff <glebius@FreeBSD.org>2017-02-14 21:56:01 +0000
committerGleb Smirnoff <glebius@FreeBSD.org>2017-02-14 21:56:01 +0000
commitb748360b8afb00561d7fac791b135c07718a6cee (patch)
tree14bfe3ef9da51fd701a9f5cb241b767da478c3f9 /tools/regression
parent661c4fe512f6e8311cc03f188a4fc3808bd52e09 (diff)
downloadsrc-b748360b8afb00561d7fac791b135c07718a6cee.tar.gz
src-b748360b8afb00561d7fac791b135c07718a6cee.zip
Add a regression test for putting a socket on kqueue, and then doing
listen(2) on it (see r313043). Based on Hartmut's code.
Notes
Notes: svn path=/head/; revision=313750
Diffstat (limited to 'tools/regression')
-rw-r--r--tools/regression/sockets/listen_kqueue/Makefile7
-rw-r--r--tools/regression/sockets/listen_kqueue/listen_kqueue.c114
2 files changed, 121 insertions, 0 deletions
diff --git a/tools/regression/sockets/listen_kqueue/Makefile b/tools/regression/sockets/listen_kqueue/Makefile
new file mode 100644
index 000000000000..d49a597fc8f5
--- /dev/null
+++ b/tools/regression/sockets/listen_kqueue/Makefile
@@ -0,0 +1,7 @@
+# $FreeBSD$
+
+PROG= listen_kqueue
+MAN=
+WARNS?= 6
+
+.include <bsd.prog.mk>
diff --git a/tools/regression/sockets/listen_kqueue/listen_kqueue.c b/tools/regression/sockets/listen_kqueue/listen_kqueue.c
new file mode 100644
index 000000000000..dd36a0f53296
--- /dev/null
+++ b/tools/regression/sockets/listen_kqueue/listen_kqueue.c
@@ -0,0 +1,114 @@
+/*-
+ * Copyright (c) 2017 Hartmut Brandt <harti@FreeBSD.org>
+ * Copyright (c) 2017 Gleb Smirnoff <glebius@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/event.h>
+#include <sys/filio.h>
+#include <sys/ioctl.h>
+#include <netinet/in.h>
+#include <err.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+/*
+ * This regression test is against a scenario when a socket is first added
+ * to a kqueue, and only then is put into listening state.
+ * This weird scenario was made a valid one in r313043, and shouldn't be
+ * broken later.
+ */
+
+int
+main()
+{
+ struct sockaddr_in addr;
+ struct kevent ev[2];
+ socklen_t socklen;
+ int kq, sock, opt, pid, nev, fd;
+
+ if ((kq = kqueue()) == -1)
+ err(1, "kqueue");
+
+ if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
+ err(1, "socket");
+
+ EV_SET(&ev[0], sock, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, NULL);
+ EV_SET(&ev[1], sock, EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, NULL);
+
+ opt = 1;
+ if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt)) == -1)
+ err(1, "setsockopt");
+
+ if (kevent(kq, ev, 2, NULL, 0, NULL) == -1)
+ err(1, "kevent");
+
+ if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1)
+ err(1, "setsockopt");
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(10000);
+
+ if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
+ err(1, "bind");
+ if (listen(sock, 0x80) == -1)
+ err(1, "listen");
+
+ if (ioctl(sock, FIONBIO, &opt) == -1)
+ err(1, "ioctl(FIONBIO)");
+
+ if (kevent(kq, ev, 2, NULL, 0, NULL) == -1)
+ err(1, "kevent");
+
+ pid = fork();
+ if (pid == -1)
+ err(1, "fork");
+ if (pid == 0) {
+ if (close(sock) == -1)
+ err(1, "close");
+ if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
+ err(1, "socket");
+ if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
+ err(1, "connect");
+ } else {
+ nev = kevent(kq, NULL, 0, ev, 2, NULL);
+ if (nev < 1)
+ err(1, "kevent");
+ for (int i = 0; i < nev; ++i) {
+ if (ev[i].ident == (uintptr_t )sock) {
+ fd = accept(ev[i].ident,
+ (struct sockaddr *)&addr, &socklen);
+ if (fd == -1)
+ err(1, "accept");
+ printf("OK\n");
+ }
+ }
+ }
+}