Question

I want to create a file using the open().

Here is the part of my code :

int fd = open("testing", O_EXCL | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
    if(fd == -1){
        perror("Error");
        return 1; }

The output should be a file named "testing" is created, with Read, Write and Execute permission for user, group and others.

But what I get is a file named "testing", with this permission :

-rwxr-xr-x

The write permission for Group and Others are missing.

I also tried using

S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH |S_IXOTH)

But I still get the same result.

How can I solve this problem?

Était-ce utile?

La solution

Permissions are modified by the process' umask. Effective permissions are (mode & ~umask).

You should change permissions after creating the file, using fchmod(). fchmod() avoids race conditions you could have if you used chmod() and the underlying file changed to point to other inode between the open() and the chmod() (google for TOCTOU races).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top