[PATCH] virsh: fflush(stdout) after fputs()

Michal Privoznik posted 1 patch 2 years, 1 month ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/940d88fa327c796c0a9955974bc2b1e5fa40e9f3.1646831479.git.mprivozn@redhat.com
Test syntax-check failed
tools/vsh.c | 4 ++++
1 file changed, 4 insertions(+)
[PATCH] virsh: fflush(stdout) after fputs()
Posted by Michal Privoznik 2 years, 1 month ago
We are not guaranteed that the string we are printing onto stdout
contains '\n' and thus that the stdout is flushed. In fact, I've
met this problem when virsh asked me whether I want to edit the
domain XML again (vshAskReedit()) but the prompt wasn't displayed
(as it does not contain a newline character) and virsh just sat
there waiting for my input, I sat there waiting for virsh's
output. Flush stdout after all fputs()-s  which do not flush
stdout.

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

diff --git a/tools/vsh.c b/tools/vsh.c
index bbde594967..499794c8fc 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -1863,6 +1863,7 @@ vshDebug(vshControl *ctl, int level, const char *format, ...)
     str = g_strdup_vprintf(format, ap);
     va_end(ap);
     fputs(str, stdout);
+    fflush(stdout);
 }
 
 void
@@ -1878,6 +1879,7 @@ vshPrintExtra(vshControl *ctl, const char *format, ...)
     str = g_strdup_vprintf(format, ap);
     va_end(ap);
     fputs(str, stdout);
+    fflush(stdout);
 }
 
 
@@ -1891,6 +1893,7 @@ vshPrint(vshControl *ctl G_GNUC_UNUSED, const char *format, ...)
     str = g_strdup_vprintf(format, ap);
     va_end(ap);
     fputs(str, stdout);
+    fflush(stdout);
 }
 
 
@@ -2938,6 +2941,7 @@ vshReadline(vshControl *ctl G_GNUC_UNUSED,
     int len;
 
     fputs(prompt, stdout);
+    fflush(stdout);
     r = fgets(line, sizeof(line), stdin);
     if (r == NULL) return NULL; /* EOF */
 
-- 
2.34.1
Re: [PATCH] virsh: fflush(stdout) after fputs()
Posted by Ján Tomko 2 years, 1 month ago
On a Wednesday in 2022, Michal Privoznik wrote:
>We are not guaranteed that the string we are printing onto stdout
>contains '\n' and thus that the stdout is flushed. In fact, I've
>met this problem when virsh asked me whether I want to edit the
>domain XML again (vshAskReedit()) but the prompt wasn't displayed
>(as it does not contain a newline character) and virsh just sat
>there waiting for my input, I sat there waiting for virsh's
>output. Flush stdout after all fputs()-s  which do not flush
>stdout.
>
>Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
>---
> tools/vsh.c | 4 ++++
> 1 file changed, 4 insertions(+)
>

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano