5 * Select works on regular files, (pseudo) terminal devices, streams-based
6 * files, FIFOs, pipes, and sockets. This test verifies selecting for regular
7 * file descriptors. "File descriptors associated with regular files shall
8 * always select true for ready to read, ready to write, and error conditions"
9 * - Open Group. Although we set a timeout, the select should return
12 * This test is part of a bigger select test. It expects as argument which sub-
19 #include <sys/types.h>
22 #include <sys/select.h>
26 #define FILE1 "selecttestb-1"
32 int errct
= 0, subtest
= -1;
35 void e(int n
, char *s
) {
36 printf("Subtest %d, error %d, %s\n", subtest
, n
, s
);
38 if (errct
++ > MAX_ERROR
) {
39 printf("Too many errors; test aborted\n");
44 int main(int argc
, char **argv
) {
46 fd_set fds_read
, fds_write
, fds_error
;
50 /* Get subtest number */
52 printf("Usage: %s subtest_no\n", argv
[0]);
54 } else if(sscanf(argv
[1], "%d", &subtest
) != 1) {
55 printf("Usage: %s subtest_no\n", argv
[0]);
63 /* Open a file for writing */
64 if((fd1
= open(FILE1
, O_WRONLY
|O_CREAT
, 0644)) == -1) {
65 snprintf(errorbuf
, sizeof(errorbuf
), "failed to open file %s for writing",
72 /* Open the same file for reading */
73 if((fd2
= open(FILE1
, O_RDONLY
)) == -1) {
74 snprintf(errorbuf
, sizeof(errorbuf
), "failed to open file %s for reading",
81 /* Clear file descriptor bit masks */
82 FD_ZERO(&fds_read
); FD_ZERO(&fds_write
); FD_ZERO(&fds_error
);
85 FD_SET(fd1
, &fds_write
);
86 FD_SET(fd2
, &fds_read
);
87 FD_SET(fd1
, &fds_error
);
88 FD_SET(fd2
, &fds_error
);
90 /* Do the select and time how long it takes */
92 retval
= select(fd2
+1, &fds_read
, &fds_write
, &fds_error
, &tv
);
95 /* Correct amount of ready file descriptors? 1 read + 1 write + 2 errors */
97 e(3, "four fds should be set");
100 /* Test resulting bit masks */
101 if(!FD_ISSET(fd1
, &fds_write
)) e(4, "write should be set");
102 if(!FD_ISSET(fd2
, &fds_read
)) e(5, "read should be set");
103 if(!FD_ISSET(fd1
, &fds_error
)) e(6, "error should be set");
104 if(!FD_ISSET(fd2
, &fds_error
)) e(7, "error should be set");
106 /* Was it instantaneous? */
107 if(end
-start
!= TIME
- TIME
) {
108 snprintf(errorbuf
,sizeof(errorbuf
),"time spent blocking is not %d, but %ld",
109 TIME
- TIME
, (long int) (end
-start
));
113 /* Wait for read to become ready on O_WRONLY. This should fail immediately. */
114 FD_ZERO(&fds_read
); FD_ZERO(&fds_write
); FD_ZERO(&fds_error
);
115 FD_SET(fd1
, &fds_read
);
116 FD_SET(fd1
, &fds_error
);
117 FD_SET(fd2
, &fds_error
);
120 retval
= select(fd2
+1, &fds_read
, NULL
, &fds_error
, &tv
);
122 /* Correct amount of ready file descriptors? 1 read + 2 error */
123 if(retval
!= 3) e(9, "incorrect amount of ready file descriptors");
124 if(!FD_ISSET(fd1
, &fds_read
)) e(10, "read should be set");
125 if(!FD_ISSET(fd1
, &fds_error
)) e(11, "error should be set");
126 if(!FD_ISSET(fd2
, &fds_error
)) e(12, "error should be set");
128 /* Try again as above, bit this time with O_RDONLY in the write set */
130 FD_SET(fd2
, &fds_write
);
131 FD_SET(fd1
, &fds_error
);
132 FD_SET(fd2
, &fds_error
);
135 retval
= select(fd2
+1, NULL
, &fds_write
, &fds_error
, &tv
);
137 /* Correct amount of ready file descriptors? 1 write + 2 errors */
138 if(retval
!= 3) e(13, "incorrect amount of ready file descriptors");
139 if(!FD_ISSET(fd2
, &fds_write
)) e(14, "write should be set");
140 if(!FD_ISSET(fd1
, &fds_error
)) e(15, "error should be set");
141 if(!FD_ISSET(fd2
, &fds_error
)) e(16, "error should be set");