exec-util: enumerate executables earlier

Then, return earlier if no executable found.
This commit is contained in:
Yu Watanabe
2023-03-17 20:38:15 +09:00
parent fb0e5f12c6
commit 753e38d984
+13 -10
View File
@@ -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;
assert(!strv_isempty((char**) directories));
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 (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);
}