NEXT UP previous
Next: mount System Call

umask System Call

You will remember that the umask command is used to set up the file creation mask. The umask value is used every time a file is created as a security measure to make sure that the processes you run cannot create new files with any more access permission than you want to give. This is done by having the permission bits in the umask value reset in the mode bits of any file that gets created on your behalf.

The umask command is implemented internally by a call to an underlying system call of the same name.

	#include <sys/types.h>
	#include <sys/stat.h>

	mode_t umask(mode_t mask);

A call to the umask() system call sets the file creation mask to the specified mask value. Only the permission bits in mask are saved, the values of the other bits are ignored.

The return value from umask() is just the previous value of the file creation mask, so that this system call can be used both to get and set the required values. Sadly, however, there is no way to get the old umask value without setting a new value at the same time.

This means that in order just to see the current value, it is necessary to execute a piece of code like the following function:

	#include <sys/types.h>
	#include <sys/stat.h>

	mode_t get_umask()
	{
		mode_t old;

		old = umask((mode_t)O);
		(void) umask(old); 
		return old;
	}

The first line of this function sets the umask value to zero, and as a side effect returns the value of interest, which then gets assigned to the variable old. The second line then restores the umask to its original value. And the final line returns the required umask value to the caller.


NEXT UP previous
Next: mount System Call