1e007b89eSGleb Smirnoff /*-
2e007b89eSGleb Smirnoff * SPDX-License-Identifier: BSD-2-Clause
3e007b89eSGleb Smirnoff *
4e007b89eSGleb Smirnoff * Copyright (c) 2024 Gleb Smirnoff <glebius@FreeBSD.org>
5e007b89eSGleb Smirnoff *
6e007b89eSGleb Smirnoff * Redistribution and use in source and binary forms, with or without
7e007b89eSGleb Smirnoff * modification, are permitted provided that the following conditions
8e007b89eSGleb Smirnoff * are met:
9e007b89eSGleb Smirnoff * 1. Redistributions of source code must retain the above copyright
10e007b89eSGleb Smirnoff * notice, this list of conditions and the following disclaimer.
11e007b89eSGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright
12e007b89eSGleb Smirnoff * notice, this list of conditions and the following disclaimer in the
13e007b89eSGleb Smirnoff * documentation and/or other materials provided with the distribution.
14e007b89eSGleb Smirnoff *
15e007b89eSGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16e007b89eSGleb Smirnoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17e007b89eSGleb Smirnoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18e007b89eSGleb Smirnoff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19e007b89eSGleb Smirnoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20e007b89eSGleb Smirnoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21e007b89eSGleb Smirnoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22e007b89eSGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23e007b89eSGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24e007b89eSGleb Smirnoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25e007b89eSGleb Smirnoff * SUCH DAMAGE.
26e007b89eSGleb Smirnoff */
27e007b89eSGleb Smirnoff
28e007b89eSGleb Smirnoff #include <sys/socket.h>
29e007b89eSGleb Smirnoff #include <sys/un.h>
30e007b89eSGleb Smirnoff #include <assert.h>
31e007b89eSGleb Smirnoff #include <errno.h>
32e007b89eSGleb Smirnoff #include <pthread.h>
33e007b89eSGleb Smirnoff #include <unistd.h>
34e007b89eSGleb Smirnoff #include <netinet/in.h>
35e007b89eSGleb Smirnoff
36e007b89eSGleb Smirnoff #include <atf-c.h>
37e007b89eSGleb Smirnoff
38e007b89eSGleb Smirnoff /*
39e007b89eSGleb Smirnoff * shutdown(2) on SOCK_DGRAM shall return ENOTCONN per POSIX. However, there
40e007b89eSGleb Smirnoff * is historical behavior of the shutdown(2) also unblocking any ongoing
41e007b89eSGleb Smirnoff * recv(2) syscall on the socket. It is known that some programs rely on this
42*b32d49cfSGleb Smirnoff * behavior, but exact list of programs isn't known. Neither we know if the
43e007b89eSGleb Smirnoff * "feature" is required on PF_UNIX sockets or on PF_INET/INET6 sockets or
44e007b89eSGleb Smirnoff * on both kinds. Feel free to improve this comment if you know any details.
45e007b89eSGleb Smirnoff *
46e007b89eSGleb Smirnoff * List of relevant commits, bug reports and reviews:
47e007b89eSGleb Smirnoff * 63649db04205
48e007b89eSGleb Smirnoff * https://reviews.freebsd.org/D10351
49e007b89eSGleb Smirnoff * b114aa79596c (regresses)
50e007b89eSGleb Smirnoff * https://reviews.freebsd.org/D3039 (regresses)
51e007b89eSGleb Smirnoff * kern/84761 c5cff17017f9 aada5cccd878
52e007b89eSGleb Smirnoff */
53e007b89eSGleb Smirnoff
54e007b89eSGleb Smirnoff
55e007b89eSGleb Smirnoff static void *
blocking_thread(void * arg)56e007b89eSGleb Smirnoff blocking_thread(void *arg)
57e007b89eSGleb Smirnoff {
58e007b89eSGleb Smirnoff int *s = arg;
59e007b89eSGleb Smirnoff char buf[1];
60e007b89eSGleb Smirnoff int error, rv;
61e007b89eSGleb Smirnoff
62e007b89eSGleb Smirnoff rv = recv(*s, buf, sizeof(buf), 0);
63e007b89eSGleb Smirnoff error = (rv == -1) ? errno : 0;
64e007b89eSGleb Smirnoff
65e007b89eSGleb Smirnoff return ((void *)(uintptr_t)error);
66e007b89eSGleb Smirnoff }
67e007b89eSGleb Smirnoff
68e007b89eSGleb Smirnoff static void
shutdown_thread(int s)69e007b89eSGleb Smirnoff shutdown_thread(int s)
70e007b89eSGleb Smirnoff {
71e007b89eSGleb Smirnoff pthread_t t;
72e007b89eSGleb Smirnoff int rv;
73e007b89eSGleb Smirnoff
74e007b89eSGleb Smirnoff ATF_REQUIRE(pthread_create(&t, NULL, blocking_thread, &s) == 0);
75e007b89eSGleb Smirnoff usleep(1000);
76e007b89eSGleb Smirnoff ATF_REQUIRE(shutdown(s, SHUT_RD) == -1);
77e007b89eSGleb Smirnoff ATF_REQUIRE(errno == ENOTCONN);
78e007b89eSGleb Smirnoff ATF_REQUIRE(pthread_join(t, (void *)&rv) == 0);
79e007b89eSGleb Smirnoff ATF_REQUIRE(rv == 0);
80e007b89eSGleb Smirnoff close(s);
81e007b89eSGleb Smirnoff }
82e007b89eSGleb Smirnoff
83e007b89eSGleb Smirnoff ATF_TC_WITHOUT_HEAD(unblock);
ATF_TC_BODY(unblock,tc)84e007b89eSGleb Smirnoff ATF_TC_BODY(unblock, tc)
85e007b89eSGleb Smirnoff {
86e007b89eSGleb Smirnoff static const struct sockaddr_un sun = {
87e007b89eSGleb Smirnoff .sun_family = AF_LOCAL,
88e007b89eSGleb Smirnoff .sun_len = sizeof(sun),
89e007b89eSGleb Smirnoff .sun_path = "shutdown-dgram-test-sock",
90e007b89eSGleb Smirnoff };
91e007b89eSGleb Smirnoff int s;
92e007b89eSGleb Smirnoff
93e007b89eSGleb Smirnoff ATF_REQUIRE((s = socket(PF_UNIX, SOCK_DGRAM, 0)) >= 0);
94e007b89eSGleb Smirnoff ATF_REQUIRE(bind(s, (struct sockaddr *)&sun, sizeof(sun)) == 0);
95e007b89eSGleb Smirnoff shutdown_thread(s);
96e007b89eSGleb Smirnoff
97e007b89eSGleb Smirnoff static const struct sockaddr_in sin = {
98e007b89eSGleb Smirnoff .sin_family = AF_INET,
99e007b89eSGleb Smirnoff .sin_len = sizeof(sin),
100e007b89eSGleb Smirnoff };
101e007b89eSGleb Smirnoff ATF_REQUIRE((s = socket(PF_INET, SOCK_DGRAM, 0)) >= 0);
102e007b89eSGleb Smirnoff ATF_REQUIRE(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0);
103e007b89eSGleb Smirnoff shutdown_thread(s);
104e007b89eSGleb Smirnoff }
105e007b89eSGleb Smirnoff
ATF_TP_ADD_TCS(tp)106e007b89eSGleb Smirnoff ATF_TP_ADD_TCS(tp)
107e007b89eSGleb Smirnoff {
108e007b89eSGleb Smirnoff ATF_TP_ADD_TC(tp, unblock);
109e007b89eSGleb Smirnoff
110e007b89eSGleb Smirnoff return (atf_no_error());
111e007b89eSGleb Smirnoff }
112