[RndTbl] How does umask really work?

Sean A. Walberg sean at ertw.com
Wed Feb 9 21:14:39 CST 2005

>From the umask(2) man page: 

The umask is used by open(2)  to set initial file permissions on a
newly-created file.  Specifically, permissions in the umask are turned
off from the mode argument to open(2) (so, for example, the common
umask default value of 022 results in new files being created with
per-missions 0666 & ~022 = 0644 = rw-r--r-- in the usual case where the
mode is specified as 0666).

And that takes us to open(2):

int open(const char *pathname, int flags, mode_t mode);
The  argument  mode specifies the permissions to use in case a new file
is created. It is modified by the process's umask in the usual way: 
the permissions  of  the  created file are (mode & ~umask).  Note that 
this mode only applies 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.

So, it would stand that the umask is used within the kernel at file 
creation time.  If you poke around fs/namei.c in the kernel source, you 
can see how sys_mknod() uses this value to figure out the actual mode of 
the file.  You'd have to dig into glibc to find where the initial value 
comes from.  ISTR that Bill was on the right track -- directories and 
files start off with different modes for the umask calculation, 
directories are 777 and files 666.


On Wed, 9 Feb 2005, Kevin McGregor wrote:

> On second thought (and after checking one book on bash), there may not 
> be a "default". If a text editor tries to create a file with permissions 
> 666 and umask is 022, then it will be created with 644 instead. So it 
> seems to depend on the process creating the file. Gilbert confirms this.
> John Lange wrote:
> > The documentation and examples on umask state that you provide a mask
> > that is subtracted from rwx (777) access to generate a default access
> > for newly created files.
> > 
> > It clearly doesn't work this way.
> > 
> > If you have a umask of 022, newly created files are rw-r--r-- (644). It
> > should be rwxr-xr-x (755). The umask is acting like its 033 and indeed
> > changing it to 033 has no effect on newly created files.
> > 
> > However, umask effects directories differently. umask 022 will create
> > directories as expected (as 755), and umask 033 will create them as 644.
> > 
> > So how do you set a umask that will create files as 755?
> > 

Sean A. Walberg <sean at ertw.com>                    http://www.ertw.com 

More information about the Roundtable mailing list