linux: getting umask of an already running process?

后端 未结 5 841
广开言路
广开言路 2021-02-01 17:04

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

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

相关标签:
5条回答
  • 2021-02-01 17:29

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

    0 讨论(0)
  • 2021-02-01 17:34

    A colleague just showed me a 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+)$/)'
    
    0 讨论(0)
  • 2021-02-01 17:41

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

    (gdb) call umask(0)
    [Switching to Thread -1217489200 (LWP 11037)]
    $1 = 18
    (gdb) call umask(18)
    $2 = 0
    (gdb) 
    

    (note: 18 = O22)

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

    0 讨论(0)
  • 2021-02-01 17:41

    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.

    0 讨论(0)
  • 2021-02-01 17:43

    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.

    0 讨论(0)
提交回复
热议问题