From nobody Thu Feb 15 18:49:05 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4TbPH52ZG0z59tCj; Thu, 15 Feb 2024 18:49:05 +0000 (UTC) (envelope-from git@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) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TbPH51wBHz4tPH; Thu, 15 Feb 2024 18:49:05 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1708022945; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=UvZHnjjT7IWwHB+s1O1C70AsJpQRqGfYDdxgUxSEg9k=; b=I6YLwxnTDhq+HaQxsU8NOTtN5vB+YX4aih/2+lh3nwRpJT6rNaEoPdyvqSsaD67lUvSUi8 o78TSPBsRL05/Km3jdCR2T/mUszX5V6hcOSpprsAZl+k8F1pfBp8nTrCEhm+i5crteRj37 PFihS12CAr23LRFwN0Beok3y+rvZAzjJmY/Pcrhcbp6OXHchRRh11QZMB2C1qTVD0ojqYK zHPBwv7vIacMRKUIda970ZtaUFItHYk3viG/8R1WZJgNvA9h8R6XpM0DSuYxMvrRdQyWC+ vOcPBeaN6GIbGt3do337fTGQY1zBSkfcMR2MnZjeyInhYqMAKYTiZ2xw5hnHKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1708022945; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=UvZHnjjT7IWwHB+s1O1C70AsJpQRqGfYDdxgUxSEg9k=; b=W2UbPnlkxZUWzwm2loHJENyiwWoMHNdOX/CQFGkfk2tSCuDarB1t2vifWINI+VCCpvbgjf fQHc6uqSfur4w85VPp57XRUroUjBijK5C1ZKdfbSol3RnnFkUNkFSTMm9nOL7+/kloq/WU H5fScxbXnq1R3GzMZVqAUMai0NzbxXvJKdYPIxqkI7OLd/D5FJhq5WN3e/9YCHmGcEleVp 982e78zgMXKNAwo5jYXVD3HvpgitoLRvBta8rEMxzK5e5ij4YUpkCXfEmNRvaMUIyTPCsc fqOU4Fx0MyUEpjFnO8OAaE5kOzK+lap09QAWNsrIW8+T5jz7FIpL1ok6PJnfJA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1708022945; a=rsa-sha256; cv=none; b=KMCJPVpJ4HY56/KcducjThKeOdeReTUwz5biPKblhqR/ZunlOiSEll3USsCvzGUQdwDznS ZVr4S7zRaEXP/IPpP8xVvshq5C+og3B0aaXrZSfDk12kHAUbcWRTnWC75/p8xX0jxAmkEy pkXRtMp4Y6GdGZHMn3x1wUYsRcalhmo8WSgSVxX2bSZBHoNjpcI5nLCAah2lkq8vgDqicJ z/YG/8ESmeMB2Jf2O0A4c8ICU845sbYaegZkdLuwYCiWyrSN5qsIvtDnn98ap4yZUUeAVN 2FAuFUMn1eNgqGNU7O3gT+Xv0kUj5lUXSSWF5iXSXgFYllI9F1/+K8kH5mYaSg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4TbPH50yYGztBl; Thu, 15 Feb 2024 18:49:05 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 41FIn5qV085047; Thu, 15 Feb 2024 18:49:05 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 41FIn5q6085044; Thu, 15 Feb 2024 18:49:05 GMT (envelope-from git) Date: Thu, 15 Feb 2024 18:49:05 GMT Message-Id: <202402151849.41FIn5q6085044@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Gleb Smirnoff Subject: git: bc9107f8c405 - main - socket tests: add listener_wakeup List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: glebius X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: bc9107f8c405ce83f47abc915ae95baadb770d6d Auto-Submitted: auto-generated The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=bc9107f8c405ce83f47abc915ae95baadb770d6d commit bc9107f8c405ce83f47abc915ae95baadb770d6d Author: Gleb Smirnoff AuthorDate: 2024-02-15 18:48:44 +0000 Commit: Gleb Smirnoff CommitDate: 2024-02-15 18:48:44 +0000 socket tests: add listener_wakeup This test runs several scenarios when sleep(9) on a listen(2)ing socket is interrupted by shutdown(2) or by close(2). What should happen in that case is not specified, neither is documented. However, there is certain behavior that we have and this test makes sure it is preserved. There is software that relies on it, see bug 227259. This test is based on submission with this bug, bugzilla attachment 192260. The test checks TCP and unix(4) stream socket behavior and SCTP can be added easily if needed. The test passes on FreeBSD 11 to 15. It won't pass on FreeBSD 10, although the wakeup behavior of shutdown(2) is the same, but it doesn't return error. PR: 227259 --- tests/sys/kern/Makefile | 2 + tests/sys/kern/listener_wakeup.c | 293 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 295 insertions(+) diff --git a/tests/sys/kern/Makefile b/tests/sys/kern/Makefile index 5a7e27319ddb..e54a43b5fd83 100644 --- a/tests/sys/kern/Makefile +++ b/tests/sys/kern/Makefile @@ -21,6 +21,7 @@ ATF_TESTS_C+= kill_zombie .if ${MK_OPENSSL} != "no" ATF_TESTS_C+= ktls_test .endif +ATF_TESTS_C+= listener_wakeup ATF_TESTS_C+= module_test ATF_TESTS_C+= ptrace_test TEST_METADATA.ptrace_test+= timeout="15" @@ -79,6 +80,7 @@ LIBADD.unix_seqpacket_test+= pthread LIBADD.kcov+= pthread CFLAGS.ktls_test+= -DOPENSSL_API_COMPAT=0x10100000L LIBADD.ktls_test+= crypto util +LIBADD.listener_wakeup+= pthread LIBADD.shutdown_dgram+= pthread LIBADD.socket_msg_waitall+= pthread LIBADD.sendfile_helper+= pthread diff --git a/tests/sys/kern/listener_wakeup.c b/tests/sys/kern/listener_wakeup.c new file mode 100644 index 000000000000..39e8596c335e --- /dev/null +++ b/tests/sys/kern/listener_wakeup.c @@ -0,0 +1,293 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Gleb Smirnoff + * Copyright (c) 2018 Rozhuk Ivan + * + * 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 +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +/* + * This test runs several scenarios when sleep(9) on a listen(2)ing socket is + * interrupted by shutdown(2) or by close(2). What should happen in that case + * is not specified, neither is documented. However, there is certain behavior + * that we have and this test makes sure it is preserved. The known software + * to rely on the behavior is FreeSWITCH telephony software (see bug 227259). + * There might be more. This test is based on submission with the bug, bugzilla + * attachment 192260. + */ + +static const struct test { + enum { + SLEEP_ACCEPT = 0, + SLEEP_SELECT, + SLEEP_POLL, + SLEEP_KQUEUE, + NSLEEP + } sleep; + enum { + WAKEUP_SHUTDOWN, + WAKEUP_CLOSE, + } wakeup; + enum { + AFTER, + BEFORE, + } when; + bool nonblock; + int result; +} tests[] = { + { SLEEP_ACCEPT, WAKEUP_SHUTDOWN, AFTER, false, ECONNABORTED }, + { SLEEP_SELECT, WAKEUP_SHUTDOWN, AFTER, false, 0 }, + { SLEEP_POLL, WAKEUP_SHUTDOWN, AFTER, false, 0 }, + { SLEEP_KQUEUE, WAKEUP_SHUTDOWN, AFTER, false, 0 }, + { SLEEP_ACCEPT, WAKEUP_CLOSE, AFTER, false, ETIMEDOUT }, + { SLEEP_SELECT, WAKEUP_CLOSE, AFTER, false, EBADF }, + { SLEEP_POLL, WAKEUP_CLOSE, AFTER, false, 0 }, + { SLEEP_KQUEUE, WAKEUP_CLOSE, AFTER, false, 0 }, + { SLEEP_ACCEPT, WAKEUP_SHUTDOWN, BEFORE, false, ECONNABORTED }, + { SLEEP_SELECT, WAKEUP_SHUTDOWN, BEFORE, false, 0 }, + { SLEEP_POLL, WAKEUP_SHUTDOWN, BEFORE, false, 0 }, + { SLEEP_KQUEUE, WAKEUP_SHUTDOWN, BEFORE, false, 0 }, + { SLEEP_SELECT, WAKEUP_SHUTDOWN, AFTER, true, 0 }, + { SLEEP_POLL, WAKEUP_SHUTDOWN, AFTER, true, 0 }, + { SLEEP_KQUEUE, WAKEUP_SHUTDOWN, AFTER, true, 0 }, + { SLEEP_SELECT, WAKEUP_SHUTDOWN, BEFORE, true, 0 }, + { SLEEP_POLL, WAKEUP_SHUTDOWN, BEFORE, true, 0 }, + { SLEEP_KQUEUE, WAKEUP_SHUTDOWN, BEFORE, true, 0 }, +}; + +static int +tcp_listen(void) +{ + struct sockaddr_in sin = { + .sin_family = PF_INET, + .sin_len = sizeof(sin), + }; + int s; + + ATF_REQUIRE((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1); + ATF_REQUIRE(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0); + ATF_REQUIRE(listen(s, -1) == 0); + + return (s); +} + +static int +unix_listen(void) +{ + struct sockaddr_un sun = { + .sun_family = AF_UNIX, + .sun_len = sizeof(sun), + .sun_path = "listen-shutdown-test.sock", + }; + int s; + + ATF_REQUIRE((s = socket(PF_UNIX, SOCK_STREAM, 0)) != -1); + (void)unlink(sun.sun_path); + ATF_REQUIRE(bind(s, (struct sockaddr *)&sun, sizeof(sun)) == 0); + ATF_REQUIRE(listen(s, -1) == 0); + + return (s); +} + +static const struct proto { + const char *name; + int (*listen)(void); +} protos[] = { + { "PF_INET", tcp_listen }, + { "PF_UNIX", unix_listen }, +}; + +static int +sleep_accept(int s) +{ + int rv; + + rv = accept(s, NULL, NULL); + + return (rv == -1 ? errno : 0); +} + +static int +sleep_select(int s) +{ + fd_set fds; + int rv; + + FD_ZERO(&fds); + FD_SET(s, &fds); + rv = select(s + 1, &fds, &fds, &fds, NULL); + + return (rv == -1 ? errno : 0); +} + +static int +sleep_poll(int s) +{ + struct pollfd fds = { + .fd = s, + .events = (POLLIN | POLLPRI | POLLRDNORM | POLLWRNORM | + POLLRDBAND | POLLWRBAND), + .revents = 0, + }; + int rv; + + rv = poll(&fds, 1, INFTIM); + + return (rv == -1 ? errno : 0); +} + +static int +sleep_kqueue(int s) +{ + struct kevent kev; + int kq, error; + + ATF_REQUIRE((kq = kqueue()) != -1); + EV_SET(&kev, s, EVFILT_READ, EV_ADD, 0, 0, NULL); + if (kevent(kq, &kev, 1, NULL, 0, NULL) == -1) { + error = errno; + } else { + if (kev.flags & EV_ERROR) + error = (int)kev.data; + else + error = 0; + } + ATF_REQUIRE(close(kq) == 0); + + return (error); +} + +typedef int sleep_syscall_t(int); +static sleep_syscall_t *sleep_syscalls[NSLEEP] = { + [SLEEP_ACCEPT] = sleep_accept, + [SLEEP_SELECT] = sleep_select, + [SLEEP_POLL] = sleep_poll, + [SLEEP_KQUEUE] = sleep_kqueue, +}; + +struct test_ctx { + struct test const *test; + int s; + int result; +}; + +static void * +sleep_syscall_thread(void *data) { + struct test_ctx *ctx = data; + + ctx->result = sleep_syscalls[ctx->test->sleep](ctx->s); + + return (NULL); +} + +static void +run_tests(const struct proto *pr) +{ + pthread_t tid; + struct timespec ts; + int error; + + for (u_int i = 0; i < nitems(tests); i ++) { + struct test const *t = &tests[i]; + struct test_ctx ctx = { + .test = t, + /* Note: tested syscalls don't return this. */ + .result = ETIMEDOUT, + }; + + ctx.s = pr->listen(); + if (t->nonblock) + ATF_REQUIRE(fcntl(ctx.s, F_SETFL, O_NONBLOCK) != -1); + + if (t->when == AFTER) { + ATF_REQUIRE(pthread_create(&tid, NULL, + sleep_syscall_thread, &ctx) == 0); + usleep(100000); + } + + switch (t->wakeup) { + case WAKEUP_SHUTDOWN: + ATF_REQUIRE(shutdown(ctx.s, SHUT_RDWR) == -1); + ATF_REQUIRE(errno == ENOTCONN); + break; + case WAKEUP_CLOSE: + ATF_REQUIRE(close(ctx.s) == 0); + break; + } + + if (t->when == BEFORE) { + ATF_REQUIRE(pthread_create(&tid, NULL, + sleep_syscall_thread, &ctx) == 0); + usleep(100000); + } + + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec++; + if ((error = pthread_timedjoin_np(tid, NULL, &ts)) != 0) { + ATF_REQUIRE(pthread_cancel(tid) == 0); + ATF_REQUIRE(error == ETIMEDOUT); + ATF_REQUIRE(ctx.result == ETIMEDOUT); + } + + ATF_REQUIRE_MSG(ctx.result == t->result, + "proto %s sleeping syscall #%d wakeup #%d nb %d, " + "expected %d, got %d", pr->name, t->sleep, t->wakeup, + t->nonblock, t->result, ctx.result); + + if (t->wakeup == WAKEUP_SHUTDOWN) + ATF_REQUIRE(close(ctx.s) == 0); + } +} + +ATF_TC_WITHOUT_HEAD(all); +ATF_TC_BODY(all, tc) +{ + for (u_int f = 0; f < nitems(protos); f++) + run_tests(&protos[f]); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, all); + + return (atf_no_error()); +}