source: trunk/minix/test/test35.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: 9.8 KB
Line 
1/* test35: utime() Author: Jan-Mark Wams (jms@cs.vu.nl) */
2
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <sys/wait.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <string.h>
9#include <fcntl.h>
10#include <limits.h>
11#include <utime.h>
12#include <errno.h>
13#include <time.h>
14#include <ctype.h>
15#include <stdio.h>
16
17#define MAX_ERROR 1
18#define ITERATIONS 10
19#define N 100
20
21#define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
22#define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
23#define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
24#define Mkfifo(f) if (mkfifo(f,0777)!=0) printf("Can't make fifo %s\n", f)
25#define Mkdir(f) if (mkdir(f,0777)!=0) printf("Can't make dir %s\n", f)
26#define Creat(f) if (close(creat(f,0777))!=0) printf("Can't creat %s\n",f)
27#define Time(t) if (time(t) == (time_t)-1) printf("Time error\n")
28#define Chown(f,u,g) if (chown(f,u,g) != 0) printf("Can't chown %s\n", f)
29#define Chmod(f,m) if (chmod(f,m) != 0) printf("Can't chmod %s\n", f)
30
31#define PASSWD_FILE "/etc/passwd"
32
33int errct = 0;
34int subtest = 1;
35int I_can_chown;
36int superuser;
37char MaxName[NAME_MAX + 1]; /* Name of maximum length */
38char MaxPath[PATH_MAX]; /* Same for path */
39char NameTooLong[NAME_MAX + 2]; /* Name of maximum +1 length */
40char PathTooLong[PATH_MAX + 1]; /* Same for path, both too long */
41
42_PROTOTYPE(void main, (int argc, char *argv[]));
43_PROTOTYPE(void test35a, (void));
44_PROTOTYPE(void test35b, (void));
45_PROTOTYPE(void test35c, (void));
46_PROTOTYPE(void makelongnames, (void));
47_PROTOTYPE(void e, (int number));
48_PROTOTYPE(void quit, (void));
49_PROTOTYPE(void getids, (uid_t * uid, gid_t * gid));
50
51void main(argc, argv)
52int argc;
53char *argv[];
54{
55 int i, m = 0xFFFF;
56
57 sync();
58 if (argc == 2) m = atoi(argv[1]);
59 printf("Test 35 ");
60 fflush(stdout);
61 System("rm -rf DIR_35; mkdir DIR_35");
62 Chdir("DIR_35");
63 makelongnames();
64 superuser = (geteuid() == 0);
65
66#ifdef _POSIX_CHOWN_RESTRICTED
67# if _POSIX_CHOWN_RESTRICTED - 0 != -1
68 I_can_chown = superuser;
69# else
70 I_can_chown = 1;
71# endif
72#else
73# include "error, this case requires dynamic checks and is not handled"
74#endif
75
76 for (i = 0; i < ITERATIONS; i++) {
77 if (m & 0001) test35a();
78 if (m & 0002) test35b();
79 if (m & 0004) test35c();
80 }
81 quit();
82}
83
84void test35a()
85{ /* Test normal operation. */
86 struct stat st;
87 struct utimbuf ub;
88 time_t time1, time2;
89 int cnt;
90
91 subtest = 1;
92
93 /* Creat scratch file. */
94 Creat("foo");
95
96 /* Set file times back two seconds. */
97 Stat("foo", &st);
98 ub.actime = st.st_atime - 2;
99 ub.modtime = st.st_mtime - 2;
100 Time(&time1);
101 utime("foo", &ub);
102 Time(&time2);
103 Stat("foo", &st);
104 if (ub.actime != st.st_atime) e(1);
105 if (ub.modtime != st.st_mtime) e(2);
106
107 /* The status changed time sould be changed. */
108#ifndef V1_FILESYSTEM
109 if (st.st_ctime < time1) e(3);
110#endif
111 if (st.st_ctime > time2) e(4);
112
113 /* Add twenty seconds. */
114 Stat("foo", &st);
115 ub.actime = st.st_atime + 20;
116 ub.modtime = st.st_mtime + 20;
117 Time(&time1);
118 utime("foo", &ub);
119 Time(&time2);
120 Stat("foo", &st);
121 if (ub.actime != st.st_atime) e(5);
122 if (ub.modtime != st.st_mtime) e(6);
123 if (st.st_ctime < time1) e(7);
124#ifndef V1_FILESYSTEM
125 if (st.st_ctime > time2) e(8);
126#endif
127
128 /* Try 100 times to do utime in less than one second. */
129 cnt = 0;
130 do {
131 Time(&time1);
132 utime("foo", (struct utimbuf *) NULL);
133 Time(&time2);
134 } while (time1 != time2 && cnt++ < 100);
135 if (time1 == time2) {
136 Stat("foo", &st);
137 Time(&time2);
138 if (st.st_atime != time1) e(9);
139 if (st.st_mtime != time1) e(10);
140 } else {
141 Stat("foo", &st);
142 if (st.st_atime > time2) e(11);
143 if (st.st_mtime > time2) e(12);
144 Time(&time2);
145 if (st.st_atime < time1) e(13);
146 if (st.st_mtime < time1) e(14);
147 }
148 if (st.st_ctime < time1) e(15);
149 if (st.st_ctime > time2) e(16);
150
151 System("rm -rf ../DIR_35/*");
152}
153
154void test35b()
155{
156 subtest = 2;
157
158 /* MaxPath and MaxName checkup. */
159 Creat(MaxName);
160 MaxPath[strlen(MaxPath) - 2] = '/';
161 MaxPath[strlen(MaxPath) - 1] = 'a'; /* make ././.../a */
162 Creat(MaxPath);
163 if (utime(MaxName, NULL) != 0) e(1);
164 if (utime(MaxPath, NULL) != 0) e(2);
165
166 /* The owner doesn't need write permisson to set times. */
167 Creat("foo");
168 if (chmod("foo", 0) != 0) e(3);
169 if (utime("foo", NULL) != 0) e(4);
170 if (chmod("foo", 0777) != 0) e(5);
171 if (utime("foo", NULL) != 0) e(6);
172
173 System("rm -rf ../DIR_35/*");
174}
175
176void test35c()
177{
178 gid_t gid, gid2;
179 uid_t uid, uid2;
180 struct utimbuf ub;
181 int stat_loc;
182
183 subtest = 3;
184
185 /* Access problems. */
186 Mkdir("bar");
187 Creat("bar/tryme");
188 if (superuser) {
189 Chmod("bar", 0000); /* No search permisson at all. */
190 if (utime("bar/tryme", NULL) != 0) e(1);
191 }
192 if (!superuser) {
193 Chmod("bar", 0677); /* No search permisson. */
194 if (utime("bar/tryme", NULL) != -1) e(2);
195 if (errno != EACCES) e(3);
196 }
197 Chmod("bar", 0777);
198
199 if (I_can_chown) {
200 switch (fork()) {
201 case -1: printf("Can't fork\n"); break;
202 case 0:
203 alarm(20);
204
205 /* Get two differend non root uids. */
206 if (superuser) {
207 getids(&uid, &gid);
208 if (uid == 0) getids(&uid, &gid);
209 if (uid == 0) e(4);
210 }
211 if (!superuser) {
212 uid = geteuid();
213 gid = getegid();
214 }
215 getids(&uid2, &gid);
216 if (uid == uid2) getids(&uid2, &gid2);
217 if (uid == uid2) e(5);
218
219 /* Creat a number of files for root, user and user2. */
220 Creat("rootfile"); /* Owned by root. */
221 Chmod("rootfile", 0600);
222 Chown("rootfile", 0, 0);
223 Creat("user2file"); /* Owned by user 2, writeable. */
224 Chmod("user2file", 0020);
225 Chown("user2file", uid2, gid);
226 Creat("user2private"); /* Owned by user 2, privately. */
227 Chmod("user2private", 0600);
228 Chown("user2private", uid2, gid);
229
230 if (superuser) {
231 setgid(gid);
232 setuid(uid);
233 }
234
235 /* We now are user ``uid'' from group ``gid''. */
236 ub.actime = (time_t) 12345L;
237 ub.modtime = (time_t) 12345L;
238
239 if (utime("rootfile", NULL) != -1) e(6);
240 if (errno != EACCES) e(7);
241 if (utime("rootfile", &ub) != -1) e(8);
242 if (errno != EPERM) e(9);
243
244 if (utime("user2file", NULL) != 0) e(10);
245 if (utime("user2file", &ub) != -1) e(11);
246 if (errno != EPERM) e(12);
247
248 if (utime("user2private", NULL) != -1) e(13);
249 if (errno != EACCES) e(14);
250 if (utime("user2private", &ub) != -1) e(15);
251 if (errno != EPERM) e(16);
252
253 exit(errct ? 1 : 0);
254 default:
255 wait(&stat_loc);
256 if (stat_loc != 0) e(17); /* Alarm? */
257 }
258 }
259
260 /* Test names that are too long. */
261#ifdef _POSIX_NO_TRUNC
262# if _POSIX_NO_TRUNC - 0 != -1
263 /* Not exist might also be a propper response? */
264 if (utime(NameTooLong, NULL) != -1) e(18);
265 if (errno != ENAMETOOLONG) e(19);
266# else
267 Creat(NameTooLong);
268 if (utime(NameTooLong, NULL) != 0) e(20);
269# endif
270#else
271# include "error, this case requires dynamic checks and is not handled"
272#endif
273
274 /* Make PathTooLong contain ././.../a */
275 PathTooLong[strlen(PathTooLong) - 2] = '/';
276 PathTooLong[strlen(PathTooLong) - 1] = 'a';
277 Creat("a");
278 if (utime(PathTooLong, NULL) != -1) e(21);
279 if (errno != ENAMETOOLONG) e(22);
280
281 /* Non existing file name. */
282 if (utime("nonexist", NULL) != -1) e(23);
283 if (errno != ENOENT) e(24);
284
285 /* Empty file name. */
286 if (utime("", NULL) != -1) e(25);
287 if (errno != ENOENT) e(26);
288
289 System("rm -rf ../DIR_35/*");
290}
291
292void makelongnames()
293{
294 register int i;
295
296 memset(MaxName, 'a', NAME_MAX);
297 MaxName[NAME_MAX] = '\0';
298 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
299 MaxPath[i++] = '.';
300 MaxPath[i] = '/';
301 }
302 MaxPath[PATH_MAX - 1] = '\0';
303
304 strcpy(NameTooLong, MaxName); /* copy them Max to TooLong */
305 strcpy(PathTooLong, MaxPath);
306
307 NameTooLong[NAME_MAX] = 'a';
308 NameTooLong[NAME_MAX + 1] = '\0'; /* extend NameTooLong by one too many*/
309 PathTooLong[PATH_MAX - 1] = '/';
310 PathTooLong[PATH_MAX] = '\0'; /* inc PathTooLong by one */
311}
312
313void e(n)
314int n;
315{
316 int err_num = errno; /* Save in case printf clobbers it. */
317
318 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
319 errno = err_num;
320 perror("");
321 if (errct++ > MAX_ERROR) {
322 printf("Too many errors; test aborted\n");
323 chdir("..");
324 system("rm -rf DIR* > /dev/null 2>/dev/null");
325 exit(1);
326 }
327 errno = 0;
328}
329
330void quit()
331{
332 Chdir("..");
333 System("rm -rf DIR_35");
334
335 if (errct == 0) {
336 printf("ok\n");
337 exit(0);
338 } else {
339 printf("%d errors\n", errct);
340 exit(1);
341 }
342}
343
344/* Getids returns a valid uid and gid. Is used PASSWD FILE.
345** It assumes the following format for a passwd file line:
346** <user_name>:<passwd>:<uid>:<gid>:<other_stuff>
347** If no uids and gids can be found, it will only return 0 ids.
348*/
349void getids(r_uid, r_gid)
350uid_t *r_uid;
351gid_t *r_gid;
352{
353 char line[N];
354 char *p;
355 uid_t uid;
356 gid_t gid;
357 FILE *fp;
358 int i;
359
360 static uid_t a_uid[N]; /* Array for uids. */
361 static gid_t a_gid[N]; /* Array for gids. */
362 static int nuid = 0, ngid = 0;/* The number of user & group ids. */
363 static int cuid = 0, cgid = 0;/* The current id index. */
364
365 /* If we don't have any uids go read some from the passwd file. */
366 if (nuid == 0) {
367 a_uid[nuid++] = 0; /* Root uid and gid. */
368 a_gid[ngid++] = 0;
369 if ((fp = fopen(PASSWD_FILE, "r")) == NULL) {
370 printf("Can't open ");
371 perror(PASSWD_FILE);
372 }
373 while (fp != NULL && fgets(line, sizeof(line), fp) != NULL) {
374 p = strchr(line, ':');
375 if (p != NULL) p = strchr(p + 1, ':');
376 if (p != NULL) {
377 p++;
378 uid = 0;
379 while (isdigit(*p)) {
380 uid *= 10;
381 uid += (uid_t) (*p - '0');
382 p++;
383 }
384 if (*p != ':') continue;
385 p++;
386 gid = 0;
387 while (isdigit(*p)) {
388 gid *= 10;
389 gid += (gid_t) (*p - '0');
390 p++;
391 }
392 if (*p != ':') continue;
393 if (nuid < N) {
394 for (i = 0; i < nuid; i++)
395 if (a_uid[i] == uid) break;
396 if (i == nuid) a_uid[nuid++] = uid;
397 }
398 if (ngid < N) {
399 for (i = 0; i < ngid; i++)
400 if (a_gid[i] == gid) break;
401 if (i == ngid) a_gid[ngid++] = gid;
402 }
403 if (nuid >= N && ngid >= N) break;
404 }
405 }
406 if (fp != NULL) fclose(fp);
407 }
408
409 /* We now have uids and gids in a_uid and a_gid. */
410 if (cuid >= nuid) cuid = 0;
411 if (cgid >= ngid) cgid = 0;
412 *r_uid = a_uid[cuid++];
413 *r_gid = a_gid[cgid++];
414}
Note: See TracBrowser for help on using the repository browser.