]>
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 $terminated = waitpid($pid, WNOHANG
);
53 return $? if $terminated > 0;
55 kill('KILL', $pid) if $params{kill};
57 my $timeout = $params{timeout
} // 5;
59 $terminated = waitpid($pid, WNOHANG
);
60 return $? if $terminated > 0;
64 log_warn
("failed to kill child pid $pid, probably stuck in D-state?\n");
66 # We tried our best, better let the child hang in the back then completely
67 # blocking installer progress .. it's a rather short-lived environment anyway
73 return run_command
($cmd, undef, undef, 1);
76 # Runs a command an a subprocess, properly handling IO via piping, cleaning up and passing back the
79 # If $cmd contains a pipe |, the command will be executed inside a bash shell.
80 # If $cmd contains 'chpasswd', the input will be specially quoted for that purpose.
83 # * $cmd - The command to run, either a single string or array with individual arguments
84 # * $func - Logging subroutine to call, receives both stdout and stderr. Might return CMD_FINISHED
85 # to exit early and ignore the rest of the process output.
86 # * $input - Stdin contents for the spawned subprocess
87 # * $noout - Whether to append any process output to the return value
88 # * $noprint - Whether to print any process output to the parents stdout
90 my ($cmd, $func, $input, $noout, $noprint) = @_;
96 # see 'man bash' for option pipefail
97 $cmd = [ '/bin/bash', '-c', "set -o pipefail && $cmd" ];
102 $cmdstr = cmd2string
($cmd);
106 if ($input && ($cmdstr !~ m/chpasswd/)) {
107 $cmdtxt = "# $cmdstr <<EOD\n$input";
109 $cmdtxt .= "\nEOD\n";
111 $cmdtxt = "# $cmdstr\n";
114 if (is_test_mode
()) {
120 my ($reader, $writer, $error) = (IO
::File-
>new(), IO
::File-
>new(), IO
::File-
>new());
124 my $pid = eval { open3
($writer, $reader, $error, @$cmd) || die $!; };
127 if ($orig_pid != $$) { # catch exec errors
133 print $writer $input if defined $input;
136 my $select = IO
::Select-
>new();
137 $select->add($reader);
138 $select->add($error);
140 my ($ostream, $logout) = ('', '', '');
143 while ($select->count) {
144 my @handles = $select->can_read (0.2);
146 # If we catch a signal, stop processing & clean up
152 Proxmox
::UI
::process_events
();
154 next if !scalar (@handles); # timeout
156 foreach my $h (@handles) {
158 my $count = sysread ($h, $buf, 4096);
159 if (!defined ($count)) {
161 wait_for_process
($pid, kill => 1);
162 die "command '$cmd' failed: $err";
164 $select->remove($h) if !$count;
166 $ostream .= $buf if !($noout || $func);
168 while ($logout =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) {
171 my $ret = $func->($line);
172 if (defined($ret) && $ret == CMD_FINISHED
) {
173 wait_for_process
($pid, kill => 1);
179 } elsif ($h eq $error) {
180 $ostream .= $buf if !($noout || $func);
182 print $buf if !$noprint;
188 &$func($logout) if $func;
190 my $ec = wait_for_process
($pid, kill => $caught_sig);
192 # behave like standard system(); returns -1 in case of errors too
193 return ($ec // -1) if $noout;
196 # Don't fail completely here to let the install continue
197 warn "command '$cmdstr' failed to exit properly\n";
198 } elsif ($ec == -1) {
199 croak
"command '$cmdstr' failed to execute\n";
200 } elsif (my $sig = ($ec & 127)) {
201 croak
"command '$cmdstr' failed - got signal $sig\n";
202 } elsif (my $exitcode = ($ec >> 8)) {
203 croak
"command '$cmdstr' failed with exit code $exitcode";
209 # forks and runs the provided coderef in the child
210 # do not use syscmd or run_command as both confuse the GTK mainloop if
211 # run from a child process
212 sub run_in_background
{
215 my $pid = fork() // die "fork failed: $!\n";
219 warn "run_in_background error: $err\n";