public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH mini-journalreader v2 1/2] fix #6410: properly close json output on error
Date: Mon,  2 Jun 2025 16:30:49 +0200	[thread overview]
Message-ID: <20250602143050.3732346-1-d.csapak@proxmox.com> (raw)

when encountering an error (e.g. there was no curser at the wanted
position), the program would close with exitcode 1, but would not
properly finish the json output if in json mode.

To fix that, introduce a `print_error_and_exit` helper that tries to
finish the json output. We set `success` to 0 to indicate an error.

Ideally we would insert the error message into the json output, but
since that can be anything, and json expects utf8 and properly escaped
strings we'd either have to do a lot of things manually here, or use a
json library for escaping. So for now simply don't write the error in
the output.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
changes from v1:
* use va_start/end and vfprintf, otherwise the variadic args won't work

 src/mini-journalreader.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/src/mini-journalreader.c b/src/mini-journalreader.c
index 98bcaac..562b390 100644
--- a/src/mini-journalreader.c
+++ b/src/mini-journalreader.c
@@ -30,6 +30,23 @@ static char BUF[BUFSIZE];
 bool json = false;
 bool first_line = true;
 
+// helper to print errors on stderr
+// if we're in json mode, print closing json body if possible
+static void print_error_and_exit(const char *fmt, ...) {
+    va_list args;
+    va_start(args, fmt);
+    vfprintf(stderr, fmt, args);
+    va_end(args);
+    if (json) {
+        size_t r = fwrite_unlocked("],\"success\":0}", 1, 14, stdout);
+        if (r < 14) {
+            fprintf(stderr, "Failed to write closing json\n");
+        }
+        fflush_unlocked(stdout);
+    }
+    exit(1);
+}
+
 static uint64_t get_timestamp(sd_journal *j) {
     uint64_t timestamp;
     int r = sd_journal_get_realtime_usec(j, &timestamp);
@@ -47,6 +64,7 @@ static void print_to_buf(const char * string, size_t length) {
 
     size_t r = fwrite_unlocked(string, 1, length, stdout);
     if (r < length) {
+        // don't use error helper here as we can't write to stdout anyway
         fprintf(stderr, "Failed to write\n");
         exit(1);
     }
@@ -57,8 +75,7 @@ static void print_cursor(sd_journal *j) {
     char *cursor = NULL;
     r = sd_journal_get_cursor(j, &cursor);
     if (r < 0) {
-        fprintf(stderr, "Failed to get cursor: %s\n", strerror(-r));
-        exit(1);
+        print_error_and_exit("Failed to get cursor: %s\n", strerror(-r));
     }
     if (json) {
         if (!first_line) {
@@ -242,6 +259,7 @@ static uint64_t arg_to_uint64(const char *argument) {
     uint64_t value = strtoull(argument, &end, 10);
     if (errno != 0 || *end != '\0') {
         fprintf(stderr, "%s is not a valid integer number\n", argument);
+        // don't use error helper here, as we did not start outputting json yet
         exit(1);
     }
 
@@ -348,15 +366,13 @@ int main(int argc, char *argv[]) {
         }
 
         if (r < 0) {
-            fprintf(stderr, "Failed to seek to end/cursor: %s\n", strerror(-r));
-            exit(1);
+            print_error_and_exit( "Failed to seek to end/cursor: %s\n", strerror(-r));
         }
 
         // seek back number entries and print cursor
         r = sd_journal_previous_skip(j, number + 1);
         if (r < 0) {
-            fprintf(stderr, "Failed to seek back: %s\n", strerror(-r));
-            exit(1);
+            print_error_and_exit("Failed to seek back: %s\n", strerror(-r));
         }
     } else {
         if (begin) {
@@ -368,16 +384,14 @@ int main(int argc, char *argv[]) {
         }
 
         if (r < 0) {
-            fprintf(stderr, "Failed to seek to begin/cursor: %s\n", strerror(-r));
-            exit(1);
+            print_error_and_exit("Failed to seek to begin/cursor: %s\n", strerror(-r));
         }
 
         // if we have a start cursor, we want to skip the first entry
         if (startcursor) {
             r = sd_journal_next(j);
             if (r < 0) {
-                fprintf(stderr, "Failed to seek to begin/cursor: %s\n", strerror(-r));
-                exit(1);
+                print_error_and_exit("Failed to seek to begin/cursor: %s\n", strerror(-r));
             }
             print_first_cursor(j);
         }
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


             reply	other threads:[~2025-06-02 14:31 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-02 14:30 Dominik Csapak [this message]
2025-06-02 14:30 ` [pve-devel] [RFC PATCH mini-journalreader v2 2/2] include error in json output Dominik Csapak
2025-06-03  7:16   ` Dominik Csapak
2025-06-03  8:53 ` [pve-devel] [PATCH mini-journalreader v2 1/2] fix #6410: properly close json output on error Dominik Csapak

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=20250602143050.3732346-1-d.csapak@proxmox.com \
    --to=d.csapak@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal