From 6636145b3e70cb9d7a4347bd364b24a45f85a4c7 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 24 Nov 2023 04:44:33 +0900 Subject: [PATCH 1/4] analyze: drop duplicated : Before, this returns e.g. "/current_dir::", After, this returns e.g. "/current_dir:". --- src/analyze/analyze-verify-util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/analyze/analyze-verify-util.c b/src/analyze/analyze-verify-util.c index 622a10f6b07..cb3d96b6610 100644 --- a/src/analyze/analyze-verify-util.c +++ b/src/analyze/analyze-verify-util.c @@ -104,7 +104,7 @@ int verify_generate_path(char **ret, char **filenames) { old = getenv("SYSTEMD_UNIT_PATH"); if (!streq_ptr(old, "")) { if (!old) - old = ":"; + old = ""; r = strv_extend(&ans, old); if (r < 0) From c131e8b706d43f9ebd895dee3a8731a8664af90e Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 24 Nov 2023 05:19:10 +0900 Subject: [PATCH 2/4] analyze: shorten code a bit No functional change, just refactoring. --- src/analyze/analyze-verify-util.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/analyze/analyze-verify-util.c b/src/analyze/analyze-verify-util.c index cb3d96b6610..54442d06a9e 100644 --- a/src/analyze/analyze-verify-util.c +++ b/src/analyze/analyze-verify-util.c @@ -95,24 +95,16 @@ int verify_generate_path(char **ret, char **filenames) { return r; } - strv_uniq(ans); + joined = strv_join(strv_uniq(ans), ":"); + if (!joined) + return -ENOMEM; /* First, prepend our directories. Second, if some path was specified, use that, and * otherwise use the defaults. Any duplicates will be filtered out in path-lookup.c. - * Treat explicit empty path to mean that nothing should be appended. - */ + * Treat explicit empty path to mean that nothing should be appended. */ old = getenv("SYSTEMD_UNIT_PATH"); - if (!streq_ptr(old, "")) { - if (!old) - old = ""; - - r = strv_extend(&ans, old); - if (r < 0) - return r; - } - - joined = strv_join(ans, ":"); - if (!joined) + if (!streq_ptr(old, "") && + !strextend_with_separator(&joined, ":", old ?: "")) return -ENOMEM; *ret = TAKE_PTR(joined); From ff7af46edb51c6548876e139c89389a388694051 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 24 Nov 2023 05:20:07 +0900 Subject: [PATCH 3/4] analyze: set SYSTEMD_UNIT_PATH in verify_generate_path() Then, rename verify_generate_path() -> verify_set_unit_path(). --- src/analyze/analyze-condition.c | 13 ++----------- src/analyze/analyze-security.c | 8 ++------ src/analyze/analyze-verify-util.c | 11 ++++------- src/analyze/analyze-verify-util.h | 2 +- 4 files changed, 9 insertions(+), 25 deletions(-) diff --git a/src/analyze/analyze-condition.c b/src/analyze/analyze-condition.c index 3b290e03230..1e9136d7c07 100644 --- a/src/analyze/analyze-condition.c +++ b/src/analyze/analyze-condition.c @@ -78,18 +78,9 @@ static int verify_conditions(char **lines, RuntimeScope scope, const char *unit, int r, q = 1; if (unit) { - _cleanup_strv_free_ char **filenames = NULL; - _cleanup_free_ char *var = NULL; - - filenames = strv_new(unit); - if (!filenames) - return log_oom(); - - r = verify_generate_path(&var, filenames); + r = verify_set_unit_path(STRV_MAKE(unit)); if (r < 0) - return log_error_errno(r, "Failed to generate unit load path: %m"); - - assert_se(set_unit_path(var) >= 0); + return log_error_errno(r, "Failed to set unit load path: %m"); } r = manager_new(scope, MANAGER_TEST_RUN_MINIMAL|MANAGER_TEST_DONT_OPEN_EXECUTOR, &m); diff --git a/src/analyze/analyze-security.c b/src/analyze/analyze-security.c index b119ad73513..5f1b5e69705 100644 --- a/src/analyze/analyze-security.c +++ b/src/analyze/analyze-security.c @@ -2695,19 +2695,15 @@ static int offline_security_checks( _cleanup_(manager_freep) Manager *m = NULL; Unit *units[strv_length(filenames)]; - _cleanup_free_ char *var = NULL; int r, k; size_t count = 0; if (strv_isempty(filenames)) return 0; - /* set the path */ - r = verify_generate_path(&var, filenames); + r = verify_set_unit_path(filenames); if (r < 0) - return log_error_errno(r, "Failed to generate unit load path: %m"); - - assert_se(set_unit_path(var) >= 0); + return log_error_errno(r, "Failed to set unit load path: %m"); r = manager_new(scope, flags, &m); if (r < 0) diff --git a/src/analyze/analyze-verify-util.c b/src/analyze/analyze-verify-util.c index 54442d06a9e..cfafbd4d44d 100644 --- a/src/analyze/analyze-verify-util.c +++ b/src/analyze/analyze-verify-util.c @@ -72,7 +72,7 @@ int verify_prepare_filename(const char *filename, char **ret) { return 0; } -int verify_generate_path(char **ret, char **filenames) { +int verify_set_unit_path(char **filenames) { _cleanup_strv_free_ char **ans = NULL; _cleanup_free_ char *joined = NULL; const char *old; @@ -107,7 +107,7 @@ int verify_generate_path(char **ret, char **filenames) { !strextend_with_separator(&joined, ":", old ?: "")) return -ENOMEM; - *ret = TAKE_PTR(joined); + assert_se(set_unit_path(joined) >= 0); return 0; } @@ -242,7 +242,6 @@ int verify_units( _cleanup_(set_destroy_ignore_pointer_max) Set *s = NULL; _unused_ _cleanup_(clear_log_syntax_callback) dummy_t dummy; Unit *units[strv_length(filenames)]; - _cleanup_free_ char *var = NULL; int r, k, count = 0; if (strv_isempty(filenames)) @@ -254,11 +253,9 @@ int verify_units( set_log_syntax_callback(log_syntax_callback, &s); /* set the path */ - r = verify_generate_path(&var, filenames); + r = verify_set_unit_path(filenames); if (r < 0) - return log_error_errno(r, "Failed to generate unit load path: %m"); - - assert_se(set_unit_path(var) >= 0); + return log_error_errno(r, "Failed to set unit load path: %m"); r = manager_new(scope, flags, &m); if (r < 0) diff --git a/src/analyze/analyze-verify-util.h b/src/analyze/analyze-verify-util.h index 2e3b0604c31..0834c59c576 100644 --- a/src/analyze/analyze-verify-util.h +++ b/src/analyze/analyze-verify-util.h @@ -14,7 +14,7 @@ typedef enum RecursiveErrors { _RECURSIVE_ERRORS_INVALID = -EINVAL, } RecursiveErrors; -int verify_generate_path(char **var, char **filenames); +int verify_set_unit_path(char **filenames); int verify_prepare_filename(const char *filename, char **ret); int verify_executable(Unit *u, const ExecCommand *exec, const char *root); int verify_units(char **filenames, RuntimeScope scope, bool check_man, bool run_generators, RecursiveErrors recursive_errors, const char *root); From 2f6181ad4d6c126e3ebf6880ba30b3b0059c6fc8 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 24 Nov 2023 05:22:33 +0900 Subject: [PATCH 4/4] analyze: do not prepend the current working directory to SYSTEMD_UNIT_PATH needlessly If the requested unit is not in the current working directory, it is not necessary to include the current working directory in SYSTEMD_UNIT_PATH. Follow-up for 45519d13a4f2a3c3585e672595762ca621abe65e. Fixes #30166. --- src/analyze/analyze-verify-util.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/analyze/analyze-verify-util.c b/src/analyze/analyze-verify-util.c index cfafbd4d44d..f3ce0ad1f17 100644 --- a/src/analyze/analyze-verify-util.c +++ b/src/analyze/analyze-verify-util.c @@ -86,6 +86,9 @@ int verify_set_unit_path(char **filenames) { if (r < 0) return r; + if (access(a, F_OK) < 0) + continue; + r = path_extract_directory(a, &t); if (r < 0) return r; @@ -99,6 +102,9 @@ int verify_set_unit_path(char **filenames) { if (!joined) return -ENOMEM; + if (isempty(joined)) + return 0; + /* First, prepend our directories. Second, if some path was specified, use that, and * otherwise use the defaults. Any duplicates will be filtered out in path-lookup.c. * Treat explicit empty path to mean that nothing should be appended. */