release.sh: allow REPO and GITBRANCH env override
[minix.git] / test / t40c.c
blobc816b2647006d1cf7522a339bde7a6a374948859
1 /* t40c.c
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)
7 * terminal devices.
9 * This test is part of a bigger select test. It expects as argument which sub-
10 * test it is.
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <sys/select.h>
20 #include <errno.h>
21 #include <sys/wait.h>
22 #include <string.h>
24 #define TERMINALW "/dev/ttypf"
25 #define TERMINALR "/dev/ptypf"
26 #define SENDSTRING "minixrocks"
27 #define MAX_ERROR 5
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");
36 exit(errct);
40 void open_terminal(int *child_fd, int *parent_fd) {
41 int fd1, fd2, i;
42 char opentermw[5+OPEN_MAX+1];
43 char opentermr[5+OPEN_MAX+1];
44 char *term[] = {"f","e","d","c","b","a","9","8","7","6","5","4","3","2","1"};
45 #define TERMS (sizeof(term)/sizeof(term[0]))
47 if (!child_fd || !parent_fd) exit(EXIT_FAILURE);
49 for (i = 0; i < TERMS; i++) {
50 snprintf(opentermw, 5+OPEN_MAX, "/dev/ttyp%s", term[i]);
51 snprintf(opentermr, 5+OPEN_MAX, "/dev/ptyp%s", term[i]);
53 /* Open master terminal for writing */
54 if((fd1 = open(opentermw, O_WRONLY)) == -1) continue;
56 /* Open slave terminal for reading */
57 if((fd2 = open(opentermr, O_RDONLY)) == -1) {
58 close(fd1);
59 continue;
62 *child_fd = fd1;
63 *parent_fd = fd2;
64 return;
67 /* If we get here we failed to find a terminal pair */
68 exit(EXIT_FAILURE);
71 int do_child(int terminal) {
72 struct timeval tv;
74 /* Going to sleep for two seconds to allow the parent proc to get ready */
75 tv.tv_sec = 2;
76 tv.tv_usec = 0;
77 select(0, NULL, NULL, NULL, &tv);
79 /* Try to write. Doesn't matter how many bytes we actually send. */
80 (void) write(terminal, SENDSTRING, strlen(SENDSTRING));
81 close(terminal);
83 /* Wait for another second to allow the parent to process incoming data */
84 tv.tv_usec = 1000000;
85 (void) select(0,NULL, NULL, NULL, &tv);
86 exit(0);
89 int do_parent(int child, int terminal) {
90 fd_set fds_read, fds_write, fds_error;
91 int retval;
93 /* Clear bit masks */
94 FD_ZERO(&fds_read); FD_ZERO(&fds_write); FD_ZERO(&fds_error);
95 /* Set read bits */
96 FD_SET(terminal, &fds_read);
97 FD_SET(terminal, &fds_write);
99 /* Test if we can read or write from/to fd. As fd is opened read only we
100 * cannot actually write, so the select should return immediately with fd
101 * set in fds_write, but not in fds_read. Note that the child waits two
102 * seconds before sending data. This gives us the opportunity run this
103 * sub-test as reading from fd is blocking at this point. */
104 retval = select(terminal+1, &fds_read, &fds_write, &fds_error, NULL);
106 if(retval != 1) e(1, "incorrect amount of ready file descriptors");
109 if(FD_ISSET(terminal, &fds_read)) e(2, "read should NOT be set");
110 if(!FD_ISSET(terminal, &fds_write)) e(3, "write should be set");
111 if(FD_ISSET(terminal, &fds_error)) e(4, "error should NOT be set");
113 /* Block until ready; until child wrote stuff */
114 FD_ZERO(&fds_read); FD_ZERO(&fds_write); FD_ZERO(&fds_error);
115 FD_SET(terminal, &fds_read);
116 retval = select(terminal+1, &fds_read, NULL, &fds_error, NULL);
118 if(retval != 1) e(5, "incorrect amount of ready file descriptors");
119 if(!FD_ISSET(terminal, &fds_read)) e(6, "read should be set");
120 if(FD_ISSET(terminal, &fds_error)) e(7, "error should not be set");
123 FD_ZERO(&fds_read); FD_ZERO(&fds_error);
124 FD_SET(terminal, &fds_write);
125 retval = select(terminal+1, NULL, &fds_write, NULL, NULL);
126 /* As it is impossible to write to a read only fd, this select should return
127 * immediately with fd set in fds_write. */
128 if(retval != 1) e(8, "incorrect amount or ready file descriptors");
130 close(terminal);
131 waitpid(child, &retval, 0);
132 exit(errct);
135 int main(int argc, char **argv) {
136 int forkres;
137 int master, slave;
139 /* Get subtest number */
140 if(argc != 2) {
141 printf("Usage: %s subtest_no\n", argv[0]);
142 exit(-1);
143 } else if(sscanf(argv[1], "%d", &subtest) != 1) {
144 printf("Usage: %s subtest_no\n", argv[0]);
145 exit(-1);
148 open_terminal(&master, &slave);
150 forkres = fork();
151 if(forkres == 0) do_child(master);
152 else if(forkres > 0) do_parent(forkres, slave);
153 else { /* Fork failed */
154 perror("Unable to fork");
155 exit(-1);
158 exit(-2); /* We're not supposed to get here. Both do_* routines should exit*/