umask

引き続きAPUE Chapter4を読んでおります。今回はSection4.8 umaskについて。

The umask() routine sets the process's file mode creation mask to cmask and returns the previous value of the mask. The 9 low-order access permission bits of cmask are used by system calls, including open(2), mkdir(2), mkfifo(2), and mknod(2) to turn off corresponding bits requested in file mode. (See chmod(2)). This clearing allows each user to restrict the default access to his files.

The default mask value is S_IWGRP | S_IWOTH (022, write access for the owner only). Child processes inherit the mask of the calling process.

umask(2)のmanをみると、プロセスのfile mode creation maskをセットして、openやmkdirシステムコールを実行する時に、対応するbitがoffになるよ、ということが書かれております。


ほとんど4.8 Exampleそのまんまですが...

#include <stdio.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
 
#define RWRWRW (S_IRUSR |S_IWUSR |S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)   
 
int main(int argc, char** argv)  
{
	int fd; 
 
	umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);   
	fd = open("/tmp/umask_test", (O_WRONLY|O_CREAT|O_TRUNC), RWRWRW);   
	close(fd); 
}

を実行すると...

$ ls -la /tmp/umask_test
-rw-------  1 kotaro  wheel  0  3 27 09:08 /tmp/umask_test

open(2)の第3引数からumaskで指定したbitが落ちます。