19

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

1

3 Answers 3

29

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

$ grep '^Umask:' "/proc/$$/status"
Umask:  0022
5
  • 1
    This helped in RHEL 7.4! Oct 23, 2018 at 13:14
  • Yes, and RHEL7.4 is 3.10.0, so I do not understand the comment about 4.7.
    – hagello
    Feb 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.
    – hagello
    Feb 18, 2020 at 11:01
  • 1
    Sté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.
    – egmont
    Feb 18, 2020 at 14:08
  • This worked for me with Amazon Linux 2.
    – CODE-REaD
    May 5, 2020 at 17:29
17

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.

2
  • 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, 2018 at 15:12
  • (gdb) call/o umask(0) gives me 'umask' has unknown return type; cast the call to its declared return type.
    – Friedrich
    May 19 at 7:06
4

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.

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.