[PATCH 4/6] vsh: Close stderr among with stdin in cmdComplete

Michal Privoznik posted 6 patches 3 months, 3 weeks ago
[PATCH 4/6] vsh: Close stderr among with stdin in cmdComplete
Posted by Michal Privoznik 3 months, 3 weeks ago
Our completer callbacks must refrain from printing anything onto
stderr, but unfortunately that's not how service code around
behaves. It may call vshError() and what not. Rather trying to
fix all possible paths (just consider opening a connection), just
close the stderr. We're already closing stdin.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 tools/vsh.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/tools/vsh.c b/tools/vsh.c
index 61a3066f49..de869248b4 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -3488,17 +3488,20 @@ const vshCmdInfo info_complete = {
 
 #ifdef WITH_READLINE
 
-static virOnceControl vshCmdCompleteCloseStdinOnce = VIR_ONCE_CONTROL_INITIALIZER;
+static virOnceControl vshCmdCompleteCloseStdinStderrOnce = VIR_ONCE_CONTROL_INITIALIZER;
 
 static void
-vshCmdCompleteCloseStdin(void)
+vshCmdCompleteCloseStdinStderr(void)
 {
     /* In non-interactive mode which is how the 'complete' command is intended
      * to be used we need to ensure that any authentication callback will not
-     * attempt to read any input which would break the completion */
+     * attempt to read any input which would break the completion. Similarly,
+     * printing anything onto stderr should be avoided. */
     int stdin_fileno = STDIN_FILENO;
+    int stderr_fileno = STDERR_FILENO;
 
     VIR_FORCE_CLOSE(stdin_fileno);
+    VIR_FORCE_CLOSE(stderr_fileno);
 }
 
 
@@ -3519,7 +3522,7 @@ cmdComplete(vshControl *ctl, const vshCmd *cmd)
      * need to prevent auth hooks reading any input. Therefore, we
      * have to close stdin and then connect ourselves. */
     if (!ctl->imode) {
-        if (virOnce(&vshCmdCompleteCloseStdinOnce, vshCmdCompleteCloseStdin) < 0)
+        if (virOnce(&vshCmdCompleteCloseStdinStderrOnce, vshCmdCompleteCloseStdinStderr) < 0)
             return false;
     }
 
-- 
2.44.1
Re: [PATCH 4/6] vsh: Close stderr among with stdin in cmdComplete
Posted by Peter Krempa 3 months, 3 weeks ago
On Mon, May 27, 2024 at 11:18:52 +0200, Michal Privoznik wrote:
> Our completer callbacks must refrain from printing anything onto
> stderr, but unfortunately that's not how service code around
> behaves. It may call vshError() and what not. Rather trying to
> fix all possible paths (just consider opening a connection), just
> close the stderr. We're already closing stdin.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  tools/vsh.c | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)

Reviewed-by: Peter Krempa <pkrempa@redhat.com>