From 4bcd851a96a155736fa20a1932ba6dd91a54f19b Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Thu, 9 Sep 2021 20:47:48 -0500 Subject: [PATCH] Feature: Allow controller program to operate directly as "init program" by default. The controller program already supports operating as an "init" program. The design is generic enough to do this and a parameter exists already to explicitly set paths. When running as "init" under normally circumstances, parameters might not be passed to it. This is a good argument to support operating natively as an "init" without requiring the init parameter. The design of the FLL programs allows them to be used as a library. Take advantage of this and rewrite some of the code to save the "init" related differences in the main.c file. The main.c file is not shared in the library and is therefore a safe candidate for holding differences. This results in two programs "controller" and "init" which use the same library. A stripped binary size of these two programs is each ~23K. The stripped library shared between them is ~235K. Having both programs on the system therefore only costs and additional ~23k. To make this easy, provide an "as_init" mode that can be added to compile in this mode. A compilation as "controller" might look like: - "fake clean build -m monolithic" A compilation as "init" might look like: - "fake clean build -m monolithic -m as_init" In both cases the program is built as "controller" under "build/programs/shared/controller". When compiling "as_init", just rename this program when installing, such as: - "cp -v build/programs/shared/controller /sbin/init". The libraries and headers need only be installed once between the two programs. This situation is a great example of the design principles of this project in use and working well. --- level_3/controller/c/controller.c | 55 ++++++++++++++++++---------------- level_3/controller/c/controller.h | 22 +++++++++----- level_3/controller/c/main.c | 12 ++++++++ level_3/controller/data/build/defines | 1 + level_3/controller/data/build/settings | 3 +- 5 files changed, 59 insertions(+), 34 deletions(-) diff --git a/level_3/controller/c/controller.c b/level_3/controller/c/controller.c index 42a58da..df7b1ef 100644 --- a/level_3/controller/c/controller.c +++ b/level_3/controller/c/controller.c @@ -11,39 +11,39 @@ extern "C" { #endif #ifndef _di_controller_print_help_ - f_status_t controller_print_help(const f_file_t output, const f_color_context_t context) { + f_status_t controller_print_help(const controller_main_t main) { - flockfile(output.stream); + flockfile(main.output.stream); - fll_program_print_help_header(output, context, controller_name_long, controller_version); + fll_program_print_help_header(main.output, main.context, main.program_name_long, controller_version); - fll_program_print_help_option(output, context, f_console_standard_short_help_s, f_console_standard_long_help_s, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Print this help message."); - fll_program_print_help_option(output, context, f_console_standard_short_dark_s, f_console_standard_long_dark_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Output using colors that show up better on dark backgrounds."); - fll_program_print_help_option(output, context, f_console_standard_short_light_s, f_console_standard_long_light_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Output using colors that show up better on light backgrounds."); - fll_program_print_help_option(output, context, f_console_standard_short_no_color_s, f_console_standard_long_no_color_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, "Do not output in color."); - fll_program_print_help_option(output, context, f_console_standard_short_quiet_s, f_console_standard_long_quiet_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Decrease verbosity beyond normal output."); - fll_program_print_help_option(output, context, f_console_standard_short_normal_s, f_console_standard_long_normal_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Set verbosity to normal output."); - fll_program_print_help_option(output, context, f_console_standard_short_verbose_s, f_console_standard_long_verbose_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Increase verbosity beyond normal output."); - fll_program_print_help_option(output, context, f_console_standard_short_debug_s, f_console_standard_long_debug_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Enable debugging, inceasing verbosity beyond normal output."); - fll_program_print_help_option(output, context, f_console_standard_short_version_s, f_console_standard_long_version_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Print only the version number."); + fll_program_print_help_option(main.output, main.context, f_console_standard_short_help_s, f_console_standard_long_help_s, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Print this help message."); + fll_program_print_help_option(main.output, main.context, f_console_standard_short_dark_s, f_console_standard_long_dark_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Output using colors that show up better on dark backgrounds."); + fll_program_print_help_option(main.output, main.context, f_console_standard_short_light_s, f_console_standard_long_light_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Output using colors that show up better on light backgrounds."); + fll_program_print_help_option(main.output, main.context, f_console_standard_short_no_color_s, f_console_standard_long_no_color_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, "Do not main.output in color."); + fll_program_print_help_option(main.output, main.context, f_console_standard_short_quiet_s, f_console_standard_long_quiet_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Decrease verbosity beyond normal main.output."); + fll_program_print_help_option(main.output, main.context, f_console_standard_short_normal_s, f_console_standard_long_normal_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Set verbosity to normal main.output."); + fll_program_print_help_option(main.output, main.context, f_console_standard_short_verbose_s, f_console_standard_long_verbose_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Increase verbosity beyond normal main.output."); + fll_program_print_help_option(main.output, main.context, f_console_standard_short_debug_s, f_console_standard_long_debug_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Enable debugging, inceasing verbosity beyond normal main.output."); + fll_program_print_help_option(main.output, main.context, f_console_standard_short_version_s, f_console_standard_long_version_s, f_console_symbol_short_disable_s, f_console_symbol_long_disable_s, " Print only the version number."); - f_print_character(f_string_eol_s[0], output.stream); + f_print_character(f_string_eol_s[0], main.output.stream); - fll_program_print_help_option(output, context, controller_short_control, controller_long_control, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Specify a custom control group file path, such as '" f_control_group_path_system_prefix f_control_group_path_system_default "'."); - fll_program_print_help_option(output, context, controller_short_daemon, controller_long_daemon, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Run in daemon only mode (do not process the entry)."); - fll_program_print_help_option(output, context, controller_short_init, controller_long_init, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " The program will run as an init replacement."); - fll_program_print_help_option(output, context, controller_short_interruptable, controller_long_interruptable, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, "Designate that this program can be interrupted."); - fll_program_print_help_option(output, context, controller_short_pid, controller_long_pid, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Specify a custom pid file path, such as '" controller_path_pid controller_string_default controller_path_suffix "'."); - fll_program_print_help_option(output, context, controller_short_settings, controller_long_settings, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Specify a custom settings path, such as '" controller_path_settings "'."); - fll_program_print_help_option(output, context, controller_short_simulate, controller_long_simulate, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Run as a simulation."); - fll_program_print_help_option(output, context, controller_short_validate, controller_long_validate, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Validate the settings (entry and rules) without running (does not simulate)."); + fll_program_print_help_option(main.output, main.context, controller_short_control, controller_long_control, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Specify a custom control group file path, such as '" f_control_group_path_system_prefix f_control_group_path_system_default "'."); + fll_program_print_help_option(main.output, main.context, controller_short_daemon, controller_long_daemon, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Run in daemon only mode (do not process the entry)."); + fll_program_print_help_option(main.output, main.context, controller_short_init, controller_long_init, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " The program will run as an init replacement."); + fll_program_print_help_option(main.output, main.context, controller_short_interruptable, controller_long_interruptable, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, "Designate that this program can be interrupted."); + fll_program_print_help_option(main.output, main.context, controller_short_pid, controller_long_pid, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Specify a custom pid file path, such as '" controller_path_pid controller_string_default controller_path_suffix "'."); + fll_program_print_help_option(main.output, main.context, controller_short_settings, controller_long_settings, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Specify a custom settings path, such as '" controller_path_settings "'."); + fll_program_print_help_option(main.output, main.context, controller_short_simulate, controller_long_simulate, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Run as a simulation."); + fll_program_print_help_option(main.output, main.context, controller_short_validate, controller_long_validate, f_console_symbol_short_enable_s, f_console_symbol_long_enable_s, " Validate the settings (entry and rules) without running (does not simulate)."); - fll_program_print_help_usage(output, context, controller_name, "entry"); + fll_program_print_help_usage(main.output, main.context, main.program_name, "entry"); - fl_print_format(" When both the %[%s%s%] parameter and the", output.stream, context.set.notable, f_console_symbol_long_enable_s, controller_long_simulate, context.set.notable); - fl_print_format(" %[%s%s%] parameter are specified, then additional information on each would be executed rule is printed but no simulation is performed.%c%c", output.stream, context.set.notable, f_console_symbol_long_enable_s, controller_long_validate, context.set.notable, f_string_eol_s[0], f_string_eol_s[0]); + fl_print_format(" When both the %[%s%s%] parameter and the", main.output.stream, main.context.set.notable, f_console_symbol_long_enable_s, controller_long_simulate, main.context.set.notable); + fl_print_format(" %[%s%s%] parameter are specified, then additional information on each would be executed rule is printed but no simulation is performed.%c%c", main.output.stream, main.context.set.notable, f_console_symbol_long_enable_s, controller_long_validate, main.context.set.notable, f_string_eol_s[0], f_string_eol_s[0]); - funlockfile(output.stream); + funlockfile(main.output.stream); return F_none; } @@ -122,7 +122,7 @@ extern "C" { } if (main->parameters[controller_parameter_help].result == f_console_result_found) { - controller_print_help(main->output, main->context); + controller_print_help(*main); controller_main_delete(main); return F_none; @@ -190,6 +190,9 @@ extern "C" { if (main->parameters[controller_parameter_init].result == f_console_result_found) { status = f_string_append(controller_path_settings_init, controller_path_settings_init_length, &setting.path_setting); } + else if (main->setting_default.used) { + status = f_string_append(main->setting_default.string, main->setting_default.used, &setting.path_setting); + } else { status = f_string_append(controller_path_settings, controller_path_settings_length, &setting.path_setting); } diff --git a/level_3/controller/c/controller.h b/level_3/controller/c/controller.h index f80cdb7..a390a49 100644 --- a/level_3/controller/c/controller.h +++ b/level_3/controller/c/controller.h @@ -103,6 +103,9 @@ extern "C" { #ifndef _di_controller_name_ #define controller_name "controller" #define controller_name_long "Controller Program" + + #define controller_name_init "init" + #define controller_name_init_long "Init Program" #endif // _di_controller_name_ #ifndef _di_controller_defines_ @@ -218,6 +221,10 @@ extern "C" { int child; f_signal_t signal; + f_string_t program_name; + f_string_t program_name_long; + f_string_static_t setting_default; + f_color_context_t context; } controller_main_t; @@ -233,6 +240,9 @@ extern "C" { 0, \ 0, \ f_signal_t_initialize, \ + f_string_t_initialize, \ + f_string_t_initialize, \ + f_string_static_t_initialize, \ f_color_context_t_initialize, \ } #endif // _di_controller_main_t_ @@ -240,16 +250,14 @@ extern "C" { /** * Print help. * - * @param output - * The file to print to. - * @param context - * The color context settings. + * @param main + * The program main data. * * @return * F_none on success. */ #ifndef _di_controller_print_help_ - extern f_status_t controller_print_help(const f_file_t output, const f_color_context_t context); + extern f_status_t controller_print_help(const controller_main_t main); #endif // _di_controller_print_help_ /** @@ -260,7 +268,7 @@ extern "C" { * @param arguments * The parameters passed to the process. * @param main - * The program main. + * The program main data. * * @return * F_none on success. @@ -279,7 +287,7 @@ extern "C" { * Be sure to call this after executing controller_main(). * * @param main - * The program main. + * The program main data. * * @return * F_none on success. diff --git a/level_3/controller/c/main.c b/level_3/controller/c/main.c index d39db7e..3f930bb 100644 --- a/level_3/controller/c/main.c +++ b/level_3/controller/c/main.c @@ -34,6 +34,18 @@ int main(const int argc, const f_string_t *argv) { // restore umask. umask(data.umask); + // when run as "init" by default, provide the default system-level init path. + // this change must only exist within this main file so that the change only exists within the program rather than the library. + #ifdef _controller_as_init_ + data.program_name = controller_name_init; + data.program_name_long = controller_name_init_long; + data.setting_default.string = controller_path_settings_init; + data.setting_default.used = controller_path_settings_init_length; + #else // _controller_as_init_ + data.program_name = controller_name; + data.program_name_long = controller_name_long; + #endif // _controller_as_init_ + status = controller_main(arguments, &data); // flush output pipes before closing. diff --git a/level_3/controller/data/build/defines b/level_3/controller/data/build/defines index 618e9b3..2d347d1 100644 --- a/level_3/controller/data/build/defines +++ b/level_3/controller/data/build/defines @@ -2,6 +2,7 @@ _di_libcap_ Disable libcap support, allow for compiling and linking without libcap (-lcap). _libcap_legacy_only_ Disable functionality provided by later versions of libcap (2.43 and later). +_controller_as_init_ Build the program to run as if it were "init" by default, including displaying the program name as "Init Program" or "Init". This changes the main.c file only, leaving the library shareable between both "controller" and "init". _override_controller_default_program_script_ Provide a custom script name string to execute (such as php). _override_controller_path_pid_init_ Provide a custom init path for the system rather than the default. _override_controller_path_pid_init_length_ The number of bytes representing the string in _override_controller_path_pid_init_ (not including the terminating NULL). diff --git a/level_3/controller/data/build/settings b/level_3/controller/data/build/settings index dd51646..3486eaf 100644 --- a/level_3/controller/data/build/settings +++ b/level_3/controller/data/build/settings @@ -13,7 +13,7 @@ environment process_pre process_post -modes individual level monolithic +modes individual level monolithic as_init modes_default monolithic build_compiler gcc @@ -49,6 +49,7 @@ search_static yes #defines_all -D_di_libcap_ -D_di_thread_support_ defines_all -D_libcap_legacy_only_ -D_pthread_attr_unsupported_ -D_pthread_sigqueue_unsupported_ +defines_all-as_init -D_controller_as_init_ defines_static defines_shared -- 1.8.3.1