Current umask of a process with <pid>

Beginning with Linux kernel 4.7 (commit), the umask is available in /proc/<pid>/status.

$ grep '^Umask:' "/proc/$$/status"
Umask:  0022

Note: this answer applies to Linux kernels 4.6 and earlier. See @egmont's answer for newer versions of the kernel.

The umask is not exposed in procfs. There was an attempt to add it without much success.

There is way to get the umask using gdb, as has been explained here before:

$ gdb --pid=4321
(gdb) call/o umask(0)
$1 = 077
(gdb) call umask($1)
$3 = 0

Keep in mind that gdb stops the process and its threads, so the temporary change of umask is negligible.

If that's good for your case, you can use this oneliner:

$ gdb --batch -ex 'call/o umask(0)' -ex 'call umask($1)' --pid=4321 2> /dev/null | awk '$1 == "$1" {print $3}'
077

Another alternative is, if you can control the running process, to write the umask to a file, an output or something similar and get it from there.


On Linux, with systemtap (as root), you could do

stap -e 'probe kernel.function("do_task_stat") {
           printf("%o\n", $task->fs->umask);
           exit()
         }
         probe begin {system("cat /proc/4321/stat>/dev/null")}'

Doing a cat /proc/4321/stat would trigger that probe on do_task_stat where we can access the fs->umask field of the corresponding process' task_struct in the kernel.

Tags:

Linux

Procfs