[pve-devel] [PATCH installer v2 3/6] sys: command: handle EINTR in run_command()

Christoph Heiss c.heiss at proxmox.com
Tue Feb 13 16:14:00 CET 2024


Previously, the I/O loop would continue endlessly until the subprocess
exited.
This explicit handling allows run_command() to be used with e.g.
alarm().

Signed-off-by: Christoph Heiss <c.heiss at proxmox.com>
---
Changes since v1:
  * new patch

 Proxmox/Sys/Command.pm |  9 ++++++++-
 test/run-command.pl    | 11 +++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/Proxmox/Sys/Command.pm b/Proxmox/Sys/Command.pm
index e64e0ee..6389b17 100644
--- a/Proxmox/Sys/Command.pm
+++ b/Proxmox/Sys/Command.pm
@@ -134,10 +134,17 @@ sub run_command {
     $select->add($error);

     my ($ostream, $logout) = ('', '', '');
+    my $caught_sig;

     while ($select->count) {
 	my @handles = $select->can_read (0.2);

+	# If we catch a signal, stop processing & clean up
+	if ($!{EINTR}) {
+	    $caught_sig = 1;
+	    last;
+	}
+
 	Proxmox::UI::process_events();

 	next if !scalar (@handles); # timeout
@@ -170,7 +177,7 @@ sub run_command {

     &$func($logout) if $func;

-    my $ec = wait_for_process($pid);
+    my $ec = wait_for_process($pid, kill => $caught_sig);

     # behave like standard system(); returns -1 in case of errors too
     return ($ec // -1) if $noout;
diff --git a/test/run-command.pl b/test/run-command.pl
index 7d5805e..19bdce0 100755
--- a/test/run-command.pl
+++ b/test/run-command.pl
@@ -27,6 +27,17 @@ my $ret = run_command('bash -c "echo test; sleep 1000; echo test"', sub {
 });
 is($ret, '', 'using CMD_FINISHED');

+# https://bugzilla.proxmox.com/show_bug.cgi?id=4872
+my $prev;
+eval {
+    local $SIG{ALRM} = sub { die "timed out!\n" };
+    $prev = alarm(1);
+    $ret = run_command('sleep 5');
+};
+alarm($prev);
+
+is($@, "timed out!\n", 'SIGALRM interaction');
+
 # Check the log for errors/warnings
 my $log = file_read_all($log_file->filename);
 ok($log !~ m/(WARN|ERROR): /, 'no warnings or errors logged');
--
2.43.0





More information about the pve-devel mailing list