I can't seem to get an exec call to print to a redefined STDOUT that is actually a pipe. Here's the code where I create the pipe and the child process
# dup stdout so we can restore it later
if( !open( $old_stdout, ">&STDOUT" ) ) {
return common::results->new( FAILURE, "Unable to dup STDOUT: $!" );
}
close( STDOUT );
# create the piped stdout
pipe( $smash_stdout, STDOUT );
# set non blocking and unbuffered
my $flags = fcntl( STDOUT, F_GETFL, 0 );
fcntl( STDOUT, F_SETFL, $flags | O_NONBLOCK );
select((select(STDOUT), $|=1)[0]);
if( ($pid = fork()) ) {
# parent process
# reset out STDOUT
if( !open( STDOUT, ">&", $old_stdout ) ) {
return common::results->new( FAILURE, "Unable to dup STDOUT: $!" );
}
close( $old_stdout );
$target->{stdout} = $smash_stdout;
$target->{buffer} = "";
} else {
exec ($cmd);
exit( -1 );
}
And then later on in the parent thread, I run the following
foreach $target (@{$self->{TARGETS}}) {
if( defined( $target->{stdout} ) ) {
my $smash_fh = $target->{stdout};
$target->{buffer} .= <$smash_fh>;
}
}
If I print something to STDOUT before I exec, I read that through the pipe but the exec routine doesn't seem to send it through the pipe. I've deleted the code to redo stdout and the exec will print to the screen so the exec'ed program is printing, just not to my new STDOUT. Is there something different that I need to do?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|