kabeldag has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use POSIX qw(setgid setuid); use IPC::Open2; my $got_sig=0; $SIG{'TERM'}=\&catch_sig; $SIG{'INT'}=\&catch_sig; $SIG{'HUP'}=\&catch_sig; $SIG{'QUIT'}=\&catch_sig; $SIG{'ABRT'}=\&catch_sig; sub catch_sig { $got_sig=1; } # Set UID & GID my $uid = xxx; my $gid = xxx; # Set GID before UID so that we can still call setuid(). setgid $uid; setuid $gid; my $program = "/path/to/some/binary"; my $actual_prog = "file_name_of_binary"; my @prog_opts = "some_sort_of_arguments"; # GMT offset needs to be changed accordingly in Daylight # Savings periods : Cause I am lazy ... my $gmt_offset = 8; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(time); my $zone_standard_hour = ($hour + $gmt_offset); my $actual_year = ($year + 1900); my $actual_mon = ($mon+1); my $actual_standard_time = "$zone_standard_hour$min$sec-$mday-$actual_ +mon-$actual_year"; my $logfile = "/absolute/path/of/logfile/logs/$actual_standard_time-se +rver.log"; ################################################### # FORGET ABOUT ANYTHING ABOVE HERE. READ BELOW !!!# ################################################### my $spid; print "Launching \"$actual_prog\" as UID($uid)/GID($gid)\n"; unless(my $ppid=fork()) { if (!(my $pid=fork())) { # Child while(1) { open(STDERR,">> $logfile"); $spid = open2('SREAD', 'SWRITE', "$program @prog_opts"); waitpid($spid,'WNOHANG'); print STDOUT "Server \"$actual_prog\" exited. Re-load!\n"; } }else{ # Parent while(1) { if($got_sig==1) { print STDOUT "Closing up shop ...\n"; kill('KILL',$spid); exit(0); }else{ sleep 5; print SWRITE "Blah blah blah\n" or print STDOUT "No good, sunny boy !\n"; } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Shared file-handles & fork fun
by shmem (Chancellor) on Nov 22, 2006 at 14:41 UTC | |
by kabeldag (Hermit) on Nov 22, 2006 at 21:16 UTC | |
|
Re: Shared file-handles & fork fun
by zentara (Cardinal) on Nov 22, 2006 at 14:27 UTC |