Interact/Transport/Role/ConnectCore.pm

filter: Statementstmt 27.2% 21 / 77 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 54.5% 6 / 11 Podpod 0.0% 0 / 2 totaltotal 22.5% 27 / 120 CC 16 SCAR 45.3
CC
16
Cov
20%
_spawn_command
182.0
connect_core
6.0
_via_spawn
6.0
CRAP 92.7
↑ Summary Interact/Phrasebook.pm »
1 package Net::CLI::Interact::Transport::Role::ConnectCore;
2 1 $Net::CLI::Interact::Transport::Role::ConnectCore::VERSION = '2.400002';
3 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MooX::Types::MooseLike::Base qw(Int);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Net::Telnet ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶sub connect_core {
Subroutine connect_core: not called CC=2 SCAR=17.9
Pod undocumented
9 0 my $self = shift;
10  
11 0 ▶ if ($self->use_net_telnet_connection) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_net_telnet_connection) { } 0 0 0
12 0 shift; # unused
13 0 return $self->_via_native(@_);
14 }
15 else {
16 0 return $self->_via_spawn(@_);
17 }
18 }
19  
20 0 ▶sub _via_native {
Subroutine _via_native: not called CC=1 SCAR=6.9
21 0 my $self = shift;
22 0 my $t = Net::Telnet->new(Cmd_remove_mode => 1, @_);
23 0 return $t;
24 }
25  
26 0 ▶sub _via_spawn {
Subroutine _via_spawn: not called CC=2 SCAR=17.9
27 0 my $self = shift;
28 0 my $t = Net::Telnet->new(
29 Binmode => 1,
30 Cmd_remove_mode => 1,
31 Telnetmode => 0,
32 );
33  
34 0 ▶ $t->fhopen( $self->_spawn_command(@_) )
Branch: 1 branch
BranchTrueFalseTotal
$t->fhopen($self->_spawn_command(@_)) or die "failed to spawn connection to target device." 0 0 0
35 or die "failed to spawn connection to target device.";
36 0 return $t;
37 }
38  
39 # this code is based on that in Expect.pm, and found to be the most reliable.
40 # minor alterations to use CORE::close and die, and to reap child.
41  
42 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use IO::Pty;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use POSIX qw(WNOHANG);
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 1 has 'childpid' => (
47 is => 'rw',
48 isa => Int,
49 );
50  
51 0 ▶sub REAPER {
Subroutine REAPER: not called CC=2 SCAR=17.9
Pod undocumented
52 # http://www.perlmonks.org/?node_id=10516
53 0 ▶ 1 while ((waitpid(-1, &WNOHANG)) > 0);
Branch: 1 branch
BranchTrueFalseTotal
waitpid(-1, &WNOHANG) > 0 and do { '???' } 0 0 0
54 0 $SIG{CHLD} = \&REAPER;
55 }
56  
57 0 ▶sub _spawn_command {
Subroutine _spawn_command: not called CC=13 SCAR=52.0
58 0 my $self = shift;
59 0 my @command = @_;
60 0 my $pty = IO::Pty->new();
61  
62 # try to install handler to reap children
63 $SIG{CHLD} = \&REAPER
64 0 ▶ if !defined $SIG{CHLD};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $SIG{"CHLD"} 0 0 0
65  
66 # set up pipe to detect childs exec error
67 0 my ($stat_rdr, $stat_wtr);
68 0 ▶ pipe($stat_rdr, $stat_wtr) or die "Cannot open pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe $stat_rdr, $stat_wtr or die "Cannot open pipe: $!" 0 0 0
69 0 $stat_wtr->autoflush(1);
70 0 eval {
71 0 fcntl($stat_wtr, F_SETFD, FD_CLOEXEC);
72 };
73  
74 0 my $pid = fork;
75  
76 0 ▶ if (! defined ($pid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 0 0
77 0 ▶ die "Cannot fork: $!" if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
78 0 return;
79 }
80  
81 0 ▶ if($pid) { # parent
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
82 0 my $errno;
83  
84 0 CORE::close $stat_wtr;
85 0 $pty->close_slave();
86 0 $pty->set_raw();
87  
88 # now wait for child exec (eof due to close-on-exit) or exec error
89 0 my $errstatus = sysread($stat_rdr, $errno, 256);
90 0 ▶ die "Cannot sync with child: $!" if not defined $errstatus;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $errstatus 0 0 0
91 0 CORE::close $stat_rdr;
92  
93 0 ▶ if ($errstatus) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errstatus) 0 0 0
94 0 $! = $errno+0;
95 0 ▶ die "Cannot exec(@command): $!\n" if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
96 0 return;
97 }
98  
99 # store pid for killing if we're in cygwin
100 0 $self->childpid( $pid );
101 }
102 else { # child
103 0 CORE::close $stat_rdr;
104  
105 0 $pty->make_slave_controlling_terminal();
106 0 ▶ my $slv = $pty->slave()
Branch: 1 branch
BranchTrueFalseTotal
my $slv = $pty->slave or die "Cannot get slave: $!" 0 0 0
107 or die "Cannot get slave: $!";
108  
109 0 $slv->set_raw();
110  
111 0 CORE::close($pty);
112  
113 0 CORE::close(STDIN);
114 0 ▶ open(STDIN, '<&', $slv->fileno())
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&", $slv->fileno or die "Couldn't reopen STDIN for reading, $!\n" 0 0 0
115 or die "Couldn't reopen STDIN for reading, $!\n";
116  
117 0 CORE::close(STDOUT);
118 0 ▶ open(STDOUT, '>&', $slv->fileno())
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $slv->fileno or die "Couldn't reopen STDOUT for writing, $!\n" 0 0 0
119 or die "Couldn't reopen STDOUT for writing, $!\n";
120  
121 0 CORE::close(STDERR);
122 0 ▶ open(STDERR, '>&', $slv->fileno())
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $slv->fileno or die "Couldn't reopen STDERR for writing, $!\n" 0 0 0
123 or die "Couldn't reopen STDERR for writing, $!\n";
124  
125 0 { exec(@command) };
126 0 print $stat_wtr $!+0;
127 0 die "Cannot exec(@command): $!\n";
128 }
129  
130 0 return $pty;
131 }
132  
133 =pod
134  
135 =for Pod::Coverage REAPER connect_core
136  
137 =cut
138  
139 1 1;
↑ Summary Interact/Phrasebook.pm »