[pve-devel] [PATCH mini-journalreader v3] fix #6410: properly close json output on error
Dominik Csapak
d.csapak at proxmox.com
Tue Jun 3 10:52:47 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.
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 at 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
More information about the pve-devel
mailing list