Interact/Transport/Role/ConnectCore.pm

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