From d7ca3f0f9dde08f44beae6e94a7b3fbddc870bff Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sun, 14 Apr 2024 23:12:45 -0500 Subject: [PATCH] Progress: Continue migrating the project. --- data/build/settings | 8 +-- sources/c/controller/controller.c | 4 +- sources/c/controller/controller.h | 13 +++- sources/c/controller/main.c | 9 +-- sources/c/init/init.h | 5 ++ sources/c/init/main.c | 5 +- sources/c/main/common.c | 35 +++++++++- sources/c/main/common.h | 6 +- sources/c/main/common/define/thread.h | 62 +++++++++++++++++ sources/c/main/common/enumeration/thread.h | 61 +++++++++++++++++ sources/c/main/common/print.c | 2 + sources/c/main/common/print.h | 2 + sources/c/main/common/type/cache.h | 2 +- sources/c/main/common/type/control.h | 22 +++--- sources/c/main/common/type/entry.h | 2 +- sources/c/main/common/type/lock.c | 59 +++++++++++++++++ sources/c/main/common/type/lock.h | 103 +++++++++++++++++++++++++++++ sources/c/main/common/type/process.c | 3 +- sources/c/main/common/type/process.h | 35 +++++----- sources/c/main/common/type/rule.h | 2 +- sources/c/main/common/type/thread.c | 18 +++++ sources/c/main/common/type/thread.h | 89 +++++++++++++++++++++++++ sources/c/main/controller.h | 5 ++ 23 files changed, 504 insertions(+), 48 deletions(-) create mode 100644 sources/c/main/common/define/thread.h create mode 100644 sources/c/main/common/enumeration/thread.h create mode 100644 sources/c/main/common/type/lock.c create mode 100644 sources/c/main/common/type/lock.h create mode 100644 sources/c/main/common/type/thread.c create mode 100644 sources/c/main/common/type/thread.h diff --git a/data/build/settings b/data/build/settings index e6bc61c..d1ef3f9 100644 --- a/data/build/settings +++ b/data/build/settings @@ -41,16 +41,16 @@ build_libraries-level -lfll_2 -lfll_1 -lfll_0 build_libraries-monolithic -lfll build_sources_library main/common.c main/common/define.c main/common/enumeration.c main/common/print.c main/common/string.c main/common/type.c -build_sources_library main/common/type/cache.c main/common/type/control.c main/common/type/entry.c main/common/type/process.c main/common/type/rule.c +build_sources_library main/common/type/cache.c main/common/type/control.c main/common/type/entry.c main/common/type/lock.c main/common/type/process.c main/common/type/rule.c main/common/type/thread.c build_sources_library main/common/string/general.c main/common/string/rule.c build_sources_library main/print/data.c main/print/debug.c main/print/error.c main/print/message.c main/print/verbose.c main/print/warning.c build_sources_library main/signal.c main/thread.c build_sources_headers main/common.h main/controller.h main/common/define.h main/common/enumeration.h main/common/print.h main/common/string.h main/common/type.h -build_sources_headers main/common/define/control.h main/common/define/entry.h main/common/define/rule.h -build_sources_headers main/common/enumeration/control.h main/common/enumeration/entry.h main/common/enumeration/process.h main/common/enumeration/rule.h +build_sources_headers main/common/define/control.h main/common/define/entry.h main/common/define/rule.h main/common/define/thread.h +build_sources_headers main/common/enumeration/control.h main/common/enumeration/entry.h main/common/enumeration/process.h main/common/enumeration/rule.h main/common/enumeration/thread.h build_sources_headers main/common/string/general.h main/common/string/rule.h -build_sources_headers main/common/type/cache.h main/common/type/control.h main/common/type/entry.h main/common/type/process.h main/common/type/rule.h +build_sources_headers main/common/type/cache.h main/common/type/control.h main/common/type/entry.h main/common/type/lock.h main/common/type/process.h main/common/type/rule.h main/common/type/thread.h build_sources_headers main/print/data.h main/print/debug.h main/print/error.h main/print/message.h main/print/verbose.h main/print/warning.h build_sources_headers main/signal.h main/thread.h diff --git a/sources/c/controller/controller.c b/sources/c/controller/controller.c index 86ae34c..579f58b 100644 --- a/sources/c/controller/controller.c +++ b/sources/c/controller/controller.c @@ -5,9 +5,9 @@ extern "C" { #endif #ifndef _di_controller_controller_main_ - void controller_controller_main(controller_main_t * const main) { + void controller_controller_main(controller_main_t * const main, controller_process_t * const process) { - if (!main) return; + if (!main || !process) return; if (F_status_is_error(main->setting.state.status)) { if ((main->setting.flag & controller_main_flag_print_last_e) && main->program.message.verbosity > f_console_verbosity_error_e) { diff --git a/sources/c/controller/controller.h b/sources/c/controller/controller.h index a1e4fd6..000a2b6 100644 --- a/sources/c/controller/controller.h +++ b/sources/c/controller/controller.h @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -67,15 +68,19 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include +#include #include #include +#include #include #include #include @@ -106,6 +111,8 @@ extern "C" { * @param main * The main program data and settings. * + * Must not be NULL. + * * This alters main.setting.state.status: * F_okay on success. * F_true on success when performing verification and verify passed. @@ -113,9 +120,13 @@ extern "C" { * F_interrupt on (exit) signal received. * * F_parameter (with error bit) if main is NULL or setting is NULL. + * @param process + * A pointer to the current process settings. + * + * Must not be NULL. */ #ifndef _di_controller_controller_main_ - extern void controller_controller_main(controller_main_t * const main); + extern void controller_controller_main(controller_main_t * const main, controller_process_t * const process); #endif // _di_controller_controller_main_ #ifdef __cplusplus diff --git a/sources/c/controller/main.c b/sources/c/controller/main.c index 4483d44..01ef621 100644 --- a/sources/c/controller/main.c +++ b/sources/c/controller/main.c @@ -3,6 +3,7 @@ int main(const int argc, const f_string_t *argv, const f_string_t *envp) { controller_main_t data = controller_main_t_initialize; + controller_process_t process = controller_process_t_initialize; data.program.debug.flag |= controller_print_flag_debug_e | controller_print_flag_out_e; data.program.error.flag |= controller_print_flag_error_e | controller_print_flag_out_e; @@ -33,10 +34,10 @@ int main(const int argc, const f_string_t *argv, const f_string_t *envp) { { const f_console_arguments_t arguments = macro_f_console_arguments_t_initialize_1(argc, argv, envp); - controller_main_setting_load(arguments, &data); + controller_main_setting_load(arguments, &data, &process); } - controller_controller_main(&data); + controller_controller_main(&data, &process); #else { f_thread_id_t id_signal; @@ -52,11 +53,11 @@ int main(const int argc, const f_string_t *argv, const f_string_t *envp) { { const f_console_arguments_t arguments = macro_f_console_arguments_t_initialize_1(argc, argv, envp); - controller_main_setting_load(arguments, &data); + controller_main_setting_load(arguments, &data, &process); } if (!controller_main_signal_check(&data)) { - controller_controller_main(&data); + controller_controller_main(&data, &process); } f_thread_cancel(id_signal); diff --git a/sources/c/init/init.h b/sources/c/init/init.h index 5717cf2..f47ded6 100644 --- a/sources/c/init/init.h +++ b/sources/c/init/init.h @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -67,15 +68,19 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include +#include #include #include +#include #include #include #include diff --git a/sources/c/init/main.c b/sources/c/init/main.c index 1aff3da..aeee890 100644 --- a/sources/c/init/main.c +++ b/sources/c/init/main.c @@ -3,6 +3,7 @@ int main(const int argc, const f_string_t *argv, const f_string_t *envp) { controller_main_t data = controller_main_t_initialize; + controller_process_t process = controller_process_t_initialize; data.program.debug.flag |= controller_print_flag_debug_e | controller_print_flag_out_e; data.program.error.flag |= controller_print_flag_error_e | controller_print_flag_out_e; @@ -33,7 +34,7 @@ int main(const int argc, const f_string_t *argv, const f_string_t *envp) { { const f_console_arguments_t arguments = macro_f_console_arguments_t_initialize_1(argc, argv, envp); - controller_main_setting_load(arguments, &data); + controller_main_setting_load(arguments, &data, &process); } controller_init_main(&data); @@ -52,7 +53,7 @@ int main(const int argc, const f_string_t *argv, const f_string_t *envp) { { const f_console_arguments_t arguments = macro_f_console_arguments_t_initialize_1(argc, argv, envp); - controller_main_setting_load(arguments, &data); + controller_main_setting_load(arguments, &data, &process); } if (!controller_main_signal_check(&data)) { diff --git a/sources/c/main/common.c b/sources/c/main/common.c index 8c1f8a3..cadd124 100644 --- a/sources/c/main/common.c +++ b/sources/c/main/common.c @@ -5,7 +5,7 @@ extern "C" { #endif #ifndef _di_controller_main_setting_load_ - void controller_main_setting_load(const f_console_arguments_t arguments, controller_main_t * const main) { + void controller_main_setting_load(const f_console_arguments_t arguments, controller_main_t * const main, controller_process_t * const process) { if (!main) return; @@ -87,6 +87,39 @@ extern "C" { else { main->setting.flag &= ~controller_main_flag_pipe_e; } + + f_string_static_t * const args = main->program.parameters.arguments.array; + + process->control.server.domain = f_socket_protocol_family_local_e; + process->control.server.type = f_socket_type_stream_e; + process->control.server.form = f_socket_address_form_local_e; + + memset(&process->control.server.address, 0, sizeof(f_socket_address_t)); + + // The first remaining argument represents the entry name. + main->setting.state.status = f_string_dynamic_append(main->program.parameters.remaining.used ? args[main->program.parameters.remaining.array[0]] : controller_default_s, &process->name_entry); + + if (F_status_is_error(main->setting.state.status)) { + if ((main->setting.flag & controller_main_flag_print_first_e) && main->program.message.verbosity > f_console_verbosity_error_e) { + fll_print_dynamic_raw(f_string_eol_s, main->program.message.to); + } + + controller_main_print_error(&main->program.error, macro_controller_f(fll_program_parameter_process_verbosity_standard)); + + return; + } + + main->setting.state.status = f_path_current(F_false, &process->path_current); + + if (F_status_is_error(main->setting.state.status)) { + if ((main->setting.flag & controller_main_flag_print_first_e) && main->program.message.verbosity > f_console_verbosity_error_e) { + fll_print_dynamic_raw(f_string_eol_s, main->program.message.to); + } + + controller_main_print_error(&main->program.error, macro_controller_f(f_path_current)); + + return; + } } #endif // _di_controller_main_setting_load_ diff --git a/sources/c/main/common.h b/sources/c/main/common.h index 9d1a594..1ed8582 100644 --- a/sources/c/main/common.h +++ b/sources/c/main/common.h @@ -33,12 +33,16 @@ extern "C" { * * Errors (with error bit) from: f_console_parameter_process(). * Errors (with error bit) from: fll_program_parameter_process_context(). + * @param process + * A pointer to the current process settings. + * + * Must not be NULL. * * @see f_console_parameter_process() * @see fll_program_parameter_process_context() */ #ifndef _di_controller_main_setting_load_ - extern void controller_main_setting_load(const f_console_arguments_t arguments, controller_main_t * const main); + extern void controller_main_setting_load(const f_console_arguments_t arguments, controller_main_t * const main, controller_process_t * const process); #endif // _di_controller_main_setting_load_ #ifdef __cplusplus diff --git a/sources/c/main/common/define/thread.h b/sources/c/main/common/define/thread.h new file mode 100644 index 0000000..0a72c81 --- /dev/null +++ b/sources/c/main/common/define/thread.h @@ -0,0 +1,62 @@ +/** + * FLL - Level 3 + * + * Project: Controller + * API Version: 0.7 + * Licenses: lgpl-2.1-or-later + * + * Provides the common thread defines. + * + * This is auto-included and should not need to be explicitly included. + */ +#ifndef _controller_main_common_define_thread_h +#define _controller_main_common_define_thread_h + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Thread related defines. + */ +#ifndef _di_controller_thread_d_ + #define controller_thread_cleanup_interval_long_d 3600 // 1 hour in seconds. + #define controller_thread_cleanup_interval_short_d 180 // 3 minutes in seconds. + #define controller_thread_exit_timeout_d 500 // 0.5 seconds in milliseconds. + #define controller_thread_exit_process_cancel_wait_d 600000000 // 0.6 seconds in nanoseconds. + #define controller_thread_exit_process_cancel_total_d 150 // 90 seconds in multiples of wait. + #define controller_thread_simulation_timeout_d 200 // 0.2 seconds in milliseconds. + + #define controller_thread_signal_wait_timeout_seconds_d 70 + #define controller_thread_signal_wait_timeout_nanoseconds_d 0 + + #define controller_thread_lock_read_timeout_seconds_d 3 + #define controller_thread_lock_read_timeout_nanoseconds_d 0 + #define controller_thread_lock_write_timeout_seconds_d 3 + #define controller_thread_lock_write_timeout_nanoseconds_d 0 + + #define controller_thread_wait_timeout_1_before_d 4 + #define controller_thread_wait_timeout_2_before_d 12 + #define controller_thread_wait_timeout_3_before_d 28 + + #define controller_thread_wait_timeout_1_seconds_d 0 + #define controller_thread_wait_timeout_1_nanoseconds_d 20000000 // 0.02 seconds in nanoseconds. + #define controller_thread_wait_timeout_2_seconds_d 0 + #define controller_thread_wait_timeout_2_nanoseconds_d 200000000 // 0.2 seconds in nanoseconds. + #define controller_thread_wait_timeout_3_seconds_d 2 + #define controller_thread_wait_timeout_3_nanoseconds_d 0 + #define controller_thread_wait_timeout_4_seconds_d 20 + #define controller_thread_wait_timeout_4_nanoseconds_d 0 + + #define controller_thread_exit_helper_timeout_seconds_d 0 + #define controller_thread_exit_helper_timeout_nanoseconds_d 100000000 // 0.1 seconds in nanoseconds. + + #define controller_thread_exit_ready_timeout_seconds_d 0 + #define controller_thread_exit_ready_timeout_nanoseconds_d 500000000 // 0.5 seconds in nanoseconds. +#endif // _di_controller_thread_d_ + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _controller_main_common_define_thread_h diff --git a/sources/c/main/common/enumeration/thread.h b/sources/c/main/common/enumeration/thread.h new file mode 100644 index 0000000..5be6ec6 --- /dev/null +++ b/sources/c/main/common/enumeration/thread.h @@ -0,0 +1,61 @@ +/** + * FLL - Level 3 + * + * Project: Controller + * API Version: 0.7 + * Licenses: lgpl-2.1-or-later + * + * Provides the common thread enumerations. + * + * This is auto-included and should not need to be explicitly included. + */ +#ifndef _controller_main_common_enumeration_thread_h +#define _controller_main_common_enumeration_thread_h + +#ifdef __cplusplus +extern "C" { +#endif + + /** + * States for the thread, designating how to stop the process. + * + * controller_thread_*_e: + * - enabled_not: The controller is no longer enabled, shut down and abort all work. + * - enabled: The controller is operating normally. + * - enabled_execute: The controller is executing another process, all running operations must terminate. + * - enabled_exit: The controller is shutting down, only process exit rules. + * - enabled_exit_execute: The controller is executing another process while in failsafe mode, all running operations must terminate. + * - enabled_exit_ready: The controller is shutting down, only process exit rules, and now ready to send termination signals. + */ + enum { + controller_thread_enabled_not_e = 0, + controller_thread_enabled_e, + controller_thread_enabled_execute_e, + controller_thread_enabled_exit_e, + controller_thread_enabled_exit_execute_e, + controller_thread_enabled_exit_ready_e, + }; // enum + + /** + * States for the thread, designating how the process is cancelled. + * + * controller_thread_cancel_*_e: + * - signal: Cancellation is triggered by a signal. + * - call: Cancellation is explicitly called. + * - execute: Cancellation is explicitly called due to an "execute" Item Action, when not during Exit. + * - exit: Cancellation is explicitly called during Exit. + * - exit_execute: Cancellation is explicitly called due to an "execute" Item Action during Exit. + */ + enum { + controller_thread_cancel_signal_e = 0, + controller_thread_cancel_call_e, + controller_thread_cancel_execute_e, + controller_thread_cancel_exit_e, + controller_thread_cancel_exit_execute_e, + }; // enum + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _controller_main_common_enumeration_thread_h diff --git a/sources/c/main/common/print.c b/sources/c/main/common/print.c index 484125e..a259bf2 100644 --- a/sources/c/main/common/print.c +++ b/sources/c/main/common/print.c @@ -7,6 +7,8 @@ extern "C" { #ifndef _di_controller_f_a_ const f_string_t controller_f_a[] = { "f_console_parameter_process", + "f_path_current", + "f_string_dynamic_append", "f_thread_create", "fll_program_parameter_process_context_standard", "fll_program_parameter_process_verbosity_standard", diff --git a/sources/c/main/common/print.h b/sources/c/main/common/print.h index 3d48967..df22450 100644 --- a/sources/c/main/common/print.h +++ b/sources/c/main/common/print.h @@ -40,6 +40,8 @@ extern "C" { #ifndef _di_controller_f_e_ enum { controller_f_f_console_parameter_process_e, + controller_f_f_path_current_e, + controller_f_f_string_dynamic_append_e, controller_f_f_thread_create_e, controller_f_fll_program_parameter_process_context_standard_e, controller_f_fll_program_parameter_process_verbosity_standard_e, diff --git a/sources/c/main/common/type/cache.h b/sources/c/main/common/type/cache.h index 9833033..342155f 100644 --- a/sources/c/main/common/type/cache.h +++ b/sources/c/main/common/type/cache.h @@ -5,7 +5,7 @@ * API Version: 0.7 * Licenses: lgpl-2.1-or-later * - * Provides the common type cache structures. + * Provides the common cache type structures. * * This is auto-included and should not need to be explicitly included. */ diff --git a/sources/c/main/common/type/control.h b/sources/c/main/common/type/control.h index 23aa30d..17fbf40 100644 --- a/sources/c/main/common/type/control.h +++ b/sources/c/main/common/type/control.h @@ -5,7 +5,7 @@ * API Version: 0.7 * Licenses: lgpl-2.1-or-later * - * Provides the common type control structures. + * Provides the common control type structures. * * This is auto-included and should not need to be explicitly included. */ @@ -19,23 +19,24 @@ extern "C" { /** * Controller control data. * - * flag: Flags from controller_control_flag_*_e. - * user: The user ID, if specified. - * group: The group ID, if specified. - * mode: The file mode, if specified. - * server: The server socket connection. - * client: The client socket connection. + * flag: Flags from controller_control_flag_*_e. + * user: The user ID, if specified. + * group: The group ID, if specified. + * mode: The file mode, if specified. + * + * server: The server socket connection. + * client: The client socket connection. + * * cache_1: A generic buffer used for caching control related data. * cache_2: A generic buffer used for caching control related data. * cache_3: A generic buffer used for caching control related data. + * * input: A buffer used for receiving data from the client. * output: A buffer used for transmitting data to the client. - * address: The socket address structure. */ #ifndef _di_controller_control_t_ typedef struct { uint8_t flag; - uid_t user; gid_t group; mode_t mode; @@ -49,8 +50,6 @@ extern "C" { f_string_dynamic_t input; f_string_dynamic_t output; - - struct sockaddr_un address; } controller_control_t; #define controller_control_t_initialize { \ @@ -65,7 +64,6 @@ extern "C" { f_string_dynamic_t_initialize, \ f_string_dynamic_t_initialize, \ f_string_dynamic_t_initialize, \ - { }, \ } #endif // _di_controller_control_t_ diff --git a/sources/c/main/common/type/entry.h b/sources/c/main/common/type/entry.h index c966a05..e4874da 100644 --- a/sources/c/main/common/type/entry.h +++ b/sources/c/main/common/type/entry.h @@ -5,7 +5,7 @@ * API Version: 0.7 * Licenses: lgpl-2.1-or-later * - * Provides the common type entry structures. + * Provides the common entry type structures. * * This is auto-included and should not need to be explicitly included. */ diff --git a/sources/c/main/common/type/lock.c b/sources/c/main/common/type/lock.c new file mode 100644 index 0000000..563cf6e --- /dev/null +++ b/sources/c/main/common/type/lock.c @@ -0,0 +1,59 @@ +#include "../../controller.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _di_controller_lock_delete_mutex_ + void controller_lock_delete_mutex(f_thread_mutex_t *mutex) { + + const f_status_t status = f_thread_mutex_delete(mutex); + + if (F_status_is_error(status)) { + if (F_status_set_fine(status) == F_busy) { + if (f_thread_mutex_delete(mutex) == F_okay) { + mutex = 0; + } + } + } + else { + mutex = 0; + } + } +#endif // _di_controller_lock_delete_mutex_ + +#ifndef _di_controller_lock_delete_rw_ + void controller_lock_delete_rw(f_thread_lock_t *lock) { + + const f_status_t status = f_thread_lock_delete(lock); + + if (F_status_is_error(status)) { + if (F_status_set_fine(status) == F_busy) { + if (f_thread_lock_delete(lock) == F_okay) { + lock = 0; + } + } + } + else { + lock = 0; + } + } +#endif // _di_controller_lock_delete_rw_ + +#ifndef _di_controller_lock_delete_ + void controller_lock_delete(controller_lock_t * const lock) { + + controller_lock_delete_mutex(&lock->alert); + controller_lock_delete_mutex(&lock->cancel); + controller_lock_delete_mutex(&lock->print); + + controller_lock_delete_rw(&lock->process); + controller_lock_delete_rw(&lock->rule); + + f_thread_condition_delete(&lock->alert_condition); + } +#endif // _di_controller_lock_delete_ + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/sources/c/main/common/type/lock.h b/sources/c/main/common/type/lock.h new file mode 100644 index 0000000..9bd5a90 --- /dev/null +++ b/sources/c/main/common/type/lock.h @@ -0,0 +1,103 @@ +/** + * FLL - Level 3 + * + * Project: Controller + * API Version: 0.7 + * Licenses: lgpl-2.1-or-later + * + * Provides the common lock type structures. + * + * This is auto-included and should not need to be explicitly included. + */ +#ifndef _controller_main_common_type_lock_h +#define _controller_main_common_type_lock_h + +#ifdef __cplusplus +extern "C" { +#endif +/** + * A structure for sharing mutexes globally between different threads. + * + * The alert lock is intended for a generic waiting on alerts operations. + * The cancel lock is intended for preventing double cancellation calls (which can happen due to interrupts). + * The print lock is intended to lock any activity printing to stdout/stderr. + * The process lock is intended to lock any activity on the process structure. + * The rule lock is intended to lock any activity on the rules structure. + * + * alert: The alert mutex lock for waking up on alerts. + * cancel: The cancel mutex lock for locking the cancel operation. + * print: The print mutex lock. + * process: The process r/w lock. + * rule: The rule r/w lock. + * alert_condition: The condition used to trigger alerts. + */ +#ifndef _di_controller_lock_t_ + typedef struct { + f_thread_mutex_t alert; + f_thread_mutex_t cancel; + f_thread_mutex_t print; + + f_thread_lock_t process; + f_thread_lock_t rule; + + f_thread_condition_t alert_condition; + } controller_lock_t; + + #define controller_lock_t_initialize { \ + f_thread_mutex_t_initialize, \ + f_thread_mutex_t_initialize, \ + f_thread_mutex_t_initialize, \ + f_thread_lock_t_initialize, \ + f_thread_lock_t_initialize, \ + f_thread_condition_t_initialize, \ + } +#endif // _di_controller_lock_t_ + +/** + * Delete the mutex lock and if the mutex lock is busy, forcibly unlock it and then delete it. + * + * @param mutex + * The mutex lock to delete. + * Will be set to NULL if delete succeeded. + * + * This pointer cannot be "* const" because of pthread_mutex_destroy(). + * + * @see f_thread_mutex_delete() + */ +#ifndef _di_controller_lock_delete_mutex_ + extern void controller_lock_delete_mutex(f_thread_mutex_t *mutex); +#endif // _di_controller_lock_delete_mutex_ + +/** + * Delete the r/w lock and if the r/w lock is busy, forcibly unlock it and then delete it. + * + * @param lock + * The r/w lock to delete. + * Will be set to NULL if delete succeeded. + * + * This pointer cannot be "* const" because of pthread_rwlock_destroy(). + * + * @see f_thread_lock_delete() + */ +#ifndef _di_controller_lock_delete_rw_ + extern void controller_lock_delete_rw(f_thread_lock_t *lock); +#endif // _di_controller_lock_delete_rw_ + +/** + * Fully deallocate all memory for the given lock without caring about return status. + * + * @param lock + * The lock to deallocate. + * + * @see f_thread_lock_delete() + * @see f_thread_mutex_delete() + */ +#ifndef _di_controller_lock_delete_ + extern void controller_lock_delete(controller_lock_t * const lock); +#endif // _di_controller_lock_delete_ + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _controller_main_common_type_lock_h diff --git a/sources/c/main/common/type/process.c b/sources/c/main/common/type/process.c index 7289a1d..f2b4b00 100644 --- a/sources/c/main/common/type/process.c +++ b/sources/c/main/common/type/process.c @@ -16,13 +16,12 @@ extern "C" { f_memory_array_resize(0, sizeof(f_char_t), (void **) &process->path_pid.string, &process->path_pid.used, &process->path_pid.size); f_memory_array_resize(0, sizeof(f_char_t), (void **) &process->path_setting.string, &process->path_setting.used, &process->path_setting.size); - controller_control_delete(&process->control); - f_memory_arrays_resize(0, sizeof(f_string_map_t), (void **) &process->entry.define.array, &process->entry.define.used, &process->entry.define.size, &f_string_maps_delete_callback); f_memory_arrays_resize(0, sizeof(f_string_map_t), (void **) &process->entry.parameter.array, &process->entry.parameter.used, &process->entry.parameter.size, &f_string_maps_delete_callback); f_memory_arrays_resize(0, sizeof(f_string_map_t), (void **) &process->exit.define.array, &process->exit.define.used, &process->exit.define.size, &f_string_maps_delete_callback); f_memory_arrays_resize(0, sizeof(f_string_map_t), (void **) &process->exit.parameter.array, &process->exit.parameter.used, &process->exit.parameter.size, &f_string_maps_delete_callback); + controller_control_delete(&process->control); controller_entry_items_delete(&process->entry.items); controller_entry_items_delete(&process->exit.items); diff --git a/sources/c/main/common/type/process.h b/sources/c/main/common/type/process.h index ea96a63..170d610 100644 --- a/sources/c/main/common/type/process.h +++ b/sources/c/main/common/type/process.h @@ -5,7 +5,7 @@ * API Version: 0.7 * Licenses: lgpl-2.1-or-later * - * Provides the common type process structures. + * Provides the common process type structures. * * This is auto-included and should not need to be explicitly included. */ @@ -19,16 +19,22 @@ extern "C" { /** * Controller process data. * - * ready: State representing if the settings are all loaded and is ready to run program operations. - * mode: Controller setting mode based on the setting mode enumerator. - * control: The control socket data. + * ready: State representing if the settings are all loaded and is ready to run program operations. + * mode: Controller setting mode based on the setting mode enumerator. + * * failsafe_item_id: The Entry Item ID to execute when failsafe execution is enabled. - * path_cgroup: Directory path to the cgroup directory. - * path_control: File path to the control socket (used for printing the path). - * path_pid: File path to the PID file. - * path_setting: File path to the setting directory. - * entry: The Entry settings. - * rules: All rules and their respective settings. + * + * name_entry: The name of the entry file. + * path_cgroup: Directory path to the cgroup directory. + * path_control: File path to the control socket (used for printing the path). + * path_current: The current path. + * path_pid: File path to the PID file. + * path_setting: File path to the setting directory. + * + * control: The control socket data. + * entry: The Entry settings. + * exit: The Exit settings. + * rules: All rules and their respective settings. */ #ifndef _di_controller_process_t_ typedef struct { @@ -37,16 +43,14 @@ extern "C" { f_number_unsigned_t failsafe_item_id; - controller_control_t control; - + f_string_dynamic_t name_entry; f_string_dynamic_t path_cgroup; f_string_dynamic_t path_control; f_string_dynamic_t path_current; f_string_dynamic_t path_pid; f_string_dynamic_t path_setting; - f_string_dynamic_t name_entry; - + controller_control_t control; controller_entry_t entry; controller_entry_t exit; controller_rules_t rules; @@ -56,14 +60,13 @@ extern "C" { 0, \ 0, \ 0, \ - 0, \ - controller_control_t_initialize, \ f_string_dynamic_t_initialize, \ f_string_dynamic_t_initialize, \ f_string_dynamic_t_initialize, \ f_string_dynamic_t_initialize, \ f_string_dynamic_t_initialize, \ f_string_dynamic_t_initialize, \ + controller_control_t_initialize, \ controller_entry_t_initialize, \ controller_entry_t_initialize, \ controller_rules_t_initialize, \ diff --git a/sources/c/main/common/type/rule.h b/sources/c/main/common/type/rule.h index 13fb4c0..2ffcbac 100644 --- a/sources/c/main/common/type/rule.h +++ b/sources/c/main/common/type/rule.h @@ -5,7 +5,7 @@ * API Version: 0.7 * Licenses: lgpl-2.1-or-later * - * Provides the common type rules structures. + * Provides the common rules type structures. * * This is auto-included and should not need to be explicitly included. */ diff --git a/sources/c/main/common/type/thread.c b/sources/c/main/common/type/thread.c new file mode 100644 index 0000000..00cf7b6 --- /dev/null +++ b/sources/c/main/common/type/thread.c @@ -0,0 +1,18 @@ +#include "../../controller.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _di_controller_thread_delete_simple_ + void controller_thread_delete_simple(controller_thread_t * const thread) { + + controller_lock_delete(&thread->lock); + controller_process_delete(&thread->process); + controller_cache_delete(&thread->cache); + } +#endif // _di_controller_thread_delete_simple_ + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/sources/c/main/common/type/thread.h b/sources/c/main/common/type/thread.h new file mode 100644 index 0000000..41c2fc7 --- /dev/null +++ b/sources/c/main/common/type/thread.h @@ -0,0 +1,89 @@ +/** + * FLL - Level 3 + * + * Project: Controller + * API Version: 0.7 + * Licenses: lgpl-2.1-or-later + * + * Provides the common thread type structures. + * + * This is auto-included and should not need to be explicitly included. + */ +#ifndef _controller_main_common_type_thread_h +#define _controller_main_common_type_thread_h + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * A structure for managing threads. + * + * This is essentially data shared globally between threads, about threads. + * + * The "enabled" and "signal" utilize the lock: lock.process. + * + * enabled: TRUE when threads are active, FALSE when inactive and the program is essentially shutting down, no new threads should be started when FALSE. + * signal: The code of any signal received. + * status: A status used by the main entry/rule processing thread for synchronous operations. + * + * id_cleanup: The thread ID representing the Cleanup Process. + * id_control: The thread ID representing the Control Process. + * id_entry: The thread ID representing the Entry or Exit Process. + * id_rule: The thread ID representing the Rule Process. + * id_signal: The thread ID representing the Signal Process. + * + * lock: A r/w lock for operating on this structure. + * process: All Rule Process thread data. + * cache: A cache used by the main entry/rule processing thread for synchronous operations. + */ +#ifndef _di_controller_thread_t_ + typedef struct { + uint8_t enabled; + int signal; + f_status_t status; + + f_thread_id_t id_cleanup; + f_thread_id_t id_control; + f_thread_id_t id_entry; + f_thread_id_t id_rule; + f_thread_id_t id_signal; + + controller_lock_t lock; + controller_process_t process; + controller_cache_t cache; + } controller_thread_t; + + #define controller_thread_t_initialize { \ + controller_thread_enabled_e, \ + 0, \ + F_none, \ + f_thread_id_t_initialize, \ + f_thread_id_t_initialize, \ + f_thread_id_t_initialize, \ + f_thread_id_t_initialize, \ + f_thread_id_t_initialize, \ + controller_lock_t_initialize, \ + controller_processs_t_initialize, \ + controller_cache_t_initialize, \ + } +#endif // _di_controller_thread_t_ + +/** + * Fully deallocate all memory for the given setting without caring about return status. + * + * @param thread + * The thread to deallocate. + * + * @see controller_asynchronouss_resize() + * @see f_thread_mutex_unlock() + */ +#ifndef _di_controller_thread_delete_simple_ + extern void controller_thread_delete_simple(controller_thread_t * const thread); +#endif // _di_controller_thread_delete_simple_ + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _controller_main_common_type_thread_h diff --git a/sources/c/main/controller.h b/sources/c/main/controller.h index f710390..73de6cc 100644 --- a/sources/c/main/controller.h +++ b/sources/c/main/controller.h @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -67,15 +68,19 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include +#include #include #include +#include #include #include #include -- 1.8.3.1