test22.c

Go to the documentation of this file.
00001 /* test22: umask()              (p) Jan-Mark Wams. email: jms@cs.vu.nl */
00002 
00003 #include <sys/types.h>
00004 #include <sys/stat.h>
00005 #include <errno.h>
00006 #include <fcntl.h>
00007 #include <unistd.h>
00008 #include <stdlib.h>
00009 #include <sys/wait.h>
00010 #include <stdio.h>
00011 
00012 #define MAX_ERROR 4             /* Stop after ``MAX_ERROR'' errors. */
00013 #define ITERATIONS 2
00014 
00015 #define System(cmd)     if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
00016 #define Chdir(dir)      if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
00017 #define Stat(a,b)       if (stat(a,b) != 0) printf("Can't stat %s\n", a)
00018 
00019 int errct = 0;                  /* Total error counter. */
00020 int subtest = 1;
00021 
00022 _PROTOTYPE(void main, (int argc, char *argv[]));
00023 _PROTOTYPE(void test22a, (void));
00024 _PROTOTYPE(int mode, (char *filename));
00025 _PROTOTYPE(int umode, (char *filename));
00026 _PROTOTYPE(void e, (int number));
00027 _PROTOTYPE(void quit, (void));
00028 
00029 void main(argc, argv)
00030 int argc;
00031 char *argv[];
00032 {
00033   int i, m = 0xFFFF;
00034 
00035   sync();
00036   if (argc == 2) m = atoi(argv[1]);
00037   printf("Test 22 ");
00038   fflush(stdout);
00039   system("chmod 777 DIR_22/* DIR_22/*/* > /dev/null 2>&1");
00040   System("rm -rf DIR_22; mkdir DIR_22");
00041   Chdir("DIR_22");
00042 
00043   for (i = 0; i < ITERATIONS; i++) {
00044         if (m & 0001) test22a();
00045   }
00046 
00047   quit();
00048 }
00049 
00050 void test22a()
00051 {
00052   int fd1, fd2;
00053   int i, oldmask;
00054   int stat_loc;                 /* For the wait sys call. */
00055 
00056   subtest = 1;
00057 
00058   system("chmod 777 ../DIR_22/* ../DIR_22/*/* > /dev/null 2>&1");
00059   System("rm -rf ../DIR_22/*");
00060 
00061   oldmask = 0123;               /* Set oldmask and umask. */
00062   umask(oldmask);               /* Set oldmask and umask. */
00063 
00064   /* Check all the possible values of umask. */
00065   for (i = 0000; i <= 0777; i++) {
00066         if (oldmask != umask(i)) e(1);  /* set umask() */
00067         fd1 = open("open", O_CREAT, 0777);
00068         if (fd1 != 3) e(2);     /* test open(), */
00069         fd2 = creat("creat", 0777);
00070         if (fd2 != 4) e(3);     /* creat(), */
00071         if (mkdir("dir", 0777) != 0) e(4);      /* mkdir(), */
00072         if (mkfifo("fifo", 0777) != 0) e(5);    /* and mkfifo(). */
00073 
00074         if (umode("open") != i) e(6);   /* see if they have */
00075         if (umode("creat") != i) e(7);  /* the proper mode */
00076         if (umode("dir") != i) e(8);
00077         if (umode("fifo") != i) e(9);
00078 
00079         /* Clean up */
00080         if (close(fd1) != 0) e(10);
00081         if (close(fd2) != 0) e(11);     /* close fd's and */
00082         unlink("open");         /* clean the dir */
00083         unlink("creat");
00084         rmdir("dir");
00085         unlink("fifo");
00086         oldmask = i;            /* save current mask */
00087   }
00088 
00089   /* Check-reset mask */
00090   if (umask(0124) != 0777) e(12);
00091 
00092   /* Check if a umask of 0000 leaves the modes alone. */
00093   if (umask(0000) != 0124) e(13);
00094   for (i = 0000; i <= 0777; i++) {
00095         fd1 = open("open", O_CREAT, i);
00096         if (fd1 != 3) e(14);    /* test open(), */
00097         fd2 = creat("creat", i);
00098         if (fd2 != 4) e(15);    /* creat(), */
00099         if (mkdir("dir", i) != 0) e(16);        /* mkdir(), */
00100         if (mkfifo("fifo", i) != 0) e(17);      /* and mkfifo(). */
00101 
00102         if (mode("open") != i) e(18);   /* see if they have */
00103         if (mode("creat") != i) e(19);  /* the proper mode */
00104         if (mode("dir") != i) e(20);
00105         if (mode("fifo") != i) e(21);
00106 
00107         /* Clean up */
00108         if (close(fd1) != 0) e(22);
00109         if (close(fd2) != 0) e(23);
00110         if (unlink("open") != 0) e(24);
00111         unlink("creat");
00112         rmdir("dir");
00113         unlink("fifo");
00114   }
00115 
00116   /* Check if umask survives a fork() */
00117   if (umask(0124) != 0000) e(25);
00118   switch (fork()) {
00119       case -1:  fprintf(stderr, "Can't fork\n");        break;
00120       case 0:
00121         mkdir("bar", 0777);     /* child makes a dir */
00122         exit(0);
00123       default:
00124         if (wait(&stat_loc) == -1) e(26);
00125   }
00126   if (umode("bar") != 0124) e(27);
00127   rmdir("bar");
00128 
00129   /* Check if umask in child changes umask in parent. */
00130   switch (fork()) {
00131       case -1:  fprintf(stderr, "Can't fork\n");        break;
00132       case 0:
00133         switch (fork()) {
00134             case -1:
00135                 fprintf(stderr, "Can't fork\n");
00136                 break;
00137             case 0:
00138                 if (umask(0432) != 0124) e(28);
00139                 exit(0);
00140             default:
00141                 if (wait(&stat_loc) == -1) e(29);
00142         }
00143         if (umask(0423) != 0124) e(30);
00144         exit(0);
00145       default:
00146         if (wait(&stat_loc) == -1) e(31);
00147   }
00148   if (umask(0342) != 0124) e(32);
00149 
00150   /* See if extra bits are ignored */
00151   if (umask(0xFFFF) != 0342) e(33);
00152   if (umask(0xFE00) != 0777) e(34);
00153   if (umask(01777) != 0000) e(35);
00154   if (umask(0022) != 0777) e(36);
00155 }
00156 
00157 int mode(arg)
00158 char *arg;
00159 {                               /* return the file mode. */
00160   struct stat st;
00161   Stat(arg, &st);
00162   return st.st_mode & 0777;
00163 }
00164 
00165 int umode(arg)
00166 char *arg;
00167 {                               /* return the umask used for this file */
00168   return 0777 ^ mode(arg);
00169 }
00170 
00171 void e(n)
00172 int n;
00173 {
00174   int err_num = errno;          /* Save in case printf clobbers it. */
00175 
00176   printf("Subtest %d,  error %d  errno=%d: ", subtest, n, errno);
00177   errno = err_num;
00178   perror("");
00179   if (errct++ > MAX_ERROR) {
00180         printf("Too many errors; test aborted\n");
00181         chdir("..");
00182         system("rm -rf DIR*");
00183         exit(1);
00184   }
00185   errno = 0;
00186 }
00187 
00188 void quit()
00189 {
00190   Chdir("..");
00191   system("chmod 777 ../DIR_22/* ../DIR_22/*/* > /dev/null 2>&1");
00192   System("rm -rf DIR_22");
00193 
00194   if (errct == 0) {
00195         printf("ok\n");
00196         exit(0);
00197   } else {
00198         printf("%d errors\n", errct);
00199         exit(1);
00200   }
00201 }

Generated on Fri Apr 14 22:57:33 2006 for minix by  doxygen 1.4.6