From 646a93f47bd13b489d1f365ff2e776e8c9ed22f9 Mon Sep 17 00:00:00 2001 From: Caelan Sayler Date: Wed, 6 Mar 2024 20:01:48 +0000 Subject: [PATCH 1/3] Add CWD option to subprocess_create_ex --- subprocess.h | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/subprocess.h b/subprocess.h index a98fc75..b98a0a2 100644 --- a/subprocess.h +++ b/subprocess.h @@ -118,6 +118,8 @@ subprocess_weak int subprocess_create(const char *const command_line[], /// for a child process (each element of the form FOO=BAR). The last element /// must be NULL to signify the end of the array. /// @param out_process The newly created process. +/// @param process_cwd The CWD of the newly created process. If null, will be +/// the same as the parent process. /// @return On success zero is returned. /// /// If `options` contains `subprocess_option_inherit_environment`, then @@ -125,7 +127,8 @@ subprocess_weak int subprocess_create(const char *const command_line[], subprocess_weak int subprocess_create_ex(const char *const command_line[], int options, const char *const environment[], - struct subprocess_s *const out_process); + struct subprocess_s *const out_process, + const char *const process_cwd); /// @brief Get the standard input file for a process. /// @param process The process to query. @@ -482,12 +485,13 @@ int subprocess_create_named_pipe_helper(void **rd, void **wr) { int subprocess_create(const char *const commandLine[], int options, struct subprocess_s *const out_process) { return subprocess_create_ex(commandLine, options, SUBPROCESS_NULL, - out_process); + out_process, SUBPROCESS_NULL); } int subprocess_create_ex(const char *const commandLine[], int options, const char *const environment[], - struct subprocess_s *const out_process) { + struct subprocess_s *const out_process, + const char *const process_cwd) { #if defined(_WIN32) int fd; void *rd, *wr; @@ -741,7 +745,7 @@ int subprocess_create_ex(const char *const commandLine[], int options, 1, // handles are inherited flags, // creation flags used_environment, // used environment - SUBPROCESS_NULL, // use parent's current directory + process_cwd, // use specified current directory SUBPROCESS_PTR_CAST(LPSTARTUPINFOA, &startInfo), // STARTUPINFO pointer SUBPROCESS_PTR_CAST(LPPROCESS_INFORMATION, &processInfo))) { @@ -819,6 +823,12 @@ int subprocess_create_ex(const char *const commandLine[], int options, return -1; } + // Set working directory + if (0 != posix_spawn_file_actions_addchdir_np(&actions, process_cwd)) { + posix_spawn_file_actions_destroy(&actions); + return -1; + } + // Close the stdin write end if (0 != posix_spawn_file_actions_addclose(&actions, stdinfd[1])) { posix_spawn_file_actions_destroy(&actions); @@ -1196,4 +1206,4 @@ int subprocess_alive(struct subprocess_s *const process) { } // extern "C" #endif -#endif /* SHEREDOM_SUBPROCESS_H_INCLUDED */ +#endif /* SHEREDOM_SUBPROCESS_H_INCLUDED */ \ No newline at end of file From 1feefb8263e064832a70796d6629de49f2de550c Mon Sep 17 00:00:00 2001 From: Caelan Sayler Date: Thu, 21 Mar 2024 10:52:02 +0000 Subject: [PATCH 2/3] Add cwd for create_ex tests --- test/main.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/test/main.c b/test/main.c index 788adfb..ee1cac3 100644 --- a/test/main.c +++ b/test/main.c @@ -779,7 +779,8 @@ UTEST(environment, illegal_inherit_environment) { ASSERT_NE(0, subprocess_create_ex(commandLine, subprocess_option_inherit_environment, - environment, &process)); + environment, &process, + SUBPROCESS_NULL)); } UTEST(environment, illegal_empty_environment_with_inherit_environment) { @@ -789,7 +790,8 @@ UTEST(environment, illegal_empty_environment_with_inherit_environment) { ASSERT_NE(0, subprocess_create_ex(commandLine, subprocess_option_inherit_environment, - environment, &process)); + environment, &process, + SUBPROCESS_NULL)); } UTEST(environment, null_environment_with_inherit_environment) { @@ -798,7 +800,8 @@ UTEST(environment, null_environment_with_inherit_environment) { ASSERT_EQ(0, subprocess_create_ex(commandLine, subprocess_option_inherit_environment, 0, - &process)); + &process, + SUBPROCESS_NULL)); ASSERT_EQ(0, subprocess_destroy(&process)); } @@ -809,7 +812,7 @@ UTEST(environment, specify_environment) { struct subprocess_s process; int ret = -1; - ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, &process)); + ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, &process, SUBPROCESS_NULL)); ASSERT_EQ(0, subprocess_join(&process, &ret)); @@ -825,7 +828,7 @@ UTEST(executable_resolve, no_slashes_with_environment) { struct subprocess_s process; int ret = -1; - ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, &process)); + ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, &process, SUBPROCESS_NULL)); ASSERT_EQ(0, subprocess_join(&process, &ret)); @@ -843,7 +846,8 @@ UTEST(executable_resolve, no_slashes_with_inherit) { ASSERT_EQ(0, subprocess_create_ex(commandLine, subprocess_option_inherit_environment, 0, - &process)); + &process, + SUBPROCESS_NULL)); ASSERT_EQ(0, subprocess_join(&process, &ret)); @@ -866,7 +870,7 @@ UTEST(executable_resolve, custom_search_path) { snprintf(path_var, sizeof(path_var), "PATH=%s", current_path); environment[0] = path_var; - ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, &process)); + ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, &process, SUBPROCESS_NULL)); ASSERT_EQ(0, subprocess_join(&process, &ret)); From 87be41ef47ee5d75e2f2bf424f9e9840c49f93c3 Mon Sep 17 00:00:00 2001 From: Caelan Sayler Date: Thu, 21 Mar 2024 17:26:00 +0000 Subject: [PATCH 3/3] Implement CWD feedback: re-order parameters, posix bug fix --- subprocess.h | 22 ++++++++++++---------- test/main.c | 30 +++++++++++++++++------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/subprocess.h b/subprocess.h index b98a0a2..e3ba7c4 100644 --- a/subprocess.h +++ b/subprocess.h @@ -117,9 +117,9 @@ subprocess_weak int subprocess_create(const char *const command_line[], /// @param environment An optional array of strings for the environment to use /// for a child process (each element of the form FOO=BAR). The last element /// must be NULL to signify the end of the array. +/// @param process_cwd The current working directory of the newly created +/// process. If NULL, will be the same as the parent process. /// @param out_process The newly created process. -/// @param process_cwd The CWD of the newly created process. If null, will be -/// the same as the parent process. /// @return On success zero is returned. /// /// If `options` contains `subprocess_option_inherit_environment`, then @@ -127,8 +127,8 @@ subprocess_weak int subprocess_create(const char *const command_line[], subprocess_weak int subprocess_create_ex(const char *const command_line[], int options, const char *const environment[], - struct subprocess_s *const out_process, - const char *const process_cwd); + const char *const process_cwd, + struct subprocess_s *const out_process); /// @brief Get the standard input file for a process. /// @param process The process to query. @@ -485,13 +485,13 @@ int subprocess_create_named_pipe_helper(void **rd, void **wr) { int subprocess_create(const char *const commandLine[], int options, struct subprocess_s *const out_process) { return subprocess_create_ex(commandLine, options, SUBPROCESS_NULL, - out_process, SUBPROCESS_NULL); + SUBPROCESS_NULL, out_process); } int subprocess_create_ex(const char *const commandLine[], int options, const char *const environment[], - struct subprocess_s *const out_process, - const char *const process_cwd) { + const char *const process_cwd, + struct subprocess_s *const out_process) { #if defined(_WIN32) int fd; void *rd, *wr; @@ -824,9 +824,11 @@ int subprocess_create_ex(const char *const commandLine[], int options, } // Set working directory - if (0 != posix_spawn_file_actions_addchdir_np(&actions, process_cwd)) { - posix_spawn_file_actions_destroy(&actions); - return -1; + if (process_cwd) { + if (0 != posix_spawn_file_actions_addchdir_np(&actions, process_cwd)) { + posix_spawn_file_actions_destroy(&actions); + return -1; + } } // Close the stdin write end diff --git a/test/main.c b/test/main.c index ee1cac3..a3bf0c7 100644 --- a/test/main.c +++ b/test/main.c @@ -779,8 +779,9 @@ UTEST(environment, illegal_inherit_environment) { ASSERT_NE(0, subprocess_create_ex(commandLine, subprocess_option_inherit_environment, - environment, &process, - SUBPROCESS_NULL)); + environment, + SUBPROCESS_NULL, + &process)); } UTEST(environment, illegal_empty_environment_with_inherit_environment) { @@ -790,8 +791,9 @@ UTEST(environment, illegal_empty_environment_with_inherit_environment) { ASSERT_NE(0, subprocess_create_ex(commandLine, subprocess_option_inherit_environment, - environment, &process, - SUBPROCESS_NULL)); + environment, + SUBPROCESS_NULL, + &process)); } UTEST(environment, null_environment_with_inherit_environment) { @@ -799,9 +801,10 @@ UTEST(environment, null_environment_with_inherit_environment) { struct subprocess_s process; ASSERT_EQ(0, subprocess_create_ex(commandLine, - subprocess_option_inherit_environment, 0, - &process, - SUBPROCESS_NULL)); + subprocess_option_inherit_environment, + 0, + SUBPROCESS_NULL, + &process)); ASSERT_EQ(0, subprocess_destroy(&process)); } @@ -812,7 +815,7 @@ UTEST(environment, specify_environment) { struct subprocess_s process; int ret = -1; - ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, &process, SUBPROCESS_NULL)); + ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, SUBPROCESS_NULL, &process)); ASSERT_EQ(0, subprocess_join(&process, &ret)); @@ -828,7 +831,7 @@ UTEST(executable_resolve, no_slashes_with_environment) { struct subprocess_s process; int ret = -1; - ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, &process, SUBPROCESS_NULL)); + ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, SUBPROCESS_NULL, &process)); ASSERT_EQ(0, subprocess_join(&process, &ret)); @@ -845,9 +848,10 @@ UTEST(executable_resolve, no_slashes_with_inherit) { int ret = -1; ASSERT_EQ(0, subprocess_create_ex(commandLine, - subprocess_option_inherit_environment, 0, - &process, - SUBPROCESS_NULL)); + subprocess_option_inherit_environment, + 0, + SUBPROCESS_NULL, + &process)); ASSERT_EQ(0, subprocess_join(&process, &ret)); @@ -870,7 +874,7 @@ UTEST(executable_resolve, custom_search_path) { snprintf(path_var, sizeof(path_var), "PATH=%s", current_path); environment[0] = path_var; - ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, &process, SUBPROCESS_NULL)); + ASSERT_EQ(0, subprocess_create_ex(commandLine, 0, environment, SUBPROCESS_NULL, &process)); ASSERT_EQ(0, subprocess_join(&process, &ret));