kernel: some boottime sanitychecks
[minix.git] / test / test7.c
blob978394995efc38efcaf510b95b6c592341737369
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 3
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 #include "common.c"
33 int subtes, xfd;
34 int whence = SEEK_SET, func_code = F_SETLK;
35 extern char **environ;
37 #define timed_test(func) (timed_test_func(#func, func));
39 int main(int argc, char *argv []);
40 void timed_test_func(const char *s, void (* func)(void));
41 void timed_test_timeout(int signum);
42 void test7a(void);
43 void test7b(void);
44 void test7c(void);
45 void test7d(void);
46 void test7e(void);
47 void test7f(void);
48 void test7g(void);
49 void test7h(void);
50 void test7i(void);
51 void test7j(void);
52 void cloexec_test(void);
53 int set(int how, int first, int last);
54 int locked(int b);
55 void sigfunc(int s);
57 int main(argc, argv)
58 int argc;
59 char *argv[];
62 int i, m = 0xFFFF;
64 if (argc == 2) m = atoi(argv[1]);
65 if (m == 0) cloexec_test(); /* important; do not remove this! */
67 start(7);
69 for (i = 0; i < ITERATIONS; i++) {
70 if (m & 00001) timed_test(test7a);
71 if (m & 00002) timed_test(test7b);
72 if (m & 00004) timed_test(test7c);
73 if (m & 00010) timed_test(test7d);
74 if (m & 00020) timed_test(test7e);
75 if (m & 00040) timed_test(test7f);
76 if (m & 00100) timed_test(test7g);
77 if (m & 00200) timed_test(test7h);
78 if (m & 00400) timed_test(test7i);
79 if (m & 01000) timed_test(test7j);
81 quit();
82 return(-1); /* impossible */
85 static jmp_buf timed_test_context;
87 void timed_test_timeout(int signum)
89 longjmp(timed_test_context, -1);
90 e(700);
91 quit();
92 exit(-1);
95 void timed_test_func(const char *s, void (* func)(void))
97 if (setjmp(timed_test_context) == 0)
99 /* the function gets 60 seconds to complete */
100 if (signal(SIGALRM, timed_test_timeout) == SIG_ERR) { e(701); return; }
101 alarm(60);
102 func();
103 alarm(0);
105 else
107 /* report timeout as error */
108 printf("timeout in %s\n", s);
109 e(702);
113 void test7a()
115 /* Test pipe(). */
117 int i, fd[2], ect;
118 char buf2[ITEMS+1];
120 /* Create a pipe, write on it, and read it back. */
121 subtest = 1;
122 if (pipe(fd) != 0) e(1);
123 if (write(fd[1], buf, ITEMS) != ITEMS) e(2);
124 buf2[0] = 0;
125 if (read(fd[0], buf2, ITEMS+1) != ITEMS) e(3);
126 ect = 0;
127 for (i = 0; i < ITEMS; i++) if (buf[i] != buf2[i]) ect++;
128 if (ect != 0) e(4);
129 if (close(fd[0]) != 0) e(5);
130 if (close(fd[1]) != 0) e(6);
132 /* Error test. Keep opening pipes until it fails. Check error code. */
133 errno = 0;
134 while (1) {
135 if (pipe(fd) < 0) break;
137 if (errno != EMFILE) e(7);
139 /* Close all the pipes. */
140 for (i = 3; i < OPEN_MAX; i++) close(i);
143 void test7b()
145 /* Test mkfifo(). */
147 int fdr, fdw, status;
148 char buf2[ITEMS+1];
149 int efork;
151 /* Create a fifo, write on it, and read it back. */
152 subtest = 2;
153 if (mkfifo("T7.b", 0777) != 0) e(1);
154 switch (fork()) {
155 case -1:
156 efork = errno;
157 printf("Fork failed: %s (%d)\n", strerror(efork), efork);
158 exit(1);
159 case 0:
160 /* Child reads from the fifo. */
161 if ( (fdr = open("T7.b", O_RDONLY)) < 0) e(5);
162 if (read(fdr, buf2, ITEMS+1) != ITEMS) e(6);
163 if (strcmp(buf, buf2) != 0) e(7);
164 if (close(fdr) != 0) e(8);
165 exit(0);
166 default:
167 /* Parent writes on the fifo. */
168 if ( (fdw = open("T7.b", O_WRONLY)) < 0) e(2);
169 if (write(fdw, buf, ITEMS) != ITEMS) e(3);
170 wait(&status);
171 if (close(fdw) != 0) e(4);
174 /* Check some error conditions. */
175 if (mkfifo("T7.b", 0777) != -1) e(9);
176 errno = 0;
177 if (mkfifo("a/b/c", 0777) != -1) e(10);
178 if (errno != ENOENT) e(11);
179 errno = 0;
180 if (mkfifo("", 0777) != -1) e(12);
181 if (errno != ENOENT) e(13);
182 errno = 0;
183 if (mkfifo("T7.b/x", 0777) != -1) e(14);
184 if (errno != ENOTDIR) e(15);
185 if (unlink("T7.b") != 0) e(16);
187 /* Now check fifos and the O_NONBLOCK flag. */
188 if (mkfifo("T7.b", 0600) != 0) e(17);
189 errno = 0;
190 if (open("T7.b", O_WRONLY | O_NONBLOCK) != -1) e(18);
191 if (errno != ENXIO) e(19);
192 if ( (fdr = open("T7.b", O_RDONLY | O_NONBLOCK)) < 0) e(20);
193 if (fork()) {
194 /* Parent reads from fdr. */
195 wait(&status); /* but first make sure writer has already run*/
196 if ( ( (status>>8) & 0377) != 77) e(21);
197 if (read(fdr, buf2, ITEMS+1) != ITEMS) e(22);
198 if (strcmp(buf, buf2) != 0) e(23);
199 if (close(fdr) != 0) e(24);
200 } else {
201 /* Child opens the fifo for writing and writes to it. */
202 if ( (fdw = open("T7.b", O_WRONLY | O_NONBLOCK)) < 0) e(25);
203 if (write(fdw, buf, ITEMS) != ITEMS) e(26);
204 if (close(fdw) != 0) e(27);
205 exit(77);
208 if (unlink("T7.b") != 0) e(28);
211 void test7c()
213 /* Test fcntl(). */
215 int fd, m, s, newfd, newfd2;
216 struct stat stat1, stat2, stat3;
218 subtest = 3;
219 errno = -100;
220 if ( (fd = creat("T7.c", 0777)) < 0) e(1);
222 /* Turn the per-file-descriptor flags on and off. */
223 if (fcntl(fd, F_GETFD) != 0) e(2); /* FD_CLOEXEC is initially off */
224 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(3);/* turn it on */
225 if (fcntl(fd, F_GETFD) != FD_CLOEXEC) e(4); /* should be on now */
226 if (fcntl(fd, F_SETFD, 0) != 0) e(5); /* turn it off */
227 if (fcntl(fd, F_GETFD) != 0) e(6); /* should be off now */
229 /* Turn the open-file-description flags on and off. Start with O_APPEND. */
230 m = O_WRONLY;
231 if (fcntl(fd, F_GETFL) != m) e(7); /* O_APPEND, O_NONBLOCK are off */
232 if (fcntl(fd, F_SETFL, O_APPEND) != 0) e(8); /* turn on O_APPEND */
233 if (fcntl(fd, F_GETFL) != (O_APPEND | m)) e(9);/* should be on now */
234 if (fcntl(fd, F_SETFL, 0) != 0) e(10); /* turn it off */
235 if (fcntl(fd, F_GETFL) != m) e(11); /* should be off now */
237 /* Turn the open-file-description flags on and off. Now try O_NONBLOCK. */
238 if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) e(12); /* turn on O_NONBLOCK */
239 if (fcntl(fd, F_GETFL) != (O_NONBLOCK | m)) e(13); /* should be on now */
240 if (fcntl(fd, F_SETFL, 0) != 0) e(14); /* turn it off */
241 if (fcntl(fd, F_GETFL) != m) e(15); /* should be off now */
243 /* Now both at once. */
244 if (fcntl(fd, F_SETFL, O_APPEND|O_NONBLOCK) != 0) e(16);
245 if (fcntl(fd, F_GETFL) != (O_NONBLOCK | O_APPEND | m)) e(17);
246 if (fcntl(fd, F_SETFL, 0) != 0) e(18);
247 if (fcntl(fd, F_GETFL) != m) e(19);
249 /* Now test F_DUPFD. */
250 if ( (newfd = fcntl(fd, F_DUPFD, 0)) != 4) e(20); /* 0-4 open */
251 if ( (newfd2 = fcntl(fd, F_DUPFD, 0)) != 5) e(21); /* 0-5 open */
252 if (close(newfd) != 0) e(22); /* 0-3, 5 open */
253 if ( (newfd = fcntl(fd, F_DUPFD, 0)) != 4) e(23); /* 0-5 open */
254 if (close(newfd) != 0) e(24); /* 0-3, 5 open */
255 if ( (newfd = fcntl(fd, F_DUPFD, 5)) != 6) e(25); /* 0-3, 5, 6 open */
256 if (close(newfd2) != 0) e(26); /* 0-3, 6 open */
258 /* O_APPEND should be inherited, but FD_CLOEXEC should be cleared. Check. */
259 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(26);/* turn FD_CLOEXEC on */
260 if (fcntl(fd, F_SETFL, O_APPEND) != 0) e(27); /* turn O_APPEND on */
261 if ( (newfd2 = fcntl(fd, F_DUPFD, 10)) != 10) e(28); /* 0-3, 6, 10 open */
262 if (fcntl(newfd2, F_GETFD) != 0) e(29); /* FD_CLOEXEC must be 0 */
263 if (fcntl(newfd2, F_GETFL) != (O_APPEND | m)) e(30); /* O_APPEND set */
264 if (fcntl(fd, F_SETFD, 0) != 0) e(31);/* turn FD_CLOEXEC off */
266 /* Check if newfd and newfd2 are the same inode. */
267 if (fstat(fd, &stat1) != 0) e(32);
268 if (fstat(fd, &stat2) != 0) e(33);
269 if (fstat(fd, &stat3) != 0) e(34);
270 if (stat1.st_dev != stat2.st_dev) e(35);
271 if (stat1.st_dev != stat3.st_dev) e(36);
272 if (stat1.st_ino != stat2.st_ino) e(37);
273 if (stat1.st_ino != stat3.st_ino) e(38);
275 /* Now check on the FD_CLOEXEC flag. Set it for fd (3) and newfd2 (10) */
276 if (fd != 3 || newfd2 != 10 || newfd != 6) e(39);
277 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(40); /* close 3 on exec */
278 if (fcntl(newfd2, F_SETFD, FD_CLOEXEC) != 0) e(41); /* close 10 on exec */
279 if (fcntl(newfd, F_SETFD, 0) != 0) e(42); /* don't close 6 */
280 if (fork()) {
281 wait(&s); /* parent just waits */
282 if (WEXITSTATUS(s) != 0) e(43);
283 } else {
284 execle("../test7", "test7", "0", (char *) 0, environ);
285 exit(1); /* the impossible never happens, right? */
288 /* Finally, close all the files. */
289 if (fcntl(fd, F_SETFD, 0) != 0) e(44); /* FD_CLOEXEC off */
290 if (fcntl(newfd2, F_SETFD, 0) != 0) e(45); /* FD_CLOEXEC off */
291 if (close(fd) != 0) e(46);
292 if (close(newfd) != 0) e(47);
293 if (close(newfd2) != 0) e(48);
296 void test7d()
298 /* Test file locking. */
300 subtest = 4;
302 if ( (xfd = creat("T7.d", 0777)) != 3) e(1);
303 close(xfd);
304 if ( (xfd = open("T7.d", O_RDWR)) < 0) e(2);
305 if (write(xfd, buf, ITEMS) != ITEMS) e(3);
306 if (set(WRITE, 0, 3) != 0) e(4);
307 if (set(WRITE, 5, 9) != 0) e(5);
308 if (set(UNLOCK, 0, 3) != 0) e(6);
309 if (set(UNLOCK, 4, 9) != 0) e(7);
311 if (set(READ, 1, 4) != 0) e(8);
312 if (set(READ, 4, 7) != 0) e(9);
313 if (set(UNLOCK, 4, 7) != 0) e(10);
314 if (set(UNLOCK, 1, 4) != 0) e(11);
316 if (set(WRITE, 0, 3) != 0) e(12);
317 if (set(WRITE, 5, 7) != 0) e(13);
318 if (set(WRITE, 9 ,10) != 0) e(14);
319 if (set(UNLOCK, 0, 4) != 0) e(15);
320 if (set(UNLOCK, 0, 7) != 0) e(16);
321 if (set(UNLOCK, 0, 2000) != 0) e(17);
323 if (set(WRITE, 0, 3) != 0) e(18);
324 if (set(WRITE, 5, 7) != 0) e(19);
325 if (set(WRITE, 9 ,10) != 0) e(20);
326 if (set(UNLOCK, 0, 100) != 0) e(21);
328 if (set(WRITE, 0, 9) != 0) e(22);
329 if (set(UNLOCK, 8, 9) != 0) e(23);
330 if (set(UNLOCK, 0, 2) != 0) e(24);
331 if (set(UNLOCK, 5, 5) != 0) e(25);
332 if (set(UNLOCK, 4, 6) != 0) e(26);
333 if (set(UNLOCK, 3, 3) != 0) e(27);
334 if (set(UNLOCK, 7, 7) != 0) e(28);
336 if (set(WRITE, 0, 10) != 0) e(29);
337 if (set(UNLOCK, 0, 1000) != 0) e(30);
339 /* Up until now, all locks have been disjoint. Now try conflicts. */
340 if (set(WRITE, 0, 4) != 0) e(31);
341 if (set(WRITE, 4, 7) != 0) e(32); /* owner may lock same byte twice */
342 if (set(WRITE, 5, 10) != 0) e(33);
343 if (set(UNLOCK, 0, 11) != 0) e(34);
345 /* File is now unlocked. Length 0 means whole file. */
346 if (set(WRITE, 2, 1) != 0) e(35); /* this locks whole file */
347 if (set(WRITE, 9,10) != 0) e(36); /* a process can relock its file */
348 if (set(WRITE, 3, 3) != 0) e(37);
349 if (set(UNLOCK, 0, -1) != 0) e(38); /* file is now unlocked. */
351 /* Test F_GETLK. */
352 if (set(WRITE, 2, 3) != 0) e(39);
353 if (locked(1) != U) e(40);
354 if (locked(2) != L) e(41);
355 if (locked(3) != L) e(42);
356 if (locked(4) != U) e(43);
357 if (set(UNLOCK, 2, 3) != 0) e(44);
358 if (locked(2) != U) e(45);
359 if (locked(3) != U) e(46);
361 close(xfd);
364 void test7e()
366 /* Test to see if SETLKW blocks as it should. */
368 int pid, s;
370 subtest = 5;
372 if ( (xfd = creat("T7.e", 0777)) != 3) e(1);
373 if (close(xfd) != 0) e(2);
374 if ( (xfd = open("T7.e", O_RDWR)) < 0) e(3);
375 if (write(xfd, buf, ITEMS) != ITEMS) e(4);
376 if (set(WRITE, 0, 3) != 0) e(5);
378 if ( (pid = fork()) ) {
379 /* Parent waits until child has started before signaling it. */
380 while (access("T7.e1", 0) != 0) ;
381 unlink("T7.e1");
382 sleep(1);
383 if (kill(pid, SIGKILL) < 0) e(6);
384 if (wait(&s) != pid) e(7);
385 } else {
386 /* Child tries to lock and should block. */
387 if (creat("T7.e1", 0777) < 0) e(8);
388 func_code = F_SETLKW;
389 if (set(WRITE, 0, 3) != 0) e(9); /* should block */
390 errno = -1000;
391 e(10); /* process should be killed by signal */
392 exit(0); /* should never happen */
394 close(xfd);
397 void test7f()
399 /* Test to see if SETLKW gives EINTR when interrupted. */
401 int pid, s;
403 subtest = 6;
405 if ( (xfd = creat("T7.f", 0777)) != 3) e(1);
406 if (close(xfd) != 0) e(2);
407 if ( (xfd = open("T7.f", O_RDWR)) < 0) e(3);
408 if (write(xfd, buf, ITEMS) != ITEMS) e(4);
409 if (set(WRITE, 0, 3) != 0) e(5);
411 if ( (pid = fork()) ) {
412 /* Parent waits until child has started before signaling it. */
413 while (access("T7.f1", 0) != 0) ;
414 unlink("T7.f1");
415 sleep(1);
416 if (kill(pid, SIGTERM) < 0) e(6);
417 if (wait(&s) != pid) e(7);
418 if ( (s>>8) != 19) e(8);
419 } else {
420 /* Child tries to lock and should block.
421 * `signal(SIGTERM, sigfunc);' to set the signal handler is inadequate
422 * because on systems like BSD the sigaction flags for signal include
423 * `SA_RESTART' so syscalls are restarted after they have been
424 * interrupted by a signal.
426 struct sigaction sa, osa;
428 sa.sa_handler = sigfunc;
429 sigemptyset(&sa.sa_mask);
430 sa.sa_flags = 0;
431 if (sigaction(SIGTERM, &sa, &osa) < 0) e(999);
432 if (creat("T7.f1", 0777) < 0) e(9);
433 func_code = F_SETLKW;
434 if (set(WRITE, 0, 3) != -1) e(10); /* should block */
435 if (errno != EINTR) e(11); /* signal should release it */
436 exit(19);
438 close(xfd);
441 void test7g()
443 /* Test to see if SETLKW unlocks when the needed lock becomes available. */
445 int pid, s;
447 subtest = 7;
449 if ( (xfd = creat("T7.g", 0777)) != 3) e(1);
450 if (close(xfd) != 0) e(2);
451 if ( (xfd = open("T7.g", O_RDWR)) < 0) e(3);
452 if (write(xfd, buf, ITEMS) != ITEMS) e(4);
453 if (set(WRITE, 0, 3) != 0) e(5); /* bytes 0 to 3 are now locked */
455 if ( (pid = fork()) ) {
456 /* Parent waits for child to start. */
457 while (access("T7.g1", 0) != 0) ;
458 unlink("T7.g1");
459 sleep(1);
460 if (set(UNLOCK, 0, 3) != 0) e(5);
461 if (wait(&s) != pid) e(6);
462 if ( (s >> 8) != 29) e(7);
463 } else {
464 /* Child tells parent it is alive, then tries to lock and is blocked.*/
465 func_code = F_SETLKW;
466 if (creat("T7.g1", 0777) < 0) e(8);
467 if (set(WRITE, 3, 3) != 0) e(9); /* process must block now */
468 if (set(UNLOCK, 3, 3) != 0) e(10);
469 exit(29);
471 close(xfd);
474 void test7h()
476 /* Test to see what happens if two processed block on the same lock. */
478 int pid, pid2, s, w;
480 subtest = 8;
482 if ( (xfd = creat("T7.h", 0777)) != 3) e(1);
483 if (close(xfd) != 0) e(2);
484 if ( (xfd = open("T7.h", O_RDWR)) < 0) e(3);
485 if (write(xfd, buf, ITEMS) != ITEMS) e(4);
486 if (set(WRITE, 0, 3) != 0) e(5); /* bytes 0 to 3 are now locked */
488 if ( (pid = fork()) ) {
489 if ( (pid2 = fork()) ) {
490 /* Parent waits for child to start. */
491 while (access("T7.h1", 0) != 0) ;
492 while (access("T7.h2", 0) != 0) ;
493 unlink("T7.h1");
494 unlink("T7.h2");
495 sleep(1);
496 if (set(UNLOCK, 0, 3) != 0) e(6);
497 w = wait(&s);
498 if (w != pid && w != pid2) e(7);
499 s = s >> 8;
500 if (s != 39 && s != 49) e(8);
501 w = wait(&s);
502 if (w != pid && w != pid2) e(9);
503 s = s >> 8;
504 if (s != 39 && s != 49) e(10);
505 } else {
506 func_code = F_SETLKW;
507 if (creat("T7.h1", 0777) < 0) e(11);
508 if (set(WRITE, 0, 0) != 0) e(12); /* block now */
509 if (set(UNLOCK, 0, 0) != 0) e(13);
510 exit(39);
512 } else {
513 /* Child tells parent it is alive, then tries to lock and is blocked.*/
514 func_code = F_SETLKW;
515 if (creat("T7.h2", 0777) < 0) e(14);
516 if (set(WRITE, 0, 1) != 0) e(15); /* process must block now */
517 if (set(UNLOCK, 0, 1) != 0) e(16);
518 exit(49);
520 close(xfd);
523 void test7i()
525 /* Check error conditions for fcntl(). */
527 int tfd, i;
529 subtest = 9;
531 errno = 0;
532 if ( (xfd = creat("T7.i", 0777)) != 3) e(1);
533 if (close(xfd) != 0) e(2);
534 if ( (xfd = open("T7.i", O_RDWR)) < 0) e(3);
535 if (write(xfd, buf, ITEMS) != ITEMS) e(4);
536 if (set(WRITE, 0, 3) != 0) e(5); /* bytes 0 to 3 are now locked */
537 if (set(WRITE, 0, 0) != 0) e(6);
538 if (errno != 0) e(7);
539 errno = 0;
540 if (set(WRITE, 3, 3) != 0) e(8);
541 if (errno != 0) e(9);
542 tfd = xfd; /* hold good value */
543 xfd = -99;
544 errno = 0;
545 if (set(WRITE, 0, 0) != -1) e(10);
546 if (errno != EBADF) e(11);
548 errno = 0;
549 if ( (xfd = open("T7.i", O_WRONLY)) < 0) e(12);
550 if (set(READ, 0, 0) != -1) e(13);
551 if (errno != EBADF) e(14);
552 if (close(xfd) != 0) e(15);
554 errno = 0;
555 if ( (xfd = open("T7.i", O_RDONLY)) < 0) e(16);
556 if (set(WRITE, 0, 0) != -1) e(17);
557 if (errno != EBADF) e(18);
558 if (close(xfd) != 0) e(19);
559 xfd = tfd; /* restore legal xfd value */
561 /* Check for EINVAL. */
562 errno = 0;
563 if (fcntl(xfd, F_DUPFD, OPEN_MAX) != -1) e(20);
564 if (errno != EINVAL) e(21);
565 errno = 0;
566 if (fcntl(xfd, F_DUPFD, -1) != -1) e(22);
567 if (errno != EINVAL) e(23);
569 xfd = 0; /* stdin does not support locking */
570 errno = 0;
571 if (set(READ, 0, 0) != -1) e(24);
572 if (errno != EINVAL) e(25);
573 xfd = tfd;
575 /* Check ENOLCK. */
576 for (i = 0; i < ITEMS; i++) {
577 if (set(WRITE, i, i) == 0) continue;
578 if (errno != ENOLCK) {
579 e(26);
580 break;
584 /* Check EMFILE. */
585 for (i = xfd + 1; i < OPEN_MAX; i++) open("T7.i", 0); /* use up all fds */
586 errno = 0;
587 if (fcntl(xfd, F_DUPFD, 0) != -1) e(27); /* No fds left */
588 if (errno != EMFILE) e(28);
590 for (i = xfd; i < OPEN_MAX; i++) if (close(i) != 0) e(29);
593 void test7j()
595 /* Test file locking with two processes. */
597 int s;
599 subtest = 10;
601 if ( (xfd = creat("T7.j", 0777)) != 3) e(1);
602 close(xfd);
603 if ( (xfd = open("T7.j", O_RDWR)) < 0) e(2);
604 if (write(xfd, buf, ITEMS) != ITEMS) e(3);
605 if (set(WRITE, 0, 4) != 0) e(4); /* lock belongs to parent */
606 if (set(READ, 10, 16) != 0) e(5); /* lock belongs to parent */
608 /* Up until now, all locks have been disjoint. Now try conflicts. */
609 if (fork()) {
610 /* Parent just waits for child to finish. */
611 wait(&s);
612 } else {
613 /* Child does the testing. */
614 errno = -100;
615 if (set(WRITE, 5, 7) < 0) e(6); /* should work */
616 if (set(WRITE, 4, 7) >= 0) e(7); /* child may not lock byte 4 */
617 if (errno != EACCES && errno != EAGAIN) e(8);
618 if (set(WRITE, 5, 9) != 0) e(9);
619 if (set(UNLOCK, 5, 9) != 0) e(10);
620 if (set(READ, 9, 17) < 0) e(11); /* shared READ lock is ok */
621 exit(0);
623 close(xfd);
626 void cloexec_test()
628 /* To text whether the FD_CLOEXEC flag actually causes files to be
629 * closed upon exec, we have to exec something. The test is carried
630 * out by forking, and then having the child exec test7 itself, but
631 * with argument 0. This is detected, and control comes here.
632 * File descriptors 3 and 10 should be closed here, and 10 open.
635 if (close(3) == 0) e(1001); /* close should fail; it was closed on exec */
636 if (close(6) != 0) e(1002); /* close should succeed */
637 if (close(10) == 0) e(1003); /* close should fail */
638 fflush(stdout);
639 exit(0);
642 int set(how, first, last)
643 int how, first, last;
645 int r;
646 struct flock flock;
648 if (how == READ) flock.l_type = F_RDLCK;
649 if (how == WRITE) flock.l_type = F_WRLCK;
650 if (how == UNLOCK) flock.l_type = F_UNLCK;
651 flock.l_whence = whence;
652 flock.l_start = (long) first;
653 flock.l_len = (long) last - (long) first + 1;
654 r = fcntl(xfd, func_code, &flock);
655 if (r != -1)
656 return(0);
657 else
658 return(-1);
661 int locked(b)
662 int b;
663 /* Test to see if byte b is locked. Return L or U */
665 struct flock flock;
666 pid_t pid;
667 int status;
669 flock.l_type = F_WRLCK;
670 flock.l_whence = whence;
671 flock.l_start = (long) b;
672 flock.l_len = 1;
674 /* Process' own locks are invisible to F_GETLK, so fork a child to test. */
675 pid = fork();
676 if (pid == 0) {
677 if (fcntl(xfd, F_GETLK, &flock) != 0) e(2000);
678 exit(flock.l_type == F_UNLCK ? U : L);
680 if (pid == -1) e(2001);
681 if (fcntl(xfd, F_GETLK, &flock) != 0) e(2002);
682 if (flock.l_type != F_UNLCK) e(2003);
683 if (wait(&status) != pid) e(2004);
684 if (!WIFEXITED(status)) e(2005);
685 return(WEXITSTATUS(status));
688 void sigfunc(s)
689 int s; /* for ANSI */