Is it possible to get current umask of a process? From /proc/<pid>/...
for example?
3 Answers
Beginning with Linux kernel 4.7 (commit), the umask is available in /proc/<pid>/status
.
$ grep '^Umask:' "/proc/$$/status"
Umask: 0022
-
1
-
Yes, and RHEL7.4 is 3.10.0, so I do not understand the comment about 4.7.– hagelloFeb 18, 2020 at 10:54
-
Right, some older kernels do not provide info about the umask, for example 2.6.18. However, the featureis already there in 3.10.0. Thus, you should not say that this solution does not work before kernel 4.7.– hagelloFeb 18, 2020 at 11:01
-
1Stéphane was kind enough to edit my post to link to the commit that clearly says when it was added, it's much newer than 3.10. Maybe it appeared much earlier in RHEL's patched kernel, but not yet in the mainline kernel, I don't know.– egmontFeb 18, 2020 at 14:08
-
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.
-
1Just 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. Mar 7, 2018 at 15:12
-
(gdb) call/o umask(0)
gives me'umask' has unknown return type; cast the call to its declared return type
. May 19 at 7:06
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.