The program I am running needs root privledges and therefore is run with sudo
, but it also needs to know what user is running it. getuid
and
You have two good choices...
Update:
The setuid bit is an access right flag in the file mode that causes a program to run with the capabilities of the executable file's owner. This is how sudo(1) is able to run things as root ... the sudo program itself has this mode.
$ ls -l /usr/bin/sudo
-r-s--x--x 1 root wheel 272384 Jun 22 2009 /usr/bin/sudo*
To make a program setuid root one might:
$ chown root a.out
$ chmod +s a.out
Needless to say, setuid root programs should be written carefully. You can setuid to a less privileged user if all you need is access to a protected directory or file.