kernel: scheduling fix for ARM
[minix.git] / test / test20.c
blobdcb5b968da324022af30a3ccf09319ee25e8ccad
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''
8 ** to ``DO CHECK'' ;-)
9 */
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>
23 #define MAX_ERROR 4
24 #define ITERATIONS 10
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)
30 #include "common.c"
32 int superuser;
34 void test20a(void);
35 void test20b(void);
36 void test20c(void);
37 void test20d(void);
38 int do_check(void);
40 char executable[1024];
42 int main(int argc, char *argv[])
44 int i;
46 sync();
47 /* If we have to check things, call do_check(). */
48 if (strcmp(argv[0], "DO CHECK") == 0) exit(do_check());
50 /* Get the path of the executable. */
51 strcpy(executable, "../");
52 strcat(executable, argv[0]);
54 start(20);
55 superuser = (geteuid() == 0);
57 for (i = 0; i < ITERATIONS; i++) {
58 test20a();
59 test20b();
60 test20c();
61 test20d();
63 quit();
65 return(-1); /* Unreachable */
68 void test20a()
69 { /* Test normal operation. */
70 subtest = 1;
71 System("rm -rf ../DIR_20/*");
74 void test20b()
76 subtest = 2;
77 System("rm -rf ../DIR_20/*");
80 void test20c()
82 subtest = 3;
83 System("rm -rf ../DIR_20/*");
86 /* Open fds 3, 4, 5 and 6. Set FD_CLOEXEC on 5 and 6. Exclusively lock the
87 ** first 10 bytes of fd no. 3. Shared lock fd no. 7. Lock fd no. 8 after
88 ** the fork. Do a ``exec()'' call with a funny argv[0] and check the return
89 ** value.
91 void test20d()
92 { /* Test locks with ``fork()'' and ``exec().'' */
93 int fd3, fd4, fd5, fd6, fd7, fd8;
94 int stat_loc;
95 int do_check_retval;
96 char *argv[2];
97 struct flock fl;
99 subtest = 4;
101 argv[0] = "DO CHECK";
102 argv[1] = (char *) NULL;
104 fl.l_whence = SEEK_SET;
105 fl.l_start = 0;
106 fl.l_len = 10;
108 /* Make a dummy files and open them. */
109 System("echo 'Great Balls Of Fire!' > file3");
110 System("echo 'Great Balls Of Fire!' > file4");
111 System("echo 'Great Balls Of Fire!' > file7");
112 System("echo 'Great Balls Of Fire!' > file8");
113 System("echo 'Great Balls Of Fire!' > file");
114 if ((fd3 = open("file3", O_RDWR)) != 3) e(1);
115 if ((fd4 = open("file4", O_RDWR)) != 4) e(2);
116 if ((fd5 = open("file", O_RDWR)) != 5) e(3);
117 if ((fd6 = open("file", O_RDWR)) != 6) e(4);
118 if ((fd7 = open("file7", O_RDWR)) != 7) e(5);
119 if ((fd8 = open("file8", O_RDWR)) != 8) e(6);
121 /* Set FD_CLOEXEC flags on fd5 and fd6. */
122 if (fcntl(fd5, F_SETFD, FD_CLOEXEC) == -1) e(7);
123 if (fcntl(fd6, F_SETFD, FD_CLOEXEC) == -1) e(8);
125 /* Lock the first ten bytes from fd3 (for writing). */
126 fl.l_type = F_WRLCK;
127 if (fcntl(fd3, F_SETLK, &fl) == -1) e(9);
129 /* Lock (for reading) fd7. */
130 fl.l_type = F_RDLCK;
131 if (fcntl(fd7, F_SETLK, &fl) == -1) e(10);
133 switch (fork()) {
134 case -1: printf("Can't fork\n"); break;
135 case 0:
136 alarm(20);
138 /* Lock fd8. */
139 fl.l_type = F_WRLCK;
140 if (fcntl(fd8, F_SETLK, &fl) == -1) e(11);
142 /* Check the lock on fd3 and fd7. */
143 fl.l_type = F_WRLCK;
144 if (fcntl(fd3, F_GETLK, &fl) == -1) e(12);
145 if (fl.l_type != F_WRLCK) e(13);
146 if (fl.l_pid != getppid()) e(14);
147 fl.l_type = F_WRLCK;
148 if (fcntl(fd7, F_GETLK, &fl) == -1) e(15);
149 if (fl.l_type != F_RDLCK) e(16);
150 if (fl.l_pid != getppid()) e(17);
152 /* Check FD_CLOEXEC flags. */
153 if ((fcntl(fd3, F_GETFD) & FD_CLOEXEC) != 0) e(18);
154 if ((fcntl(fd4, F_GETFD) & FD_CLOEXEC) != 0) e(19);
155 if ((fcntl(fd5, F_GETFD) & FD_CLOEXEC) != FD_CLOEXEC) e(20);
156 if ((fcntl(fd6, F_GETFD) & FD_CLOEXEC) != FD_CLOEXEC) e(21);
157 if ((fcntl(fd7, F_GETFD) & FD_CLOEXEC) != 0) e(22);
158 if ((fcntl(fd8, F_GETFD) & FD_CLOEXEC) != 0) e(23);
160 execlp(executable + 3, "DO CHECK", (char *) NULL);
161 execlp(executable, "DO CHECK", (char *) NULL);
162 printf("Can't exec %s or %s\n", executable + 3, executable);
163 exit(0);
165 default:
166 wait(&stat_loc);
167 if (WIFSIGNALED(stat_loc)) e(24); /* Alarm? */
168 if (WIFEXITED(stat_loc) == 0) {
169 errct=10000;
170 quit();
174 /* Check the return value of do_check(). */
175 do_check_retval = WEXITSTATUS(stat_loc);
176 if ((do_check_retval & 0x11) == 0x11) e(25);
177 if ((do_check_retval & 0x12) == 0x12) e(26);
178 if ((do_check_retval & 0x14) == 0x14) e(27);
179 if ((do_check_retval & 0x18) == 0x18) e(28);
180 if ((do_check_retval & 0x21) == 0x21) e(29);
181 if ((do_check_retval & 0x22) == 0x22) e(30);
182 if ((do_check_retval & 0x24) == 0x24) e(31);
183 if ((do_check_retval & 0x28) == 0x28) e(32);
184 if ((do_check_retval & 0x41) == 0x41) e(33);
185 if ((do_check_retval & 0x42) == 0x42) e(34);
186 if ((do_check_retval & 0x44) == 0x44) e(35);
187 if ((do_check_retval & 0x48) == 0x48) e(36);
188 if ((do_check_retval & 0x81) == 0x81) e(37);
189 if ((do_check_retval & 0x82) == 0x82) e(38);
190 if ((do_check_retval & 0x84) == 0x84) e(39);
191 if ((do_check_retval & 0x88) == 0x88) e(40);
193 switch (fork()) {
194 case -1: printf("Can't fork\n"); break;
195 case 0:
196 alarm(20);
198 /* Lock fd8. */
199 fl.l_type = F_WRLCK;
200 if (fcntl(fd8, F_SETLK, &fl) == -1) e(41);
202 execvp(executable + 3, argv);
203 execvp(executable, argv);
204 printf("Can't exec %s or %s\n", executable + 3, executable);
205 exit(0);
207 default:
208 wait(&stat_loc);
209 if (WIFSIGNALED(stat_loc)) e(48); /* Alarm? */
212 /* Check the return value of do_check(). */
213 do_check_retval = WEXITSTATUS(stat_loc);
214 if ((do_check_retval & 0x11) == 0x11) e(49);
215 if ((do_check_retval & 0x12) == 0x12) e(50);
216 if ((do_check_retval & 0x14) == 0x14) e(51);
217 if ((do_check_retval & 0x18) == 0x18) e(52);
218 if ((do_check_retval & 0x21) == 0x21) e(53);
219 if ((do_check_retval & 0x22) == 0x22) e(54);
220 if ((do_check_retval & 0x24) == 0x24) e(55);
221 if ((do_check_retval & 0x28) == 0x28) e(56);
222 if ((do_check_retval & 0x41) == 0x41) e(57);
223 if ((do_check_retval & 0x42) == 0x42) e(58);
224 if ((do_check_retval & 0x44) == 0x44) e(59);
225 if ((do_check_retval & 0x48) == 0x48) e(60);
226 if ((do_check_retval & 0x81) == 0x81) e(61);
227 if ((do_check_retval & 0x82) == 0x82) e(62);
228 if ((do_check_retval & 0x84) == 0x84) e(63);
229 if ((do_check_retval & 0x88) == 0x88) e(64);
231 fl.l_type = F_UNLCK;
232 if (fcntl(fd3, F_SETLK, &fl) == -1) e(65);
233 if (fcntl(fd7, F_SETLK, &fl) == -1) e(66);
235 if (close(fd3) != 0) e(67);
236 if (close(fd4) != 0) e(68);
237 if (close(fd5) != 0) e(69);
238 if (close(fd6) != 0) e(70);
239 if (close(fd7) != 0) e(71);
240 if (close(fd8) != 0) e(72);
242 System("rm -f ../DIR_20/*\n");
245 /* This routine checks that fds 0 through 4, 7 and 8 are open and the rest
246 ** is closed. It also checks if we can lock the first 10 bytes on fd no. 3
247 ** and 4. It should not be possible to lock fd no. 3, but it should be
248 ** possible to lock fd no. 4. See ``test20d()'' for usage of this routine.
250 int do_check()
252 int i;
253 int retval = 0;
254 struct flock fl;
256 fl.l_whence = SEEK_SET;
257 fl.l_start = 0;
258 fl.l_len = 10;
260 /* All std.. are open. */
261 if (fcntl(0, F_GETFD) == -1) retval |= 0x11;
262 if (fcntl(1, F_GETFD) == -1) retval |= 0x11;
263 if (fcntl(2, F_GETFD) == -1) retval |= 0x11;
265 /* Fd no. 3, 4, 7 and 8 are open. */
266 if (fcntl(3, F_GETFD) == -1) retval |= 0x12;
267 if (fcntl(4, F_GETFD) == -1) retval |= 0x12;
268 if (fcntl(7, F_GETFD) == -1) retval |= 0x12;
270 /* Fd no. 5, 6 and 9 trough OPEN_MAX are closed. */
271 if (fcntl(5, F_GETFD) != -1) retval |= 0x14;
272 if (fcntl(6, F_GETFD) != -1) retval |= 0x14;
273 for (i = 9; i < OPEN_MAX; i++)
274 if (fcntl(i, F_GETFD) != -1) retval |= 0x18;
276 #if 0
277 /* Fd no. 3 is WRLCKed. */
278 fl.l_type = F_WRLCK;
279 if (fcntl(3, F_SETLK, &fl) != -1) retval |= 0x21;
280 if (errno != EACCES && errno != EAGAIN) retval |= 0x22;
281 fl.l_type = F_RDLCK;
282 if (fcntl(3, F_SETLK, &fl) != -1) retval |= 0x24;
283 if (errno != EACCES && errno != EAGAIN) retval |= 0x22;
284 fl.l_type = F_RDLCK;
285 if (fcntl(3, F_GETLK, &fl) == -1) retval |= 0x28;
286 if (fl.l_type != F_WRLCK) retval |= 0x28;
287 if (fl.l_pid != getpid()) retval |= 0x28;
288 fl.l_type = F_WRLCK;
289 if (fcntl(3, F_GETLK, &fl) == -1) retval |= 0x28;
290 if (fl.l_type != F_WRLCK) retval |= 0x28;
291 if (fl.l_pid != getpid()) retval |= 0x28;
292 #endif
294 /* Fd no. 4 is not locked. */
295 fl.l_type = F_WRLCK;
296 if (fcntl(4, F_SETLK, &fl) == -1) retval |= 0x41;
297 if (fcntl(4, F_GETLK, &fl) == -1) retval |= 0x42;
298 #if 0 /* XXX - see test7.c */
299 if (fl.l_type != F_WRLCK) retval |= 0x42;
300 if (fl.l_pid != getpid()) retval |= 0x42;
301 #endif /* 0 */
303 /* Fd no. 8 is locked after the fork, it is ours. */
304 fl.l_type = F_WRLCK;
305 if (fcntl(8, F_SETLK, &fl) == -1) retval |= 0x44;
306 if (fcntl(8, F_GETLK, &fl) == -1) retval |= 0x48;
307 #if 0 /* XXX - see test7.c */
308 if (fl.l_type != F_WRLCK) retval |= 0x48;
309 if (fl.l_pid != getpid()) retval |= 0x48;
310 #endif /* 0 */
312 #if 0
313 /* Fd no. 7 is RDLCKed. */
314 fl.l_type = F_WRLCK;
315 if (fcntl(7, F_SETLK, &fl) != -1) retval |= 0x81;
316 if (errno != EACCES && errno != EAGAIN) retval |= 0x82;
317 fl.l_type = F_RDLCK;
318 if (fcntl(7, F_SETLK, &fl) == -1) retval |= 0x84;
319 fl.l_type = F_RDLCK;
320 if (fcntl(7, F_GETLK, &fl) == -1) retval |= 0x88;
321 if (fl.l_type != F_UNLCK) retval |= 0x88;
322 fl.l_type = F_WRLCK;
323 if (fcntl(7, F_GETLK, &fl) == -1) retval |= 0x88;
324 if (fl.l_type != F_RDLCK) retval |= 0x88;
325 if (fl.l_pid != getppid()) retval |= 0x88;
326 #endif
328 return retval;