[pve-devel] [PATCH mini-journalreader v2 1/2] fix #6410: properly close json output on error
Dominik Csapak
d.csapak at proxmox.com
Mon Jun 2 16:30:49 CEST 2025
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 at 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, ×tamp);
@@ -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
More information about the pve-devel
mailing list