1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

journalctl: move get_possible_units() to journalctl-util.c

No functional change. Preparation for the next commit.
This commit is contained in:
Yu Watanabe 2024-12-11 09:04:06 +09:00 committed by Luca Boccassi
parent e8823b5e35
commit 48b22321af
3 changed files with 77 additions and 70 deletions

View 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;

View 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;

View 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);