[PATCH v6 21/27] util: fix interleaving of error prefixes

Daniel P. Berrangé posted 27 patches 20 hours ago
Maintainers: Gerd Hoffmann <kraxel@redhat.com>, Christian Schoenebeck <qemu_oss@crudebyte.com>, Pierrick Bouvier <pierrick.bouvier@linaro.org>, Markus Armbruster <armbru@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, "Dr. David Alan Gilbert" <dave@treblig.org>, Manos Pitsidianakis <manos.pitsidianakis@linaro.org>, Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>, Stefan Weil <sw@weilnetz.de>
[PATCH v6 21/27] util: fix interleaving of error prefixes
Posted by Daniel P. Berrangé 20 hours ago
The vreport() function will optionally emit an prefix for error
messages which is output to stderr incrementally. In the event
that two vreport() calls execute concurrently, there is a risk
that the prefix output will interleave. To address this it is
required to take a lock on 'stderr' when outputting errors.

Reported-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 util/error-report.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/util/error-report.c b/util/error-report.c
index 052f0f68b8..516d323ffe 100644
--- a/util/error-report.c
+++ b/util/error-report.c
@@ -234,6 +234,9 @@ static void vreport(report_type type, const char *fmt, va_list ap)
     if (monitor_cur_is_qmp()) {
         cur = NULL;
     }
+    if (!cur) {
+        qemu_flockfile(stderr);
+    }
 
     if (message_with_timestamp && !cur) {
         timestr = real_time_iso8601();
@@ -261,6 +264,10 @@ static void vreport(report_type type, const char *fmt, va_list ap)
 
     error_vprintf_mon(cur, fmt, ap);
     error_printf_mon(cur, "\n");
+
+    if (!cur) {
+        qemu_funlockfile(stderr);
+    }
 }
 
 /*
-- 
2.53.0