mirror of
https://github.com/systemd/systemd.git
synced 2025-05-30 05:05:57 +03:00
journalctl-filter: modernize add_units()
- add missing assertions, - use set_ensure_consume() and strv_consume(), - use string_hash_ops_free and _cleanup_set_free_, - use strv_fnmatch_full(), - replace unused counter with a boolean flag, - return earlier if no unit filtering is requested.
This commit is contained in:
parent
17c512f1cc
commit
5d53eef97b
@ -85,14 +85,14 @@ static int get_possible_units(
|
|||||||
sd_journal *j,
|
sd_journal *j,
|
||||||
const char *fields,
|
const char *fields,
|
||||||
char **patterns,
|
char **patterns,
|
||||||
Set **units) {
|
Set **ret) {
|
||||||
|
|
||||||
_cleanup_set_free_free_ Set *found = NULL;
|
_cleanup_set_free_ Set *found = NULL;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
found = set_new(&string_hash_ops);
|
assert(j);
|
||||||
if (!found)
|
assert(fields);
|
||||||
return -ENOMEM;
|
assert(ret);
|
||||||
|
|
||||||
NULSTR_FOREACH(field, fields) {
|
NULSTR_FOREACH(field, fields) {
|
||||||
const void *data;
|
const void *data;
|
||||||
@ -103,36 +103,31 @@ static int get_possible_units(
|
|||||||
return r;
|
return r;
|
||||||
|
|
||||||
SD_JOURNAL_FOREACH_UNIQUE(j, data, size) {
|
SD_JOURNAL_FOREACH_UNIQUE(j, data, size) {
|
||||||
char *eq;
|
|
||||||
size_t prefix;
|
|
||||||
_cleanup_free_ char *u = NULL;
|
_cleanup_free_ char *u = NULL;
|
||||||
|
char *eq;
|
||||||
|
|
||||||
eq = memchr(data, '=', size);
|
eq = memchr(data, '=', size);
|
||||||
if (eq)
|
if (eq) {
|
||||||
prefix = eq - (char*) data + 1;
|
size -= eq - (char*) data + 1;
|
||||||
else
|
data = ++eq;
|
||||||
prefix = 0;
|
}
|
||||||
|
|
||||||
u = strndup((char*) data + prefix, size - prefix);
|
u = strndup(data, size);
|
||||||
if (!u)
|
if (!u)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
STRV_FOREACH(pattern, patterns)
|
size_t i;
|
||||||
if (fnmatch(*pattern, u, FNM_NOESCAPE) == 0) {
|
if (!strv_fnmatch_full(patterns, u, FNM_NOESCAPE, &i))
|
||||||
log_debug("Matched %s with pattern %s=%s", u, field, *pattern);
|
continue;
|
||||||
|
|
||||||
r = set_consume(found, u);
|
log_debug("Matched %s with pattern %s=%s", u, field, patterns[i]);
|
||||||
u = NULL;
|
r = set_ensure_consume(&found, &string_hash_ops_free, TAKE_PTR(u));
|
||||||
if (r < 0 && r != -EEXIST)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*units = TAKE_PTR(found);
|
*ret = TAKE_PTR(found);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,10 +150,14 @@ static int get_possible_units(
|
|||||||
|
|
||||||
static int add_units(sd_journal *j) {
|
static int add_units(sd_journal *j) {
|
||||||
_cleanup_strv_free_ char **patterns = NULL;
|
_cleanup_strv_free_ char **patterns = NULL;
|
||||||
int r, count = 0;
|
bool added = false;
|
||||||
|
int r;
|
||||||
|
|
||||||
assert(j);
|
assert(j);
|
||||||
|
|
||||||
|
if (strv_isempty(arg_system_units) && strv_isempty(arg_user_units))
|
||||||
|
return 0;
|
||||||
|
|
||||||
STRV_FOREACH(i, arg_system_units) {
|
STRV_FOREACH(i, arg_system_units) {
|
||||||
_cleanup_free_ char *u = NULL;
|
_cleanup_free_ char *u = NULL;
|
||||||
|
|
||||||
@ -167,10 +166,9 @@ static int add_units(sd_journal *j) {
|
|||||||
return r;
|
return r;
|
||||||
|
|
||||||
if (string_is_glob(u)) {
|
if (string_is_glob(u)) {
|
||||||
r = strv_push(&patterns, u);
|
r = strv_consume(&patterns, TAKE_PTR(u));
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
u = NULL;
|
|
||||||
} else {
|
} else {
|
||||||
r = add_matches_for_unit(j, u);
|
r = add_matches_for_unit(j, u);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
@ -178,12 +176,12 @@ static int add_units(sd_journal *j) {
|
|||||||
r = sd_journal_add_disjunction(j);
|
r = sd_journal_add_disjunction(j);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
count++;
|
added = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!strv_isempty(patterns)) {
|
if (!strv_isempty(patterns)) {
|
||||||
_cleanup_set_free_free_ Set *units = NULL;
|
_cleanup_set_free_ Set *units = NULL;
|
||||||
char *u;
|
char *u;
|
||||||
|
|
||||||
r = get_possible_units(j, SYSTEM_UNITS, patterns, &units);
|
r = get_possible_units(j, SYSTEM_UNITS, patterns, &units);
|
||||||
@ -197,7 +195,7 @@ static int add_units(sd_journal *j) {
|
|||||||
r = sd_journal_add_disjunction(j);
|
r = sd_journal_add_disjunction(j);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
count++;
|
added = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,10 +209,9 @@ static int add_units(sd_journal *j) {
|
|||||||
return r;
|
return r;
|
||||||
|
|
||||||
if (string_is_glob(u)) {
|
if (string_is_glob(u)) {
|
||||||
r = strv_push(&patterns, u);
|
r = strv_consume(&patterns, TAKE_PTR(u));
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
u = NULL;
|
|
||||||
} else {
|
} else {
|
||||||
r = add_matches_for_user_unit(j, u, getuid());
|
r = add_matches_for_user_unit(j, u, getuid());
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
@ -222,12 +219,12 @@ static int add_units(sd_journal *j) {
|
|||||||
r = sd_journal_add_disjunction(j);
|
r = sd_journal_add_disjunction(j);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
count++;
|
added = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!strv_isempty(patterns)) {
|
if (!strv_isempty(patterns)) {
|
||||||
_cleanup_set_free_free_ Set *units = NULL;
|
_cleanup_set_free_ Set *units = NULL;
|
||||||
char *u;
|
char *u;
|
||||||
|
|
||||||
r = get_possible_units(j, USER_UNITS, patterns, &units);
|
r = get_possible_units(j, USER_UNITS, patterns, &units);
|
||||||
@ -241,20 +238,16 @@ static int add_units(sd_journal *j) {
|
|||||||
r = sd_journal_add_disjunction(j);
|
r = sd_journal_add_disjunction(j);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
count++;
|
added = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Complain if the user request matches but nothing whatsoever was
|
/* Complain if the user request matches but nothing whatsoever was found, since otherwise everything
|
||||||
* found, since otherwise everything would be matched. */
|
* would be matched. */
|
||||||
if (!(strv_isempty(arg_system_units) && strv_isempty(arg_user_units)) && count == 0)
|
if (!added)
|
||||||
return -ENODATA;
|
return -ENODATA;
|
||||||
|
|
||||||
r = sd_journal_add_conjunction(j);
|
return sd_journal_add_conjunction(j);
|
||||||
if (r < 0)
|
|
||||||
return r;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int add_syslog_identifier(sd_journal *j) {
|
static int add_syslog_identifier(sd_journal *j) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user