I am creating a file with full permission (777) using the open
system call, but when I do ls -l
I can see only permission as (755). Could you please tell why file permission is not 777?
Code
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
int fd;
/* Creates a file with full permission*/
fd = open("test", O_CREAT | O_RDWR | O_APPEND, 0777);
if (fd = -1)
{
return -1;
}
close(fd);
}
Output
$ ls -l
-rwxr-xr-x 1 ubuntu ubuntu 0 2012-09-19 11:55 test
The permissions of a created file are restricted by the process's current
umask
-- your currentumask
is022
, so group and world write are always disabled by default. (Which is a good thing, in most cases.) If you really want a group- and world-writable file, you will need to temporarily setumask(0)
while creating this file (make sure to save the old value returned by the system call, and set it back afterwards!), or "manually" set the file's permissions usingchmod()
.umask
will return the original value of the mask, so to reset it temporarily you just need to dothough it would be perhaps preferable to use
fchmod
after opening the file - this will avoid the problem of changing process-global state:There's a value maintained by the system called the
umask
; it is a property of the process, just like the PID (process ID) or EUID (effective user ID) is. It will be set to022
(octal), which indicates that the system should remove the group and other write permission from files that are created.You can call
umask(0);
before usingopen()
so that the mode you specify inopen()
won't be altered. You should certainly do this to demonstrate thatumask
is the issue. However, it is generally best to let the user's choice ofumask
prevail — I for one get very stroppy if a program doesn't obey my umask setting; it tends not to be used again after I spot and verify the problem.The shell also has a (built-in) command
umask
which you can use. The022
value is a sensible default; most of the time, you do not want just anybody writing to your files.