Unix 过程目录

发布于 2024-07-05 16:54:31 字数 64 浏览 12 评论 0原文

我正在尝试查找包含当前用户 ID 的虚拟文件。 有人告诉我可以在 proc 目录中找到它,但不太确定是哪个文件。

I am trying to find the virtual file that contains the current users id. I was told that I could find it in the proc directory, but not quite sure which file.

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

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

发布评论

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

评论(7

谢绝鈎搭 2024-07-12 16:54:31

您实际上需要 /proc/self/status,它将为您提供有关当前执行进程的信息。

下面是一个示例:

$ cat /proc/self/status
Name:   cat
State:  R (running)
Tgid:   17618
Pid:    17618
PPid:   3083
TracerPid:      0
Uid:    500 500 500 500
Gid:    500 500 500 500
FDSize: 32
Groups: 10 488 500 
VmPeak:     4792 kB
VmSize:     4792 kB
VmLck:         0 kB
VmHWM:       432 kB
VmRSS:       432 kB
VmData:      156 kB
VmStk:        84 kB
VmExe:        32 kB
VmLib:      1532 kB
VmPTE:        24 kB
Threads:    1
SigQ:   0/32268
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000000000
SigCgt: 0000000000000000
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
Cpus_allowed:   00000003
Mems_allowed:   1
voluntary_ctxt_switches:    0
nonvoluntary_ctxt_switches: 3

您可能想查看 Uid 和 Gid 行上的第一个数字。 您可以通过查看 /etc/passwd 来查找哪些 uid 编号映射到哪个用户名,或者调用相关函数以将 uid 映射到您使用的任何语言的用户名。

理想情况下,您只需调用系统调用 getuid() 来查找此信息,通过查看 /proc/ 来执行此操作会适得其反。

You actually want /proc/self/status, which will give you information about the currently executed process.

Here is an example:

$ cat /proc/self/status
Name:   cat
State:  R (running)
Tgid:   17618
Pid:    17618
PPid:   3083
TracerPid:      0
Uid:    500 500 500 500
Gid:    500 500 500 500
FDSize: 32
Groups: 10 488 500 
VmPeak:     4792 kB
VmSize:     4792 kB
VmLck:         0 kB
VmHWM:       432 kB
VmRSS:       432 kB
VmData:      156 kB
VmStk:        84 kB
VmExe:        32 kB
VmLib:      1532 kB
VmPTE:        24 kB
Threads:    1
SigQ:   0/32268
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000000000
SigCgt: 0000000000000000
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
Cpus_allowed:   00000003
Mems_allowed:   1
voluntary_ctxt_switches:    0
nonvoluntary_ctxt_switches: 3

You probably want to look at the first numbers on the Uid and Gid lines. You can look up which uid numbers map to what username by looking at /etc/passwd, or calling the relevant functions for mapping uid to username in whatever language you're using.

Ideally, you would just call the system call getuid() to look up this information, doing it by looking at /proc/ is counterproductive.

坠似风落 2024-07-12 16:54:31

为什么不直接使用“id -u”?

Why not just use "id -u"?

も星光 2024-07-12 16:54:31

据我所知, /proc 是 Linux 特有的,一般来说 UNIX 中没有。 如果您确实只需要当前 UID,请使用 getuid()geteuid() 函数。

如果您知道自己只能使用 Linux,则可以浏览 /proc/self/* 下的层次结构,它包含有关当前进程的各种信息。 请记住,/proc 是“神奇的”,它是内核提供服务的虚拟文件系统,内容是在您请求时动态生成的。 因此它可以返回当前进程的特定信息。

例如,尝试以下命令:cat /proc/self/status

As far as I know, /proc is specific to Linux, it's not in UNIX in general. If you really just want the current UID, use the getuid() or geteuid() function.

If you know you'll be on Linux only, you can explore the hierarchy under /proc/self/*, it contains various information about the current process. Remember that /proc is "magical", it's a virtual filesystem the kernel serves and the contents is dynamically generated at the point you request it. Therefore it can return information specific for the current process.

For example, try this command: cat /proc/self/status

无言温柔 2024-07-12 16:54:31

我不确定是否可以在 /proc 中找到它。 您可以尝试使用 getuid() 函数或 $USER 环境变量。

I'm not sure that can be found in /proc. You could try using the getuid() function or the $USER environment variable.

刘备忘录 2024-07-12 16:54:31

/proc/process_id/status (至少在 Linux 上)你会发现这样一行:

Uid:      1000    1000    1000    1000

这告诉你进程所在帐户下的用户的 uid跑步。

然而,要找出当前进程的进程id,你需要一个系统调用,然后你不妨调用getuid直接获取uid。

编辑:啊,/proc/self/status...每天都学习新东西!

In /proc/process_id/status (at least on Linux) you'll find a line like this:

Uid:      1000    1000    1000    1000

This tells you the uid of the user under whose account the process is running.

However, to find out the process id of the current process you would need a system call, and then you might as well call getuid to get the uid directly.

Edit: ah, /proc/self/status... learning something new every day!

我也只是我 2024-07-12 16:54:31

最有可能的是,您想要检查 $USER 环境变量。 其他选项包括 getuid 和 id -u,但搜索 /proc 肯定不是最好的操作方法。

Most likely, you either want to check the $USER environment variable. Other options include getuid and id -u, but searching /proc is certainly not the best method of action.

听不够的曲调 2024-07-12 16:54:31

您要查找的内容可能在环境变量中。 检查环境变量时,您需要小心您使用的 shell。 bash 使用“UID”,而 tcsh 使用“uid”,并且在 *nix 情况下很重要。 我还发现 tcsh 设置了“gid”,但我无法在 bash 中找到匹配的变量。

The things you are looking for may be in environment variables. You need to be careful about what shell you are using when you check environment variables. bash uses "UID" while tcsh uses "uid" and in *nix case matters. I've also found that tcsh sets "gid" but I wasn't able to find a matching variable in bash.

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