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 [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id C468B1FF191 for <inbox@lore.proxmox.com>; Mon, 2 Jun 2025 16:24:12 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 7B9AF35E31; Mon, 2 Jun 2025 16:24:23 +0200 (CEST) From: Dominik Csapak <d.csapak@proxmox.com> To: pve-devel@lists.proxmox.com Date: Mon, 2 Jun 2025 16:24:19 +0200 Message-Id: <20250602142419.3692918-2-d.csapak@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250602142419.3692918-1-d.csapak@proxmox.com> References: <20250602142419.3692918-1-d.csapak@proxmox.com> 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 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [gnu.org] Subject: [pve-devel] [RFC PATCH mini-journalreader 2/2] include error in json output 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> by using json-c and printing the string into a variable via `vasprintf`. we use a json library to correctly escape and format the error string we get, since we cannot be sure about the content. Signed-off-by: Dominik Csapak <d.csapak@proxmox.com> --- not sure if it's worth to include json-c here just for the error message... debian/control | 2 +- src/Makefile | 2 +- src/mini-journalreader.c | 33 +++++++++++++++++++++++++++------ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/debian/control b/debian/control index 8280f6d..d03772e 100644 --- a/debian/control +++ b/debian/control @@ -2,7 +2,7 @@ Source: proxmox-mini-journalreader Section: admin Priority: optional Maintainer: Proxmox Support Team <support@proxmox.com> -Build-Depends: debhelper-compat (= 13), libsystemd-dev, pkg-config, scdoc, +Build-Depends: debhelper-compat (= 13), libsystemd-dev, pkg-config, scdoc, libjson-c-dev Standards-Version: 4.6.2 Package: proxmox-mini-journalreader diff --git a/src/Makefile b/src/Makefile index 449004f..adb2782 100644 --- a/src/Makefile +++ b/src/Makefile @@ -4,7 +4,7 @@ SOURCES=mini-journalreader.c BIN_DIR ?= $(DESTDIR)/usr/bin MAN1_DIR ?= $(DESTDIR)/usr/share/man/man1 -LIBS := libsystemd +LIBS := libsystemd json-c CFLAGS += -Werror -Wall -Wextra -Wl,-z,relro -g -O2 --std=gnu11 CFLAGS += -fstack-protector-strong -D_FORTIFY_SOURCE=2 CFLAGS += $(shell pkg-config --cflags $(LIBS)) diff --git a/src/mini-journalreader.c b/src/mini-journalreader.c index d934d43..62d8850 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> @@ -23,6 +27,7 @@ #include <systemd/sd-journal.h> #include <time.h> #include <unistd.h> +#include <json.h> #define BUFSIZE 4096 @@ -33,15 +38,31 @@ 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; - 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"); + char *message; + va_start(args, fmt); + int r = vasprintf(&message, fmt, args); + if (r < 0) { + // could not allocate the string, so write it directly and don't + // include the error in the json output + fprintf(stderr, fmt, args); + if (json) { + fprintf(stdout, "],\"success\":0}"); } - fflush_unlocked(stdout); + exit(1); + } + fprintf(stderr, "%s", message); + va_end(args); + + if (json) { + struct json_object *obj = json_object_new_string(message); + const char *json_message = json_object_to_json_string(obj); + fprintf(stdout, "], \"error\":%s,\"success\":0}", json_message); + json_object_put(obj); } + free(message); exit(1); } -- 2.39.5 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel