Re: Forking as another user

2016-03-14 Thread Lars Noodén
On 03/13/2016 02:14 PM, Peter Scott wrote:
> On Sat, 12 Mar 2016 18:44:18 +0200, Lars Noodén wrote:
[snip]
>> What modification can prevent zombies yet allow multiple concurrent
>> clients to attach?
> 
> You need the WNOHANG option on your wait() to make it nonblocking.

Thanks.  I've been exploring that and seem to have found out how to make
it work in my case where the program forks children on an ongoing basis
and the parent needs to stay running.

Prior to handling SIGCHLD myself, the $socket->accept() loop was the
outer loop, but returning from the subroutine always caused the loop to
exit.  What causes that and how can it be prevented?  What I use right
now is another loop around the first loop, but that seems awkward:

 sub trap_chld {
 waitpid( -1,WNOHANG );
 return( 1 );
 }

 $SIG{CHLD} = \&trap_chld;

 ...

 while ( 1 ) {
 while ( my $client_socket = $socket->accept() ) {
 ...
 }
 }
 exit( 0 );

Regards,
/Lars

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Forking as another user

2016-03-13 Thread Peter Scott
On Sat, 12 Mar 2016 18:44:18 +0200, Lars Noodén wrote:
> The snippet below works to fork as a specific user, if run as root.
> However, it generates zombies.  The functions wait() or waitpid() seem
> to be available to use to stop that, but if I insert either of them in
> the outer while loop, only one client can connect at a time.
> 
> What modification can prevent zombies yet allow multiple concurrent
> clients to attach?

You need the WNOHANG option on your wait() to make it nonblocking.

-- 
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=9780133036268

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Forking as another user

2016-03-12 Thread Lars Noodén
Thanks, David and Shlomi (and those that read),

I understand fork() and the style of writing better.  Strange that the
defaults for 'use English' are not optimal.

The snippet below works to fork as a specific user, if run as root.
However, it generates zombies.  The functions wait() or waitpid() seem
to be available to use to stop that, but if I insert either of them in
the outer while loop, only one client can connect at a time.

What modification can prevent zombies yet allow multiple concurrent
clients to attach?

Regards,
Lars

-

while ( my $client = $server->accept() ) {

$EUID = $priv_sep_uid;
my $pid = fork();

unless ( $pid ) {
print qq(\tforked as $PROCESS_ID\n);
while ( my $input = <$client> ) {
1;
}
exit( 0 );
}

}

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Forking as another user

2016-03-12 Thread Shlomi Fish
Hi Lars,

some comments on your code:

On Fri, 11 Mar 2016 20:22:34 +0200
Lars Noodén  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
> 

See http://perl-begin.org/tutorials/bad-elements/#non_recommended_regex_vars .

> 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);

Here you output trailing whitespace:

http://perl-begin.org/tutorials/bad-elements/#trailing-whitespace

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Star Trek: “We, the Living Dead” - http://shlom.in/st-wtld

I may be a geek, but I’m a true Klingon geek‐warrior! And a true Klingon geek
warrior ALWAYS bottom‐posts.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Forking as another user

2016-03-11 Thread David Emanuel da Costa Santiago
-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  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-


Forking as another user

2016-03-11 Thread Lars Noodén
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;

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/