]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
exec-util: enumerate executables earlier
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 17 Mar 2023 03:59:40 +0000 (12:59 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 17 Mar 2023 11:38:15 +0000 (20:38 +0900)
Then, return earlier if no executable found.

src/shared/exec-util.c

index 1562911ec66359037ff571e36f88c9f2994e8a2b..e44323e0e900478fe035f2586ac3474fe8fffeeb 100644 (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;
+
+        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);
         }