Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Running process as another user

by morgon (Deacon)
on May 14, 2014 at 09:35 UTC ( #1085997=perlquestion: print w/ replies, xml ) Need Help??
morgon has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a script that is started by udev and so runs as root. What I want the script to do is to fork off another long-running process that is supposed to run as another user.

Here is what I do at the moment:

if(fork()) { # do some stuff in the parent as root } else { # child - run process as another user exec "/usr/bin/sudo -u some_other_id $command_to_run" }

This works but in the process table I end up with 2 processes: One for the started command running as some_other_id and a root-process for the sudo.

My question is now how to avoid having the sudo-process staying around.

To rephrase: How do you fork off a long-runnning process under an other user-id while avoiding a root-owned sudo-process staying around as well.

Please note that I want the new process to appear in the process-table as beloning to the other user, so setting effective uid does not do the trick as the process would still appear as root in ps.

Many thanks!

Comment on Running process as another user
Download Code
Re: Running process as another user
by Corion (Pope) on May 14, 2014 at 09:44 UTC

    Looking at perlvar for $< and $>, it suggests that you may want to call POSIX::setuid() to set both in one call. Would that work for you? Or would simply setting both, $< and $> work for you, too?

    ($<,$>)= ($run_as, $run_as);
    or
    use POSIX qw(setuid); setuid( $run_as );
      I actually mentioned in my post why this is not what I want.

      The problem with this approach is that the resulting process (while running under the correct effective user-id) still seems to be running as root in ps.

      I would like to have a process running under another effective id and be visible as such in ps.

        Ah - I'm sorry, I thought that one was for setting the real user-id in addition to the effective user-id would have that effect.

Re: Running process as another user
by Anonymous Monk on May 14, 2014 at 11:08 UTC
    Don't know how to do it in Perl but the usual approach seems to be to fork a process that setuid's to another user, then execs the target.
Re: Running process as another user
by mhearse (Hermit) on May 14, 2014 at 11:25 UTC
    Have udev call:
    #!/bin/bash sudo su - some_other_uname # validate with whoami or id if desired # run your command in the background /usr/bin/yourcommand > /dev/null 2>&1 & # script exits... but your command lives on.... running as the desired + user. # forever zorked

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1085997]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2014-09-16 22:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (51 votes), past polls