| 1 | # PiFlash::Command - run commands including fork paramaters and piping input & output | ||||||||||||||||||||||||||||
| 2 | # by Ian Kluft | ||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use v5.14.0; # require 2011 or newer version of Perl | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use PiFlash::State; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | package PiFlash::Command; | ||||||||||||||||||||||||||||
| 12 | 5 | $PiFlash::Command::VERSION = '0.4.3'; | |||||||||||||||||||||||||||
| 13 | 5 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use POSIX; # included with perl | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use IO::Handle; # rpm: "dnf install perl-IO", deb: included with perl | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use IO::Poll qw(POLLIN POLLHUP); # same as IO::Handle | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # ABSTRACT: process/command running utilities for piflash | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # fork wrapper function | ||||||||||||||||||||||||||||
| 24 | # borrowed from Aaron Crane's YAPC::EU 2009 presentation slides online | ||||||||||||||||||||||||||||
| 25 | sub fork_child | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
fork_child: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 0 | my ($child_process_code) = @_; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # fork and catch errors | ||||||||||||||||||||||||||||
| 30 | 0 | my $pid = fork; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | if (!defined $pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | PiFlash::State->error("Failed to fork: $!\n"); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # if in parent process, return child pid | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if ($pid != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | return $pid; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # if in child process, run requested code | ||||||||||||||||||||||||||||
| 41 | 0 | my $result = $child_process_code->(); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # if we got here, child code returned - so exit to end the subprocess | ||||||||||||||||||||||||||||
| 44 | 0 | exit $result; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # command logging function | ||||||||||||||||||||||||||||
| 48 | sub cmd_log | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
cmd_log: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | # record all command return codes, stdout & stderr in a new top-level store in State | ||||||||||||||||||||||||||||
| 51 | # it's overhead but useful for problem-reporting, troubleshooting, debugging and testing | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if (PiFlash::State::verbose() or PiFlash::State::logging()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
PiFlash::State::verbose() or PiFlash::State::logging()
MC/DC0%0/2
PiFlash::State::verbose() or PiFlash::State::logging()
PiFlash::State::verbose()
PiFlash::State::logging()
Truth table0%0/3
A or B
A= PiFlash::State::verbose()B= PiFlash::State::logging()
| |||||||||||||||||||||||||||||
| 53 | 0 | my $log = PiFlash::State::log(); | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | if (!exists $log->{cmd}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | $log->{cmd} = []; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 0 | push @{$log->{cmd}}, { @_ }; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # fork/exec wrapper to run child processes and collect output/error results | ||||||||||||||||||||||||||||
| 62 | # used as lower level call by cmd() and cmd2str() | ||||||||||||||||||||||||||||
| 63 | # adds more capability than qx()/backtick/system - wrapper lets us send input & capture output/error data | ||||||||||||||||||||||||||||
| 64 | ## no critic (RequireArgUnpacking) | ||||||||||||||||||||||||||||
| 65 | sub fork_exec | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
fork_exec: not called CC=28 SCAR=67.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | # input for child process may be provided as reference to array - use it and remove it from parameters | ||||||||||||||||||||||||||||
| 68 | 0 | my @input; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ( ref $_[0] eq "ARRAY" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | my $input_ref = shift; | |||||||||||||||||||||||||||
| 71 | 0 | @input = @$input_ref; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if (PiFlash::State::verbose()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | say STDERR "fork_exec running: ".join(" ", @_); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 0 | my $cmdname = shift; | |||||||||||||||||||||||||||
| 77 | 0 | my @args = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # open pipes for child process stdin, stdout, stderr | ||||||||||||||||||||||||||||
| 80 | 0 | my ($child_in_reader, $child_in_writer, $child_out_reader, $child_out_writer, | |||||||||||||||||||||||||||
| 81 | $child_err_reader, $child_err_writer); | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | pipe $child_in_reader, $child_in_writer | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | or PiFlash::State->error("fork_exec($cmdname): failed to open child process input pipe: $!"); | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | pipe $child_out_reader, $child_out_writer | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | or PiFlash::State->error("fork_exec($cmdname): failed to open child process output pipe: $!"); | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | pipe $child_err_reader, $child_err_writer | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | or PiFlash::State->error("fork_exec($cmdname): failed to open child process error pipe: $!"); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # fork the child process | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | my $pid = fork_child(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 91 | # in child process | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # close our copy of parent's end of pipes to avoid deadlock - it must now be only one with them open | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | close $child_in_writer | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | or croak "fork_exec($cmdname): child failed to close parent process input writer pipe: $!"; | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | close $child_out_reader | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | or croak "fork_exec($cmdname): child failed to close parent process output reader pipe: $!"; | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | close $child_err_reader | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | or croak "fork_exec($cmdname): child failed to close parent process error reader pipe: $!"; | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # dup file descriptors into child's standard in=0/out=1/err=2 positions | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | POSIX::dup2(fileno $child_in_reader, 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | or croak "fork_exec($cmdname): child failed to reopen stdin from pipe: $!\n"; | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | POSIX::dup2(fileno $child_out_writer, 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | or croak "fork_exec($cmdname): child failed to reopen stdout to pipe: $!\n"; | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | POSIX::dup2(fileno $child_err_writer, 2) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | or croak "fork_exec($cmdname): child failed to reopen stderr to pipe: $!\n"; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # close the file descriptors that were just consumed by dup2 | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | close $child_in_reader | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | or croak "fork_exec($cmdname): child failed to close child process input reader pipe: $!"; | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | close $child_out_writer | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | or croak "fork_exec($cmdname): child failed to close child process output writer pipe: $!"; | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | close $child_err_writer | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | or croak "fork_exec($cmdname): child failed to close child process error writer pipe: $!"; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # execute the command | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | exec @args | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | or croak "fork_exec($cmdname): failed to execute command - returned $?"; | ||||||||||||||||||||||||||||
| 120 | 0 | }); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # in parent process | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # close our copy of child's end of pipes to avoid deadlock - it must now be only one with them open | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | close $child_in_reader | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | or PiFlash::State->error("fork_exec($cmdname): parent failed to close child process input reader pipe: $!"); | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | close $child_out_writer | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | or PiFlash::State->error("fork_exec($cmdname): parent failed to close child process output writer pipe: $!"); | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | close $child_err_writer | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | or PiFlash::State->error("fork_exec($cmdname): parent failed to close child process error writer pipe: $!"); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # write to child's input if any content was provided | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | if (@input) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | # blocks until input is accepted - this interface reqiuires child commands using input take it before output | ||||||||||||||||||||||||||||
| 135 | # because parent process is not multithreaded | ||||||||||||||||||||||||||||
| 136 | 0 | ▶ | if (! print $child_in_writer join("\n", @input)."\n") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | PiFlash::State->error("fork_exec($cmdname): failed to write child process input: $!"); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 0 | close $child_in_writer; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # use IO::Poll to collect child output and error separately | ||||||||||||||||||||||||||||
| 143 | 0 | my @fd = ($child_out_reader, $child_err_reader); # file descriptors for out(0) and err(1) | |||||||||||||||||||||||||||
| 144 | 0 | my @text = (undef, undef); # received text for out(0) and err(1) | |||||||||||||||||||||||||||
| 145 | 0 | my @done = (0, 0); # done flags for out(0) and err(1) | |||||||||||||||||||||||||||
| 146 | 0 | my $poll = IO::Poll->new(); | |||||||||||||||||||||||||||
| 147 | 0 | $poll->mask($fd[0] => POLLIN); | |||||||||||||||||||||||||||
| 148 | 0 | $poll->mask($fd[1] => POLLIN); | |||||||||||||||||||||||||||
| 149 | 0 | ▶ | while (not $done[0] or not $done[1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$done[0] and $done[1]
MC/DC0%0/2
$done[0] and $done[1]
$done[0]
$done[1]
Truth table0%0/3
A and B
A= $done[0]B= $done[1]
| |||||||||||||||||||||||||||||
| 150 | # wait for input | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if ($poll->poll() == -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | PiFlash::State->error("fork_exec($cmdname): poll failed: $!"); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | for (my $i=0; $i<=1; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if (!$done[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | my $events = $poll->events($fd[$i]); | |||||||||||||||||||||||||||
| 157 | 0 | ▶ | if ($events && (POLLIN || POLLHUP)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$events and 1
MC/DC0%0/1
$events and 1
$events
Truth table0%0/2
A
A= $events
| |||||||||||||||||||||||||||||
| 158 | # read all available input for input or hangup events | ||||||||||||||||||||||||||||
| 159 | # we do this for hangup because Linux kernel doesn't report input when a hangup occurs | ||||||||||||||||||||||||||||
| 160 | 0 | my $buffer; | |||||||||||||||||||||||||||
| 161 | 0 | ▶ | while (read($fd[$i], $buffer, 1024) != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | if (!defined $text[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | $text[$i] = ""; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 0 | $text[$i] .= $buffer; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | if ($events && (POLLHUP)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$events and 16
MC/DC0%0/1
$events and 16
$events
Truth table0%0/2
A
A= $events
| |||||||||||||||||||||||||||||
| 168 | # hangup event means this fd (out=0, err=1) was closed by the child | ||||||||||||||||||||||||||||
| 169 | 0 | $done[$i] = 1; | |||||||||||||||||||||||||||
| 170 | 0 | $poll->remove($fd[$i]); | |||||||||||||||||||||||||||
| 171 | 0 | close $fd[$i]; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # reap the child process status | ||||||||||||||||||||||||||||
| 179 | 0 | waitpid( $pid, 0 ); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # record all command return codes, stdout & stderr in a new top-level store in State | ||||||||||||||||||||||||||||
| 182 | # it's overhead but useful for problem-reporting, troubleshooting, debugging and testing | ||||||||||||||||||||||||||||
| 183 | 0 | ▶ | cmd_log ( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 184 | cmdname => $cmdname, | ||||||||||||||||||||||||||||
| 185 | cmdline => [@args], | ||||||||||||||||||||||||||||
| 186 | returncode => $? >> 8, | ||||||||||||||||||||||||||||
| 187 | (($? & 127) ? (signal => sprintf "signal %d%s", ($? & 127), (($? & 128) ? " with coredump" : "")) : ()), | ||||||||||||||||||||||||||||
| 188 | out => $text[0], | ||||||||||||||||||||||||||||
| 189 | err => $text[1] | ||||||||||||||||||||||||||||
| 190 | ); | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | # catch errors | ||||||||||||||||||||||||||||
| 193 | 0 | ▶ | if ($? == -1) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 194 | 0 | PiFlash::State->error("failed to execute $cmdname command: $!"); | |||||||||||||||||||||||||||
| 195 | } elsif ($? & 127) { | ||||||||||||||||||||||||||||
| 196 | 0 | ▶ | PiFlash::State->error(sprintf "%s command died with signal %d, %s coredump", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | $cmdname, ($? & 127), ($? & 128) ? 'with' : 'without'); | ||||||||||||||||||||||||||||
| 198 | } elsif ($? != 0) { | ||||||||||||||||||||||||||||
| 199 | 0 | PiFlash::State->error(sprintf "%s command exited with value %d", $cmdname, $? >> 8); | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | # return output/error | ||||||||||||||||||||||||||||
| 203 | 0 | return @text; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | ## use critic | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # run a command | ||||||||||||||||||||||||||||
| 208 | # usage: cmd( label, command_line) | ||||||||||||||||||||||||||||
| 209 | # label: a descriptive name of the action this is performing | ||||||||||||||||||||||||||||
| 210 | # command_line: shell command line (pipes and other shell metacharacters allowed) | ||||||||||||||||||||||||||||
| 211 | # note: if there are no shell special characters then all command-line parameters need to be passed separately. | ||||||||||||||||||||||||||||
| 212 | # If there are shell special characters then it will be given to the shell for parsing. | ||||||||||||||||||||||||||||
| 213 | ## no critic (RequireArgUnpacking) | ||||||||||||||||||||||||||||
| 214 | sub cmd | ||||||||||||||||||||||||||||
| 215 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
cmd: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 216 | 0 | my $cmdname = shift; | |||||||||||||||||||||||||||
| 217 | 0 | ▶ | if (PiFlash::State::verbose()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 0 | say STDERR "cmd running: ".join(" ", @_); | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 0 | my @args = @_; | |||||||||||||||||||||||||||
| 221 | 0 | system (@args); | |||||||||||||||||||||||||||
| 222 | 0 | ▶ | cmd_log ( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 223 | cmdname => $cmdname, | ||||||||||||||||||||||||||||
| 224 | cmdline => [@args], | ||||||||||||||||||||||||||||
| 225 | returncode => $? >> 8, | ||||||||||||||||||||||||||||
| 226 | (($? & 127) ? (signal => sprintf "signal %d%s", ($? & 127), (($? & 128) ? " with coredump" : "")) : ()), | ||||||||||||||||||||||||||||
| 227 | ); | ||||||||||||||||||||||||||||
| 228 | 0 | ▶ | if ($? == -1) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 229 | 0 | PiFlash::State->error("failed to execute $cmdname command: $!"); | |||||||||||||||||||||||||||
| 230 | } elsif ($? & 127) { | ||||||||||||||||||||||||||||
| 231 | 0 | ▶ | PiFlash::State->error(sprintf "%s command died with signal %d, %s coredump", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | $cmdname, ($? & 127), ($? & 128) ? 'with' : 'without'); | ||||||||||||||||||||||||||||
| 233 | } elsif ($? != 0) { | ||||||||||||||||||||||||||||
| 234 | 0 | PiFlash::State->error(sprintf "%s command exited with value %d", $cmdname, $? >> 8); | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | 0 | return 1; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | ## use critic | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | # run a command and return the output as a string | ||||||||||||||||||||||||||||
| 241 | # This originally used qx() to fork child process and obtain output. But Perl::Critic discourages use of qx/backtick. | ||||||||||||||||||||||||||||
| 242 | # And it would be useful to provide input to child process, rather than using a wasteful echo-to-pipe shell command. | ||||||||||||||||||||||||||||
| 243 | # So the fork_exec_wrapper() was added as a lower-level base for cmd() and cmd2str(). | ||||||||||||||||||||||||||||
| 244 | ## no critic (RequireArgUnpacking) | ||||||||||||||||||||||||||||
| 245 | sub cmd2str | ||||||||||||||||||||||||||||
| 246 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
cmd2str: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 247 | 0 | my $cmdname = shift; | |||||||||||||||||||||||||||
| 248 | 0 | my ($out, $err) = fork_exec($cmdname, @_); | |||||||||||||||||||||||||||
| 249 | 0 | ▶ | if (defined $err) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | 0 | carp("$cmdname had error output:\n".$err); | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 0 | return split /\n/, $out; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | 0 | return $out; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | ## use critic | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | # generate name of environment variable for where to find a command | ||||||||||||||||||||||||||||
| 260 | # this is broken out as a separate function for tests to use it | ||||||||||||||||||||||||||||
| 261 | sub envprog | ||||||||||||||||||||||||||||
| 262 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
envprog: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 263 | 0 | my $progname = shift; | |||||||||||||||||||||||||||
| 264 | 0 | my $envprog = (uc $progname)."_PROG"; | |||||||||||||||||||||||||||
| 265 | 0 | $envprog =~ s/[\W-]+/_/g; # collapse any sequences of non-alphanumeric/non-underscore to a single underscore | |||||||||||||||||||||||||||
| 266 | 0 | return $envprog; | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | # look up secure program path | ||||||||||||||||||||||||||||
| 270 | ## no critic (RequireFinalReturn) | ||||||||||||||||||||||||||||
| 271 | sub prog | ||||||||||||||||||||||||||||
| 272 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
prog: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 273 | 0 | my $progname = shift; | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 0 | ▶ | if (!PiFlash::State::has_system("prog")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 0 | PiFlash::State::system("prog", {}); | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | 0 | my $prog = PiFlash::State::system("prog"); | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | # call with undef to initialize cache (mainly needed for testing because normal use will auto-create it) | ||||||||||||||||||||||||||||
| 281 | 0 | ▶ | if (!defined $progname) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 0 | return; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | # return value from cache if found | ||||||||||||||||||||||||||||
| 286 | 0 | ▶ | if (exists $prog->{$progname}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 0 | return $prog->{$progname}; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | # if we didn't have the location of the program, look for it and cache the result | ||||||||||||||||||||||||||||
| 291 | 0 | my $envprog = envprog($progname); | |||||||||||||||||||||||||||
| 292 | 0 | ▶ | if (exists $ENV{$envprog} and -x $ENV{$envprog}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $ENV{$envprog} and -x $ENV{$envprog}
MC/DC0%0/2
exists $ENV{$envprog} and -x $ENV{$envprog}
exists $ENV{$envprog}
-x $ENV{$envprog}
Truth table0%0/3
A and B
A= exists $ENV{$envprog}B= -x $ENV{$envprog}
| |||||||||||||||||||||||||||||
| 293 | 0 | $prog->{$progname} = $ENV{$envprog}; | |||||||||||||||||||||||||||
| 294 | 0 | return $prog->{$progname}; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | # search paths in order emphasizing recent Linux Filesystem that prefers /usr/bin, then Unix PATH order | ||||||||||||||||||||||||||||
| 298 | 0 | for my $path ("/usr/bin", "/sbin", "/usr/sbin", "/bin") { | |||||||||||||||||||||||||||
| 299 | 0 | ▶ | if (-x "$path/$progname") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 0 | $prog->{$progname} = "$path/$progname"; | |||||||||||||||||||||||||||
| 301 | 0 | return $prog->{$progname}; | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | # if we get here, we didn't find a known secure location for the program | ||||||||||||||||||||||||||||
| 306 | 0 | PiFlash::State->error("unknown secure location for $progname - install it or set " | |||||||||||||||||||||||||||
| 307 | ."$envprog to point to it"); | ||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | ## use critic | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 5 | 1; | |||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | __END__ | ||||||||||||||||||||||||||||