From edd2b336593cb1d3cbb38f1e3a047598a959c554 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 11 Apr 2024 19:03:54 +0200 Subject: [PATCH 1/3] journalctl: add missing --help line for --list-namespaces Follow-up for: #32236 68f66a171398e27280a95e58ae7464219cccaaec --- src/journal/journalctl.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 0008e8a755..cc59869790 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -265,6 +265,7 @@ static int help(void) { " -N --fields List all field names currently used\n" " -F --field=FIELD List all values that a specified field takes\n" " --list-boots Show terse information about recorded boots\n" + " --list-namespaces Show list of journal namespaces\n" " --disk-usage Show total disk usage of all journal files\n" " --vacuum-size=BYTES Reduce disk usage below specified size\n" " --vacuum-files=INT Leave only the specified number of journal files\n" From e1005cae75e146264411865d43ef4bbbc5015280 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 11 Apr 2024 19:04:14 +0200 Subject: [PATCH 2/3] journalctl: show human readable text if no namespace is found The common case will be that no namespaces are defined, hence by default we'd just output "NAMESPACE" and exit, which is very confusing. Generate a nice message instead, how many of our tools do it. Suppress it on --quiet however. --- src/journal/journalctl-misc.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/journal/journalctl-misc.c b/src/journal/journalctl-misc.c index c7a49eefea..9d878e8e05 100644 --- a/src/journal/journalctl-misc.c +++ b/src/journal/journalctl-misc.c @@ -261,9 +261,14 @@ int action_list_namespaces(void) { } } - r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, !arg_quiet); - if (r < 0) - return table_log_print_error(r); + if (table_isempty(table) && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { + if (!arg_quiet) + log_notice("No namespaces found."); + } else { + r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, !arg_quiet); + if (r < 0) + return r; + } return 0; } From 2807a8f93ca0701a19e8dc3bc5e09a2ab50dff5c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 11 Apr 2024 19:07:21 +0200 Subject: [PATCH 3/3] journalctl: tighten rules on parsing namespace journal dir suffixes The dot must follow the machine ID immediately, let's check for that. Also, I think it's generally better to parse the machine ID and then comparing it, instead of comparing the string representation. That's more in line how we usually do it, as we parse 128bit IDs generally case-insensitively. --- src/journal/journalctl-misc.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/journal/journalctl-misc.c b/src/journal/journalctl-misc.c index 9d878e8e05..47eb72575a 100644 --- a/src/journal/journalctl-misc.c +++ b/src/journal/journalctl-misc.c @@ -211,7 +211,6 @@ int action_list_field_names(void) { int action_list_namespaces(void) { _cleanup_(table_unrefp) Table *table = NULL; sd_id128_t machine; - char machine_id[SD_ID128_STRING_MAX]; int r; assert(arg_action == ACTION_LIST_NAMESPACES); @@ -220,8 +219,6 @@ int action_list_namespaces(void) { if (r < 0) return log_error_errno(r, "Failed to get machine ID: %m"); - sd_id128_to_string(machine, machine_id); - table = table_new("namespace"); if (!table) return log_oom(); @@ -243,19 +240,29 @@ int action_list_namespaces(void) { } FOREACH_DIRENT(de, dirp, return log_error_errno(errno, "Failed to iterate through %s: %m", path)) { - char *dot; - if (!startswith(de->d_name, machine_id)) + const char *e = strchr(de->d_name, '.'); + if (!e) continue; - dot = strchr(de->d_name, '.'); - if (!dot) + _cleanup_free_ char *ids = strndup(de->d_name, e - de->d_name); + if (!ids) + return log_oom(); + + sd_id128_t id; + r = sd_id128_from_string(ids, &id); + if (r < 0) continue; - if (!log_namespace_name_valid(dot + 1)) + if (!sd_id128_equal(machine, id)) continue; - r = table_add_cell(table, NULL, TABLE_STRING, dot + 1); + e++; + + if (!log_namespace_name_valid(e)) + continue; + + r = table_add_cell(table, NULL, TABLE_STRING, e); if (r < 0) return table_log_add_error(r); }