I opened a file in C and wrote it with this code:
int fd = open("soandso.txt", O_RDWR | O_CREAT); write(fd, buf_of_data, 3);
The file now has write permission only for me and that is not what I specified in open. What am I doing wrong?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
File creation permissions are modified by the
So, for example:
$ umask 0022 $ touch xyz $ ls -l xyz -rw-r--r-- 1 sweh sweh 0 Sep 22 08:37 xyz
The umask value matches the standard rwx pattern, and so a value of
----w--w- permissions from the newly created file.
We can change this:
$ umask 002 $ touch abc $ ls -l abc -rw-rw-r-- 1 sweh sweh 0 Sep 22 08:38 abc
Now the 002 mask only removes
To create files with no masking
$ umask 0 $ touch def $ ls -l def -rw-rw-rw- 1 sweh sweh 0 Sep 22 08:40 def
If you want your program to ignore the
umask value then you must explicitly call the
umask(2) function before calling
Note that when using
open(2) with the
O_CREAT flag then you should also pass a permission mode.
In this case the mode field is also modified via the
umask value so the actual mode of the creation is
(mode & ~umask).