t40c term[] count fix
[minix.git] / test / test38.c
blobff1b558ce077b58bf28bf89819fa4a4d7d6d0e38
1 /* Many of the tests require 1.6.n, n > 16, so we may as well assume that
2 * POSIX signals are implemented.
3 */
4 #define SIGACTION
6 /* test38: read(), write() Author: Jan-Mark Wams (jms@cs.vu.nl) */
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/wait.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <fcntl.h>
15 #include <limits.h>
16 #include <errno.h>
17 #include <time.h>
18 #include <signal.h>
19 #include <stdio.h>
21 #define MAX_ERROR 4
22 #define ITERATIONS 3
23 #define BUF_SIZE 1024
25 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
26 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
27 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
29 #include "common.c"
31 int superuser;
32 int signumber = 0;
34 void test38a(void);
35 void test38b(void);
36 void test38c(void);
37 void setsignumber(int _signumber);
39 int main(int argc, char *argv[])
41 int i, m = 0xFFFF;
43 sync();
44 start(38);
46 if (argc == 2) m = atoi(argv[1]);
47 superuser = (geteuid() == 0);
48 umask(0000);
50 for (i = 0; i < ITERATIONS; i++) {
51 if (m & 0001) test38a();
52 if (m & 0002) test38b();
53 if (m & 0004) test38c();
55 quit();
57 return(-1); /* Unreachable */
60 void test38a()
61 { /* Try normal operation. */
62 int fd1;
63 struct stat st1, st2;
64 time_t time1;
65 char buf[BUF_SIZE];
66 int stat_loc;
67 int i, j;
68 int tube[2];
70 subtest = 1;
71 System("rm -rf ../DIR_38/*");
73 /* Let's open bar. */
74 if ((fd1 = open("bar", O_RDWR | O_CREAT, 0777)) != 3) e(1);
75 Stat("bar", &st1);
77 /* Writing nothing should not affect the file at all. */
78 if (write(fd1, "", 0) != 0) e(2);
79 Stat("bar", &st2);
80 if (st1.st_uid != st2.st_uid) e(3);
81 if (st1.st_gid != st2.st_gid) e(4); /* should be same */
82 if (st1.st_mode != st2.st_mode) e(5);
83 if (st1.st_size != st2.st_size) e(6);
84 if (st1.st_nlink != st2.st_nlink) e(7);
85 if (st1.st_mtime != st2.st_mtime) e(8);
86 if (st1.st_ctime != st2.st_ctime) e(9);
87 if (st1.st_atime != st2.st_atime) e(10);
89 /* A write should update some status fields. */
90 time(&time1);
91 while (time1 >= time((time_t *)0))
93 if (write(fd1, "foo", 4) != 4) e(11);
94 Stat("bar", &st2);
95 if (st1.st_mode != st2.st_mode) e(12);
96 if (st1.st_size >= st2.st_size) e(13);
97 if ((off_t) 4 != st2.st_size) e(14);
98 if (st1.st_nlink != st2.st_nlink) e(15);
99 if (st1.st_mtime >= st2.st_mtime) e(16);
100 if (st1.st_ctime >= st2.st_ctime) e(17);
101 if (st1.st_atime != st2.st_atime) e(18);
103 /* Lseeks should not change the file status. */
104 if (lseek(fd1, (off_t) - 2, SEEK_END) != 2) e(19);
105 Stat("bar", &st1);
106 if (st1.st_mode != st2.st_mode) e(20);
107 if (st1.st_size != st2.st_size) e(21);
108 if (st1.st_nlink != st2.st_nlink) e(22);
109 if (st1.st_mtime != st2.st_mtime) e(23);
110 if (st1.st_ctime != st2.st_ctime) e(24);
111 if (st1.st_atime != st2.st_atime) e(25);
113 /* Writing should start at the current (2) position. */
114 if (write(fd1, "foo", 4) != 4) e(26);
115 Stat("bar", &st2);
116 if (st1.st_mode != st2.st_mode) e(27);
117 if (st1.st_size >= st2.st_size) e(28);
118 if ((off_t) 6 != st2.st_size) e(29);
119 if (st1.st_nlink != st2.st_nlink) e(30);
120 if (st1.st_mtime > st2.st_mtime) e(31);
121 if (st1.st_ctime > st2.st_ctime) e(32);
122 if (st1.st_atime != st2.st_atime) e(33);
124 /* A read of zero bytes should not affect anything. */
125 if (read(fd1, buf, 0) != 0) e(34);
126 Stat("bar", &st1);
127 if (st1.st_uid != st2.st_uid) e(35);
128 if (st1.st_gid != st2.st_gid) e(36); /* should be same */
129 if (st1.st_mode != st2.st_mode) e(37);
130 if (st1.st_size != st2.st_size) e(38);
131 if (st1.st_nlink != st2.st_nlink) e(39);
132 if (st1.st_mtime != st2.st_mtime) e(40);
133 if (st1.st_ctime != st2.st_ctime) e(41);
134 if (st1.st_atime != st2.st_atime) e(42);
136 /* The file now should contain ``fofoo\0'' Let's check that. */
137 if (lseek(fd1, (off_t) 0, SEEK_SET) != 0) e(43);
138 if (read(fd1, buf, BUF_SIZE) != 6) e(44);
139 if (strcmp(buf, "fofoo") != 0) e(45);
141 /* Only the Access Time should be updated. */
142 Stat("bar", &st2);
143 if (st1.st_mtime != st2.st_mtime) e(46);
144 if (st1.st_ctime != st2.st_ctime) e(47);
145 if (st1.st_atime >= st2.st_atime) e(48);
147 /* A read of zero bytes should do nothing even at the end of the file. */
148 time(&time1);
149 while (time1 >= time((time_t *)0))
151 if (read(fd1, buf, 0) != 0) e(49);
152 Stat("bar", &st1);
153 if (st1.st_size != st2.st_size) e(50);
154 if (st1.st_mtime != st2.st_mtime) e(51);
155 if (st1.st_ctime != st2.st_ctime) e(52);
156 if (st1.st_atime != st2.st_atime) e(53);
158 /* Reading should be done from the current offset. */
159 if (read(fd1, buf, BUF_SIZE) != 0) e(54);
160 if (lseek(fd1, (off_t) 2, SEEK_SET) != 2) e(55);
161 if (read(fd1, buf, BUF_SIZE) != 4) e(56);
162 if (strcmp(buf, "foo") != 0) e(57);
164 /* Reading should effect the current file position. */
165 if (lseek(fd1, (off_t) 2, SEEK_SET) != 2) e(58);
166 if (read(fd1, buf, 1) != 1) e(59);
167 if (*buf != 'f') e(60);
168 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 3) e(61);
169 if (read(fd1, buf, 1) != 1) e(62);
170 if (*buf != 'o') e(63);
171 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 4) e(64);
172 if (read(fd1, buf, 1) != 1) e(65);
173 if (*buf != 'o') e(66);
174 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 5) e(67);
175 if (read(fd1, buf, 1) != 1) e(68);
176 if (*buf != '\0') e(69);
177 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 6) e(70);
179 /* Read's at EOF should return 0. */
180 if (read(fd1, buf, BUF_SIZE) != 0) e(71);
181 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 6) e(72);
182 if (read(fd1, buf, BUF_SIZE) != 0) e(73);
183 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 6) e(74);
184 if (read(fd1, buf, BUF_SIZE) != 0) e(75);
185 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 6) e(76);
186 if (read(fd1, buf, BUF_SIZE) != 0) e(77);
187 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 6) e(78);
188 if (read(fd1, buf, BUF_SIZE) != 0) e(79);
189 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 6) e(80);
191 /* Writing should not always change the file size. */
192 if (lseek(fd1, (off_t) 2, SEEK_SET) != 2) e(81);
193 if (write(fd1, "ba", 2) != 2) e(82);
194 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 4) e(83);
195 Stat("bar", &st1);
196 if (st1.st_size != 6) e(84);
198 /* Kill the \0 at the end. */
199 if (lseek(fd1, (off_t) 5, SEEK_SET) != 5) e(85);
200 if (write(fd1, "x", 1) != 1) e(86);
202 /* And close the bar. */
203 if (close(fd1) != 0) e(87);
205 /* Try some stuff with O_APPEND. Bar contains ``fobaox'' */
206 if ((fd1 = open("bar", O_RDWR | O_APPEND)) != 3) e(88);
208 /* No matter what the file position is. Writes should append. */
209 if (lseek(fd1, (off_t) 2, SEEK_SET) != 2) e(89);
210 if (write(fd1, "y", 1) != 1) e(90);
211 Stat("bar", &st1);
212 if (st1.st_size != (off_t) 7) e(91);
213 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 7) e(92);
214 if (lseek(fd1, (off_t) 2, SEEK_SET) != 2) e(93);
215 if (write(fd1, "z", 2) != 2) e(94);
217 /* The file should contain ``fobaoxyz\0'' == 9 chars long. */
218 Stat("bar", &st1);
219 if (st1.st_size != (off_t) 9) e(95);
220 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 9) e(96);
222 /* Reading on a O_APPEND flag should be from the current offset. */
223 if (lseek(fd1, (off_t) 0, SEEK_SET) != 0) e(97);
224 if (read(fd1, buf, BUF_SIZE) != 9) e(98);
225 if (strcmp(buf, "fobaoxyz") != 0) e(99);
226 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 9) e(100);
228 if (close(fd1) != 0) e(101);
230 /* Let's test fifo writes. First blocking. */
231 if (mkfifo("fifo", 0777) != 0) e(102);
233 /* Read from fifo but no writer. */
234 System("rm -rf /tmp/sema.38a");
235 switch (fork()) {
236 case -1: printf("Can't fork\n"); break;
238 case 0:
239 alarm(20);
240 if ((fd1 = open("fifo", O_RDONLY)) != 3) e(103);
241 system("> /tmp/sema.38a");
242 system("while test -f /tmp/sema.38a; do sleep 1; done");
243 errno =0;
244 if (read(fd1, buf, BUF_SIZE) != 0) e(104);
245 if (read(fd1, buf, BUF_SIZE) != 0) e(105);
246 if (read(fd1, buf, BUF_SIZE) != 0) e(106);
247 if (close(fd1) != 0) e(107);
248 exit(0);
250 default:
251 if ((fd1 = open("fifo", O_WRONLY)) != 3) e(108);
252 while (stat("/tmp/sema.38a", &st1) != 0) sleep(1);
253 if (close(fd1) != 0) e(109);
254 unlink("/tmp/sema.38a");
255 if (wait(&stat_loc) == -1) e(110);
256 if (stat_loc != 0) e(111); /* Alarm? */
259 /* Read from fifo should wait for writer. */
260 switch (fork()) {
261 case -1: printf("Can't fork\n"); break;
263 case 0:
264 alarm(20);
265 if ((fd1 = open("fifo", O_RDONLY)) != 3) e(112);
266 if (read(fd1, buf, BUF_SIZE) != 10) e(113);
267 if (strcmp(buf, "Hi reader") != 0) e(114);
268 if (close(fd1) != 0) e(115);
269 exit(0);
271 default:
272 if ((fd1 = open("fifo", O_WRONLY)) != 3) e(116);
273 sleep(1);
274 if (write(fd1, "Hi reader", 10) != 10) e(117);
275 if (close(fd1) != 0) e(118);
276 if (wait(&stat_loc) == -1) e(119);
277 if (stat_loc != 0) e(120); /* Alarm? */
280 #if DEAD_CODE
281 /* Does this test test what it is supposed to test??? */
283 /* Read from fifo should wait for all writers to close. */
284 switch (fork()) {
285 case -1: printf("Can't fork\n"); break;
287 case 0:
288 alarm(60);
289 switch (fork()) {
290 case -1: printf("Can't fork\n"); break;
291 case 0:
292 alarm(20);
293 if ((fd1 = open("fifo", O_WRONLY)) != 3) e(121);
294 printf("C2 did open\n");
295 if (close(fd1) != 0) e(122);
296 printf("C2 did close\n");
297 exit(0);
298 default:
299 printf("C1 scheduled\n");
300 if ((fd1 = open("fifo", O_WRONLY)) != 3) e(123);
301 printf("C1 did open\n");
302 sleep(2);
303 if (close(fd1) != 0) e(124);
304 printf("C1 did close\n");
305 sleep(1);
306 if (wait(&stat_loc) == -1) e(125);
307 if (stat_loc != 0) e(126); /* Alarm? */
309 exit(stat_loc);
311 default: {
312 int wait_status;
313 printf("Parent running\n");
314 sleep(1); /* open in childs first */
315 if ((fd1 = open("fifo", O_RDONLY)) != 3) e(127);
316 if (read(fd1, buf, BUF_SIZE) != 0) e(128);
317 if (close(fd1) != 0) e(129);
318 printf("Parent closed\n");
319 if ((wait_status=wait(&stat_loc)) == -1) e(130);
321 printf("wait_status %d, stat_loc %d:", wait_status, stat_loc);
322 if (WIFSIGNALED(stat_loc)) {
323 printf(" killed, signal number %d\n", WTERMSIG(stat_loc));
325 else if (WIFEXITED(stat_loc)) {
326 printf(" normal exit, status %d\n", WEXITSTATUS(stat_loc));
329 if (stat_loc != 0) e(131); /* Alarm? */
332 #endif
334 /* PIPE_BUF has to have a nice value. */
335 if (PIPE_BUF < 5) e(132);
336 if (BUF_SIZE < 1000) e(133);
338 /* Writes of blocks smaller than PIPE_BUF should be atomic. */
339 System("rm -rf /tmp/sema.38b;> /tmp/sema.38b");
340 switch (fork()) {
341 case -1: printf("Can't fork\n"); break;
343 case 0:
344 alarm(20);
345 switch (fork()) {
346 case -1: printf("Can't fork\n"); break;
348 case 0:
349 alarm(20);
350 if ((fd1 = open("fifo", O_WRONLY)) != 3) e(134);
351 for (i = 0; i < 100; i++) write(fd1, "1234 ", 5);
352 system("while test -f /tmp/sema.38b; do sleep 1; done");
353 if (close(fd1) != 0) e(135);
354 exit(0);
356 default:
357 if ((fd1 = open("fifo", O_WRONLY)) != 3) e(136);
358 for (i = 0; i < 100; i++) write(fd1, "1234 ", 5);
359 while (stat("/tmp/sema.38b", &st1) == 0) sleep(1);
360 if (close(fd1) != 0) e(137);
361 if (wait(&stat_loc) == -1) e(138);
362 if (stat_loc != 0) e(139); /* Alarm? */
364 exit(stat_loc);
366 default:
367 if ((fd1 = open("fifo", O_RDONLY)) != 3) e(140);
368 i = 0;
369 memset(buf, '\0', BUF_SIZE);
371 /* Read buffer full or till EOF. */
372 do {
373 j = read(fd1, buf + i, BUF_SIZE - i);
374 if (j > 0) {
375 if (j % 5 != 0) e(141);
376 i += j;
378 } while (j > 0 && i < 1000);
380 /* Signal the children to close write ends. This should not be */
381 /* Necessary. But due to a bug in 1.16.6 this is necessary. */
382 unlink("/tmp/sema.38b");
383 if (j < 0) e(142);
384 if (i != 1000) e(143);
385 if (wait(&stat_loc) == -1) e(144);
386 if (stat_loc != 0) e(145); /* Alarm? */
388 /* Check 200 times 1234. */
389 for (i = 0; i < 200; i++)
390 if (strncmp(buf + (i * 5), "1234 ", 5) != 0) break;
391 if (i != 200) e(146);
392 if (buf[1000] != '\0') e(147);
393 if (buf[1005] != '\0') e(148);
394 if (buf[1010] != '\0') e(149);
395 if (read(fd1, buf, BUF_SIZE) != 0) e(150);
396 if (close(fd1) != 0) e(151);
399 /* Read from pipe should wait for writer. */
400 if (pipe(tube) != 0) e(152);
401 switch (fork()) {
402 case -1: printf("Can't fork\n"); break;
403 case 0:
404 alarm(20);
405 if (close(tube[1]) != 0) e(153);
406 if (read(tube[0], buf, BUF_SIZE) != 10) e(154);
407 if (strcmp(buf, "Hi reader") != 0) e(155);
408 if (close(tube[0]) != 0) e(156);
409 exit(0);
410 default:
411 if (close(tube[0]) != 0) e(157);
412 sleep(1);
413 if (write(tube[1], "Hi reader", 10) != 10) e(158);
414 if (close(tube[1]) != 0) e(159);
415 if (wait(&stat_loc) == -1) e(160);
416 if (stat_loc != 0) e(161); /* Alarm? */
419 /* Read from pipe should wait for all writers to close. */
420 if (pipe(tube) != 0) e(162);
421 switch (fork()) {
422 case -1: printf("Can't fork\n"); break;
423 case 0:
424 alarm(20);
425 if (close(tube[0]) != 0) e(163);
426 switch (fork()) {
427 case -1: printf("Can't fork\n"); break;
428 case 0:
429 alarm(20);
430 if (close(tube[1]) != 0) e(164);
431 exit(0);
432 default:
433 sleep(1);
434 if (close(tube[1]) != 0) e(165);
435 if (wait(&stat_loc) == -1) e(166);
436 if (stat_loc != 0) e(167); /* Alarm? */
438 exit(stat_loc);
439 default:
440 if (close(tube[1]) != 0) e(168);
441 if (read(tube[0], buf, BUF_SIZE) != 0) e(169);
442 if (close(tube[0]) != 0) e(170);
443 if (wait(&stat_loc) == -1) e(171);
444 if (stat_loc != 0) e(172); /* Alarm? */
447 /* Writes of blocks smaller than PIPE_BUF should be atomic. */
448 System("rm -rf /tmp/sema.38c;> /tmp/sema.38c");
449 if (pipe(tube) != 0) e(173);
450 switch (fork()) {
451 case -1: printf("Can't fork\n"); break;
452 case 0:
453 alarm(20);
454 if (close(tube[0]) != 0) e(174);
455 switch (fork()) {
456 case -1: printf("Can't fork\n"); break;
457 case 0:
458 alarm(20);
459 for (i = 0; i < 100; i++) write(tube[1], "1234 ", 5);
460 system("while test -f /tmp/sema.38c; do sleep 1; done");
461 if (close(tube[1]) != 0) e(175);
462 exit(0);
463 default:
464 for (i = 0; i < 100; i++) write(tube[1], "1234 ", 5);
465 while (stat("/tmp/sema.38c", &st1) == 0) sleep(1);
466 if (close(tube[1]) != 0) e(176);
467 if (wait(&stat_loc) == -1) e(177);
468 if (stat_loc != 0) e(178); /* Alarm? */
470 exit(stat_loc);
471 default:
472 i = 0;
473 if (close(tube[1]) != 0) e(179);
474 memset(buf, '\0', BUF_SIZE);
475 do {
476 j = read(tube[0], buf + i, BUF_SIZE - i);
477 if (j > 0) {
478 if (j % 5 != 0) e(180);
479 i += j;
480 } else
481 break; /* EOF seen. */
482 } while (i < 1000);
483 unlink("/tmp/sema.38c");
484 if (j < 0) e(181);
485 if (i != 1000) e(182);
486 if (close(tube[0]) != 0) e(183);
487 if (wait(&stat_loc) == -1) e(184);
488 if (stat_loc != 0) e(185); /* Alarm? */
490 /* Check 200 times 1234. */
491 for (i = 0; i < 200; i++)
492 if (strncmp(buf + (i * 5), "1234 ", 5) != 0) break;
493 if (i != 200) e(186);
497 void test38b()
499 int i, fd, stat_loc;
500 char buf[BUF_SIZE];
501 char buf2[BUF_SIZE];
502 struct stat st;
504 subtest = 2;
505 System("rm -rf ../DIR_38/*");
507 /* Lets try sequential writes. */
508 system("rm -rf /tmp/sema.38d");
509 System("> testing");
510 switch (fork()) {
511 case -1: printf("Can't fork\n"); break;
512 case 0:
513 alarm(20);
514 if ((fd = open("testing", O_WRONLY | O_APPEND)) != 3) e(1);
515 if (write(fd, "one ", 4) != 4) e(2);
516 if (close(fd) != 0) e(3);
517 system("> /tmp/sema.38d");
518 system("while test -f /tmp/sema.38d; do sleep 1; done");
519 if ((fd = open("testing", O_WRONLY | O_APPEND)) != 3) e(4);
520 if (write(fd, "three ", 6) != 6) e(5);
521 if (close(fd) != 0) e(6);
522 system("> /tmp/sema.38d");
523 exit(0);
524 default:
525 while (stat("/tmp/sema.38d", &st) != 0) sleep(1);
526 if ((fd = open("testing", O_WRONLY | O_APPEND)) != 3) e(7);
527 if (write(fd, "two ", 4) != 4) e(8);
528 if (close(fd) != 0) e(9);
529 unlink("/tmp/sema.38d");
530 while (stat("/tmp/sema.38d", &st) != 0) sleep(1);
531 if ((fd = open("testing", O_WRONLY | O_APPEND)) != 3) e(10);
532 if (write(fd, "four", 5) != 5) e(11);
533 if (close(fd) != 0) e(12);
534 if (wait(&stat_loc) == -1) e(13);
535 if (stat_loc != 0) e(14); /* The alarm went off? */
536 unlink("/tmp/sema.38d");
538 if ((fd = open("testing", O_RDONLY)) != 3) e(15);
539 if (read(fd, buf, BUF_SIZE) != 19) e(16);
540 if (strcmp(buf, "one two three four") != 0) e(17);
541 if (close(fd) != 0) e(18);
543 /* Non written bytes in regular files should be zero. */
544 memset(buf2, '\0', BUF_SIZE);
545 if ((fd = open("bigfile", O_RDWR | O_CREAT, 0644)) != 3) e(19);
546 if (lseek(fd, (off_t) 102400, SEEK_SET) != (off_t) 102400L) e(20);
547 if (read(fd, buf, BUF_SIZE) != 0) e(21);
548 if (write(fd, ".", 1) != 1) e(22);
549 Stat("bigfile", &st);
550 if (st.st_size != (off_t) 102401) e(23);
551 if (lseek(fd, (off_t) 0, SEEK_SET) != 0) e(24);
552 for (i = 0; i < 102400 / BUF_SIZE; i++) {
553 if (read(fd, buf, BUF_SIZE) != BUF_SIZE) e(25);
554 if (memcmp(buf, buf2, BUF_SIZE) != 0) e(26);
556 if (close(fd) != 0) e(27);
559 void test38c()
560 { /* Test correct error behavior. */
561 char buf[BUF_SIZE];
562 int fd, tube[2], stat_loc;
563 struct stat st;
564 pid_t pid;
565 #ifdef SIGACTION
566 struct sigaction act, oact;
567 #else
568 void (*oldfunc) (int);
569 #endif
571 subtest = 3;
572 System("rm -rf ../DIR_38/*");
574 /* To test if writing processes on closed pipes are signumbered. */
575 #ifdef SIGACTION
576 act.sa_handler = setsignumber;
577 sigemptyset(&act.sa_mask);
578 act.sa_flags = 0;
579 if (sigaction(SIGPIPE, &act, &oact) != 0) e(1);
580 #else
581 oldfunc = signal(SIGPIPE, setsignumber);
582 #endif
584 /* Non valid file descriptors should be an error. */
585 for (fd = -111; fd < 0; fd++) {
586 errno = 0;
587 if (read(fd, buf, BUF_SIZE) != -1) e(2);
588 if (errno != EBADF) e(3);
590 for (fd = 3; fd < 111; fd++) {
591 errno = 0;
592 if (read(fd, buf, BUF_SIZE) != -1) e(4);
593 if (errno != EBADF) e(5);
595 for (fd = -111; fd < 0; fd++) {
596 errno = 0;
597 if (write(fd, buf, BUF_SIZE) != -1) e(6);
598 if (errno != EBADF) e(7);
600 for (fd = 3; fd < 111; fd++) {
601 errno = 0;
602 if (write(fd, buf, BUF_SIZE) != -1) e(8);
603 if (errno != EBADF) e(9);
606 /* Writing a pipe with no readers should trigger SIGPIPE. */
607 if (pipe(tube) != 0) e(10);
608 close(tube[0]);
609 switch (fork()) {
610 case -1: printf("Can't fork\n"); break;
611 case 0:
612 alarm(20);
613 signumber = 0;
614 if (write(tube[1], buf, BUF_SIZE) != -1) e(11);
615 if (errno != EPIPE) e(12);
616 if (signumber != SIGPIPE) e(13);
617 if (close(tube[1]) != 0) e(14);
618 exit(0);
619 default:
620 close(tube[1]);
621 if (wait(&stat_loc) == -1) e(15);
622 if (stat_loc != 0) e(16); /* Alarm? */
625 /* Writing a fifo with no readers should trigger SIGPIPE. */
626 System("> /tmp/sema.38e");
627 if (mkfifo("fifo", 0666) != 0) e(17);
628 switch (fork()) {
629 case -1: printf("Can't fork\n"); break;
630 case 0:
631 alarm(20);
632 if ((fd = open("fifo", O_WRONLY)) != 3) e(18);
633 system("while test -f /tmp/sema.38e; do sleep 1; done");
634 signumber = 0;
635 if (write(fd, buf, BUF_SIZE) != -1) e(19);
636 if (errno != EPIPE) e(20);
637 if (signumber != SIGPIPE) e(21);
638 if (close(fd) != 0) e(22);
639 exit(0);
640 default:
641 if ((fd = open("fifo", O_RDONLY)) != 3) e(23);
642 if (close(fd) != 0) e(24);
643 unlink("/tmp/sema.38e");
644 if (wait(&stat_loc) == -1) e(25);
645 if (stat_loc != 0) e(26); /* Alarm? */
648 #ifdef SIGACTION
649 /* Restore normal (re)action to SIGPIPE. */
650 if (sigaction(SIGPIPE, &oact, NULL) != 0) e(27);
651 #else
652 signal(SIGPIPE, oldfunc);
653 #endif
655 /* Read from fifo should return -1 and set errno to EAGAIN. */
656 System("rm -rf /tmp/sema.38[fgh]");
657 switch (fork()) {
658 case -1: printf("Can't fork\n"); break;
659 case 0:
660 alarm(20);
661 system("while test ! -f /tmp/sema.38f; do sleep 1; done");
662 System("rm -rf /tmp/sema.38f");
663 if ((fd = open("fifo", O_WRONLY | O_NONBLOCK)) != 3) e(28);
664 close(creat("/tmp/sema.38g", 0666));
665 system("while test ! -f /tmp/sema.38h; do sleep 1; done");
666 if (close(fd) != 0) e(38);
667 System("rm -rf /tmp/sema.38h");
668 exit(0);
669 default:
670 if ((fd = open("fifo", O_RDONLY | O_NONBLOCK)) != 3) e(30);
671 close(creat("/tmp/sema.38f", 0666));
672 system("while test ! -f /tmp/sema.38g; do sleep 1; done");
673 System("rm -rf /tmp/sema.38g");
674 if (read(fd, buf, BUF_SIZE) != -1) e(31);
675 if (errno != EAGAIN) e(32);
676 if (read(fd, buf, BUF_SIZE) != -1) e(33);
677 if (errno != EAGAIN) e(34);
678 if (read(fd, buf, BUF_SIZE) != -1) e(35);
679 if (errno != EAGAIN) e(36);
680 close(creat("/tmp/sema.38h", 0666));
681 while (stat("/tmp/sema.38h", &st) == 0) sleep(1);
682 if (read(fd, buf, BUF_SIZE) != 0) e(37);
683 if (close(fd) != 0) e(38);
684 if (wait(&stat_loc) == -1) e(39);
685 if (stat_loc != 0) e(40); /* Alarm? */
687 System("rm -rf fifo");
689 /* If a read is interrupted by a SIGNAL. */
690 if (pipe(tube) != 0) e(41);
691 switch (pid = fork()) {
692 case -1: printf("Can't fork\n"); break;
693 case 0:
694 alarm(20);
695 #ifdef SIGACTION
696 act.sa_handler = setsignumber;
697 sigemptyset(&act.sa_mask);
698 act.sa_flags = 0;
699 if (sigaction(SIGUSR1, &act, &oact) != 0) e(42);
700 #else
701 oldfunc = signal(SIGUSR1, setsignumber);
702 #endif
703 if (read(tube[0], buf, BUF_SIZE) != -1) e(43);
704 if (errno != EINTR) e(44);
705 if (signumber != SIGUSR1) e(45);
706 #ifdef SIGACTION
707 /* Restore normal (re)action to SIGPIPE. */
708 if (sigaction(SIGUSR1, &oact, NULL) != 0) e(46);
709 #else
710 signal(SIGUSR1, oldfunc);
711 #endif
712 close(tube[0]);
713 close(tube[1]);
714 exit(0);
715 default:
716 /* The sleep 1 should give the child time to start the read. */
717 sleep(1);
718 close(tube[0]);
719 kill(pid, SIGUSR1);
720 wait(&stat_loc);
721 if (stat_loc != 0) e(47); /* Alarm? */
722 close(tube[1]);
726 void setsignumber(signum)
727 int signum;
729 signumber = signum;