Hello.

I have a script for downloading bunch of data from some servers.

For each server I start ssh via IPC::Open2 like this:

my $pid = open2($rfh, $wfh, "ssh $opts $hostspec $cmd");

Then I download all data in loop via select() (4 arg version). When some ssh dies, I need to modify some program structures (same applies for INT/TERM signal). So I decided I will do it right after select() via self-pipe trick (described in NOTES in linux select(2) manpage).

### Self-pipe init. pipe(SIGRD, SIGWR); # Make SIGRD non-blocking. vec($rin, fileno SIGRD, 1) = 1;

CHLD handler does just this (INT/TERM handlers sets $term variable in addition):

my $byte = 1; syswrite(SIGWR, $byte, 1);

This is top of the main while loop.

my $nfound = select($rout = $rin, $wout = $win, undef, undef); # Check $nfound for errors. if(vec($rout, fileno SIGRD, 1)) { my $byte; while(sysread(SIGRD, $byte, 1)) { if($term) { # INT/TERM arrived, kill sshs, modify some data, ... $term = 0; } else { # CHLD arrived, do waitpid(), modify some data, ... } }

After I do waitpid() I need to check $? if ssh exited with error. However in description of $? in perlvar is stated: If you have installed a signal handler for "SIGCHLD", the value of $? will usually be wrong outside that handler. My question is why it will be wrong when I will use it just after waitpid()? Thank you.


In reply to $? outside SIGCHLD handler by kenshin

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.