-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256


Hello all.

AFAIK, you can't do that. When you fork everything is copied, including
the UID.

To achieve something similar, you need to launch another process (but
you'll need to be root to launch it as another user)

Regards,
David Santiago


On Fri, 11 Mar 2016 20:22:34 +0200
Lars Noodén <lars.noo...@gmail.com> wrote:

> If I have the code below to fork a child process, how would the right
> way be to fork as a different user?  I gather that fork() itself does
> not support that, so some other method must be used.
> 
> Regards,
> Lars
> 
> -----
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> use English;    # for $UID and such
> 
> my $old_uid = $UID;
> 
> $UID = 1000;
> 
> my $pid = fork();
> 
> unless( $pid ){
>     print qq(\tThis is a child process, PID $$\n);
>     print qq(\tThe child UID is $UID \n);
> 
>     sleep 5;
> 
>     exit 0;
> }
> 
> $UID = $old_uid;
> 
> print qq(This is the parent process PID $PID and a child PID is
> $pid\n); print qq(The parent is running as UID $UID \n);
> 
> sleep 5;
> 
> exit 0;
> 

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBCAAGBQJW41ZSAAoJEJ/OLjwuDYzKj+sH/i2IT675x1DJeEDUlkPPchtA
p9GXwHPm0YTp/DPuyAlsS0UVe4jGB4dmhOk+F+3xaE9y4muv/s+F0HxQtWiGZLs9
DbhBoIjWcsgcC2z4ithRtb4gCHNHmr5Wbf+J7dZWIM/ajVhB7ZgGhaHvFWIJFfkg
xYB8jr93IC26XlxNPAtOmRB9jOIrUWnz5+Ut/eBlTdaKc8JW4VZ7NNiaHPx2LyVl
dR2R6YH5s3o6yNoyMO0RnBcRcqXL/3e+s37ufmRslPKjSnoz+dg8V+WeV8PT2AQv
FNQjV8fZMxY+gKDAKAjyZCDUl5vsZOsnc94toO3L2WyHl55e13+cZKcB//PljXU=
=eyGf
-----END PGP SIGNATURE-----

Reply via email to