3 * Test (pseudo) terminal devices
5 * Select works on regular files, (pseudo) terminal devices, streams-based
6 * files, FIFOs, pipes, and sockets. This test verifies selecting for (pseudo)
9 * This test is part of a bigger select test. It expects as argument which sub-
16 #include <sys/types.h>
19 #include <sys/select.h>
24 #define TERMINALW "/dev/ttypf"
25 #define TERMINALR "/dev/ptypf"
26 #define SENDSTRING "minixrocks"
29 int errct
= 0, subtest
= -1;
31 void e(int n
, char *s
) {
32 printf("Subtest %d, error %d, %s\n", subtest
, n
, s
);
34 if (errct
++ > MAX_ERROR
) {
35 printf("Too many errors; test aborted\n");
44 /* Opening master terminal for writing */
45 if((fd
= open(TERMINALW
, O_WRONLY
)) == -1) {
46 printf("Error opening %s for writing, signalling parent to quit\n",
49 printf("Please make sure that %s is not in use while running this test\n",
54 /* Going to sleep for two seconds to allow the parent proc to get ready */
57 select(0, NULL
, NULL
, NULL
, &tv
);
59 /* Try to write. Doesn't matter how many bytes we actually send. */
60 retval
= write(fd
, SENDSTRING
, strlen(SENDSTRING
));
63 /* Wait for another second to allow the parent to process incoming data */
65 retval
= select(0,NULL
, NULL
, NULL
, &tv
);
69 int do_parent(int child
) {
71 fd_set fds_read
, fds_write
, fds_error
;
74 /* Open slave terminal for reading */
75 if((fd
= open(TERMINALR
, O_RDONLY
)) == -1) {
76 printf("Error opening %s for reading\n", TERMINALR
);
78 printf("Please make sure that %s is not in use while running this test.\n",
80 waitpid(child
, &retval
, 0);
85 FD_ZERO(&fds_read
); FD_ZERO(&fds_write
); FD_ZERO(&fds_error
);
87 FD_SET(fd
, &fds_read
);
88 FD_SET(fd
, &fds_write
);
90 /* Test if we can read or write from/to fd. As fd is opened read only we
91 * cannot actually write, so the select should return immediately with fd
92 * set in fds_write, but not in fds_read. Note that the child waits two
93 * seconds before sending data. This gives us the opportunity run this
94 * sub-test as reading from fd is blocking at this point. */
95 retval
= select(fd
+1, &fds_read
, &fds_write
, &fds_error
, NULL
);
97 if(retval
!= 1) e(1, "incorrect amount of ready file descriptors");
100 if(FD_ISSET(fd
, &fds_read
)) e(2, "read should NOT be set");
101 if(!FD_ISSET(fd
, &fds_write
)) e(3, "write should be set");
102 if(FD_ISSET(fd
, &fds_error
)) e(4, "error should NOT be set");
104 /* Block until ready; until child wrote stuff */
105 FD_ZERO(&fds_read
); FD_ZERO(&fds_write
); FD_ZERO(&fds_error
);
106 FD_SET(fd
, &fds_read
);
107 retval
= select(fd
+1, &fds_read
, NULL
, &fds_error
, NULL
);
109 if(retval
!= 1) e(5, "incorrect amount of ready file descriptors");
110 if(!FD_ISSET(fd
, &fds_read
)) e(6, "read should be set");
111 if(FD_ISSET(fd
, &fds_error
)) e(7, "error should not be set");
114 FD_ZERO(&fds_read
); FD_ZERO(&fds_error
);
115 FD_SET(fd
,&fds_write
);
116 retval
= select(fd
+1, NULL
, &fds_write
, NULL
, NULL
);
117 /* As it is impossible to write to a read only fd, this select should return
118 * immediately with fd set in fds_write. */
119 if(retval
!= 1) e(8, "incorrect amount or ready file descriptors");
122 waitpid(child
, &retval
, 0);
126 int main(int argc
, char **argv
) {
129 /* Get subtest number */
131 printf("Usage: %s subtest_no\n", argv
[0]);
133 } else if(sscanf(argv
[1], "%d", &subtest
) != 1) {
134 printf("Usage: %s subtest_no\n", argv
[0]);
139 if(forkres
== 0) do_child();
140 else if(forkres
> 0) do_parent(forkres
);
141 else { /* Fork failed */
142 perror("Unable to fork");
146 exit(-2); /* We're not supposed to get here. Both do_* routines should exit*/