File Coverage

lib/IOMux/Pipe/Read.pm
Criterion Covered Total %
statement 59 63 93.6
branch 10 20 50.0
condition 2 4 50.0
subroutine 11 14 78.5
pod 5 6 83.3
total 87 107 81.3


line stmt bran cond sub pod time code
1             # Copyrights 2011-2020 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 2.02.
5             # This code is part of distribution IOMux. Meta-POD processed with OODoc
6             # into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package IOMux::Pipe::Read;
10 5     5   2003748 use vars '$VERSION';
  5         10  
  5         308  
11             $VERSION = '1.01';
12              
13 5     5   24 use base 'IOMux::Handler::Read';
  5         8  
  5         1053  
14              
15 5     5   27 use warnings;
  5         5  
  5         91  
16 5     5   19 use strict;
  5         7  
  5         101  
17              
18 5     5   19 use Log::Report 'iomux';
  5         5  
  5         22  
19 5     5   979 use Fcntl;
  5         9  
  5         930  
20 5     5   29 use POSIX qw/:errno_h :sys_wait_h/;
  5         5  
  5         21  
21 5     5   1572 use File::Basename 'basename';
  5         21  
  5         2754  
22              
23              
24             sub init($)
25 2     2 0 6 { my ($self, $args) = @_;
26             my $command = $args->{command}
27 2 50       8 or error __x"no command to run specified in {pkg}", pkg => __PACKAGE__;
28              
29 2 50       10 my ($cmd, @cmdopts) = ref $command eq 'ARRAY' ? @$command : $command;
30 2         166 my $name = $args->{name} = (basename $cmd)."|";
31              
32 2         4 my ($rh, $wh);
33 2 50       66 pipe $rh, $wh
34             or fault __x"cannot create pipe for {cmd}", cmd => $name;
35              
36 2         1956 my $pid = fork;
37 2 50       134 defined $pid
38             or fault __x"failed to fork for pipe {cmd}", cmd => $name;
39              
40 2 100       82 if($pid==0)
41             { # client
42 1         31 close $rh;
43 1         220 open STDIN, '<', File::Spec->devnull;
44 1 50       68 open STDOUT, '>&', $wh
45             or fault __x"failed to redirect STDOUT for pipe {cmd}", cmd=>$name;
46 1         38 open STDERR, '>', File::Spec->devnull;
47              
48 1 0       0 exec $cmd, @cmdopts
49             or fault __x"failed to exec for pipe {cmd}", cmd => $name;
50             }
51              
52             # parent
53              
54 1         68 $self->{IMPR_pid} = $pid;
55 1   50     104 $args->{read_size} ||= 4096; # Unix typical BUFSIZ
56              
57 1         17 close $wh;
58 1         19 fcntl $rh, F_SETFL, O_NONBLOCK;
59 1         13 $args->{fh} = $rh;
60              
61 1         62 $self->SUPER::init($args);
62 1         54 $self;
63             }
64              
65              
66             sub bare($%)
67 2     2 1 6 { my ($class, %args) = @_;
68 2         4 my $self = bless {}, $class;
69              
70 2         2 my ($rh, $wh);
71 2 50       48 pipe $rh, $wh
72             or fault __x"cannot create bare pipe reader";
73              
74 2   50     12 $args{read_size} ||= 4096; # Unix typical BUFSIZ
75              
76 2         8 fcntl $rh, F_SETFL, O_NONBLOCK;
77 2         4 $args{fh} = $rh;
78              
79 2         14 $self->SUPER::init(\%args);
80 2         6 ($self, $wh);
81             }
82              
83              
84             sub open($$@)
85 0     0 1 0 { my ($class, $mode, $cmd) = (shift, shift, shift);
86 0 0       0 ref $cmd eq 'ARRAY'
87             ? $class->new(command => $cmd, mode => $mode, @_)
88             : $class->new(command => [$cmd, @_] , mode => $mode);
89             }
90              
91             #-------------------
92              
93 0     0 1 0 sub mode() {shift->{IMPR_mode}}
94 0     0 1 0 sub childPid() {shift->{IMPR_pid}}
95              
96             #-------------------
97              
98             sub close($)
99 2     2 1 1265 { my ($self, $cb) = @_;
100             my $pid = $self->{IMPR_pid}
101 2 100       16 or return $self->SUPER::close($cb);
102              
103 1         15 waitpid $pid, WNOHANG;
104 1         4 local $?;
105 1         18 $self->SUPER::close($cb);
106             }
107              
108             1;