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)
34 _PROTOTYPE(void main
, (int argc
, char *argv
[]));
35 _PROTOTYPE(void test38a
, (void));
36 _PROTOTYPE(void test38b
, (void));
37 _PROTOTYPE(void test38c
, (void));
38 _PROTOTYPE(void setsignumber
, (int _signumber
));
39 _PROTOTYPE(void e
, (int number
));
40 _PROTOTYPE(void quit
, (void));
49 if (argc
== 2) m
= atoi(argv
[1]);
52 System("rm -rf DIR_38; mkdir DIR_38");
54 superuser
= (geteuid() == 0);
57 for (i
= 0; i
< ITERATIONS
; i
++) {
58 if (m
& 0001) test38a();
59 if (m
& 0002) test38b();
60 if (m
& 0004) test38c();
66 { /* Try normal operation. */
76 System("rm -rf ../DIR_38/*");
79 if ((fd1
= open("bar", O_RDWR
| O_CREAT
, 0777)) != 3) e(1);
82 /* Writing nothing should not affect the file at all. */
83 if (write(fd1
, "", 0) != 0) e(2);
85 if (st1
.st_uid
!= st2
.st_uid
) e(3);
86 if (st1
.st_gid
!= st2
.st_gid
) e(4); /* should be same */
87 if (st1
.st_mode
!= st2
.st_mode
) e(5);
88 if (st1
.st_size
!= st2
.st_size
) e(6);
89 if (st1
.st_nlink
!= st2
.st_nlink
) e(7);
90 if (st1
.st_mtime
!= st2
.st_mtime
) e(8);
91 if (st1
.st_ctime
!= st2
.st_ctime
) e(9);
92 if (st1
.st_atime
!= st2
.st_atime
) e(10);
94 /* A write should update some status fields. */
96 while (time1
>= time((time_t *)0))
98 if (write(fd1
, "foo", 4) != 4) e(11);
100 if (st1
.st_mode
!= st2
.st_mode
) e(12);
101 if (st1
.st_size
>= st2
.st_size
) e(13);
102 if ((off_t
) 4 != st2
.st_size
) e(14);
103 if (st1
.st_nlink
!= st2
.st_nlink
) e(15);
104 if (st1
.st_mtime
>= st2
.st_mtime
) e(16);
105 if (st1
.st_ctime
>= st2
.st_ctime
) e(17);
106 if (st1
.st_atime
!= st2
.st_atime
) e(18);
108 /* Lseeks should not change the file status. */
109 if (lseek(fd1
, (off_t
) - 2, SEEK_END
) != 2) e(19);
111 if (st1
.st_mode
!= st2
.st_mode
) e(20);
112 if (st1
.st_size
!= st2
.st_size
) e(21);
113 if (st1
.st_nlink
!= st2
.st_nlink
) e(22);
114 if (st1
.st_mtime
!= st2
.st_mtime
) e(23);
115 if (st1
.st_ctime
!= st2
.st_ctime
) e(24);
116 if (st1
.st_atime
!= st2
.st_atime
) e(25);
118 /* Writing should start at the current (2) position. */
119 if (write(fd1
, "foo", 4) != 4) e(26);
121 if (st1
.st_mode
!= st2
.st_mode
) e(27);
122 if (st1
.st_size
>= st2
.st_size
) e(28);
123 if ((off_t
) 6 != st2
.st_size
) e(29);
124 if (st1
.st_nlink
!= st2
.st_nlink
) e(30);
125 if (st1
.st_mtime
> st2
.st_mtime
) e(31);
126 if (st1
.st_ctime
> st2
.st_ctime
) e(32);
127 if (st1
.st_atime
!= st2
.st_atime
) e(33);
129 /* A read of zero bytes should not affect anything. */
130 if (read(fd1
, buf
, 0) != 0) e(34);
132 if (st1
.st_uid
!= st2
.st_uid
) e(35);
133 if (st1
.st_gid
!= st2
.st_gid
) e(36); /* should be same */
134 if (st1
.st_mode
!= st2
.st_mode
) e(37);
135 if (st1
.st_size
!= st2
.st_size
) e(38);
136 if (st1
.st_nlink
!= st2
.st_nlink
) e(39);
137 if (st1
.st_mtime
!= st2
.st_mtime
) e(40);
138 if (st1
.st_ctime
!= st2
.st_ctime
) e(41);
139 if (st1
.st_atime
!= st2
.st_atime
) e(42);
141 /* The file now should contain ``fofoo\0'' Let's check that. */
142 if (lseek(fd1
, (off_t
) 0, SEEK_SET
) != 0) e(43);
143 if (read(fd1
, buf
, BUF_SIZE
) != 6) e(44);
144 if (strcmp(buf
, "fofoo") != 0) e(45);
146 /* Only the Access Time should be updated. */
148 if (st1
.st_mtime
!= st2
.st_mtime
) e(46);
149 if (st1
.st_ctime
!= st2
.st_ctime
) e(47);
150 if (st1
.st_atime
>= st2
.st_atime
) e(48);
152 /* A read of zero bytes should do nothing even at the end of the file. */
154 while (time1
>= time((time_t *)0))
156 if (read(fd1
, buf
, 0) != 0) e(49);
158 if (st1
.st_size
!= st2
.st_size
) e(50);
159 if (st1
.st_mtime
!= st2
.st_mtime
) e(51);
160 if (st1
.st_ctime
!= st2
.st_ctime
) e(52);
161 if (st1
.st_atime
!= st2
.st_atime
) e(53);
163 /* Reading should be done from the current offset. */
164 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(54);
165 if (lseek(fd1
, (off_t
) 2, SEEK_SET
) != 2) e(55);
166 if (read(fd1
, buf
, BUF_SIZE
) != 4) e(56);
167 if (strcmp(buf
, "foo") != 0) e(57);
169 /* Reading should effect the current file position. */
170 if (lseek(fd1
, (off_t
) 2, SEEK_SET
) != 2) e(58);
171 if (read(fd1
, buf
, 1) != 1) e(59);
172 if (*buf
!= 'f') e(60);
173 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 3) e(61);
174 if (read(fd1
, buf
, 1) != 1) e(62);
175 if (*buf
!= 'o') e(63);
176 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 4) e(64);
177 if (read(fd1
, buf
, 1) != 1) e(65);
178 if (*buf
!= 'o') e(66);
179 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 5) e(67);
180 if (read(fd1
, buf
, 1) != 1) e(68);
181 if (*buf
!= '\0') e(69);
182 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 6) e(70);
184 /* Read's at EOF should return 0. */
185 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(71);
186 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 6) e(72);
187 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(73);
188 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 6) e(74);
189 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(75);
190 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 6) e(76);
191 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(77);
192 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 6) e(78);
193 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(79);
194 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 6) e(80);
196 /* Writing should not always change the file size. */
197 if (lseek(fd1
, (off_t
) 2, SEEK_SET
) != 2) e(81);
198 if (write(fd1
, "ba", 2) != 2) e(82);
199 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 4) e(83);
201 if (st1
.st_size
!= 6) e(84);
203 /* Kill the \0 at the end. */
204 if (lseek(fd1
, (off_t
) 5, SEEK_SET
) != 5) e(85);
205 if (write(fd1
, "x", 1) != 1) e(86);
207 /* And close the bar. */
208 if (close(fd1
) != 0) e(87);
210 /* Try some stuff with O_APPEND. Bar contains ``fobaox'' */
211 if ((fd1
= open("bar", O_RDWR
| O_APPEND
)) != 3) e(88);
213 /* No matter what the file position is. Writes should append. */
214 if (lseek(fd1
, (off_t
) 2, SEEK_SET
) != 2) e(89);
215 if (write(fd1
, "y", 1) != 1) e(90);
217 if (st1
.st_size
!= (off_t
) 7) e(91);
218 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 7) e(92);
219 if (lseek(fd1
, (off_t
) 2, SEEK_SET
) != 2) e(93);
220 if (write(fd1
, "z", 2) != 2) e(94);
222 /* The file should contain ``fobaoxyz\0'' == 9 chars long. */
224 if (st1
.st_size
!= (off_t
) 9) e(95);
225 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 9) e(96);
227 /* Reading on a O_APPEND flag should be from the current offset. */
228 if (lseek(fd1
, (off_t
) 0, SEEK_SET
) != 0) e(97);
229 if (read(fd1
, buf
, BUF_SIZE
) != 9) e(98);
230 if (strcmp(buf
, "fobaoxyz") != 0) e(99);
231 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 9) e(100);
233 if (close(fd1
) != 0) e(101);
235 /* Let's test fifo writes. First blocking. */
236 if (mkfifo("fifo", 0777) != 0) e(102);
238 /* Read from fifo but no writer. */
239 System("rm -rf /tmp/sema.38a");
241 case -1: printf("Can't fork\n"); break;
245 if ((fd1
= open("fifo", O_RDONLY
)) != 3) e(103);
246 system("> /tmp/sema.38a");
247 system("while test -f /tmp/sema.38a; do sleep 1; done");
249 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(104);
250 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(105);
251 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(106);
252 if (close(fd1
) != 0) e(107);
256 if ((fd1
= open("fifo", O_WRONLY
)) != 3) e(108);
257 while (stat("/tmp/sema.38a", &st1
) != 0) sleep(1);
258 if (close(fd1
) != 0) e(109);
259 unlink("/tmp/sema.38a");
260 if (wait(&stat_loc
) == -1) e(110);
261 if (stat_loc
!= 0) e(111); /* Alarm? */
264 /* Read from fifo should wait for writer. */
266 case -1: printf("Can't fork\n"); break;
270 if ((fd1
= open("fifo", O_RDONLY
)) != 3) e(112);
271 if (read(fd1
, buf
, BUF_SIZE
) != 10) e(113);
272 if (strcmp(buf
, "Hi reader") != 0) e(114);
273 if (close(fd1
) != 0) e(115);
277 if ((fd1
= open("fifo", O_WRONLY
)) != 3) e(116);
279 if (write(fd1
, "Hi reader", 10) != 10) e(117);
280 if (close(fd1
) != 0) e(118);
281 if (wait(&stat_loc
) == -1) e(119);
282 if (stat_loc
!= 0) e(120); /* Alarm? */
286 /* Does this test test what it is supposed to test??? */
288 /* Read from fifo should wait for all writers to close. */
290 case -1: printf("Can't fork\n"); break;
295 case -1: printf("Can't fork\n"); break;
298 if ((fd1
= open("fifo", O_WRONLY
)) != 3) e(121);
299 printf("C2 did open\n");
300 if (close(fd1
) != 0) e(122);
301 printf("C2 did close\n");
304 printf("C1 scheduled\n");
305 if ((fd1
= open("fifo", O_WRONLY
)) != 3) e(123);
306 printf("C1 did open\n");
308 if (close(fd1
) != 0) e(124);
309 printf("C1 did close\n");
311 if (wait(&stat_loc
) == -1) e(125);
312 if (stat_loc
!= 0) e(126); /* Alarm? */
318 printf("Parent running\n");
319 sleep(1); /* open in childs first */
320 if ((fd1
= open("fifo", O_RDONLY
)) != 3) e(127);
321 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(128);
322 if (close(fd1
) != 0) e(129);
323 printf("Parent closed\n");
324 if ((wait_status
=wait(&stat_loc
)) == -1) e(130);
326 printf("wait_status %d, stat_loc %d:", wait_status
, stat_loc
);
327 if (WIFSIGNALED(stat_loc
)) {
328 printf(" killed, signal number %d\n", WTERMSIG(stat_loc
));
330 else if (WIFEXITED(stat_loc
)) {
331 printf(" normal exit, status %d\n", WEXITSTATUS(stat_loc
));
334 if (stat_loc
!= 0) e(131); /* Alarm? */
339 /* PIPE_BUF has to have a nice value. */
340 if (PIPE_BUF
< 5) e(132);
341 if (BUF_SIZE
< 1000) e(133);
343 /* Writes of blocks smaller than PIPE_BUF should be atomic. */
344 System("rm -rf /tmp/sema.38b;> /tmp/sema.38b");
346 case -1: printf("Can't fork\n"); break;
351 case -1: printf("Can't fork\n"); break;
355 if ((fd1
= open("fifo", O_WRONLY
)) != 3) e(134);
356 for (i
= 0; i
< 100; i
++) write(fd1
, "1234 ", 5);
357 system("while test -f /tmp/sema.38b; do sleep 1; done");
358 if (close(fd1
) != 0) e(135);
362 if ((fd1
= open("fifo", O_WRONLY
)) != 3) e(136);
363 for (i
= 0; i
< 100; i
++) write(fd1
, "1234 ", 5);
364 while (stat("/tmp/sema.38b", &st1
) == 0) sleep(1);
365 if (close(fd1
) != 0) e(137);
366 if (wait(&stat_loc
) == -1) e(138);
367 if (stat_loc
!= 0) e(139); /* Alarm? */
372 if ((fd1
= open("fifo", O_RDONLY
)) != 3) e(140);
374 memset(buf
, '\0', BUF_SIZE
);
376 /* Read buffer full or till EOF. */
378 j
= read(fd1
, buf
+ i
, BUF_SIZE
- i
);
380 if (j
% 5 != 0) e(141);
383 } while (j
> 0 && i
< 1000);
385 /* Signal the children to close write ends. This should not be */
386 /* Necessary. But due to a bug in 1.16.6 this is necessary. */
387 unlink("/tmp/sema.38b");
389 if (i
!= 1000) e(143);
390 if (wait(&stat_loc
) == -1) e(144);
391 if (stat_loc
!= 0) e(145); /* Alarm? */
393 /* Check 200 times 1234. */
394 for (i
= 0; i
< 200; i
++)
395 if (strncmp(buf
+ (i
* 5), "1234 ", 5) != 0) break;
396 if (i
!= 200) e(146);
397 if (buf
[1000] != '\0') e(147);
398 if (buf
[1005] != '\0') e(148);
399 if (buf
[1010] != '\0') e(149);
400 if (read(fd1
, buf
, BUF_SIZE
) != 0) e(150);
401 if (close(fd1
) != 0) e(151);
404 /* Read from pipe should wait for writer. */
405 if (pipe(tube
) != 0) e(152);
407 case -1: printf("Can't fork\n"); break;
410 if (close(tube
[1]) != 0) e(153);
411 if (read(tube
[0], buf
, BUF_SIZE
) != 10) e(154);
412 if (strcmp(buf
, "Hi reader") != 0) e(155);
413 if (close(tube
[0]) != 0) e(156);
416 if (close(tube
[0]) != 0) e(157);
418 if (write(tube
[1], "Hi reader", 10) != 10) e(158);
419 if (close(tube
[1]) != 0) e(159);
420 if (wait(&stat_loc
) == -1) e(160);
421 if (stat_loc
!= 0) e(161); /* Alarm? */
424 /* Read from pipe should wait for all writers to close. */
425 if (pipe(tube
) != 0) e(162);
427 case -1: printf("Can't fork\n"); break;
430 if (close(tube
[0]) != 0) e(163);
432 case -1: printf("Can't fork\n"); break;
435 if (close(tube
[1]) != 0) e(164);
439 if (close(tube
[1]) != 0) e(165);
440 if (wait(&stat_loc
) == -1) e(166);
441 if (stat_loc
!= 0) e(167); /* Alarm? */
445 if (close(tube
[1]) != 0) e(168);
446 if (read(tube
[0], buf
, BUF_SIZE
) != 0) e(169);
447 if (close(tube
[0]) != 0) e(170);
448 if (wait(&stat_loc
) == -1) e(171);
449 if (stat_loc
!= 0) e(172); /* Alarm? */
452 /* Writes of blocks smaller than PIPE_BUF should be atomic. */
453 System("rm -rf /tmp/sema.38c;> /tmp/sema.38c");
454 if (pipe(tube
) != 0) e(173);
456 case -1: printf("Can't fork\n"); break;
459 if (close(tube
[0]) != 0) e(174);
461 case -1: printf("Can't fork\n"); break;
464 for (i
= 0; i
< 100; i
++) write(tube
[1], "1234 ", 5);
465 system("while test -f /tmp/sema.38c; do sleep 1; done");
466 if (close(tube
[1]) != 0) e(175);
469 for (i
= 0; i
< 100; i
++) write(tube
[1], "1234 ", 5);
470 while (stat("/tmp/sema.38c", &st1
) == 0) sleep(1);
471 if (close(tube
[1]) != 0) e(176);
472 if (wait(&stat_loc
) == -1) e(177);
473 if (stat_loc
!= 0) e(178); /* Alarm? */
478 if (close(tube
[1]) != 0) e(179);
479 memset(buf
, '\0', BUF_SIZE
);
481 j
= read(tube
[0], buf
+ i
, BUF_SIZE
- i
);
483 if (j
% 5 != 0) e(180);
486 break; /* EOF seen. */
488 unlink("/tmp/sema.38c");
490 if (i
!= 1000) e(182);
491 if (close(tube
[0]) != 0) e(183);
492 if (wait(&stat_loc
) == -1) e(184);
493 if (stat_loc
!= 0) e(185); /* Alarm? */
495 /* Check 200 times 1234. */
496 for (i
= 0; i
< 200; i
++)
497 if (strncmp(buf
+ (i
* 5), "1234 ", 5) != 0) break;
498 if (i
!= 200) e(186);
510 System("rm -rf ../DIR_38/*");
512 /* Lets try sequential writes. */
513 system("rm -rf /tmp/sema.38d");
516 case -1: printf("Can't fork\n"); break;
519 if ((fd
= open("testing", O_WRONLY
| O_APPEND
)) != 3) e(1);
520 if (write(fd
, "one ", 4) != 4) e(2);
521 if (close(fd
) != 0) e(3);
522 system("> /tmp/sema.38d");
523 system("while test -f /tmp/sema.38d; do sleep 1; done");
524 if ((fd
= open("testing", O_WRONLY
| O_APPEND
)) != 3) e(4);
525 if (write(fd
, "three ", 6) != 6) e(5);
526 if (close(fd
) != 0) e(6);
527 system("> /tmp/sema.38d");
530 while (stat("/tmp/sema.38d", &st
) != 0) sleep(1);
531 if ((fd
= open("testing", O_WRONLY
| O_APPEND
)) != 3) e(7);
532 if (write(fd
, "two ", 4) != 4) e(8);
533 if (close(fd
) != 0) e(9);
534 unlink("/tmp/sema.38d");
535 while (stat("/tmp/sema.38d", &st
) != 0) sleep(1);
536 if ((fd
= open("testing", O_WRONLY
| O_APPEND
)) != 3) e(10);
537 if (write(fd
, "four", 5) != 5) e(11);
538 if (close(fd
) != 0) e(12);
539 if (wait(&stat_loc
) == -1) e(13);
540 if (stat_loc
!= 0) e(14); /* The alarm went off? */
541 unlink("/tmp/sema.38d");
543 if ((fd
= open("testing", O_RDONLY
)) != 3) e(15);
544 if (read(fd
, buf
, BUF_SIZE
) != 19) e(16);
545 if (strcmp(buf
, "one two three four") != 0) e(17);
546 if (close(fd
) != 0) e(18);
548 /* Non written bytes in regular files should be zero. */
549 memset(buf2
, '\0', BUF_SIZE
);
550 if ((fd
= open("bigfile", O_RDWR
| O_CREAT
, 0644)) != 3) e(19);
551 if (lseek(fd
, (off_t
) 102400, SEEK_SET
) != (off_t
) 102400L) e(20);
552 if (read(fd
, buf
, BUF_SIZE
) != 0) e(21);
553 if (write(fd
, ".", 1) != 1) e(22);
554 Stat("bigfile", &st
);
555 if (st
.st_size
!= (off_t
) 102401) e(23);
556 if (lseek(fd
, (off_t
) 0, SEEK_SET
) != 0) e(24);
557 for (i
= 0; i
< 102400 / BUF_SIZE
; i
++) {
558 if (read(fd
, buf
, BUF_SIZE
) != BUF_SIZE
) e(25);
559 if (memcmp(buf
, buf2
, BUF_SIZE
) != 0) e(26);
561 if (close(fd
) != 0) e(27);
565 { /* Test correct error behavior. */
567 int fd
, tube
[2], stat_loc
;
571 struct sigaction act
, oact
;
574 void (*oldfunc
) (int);
581 System("rm -rf ../DIR_38/*");
583 /* To test if writing processes on closed pipes are signumbered. */
585 act
.sa_handler
= setsignumber
;
586 sigemptyset(&act
.sa_mask
);
588 if (sigaction(SIGPIPE
, &act
, &oact
) != 0) e(1);
590 oldfunc
= signal(SIGPIPE
, setsignumber
);
593 /* Non valid file descriptors should be an error. */
594 for (fd
= -111; fd
< 0; fd
++) {
596 if (read(fd
, buf
, BUF_SIZE
) != -1) e(2);
597 if (errno
!= EBADF
) e(3);
599 for (fd
= 3; fd
< 111; fd
++) {
601 if (read(fd
, buf
, BUF_SIZE
) != -1) e(4);
602 if (errno
!= EBADF
) e(5);
604 for (fd
= -111; fd
< 0; fd
++) {
606 if (write(fd
, buf
, BUF_SIZE
) != -1) e(6);
607 if (errno
!= EBADF
) e(7);
609 for (fd
= 3; fd
< 111; fd
++) {
611 if (write(fd
, buf
, BUF_SIZE
) != -1) e(8);
612 if (errno
!= EBADF
) e(9);
615 /* Writing a pipe with no readers should trigger SIGPIPE. */
616 if (pipe(tube
) != 0) e(10);
619 case -1: printf("Can't fork\n"); break;
623 if (write(tube
[1], buf
, BUF_SIZE
) != -1) e(11);
624 if (errno
!= EPIPE
) e(12);
625 if (signumber
!= SIGPIPE
) e(13);
626 if (close(tube
[1]) != 0) e(14);
630 if (wait(&stat_loc
) == -1) e(15);
631 if (stat_loc
!= 0) e(16); /* Alarm? */
634 /* Writing a fifo with no readers should trigger SIGPIPE. */
635 System("> /tmp/sema.38e");
636 if (mkfifo("fifo", 0666) != 0) e(17);
638 case -1: printf("Can't fork\n"); break;
641 if ((fd
= open("fifo", O_WRONLY
)) != 3) e(18);
642 system("while test -f /tmp/sema.38e; do sleep 1; done");
644 if (write(fd
, buf
, BUF_SIZE
) != -1) e(19);
645 if (errno
!= EPIPE
) e(20);
646 if (signumber
!= SIGPIPE
) e(21);
647 if (close(fd
) != 0) e(22);
650 if ((fd
= open("fifo", O_RDONLY
)) != 3) e(23);
651 if (close(fd
) != 0) e(24);
652 unlink("/tmp/sema.38e");
653 if (wait(&stat_loc
) == -1) e(25);
654 if (stat_loc
!= 0) e(26); /* Alarm? */
658 /* Restore normal (re)action to SIGPIPE. */
659 if (sigaction(SIGPIPE
, &oact
, NULL
) != 0) e(27);
661 signal(SIGPIPE
, oldfunc
);
664 /* Read from fifo should return -1 and set errno to EAGAIN. */
665 System("rm -rf /tmp/sema.38[fgh]");
667 case -1: printf("Can't fork\n"); break;
670 system("while test ! -f /tmp/sema.38f; do sleep 1; done");
671 System("rm -rf /tmp/sema.38f");
672 if ((fd
= open("fifo", O_WRONLY
| O_NONBLOCK
)) != 3) e(28);
673 close(creat("/tmp/sema.38g", 0666));
674 system("while test ! -f /tmp/sema.38h; do sleep 1; done");
675 if (close(fd
) != 0) e(38);
676 System("rm -rf /tmp/sema.38h");
679 if ((fd
= open("fifo", O_RDONLY
| O_NONBLOCK
)) != 3) e(30);
680 close(creat("/tmp/sema.38f", 0666));
681 system("while test ! -f /tmp/sema.38g; do sleep 1; done");
682 System("rm -rf /tmp/sema.38g");
683 if (read(fd
, buf
, BUF_SIZE
) != -1) e(31);
684 if (errno
!= EAGAIN
) e(32);
685 if (read(fd
, buf
, BUF_SIZE
) != -1) e(33);
686 if (errno
!= EAGAIN
) e(34);
687 if (read(fd
, buf
, BUF_SIZE
) != -1) e(35);
688 if (errno
!= EAGAIN
) e(36);
689 close(creat("/tmp/sema.38h", 0666));
690 while (stat("/tmp/sema.38h", &st
) == 0) sleep(1);
691 if (read(fd
, buf
, BUF_SIZE
) != 0) e(37);
692 if (close(fd
) != 0) e(38);
693 if (wait(&stat_loc
) == -1) e(39);
694 if (stat_loc
!= 0) e(40); /* Alarm? */
696 System("rm -rf fifo");
698 /* If a read is interrupted by a SIGNAL. */
699 if (pipe(tube
) != 0) e(41);
700 switch (pid
= fork()) {
701 case -1: printf("Can't fork\n"); break;
705 act
.sa_handler
= setsignumber
;
706 sigemptyset(&act
.sa_mask
);
708 if (sigaction(SIGUSR1
, &act
, &oact
) != 0) e(42);
710 oldfunc
= signal(SIGUSR1
, setsignumber
);
712 if (read(tube
[0], buf
, BUF_SIZE
) != -1) e(43);
713 if (errno
!= EINTR
) e(44);
714 if (signumber
!= SIGUSR1
) e(45);
716 /* Restore normal (re)action to SIGPIPE. */
717 if (sigaction(SIGUSR1
, &oact
, NULL
) != 0) e(46);
719 signal(SIGUSR1
, oldfunc
);
725 /* The sleep 1 should give the child time to start the read. */
730 if (stat_loc
!= 0) e(47); /* Alarm? */
735 void setsignumber(signum
)
744 int err_num
= errno
; /* Save in case printf clobbers it. */
746 printf("Subtest %d, error %d errno=%d: ", subtest
, n
, errno
);
749 if (errct
++ > MAX_ERROR
) {
750 printf("Too many errors; test aborted\n");
752 system("rm -rf DIR*");
761 System("rm -rf DIR_38");
767 printf("%d errors\n", errct
);