From owner-svn-src-all@freebsd.org Mon Apr 27 15:59:36 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 0BE542BD809; Mon, 27 Apr 2020 15:59:36 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 499qGb4CxSz4FP1; Mon, 27 Apr 2020 15:59:35 +0000 (UTC) (envelope-from markj@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 mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 8BF6BCF80; Mon, 27 Apr 2020 15:59:35 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 03RFxZON076812; Mon, 27 Apr 2020 15:59:35 GMT (envelope-from markj@FreeBSD.org) Received: (from markj@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 03RFxYhB076807; Mon, 27 Apr 2020 15:59:34 GMT (envelope-from markj@FreeBSD.org) Message-Id: <202004271559.03RFxYhB076807@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: markj set sender to markj@FreeBSD.org using -f From: Mark Johnston Date: Mon, 27 Apr 2020 15:59:34 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r360381 - in head/tests/sys: fifo kern/pipe X-SVN-Group: head X-SVN-Commit-Author: markj X-SVN-Commit-Paths: in head/tests/sys: fifo kern/pipe X-SVN-Commit-Revision: 360381 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Apr 2020 15:59:36 -0000 Author: markj Date: Mon Apr 27 15:59:34 2020 New Revision: 360381 URL: https://svnweb.freebsd.org/changeset/base/360381 Log: Add some regression tests for read and write kevents on pipes. Submitted by: Jan Kokemüller MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D24528 Added: head/tests/sys/fifo/fifo_kqueue.c (contents, props changed) head/tests/sys/kern/pipe/pipe_kqueue_test.c (contents, props changed) Modified: head/tests/sys/fifo/Makefile head/tests/sys/kern/pipe/Makefile Modified: head/tests/sys/fifo/Makefile ============================================================================== --- head/tests/sys/fifo/Makefile Mon Apr 27 15:59:19 2020 (r360380) +++ head/tests/sys/fifo/Makefile Mon Apr 27 15:59:34 2020 (r360381) @@ -4,6 +4,7 @@ TESTSDIR= ${TESTSBASE}/sys/fifo PLAIN_TESTS_C+= fifo_create PLAIN_TESTS_C+= fifo_io +ATF_TESTS_C+= fifo_kqueue PLAIN_TESTS_C+= fifo_misc PLAIN_TESTS_C+= fifo_open Added: head/tests/sys/fifo/fifo_kqueue.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tests/sys/fifo/fifo_kqueue.c Mon Apr 27 15:59:34 2020 (r360381) @@ -0,0 +1,430 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2020 Jan Kokemüller + * + * 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +ATF_TC_WITHOUT_HEAD(fifo_kqueue__writes); +ATF_TC_BODY(fifo_kqueue__writes, tc) +{ + int p[2] = { -1, -1 }; + + ATF_REQUIRE(mkfifo("testfifo", 0600) == 0); + + ATF_REQUIRE((p[0] = open("testfifo", + O_RDONLY | O_CLOEXEC | O_NONBLOCK)) >= 0); + ATF_REQUIRE((p[1] = open("testfifo", + O_WRONLY | O_CLOEXEC | O_NONBLOCK)) >= 0); + + int kq = kqueue(); + ATF_REQUIRE(kq >= 0); + + struct kevent kev[32]; + EV_SET(&kev[0], p[1], EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, 0); + EV_SET(&kev[1], p[1], EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, 0); + + ATF_REQUIRE(kevent(kq, kev, 2, NULL, 0, NULL) == 0); + + /* A new writer should immediately get a EVFILT_WRITE event. */ + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]); + ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); + ATF_REQUIRE(kev[0].flags == EV_CLEAR); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == 16384); + ATF_REQUIRE(kev[0].udata == 0); + + /* Filling up the pipe should make the EVFILT_WRITE disappear. */ + + char c = 0; + ssize_t r; + while ((r = write(p[1], &c, 1)) == 1) { + } + ATF_REQUIRE(r < 0); + ATF_REQUIRE(errno == EAGAIN || errno == EWOULDBLOCK); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 0); + + /* Reading (PIPE_BUF - 1) bytes will not trigger a EVFILT_WRITE yet. */ + + for (int i = 0; i < PIPE_BUF - 1; ++i) { + ATF_REQUIRE(read(p[0], &c, 1) == 1); + } + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 0); + + /* Reading one additional byte triggers the EVFILT_WRITE. */ + + ATF_REQUIRE(read(p[0], &c, 1) == 1); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]); + ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); + ATF_REQUIRE(kev[0].flags == EV_CLEAR); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == PIPE_BUF); + ATF_REQUIRE(kev[0].udata == 0); + + /* + * Reading another byte triggers the EVFILT_WRITE again with a changed + * 'data' field. + */ + + ATF_REQUIRE(read(p[0], &c, 1) == 1); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]); + ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); + ATF_REQUIRE(kev[0].flags == EV_CLEAR); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == PIPE_BUF + 1); + ATF_REQUIRE(kev[0].udata == 0); + + /* + * Closing the read end should make a EV_EOF appear but leave the 'data' + * field unchanged. + */ + + ATF_REQUIRE(close(p[0]) == 0); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), NULL) == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]); + ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); + ATF_REQUIRE(kev[0].flags == (EV_CLEAR | EV_EOF)); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == PIPE_BUF + 1); + ATF_REQUIRE(kev[0].udata == 0); + + ATF_REQUIRE(close(kq) == 0); + ATF_REQUIRE(close(p[1]) == 0); +} + +ATF_TC_WITHOUT_HEAD(fifo_kqueue__connecting_reader); +ATF_TC_BODY(fifo_kqueue__connecting_reader, tc) +{ + int p[2] = { -1, -1 }; + + ATF_REQUIRE(mkfifo("testfifo", 0600) == 0); + + ATF_REQUIRE((p[0] = open("testfifo", + O_RDONLY | O_CLOEXEC | O_NONBLOCK)) >= 0); + ATF_REQUIRE((p[1] = open("testfifo", + O_WRONLY | O_CLOEXEC | O_NONBLOCK)) >= 0); + + int kq = kqueue(); + ATF_REQUIRE(kq >= 0); + + struct kevent kev[32]; + EV_SET(&kev[0], p[1], EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, 0); + EV_SET(&kev[1], p[1], EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, 0); + + ATF_REQUIRE(kevent(kq, kev, 2, NULL, 0, NULL) == 0); + + /* A new writer should immediately get a EVFILT_WRITE event. */ + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]); + ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 0); + + /* + * Filling the pipe, reading (PIPE_BUF + 1) bytes, then closing the + * read end leads to a EVFILT_WRITE with EV_EOF set. + */ + + char c = 0; + ssize_t r; + while ((r = write(p[1], &c, 1)) == 1) { + } + ATF_REQUIRE(r < 0); + ATF_REQUIRE(errno == EAGAIN || errno == EWOULDBLOCK); + + for (int i = 0; i < PIPE_BUF + 1; ++i) { + ATF_REQUIRE(read(p[0], &c, 1) == 1); + } + + ATF_REQUIRE(close(p[0]) == 0); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), NULL) == 1); + ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); + ATF_REQUIRE((kev[0].flags & EV_EOF) != 0); + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 0); + + /* Opening the reader again must trigger the EVFILT_WRITE. */ + + ATF_REQUIRE((p[0] = open("testfifo", + O_RDONLY | O_CLOEXEC | O_NONBLOCK)) >= 0); + + r = kevent(kq, NULL, 0, kev, nitems(kev), &(struct timespec) { 1, 0 }); + ATF_REQUIRE(r == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]); + ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); + ATF_REQUIRE(kev[0].flags == EV_CLEAR); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == PIPE_BUF + 1); + ATF_REQUIRE(kev[0].udata == 0); + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 0); + + ATF_REQUIRE(close(kq) == 0); + ATF_REQUIRE(close(p[0]) == 0); + ATF_REQUIRE(close(p[1]) == 0); +} + +ATF_TC_WITHOUT_HEAD(fifo_kqueue__reads); +ATF_TC_BODY(fifo_kqueue__reads, tc) +{ + int p[2] = { -1, -1 }; + + ATF_REQUIRE(mkfifo("testfifo", 0600) == 0); + + ATF_REQUIRE((p[0] = open("testfifo", + O_RDONLY | O_CLOEXEC | O_NONBLOCK)) >= 0); + ATF_REQUIRE((p[1] = open("testfifo", + O_WRONLY | O_CLOEXEC | O_NONBLOCK)) >= 0); + + /* Check that EVFILT_READ behaves sensibly on a FIFO reader. */ + + char c = 0; + ssize_t r; + while ((r = write(p[1], &c, 1)) == 1) { + } + ATF_REQUIRE(r < 0); + ATF_REQUIRE(errno == EAGAIN || errno == EWOULDBLOCK); + + for (int i = 0; i < PIPE_BUF + 1; ++i) { + ATF_REQUIRE(read(p[0], &c, 1) == 1); + } + + int kq = kqueue(); + ATF_REQUIRE(kq >= 0); + + struct kevent kev[32]; + EV_SET(&kev[0], p[0], EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, 0); + + ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, NULL) == 0); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[0]); + ATF_REQUIRE(kev[0].filter == EVFILT_READ); + ATF_REQUIRE(kev[0].flags == EV_CLEAR); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == 65023); + ATF_REQUIRE(kev[0].udata == 0); + + while ((r = read(p[0], &c, 1)) == 1) { + } + ATF_REQUIRE(r < 0); + ATF_REQUIRE(errno == EAGAIN || errno == EWOULDBLOCK); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 0); + + ATF_REQUIRE(close(kq) == 0); + ATF_REQUIRE(close(p[0]) == 0); + ATF_REQUIRE(close(p[1]) == 0); +} + +ATF_TC_WITHOUT_HEAD(fifo_kqueue__read_eof_wakeups); +ATF_TC_BODY(fifo_kqueue__read_eof_wakeups, tc) +{ + int p[2] = { -1, -1 }; + + ATF_REQUIRE(mkfifo("testfifo", 0600) == 0); + + ATF_REQUIRE((p[0] = open("testfifo", + O_RDONLY | O_CLOEXEC | O_NONBLOCK)) >= 0); + ATF_REQUIRE((p[1] = open("testfifo", + O_WRONLY | O_CLOEXEC | O_NONBLOCK)) >= 0); + + int kq = kqueue(); + ATF_REQUIRE(kq >= 0); + + struct kevent kev[32]; + + EV_SET(&kev[0], p[0], EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, 0); + ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, NULL) == 0); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 0); + + /* + * Closing the writer must trigger a EVFILT_READ edge with EV_EOF set. + */ + + ATF_REQUIRE(close(p[1]) == 0); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[0]); + ATF_REQUIRE(kev[0].filter == EVFILT_READ); + ATF_REQUIRE(kev[0].flags == (EV_EOF | EV_CLEAR)); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == 0); + ATF_REQUIRE(kev[0].udata == 0); + + /* + * Trying to read from a closed pipe should not trigger EVFILT_READ + * edges. + */ + + char c; + ATF_REQUIRE(read(p[0], &c, 1) == 0); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 0); + + ATF_REQUIRE(close(kq) == 0); + ATF_REQUIRE(close(p[0]) == 0); +} + +ATF_TC_WITHOUT_HEAD(fifo_kqueue__read_eof_state_when_reconnecting); +ATF_TC_BODY(fifo_kqueue__read_eof_state_when_reconnecting, tc) +{ + int p[2] = { -1, -1 }; + + ATF_REQUIRE(mkfifo("testfifo", 0600) == 0); + + ATF_REQUIRE((p[0] = open("testfifo", + O_RDONLY | O_CLOEXEC | O_NONBLOCK)) >= 0); + ATF_REQUIRE((p[1] = open("testfifo", + O_WRONLY | O_CLOEXEC | O_NONBLOCK)) >= 0); + + int kq = kqueue(); + ATF_REQUIRE(kq >= 0); + + struct kevent kev[32]; + + EV_SET(&kev[0], p[0], EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, 0); + ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, NULL) == 0); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 0); + + /* + * Closing the writer must trigger a EVFILT_READ edge with EV_EOF set. + */ + + ATF_REQUIRE(close(p[1]) == 0); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[0]); + ATF_REQUIRE(kev[0].filter == EVFILT_READ); + ATF_REQUIRE(kev[0].flags == (EV_EOF | EV_CLEAR)); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == 0); + ATF_REQUIRE(kev[0].udata == 0); + + /* A new reader shouldn't see the EOF flag. */ + + { + int new_reader; + ATF_REQUIRE((new_reader = open("testfifo", + O_RDONLY | O_CLOEXEC | O_NONBLOCK)) >= 0); + + int new_kq = kqueue(); + ATF_REQUIRE(new_kq >= 0); + + struct kevent new_kev[32]; + EV_SET(&new_kev[0], new_reader, EVFILT_READ, EV_ADD | EV_CLEAR, + 0, 0, 0); + ATF_REQUIRE(kevent(new_kq, new_kev, 1, NULL, 0, NULL) == 0); + + ATF_REQUIRE(kevent(new_kq, NULL, 0, new_kev, nitems(new_kev), + &(struct timespec) { 0, 0 }) == 0); + + ATF_REQUIRE(close(new_kq) == 0); + ATF_REQUIRE(close(new_reader) == 0); + } + + /* + * Simply reopening the writer does not trigger the EVFILT_READ again -- + * EV_EOF should be cleared, but there is no data yet so the filter + * does not trigger. + */ + + ATF_REQUIRE((p[1] = open("testfifo", + O_WRONLY | O_CLOEXEC | O_NONBLOCK)) >= 0); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 0); + + /* Writing a byte should trigger a EVFILT_READ. */ + + char c = 0; + ATF_REQUIRE(write(p[1], &c, 1) == 1); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[0]); + ATF_REQUIRE(kev[0].filter == EVFILT_READ); + ATF_REQUIRE(kev[0].flags == EV_CLEAR); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == 1); + ATF_REQUIRE(kev[0].udata == 0); + + ATF_REQUIRE(close(kq) == 0); + ATF_REQUIRE(close(p[0]) == 0); + ATF_REQUIRE(close(p[1]) == 0); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, fifo_kqueue__writes); + ATF_TP_ADD_TC(tp, fifo_kqueue__connecting_reader); + ATF_TP_ADD_TC(tp, fifo_kqueue__reads); + ATF_TP_ADD_TC(tp, fifo_kqueue__read_eof_wakeups); + ATF_TP_ADD_TC(tp, fifo_kqueue__read_eof_state_when_reconnecting); + + return atf_no_error(); +} Modified: head/tests/sys/kern/pipe/Makefile ============================================================================== --- head/tests/sys/kern/pipe/Makefile Mon Apr 27 15:59:19 2020 (r360380) +++ head/tests/sys/kern/pipe/Makefile Mon Apr 27 15:59:34 2020 (r360381) @@ -5,6 +5,7 @@ TESTSDIR= ${TESTSBASE}/sys/kern/pipe PLAIN_TESTS_C+= big_pipe_test PLAIN_TESTS_C+= pipe_fstat_bug_test PLAIN_TESTS_C+= pipe_ino_test +ATF_TESTS_C+= pipe_kqueue_test PLAIN_TESTS_C+= pipe_overcommit1_test PLAIN_TESTS_C+= pipe_overcommit2_test PLAIN_TESTS_C+= pipe_reverse2_test Added: head/tests/sys/kern/pipe/pipe_kqueue_test.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tests/sys/kern/pipe/pipe_kqueue_test.c Mon Apr 27 15:59:34 2020 (r360381) @@ -0,0 +1,366 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2020 Jan Kokemüller + * + * 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +ATF_TC_WITHOUT_HEAD(pipe_kqueue__write_end); +ATF_TC_BODY(pipe_kqueue__write_end, tc) +{ + int p[2] = { -1, -1 }; + + ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0); + ATF_REQUIRE(p[0] >= 0); + ATF_REQUIRE(p[1] >= 0); + + int kq = kqueue(); + ATF_REQUIRE(kq >= 0); + + struct kevent kev[32]; + EV_SET(&kev[0], p[1], EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, 0); + + ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, NULL) == 0); + + /* Test that EVFILT_WRITE behaves sensibly on the write end. */ + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]); + ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); + ATF_REQUIRE(kev[0].flags == EV_CLEAR); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == 16384); + ATF_REQUIRE(kev[0].udata == 0); + + /* Filling up the pipe should make the EVFILT_WRITE disappear. */ + + char c = 0; + ssize_t r; + while ((r = write(p[1], &c, 1)) == 1) { + } + ATF_REQUIRE(r < 0); + ATF_REQUIRE(errno == EAGAIN || errno == EWOULDBLOCK); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 0); + + /* Reading (PIPE_BUF - 1) bytes will not trigger a EVFILT_WRITE yet. */ + + for (int i = 0; i < PIPE_BUF - 1; ++i) { + ATF_REQUIRE(read(p[0], &c, 1) == 1); + } + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 0); + + /* Reading one additional byte triggers the EVFILT_WRITE. */ + + ATF_REQUIRE(read(p[0], &c, 1) == 1); + + r = kevent(kq, NULL, 0, kev, nitems(kev), &(struct timespec) { 0, 0 }); + ATF_REQUIRE(r == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]); + ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); + ATF_REQUIRE(kev[0].flags == EV_CLEAR); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == PIPE_BUF); + ATF_REQUIRE(kev[0].udata == 0); + + /* + * Reading another byte triggers the EVFILT_WRITE again with a changed + * 'data' field. + */ + + ATF_REQUIRE(read(p[0], &c, 1) == 1); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]); + ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); + ATF_REQUIRE(kev[0].flags == EV_CLEAR); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == PIPE_BUF + 1); + ATF_REQUIRE(kev[0].udata == 0); + + /* + * Closing the read end should make a EV_EOF appear but leave the 'data' + * field unchanged. + */ + + ATF_REQUIRE(close(p[0]) == 0); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]); + ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); + ATF_REQUIRE(kev[0].flags == (EV_CLEAR | EV_EOF | EV_ONESHOT)); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == PIPE_BUF + 1); + ATF_REQUIRE(kev[0].udata == 0); + + ATF_REQUIRE(close(kq) == 0); + ATF_REQUIRE(close(p[1]) == 0); +} + +ATF_TC_WITHOUT_HEAD(pipe_kqueue__closed_read_end); +ATF_TC_BODY(pipe_kqueue__closed_read_end, tc) +{ + int p[2] = { -1, -1 }; + + ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0); + ATF_REQUIRE(p[0] >= 0); + ATF_REQUIRE(p[1] >= 0); + + ATF_REQUIRE(close(p[0]) == 0); + + int kq = kqueue(); + ATF_REQUIRE(kq >= 0); + + struct kevent kev[32]; + EV_SET(&kev[0], p[1], EVFILT_READ, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/ + 0, 0, 0); + EV_SET(&kev[1], p[1], EVFILT_WRITE, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/ + 0, 0, 0); + + /* + * Trying to register EVFILT_WRITE when the pipe is closed leads to an + * EPIPE error. + */ + + ATF_REQUIRE(kevent(kq, kev, 2, kev, 2, NULL) == 2); + ATF_REQUIRE((kev[0].flags & EV_ERROR) != 0); + ATF_REQUIRE(kev[0].data == 0); + ATF_REQUIRE((kev[1].flags & EV_ERROR) != 0); + ATF_REQUIRE(kev[1].data == EPIPE); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]); + ATF_REQUIRE(kev[0].filter == EVFILT_READ); + ATF_REQUIRE(kev[0].flags == (EV_EOF | EV_CLEAR | EV_RECEIPT)); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == 0); + ATF_REQUIRE(kev[0].udata == 0); + + ATF_REQUIRE(close(kq) == 0); + ATF_REQUIRE(close(p[1]) == 0); +} + +ATF_TC_WITHOUT_HEAD(pipe_kqueue__closed_read_end_register_before_close); +ATF_TC_BODY(pipe_kqueue__closed_read_end_register_before_close, tc) +{ + int p[2] = { -1, -1 }; + + ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0); + ATF_REQUIRE(p[0] >= 0); + ATF_REQUIRE(p[1] >= 0); + + int kq = kqueue(); + ATF_REQUIRE(kq >= 0); + + struct kevent kev[32]; + EV_SET(&kev[0], p[1], EVFILT_READ, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/ + 0, 0, 0); + EV_SET(&kev[1], p[1], EVFILT_WRITE, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/ + 0, 0, 0); + + /* + * Registering EVFILT_WRITE before the pipe is closed leads to a + * EVFILT_WRITE event with EV_EOF set. + */ + + ATF_REQUIRE(kevent(kq, kev, 2, kev, 2, NULL) == 2); + ATF_REQUIRE((kev[0].flags & EV_ERROR) != 0); + ATF_REQUIRE(kev[0].data == 0); + ATF_REQUIRE((kev[1].flags & EV_ERROR) != 0); + ATF_REQUIRE(kev[1].data == 0); + + ATF_REQUIRE(close(p[0]) == 0); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 2); + { + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]); + ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); + ATF_REQUIRE(kev[0].flags == + (EV_EOF | EV_CLEAR | EV_ONESHOT | EV_RECEIPT)); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == 16384); + ATF_REQUIRE(kev[0].udata == 0); + } + { + ATF_REQUIRE(kev[1].ident == (uintptr_t)p[1]); + ATF_REQUIRE(kev[1].filter == EVFILT_READ); + ATF_REQUIRE(kev[1].flags == (EV_EOF | EV_CLEAR | EV_RECEIPT)); + ATF_REQUIRE(kev[1].fflags == 0); + ATF_REQUIRE(kev[1].data == 0); + ATF_REQUIRE(kev[1].udata == 0); + } + + ATF_REQUIRE(close(kq) == 0); + ATF_REQUIRE(close(p[1]) == 0); +} + +ATF_TC_WITHOUT_HEAD(pipe_kqueue__closed_write_end); +ATF_TC_BODY(pipe_kqueue__closed_write_end, tc) +{ + int p[2] = { -1, -1 }; + + ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0); + ATF_REQUIRE(p[0] >= 0); + ATF_REQUIRE(p[1] >= 0); + + char c = 0; + ssize_t r; + while ((r = write(p[1], &c, 1)) == 1) { + } + ATF_REQUIRE(r < 0); + ATF_REQUIRE(errno == EAGAIN || errno == EWOULDBLOCK); + + ATF_REQUIRE(close(p[1]) == 0); + + int kq = kqueue(); + ATF_REQUIRE(kq >= 0); + + struct kevent kev[32]; + EV_SET(&kev[0], p[0], EVFILT_READ, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/ + 0, 0, 0); + EV_SET(&kev[1], p[0], EVFILT_WRITE, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/ + 0, 0, 0); + + /* + * Trying to register EVFILT_WRITE when the pipe is closed leads to an + * EPIPE error. + */ + + ATF_REQUIRE(kevent(kq, kev, 2, kev, 2, NULL) == 2); + ATF_REQUIRE((kev[0].flags & EV_ERROR) != 0); + ATF_REQUIRE(kev[0].data == 0); + ATF_REQUIRE((kev[1].flags & EV_ERROR) != 0); + ATF_REQUIRE(kev[1].data == EPIPE); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 1); + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[0]); + ATF_REQUIRE(kev[0].filter == EVFILT_READ); + ATF_REQUIRE(kev[0].flags == (EV_EOF | EV_CLEAR | EV_RECEIPT)); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == 65536); + ATF_REQUIRE(kev[0].udata == 0); + + ATF_REQUIRE(close(kq) == 0); + ATF_REQUIRE(close(p[0]) == 0); +} + +ATF_TC_WITHOUT_HEAD(pipe_kqueue__closed_write_end_register_before_close); +ATF_TC_BODY(pipe_kqueue__closed_write_end_register_before_close, tc) +{ + int p[2] = { -1, -1 }; + + ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0); + ATF_REQUIRE(p[0] >= 0); + ATF_REQUIRE(p[1] >= 0); + + int kq = kqueue(); + ATF_REQUIRE(kq >= 0); + + struct kevent kev[32]; + EV_SET(&kev[0], p[0], EVFILT_READ, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/ + 0, 0, 0); + EV_SET(&kev[1], p[0], EVFILT_WRITE, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/ + 0, 0, 0); + + /* + * Registering EVFILT_WRITE before the pipe is closed leads to a + * EVFILT_WRITE event with EV_EOF set. + */ + + ATF_REQUIRE(kevent(kq, kev, 2, kev, 2, NULL) == 2); + ATF_REQUIRE((kev[0].flags & EV_ERROR) != 0); + ATF_REQUIRE(kev[0].data == 0); + ATF_REQUIRE((kev[1].flags & EV_ERROR) != 0); + ATF_REQUIRE(kev[1].data == 0); + + char c = 0; + ssize_t r; + while ((r = write(p[1], &c, 1)) == 1) { + } + ATF_REQUIRE(r < 0); + ATF_REQUIRE(errno == EAGAIN || errno == EWOULDBLOCK); + + ATF_REQUIRE(close(p[1]) == 0); + + ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev), + &(struct timespec) { 0, 0 }) == 2); + { + ATF_REQUIRE(kev[0].ident == (uintptr_t)p[0]); + ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); + ATF_REQUIRE(kev[0].flags == + (EV_EOF | EV_CLEAR | EV_ONESHOT | EV_RECEIPT)); + ATF_REQUIRE(kev[0].fflags == 0); + ATF_REQUIRE(kev[0].data == 4096 || + kev[0].data == 512 /* on FreeBSD 11.3 */); + ATF_REQUIRE(kev[0].udata == 0); + } + { + ATF_REQUIRE(kev[1].ident == (uintptr_t)p[0]); + ATF_REQUIRE(kev[1].filter == EVFILT_READ); + ATF_REQUIRE(kev[1].flags == (EV_EOF | EV_CLEAR | EV_RECEIPT)); + ATF_REQUIRE(kev[1].fflags == 0); + ATF_REQUIRE(kev[1].data == 65536); + ATF_REQUIRE(kev[1].udata == 0); + } + + ATF_REQUIRE(close(kq) == 0); + ATF_REQUIRE(close(p[0]) == 0); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, pipe_kqueue__write_end); + ATF_TP_ADD_TC(tp, pipe_kqueue__closed_read_end); + ATF_TP_ADD_TC(tp, pipe_kqueue__closed_read_end_register_before_close); + ATF_TP_ADD_TC(tp, pipe_kqueue__closed_write_end); + ATF_TP_ADD_TC(tp, pipe_kqueue__closed_write_end_register_before_close); + + return atf_no_error(); +}