summaryrefslogtreecommitdiffstats
path: root/tools/regression/poll/pipeselect.c
blob: fe6e8ed33daa802ad4354bb3575f0c630434027f (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* $FreeBSD$ */

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

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

#define	FIFONAME	"fifo.tmp"
#define	FT_END		3
#define	FT_FIFO		2
#define	FT_PIPE		0
#define	FT_SOCKETPAIR	1

#define	SETUP(fd, rfds, tv) do {				\
	FD_ZERO(&(rfds));					\
	FD_SET((fd), &(rfds));					\
	(tv).tv_sec = 0;					\
	(tv).tv_usec = 0;					\
} while (0)

static int filetype;

static const char *
decode_events(int events)
{
	return (events ? "set" : "clear");
}

static void
report(int num, const char *state, int expected, int got)
{
	if (!expected == !got)
		printf("ok %-2d    ", num);
	else
		printf("not ok %-2d", num);
	printf(" %s state %s: expected %s; got %s\n",
	    filetype == FT_PIPE ? "Pipe" :
	    filetype == FT_SOCKETPAIR ? "Sock" : "FIFO",
	    state, decode_events(expected), decode_events(got));
	fflush(stdout);
}

static pid_t cpid;
static pid_t ppid;
static volatile sig_atomic_t state;

static void
catch(int sig)
{
	state++;
}

static void
child(int fd, int num)
{
	fd_set rfds;
	struct timeval tv;
	int fd1, fd2;
	char buf[256];

	if (filetype == FT_FIFO) {
		fd = open(FIFONAME, O_RDONLY | O_NONBLOCK);
		if (fd < 0)
			err(1, "open for read");
	}
	if (fd >= FD_SETSIZE)
		errx(1, "fd = %d too large for select()", fd);

	if (filetype == FT_FIFO) {
		SETUP(fd, rfds, tv);
		if (select(fd + 1, &rfds, NULL, NULL, &tv) < 0)
			err(1, "select");
		/*
		 * This state (a reader for which there has never been a
		 * writer) is reported quite differently for select() than
		 * for poll().  select() must see a ready-to-read descriptor
		 * since read() will see EOF and not block; it cannot
		 * distinguish this state from the one of a reader for which
		 * there has been a writer but all writers have gone away
		 * and all data has been read.  poll() and distinguish these
		 * states by returning POLLHUP only for the latter; it does
		 * this, although this makes it inconsistent with the
		 * blockability of read() in the former.
		 */
		report(num++, "0", 1, FD_ISSET(fd, &rfds));
	}
	kill(ppid, SIGUSR1);

	usleep(1);
	while (state != 1)
		;
	if (filetype != FT_FIFO) {
		/*
		 * The connection cannot be reestablished.  Use the code that
		 * delays the read until after the writer disconnects since
		 * that case is more interesting.
		 */
		state = 4;
		goto state4;
	}
	SETUP(fd, rfds, tv);
	if (select(fd + 1, &rfds, NULL, NULL, &tv) < 0)
		err(1, "select");
	report(num++, "1", 0, FD_ISSET(fd, &rfds));
	kill(ppid, SIGUSR1);

	usleep(1);
	while (state != 2)
		;
	SETUP(fd, rfds, tv);
	if (select(fd + 1, &rfds, NULL, NULL, &tv) < 0)
		err(1, "select");
	report(num++, "2", 1, FD_ISSET(fd, &rfds));
	if (read(fd, buf, sizeof buf) != 1)
		err(1, "read");
	SETUP(fd, rfds, tv);
	if (select(fd + 1, &rfds, NULL, NULL, &tv) < 0)
		err(1, "select");
	report(num++, "2a", 0, FD_ISSET(fd, &rfds));
	kill(ppid, SIGUSR1);

	usleep(1);
	while (state != 3)
		;
	SETUP(fd, rfds, tv);
	if (select(fd + 1, &rfds, NULL, NULL, &tv) < 0)
		err(1, "select");
	report(num++, "3", 1, FD_ISSET(fd, &rfds));
	kill(ppid, SIGUSR1);

	/*
	 * Now we expect a new writer, and a new connection too since
	 * we read all the data.  The only new point is that we didn't
	 * start quite from scratch since the read fd is not new.  Check
	 * startup state as above, but don't do the read as above.
	 */
	usleep(1);
	while (state != 4)
		;
state4:
	SETUP(fd, rfds, tv);
	if (select(fd + 1, &rfds, NULL, NULL, &tv) < 0)
		err(1, "select");
	report(num++, "4", 0, FD_ISSET(fd, &rfds));
	kill(ppid, SIGUSR1);

	usleep(1);
	while (state != 5)
		;
	SETUP(fd, rfds, tv);
	if (select(fd + 1, &rfds, NULL, NULL, &tv) < 0)
		err(1, "select");
	report(num++, "5", 1, FD_ISSET(fd, &rfds));
	kill(ppid, SIGUSR1);

	usleep(1);
	while (state != 6)
		;
	/*
	 * Now we have no writer, but should still have data from the old
	 * writer.  Check that we have a data-readable condition, and that
	 * the data can be read in the usual way.
	 */
	SETUP(fd, rfds, tv);
	if (select(fd + 1, &rfds, NULL, NULL, &tv) < 0)
		err(1, "select");
	report(num++, "6", 1, FD_ISSET(fd, &rfds));
	if (read(fd, buf, sizeof buf) != 1)
		err(1, "read");
	SETUP(fd, rfds, tv);
	if (select(fd + 1, &rfds, NULL, NULL, &tv) < 0)
		err(1, "select");
	report(num++, "6a", 1, FD_ISSET(fd, &rfds));
	if (filetype == FT_FIFO) {
		/*
		 * Check that the readable-data condition is sticky for a
		 * new reader and for the old reader.  We really only have
		 * a hangup condition, but select() can only see this as
		 * a readable-data condition for null data.  select()
		 * cannot distinguish this state from the initial state
		 * where there is a reader but has never been a writer, so
		 * the following tests (to follow the pattern in pipepoll.c)
		 * essentially test state 0 again.
		 */
		fd2 = open(FIFONAME, O_RDONLY | O_NONBLOCK);
		if (fd2 < 0)
			err(1, "open for read");
		fd1 = fd;
		fd = fd2;
		SETUP(fd, rfds, tv);
		if (select(fd + 1, &rfds, NULL, NULL, &tv) < 0)
			err(1, "select");
		report(num++, "6b", 1, FD_ISSET(fd, &rfds));
		fd = fd1;
		SETUP(fd, rfds, tv);
		if (select(fd + 1, &rfds, NULL, NULL, &tv) < 0)
			err(1, "select");
		report(num++, "6c", 1, FD_ISSET(fd, &rfds));
		close(fd2);
		SETUP(fd, rfds, tv);
		if (select(fd + 1, &rfds, NULL, NULL, &tv) < 0)
			err(1, "select");
		report(num++, "6d", 1, FD_ISSET(fd, &rfds));
	}
	close(fd);
	kill(ppid, SIGUSR1);

	exit(0);
}

static void
parent(int fd)
{
	usleep(1);
	while (state != 1)
		;
	if (filetype == FT_FIFO) {
		fd = open(FIFONAME, O_WRONLY | O_NONBLOCK);
		if (fd < 0)
			err(1, "open for write");
	}
	kill(cpid, SIGUSR1);

	usleep(1);
	while (state != 2)
		;
	if (write(fd, "", 1) != 1)
		err(1, "write");
	kill(cpid, SIGUSR1);

	usleep(1);
	while (state != 3)
		;
	if (close(fd) != 0)
		err(1, "close for write");
	kill(cpid, SIGUSR1);

	usleep(1);
	while (state != 4)
		;
	if (filetype != FT_FIFO)
		return;
	fd = open(FIFONAME, O_WRONLY | O_NONBLOCK);
	if (fd < 0)
		err(1, "open for write");
	kill(cpid, SIGUSR1);

	usleep(1);
	while (state != 5)
		;
	if (write(fd, "", 1) != 1)
		err(1, "write");
	kill(cpid, SIGUSR1);

	usleep(1);
	while (state != 6)
		;
	if (close(fd) != 0)
		err(1, "close for write");
	kill(cpid, SIGUSR1);

	usleep(1);
	while (state != 7)
		;
}

int
main(void)
{
	int fd[2], num;

	num = 1;
	printf("1..20\n");
	fflush(stdout);
	signal(SIGUSR1, catch);
	ppid = getpid();
	for (filetype = 0; filetype < FT_END; filetype++) {
		switch (filetype) {
		case FT_FIFO:
			if (mkfifo(FIFONAME, 0666) != 0)
				err(1, "mkfifo");
			fd[0] = -1;
			fd[1] = -1;
			break;
		case FT_SOCKETPAIR:
			if (socketpair(AF_UNIX, SOCK_STREAM, AF_UNSPEC,
			    fd) != 0)
				err(1, "socketpair");
			break;
		case FT_PIPE:
			if (pipe(fd) != 0)
				err(1, "pipe");
			break;
		}
		state = 0;
		switch (cpid = fork()) {
		case -1:
			err(1, "fork");
		case 0:
			(void)close(fd[1]);
			child(fd[0], num);
			break;
		default:
			(void)close(fd[0]);
			parent(fd[1]);
			break;
		}
		num += filetype == FT_FIFO ? 12 : 4;
	}
	(void)unlink(FIFONAME);
	return (0);
}
OpenPOWER on IntegriCloud