use oxpcie only if enabled to avoid baud bottleneck of uart.
[minix.git] / test / test7.c
blob80b0cc73151b06c9d8715253b115569c2fd9af27
1 /* POSIX test program (7). Author: Andy Tanenbaum */
3 /* The following POSIX calls are tested:
4 * pipe(), mkfifo(), fcntl()
5 */
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <sys/wait.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <limits.h>
13 #include <signal.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <setjmp.h>
20 #define ITERATIONS 4
21 #define MAX_ERROR 4
22 #define ITEMS 32
23 #define READ 10
24 #define WRITE 20
25 #define UNLOCK 30
26 #define U 70
27 #define L 80
29 char buf[ITEMS] = {0,1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1,0,1,2,3,4,5,6,7,8,9};
31 int subtest, errct, xfd;
32 int whence = SEEK_SET, func_code = F_SETLK;
33 extern char **environ;
35 #define timed_test(func) (timed_test_func(#func, func));
37 _PROTOTYPE(int main, (int argc, char *argv []));
38 _PROTOTYPE(void timed_test_func, (const char *s, void (* func)(void)));
39 _PROTOTYPE(void timed_test_timeout, (int signum));
40 _PROTOTYPE(void test7a, (void));
41 _PROTOTYPE(void test7b, (void));
42 _PROTOTYPE(void test7c, (void));
43 _PROTOTYPE(void test7d, (void));
44 _PROTOTYPE(void test7e, (void));
45 _PROTOTYPE(void test7f, (void));
46 _PROTOTYPE(void test7g, (void));
47 _PROTOTYPE(void test7h, (void));
48 _PROTOTYPE(void test7i, (void));
49 _PROTOTYPE(void test7j, (void));
50 _PROTOTYPE(void cloexec_test, (void));
51 _PROTOTYPE(int set, (int how, int first, int last));
52 _PROTOTYPE(int locked, (int b));
53 _PROTOTYPE(void e, (int n));
54 _PROTOTYPE(void sigfunc, (int s));
55 _PROTOTYPE(void quit, (void));
57 int main(argc, argv)
58 int argc;
59 char *argv[];
62 int i, m = 0xFFFF;
64 sync();
66 if (argc == 2) m = atoi(argv[1]);
67 if (m == 0) cloexec_test(); /* important; do not remove this! */
68 printf("Test 7 ");
69 fflush(stdout);
71 system("rm -rf DIR_07; mkdir DIR_07");
72 chdir("DIR_07");
74 for (i = 0; i < ITERATIONS; i++) {
75 if (m & 00001) timed_test(test7a);
76 if (m & 00002) timed_test(test7b);
77 if (m & 00004) timed_test(test7c);
78 if (m & 00010) timed_test(test7d);
79 if (m & 00020) timed_test(test7e);
80 if (m & 00040) timed_test(test7f);
81 if (m & 00100) timed_test(test7g);
82 if (m & 00200) timed_test(test7h);
83 if (m & 00400) timed_test(test7i);
84 if (m & 01000) timed_test(test7j);
86 quit();
87 return(-1); /* impossible */
90 static jmp_buf timed_test_context;
92 void timed_test_timeout(int signum)
94 longjmp(timed_test_context, -1);
95 e(700);
96 quit();
97 exit(-1);
100 void timed_test_func(const char *s, void (* func)(void))
102 if (setjmp(timed_test_context) == 0)
104 /* the function gets 60 seconds to complete */
105 if (signal(SIGALRM, timed_test_timeout) == SIG_ERR) { e(701); return; }
106 alarm(60);
107 func();
108 alarm(0);
110 else
112 /* report timeout as error */
113 printf("timeout in %s\n", s);
114 e(702);
118 void test7a()
120 /* Test pipe(). */
122 int i, fd[2], ect;
123 char buf2[ITEMS+1];
125 /* Create a pipe, write on it, and read it back. */
126 subtest = 1;
127 if (pipe(fd) != 0) e(1);
128 if (write(fd[1], buf, ITEMS) != ITEMS) e(2);
129 buf2[0] = 0;
130 if (read(fd[0], buf2, ITEMS+1) != ITEMS) e(3);
131 ect = 0;
132 for (i = 0; i < ITEMS; i++) if (buf[i] != buf2[i]) ect++;
133 if (ect != 0) e(4);
134 if (close(fd[0]) != 0) e(5);
135 if (close(fd[1]) != 0) e(6);
137 /* Error test. Keep opening pipes until it fails. Check error code. */
138 errno = 0;
139 while (1) {
140 if (pipe(fd) < 0) break;
142 if (errno != EMFILE) e(7);
144 /* Close all the pipes. */
145 for (i = 3; i < OPEN_MAX; i++) close(i);
148 void test7b()
150 /* Test mkfifo(). */
152 int fdr, fdw, status;
153 char buf2[ITEMS+1];
154 int efork;
156 /* Create a fifo, write on it, and read it back. */
157 subtest = 2;
158 if (mkfifo("T7.b", 0777) != 0) e(1);
159 switch (fork()) {
160 case -1:
161 efork = errno;
162 printf("Fork failed: %s (%d)\n", strerror(efork), efork);
163 exit(1);
164 case 0:
165 /* Child reads from the fifo. */
166 if ( (fdr = open("T7.b", O_RDONLY)) < 0) e(5);
167 if (read(fdr, buf2, ITEMS+1) != ITEMS) e(6);
168 if (strcmp(buf, buf2) != 0) e(7);
169 if (close(fdr) != 0) e(8);
170 exit(0);
171 default:
172 /* Parent writes on the fifo. */
173 if ( (fdw = open("T7.b", O_WRONLY)) < 0) e(2);
174 if (write(fdw, buf, ITEMS) != ITEMS) e(3);
175 wait(&status);
176 if (close(fdw) != 0) e(4);
179 /* Check some error conditions. */
180 if (mkfifo("T7.b", 0777) != -1) e(9);
181 errno = 0;
182 if (mkfifo("a/b/c", 0777) != -1) e(10);
183 if (errno != ENOENT) e(11);
184 errno = 0;
185 if (mkfifo("", 0777) != -1) e(12);
186 if (errno != ENOENT) e(13);
187 errno = 0;
188 if (mkfifo("T7.b/x", 0777) != -1) e(14);
189 if (errno != ENOTDIR) e(15);
190 if (unlink("T7.b") != 0) e(16);
192 /* Now check fifos and the O_NONBLOCK flag. */
193 if (mkfifo("T7.b", 0600) != 0) e(17);
194 errno = 0;
195 if (open("T7.b", O_WRONLY | O_NONBLOCK) != -1) e(18);
196 if (errno != ENXIO) e(19);
197 if ( (fdr = open("T7.b", O_RDONLY | O_NONBLOCK)) < 0) e(20);
198 if (fork()) {
199 /* Parent reads from fdr. */
200 wait(&status); /* but first make sure writer has already run*/
201 if ( ( (status>>8) & 0377) != 77) e(21);
202 if (read(fdr, buf2, ITEMS+1) != ITEMS) e(22);
203 if (strcmp(buf, buf2) != 0) e(23);
204 if (close(fdr) != 0) e(24);
205 } else {
206 /* Child opens the fifo for writing and writes to it. */
207 if ( (fdw = open("T7.b", O_WRONLY | O_NONBLOCK)) < 0) e(25);
208 if (write(fdw, buf, ITEMS) != ITEMS) e(26);
209 if (close(fdw) != 0) e(27);
210 exit(77);
213 if (unlink("T7.b") != 0) e(28);
216 void test7c()
218 /* Test fcntl(). */
220 int fd, m, s, newfd, newfd2;
221 struct stat stat1, stat2, stat3;
223 subtest = 3;
224 errno = -100;
225 if ( (fd = creat("T7.c", 0777)) < 0) e(1);
227 /* Turn the per-file-descriptor flags on and off. */
228 if (fcntl(fd, F_GETFD) != 0) e(2); /* FD_CLOEXEC is initially off */
229 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(3);/* turn it on */
230 if (fcntl(fd, F_GETFD) != FD_CLOEXEC) e(4); /* should be on now */
231 if (fcntl(fd, F_SETFD, 0) != 0) e(5); /* turn it off */
232 if (fcntl(fd, F_GETFD) != 0) e(6); /* should be off now */
234 /* Turn the open-file-description flags on and off. Start with O_APPEND. */
235 m = O_WRONLY;
236 if (fcntl(fd, F_GETFL) != m) e(7); /* O_APPEND, O_NONBLOCK are off */
237 if (fcntl(fd, F_SETFL, O_APPEND) != 0) e(8); /* turn on O_APPEND */
238 if (fcntl(fd, F_GETFL) != (O_APPEND | m)) e(9);/* should be on now */
239 if (fcntl(fd, F_SETFL, 0) != 0) e(10); /* turn it off */
240 if (fcntl(fd, F_GETFL) != m) e(11); /* should be off now */
242 /* Turn the open-file-description flags on and off. Now try O_NONBLOCK. */
243 if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) e(12); /* turn on O_NONBLOCK */
244 if (fcntl(fd, F_GETFL) != (O_NONBLOCK | m)) e(13); /* should be on now */
245 if (fcntl(fd, F_SETFL, 0) != 0) e(14); /* turn it off */
246 if (fcntl(fd, F_GETFL) != m) e(15); /* should be off now */
248 /* Now both at once. */
249 if (fcntl(fd, F_SETFL, O_APPEND|O_NONBLOCK) != 0) e(16);
250 if (fcntl(fd, F_GETFL) != (O_NONBLOCK | O_APPEND | m)) e(17);
251 if (fcntl(fd, F_SETFL, 0) != 0) e(18);
252 if (fcntl(fd, F_GETFL) != m) e(19);
254 /* Now test F_DUPFD. */
255 if ( (newfd = fcntl(fd, F_DUPFD, 0)) != 4) e(20); /* 0-4 open */
256 if ( (newfd2 = fcntl(fd, F_DUPFD, 0)) != 5) e(21); /* 0-5 open */
257 if (close(newfd) != 0) e(22); /* 0-3, 5 open */
258 if ( (newfd = fcntl(fd, F_DUPFD, 0)) != 4) e(23); /* 0-5 open */
259 if (close(newfd) != 0) e(24); /* 0-3, 5 open */
260 if ( (newfd = fcntl(fd, F_DUPFD, 5)) != 6) e(25); /* 0-3, 5, 6 open */
261 if (close(newfd2) != 0) e(26); /* 0-3, 6 open */
263 /* O_APPEND should be inherited, but FD_CLOEXEC should be cleared. Check. */
264 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(26);/* turn FD_CLOEXEC on */
265 if (fcntl(fd, F_SETFL, O_APPEND) != 0) e(27); /* turn O_APPEND on */
266 if ( (newfd2 = fcntl(fd, F_DUPFD, 10)) != 10) e(28); /* 0-3, 6, 10 open */
267 if (fcntl(newfd2, F_GETFD) != 0) e(29); /* FD_CLOEXEC must be 0 */
268 if (fcntl(newfd2, F_GETFL) != (O_APPEND | m)) e(30); /* O_APPEND set */
269 if (fcntl(fd, F_SETFD, 0) != 0) e(31);/* turn FD_CLOEXEC off */
271 /* Check if newfd and newfd2 are the same inode. */
272 if (fstat(fd, &stat1) != 0) e(32);
273 if (fstat(fd, &stat2) != 0) e(33);
274 if (fstat(fd, &stat3) != 0) e(34);
275 if (stat1.st_dev != stat2.st_dev) e(35);
276 if (stat1.st_dev != stat3.st_dev) e(36);
277 if (stat1.st_ino != stat2.st_ino) e(37);
278 if (stat1.st_ino != stat3.st_ino) e(38);
280 /* Now check on the FD_CLOEXEC flag. Set it for fd (3) and newfd2 (10) */
281 if (fd != 3 || newfd2 != 10 || newfd != 6) e(39);
282 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(40); /* close 3 on exec */
283 if (fcntl(newfd2, F_SETFD, FD_CLOEXEC) != 0) e(41); /* close 10 on exec */
284 if (fcntl(newfd, F_SETFD, 0) != 0) e(42); /* don't close 6 */
285 if (fork()) {
286 wait(&s); /* parent just waits */
287 if (WEXITSTATUS(s) != 0) e(43);
288 } else {
289 execle("../test7", "test7", "0", (char *) 0, environ);
290 exit(1); /* the impossible never happens, right? */
293 /* Finally, close all the files. */
294 if (fcntl(fd, F_SETFD, 0) != 0) e(44); /* FD_CLOEXEC off */
295 if (fcntl(newfd2, F_SETFD, 0) != 0) e(45); /* FD_CLOEXEC off */
296 if (close(fd) != 0) e(46);
297 if (close(newfd) != 0) e(47);
298 if (close(newfd2) != 0) e(48);
301 void test7d()
303 /* Test file locking. */
305 subtest = 4;
307 if ( (xfd = creat("T7.d", 0777)) != 3) e(1);
308 close(xfd);
309 if ( (xfd = open("T7.d", O_RDWR)) < 0) e(2);
310 if (write(xfd, buf, ITEMS) != ITEMS) e(3);
311 if (set(WRITE, 0, 3) != 0) e(4);
312 if (set(WRITE, 5, 9) != 0) e(5);
313 if (set(UNLOCK, 0, 3) != 0) e(6);
314 if (set(UNLOCK, 4, 9) != 0) e(7);
316 if (set(READ, 1, 4) != 0) e(8);
317 if (set(READ, 4, 7) != 0) e(9);
318 if (set(UNLOCK, 4, 7) != 0) e(10);
319 if (set(UNLOCK, 1, 4) != 0) e(11);
321 if (set(WRITE, 0, 3) != 0) e(12);
322 if (set(WRITE, 5, 7) != 0) e(13);
323 if (set(WRITE, 9 ,10) != 0) e(14);
324 if (set(UNLOCK, 0, 4) != 0) e(15);
325 if (set(UNLOCK, 0, 7) != 0) e(16);
326 if (set(UNLOCK, 0, 2000) != 0) e(17);
328 if (set(WRITE, 0, 3) != 0) e(18);
329 if (set(WRITE, 5, 7) != 0) e(19);
330 if (set(WRITE, 9 ,10) != 0) e(20);
331 if (set(UNLOCK, 0, 100) != 0) e(21);
333 if (set(WRITE, 0, 9) != 0) e(22);
334 if (set(UNLOCK, 8, 9) != 0) e(23);
335 if (set(UNLOCK, 0, 2) != 0) e(24);
336 if (set(UNLOCK, 5, 5) != 0) e(25);
337 if (set(UNLOCK, 4, 6) != 0) e(26);
338 if (set(UNLOCK, 3, 3) != 0) e(27);
339 if (set(UNLOCK, 7, 7) != 0) e(28);
341 if (set(WRITE, 0, 10) != 0) e(29);
342 if (set(UNLOCK, 0, 1000) != 0) e(30);
344 /* Up until now, all locks have been disjoint. Now try conflicts. */
345 if (set(WRITE, 0, 4) != 0) e(31);
346 if (set(WRITE, 4, 7) != 0) e(32); /* owner may lock same byte twice */
347 if (set(WRITE, 5, 10) != 0) e(33);
348 if (set(UNLOCK, 0, 11) != 0) e(34);
350 /* File is now unlocked. Length 0 means whole file. */
351 if (set(WRITE, 2, 1) != 0) e(35); /* this locks whole file */
352 if (set(WRITE, 9,10) != 0) e(36); /* a process can relock its file */
353 if (set(WRITE, 3, 3) != 0) e(37);
354 if (set(UNLOCK, 0, -1) != 0) e(38); /* file is now unlocked. */
356 /* Test F_GETLK. */
357 if (set(WRITE, 2, 3) != 0) e(39);
358 if (locked(1) != U) e(40);
359 if (locked(2) != L) e(41);
360 if (locked(3) != L) e(42);
361 if (locked(4) != U) e(43);
362 if (set(UNLOCK, 2, 3) != 0) e(44);
363 if (locked(2) != U) e(45);
364 if (locked(3) != U) e(46);
366 close(xfd);
369 void test7e()
371 /* Test to see if SETLKW blocks as it should. */
373 int pid, s;
375 subtest = 5;
377 if ( (xfd = creat("T7.e", 0777)) != 3) e(1);
378 if (close(xfd) != 0) e(2);
379 if ( (xfd = open("T7.e", O_RDWR)) < 0) e(3);
380 if (write(xfd, buf, ITEMS) != ITEMS) e(4);
381 if (set(WRITE, 0, 3) != 0) e(5);
383 if ( (pid = fork()) ) {
384 /* Parent waits until child has started before signaling it. */
385 while (access("T7.e1", 0) != 0) ;
386 unlink("T7.e1");
387 sleep(1);
388 if (kill(pid, SIGKILL) < 0) e(6);
389 if (wait(&s) != pid) e(7);
390 } else {
391 /* Child tries to lock and should block. */
392 if (creat("T7.e1", 0777) < 0) e(8);
393 func_code = F_SETLKW;
394 if (set(WRITE, 0, 3) != 0) e(9); /* should block */
395 errno = -1000;
396 e(10); /* process should be killed by signal */
397 exit(0); /* should never happen */
399 close(xfd);
402 void test7f()
404 /* Test to see if SETLKW gives EINTR when interrupted. */
406 int pid, s;
408 subtest = 6;
410 if ( (xfd = creat("T7.f", 0777)) != 3) e(1);
411 if (close(xfd) != 0) e(2);
412 if ( (xfd = open("T7.f", O_RDWR)) < 0) e(3);
413 if (write(xfd, buf, ITEMS) != ITEMS) e(4);
414 if (set(WRITE, 0, 3) != 0) e(5);
416 if ( (pid = fork()) ) {
417 /* Parent waits until child has started before signaling it. */
418 while (access("T7.f1", 0) != 0) ;
419 unlink("T7.f1");
420 sleep(1);
421 if (kill(pid, SIGTERM) < 0) e(6);
422 if (wait(&s) != pid) e(7);
423 if ( (s>>8) != 19) e(8);
424 } else {
425 /* Child tries to lock and should block.
426 * `signal(SIGTERM, sigfunc);' to set the signal handler is inadequate
427 * because on systems like BSD the sigaction flags for signal include
428 * `SA_RESTART' so syscalls are restarted after they have been
429 * interrupted by a signal.
431 struct sigaction sa, osa;
433 sa.sa_handler = sigfunc;
434 sigemptyset(&sa.sa_mask);
435 sa.sa_flags = 0;
436 if (sigaction(SIGTERM, &sa, &osa) < 0) e(999);
437 if (creat("T7.f1", 0777) < 0) e(9);
438 func_code = F_SETLKW;
439 if (set(WRITE, 0, 3) != -1) e(10); /* should block */
440 if (errno != EINTR) e(11); /* signal should release it */
441 exit(19);
443 close(xfd);
446 void test7g()
448 /* Test to see if SETLKW unlocks when the needed lock becomes available. */
450 int pid, s;
452 subtest = 7;
454 if ( (xfd = creat("T7.g", 0777)) != 3) e(1);
455 if (close(xfd) != 0) e(2);
456 if ( (xfd = open("T7.g", O_RDWR)) < 0) e(3);
457 if (write(xfd, buf, ITEMS) != ITEMS) e(4);
458 if (set(WRITE, 0, 3) != 0) e(5); /* bytes 0 to 3 are now locked */
460 if ( (pid = fork()) ) {
461 /* Parent waits for child to start. */
462 while (access("T7.g1", 0) != 0) ;
463 unlink("T7.g1");
464 sleep(1);
465 if (set(UNLOCK, 0, 3) != 0) e(5);
466 if (wait(&s) != pid) e(6);
467 if ( (s >> 8) != 29) e(7);
468 } else {
469 /* Child tells parent it is alive, then tries to lock and is blocked.*/
470 func_code = F_SETLKW;
471 if (creat("T7.g1", 0777) < 0) e(8);
472 if (set(WRITE, 3, 3) != 0) e(9); /* process must block now */
473 if (set(UNLOCK, 3, 3) != 0) e(10);
474 exit(29);
476 close(xfd);
479 void test7h()
481 /* Test to see what happens if two processed block on the same lock. */
483 int pid, pid2, s, w;
485 subtest = 8;
487 if ( (xfd = creat("T7.h", 0777)) != 3) e(1);
488 if (close(xfd) != 0) e(2);
489 if ( (xfd = open("T7.h", O_RDWR)) < 0) e(3);
490 if (write(xfd, buf, ITEMS) != ITEMS) e(4);
491 if (set(WRITE, 0, 3) != 0) e(5); /* bytes 0 to 3 are now locked */
493 if ( (pid = fork()) ) {
494 if ( (pid2 = fork()) ) {
495 /* Parent waits for child to start. */
496 while (access("T7.h1", 0) != 0) ;
497 while (access("T7.h2", 0) != 0) ;
498 unlink("T7.h1");
499 unlink("T7.h2");
500 sleep(1);
501 if (set(UNLOCK, 0, 3) != 0) e(6);
502 w = wait(&s);
503 if (w != pid && w != pid2) e(7);
504 s = s >> 8;
505 if (s != 39 && s != 49) e(8);
506 w = wait(&s);
507 if (w != pid && w != pid2) e(9);
508 s = s >> 8;
509 if (s != 39 && s != 49) e(10);
510 } else {
511 func_code = F_SETLKW;
512 if (creat("T7.h1", 0777) < 0) e(11);
513 if (set(WRITE, 0, 0) != 0) e(12); /* block now */
514 if (set(UNLOCK, 0, 0) != 0) e(13);
515 exit(39);
517 } else {
518 /* Child tells parent it is alive, then tries to lock and is blocked.*/
519 func_code = F_SETLKW;
520 if (creat("T7.h2", 0777) < 0) e(14);
521 if (set(WRITE, 0, 1) != 0) e(15); /* process must block now */
522 if (set(UNLOCK, 0, 1) != 0) e(16);
523 exit(49);
525 close(xfd);
528 void test7i()
530 /* Check error conditions for fcntl(). */
532 int tfd, i;
534 subtest = 9;
536 errno = 0;
537 if ( (xfd = creat("T7.i", 0777)) != 3) e(1);
538 if (close(xfd) != 0) e(2);
539 if ( (xfd = open("T7.i", O_RDWR)) < 0) e(3);
540 if (write(xfd, buf, ITEMS) != ITEMS) e(4);
541 if (set(WRITE, 0, 3) != 0) e(5); /* bytes 0 to 3 are now locked */
542 if (set(WRITE, 0, 0) != 0) e(6);
543 if (errno != 0) e(7);
544 errno = 0;
545 if (set(WRITE, 3, 3) != 0) e(8);
546 if (errno != 0) e(9);
547 tfd = xfd; /* hold good value */
548 xfd = -99;
549 errno = 0;
550 if (set(WRITE, 0, 0) != -1) e(10);
551 if (errno != EBADF) e(11);
553 errno = 0;
554 if ( (xfd = open("T7.i", O_WRONLY)) < 0) e(12);
555 if (set(READ, 0, 0) != -1) e(13);
556 if (errno != EBADF) e(14);
557 if (close(xfd) != 0) e(15);
559 errno = 0;
560 if ( (xfd = open("T7.i", O_RDONLY)) < 0) e(16);
561 if (set(WRITE, 0, 0) != -1) e(17);
562 if (errno != EBADF) e(18);
563 if (close(xfd) != 0) e(19);
564 xfd = tfd; /* restore legal xfd value */
566 /* Check for EINVAL. */
567 errno = 0;
568 if (fcntl(xfd, F_DUPFD, OPEN_MAX) != -1) e(20);
569 if (errno != EINVAL) e(21);
570 errno = 0;
571 if (fcntl(xfd, F_DUPFD, -1) != -1) e(22);
572 if (errno != EINVAL) e(23);
574 xfd = 0; /* stdin does not support locking */
575 errno = 0;
576 if (set(READ, 0, 0) != -1) e(24);
577 if (errno != EINVAL) e(25);
578 xfd = tfd;
580 /* Check ENOLCK. */
581 for (i = 0; i < ITEMS; i++) {
582 if (set(WRITE, i, i) == 0) continue;
583 if (errno != ENOLCK) {
584 e(26);
585 break;
589 /* Check EMFILE. */
590 for (i = xfd + 1; i < OPEN_MAX; i++) open("T7.i", 0); /* use up all fds */
591 errno = 0;
592 if (fcntl(xfd, F_DUPFD, 0) != -1) e(27); /* No fds left */
593 if (errno != EMFILE) e(28);
595 for (i = xfd; i < OPEN_MAX; i++) if (close(i) != 0) e(29);
598 void test7j()
600 /* Test file locking with two processes. */
602 int s;
604 subtest = 10;
606 if ( (xfd = creat("T7.j", 0777)) != 3) e(1);
607 close(xfd);
608 if ( (xfd = open("T7.j", O_RDWR)) < 0) e(2);
609 if (write(xfd, buf, ITEMS) != ITEMS) e(3);
610 if (set(WRITE, 0, 4) != 0) e(4); /* lock belongs to parent */
611 if (set(READ, 10, 16) != 0) e(5); /* lock belongs to parent */
613 /* Up until now, all locks have been disjoint. Now try conflicts. */
614 if (fork()) {
615 /* Parent just waits for child to finish. */
616 wait(&s);
617 } else {
618 /* Child does the testing. */
619 errno = -100;
620 if (set(WRITE, 5, 7) < 0) e(6); /* should work */
621 if (set(WRITE, 4, 7) >= 0) e(7); /* child may not lock byte 4 */
622 if (errno != EACCES && errno != EAGAIN) e(8);
623 if (set(WRITE, 5, 9) != 0) e(9);
624 if (set(UNLOCK, 5, 9) != 0) e(10);
625 if (set(READ, 9, 17) < 0) e(11); /* shared READ lock is ok */
626 exit(0);
628 close(xfd);
631 void cloexec_test()
633 /* To text whether the FD_CLOEXEC flag actually causes files to be
634 * closed upon exec, we have to exec something. The test is carried
635 * out by forking, and then having the child exec test7 itself, but
636 * with argument 0. This is detected, and control comes here.
637 * File descriptors 3 and 10 should be closed here, and 10 open.
640 if (close(3) == 0) e(1001); /* close should fail; it was closed on exec */
641 if (close(6) != 0) e(1002); /* close should succeed */
642 if (close(10) == 0) e(1003); /* close should fail */
643 fflush(stdout);
644 exit(0);
647 int set(how, first, last)
648 int how, first, last;
650 int r;
651 struct flock flock;
653 if (how == READ) flock.l_type = F_RDLCK;
654 if (how == WRITE) flock.l_type = F_WRLCK;
655 if (how == UNLOCK) flock.l_type = F_UNLCK;
656 flock.l_whence = whence;
657 flock.l_start = (long) first;
658 flock.l_len = (long) last - (long) first + 1;
659 r = fcntl(xfd, func_code, &flock);
660 if (r != -1)
661 return(0);
662 else
663 return(-1);
666 int locked(b)
667 int b;
668 /* Test to see if byte b is locked. Return L or U */
670 struct flock flock;
671 pid_t pid;
672 int status;
674 flock.l_type = F_WRLCK;
675 flock.l_whence = whence;
676 flock.l_start = (long) b;
677 flock.l_len = 1;
679 /* Process' own locks are invisible to F_GETLK, so fork a child to test. */
680 pid = fork();
681 if (pid == 0) {
682 if (fcntl(xfd, F_GETLK, &flock) != 0) e(2000);
683 exit(flock.l_type == F_UNLCK ? U : L);
685 if (pid == -1) e(2001);
686 if (fcntl(xfd, F_GETLK, &flock) != 0) e(2002);
687 if (flock.l_type != F_UNLCK) e(2003);
688 if (wait(&status) != pid) e(2004);
689 if (!WIFEXITED(status)) e(2005);
690 return(WEXITSTATUS(status));
693 void e(n)
694 int n;
696 int err_num = errno; /* save errno in case printf clobbers it */
698 printf("Subtest %d, error %d errno=%d ", subtest, n, errno);
699 fflush(stdout);
700 errno = err_num; /* restore errno, just in case */
701 perror("");
702 if (errct++ > MAX_ERROR) {
703 printf("Too many errors; test aborted\n");
704 chdir("..");
705 system("rm -rf DIR*");
706 exit(1);
710 void sigfunc(s)
711 int s; /* for ANSI */
715 void quit()
718 chdir("..");
719 system("rm -rf DIR*");
721 if (errct == 0) {
722 printf("ok\n");
723 exit(0);
724 } else {
725 printf("%d errors\n", errct);
726 exit(1);