xref: /minix3/minix/tests/test7.c (revision 424cad2cd6b21072ac918889f0901f3920356e25)
1433d6423SLionel Sambuc /* POSIX test program (7).			Author: Andy Tanenbaum */
2433d6423SLionel Sambuc 
3433d6423SLionel Sambuc /* The following POSIX calls are tested:
4433d6423SLionel Sambuc  *	pipe(), mkfifo(), fcntl()
5433d6423SLionel Sambuc  */
6433d6423SLionel Sambuc 
7433d6423SLionel Sambuc #include <sys/types.h>
8433d6423SLionel Sambuc #include <sys/stat.h>
9433d6423SLionel Sambuc #include <sys/wait.h>
10433d6423SLionel Sambuc #include <errno.h>
11433d6423SLionel Sambuc #include <fcntl.h>
12433d6423SLionel Sambuc #include <limits.h>
13433d6423SLionel Sambuc #include <signal.h>
14433d6423SLionel Sambuc #include <stdlib.h>
15433d6423SLionel Sambuc #include <string.h>
16433d6423SLionel Sambuc #include <unistd.h>
17433d6423SLionel Sambuc #include <stdio.h>
18433d6423SLionel Sambuc #include <setjmp.h>
19433d6423SLionel Sambuc 
20433d6423SLionel Sambuc #define ITERATIONS        4
21433d6423SLionel Sambuc int max_error = 3;
22433d6423SLionel Sambuc #include "common.h"
23433d6423SLionel Sambuc 
24433d6423SLionel Sambuc #define ITEMS  32
25433d6423SLionel Sambuc #define READ   10
26433d6423SLionel Sambuc #define WRITE  20
27433d6423SLionel Sambuc #define UNLOCK 30
28433d6423SLionel Sambuc #define U      70
29433d6423SLionel Sambuc #define L      80
30433d6423SLionel Sambuc 
31433d6423SLionel Sambuc 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};
32433d6423SLionel Sambuc 
33433d6423SLionel Sambuc 
34433d6423SLionel Sambuc int subtes, xfd;
35433d6423SLionel Sambuc int whence = SEEK_SET, func_code = F_SETLK;
36433d6423SLionel Sambuc extern char **environ;
37433d6423SLionel Sambuc 
38433d6423SLionel Sambuc #define timed_test(func) (timed_test_func(#func, func));
39433d6423SLionel Sambuc 
40433d6423SLionel Sambuc int main(int argc, char *argv []);
41433d6423SLionel Sambuc void timed_test_func(const char *s, void (* func)(void));
42433d6423SLionel Sambuc void timed_test_timeout(int signum);
43433d6423SLionel Sambuc void test7a(void);
44433d6423SLionel Sambuc void test7b(void);
45433d6423SLionel Sambuc void test7c(void);
46433d6423SLionel Sambuc void test7d(void);
47433d6423SLionel Sambuc void test7e(void);
48433d6423SLionel Sambuc void test7f(void);
49433d6423SLionel Sambuc void test7g(void);
50433d6423SLionel Sambuc void test7h(void);
51433d6423SLionel Sambuc void test7i(void);
52433d6423SLionel Sambuc void test7j(void);
53433d6423SLionel Sambuc void cloexec_test(void);
54433d6423SLionel Sambuc int set(int how, int first, int last);
55433d6423SLionel Sambuc int locked(int b);
56433d6423SLionel Sambuc void sigfunc(int s);
57433d6423SLionel Sambuc 
main(argc,argv)58433d6423SLionel Sambuc int main(argc, argv)
59433d6423SLionel Sambuc int argc;
60433d6423SLionel Sambuc char *argv[];
61433d6423SLionel Sambuc {
62433d6423SLionel Sambuc 
63433d6423SLionel Sambuc   int i, m = 0xFFFF;
64433d6423SLionel Sambuc 
65433d6423SLionel Sambuc   if (argc == 2) m = atoi(argv[1]);
66433d6423SLionel Sambuc   if (m == 0) cloexec_test();	/* important; do not remove this! */
67433d6423SLionel Sambuc 
68433d6423SLionel Sambuc   start(7);
69433d6423SLionel Sambuc 
70433d6423SLionel Sambuc   for (i = 0; i < ITERATIONS; i++) {
71433d6423SLionel Sambuc 	if (m & 00001) timed_test(test7a);
72433d6423SLionel Sambuc 	if (m & 00002) timed_test(test7b);
73433d6423SLionel Sambuc 	if (m & 00004) timed_test(test7c);
74433d6423SLionel Sambuc 	if (m & 00010) timed_test(test7d);
75433d6423SLionel Sambuc 	if (m & 00020) timed_test(test7e);
76433d6423SLionel Sambuc 	if (m & 00040) timed_test(test7f);
77433d6423SLionel Sambuc 	if (m & 00100) timed_test(test7g);
78433d6423SLionel Sambuc 	if (m & 00200) timed_test(test7h);
79433d6423SLionel Sambuc 	if (m & 00400) timed_test(test7i);
80433d6423SLionel Sambuc 	if (m & 01000) timed_test(test7j);
81433d6423SLionel Sambuc   }
82433d6423SLionel Sambuc   quit();
83433d6423SLionel Sambuc   return(-1);			/* impossible */
84433d6423SLionel Sambuc }
85433d6423SLionel Sambuc 
86433d6423SLionel Sambuc static jmp_buf timed_test_context;
87433d6423SLionel Sambuc 
timed_test_timeout(int signum)88433d6423SLionel Sambuc void timed_test_timeout(int signum)
89433d6423SLionel Sambuc {
90433d6423SLionel Sambuc   longjmp(timed_test_context, -1);
91433d6423SLionel Sambuc   e(700);
92433d6423SLionel Sambuc   quit();
93433d6423SLionel Sambuc   exit(-1);
94433d6423SLionel Sambuc }
95433d6423SLionel Sambuc 
timed_test_func(const char * s,void (* func)(void))96433d6423SLionel Sambuc void timed_test_func(const char *s, void (* func)(void))
97433d6423SLionel Sambuc {
98433d6423SLionel Sambuc   if (setjmp(timed_test_context) == 0)
99433d6423SLionel Sambuc   {
100433d6423SLionel Sambuc     /* the function gets 60 seconds to complete */
101433d6423SLionel Sambuc     if (signal(SIGALRM, timed_test_timeout) == SIG_ERR) { e(701); return; }
102433d6423SLionel Sambuc     alarm(60);
103433d6423SLionel Sambuc     func();
104433d6423SLionel Sambuc     alarm(0);
105433d6423SLionel Sambuc   }
106433d6423SLionel Sambuc   else
107433d6423SLionel Sambuc   {
108433d6423SLionel Sambuc     /* report timeout as error */
109433d6423SLionel Sambuc     printf("timeout in %s\n", s);
110433d6423SLionel Sambuc     e(702);
111433d6423SLionel Sambuc   }
112433d6423SLionel Sambuc }
113433d6423SLionel Sambuc 
test7a()114433d6423SLionel Sambuc void test7a()
115433d6423SLionel Sambuc {
116433d6423SLionel Sambuc /* Test pipe(). */
117433d6423SLionel Sambuc 
118433d6423SLionel Sambuc   int i, fd[2], ect;
119433d6423SLionel Sambuc   char buf2[ITEMS+1];
120433d6423SLionel Sambuc 
121433d6423SLionel Sambuc   /* Create a pipe, write on it, and read it back. */
122433d6423SLionel Sambuc   subtest = 1;
123433d6423SLionel Sambuc   if (pipe(fd) != 0) e(1);
124433d6423SLionel Sambuc   if (write(fd[1], buf, ITEMS) != ITEMS) e(2);
125433d6423SLionel Sambuc   buf2[0] = 0;
126433d6423SLionel Sambuc   if (read(fd[0], buf2, ITEMS+1) != ITEMS) e(3);
127433d6423SLionel Sambuc   ect = 0;
128433d6423SLionel Sambuc   for (i = 0; i < ITEMS; i++) if (buf[i] != buf2[i]) ect++;
129433d6423SLionel Sambuc   if (ect != 0) e(4);
130433d6423SLionel Sambuc   if (close(fd[0]) != 0) e(5);
131433d6423SLionel Sambuc   if (close(fd[1]) != 0) e(6);
132433d6423SLionel Sambuc 
133433d6423SLionel Sambuc   /* Error test.  Keep opening pipes until it fails.  Check error code. */
134433d6423SLionel Sambuc   errno = 0;
135433d6423SLionel Sambuc   while (1) {
136433d6423SLionel Sambuc 	if (pipe(fd) < 0) break;
137433d6423SLionel Sambuc   }
138433d6423SLionel Sambuc   if (errno != EMFILE) e(7);
139433d6423SLionel Sambuc 
140433d6423SLionel Sambuc   /* Close all the pipes. */
141433d6423SLionel Sambuc   for (i = 3; i < OPEN_MAX; i++) close(i);
142433d6423SLionel Sambuc }
143433d6423SLionel Sambuc 
test7b()144433d6423SLionel Sambuc void test7b()
145433d6423SLionel Sambuc {
146433d6423SLionel Sambuc /* Test mkfifo(). */
147433d6423SLionel Sambuc 
148433d6423SLionel Sambuc   int fdr, fdw, status;
149433d6423SLionel Sambuc   char buf2[ITEMS+1];
150433d6423SLionel Sambuc   int efork;
151433d6423SLionel Sambuc 
152433d6423SLionel Sambuc   /* Create a fifo, write on it, and read it back. */
153433d6423SLionel Sambuc   subtest = 2;
154433d6423SLionel Sambuc   if (mkfifo("T7.b", 0777) != 0) e(1);
155433d6423SLionel Sambuc   switch (fork()) {
156433d6423SLionel Sambuc   case -1:
157433d6423SLionel Sambuc   	efork = errno;
158433d6423SLionel Sambuc   	printf("Fork failed: %s (%d)\n", strerror(efork), efork);
159433d6423SLionel Sambuc   	exit(1);
160433d6423SLionel Sambuc   case 0:
161433d6423SLionel Sambuc 	/* Child reads from the fifo. */
162433d6423SLionel Sambuc 	if ( (fdr = open("T7.b", O_RDONLY)) < 0) e(5);
163433d6423SLionel Sambuc 	if (read(fdr, buf2, ITEMS+1) != ITEMS) e(6);
164433d6423SLionel Sambuc 	if (strcmp(buf, buf2) != 0) e(7);
165433d6423SLionel Sambuc 	if (close(fdr) != 0) e(8);
166433d6423SLionel Sambuc 	exit(0);
167433d6423SLionel Sambuc   default:
168433d6423SLionel Sambuc 	/* Parent writes on the fifo. */
169433d6423SLionel Sambuc 	if ( (fdw = open("T7.b", O_WRONLY)) < 0) e(2);
170433d6423SLionel Sambuc 	if (write(fdw, buf, ITEMS) != ITEMS) e(3);
171433d6423SLionel Sambuc 	wait(&status);
172433d6423SLionel Sambuc 	if (close(fdw) != 0) e(4);
173433d6423SLionel Sambuc   }
174433d6423SLionel Sambuc 
175433d6423SLionel Sambuc   /* Check some error conditions. */
176433d6423SLionel Sambuc   if (mkfifo("T7.b", 0777) != -1) e(9);
177433d6423SLionel Sambuc   errno = 0;
178433d6423SLionel Sambuc   if (mkfifo("a/b/c", 0777) != -1) e(10);
179433d6423SLionel Sambuc   if (errno != ENOENT) e(11);
180433d6423SLionel Sambuc   errno = 0;
181433d6423SLionel Sambuc   if (mkfifo("", 0777) != -1) e(12);
182433d6423SLionel Sambuc   if (errno != ENOENT) e(13);
183433d6423SLionel Sambuc   errno = 0;
184433d6423SLionel Sambuc   if (mkfifo("T7.b/x", 0777) != -1) e(14);
185433d6423SLionel Sambuc   if (errno != ENOTDIR) e(15);
186433d6423SLionel Sambuc   if (unlink("T7.b") != 0) e(16);
187433d6423SLionel Sambuc 
188433d6423SLionel Sambuc   /* Now check fifos and the O_NONBLOCK flag. */
189433d6423SLionel Sambuc   if (mkfifo("T7.b", 0600) != 0) e(17);
190433d6423SLionel Sambuc   errno = 0;
191433d6423SLionel Sambuc   if (open("T7.b", O_WRONLY | O_NONBLOCK) != -1) e(18);
192433d6423SLionel Sambuc   if (errno != ENXIO) e(19);
193433d6423SLionel Sambuc   if ( (fdr = open("T7.b", O_RDONLY | O_NONBLOCK)) < 0) e(20);
194433d6423SLionel Sambuc   if (fork()) {
195433d6423SLionel Sambuc 	/* Parent reads from fdr. */
196433d6423SLionel Sambuc 	wait(&status);		/* but first make sure writer has already run*/
197433d6423SLionel Sambuc 	if ( ( (status>>8) & 0377) != 77) e(21);
198433d6423SLionel Sambuc 	if (read(fdr, buf2, ITEMS+1) != ITEMS) e(22);
199433d6423SLionel Sambuc 	if (strcmp(buf, buf2) != 0) e(23);
200433d6423SLionel Sambuc 	if (close(fdr) != 0) e(24);
201433d6423SLionel Sambuc   } else {
202433d6423SLionel Sambuc 	/* Child opens the fifo for writing and writes to it. */
203433d6423SLionel Sambuc 	if ( (fdw = open("T7.b", O_WRONLY | O_NONBLOCK)) < 0) e(25);
204433d6423SLionel Sambuc 	if (write(fdw, buf, ITEMS) != ITEMS) e(26);
205433d6423SLionel Sambuc 	if (close(fdw) != 0) e(27);
206433d6423SLionel Sambuc 	exit(77);
207433d6423SLionel Sambuc   }
208433d6423SLionel Sambuc 
209433d6423SLionel Sambuc   if (unlink("T7.b") != 0) e(28);
210433d6423SLionel Sambuc }
211433d6423SLionel Sambuc 
test7c()212433d6423SLionel Sambuc void test7c()
213433d6423SLionel Sambuc {
214433d6423SLionel Sambuc /* Test fcntl(). */
215433d6423SLionel Sambuc 
216*424cad2cSDavid van Moolenbroek   int fd, m, s, newfd, newfd2, newfd3, newfd4;
217*424cad2cSDavid van Moolenbroek   struct stat stat1, stat2, stat3, stat4, stat5;
218433d6423SLionel Sambuc 
219433d6423SLionel Sambuc   subtest = 3;
220433d6423SLionel Sambuc   errno = -100;
221433d6423SLionel Sambuc   if ( (fd = creat("T7.c", 0777)) < 0) e(1);
222433d6423SLionel Sambuc 
223433d6423SLionel Sambuc   /* Turn the per-file-descriptor flags on and off. */
224433d6423SLionel Sambuc   if (fcntl(fd, F_GETFD) != 0) e(2);	/* FD_CLOEXEC is initially off */
225433d6423SLionel Sambuc   if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(3);/* turn it on */
226433d6423SLionel Sambuc   if (fcntl(fd, F_GETFD) != FD_CLOEXEC) e(4);	/* should be on now */
227433d6423SLionel Sambuc   if (fcntl(fd, F_SETFD, 0) != 0) e(5);		/* turn it off */
228433d6423SLionel Sambuc   if (fcntl(fd, F_GETFD) != 0) e(6);		/* should be off now */
229433d6423SLionel Sambuc 
230433d6423SLionel Sambuc   /* Turn the open-file-description flags on and off. Start with O_APPEND. */
231433d6423SLionel Sambuc   m = O_WRONLY;
232433d6423SLionel Sambuc   if (fcntl(fd, F_GETFL) != m) e(7);	/* O_APPEND, O_NONBLOCK are off */
233433d6423SLionel Sambuc   if (fcntl(fd, F_SETFL, O_APPEND) != 0) e(8);	 /* turn on O_APPEND */
234433d6423SLionel Sambuc   if (fcntl(fd, F_GETFL) != (O_APPEND | m)) e(9);/* should be on now */
235433d6423SLionel Sambuc   if (fcntl(fd, F_SETFL, 0) != 0) e(10);	 /* turn it off */
236433d6423SLionel Sambuc   if (fcntl(fd, F_GETFL) != m) e(11);		 /* should be off now */
237433d6423SLionel Sambuc 
238433d6423SLionel Sambuc   /* Turn the open-file-description flags on and off. Now try O_NONBLOCK.  */
239433d6423SLionel Sambuc   if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) e(12);      /* turn on O_NONBLOCK */
240433d6423SLionel Sambuc   if (fcntl(fd, F_GETFL) != (O_NONBLOCK | m)) e(13);   /* should be on now */
241433d6423SLionel Sambuc   if (fcntl(fd, F_SETFL, 0) != 0) e(14);	       /* turn it off */
242433d6423SLionel Sambuc   if (fcntl(fd, F_GETFL) != m) e(15);		       /* should be off now */
243433d6423SLionel Sambuc 
244433d6423SLionel Sambuc   /* Now both at once. */
245433d6423SLionel Sambuc   if (fcntl(fd, F_SETFL, O_APPEND|O_NONBLOCK) != 0) e(16);
246433d6423SLionel Sambuc   if (fcntl(fd, F_GETFL) != (O_NONBLOCK | O_APPEND | m)) e(17);
247433d6423SLionel Sambuc   if (fcntl(fd, F_SETFL, 0) != 0) e(18);
248433d6423SLionel Sambuc   if (fcntl(fd, F_GETFL) != m) e(19);
249433d6423SLionel Sambuc 
250433d6423SLionel Sambuc   /* Now test F_DUPFD. */
251433d6423SLionel Sambuc   if ( (newfd = fcntl(fd, F_DUPFD, 0)) != 4) e(20);	/* 0-4 open */
252433d6423SLionel Sambuc   if ( (newfd2 = fcntl(fd, F_DUPFD, 0)) != 5) e(21);	/* 0-5 open */
253433d6423SLionel Sambuc   if (close(newfd) != 0) e(22);				/* 0-3, 5 open */
254433d6423SLionel Sambuc   if ( (newfd = fcntl(fd, F_DUPFD, 0)) != 4) e(23);	/* 0-5 open */
255433d6423SLionel Sambuc   if (close(newfd) != 0) e(24);				/* 0-3, 5 open */
256433d6423SLionel Sambuc   if ( (newfd = fcntl(fd, F_DUPFD, 5)) != 6) e(25);	/* 0-3, 5, 6 open */
257433d6423SLionel Sambuc   if (close(newfd2) != 0) e(26);			/* 0-3, 6 open */
258433d6423SLionel Sambuc 
259433d6423SLionel Sambuc   /* O_APPEND should be inherited, but FD_CLOEXEC should be cleared.  Check. */
260433d6423SLionel Sambuc   if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(26);/* turn FD_CLOEXEC on */
261433d6423SLionel Sambuc   if (fcntl(fd, F_SETFL, O_APPEND) != 0) e(27);	 /* turn O_APPEND on */
262433d6423SLionel Sambuc   if ( (newfd2 = fcntl(fd, F_DUPFD, 10)) != 10) e(28);	/* 0-3, 6, 10 open */
263433d6423SLionel Sambuc   if (fcntl(newfd2, F_GETFD) != 0) e(29);	/* FD_CLOEXEC must be 0 */
264433d6423SLionel Sambuc   if (fcntl(newfd2, F_GETFL) != (O_APPEND | m)) e(30);	/* O_APPEND set */
265433d6423SLionel Sambuc   if (fcntl(fd, F_SETFD, 0) != 0) e(31);/* turn FD_CLOEXEC off */
266433d6423SLionel Sambuc 
267*424cad2cSDavid van Moolenbroek   /* Also test F_DUPFD_CLOEXEC. */
268*424cad2cSDavid van Moolenbroek   if ( (newfd3 = fcntl(fd, F_DUPFD_CLOEXEC, 0)) != 4) e(0);
269*424cad2cSDavid van Moolenbroek   if (fcntl(newfd3, F_GETFD) != FD_CLOEXEC) e(0); /* FD_CLOEXEC must be set */
270*424cad2cSDavid van Moolenbroek   if (close(newfd3) != 0) e(0);
271*424cad2cSDavid van Moolenbroek   if ( (newfd3 = fcntl(fd, F_DUPFD_CLOEXEC, 12)) != 12) e(0);
272*424cad2cSDavid van Moolenbroek   if (fcntl(newfd3, F_GETFD) != FD_CLOEXEC) e(0); /* FD_CLOEXEC must be set */
273*424cad2cSDavid van Moolenbroek   if ( (newfd4 = fcntl(newfd3, F_DUPFD, 0)) != 4) e(0);
274*424cad2cSDavid van Moolenbroek   if (fcntl(newfd4, F_GETFD) != 0) e(0);	/* FD_CLOEXEC must be unset */
275*424cad2cSDavid van Moolenbroek   if (close(newfd4) != 0) e(0);
276*424cad2cSDavid van Moolenbroek   if ( (newfd4 = fcntl(newfd3, F_DUPFD_CLOEXEC, 15)) != 15) e(0);
277*424cad2cSDavid van Moolenbroek   if (fcntl(newfd4, F_GETFD) != FD_CLOEXEC) e(0); /* FD_CLOEXEC must be set */
278*424cad2cSDavid van Moolenbroek   if (fcntl(newfd4, F_SETFD, 0) != 0) e(0);	/* turn FD_CLOEXEC off */
279*424cad2cSDavid van Moolenbroek   if (fcntl(newfd4, F_GETFD) != 0) e(0);	/* FD_CLOEXEC must be unset */
280*424cad2cSDavid van Moolenbroek 
281*424cad2cSDavid van Moolenbroek   /* Check if all file descriptors are for the same inode. */
282433d6423SLionel Sambuc   if (fstat(fd, &stat1) != 0) e(32);
283*424cad2cSDavid van Moolenbroek   if (fstat(newfd, &stat2) != 0) e(33);
284*424cad2cSDavid van Moolenbroek   if (fstat(newfd2, &stat3) != 0) e(34);
285*424cad2cSDavid van Moolenbroek   if (fstat(newfd3, &stat4) != 0) e(0);
286*424cad2cSDavid van Moolenbroek   if (fstat(newfd4, &stat5) != 0) e(0);
287433d6423SLionel Sambuc   if (stat1.st_dev != stat2.st_dev) e(35);
288433d6423SLionel Sambuc   if (stat1.st_dev != stat3.st_dev) e(36);
289*424cad2cSDavid van Moolenbroek   if (stat1.st_dev != stat4.st_dev) e(0);
290*424cad2cSDavid van Moolenbroek   if (stat1.st_dev != stat5.st_dev) e(0);
291433d6423SLionel Sambuc   if (stat1.st_ino != stat2.st_ino) e(37);
292433d6423SLionel Sambuc   if (stat1.st_ino != stat3.st_ino) e(38);
293*424cad2cSDavid van Moolenbroek   if (stat1.st_ino != stat4.st_ino) e(0);
294*424cad2cSDavid van Moolenbroek   if (stat1.st_ino != stat5.st_ino) e(0);
295433d6423SLionel Sambuc 
296433d6423SLionel Sambuc   /* Now check on the FD_CLOEXEC flag.  Set it for fd (3) and newfd2 (10) */
297*424cad2cSDavid van Moolenbroek   if (fd != 3 || newfd2 != 10 || newfd3 != 12 || newfd4 != 15 || newfd != 6)
298*424cad2cSDavid van Moolenbroek 	e(39);
299433d6423SLionel Sambuc   if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) e(40);	/* close 3 on exec */
300433d6423SLionel Sambuc   if (fcntl(newfd2, F_SETFD, FD_CLOEXEC) != 0) e(41);	/* close 10 on exec */
301433d6423SLionel Sambuc   if (fcntl(newfd, F_SETFD, 0) != 0) e(42);		/* don't close 6 */
302*424cad2cSDavid van Moolenbroek   /* leave 12 and 15 as is */
303433d6423SLionel Sambuc   if (fork()) {
304433d6423SLionel Sambuc 	wait(&s);		/* parent just waits */
305433d6423SLionel Sambuc 	if (WEXITSTATUS(s) != 0) e(43);
306433d6423SLionel Sambuc   } else {
307433d6423SLionel Sambuc 	execle("../test7", "test7", "0", (char *) 0, environ);
308433d6423SLionel Sambuc 	exit(1);		/* the impossible never happens, right? */
309433d6423SLionel Sambuc   }
310433d6423SLionel Sambuc 
311433d6423SLionel Sambuc   /* Finally, close all the files. */
312433d6423SLionel Sambuc   if (fcntl(fd, F_SETFD, 0) != 0) e(44);	/* FD_CLOEXEC off */
313433d6423SLionel Sambuc   if (fcntl(newfd2, F_SETFD, 0) != 0) e(45);	/* FD_CLOEXEC off */
314433d6423SLionel Sambuc   if (close(fd) != 0) e(46);
315433d6423SLionel Sambuc   if (close(newfd) != 0) e(47);
316433d6423SLionel Sambuc   if (close(newfd2) != 0) e(48);
317433d6423SLionel Sambuc }
318433d6423SLionel Sambuc 
test7d()319433d6423SLionel Sambuc void test7d()
320433d6423SLionel Sambuc {
321433d6423SLionel Sambuc /* Test file locking. */
322433d6423SLionel Sambuc 
323433d6423SLionel Sambuc   subtest = 4;
324433d6423SLionel Sambuc 
325433d6423SLionel Sambuc   if ( (xfd = creat("T7.d", 0777)) != 3) e(1);
326433d6423SLionel Sambuc   close(xfd);
327433d6423SLionel Sambuc   if ( (xfd = open("T7.d", O_RDWR)) < 0) e(2);
328433d6423SLionel Sambuc   if (write(xfd, buf, ITEMS) != ITEMS) e(3);
329433d6423SLionel Sambuc   if (set(WRITE, 0, 3) != 0) e(4);
330433d6423SLionel Sambuc   if (set(WRITE, 5, 9) != 0) e(5);
331433d6423SLionel Sambuc   if (set(UNLOCK, 0, 3) != 0) e(6);
332433d6423SLionel Sambuc   if (set(UNLOCK, 4, 9) != 0) e(7);
333433d6423SLionel Sambuc 
334433d6423SLionel Sambuc   if (set(READ, 1, 4) != 0) e(8);
335433d6423SLionel Sambuc   if (set(READ, 4, 7) != 0) e(9);
336433d6423SLionel Sambuc   if (set(UNLOCK, 4, 7) != 0) e(10);
337433d6423SLionel Sambuc   if (set(UNLOCK, 1, 4) != 0) e(11);
338433d6423SLionel Sambuc 
339433d6423SLionel Sambuc   if (set(WRITE, 0, 3) != 0) e(12);
340433d6423SLionel Sambuc   if (set(WRITE, 5, 7) != 0) e(13);
341433d6423SLionel Sambuc   if (set(WRITE, 9 ,10) != 0) e(14);
342433d6423SLionel Sambuc   if (set(UNLOCK, 0, 4) != 0) e(15);
343433d6423SLionel Sambuc   if (set(UNLOCK, 0, 7) != 0) e(16);
344433d6423SLionel Sambuc   if (set(UNLOCK, 0, 2000) != 0) e(17);
345433d6423SLionel Sambuc 
346433d6423SLionel Sambuc   if (set(WRITE, 0, 3) != 0) e(18);
347433d6423SLionel Sambuc   if (set(WRITE, 5, 7) != 0) e(19);
348433d6423SLionel Sambuc   if (set(WRITE, 9 ,10) != 0) e(20);
349433d6423SLionel Sambuc   if (set(UNLOCK, 0, 100) != 0) e(21);
350433d6423SLionel Sambuc 
351433d6423SLionel Sambuc   if (set(WRITE, 0, 9) != 0) e(22);
352433d6423SLionel Sambuc   if (set(UNLOCK, 8, 9) != 0) e(23);
353433d6423SLionel Sambuc   if (set(UNLOCK, 0, 2) != 0) e(24);
354433d6423SLionel Sambuc   if (set(UNLOCK, 5, 5) != 0) e(25);
355433d6423SLionel Sambuc   if (set(UNLOCK, 4, 6) != 0) e(26);
356433d6423SLionel Sambuc   if (set(UNLOCK, 3, 3) != 0) e(27);
357433d6423SLionel Sambuc   if (set(UNLOCK, 7, 7) != 0) e(28);
358433d6423SLionel Sambuc 
359433d6423SLionel Sambuc   if (set(WRITE, 0, 10) != 0) e(29);
360433d6423SLionel Sambuc   if (set(UNLOCK, 0, 1000) != 0) e(30);
361433d6423SLionel Sambuc 
362433d6423SLionel Sambuc   /* Up until now, all locks have been disjoint.  Now try conflicts. */
363433d6423SLionel Sambuc   if (set(WRITE, 0, 4) != 0) e(31);
364433d6423SLionel Sambuc   if (set(WRITE, 4, 7) != 0) e(32);	/* owner may lock same byte twice */
365433d6423SLionel Sambuc   if (set(WRITE, 5, 10) != 0) e(33);
366433d6423SLionel Sambuc   if (set(UNLOCK, 0, 11) != 0) e(34);
367433d6423SLionel Sambuc 
368433d6423SLionel Sambuc   /* File is now unlocked. Length 0 means whole file. */
369433d6423SLionel Sambuc   if (set(WRITE, 2, 1) != 0) e(35);	/* this locks whole file */
370433d6423SLionel Sambuc   if (set(WRITE, 9,10) != 0) e(36);	/* a process can relock its file */
371433d6423SLionel Sambuc   if (set(WRITE, 3, 3) != 0) e(37);
372433d6423SLionel Sambuc   if (set(UNLOCK, 0, -1) != 0) e(38);	/* file is now unlocked. */
373433d6423SLionel Sambuc 
374433d6423SLionel Sambuc   /* Test F_GETLK. */
375433d6423SLionel Sambuc   if (set(WRITE, 2, 3) != 0) e(39);
376433d6423SLionel Sambuc   if (locked(1) != U) e(40);
377433d6423SLionel Sambuc   if (locked(2) != L) e(41);
378433d6423SLionel Sambuc   if (locked(3) != L) e(42);
379433d6423SLionel Sambuc   if (locked(4) != U) e(43);
380433d6423SLionel Sambuc   if (set(UNLOCK, 2, 3) != 0) e(44);
381433d6423SLionel Sambuc   if (locked(2) != U) e(45);
382433d6423SLionel Sambuc   if (locked(3) != U) e(46);
383433d6423SLionel Sambuc 
384433d6423SLionel Sambuc   close(xfd);
385433d6423SLionel Sambuc }
386433d6423SLionel Sambuc 
test7e()387433d6423SLionel Sambuc void test7e()
388433d6423SLionel Sambuc {
389433d6423SLionel Sambuc /* Test to see if SETLKW blocks as it should. */
390433d6423SLionel Sambuc 
391433d6423SLionel Sambuc   int pid, s;
392433d6423SLionel Sambuc 
393433d6423SLionel Sambuc   subtest = 5;
394433d6423SLionel Sambuc 
395433d6423SLionel Sambuc   if ( (xfd = creat("T7.e", 0777)) != 3) e(1);
396433d6423SLionel Sambuc   if (close(xfd) != 0) e(2);
397433d6423SLionel Sambuc   if ( (xfd = open("T7.e", O_RDWR)) < 0) e(3);
398433d6423SLionel Sambuc   if (write(xfd, buf, ITEMS) != ITEMS) e(4);
399433d6423SLionel Sambuc   if (set(WRITE, 0, 3) != 0) e(5);
400433d6423SLionel Sambuc 
401433d6423SLionel Sambuc   if ( (pid = fork()) ) {
402433d6423SLionel Sambuc 	/* Parent waits until child has started before signaling it. */
403433d6423SLionel Sambuc 	while (access("T7.e1", 0) != 0) ;
404433d6423SLionel Sambuc 	unlink("T7.e1");
405433d6423SLionel Sambuc 	sleep(1);
406433d6423SLionel Sambuc 	if (kill(pid, SIGKILL) < 0) e(6);
407433d6423SLionel Sambuc 	if (wait(&s) != pid) e(7);
408433d6423SLionel Sambuc   } else {
409433d6423SLionel Sambuc 	/* Child tries to lock and should block. */
410433d6423SLionel Sambuc 	if (creat("T7.e1", 0777) < 0) e(8);
411433d6423SLionel Sambuc 	func_code = F_SETLKW;
412433d6423SLionel Sambuc 	if (set(WRITE, 0, 3) != 0) e(9);	/* should block */
413433d6423SLionel Sambuc 	errno = -1000;
414433d6423SLionel Sambuc 	e(10);			/* process should be killed by signal */
415433d6423SLionel Sambuc 	exit(0);		/* should never happen */
416433d6423SLionel Sambuc   }
417433d6423SLionel Sambuc   close(xfd);
418433d6423SLionel Sambuc }
419433d6423SLionel Sambuc 
test7f()420433d6423SLionel Sambuc void test7f()
421433d6423SLionel Sambuc {
422433d6423SLionel Sambuc /* Test to see if SETLKW gives EINTR when interrupted. */
423433d6423SLionel Sambuc 
424433d6423SLionel Sambuc   int pid, s;
425433d6423SLionel Sambuc 
426433d6423SLionel Sambuc   subtest = 6;
427433d6423SLionel Sambuc 
428433d6423SLionel Sambuc   if ( (xfd = creat("T7.f", 0777)) != 3) e(1);
429433d6423SLionel Sambuc   if (close(xfd) != 0) e(2);
430433d6423SLionel Sambuc   if ( (xfd = open("T7.f", O_RDWR)) < 0) e(3);
431433d6423SLionel Sambuc   if (write(xfd, buf, ITEMS) != ITEMS) e(4);
432433d6423SLionel Sambuc   if (set(WRITE, 0, 3) != 0) e(5);
433433d6423SLionel Sambuc 
434433d6423SLionel Sambuc   if ( (pid = fork()) ) {
435433d6423SLionel Sambuc 	/* Parent waits until child has started before signaling it. */
436433d6423SLionel Sambuc 	while (access("T7.f1", 0) != 0) ;
437433d6423SLionel Sambuc 	unlink("T7.f1");
438433d6423SLionel Sambuc 	sleep(1);
439433d6423SLionel Sambuc 	if (kill(pid, SIGTERM) < 0) e(6);
440433d6423SLionel Sambuc 	if (wait(&s) != pid) e(7);
441433d6423SLionel Sambuc 	if ( (s>>8) != 19) e(8);
442433d6423SLionel Sambuc   } else {
443433d6423SLionel Sambuc 	/* Child tries to lock and should block.
444433d6423SLionel Sambuc 	 * `signal(SIGTERM, sigfunc);' to set the signal handler is inadequate
445433d6423SLionel Sambuc 	 * because on systems like BSD the sigaction flags for signal include
446433d6423SLionel Sambuc 	 * `SA_RESTART' so syscalls are restarted after they have been
447433d6423SLionel Sambuc 	 * interrupted by a signal.
448433d6423SLionel Sambuc 	 */
449433d6423SLionel Sambuc 	struct sigaction sa, osa;
450433d6423SLionel Sambuc 
451433d6423SLionel Sambuc 	sa.sa_handler = sigfunc;
452433d6423SLionel Sambuc 	sigemptyset(&sa.sa_mask);
453433d6423SLionel Sambuc 	sa.sa_flags = 0;
454433d6423SLionel Sambuc 	if (sigaction(SIGTERM, &sa, &osa) < 0) e(999);
455433d6423SLionel Sambuc 	if (creat("T7.f1", 0777) < 0) e(9);
456433d6423SLionel Sambuc 	func_code = F_SETLKW;
457433d6423SLionel Sambuc 	if (set(WRITE, 0, 3) != -1) e(10);	/* should block */
458433d6423SLionel Sambuc 	if (errno != EINTR) e(11);	/* signal should release it */
459433d6423SLionel Sambuc 	exit(19);
460433d6423SLionel Sambuc   }
461433d6423SLionel Sambuc   close(xfd);
462433d6423SLionel Sambuc }
463433d6423SLionel Sambuc 
test7g()464433d6423SLionel Sambuc void test7g()
465433d6423SLionel Sambuc {
466433d6423SLionel Sambuc /* Test to see if SETLKW unlocks when the needed lock becomes available. */
467433d6423SLionel Sambuc 
468433d6423SLionel Sambuc   int pid, s;
469433d6423SLionel Sambuc 
470433d6423SLionel Sambuc   subtest = 7;
471433d6423SLionel Sambuc 
472433d6423SLionel Sambuc   if ( (xfd = creat("T7.g", 0777)) != 3) e(1);
473433d6423SLionel Sambuc   if (close(xfd) != 0) e(2);
474433d6423SLionel Sambuc   if ( (xfd = open("T7.g", O_RDWR)) < 0) e(3);
475433d6423SLionel Sambuc   if (write(xfd, buf, ITEMS) != ITEMS) e(4);
476433d6423SLionel Sambuc   if (set(WRITE, 0, 3) != 0) e(5);	/* bytes 0 to 3 are now locked */
477433d6423SLionel Sambuc 
478433d6423SLionel Sambuc   if ( (pid = fork()) ) {
479433d6423SLionel Sambuc 	/* Parent waits for child to start. */
480433d6423SLionel Sambuc 	while (access("T7.g1", 0) != 0) ;
481433d6423SLionel Sambuc 	unlink("T7.g1");
482433d6423SLionel Sambuc 	sleep(1);
483433d6423SLionel Sambuc 	if (set(UNLOCK, 0, 3) != 0) e(5);
484433d6423SLionel Sambuc 	if (wait(&s) != pid) e(6);
485433d6423SLionel Sambuc 	if ( (s >> 8) != 29) e(7);
486433d6423SLionel Sambuc   } else {
487433d6423SLionel Sambuc 	/* Child tells parent it is alive, then tries to lock and is blocked.*/
488433d6423SLionel Sambuc 	func_code = F_SETLKW;
489433d6423SLionel Sambuc 	if (creat("T7.g1", 0777) < 0) e(8);
490433d6423SLionel Sambuc 	if (set(WRITE, 3, 3) != 0) e(9);	/* process must block now */
491433d6423SLionel Sambuc 	if (set(UNLOCK, 3, 3) != 0) e(10);
492433d6423SLionel Sambuc 	exit(29);
493433d6423SLionel Sambuc   }
494433d6423SLionel Sambuc   close(xfd);
495433d6423SLionel Sambuc }
496433d6423SLionel Sambuc 
test7h()497433d6423SLionel Sambuc void test7h()
498433d6423SLionel Sambuc {
499433d6423SLionel Sambuc /* Test to see what happens if two processed block on the same lock. */
500433d6423SLionel Sambuc 
501433d6423SLionel Sambuc   int pid, pid2, s, w;
502433d6423SLionel Sambuc 
503433d6423SLionel Sambuc   subtest = 8;
504433d6423SLionel Sambuc 
505433d6423SLionel Sambuc   if ( (xfd = creat("T7.h", 0777)) != 3) e(1);
506433d6423SLionel Sambuc   if (close(xfd) != 0) e(2);
507433d6423SLionel Sambuc   if ( (xfd = open("T7.h", O_RDWR)) < 0) e(3);
508433d6423SLionel Sambuc   if (write(xfd, buf, ITEMS) != ITEMS) e(4);
509433d6423SLionel Sambuc   if (set(WRITE, 0, 3) != 0) e(5);	/* bytes 0 to 3 are now locked */
510433d6423SLionel Sambuc 
511433d6423SLionel Sambuc   if ( (pid = fork()) ) {
512433d6423SLionel Sambuc 	if ( (pid2 = fork()) ) {
513433d6423SLionel Sambuc 		/* Parent waits for child to start. */
514433d6423SLionel Sambuc 		while (access("T7.h1", 0) != 0) ;
515433d6423SLionel Sambuc 		while (access("T7.h2", 0) != 0) ;
516433d6423SLionel Sambuc 		unlink("T7.h1");
517433d6423SLionel Sambuc 		unlink("T7.h2");
518433d6423SLionel Sambuc 		sleep(1);
519433d6423SLionel Sambuc 		if (set(UNLOCK, 0, 3) != 0) e(6);
520433d6423SLionel Sambuc 		w = wait(&s);
521433d6423SLionel Sambuc 		if (w != pid && w != pid2) e(7);
522433d6423SLionel Sambuc 		s = s >> 8;
523433d6423SLionel Sambuc 		if (s != 39 && s != 49) e(8);
524433d6423SLionel Sambuc 		w = wait(&s);
525433d6423SLionel Sambuc 		if (w != pid && w != pid2) e(9);
526433d6423SLionel Sambuc 		s = s >> 8;
527433d6423SLionel Sambuc 		if (s != 39 && s != 49) e(10);
528433d6423SLionel Sambuc 	} else {
529433d6423SLionel Sambuc 		func_code = F_SETLKW;
530433d6423SLionel Sambuc 		if (creat("T7.h1", 0777) < 0) e(11);
531433d6423SLionel Sambuc 		if (set(WRITE, 0, 0) != 0) e(12);	/* block now */
532433d6423SLionel Sambuc 		if (set(UNLOCK, 0, 0) != 0) e(13);
533433d6423SLionel Sambuc 		exit(39);
534433d6423SLionel Sambuc 	}
535433d6423SLionel Sambuc   } else {
536433d6423SLionel Sambuc 	/* Child tells parent it is alive, then tries to lock and is blocked.*/
537433d6423SLionel Sambuc 	func_code = F_SETLKW;
538433d6423SLionel Sambuc 	if (creat("T7.h2", 0777) < 0) e(14);
539433d6423SLionel Sambuc 	if (set(WRITE, 0, 1) != 0) e(15);	/* process must block now */
540433d6423SLionel Sambuc 	if (set(UNLOCK, 0, 1) != 0) e(16);
541433d6423SLionel Sambuc 	exit(49);
542433d6423SLionel Sambuc   }
543433d6423SLionel Sambuc   close(xfd);
544433d6423SLionel Sambuc }
545433d6423SLionel Sambuc 
test7i()546433d6423SLionel Sambuc void test7i()
547433d6423SLionel Sambuc {
548433d6423SLionel Sambuc /* Check error conditions for fcntl(). */
549433d6423SLionel Sambuc 
550433d6423SLionel Sambuc   int tfd, i;
551433d6423SLionel Sambuc 
552433d6423SLionel Sambuc   subtest = 9;
553433d6423SLionel Sambuc 
554433d6423SLionel Sambuc   errno = 0;
555433d6423SLionel Sambuc   if ( (xfd = creat("T7.i", 0777)) != 3) e(1);
556433d6423SLionel Sambuc   if (close(xfd) != 0) e(2);
557433d6423SLionel Sambuc   if ( (xfd = open("T7.i", O_RDWR)) < 0) e(3);
558433d6423SLionel Sambuc   if (write(xfd, buf, ITEMS) != ITEMS) e(4);
559433d6423SLionel Sambuc   if (set(WRITE, 0, 3) != 0) e(5);	/* bytes 0 to 3 are now locked */
560433d6423SLionel Sambuc   if (set(WRITE, 0, 0) != 0) e(6);
561433d6423SLionel Sambuc   if (errno != 0) e(7);
562433d6423SLionel Sambuc   errno = 0;
563433d6423SLionel Sambuc   if (set(WRITE, 3, 3) != 0) e(8);
564433d6423SLionel Sambuc   if (errno != 0) e(9);
565433d6423SLionel Sambuc   tfd = xfd;			/* hold good value */
566433d6423SLionel Sambuc   xfd = -99;
567433d6423SLionel Sambuc   errno = 0;
568433d6423SLionel Sambuc   if (set(WRITE, 0, 0) != -1) e(10);
569433d6423SLionel Sambuc   if (errno != EBADF) e(11);
570433d6423SLionel Sambuc 
571433d6423SLionel Sambuc   errno = 0;
572433d6423SLionel Sambuc   if ( (xfd = open("T7.i", O_WRONLY)) < 0) e(12);
573433d6423SLionel Sambuc   if (set(READ, 0, 0) != -1) e(13);
574433d6423SLionel Sambuc   if (errno != EBADF) e(14);
575433d6423SLionel Sambuc   if (close(xfd) != 0) e(15);
576433d6423SLionel Sambuc 
577433d6423SLionel Sambuc   errno = 0;
578433d6423SLionel Sambuc   if ( (xfd = open("T7.i", O_RDONLY)) < 0) e(16);
579433d6423SLionel Sambuc   if (set(WRITE, 0, 0) != -1) e(17);
580433d6423SLionel Sambuc   if (errno != EBADF) e(18);
581433d6423SLionel Sambuc   if (close(xfd) != 0) e(19);
582433d6423SLionel Sambuc   xfd = tfd;			/* restore legal xfd value */
583433d6423SLionel Sambuc 
584433d6423SLionel Sambuc   /* Check for EINVAL. */
585433d6423SLionel Sambuc   errno = 0;
586433d6423SLionel Sambuc   if (fcntl(xfd, F_DUPFD, OPEN_MAX) != -1) e(20);
587433d6423SLionel Sambuc   if (errno != EINVAL) e(21);
588433d6423SLionel Sambuc   errno = 0;
589433d6423SLionel Sambuc   if (fcntl(xfd, F_DUPFD, -1) != -1) e(22);
590433d6423SLionel Sambuc   if (errno != EINVAL) e(23);
591433d6423SLionel Sambuc 
592433d6423SLionel Sambuc   xfd = 0;			/* stdin does not support locking */
593433d6423SLionel Sambuc   errno = 0;
594433d6423SLionel Sambuc   if (set(READ, 0, 0) != -1) e(24);
595433d6423SLionel Sambuc   if (errno != EINVAL) e(25);
596433d6423SLionel Sambuc   xfd = tfd;
597433d6423SLionel Sambuc 
598433d6423SLionel Sambuc   /* Check ENOLCK. */
599433d6423SLionel Sambuc   for (i = 0; i < ITEMS; i++) {
600433d6423SLionel Sambuc 	if (set(WRITE, i, i) == 0) continue;
601433d6423SLionel Sambuc 	if (errno != ENOLCK) {
602433d6423SLionel Sambuc 		e(26);
603433d6423SLionel Sambuc 		break;
604433d6423SLionel Sambuc 	}
605433d6423SLionel Sambuc   }
606433d6423SLionel Sambuc 
607433d6423SLionel Sambuc   /* Check EMFILE. */
608433d6423SLionel Sambuc   for (i = xfd + 1; i < OPEN_MAX; i++) open("T7.i", 0);	/* use up all fds */
609433d6423SLionel Sambuc   errno = 0;
610433d6423SLionel Sambuc   if (fcntl(xfd, F_DUPFD, 0) != -1) e(27);	/* No fds left */
611433d6423SLionel Sambuc   if (errno != EMFILE) e(28);
612433d6423SLionel Sambuc 
613433d6423SLionel Sambuc   for (i = xfd; i < OPEN_MAX; i++) if (close(i) != 0) e(29);
614433d6423SLionel Sambuc }
615433d6423SLionel Sambuc 
test7j()616433d6423SLionel Sambuc void test7j()
617433d6423SLionel Sambuc {
618433d6423SLionel Sambuc /* Test file locking with two processes. */
619433d6423SLionel Sambuc 
620433d6423SLionel Sambuc   int s;
621433d6423SLionel Sambuc 
622433d6423SLionel Sambuc   subtest = 10;
623433d6423SLionel Sambuc 
624433d6423SLionel Sambuc   if ( (xfd = creat("T7.j", 0777)) != 3) e(1);
625433d6423SLionel Sambuc   close(xfd);
626433d6423SLionel Sambuc   if ( (xfd = open("T7.j", O_RDWR)) < 0) e(2);
627433d6423SLionel Sambuc   if (write(xfd, buf, ITEMS) != ITEMS) e(3);
628433d6423SLionel Sambuc   if (set(WRITE, 0, 4) != 0) e(4);	/* lock belongs to parent */
629433d6423SLionel Sambuc   if (set(READ, 10, 16) != 0) e(5);	/* lock belongs to parent */
630433d6423SLionel Sambuc 
631433d6423SLionel Sambuc   /* Up until now, all locks have been disjoint.  Now try conflicts. */
632433d6423SLionel Sambuc   if (fork()) {
633433d6423SLionel Sambuc 	/* Parent just waits for child to finish. */
634433d6423SLionel Sambuc 	wait(&s);
635433d6423SLionel Sambuc   } else {
636433d6423SLionel Sambuc 	/* Child does the testing. */
637433d6423SLionel Sambuc 	errno = -100;
638433d6423SLionel Sambuc 	if (set(WRITE, 5, 7) < 0) e(6);	/* should work */
639433d6423SLionel Sambuc 	if (set(WRITE, 4, 7) >= 0) e(7);	/* child may not lock byte 4 */
640433d6423SLionel Sambuc 	if (errno != EACCES && errno != EAGAIN) e(8);
641433d6423SLionel Sambuc 	if (set(WRITE, 5, 9) != 0) e(9);
642433d6423SLionel Sambuc 	if (set(UNLOCK, 5, 9) != 0) e(10);
643433d6423SLionel Sambuc 	if (set(READ, 9, 17) < 0) e(11);	/* shared READ lock is ok */
644433d6423SLionel Sambuc 	exit(0);
645433d6423SLionel Sambuc   }
646433d6423SLionel Sambuc   close(xfd);
647433d6423SLionel Sambuc }
648433d6423SLionel Sambuc 
cloexec_test()649433d6423SLionel Sambuc void cloexec_test()
650433d6423SLionel Sambuc {
651433d6423SLionel Sambuc /* To text whether the FD_CLOEXEC flag actually causes files to be
652433d6423SLionel Sambuc  * closed upon exec, we have to exec something.  The test is carried
653433d6423SLionel Sambuc  * out by forking, and then having the child exec test7 itself, but
654433d6423SLionel Sambuc  * with argument 0.  This is detected, and control comes here.
655*424cad2cSDavid van Moolenbroek  * File descriptors 3, 10, and 12 should be closed here, and 6 and 15 open.
656433d6423SLionel Sambuc  */
657433d6423SLionel Sambuc 
658433d6423SLionel Sambuc   if (close(3) == 0) e(1001);	/* close should fail; it was closed on exec */
659433d6423SLionel Sambuc   if (close(6) != 0) e(1002);	/* close should succeed */
660433d6423SLionel Sambuc   if (close(10) == 0) e(1003);	/* close should fail */
661*424cad2cSDavid van Moolenbroek   if (close(12) == 0) e(1004);	/* close should fail */
662*424cad2cSDavid van Moolenbroek   if (close(15) != 0) e(1005);	/* close should succeed */
663433d6423SLionel Sambuc   fflush(stdout);
664433d6423SLionel Sambuc   exit(0);
665433d6423SLionel Sambuc }
666433d6423SLionel Sambuc 
set(how,first,last)667433d6423SLionel Sambuc int set(how, first, last)
668433d6423SLionel Sambuc int how, first, last;
669433d6423SLionel Sambuc {
670433d6423SLionel Sambuc   int r;
671433d6423SLionel Sambuc   struct flock flock;
672433d6423SLionel Sambuc 
673433d6423SLionel Sambuc   if (how == READ) flock.l_type = F_RDLCK;
674433d6423SLionel Sambuc   if (how == WRITE) flock.l_type = F_WRLCK;
675433d6423SLionel Sambuc   if (how == UNLOCK) flock.l_type = F_UNLCK;
676433d6423SLionel Sambuc   flock.l_whence = whence;
677433d6423SLionel Sambuc   flock.l_start = (long) first;
678433d6423SLionel Sambuc   flock.l_len = (long) last - (long) first + 1;
679433d6423SLionel Sambuc   r = fcntl(xfd, func_code, &flock);
680433d6423SLionel Sambuc   if (r != -1)
681433d6423SLionel Sambuc 	return(0);
682433d6423SLionel Sambuc   else
683433d6423SLionel Sambuc 	return(-1);
684433d6423SLionel Sambuc }
685433d6423SLionel Sambuc 
locked(b)686433d6423SLionel Sambuc int locked(b)
687433d6423SLionel Sambuc int b;
688433d6423SLionel Sambuc /* Test to see if byte b is locked.  Return L or U */
689433d6423SLionel Sambuc {
690433d6423SLionel Sambuc   struct flock flock;
691433d6423SLionel Sambuc   pid_t pid;
692433d6423SLionel Sambuc   int status;
693433d6423SLionel Sambuc 
694433d6423SLionel Sambuc   flock.l_type = F_WRLCK;
695433d6423SLionel Sambuc   flock.l_whence = whence;
696433d6423SLionel Sambuc   flock.l_start = (long) b;
697433d6423SLionel Sambuc   flock.l_len = 1;
698433d6423SLionel Sambuc 
699433d6423SLionel Sambuc   /* Process' own locks are invisible to F_GETLK, so fork a child to test. */
700433d6423SLionel Sambuc   pid = fork();
701433d6423SLionel Sambuc   if (pid == 0) {
702433d6423SLionel Sambuc 	if (fcntl(xfd, F_GETLK, &flock) != 0) e(2000);
703433d6423SLionel Sambuc 	exit(flock.l_type == F_UNLCK ? U : L);
704433d6423SLionel Sambuc   }
705433d6423SLionel Sambuc   if (pid == -1) e(2001);
706433d6423SLionel Sambuc   if (fcntl(xfd, F_GETLK, &flock) != 0) e(2002);
707433d6423SLionel Sambuc   if (flock.l_type != F_UNLCK) e(2003);
708433d6423SLionel Sambuc   if (wait(&status) != pid) e(2004);
709433d6423SLionel Sambuc   if (!WIFEXITED(status)) e(2005);
710433d6423SLionel Sambuc   return(WEXITSTATUS(status));
711433d6423SLionel Sambuc }
712433d6423SLionel Sambuc 
sigfunc(s)713433d6423SLionel Sambuc void sigfunc(s)
714433d6423SLionel Sambuc int s;				/* for ANSI */
715433d6423SLionel Sambuc {
716433d6423SLionel Sambuc }
717433d6423SLionel Sambuc 
718