1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-05 13:18:06 +03:00

Merge pull request #34022 from YHNdnzj/unit-is-filtered

core/unit: two trivial cleanups
This commit is contained in:
Yu Watanabe 2024-08-19 04:29:54 +09:00 committed by GitHub
commit dc64f66756
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 28 additions and 27 deletions

View File

@ -156,7 +156,7 @@ int verify_set_unit_path(char **filenames) {
!strextend_with_separator(&joined, ":", strempty(old)))
return -ENOMEM;
assert_se(set_unit_path(joined) >= 0);
assert_se(setenv_unit_path(joined) >= 0);
return 0;
}

View File

@ -235,7 +235,6 @@ bool strv_fnmatch_full(char* const* patterns, const char *s, int flags, size_t *
static inline bool strv_fnmatch(char* const* patterns, const char *s) {
return strv_fnmatch_full(patterns, s, 0, NULL);
}
static inline bool strv_fnmatch_or_empty(char* const* patterns, const char *s, int flags) {
assert(s);
return strv_isempty(patterns) ||

View File

@ -1218,7 +1218,7 @@ static int list_units_filtered(sd_bus_message *message, void *userdata, sd_bus_e
if (k != u->id)
continue;
if (unit_is_filtered(u, states, patterns))
if (!unit_passes_filter(u, states, patterns))
continue;
r = reply_unit_info(reply, u);

View File

@ -3317,9 +3317,11 @@ int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency
return unit_add_two_dependencies(u, d, e, other, add_reference, mask);
}
int set_unit_path(const char *p) {
int setenv_unit_path(const char *p) {
assert(p);
/* This is mostly for debug purposes */
return RET_NERRNO(setenv("SYSTEMD_UNIT_PATH", p, 1));
return RET_NERRNO(setenv("SYSTEMD_UNIT_PATH", p, /* overwrite = */ true));
}
char* unit_dbus_path(Unit *u) {
@ -6470,20 +6472,20 @@ int unit_arm_timer(
return 0;
}
bool unit_is_filtered(Unit *u, char *const *states, char *const *patterns) {
bool unit_passes_filter(Unit *u, char * const *states, char * const *patterns) {
assert(u);
if (!strv_isempty(states)) {
char *const *unit_states = STRV_MAKE(
char * const *unit_states = STRV_MAKE(
unit_load_state_to_string(u->load_state),
unit_active_state_to_string(unit_active_state(u)),
unit_sub_state_to_string(u));
if (!strv_overlap(states, unit_states))
return true;
return false;
}
return !strv_isempty(patterns) && !strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE);
return strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE);
}
static int unit_get_nice(Unit *u) {

View File

@ -880,7 +880,7 @@ void unit_unwatch_bus_name(Unit *u, const char *name);
bool unit_job_is_applicable(Unit *u, JobType j);
int set_unit_path(const char *p);
int setenv_unit_path(const char *p);
char* unit_dbus_path(Unit *u);
char* unit_dbus_path_invocation_id(Unit *u);
@ -1040,7 +1040,7 @@ Condition *unit_find_failed_condition(Unit *u);
int unit_arm_timer(Unit *u, sd_event_source **source, bool relative, usec_t usec, sd_event_time_handler_t handler);
bool unit_is_filtered(Unit *u, char *const *states, char *const *patterns);
bool unit_passes_filter(Unit *u, char * const *states, char * const *patterns);
int unit_compare_priority(Unit *a, Unit *b);

View File

@ -56,7 +56,7 @@ int main(int argc, char *argv[]) {
_cleanup_free_ char *unit_dir = NULL;
ASSERT_OK(get_testdata_dir("units", &unit_dir));
ASSERT_OK(set_unit_path(unit_dir));
ASSERT_OK(setenv_unit_path(unit_dir));
assert_se(runtime_dir = setup_fake_runtime_dir());
r = bpf_program_new(BPF_PROG_TYPE_CGROUP_SKB, "sd_trivial", &p);

View File

@ -298,7 +298,7 @@ int main(int argc, char *argv[]) {
return log_tests_skipped("cgroupfs not available");
ASSERT_OK(get_testdata_dir("units", &unit_dir));
ASSERT_OK(set_unit_path(unit_dir));
ASSERT_OK(setenv_unit_path(unit_dir));
assert_se(runtime_dir = setup_fake_runtime_dir());
ASSERT_OK(manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &m));

View File

@ -83,7 +83,7 @@ int main(int argc, char *argv[]) {
return log_tests_skipped("cgroupfs not available");
ASSERT_OK(get_testdata_dir("units", &unit_dir));
ASSERT_OK(set_unit_path(unit_dir));
ASSERT_OK(setenv_unit_path(unit_dir));
assert_se(runtime_dir = setup_fake_runtime_dir());
ASSERT_OK(manager_new(RUNTIME_SCOPE_SYSTEM, MANAGER_TEST_RUN_BASIC, &m));

View File

@ -40,7 +40,7 @@ TEST_RET(cgroup_mask, .sd_booted = true) {
/* Prepare the manager. */
_cleanup_free_ char *unit_dir = NULL;
ASSERT_OK(get_testdata_dir("units", &unit_dir));
ASSERT_OK(set_unit_path(unit_dir));
ASSERT_OK(setenv_unit_path(unit_dir));
assert_se(runtime_dir = setup_fake_runtime_dir());
r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &m);
if (IN_SET(r, -EPERM, -EACCES)) {

View File

@ -24,7 +24,7 @@ TEST_RET(default_memory_low, .sd_booted = true) {
_cleanup_free_ char *unit_dir = NULL;
ASSERT_OK(get_testdata_dir("units", &unit_dir));
ASSERT_OK(set_unit_path(unit_dir));
ASSERT_OK(setenv_unit_path(unit_dir));
assert_se(runtime_dir = setup_fake_runtime_dir());
r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &m);
if (IN_SET(r, -EPERM, -EACCES)) {

View File

@ -89,8 +89,8 @@ int main(int argc, char *argv[]) {
/* prepare the test */
_cleanup_free_ char *unit_dir = NULL;
assert_se(get_testdata_dir("units", &unit_dir) >= 0);
assert_se(set_unit_path(unit_dir) >= 0);
ASSERT_OK(get_testdata_dir("units", &unit_dir));
ASSERT_OK(setenv_unit_path(unit_dir));
assert_se(runtime_dir = setup_fake_runtime_dir());
r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &m);

View File

@ -1397,7 +1397,7 @@ static void run_tests(RuntimeScope scope, char **patterns) {
ASSERT_NOT_NULL(runtime_dir = setup_fake_runtime_dir());
ASSERT_NOT_NULL(user_runtime_unit_dir = path_join(runtime_dir, "systemd/user"));
ASSERT_NOT_NULL(unit_paths = strjoin(PRIVATE_UNIT_DIR, ":", user_runtime_unit_dir));
ASSERT_OK(set_unit_path(unit_paths));
ASSERT_OK(setenv_unit_path(unit_paths));
r = manager_new(scope, MANAGER_TEST_RUN_BASIC, &m);
if (manager_errno_skip_test(r))

View File

@ -397,8 +397,8 @@ int main(int argc, char *argv[]) {
test_setup_logging(LOG_INFO);
assert_se(get_testdata_dir("test-path", &test_path) >= 0);
assert_se(set_unit_path(test_path) >= 0);
ASSERT_OK(get_testdata_dir("test-path", &test_path));
ASSERT_OK(setenv_unit_path(test_path));
assert_se(runtime_dir = setup_fake_runtime_dir());
for (const test_function_t *test = tests; *test; test++) {

View File

@ -26,8 +26,8 @@ int main(int argc, char *argv[]) {
/* prepare the test */
_cleanup_free_ char *unit_dir = NULL;
assert_se(get_testdata_dir("units", &unit_dir) >= 0);
assert_se(set_unit_path(unit_dir) >= 0);
ASSERT_OK(get_testdata_dir("units", &unit_dir));
ASSERT_OK(setenv_unit_path(unit_dir));
assert_se(runtime_dir = setup_fake_runtime_dir());
r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &m);

View File

@ -126,8 +126,8 @@ int main(int argc, char *argv[]) {
if (r == -ENOMEDIUM)
return log_tests_skipped("cgroupfs not available");
assert_se(get_testdata_dir("units", &unit_dir) >= 0);
assert_se(set_unit_path(unit_dir) >= 0);
ASSERT_OK(get_testdata_dir("units", &unit_dir));
ASSERT_OK(setenv_unit_path(unit_dir));
assert_se(runtime_dir = setup_fake_runtime_dir());
assert_se(manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &m) >= 0);

View File

@ -22,8 +22,8 @@ int main(int argc, char *argv[]) {
return log_tests_skipped("cgroupfs not available");
_cleanup_free_ char *unit_dir = NULL;
assert_se(get_testdata_dir("units/", &unit_dir) >= 0);
assert_se(set_unit_path(unit_dir) >= 0);
ASSERT_OK(get_testdata_dir("units/", &unit_dir));
ASSERT_OK(setenv_unit_path(unit_dir));
assert_se(runtime_dir = setup_fake_runtime_dir());