source: trunk/minix/test/test38.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 21.2 KB
Line 
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
5
6/* test38: read(), write() Author: Jan-Mark Wams (jms@cs.vu.nl) */
7
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>
20
21#define MAX_ERROR 4
22#define ITERATIONS 3
23#define BUF_SIZE 1024
24
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)
28
29int errct = 0;
30int subtest = 1;
31int superuser;
32int signumber = 0;
33
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));
41
42void main(argc, argv)
43int argc;
44char *argv[];
45{
46 int i, m = 0xFFFF;
47
48 sync();
49 if (argc == 2) m = atoi(argv[1]);
50 printf("Test 38 ");
51 fflush(stdout);
52 System("rm -rf DIR_38; mkdir DIR_38");
53 Chdir("DIR_38");
54 superuser = (geteuid() == 0);
55 umask(0000);
56
57 for (i = 0; i < ITERATIONS; i++) {
58 if (m & 0001) test38a();
59 if (m & 0002) test38b();
60 if (m & 0004) test38c();
61 }
62 quit();
63}
64
65void test38a()
66{ /* Try normal operation. */
67 int fd1;
68 struct stat st1, st2;
69 time_t time1;
70 char buf[BUF_SIZE];
71 int stat_loc;
72 int i, j;
73 int tube[2];
74
75 subtest = 1;
76 System("rm -rf ../DIR_38/*");
77
78 /* Let's open bar. */
79 if ((fd1 = open("bar", O_RDWR | O_CREAT, 0777)) != 3) e(1);
80 Stat("bar", &st1);
81
82 /* Writing nothing should not affect the file at all. */
83 if (write(fd1, "", 0) != 0) e(2);
84 Stat("bar", &st2);
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);
93
94 /* A write should update some status fields. */
95 time(&time1);
96 while (time1 >= time((time_t *)0))
97 ;
98 if (write(fd1, "foo", 4) != 4) e(11);
99 Stat("bar", &st2);
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);
107
108 /* Lseeks should not change the file status. */
109 if (lseek(fd1, (off_t) - 2, SEEK_END) != 2) e(19);
110 Stat("bar", &st1);
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);
117
118 /* Writing should start at the current (2) position. */
119 if (write(fd1, "foo", 4) != 4) e(26);
120 Stat("bar", &st2);
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);
128
129 /* A read of zero bytes should not affect anything. */
130 if (read(fd1, buf, 0) != 0) e(34);
131 Stat("bar", &st1);
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);
140
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);
145
146 /* Only the Access Time should be updated. */
147 Stat("bar", &st2);
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);
151
152 /* A read of zero bytes should do nothing even at the end of the file. */
153 time(&time1);
154 while (time1 >= time((time_t *)0))
155 ;
156 if (read(fd1, buf, 0) != 0) e(49);
157 Stat("bar", &st1);
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);
162
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);
168
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);
183
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);
195
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);
200 Stat("bar", &st1);
201 if (st1.st_size != 6) e(84);
202
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);
206
207 /* And close the bar. */
208 if (close(fd1) != 0) e(87);
209
210 /* Try some stuff with O_APPEND. Bar contains ``fobaox'' */
211 if ((fd1 = open("bar", O_RDWR | O_APPEND)) != 3) e(88);
212
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);
216 Stat("bar", &st1);
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);
221
222 /* The file should contain ``fobaoxyz\0'' == 9 chars long. */
223 Stat("bar", &st1);
224 if (st1.st_size != (off_t) 9) e(95);
225 if (lseek(fd1, (off_t) 0, SEEK_CUR) != 9) e(96);
226
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);
232
233 if (close(fd1) != 0) e(101);
234
235 /* Let's test fifo writes. First blocking. */
236 if (mkfifo("fifo", 0777) != 0) e(102);
237
238 /* Read from fifo but no writer. */
239 System("rm -rf /tmp/sema.38a");
240 switch (fork()) {
241 case -1: printf("Can't fork\n"); break;
242
243 case 0:
244 alarm(20);
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");
248errno =0;
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);
253 exit(0);
254
255 default:
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? */
262 }
263
264 /* Read from fifo should wait for writer. */
265 switch (fork()) {
266 case -1: printf("Can't fork\n"); break;
267
268 case 0:
269 alarm(20);
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);
274 exit(0);
275
276 default:
277 if ((fd1 = open("fifo", O_WRONLY)) != 3) e(116);
278 sleep(1);
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? */
283 }
284
285#if DEAD_CODE
286 /* Does this test test what it is supposed to test??? */
287
288 /* Read from fifo should wait for all writers to close. */
289 switch (fork()) {
290 case -1: printf("Can't fork\n"); break;
291
292 case 0:
293 alarm(60);
294 switch (fork()) {
295 case -1: printf("Can't fork\n"); break;
296 case 0:
297 alarm(20);
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");
302 exit(0);
303 default:
304 printf("C1 scheduled\n");
305 if ((fd1 = open("fifo", O_WRONLY)) != 3) e(123);
306 printf("C1 did open\n");
307 sleep(2);
308 if (close(fd1) != 0) e(124);
309 printf("C1 did close\n");
310 sleep(1);
311 if (wait(&stat_loc) == -1) e(125);
312 if (stat_loc != 0) e(126); /* Alarm? */
313 }
314 exit(stat_loc);
315
316 default: {
317 int wait_status;
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);
325
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));
329 }
330 else if (WIFEXITED(stat_loc)) {
331 printf(" normal exit, status %d\n", WEXITSTATUS(stat_loc));
332 }
333
334 if (stat_loc != 0) e(131); /* Alarm? */
335 }
336 }
337#endif
338
339 /* PIPE_BUF has to have a nice value. */
340 if (PIPE_BUF < 5) e(132);
341 if (BUF_SIZE < 1000) e(133);
342
343 /* Writes of blocks smaller than PIPE_BUF should be atomic. */
344 System("rm -rf /tmp/sema.38b;> /tmp/sema.38b");
345 switch (fork()) {
346 case -1: printf("Can't fork\n"); break;
347
348 case 0:
349 alarm(20);
350 switch (fork()) {
351 case -1: printf("Can't fork\n"); break;
352
353 case 0:
354 alarm(20);
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);
359 exit(0);
360
361 default:
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? */
368 }
369 exit(stat_loc);
370
371 default:
372 if ((fd1 = open("fifo", O_RDONLY)) != 3) e(140);
373 i = 0;
374 memset(buf, '\0', BUF_SIZE);
375
376 /* Read buffer full or till EOF. */
377 do {
378 j = read(fd1, buf + i, BUF_SIZE - i);
379 if (j > 0) {
380 if (j % 5 != 0) e(141);
381 i += j;
382 }
383 } while (j > 0 && i < 1000);
384
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");
388 if (j < 0) e(142);
389 if (i != 1000) e(143);
390 if (wait(&stat_loc) == -1) e(144);
391 if (stat_loc != 0) e(145); /* Alarm? */
392
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);
402 }
403
404 /* Read from pipe should wait for writer. */
405 if (pipe(tube) != 0) e(152);
406 switch (fork()) {
407 case -1: printf("Can't fork\n"); break;
408 case 0:
409 alarm(20);
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);
414 exit(0);
415 default:
416 if (close(tube[0]) != 0) e(157);
417 sleep(1);
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? */
422 }
423
424 /* Read from pipe should wait for all writers to close. */
425 if (pipe(tube) != 0) e(162);
426 switch (fork()) {
427 case -1: printf("Can't fork\n"); break;
428 case 0:
429 alarm(20);
430 if (close(tube[0]) != 0) e(163);
431 switch (fork()) {
432 case -1: printf("Can't fork\n"); break;
433 case 0:
434 alarm(20);
435 if (close(tube[1]) != 0) e(164);
436 exit(0);
437 default:
438 sleep(1);
439 if (close(tube[1]) != 0) e(165);
440 if (wait(&stat_loc) == -1) e(166);
441 if (stat_loc != 0) e(167); /* Alarm? */
442 }
443 exit(stat_loc);
444 default:
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? */
450 }
451
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);
455 switch (fork()) {
456 case -1: printf("Can't fork\n"); break;
457 case 0:
458 alarm(20);
459 if (close(tube[0]) != 0) e(174);
460 switch (fork()) {
461 case -1: printf("Can't fork\n"); break;
462 case 0:
463 alarm(20);
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);
467 exit(0);
468 default:
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? */
474 }
475 exit(stat_loc);
476 default:
477 i = 0;
478 if (close(tube[1]) != 0) e(179);
479 memset(buf, '\0', BUF_SIZE);
480 do {
481 j = read(tube[0], buf + i, BUF_SIZE - i);
482 if (j > 0) {
483 if (j % 5 != 0) e(180);
484 i += j;
485 } else
486 break; /* EOF seen. */
487 } while (i < 1000);
488 unlink("/tmp/sema.38c");
489 if (j < 0) e(181);
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? */
494
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);
499 }
500}
501
502void test38b()
503{
504 int i, fd, stat_loc;
505 char buf[BUF_SIZE];
506 char buf2[BUF_SIZE];
507 struct stat st;
508
509 subtest = 2;
510 System("rm -rf ../DIR_38/*");
511
512 /* Lets try sequential writes. */
513 system("rm -rf /tmp/sema.38d");
514 System("> testing");
515 switch (fork()) {
516 case -1: printf("Can't fork\n"); break;
517 case 0:
518 alarm(20);
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");
528 exit(0);
529 default:
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");
542 }
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);
547
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);
560 }
561 if (close(fd) != 0) e(27);
562}
563
564void test38c()
565{ /* Test correct error behavior. */
566 char buf[BUF_SIZE];
567 int fd, tube[2], stat_loc;
568 struct stat st;
569 pid_t pid;
570#ifdef SIGACTION
571 struct sigaction act, oact;
572#else
573#if _ANSI
574 void (*oldfunc) (int);
575#else
576 void (*oldfunc) ();
577#endif
578#endif
579
580 subtest = 3;
581 System("rm -rf ../DIR_38/*");
582
583 /* To test if writing processes on closed pipes are signumbered. */
584#ifdef SIGACTION
585 act.sa_handler = setsignumber;
586 sigemptyset(&act.sa_mask);
587 act.sa_flags = 0;
588 if (sigaction(SIGPIPE, &act, &oact) != 0) e(1);
589#else
590 oldfunc = signal(SIGPIPE, setsignumber);
591#endif
592
593 /* Non valid file descriptors should be an error. */
594 for (fd = -111; fd < 0; fd++) {
595 errno = 0;
596 if (read(fd, buf, BUF_SIZE) != -1) e(2);
597 if (errno != EBADF) e(3);
598 }
599 for (fd = 3; fd < 111; fd++) {
600 errno = 0;
601 if (read(fd, buf, BUF_SIZE) != -1) e(4);
602 if (errno != EBADF) e(5);
603 }
604 for (fd = -111; fd < 0; fd++) {
605 errno = 0;
606 if (write(fd, buf, BUF_SIZE) != -1) e(6);
607 if (errno != EBADF) e(7);
608 }
609 for (fd = 3; fd < 111; fd++) {
610 errno = 0;
611 if (write(fd, buf, BUF_SIZE) != -1) e(8);
612 if (errno != EBADF) e(9);
613 }
614
615 /* Writing a pipe with no readers should trigger SIGPIPE. */
616 if (pipe(tube) != 0) e(10);
617 close(tube[0]);
618 switch (fork()) {
619 case -1: printf("Can't fork\n"); break;
620 case 0:
621 alarm(20);
622 signumber = 0;
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);
627 exit(0);
628 default:
629 close(tube[1]);
630 if (wait(&stat_loc) == -1) e(15);
631 if (stat_loc != 0) e(16); /* Alarm? */
632 }
633
634 /* Writing a fifo with no readers should trigger SIGPIPE. */
635 System("> /tmp/sema.38e");
636 if (mkfifo("fifo", 0666) != 0) e(17);
637 switch (fork()) {
638 case -1: printf("Can't fork\n"); break;
639 case 0:
640 alarm(20);
641 if ((fd = open("fifo", O_WRONLY)) != 3) e(18);
642 system("while test -f /tmp/sema.38e; do sleep 1; done");
643 signumber = 0;
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);
648 exit(0);
649 default:
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? */
655 }
656
657#ifdef SIGACTION
658 /* Restore normal (re)action to SIGPIPE. */
659 if (sigaction(SIGPIPE, &oact, NULL) != 0) e(27);
660#else
661 signal(SIGPIPE, oldfunc);
662#endif
663
664 /* Read from fifo should return -1 and set errno to EAGAIN. */
665 System("rm -rf /tmp/sema.38[fgh]");
666 switch (fork()) {
667 case -1: printf("Can't fork\n"); break;
668 case 0:
669 alarm(20);
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");
677 exit(0);
678 default:
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? */
695 }
696 System("rm -rf fifo");
697
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;
702 case 0:
703 alarm(20);
704#ifdef SIGACTION
705 act.sa_handler = setsignumber;
706 sigemptyset(&act.sa_mask);
707 act.sa_flags = 0;
708 if (sigaction(SIGUSR1, &act, &oact) != 0) e(42);
709#else
710 oldfunc = signal(SIGUSR1, setsignumber);
711#endif
712 if (read(tube[0], buf, BUF_SIZE) != -1) e(43);
713 if (errno != EINTR) e(44);
714 if (signumber != SIGUSR1) e(45);
715#ifdef SIGACTION
716 /* Restore normal (re)action to SIGPIPE. */
717 if (sigaction(SIGUSR1, &oact, NULL) != 0) e(46);
718#else
719 signal(SIGUSR1, oldfunc);
720#endif
721 close(tube[0]);
722 close(tube[1]);
723 exit(0);
724 default:
725 /* The sleep 1 should give the child time to start the read. */
726 sleep(1);
727 close(tube[0]);
728 kill(pid, SIGUSR1);
729 wait(&stat_loc);
730 if (stat_loc != 0) e(47); /* Alarm? */
731 close(tube[1]);
732 }
733}
734
735void setsignumber(signum)
736int signum;
737{
738 signumber = signum;
739}
740
741void e(n)
742int n;
743{
744 int err_num = errno; /* Save in case printf clobbers it. */
745
746 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
747 errno = err_num;
748 perror("");
749 if (errct++ > MAX_ERROR) {
750 printf("Too many errors; test aborted\n");
751 chdir("..");
752 system("rm -rf DIR*");
753 exit(1);
754 }
755 errno = 0;
756}
757
758void quit()
759{
760 Chdir("..");
761 System("rm -rf DIR_38");
762
763 if (errct == 0) {
764 printf("ok\n");
765 exit(0);
766 } else {
767 printf("%d errors\n", errct);
768 exit(1);
769 }
770}
Note: See TracBrowser for help on using the repository browser.