umask 所应用的值从何而来

umask 所应用的值从何而来

我试图正确理解 umask。

如果我将 umask 设置为 0000,然后创建一个文件,我将获得以下权限:

-RW-RW-RW-

我认为该值(或权限集)是 umask 掩码所应用的。

是什么决定了这个未掩盖的或原始的价值是什么?换句话说:umask 应用于什么值?

谢谢你的帮助。

答案1

让您开始 - 从打开(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 +++
$ 

相关内容