brp4h has asked for the wisdom of the Perl Monks concerning the following question:

I'm having trouble using the system call of the Net::OpenSSH module from a child process. After I fork, the child successfully creates a new SSH object, but then when I try this code the program dies:
$ssh->system({stdin_data => $sas_string}, "cat >$rmt_dir/$rmt_file") or die "unable to write file: " . $ssh->error;
and the error is unable to write file: child process 14947 does not exists: No child processes Is there something I should know that could be causing this? It works fine without the fork.

Replies are listed 'Best First'.
Re: Net::OpenSSH with fork
by ikegami (Patriarch) on Jan 26, 2010 at 18:36 UTC
    Does that child have a $SIG{CHLD} handler? Just guessing here, but it could interfere.
      Yep, that was it. Thank you!
Re: Net::OpenSSH with fork
by salva (Canon) on Jan 26, 2010 at 23:01 UTC
    I (the module author) am considering solving that inside the module:

    Other system implementations (i.e. NetBSD) call sigprocmask(2) to disable delivering of CHLD signals inside the function, but I am not sure how portable that approach is. Does anybody have any experience using POSIX::sigprocmask() across different Unix/Linux OSs?

    BTW, Perl builtins (system and qx()) do nothing to handle that case and also fail when $SIG{CHLD} is set to 'IGNORE' or to some funny handler reaping children.

    update: at least on Linux, blocking SIGCHLD with sigprocmask(2) is not enough when the SA_NOCLDWAIT flag (aka $SIG{CHLD}='IGNORE') is set. The OS continues to reap dead children and waitpid fails.