The umask function creates a blocked word for the process setting file mode and returns the previous value. Function prototype:
# Include <sys/stat. h>
Mode_t umask (mode_t cmask );
Cmask is composed of several bitwise OR constants listed in the following table.
S_IRUSR User Read
S_IWUSR User Write
S_IXUSR user execution
S_IRGRP Group read
S_IWGRP group write
S_IXGRP group execution
S_IROTH other reads
S_IWOTH other writes
S_IXOTH other executions
In Linux, the permissions of a file are divided into nine permissions in three groups: users, groups, and others. For example:
Ls-l test
-Rw-1 shmily 0 May 6 17:16 test
Indicates that the test file has the following permissions: User readable and writable, group readable and writable, and other readable and writable (r indicates readable w indicates writable x indicates executable-Indicates not set)
The three-digit octal number indicates that the test permission is 666.
----------------------------------------------------------------------------
Umask is mainly used to createFileHourSetOr block some permissions on the file.
Specify the permission of the file when creating a file. The last mode parameter of the open function is not the permission to be set. It must perform the following operations:
Mode &(~ Cmask)
For example, the 4_3 function in APUE
#include "apue.h"#include <fcntl.h> #define RWRWRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) int main(void){ umask(0); if (creat("foo", RWRWRW) < 0) err_sys("create error for foo"); umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if (creat("bar", RWRWRW) < 0) err_sys("creat error for bar"); return 0;}
In the initial cmask is 0, that is, 000 000 000
Set the mode to 666 when using the creat function.
Mode &(~ Cmask) = 110 110 110 & 111 111 111 = 110 110 110
So the permission for the foo file is rw-
Then cmask is 066, that is, 000 110 110
The mode of the crear function is still 666.
Mode &(~ Cmask) = 110 110 110 & 111 001 001 = 110 000 000
So the bar File Permission is rw -------
Shmily @ pc-Shmily :~ /Code/UnixCode $ ls-l foo bar
-Rw ------- 1 shmily 0 May 6 17:16 bar
-Rw-1 shmily 0 May 6 17:16 foo