umask() 什么时候有用?
umask(0);
fd = open("/dev/null", O_RDWR);
这是 man 2 umask
:
umask() sets the calling process’s file mode creation mask (umask) to mask & 0777.
但这对我来说没有意义,因为当我们调用 open
时,我们还将提供一个模式参数。
那么umask
有什么意义呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
umask 适用于文件系统操作中使用的所有模式。来自手册
open(2)
:因此,只需调用 umask,您就可以影响所有创建文件的模式。
当程序希望用户允许推翻其创建的文件/目录的默认授权时,通常使用此方法。偏执的用户(或 root)可以将 umask 设置为
0077
,这意味着即使您在open(2)
中指定0777
,只有当前用户将有权访问。The umask is applied to all modes used in file system operations. From the manual
open(2)
:So with a single call to umask, you can influence the mode of all create files.
This is usually used when a program wants the user to allow to overrule the default grants for files/directories it creates. A paranoid user (or root) can set the umask to
0077
which means that even if you specify0777
inopen(2)
, only the current user will have access.我知道这是一个老问题,但这是我的两分钱:
共享内存对象的权限
我试图创建一个共享内存对象,其中:
生成的共享内存没有在模式下设置的权限参数,所以我阅读了 shm_open 手册页,它引导我进入 open 函数 手册页 和那里说:
,因此我尝试使用以下命令修改 umask:
但它也不起作用,因此经过更多谷歌之后,我发现了这个 设置权限 gnu.org 中的文档
建议:
并使用 fchmod 我的功能按我想要的方式工作:)她是:
I know this is and old question but here is my two cents:
Permissions of shared memory object
I was trying to make a shared memory object, with:
The resulting shared memory did not have the permission set in mode argument, so I read the shm_open man page which led me to the open function man page and there it says:
So I tried to modify the umask with:
but it did not work either, so after more google I found this Setting Permission document in gnu.org
Which recommends:
and with fchmod my function worked as I wanted :) her it is:
引用这篇文章:
Citing this article:
大多数 Mac 开发人员(以及大多数软件测试人员)从他们还是婴儿的时候起,就把这个放在他们的 .cshrc 中。
但是,大多数最终用户不知道 umask,所以如果他们在计算机上创建一个新用户,并运行在您的应用程序中,您可能会在没有组读/写权限的情况下创建一堆日志文件之类的东西。
然后他们再次切换用户,突然你的应用程序无法运行。
出于这个原因,我们将其添加到我们的所有应用程序中。
我们在安全方面的经验法则是“我们希望用户能够使用我们的软件”。
Most Mac developers (and by extension most software testers), from the time they were babies, put this in their .cshrc
However, most end users don't know about umask, so if they create a new user on the machine, and run your app, you are likely to create a bunch of log files and whatnot without group read/write permissions.
Then they switch users again and suddenly your app doesn't work.
For this reason, we're adding this to all our apps.
Our rule-of-thumb when it comes to security is that "we want users to be able to use our software".