From owner-svn-src-head@freebsd.org Tue Feb 14 21:56:03 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 1D7A2CDFD15; Tue, 14 Feb 2017 21:56:03 +0000 (UTC) (envelope-from glebius@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id D185315B8; Tue, 14 Feb 2017 21:56:02 +0000 (UTC) (envelope-from glebius@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v1ELu16g068758; Tue, 14 Feb 2017 21:56:01 GMT (envelope-from glebius@FreeBSD.org) Received: (from glebius@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v1ELu1rq068755; Tue, 14 Feb 2017 21:56:01 GMT (envelope-from glebius@FreeBSD.org) Message-Id: <201702142156.v1ELu1rq068755@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: glebius set sender to glebius@FreeBSD.org using -f From: Gleb Smirnoff Date: Tue, 14 Feb 2017 21:56:01 +0000 (UTC) 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 X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Feb 2017 21:56:03 -0000 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 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 + * Copyright (c) 2017 Gleb Smirnoff + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * 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"); + } + } + } +}