Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open() not setting file permissions correctly [duplicate]

Tags:

c

posix

I create a file using the code below:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
    const char* filename = "./test.out";
    int fd;
    if(-1 == (fd = open(filename, O_CREAT|O_RDWR, 0666)))
    {
        perror("Error");
        errno = 0;
    }       
    else
        puts("File opened");

    if(-1 == (close(fd)))
    {
        perror("Error");
        errno = 0;
    }
    else
        puts("File closed");

    return 0;
}

I specify the mode argument as 0666, which should grant read,write access to everyone. However, an ls -l shows

-rw-r--r-- 1 kmehta users 0 2012-01-29 16:29 test.out

As you can see, write permissions are only granted to the owner of the file. I do not know why everyone else is not granted permissions correctly. chmod a+w test.out sets the permissions correctly though.

Code compiled as gcc -Wall test.c

Specs: gcc v 4.5.0 on Opensuse 11.3 64 bit

like image 897
jitihsk Avatar asked Jan 29 '12 22:01

jitihsk


People also ask

How do I give permission to 777 in Linux?

The command chmod -R 777 / makes every single file on the system under / (root) have rwxrwxrwx permissions. This is equivalent to allowing ALL users read/write/execute permissions. If other directories such as home, media, etc are under root then those will be affected as well.

How do I give permission to 644?

Change directory with cd command to the desired location under with you need to all directories to 755, and all files to 644 permissions. Then use first command to chmod 755 for all directories and sub directories. The second command will change all the files permission to 0644 (chmod 644) under the directory tree.

How do I copy permissions from one directory to another in Linux?

To copy file permissions from one file to another file, use chmod command with the --reference switch in the following syntax, where reference_file is the file from which permissions will be copied rather than specifying mode (i.e octal or numerical mode permissions) for file.

Do you need write permissions to copy?

For a real copy (as it is needed when you move the file to a different file system), you need read permissions on the file itself. No write permissions on the original are necessary, as deletion of a file is not writing to it (but to the directory it is in).


2 Answers

The mode argument to open specifies the maximum allowed permissions. The umask setting is then applied to further restrict the permissions.

If you need to make the permissions be 0666 specifically you will need to use fchmod on the file handle after the open succeeds or use umask to set the process’ permissions mask before the open.

like image 125
Zan Lynx Avatar answered Nov 15 '22 16:11

Zan Lynx


Executing this code :

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(void)
{
        int fd;
        if((fd = open("new.file",O_CREAT,S_IRWXU | S_IRWXG | S_IRWXO)) == -1)
        {
                perror("open");
                return 1;
        }
        close(fd);
        return 0;
}

on my Linux box, where umask returns 0022, gives me a file with the following attributes :

-rwxr-xr-x 1 daniel daniel 0 Jan 29 23:46 new.file

So, as you can see, the umask masks out the write bits in my case. It looks like it's the same on your system, too.

like image 40
Daniel Kamil Kozar Avatar answered Nov 15 '22 15:11

Daniel Kamil Kozar