2

I'm trying to understand umask properly.

If I set umask to 0000 and then create a file I get the following permissions:

-rw-rw-rw-

I presume that this value (or set of permissions) is what the umask mask is applied to.

What decides what this unmasked or raw value is? In other words: what value is the umask applied to?

Thanks for any help.

  • "Duplicate" of Where do default file permissions before umask come from? (on Super User). – Scott Mar 11 '15 at 4:15
  • You are right this does look like a duplicate and I didn't manage to find the other question before asking this. However the answer provided is not as good as the answer provided here. – bplus Mar 13 '15 at 14:47
2

To get you started - from the OPEN(2) man page (man -S2 open) :

   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  direc‐
          tory  (depending  on  filesystem type and mount options, and the
          mode of the parent directory, see the  mount  options  bsdgroups
          and sysvgroups described in mount(8)).

          mode specifies the permissions to use in case a new file is cre‐
          ated.  This argument must be supplied when O_CREAT is  specified
          in  flags;  if  O_CREAT  is not specified, then mode is ignored.
          The effective permissions are modified by the process's umask in
          the   usual  way:  The  permissions  of  the  created  file  are
          (mode & ~umask).  Note that this mode  applies  only  to  future
          accesses of the newly created file; the open() call that creates
          a read-only file may well return a read/write file descriptor.

If you strace a touch command to create a new file, you'll see the mode passed to open() is 0666 (i.e. -rw-rw-rw-). The umask mask will be applied to that mode.

$ strace -e open touch my-new-file
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
open("my-new-file", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3
+++ exited with 0 +++
$ 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.