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