]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: move get_possible_units() to journalctl-util.c
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 11 Dec 2024 00:04:06 +0000 (09:04 +0900)
committerLuca Boccassi <bluca@debian.org>
Wed, 11 Dec 2024 16:32:22 +0000 (16:32 +0000)
No functional change. Preparation for the next commit.

src/journal/journalctl-filter.c
src/journal/journalctl-util.c
src/journal/journalctl-util.h

index 1c6348574c5f0cfead6351c1903eb380c3dc3984..b171cf1f90388f25702d05935b7e8cacd3cccda1 100644 (file)
@@ -12,7 +12,6 @@
 #include "journalctl-util.h"
 #include "logs-show.h"
 #include "missing_sched.h"
-#include "nulstr-util.h"
 #include "path-util.h"
 #include "unit-name.h"
 
@@ -65,73 +64,6 @@ static int add_dmesg(sd_journal *j) {
         return sd_journal_add_conjunction(j);
 }
 
-static int get_possible_units(
-                sd_journal *j,
-                const char *fields,
-                char **patterns,
-                Set **ret) {
-
-        _cleanup_set_free_ Set *found = NULL;
-        int r;
-
-        assert(j);
-        assert(fields);
-        assert(ret);
-
-        NULSTR_FOREACH(field, fields) {
-                const void *data;
-                size_t size;
-
-                r = sd_journal_query_unique(j, field);
-                if (r < 0)
-                        return r;
-
-                SD_JOURNAL_FOREACH_UNIQUE(j, data, size) {
-                        _cleanup_free_ char *u = NULL;
-                        char *eq;
-
-                        eq = memchr(data, '=', size);
-                        if (eq) {
-                                size -= eq - (char*) data + 1;
-                                data = ++eq;
-                        }
-
-                        u = strndup(data, size);
-                        if (!u)
-                                return -ENOMEM;
-
-                        size_t i;
-                        if (!strv_fnmatch_full(patterns, u, FNM_NOESCAPE, &i))
-                                continue;
-
-                        log_debug("Matched %s with pattern %s=%s", u, field, patterns[i]);
-                        r = set_ensure_consume(&found, &string_hash_ops_free, TAKE_PTR(u));
-                        if (r < 0)
-                                return r;
-                }
-        }
-
-        *ret = TAKE_PTR(found);
-        return 0;
-}
-
-/* This list is supposed to return the superset of unit names
- * possibly matched by rules added with add_matches_for_unit... */
-#define SYSTEM_UNITS                 \
-        "_SYSTEMD_UNIT\0"            \
-        "COREDUMP_UNIT\0"            \
-        "UNIT\0"                     \
-        "OBJECT_SYSTEMD_UNIT\0"      \
-        "_SYSTEMD_SLICE\0"
-
-/* ... and add_matches_for_user_unit */
-#define USER_UNITS                   \
-        "_SYSTEMD_USER_UNIT\0"       \
-        "USER_UNIT\0"                \
-        "COREDUMP_USER_UNIT\0"       \
-        "OBJECT_SYSTEMD_USER_UNIT\0" \
-        "_SYSTEMD_USER_SLICE\0"
-
 static int add_units(sd_journal *j) {
         _cleanup_strv_free_ char **patterns = NULL;
         bool added = false;
@@ -175,7 +107,7 @@ static int add_units(sd_journal *j) {
                 _cleanup_set_free_ Set *units = NULL;
                 char *u;
 
-                r = get_possible_units(j, SYSTEM_UNITS, patterns, &units);
+                r = get_possible_units(j, SYSTEM_UNITS_FULL, patterns, &units);
                 if (r < 0)
                         return r;
 
@@ -218,7 +150,7 @@ static int add_units(sd_journal *j) {
                 _cleanup_set_free_ Set *units = NULL;
                 char *u;
 
-                r = get_possible_units(j, USER_UNITS, patterns, &units);
+                r = get_possible_units(j, USER_UNITS_FULL, patterns, &units);
                 if (r < 0)
                         return r;
 
index 58257abc32389da125d2e2a924b0fd0feb17d8e2..82ddb64f2b3fdf8bc190e7fa6f57a0149606bb90 100644 (file)
@@ -7,6 +7,7 @@
 #include "journalctl.h"
 #include "journalctl-util.h"
 #include "logs-show.h"
+#include "nulstr-util.h"
 #include "rlimit-util.h"
 #include "strv.h"
 #include "terminal-util.h"
@@ -112,6 +113,56 @@ int journal_acquire_boot(sd_journal *j) {
         return 1;
 }
 
+int get_possible_units(
+                sd_journal *j,
+                const char *fields,
+                char * const *patterns,
+                Set **ret) {
+
+        _cleanup_set_free_ Set *found = NULL;
+        int r;
+
+        assert(j);
+        assert(fields);
+        assert(ret);
+
+        NULSTR_FOREACH(field, fields) {
+                const void *data;
+                size_t size;
+
+                r = sd_journal_query_unique(j, field);
+                if (r < 0)
+                        return r;
+
+                SD_JOURNAL_FOREACH_UNIQUE(j, data, size) {
+                        _cleanup_free_ char *u = NULL;
+                        char *eq;
+
+                        eq = memchr(data, '=', size);
+                        if (eq) {
+                                size -= eq - (char*) data + 1;
+                                data = ++eq;
+                        }
+
+                        u = strndup(data, size);
+                        if (!u)
+                                return -ENOMEM;
+
+                        size_t i;
+                        if (!strv_fnmatch_full(patterns, u, FNM_NOESCAPE, &i))
+                                continue;
+
+                        log_debug("Matched %s with pattern %s=%s", u, field, patterns[i]);
+                        r = set_ensure_consume(&found, &string_hash_ops_free, TAKE_PTR(u));
+                        if (r < 0)
+                                return r;
+                }
+        }
+
+        *ret = TAKE_PTR(found);
+        return 0;
+}
+
 int acquire_unit(const char *option_name, const char **ret_unit, LogIdType *ret_type) {
         size_t n;
         int r;
index 14e3d569a6464c0d0f81c829407b166ffc4ba4a2..49a4d3e08b045548a5b58658bac5f0db7d6693f3 100644 (file)
@@ -4,11 +4,35 @@
 #include "sd-journal.h"
 
 #include "logs-show.h"
+#include "set.h"
 #include "time-util.h"
 
+/* The lists below are supposed to return the superset of unit names possibly matched by rules added with
+ * add_matches_for_unit() and add_matches_for_user_unit(). */
+#define SYSTEM_UNITS                 \
+        "_SYSTEMD_UNIT\0"            \
+        "UNIT\0"                     \
+        "OBJECT_SYSTEMD_UNIT\0"
+
+#define SYSTEM_UNITS_FULL            \
+        SYSTEM_UNITS                 \
+        "COREDUMP_UNIT\0"            \
+        "_SYSTEMD_SLICE\0"
+
+#define USER_UNITS                   \
+        "_SYSTEMD_USER_UNIT\0"       \
+        "USER_UNIT\0"                \
+        "OBJECT_SYSTEMD_USER_UNIT\0"
+
+#define USER_UNITS_FULL              \
+        USER_UNITS                   \
+        "COREDUMP_USER_UNIT\0"       \
+        "_SYSTEMD_USER_SLICE\0"
+
 char* format_timestamp_maybe_utc(char *buf, size_t l, usec_t t);
 int acquire_journal(sd_journal **ret);
 bool journal_boot_has_effect(sd_journal *j);
 int journal_acquire_boot(sd_journal *j);
+int get_possible_units(sd_journal *j, const char *fields, char * const *patterns, Set **ret);
 int acquire_unit(const char *option_name, const char **ret_unit, LogIdType *ret_type);
 int journal_acquire_invocation(sd_journal *j);