From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id B67941FF191
	for <inbox@lore.proxmox.com>; Mon,  2 Jun 2025 16:24:07 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id E639735D7B;
	Mon,  2 Jun 2025 16:24:22 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Mon,  2 Jun 2025 16:24:18 +0200
Message-Id: <20250602142419.3692918-1-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.39.5
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.022 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH mini-journalreader 1/2] fix #6410: properly
 close json output on error
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

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>
---
 src/mini-journalreader.c | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/src/mini-journalreader.c b/src/mini-journalreader.c
index 98bcaac..d934d43 100644
--- a/src/mini-journalreader.c
+++ b/src/mini-journalreader.c
@@ -30,6 +30,21 @@ 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;
+    fprintf(stderr, fmt, 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 +62,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 +73,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 +257,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 +364,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 +382,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