umask(0);
fd = open("/dev/null", O_RDWR);
Here's man 2 umask
:
umask() sets the calling process’s file mode creation mask (umask) to mask & 0777.
But it doesn't make sense for me,as when we call open
,we will also provide a mode parameter.
So what's the point of umask
?
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.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:
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".