Hi folks,
How would I be able to write to the opened binary process in the child process from the parent
process in the
else block.
Should I share/reference the SREAD and SWRITE handles between both
the child and parent ? And how would I do that (without opening the handles globally) !?
I also need to be able to monitor the state of the binary process that I have spawned, that's why I have used
open2(),
because it returns a
PID that I can use
waitpid() on and re-load the binary when it has terminated.
But at the same time, I also need to be able to write to the binary process. That's why I have separated both tasks with
fork().
Maybe you think that what I am doing should/could be done differently ? Got any ideas ?
It's too damned hot. I need a cold glass of water ...
Update: PS --- All processes MUST be daemons/run in the background
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";
}
}
}
}