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