From 19b6ade38fdff49dad6450da97e13cc68c0c8f38 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Fri, 1 Oct 2021 23:54:06 -0500 Subject: [PATCH] Update: Make fl_execute_into() slightly more consistent with fl_execute_program(), adding environment processing. Allow for the environment to be conditionally cleared and re-defined in the same way fl_execute_program() does. --- level_2/fll_execute/c/execute.c | 18 +++++++++++++++++- level_2/fll_execute/c/execute.h | 26 +++++++++++++++----------- level_3/controller/c/private-controller.c | 2 +- level_3/controller/c/private-thread.c | 3 --- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/level_2/fll_execute/c/execute.c b/level_2/fll_execute/c/execute.c index f62b2e6..cea1d16 100644 --- a/level_2/fll_execute/c/execute.c +++ b/level_2/fll_execute/c/execute.c @@ -130,7 +130,7 @@ extern "C" { #endif // _di_fll_execute_arguments_dynamic_add_set_ #ifndef _di_fll_execute_into_ - f_status_t fll_execute_into(const f_string_t program, const f_string_statics_t arguments, const uint8_t option, int *result) { + f_status_t fll_execute_into(const f_string_t program, const f_string_statics_t arguments, const uint8_t option, const f_string_maps_t *environment, int *result) { #ifndef _di_level_2_parameter_checking_ if (!program && !arguments.used) return F_status_set_error(F_parameter); if (!result) return F_status_set_error(F_parameter); @@ -248,9 +248,25 @@ extern "C" { fixed_arguments[0] = program_path; + if (environment) { + clearenv(); + + for (f_array_length_t i = 0; i < environment->used; ++i) { + f_environment_set_dynamic(environment->array[i].name, environment->array[i].value, F_true); + } // for + } + code = execv(program_path, fixed_arguments); } else { + if (environment) { + clearenv(); + + for (f_array_length_t i = 0; i < environment->used; ++i) { + f_environment_set_dynamic(environment->array[i].name, environment->array[i].value, F_true); + } // for + } + code = last_slash ? execv(program ? program : arguments.array[0].string, fixed_arguments) : execvp(program ? program : arguments.array[0].string, fixed_arguments); } diff --git a/level_2/fll_execute/c/execute.h b/level_2/fll_execute/c/execute.h index 340cd4d..8e2a43e 100644 --- a/level_2/fll_execute/c/execute.h +++ b/level_2/fll_execute/c/execute.h @@ -362,7 +362,15 @@ extern "C" { * @param option * A bitwise set of options, such as: fl_execute_parameter_option_exit and fl_execute_parameter_option_path. * If fl_execute_parameter_option_exit: this will call exit() at the end of execution (be it success or failure). - * If fl_execute_parameter_option_path: use the whole program path (such as "/bin/bash" instead "bash" when populated argument[0]. + * If fl_execute_parameter_option_path: use the whole program path (such as "/bin/bash" instead "bash" when populating argument[0]. + * @param environment + * (optional) An map of strings representing the environment variable names and their respective values. + * Completely clears the environment and then creates environment variables for each name and value pair in this map. + * Set to an empty map (set map used length to 0). + * Set to NULL to not make any changes to the environment. + * Be careful, when executing without the full path (such as "bash" rather than "/bin/bash") either set this to NULL or be sure to include the PATH in this map. + * Be careful, scripts might return and the environment will have changed when this is not NULL. + * Be careful, if this returns F_failure, the environment will have changed when this is not NULL. * @param result * The code returned after finishing execution of program. * @@ -378,7 +386,7 @@ extern "C" { * @see strnlen() */ #ifndef _di_fll_execute_into_ - extern f_status_t fll_execute_into(const f_string_t program, const f_string_statics_t arguments, const uint8_t option, int *result); + extern f_status_t fll_execute_into(const f_string_t program, const f_string_statics_t arguments, const uint8_t option, const f_string_maps_t *environment, int *result); #endif // _di_fll_execute_into_ /** @@ -386,11 +394,6 @@ extern "C" { * * The program will be executed via a forked child process. * - * If parameter.names is specified: - * Uses the provided environment array to designate the environment for the program being executed. - * The environment is defined by the names and values pair. - * This requires paramete.values to also be specified with the same used length as parameter.names. - * * When the path has a slash "/" or the environment is to be cleared, then this does validate the path to the program. * Otherwise, this does not validate the path to the program. * @@ -412,10 +415,11 @@ extern "C" { * (optional) This and most of its fields are optional and are disabled when set to 0. * option: * A bitwise set of options, such as: fl_execute_parameter_option_exit and fl_execute_parameter_option_path. - * names: - * An array of strings representing the environment variable names. - * At most names.used variables are created. - * Duplicate names are overwritten. + * environment: + * An map of strings representing the environment variable names and their respective values. + * Completely clears the environment and then creates environment variables for each name and value pair in this map. + * Set to an empty map (set map used length to 0). + * Set to NULL to not make any changes to the environment. * values: * An array of strings representing the environment variable names. * The values.used must be of at least names.used. diff --git a/level_3/controller/c/private-controller.c b/level_3/controller/c/private-controller.c index dc9baa1..ba10b5b 100644 --- a/level_3/controller/c/private-controller.c +++ b/level_3/controller/c/private-controller.c @@ -1180,7 +1180,7 @@ extern "C" { int result = 0; - status = fll_execute_into(0, entry_action->parameters, fl_execute_parameter_option_path, &result); + status = fll_execute_into(0, entry_action->parameters, fl_execute_parameter_option_path, 0, &result); if (F_status_is_error(status)) { if (F_status_set_fine(status) == F_file_found_not) { diff --git a/level_3/controller/c/private-thread.c b/level_3/controller/c/private-thread.c index fb47c10..462544e 100644 --- a/level_3/controller/c/private-thread.c +++ b/level_3/controller/c/private-thread.c @@ -222,7 +222,6 @@ extern "C" { } else { if (main->parameters[controller_parameter_daemon].result == f_console_result_found) { - setting->ready = controller_setting_ready_done; if (f_file_exists(setting->path_pid.string) == F_true) { @@ -241,7 +240,6 @@ extern "C" { } } else if (global.setting->name_entry.used) { - const controller_main_entry_t entry = macro_controller_main_entry_t_initialize(&global, global.setting); status = f_thread_create(0, &thread.id_entry, &controller_thread_entry, (void *) &entry); @@ -262,7 +260,6 @@ extern "C" { // only make the rule and control threads available once any/all pre-processing and are completed. if (F_status_is_error_not(status) && status != F_signal && status != F_failure && status != F_child && thread.enabled == controller_thread_enabled) { - if (main->parameters[controller_parameter_validate].result == f_console_result_none) { // wait for the entry thread to complete before starting the rule thread. -- 1.8.3.1