umask 'relative to' 是什么?

What is the umask 'relative to'?

我知道 umask 是一个 'last stage filter',它确保在新创建的文件或目录上设置特定的权限位。我的问题是,它的作用是什么?输入权限集是如何确定的?

为了说明我的困惑,考虑这个:

为什么不同?

umask 由创建文件系统对象的某些系统调用解释。它通常与传递给系统调用的显式模式参数有关。例如,mkdir 是这样工作的:

int mkdir(const char *path, mode_t mode);

The file permission bits of the new directory shall be initialized from mode. These file permission bits of the mode argument shall be modified by the process' file creation mask.

并且 open 这样做:

int open(const char *path, int oflag, ... );

the access permission bits (see <sys/stat.h>) of the file mode shall be set to the value of the third argument taken as type mode_t modified as follows: a bitwise AND is performed on the file-mode bits and the corresponding bits in the complement of the process' file mode creation mask. Thus, all bits in the file mode whose corresponding bit in the file mode creation mask is set are cleared.

进程的 umask 由其子进程继承,当然任何进程都可以更改自己的 umask。

根据 OP 提供的信息,我假设 OP 运行从 shell:

执行以下命令
umask 000
mkdir ~/foo
sudo mkdir /foo

mkdir命令使用默认模式如下:

The value of the bitwise-inclusive OR of S_IRWXU, S_IRWXG, and S_IRWXO is used as the mode argument.

所以 S_IRWXU|S_IRWXG|S_IRWXO 是 0777,正如预期的那样,umask 为 000,~/foo 的模式将是 0777。

但是 sudo 总是设置一个 umask,要么是 /etc/sudoers 中列出的值,要么是编译时选择的默认值,在 Ubuntu 上是 022。所以 sudo mkdir /root/foo 将 运行 的 umask 为 022,结果目录为 0755。