32

How can I check the umask of a program which is currently running?

[update: another process, not the current process.]

Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
  • Of the current process? Or a foreign process? – C. K. Young Oct 03 '08 at 00:26
  • There was a [patch](https://lkml.org/lkml/2012/5/4/451) that went by for this a while ago, to report the process' umask in `/proc/pid/status` and `/proc/pid/stat`. But it doesn't seem to have gone into the mainline kernel. – Craig McQueen Nov 18 '15 at 04:13
  • Same question on [unix.se]: [Current umask of a process with pid](http://unix.stackexchange.com/q/258284) – Stephane Chazelas Jan 29 '16 at 23:06

6 Answers6

28

You can attach gdb to a running process and then call umask in the debugger:

(gdb) attach <your pid>
...
(gdb) call umask(0)
[Switching to Thread -1217489200 (LWP 11037)]
$1 = 18 # this is the umask
(gdb) call umask(18) # reset umask
$2 = 0
(gdb) 

(note: 18 corresponds to a umask of O22 in this example)

This suggests that there may be a really ugly way to get the umask using ptrace.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
16

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

egmont
  • 581
  • 3
  • 11
8

From the GNU C Library manual:

Here is an example showing how to read the mask with umask without changing it permanently:

mode_t
read_umask (void)
{
  mode_t mask = umask (0);
  umask (mask);
  return mask;
}

However, it is better to use getumask if you just want to read the mask value, because it is reentrant (at least if you use the GNU operating system).

getumask is glibc-specific, though. So if you value portability, then the non-reentrant solution is the only one there is.

Edit: I've just grepped for ->umask all through the Linux source code. There is nowhere that will get you the umask of a different process. Also, there is no getumask; apparently that's a Hurd-only thing.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • Yes, getumask is only on Hurd. The Linux man page has this helpful note: This function is documented in the glibc manual, but, as at glibc version 2.24, it is not implemented on Linux. – Pete Forman Apr 07 '17 at 08:30
2

If you're the current process, you can write a file to /tmp and check its setting. A better solution is to call umask(3) passing zero - the function returns the setting prior to the call - and then reset it back by passing that value back into umask.

The umask for another process doesn't seem to be exposed.

Josh
  • 7,936
  • 5
  • 41
  • 63
1

A colleague just showed me this command line pattern for this. I always have emacs running, so that's in the example below. The perl is my contribution:

sudo gdb --pid=$(pgrep emacs) --batch -ex 'call/o umask(0)' -ex 'call umask($1)' 2> /dev/null | perl -ne 'print("$1\n")if(/^\$1 = (\d+)$/)'
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
tbc0
  • 1,563
  • 1
  • 17
  • 21
0

At least with Kernel 4.18, there is an option to search the status proc file: grep Umask /proc/<PID>/status