Date: Tue, 14 Feb 2017 21:56:01 +0000 (UTC) From: Gleb Smirnoff <glebius@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r313750 - head/tools/regression/sockets/listen_kqueue Message-ID: <201702142156.v1ELu1rq068755@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: glebius Date: Tue Feb 14 21:56:01 2017 New Revision: 313750 URL: https://svnweb.freebsd.org/changeset/base/313750 Log: Add a regression test for putting a socket on kqueue, and then doing listen(2) on it (see r313043). Based on Hartmut's code. Added: head/tools/regression/sockets/listen_kqueue/ head/tools/regression/sockets/listen_kqueue/Makefile (contents, props changed) head/tools/regression/sockets/listen_kqueue/listen_kqueue.c (contents, props changed) Added: head/tools/regression/sockets/listen_kqueue/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/regression/sockets/listen_kqueue/Makefile Tue Feb 14 21:56:01 2017 (r313750) @@ -0,0 +1,7 @@ +# $FreeBSD$ + +PROG= listen_kqueue +MAN= +WARNS?= 6 + +.include <bsd.prog.mk> Added: head/tools/regression/sockets/listen_kqueue/listen_kqueue.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/regression/sockets/listen_kqueue/listen_kqueue.c Tue Feb 14 21:56:01 2017 (r313750) @@ -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"); + } + } + } +}
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201702142156.v1ELu1rq068755>