. service tells you which device it couldn't stat
[minix3.git] / test / test7.c
blob47d954ea38511ced66fe2b118b7eec254be40872
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>
19 #define ITERATIONS 4
20 #define MAX_ERROR 4
21 #define ITEMS 32
22 #define READ 10
23 #define WRITE 20
24 #define UNLOCK 30
25 #define U 70
26 #define L 80
28 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};
30 int subtest, errct, xfd;
31 int whence = SEEK_SET, func_code = F_SETLK;
32 extern char **environ;
34 _PROTOTYPE(int main, (int argc, char *argv []));
35 _PROTOTYPE(void test7a, (void));
36 _PROTOTYPE(void test7b, (void));
37 _PROTOTYPE(void test7c, (void));
38 _PROTOTYPE(void test7d, (void));
39 _PROTOTYPE(void test7e, (void));
40 _PROTOTYPE(void test7f, (void));
41 _PROTOTYPE(void test7g, (void));
42 _PROTOTYPE(void test7h, (void));
43 _PROTOTYPE(void test7i, (void));
44 _PROTOTYPE(void test7j, (void));
45 _PROTOTYPE(void cloexec_test, (void));
46 _PROTOTYPE(int set, (int how, int first, int last));
47 _PROTOTYPE(int locked, (int b));
48 _PROTOTYPE(void e, (int n));
49 _PROTOTYPE(void sigfunc, (int s));
50 _PROTOTYPE(void quit, (void));
52 int main(argc, argv)
53 int argc;
54 char *argv[];
57 int i, m = 0xFFFF;
59 sync();
61 if (argc == 2) m = atoi(argv[1]);
62 if (m == 0) cloexec_test(); /* important; do not remove this! */
63 printf("Test 7 ");
64 fflush(stdout);
66 system("rm -rf DIR_07; mkdir DIR_07");
67 chdir("DIR_07");
69 for (i = 0; i < ITERATIONS; i++) {
70 if (m & 00001) test7a();
71 if (m & 00002) test7b();
72 if (m & 00004) test7c();
73 if (m & 00010) test7d();
74 if (m & 00020) test7e();
75 if (m & 00040) test7f();
76 if (m & 00100) test7g();
77 if (m & 00200) test7h();
78 if (m & 00400) test7i();
79 if (m & 01000) test7j();
81 quit();
82 return(-1); /* impossible */
85 void test7a()
87 /* Test pipe(). */
89 int i, fd[2], ect;
90 char buf2[ITEMS+1];
92 /* Create a pipe, write on it, and read it back. */
93 subtest = 1;
94 if (pipe(fd) != 0) e(1);
95 if (write(fd[1], buf, ITEMS) != ITEMS) e(2);
96 buf2[0] = 0;
97 if (read(fd[0], buf2, ITEMS+1) != ITEMS) e(3);
98 ect = 0;
99 for (i = 0; i < ITEMS; i++) if (buf[i] != buf2[i]) ect++;
100 if (ect != 0) e(4);
101 if (close(fd[0]) != 0) e(5);
102 if (close(fd[1]) != 0) e(6);
104 /* Error test. Keep opening pipes until it fails. Check error code. */
105 errno = 0;
106 while (1) {
107 if (pipe(fd) < 0) break;
109 if (errno != EMFILE) e(7);
111 /* Close all the pipes. */
112 for (i = 3; i < OPEN_MAX; i++) close(i);
115 void test7b()
117 /* Test mkfifo(). */
119 int fdr, fdw, status;
120 char buf2[ITEMS+1];
121 int efork;
123 /* Create a fifo, write on it, and read it back. */
124 subtest = 2;
125 if (mkfifo("T7.b", 0777) != 0) e(1);
126 switch (fork()) {
127 case -1:
128 efork = errno;
129 printf("Fork failed: %s (%d)\n", strerror(efork), efork);
130 exit(1);
131 case 0:
132 /* Child reads from the fifo. */
133 if ( (fdr = open("T7.b", O_RDONLY)) < 0) e(5);
134 if (read(fdr, buf2, ITEMS+1) != ITEMS) e(6);
135 if (strcmp(buf, buf2) != 0) e(7);
136 if (close(fdr) != 0) e(8);
137 exit(0);
138 default:
139 /* Parent writes on the fifo. */
140 if ( (fdw = open("T7.b", O_WRONLY)) < 0) e(2);
141 if (write(fdw, buf, ITEMS) != ITEMS) e(3);
142 wait(&status);
143 if (close(fdw) != 0) e(4);
146 /* Check some error conditions. */
147 if (mkfifo("T7.b", 0777) != -1) e(9);
148 errno = 0;
149 if (mkfifo("a/b/c", 0777) != -1) e(10);
150 if (errno != ENOENT) e(11);
151 errno = 0;
152 if (mkfifo("", 0777) != -1) e(12);
153 if (errno != ENOENT) e(13);
154 errno = 0;
155 if (mkfifo("T7.b/x", 0777) != -1) e(14);
156 if (errno != ENOTDIR) e(15);
157 if (unlink("T7.b") != 0) e(16);
159 /* Now check fifos and the O_NONBLOCK flag. */
160 if (mkfifo("T7.b", 0600) != 0) e(17);
161 errno = 0;
162 if (open("T7.b", O_WRONLY | O_NONBLOCK) != -1) e(18);
163 if (errno != ENXIO) e(19);
164 if ( (fdr = open("T7.b", O_RDONLY | O_NONBLOCK)) < 0) e(20);
165 if (fork()) {
166 /* Parent reads from fdr. */
167 wait(&status); /* but first make sure writer has already run*/
168 if ( ( (status>>8) & 0377) != 77) e(21);
169 if (read(fdr, buf2, ITEMS+1) != ITEMS) e(22);
170 if (strcmp(buf, buf2) != 0) e(23);
171 if (close(fdr) != 0) e(24);
172 } else {
173 /* Child opens the fifo for writing and writes to it. */
174 if ( (fdw = open("T7.b", O_WRONLY | O_NONBLOCK)) < 0) e(25);
175 if (write(fdw, buf, ITEMS) != ITEMS) e(26);
176 if (close(fdw) != 0) e(27);
177 exit(77);
180 if (unlink("T7.b") != 0) e(28);
183 void test7c()
185 /* Test fcntl(). */
187 int fd, m, s, newfd, newfd2;
188 struct stat stat1, stat2, stat3;
190 subtest = 3;
191 errno = -100;
192 if ( (fd = creat("T7.c", 0777)) < 0) e(1);
194 /* Turn the per-file-descriptor flags on and off. */
195 if (fcntl(fd, F_GETFD) != 0) e(2); /* FD_CLOEXEC is initially off */
196 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(3);/* turn it on */
197 if (fcntl(fd, F_GETFD) != FD_CLOEXEC) e(4); /* should be on now */
198 if (fcntl(fd, F_SETFD, 0) != 0) e(5); /* turn it off */
199 if (fcntl(fd, F_GETFD) != 0) e(6); /* should be off now */
201 /* Turn the open-file-description flags on and off. Start with O_APPEND. */
202 m = O_WRONLY;
203 if (fcntl(fd, F_GETFL) != m) e(7); /* O_APPEND, O_NONBLOCK are off */
204 if (fcntl(fd, F_SETFL, O_APPEND) != 0) e(8); /* turn on O_APPEND */
205 if (fcntl(fd, F_GETFL) != (O_APPEND | m)) e(9);/* should be on now */
206 if (fcntl(fd, F_SETFL, 0) != 0) e(10); /* turn it off */
207 if (fcntl(fd, F_GETFL) != m) e(11); /* should be off now */
209 /* Turn the open-file-description flags on and off. Now try O_NONBLOCK. */
210 if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) e(12); /* turn on O_NONBLOCK */
211 if (fcntl(fd, F_GETFL) != (O_NONBLOCK | m)) e(13); /* should be on now */
212 if (fcntl(fd, F_SETFL, 0) != 0) e(14); /* turn it off */
213 if (fcntl(fd, F_GETFL) != m) e(15); /* should be off now */
215 /* Now both at once. */
216 if (fcntl(fd, F_SETFL, O_APPEND|O_NONBLOCK) != 0) e(16);
217 if (fcntl(fd, F_GETFL) != (O_NONBLOCK | O_APPEND | m)) e(17);
218 if (fcntl(fd, F_SETFL, 0) != 0) e(18);
219 if (fcntl(fd, F_GETFL) != m) e(19);
221 /* Now test F_DUPFD. */
222 if ( (newfd = fcntl(fd, F_DUPFD, 0)) != 4) e(20); /* 0-4 open */
223 if ( (newfd2 = fcntl(fd, F_DUPFD, 0)) != 5) e(21); /* 0-5 open */
224 if (close(newfd) != 0) e(22); /* 0-3, 5 open */
225 if ( (newfd = fcntl(fd, F_DUPFD, 0)) != 4) e(23); /* 0-5 open */
226 if (close(newfd) != 0) e(24); /* 0-3, 5 open */
227 if ( (newfd = fcntl(fd, F_DUPFD, 5)) != 6) e(25); /* 0-3, 5, 6 open */
228 if (close(newfd2) != 0) e(26); /* 0-3, 6 open */
230 /* O_APPEND should be inherited, but FD_CLOEXEC should be cleared. Check. */
231 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(26);/* turn FD_CLOEXEC on */
232 if (fcntl(fd, F_SETFL, O_APPEND) != 0) e(27); /* turn O_APPEND on */
233 if ( (newfd2 = fcntl(fd, F_DUPFD, 10)) != 10) e(28); /* 0-3, 6, 10 open */
234 if (fcntl(newfd2, F_GETFD) != 0) e(29); /* FD_CLOEXEC must be 0 */
235 if (fcntl(newfd2, F_GETFL) != (O_APPEND | m)) e(30); /* O_APPEND set */
236 if (fcntl(fd, F_SETFD, 0) != 0) e(31);/* turn FD_CLOEXEC off */
238 /* Check if newfd and newfd2 are the same inode. */
239 if (fstat(fd, &stat1) != 0) e(32);
240 if (fstat(fd, &stat2) != 0) e(33);
241 if (fstat(fd, &stat3) != 0) e(34);
242 if (stat1.st_dev != stat2.st_dev) e(35);
243 if (stat1.st_dev != stat3.st_dev) e(36);
244 if (stat1.st_ino != stat2.st_ino) e(37);
245 if (stat1.st_ino != stat3.st_ino) e(38);
247 /* Now check on the FD_CLOEXEC flag. Set it for fd (3) and newfd2 (10) */
248 if (fd != 3 || newfd2 != 10 || newfd != 6) e(39);
249 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(40); /* close 3 on exec */
250 if (fcntl(newfd2, F_SETFD, FD_CLOEXEC) != 0) e(41); /* close 10 on exec */
251 if (fcntl(newfd, F_SETFD, 0) != 0) e(42); /* don't close 6 */
252 if (fork()) {
253 wait(&s); /* parent just waits */
254 if (WEXITSTATUS(s) != 0) e(43);
255 } else {
256 execle("../test7", "test7", "0", (char *) 0, environ);
257 exit(1); /* the impossible never happens, right? */
260 /* Finally, close all the files. */
261 if (fcntl(fd, F_SETFD, 0) != 0) e(44); /* FD_CLOEXEC off */
262 if (fcntl(newfd2, F_SETFD, 0) != 0) e(45); /* FD_CLOEXEC off */
263 if (close(fd) != 0) e(46);
264 if (close(newfd) != 0) e(47);
265 if (close(newfd2) != 0) e(48);
268 void test7d()
270 /* Test file locking. */
272 subtest = 4;
274 if ( (xfd = creat("T7.d", 0777)) != 3) e(1);
275 close(xfd);
276 if ( (xfd = open("T7.d", O_RDWR)) < 0) e(2);
277 if (write(xfd, buf, ITEMS) != ITEMS) e(3);
278 if (set(WRITE, 0, 3) != 0) e(4);
279 if (set(WRITE, 5, 9) != 0) e(5);
280 if (set(UNLOCK, 0, 3) != 0) e(6);
281 if (set(UNLOCK, 4, 9) != 0) e(7);
283 if (set(READ, 1, 4) != 0) e(8);
284 if (set(READ, 4, 7) != 0) e(9);
285 if (set(UNLOCK, 4, 7) != 0) e(10);
286 if (set(UNLOCK, 1, 4) != 0) e(11);
288 if (set(WRITE, 0, 3) != 0) e(12);
289 if (set(WRITE, 5, 7) != 0) e(13);
290 if (set(WRITE, 9 ,10) != 0) e(14);
291 if (set(UNLOCK, 0, 4) != 0) e(15);
292 if (set(UNLOCK, 0, 7) != 0) e(16);
293 if (set(UNLOCK, 0, 2000) != 0) e(17);
295 if (set(WRITE, 0, 3) != 0) e(18);
296 if (set(WRITE, 5, 7) != 0) e(19);
297 if (set(WRITE, 9 ,10) != 0) e(20);
298 if (set(UNLOCK, 0, 100) != 0) e(21);
300 if (set(WRITE, 0, 9) != 0) e(22);
301 if (set(UNLOCK, 8, 9) != 0) e(23);
302 if (set(UNLOCK, 0, 2) != 0) e(24);
303 if (set(UNLOCK, 5, 5) != 0) e(25);
304 if (set(UNLOCK, 4, 6) != 0) e(26);
305 if (set(UNLOCK, 3, 3) != 0) e(27);
306 if (set(UNLOCK, 7, 7) != 0) e(28);
308 if (set(WRITE, 0, 10) != 0) e(29);
309 if (set(UNLOCK, 0, 1000) != 0) e(30);
311 /* Up until now, all locks have been disjoint. Now try conflicts. */
312 if (set(WRITE, 0, 4) != 0) e(31);
313 if (set(WRITE, 4, 7) != 0) e(32); /* owner may lock same byte twice */
314 if (set(WRITE, 5, 10) != 0) e(33);
315 if (set(UNLOCK, 0, 11) != 0) e(34);
317 /* File is now unlocked. Length 0 means whole file. */
318 if (set(WRITE, 2, 1) != 0) e(35); /* this locks whole file */
319 if (set(WRITE, 9,10) != 0) e(36); /* a process can relock its file */
320 if (set(WRITE, 3, 3) != 0) e(37);
321 if (set(UNLOCK, 0, -1) != 0) e(38); /* file is now unlocked. */
323 /* Test F_GETLK. */
324 if (set(WRITE, 2, 3) != 0) e(39);
325 if (locked(1) != U) e(40);
326 if (locked(2) != L) e(41);
327 if (locked(3) != L) e(42);
328 if (locked(4) != U) e(43);
329 if (set(UNLOCK, 2, 3) != 0) e(44);
330 if (locked(2) != U) e(45);
331 if (locked(3) != U) e(46);
333 close(xfd);
336 void test7e()
338 /* Test to see if SETLKW blocks as it should. */
340 int pid, s;
342 subtest = 5;
344 if ( (xfd = creat("T7.e", 0777)) != 3) e(1);
345 if (close(xfd) != 0) e(2);
346 if ( (xfd = open("T7.e", O_RDWR)) < 0) e(3);
347 if (write(xfd, buf, ITEMS) != ITEMS) e(4);
348 if (set(WRITE, 0, 3) != 0) e(5);
350 if ( (pid = fork()) ) {
351 /* Parent waits until child has started before signaling it. */
352 while (access("T7.e1", 0) != 0) ;
353 unlink("T7.e1");
354 sleep(1);
355 if (kill(pid, SIGKILL) < 0) e(6);
356 if (wait(&s) != pid) e(7);
357 } else {
358 /* Child tries to lock and should block. */
359 if (creat("T7.e1", 0777) < 0) e(8);
360 func_code = F_SETLKW;
361 if (set(WRITE, 0, 3) != 0) e(9); /* should block */
362 errno = -1000;
363 e(10); /* process should be killed by signal */
364 exit(0); /* should never happen */
366 close(xfd);
369 void test7f()
371 /* Test to see if SETLKW gives EINTR when interrupted. */
373 int pid, s;
375 subtest = 6;
377 if ( (xfd = creat("T7.f", 0777)) != 3) e(1);
378 if (close(xfd) != 0) e(2);
379 if ( (xfd = open("T7.f", 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.f1", 0) != 0) ;
386 unlink("T7.f1");
387 sleep(1);
388 if (kill(pid, SIGTERM) < 0) e(6);
389 if (wait(&s) != pid) e(7);
390 if ( (s>>8) != 19) e(8);
391 } else {
392 /* Child tries to lock and should block.
393 * `signal(SIGTERM, sigfunc);' to set the signal handler is inadequate
394 * because on systems like BSD the sigaction flags for signal include
395 * `SA_RESTART' so syscalls are restarted after they have been
396 * interrupted by a signal.
398 struct sigaction sa, osa;
400 sa.sa_handler = sigfunc;
401 sigemptyset(&sa.sa_mask);
402 sa.sa_flags = 0;
403 if (sigaction(SIGTERM, &sa, &osa) < 0) e(999);
404 if (creat("T7.f1", 0777) < 0) e(9);
405 func_code = F_SETLKW;
406 if (set(WRITE, 0, 3) != -1) e(10); /* should block */
407 if (errno != EINTR) e(11); /* signal should release it */
408 exit(19);
410 close(xfd);
413 void test7g()
415 /* Test to see if SETLKW unlocks when the needed lock becomes available. */
417 int pid, s;
419 subtest = 7;
421 if ( (xfd = creat("T7.g", 0777)) != 3) e(1);
422 if (close(xfd) != 0) e(2);
423 if ( (xfd = open("T7.g", O_RDWR)) < 0) e(3);
424 if (write(xfd, buf, ITEMS) != ITEMS) e(4);
425 if (set(WRITE, 0, 3) != 0) e(5); /* bytes 0 to 3 are now locked */
427 if ( (pid = fork()) ) {
428 /* Parent waits for child to start. */
429 while (access("T7.g1", 0) != 0) ;
430 unlink("T7.g1");
431 sleep(1);
432 if (set(UNLOCK, 0, 3) != 0) e(5);
433 if (wait(&s) != pid) e(6);
434 if ( (s >> 8) != 29) e(7);
435 } else {
436 /* Child tells parent it is alive, then tries to lock and is blocked.*/
437 func_code = F_SETLKW;
438 if (creat("T7.g1", 0777) < 0) e(8);
439 if (set(WRITE, 3, 3) != 0) e(9); /* process must block now */
440 if (set(UNLOCK, 3, 3) != 0) e(10);
441 exit(29);
443 close(xfd);
446 void test7h()
448 /* Test to see what happens if two processed block on the same lock. */
450 int pid, pid2, s, w;
452 subtest = 8;
454 if ( (xfd = creat("T7.h", 0777)) != 3) e(1);
455 if (close(xfd) != 0) e(2);
456 if ( (xfd = open("T7.h", 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 if ( (pid2 = fork()) ) {
462 /* Parent waits for child to start. */
463 while (access("T7.h1", 0) != 0) ;
464 while (access("T7.h2", 0) != 0) ;
465 unlink("T7.h1");
466 unlink("T7.h2");
467 sleep(1);
468 if (set(UNLOCK, 0, 3) != 0) e(6);
469 w = wait(&s);
470 if (w != pid && w != pid2) e(7);
471 s = s >> 8;
472 if (s != 39 && s != 49) e(8);
473 w = wait(&s);
474 if (w != pid && w != pid2) e(9);
475 s = s >> 8;
476 if (s != 39 && s != 49) e(10);
477 } else {
478 func_code = F_SETLKW;
479 if (creat("T7.h1", 0777) < 0) e(11);
480 if (set(WRITE, 0, 0) != 0) e(12); /* block now */
481 if (set(UNLOCK, 0, 0) != 0) e(13);
482 exit(39);
484 } else {
485 /* Child tells parent it is alive, then tries to lock and is blocked.*/
486 func_code = F_SETLKW;
487 if (creat("T7.h2", 0777) < 0) e(14);
488 if (set(WRITE, 0, 1) != 0) e(15); /* process must block now */
489 if (set(UNLOCK, 0, 1) != 0) e(16);
490 exit(49);
492 close(xfd);
495 void test7i()
497 /* Check error conditions for fcntl(). */
499 int tfd, i;
501 subtest = 9;
503 errno = 0;
504 if ( (xfd = creat("T7.i", 0777)) != 3) e(1);
505 if (close(xfd) != 0) e(2);
506 if ( (xfd = open("T7.i", O_RDWR)) < 0) e(3);
507 if (write(xfd, buf, ITEMS) != ITEMS) e(4);
508 if (set(WRITE, 0, 3) != 0) e(5); /* bytes 0 to 3 are now locked */
509 if (set(WRITE, 0, 0) != 0) e(6);
510 if (errno != 0) e(7);
511 errno = 0;
512 if (set(WRITE, 3, 3) != 0) e(8);
513 if (errno != 0) e(9);
514 tfd = xfd; /* hold good value */
515 xfd = -99;
516 errno = 0;
517 if (set(WRITE, 0, 0) != -1) e(10);
518 if (errno != EBADF) e(11);
520 errno = 0;
521 if ( (xfd = open("T7.i", O_WRONLY)) < 0) e(12);
522 if (set(READ, 0, 0) != -1) e(13);
523 if (errno != EBADF) e(14);
524 if (close(xfd) != 0) e(15);
526 errno = 0;
527 if ( (xfd = open("T7.i", O_RDONLY)) < 0) e(16);
528 if (set(WRITE, 0, 0) != -1) e(17);
529 if (errno != EBADF) e(18);
530 if (close(xfd) != 0) e(19);
531 xfd = tfd; /* restore legal xfd value */
533 /* Check for EINVAL. */
534 errno = 0;
535 if (fcntl(xfd, F_DUPFD, OPEN_MAX) != -1) e(20);
536 if (errno != EINVAL) e(21);
537 errno = 0;
538 if (fcntl(xfd, F_DUPFD, -1) != -1) e(22);
539 if (errno != EINVAL) e(23);
541 xfd = 0; /* stdin does not support locking */
542 errno = 0;
543 if (set(READ, 0, 0) != -1) e(24);
544 if (errno != EINVAL) e(25);
545 xfd = tfd;
547 /* Check ENOLCK. */
548 for (i = 0; i < ITEMS; i++) {
549 if (set(WRITE, i, i) == 0) continue;
550 if (errno != ENOLCK) {
551 e(26);
552 break;
556 /* Check EMFILE. */
557 for (i = xfd + 1; i < OPEN_MAX; i++) open("T7.i", 0); /* use up all fds */
558 errno = 0;
559 if (fcntl(xfd, F_DUPFD, 0) != -1) e(27); /* No fds left */
560 if (errno != EMFILE) e(28);
562 for (i = xfd; i < OPEN_MAX; i++) if (close(i) != 0) e(29);
565 void test7j()
567 /* Test file locking with two processes. */
569 int s;
571 subtest = 10;
573 if ( (xfd = creat("T7.j", 0777)) != 3) e(1);
574 close(xfd);
575 if ( (xfd = open("T7.j", O_RDWR)) < 0) e(2);
576 if (write(xfd, buf, ITEMS) != ITEMS) e(3);
577 if (set(WRITE, 0, 4) != 0) e(4); /* lock belongs to parent */
578 if (set(READ, 10, 16) != 0) e(5); /* lock belongs to parent */
580 /* Up until now, all locks have been disjoint. Now try conflicts. */
581 if (fork()) {
582 /* Parent just waits for child to finish. */
583 wait(&s);
584 } else {
585 /* Child does the testing. */
586 errno = -100;
587 if (set(WRITE, 5, 7) < 0) e(6); /* should work */
588 if (set(WRITE, 4, 7) >= 0) e(7); /* child may not lock byte 4 */
589 if (errno != EACCES && errno != EAGAIN) e(8);
590 if (set(WRITE, 5, 9) != 0) e(9);
591 if (set(UNLOCK, 5, 9) != 0) e(10);
592 if (set(READ, 9, 17) < 0) e(11); /* shared READ lock is ok */
593 exit(0);
595 close(xfd);
598 void cloexec_test()
600 /* To text whether the FD_CLOEXEC flag actually causes files to be
601 * closed upon exec, we have to exec something. The test is carried
602 * out by forking, and then having the child exec test7 itself, but
603 * with argument 0. This is detected, and control comes here.
604 * File descriptors 3 and 10 should be closed here, and 10 open.
607 if (close(3) == 0) e(1001); /* close should fail; it was closed on exec */
608 if (close(6) != 0) e(1002); /* close should succeed */
609 if (close(10) == 0) e(1003); /* close should fail */
610 fflush(stdout);
611 exit(0);
614 int set(how, first, last)
615 int how, first, last;
617 int r;
618 struct flock flock;
620 if (how == READ) flock.l_type = F_RDLCK;
621 if (how == WRITE) flock.l_type = F_WRLCK;
622 if (how == UNLOCK) flock.l_type = F_UNLCK;
623 flock.l_whence = whence;
624 flock.l_start = (long) first;
625 flock.l_len = (long) last - (long) first + 1;
626 r = fcntl(xfd, func_code, &flock);
627 if (r != -1)
628 return(0);
629 else
630 return(-1);
633 int locked(b)
634 int b;
635 /* Test to see if byte b is locked. Return L or U */
637 struct flock flock;
638 pid_t pid;
639 int status;
641 flock.l_type = F_WRLCK;
642 flock.l_whence = whence;
643 flock.l_start = (long) b;
644 flock.l_len = 1;
646 /* Process' own locks are invisible to F_GETLK, so fork a child to test. */
647 pid = fork();
648 if (pid == 0) {
649 if (fcntl(xfd, F_GETLK, &flock) != 0) e(2000);
650 exit(flock.l_type == F_UNLCK ? U : L);
652 if (pid == -1) e(2001);
653 if (fcntl(xfd, F_GETLK, &flock) != 0) e(2002);
654 if (flock.l_type != F_UNLCK) e(2003);
655 if (wait(&status) != pid) e(2004);
656 if (!WIFEXITED(status)) e(2005);
657 return(WEXITSTATUS(status));
660 void e(n)
661 int n;
663 int err_num = errno; /* save errno in case printf clobbers it */
665 printf("Subtest %d, error %d errno=%d ", subtest, n, errno);
666 fflush(stdout);
667 errno = err_num; /* restore errno, just in case */
668 perror("");
669 if (errct++ > MAX_ERROR) {
670 printf("Too many errors; test aborted\n");
671 chdir("..");
672 system("rm -rf DIR*");
673 exit(1);
677 void sigfunc(s)
678 int s; /* for ANSI */
682 void quit()
685 chdir("..");
686 system("rm -rf DIR*");
688 if (errct == 0) {
689 printf("ok\n");
690 exit(0);
691 } else {
692 printf("%d errors\n", errct);
693 exit(1);