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 /* test29: 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
29 int errct = 0;
30 int subtest = 1;
31 int superuser;
32 volatile int signumber = 0;
33
34 _PROTOTYPE(void main, (int argc, char *argv[]));
35 _PROTOTYPE(void test29a, (void));
36 _PROTOTYPE(void test29b, (void));
37 _PROTOTYPE(void test29c, (void));
38 _PROTOTYPE(void setsignumber, (int _signumber));
39 _PROTOTYPE(void e, (int number));
40 _PROTOTYPE(void quit, (void));
41
42 void main(argc, argv)
43 int argc;
44 char *argv[];
45 {
46 int i, m = 0xFFFF;
47
48 sync();
49 if (argc == 2) m = atoi(argv[1]);
50 printf("Test 29 ");
51 fflush(stdout);
52 System("rm -rf DIR_29; mkdir DIR_29");
53 Chdir("DIR_29");
54 superuser = (geteuid() == 0);
55 umask(0000);
56
57 for (i = 0; i < ITERATIONS; i++) {
58 if (m & 0001) test29a();
59 if (m & 0002) test29b();
60 if (m & 0004) test29c();
61 }
62 quit();
63 }
64
65 void test29a()
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_29/*");
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.29a");
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.29a");
247 system("while test -f /tmp/sema.29a; do sleep 1; done");
248 errno =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.29a", &st1) != 0) sleep(1);
258 if (close(fd1) != 0) e(109);
259 unlink("/tmp/sema.29a");
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 /* Read from fifo should wait for all writers to close. */
286 switch (fork()) {
287 case -1: printf("Can't fork\n"); break;
288
289 case 0:
290 alarm(20);
291 switch (fork()) {
292 case -1: printf("Can't fork\n"); break;
293 case 0:
294 alarm(20);
295 if ((fd1 = open("fifo", O_WRONLY)) != 3) e(121);
296 if (close(fd1) != 0) e(122);
297 exit(0);
298 default:
299 if ((fd1 = open("fifo", O_WRONLY)) != 3) e(123);
300 sleep(1);
301 if (close(fd1) != 0) e(124);
302 if (wait(&stat_loc) == -1) e(125);
303 if (stat_loc != 0) e(126); /* Alarm? */
304 }
305 exit(stat_loc);
306
307 default:
308 if ((fd1 = open("fifo", O_RDONLY)) != 3) e(127);
309 if (read(fd1, buf, BUF_SIZE) != 0) e(128);
310 if (close(fd1) != 0) e(129);
311 if (wait(&stat_loc) == -1) e(130);
312 if (stat_loc != 0) e(131); /* Alarm? */
313 }
314
315 /* PIPE_BUF has to have a nice value. */
316 if (PIPE_BUF < 5) e(132);
317 if (BUF_SIZE < 1000) e(133);
318
319 /* Writes of blocks smaller than PIPE_BUF should be atomic. */
320 System("rm -rf /tmp/sema.29b;> /tmp/sema.29b");
321 switch (fork()) {
322 case -1: printf("Can't fork\n"); break;
323
324 case 0:
325 alarm(20);
326 switch (fork()) {
327 case -1: printf("Can't fork\n"); break;
328
329 case 0:
330 alarm(20);
331 if ((fd1 = open("fifo", O_WRONLY)) != 3) e(134);
332 for (i = 0; i < 100; i++) write(fd1, "1234 ", 5);
333 system("while test -f /tmp/sema.29b; do sleep 1; done");
334 if (close(fd1) != 0) e(135);
335 exit(0);
336
337 default:
338 if ((fd1 = open("fifo", O_WRONLY)) != 3) e(136);
339 for (i = 0; i < 100; i++) write(fd1, "1234 ", 5);
340 while (stat("/tmp/sema.29b", &st1) == 0) sleep(1);
341 if (close(fd1) != 0) e(137);
342 if (wait(&stat_loc) == -1) e(138);
343 if (stat_loc != 0) e(139); /* Alarm? */
344 }
345 exit(stat_loc);
346
347 default:
348 if ((fd1 = open("fifo", O_RDONLY)) != 3) e(140);
349 i = 0;
350 memset(buf, '\0', BUF_SIZE);
351
352 /* Read buffer full or till EOF. */
353 do {
354 j = read(fd1, buf + i, BUF_SIZE - i);
355 if (j > 0) {
356 if (j % 5 != 0) e(141);
357 i += j;
358 }
359 } while (j > 0 && i < 1000);
360
361 /* Signal the children to close write ends. This should not be */
362 /* Necessary. But due to a bug in 1.16.6 this is necessary. */
363 unlink("/tmp/sema.29b");
364 if (j < 0) e(142);
365 if (i != 1000) e(143);
366 if (wait(&stat_loc) == -1) e(144);
367 if (stat_loc != 0) e(145); /* Alarm? */
368
369 /* Check 200 times 1234. */
370 for (i = 0; i < 200; i++)
371 if (strncmp(buf + (i * 5), "1234 ", 5) != 0) break;
372 if (i != 200) e(146);
373 if (buf[1000] != '\0') e(147);
374 if (buf[1005] != '\0') e(148);
375 if (buf[1010] != '\0') e(149);
376 if (read(fd1, buf, BUF_SIZE) != 0) e(150);
377 if (close(fd1) != 0) e(151);
378 }
379
380 /* Read from pipe should wait for writer. */
381 if (pipe(tube) != 0) e(152);
382 switch (fork()) {
383 case -1: printf("Can't fork\n"); break;
384 case 0:
385 alarm(20);
386 if (close(tube[1]) != 0) e(153);
387 if (read(tube[0], buf, BUF_SIZE) != 10) e(154);
388 if (strcmp(buf, "Hi reader") != 0) e(155);
389 if (close(tube[0]) != 0) e(156);
390 exit(0);
391 default:
392 if (close(tube[0]) != 0) e(157);
393 sleep(1);
394 if (write(tube[1], "Hi reader", 10) != 10) e(158);
395 if (close(tube[1]) != 0) e(159);
396 if (wait(&stat_loc) == -1) e(160);
397 if (stat_loc != 0) e(161); /* Alarm? */
398 }
399
400 /* Read from pipe should wait for all writers to close. */
401 if (pipe(tube) != 0) e(162);
402 switch (fork()) {
403 case -1: printf("Can't fork\n"); break;
404 case 0:
405 alarm(20);
406 if (close(tube[0]) != 0) e(163);
407 switch (fork()) {
408 case -1: printf("Can't fork\n"); break;
409 case 0:
410 alarm(20);
411 if (close(tube[1]) != 0) e(164);
412 exit(0);
413 default:
414 sleep(1);
415 if (close(tube[1]) != 0) e(165);
416 if (wait(&stat_loc) == -1) e(166);
417 if (stat_loc != 0) e(167); /* Alarm? */
418 }
419 exit(stat_loc);
420 default:
421 if (close(tube[1]) != 0) e(168);
422 if (read(tube[0], buf, BUF_SIZE) != 0) e(169);
423 if (close(tube[0]) != 0) e(170);
424 if (wait(&stat_loc) == -1) e(171);
425 if (stat_loc != 0) e(172); /* Alarm? */
426 }
427
428 /* Writes of blocks smaller than PIPE_BUF should be atomic. */
429 System("rm -rf /tmp/sema.29c;> /tmp/sema.29c");
430 if (pipe(tube) != 0) e(173);
431 switch (fork()) {
432 case -1: printf("Can't fork\n"); break;
433 case 0:
434 alarm(20);
435 if (close(tube[0]) != 0) e(174);
436 switch (fork()) {
437 case -1: printf("Can't fork\n"); break;
438 case 0:
439 alarm(20);
440 for (i = 0; i < 100; i++) write(tube[1], "1234 ", 5);
441 system("while test -f /tmp/sema.29c; do sleep 1; done");
442 if (close(tube[1]) != 0) e(175);
443 exit(0);
444 default:
445 for (i = 0; i < 100; i++) write(tube[1], "1234 ", 5);
446 while (stat("/tmp/sema.29c", &st1) == 0) sleep(1);
447 if (close(tube[1]) != 0) e(176);
448 if (wait(&stat_loc) == -1) e(177);
449 if (stat_loc != 0) e(178); /* Alarm? */
450 }
451 exit(stat_loc);
452 default:
453 i = 0;
454 if (close(tube[1]) != 0) e(179);
455 memset(buf, '\0', BUF_SIZE);
456 do {
457 j = read(tube[0], buf + i, BUF_SIZE - i);
458 if (j > 0) {
459 if (j % 5 != 0) e(180);
460 i += j;
461 } else
462 break; /* EOF seen. */
463 } while (i < 1000);
464 unlink("/tmp/sema.29c");
465 if (j < 0) e(181);
466 if (i != 1000) e(182);
467 if (close(tube[0]) != 0) e(183);
468 if (wait(&stat_loc) == -1) e(184);
469 if (stat_loc != 0) e(185); /* Alarm? */
470
471 /* Check 200 times 1234. */
472 for (i = 0; i < 200; i++)
473 if (strncmp(buf + (i * 5), "1234 ", 5) != 0) break;
474 if (i != 200) e(186);
475 }
476 }
477
478
479 void test29b()
480 {
481 int i, fd, stat_loc;
482 char buf[BUF_SIZE];
483 char buf2[BUF_SIZE];
484 struct stat st;
485
486 subtest = 2;
487 System("rm -rf ../DIR_29/*");
488
489 /* Lets try sequential writes. */
490 system("rm -rf /tmp/sema.29d");
491 System("> testing");
492 switch (fork()) {
493 case -1: printf("Can't fork\n"); break;
494 case 0:
495 alarm(20);
496 if ((fd = open("testing", O_WRONLY | O_APPEND)) != 3) e(1);
497 if (write(fd, "one ", 4) != 4) e(2);
498 if (close(fd) != 0) e(3);
499 system("> /tmp/sema.29d");
500 system("while test -f /tmp/sema.29d; do sleep 1; done");
501 if ((fd = open("testing", O_WRONLY | O_APPEND)) != 3) e(4);
502 if (write(fd, "three ", 6) != 6) e(5);
503 if (close(fd) != 0) e(6);
504 system("> /tmp/sema.29d");
505 exit(0);
506 default:
507 while (stat("/tmp/sema.29d", &st) != 0) sleep(1);
508 if ((fd = open("testing", O_WRONLY | O_APPEND)) != 3) e(7);
509 if (write(fd, "two ", 4) != 4) e(8);
510 if (close(fd) != 0) e(9);
511 unlink("/tmp/sema.29d");
512 while (stat("/tmp/sema.29d", &st) != 0) sleep(1);
513 if ((fd = open("testing", O_WRONLY | O_APPEND)) != 3) e(10);
514 if (write(fd, "four", 5) != 5) e(11);
515 if (close(fd) != 0) e(12);
516 if (wait(&stat_loc) == -1) e(13);
517 if (stat_loc != 0) e(14); /* The alarm went off? */
518 unlink("/tmp/sema.29d");
519 }
520 if ((fd = open("testing", O_RDONLY)) != 3) e(15);
521 if (read(fd, buf, BUF_SIZE) != 19) e(16);
522 if (strcmp(buf, "one two three four") != 0) e(17);
523 if (close(fd) != 0) e(18);
524
525
526 /* Non written bytes in regular files should be zero. */
527 memset(buf2, '\0', BUF_SIZE);
528 if ((fd = open("bigfile", O_RDWR | O_CREAT, 0644)) != 3) e(19);
529 if (lseek(fd, (off_t) 102400, SEEK_SET) != (off_t) 102400L) e(20);
530 if (read(fd, buf, BUF_SIZE) != 0) e(21);
531 if (write(fd, ".", 1) != 1) e(22);
532 Stat("bigfile", &st);
533 if (st.st_size != (off_t) 102401) e(23);
534 if (lseek(fd, (off_t) 0, SEEK_SET) != 0) e(24);
535 for (i = 0; i < 102400 / BUF_SIZE; i++) {
536 if (read(fd, buf, BUF_SIZE) != BUF_SIZE) e(25);
537 if (memcmp(buf, buf2, BUF_SIZE) != 0) e(26);
538 }
539 if (close(fd) != 0) e(27);
540 }
541
542 void test29c()
543 { /* Test correct error behavior. */
544 char buf[BUF_SIZE];
545 int fd, tube[2], stat_loc;
546 struct stat st;
547 pid_t pid;
548 #ifdef SIGACTION
549 struct sigaction act, oact;
550 #else
551 #if _ANSI
552 void (*oldfunc) (int);
553 #else
554 void (*oldfunc) ();
555 #endif
556 #endif
557
558 subtest = 3;
559 System("rm -rf ../DIR_29/*");
560
561 /* To test if writing processes on closed pipes are signumbered. */
562 #ifdef SIGACTION
563 act.sa_handler = setsignumber;
564 sigemptyset(&act.sa_mask);
565 act.sa_flags = 0;
566 if (sigaction(SIGPIPE, &act, &oact) != 0) e(1);
567 #else
568 oldfunc = signal(SIGPIPE, setsignumber);
569 #endif
570
571 /* Non valid file descriptors should be an error. */
572 for (fd = -111; fd < 0; fd++) {
573 errno = 0;
574 if (read(fd, buf, BUF_SIZE) != -1) e(2);
575 if (errno != EBADF) e(3);
576 }
577 for (fd = 3; fd < 111; fd++) {
578 errno = 0;
579 if (read(fd, buf, BUF_SIZE) != -1) e(4);
580 if (errno != EBADF) e(5);
581 }
582 for (fd = -111; fd < 0; fd++) {
583 errno = 0;
584 if (write(fd, buf, BUF_SIZE) != -1) e(6);
585 if (errno != EBADF) e(7);
586 }
587 for (fd = 3; fd < 111; fd++) {
588 errno = 0;
589 if (write(fd, buf, BUF_SIZE) != -1) e(8);
590 if (errno != EBADF) e(9);
591 }
592
593 /* Writing a pipe with no readers should trigger SIGPIPE. */
594 if (pipe(tube) != 0) e(10);
595 close(tube[0]);
596 switch (fork()) {
597 case -1: printf("Can't fork\n"); break;
598 case 0:
599 alarm(20);
600 signumber = 0;
601 if (write(tube[1], buf, BUF_SIZE) != -1) e(11);
602 if (errno != EPIPE) e(12);
603 if (signumber != SIGPIPE) e(13);
604 if (close(tube[1]) != 0) e(14);
605 exit(0);
606 default:
607 close(tube[1]);
608 if (wait(&stat_loc) == -1) e(15);
609 if (stat_loc != 0) e(16); /* Alarm? */
610 }
611
612 /* Writing a fifo with no readers should trigger SIGPIPE. */
613 System("> /tmp/sema.29e");
614 if (mkfifo("fifo", 0666) != 0) e(17);
615 switch (fork()) {
616 case -1: printf("Can't fork\n"); break;
617 case 0:
618 alarm(20);
619 if ((fd = open("fifo", O_WRONLY)) != 3) e(18);
620 system("while test -f /tmp/sema.29e; do sleep 1; done");
621 signumber = 0;
622 if (write(fd, buf, BUF_SIZE) != -1) e(19);
623 if (errno != EPIPE) e(20);
624 if (signumber != SIGPIPE) e(21);
625 if (close(fd) != 0) e(22);
626 exit(0);
627 default:
628 if ((fd = open("fifo", O_RDONLY)) != 3) e(23);
629 if (close(fd) != 0) e(24);
630 unlink("/tmp/sema.29e");
631 if (wait(&stat_loc) == -1) e(25);
632 if (stat_loc != 0) e(26); /* Alarm? */
633 }
634
635 #ifdef SIGACTION
636 /* Restore normal (re)action to SIGPIPE. */
637 if (sigaction(SIGPIPE, &oact, NULL) != 0) e(27);
638 #else
639 signal(SIGPIPE, oldfunc);
640 #endif
641
642 /* Read from fifo should return -1 and set errno to EAGAIN. */
643 System("rm -rf /tmp/sema.29[fgh]");
644 switch (fork()) {
645 case -1: printf("Can't fork\n"); break;
646 case 0:
647 alarm(20);
648 system("while test ! -f /tmp/sema.29f; do sleep 1; done");
649 System("rm -rf /tmp/sema.29f");
650 if ((fd = open("fifo", O_WRONLY | O_NONBLOCK)) != 3) e(28);
651 close(creat("/tmp/sema.29g", 0666));
652 system("while test ! -f /tmp/sema.29h; do sleep 1; done");
653 if (close(fd) != 0) e(29);
654 System("rm -rf /tmp/sema.29h");
655 exit(0);
656 default:
657 if ((fd = open("fifo", O_RDONLY | O_NONBLOCK)) != 3) e(30);
658 close(creat("/tmp/sema.29f", 0666));
659 system("while test ! -f /tmp/sema.29g; do sleep 1; done");
660 System("rm -rf /tmp/sema.29g");
661 if (read(fd, buf, BUF_SIZE) != -1) e(31);
662 if (errno != EAGAIN) e(32);
663 if (read(fd, buf, BUF_SIZE) != -1) e(33);
664 if (errno != EAGAIN) e(34);
665 if (read(fd, buf, BUF_SIZE) != -1) e(35);
666 if (errno != EAGAIN) e(36);
667 close(creat("/tmp/sema.29h", 0666));
668 while (stat("/tmp/sema.29h", &st) == 0) sleep(1);
669 if (read(fd, buf, BUF_SIZE) != 0) e(37);
670 if (close(fd) != 0) e(38);
671 if (wait(&stat_loc) == -1) e(39);
672 if (stat_loc != 0) e(40); /* Alarm? */
673 }
674 System("rm -rf fifo");
675
676 /* If a read is interrupted by a SIGNAL. */
677 if (pipe(tube) != 0) e(41);
678 switch (pid = fork()) {
679 case -1: printf("Can't fork\n"); break;
680 case 0:
681 alarm(20);
682 #ifdef SIGACTION
683 act.sa_handler = setsignumber;
684 sigemptyset(&act.sa_mask);
685 act.sa_flags = 0;
686 if (sigaction(SIGUSR1, &act, &oact) != 0) e(42);
687 #else
688 oldfunc = signal(SIGUSR1, setsignumber);
689 #endif
690 if (read(tube[0], buf, BUF_SIZE) != -1) e(43);
691 if (errno != EINTR) e(44);
692 if (signumber != SIGUSR1) e(45);
693 #ifdef SIGACTION
694 /* Restore normal (re)action to SIGPIPE. */
695 if (sigaction(SIGUSR1, &oact, NULL) != 0) e(46);
696 #else
697 signal(SIGUSR1, oldfunc);
698 #endif
699 close(tube[0]);
700 close(tube[1]);
701 exit(0);
702 default:
703 /* The sleep 1 should give the child time to start the read. */
704 sleep(1);
705 close(tube[0]);
706 kill(pid, SIGUSR1);
707 wait(&stat_loc);
708 if (stat_loc != 0) e(47); /* Alarm? */
709 close(tube[1]);
710 }
711 }
712
713
714 void setsignumber(signum)
715 int signum;
716 {
717 signumber = signum;
718 }
719
720 void e(n)
721 int n;
722 {
723 int err_num = errno; /* Save in case printf clobbers it. */
724
725 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
726 errno = err_num;
727 perror("");
728 if (errct++ > MAX_ERROR) {
729 printf("Too many errors; test aborted\n");
730 chdir("..");
731 system("rm -rf DIR*");
732 exit(1);
733 }
734 errno = 0;
735 }
736
737
738 void quit()
739 {
740 Chdir("..");
741 System("rm -rf DIR_29");
742
743 if (errct == 0) {
744 printf("ok\n");
745 exit(0);
746 } else {
747 printf("%d errors\n", errct);
748 exit(1);
749 }
750 }
751
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.