1 /* test8: pipe() Author: Jan-Mark Wams (jms@cs.vu.nl) */
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")
29 int main(int argc
, char *argv
[])
33 if (argc
== 2) m
= atoi(argv
[1]);
35 for (i
= 0; i
< ITERATIONS
; i
++) {
36 if (m
& 0001) test8a();
37 if (m
& 0002) test8b();
40 return(-1); /* Unreachable */
44 { /* Test fcntl flags. */
45 int tube
[2], t1
[2], t2
[2], t3
[2];
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. */
81 if (pipe(tube
) != 0) e(25);
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. */
117 case -1: printf("Can't fork\n"); break;
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);
125 if (read(tube
[0], buf
, sizeof(buf
)) != 6) e(55);
126 if (strncmp(buf
, "Hello", 6) != 0) e(56);
128 if (stat_loc
!= 0) e(57); /* Alarm? */
130 if (close(tube
[0]) != 0) e(58);
131 if (close(tube
[1]) != 0) e(59);
136 int tube
[2], child2parent
[2], parent2child
[2];
137 int i
, nchild
= 0, nopen
= 3, stat_loc
;
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. */
155 if (nopen
< OPEN_MAX
- 2) {
156 if (pipe(tube
) != -1) e(2);
158 case EMFILE
: /* Errno value is ok. */
160 case ENFILE
: /* No process can open files any more. */
163 printf("Can't fork\n");
167 if (open("/", O_RDONLY
) != -1) e(3);
168 if (errno
!= ENFILE
) e(4);
172 if (stat_loc
!= 0) e(5); /* Alarm? */
175 default: /* Wrong value for errno. */
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) {
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);
207 /* Wait for child to grab OPEN_MAX fds. */
208 if (read(child2parent
[0], &c
, 1) != 1) e(11);
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);
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
);