views:

1246

answers:

7

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.

+3  A: 

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

Adam Pierce
+4  A: 

Why not just use "id -u"?

dreamlax
+1  A: 

Most likley, 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.

William Keller
+3  A: 

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

jfs
+1  A: 

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!

Thomas
+7  A: 

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.

Jerub
You should never parse /etc/passwd yourself, because most modern systems use PAM, and in that case the user database might not be /etc/passwd at all, but might instead be on NIS, an LDAP server or something entirely different.
jfs
A: 

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.