1 /* test20: fcntl() Author: Jan-Mark Wams (jms@cs.vu.nl) */
3 /* Some things have to be checked for ``exec()'' call's. Therefor
4 ** there is a check routine called ``do_check()'' that will be
5 ** called if the first argument (``argv[0]'') equals ``DO CHECK.''
6 ** Note that there is no way the shell (``/bin/sh'') will set
7 ** ``argv[0]'' to this funny value. (Unless we rename ``test20''
11 #include <sys/types.h>
26 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
27 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
28 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
33 char MaxName
[NAME_MAX
+ 1]; /* Name of maximum length */
34 char MaxPath
[PATH_MAX
]; /* Same for path */
35 char ToLongName
[NAME_MAX
+ 2]; /* Name of maximum +1 length */
36 char ToLongPath
[PATH_MAX
+ 1]; /* Same for path, both too long */
38 _PROTOTYPE(void main
, (int argc
, char *argv
[]));
39 _PROTOTYPE(void test20a
, (void));
40 _PROTOTYPE(void test20b
, (void));
41 _PROTOTYPE(void test20c
, (void));
42 _PROTOTYPE(void test20d
, (void));
43 _PROTOTYPE(int do_check
, (void));
44 _PROTOTYPE(void makelongnames
, (void));
45 _PROTOTYPE(void e
, (int number
));
46 _PROTOTYPE(void quit
, (void));
48 char executable
[1024];
57 if (argc
== 2) m
= atoi(argv
[1]);
59 /* If we have to check things, call do_check(). */
60 if (strcmp(argv
[0], "DO CHECK") == 0) exit(do_check());
62 /* Get the path of the executable. */
63 strcpy(executable
, "../");
64 strcat(executable
, argv
[0]);
68 System("rm -rf DIR_20; mkdir DIR_20");
71 superuser
= (geteuid() == 0);
73 for (i
= 0; i
< ITERATIONS
; i
++) {
83 { /* Test normal operation. */
85 System("rm -rf ../DIR_20/*");
91 System("rm -rf ../DIR_20/*");
97 System("rm -rf ../DIR_20/*");
100 /* Open fds 3, 4, 5 and 6. Set FD_CLOEXEC on 5 and 6. Exclusively lock the
101 ** first 10 bytes of fd no. 3. Shared lock fd no. 7. Lock fd no. 8 after
102 ** the fork. Do a ``exec()'' call with a funny argv[0] and check the return
106 { /* Test locks with ``fork()'' and ``exec().'' */
107 int fd3
, fd4
, fd5
, fd6
, fd7
, fd8
;
115 argv
[0] = "DO CHECK";
116 argv
[1] = (char *) NULL
;
118 fl
.l_whence
= SEEK_SET
;
122 /* Make a dummy files and open them. */
123 System("echo 'Great Balls Of Fire!' > file3");
124 System("echo 'Great Balls Of Fire!' > file4");
125 System("echo 'Great Balls Of Fire!' > file7");
126 System("echo 'Great Balls Of Fire!' > file8");
127 System("echo 'Great Balls Of Fire!' > file");
128 if ((fd3
= open("file3", O_RDWR
)) != 3) e(1);
129 if ((fd4
= open("file4", O_RDWR
)) != 4) e(2);
130 if ((fd5
= open("file", O_RDWR
)) != 5) e(3);
131 if ((fd6
= open("file", O_RDWR
)) != 6) e(4);
132 if ((fd7
= open("file7", O_RDWR
)) != 7) e(5);
133 if ((fd8
= open("file8", O_RDWR
)) != 8) e(6);
135 /* Set FD_CLOEXEC flags on fd5 and fd6. */
136 if (fcntl(fd5
, F_SETFD
, FD_CLOEXEC
) == -1) e(7);
137 if (fcntl(fd6
, F_SETFD
, FD_CLOEXEC
) == -1) e(8);
139 /* Lock the first ten bytes from fd3 (for writing). */
141 if (fcntl(fd3
, F_SETLK
, &fl
) == -1) e(9);
143 /* Lock (for reading) fd7. */
145 if (fcntl(fd7
, F_SETLK
, &fl
) == -1) e(10);
148 case -1: printf("Can't fork\n"); break;
154 if (fcntl(fd8
, F_SETLK
, &fl
) == -1) e(11);
156 /* Check the lock on fd3 and fd7. */
158 if (fcntl(fd3
, F_GETLK
, &fl
) == -1) e(12);
159 if (fl
.l_type
!= F_WRLCK
) e(13);
160 if (fl
.l_pid
!= getppid()) e(14);
162 if (fcntl(fd7
, F_GETLK
, &fl
) == -1) e(15);
163 if (fl
.l_type
!= F_RDLCK
) e(16);
164 if (fl
.l_pid
!= getppid()) e(17);
166 /* Check FD_CLOEXEC flags. */
167 if ((fcntl(fd3
, F_GETFD
) & FD_CLOEXEC
) != 0) e(18);
168 if ((fcntl(fd4
, F_GETFD
) & FD_CLOEXEC
) != 0) e(19);
169 if ((fcntl(fd5
, F_GETFD
) & FD_CLOEXEC
) != FD_CLOEXEC
) e(20);
170 if ((fcntl(fd6
, F_GETFD
) & FD_CLOEXEC
) != FD_CLOEXEC
) e(21);
171 if ((fcntl(fd7
, F_GETFD
) & FD_CLOEXEC
) != 0) e(22);
172 if ((fcntl(fd8
, F_GETFD
) & FD_CLOEXEC
) != 0) e(23);
174 execlp(executable
+ 3, "DO CHECK", (char *) NULL
);
175 execlp(executable
, "DO CHECK", (char *) NULL
);
176 printf("Can't exec %s or %s\n", executable
+ 3, executable
);
181 if (WIFSIGNALED(stat_loc
)) e(24); /* Alarm? */
182 if (WIFEXITED(stat_loc
) == 0) {
188 /* Check the return value of do_check(). */
189 do_check_retval
= WEXITSTATUS(stat_loc
);
190 if ((do_check_retval
& 0x11) == 0x11) e(25);
191 if ((do_check_retval
& 0x12) == 0x12) e(26);
192 if ((do_check_retval
& 0x14) == 0x14) e(27);
193 if ((do_check_retval
& 0x18) == 0x18) e(28);
194 if ((do_check_retval
& 0x21) == 0x21) e(29);
195 if ((do_check_retval
& 0x22) == 0x22) e(30);
196 if ((do_check_retval
& 0x24) == 0x24) e(31);
197 if ((do_check_retval
& 0x28) == 0x28) e(32);
198 if ((do_check_retval
& 0x41) == 0x41) e(33);
199 if ((do_check_retval
& 0x42) == 0x42) e(34);
200 if ((do_check_retval
& 0x44) == 0x44) e(35);
201 if ((do_check_retval
& 0x48) == 0x48) e(36);
202 if ((do_check_retval
& 0x81) == 0x81) e(37);
203 if ((do_check_retval
& 0x82) == 0x82) e(38);
204 if ((do_check_retval
& 0x84) == 0x84) e(39);
205 if ((do_check_retval
& 0x88) == 0x88) e(40);
208 case -1: printf("Can't fork\n"); break;
214 if (fcntl(fd8
, F_SETLK
, &fl
) == -1) e(41);
216 execvp(executable
+ 3, argv
);
217 execvp(executable
, argv
);
218 printf("Can't exec %s or %s\n", executable
+ 3, executable
);
223 if (WIFSIGNALED(stat_loc
)) e(48); /* Alarm? */
226 /* Check the return value of do_check(). */
227 do_check_retval
= WEXITSTATUS(stat_loc
);
228 if ((do_check_retval
& 0x11) == 0x11) e(49);
229 if ((do_check_retval
& 0x12) == 0x12) e(50);
230 if ((do_check_retval
& 0x14) == 0x14) e(51);
231 if ((do_check_retval
& 0x18) == 0x18) e(52);
232 if ((do_check_retval
& 0x21) == 0x21) e(53);
233 if ((do_check_retval
& 0x22) == 0x22) e(54);
234 if ((do_check_retval
& 0x24) == 0x24) e(55);
235 if ((do_check_retval
& 0x28) == 0x28) e(56);
236 if ((do_check_retval
& 0x41) == 0x41) e(57);
237 if ((do_check_retval
& 0x42) == 0x42) e(58);
238 if ((do_check_retval
& 0x44) == 0x44) e(59);
239 if ((do_check_retval
& 0x48) == 0x48) e(60);
240 if ((do_check_retval
& 0x81) == 0x81) e(61);
241 if ((do_check_retval
& 0x82) == 0x82) e(62);
242 if ((do_check_retval
& 0x84) == 0x84) e(63);
243 if ((do_check_retval
& 0x88) == 0x88) e(64);
246 if (fcntl(fd3
, F_SETLK
, &fl
) == -1) e(65);
247 if (fcntl(fd7
, F_SETLK
, &fl
) == -1) e(66);
249 if (close(fd3
) != 0) e(67);
250 if (close(fd4
) != 0) e(68);
251 if (close(fd5
) != 0) e(69);
252 if (close(fd6
) != 0) e(70);
253 if (close(fd7
) != 0) e(71);
254 if (close(fd8
) != 0) e(72);
256 System("rm -f ../DIR_20/*\n");
259 /* This routine checks that fds 0 through 4, 7 and 8 are open and the rest
260 ** is closed. It also checks if we can lock the first 10 bytes on fd no. 3
261 ** and 4. It should not be possible to lock fd no. 3, but it should be
262 ** possible to lock fd no. 4. See ``test20d()'' for usage of this routine.
270 fl
.l_whence
= SEEK_SET
;
274 /* All std.. are open. */
275 if (fcntl(0, F_GETFD
) == -1) retval
|= 0x11;
276 if (fcntl(1, F_GETFD
) == -1) retval
|= 0x11;
277 if (fcntl(2, F_GETFD
) == -1) retval
|= 0x11;
279 /* Fd no. 3, 4, 7 and 8 are open. */
280 if (fcntl(3, F_GETFD
) == -1) retval
|= 0x12;
281 if (fcntl(4, F_GETFD
) == -1) retval
|= 0x12;
282 if (fcntl(7, F_GETFD
) == -1) retval
|= 0x12;
284 /* Fd no. 5, 6 and 9 trough OPEN_MAX are closed. */
285 if (fcntl(5, F_GETFD
) != -1) retval
|= 0x14;
286 if (fcntl(6, F_GETFD
) != -1) retval
|= 0x14;
287 for (i
= 9; i
< OPEN_MAX
; i
++)
288 if (fcntl(i
, F_GETFD
) != -1) retval
|= 0x18;
291 /* Fd no. 3 is WRLCKed. */
293 if (fcntl(3, F_SETLK
, &fl
) != -1) retval
|= 0x21;
294 if (errno
!= EACCES
&& errno
!= EAGAIN
) retval
|= 0x22;
296 if (fcntl(3, F_SETLK
, &fl
) != -1) retval
|= 0x24;
297 if (errno
!= EACCES
&& errno
!= EAGAIN
) retval
|= 0x22;
299 if (fcntl(3, F_GETLK
, &fl
) == -1) retval
|= 0x28;
300 if (fl
.l_type
!= F_WRLCK
) retval
|= 0x28;
301 if (fl
.l_pid
!= getpid()) retval
|= 0x28;
303 if (fcntl(3, F_GETLK
, &fl
) == -1) retval
|= 0x28;
304 if (fl
.l_type
!= F_WRLCK
) retval
|= 0x28;
305 if (fl
.l_pid
!= getpid()) retval
|= 0x28;
308 /* Fd no. 4 is not locked. */
310 if (fcntl(4, F_SETLK
, &fl
) == -1) retval
|= 0x41;
311 if (fcntl(4, F_GETLK
, &fl
) == -1) retval
|= 0x42;
312 #if 0 /* XXX - see test7.c */
313 if (fl
.l_type
!= F_WRLCK
) retval
|= 0x42;
314 if (fl
.l_pid
!= getpid()) retval
|= 0x42;
317 /* Fd no. 8 is locked after the fork, it is ours. */
319 if (fcntl(8, F_SETLK
, &fl
) == -1) retval
|= 0x44;
320 if (fcntl(8, F_GETLK
, &fl
) == -1) retval
|= 0x48;
321 #if 0 /* XXX - see test7.c */
322 if (fl
.l_type
!= F_WRLCK
) retval
|= 0x48;
323 if (fl
.l_pid
!= getpid()) retval
|= 0x48;
327 /* Fd no. 7 is RDLCKed. */
329 if (fcntl(7, F_SETLK
, &fl
) != -1) retval
|= 0x81;
330 if (errno
!= EACCES
&& errno
!= EAGAIN
) retval
|= 0x82;
332 if (fcntl(7, F_SETLK
, &fl
) == -1) retval
|= 0x84;
334 if (fcntl(7, F_GETLK
, &fl
) == -1) retval
|= 0x88;
335 if (fl
.l_type
!= F_UNLCK
) retval
|= 0x88;
337 if (fcntl(7, F_GETLK
, &fl
) == -1) retval
|= 0x88;
338 if (fl
.l_type
!= F_RDLCK
) retval
|= 0x88;
339 if (fl
.l_pid
!= getppid()) retval
|= 0x88;
349 memset(MaxName
, 'a', NAME_MAX
);
350 MaxName
[NAME_MAX
] = '\0';
351 for (i
= 0; i
< PATH_MAX
- 1; i
++) { /* idem path */
355 MaxPath
[PATH_MAX
- 1] = '\0';
357 strcpy(ToLongName
, MaxName
); /* copy them Max to ToLong */
358 strcpy(ToLongPath
, MaxPath
);
360 ToLongName
[NAME_MAX
] = 'a';
361 ToLongName
[NAME_MAX
+ 1] = '\0'; /* extend ToLongName by one too many */
362 ToLongPath
[PATH_MAX
- 1] = '/';
363 ToLongPath
[PATH_MAX
] = '\0'; /* inc ToLongPath by one */
369 int err_num
= errno
; /* Save in case printf clobbers it. */
371 printf("Subtest %d, error %d errno=%d: ", subtest
, n
, errno
);
374 if (errct
++ > MAX_ERROR
) {
375 printf("Too many errors; test aborted\n");
377 system("rm -rf DIR*");
386 System("rm -rf DIR_20");
391 } else if (errct
< 10000) {
392 printf("%d errors\n", errct
);