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