1 /*
2  * Rudimentary test suite used while implementing pselect(2).
3  */
4 
5 #include <assert.h>
6 #include <errno.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <unistd.h>
11 
12 
13 static int alarm_flag = 0;
14 
15 
16 static void
17 nop(int signo)
18 {
19 }
20 
21 
22 static void
23 set_alarm_flag(int signo)
24 {
25 	alarm_flag = 1;
26 }
27 
28 
29 /*
30  * Try to detect regressions in select(2).
31  */
32 
33 static void
34 test_select()
35 {
36 	fd_set rset;
37 	fd_set wset;
38 	struct timeval timeout;
39 	int des[2];
40 	int r;
41 	char buf[1];
42 
43 	printf("test_select\n");
44 
45 	/*
46 	 * It is always possible to write to stdout (if not redirected).
47 	 */
48 
49 	FD_ZERO(&wset);
50 	FD_SET(1, &wset);
51 
52 	r = select(2, NULL, &wset, NULL, NULL);
53 	assert(r == 1);
54 	assert(FD_ISSET(1, &wset));
55 
56 	/*
57 	 * Write to a pipe and check a select on the read end does not block.
58 	 */
59 
60 	r = pipe(des);
61 	assert(r == 0);
62 
63 	FD_ZERO(&rset);
64 	FD_SET(des[0], &rset);
65 
66 	buf[0] = 'f';
67 	r = write(des[1], buf, 1);
68 	assert(r == 1);
69 
70 	r = select(des[0]+1, &rset, NULL, NULL, NULL);
71 	assert(r == 1);
72 	assert(FD_ISSET(des[0], &rset));
73 
74 	r = read(des[0], buf, 1);
75 	assert(r == 1);
76 	assert(buf[0] == 'f');
77 
78 	/*
79 	 * Block until signal reception.
80 	 */
81 
82 	signal(SIGALRM, nop);
83 	alarm(1);
84 
85 	FD_ZERO(&rset);
86 	FD_SET(des[0], &rset);
87 
88 	r = select(des[0]+1, &rset, NULL, NULL, NULL);
89 	assert(r == -1);
90 	assert(errno == EINTR);
91 
92 	/*
93 	 * Block until timeout.
94 	 */
95 
96 	FD_ZERO(&rset);
97 	FD_SET(des[0], &rset);
98 
99 	timeout.tv_sec = 1;
100 	timeout.tv_usec = 0;
101 	r = select(des[0]+1, &rset, NULL, NULL, &timeout);
102 	assert(r == 0);
103 
104 	/*
105 	 * When the timeout is zero, the call should not block.
106 	 */
107 
108 	timeout.tv_sec = 0;
109 	timeout.tv_usec = 0;
110 	FD_ZERO(&rset);
111 	FD_SET(des[0], &rset);
112 
113 	r = select(des[0]+1, &rset, NULL, NULL, &timeout);
114 	assert(r == 0);
115 
116 	close(des[0]);
117 	close(des[1]);
118 }
119 
120 int
121 main(void)
122 {
123 	test_select();
124 	return (0);
125 }
126