Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix Proc Directory

Tags:

linux

unix

procfs

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.

like image 291
Jose Vega Avatar asked Sep 18 '08 03:09

Jose Vega


People also ask

Where is the proc directory?

It does not exist on a disk. Instead, the kernel creates it in memory. It is used to provide information about the system (originally about processes, hence the name). Some of the more important files and directories are explained below.

What is proc file system in Unix?

The proc filesystem (procfs) is a special filesystem in Unix-like operating systems that presents information about processes and other system information in a hierarchical file-like structure, providing a more convenient and standardized method for dynamically accessing process data held in the kernel than traditional ...

What file in the proc directory contains?

Files in the /proc directory contain information about your hardware and current processes running on your system. Files that have write permission can be modified to change the configuration of the kernel.

Which filesystem is the proc directory mounted to?

The proc filesystem is a pseudo-filesystem which provides an interface to kernel data structures. It is commonly mounted at /proc.


1 Answers

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.

like image 52
Jerub Avatar answered Oct 08 '22 22:10

Jerub