From: Yu Watanabe Date: Thu, 16 Mar 2023 02:16:51 +0000 (+0900) Subject: exec-util: extract the core logic of execute_directories() as execute_strv() X-Git-Tag: v254-rc1~990^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f691157b87371266d0cab1b9e67a3bc3808ce30f;p=thirdparty%2Fsystemd.git exec-util: extract the core logic of execute_directories() as execute_strv() Then, we can use it with a custom enumerator of executables. No functional change, preparation for later commits. --- diff --git a/src/shared/exec-util.c b/src/shared/exec-util.c index e44323e0e90..29f83818833 100644 --- a/src/shared/exec-util.c +++ b/src/shared/exec-util.c @@ -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; diff --git a/src/shared/exec-util.h b/src/shared/exec-util.h index ba4506e5aa9..a18c108e1bd 100644 --- a/src/shared/exec-util.h +++ b/src/shared/exec-util.h @@ -30,6 +30,16 @@ typedef enum ExecCommandFlags { _EXEC_COMMAND_FLAGS_INVALID = -EINVAL, } ExecCommandFlags; +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], + char *argv[], + char *envp[], + ExecDirFlags flags); + int execute_directories( const char* const* directories, usec_t timeout,