]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/exec-util.c
exec-util: extract the core logic of execute_directories() as execute_strv()
[thirdparty/systemd.git] / src / shared / exec-util.c
index e44323e0e900478fe035f2586ac3474fe8fffeeb..29f83818833f977da651f673971587312737a808 100644 (file)
@@ -188,8 +188,9 @@ static int do_execute(
         return 0;
 }
 
-int execute_directories(
-                const char* const* directories,
+int execute_strv(
+                const char *name,
+                char* const* paths,
                 usec_t timeout,
                 gather_stdout_callback_t const callbacks[_STDOUT_CONSUME_MAX],
                 void* const callback_args[_STDOUT_CONSUME_MAX],
@@ -197,32 +198,20 @@ int execute_directories(
                 char *envp[],
                 ExecDirFlags flags) {
 
-        _cleanup_strv_free_ char **paths = NULL;
         _cleanup_close_ int fd = -EBADF;
         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");
-
-        if (strv_isempty(paths)) {
-                log_debug("No executables found.");
+        if (strv_isempty(paths))
                 return 0;
-        }
 
         if (callbacks) {
+                assert(name);
                 assert(callback_args);
                 assert(callbacks[STDOUT_GENERATE]);
                 assert(callbacks[STDOUT_COLLECT]);
                 assert(callbacks[STDOUT_CONSUME]);
 
-                _cleanup_free_ char *name = NULL;
-
-                r = path_extract_filename(directories[0], &name);
-                if (r < 0)
-                        return log_error_errno(r, "Failed to extract file name from '%s': %m", directories[0]);
-
                 fd = open_serialization_fd(name);
                 if (fd < 0)
                         return log_error_errno(fd, "Failed to open serialization file: %m");
@@ -258,6 +247,39 @@ int execute_directories(
         return 0;
 }
 
+int execute_directories(
+                const char* const* directories,
+                usec_t timeout,
+                gather_stdout_callback_t const callbacks[_STDOUT_CONSUME_MAX],
+                void* const callback_args[_STDOUT_CONSUME_MAX],
+                char *argv[],
+                char *envp[],
+                ExecDirFlags flags) {
+
+        _cleanup_strv_free_ char **paths = NULL;
+        _cleanup_free_ char *name = NULL;
+        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) {
+                r = path_extract_filename(directories[0], &name);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to extract file name from '%s': %m", directories[0]);
+        }
+
+        return execute_strv(name, paths, timeout, callbacks, callback_args, argv, envp, flags);
+}
+
 static int gather_environment_generate(int fd, void *arg) {
         char ***env = ASSERT_PTR(arg);
         _cleanup_fclose_ FILE *f = NULL;