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"; } } } }

In reply to Shared file-handles & fork fun by kabeldag

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.