Is it possible to get current umask of a process? From /proc/<pid>/... for example?


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}'

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.

  • 1
    Just so this answer also shows up when googling those terms, it also explains how to modify umask of running process (since getting it requires temporarily changing it). I initially dismissed it when searching this. – Hugues M. Mar 7 '18 at 15:12

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

stap -e 'probe kernel.function("do_task_stat") {
           printf("%o\n", $task->fs->umask);
         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.

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.