Umask

umask 應用的值從何而來

  • March 10, 2015

我正在嘗試正確理解 umask。

如果我將 umask 設置為 0000 然後創建一個文件,我將獲得以下權限:

-rw-rw-rw-

我認為這個值(或一組權限)是 umask 遮罩所應用的。

是什麼決定了這個未屏蔽或原始值是什麼?換句話說:umask 應用於什麼值?

謝謝你的幫助。

為了讓您開始 - 從**OPEN(2)**手冊頁 ( man -S2 open) :

   O_CREAT
          If  the file does not exist it will be created.  The owner (user
          ID) of the file is set to the effective user ID of the  process.
          The  group  ownership  (group ID) is set either to the effective
          group ID of the process or to the group ID of the parent  direc‐
          tory  (depending  on  filesystem type and mount options, and the
          mode of the parent directory, see the  mount  options  bsdgroups
          and sysvgroups described in mount(8)).

          mode specifies the permissions to use in case a new file is cre‐
          ated.  This argument must be supplied when O_CREAT is  specified
          in  flags;  if  O_CREAT  is not specified, then mode is ignored.
          The effective permissions are modified by the process's umask in
          the   usual  way:  The  permissions  of  the  created  file  are
          (mode & ~umask).  Note that this mode  applies  only  to  future
          accesses of the newly created file; the open() call that creates
          a read-only file may well return a read/write file descriptor.

如果您strace使用touch命令創建新文件,您將看到傳遞給的模式open()是 0666(即-rw-rw-rw-)。umask 遮罩將應用於該模式。

$ strace -e open touch my-new-file
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
open("my-new-file", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3
+++ exited with 0 +++
$ 

引用自:https://unix.stackexchange.com/questions/189375