]> Kevux Git Server - fll/commitdiff
Progress: controller program.
authorKevin Day <thekevinday@gmail.com>
Tue, 1 Dec 2020 05:05:10 +0000 (23:05 -0600)
committerKevin Day <thekevinday@gmail.com>
Tue, 1 Dec 2020 05:05:10 +0000 (23:05 -0600)
level_3/controller/c/controller.c
level_3/controller/c/controller.h
level_3/controller/c/main.c
level_3/controller/c/private-controller.c
level_3/controller/c/private-controller.h
level_3/controller/data/build/dependencies
level_3/controller/data/build/settings

index a26ab055e8852b25de6f7587f50b4f0c98f1ed61..ac4b91a22b0bdad445464daaf57f33644d013032 100644 (file)
@@ -229,6 +229,11 @@ extern "C" {
       }
     }
 
+    // @todo create pid file but not until "ready", so be sure to add this after pre-processing the entry file.
+    if (setting.ready) {
+      controller_file_pid_create(*data, setting.path_pid);
+    }
+
     if (F_status_is_error_not(status)) {
       if (data->parameters[controller_parameter_test].result == f_console_result_found || data->parameters[controller_parameter_validate].result == f_console_result_found) {
         // @todo validate happens first, report and handle validation problems or success.
@@ -250,9 +255,12 @@ extern "C" {
       }
     }
 
+    controller_file_pid_delete(*data, setting.path_pid);
+
     macro_controller_setting_t_delete_simple(setting);
     macro_controller_entry_cache_t_delete_simple(cache_entry);
     macro_controller_rule_cache_t_delete_simple(cache_rule);
+
     controller_delete_data(data);
 
     return status;
index 4036d7b0c67e7423fd768af36e86877439d36b06..6d97acf97a9c633151fb301e2602ac4ddd36128e 100644 (file)
  * This program can be controlled from user-space via the "control" program.
  *
  * @todo research containers and build in container support into this, providing "container" appropriate verbiage for individual rules.
+ * @todo research namespaces and user_namespaces, they may be important to support.
  */
 #ifndef _controller_h
 
 // libc includes
 #include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 // fll-0 includes
 #include <level_0/type.h>
@@ -68,10 +71,10 @@ extern "C" {
   // must be at least 2.
   #define controller_default_allocation_step 4
 
-  #define controller_path_pid      "/var/run/controller.pid"
+  #define controller_path_pid      "/var/run/controller/controller.pid"
   #define controller_path_settings "/etc/controller"
 
-  #define controller_path_pid_length      23
+  #define controller_path_pid_length      34
   #define controller_path_settings_length 15
 
   #define controller_short_interruptable "i"
@@ -131,6 +134,7 @@ extern "C" {
 
     f_string_lengths_t remaining;
     bool process_pipe;
+    pid_t pid;
 
     f_file_t output;
     fll_error_print_t error;
@@ -144,6 +148,7 @@ extern "C" {
       controller_console_parameter_t_initialize, \
       f_string_lengths_t_initialize, \
       F_false, \
+      0, \
       f_macro_file_t_initialize(f_type_output, f_type_descriptor_output, f_file_flag_write_only), \
       fll_error_print_t_initialize, \
       fll_macro_error_print_t_initialize_warning(), \
index a7833b5ee05547bff8be24487d7ebb29ba624148..653a1e4028e1e42306039349dece48277d887110 100644 (file)
@@ -8,6 +8,8 @@ int main(const unsigned long argc, const f_string_t *argv) {
     data.process_pipe = F_true;
   }
 
+  data.pid = getpid();
+
   if (F_status_is_error(controller_main(arguments, &data))) {
     return 1;
   }
index 2bfb625adcf23accbef91bd8e4cfb3c3972d535a..72d526cfb7a3c8b43ce89d9ef09ff269dcb7464e 100644 (file)
@@ -82,6 +82,93 @@ extern "C" {
   }
 #endif // _di_controller_file_load_
 
+#ifndef _di_controller_file_pid_create_
+  f_return_status controller_file_pid_create(const controller_data_t data, const f_string_static_t path_pid) {
+    f_status_t status = F_none;
+
+    // the file exists, do not attempt to overwrite.
+    if (f_file_exists(path_pid.string) == F_true) {
+      return F_status_set_error(F_file_found);
+    }
+
+    {
+      f_string_dynamic_t path_directory = f_string_dynamic_t_initialize;
+
+      status = f_file_name_directory(path_pid.string, path_pid.used, &path_directory);
+
+      if (F_status_is_error_not(status)) {
+        status = f_directory_exists(path_directory.string);
+      }
+
+      f_macro_string_dynamic_t_delete_simple(path_directory);
+
+      if (F_status_is_error(status)) return status;
+
+      // the directory does not exist so do not bother attempting to create a pid file.
+      if (status == F_false) {
+        return F_status_set_error(F_directory_not);
+      }
+    }
+
+    f_file_t file = f_file_t_initialize;
+
+    file.flag = f_file_flag_write_only;
+
+    status = f_file_stream_open(path_pid.string, f_macro_file_open_mode_truncate, &file);
+    if (F_status_is_error(status)) return status;
+
+    fprintf(file.stream, "%llu\n", data.pid);
+
+    f_file_stream_close(F_true, &file);
+
+    if (F_status_is_error(status)) return status;
+
+    return F_none;
+  }
+#endif // _di_controller_file_pid_create_
+
+#ifndef _di_controller_file_pid_delete_
+  void controller_file_pid_delete(const controller_data_t data, const f_string_static_t path_pid) {
+
+    if (f_file_exists(path_pid.string) != F_true) {
+      return;
+    }
+
+    f_status_t status = F_none;
+    f_file_t pid_file = f_file_t_initialize;
+
+    status = f_file_stream_open(path_pid.string, f_macro_file_open_mode_read, &pid_file);
+    if (F_status_is_error(status)) return;
+
+    f_string_dynamic_t pid_buffer = f_string_dynamic_t_initialize;
+
+    status = f_file_stream_read(pid_file, 1, &pid_buffer);
+
+    f_file_stream_close(F_true, &pid_file);
+
+    if (F_status_is_error_not(status)) {
+      f_number_unsigned_t number = 0;
+      f_string_range_t range = f_macro_string_range_t_initialize(pid_buffer.used);
+
+      for (; range.start < pid_buffer.used; ++range.start) {
+        if (!isspace(pid_buffer.string[range.start])) break;
+      } // for
+
+      for (; range.stop > 0; --range.stop) {
+        if (!isspace(pid_buffer.string[range.stop])) break;
+      } // for
+
+      status = fl_conversion_string_to_decimal_unsigned(pid_buffer.string, &number, range);
+
+      if (F_status_is_error_not(status) && number == data.pid) {
+        f_file_remove(path_pid.string);
+      }
+    }
+
+    f_macro_string_dynamic_t_delete_simple(pid_buffer);
+  }
+#endif // _di_controller_file_pid_delete_
+
 #ifndef _di_controller_status_simplify_
   f_return_status controller_status_simplify(const f_status_t status) {
 
index ae5c962dee02e70dfcd0ae9a1131029332def89b..7a57e184fa74b43b34e4ff9834a7bb460cd38ae1 100644 (file)
@@ -505,6 +505,45 @@ extern "C" {
 #endif // _di_controller_file_load_
 
 /**
+ * Create the pid file, if possible.
+ *
+ * @param data
+ *   The program data.
+ * @param path_pid
+ *   The file path to the pid file to create.
+ *
+ * @return
+ *   F_none on success.
+ *   F_access_denied if pid file is not created due to access denied errors.
+ *   F_directory_not if pid file is not created due to a parent directory is unavailable or invalid.
+ *
+ *   Errors (with error bit) from: f_directory_exists().
+ *   Errors (with error bit) from: f_file_name_directory().
+ *   Errors (with error bit) from: f_file_stream_open().
+ *
+ * @see f_directory_exists()
+ * @see f_file_name_directory()
+ * @see f_file_stream_open()
+ */
+#ifndef _di_controller_file_pid_create_
+  f_return_status controller_file_pid_create(const controller_data_t data, const f_string_static_t path_pid) f_gcc_attribute_visibility_internal;
+#endif // _di_controller_file_pid_create_
+
+/**
+ * Delete the pid file, if exists and is valid.
+ *
+ * This is meant to be called on exit and avoids checking status codes, returning void.
+ *
+ * @param data
+ *   The program data.
+ * @param path_pid
+ *   The file path to the pid file to create.
+ */
+#ifndef _di_controller_file_pid_delete_
+  void controller_file_pid_delete(const controller_data_t data, const f_string_static_t path_pid) f_gcc_attribute_visibility_internal;
+#endif // _di_controller_file_pid_delete_
+
+/**
  * Given a wide range of status codes, simplify them down to a small subset.
  *
  * @param status
index 3dba6bd7721215d31443033e535f855aa574bc8c..739d2da0723fd446e2f200c68056ac19190c4bec 100644 (file)
@@ -8,6 +8,7 @@ f_utf
 f_color
 f_console
 f_conversion
+f_directory
 f_file
 f_fss
 f_iki
index 8eeb29b9d000471d7697679d90abfb0743facf94..6ebae0de1d529c6029d48d98dc3a58722e0870c3 100644 (file)
@@ -19,7 +19,7 @@ build_compiler gcc
 build_indexer ar
 build_language c
 build_libraries -lc
-build_libraries-individual -lfll_error -lfll_fss -lfll_path -lfll_program -lfll_status -lfl_color -lfl_console -lfl_conversion -lfl_fss -lfl_status -lfl_string -lf_console -lf_conversion -lf_file -lf_fss -lf_memory -lf_path -lf_pipe -lf_print -lf_utf
+build_libraries-individual -lfll_error -lfll_fss -lfll_path -lfll_program -lfll_status -lfl_color -lfl_console -lfl_conversion -lfl_fss -lfl_iki -lfl_status -lfl_string -lf_console -lf_conversion -lf_directory -lf_file -lf_fss -lf_iki -lf_memory -lf_path -lf_pipe -lf_print -lf_utf
 build_libraries-level -lfll_2 -lfll_1 -lfll_0
 build_libraries-monolithic -lfll
 build_sources_library controller.c private-control.c private-controller.c private-entry.c private-rule.c