5 * Select works on regular files, (pseudo) terminal devices, streams-based
6 * files, FIFOs, pipes, and sockets. This test verifies selecting for FIFOs
7 * (named pipes) and pipes (anonymous pipes). This test will not verify most
8 * error file descriptors, as the setting of this fdset in the face of an error
9 * condition is implementation-specific (except for regular files (alway set)
10 * and sockets (protocol-specific or OOB data received), but those file types
11 * are not being tested in this specific test).
13 * This test is part of a bigger select test. It expects as argument which sub-
16 * [1] If a socket has a pending error, it shall be considered to have an
17 * exceptional condition pending. Otherwise, what constitutes an exceptional
18 * condition is file type-specific. For a file descriptor for use with a
19 * socket, it is protocol-specific except as noted below. For other file types
20 * it is implementation-defined. If the operation is meaningless for a
21 * particular file type, pselect() or select() shall indicate that the
22 * descriptor is ready for read or write operations, and shall indicate that
23 * the descriptor has no exceptional condition pending.
29 #include <sys/types.h>
32 #include <sys/select.h>
39 #define NAMEDPIPE1 "selecttestd-1"
40 #define NAMEDPIPE2 "selecttestd-2"
41 #define SENDSTRING "minixrocks"
42 #define DO_HANDLEDATA 1
50 int fd_ap
[2]; /* Anonymous pipe; read from fd_ap[0], write to fd_ap[1] */
51 int fd_np1
; /* Named pipe */
52 int fd_np2
; /* Named pipe */
54 static void do_child(void) {
57 /* Open named pipe for writing. This will block until a reader arrives. */
58 if((fd_np1
= open(NAMEDPIPE1
, O_WRONLY
)) == -1) {
59 printf("Error opening %s for writing, signalling parent to quit\n",
62 printf("Please make sure that %s is not in use while running this test\n",
67 /* Going to sleep for three seconds to allow the parent proc to get ready */
68 tv
.tv_sec
= DO_HANDLEDATA
;
70 select(0, NULL
, NULL
, NULL
, &tv
);
72 /* Try to write. Doesn't matter how many bytes we actually send. */
73 (void) write(fd_np1
, SENDSTRING
, strlen(SENDSTRING
));
75 /* Wait for another second to allow the parent to process incoming data */
76 tv
.tv_sec
= DO_HANDLEDATA
;
78 (void) select(0,NULL
, NULL
, NULL
, &tv
);
82 /* Wait for another second to allow the parent to process incoming data */
83 tv
.tv_sec
= DO_HANDLEDATA
;
85 (void) select(0,NULL
, NULL
, NULL
, &tv
);
87 /* Open named pipe for reading. This will block until a writer arrives. */
88 if((fd_np2
= open(NAMEDPIPE2
, O_RDONLY
)) == -1) {
89 printf("Error opening %s for reading, signalling parent to quit\n",
92 printf("Please make sure that %s is not in use while running this test\n",
97 /* Wait for another second to allow the parent to run some tests. */
98 tv
.tv_sec
= DO_HANDLEDATA
;
100 (void) select(0, NULL
, NULL
, NULL
, &tv
);
106 /* Let the parent do initial read and write tests from and to the pipe. */
107 tv
.tv_sec
= DO_PAUSE
;
109 (void) select(0, NULL
, NULL
, NULL
, &tv
);
111 /* Unblock blocking read select by writing data */
112 if(write(fd_ap
[1], SENDSTRING
, strlen(SENDSTRING
)) < 0) {
113 perror("Could not write to anonymous pipe");
121 static int count_fds(int nfds
, fd_set
*fds
) {
122 /* Return number of bits set in fds */
124 assert(fds
!= NULL
&& nfds
> 0);
125 for(i
= 0; i
< nfds
; i
++) {
126 if(FD_ISSET(i
, fds
)) result
++;
132 static int empty_fds(int nfds
, fd_set
*fds
) {
133 /* Returns nonzero if the first bits up to nfds in fds are not set */
135 assert(fds
!= NULL
&& nfds
> 0);
136 for(i
= 0; i
< nfds
; i
++) if(FD_ISSET(i
, fds
)) return 0;
140 static int compare_fds(int nfds
, fd_set
*lh
, fd_set
*rh
) {
141 /* Returns nonzero if lh equals rh up to nfds bits */
143 assert(lh
!= NULL
&& rh
!= NULL
&& nfds
> 0);
144 for(i
= 0; i
< nfds
; i
++) {
145 if((FD_ISSET(i
, lh
) && !FD_ISSET(i
, rh
)) ||
146 (!FD_ISSET(i
, lh
) && FD_ISSET(i
, rh
))) {
154 static void dump_fds(int nfds
, fd_set
*fds
) {
155 /* Print a graphical representation of bits in fds */
157 if(fds
!= NULL
&& nfds
> 0) {
158 for(i
= 0; i
< nfds
; i
++) printf("%d ", (FD_ISSET(i
, fds
) ? 1 : 0));
164 static void do_parent(int child
) {
165 fd_set fds_read
, fds_write
, fds_error
;
166 fd_set fds_compare_read
, fds_compare_write
;
172 /* Open named pipe for reading. This will block until a writer arrives. */
173 if((fd_np1
= open(NAMEDPIPE1
, O_RDONLY
)) == -1) {
174 printf("Error opening %s for reading\n", NAMEDPIPE1
);
176 printf("Please make sure that %s is not in use while running this test.\n",
178 waitpid(child
, &retval
, 0);
182 /* Clear bit masks */
183 FD_ZERO(&fds_read
); FD_ZERO(&fds_write
);
184 /* Set read and write bits */
185 FD_SET(fd_np1
, &fds_read
);
186 FD_SET(fd_np1
, &fds_write
);
187 tv
.tv_sec
= DO_TIMEOUT
;
190 /* Test if we can read or write from/to fd_np1. As fd_np1 is opened read only
191 * we cannot actually write, so the select should return immediately [1] and
192 * the offending bit set in the fd set. We read from a pipe that is opened
193 * with O_NONBLOCKING cleared, so it is guaranteed we can read.
194 * However, at this moment the writer is sleeping, so the pipe is empty and
195 * read is supposed to block. Therefore, only 1 file descriptor should be
196 * ready. A timeout value is still set in case an error occurs in a faulty
198 retval
= select(fd_np1
+1, &fds_read
, &fds_write
, NULL
, &tv
);
200 /* Did we receive an error? */
202 snprintf(errbuf
, sizeof(errbuf
),
203 "one fd should be set%s", (retval
== 0 ? " (TIMEOUT)" : ""));
207 if(!empty_fds(fd_np1
+1,&fds_read
)) em(2, "no read bits should be set");
210 /* Make sure the write bit is set (and just 1 bit) */
211 FD_ZERO(&fds_compare_write
); FD_SET(fd_np1
, &fds_compare_write
);
212 if(!compare_fds(fd_np1
+1, &fds_compare_write
, &fds_write
))
213 em(3, "write should be set");
215 /* Clear sets and set up new bit masks */
216 FD_ZERO(&fds_read
); FD_ZERO(&fds_write
);
217 FD_SET(fd_np1
, &fds_read
);
218 tv
.tv_sec
= DO_TIMEOUT
; /* To make sure we get to see some error messages
219 instead of blocking forever when the
220 implementation is faulty. A timeout causes retval
223 /* The sleeping writer is about to wake up and write data to the pipe. */
224 retval
= select(fd_np1
+1, &fds_read
, &fds_write
, NULL
, &tv
);
226 /* Correct amount of ready file descriptors? Just 1 read */
228 snprintf(errbuf
, sizeof(errbuf
),
229 "one fd should be set%s", (retval
== 0 ? " (TIMEOUT)" : ""));
233 if(!FD_ISSET(fd_np1
, &fds_read
)) em(5, "read should be set");
235 /* Note that we left the write set empty. This should be equivalent to
236 * setting this parameter to NULL. */
237 if(!empty_fds(fd_np1
+1, &fds_write
)) em(6, "write should NOT be set");
239 /* In case something went wrong above, we might end up with a child process
240 * blocking on a write call we close the file descriptor now. Synchronize on
242 if(read(fd_np1
, buf
, sizeof(SENDSTRING
)) < 0) perror("Read error");
244 /* Close file descriptor. We're going to reverse the test */
247 /* Wait for a second to allow the child to close the pipe as well */
248 tv
.tv_sec
= DO_HANDLEDATA
;
250 retval
= select(0,NULL
, NULL
, NULL
, &tv
);
252 /* Open named pipe for writing. This call blocks until a reader arrives. */
253 if((fd_np2
= open(NAMEDPIPE2
, O_WRONLY
)) == -1) {
254 printf("Error opening %s for writing\n",
257 printf("Please make sure that %s is not in use while running this test\n",
262 /* At this moment the child process has opened the named pipe for reading and
263 * we have opened it for writing. We're now going to reverse some of the
264 * tests we've done earlier. */
266 /* Clear sets and set up bit masks */
267 FD_ZERO(&fds_read
); FD_ZERO(&fds_write
); FD_ZERO(&fds_error
);
268 FD_SET(fd_np2
, &fds_read
);
269 FD_SET(fd_np2
, &fds_write
);
270 tv
.tv_sec
= DO_TIMEOUT
;
272 /* Select for reading from an fd opened O_WRONLY. This should return
273 * immediately as it is not a meaningful operation [1] and is therefore not
274 * blocking. The select should return two file descriptors are ready (the
275 * failing read and valid write). */
277 retval
= select(fd_np2
+1, &fds_read
, &fds_write
, &fds_error
, &tv
);
279 /* Did we receive an error? */
281 snprintf(errbuf
, sizeof(errbuf
),
282 "two fds should be set%s", (retval
== 0 ? " (TIMEOUT)" : ""));
286 /* Make sure read bit is set (and just 1 bit) */
287 FD_ZERO(&fds_compare_read
); FD_SET(fd_np2
, &fds_compare_read
);
288 if(!compare_fds(fd_np2
+1, &fds_compare_read
, &fds_read
))
289 em(8, "read should be set");
291 /* Write bit should be set (and just 1 bit) */
292 FD_ZERO(&fds_compare_write
); FD_SET(fd_np2
, &fds_compare_write
);
293 if(!compare_fds(fd_np2
+1, &fds_compare_write
, &fds_write
))
294 em(9, "write should be set");
296 if(!empty_fds(fd_np2
+1, &fds_error
))
297 em(10, "Error should NOT be set");
299 FD_ZERO(&fds_read
); FD_ZERO(&fds_write
);
300 FD_SET(fd_np2
, &fds_write
);
301 tv
.tv_sec
= DO_TIMEOUT
;
303 retval
= select(fd_np2
+1, &fds_read
, &fds_write
, NULL
, &tv
);
305 /* Correct amount of ready file descriptors? Just 1 write */
307 snprintf(errbuf
, sizeof(errbuf
),
308 "one fd should be set%s", (retval
== 0 ? " (TIMEOUT)" : ""));
312 if(!empty_fds(fd_np2
+1, &fds_read
)) em(12, "read should NOT be set");
316 /* Check if we can write to the pipe */
317 FD_ZERO(&fds_read
); FD_ZERO(&fds_write
);
318 FD_SET(fd_ap
[1], &fds_write
);
319 tv
.tv_sec
= DO_TIMEOUT
;
321 retval
= select(fd_ap
[1]+1, NULL
, &fds_write
, NULL
, &tv
);
323 /* Correct amount of ready file descriptors? Just 1 write */
325 snprintf(errbuf
, sizeof(errbuf
),
326 "one fd should be set%s", (retval
== 0 ? " (TIMEOUT)" : ""));
330 /* Make sure write bit is set (and just 1 bit) */
331 FD_ZERO(&fds_compare_write
); FD_SET(fd_ap
[1], &fds_compare_write
);
332 if(!compare_fds(fd_ap
[1]+1, &fds_compare_write
, &fds_write
))
333 em(14, "write should be set");
335 /* Intentionally test reading from pipe and letting it time out. */
336 FD_SET(fd_ap
[0], &fds_read
);
340 retval
= select(fd_ap
[0]+1, &fds_read
, NULL
, NULL
, &tv
);
343 /* Did we time out? */
344 if(retval
!= 0) em(15, "we should have timed out");
346 /* Did it take us approximately 1 second? */
347 if((int) (end
- start
) != 1) {
348 snprintf(errbuf
, sizeof(errbuf
),
349 "time out is not 1 second (instead, it is %ld)",
350 (long int) (end
- start
));
354 /* Do another read, but this time we expect incoming data from child. */
356 FD_SET(fd_ap
[0], &fds_read
);
357 tv
.tv_sec
= DO_TIMEOUT
;
359 retval
= select(fd_ap
[0]+1, &fds_read
, NULL
, NULL
, &tv
);
361 /* Correct amount of ready file descriptors? Just 1 read. */
362 if(retval
!= 1) em(17, "one fd should be set");
364 /* Is the read bit set? And just 1 bit. */
365 FD_ZERO(&fds_compare_read
); FD_SET(fd_ap
[0], &fds_compare_read
);
366 if(!compare_fds(fd_ap
[0]+1, &fds_compare_read
, &fds_read
))
367 em(18, "read should be set.");
369 /* By convention fd_ap[0] is meant to be used for reading from the pipe and
370 * fd_ap[1] is meant for writing, where fd_ap is a an anonymous pipe.
371 * However, it is unspecified what happens when fd_ap[0] is used for writing
372 * and fd_ap[1] for reading. (It is unsupported on Minix.) As such, it is not
373 * necessary to make test cases for wrong pipe file descriptors using select.
376 waitpid(child
, &retval
, 0);
382 int main(int argc
, char **argv
) {
385 /* Get subtest number */
387 printf("Usage: %s subtest_no\n", argv
[0]);
389 } else if(sscanf(argv
[1], "%d", &subtest
) != 1) {
390 printf("Usage: %s subtest_no\n", argv
[0]);
394 /* Set up anonymous pipe */
395 if(pipe(fd_ap
) < 0) {
396 perror("Could not create anonymous pipe");
400 /* Create named pipe2. It is unlinked by do_parent. */
401 if(mkfifo(NAMEDPIPE1
, 0600) < 0) {
402 printf("Could not create named pipe %s", NAMEDPIPE1
);
407 if(mkfifo(NAMEDPIPE2
, 0600) < 0) {
408 printf("Could not create named pipe %s", NAMEDPIPE2
);
414 if(forkres
== 0) do_child();
415 else if(forkres
> 0) do_parent(forkres
);
416 else { /* Fork failed */
417 perror("Unable to fork");
421 exit(-2); /* We're not supposed to get here. Both do_* routines should exit*/