linux:获取已运行进程的umask?

发布于 2024-07-06 18:53:58 字数 56 浏览 7 评论 0原文

如何检查当前正在运行的程序的umask?

[更新:另一个进程,不是当前进程。]

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

以为你会在 2024-07-13 18:53:58

您可以将 gdb 附加到正在运行的进程,然后在调试器中调用 umask:(

(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) 

注意:在此示例中,18 对应于 O22 的 umask)

这表明可能有一种非常丑陋的方法来获取使用 ptrace 的 umask。

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.

温柔戏命师 2024-07-13 18:53:58

从 Linux 内核 4.7 开始,umask 在 /proc//status 中可用。

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

失退 2024-07-13 18:53:58

来自 GNU C 库手册:

这里是一个示例,展示如何使用umask读取掩码
而不永久更改它:

<前><代码>mode_t
read_umask(空)
{
mode_t mask = umask(0);
umask(掩码);
返回掩码;
}

但是,如果您只想阅读,最好使用getumask
掩码值,因为它是可重入的(至少如果您使用
GNU 操作系统)。

不过,getumask 是 glibc 特定的。 因此,如果您重视可移植性,那么不可重入的解决方案是唯一的解决方案。

编辑:我刚刚在 Linux 源代码中查找了 ->umask 。 没有任何地方可以让您获得不同进程的 umask。 另外,没有getummask; 显然这是赫德独有的事情。

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.

尘世孤行 2024-07-13 18:53:58

如果您是当前进程,则可以将文件写入 /tmp 并检查其设置。 更好的解决方案是调用 umask(3) 并传递零 - 该函数返回调用之前的设置 - 然后通过将该值传递回 umask 来重置它。

另一个进程的 umask 似乎没有暴露。

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.

梦纸 2024-07-13 18:53:58

一位同事刚刚向我展示了这个命令行模式。 我总是运行 emacs,所以如下例所示。 perl 是我的贡献:

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

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+)$/)'
2024-07-13 18:53:58

至少在内核 4.18 中,有一个选项可以搜索状态 proc 文件:
grep Umask /proc//status

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文