Reputation: 1116
Let's say I want to spawn a process from a daemon (running as root) using exec() and fork(), and that I also want to impersonate a different user before spawning the process using seteuid() and setegid(). If I also want to inherit the environmental variables set for that particular user, what's the best way you can suggest to do so ? Is there also another way without invoking a /sbin/sh and/or sudo ?
Would like to do that either on Linux and on Mac OS X !
Thanks !
Upvotes: 0
Views: 104
Reputation: 44181
There is no such thing as "environmental variables set for that particular user". The variables are not stored in a list somewhere, they are set by programs run upon login. You will have to run the same scripts. So just exec
a shell with -l
(login shell) and have it run (with -c
) the desired command you would have passed to exec
before.
Note that this might still not replicate the entire environment. Some variables (DISPLAY
, for example) are often set by programs which are run in another way.
To obtain the PID of the final child, make sure to use exec
in the command passed with -c
:
# sh -l -c "exec sleep 50" &
[1] 30331
# ps -a
PID TTY TIME CMD
30331 pts/1 00:00:00 sleep
Upvotes: 2