From: Kevin Day Date: Sun, 7 Jul 2024 04:38:02 +0000 (-0500) Subject: Progress: Continue migrating the project. X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=f8fe8732155b49d0038df0066951becc8e19a416;p=controller Progress: Continue migrating the project. --- diff --git a/sources/c/main/common/type/instance.c b/sources/c/main/common/type/instance.c index 5d95132..b3384bf 100644 --- a/sources/c/main/common/type/instance.c +++ b/sources/c/main/common/type/instance.c @@ -16,10 +16,6 @@ extern "C" { instance->id_thread = 0; } - // TODO: Review how the project uses locks and how to safely delete them. - // The POSIX standard might quite horribly provide undefined behavior on double de-allocaton without providing a way to tell if the data is already de-allocated or not. - // There may need to be a boolean/flag on each instance to designate whether or not the locks were initialized or not. - // Actually, the instances is a double-pointer so that double-point can act as a boolean/flag. f_thread_condition_delete(&instance->wait); f_thread_lock_delete(&instance->lock); f_thread_lock_delete(&instance->active); @@ -58,6 +54,9 @@ extern "C" { #ifndef _di_controller_instance_initialize_ f_status_t controller_instance_initialize(controller_instance_t ** restrict const instance) { + if (!instance) return F_status_set_error(F_parameter); + if (*instance) return F_okay; + f_status_t status = f_memory_new(1, sizeof(controller_instance_t), (void **) instance); if (F_status_is_error_not(status)) { @@ -80,9 +79,6 @@ extern "C" { (*instance)->rule.status[i] = F_known_not; } // for - // TODO: There probably should be a boolean/flag to represent that the locks were initialized. - // And on allocation failure on any lock before all locks are allocated should require that any allocated locks be de-allocated before returning on error. - return F_status_is_error(status) ? status : F_okay; } #endif // _di_controller_instance_initialize_ diff --git a/sources/c/main/common/type/instance.h b/sources/c/main/common/type/instance.h index bfdac65..5294464 100644 --- a/sources/c/main/common/type/instance.h +++ b/sources/c/main/common/type/instance.h @@ -176,6 +176,8 @@ extern "C" { * @return * F_okay on success. * + * F_parameter (with error bit) on invalid parameter. + * * Errors (with error bit) from: f_thread_condition_create(). * Errors (with error bit) from: f_thread_lock_create(). * Errors (with error bit) from: f_thread_mutex_create(). diff --git a/sources/c/main/instance/prepare.c b/sources/c/main/instance/prepare.c index 39450ec..c43df96 100644 --- a/sources/c/main/instance/prepare.c +++ b/sources/c/main/instance/prepare.c @@ -23,21 +23,16 @@ extern "C" { status = f_memory_array_increase(controller_allocation_small_d, sizeof(controller_instance_t), (void **) &main->thread.instances.array, &main->thread.instances.used, &main->thread.instances.size); } - printf("\nDEBUG: instances = (%lu, %lu), status=%d, %lu\n", main->thread.instances.used, main->thread.instances.size, F_status_set_fine(status), main->thread.instances.array[main->thread.instances.used]); - // The Instances array has instance as a double-pointer. if (F_status_is_error_not(status) && !main->thread.instances.array[main->thread.instances.used]) { status = controller_instance_initialize(&main->thread.instances.array[main->thread.instances.used]); } if (F_status_is_error_not(status)) { - printf("\nDEBUG: instances before initialization = (%lu, %lu)\n", main->thread.instances.used, main->thread.instances.size); // The Instances array has instance as a double-pointer. status = controller_instance_initialize(&main->thread.instances.array[main->thread.instances.used]); - printf("\nDEBUG: initialized instance at %lu, status = %d\n", main->thread.instances.used, F_status_set_fine(status)); - controller_instance_t * const instance = F_status_is_error_not(status) ? main->thread.instances.array[main->thread.instances.used] : 0; if (F_status_is_error_not(status)) { diff --git a/sources/c/main/process.c b/sources/c/main/process.c index 42a8f1b..477c665 100644 --- a/sources/c/main/process.c +++ b/sources/c/main/process.c @@ -25,50 +25,57 @@ extern "C" { return; } - // The locks must be initialized, but only once, so initialize immediately upon allocation. - f_status_t status = controller_lock_create(&main->thread.lock); - - if (F_status_is_error(status)) { - controller_print_error_status(&main->program.error, macro_controller_f(controller_lock_create), status); - }/* else { // TODO: Is this block needed here, given allocation will also happen later on as needed? - status = f_memory_array_increase(controller_allocation_small_d, sizeof(controller_instance_t), (void **) &main->thread.instances.array, &main->thread.instances.used, &main->thread.instances.size); + f_signal_set_fill(&main->program.signal.set); - if (F_status_is_error(status)) { - controller_print_error_status(&main->program.error, macro_controller_f(f_memory_array_increase), status); - } - }*/ + f_status_t status = f_thread_signal_mask(SIG_BLOCK, &main->program.signal.set, 0); if (F_status_is_error_not(status)) { - status = f_thread_create(0, &main->thread.id_signal, &controller_thread_signal_normal, (void *) main); + status = f_signal_open(&main->program.signal); } + // If there is an error opening a signal descriptor, then do not handle signals. if (F_status_is_error(status)) { - main->thread.id_signal = 0; + f_thread_signal_mask(SIG_UNBLOCK, &main->program.signal.set, 0); + f_signal_close(&main->program.signal); + } + + // The locks must be initialized, but only once, so initialize immediately upon allocation. + status = controller_lock_create(&main->thread.lock); - controller_print_error_status(&main->program.error, macro_controller_f(f_thread_create), status); + if (F_status_is_error(status)) { + controller_print_error_status(&main->program.error, macro_controller_f(controller_lock_create), status); } else { - if (main->setting.flag & controller_main_flag_daemon_e) { - main->process.ready = controller_process_ready_done_e; + status = f_thread_create(0, &main->thread.id_signal, &controller_thread_signal_normal, (void *) main); - if (f_file_exists(main->process.path_pid, F_true) == F_true) { - status = F_status_set_error(F_available_not); - main->process.ready = controller_process_ready_abort_e; + if (F_status_is_error(status)) { + main->thread.id_signal = 0; - controller_print_error_file_pid_exists(&main->program.error, &main->thread, main->process.path_pid); - } + controller_print_error_status(&main->program.error, macro_controller_f(f_thread_create), status); } - else if (main->process.name_entry.used) { - status = f_thread_create(0, &main->thread.id_entry, &controller_thread_entry, (void *) main); + else { + if (main->setting.flag & controller_main_flag_daemon_e) { + main->process.ready = controller_process_ready_done_e; - if (F_status_is_error(status)) { - controller_print_error_status(&main->program.error, macro_controller_f(f_thread_create), status); + if (f_file_exists(main->process.path_pid, F_true) == F_true) { + status = F_status_set_error(F_available_not); + main->process.ready = controller_process_ready_abort_e; + + controller_print_error_file_pid_exists(&main->program.error, &main->thread, main->process.path_pid); + } } - else { - controller_thread_join(&main->thread.id_entry); + else if (main->process.name_entry.used) { + status = f_thread_create(0, &main->thread.id_entry, &controller_thread_entry, (void *) main); - status = main->thread.status; - main->thread.id_entry = 0; + if (F_status_is_error(status)) { + controller_print_error_status(&main->program.error, macro_controller_f(f_thread_create), status); + } + else { + controller_thread_join(&main->thread.id_entry); + + status = main->thread.status; + main->thread.id_entry = 0; + } } } } diff --git a/sources/c/main/rule.c b/sources/c/main/rule.c index 6916248..0586021 100644 --- a/sources/c/main/rule.c +++ b/sources/c/main/rule.c @@ -5,7 +5,9 @@ extern "C" { #endif #ifndef _di_controller_rule_copy_ - f_status_t controller_rule_copy(const controller_rule_t source, controller_rule_t *destination) { + f_status_t controller_rule_copy(const controller_rule_t source, controller_rule_t * const destination) { + + if (!destination) return F_status_set_error(F_parameter); // Delete the third party structures. f_memory_array_resize(0, sizeof(f_char_t), (void **) &destination->cgroup.path.string, &destination->cgroup.path.used, &destination->cgroup.path.size); @@ -123,7 +125,7 @@ extern "C" { destination->ons.used = source.ons.used; } - status = f_memory_array_append_all(source.affinity.array, source.affinity.used, sizeof(int32_t), (void **) &destination->affinity.array, &destination->affinity.used, &destination->affinity.size); + status = f_memory_array_append_all((void *) source.affinity.array, source.affinity.used, sizeof(int32_t), (void **) &destination->affinity.array, &destination->affinity.used, &destination->affinity.size); if (F_status_is_error(status)) return status; if (source.capability) { @@ -214,8 +216,9 @@ extern "C" { #endif // _di_controller_rule_copy_ #ifndef _di_controller_rule_find_ - f_status_t controller_rule_find(const f_string_static_t alias, const controller_rules_t rules, f_number_unsigned_t *at) { + f_status_t controller_rule_find(const f_string_static_t alias, const controller_rules_t rules, f_number_unsigned_t * const at) { + if (!at) return F_status_set_error(F_parameter); if (!alias.used) return F_okay; if (!rules.used) return F_false; diff --git a/sources/c/main/rule.h b/sources/c/main/rule.h index bc148ee..447acb7 100644 --- a/sources/c/main/rule.h +++ b/sources/c/main/rule.h @@ -26,6 +26,8 @@ extern "C" { * @param destination * The destination rule to copy to. * + * Must not be NULL. + * * @return * F_okay on success. * @@ -48,7 +50,7 @@ extern "C" { * @see f_string_maps_append_all() */ #ifndef _di_controller_rule_copy_ - extern f_status_t controller_rule_copy(const controller_rule_t source, controller_rule_t *destination); + extern f_status_t controller_rule_copy(const controller_rule_t source, controller_rule_t * const destination); #endif // _di_controller_rule_copy_ /** @@ -59,8 +61,9 @@ extern "C" { * @param rules * The rules to search through. * @param at - * The index the rule was found at. - * (optional) Set to NULL to disable. + * (optional) The index the rule was found at. + * + * Set to NULL to disable. * * @return * F_okay on success, but the id.used is 0. @@ -68,7 +71,7 @@ extern "C" { * F_false on success and rule was not found. */ #ifndef _di_controller_rule_find_ - extern f_status_t controller_rule_find(const f_string_static_t alias, const controller_rules_t rules, f_number_unsigned_t *at); + extern f_status_t controller_rule_find(const f_string_static_t alias, const controller_rules_t rules, f_number_unsigned_t * const at); #endif // _di_controller_rule_find_ /** diff --git a/sources/c/main/thread/signal.c b/sources/c/main/thread/signal.c index 0b68e9d..5a7b303 100644 --- a/sources/c/main/thread/signal.c +++ b/sources/c/main/thread/signal.c @@ -11,18 +11,16 @@ extern "C" { if (!controller_thread_is_enabled(is_normal, &main->thread)) return; if (!(main->setting.flag & controller_main_flag_interruptible_e)) return; - f_status_t status = F_okay; siginfo_t information; f_time_spec_t time = f_time_spec_t_initialize; while (controller_thread_is_enabled(is_normal, &main->thread)) { - controller_time_now(controller_thread_exit_ready_timeout_seconds_d, controller_thread_exit_ready_timeout_nanoseconds_d, &time); - memset((void *) &information, 0, sizeof(siginfo_t)); - status = f_signal_wait_until(&main->program.signal.set, &time, &information); - if (status == F_time_out) continue; + controller_time_now(controller_thread_exit_ready_timeout_seconds_d, controller_thread_exit_ready_timeout_nanoseconds_d, &time); + + if (f_signal_wait_until(&main->program.signal.set, &time, &information) == F_time_out) continue; if (information.si_signo == F_signal_interrupt || information.si_signo == F_signal_abort || information.si_signo == F_signal_quit || information.si_signo == F_signal_termination) { main->thread.signal = information.si_signo; diff --git a/sources/c/main/time.c b/sources/c/main/time.c index bd78755..037c938 100644 --- a/sources/c/main/time.c +++ b/sources/c/main/time.c @@ -13,14 +13,14 @@ extern "C" { f_time_of_day_get(&now); time->tv_sec = now.tv_sec + seconds; - time->tv_nsec = (now.tv_usec * 1000) + nanoseconds; + time->tv_nsec = (now.tv_usec * 1000); } - // If tv_nsec is 1 second or greater, then increment seconds. - if (time->tv_nsec >= 1000000000) { + // If result would be greater than 1 second, then increment seconds without overflowing. + if (time->tv_nsec > 500000000 && nanoseconds > 500000000) { ++(time->tv_sec); - time->tv_nsec -= 1000000000; + time->tv_nsec = (time->tv_nsec - 500000000) + (nanoseconds - 500000000); } } #endif // _di_controller_time_now_