| 1 | # Copyrights 2011-2020 by [Mark Overmeer <markov@cpan.org>]. | ||||||||||||
| 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::Write; | ||||||||||||
| 10 | 6 | ▶ | use vars '$VERSION'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 6 | $VERSION = '1.01'; | |||||||||||
| 12 | |||||||||||||
| 13 | 6 | ▶ | use base 'IOMux::Handler::Write'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 6 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 6 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 6 | ▶ | use Log::Report 'iomux'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 6 | ▶ | use Fcntl; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 6 | ▶ | use POSIX qw/:errno_h :sys_wait_h/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 6 | ▶ | use File::Spec (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 6 | ▶ | use File::Basename 'basename'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | |||||||||||||
| 24 | 6 | ▶ | use constant PIPE_BUF_SIZE => 4096; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 25 | |||||||||||||
| 26 | |||||||||||||
| 27 | sub init($) | ||||||||||||
| 28 | 3 | ▶ | { my ($self, $args) = @_; | ||||||||||
|
Subroutine
init: called CC=8 SCAR=21.5
Pod
undocumented
| |||||||||||||
| 29 | |||||||||||||
| 30 | my $command = $args->{command} | ||||||||||||
| 31 | 3 | ▶ | or error __x"no command to run specified in {pkg}", pkg => __PACKAGE__; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | |||||||||||||
| 33 | 3 | ▶ | my ($cmd, @cmdopts) = ref $command eq 'ARRAY' ? @$command : $command; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | 3 | my $name = $args->{name} = '|'.(basename $cmd); | |||||||||||
| 35 | |||||||||||||
| 36 | 3 | my ($rh, $wh); | |||||||||||
| 37 | 3 | ▶ | pipe $rh, $wh | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | or fault __x"cannot create pipe for {cmd}", cmd => $name; | ||||||||||||
| 39 | |||||||||||||
| 40 | 3 | my $pid = fork; | |||||||||||
| 41 | 3 | ▶ | defined $pid | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | or fault __x"failed to fork for pipe {cmd}", cmd => $name; | ||||||||||||
| 43 | |||||||||||||
| 44 | 3 | ▶ | if($pid==0) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | { # client | ||||||||||||
| 46 | 1 | close $wh; | |||||||||||
| 47 | 1 | ▶ | open STDIN, '<&', $rh | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | or fault __x"failed to redirect STDIN for pipe {cmd}", cmd => $name; | ||||||||||||
| 49 | 1 | open STDOUT, '>', File::Spec->devnull; | |||||||||||
| 50 | 1 | open STDERR, '>', File::Spec->devnull; | |||||||||||
| 51 | |||||||||||||
| 52 | 1 | ▶ | exec $cmd, @cmdopts | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | or fault __x"failed to exec for pipe {cmd}", cmd => $name; | ||||||||||||
| 54 | } | ||||||||||||
| 55 | 2 | $self->{IMPW_pid} = $pid; | |||||||||||
| 56 | |||||||||||||
| 57 | # parent | ||||||||||||
| 58 | |||||||||||||
| 59 | 2 | close $rh; | |||||||||||
| 60 | 2 | fcntl $wh, F_SETFL, O_NONBLOCK; | |||||||||||
| 61 | 2 | $args->{fh} = $wh; | |||||||||||
| 62 | |||||||||||||
| 63 | 2 | $self->SUPER::init($args); | |||||||||||
| 64 | 2 | $self; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | |||||||||||||
| 68 | sub bare($%) | ||||||||||||
| 69 | 2 | ▶ | { my ($class, %args) = @_; | ||||||||||
|
Subroutine
bare: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||
| 70 | 2 | my $self = bless {}, $class; | |||||||||||
| 71 | |||||||||||||
| 72 | 2 | my ($rh, $wh); | |||||||||||
| 73 | 2 | ▶ | pipe $rh, $wh | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | or fault __x"cannot create bare pipe writer"; | ||||||||||||
| 75 | |||||||||||||
| 76 | 2 | ▶ | $args{read_size} ||= 4096; | ||||||||||
|
Condition50%1/2
$args{"read_size"} ||= 4096
MC/DC0%0/1
$args{"read_size"} ||= 4096
$args{"read_size"}
Truth table50%1/2
A
A= $args{"read_size"}
| |||||||||||||
| 77 | |||||||||||||
| 78 | 2 | fcntl $wh, F_SETFL, O_NONBLOCK; | |||||||||||
| 79 | 2 | $args{fh} = $wh; | |||||||||||
| 80 | |||||||||||||
| 81 | 2 | $self->SUPER::init(\%args); | |||||||||||
| 82 | 2 | ($self, $rh); | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | |||||||||||||
| 86 | sub open($$@) | ||||||||||||
| 87 | 0 | ▶ | { my ($class, $mode, $cmd) = (shift, shift, shift); | ||||||||||
|
Subroutine
open: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 88 | 0 | ▶ | ref $cmd eq 'ARRAY' | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | ? $class->new(command => $cmd, mode => $mode, @_) | ||||||||||||
| 90 | : $class->new(command => [$cmd, @_] , mode => $mode); | ||||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | #------------------- | ||||||||||||
| 94 | |||||||||||||
| 95 | 0 | ▶ | sub mode() {shift->{IMPW_mode}} | ||||||||||
|
Subroutine
mode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 96 | 0 | ▶ | sub childPid() {shift->{IMPW_pid}} | ||||||||||
|
Subroutine
childPid: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 97 | |||||||||||||
| 98 | #------------------- | ||||||||||||
| 99 | |||||||||||||
| 100 | sub close($) | ||||||||||||
| 101 | 4 | ▶ | { my ($self, $cb) = @_; | ||||||||||
|
Subroutine
close: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 102 | my $pid = $self->{IMPW_pid} | ||||||||||||
| 103 | 4 | ▶ | or return $self->SUPER::close($cb); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | |||||||||||||
| 105 | 2 | waitpid $pid, WNOHANG; | |||||||||||
| 106 | 2 | local $?; | |||||||||||
| 107 | 2 | $self->SUPER::close($cb); | |||||||||||
| 108 | } | ||||||||||||
| 109 | |||||||||||||
| 110 | |||||||||||||
| 111 | |||||||||||||
| 112 | 6 | 1; | |||||||||||