aboutsummaryrefslogtreecommitdiff
path: root/tests/sys/kern/socket_msg_waitall.c
blob: e9018b47eec133d8bae108a880e3efb074f8e39a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2023 The FreeBSD Foundation
 *
 * This software was developed by Mark Johnston under sponsorship from
 * the FreeBSD Foundation.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <netinet/in.h>

#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

#include <atf-c.h>

struct close_test_params {
	struct sockaddr_storage sa;
	size_t msglen;
	int count;
	int af, type, proto;
};

static void *
close_test_client(void *arg)
{
	struct close_test_params *p = arg;
	char *buf;
	size_t buflen;

	buflen = p->msglen + 1;
	buf = malloc(buflen);
	ATF_REQUIRE(buf != NULL);

	while (p->count-- > 0) {
		ssize_t n;
		int error, s;

		s = socket(p->af, p->type, p->proto);
		ATF_REQUIRE_MSG(s >= 0, "socket: %s", strerror(errno));

		error = connect(s, (struct sockaddr *)&p->sa, p->sa.ss_len);
		ATF_REQUIRE_MSG(error == 0, "connect: %s", strerror(errno));

		n = recv(s, buf, buflen, MSG_WAITALL);
		ATF_REQUIRE_MSG(n == (ssize_t)p->msglen,
		    "recv: %s", strerror(errno));

		ATF_REQUIRE(close(s) == 0);
	}

	return (NULL);
}

static void
close_test(struct sockaddr *sa, unsigned int count, int af, int type, int proto)
{
	struct close_test_params p;
	const char *msg;
	pthread_t t;
	size_t msglen;
	int error, s;

	s = socket(af, type, proto);
	ATF_REQUIRE_MSG(s >= 0, "socket %s", strerror(errno));

	ATF_REQUIRE_MSG(bind(s, sa, sa->sa_len) == 0,
	    "bind: %s", strerror(errno));
	ATF_REQUIRE_MSG(listen(s, 1) == 0,
	    "listen: %s", strerror(errno));
	ATF_REQUIRE_MSG(getsockname(s, sa, &(socklen_t){ sa->sa_len }) == 0,
	    "getsockname: %s", strerror(errno));

	msg = "hello bonjour";
	msglen = strlen(msg) + 1;
	p = (struct close_test_params){
		.count = count,
		.msglen = msglen,
		.af = af,
		.type = type,
		.proto = proto,
	};
	memcpy(&p.sa, sa, sa->sa_len);
	error = pthread_create(&t, NULL, close_test_client, &p);
	ATF_REQUIRE_MSG(error == 0, "pthread_create: %s", strerror(error));

	while (count-- > 0) {
		ssize_t n;
		int cs;

		cs = accept(s, NULL, NULL);
		ATF_REQUIRE_MSG(cs >= 0, "accept: %s", strerror(errno));

		n = send(cs, msg, msglen, 0);
		ATF_REQUIRE_MSG(n == (ssize_t)msglen,
		    "send: %s", strerror(errno));

		ATF_REQUIRE(close(cs) == 0);
	}

	ATF_REQUIRE(close(s) == 0);
	ATF_REQUIRE(pthread_join(t, NULL) == 0);
}

/*
 * Make sure that closing a connection kicks a MSG_WAITALL recv() out of the
 * syscall.  See bugzilla PR 212716.
 */
ATF_TC(close_tcp);
ATF_TC_HEAD(close_tcp, tc)
{
	atf_tc_set_md_var(tc, "timeout", "10");
}
ATF_TC_BODY(close_tcp, tc)
{
	struct sockaddr_in sin;

	sin = (struct sockaddr_in){
		.sin_len = sizeof(sin),
		.sin_family = AF_INET,
		.sin_addr = { htonl(INADDR_LOOPBACK) },
		.sin_port = htons(0),
	};
	close_test((struct sockaddr *)&sin, 1000, AF_INET, SOCK_STREAM,
	    IPPROTO_TCP);
}

/* A variant of the above test for UNIX domain stream sockets. */
ATF_TC(close_unix_stream);
ATF_TC_HEAD(close_unix_stream, tc)
{
	atf_tc_set_md_var(tc, "timeout", "10");
}
ATF_TC_BODY(close_unix_stream, tc)
{
	struct sockaddr_un sun;

	sun = (struct sockaddr_un){
		.sun_len = sizeof(sun),
		.sun_family = AF_UNIX,
		.sun_path = "socket_msg_waitall_unix",
	};
	close_test((struct sockaddr *)&sun, 1000, AF_UNIX, SOCK_STREAM, 0);
	ATF_REQUIRE_MSG(unlink(sun.sun_path) == 0,
	    "unlink: %s", strerror(errno));
}

/* A variant of the above test for UNIX domain seqpacket sockets. */
ATF_TC(close_unix_seqpacket);
ATF_TC_HEAD(close_unix_seqpacket, tc)
{
	atf_tc_set_md_var(tc, "timeout", "10");
}
ATF_TC_BODY(close_unix_seqpacket, tc)
{
	struct sockaddr_un sun;

	sun = (struct sockaddr_un){
		.sun_len = sizeof(sun),
		.sun_family = AF_UNIX,
		.sun_path = "socket_msg_waitall_unix",
	};
	close_test((struct sockaddr *)&sun, 1000, AF_UNIX, SOCK_SEQPACKET, 0);
	ATF_REQUIRE_MSG(unlink(sun.sun_path) == 0,
	    "unlink: %s", strerror(errno));
}

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, close_tcp);
	ATF_TP_ADD_TC(tp, close_unix_stream);
	ATF_TP_ADD_TC(tp, close_unix_seqpacket);

	return (atf_no_error());
}