aboutsummaryrefslogtreecommitdiff
path: root/tools/regression/poll/sockpoll.c
blob: 31ebcaf9bcb2cfd107b8c76c31c773e91631be98 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* $FreeBSD$ */

#define _GNU_SOURCE         /* expose POLLRDHUP when testing on Linux */

#include <sys/socket.h>
#include <sys/stat.h>

#include <err.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static void
append(char *out, size_t out_size, const char *s)
{
	size_t size = strlen(out);

	snprintf(out + size, out_size - size, "%s", s);
}

static void
decode_events(int events, char *out, size_t out_size)
{
	int unknown;

	out[0] = 0;

	if (events == 0) {
		append(out, out_size, "0");
		return;
	}

#define DECODE_FLAG(x) \
	if (events & (x)) { \
		if (out[0] != 0) \
			append(out, out_size, " | "); \
		append(out, out_size, #x); \
	}

	/* Show the expected flags by name. */
	DECODE_FLAG(POLLIN);
	DECODE_FLAG(POLLOUT);
	DECODE_FLAG(POLLHUP);
#ifndef POLLRDHUP
#define KNOWN_FLAGS (POLLIN | POLLOUT | POLLHUP)
#else
	DECODE_FLAG(POLLRDHUP);
#define KNOWN_FLAGS (POLLIN | POLLOUT | POLLHUP | POLLRDHUP);
#endif

	/* Show any unexpected bits as hex. */
	unknown = events & ~KNOWN_FLAGS;
	if (unknown != 0) {
		char buf[80];

		snprintf(buf, sizeof(buf), "%s%x", out[0] != 0 ? " | " : "",
			unknown);
		append(out, out_size, buf);
	}
}

static void
report(int num, const char *state, int expected, int got)
{
	char expected_str[80];
	char got_str[80];

	decode_events(expected, expected_str, sizeof(expected_str));
	decode_events(got, got_str, sizeof(got_str));
	if (expected == got)
		printf("ok %-2d    ", num);
	else
		printf("not ok %-2d", num);
	printf(" state %s: expected %s; got %s\n",
	    state, expected_str, got_str);
	fflush(stdout);
}

static int
set_nonblocking(int sck)
{
	int flags;

	flags = fcntl(sck, F_GETFL, 0);
	flags |= O_NONBLOCK;

	if (fcntl(sck, F_SETFL, flags))
		return -1;

	return 0;
}

static char largeblock[1048576]; /* should be more than AF_UNIX sockbuf size */
static int fd[2];
static struct pollfd pfd0;
static struct pollfd pfd1;

void
setup(void)
{
	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) != 0)
		err(1, "socketpair");
	if (set_nonblocking(fd[0]) == -1)
		err(1, "fcntl");
	if (set_nonblocking(fd[1]) == -1)
		err(1, "fcntl");
	pfd0.fd = fd[0];
	pfd0.events = POLLIN | POLLOUT;
	pfd1.fd = fd[1];
	pfd1.events = POLLIN | POLLOUT;
}

int
main(void)
{
	int num;

	num = 1;
	printf("1..18\n");
	fflush(stdout);

	/* Large write with close */
	setup();
	if (poll(&pfd0, 1, 0) == -1)
		err(1, "poll");
	report(num++, "initial 0", POLLOUT, pfd0.revents);
	if (poll(&pfd1, 1, 0) == -1)
		err(1, "poll");
	report(num++, "initial 1", POLLOUT, pfd1.revents);
	if (write(fd[0], largeblock, sizeof(largeblock)) == -1)
		err(1, "write");
	if (poll(&pfd0, 1, 0) == -1)
		err(1, "poll");
	report(num++, "after large write", 0, pfd0.revents);
	if (poll(&pfd1, 1, 0) == -1)
		err(1, "poll");
	report(num++, "other side after large write", POLLIN | POLLOUT, pfd1.revents);
	close(fd[0]);
	if (poll(&pfd1, 1, 0) == -1)
		err(1, "poll");
	report(num++, "other side after close", POLLIN | POLLHUP, pfd1.revents);
	if (read(fd[1], largeblock, sizeof(largeblock)) == -1)
		err(1, "read");
	if (poll(&pfd1, 1, 0) == -1)
		err(1, "poll");
	report(num++, "other side after reading input", POLLHUP, pfd1.revents);
	close(fd[1]);

	/* With shutdown(SHUT_WR) */
	setup();
	if (shutdown(fd[0], SHUT_WR) == -1)
		err(1, "shutdown");
	if (poll(&pfd0, 1, 0) == -1)
		err(1, "poll");
	report(num++, "after shutdown(SHUT_WR)", POLLOUT, pfd0.revents);
	if (poll(&pfd1, 1, 0) == -1)
		err(1, "poll");
	report(num++, "other side after shutdown(SHUT_WR)", POLLIN | POLLOUT, pfd1.revents);
	switch (read(fd[1], largeblock, sizeof(largeblock))) {
		case 0:
			break;
		case -1:
			err(1, "read after other side shutdown");
			break;
		default:
			errx(1, "kernel made up data that was never written");
	}
	if (poll(&pfd1, 1, 0) == -1)
		err(1, "poll");
	report(num++, "other side after reading EOF", POLLIN | POLLOUT, pfd1.revents);
	if (write(fd[1], largeblock, sizeof(largeblock)) == -1)
		err(1, "write");
	if (poll(&pfd0, 1, 0) == -1)
		err(1, "poll");
	report(num++, "after data from other side", POLLIN | POLLOUT, pfd0.revents);
	if (poll(&pfd1, 1, 0) == -1)
		err(1, "poll");
	report(num++, "after writing", POLLIN, pfd1.revents);
	if (shutdown(fd[1], SHUT_WR) == -1)
		err(1, "shutdown second");
	if (poll(&pfd0, 1, 0) == -1)
		err(1, "poll");
	report(num++, "after second shutdown", POLLIN | POLLHUP, pfd0.revents);
	if (poll(&pfd1, 1, 0) == -1)
		err(1, "poll");
	report(num++, "after second shutdown", POLLHUP, pfd1.revents);
	close(fd[0]);
	if (poll(&pfd1, 1, 0) == -1)
		err(1, "poll");
	report(num++, "after close", POLLHUP, pfd1.revents);
	close(fd[1]);

	/*
	 * With shutdown(SHUT_RD)
	 * Note that shutdown(SHUT_WR) is passed to the peer, but
	 * shutdown(SHUT_RD) is not.
	 */
	setup();
	if (shutdown(fd[0], SHUT_RD) == -1)
		err(1, "shutdown");
	if (poll(&pfd0, 1, 0) == -1)
		err(1, "poll");
	report(num++, "after shutdown(SHUT_RD)", POLLIN | POLLOUT, pfd0.revents);
	if (poll(&pfd1, 1, 0) == -1)
		err(1, "poll");
	report(num++, "other side after shutdown(SHUT_RD)", POLLOUT, pfd1.revents);
	if (shutdown(fd[0], SHUT_WR) == -1)
		err(1, "shutdown");
	if (poll(&pfd0, 1, 0) == -1)
		err(1, "poll");
	report(num++, "after shutdown(SHUT_WR)", POLLHUP, pfd0.revents);
	if (poll(&pfd1, 1, 0) == -1)
		err(1, "poll");
	report(num++, "other side after shutdown(SHUT_WR)", POLLIN | POLLOUT, pfd1.revents);
	close(fd[0]);
	close(fd[1]);

#ifdef POLLRDHUP
	setup();
	pfd1.events |= POLLRDHUP;
	if (shutdown(fd[0], SHUT_RD) == -1)
		err(1, "shutdown");
	if (poll(&pfd1, 1, 0) == -1)
		err(1, "poll");
	report(num++, "other side after shutdown(SHUT_RD)", POLLOUT, pfd1.revents);
	if (write(fd[0], "x", 1) != 1)
		err(1, "write");
	if (poll(&pfd1, 1, 0) == -1)
		err(1, "poll");
	report(num++, "other side after write", POLLIN | POLLOUT, pfd1.revents);
	if (shutdown(fd[0], SHUT_WR) == -1)
		err(1, "shutdown");
	if (poll(&pfd1, 1, 0) == -1)
		err(1, "poll");
	report(num++, "other side after shutdown(SHUT_WR)", POLLIN | POLLOUT | POLLRDHUP, pfd1.revents);
	close(fd[0]);
	close(fd[1]);
#endif

	return (0);
}