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) */
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)
37 void setsignumber(int _signumber
);
39 int main(int argc
, char *argv
[])
46 if (argc
== 2) m
= atoi(argv
[1]);
47 superuser
= (geteuid() == 0);
50 for (i
= 0; i
< ITERATIONS
; i
++) {
51 if (m
& 0001) test38a();
52 if (m
& 0002) test38b();
53 if (m
& 0004) test38c();
57 return(-1); /* Unreachable */
61 { /* Try normal operation. */
71 System("rm -rf ../DIR_38/*");
74 if ((fd1
= open("bar", O_RDWR
| O_CREAT
, 0777)) != 3) e(1);
77 /* Writing nothing should not affect the file at all. */
78 if (write(fd1
, "", 0) != 0) e(2);
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. */
91 while (time1
>= time((time_t *)0))
93 if (write(fd1
, "foo", 4) != 4) e(11);
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);
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);
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);
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. */
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. */
149 while (time1
>= time((time_t *)0))
151 if (read(fd1
, buf
, 0) != 0) e(49);
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);
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);
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. */
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");
236 case -1: printf("Can't fork\n"); break;
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");
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);
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. */
261 case -1: printf("Can't fork\n"); break;
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);
272 if ((fd1
= open("fifo", O_WRONLY
)) != 3) e(116);
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? */
281 /* Does this test test what it is supposed to test??? */
283 /* Read from fifo should wait for all writers to close. */
285 case -1: printf("Can't fork\n"); break;
290 case -1: printf("Can't fork\n"); break;
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");
299 printf("C1 scheduled\n");
300 if ((fd1
= open("fifo", O_WRONLY
)) != 3) e(123);
301 printf("C1 did open\n");
303 if (close(fd1
) != 0) e(124);
304 printf("C1 did close\n");
306 if (wait(&stat_loc
) == -1) e(125);
307 if (stat_loc
!= 0) e(126); /* Alarm? */
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? */
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");
341 case -1: printf("Can't fork\n"); break;
346 case -1: printf("Can't fork\n"); break;
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);
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? */
367 if ((fd1
= open("fifo", O_RDONLY
)) != 3) e(140);
369 memset(buf
, '\0', BUF_SIZE
);
371 /* Read buffer full or till EOF. */
373 j
= read(fd1
, buf
+ i
, BUF_SIZE
- i
);
375 if (j
% 5 != 0) e(141);
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");
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);
402 case -1: printf("Can't fork\n"); break;
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);
411 if (close(tube
[0]) != 0) e(157);
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);
422 case -1: printf("Can't fork\n"); break;
425 if (close(tube
[0]) != 0) e(163);
427 case -1: printf("Can't fork\n"); break;
430 if (close(tube
[1]) != 0) e(164);
434 if (close(tube
[1]) != 0) e(165);
435 if (wait(&stat_loc
) == -1) e(166);
436 if (stat_loc
!= 0) e(167); /* Alarm? */
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);
451 case -1: printf("Can't fork\n"); break;
454 if (close(tube
[0]) != 0) e(174);
456 case -1: printf("Can't fork\n"); break;
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);
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? */
473 if (close(tube
[1]) != 0) e(179);
474 memset(buf
, '\0', BUF_SIZE
);
476 j
= read(tube
[0], buf
+ i
, BUF_SIZE
- i
);
478 if (j
% 5 != 0) e(180);
481 break; /* EOF seen. */
483 unlink("/tmp/sema.38c");
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);
505 System("rm -rf ../DIR_38/*");
507 /* Lets try sequential writes. */
508 system("rm -rf /tmp/sema.38d");
511 case -1: printf("Can't fork\n"); break;
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");
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);
560 { /* Test correct error behavior. */
562 int fd
, tube
[2], stat_loc
;
566 struct sigaction act
, oact
;
568 void (*oldfunc
) (int);
572 System("rm -rf ../DIR_38/*");
574 /* To test if writing processes on closed pipes are signumbered. */
576 act
.sa_handler
= setsignumber
;
577 sigemptyset(&act
.sa_mask
);
579 if (sigaction(SIGPIPE
, &act
, &oact
) != 0) e(1);
581 oldfunc
= signal(SIGPIPE
, setsignumber
);
584 /* Non valid file descriptors should be an error. */
585 for (fd
= -111; fd
< 0; fd
++) {
587 if (read(fd
, buf
, BUF_SIZE
) != -1) e(2);
588 if (errno
!= EBADF
) e(3);
590 for (fd
= 3; fd
< 111; fd
++) {
592 if (read(fd
, buf
, BUF_SIZE
) != -1) e(4);
593 if (errno
!= EBADF
) e(5);
595 for (fd
= -111; fd
< 0; fd
++) {
597 if (write(fd
, buf
, BUF_SIZE
) != -1) e(6);
598 if (errno
!= EBADF
) e(7);
600 for (fd
= 3; fd
< 111; fd
++) {
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);
610 case -1: printf("Can't fork\n"); break;
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);
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);
629 case -1: printf("Can't fork\n"); break;
632 if ((fd
= open("fifo", O_WRONLY
)) != 3) e(18);
633 system("while test -f /tmp/sema.38e; do sleep 1; done");
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);
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? */
649 /* Restore normal (re)action to SIGPIPE. */
650 if (sigaction(SIGPIPE
, &oact
, NULL
) != 0) e(27);
652 signal(SIGPIPE
, oldfunc
);
655 /* Read from fifo should return -1 and set errno to EAGAIN. */
656 System("rm -rf /tmp/sema.38[fgh]");
658 case -1: printf("Can't fork\n"); break;
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");
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;
696 act
.sa_handler
= setsignumber
;
697 sigemptyset(&act
.sa_mask
);
699 if (sigaction(SIGUSR1
, &act
, &oact
) != 0) e(42);
701 oldfunc
= signal(SIGUSR1
, setsignumber
);
703 if (read(tube
[0], buf
, BUF_SIZE
) != -1) e(43);
704 if (errno
!= EINTR
) e(44);
705 if (signumber
!= SIGUSR1
) e(45);
707 /* Restore normal (re)action to SIGPIPE. */
708 if (sigaction(SIGUSR1
, &oact
, NULL
) != 0) e(46);
710 signal(SIGUSR1
, oldfunc
);
716 /* The sleep 1 should give the child time to start the read. */
721 if (stat_loc
!= 0) e(47); /* Alarm? */
726 void setsignumber(signum
)