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