logs-show: handle bad messages like EOF

Similar to the previous commit, but for logs-show.c
This commit is contained in:
Lennart Poettering
2025-05-13 15:39:57 +02:00
parent 3a8d562a21
commit 010e2562dd
+61 -11
View File
@@ -811,7 +811,7 @@ static int output_verbose(
r = get_display_realtime(j, &usec);
if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) {
log_debug_errno(r, "Skipping message we can't read: %m");
log_debug_errno(r, "Unable to read realtime timestamp from entry, assuming bad or partially written entry: %m");
return 0;
}
if (r < 0)
@@ -821,6 +821,10 @@ static int output_verbose(
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No valid realtime timestamp available");
r = sd_journal_get_cursor(j, &cursor);
if (r == -EBADMSG) {
log_debug_errno(r, "Unable to determine cursor for entry, assuming bad or partially written entry: %m");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to get cursor: %m");
@@ -841,12 +845,16 @@ static int output_verbose(
size_t fieldlen, valuelen;
c = memchr(data, '=', length);
if (!c)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid field.");
if (!c) {
log_debug("Encountered field without '=', assuming bad or partially written entry, leaving.");
break;
}
fieldlen = c - (const char*) data;
if (!journal_field_valid(data, fieldlen, true))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid field.");
if (!journal_field_valid(data, fieldlen, /* allow_protected= */ true)) {
log_debug("Encountered invalid field, assuming bad or partially written entry, leaving.");
break;
}
r = field_set_test(output_fields, data, fieldlen);
if (r < 0)
@@ -929,18 +937,34 @@ static int output_export(
(void) sd_journal_set_data_threshold(j, 0);
r = sd_journal_get_cursor(j, &cursor);
if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) {
log_debug_errno(r, "Unable to determine cursor of entry, assuming bad or partially written entry: %m");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to get cursor: %m");
r = sd_journal_get_realtime_usec(j, &realtime);
if (r == -EBADMSG) {
log_debug_errno(r, "Unable to read realtime timestamp of entry, assuming bad or partially written entry: %m");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to get realtime timestamp: %m");
r = sd_journal_get_monotonic_usec(j, &monotonic, &journal_boot_id);
if (r == -EBADMSG) {
log_debug_errno(r, "Unable to read monotonic timestamp of entry, assuming bad or partially written entry: %m");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to get monotonic timestamp: %m");
r = sd_journal_get_seqnum(j, &seqnum, &seqnum_id);
if (r == -EBADMSG) {
log_debug_errno(r, "Unable to read sequence number of entry, assuming bad or partially written entry: %m");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to get seqnum: %m");
@@ -967,12 +991,16 @@ static int output_export(
continue;
c = memchr(data, '=', length);
if (!c)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid field.");
if (!c) {
log_debug("Encountered data field without '=', assuming bad or partially written entry, leaving.");
break;
}
fieldlen = c - (const char*) data;
if (!journal_field_valid(data, fieldlen, true))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid field.");
if (!journal_field_valid(data, fieldlen, /* allow_protected= */ true)) {
log_debug("Encountered invalid field, assuming bad or partially written entry, leaving.");
break;
}
r = field_set_test(output_fields, data, fieldlen);
if (r < 0)
@@ -1158,8 +1186,10 @@ static int update_json_data_split(
return 0;
fieldlen = eq - (const char*) data;
if (!journal_field_valid(data, fieldlen, true))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid field.");
if (!journal_field_valid(data, fieldlen, /* allow_protected= */ true)) {
log_debug("Encountered invalid field, assuming bad or incompletely written field, leaving.");
return 0;
}
name = strndupa_safe(data, fieldlen);
if (output_fields && !set_contains(output_fields, name))
@@ -1196,18 +1226,34 @@ static int output_json(
(void) sd_journal_set_data_threshold(j, flags & OUTPUT_SHOW_ALL ? 0 : JSON_THRESHOLD);
r = sd_journal_get_cursor(j, &cursor);
if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) {
log_debug_errno(r, "Unable to determine cursor of entry, assuming bad or partially written entry: %m");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to get cursor: %m");
r = sd_journal_get_realtime_usec(j, &realtime);
if (r == -EBADMSG) {
log_debug_errno(r, "Unable to read realtime timestamp of entry, assuming bad or partially written entry: %m");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to get realtime timestamp: %m");
r = sd_journal_get_monotonic_usec(j, &monotonic, &journal_boot_id);
if (r == -EBADMSG) {
log_debug_errno(r, "Unable to read monotonic timestamp of entry, assuming bad or partially written entry: %m");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to get monotonic timestamp: %m");
r = sd_journal_get_seqnum(j, &seqnum, &seqnum_id);
if (r == -EBADMSG) {
log_debug_errno(r, "Unable to read sequence number of entry, assuming bad or partially written entry: %m");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to get seqnum: %m");
@@ -1523,6 +1569,10 @@ int show_journal(
if (need_seek) {
r = sd_journal_next(j);
if (r == -EBADMSG) {
log_debug_errno(r, "Bad or partially written entry, leaving.");
break;
}
if (r < 0)
return log_error_errno(r, "Failed to iterate through journal: %m");
}