Why does open() create my file with the wrong permissions?

前端 未结 7 1691
天涯浪人
天涯浪人 2020-11-29 23:47

I am trying to read some text from a file and write it to another using open(), read() and write().

This is my open()

相关标签:
7条回答
  • 2020-11-30 00:02

    you can call umask(0); system call before using open(); system call to set your choices permissions to file correctly.

    0 讨论(0)
  • 2020-11-30 00:05

    This is kind of an old thread, but I think people should be aware of the "sys/stat.h" library. This includes a bunch of symbolic constants for setting permission bits.

    For example: To open a file with Read/Write permissions enabled for the user

    #include <fcntl.h>
    #include <sys/stat.h>
    
    open("Your/File/Path", O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
    

    where:

    S_IWUSR // Sets the Users Write bit
    S_IRUSR // Sets the Users Read bit
    

    This library includes a bunch of others, I won't list them all here but you can read up on it all here.

    Of course you can put in the octal values to set these bits, however some may argue that it is poor coding practice.

    0 讨论(0)
  • 2020-11-30 00:08

    open() takes a third argument which is the set of permissions, i.e.

    open(filename, O_RDWR|O_CREAT, 0666)
    

    0666 is an octal number, i.e. every one of the 6's corresponds to three permission bits

    6 = rw

    7 = rwx

    first three bits for owner permission, next three bits for group permission and next is for the world the first digit - represents that is file or directory. (0 - file, d - directory) here we used 0 means file

    It's a typical pitfall. The compiler allows you to leave the permission argument away because when you open an existing file the permission bits don't make sense. But when you forget the argument when you create a file, you get a random set of permissions, e.g. 0000 in your case (---).

    0 讨论(0)
  • 2020-11-30 00:09

    This question recently helped me out, so I wanted to do my part to add a bit more depth as to what's going on. Like it was stated before, you were missing the third argument to open(). However, the permissions you see aren't random; they're coming from the stack. Look at the following code snippet:

        asm("push $0");
        asm("push $0");
        asm("push $0");
        fd = open("base", O_RDWR|O_CREAT);
    

    Note the following result:

        ----------. 1 user user 4 Feb 26 08:21 base
    

    Let's change the first push to 1, i.e. execute permission:

        asm("push $1;push $0;push $0");
        fd = open("base", O_RDWR|O_CREAT);
    

    and we get:

        ---------x. 1 user user 4 Feb 26 08:25 base
    

    Change the push to 4, i.e. read permission, and mess with the other two values:

        asm("push $4;push $5;push $6");
        fd = open("base", O_RDWR|O_CREAT);
    

    and we get:

        -------r--. 1 user user 4 Feb 26 08:27 base
    

    Thus we can see the third value popped off the stack (first pushed) is what really matters. Finally for fun we can try 5 and then 50, which respectively result in:

        -------r-x. 1 user user 4 Feb 26 08:27 base
        ----rw----. 1 user user 4 Feb 26 08:28 base
    

    Hope this adds some clarity!

    0 讨论(0)
  • 2020-11-30 00:10

    Reading http://linux.die.net/man/2/open it seems you missed the mode parameter for open:

    mode must be specified when O_CREAT is in the flags, and is ignored otherwise. The argument mode specifies the permissions to use in case a new file is created.

    0 讨论(0)
  • 2020-11-30 00:11

    Actually umask() only filters permissions and does not set them. The typical umask() value is 0002 ("don't give away write permission to the world") and if your mode value in the open( "file", O_CREAT, 0777) gave all permissions, the resulting file would have 775 as its permssions.

    0 讨论(0)
提交回复
热议问题