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