| 1 | # | ||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Rex::Interface::Exec::Local; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 60 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 60 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 60 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 60 | ▶ | use Rex::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 60 | ▶ | use Rex::Commands; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 60 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 60 | ▶ | use Symbol 'gensym'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 60 | ▶ | use IPC::Open3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 60 | ▶ | use IO::Select; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 60 | ▶ | use Rex::Interface::Exec::IOReader; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Use 'parent' is recommended, but from Perl 5.10.1 its in core | ||||||||||||||||||||||||||||
| 22 | 60 | ▶ | use base qw(Rex::Interface::Exec::Base Rex::Interface::Exec::IOReader); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2250 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 2250 | my $that = shift; | |||||||||||||||||||||||||||
| 26 | 2250 | ▶ | my $proto = ref($that) || $that; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table33%1/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||||||||||
| 27 | 2250 | my $self = {@_}; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2250 | bless( $self, $proto ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2250 | return $self; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 49 | ▶ | sub set_env { | ||||||||||||||||||||||||||
|
Subroutine
set_env: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | 49 | my ( $self, $env ) = @_; | |||||||||||||||||||||||||||
| 36 | 49 | my $cmd = undef; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 49 | ▶ | die("Error: env must be a hash") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | if ( ref $env ne "HASH" ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 49 | ▶ | while ( my ( $k, $v ) = each(%$env) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 95 | ▶ | $cmd .= $OSNAME eq 'MSWin32' ? "set $k=$v && " : "export $k='$v'; "; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 49 | $self->{env} = $cmd; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1221 | ▶ | sub exec { | ||||||||||||||||||||||||||
|
Subroutine
exec: called CC=16 SCAR=28.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 1221 | my ( $self, $cmd, $path, $option ) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1221 | my ( $out, $err, $pid ); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1221 | ▶ | if ( exists $option->{cwd} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 71 | ▶ | my $cd_cmd = $OSNAME eq 'MSWin32' ? 'cd /d' : 'cd'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 71 | $cmd = "$cd_cmd " . $option->{cwd} . " && $cmd"; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1221 | ▶ | if ( exists $option->{path} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | $path = $option->{path}; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1221 | ▶ | if ( exists $option->{env} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 49 | $self->set_env( $option->{env} ); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1221 | ▶ | if ( exists $option->{format_cmd} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $option->{format_cmd} =~ s/\{\{CMD\}\}/$cmd/; | |||||||||||||||||||||||||||
| 67 | 0 | $cmd = $option->{format_cmd}; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1221 | Rex::Commands::profiler()->start("exec: $cmd"); | |||||||||||||||||||||||||||
| 71 | 1221 | ▶ | if ( $^O !~ m/^MSWin/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 1221 | ▶ | if ($path) { $path = "PATH=$path" } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 1221 | ▶ | $path ||= ""; | ||||||||||||||||||||||||||
|
Condition100%2/2
$path ||= ""
MC/DC100%1/1
$path ||= ""
$path
Truth table100%2/2
A
A= $path
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1221 | my $new_cmd = "LC_ALL=C $cmd"; | |||||||||||||||||||||||||||
| 76 | 1221 | ▶ | if ($path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 984 | $new_cmd = "export $path ; $new_cmd"; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1221 | ▶ | if ( $self->{env} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 48 | $new_cmd = $self->{env} . " $new_cmd"; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1221 | ▶ | if ( Rex::Config->get_source_global_profile ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | $new_cmd = ". /etc/profile >/dev/null 2>&1; $new_cmd"; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1221 | $cmd = $new_cmd; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ( $self->{env} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | $cmd = $self->{env} . " $cmd"; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1221 | Rex::Logger::debug("Executing: $cmd"); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1221 | ( $out, $err ) = $self->_exec( $cmd, $option ); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 1221 | ▶ | Rex::Logger::debug($out) if ($out); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 1221 | ▶ | if ($err) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 2 | Rex::Logger::debug("========= ERR ============"); | |||||||||||||||||||||||||||
| 103 | 2 | Rex::Logger::debug($err); | |||||||||||||||||||||||||||
| 104 | 2 | Rex::Logger::debug("========= ERR ============"); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1221 | Rex::Commands::profiler()->end("exec: $cmd"); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1221 | ▶ | if (wantarray) { return ( $out, $err ); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 236 | return $out; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 556 | ▶ | sub can_run { | ||||||||||||||||||||||||||
|
Subroutine
can_run: called CC=3 SCAR=12.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | 556 | my ( $self, $commands_to_check, $check_with_command ) = @_; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 556 | ▶ | $check_with_command ||= $^O =~ /^MSWin/i ? 'where' : 'command -v'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$check_with_command ||= $^O =~ /^MSWin/iu ? "where" : "command -v"
MC/DC0%0/2
$check_with_command ||= $^O =~ /^MSWin/iu ? "where" : "command -v"
$check_with_command
$^O =~ /^MSWin/iu ? "where" : "command -v"
Truth table33%1/3
A ||= B
A= $check_with_commandB= $^O =~ /^MSWin/iu ? "where" : "command -v"
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 556 | return $self->SUPER::can_run( $commands_to_check, $check_with_command ); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1237 | ▶ | sub _exec { | ||||||||||||||||||||||||||
|
Subroutine
_exec: called CC=8 SCAR=21.3
| |||||||||||||||||||||||||||||
| 123 | 1237 | my ( $self, $cmd, $option ) = @_; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1237 | my ( $pid, $writer, $reader, $error, $out, $err ); | |||||||||||||||||||||||||||
| 126 | 1237 | $error = gensym; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 1237 | ▶ | if ( $^O !~ m/^MSWin/ && Rex::Config->get_no_tty ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $^O =~ /^MSWin/u and "Rex::Config"->get_no_tty
MC/DC50%1/2
not $^O =~ /^MSWin/u and "Rex::Config"->get_no_tty
not $^O =~ /^MSWin/u
"Rex::Config"->get_no_tty
Truth table66%2/3
not(A) and B
A= not $^O =~ /^MSWin/uB= "Rex::Config"->get_no_tty
| |||||||||||||||||||||||||||||
| 129 | 2 | $pid = open3( $writer, $reader, $error, $cmd ); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 2 | ( $out, $err ) = $self->io_read( $reader, $error, $pid, $option ); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 2 | ▶ | waitpid( $pid, 0 ) or die($!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | else { | ||||||||||||||||||||||||||||
| 136 | 1235 | ▶ | $pid = open( my $fh, "-|", "$cmd 2>&1" ) or die($!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 1233 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 5229 | $out .= $_; | |||||||||||||||||||||||||||
| 139 | 5229 | chomp; | |||||||||||||||||||||||||||
| 140 | $self->execute_line_based_operation( $_, $option ) | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | && do { kill( 'KILL', $pid ); last }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 1233 | ▶ | waitpid( $pid, 0 ) or die($!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # we need to bitshift $? so that $? contains the right (and for all | ||||||||||||||||||||||||||||
| 148 | # connection methods the same) exit code after a run()/i_run() call. | ||||||||||||||||||||||||||||
| 149 | # this is for the user, so that he can query $? in his task. | ||||||||||||||||||||||||||||
| 150 | 1235 | $? >>= 8; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 1235 | return ( $out, $err ); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 60 | 1; | |||||||||||||||||||||||||||