| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # Copyrights 2011 by Mark Overmeer. | 
| 2 |  |  |  |  |  |  | #  For other contributors see ChangeLog. | 
| 3 |  |  |  |  |  |  | # See the manual pages for details on the licensing terms. | 
| 4 |  |  |  |  |  |  | # Pod stripped from pm file by OODoc 1.07. | 
| 5 | 5 |  |  | 5 |  | 4631 | use warnings; | 
|  | 5 |  |  |  |  | 11 |  | 
|  | 5 |  |  |  |  | 259 |  | 
| 6 | 5 |  |  | 5 |  | 94 | use strict; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 308 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | package IOMux::Pipe::Read; | 
| 9 | 5 |  |  | 5 |  | 25 | use vars '$VERSION'; | 
|  | 5 |  |  |  |  | 8 |  | 
|  | 5 |  |  |  |  | 667 |  | 
| 10 |  |  |  |  |  |  | $VERSION = '0.12'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 5 |  |  | 5 |  | 28 | use base 'IOMux::Handler::Read'; | 
|  | 5 |  |  |  |  | 13 |  | 
|  | 5 |  |  |  |  | 1461 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 5 |  |  | 5 |  | 27 | use Log::Report    'iomux'; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 42 |  | 
| 15 | 5 |  |  | 5 |  | 1259 | use Fcntl; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 2041 |  | 
| 16 | 5 |  |  | 5 |  | 75 | use POSIX          qw/:errno_h :sys_wait_h/; | 
|  | 5 |  |  |  |  | 11 |  | 
|  | 5 |  |  |  |  | 32 |  | 
| 17 | 5 |  |  | 5 |  | 2738 | use File::Basename 'basename'; | 
|  | 5 |  |  |  |  | 11 |  | 
|  | 5 |  |  |  |  | 3633 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub init($) | 
| 21 | 2 |  |  | 2 | 0 | 6 | {   my ($self, $args) = @_; | 
| 22 | 2 | 50 |  |  |  | 12 | my $command = $args->{command} | 
| 23 |  |  |  |  |  |  | or error __x"no command to run specified in {pkg}", pkg => __PACKAGE__; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 2 | 50 |  |  |  | 16 | my ($cmd, @cmdopts) = ref $command eq 'ARRAY' ? @$command : $command; | 
| 26 | 2 |  |  |  |  | 286 | my $name = $args->{name} = (basename $cmd)."|"; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 2 |  |  |  |  | 4 | my ($rh, $wh); | 
| 29 | 2 | 50 |  |  |  | 74 | pipe $rh, $wh | 
| 30 |  |  |  |  |  |  | or fault __x"cannot create pipe for {cmd}", cmd => $name; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 2 |  |  |  |  | 2582 | my $pid = fork; | 
| 33 | 2 | 50 |  |  |  | 160 | defined $pid | 
| 34 |  |  |  |  |  |  | or fault __x"failed to fork for pipe {cmd}", cmd => $name; | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 2 | 100 |  |  |  | 81 | if($pid==0) | 
| 37 |  |  |  |  |  |  | {   # client | 
| 38 | 1 |  |  |  |  | 48 | close $rh; | 
| 39 | 1 |  |  |  |  | 331 | open STDIN,  '<', File::Spec->devnull; | 
| 40 | 1 | 50 |  |  |  | 58 | open STDOUT, '>&', $wh | 
| 41 |  |  |  |  |  |  | or fault __x"failed to redirect STDOUT for pipe {cmd}", cmd=>$name; | 
| 42 | 1 |  |  |  |  | 52 | open STDERR, '>', File::Spec->devnull; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 1 | 0 |  |  |  | 0 | exec $cmd, @cmdopts | 
| 45 |  |  |  |  |  |  | or fault __x"failed to exec for pipe {cmd}", cmd => $name; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | # parent | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 1 |  |  |  |  | 64 | $self->{IMPR_pid}    = $pid; | 
| 51 | 1 |  | 50 |  |  | 57 | $args->{read_size} ||= 4096;  # Unix typical BUFSIZ | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 1 |  |  |  |  | 33 | close $wh; | 
| 54 | 1 |  |  |  |  | 23 | fcntl $rh, F_SETFL, O_NONBLOCK; | 
| 55 | 1 |  |  |  |  | 20 | $args->{fh} = $rh; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 1 |  |  |  |  | 98 | $self->SUPER::init($args); | 
| 58 | 1 |  |  |  |  | 61 | $self; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub bare($%) | 
| 63 | 2 |  |  | 2 | 1 | 6 | {   my ($class, %args) = @_; | 
| 64 | 2 |  |  |  |  | 6 | my $self = bless {}, $class; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 2 |  |  |  |  | 4 | my ($rh, $wh); | 
| 67 | 2 | 50 |  |  |  | 50 | pipe $rh, $wh | 
| 68 |  |  |  |  |  |  | or fault __x"cannot create bare pipe reader"; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 2 |  | 50 |  |  | 10 | $args{read_size} ||= 4096;  # Unix typical BUFSIZ | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 2 |  |  |  |  | 8 | fcntl $rh, F_SETFL, O_NONBLOCK; | 
| 73 | 2 |  |  |  |  | 6 | $args{fh} = $rh; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 2 |  |  |  |  | 18 | $self->SUPER::init(\%args); | 
| 76 | 2 |  |  |  |  | 8 | ($self, $wh); | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | sub open($$@) | 
| 81 | 0 |  |  | 0 | 1 | 0 | {   my ($class, $mode, $cmd) = (shift, shift, shift); | 
| 82 | 0 | 0 |  |  |  | 0 | ref $cmd eq 'ARRAY' | 
| 83 |  |  |  |  |  |  | ? $class->new(command => $cmd, mode => $mode, @_) | 
| 84 |  |  |  |  |  |  | : $class->new(command => [$cmd, @_] , mode => $mode); | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | #------------------- | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 0 |  |  | 0 | 1 | 0 | sub mode()     {shift->{IMPR_mode}} | 
| 90 | 0 |  |  | 0 | 1 | 0 | sub childPid() {shift->{IMPR_pid}} | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | #------------------- | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub close($) | 
| 95 | 2 |  |  | 2 | 1 | 1078 | {   my ($self, $cb) = @_; | 
| 96 | 2 | 100 |  |  |  | 26 | my $pid = $self->{IMPR_pid} | 
| 97 |  |  |  |  |  |  | or return $self->SUPER::close($cb); | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 1 |  |  |  |  | 34 | waitpid $pid, WNOHANG; | 
| 100 | 1 |  |  |  |  | 5 | local $?; | 
| 101 | 1 |  |  |  |  | 16 | $self->SUPER::close($cb); | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | 1; |