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>
28 #define FILE1 "selecttestb-1"
36 int main(int argc
, char **argv
) {
38 fd_set fds_read
, fds_write
, fds_error
;
42 /* Get subtest number */
44 printf("Usage: %s subtest_no\n", argv
[0]);
46 } else if(sscanf(argv
[1], "%d", &subtest
) != 1) {
47 printf("Usage: %s subtest_no\n", argv
[0]);
55 /* Open a file for writing */
56 if((fd1
= open(FILE1
, O_WRONLY
|O_CREAT
, 0644)) == -1) {
57 snprintf(errorbuf
, sizeof(errorbuf
), "failed to open file %s for writing",
64 /* Open the same file for reading */
65 if((fd2
= open(FILE1
, O_RDONLY
)) == -1) {
66 snprintf(errorbuf
, sizeof(errorbuf
), "failed to open file %s for reading",
73 /* Clear file descriptor bit masks */
74 FD_ZERO(&fds_read
); FD_ZERO(&fds_write
); FD_ZERO(&fds_error
);
77 FD_SET(fd1
, &fds_write
);
78 FD_SET(fd2
, &fds_read
);
79 FD_SET(fd1
, &fds_error
);
80 FD_SET(fd2
, &fds_error
);
82 /* Do the select and time how long it takes */
84 retval
= select(fd2
+1, &fds_read
, &fds_write
, &fds_error
, &tv
);
87 /* Correct amount of ready file descriptors? 1 read + 1 write + 2 errors */
89 em(3, "four fds should be set");
92 /* Test resulting bit masks */
93 if(!FD_ISSET(fd1
, &fds_write
)) em(4, "write should be set");
94 if(!FD_ISSET(fd2
, &fds_read
)) em(5, "read should be set");
95 if(!FD_ISSET(fd1
, &fds_error
)) em(6, "error should be set");
96 if(!FD_ISSET(fd2
, &fds_error
)) em(7, "error should be set");
98 /* Was it instantaneous? */
99 if(end
-start
!= TIME
- TIME
) {
100 snprintf(errorbuf
,sizeof(errorbuf
),"time spent blocking is not %d, but %ld",
101 TIME
- TIME
, (long int) (end
-start
));
105 /* Wait for read to become ready on O_WRONLY. This should fail immediately. */
106 FD_ZERO(&fds_read
); FD_ZERO(&fds_write
); FD_ZERO(&fds_error
);
107 FD_SET(fd1
, &fds_read
);
108 FD_SET(fd1
, &fds_error
);
109 FD_SET(fd2
, &fds_error
);
112 retval
= select(fd2
+1, &fds_read
, NULL
, &fds_error
, &tv
);
114 /* Correct amount of ready file descriptors? 1 read + 2 error */
115 if(retval
!= 3) em(9, "incorrect amount of ready file descriptors");
116 if(!FD_ISSET(fd1
, &fds_read
)) em(10, "read should be set");
117 if(!FD_ISSET(fd1
, &fds_error
)) em(11, "error should be set");
118 if(!FD_ISSET(fd2
, &fds_error
)) em(12, "error should be set");
120 /* Try again as above, bit this time with O_RDONLY in the write set */
122 FD_SET(fd2
, &fds_write
);
123 FD_SET(fd1
, &fds_error
);
124 FD_SET(fd2
, &fds_error
);
127 retval
= select(fd2
+1, NULL
, &fds_write
, &fds_error
, &tv
);
129 /* Correct amount of ready file descriptors? 1 write + 2 errors */
130 if(retval
!= 3) em(13, "incorrect amount of ready file descriptors");
131 if(!FD_ISSET(fd2
, &fds_write
)) em(14, "write should be set");
132 if(!FD_ISSET(fd1
, &fds_error
)) em(15, "error should be set");
133 if(!FD_ISSET(fd2
, &fds_error
)) em(16, "error should be set");