22

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

basin
  • 2,051
  • 1
    If you are not the faint of heart using gdb, there is a no-so-user-friendly way of getting this infor here: http://stackoverflow.com/questions/165212/linux-getting-umask-of-an-already-running-process – MelBurslan Jan 28 '16 at 15:09

3 Answers3

31

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

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

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.

zuazo
  • 3,042
  • 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 07 '18 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 '22 at 07: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.