xref: /freebsd/tests/sys/kern/socket_msg_waitall.c (revision c8e7f78a3d28ff6e6223ed136ada8e1e2f34965e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2023 The FreeBSD Foundation
5  *
6  * This software was developed by Mark Johnston under sponsorship from
7  * the FreeBSD Foundation.
8  */
9 
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <sys/un.h>
13 
14 #include <netinet/in.h>
15 
16 #include <errno.h>
17 #include <pthread.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 
21 #include <atf-c.h>
22 
23 struct close_test_params {
24 	struct sockaddr_storage sa;
25 	size_t msglen;
26 	int count;
27 	int af, type, proto;
28 };
29 
30 static void *
31 close_test_client(void *arg)
32 {
33 	struct close_test_params *p = arg;
34 	char *buf;
35 	size_t buflen;
36 
37 	buflen = p->msglen + 1;
38 	buf = malloc(buflen);
39 	ATF_REQUIRE(buf != NULL);
40 
41 	while (p->count-- > 0) {
42 		ssize_t n;
43 		int error, s;
44 
45 		s = socket(p->af, p->type, p->proto);
46 		ATF_REQUIRE_MSG(s >= 0, "socket: %s", strerror(errno));
47 
48 		error = connect(s, (struct sockaddr *)&p->sa, p->sa.ss_len);
49 		ATF_REQUIRE_MSG(error == 0, "connect: %s", strerror(errno));
50 
51 		n = recv(s, buf, buflen, MSG_WAITALL);
52 		ATF_REQUIRE_MSG(n == (ssize_t)p->msglen,
53 		    "recv: %s", strerror(errno));
54 
55 		ATF_REQUIRE(close(s) == 0);
56 	}
57 	free(buf);
58 
59 	return (NULL);
60 }
61 
62 static void
63 close_test(struct sockaddr *sa, unsigned int count, int af, int type, int proto)
64 {
65 	struct close_test_params p;
66 	const char *msg;
67 	pthread_t t;
68 	size_t msglen;
69 	int error, s;
70 
71 	s = socket(af, type, proto);
72 	ATF_REQUIRE_MSG(s >= 0, "socket %s", strerror(errno));
73 
74 	ATF_REQUIRE_MSG(bind(s, sa, sa->sa_len) == 0,
75 	    "bind: %s", strerror(errno));
76 	ATF_REQUIRE_MSG(listen(s, 1) == 0,
77 	    "listen: %s", strerror(errno));
78 	ATF_REQUIRE_MSG(getsockname(s, sa, &(socklen_t){ sa->sa_len }) == 0,
79 	    "getsockname: %s", strerror(errno));
80 
81 	msg = "hello bonjour";
82 	msglen = strlen(msg) + 1;
83 	p = (struct close_test_params){
84 		.count = count,
85 		.msglen = msglen,
86 		.af = af,
87 		.type = type,
88 		.proto = proto,
89 	};
90 	memcpy(&p.sa, sa, sa->sa_len);
91 	error = pthread_create(&t, NULL, close_test_client, &p);
92 	ATF_REQUIRE_MSG(error == 0, "pthread_create: %s", strerror(error));
93 
94 	while (count-- > 0) {
95 		ssize_t n;
96 		int cs;
97 
98 		cs = accept(s, NULL, NULL);
99 		ATF_REQUIRE_MSG(cs >= 0, "accept: %s", strerror(errno));
100 
101 		n = send(cs, msg, msglen, 0);
102 		ATF_REQUIRE_MSG(n == (ssize_t)msglen,
103 		    "send: %s", strerror(errno));
104 
105 		ATF_REQUIRE(close(cs) == 0);
106 	}
107 
108 	ATF_REQUIRE(close(s) == 0);
109 	ATF_REQUIRE(pthread_join(t, NULL) == 0);
110 }
111 
112 /*
113  * Make sure that closing a connection kicks a MSG_WAITALL recv() out of the
114  * syscall.  See bugzilla PR 212716.
115  */
116 ATF_TC(close_tcp);
117 ATF_TC_HEAD(close_tcp, tc)
118 {
119 	atf_tc_set_md_var(tc, "timeout", "10");
120 }
121 ATF_TC_BODY(close_tcp, tc)
122 {
123 	struct sockaddr_in sin;
124 
125 	sin = (struct sockaddr_in){
126 		.sin_len = sizeof(sin),
127 		.sin_family = AF_INET,
128 		.sin_addr = { htonl(INADDR_LOOPBACK) },
129 		.sin_port = htons(0),
130 	};
131 	close_test((struct sockaddr *)&sin, 1000, AF_INET, SOCK_STREAM,
132 	    IPPROTO_TCP);
133 }
134 
135 /* A variant of the above test for UNIX domain stream sockets. */
136 ATF_TC(close_unix_stream);
137 ATF_TC_HEAD(close_unix_stream, tc)
138 {
139 	atf_tc_set_md_var(tc, "timeout", "10");
140 }
141 ATF_TC_BODY(close_unix_stream, tc)
142 {
143 	struct sockaddr_un sun;
144 
145 	sun = (struct sockaddr_un){
146 		.sun_len = sizeof(sun),
147 		.sun_family = AF_UNIX,
148 		.sun_path = "socket_msg_waitall_unix",
149 	};
150 	close_test((struct sockaddr *)&sun, 1000, AF_UNIX, SOCK_STREAM, 0);
151 	ATF_REQUIRE_MSG(unlink(sun.sun_path) == 0,
152 	    "unlink: %s", strerror(errno));
153 }
154 
155 /* A variant of the above test for UNIX domain seqpacket sockets. */
156 ATF_TC(close_unix_seqpacket);
157 ATF_TC_HEAD(close_unix_seqpacket, tc)
158 {
159 	atf_tc_set_md_var(tc, "timeout", "10");
160 }
161 ATF_TC_BODY(close_unix_seqpacket, tc)
162 {
163 	struct sockaddr_un sun;
164 
165 	sun = (struct sockaddr_un){
166 		.sun_len = sizeof(sun),
167 		.sun_family = AF_UNIX,
168 		.sun_path = "socket_msg_waitall_unix",
169 	};
170 	close_test((struct sockaddr *)&sun, 1000, AF_UNIX, SOCK_SEQPACKET, 0);
171 	ATF_REQUIRE_MSG(unlink(sun.sun_path) == 0,
172 	    "unlink: %s", strerror(errno));
173 }
174 
175 ATF_TP_ADD_TCS(tp)
176 {
177 	ATF_TP_ADD_TC(tp, close_tcp);
178 	ATF_TP_ADD_TC(tp, close_unix_stream);
179 	ATF_TP_ADD_TC(tp, close_unix_seqpacket);
180 
181 	return (atf_no_error());
182 }
183