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?

有帮助吗?

解决方案

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).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top