From 42941850db6b7d730b6d1341542dbf87fcedc84a Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Thu, 11 Jul 2024 00:21:10 -0500 Subject: [PATCH] Cleanup: Execute process child id structure. Make the variable "child" more verbose in its name to make it clear that it is coming from the process. This makes it more clear why there is a write lock obtained to change this. Perform other minor structural changes. --- level_3/controller/c/rule/private-rule.c | 64 +++++++++++--------------------- 1 file changed, 21 insertions(+), 43 deletions(-) diff --git a/level_3/controller/c/rule/private-rule.c b/level_3/controller/c/rule/private-rule.c index 99d50b9..38d1875 100644 --- a/level_3/controller/c/rule/private-rule.c +++ b/level_3/controller/c/rule/private-rule.c @@ -1394,7 +1394,6 @@ extern "C" { f_status_t controller_rule_execute_foreground(const uint8_t type, const f_string_static_t program, const f_string_statics_t arguments, const uint8_t options, controller_execute_set_t * const execute_set, controller_process_t * const process) { f_status_t status = F_none; - f_status_t status_lock = F_none; controller_main_t * const main = (controller_main_t *) process->main_data; controller_thread_t * const thread = (controller_thread_t *) process->main_thread; @@ -1409,7 +1408,7 @@ extern "C" { return status; } - pid_t *child = 0; + pid_t *process_child_id = 0; { f_array_length_t i = 0; @@ -1418,7 +1417,7 @@ extern "C" { ++i; } // while - child = &process->childs.array[i]; + process_child_id = &process->childs.array[i]; if (i == process->childs.used) { ++process->childs.used; @@ -1474,7 +1473,7 @@ extern "C" { f_thread_unlock(&process->lock); - status_lock = controller_lock_write_process(process, thread, &process->lock); + f_status_t status_lock = controller_lock_write_process(process, thread, &process->lock); if (F_status_is_error(status_lock)) { controller_lock_print_error_critical(main->error, F_status_set_fine(status_lock), F_false, thread); @@ -1491,7 +1490,7 @@ extern "C" { } // Assign the child process id to allow for the cancel process to send appropriate termination signals to the child process. - *child = id_child; + *process_child_id = id_child; f_thread_unlock(&process->lock); @@ -1501,18 +1500,13 @@ extern "C" { controller_lock_print_error_critical(main->error, F_status_set_fine(status_lock), F_true, thread); } + // Have the parent wait for the child process to finish. if (F_status_set_fine(status_lock) != F_interrupt) { - - // Have the parent wait for the child process to finish. waitpid(id_child, &result.status, 0); } if (F_status_set_fine(status_lock) == F_interrupt || !controller_thread_is_enabled_process(process, thread)) { - if (status_lock == F_none) { - return F_status_set_error(F_interrupt); - } - - return F_status_set_error(F_lock); + return status_lock == F_none ? F_status_set_error(F_interrupt) : F_status_set_error(F_lock); } if (status_lock == F_none) { @@ -1526,10 +1520,7 @@ extern "C" { if (F_status_set_fine(status_lock) != F_interrupt) { status = controller_lock_read_process(process, thread, &process->lock); - - if (status == F_none) { - return status_lock; - } + if (status == F_none) return status_lock; } return F_status_set_error(F_lock); @@ -1538,7 +1529,7 @@ extern "C" { process->result = result.status; // Remove the pid now that waidpid() has returned. - *child = 0; + *process_child_id = 0; f_thread_unlock(&process->lock); @@ -1576,9 +1567,7 @@ extern "C" { } } - if (status == F_child || F_status_set_fine(status) == F_interrupt) { - return status; - } + if (status == F_child || F_status_set_fine(status) == F_interrupt) return status; if (F_status_is_error(status)) { status = F_status_set_fine(status); @@ -1601,7 +1590,6 @@ extern "C" { f_status_t controller_rule_execute_pid_with(const f_string_dynamic_t pid_file, const uint8_t type, const f_string_static_t program, const f_string_statics_t arguments, const uint8_t options, const uint8_t with, controller_execute_set_t * const execute_set, controller_process_t * const process) { f_status_t status = F_none; - f_status_t status_lock = F_none; controller_main_t * const main = (controller_main_t *) process->main_data; controller_thread_t * const thread = (controller_thread_t *) process->main_thread; @@ -1624,8 +1612,8 @@ extern "C" { return status; } - pid_t *child = 0; - f_string_dynamic_t *child_pid_file = 0; + pid_t *process_child_id = 0; + f_string_dynamic_t *process_child_pid_file = 0; { f_array_length_t i = 0; @@ -1634,7 +1622,7 @@ extern "C" { ++i; } // while - child = &process->childs.array[i]; + process_child_id = &process->childs.array[i]; if (i == process->childs.used) { ++process->childs.used; @@ -1646,7 +1634,7 @@ extern "C" { ++i; } // while - child_pid_file = &process->path_pids.array[i]; + process_child_pid_file = &process->path_pids.array[i]; if (i == process->path_pids.used) { ++process->path_pids.used; @@ -1667,7 +1655,7 @@ extern "C" { return F_status_set_error(F_file_found); } - status = f_string_dynamic_append_nulless(pid_file, child_pid_file); + status = f_string_dynamic_append_nulless(pid_file, process_child_pid_file); if (F_status_is_error(status)) { controller_print_error(thread, main->error, F_status_set_fine(status), "f_string_dynamic_append_nulless", F_true); @@ -1724,7 +1712,7 @@ extern "C" { f_thread_unlock(&process->lock); - status_lock = controller_lock_write_process(process, thread, &process->lock); + f_status_t status_lock = controller_lock_write_process(process, thread, &process->lock); if (F_status_is_error(status_lock)) { controller_lock_print_error_critical(main->error, F_status_set_fine(status_lock), F_false, thread); @@ -1741,7 +1729,7 @@ extern "C" { } // Assign the child process id to allow for the cancel process to send appropriate termination signals to the child process. - *child = id_child; + *process_child_id = id_child; f_thread_unlock(&process->lock); @@ -1751,18 +1739,13 @@ extern "C" { controller_lock_print_error_critical(main->error, F_status_set_fine(status_lock), F_true, thread); } + // The child process should perform the change into background, therefore it is safe to wait for the child to exit (another process is spawned). if (F_status_set_fine(status_lock) != F_interrupt) { - - // The child process should perform the change into background, therefore it is safe to wait for the child to exit (another process is spawned). waitpid(id_child, &result.status, 0); } if (!controller_thread_is_enabled_process(process, thread)) { - if (status_lock == F_none) { - return F_status_set_error(F_interrupt); - } - - return F_status_set_error(F_lock); + return status_lock == F_none ? F_status_set_error(F_interrupt) : F_status_set_error(F_lock); } if (status_lock == F_none) { @@ -1776,10 +1759,7 @@ extern "C" { if (F_status_set_fine(status_lock) != F_interrupt) { status = controller_lock_read_process(process, thread, &process->lock); - - if (status == F_none) { - return status_lock; - } + if (status == F_none) return status_lock; } return F_status_set_error(F_lock); @@ -1788,7 +1768,7 @@ extern "C" { process->result = result.status; // Remove the pid now that waidpid() has returned. - *child = 0; + *process_child_id = 0; f_thread_unlock(&process->lock); @@ -1826,9 +1806,7 @@ extern "C" { } } - if (status == F_child || F_status_set_fine(status) == F_interrupt) { - return status; - } + if (status == F_child || F_status_set_fine(status) == F_interrupt) return status; if (F_status_is_error(status)) { status = F_status_set_fine(status); -- 1.8.3.1