vm: use assert() instead of vm_assert(); remove vm_assert().
[minix.git] / test / t40c.c
blob7cdfeef3e4d2d8a04d29eb982c10da7fe46888d4
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 int do_child(void) {
41 int fd, retval;
42 struct timeval tv;
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",
47 TERMINALW);
48 perror(NULL);
49 printf("Please make sure that %s is not in use while running this test\n",
50 TERMINALW);
51 exit(-1);
54 /* Going to sleep for two seconds to allow the parent proc to get ready */
55 tv.tv_sec = 2;
56 tv.tv_usec = 0;
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));
61 close(fd);
63 /* Wait for another second to allow the parent to process incoming data */
64 tv.tv_usec = 1000000;
65 retval = select(0,NULL, NULL, NULL, &tv);
66 exit(0);
69 int do_parent(int child) {
70 int fd;
71 fd_set fds_read, fds_write, fds_error;
72 int retval;
74 /* Open slave terminal for reading */
75 if((fd = open(TERMINALR, O_RDONLY)) == -1) {
76 printf("Error opening %s for reading\n", TERMINALR);
77 perror(NULL);
78 printf("Please make sure that %s is not in use while running this test.\n",
79 TERMINALR);
80 waitpid(child, &retval, 0);
81 exit(-1);
84 /* Clear bit masks */
85 FD_ZERO(&fds_read); FD_ZERO(&fds_write); FD_ZERO(&fds_error);
86 /* Set read bits */
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");
121 close(fd);
122 waitpid(child, &retval, 0);
123 exit(errct);
126 int main(int argc, char **argv) {
127 int forkres;
129 /* Get subtest number */
130 if(argc != 2) {
131 printf("Usage: %s subtest_no\n", argv[0]);
132 exit(-1);
133 } else if(sscanf(argv[1], "%d", &subtest) != 1) {
134 printf("Usage: %s subtest_no\n", argv[0]);
135 exit(-1);
138 forkres = fork();
139 if(forkres == 0) do_child();
140 else if(forkres > 0) do_parent(forkres);
141 else { /* Fork failed */
142 perror("Unable to fork");
143 exit(-1);
146 exit(-2); /* We're not supposed to get here. Both do_* routines should exit*/