LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   open() file creation weirdness (https://www.linuxquestions.org/questions/programming-9/open-file-creation-weirdness-235902/)

itsme86 09-27-2004 03:49 PM

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


itsme86 09-27-2004 04:41 PM

Nevermind, problem found. Thanks.

The mode parameter is not optional like the man page leads you to believe.

jlliagre 09-28-2004 03:44 AM

The manual page is not that confusing on this subject:
Quote:

mode must be specified when O_CREAT is in the flags, and is ignored otherwise.


All times are GMT -5. The time now is 01:03 PM.