From nobody Fri Jan 12 10:04:27 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 4TBHFS3YRjz56W4j; Fri, 12 Jan 2024 10:04:28 +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 4TBHFS18fsz4fcX; Fri, 12 Jan 2024 10:04:28 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705053868; 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=hzMfXXb+lLCwGq+5dQjzuvgjp8Uqd1EhlSpTCShc1vI=; b=I+I8iDUdLgShFQWpi2kZVZJaTUC+SVrfss+QeW9JIORdw09gkmdSzd/xyLx6U+drvO6QH/ 0UYsScZNNVmFYBDrgYMwCl0xI5pKI8Ysq0TzSAjnne9OQkPyQAeHuHmD9EzCFyVkQUTonl HMXRzlTl3gKlr68rSu8Gcg2X3rqRhrxFEesk1vhUNCqUtZ6r9rowrSaZT8T8ZV9QhCWdJp Pbfq6UTFRgQ0eGuPzvl0DV7IA55ppvGam2A3ez6s8Pbpen+VNvsyqpLc6ZP/WJS0KisR6o Hzp8FWKIFr6qokzorE7wngyZ7qMx7wuZ8RkKHk2JzafZ0FxWRS+HvN6rl1ILCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705053868; 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=hzMfXXb+lLCwGq+5dQjzuvgjp8Uqd1EhlSpTCShc1vI=; b=NlmFp7ga8UU5Qvy2uxJfAh6DGJNBR+8EBjGZyOVd0rBSHZQ2zknu4cSh6PL85v/k1GTy4L 2MBk0ZY9pVxZVgu35cPMQmnWcQyrqMO8NKlGA1eud4Q0bq7LAsPgPGV9GUbRO8GzMdWwUC 3X0ZUB6i2mXfar4974KnyRVpq7pnZFKf9ta1a4bjom4MdgWLe1DK3Wq/MKmGR9rQDYXlVg oihuC7h60orHtZoUwn7pzbdTofjp82SKRdokbPPWDMMxw26O9P5N5IdAx8Wv0l/XsgRlrW bw7F5jlR6UbfTm2SD4Fj46SjnPlXn6+01Tq3hk484i5iggUMX4SU20d4NfXRjQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1705053868; a=rsa-sha256; cv=none; b=dV2u7QSj0aRW/EFHQc/KbLqTU+TlRsF9v86//ek3Gh/diJUc6/TGwmBbRaUdvK/Gdf/7pT hKfXGo58yh6uIJg6Xl90B3kOtYtWNy6Ram8A1DwoZ/1MQOx5hsJZwZ/4380SSR1XfI3hB+ w09XKKDyirFpecc6Aglh6cST4wXcGExk4GZgq2ee4Zt5FN2n+PlfNqLauO8p0xRSr0xm2o TMeX4CjPQYfe84An8pEDuGJwuAJA4Ji4J9X5kdhVUWJYj85UQPqg8vrWiQRPUWCumN7xU0 ZCg2ZE5e5pC7FjVL4uy3hjTESRzEV9sRNSGfa3kVcrNFveXVUDvLjmJL3l0ZoA== 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 4TBHFS0GHlzxk7; Fri, 12 Jan 2024 10:04:28 +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 40CA4RaW003627; Fri, 12 Jan 2024 10:04:27 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 40CA4Rw0003624; Fri, 12 Jan 2024 10:04:27 GMT (envelope-from git) Date: Fri, 12 Jan 2024 10:04:27 GMT Message-Id: <202401121004.40CA4Rw0003624@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: e007b89e9d31 - main - tests: add a test for shutdown(2) on a datagram socket 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: e007b89e9d311daec88a3c7ae38646bcd015c396 Auto-Submitted: auto-generated The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=e007b89e9d311daec88a3c7ae38646bcd015c396 commit e007b89e9d311daec88a3c7ae38646bcd015c396 Author: Gleb Smirnoff AuthorDate: 2024-01-12 10:04:04 +0000 Commit: Gleb Smirnoff CommitDate: 2024-01-12 10:04:04 +0000 tests: add a test for shutdown(2) on a datagram socket --- tests/sys/kern/Makefile | 2 + tests/sys/kern/shutdown_dgram.c | 111 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/tests/sys/kern/Makefile b/tests/sys/kern/Makefile index 308cfd22a0ec..143c13297585 100644 --- a/tests/sys/kern/Makefile +++ b/tests/sys/kern/Makefile @@ -26,6 +26,7 @@ ATF_TESTS_C+= ptrace_test TEST_METADATA.ptrace_test+= timeout="15" ATF_TESTS_C+= reaper ATF_TESTS_C+= sched_affinity +ATF_TESTS_C+= shutdown_dgram ATF_TESTS_C+= sigaltstack ATF_TESTS_C+= sigwait ATF_TESTS_C+= socket_accept @@ -75,6 +76,7 @@ LIBADD.unix_seqpacket_test+= pthread LIBADD.kcov+= pthread CFLAGS.ktls_test+= -DOPENSSL_API_COMPAT=0x10100000L LIBADD.ktls_test+= crypto util +LIBADD.shutdown_dgram+= pthread LIBADD.socket_msg_waitall+= pthread LIBADD.sendfile_helper+= pthread LIBADD.fdgrowtable_test+= util pthread kvm procstat diff --git a/tests/sys/kern/shutdown_dgram.c b/tests/sys/kern/shutdown_dgram.c new file mode 100644 index 000000000000..04382d14f48f --- /dev/null +++ b/tests/sys/kern/shutdown_dgram.c @@ -0,0 +1,111 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Gleb Smirnoff + * + * 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 + +/* + * shutdown(2) on SOCK_DGRAM shall return ENOTCONN per POSIX. However, there + * is historical behavior of the shutdown(2) also unblocking any ongoing + * recv(2) syscall on the socket. It is known that some programs rely on this + * behavior, but exact list of problems isn't known. Neither we know if the + * "feature" is required on PF_UNIX sockets or on PF_INET/INET6 sockets or + * on both kinds. Feel free to improve this comment if you know any details. + * + * List of relevant commits, bug reports and reviews: + * 63649db04205 + * https://reviews.freebsd.org/D10351 + * b114aa79596c (regresses) + * https://reviews.freebsd.org/D3039 (regresses) + * kern/84761 c5cff17017f9 aada5cccd878 + */ + + +static void * +blocking_thread(void *arg) +{ + int *s = arg; + char buf[1]; + int error, rv; + + rv = recv(*s, buf, sizeof(buf), 0); + error = (rv == -1) ? errno : 0; + + return ((void *)(uintptr_t)error); +} + +static void +shutdown_thread(int s) +{ + pthread_t t; + int rv; + + ATF_REQUIRE(pthread_create(&t, NULL, blocking_thread, &s) == 0); + usleep(1000); + ATF_REQUIRE(shutdown(s, SHUT_RD) == -1); + ATF_REQUIRE(errno == ENOTCONN); + ATF_REQUIRE(pthread_join(t, (void *)&rv) == 0); + ATF_REQUIRE(rv == 0); + close(s); +} + +ATF_TC_WITHOUT_HEAD(unblock); +ATF_TC_BODY(unblock, tc) +{ + static const struct sockaddr_un sun = { + .sun_family = AF_LOCAL, + .sun_len = sizeof(sun), + .sun_path = "shutdown-dgram-test-sock", + }; + int s; + + ATF_REQUIRE((s = socket(PF_UNIX, SOCK_DGRAM, 0)) >= 0); + ATF_REQUIRE(bind(s, (struct sockaddr *)&sun, sizeof(sun)) == 0); + shutdown_thread(s); + + static const struct sockaddr_in sin = { + .sin_family = AF_INET, + .sin_len = sizeof(sin), + }; + ATF_REQUIRE((s = socket(PF_INET, SOCK_DGRAM, 0)) >= 0); + ATF_REQUIRE(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0); + shutdown_thread(s); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, unblock); + + return (atf_no_error()); +}