Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Running process as another user

by morgon (Priest)
on May 14, 2014 at 09:35 UTC ( [id://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!

Replies are listed 'Best First'.
Re: Running process as another user
by Corion (Patriarch) 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 mhearse (Chaplain) 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
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found