From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [RFC PATCH mini-journalreader v2 2/2] include error in json output
Date: Mon, 2 Jun 2025 16:30:50 +0200 [thread overview]
Message-ID: <20250602143050.3732346-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20250602143050.3732346-1-d.csapak@proxmox.com>
by using json-c and printing the string into a variable via `vasprintf`.
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
changes from RFC v1:
* rebase on last patch and also use vfprintf in fallback case
debian/control | 2 +-
src/Makefile | 2 +-
src/mini-journalreader.c | 31 +++++++++++++++++++++++++------
3 files changed, 27 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 562b390..e3982ab 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,17 +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;
+ char *message;
va_start(args, fmt);
- vfprintf(stderr, fmt, args);
+ 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
+ vfprintf(stderr, fmt, args);
+ if (json) {
+ fprintf(stdout, "],\"success\":0}");
+ }
+ exit(1);
+ }
+ fprintf(stderr, "%s", message);
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);
+ 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
next prev parent reply other threads:[~2025-06-02 14:30 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-02 14:30 [pve-devel] [PATCH mini-journalreader v2 1/2] fix #6410: properly close json output on error Dominik Csapak
2025-06-02 14:30 ` Dominik Csapak [this message]
2025-06-03 7:16 ` [pve-devel] [RFC PATCH mini-journalreader v2 2/2] include error in json output 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-2-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.