The umask function creates a blocked word for the process setting file mode and returns the previous value. The function is defined as follows:
# Include <sys/stat. h> mode_t umask (mode_t cmask); // The function only returns a successful result: a blocked word is created in the previous file mode and no error is returned.
The value of cmask is the same as that of mode in open and creat functions:
If one of the blocked characters in file mode is set, the permission for this bit in the new file is disabled. If S_IRUSR | S_IWUSR is set in cmask,
The user's read and write operations are blocked in the new file.
In linux, you can run the umask command to view the setting file mode of the Current shell to create a blocking word.
Yan @ yan-vm :~ $ Umask
0002
What is the meaning of 0002? first look:
Therefore, 0002 blocks other write permissions. If it is 0077, the Group read/write execution and other read/write operations are blocked.
Practice:
#include <stdio.h>#include <sys/stat.h>#include <fcntl.h>int main(void){ umask(0); if(creat("a",S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)<0){ perror("creat1"); return -1; } umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); if(creat("b",S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)<0){ perror("creat2"); return -1; } return 0;}
Running result:
Yan @ yan-vm :~ /Apue $ ll a B
-Rw-1 yan 0 Jun 12 13:02
-Rw ------- 1 yan 0 Jun 12 13:02 B
Yan @ yan-vm :~ /Apue $ umask
0002
Finally, it is found that the umask value (0066) in the program does not take effect in the shell, because after the program is executed, the shell generates a sub-process,
In the child process, umask is set to 0066. After the child process ends, it returns to the parent process (shell). The shell umaks are still 0002.
int main(void){ umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH|S_ISUID|S_ISGID); if(creat("a",S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH|S_ISUID|S_ISGID)<0){ perror("creat"); return -1; } return 0;}
-RwS -- S --- 1 yan 0 Jun 12 13:32
The final conclusion is that umask still uses the nine parameters in the first figure. S_ISUID, S_ISGID, and S_ISVTX cannot be used.