source: trunk/minix/test/test22.c@ 20

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

Minix 3.1.2a

File size: 4.7 KB
Line 
1/* test22: umask() (p) Jan-Mark Wams. email: jms@cs.vu.nl */
2
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>
11
12#define MAX_ERROR 4 /* Stop after ``MAX_ERROR'' errors. */
13#define ITERATIONS 2
14
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)
18
19int errct = 0; /* Total error counter. */
20int subtest = 1;
21
22_PROTOTYPE(void main, (int argc, char *argv[]));
23_PROTOTYPE(void test22a, (void));
24_PROTOTYPE(int mode, (char *filename));
25_PROTOTYPE(int umode, (char *filename));
26_PROTOTYPE(void e, (int number));
27_PROTOTYPE(void quit, (void));
28
29void main(argc, argv)
30int argc;
31char *argv[];
32{
33 int i, m = 0xFFFF;
34
35 sync();
36 if (argc == 2) m = atoi(argv[1]);
37 printf("Test 22 ");
38 fflush(stdout);
39 system("chmod 777 DIR_22/* DIR_22/*/* > /dev/null 2>&1");
40 System("rm -rf DIR_22; mkdir DIR_22");
41 Chdir("DIR_22");
42
43 for (i = 0; i < ITERATIONS; i++) {
44 if (m & 0001) test22a();
45 }
46
47 quit();
48}
49
50void test22a()
51{
52 int fd1, fd2;
53 int i, oldmask;
54 int stat_loc; /* For the wait sys call. */
55
56 subtest = 1;
57
58 system("chmod 777 ../DIR_22/* ../DIR_22/*/* > /dev/null 2>&1");
59 System("rm -rf ../DIR_22/*");
60
61 oldmask = 0123; /* Set oldmask and umask. */
62 umask(oldmask); /* Set oldmask and umask. */
63
64 /* Check all the possible values of umask. */
65 for (i = 0000; i <= 0777; i++) {
66 if (oldmask != umask(i)) e(1); /* set umask() */
67 fd1 = open("open", O_CREAT, 0777);
68 if (fd1 != 3) e(2); /* test open(), */
69 fd2 = creat("creat", 0777);
70 if (fd2 != 4) e(3); /* creat(), */
71 if (mkdir("dir", 0777) != 0) e(4); /* mkdir(), */
72 if (mkfifo("fifo", 0777) != 0) e(5); /* and mkfifo(). */
73
74 if (umode("open") != i) e(6); /* see if they have */
75 if (umode("creat") != i) e(7); /* the proper mode */
76 if (umode("dir") != i) e(8);
77 if (umode("fifo") != i) e(9);
78
79 /* Clean up */
80 if (close(fd1) != 0) e(10);
81 if (close(fd2) != 0) e(11); /* close fd's and */
82 unlink("open"); /* clean the dir */
83 unlink("creat");
84 rmdir("dir");
85 unlink("fifo");
86 oldmask = i; /* save current mask */
87 }
88
89 /* Check-reset mask */
90 if (umask(0124) != 0777) e(12);
91
92 /* Check if a umask of 0000 leaves the modes alone. */
93 if (umask(0000) != 0124) e(13);
94 for (i = 0000; i <= 0777; i++) {
95 fd1 = open("open", O_CREAT, i);
96 if (fd1 != 3) e(14); /* test open(), */
97 fd2 = creat("creat", i);
98 if (fd2 != 4) e(15); /* creat(), */
99 if (mkdir("dir", i) != 0) e(16); /* mkdir(), */
100 if (mkfifo("fifo", i) != 0) e(17); /* and mkfifo(). */
101
102 if (mode("open") != i) e(18); /* see if they have */
103 if (mode("creat") != i) e(19); /* the proper mode */
104 if (mode("dir") != i) e(20);
105 if (mode("fifo") != i) e(21);
106
107 /* Clean up */
108 if (close(fd1) != 0) e(22);
109 if (close(fd2) != 0) e(23);
110 if (unlink("open") != 0) e(24);
111 unlink("creat");
112 rmdir("dir");
113 unlink("fifo");
114 }
115
116 /* Check if umask survives a fork() */
117 if (umask(0124) != 0000) e(25);
118 switch (fork()) {
119 case -1: fprintf(stderr, "Can't fork\n"); break;
120 case 0:
121 mkdir("bar", 0777); /* child makes a dir */
122 exit(0);
123 default:
124 if (wait(&stat_loc) == -1) e(26);
125 }
126 if (umode("bar") != 0124) e(27);
127 rmdir("bar");
128
129 /* Check if umask in child changes umask in parent. */
130 switch (fork()) {
131 case -1: fprintf(stderr, "Can't fork\n"); break;
132 case 0:
133 switch (fork()) {
134 case -1:
135 fprintf(stderr, "Can't fork\n");
136 break;
137 case 0:
138 if (umask(0432) != 0124) e(28);
139 exit(0);
140 default:
141 if (wait(&stat_loc) == -1) e(29);
142 }
143 if (umask(0423) != 0124) e(30);
144 exit(0);
145 default:
146 if (wait(&stat_loc) == -1) e(31);
147 }
148 if (umask(0342) != 0124) e(32);
149
150 /* See if extra bits are ignored */
151 if (umask(0xFFFF) != 0342) e(33);
152 if (umask(0xFE00) != 0777) e(34);
153 if (umask(01777) != 0000) e(35);
154 if (umask(0022) != 0777) e(36);
155}
156
157int mode(arg)
158char *arg;
159{ /* return the file mode. */
160 struct stat st;
161 Stat(arg, &st);
162 return st.st_mode & 0777;
163}
164
165int umode(arg)
166char *arg;
167{ /* return the umask used for this file */
168 return 0777 ^ mode(arg);
169}
170
171void e(n)
172int n;
173{
174 int err_num = errno; /* Save in case printf clobbers it. */
175
176 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
177 errno = err_num;
178 perror("");
179 if (errct++ > MAX_ERROR) {
180 printf("Too many errors; test aborted\n");
181 chdir("..");
182 system("rm -rf DIR*");
183 exit(1);
184 }
185 errno = 0;
186}
187
188void quit()
189{
190 Chdir("..");
191 system("chmod 777 ../DIR_22/* ../DIR_22/*/* > /dev/null 2>&1");
192 System("rm -rf DIR_22");
193
194 if (errct == 0) {
195 printf("ok\n");
196 exit(0);
197 } else {
198 printf("%d errors\n", errct);
199 exit(1);
200 }
201}
Note: See TracBrowser for help on using the repository browser.