1 /* test39: fcntl() Author: Jan-Mark Wams (jms@cs.vu.nl) */
2
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 ``test39''
8 ** to ``DO CHECK'' ;-)
9 */
10
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/wait.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <fcntl.h>
18 #include <limits.h>
19 #include <errno.h>
20 #include <time.h>
21 #include <stdio.h>
22
23 #define MAX_ERROR 4
24 #define ITERATIONS 10
25
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)
29
30 int errct = 0;
31 int subtest = 1;
32 int superuser;
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 */
37
38 _PROTOTYPE(void main, (int argc, char *argv[]));
39 _PROTOTYPE(void test39a, (void));
40 _PROTOTYPE(void test39b, (void));
41 _PROTOTYPE(void test39c, (void));
42 _PROTOTYPE(void test39d, (void));
43 _PROTOTYPE(int do_check, (void));
44 _PROTOTYPE(void makelongnames, (void));
45 _PROTOTYPE(void e, (int number));
46 _PROTOTYPE(void quit, (void));
47
48 char executable[1024];
49
50 void main(argc, argv)
51 int argc;
52 char *argv[];
53 {
54 int i, m = 0xFFFF;
55
56 sync();
57 if (argc == 2) m = atoi(argv[1]);
58
59 /* If we have to check things, call do_check(). */
60 if (strcmp(argv[0], "DO CHECK") == 0) exit(do_check());
61
62 /* Get the path of the executable. */
63 strcpy(executable, "../");
64 strcat(executable, argv[0]);
65
66 printf("Test 39 ");
67 fflush(stdout);
68 System("rm -rf DIR_39; mkdir DIR_39");
69 Chdir("DIR_39");
70 makelongnames();
71 superuser = (geteuid() == 0);
72
73 for (i = 0; i < ITERATIONS; i++) {
74 test39a();
75 test39b();
76 test39c();
77 test39d();
78 }
79 quit();
80 }
81
82 void test39a()
83 { /* Test normal operation. */
84 subtest = 1;
85 System("rm -rf ../DIR_39/*");
86 }
87
88 void test39b()
89 {
90 subtest = 2;
91 System("rm -rf ../DIR_39/*");
92 }
93
94 void test39c()
95 {
96 subtest = 3;
97 System("rm -rf ../DIR_39/*");
98 }
99
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
103 ** value.
104 */
105 void test39d()
106 { /* Test locks with ``fork()'' and ``exec().'' */
107 int fd3, fd4, fd5, fd6, fd7, fd8;
108 int stat_loc;
109 int do_check_retval;
110 char *argv[2];
111 struct flock fl;
112
113 subtest = 4;
114
115 argv[0] = "DO CHECK";
116 argv[1] = (char *) NULL;
117
118 fl.l_whence = SEEK_SET;
119 fl.l_start = 0;
120 fl.l_len = 10;
121
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);
134
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);
138
139 /* Lock the first ten bytes from fd3 (for writing). */
140 fl.l_type = F_WRLCK;
141 if (fcntl(fd3, F_SETLK, &fl) == -1) e(9);
142
143 /* Lock (for reading) fd7. */
144 fl.l_type = F_RDLCK;
145 if (fcntl(fd7, F_SETLK, &fl) == -1) e(10);
146
147
148 switch (fork()) {
149 case -1: printf("Can't fork\n"); break;
150 case 0:
151 alarm(20);
152
153 /* Lock fd8. */
154 fl.l_type = F_WRLCK;
155 if (fcntl(fd8, F_SETLK, &fl) == -1) e(11);
156
157 /* Check the lock on fd3 and fd7. */
158 fl.l_type = F_WRLCK;
159 if (fcntl(fd3, F_GETLK, &fl) == -1) e(12);
160 if (fl.l_type != F_WRLCK) e(13);
161 if (fl.l_pid != getppid()) e(14);
162 fl.l_type = F_WRLCK;
163 if (fcntl(fd7, F_GETLK, &fl) == -1) e(15);
164 if (fl.l_type != F_RDLCK) e(16);
165 if (fl.l_pid != getppid()) e(17);
166
167 /* Check FD_CLOEXEC flags. */
168 if ((fcntl(fd3, F_GETFD) & FD_CLOEXEC) != 0) e(18);
169 if ((fcntl(fd4, F_GETFD) & FD_CLOEXEC) != 0) e(19);
170 if ((fcntl(fd5, F_GETFD) & FD_CLOEXEC) != FD_CLOEXEC) e(20);
171 if ((fcntl(fd6, F_GETFD) & FD_CLOEXEC) != FD_CLOEXEC) e(21);
172 if ((fcntl(fd7, F_GETFD) & FD_CLOEXEC) != 0) e(22);
173 if ((fcntl(fd8, F_GETFD) & FD_CLOEXEC) != 0) e(23);
174
175 execlp(executable + 3, "DO CHECK", (char *) NULL);
176 execlp(executable, "DO CHECK", (char *) NULL);
177 printf("Can't exec %s or %s\n", executable + 3, executable);
178 exit(0);
179
180 default:
181 wait(&stat_loc);
182 if (WIFSIGNALED(stat_loc)) e(24); /* Alarm? */
183 if (WIFEXITED(stat_loc) == 0) {
184 errct=10000;
185 quit();
186 }
187 }
188
189 /* Check the return value of do_check(). */
190 do_check_retval = WEXITSTATUS(stat_loc);
191 if ((do_check_retval & 0x11) == 0x11) e(25);
192 if ((do_check_retval & 0x12) == 0x12) e(26);
193 if ((do_check_retval & 0x14) == 0x14) e(27);
194 if ((do_check_retval & 0x18) == 0x18) e(28);
195 if ((do_check_retval & 0x21) == 0x21) e(29);
196 if ((do_check_retval & 0x22) == 0x22) e(30);
197 if ((do_check_retval & 0x24) == 0x24) e(31);
198 if ((do_check_retval & 0x28) == 0x28) e(32);
199 if ((do_check_retval & 0x41) == 0x41) e(33);
200 if ((do_check_retval & 0x42) == 0x42) e(34);
201 if ((do_check_retval & 0x44) == 0x44) e(35);
202 if ((do_check_retval & 0x48) == 0x48) e(36);
203 if ((do_check_retval & 0x81) == 0x81) e(37);
204 if ((do_check_retval & 0x82) == 0x82) e(38);
205 if ((do_check_retval & 0x84) == 0x84) e(39);
206 if ((do_check_retval & 0x88) == 0x88) e(40);
207
208 switch (fork()) {
209 case -1: printf("Can't fork\n"); break;
210 case 0:
211 alarm(20);
212
213 /* Lock fd8. */
214 fl.l_type = F_WRLCK;
215 if (fcntl(fd8, F_SETLK, &fl) == -1) e(41);
216
217 execvp(executable + 3, argv);
218 execvp(executable, argv);
219 printf("Can't exec %s or %s\n", executable + 3, executable);
220 exit(0);
221
222 default:
223 wait(&stat_loc);
224 if (WIFSIGNALED(stat_loc)) e(48); /* Alarm? */
225 }
226
227 /* Check the return value of do_check(). */
228 do_check_retval = WEXITSTATUS(stat_loc);
229 if ((do_check_retval & 0x11) == 0x11) e(49);
230 if ((do_check_retval & 0x12) == 0x12) e(50);
231 if ((do_check_retval & 0x14) == 0x14) e(51);
232 if ((do_check_retval & 0x18) == 0x18) e(52);
233 if ((do_check_retval & 0x21) == 0x21) e(53);
234 if ((do_check_retval & 0x22) == 0x22) e(54);
235 if ((do_check_retval & 0x24) == 0x24) e(55);
236 if ((do_check_retval & 0x28) == 0x28) e(56);
237 if ((do_check_retval & 0x41) == 0x41) e(57);
238 if ((do_check_retval & 0x42) == 0x42) e(58);
239 if ((do_check_retval & 0x44) == 0x44) e(59);
240 if ((do_check_retval & 0x48) == 0x48) e(60);
241 if ((do_check_retval & 0x81) == 0x81) e(61);
242 if ((do_check_retval & 0x82) == 0x82) e(62);
243 if ((do_check_retval & 0x84) == 0x84) e(63);
244 if ((do_check_retval & 0x88) == 0x88) e(64);
245
246 fl.l_type = F_UNLCK;
247 if (fcntl(fd3, F_SETLK, &fl) == -1) e(65);
248 if (fcntl(fd7, F_SETLK, &fl) == -1) e(66);
249
250 if (close(fd3) != 0) e(67);
251 if (close(fd4) != 0) e(68);
252 if (close(fd5) != 0) e(69);
253 if (close(fd6) != 0) e(70);
254 if (close(fd7) != 0) e(71);
255 if (close(fd8) != 0) e(72);
256
257 System("rm -f ../DIR_39/*\n");
258 }
259
260
261 /* This routine checks that fds 0 through 4, 7 and 8 are open and the rest
262 ** is closed. It also checks if we can lock the first 10 bytes on fd no. 3
263 ** and 4. It should not be possible to lock fd no. 3, but it should be
264 ** possible to lock fd no. 4. See ``test39d()'' for usage of this routine.
265 */
266 int do_check()
267 {
268 int i;
269 int retval = 0;
270 struct flock fl;
271
272 fl.l_whence = SEEK_SET;
273 fl.l_start = 0;
274 fl.l_len = 10;
275
276 /* All std.. are open. */
277 if (fcntl(0, F_GETFD) == -1) retval |= 0x11;
278 if (fcntl(1, F_GETFD) == -1) retval |= 0x11;
279 if (fcntl(2, F_GETFD) == -1) retval |= 0x11;
280
281 /* Fd no. 3, 4, 7 and 8 are open. */
282 if (fcntl(3, F_GETFD) == -1) retval |= 0x12;
283 if (fcntl(4, F_GETFD) == -1) retval |= 0x12;
284 if (fcntl(7, F_GETFD) == -1) retval |= 0x12;
285
286 /* Fd no. 5, 6 and 9 trough OPEN_MAX are closed. */
287 if (fcntl(5, F_GETFD) != -1) retval |= 0x14;
288 if (fcntl(6, F_GETFD) != -1) retval |= 0x14;
289 for (i = 9; i < OPEN_MAX; i++)
290 if (fcntl(i, F_GETFD) != -1) retval |= 0x18;
291
292 #if 0
293 /* Fd no. 3 is WRLCKed. */
294 fl.l_type = F_WRLCK;
295 if (fcntl(3, F_SETLK, &fl) != -1) retval |= 0x21;
296 if (errno != EACCES && errno != EAGAIN) retval |= 0x22;
297 fl.l_type = F_RDLCK;
298 if (fcntl(3, F_SETLK, &fl) != -1) retval |= 0x24;
299 if (errno != EACCES && errno != EAGAIN) retval |= 0x22;
300 fl.l_type = F_RDLCK;
301 if (fcntl(3, F_GETLK, &fl) == -1) retval |= 0x28;
302 if (fl.l_type != F_WRLCK) retval |= 0x28;
303 if (fl.l_pid != getpid()) retval |= 0x28;
304 fl.l_type = F_WRLCK;
305 if (fcntl(3, F_GETLK, &fl) == -1) retval |= 0x28;
306 if (fl.l_type != F_WRLCK) retval |= 0x28;
307 if (fl.l_pid != getpid()) retval |= 0x28;
308 #endif
309
310 /* Fd no. 4 is not locked. */
311 fl.l_type = F_WRLCK;
312 if (fcntl(4, F_SETLK, &fl) == -1) retval |= 0x41;
313 if (fcntl(4, F_GETLK, &fl) == -1) retval |= 0x42;
314 #if 0 /* XXX - see test7.c */
315 if (fl.l_type != F_WRLCK) retval |= 0x42;
316 if (fl.l_pid != getpid()) retval |= 0x42;
317 #endif /* 0 */
318
319 /* Fd no. 8 is locked after the fork, it is ours. */
320 fl.l_type = F_WRLCK;
321 if (fcntl(8, F_SETLK, &fl) == -1) retval |= 0x44;
322 if (fcntl(8, F_GETLK, &fl) == -1) retval |= 0x48;
323 #if 0 /* XXX - see test7.c */
324 if (fl.l_type != F_WRLCK) retval |= 0x48;
325 if (fl.l_pid != getpid()) retval |= 0x48;
326 #endif /* 0 */
327
328 #if 0
329 /* Fd no. 7 is RDLCKed. */
330 fl.l_type = F_WRLCK;
331 if (fcntl(7, F_SETLK, &fl) != -1) retval |= 0x81;
332 if (errno != EACCES && errno != EAGAIN) retval |= 0x82;
333 fl.l_type = F_RDLCK;
334 if (fcntl(7, F_SETLK, &fl) == -1) retval |= 0x84;
335 fl.l_type = F_RDLCK;
336 if (fcntl(7, F_GETLK, &fl) == -1) retval |= 0x88;
337 if (fl.l_type != F_UNLCK) retval |= 0x88;
338 fl.l_type = F_WRLCK;
339 if (fcntl(7, F_GETLK, &fl) == -1) retval |= 0x88;
340 if (fl.l_type != F_RDLCK) retval |= 0x88;
341 if (fl.l_pid != getppid()) retval |= 0x88;
342 #endif
343
344 return retval;
345 }
346
347 void makelongnames()
348 {
349 register int i;
350
351 memset(MaxName, 'a', NAME_MAX);
352 MaxName[NAME_MAX] = '\0';
353 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
354 MaxPath[i++] = '.';
355 MaxPath[i] = '/';
356 }
357 MaxPath[PATH_MAX - 1] = '\0';
358
359 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
360 strcpy(ToLongPath, MaxPath);
361
362 ToLongName[NAME_MAX] = 'a';
363 ToLongName[NAME_MAX + 1] = '\0'; /* extend ToLongName by one too many */
364 ToLongPath[PATH_MAX - 1] = '/';
365 ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */
366 }
367
368
369
370 void e(n)
371 int n;
372 {
373 int err_num = errno; /* Save in case printf clobbers it. */
374
375 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
376 errno = err_num;
377 perror("");
378 if (errct++ > MAX_ERROR) {
379 printf("Too many errors; test aborted\n");
380 chdir("..");
381 system("rm -rf DIR*");
382 exit(1);
383 }
384 errno = 0;
385 }
386
387
388 void quit()
389 {
390 Chdir("..");
391 System("rm -rf DIR_39");
392
393 if (errct == 0) {
394 printf("ok\n");
395 exit(0);
396 } else if (errct < 10000) {
397 printf("%d errors\n", errct);
398 exit(1);
399 } else {
400 printf("errors\n");
401 exit(2);
402 }
403 }
404
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.