source: trunk/minix/test/test20.c@ 12

Last change on this file since 12 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 11.0 KB
Line 
1/* test20: 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 ``test20''
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
30int errct = 0;
31int subtest = 1;
32int superuser;
33char MaxName[NAME_MAX + 1]; /* Name of maximum length */
34char MaxPath[PATH_MAX]; /* Same for path */
35char ToLongName[NAME_MAX + 2]; /* Name of maximum +1 length */
36char ToLongPath[PATH_MAX + 1]; /* Same for path, both too long */
37
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));
47
48char executable[1024];
49
50void main(argc, argv)
51int argc;
52char *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 20 ");
67 fflush(stdout);
68 System("rm -rf DIR_20; mkdir DIR_20");
69 Chdir("DIR_20");
70 makelongnames();
71 superuser = (geteuid() == 0);
72
73 for (i = 0; i < ITERATIONS; i++) {
74 test20a();
75 test20b();
76 test20c();
77 test20d();
78 }
79 quit();
80}
81
82void test20a()
83{ /* Test normal operation. */
84 subtest = 1;
85 System("rm -rf ../DIR_20/*");
86}
87
88void test20b()
89{
90 subtest = 2;
91 System("rm -rf ../DIR_20/*");
92}
93
94void test20c()
95{
96 subtest = 3;
97 System("rm -rf ../DIR_20/*");
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*/
105void test20d()
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 switch (fork()) {
148 case -1: printf("Can't fork\n"); break;
149 case 0:
150 alarm(20);
151
152 /* Lock fd8. */
153 fl.l_type = F_WRLCK;
154 if (fcntl(fd8, F_SETLK, &fl) == -1) e(11);
155
156 /* Check the lock on fd3 and fd7. */
157 fl.l_type = F_WRLCK;
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);
161 fl.l_type = F_WRLCK;
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);
165
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);
173
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);
177 exit(0);
178
179 default:
180 wait(&stat_loc);
181 if (WIFSIGNALED(stat_loc)) e(24); /* Alarm? */
182 if (WIFEXITED(stat_loc) == 0) {
183 errct=10000;
184 quit();
185 }
186 }
187
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);
206
207 switch (fork()) {
208 case -1: printf("Can't fork\n"); break;
209 case 0:
210 alarm(20);
211
212 /* Lock fd8. */
213 fl.l_type = F_WRLCK;
214 if (fcntl(fd8, F_SETLK, &fl) == -1) e(41);
215
216 execvp(executable + 3, argv);
217 execvp(executable, argv);
218 printf("Can't exec %s or %s\n", executable + 3, executable);
219 exit(0);
220
221 default:
222 wait(&stat_loc);
223 if (WIFSIGNALED(stat_loc)) e(48); /* Alarm? */
224 }
225
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);
244
245 fl.l_type = F_UNLCK;
246 if (fcntl(fd3, F_SETLK, &fl) == -1) e(65);
247 if (fcntl(fd7, F_SETLK, &fl) == -1) e(66);
248
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);
255
256 System("rm -f ../DIR_20/*\n");
257}
258
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.
263*/
264int do_check()
265{
266 int i;
267 int retval = 0;
268 struct flock fl;
269
270 fl.l_whence = SEEK_SET;
271 fl.l_start = 0;
272 fl.l_len = 10;
273
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;
278
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;
283
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;
289
290#if 0
291 /* Fd no. 3 is WRLCKed. */
292 fl.l_type = F_WRLCK;
293 if (fcntl(3, F_SETLK, &fl) != -1) retval |= 0x21;
294 if (errno != EACCES && errno != EAGAIN) retval |= 0x22;
295 fl.l_type = F_RDLCK;
296 if (fcntl(3, F_SETLK, &fl) != -1) retval |= 0x24;
297 if (errno != EACCES && errno != EAGAIN) retval |= 0x22;
298 fl.l_type = F_RDLCK;
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;
302 fl.l_type = F_WRLCK;
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;
306#endif
307
308 /* Fd no. 4 is not locked. */
309 fl.l_type = F_WRLCK;
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;
315#endif /* 0 */
316
317 /* Fd no. 8 is locked after the fork, it is ours. */
318 fl.l_type = F_WRLCK;
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;
324#endif /* 0 */
325
326#if 0
327 /* Fd no. 7 is RDLCKed. */
328 fl.l_type = F_WRLCK;
329 if (fcntl(7, F_SETLK, &fl) != -1) retval |= 0x81;
330 if (errno != EACCES && errno != EAGAIN) retval |= 0x82;
331 fl.l_type = F_RDLCK;
332 if (fcntl(7, F_SETLK, &fl) == -1) retval |= 0x84;
333 fl.l_type = F_RDLCK;
334 if (fcntl(7, F_GETLK, &fl) == -1) retval |= 0x88;
335 if (fl.l_type != F_UNLCK) retval |= 0x88;
336 fl.l_type = F_WRLCK;
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;
340#endif
341
342 return retval;
343}
344
345void makelongnames()
346{
347 register int i;
348
349 memset(MaxName, 'a', NAME_MAX);
350 MaxName[NAME_MAX] = '\0';
351 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
352 MaxPath[i++] = '.';
353 MaxPath[i] = '/';
354 }
355 MaxPath[PATH_MAX - 1] = '\0';
356
357 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
358 strcpy(ToLongPath, MaxPath);
359
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 */
364}
365
366void e(n)
367int n;
368{
369 int err_num = errno; /* Save in case printf clobbers it. */
370
371 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
372 errno = err_num;
373 perror("");
374 if (errct++ > MAX_ERROR) {
375 printf("Too many errors; test aborted\n");
376 chdir("..");
377 system("rm -rf DIR*");
378 exit(1);
379 }
380 errno = 0;
381}
382
383void quit()
384{
385 Chdir("..");
386 System("rm -rf DIR_20");
387
388 if (errct == 0) {
389 printf("ok\n");
390 exit(0);
391 } else if (errct < 10000) {
392 printf("%d errors\n", errct);
393 exit(1);
394 } else {
395 printf("errors\n");
396 exit(2);
397 }
398}
Note: See TracBrowser for help on using the repository browser.