Unix Proc Directory Unix Proc Directory unix unix

Unix Proc Directory


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

Here is an example:

$ cat /proc/self/statusName:   catState:  R (running)Tgid:   17618Pid:    17618PPid:   3083TracerPid:      0Uid:    500 500 500 500Gid:    500 500 500 500FDSize: 32Groups: 10 488 500 VmPeak:     4792 kBVmSize:     4792 kBVmLck:         0 kBVmHWM:       432 kBVmRSS:       432 kBVmData:      156 kBVmStk:        84 kBVmExe:        32 kBVmLib:      1532 kBVmPTE:        24 kBThreads:    1SigQ:   0/32268SigPnd: 0000000000000000ShdPnd: 0000000000000000SigBlk: 0000000000000000SigIgn: 0000000000000000SigCgt: 0000000000000000CapInh: 0000000000000000CapPrm: 0000000000000000CapEff: 0000000000000000Cpus_allowed:   00000003Mems_allowed:   1voluntary_ctxt_switches:    0nonvoluntary_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.


Why not just use "id -u"?


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