- my $receive = new IO::File ('|scripts/receive.in') or die "Unable to start receive.in: $!";
-
- print {$receive} create_mime_message($param{headers},
- $param{body}) or die "Unable to to print to receive.in";
- close($receive) or die "Unable to close receive.in";
- $? == 0 or die "receive.in failed";
+ my ($rfd,$wfd);
+ my $output='';
+ my $pipe_handler = $SIG{PIPE};
+ $SIG{PIPE} = 'IGNORE';
+ $SIG{CHLD} = 'DEFAULT';
+ my $pid = open3($wfd,$rfd,$rfd,'scripts/receive')
+ or die "Unable to start receive: $!";
+ print {$wfd} create_mime_message($param{headers},
+ $param{body},
+ $param{attachments}) or
+ die "Unable to to print to receive";
+ close($wfd) or die "Unable to close receive";
+ $SIG{PIPE} = $pipe_handler;
+ my $err = $? >> 8;
+ my $childpid = waitpid($pid,0);
+ if ($childpid != -1) {
+ $err = $? >> 8;
+ print STDERR "receive pid: $pid doesn't match childpid: $childpid\n" if $childpid != $pid;
+ }
+ if ($err != 0 ) {
+ my $rfh = IO::Handle->new_from_fd($rfd,'r') or die "Unable to create filehandle: $!";
+ $rfh->blocking(0);
+ my $rv;
+ while ($rv = $rfh->sysread($output,1000,length($output))) {}
+ if (not defined $rv) {
+ print STDERR "Reading from STDOUT/STDERR would have blocked.";
+ }
+ print STDERR $output,qq(\n);
+ die "receive failed with exit status $err";
+ }