]>
git.proxmox.com Git - pve-installer.git/blob - Proxmox/Sys/Command.pm
1 package Proxmox
::Sys
::Command
;
10 use String
::ShellQuote
;
11 use POSIX
":sys_wait_h";
13 use Proxmox
::Install
::ISOEnv
;
17 use base
qw(Exporter);
18 our @EXPORT_OK = qw(run_command syscmd CMD_FINISHED);
20 use constant CMD_FINISHED
=> 1;
24 return String
::ShellQuote
::shell_quote
($str);
30 die "no arguments" if !$cmd;
31 return $cmd if !ref($cmd);
33 my $quoted_args = [ map { shellquote
($_) } $cmd->@* ];
35 return join (' ', $quoted_args->@*);
38 # Safely for the (sub-)process specified by $pid to exit, using a timeout.
40 # When kill => 1 is set, at first a TERM-signal is sent to the process before
41 # checking if it exited.
42 # If that fails, KILL is sent to process and then up to timeout => $timeout
43 # seconds (default: 5) are waited for the process to exit.
45 # On sucess, the exitcode of the process is returned, otherwise `undef` (aka.
46 # the process was unkillable).
47 my sub wait_for_process
{
48 my ($pid, %params) = @_;
50 kill('TERM', $pid) if $params{kill};
52 my $timeout = $params{timeout
} // 5;
54 my $terminated = waitpid($pid, WNOHANG
);
55 return $? if $terminated > 0;
57 sleep(1) if $_ != $timeout; # all but last round
58 kill('KILL', $pid) if $params{kill} && $_ == 1; # just first round
61 log_warn
("failed to kill child pid $pid, probably stuck in D-state?\n");
63 # We tried our best, better let the child hang in the back then completely
64 # blocking installer progress .. it's a rather short-lived environment anyway
70 return run_command
($cmd, undef, undef, 1);
73 # Runs a command an a subprocess, properly handling IO via piping, cleaning up and passing back the
76 # If $cmd contains a pipe |, the command will be executed inside a bash shell.
77 # If $cmd contains 'chpasswd', the input will be specially quoted for that purpose.
80 # * $cmd - The command to run, either a single string or array with individual arguments
81 # * $func - Logging subroutine to call, receives both stdout and stderr. Might return CMD_FINISHED
82 # to exit early and ignore the rest of the process output.
83 # * $input - Stdin contents for the spawned subprocess
84 # * $noout - Whether to append any process output to the return value
85 # * $noprint - Whether to print any process output to the parents stdout
87 my ($cmd, $func, $input, $noout, $noprint) = @_;
93 # see 'man bash' for option pipefail
94 $cmd = [ '/bin/bash', '-c', "set -o pipefail && $cmd" ];
99 $cmdstr = cmd2string
($cmd);
103 if ($input && ($cmdstr !~ m/chpasswd/)) {
104 $cmdtxt = "# $cmdstr <<EOD\n$input";
106 $cmdtxt .= "\nEOD\n";
108 $cmdtxt = "# $cmdstr\n";
111 if (is_test_mode
()) {
117 my ($reader, $writer, $error) = (IO
::File-
>new(), IO
::File-
>new(), IO
::File-
>new());
121 my $pid = eval { open3
($writer, $reader, $error, @$cmd) || die $!; };
124 if ($orig_pid != $$) { # catch exec errors
130 print $writer $input if defined $input;
133 my $select = IO
::Select-
>new();
134 $select->add($reader);
135 $select->add($error);
137 my ($ostream, $logout) = ('', '', '');
140 while ($select->count) {
141 my @handles = $select->can_read (0.2);
143 # If we catch a signal, stop processing & clean up
149 Proxmox
::UI
::process_events
();
151 next if !scalar (@handles); # timeout
153 foreach my $h (@handles) {
155 my $count = sysread ($h, $buf, 4096);
156 if (!defined ($count)) {
158 wait_for_process
($pid, kill => 1);
159 die "command '$cmd' failed: $err";
161 $select->remove($h) if !$count;
163 $ostream .= $buf if !($noout || $func);
165 while ($logout =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) {
168 my $ret = $func->($line);
169 if (defined($ret) && $ret == CMD_FINISHED
) {
170 wait_for_process
($pid, kill => 1);
176 } elsif ($h eq $error) {
177 $ostream .= $buf if !($noout || $func);
179 print $buf if !$noprint;
185 &$func($logout) if $func;
187 my $ec = wait_for_process
($pid, kill => $caught_sig);
189 # behave like standard system(); returns -1 in case of errors too
190 return ($ec // -1) if $noout;
193 # Don't fail completely here to let the install continue
194 warn "command '$cmdstr' failed to exit properly\n";
195 } elsif ($ec == -1) {
196 croak
"command '$cmdstr' failed to execute\n";
197 } elsif (my $sig = ($ec & 127)) {
198 croak
"command '$cmdstr' failed - got signal $sig\n";
199 } elsif (my $exitcode = ($ec >> 8)) {
200 croak
"command '$cmdstr' failed with exit code $exitcode";
206 # forks and runs the provided coderef in the child
207 # do not use syscmd or run_command as both confuse the GTK mainloop if
208 # run from a child process
209 sub run_in_background
{
212 my $pid = fork() // die "fork failed: $!\n";
216 warn "run_in_background error: $err\n";