Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux: getting umask of an already running process?

Tags:

linux

unix

umask

People also ask

How do I find the umask value in Linux?

To test the default umask value: Open a Terminal session and log in as the root user, or enter sudo su root to become root . If logged in as another user, enter sudo su root -c umask . If the value returned is not 0022, consult your system administrator to have the default value changed back to 0022.

What is the umask of a process?

Background. The umask , or 'user file creation mask', allows the user to influence the permissions given to newly created files and directories. Any bits that are set within the umask are automatically cleared within the file mode. Like file modes, umask s are normally written in octal.

What does umask 022 mean?

Brief summary of umask value meanings: umask 022 - Assigns permissions so that only you have read/write access for files, and read/write/search for directories you own. All others have read access only to your files, and read/search access to your directories.

What does umask 027 mean?

The 027 umask setting means that the owning group would be allowed to read the newly-created files as well. This moves the permission granting model a little further from dealing with permission bits and bases it on group ownership. This will create directories with permission 750.


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.


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


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.


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.


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+)$/)'