]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/journal/journalctl-misc.c
journalctl: tighten rules on parsing namespace journal dir suffixes
[thirdparty/systemd.git] / src / journal / journalctl-misc.c
index c7a49eefea2a35e22a61c01ac10dd9bb64ce4675..47eb72575ab92e86d9bbeab1a174d8a848535f82 100644 (file)
@@ -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,27 +240,42 @@ 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;
+
+                        _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;
 
-                        dot = strchr(de->d_name, '.');
-                        if (!dot)
+                        if (!sd_id128_equal(machine, id))
                                 continue;
 
-                        if (!log_namespace_name_valid(dot + 1))
+                        e++;
+
+                        if (!log_namespace_name_valid(e))
                                 continue;
 
-                        r = table_add_cell(table, NULL, TABLE_STRING, dot + 1);
+                        r = table_add_cell(table, NULL, TABLE_STRING, e);
                         if (r < 0)
                                 return table_log_add_error(r);
                 }
         }
 
-        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;
 }