add opendir alias
[minix.git] / test / test8.c
blobcfc20a531d8c98a4e6f4223ca9cd29860eae7bbb
1 /* test8: pipe() Author: Jan-Mark Wams (jms@cs.vu.nl) */
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <sys/wait.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <limits.h>
11 #include <errno.h>
12 #include <time.h>
13 #include <stdio.h>
15 #define MAX_ERROR 4
16 #define ITERATIONS 60
18 #define Fstat(a,b) if (fstat(a,b) != 0) printf("Can't fstat %d\n", a)
19 #define Time(t) if (time(t) == (time_t)-1) printf("Time error\n")
21 #include "common.c"
23 int subtest;
25 void test8a(void);
26 void test8b(void);
28 int main(int argc, char *argv[])
30 int i, m = 0xFFFF;
32 if (argc == 2) m = atoi(argv[1]);
33 start(8);
34 for (i = 0; i < ITERATIONS; i++) {
35 if (m & 0001) test8a();
36 if (m & 0002) test8b();
38 quit();
39 return(-1); /* Unreachable */
42 void test8a()
43 { /* Test fcntl flags. */
44 int tube[2], t1[2], t2[2], t3[2];
45 time_t time1, time2;
46 char buf[128];
47 struct stat st1, st2;
48 int stat_loc, flags;
50 subtest = 1;
52 /* Check if lowest fds are returned. */
53 if (pipe(tube) != 0) e(1);
54 if (tube[0] != 3 && tube[1] != 3) e(2);
55 if (tube[1] != 4 && tube[0] != 4) e(3);
56 if (tube[1] == tube[0]) e(4);
57 if (pipe(t1) != 0) e(5);
58 if (t1[0] != 5 && t1[1] != 5) e(6);
59 if (t1[1] != 6 && t1[0] != 6) e(7);
60 if (t1[1] == t1[0]) e(8);
61 if (close(t1[0]) != 0) e(9);
62 if (close(tube[0]) != 0) e(10);
63 if (pipe(t2) != 0) e(11);
64 if (t2[0] != tube[0] && t2[1] != tube[0]) e(12);
65 if (t2[1] != t1[0] && t2[0] != t1[0]) e(13);
66 if (t2[1] == t2[0]) e(14);
67 if (pipe(t3) != 0) e(15);
68 if (t3[0] != 7 && t3[1] != 7) e(16);
69 if (t3[1] != 8 && t3[0] != 8) e(17);
70 if (t3[1] == t3[0]) e(18);
71 if (close(tube[1]) != 0) e(19);
72 if (close(t1[1]) != 0) e(20);
73 if (close(t2[0]) != 0) e(21);
74 if (close(t2[1]) != 0) e(22);
75 if (close(t3[0]) != 0) e(23);
76 if (close(t3[1]) != 0) e(24);
78 /* All time fields should be marked for update. */
79 Time(&time1);
80 if (pipe(tube) != 0) e(25);
81 Fstat(tube[0], &st1);
82 Fstat(tube[1], &st2);
83 Time(&time2);
84 if (st1.st_atime < time1) e(26);
85 if (st1.st_ctime < time1) e(27);
86 if (st1.st_mtime < time1) e(28);
87 if (st1.st_atime > time2) e(29);
88 if (st1.st_ctime > time2) e(30);
89 if (st1.st_mtime > time2) e(31);
90 if (st2.st_atime < time1) e(32);
91 if (st2.st_ctime < time1) e(33);
92 if (st2.st_mtime < time1) e(34);
93 if (st2.st_atime > time2) e(35);
94 if (st2.st_ctime > time2) e(36);
95 if (st2.st_mtime > time2) e(37);
97 /* Check the file characteristics. */
98 if ((flags = fcntl(tube[0], F_GETFD)) != 0) e(38);
99 if ((flags & FD_CLOEXEC) != 0) e(39);
100 if ((flags = fcntl(tube[0], F_GETFL)) != 0) e(40);
101 if ((flags & O_RDONLY) != O_RDONLY) e(41);
102 if ((flags & O_NONBLOCK) != 0) e(42);
103 if ((flags & O_RDWR) != 0) e(43);
104 if ((flags & O_WRONLY) != 0) e(44);
106 if ((flags = fcntl(tube[1], F_GETFD)) != 0) e(45);
107 if ((flags & FD_CLOEXEC) != 0) e(46);
108 if ((flags = fcntl(tube[1], F_GETFL)) == -1) e(47);
109 if ((flags & O_WRONLY) != O_WRONLY) e(48);
110 if ((flags & O_NONBLOCK) != 0) e(49);
111 if ((flags & O_RDWR) != 0) e(50);
112 if ((flags & O_RDONLY) != 0) e(51);
114 /* Check if we can read and write. */
115 switch (fork()) {
116 case -1: printf("Can't fork\n"); break;
117 case 0:
118 alarm(20);
119 if (close(tube[0]) != 0) e(52);
120 if (write(tube[1], "Hello", 6) != 6) e(53);
121 if (close(tube[1]) != 0) e(54);
122 exit(0);
123 default:
124 if (read(tube[0], buf, sizeof(buf)) != 6) e(55);
125 if (strncmp(buf, "Hello", 6) != 0) e(56);
126 wait(&stat_loc);
127 if (stat_loc != 0) e(57); /* Alarm? */
129 if (close(tube[0]) != 0) e(58);
130 if (close(tube[1]) != 0) e(59);
133 void test8b()
135 int tube[2], child2parent[2], parent2child[2];
136 int i, nchild = 0, nopen = 3, stat_loc;
137 int fd;
138 int forkfailed = 0;
139 char c;
141 subtest = 2;
143 /* Take all the pipes we can get. */
144 while (nopen < OPEN_MAX - 2) {
145 if (pipe(tube) != 0) {
146 /* We have not reached OPEN_MAX yet, so we have ENFILE. */
147 if (errno != ENFILE) e(1);
148 sleep(2); /* Wait for others to (maybe) closefiles. */
149 break;
151 nopen += 2;
154 if (nopen < OPEN_MAX - 2) {
155 if (pipe(tube) != -1) e(2);
156 switch (errno) {
157 case EMFILE: /* Errno value is ok. */
158 break;
159 case ENFILE: /* No process can open files any more. */
160 switch (fork()) {
161 case -1:
162 printf("Can't fork\n");
163 break;
164 case 0:
165 alarm(20);
166 if (open("/", O_RDONLY) != -1) e(3);
167 if (errno != ENFILE) e(4);
168 exit(0);
169 default:
170 wait(&stat_loc);
171 if (stat_loc != 0) e(5); /* Alarm? */
173 break;
174 default: /* Wrong value for errno. */
175 e(6);
179 /* Close all but stdin,out,err. */
180 for (i = 3; i < OPEN_MAX; i++) (void) close(i);
182 /* ENFILE test. Have children each grab OPEN_MAX fds. */
183 if (pipe(child2parent) != 0) e(7);
184 if (pipe(parent2child) != 0) e(8);
185 while (!forkfailed && (fd = open("/", O_RDONLY)) != -1) {
186 close(fd);
187 switch (fork()) {
188 case -1:
189 forkfailed = 1;
190 break;
191 case 0:
192 alarm(60);
194 /* Grab all the fds. */
195 while (pipe(tube) != -1);
196 while (open("/", O_RDONLY) != -1);
198 /* Signal parent OPEN_MAX fds gone. */
199 if (write(child2parent[1], "*", 1) != 1) e(9);
201 /* Wait for parent befor freeing all the fds. */
202 if (read(parent2child[0], &c, 1) != 1) e(10);
203 exit(0);
204 default:
206 /* Wait for child to grab OPEN_MAX fds. */
207 if (read(child2parent[0], &c, 1) != 1) e(11);
208 nchild++;
209 break;
213 if (!forkfailed) {
214 if (pipe(tube) != -1) e(12);
215 if (errno != ENFILE) e(13);
218 /* Signal children to die and wait for it. */
219 while (nchild-- > 0) {
220 if (write(parent2child[1], "*", 1) != 1) e(14);
221 wait(&stat_loc);
222 if (stat_loc != 0) e(15); /* Alarm? */
225 /* Close all but stdin,out,err. */
226 for (i = 3; i < OPEN_MAX; i++) (void) close(i);