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