1 /* Many of the tests require 1.6.n, n > 16, so we may as well assume that
2 * POSIX signals are implemented.
6 /* test38: read(), write() Author: Jan-Mark Wams (jms@cs.vu.nl) */
27 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
28 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
29 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
38 void setsignumber(int _signumber
);
40 int main(int argc
, char *argv
[])
47 if (argc
== 2) m
= atoi(argv
[1]);
48 superuser
= (geteuid() == 0);
51 for (i
= 0; i
< ITERATIONS
; i
++) {
52 if (m
& 0001) test38a();
53 if (m
& 0002) test38b();
54 if (m
& 0004) test38c();
58 return(-1); /* Unreachable */
62 { /* Try normal operation. */
72 System("rm -rf ../DIR_38/*");
75 if ((fd1
= open("bar", O_RDWR
| O_CREAT
, 0777)) != 3) e(1);
78 /* Writing nothing should not affect the file at all. */
79 if (write(fd1
, "", 0) != 0) e(2);
81 if (st1
.st_uid
!= st2
.st_uid
) e(3);
82 if (st1
.st_gid
!= st2
.st_gid
) e(4); /* should be same */
83 if (st1
.st_mode
!= st2
.st_mode
) e(5);
84 if (st1
.st_size
!= st2
.st_size
) e(6);
85 if (st1
.st_nlink
!= st2
.st_nlink
) e(7);
86 if (st1
.st_mtime
!= st2
.st_mtime
) e(8);
87 if (st1
.st_ctime
!= st2
.st_ctime
) e(9);
88 if (st1
.st_atime
!= st2
.st_atime
) e(10);
90 /* A write should update some status fields. */
92 while (time1
>= time((time_t *)0))
94 if (write(fd1
, "foo", 4) != 4) e(11);
96 if (st1
.st_mode
!= st2
.st_mode
) e(12);
97 if (st1
.st_size
>= st2
.st_size
) e(13);
98 if ((off_t
) 4 != st2
.st_size
) e(14);
99 if (st1
.st_nlink
!= st2
.st_nlink
) e(15);
100 if (st1
.st_mtime
>= st2
.st_mtime
) e(16);
101 if (st1
.st_ctime
>= st2
.st_ctime
) e(17);
102 if (st1
.st_atime
!= st2
.st_atime
) e(18);
104 /* Lseeks should not change the file status. */
105 if (lseek(fd1
, (off_t
) - 2, SEEK_END
) != 2) e(19);
107 if (st1
.st_mode
!= st2
.st_mode
) e(20);
108 if (st1
.st_size
!= st2
.st_size
) e(21);
109 if (st1
.st_nlink
!= st2
.st_nlink
) e(22);
110 if (st1
.st_mtime
!= st2
.st_mtime
) e(23);
111 if (st1
.st_ctime
!= st2
.st_ctime
) e(24);
112 if (st1
.st_atime
!= st2
.st_atime
) e(25);
114 /* Writing should start at the current (2) position. */
115 if (write(fd1
, "foo", 4) != 4) e(26);
117 if (st1
.st_mode
!= st2
.st_mode
) e(27);
118 if (st1
.st_size
>= st2
.st_size
) e(28);
119 if ((off_t
) 6 != st2
.st_size
) e(29);
120 if (st1
.st_nlink
!= st2
.st_nlink
) e(30);
121 if (st1
.st_mtime
> st2
.st_mtime
) e(31);
122 if (st1
.st_ctime
> st2
.st_ctime
) e(32);
123 if (st1
.st_atime
!= st2
.st_atime
) e(33);
125 /* A read of zero bytes should not affect anything. */
126 if (read(fd1
, buf
, 0) != 0) e(34);
128 if (st1
.st_uid
!= st2
.st_uid
) e(35);
129 if (st1
.st_gid
!= st2
.st_gid
) e(36); /* should be same */
130 if (st1
.st_mode
!= st2
.st_mode
) e(37);
131 if (st1
.st_size
!= st2
.st_size
) e(38);
132 if (st1
.st_nlink
!= st2
.st_nlink
) e(39);
133 if (st1
.st_mtime
!= st2
.st_mtime
) e(40);
134 if (st1
.st_ctime
!= st2
.st_ctime
) e(41);
135 if (st1
.st_atime
!= st2
.st_atime
) e(42);
137 /* The file now should contain ``fofoo\0'' Let's check that. */
138 if (lseek(fd1
, (off_t
) 0, SEEK_SET
) != 0) e(43);
139 if (read(fd1
, buf
, BUF_SIZE
) != 6) e(44);
140 if (strcmp(buf
, "fofoo") != 0) e(45);
142 /* Only the Access Time should be updated. */
144 if (st1
.st_mtime
!= st2
.st_mtime
) e(46);
145 if (st1
.st_ctime
!= st2
.st_ctime
) e(47);
146 if (st1
.st_atime
>= st2
.st_atime
) e(48);
148 /* A read of zero bytes should do nothing even at the end of the file. */
150 while (time1
>= time((time_t *)0))
152 if (read(fd1
, buf
, 0) != 0) e(49);
154 if (st1
.st_size
!= st2
.st_size
) e(50);
155 if (st1
.st_mtime
!= st2
.st_mtime
) e(51);
156 if (st1
.st_ctime
!= st2
.st_ctime
) e(52);
157 if (st1
.st_atime
!= st2
.st_atime
) e(53);
159 /* Reading should be done from the current offset. */
160 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(54);
161 if (lseek(fd1
, (off_t
) 2, SEEK_SET
) != 2) e(55);
162 if (read(fd1
, buf
, BUF_SIZE
) != 4) e(56);
163 if (strcmp(buf
, "foo") != 0) e(57);
165 /* Reading should effect the current file position. */
166 if (lseek(fd1
, (off_t
) 2, SEEK_SET
) != 2) e(58);
167 if (read(fd1
, buf
, 1) != 1) e(59);
168 if (*buf
!= 'f') e(60);
169 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 3) e(61);
170 if (read(fd1
, buf
, 1) != 1) e(62);
171 if (*buf
!= 'o') e(63);
172 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 4) e(64);
173 if (read(fd1
, buf
, 1) != 1) e(65);
174 if (*buf
!= 'o') e(66);
175 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 5) e(67);
176 if (read(fd1
, buf
, 1) != 1) e(68);
177 if (*buf
!= '\0') e(69);
178 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 6) e(70);
180 /* Read's at EOF should return 0. */
181 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(71);
182 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 6) e(72);
183 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(73);
184 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 6) e(74);
185 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(75);
186 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 6) e(76);
187 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(77);
188 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 6) e(78);
189 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(79);
190 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 6) e(80);
192 /* Writing should not always change the file size. */
193 if (lseek(fd1
, (off_t
) 2, SEEK_SET
) != 2) e(81);
194 if (write(fd1
, "ba", 2) != 2) e(82);
195 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 4) e(83);
197 if (st1
.st_size
!= 6) e(84);
199 /* Kill the \0 at the end. */
200 if (lseek(fd1
, (off_t
) 5, SEEK_SET
) != 5) e(85);
201 if (write(fd1
, "x", 1) != 1) e(86);
203 /* And close the bar. */
204 if (close(fd1
) != 0) e(87);
206 /* Try some stuff with O_APPEND. Bar contains ``fobaox'' */
207 if ((fd1
= open("bar", O_RDWR
| O_APPEND
)) != 3) e(88);
209 /* No matter what the file position is. Writes should append. */
210 if (lseek(fd1
, (off_t
) 2, SEEK_SET
) != 2) e(89);
211 if (write(fd1
, "y", 1) != 1) e(90);
213 if (st1
.st_size
!= (off_t
) 7) e(91);
214 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 7) e(92);
215 if (lseek(fd1
, (off_t
) 2, SEEK_SET
) != 2) e(93);
216 if (write(fd1
, "z", 2) != 2) e(94);
218 /* The file should contain ``fobaoxyz\0'' == 9 chars long. */
220 if (st1
.st_size
!= (off_t
) 9) e(95);
221 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 9) e(96);
223 /* Reading on a O_APPEND flag should be from the current offset. */
224 if (lseek(fd1
, (off_t
) 0, SEEK_SET
) != 0) e(97);
225 if (read(fd1
, buf
, BUF_SIZE
) != 9) e(98);
226 if (strcmp(buf
, "fobaoxyz") != 0) e(99);
227 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 9) e(100);
229 if (close(fd1
) != 0) e(101);
231 /* Let's test fifo writes. First blocking. */
232 if (mkfifo("fifo", 0777) != 0) e(102);
234 /* Read from fifo but no writer. */
235 System("rm -rf /tmp/sema.38a");
237 case -1: printf("Can't fork\n"); break;
241 if ((fd1
= open("fifo", O_RDONLY
)) != 3) e(103);
242 system("> /tmp/sema.38a");
243 system("while test -f /tmp/sema.38a; do sleep 1; done");
245 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(104);
246 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(105);
247 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(106);
248 if (close(fd1
) != 0) e(107);
252 if ((fd1
= open("fifo", O_WRONLY
)) != 3) e(108);
253 while (stat("/tmp/sema.38a", &st1
) != 0) sleep(1);
254 if (close(fd1
) != 0) e(109);
255 unlink("/tmp/sema.38a");
256 if (wait(&stat_loc
) == -1) e(110);
257 if (stat_loc
!= 0) e(111); /* Alarm? */
260 /* Read from fifo should wait for writer. */
262 case -1: printf("Can't fork\n"); break;
266 if ((fd1
= open("fifo", O_RDONLY
)) != 3) e(112);
267 if (read(fd1
, buf
, BUF_SIZE
) != 10) e(113);
268 if (strcmp(buf
, "Hi reader") != 0) e(114);
269 if (close(fd1
) != 0) e(115);
273 if ((fd1
= open("fifo", O_WRONLY
)) != 3) e(116);
275 if (write(fd1
, "Hi reader", 10) != 10) e(117);
276 if (close(fd1
) != 0) e(118);
277 if (wait(&stat_loc
) == -1) e(119);
278 if (stat_loc
!= 0) e(120); /* Alarm? */
282 /* Does this test test what it is supposed to test??? */
284 /* Read from fifo should wait for all writers to close. */
286 case -1: printf("Can't fork\n"); break;
291 case -1: printf("Can't fork\n"); break;
294 if ((fd1
= open("fifo", O_WRONLY
)) != 3) e(121);
295 printf("C2 did open\n");
296 if (close(fd1
) != 0) e(122);
297 printf("C2 did close\n");
300 printf("C1 scheduled\n");
301 if ((fd1
= open("fifo", O_WRONLY
)) != 3) e(123);
302 printf("C1 did open\n");
304 if (close(fd1
) != 0) e(124);
305 printf("C1 did close\n");
307 if (wait(&stat_loc
) == -1) e(125);
308 if (stat_loc
!= 0) e(126); /* Alarm? */
314 printf("Parent running\n");
315 sleep(1); /* open in childs first */
316 if ((fd1
= open("fifo", O_RDONLY
)) != 3) e(127);
317 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(128);
318 if (close(fd1
) != 0) e(129);
319 printf("Parent closed\n");
320 if ((wait_status
=wait(&stat_loc
)) == -1) e(130);
322 printf("wait_status %d, stat_loc %d:", wait_status
, stat_loc
);
323 if (WIFSIGNALED(stat_loc
)) {
324 printf(" killed, signal number %d\n", WTERMSIG(stat_loc
));
326 else if (WIFEXITED(stat_loc
)) {
327 printf(" normal exit, status %d\n", WEXITSTATUS(stat_loc
));
330 if (stat_loc
!= 0) e(131); /* Alarm? */
335 /* PIPE_BUF has to have a nice value. */
336 if (PIPE_BUF
< 5) e(132);
337 if (BUF_SIZE
< 1000) e(133);
339 /* Writes of blocks smaller than PIPE_BUF should be atomic. */
340 System("rm -rf /tmp/sema.38b;> /tmp/sema.38b");
342 case -1: printf("Can't fork\n"); break;
347 case -1: printf("Can't fork\n"); break;
351 if ((fd1
= open("fifo", O_WRONLY
)) != 3) e(134);
352 for (i
= 0; i
< 100; i
++) write(fd1
, "1234 ", 5);
353 system("while test -f /tmp/sema.38b; do sleep 1; done");
354 if (close(fd1
) != 0) e(135);
358 if ((fd1
= open("fifo", O_WRONLY
)) != 3) e(136);
359 for (i
= 0; i
< 100; i
++) write(fd1
, "1234 ", 5);
360 while (stat("/tmp/sema.38b", &st1
) == 0) sleep(1);
361 if (close(fd1
) != 0) e(137);
362 if (wait(&stat_loc
) == -1) e(138);
363 if (stat_loc
!= 0) e(139); /* Alarm? */
368 if ((fd1
= open("fifo", O_RDONLY
)) != 3) e(140);
370 memset(buf
, '\0', BUF_SIZE
);
372 /* Read buffer full or till EOF. */
374 j
= read(fd1
, buf
+ i
, BUF_SIZE
- i
);
376 if (j
% 5 != 0) e(141);
379 } while (j
> 0 && i
< 1000);
381 /* Signal the children to close write ends. This should not be */
382 /* Necessary. But due to a bug in 1.16.6 this is necessary. */
383 unlink("/tmp/sema.38b");
385 if (i
!= 1000) e(143);
386 if (wait(&stat_loc
) == -1) e(144);
387 if (stat_loc
!= 0) e(145); /* Alarm? */
389 /* Check 200 times 1234. */
390 for (i
= 0; i
< 200; i
++)
391 if (strncmp(buf
+ (i
* 5), "1234 ", 5) != 0) break;
392 if (i
!= 200) e(146);
393 if (buf
[1000] != '\0') e(147);
394 if (buf
[1005] != '\0') e(148);
395 if (buf
[1010] != '\0') e(149);
396 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(150);
397 if (close(fd1
) != 0) e(151);
400 /* Read from pipe should wait for writer. */
401 if (pipe(tube
) != 0) e(152);
403 case -1: printf("Can't fork\n"); break;
406 if (close(tube
[1]) != 0) e(153);
407 if (read(tube
[0], buf
, BUF_SIZE
) != 10) e(154);
408 if (strcmp(buf
, "Hi reader") != 0) e(155);
409 if (close(tube
[0]) != 0) e(156);
412 if (close(tube
[0]) != 0) e(157);
414 if (write(tube
[1], "Hi reader", 10) != 10) e(158);
415 if (close(tube
[1]) != 0) e(159);
416 if (wait(&stat_loc
) == -1) e(160);
417 if (stat_loc
!= 0) e(161); /* Alarm? */
420 /* Read from pipe should wait for all writers to close. */
421 if (pipe(tube
) != 0) e(162);
423 case -1: printf("Can't fork\n"); break;
426 if (close(tube
[0]) != 0) e(163);
428 case -1: printf("Can't fork\n"); break;
431 if (close(tube
[1]) != 0) e(164);
435 if (close(tube
[1]) != 0) e(165);
436 if (wait(&stat_loc
) == -1) e(166);
437 if (stat_loc
!= 0) e(167); /* Alarm? */
441 if (close(tube
[1]) != 0) e(168);
442 if (read(tube
[0], buf
, BUF_SIZE
) != 0) e(169);
443 if (close(tube
[0]) != 0) e(170);
444 if (wait(&stat_loc
) == -1) e(171);
445 if (stat_loc
!= 0) e(172); /* Alarm? */
448 /* Writes of blocks smaller than PIPE_BUF should be atomic. */
449 System("rm -rf /tmp/sema.38c;> /tmp/sema.38c");
450 if (pipe(tube
) != 0) e(173);
452 case -1: printf("Can't fork\n"); break;
455 if (close(tube
[0]) != 0) e(174);
457 case -1: printf("Can't fork\n"); break;
460 for (i
= 0; i
< 100; i
++) write(tube
[1], "1234 ", 5);
461 system("while test -f /tmp/sema.38c; do sleep 1; done");
462 if (close(tube
[1]) != 0) e(175);
465 for (i
= 0; i
< 100; i
++) write(tube
[1], "1234 ", 5);
466 while (stat("/tmp/sema.38c", &st1
) == 0) sleep(1);
467 if (close(tube
[1]) != 0) e(176);
468 if (wait(&stat_loc
) == -1) e(177);
469 if (stat_loc
!= 0) e(178); /* Alarm? */
474 if (close(tube
[1]) != 0) e(179);
475 memset(buf
, '\0', BUF_SIZE
);
477 j
= read(tube
[0], buf
+ i
, BUF_SIZE
- i
);
479 if (j
% 5 != 0) e(180);
482 break; /* EOF seen. */
484 unlink("/tmp/sema.38c");
486 if (i
!= 1000) e(182);
487 if (close(tube
[0]) != 0) e(183);
488 if (wait(&stat_loc
) == -1) e(184);
489 if (stat_loc
!= 0) e(185); /* Alarm? */
491 /* Check 200 times 1234. */
492 for (i
= 0; i
< 200; i
++)
493 if (strncmp(buf
+ (i
* 5), "1234 ", 5) != 0) break;
494 if (i
!= 200) e(186);
506 System("rm -rf ../DIR_38/*");
508 /* Lets try sequential writes. */
509 system("rm -rf /tmp/sema.38d");
512 case -1: printf("Can't fork\n"); break;
515 if ((fd
= open("testing", O_WRONLY
| O_APPEND
)) != 3) e(1);
516 if (write(fd
, "one ", 4) != 4) e(2);
517 if (close(fd
) != 0) e(3);
518 system("> /tmp/sema.38d");
519 system("while test -f /tmp/sema.38d; do sleep 1; done");
520 if ((fd
= open("testing", O_WRONLY
| O_APPEND
)) != 3) e(4);
521 if (write(fd
, "three ", 6) != 6) e(5);
522 if (close(fd
) != 0) e(6);
523 system("> /tmp/sema.38d");
526 while (stat("/tmp/sema.38d", &st
) != 0) sleep(1);
527 if ((fd
= open("testing", O_WRONLY
| O_APPEND
)) != 3) e(7);
528 if (write(fd
, "two ", 4) != 4) e(8);
529 if (close(fd
) != 0) e(9);
530 unlink("/tmp/sema.38d");
531 while (stat("/tmp/sema.38d", &st
) != 0) sleep(1);
532 if ((fd
= open("testing", O_WRONLY
| O_APPEND
)) != 3) e(10);
533 if (write(fd
, "four", 5) != 5) e(11);
534 if (close(fd
) != 0) e(12);
535 if (wait(&stat_loc
) == -1) e(13);
536 if (stat_loc
!= 0) e(14); /* The alarm went off? */
537 unlink("/tmp/sema.38d");
539 if ((fd
= open("testing", O_RDONLY
)) != 3) e(15);
540 if (read(fd
, buf
, BUF_SIZE
) != 19) e(16);
541 if (strcmp(buf
, "one two three four") != 0) e(17);
542 if (close(fd
) != 0) e(18);
544 /* Non written bytes in regular files should be zero. */
545 memset(buf2
, '\0', BUF_SIZE
);
546 if ((fd
= open("bigfile", O_RDWR
| O_CREAT
, 0644)) != 3) e(19);
547 if (lseek(fd
, (off_t
) 102400, SEEK_SET
) != (off_t
) 102400L) e(20);
548 if (read(fd
, buf
, BUF_SIZE
) != 0) e(21);
549 if (write(fd
, ".", 1) != 1) e(22);
550 Stat("bigfile", &st
);
551 if (st
.st_size
!= (off_t
) 102401) e(23);
552 if (lseek(fd
, (off_t
) 0, SEEK_SET
) != 0) e(24);
553 for (i
= 0; i
< 102400 / BUF_SIZE
; i
++) {
554 if (read(fd
, buf
, BUF_SIZE
) != BUF_SIZE
) e(25);
555 if (memcmp(buf
, buf2
, BUF_SIZE
) != 0) e(26);
557 if (close(fd
) != 0) e(27);
561 { /* Test correct error behavior. */
563 int fd
, tube
[2], stat_loc
;
567 struct sigaction act
, oact
;
569 void (*oldfunc
) (int);
573 System("rm -rf ../DIR_38/*");
575 /* To test if writing processes on closed pipes are signumbered. */
577 act
.sa_handler
= setsignumber
;
578 sigemptyset(&act
.sa_mask
);
580 if (sigaction(SIGPIPE
, &act
, &oact
) != 0) e(1);
582 oldfunc
= signal(SIGPIPE
, setsignumber
);
585 /* Non valid file descriptors should be an error. */
586 for (fd
= -111; fd
< 0; fd
++) {
588 if (read(fd
, buf
, BUF_SIZE
) != -1) e(2);
589 if (errno
!= EBADF
) e(3);
591 for (fd
= 3; fd
< 111; fd
++) {
593 if (read(fd
, buf
, BUF_SIZE
) != -1) e(4);
594 if (errno
!= EBADF
) e(5);
596 for (fd
= -111; fd
< 0; fd
++) {
598 if (write(fd
, buf
, BUF_SIZE
) != -1) e(6);
599 if (errno
!= EBADF
) e(7);
601 for (fd
= 3; fd
< 111; fd
++) {
603 if (write(fd
, buf
, BUF_SIZE
) != -1) e(8);
604 if (errno
!= EBADF
) e(9);
607 /* Writing a pipe with no readers should trigger SIGPIPE. */
608 if (pipe(tube
) != 0) e(10);
611 case -1: printf("Can't fork\n"); break;
615 if (write(tube
[1], buf
, BUF_SIZE
) != -1) e(11);
616 if (errno
!= EPIPE
) e(12);
617 if (signumber
!= SIGPIPE
) e(13);
618 if (close(tube
[1]) != 0) e(14);
622 if (wait(&stat_loc
) == -1) e(15);
623 if (stat_loc
!= 0) e(16); /* Alarm? */
626 /* Writing a fifo with no readers should trigger SIGPIPE. */
627 System("> /tmp/sema.38e");
628 if (mkfifo("fifo", 0666) != 0) e(17);
630 case -1: printf("Can't fork\n"); break;
633 if ((fd
= open("fifo", O_WRONLY
)) != 3) e(18);
634 system("while test -f /tmp/sema.38e; do sleep 1; done");
636 if (write(fd
, buf
, BUF_SIZE
) != -1) e(19);
637 if (errno
!= EPIPE
) e(20);
638 if (signumber
!= SIGPIPE
) e(21);
639 if (close(fd
) != 0) e(22);
642 if ((fd
= open("fifo", O_RDONLY
)) != 3) e(23);
643 if (close(fd
) != 0) e(24);
644 unlink("/tmp/sema.38e");
645 if (wait(&stat_loc
) == -1) e(25);
646 if (stat_loc
!= 0) e(26); /* Alarm? */
650 /* Restore normal (re)action to SIGPIPE. */
651 if (sigaction(SIGPIPE
, &oact
, NULL
) != 0) e(27);
653 signal(SIGPIPE
, oldfunc
);
656 /* Read from fifo should return -1 and set errno to EAGAIN. */
657 System("rm -rf /tmp/sema.38[fgh]");
659 case -1: printf("Can't fork\n"); break;
662 system("while test ! -f /tmp/sema.38f; do sleep 1; done");
663 System("rm -rf /tmp/sema.38f");
664 if ((fd
= open("fifo", O_WRONLY
| O_NONBLOCK
)) != 3) e(28);
665 close(creat("/tmp/sema.38g", 0666));
666 system("while test ! -f /tmp/sema.38h; do sleep 1; done");
667 if (close(fd
) != 0) e(38);
668 System("rm -rf /tmp/sema.38h");
671 if ((fd
= open("fifo", O_RDONLY
| O_NONBLOCK
)) != 3) e(30);
672 close(creat("/tmp/sema.38f", 0666));
673 system("while test ! -f /tmp/sema.38g; do sleep 1; done");
674 System("rm -rf /tmp/sema.38g");
675 if (read(fd
, buf
, BUF_SIZE
) != -1) e(31);
676 if (errno
!= EAGAIN
) e(32);
677 if (read(fd
, buf
, BUF_SIZE
) != -1) e(33);
678 if (errno
!= EAGAIN
) e(34);
679 if (read(fd
, buf
, BUF_SIZE
) != -1) e(35);
680 if (errno
!= EAGAIN
) e(36);
681 close(creat("/tmp/sema.38h", 0666));
682 while (stat("/tmp/sema.38h", &st
) == 0) sleep(1);
683 if (read(fd
, buf
, BUF_SIZE
) != 0) e(37);
684 if (close(fd
) != 0) e(38);
685 if (wait(&stat_loc
) == -1) e(39);
686 if (stat_loc
!= 0) e(40); /* Alarm? */
688 System("rm -rf fifo");
690 /* If a read is interrupted by a SIGNAL. */
691 if (pipe(tube
) != 0) e(41);
692 switch (pid
= fork()) {
693 case -1: printf("Can't fork\n"); break;
697 act
.sa_handler
= setsignumber
;
698 sigemptyset(&act
.sa_mask
);
700 if (sigaction(SIGUSR1
, &act
, &oact
) != 0) e(42);
702 oldfunc
= signal(SIGUSR1
, setsignumber
);
704 if (read(tube
[0], buf
, BUF_SIZE
) != -1) e(43);
705 if (errno
!= EINTR
) e(44);
706 if (signumber
!= SIGUSR1
) e(45);
708 /* Restore normal (re)action to SIGPIPE. */
709 if (sigaction(SIGUSR1
, &oact
, NULL
) != 0) e(46);
711 signal(SIGUSR1
, oldfunc
);
717 /* The sleep 1 should give the child time to start the read. */
722 if (stat_loc
!= 0) e(47); /* Alarm? */
727 void setsignumber(signum
)