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 D5BD41FF15E for <inbox@lore.proxmox.com>; Tue, 3 Jun 2025 10:53:03 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id C753212DCF; Tue, 3 Jun 2025 10:53:20 +0200 (CEST) From: Dominik Csapak <d.csapak@proxmox.com> To: pve-devel@lists.proxmox.com Date: Tue, 3 Jun 2025 10:52:47 +0200 Message-Id: <20250603085247.917673-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.020 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 KAM_SHORT 0.001 Use of a URL Shortener for very short URL 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 v3] 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. Include the error message in the json output too if possible, but encode problematic characters like we do for the messages. Signed-off-by: Dominik Csapak <d.csapak@proxmox.com> --- changes from v3: * better handling of the va args; use start/end on every path we need it, otherwise it's maybe not correctly initialized * flush stdout before exit * don't use json-c, but use our own escaping like in print_message src/mini-journalreader.c | 64 +++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/src/mini-journalreader.c b/src/mini-journalreader.c index 98bcaac..04f7c30 100644 --- a/src/mini-journalreader.c +++ b/src/mini-journalreader.c @@ -15,6 +15,10 @@ * If not, see <https://www.gnu.org/licenses/>. */ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + #include <errno.h> #include <stdbool.h> #include <stdio.h> @@ -30,6 +34,49 @@ 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, ...) { + fflush_unlocked(stdout); + + va_list args_message; + char *message = NULL; + va_start(args_message, fmt); + int message_len = vasprintf(&message, fmt, args_message); + va_end(args_message); + + if (message_len < 0 || message == NULL) { + // could not allocate the string, so write it directly and don't + // include the error in the json output + va_list args; + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + if (json) { + fprintf(stdout, "],\"success\":0}"); + fflush(stdout); + } + exit(1); + } + fprintf(stderr, "%s", message); + + if (json) { + fprintf(stdout, "], \"error\":\""); + for (int i = 0; i < message_len; i++) { + char c = message[i]; + if (c == '"' || c == '\\' || (c >= 0 && c <= 0x1F)) { + fprintf(stdout, "\\u%04X", c); + } else { + fprintf(stdout, "%c", c); + } + } + fprintf(stdout, "\",\"success\":0}"); + fflush(stdout); + } + free(message); + exit(1); +} + static uint64_t get_timestamp(sd_journal *j) { uint64_t timestamp; int r = sd_journal_get_realtime_usec(j, ×tamp); @@ -47,6 +94,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 +105,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 +289,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 +396,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 +414,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