From: Yu Watanabe Date: Wed, 11 Dec 2024 00:04:06 +0000 (+0900) Subject: journalctl: move get_possible_units() to journalctl-util.c X-Git-Tag: v258-rc1~1906^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=48b22321af9ed0336716dbb4d4d095e18d7eebec;p=thirdparty%2Fsystemd.git journalctl: move get_possible_units() to journalctl-util.c No functional change. Preparation for the next commit. --- diff --git a/src/journal/journalctl-filter.c b/src/journal/journalctl-filter.c index 1c6348574c5..b171cf1f903 100644 --- a/src/journal/journalctl-filter.c +++ b/src/journal/journalctl-filter.c @@ -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; diff --git a/src/journal/journalctl-util.c b/src/journal/journalctl-util.c index 58257abc323..82ddb64f2b3 100644 --- a/src/journal/journalctl-util.c +++ b/src/journal/journalctl-util.c @@ -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; diff --git a/src/journal/journalctl-util.h b/src/journal/journalctl-util.h index 14e3d569a64..49a4d3e08b0 100644 --- a/src/journal/journalctl-util.h +++ b/src/journal/journalctl-util.h @@ -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);