From: Mike Yuan Date: Fri, 11 Oct 2024 15:57:06 +0000 (+0200) Subject: core/manager: minor cleanup for generator_path_any() and friends X-Git-Tag: v257-rc1~227^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=de41622bf39cfa01c8d79e2898b15e8a9bbc89ab;p=thirdparty%2Fsystemd.git core/manager: minor cleanup for generator_path_any() and friends --- diff --git a/src/core/manager.c b/src/core/manager.c index 456ad46135b..373f4d66e7b 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -4007,30 +4007,26 @@ void manager_send_reloading(Manager *m) { m->ready_sent = false; } -static bool generator_path_any(const char* const* paths) { - bool found = false; +static bool generator_path_any(char * const *paths) { - /* Optimize by skipping the whole process by not creating output directories - * if no generators are found. */ - STRV_FOREACH(path, paths) - if (access(*path, F_OK) == 0) - found = true; - else if (errno != ENOENT) - log_warning_errno(errno, "Failed to open generator directory %s: %m", *path); + /* Optimize by skipping the whole process by not creating output directories if no generators are found. */ - return found; + STRV_FOREACH(i, paths) { + if (access(*i, F_OK) >= 0) + return true; + if (errno != ENOENT) + log_warning_errno(errno, "Failed to check if generator dir '%s' exists, assuming not: %m", *i); + } + + return false; } static int manager_run_environment_generators(Manager *m) { - char **tmp = NULL; /* this is only used in the forked process, no cleanup here */ _cleanup_strv_free_ char **paths = NULL; - void* args[] = { - [STDOUT_GENERATE] = &tmp, - [STDOUT_COLLECT] = &tmp, - [STDOUT_CONSUME] = &m->transient_environment, - }; int r; + assert(m); + if (MANAGER_IS_TEST_RUN(m) && !(m->test_run_flags & MANAGER_TEST_RUN_ENV_GENERATORS)) return 0; @@ -4038,9 +4034,16 @@ static int manager_run_environment_generators(Manager *m) { if (!paths) return log_oom(); - if (!generator_path_any((const char* const*) paths)) + if (!generator_path_any(paths)) return 0; + char **tmp = NULL; /* this is only used in the forked process, no cleanup here */ + void *args[_STDOUT_CONSUME_MAX] = { + [STDOUT_GENERATE] = &tmp, + [STDOUT_COLLECT] = &tmp, + [STDOUT_CONSUME] = &m->transient_environment, + }; + WITH_UMASK(0022) r = execute_directories((const char* const*) paths, DEFAULT_TIMEOUT_USEC, gather_environment, args, NULL, m->transient_environment, @@ -4117,17 +4120,12 @@ static int build_generator_environment(Manager *m, char ***ret) { return 0; } -static int manager_execute_generators(Manager *m, char **paths, bool remount_ro) { +static int manager_execute_generators(Manager *m, char * const *paths, bool remount_ro) { _cleanup_strv_free_ char **ge = NULL; - const char *argv[] = { - NULL, /* Leave this empty, execute_directory() will fill something in */ - m->lookup_paths.generator, - m->lookup_paths.generator_early, - m->lookup_paths.generator_late, - NULL, - }; int r; + assert(m); + r = build_generator_environment(m, &ge); if (r < 0) return log_error_errno(r, "Failed to build generator environment: %m"); @@ -4144,6 +4142,14 @@ static int manager_execute_generators(Manager *m, char **paths, bool remount_ro) log_warning_errno(r, "Read-only bind remount failed, ignoring: %m"); } + const char *argv[] = { + NULL, /* Leave this empty, execute_directory() will fill something in */ + m->lookup_paths.generator, + m->lookup_paths.generator_early, + m->lookup_paths.generator_late, + NULL, + }; + BLOCK_WITH_UMASK(0022); return execute_directories( (const char* const*) paths, @@ -4168,7 +4174,7 @@ static int manager_run_generators(Manager *m) { if (!paths) return log_oom(); - if (!generator_path_any((const char* const*) paths)) + if (!generator_path_any(paths)) return 0; r = lookup_paths_mkdir_generator(&m->lookup_paths);