Thread: open() file creation weirdness

  1. #1
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218

    open() file creation weirdness

    I think I'm losing my mind. Someone please tell me why "testfile" is being created with such an insane mode. If I specifically pass something like 0644 as the mode to open() it creates the file as expected, but shouldn't that be the default? And the same thing happens on my other linux box too!

    (This is a little test program I wrote since I was having a similar problem in a different program that was creating files with mode -------r-- )

    Code:
    itsme@itsme:~/C/testdir$ cat opentest.c
    #include <stdio.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    int main(void)
    {
      int fd;
    
      fd = open("testfile", O_WRONLY|O_TRUNC|O_CREAT);
      close(fd);
    
      return 0;
    }
    Code:
    itsme@itsme:~/C/testdir$ gcc -Wall opentest.c -o opentest
    itsme@itsme:~/C/testdir$ ls -al
    total 24
    drwxr-xr-x  2 itsme users  4096 2004-09-27 13:20 ./
    drwxr-xr-x  5 itsme users  4096 2004-09-27 13:19 ../
    -rwxr-xr-x  1 itsme users 10690 2004-09-27 13:20 opentest*
    -rw-------  1 itsme users   211 2004-09-27 13:19 opentest.c
    itsme@itsme:~/C/testdir$ ./opentest
    itsme@itsme:~/C/testdir$ ls -al
    total 24
    drwxr-xr-x  2 itsme users  4096 2004-09-27 13:20 ./
    drwxr-xr-x  5 itsme users  4096 2004-09-27 13:19 ../
    -rwxr-xr-x  1 itsme users 10690 2004-09-27 13:20 opentest*
    -rw-------  1 itsme users   211 2004-09-27 13:19 opentest.c
    -r-x--s--T  1 itsme users     0 2004-09-27 13:20 testfile*
    itsme@itsme:~/C/testdir$ umask
    0022
    itsme@itsme:~/C/testdir$ touch foo
    itsme@itsme:~/C/testdir$ ls -al
    total 24
    drwxr-xr-x  2 itsme users  4096 2004-09-27 13:22 ./
    drwxr-xr-x  5 itsme users  4096 2004-09-27 13:19 ../
    -rw-r--r--  1 itsme users     0 2004-09-27 13:22 foo
    -rwxr-xr-x  1 itsme users 10690 2004-09-27 13:20 opentest*
    -rw-------  1 itsme users   211 2004-09-27 13:19 opentest.c
    -r-x--s--T  1 itsme users     0 2004-09-27 13:20 testfile*
    itsme@itsme:~/C/testdir$
    This is what's making me think it should have a sane mode if I leave it unspecified in my program:
    Code:
           O_CREAT
                  If the file does not exist it will be created.  The
                  owner (user ID) of the file is set to the effective
                  user  ID of the process. The group ownership (group
                  ID) is set either to the effective group ID of  the
                  process  or to the group ID of the parent directory
                  (depending on filesystem type  and  mount  options,
                  and  the  mode  of the parent directory, see, e.g.,
                  the mount options bsdgroups and sysvgroups  of  the
                  ext2 filesystem, as described in mount(8)).
    Last edited by itsme86; 09-27-2004 at 02:44 PM.
    If you understand what you're doing, you're not learning anything.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Jump in if I'm wrong here anyone, but open() should inherit the default permissions from its parent directory.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by master5001
    Jump in if I'm wrong here anyone, but open() should inherit the default permissions from its parent directory.
    Yeah, I just added that last quote to my original post from 'man open'
    If you understand what you're doing, you're not learning anything.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Oops well I guess I posted at the same time you fixed or something. You can override those permissions by just or'ing in what you want.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Yeah, I already tested passing 0644 in as the 3rd argument to open() to see if it gave me what I expected (-rw-r--r--) and indeed it did. What I'm wondering is why I'm needing to do that.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well itsme86, open() is a more system level type of function. I think there is actually a header that defines different permissions, come to think of it. I find its just faster to do 0644 when you know it and every other programmer in the world knows what you are wanting. I know my answer isn't quite what you are looking for, but its the truth. Usually open() is used by more savvy individuals who are well aware of the file permission masks anyway.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're right, it would just be faster to pass along the mode specifically instead of letting open() figure out which one to use on its own. But I just can't let go of it that easy. I have this need to know why and how everything works so if it doesn't look like my logic is wrong I might have to break into the open() code to see if I can figure it out from there.

    I appreciate the answer though. At least I know it's not something stupid and trivial that I just wasn't thinking of. Unless we both just weren't thinking of it, but let's not go there
    If you understand what you're doing, you're not learning anything.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fd = open("testfile", O_WRONLY|O_TRUNC|O_CREAT);
    C does not have default parameters, so you have to explicitly pass a mode parameter when you use an option which uses the mode parameter.
    Otherwise it just uses whatever is next on the stack and you get the sort of whacky fun which you've seen.

    Another example
    printf( "The value is %d\n" ); // oops, didn't pass enough parameters
    C doesn't know (or care), it just does what you tell it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Hmm...good point. How would it know if I passed a parameter or not? You'd think that would be specifically mentioned in the man page. And you'd also think they'd have a proper prototype for open() like int open(const char *filename, int flags, mode_t mode); instead of leaving it open like int open();. And if the problem isn't with the prototype then why doesn't gcc complain about passing too few parameters? Anyway, thanks
    If you understand what you're doing, you're not learning anything.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    My man page has
    Code:
           int open(const char *pathname, int flags);
           int open(const char *pathname, int flags, mode_t mode);
    And then later on, it says
    Code:
           mode  must  be  specified  when O_CREAT is in the flags, and is ignored
           otherwise.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'm batting a thousand today...*hangs his head*

    Thanks again.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM