all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer v2 4/6] sys: command: allow terminating the process early from log subroutine
Date: Tue, 13 Feb 2024 16:14:01 +0100	[thread overview]
Message-ID: <20240213151405.1282639-5-c.heiss@proxmox.com> (raw)
In-Reply-To: <20240213151405.1282639-1-c.heiss@proxmox.com>

If the logging subroutine $func returns CMD_FINISHED after processing a
line, the running subprocess is killed early.
This mechanism can be used when e.g. only a certain part of the output
of a (long-running) command is needed, avoiding the extra time it would
take the command to finish properly.

This is done in a entirely backwards-compatible way, i.e. existing
usages don't need any modification.

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
Changes since v1:
  * introduced constant for return value (thanks Thomas!)
  * added documentation

 Proxmox/Sys/Command.pm | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/Proxmox/Sys/Command.pm b/Proxmox/Sys/Command.pm
index 6389b17..36e99c1 100644
--- a/Proxmox/Sys/Command.pm
+++ b/Proxmox/Sys/Command.pm
@@ -15,7 +15,9 @@ use Proxmox::Log;
 use Proxmox::UI;

 use base qw(Exporter);
-our @EXPORT_OK = qw(run_command syscmd);
+our @EXPORT_OK = qw(run_command syscmd CMD_FINISHED);
+
+use constant CMD_FINISHED => 1;

 my sub shellquote {
     my $str = shift;
@@ -79,7 +81,8 @@ sub syscmd {
 #
 # Arguments:
 # * $cmd - The command to run, either a single string or array with individual arguments
-# * $func - Logging subroutine to call, receives both stdout and stderr
+# * $func - Logging subroutine to call, receives both stdout and stderr. Might return CMD_FINISHED
+#           to exit early and ignore the rest of the process output.
 # * $input - Stdin contents for the spawned subprocess
 # * $noout - Whether to append any process output to the return value
 sub run_command {
@@ -163,7 +166,13 @@ sub run_command {
 		$logout .= $buf;
 		while ($logout =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) {
 		    my $line = $1;
-		    $func->($line) if $func;
+		    if ($func) {
+			my $ret = $func->($line);
+			if (defined($ret) && $ret == CMD_FINISHED) {
+			    wait_for_process($pid, kill => 1);
+			    return $ostream;
+			}
+		    };
 		}

 	    } elsif ($h eq $error) {
--
2.43.0





  parent reply	other threads:[~2024-02-13 15:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-13 15:13 [pve-devel] [PATCH installer v2 0/6] fix #4872: properly timeout `traceroute` command in country detection Christoph Heiss
2024-02-13 15:13 ` [pve-devel] [PATCH installer v2 1/6] low-level: initialize UI backend for 'dump-env' subcommand too Christoph Heiss
2024-02-13 15:13 ` [pve-devel] [PATCH installer v2 2/6] sys: command: factor out kill() + waitpid() from run_command() Christoph Heiss
2024-02-13 15:14 ` [pve-devel] [PATCH installer v2 3/6] sys: command: handle EINTR in run_command() Christoph Heiss
2024-02-13 15:14 ` Christoph Heiss [this message]
2024-02-13 15:14 ` [pve-devel] [PATCH installer v2 5/6] sys: command: add option to not print process output to stdout Christoph Heiss
2024-02-13 15:14 ` [pve-devel] [PATCH installer v2 6/6] fix #4872: run env: use run_command() for country detection Christoph Heiss
2024-02-23 15:17 ` [pve-devel] applied-esries: [PATCH installer v2 0/6] fix #4872: properly timeout `traceroute` command in " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240213151405.1282639-5-c.heiss@proxmox.com \
    --to=c.heiss@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal