From 0f8e03e2b79cb1ea249ff5d3acbfc0c7c7892a2b Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sun, 13 Sep 2020 23:56:44 -0500 Subject: [PATCH] Update: get only exist status when processing return, use "return_code", and set return error manually where applicable. The return code also stores signal information. To properly return the expected return code, WEXITSTATUS() should be used. In some cases a return code of "0" is always returned, even on error. In these cases, now return "1" if an error is detected. Use "return_code" more consistently, it is more explicit than "result" or "results". --- level_3/fake/c/private-build.c | 4 ++-- level_3/fake/c/private-make.c | 44 ++++++++++++++++++++++++----------- level_3/firewall/c/firewall.c | 8 +++---- level_3/firewall/c/private-firewall.c | 24 +++++++++---------- 4 files changed, 48 insertions(+), 32 deletions(-) diff --git a/level_3/fake/c/private-build.c b/level_3/fake/c/private-build.c index 5c40a23..fdbe304 100644 --- a/level_3/fake/c/private-build.c +++ b/level_3/fake/c/private-build.c @@ -567,14 +567,14 @@ extern "C" { f_macro_string_dynamics_t_delete_simple(arguments); } else { - int result = 0; + int return_code = 0; // child processes should receive all signals, without blocking. f_signal_how_t signals = f_signal_how_t_initialize; f_signal_set_empty(&signals.block); f_signal_set_fill(&signals.block_not); - *status = fll_execute_path_environment(path.string, arguments, &signals, data_build.environment.names, data_build.environment.values, &result); + *status = fll_execute_path_environment(path.string, arguments, &signals, data_build.environment.names, data_build.environment.values, &return_code); f_macro_string_dynamics_t_delete_simple(arguments); diff --git a/level_3/fake/c/private-make.c b/level_3/fake/c/private-make.c index 7658b33..3e6a9cd 100644 --- a/level_3/fake/c/private-make.c +++ b/level_3/fake/c/private-make.c @@ -2220,9 +2220,8 @@ extern "C" { if (F_status_is_error(*status)) { fake_print_message(data, F_status_set_fine(*status), "fake_execute", F_true, data_make->print); } - else { - fake_make_operate_process_return(data, return_code, data_make, status); - } + + fake_make_operate_process_return(data, return_code, data_make, status); return; } @@ -2256,7 +2255,13 @@ extern "C" { return; } - fake_make_operate_process_return(data, 0, data_make, status); + if (F_status_is_error(*status)) { + fake_make_operate_process_return(data, 1, data_make, status); + } + else { + fake_make_operate_process_return(data, 0, data_make, status); + } + return; } @@ -2268,7 +2273,13 @@ extern "C" { return; } - fake_make_operate_process_return(data, 0, data_make, status); + if (F_status_is_error(*status)) { + fake_make_operate_process_return(data, 1, data_make, status); + } + else { + fake_make_operate_process_return(data, 0, data_make, status); + } + return; } @@ -2357,9 +2368,8 @@ extern "C" { if (F_status_is_error(*status)) { fake_print_message(data, F_status_set_fine(*status), "fake_execute", F_true, data_make->print); } - else { - fake_make_operate_process_return(data, return_code, data_make, status); - } + + fake_make_operate_process_return(data, return_code, data_make, status); return; } @@ -3447,7 +3457,13 @@ extern "C" { return; } - fake_make_operate_process_return(data, 0, data_make, status); + if (F_status_is_error(*status)) { + fake_make_operate_process_return(data, 1, data_make, status); + } + else { + fake_make_operate_process_return(data, 0, data_make, status); + } + return; } @@ -3651,7 +3667,7 @@ extern "C" { fflush(f_type_output); } - int result = 0; + int return_code = 0; // child processes should receive all signals, without blocking. f_signal_how_t signals = f_signal_how_t_initialize; @@ -3659,10 +3675,10 @@ extern "C" { f_signal_set_fill(&signals.block_not); if (as_shell) { - status = fll_execute_path_environment(program.string, arguments, &signals, data_make->environment.names, data_make->environment.values, &result); + status = fll_execute_path_environment(program.string, arguments, &signals, data_make->environment.names, data_make->environment.values, &return_code); } else { - status = fll_execute_program_environment(program.string, arguments, &signals, data_make->environment.names, data_make->environment.values, &result); + status = fll_execute_program_environment(program.string, arguments, &signals, data_make->environment.names, data_make->environment.values, &return_code); } if (status == F_status_set_error(F_signal)) { @@ -3683,7 +3699,7 @@ extern "C" { } } - fake_make_operate_process_return(data, result, data_make, &status); + fake_make_operate_process_return(data, return_code, data_make, &status); return status; } @@ -3709,7 +3725,7 @@ extern "C" { if (return_code) { f_string_dynamic_t number = f_string_dynamic_t_initialize; - status2 = f_conversion_number_signed_to_string(return_code, 10, &number); + status2 = f_conversion_number_signed_to_string(WEXITSTATUS(return_code), 10, &number); if (F_status_is_error(status2)) { *status = status2; diff --git a/level_3/firewall/c/firewall.c b/level_3/firewall/c/firewall.c index 1b0ea6c..547e4f6 100644 --- a/level_3/firewall/c/firewall.c +++ b/level_3/firewall/c/firewall.c @@ -149,7 +149,7 @@ extern "C" { bool show_ports = F_true; f_string_dynamics_t parameters = f_string_dynamics_t_initialize; - int results = 0; + int return_code = 0; if (data->remaining.used > 0) { show_nat = F_false; @@ -209,7 +209,7 @@ extern "C" { parameters.array[4].used = 9; parameters.array[5].used = 6; - status = fll_execute_program((f_string_t) firewall_tool_iptables, parameters, 0, &results); + status = fll_execute_program((f_string_t) firewall_tool_iptables, parameters, 0, &return_code); fprintf(f_type_output, "\n"); fflush(f_type_output); @@ -237,7 +237,7 @@ extern "C" { parameters.array[4].used = 9; parameters.array[5].used = 6; - status = fll_execute_program((f_string_t) firewall_tool_iptables, parameters, 0, &results); + status = fll_execute_program((f_string_t) firewall_tool_iptables, parameters, 0, &return_code); fprintf(f_type_output, "\n"); fflush(f_type_output); @@ -261,7 +261,7 @@ extern "C" { parameters.array[2].used = 9; parameters.array[3].used = 6; - status = fll_execute_program((f_string_t) firewall_tool_iptables, parameters, 0, &results); + status = fll_execute_program((f_string_t) firewall_tool_iptables, parameters, 0, &return_code); fprintf(f_type_output, "\n"); fflush(f_type_output); diff --git a/level_3/firewall/c/private-firewall.c b/level_3/firewall/c/private-firewall.c index e5817cf..606001a 100644 --- a/level_3/firewall/c/private-firewall.c +++ b/level_3/firewall/c/private-firewall.c @@ -12,7 +12,7 @@ f_return_status firewall_perform_commands(const firewall_local_data_t local, con f_string_dynamics_t arguments = f_string_dynamics_t_initialize; f_string_dynamic_t argument = f_string_dynamic_t_initialize; - int results = 0; + int return_code = 0; f_string_length_t length = 0; bool invalid = F_false; bool is_ip_list = F_false; @@ -743,7 +743,7 @@ f_return_status firewall_perform_commands(const firewall_local_data_t local, con } #endif // _en_firewall_debug_ - status = fll_execute_program((f_string_t) current_tool, arguments, 0, &results); + status = fll_execute_program((f_string_t) current_tool, arguments, 0, &return_code); if (status == F_failure) { fl_color_print_line(f_type_error, data.context.error, data.context.reset, "ERROR: Failed to perform requested %s operation:", current_tool); @@ -813,7 +813,7 @@ f_return_status firewall_perform_commands(const firewall_local_data_t local, con } #endif // _en_firewall_debug_ - status = fll_execute_program(current_tool, arguments, 0, &results); + status = fll_execute_program(current_tool, arguments, 0, &return_code); if (status == F_failure) { fl_color_print_line(f_type_error, data.context.error, data.context.reset, "ERROR: Failed to perform requested %s operation:", current_tool); @@ -854,7 +854,7 @@ f_return_status firewall_create_custom_chains(firewall_reserved_chains_t *reserv uint8_t tool = firewall_program_iptables; bool new_chain = F_false; bool create_chain = F_false; - int results = 0; + int return_code = 0; f_array_length_t i = 0; f_array_length_t j = 0; @@ -1054,7 +1054,7 @@ f_return_status firewall_create_custom_chains(firewall_reserved_chains_t *reserv #endif // _en_firewall_debug_ tool = firewall_program_iptables; - status = fll_execute_program((f_string_t) firewall_tool_iptables, arguments, 0, &results); + status = fll_execute_program((f_string_t) firewall_tool_iptables, arguments, 0, &return_code); if (F_status_is_error_not(status)) { // print command when debugging. @@ -1073,7 +1073,7 @@ f_return_status firewall_create_custom_chains(firewall_reserved_chains_t *reserv #endif // _en_firewall_debug_ tool = firewall_program_ip6tables; - status = fll_execute_program((f_string_t) firewall_tool_ip6tables, arguments, 0, &results); + status = fll_execute_program((f_string_t) firewall_tool_ip6tables, arguments, 0, &return_code); } if (F_status_is_error(status)) { @@ -1134,7 +1134,7 @@ f_return_status firewall_delete_chains(const firewall_data_t data) { for (f_string_length_t i = 0; i < 2; i++) { f_string_dynamics_t arguments = f_string_dynamics_t_initialize; f_string_dynamic_t argument[1] = f_string_dynamic_t_initialize; - int results = 0; + int return_code = 0; argument[0].string = (f_string_t) "-F"; argument[0].size = 2; @@ -1159,7 +1159,7 @@ f_return_status firewall_delete_chains(const firewall_data_t data) { } #endif // _en_firewall_debug_ - status = fll_execute_program(tools[i], arguments, 0, &results); + status = fll_execute_program(tools[i], arguments, 0, &return_code); if (F_status_is_error(status)) { status = F_status_set_fine(status); @@ -1192,7 +1192,7 @@ f_return_status firewall_delete_chains(const firewall_data_t data) { for (f_string_length_t i = 0; i < 2; i++) { f_string_dynamics_t arguments = f_string_dynamics_t_initialize; f_string_dynamic_t argument[1] = f_string_dynamic_t_initialize; - int results = 0; + int return_code = 0; argument[0].string = (f_string_t) firewall_chain_delete_command; argument[0].size = firewall_chain_delete_command_length; @@ -1217,7 +1217,7 @@ f_return_status firewall_delete_chains(const firewall_data_t data) { } #endif // _en_firewall_debug_ - status = fll_execute_program(tools[i], arguments, 0, &results); + status = fll_execute_program(tools[i], arguments, 0, &return_code); if (F_status_is_error(status)) { status = F_status_set_fine(status); @@ -1279,7 +1279,7 @@ f_return_status firewall_default_lock(const firewall_data_t data) { arguments.array[2].size = arguments.array[2].used; for (f_string_length_t j = 0; j < 2; j++) { - int results = 0; + int return_code = 0; // print command when debugging. #ifdef _en_firewall_debug_ @@ -1296,7 +1296,7 @@ f_return_status firewall_default_lock(const firewall_data_t data) { } #endif // _en_firewall_debug_ - status = fll_execute_program(tools[j], arguments, 0, &results); + status = fll_execute_program(tools[j], arguments, 0, &return_code); if (F_status_is_error(status)) { status = F_status_set_fine(status); -- 1.8.3.1