add opendir alias
[minix.git] / test / t40f.c
blob239af96b586d2ce1bed05a1e5222861563efc1c2
1 /* t40f.c
3 * Test timing
5 * Select works on regular files, (pseudo) terminal devices, streams-based
6 * files, FIFOs, pipes, and sockets. This test verifies selecting with a time
7 * out set.
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/select.h>
15 #include <sys/wait.h>
16 #include <sys/time.h>
17 #include <time.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <signal.h>
22 #define DO_HANDLEDATA 1
23 #define DO_PAUSE 3
24 #define DO_TIMEOUT 7
25 #define DO_DELTA 0.5
26 #define MAX_ERROR 5
27 #define DELTA(x,y) (x.tv_sec - y.tv_sec) * CLOCKS_PER_SEC \
28 + (x.tv_usec - y.tv_usec) * CLOCKS_PER_SEC / 1000000
30 int errct = 0, subtest = -1, got_signal = 0;
31 int fd_ap[2];
33 void catch_signal(int sig_no) {
34 got_signal = 1;
37 void e(int n, char *s) {
38 printf("Subtest %d, error %d, %s\n", subtest, n, s);
40 if (errct++ > MAX_ERROR) {
41 printf("Too many errors; test aborted\n");
42 exit(errct);
46 float compute_diff(struct timeval start, struct timeval end, float compare) {
47 /* Compute time difference. It is assumed that the value of start <= end. */
48 clock_t delta;
49 int seconds, hundreths;
50 float diff;
52 delta = DELTA(end, start); /* delta is in ticks */
53 seconds = (int) (delta / CLOCKS_PER_SEC);
54 hundreths = (int) (delta * 100 / CLOCKS_PER_SEC) - (seconds * 100);
56 diff = seconds + (hundreths / 100.0);
57 diff -= compare;
58 if(diff < 0) diff *= -1; /* Make diff a positive value */
60 return diff;
63 void do_child(void) {
64 struct timeval tv;
66 /* Let the parent do initial read and write tests from and to the pipe. */
67 tv.tv_sec = DO_PAUSE + DO_PAUSE + DO_PAUSE + 1;
68 tv.tv_usec = 0;
69 (void) select(0, NULL, NULL, NULL, &tv);
71 /* At this point the parent has a pending select with a DO_TIMEOUT timeout.
72 We're going to interrupt by sending a signal */
73 if(kill(getppid(), SIGUSR1) < 0) perror("Failed to send signal");
75 exit(0);
78 void do_parent(int child) {
79 fd_set fds_read;
80 struct timeval tv, start_time, end_time;
81 int retval;
83 /* Install signal handler for SIGUSR1 */
84 signal(SIGUSR1, catch_signal);
86 /* Parent and child share an anonymous pipe. Select for read and wait for the
87 timeout to occur. We wait for DO_PAUSE seconds. Let's see if that's
88 approximately right.*/
89 FD_ZERO(&fds_read);
90 FD_SET(fd_ap[0], &fds_read);
91 tv.tv_sec = DO_PAUSE;
92 tv.tv_usec = 0;
94 (void) gettimeofday(&start_time, NULL); /* Record starting time */
95 retval = select(fd_ap[0]+1, &fds_read, NULL, NULL, &tv);
96 (void) gettimeofday(&end_time, NULL); /* Record ending time */
98 /* Did we time out? */
99 if(retval != 0) e(1, "Should have timed out");
101 /* Approximately right? The standard does not specify how precise the timeout
102 should be. Instead, the granularity is implementation-defined. In this
103 test we assume that the difference should be no more than half a second.*/
104 if(compute_diff(start_time, end_time, DO_PAUSE) > DO_DELTA)
105 e(2, "Time difference too large");
107 /* Let's wait for another DO_PAUSE seconds, expressed as microseconds */
108 FD_ZERO(&fds_read);
109 FD_SET(fd_ap[0], &fds_read);
110 tv.tv_sec = 0;
111 tv.tv_usec = DO_PAUSE * 1000000L;
113 (void) gettimeofday(&start_time, NULL); /* Record starting time */
114 retval = select(fd_ap[0]+1, &fds_read, NULL, NULL, &tv);
115 (void) gettimeofday(&end_time, NULL); /* Record ending time */
117 if(retval != 0) e(3, "Should have timed out");
118 if(compute_diff(start_time, end_time, DO_PAUSE) > DO_DELTA)
119 e(4, "Time difference too large");
121 /* Let's wait for another DO_PAUSE seconds, expressed in seconds and micro
122 seconds. */
123 FD_ZERO(&fds_read);
124 FD_SET(fd_ap[0], &fds_read);
125 tv.tv_sec = DO_PAUSE - 1;
126 tv.tv_usec = (DO_PAUSE - tv.tv_sec) * 1000000L;
128 (void) gettimeofday(&start_time, NULL); /* Record starting time */
129 retval = select(fd_ap[0]+1, &fds_read, NULL, NULL, &tv);
130 (void) gettimeofday(&end_time, NULL); /* Record ending time */
132 if(retval != 0) e(5, "Should have timed out");
133 if(compute_diff(start_time, end_time, DO_PAUSE) > DO_DELTA)
134 e(6, "Time difference too large");
136 /* Finally, we test if our timeout is interrupted by a signal */
137 FD_ZERO(&fds_read);
138 FD_SET(fd_ap[0], &fds_read);
139 tv.tv_sec = DO_TIMEOUT;
140 tv.tv_usec = 0;
142 (void) gettimeofday(&start_time, NULL); /* Record starting time */
143 retval = select(fd_ap[0]+1, &fds_read, NULL, NULL, &tv);
144 (void) gettimeofday(&end_time, NULL); /* Record ending time */
146 if(retval != -1) e(7, "Should have been interrupted");
147 if(compute_diff(start_time, end_time, DO_TIMEOUT) < DO_DELTA)
148 e(8, "Failed to get interrupted by a signal");
150 if(!got_signal) e(9, "Failed to get interrupted by a signal");
152 waitpid(child, &retval, 0);
153 exit(errct);
156 int main(int argc, char **argv) {
157 int forkres;
159 /* Get subtest number */
160 if(argc != 2) {
161 printf("Usage: %s subtest_no\n", argv[0]);
162 exit(-2);
163 } else if(sscanf(argv[1], "%d", &subtest) != 1) {
164 printf("Usage: %s subtest_no\n", argv[0]);
165 exit(-2);
168 /* Set up anonymous pipe */
169 if(pipe(fd_ap) < 0) {
170 perror("Could not create anonymous pipe");
171 exit(-1);
174 forkres = fork();
175 if(forkres == 0) do_child();
176 else if(forkres > 0) do_parent(forkres);
177 else { /* Fork failed */
178 perror("Unable to fork");
179 exit(-1);
182 exit(-2); /* We're not supposed to get here. Both do_* routines should exit*/