From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <c.heiss@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 31761910F3
 for <pve-devel@lists.proxmox.com>; Tue, 13 Feb 2024 16:14:15 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 19F2938332
 for <pve-devel@lists.proxmox.com>; Tue, 13 Feb 2024 16:14:15 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Tue, 13 Feb 2024 16:14:14 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 06E2647CD6
 for <pve-devel@lists.proxmox.com>; Tue, 13 Feb 2024 16:14:14 +0100 (CET)
From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue, 13 Feb 2024 16:14:00 +0100
Message-ID: <20240213151405.1282639-4-c.heiss@proxmox.com>
X-Mailer: git-send-email 2.43.0
In-Reply-To: <20240213151405.1282639-1-c.heiss@proxmox.com>
References: <20240213151405.1282639-1-c.heiss@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.003 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [proxmox.com, run-command.pl, command.pm]
Subject: [pve-devel] [PATCH installer v2 3/6] sys: command: handle EINTR in
 run_command()
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 13 Feb 2024 15:14:15 -0000

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@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