1 /* test8: pipe() Author: Jan-Mark Wams (jms@cs.vu.nl) */
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")
28 int main(int argc
, char *argv
[])
32 if (argc
== 2) m
= atoi(argv
[1]);
34 for (i
= 0; i
< ITERATIONS
; i
++) {
35 if (m
& 0001) test8a();
36 if (m
& 0002) test8b();
39 return(-1); /* Unreachable */
43 { /* Test fcntl flags. */
44 int tube
[2], t1
[2], t2
[2], t3
[2];
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. */
80 if (pipe(tube
) != 0) e(25);
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. */
116 case -1: printf("Can't fork\n"); break;
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);
124 if (read(tube
[0], buf
, sizeof(buf
)) != 6) e(55);
125 if (strncmp(buf
, "Hello", 6) != 0) e(56);
127 if (stat_loc
!= 0) e(57); /* Alarm? */
129 if (close(tube
[0]) != 0) e(58);
130 if (close(tube
[1]) != 0) e(59);
135 int tube
[2], child2parent
[2], parent2child
[2];
136 int i
, nchild
= 0, nopen
= 3, stat_loc
;
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. */
154 if (nopen
< OPEN_MAX
- 2) {
155 if (pipe(tube
) != -1) e(2);
157 case EMFILE
: /* Errno value is ok. */
159 case ENFILE
: /* No process can open files any more. */
162 printf("Can't fork\n");
166 if (open("/", O_RDONLY
) != -1) e(3);
167 if (errno
!= ENFILE
) e(4);
171 if (stat_loc
!= 0) e(5); /* Alarm? */
174 default: /* Wrong value for errno. */
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) {
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);
206 /* Wait for child to grab OPEN_MAX fds. */
207 if (read(child2parent
[0], &c
, 1) != 1) e(11);
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);
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
);