From: Yu Watanabe Date: Fri, 17 Mar 2023 03:59:40 +0000 (+0900) Subject: exec-util: enumerate executables earlier X-Git-Tag: v254-rc1~990^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=753e38d9849a13ed58970161d89dc4a5834d835d;p=thirdparty%2Fsystemd.git exec-util: enumerate executables earlier Then, return earlier if no executable found. --- diff --git a/src/shared/exec-util.c b/src/shared/exec-util.c index 1562911ec66..e44323e0e90 100644 --- a/src/shared/exec-util.c +++ b/src/shared/exec-util.c @@ -77,7 +77,7 @@ static int do_spawn(const char *path, char *argv[], int stdout_fd, pid_t *pid, b } static int do_execute( - const char* const* directories, + char* const* paths, usec_t timeout, gather_stdout_callback_t const callbacks[_STDOUT_CONSUME_MAX], void* const callback_args[_STDOUT_CONSUME_MAX], @@ -87,9 +87,8 @@ static int do_execute( ExecDirFlags flags) { _cleanup_hashmap_free_free_ Hashmap *pids = NULL; - _cleanup_strv_free_ char **paths = NULL; - int r; bool parallel_execution; + int r; /* We fork this all off from a child process so that we can somewhat cleanly make * use of SIGALRM to set a time limit. @@ -99,10 +98,6 @@ static int do_execute( */ parallel_execution = FLAGS_SET(flags, EXEC_DIR_PARALLEL) && !callbacks; - r = conf_files_list_strv(&paths, NULL, NULL, CONF_FILES_EXECUTABLE|CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, directories); - if (r < 0) - return log_error_errno(r, "Failed to enumerate executables: %m"); - if (parallel_execution) { pids = hashmap_new(NULL); if (!pids) @@ -202,11 +197,19 @@ int execute_directories( char *envp[], ExecDirFlags flags) { + _cleanup_strv_free_ char **paths = NULL; _cleanup_close_ int fd = -EBADF; - int r; pid_t executor_pid; + int r; + + r = conf_files_list_strv(&paths, NULL, NULL, CONF_FILES_EXECUTABLE|CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, directories); + if (r < 0) + return log_error_errno(r, "Failed to enumerate executables: %m"); - assert(!strv_isempty((char**) directories)); + if (strv_isempty(paths)) { + log_debug("No executables found."); + return 0; + } if (callbacks) { assert(callback_args); @@ -233,7 +236,7 @@ int execute_directories( if (r < 0) return r; if (r == 0) { - r = do_execute(directories, timeout, callbacks, callback_args, fd, argv, envp, flags); + r = do_execute(paths, timeout, callbacks, callback_args, fd, argv, envp, flags); _exit(r < 0 ? EXIT_FAILURE : r); }