kernel: some boottime sanitychecks
[minix.git] / test / test22.c
blob3bdc0c8c235960fdafe8d07fc438f25bee7dcd26
1 /* test22: umask() (p) Jan-Mark Wams. email: jms@cs.vu.nl */
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <sys/wait.h>
10 #include <stdio.h>
12 #define MAX_ERROR 4 /* Stop after ``MAX_ERROR'' errors. */
13 #define ITERATIONS 2
15 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
16 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
17 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
19 #include "common.c"
21 void test22a(void);
22 int mode(char *filename);
23 int umode(char *filename);
24 void e(int number);
25 void quit(void);
27 int main(int argc, char *argv[])
29 int i, m = 0xFFFF;
31 sync();
32 if (argc == 2) m = atoi(argv[1]);
33 start(22);
35 for (i = 0; i < ITERATIONS; i++) {
36 if (m & 0001) test22a();
39 quit();
41 return (-1); /* Unreachable */
44 void test22a()
46 int fd1, fd2;
47 int i, oldmask;
48 int stat_loc; /* For the wait sys call. */
50 subtest = 1;
52 system("chmod 777 ../DIR_22/* ../DIR_22/*/* > /dev/null 2>&1");
53 System("rm -rf ../DIR_22/*");
55 oldmask = 0123; /* Set oldmask and umask. */
56 umask(oldmask); /* Set oldmask and umask. */
58 /* Check all the possible values of umask. */
59 for (i = 0000; i <= 0777; i++) {
60 if (oldmask != umask(i)) e(1); /* set umask() */
61 fd1 = open("open", O_CREAT, 0777);
62 if (fd1 != 3) e(2); /* test open(), */
63 fd2 = creat("creat", 0777);
64 if (fd2 != 4) e(3); /* creat(), */
65 if (mkdir("dir", 0777) != 0) e(4); /* mkdir(), */
66 if (mkfifo("fifo", 0777) != 0) e(5); /* and mkfifo(). */
68 if (umode("open") != i) e(6); /* see if they have */
69 if (umode("creat") != i) e(7); /* the proper mode */
70 if (umode("dir") != i) e(8);
71 if (umode("fifo") != i) e(9);
73 /* Clean up */
74 if (close(fd1) != 0) e(10);
75 if (close(fd2) != 0) e(11); /* close fd's and */
76 unlink("open"); /* clean the dir */
77 unlink("creat");
78 rmdir("dir");
79 unlink("fifo");
80 oldmask = i; /* save current mask */
83 /* Check-reset mask */
84 if (umask(0124) != 0777) e(12);
86 /* Check if a umask of 0000 leaves the modes alone. */
87 if (umask(0000) != 0124) e(13);
88 for (i = 0000; i <= 0777; i++) {
89 fd1 = open("open", O_CREAT, i);
90 if (fd1 != 3) e(14); /* test open(), */
91 fd2 = creat("creat", i);
92 if (fd2 != 4) e(15); /* creat(), */
93 if (mkdir("dir", i) != 0) e(16); /* mkdir(), */
94 if (mkfifo("fifo", i) != 0) e(17); /* and mkfifo(). */
96 if (mode("open") != i) e(18); /* see if they have */
97 if (mode("creat") != i) e(19); /* the proper mode */
98 if (mode("dir") != i) e(20);
99 if (mode("fifo") != i) e(21);
101 /* Clean up */
102 if (close(fd1) != 0) e(22);
103 if (close(fd2) != 0) e(23);
104 if (unlink("open") != 0) e(24);
105 unlink("creat");
106 rmdir("dir");
107 unlink("fifo");
110 /* Check if umask survives a fork() */
111 if (umask(0124) != 0000) e(25);
112 switch (fork()) {
113 case -1: fprintf(stderr, "Can't fork\n"); break;
114 case 0:
115 mkdir("bar", 0777); /* child makes a dir */
116 exit(0);
117 default:
118 if (wait(&stat_loc) == -1) e(26);
120 if (umode("bar") != 0124) e(27);
121 rmdir("bar");
123 /* Check if umask in child changes umask in parent. */
124 switch (fork()) {
125 case -1: fprintf(stderr, "Can't fork\n"); break;
126 case 0:
127 switch (fork()) {
128 case -1:
129 fprintf(stderr, "Can't fork\n");
130 break;
131 case 0:
132 if (umask(0432) != 0124) e(28);
133 exit(0);
134 default:
135 if (wait(&stat_loc) == -1) e(29);
137 if (umask(0423) != 0124) e(30);
138 exit(0);
139 default:
140 if (wait(&stat_loc) == -1) e(31);
142 if (umask(0342) != 0124) e(32);
144 /* See if extra bits are ignored */
145 if (umask(0xFFFF) != 0342) e(33);
146 if (umask(0xFE00) != 0777) e(34);
147 if (umask(01777) != 0000) e(35);
148 if (umask(0022) != 0777) e(36);
151 int mode(arg)
152 char *arg;
153 { /* return the file mode. */
154 struct stat st;
155 Stat(arg, &st);
156 return st.st_mode & 0777;
159 int umode(arg)
160 char *arg;
161 { /* return the umask used for this file */
162 return 0777 ^ mode(arg);