[chrony-dev] [RFC PATCH v1 15/17] util, main, logging: Add cleanup code for dynamically allocated auxiliary clocks

[ Thread Index | Date Index | More chrony.tuxfamily.org/chrony-dev Archives ]


Current Chrony cleanup code is only guaranteed to execute if the application exits normally. This patch
adds an additional mechanism that is called when the application exits normally and when a fatal error
occurs. There are two functions added to util.c to register a handler [UTI_SetSystemCleanupHandler()] and
call the handler [UTI_CallSystemCleanupHandler()]. The cleanup handler is called from main.c and logging.c
when a fatal error occurs. The cleanup code is not called on abort()/assert fail.
---
 logging.c |  1 +
 main.c    | 13 +++++++++++++
 main.h    |  3 +++
 util.c    | 19 +++++++++++++++++++
 util.h    |  3 +++
 5 files changed, 39 insertions(+)

diff --git a/logging.c b/logging.c
index d0f3e92..c28150e 100644
--- a/logging.c
+++ b/logging.c
@@ -199,6 +199,7 @@ void LOG_Message(LOG_Severity severity,
         system_log = 0;
         log_message(1, severity, buf);
       }
+      UTI_CallSystemCleanupHandler();
       exit(1);
       break;
     default:
diff --git a/main.c b/main.c
index 77d62e3..97eaf8d 100644
--- a/main.c
+++ b/main.c
@@ -134,6 +134,16 @@ notify_system_manager(int start)
 
 /* ================================================== */
 
+void
+MAI_SystemCleanup(void)
+{
+#ifdef FEAT_AUXCLOCK
+  CNF_FreeAuxClockId();
+#endif
+}
+
+/* ================================================== */
+
 void
 MAI_CleanupAndExit(void)
 {
@@ -175,6 +185,7 @@ MAI_CleanupAndExit(void)
 
   delete_pidfile();
   
+  UTI_CallSystemCleanupHandler();
   CNF_Finalise();
   HSH_Finalise();
   LOG_Finalise();
@@ -634,6 +645,8 @@ int main
   if (print_config)
     return 0;
 
+  UTI_SetSystemCleanupHandler(MAI_SystemCleanup);
+
   /* Check whether another chronyd may already be running */
   check_pidfile();
 
diff --git a/main.h b/main.h
index d6d502a..231fa67 100644
--- a/main.h
+++ b/main.h
@@ -30,6 +30,9 @@
 /* Function to clean up at end of run */
 extern void MAI_CleanupAndExit(void);
 
+/* Function performs cleanup to restore system state, also called on fatal error */
+extern void MAI_SystemCleanup(void);
+
 #endif /* GOT_MAIN_H */
 
 
diff --git a/util.c b/util.c
index 7768fb9..13d0bbb 100644
--- a/util.c
+++ b/util.c
@@ -49,6 +49,8 @@
 
 #define NSEC_PER_SEC 1000000000
 
+void (*system_cleanup_handler)(void);
+
 /* ================================================== */
 
 void
@@ -1699,3 +1701,20 @@ UTI_IsMemoryEqual(const void *s1, const void *s2, unsigned int len)
   return x == 0;
 #endif
 }
+
+/* ================================================== */
+
+extern void UTI_CallSystemCleanupHandler(void)
+{
+  if (system_cleanup_handler)
+    system_cleanup_handler();
+}
+
+/* ================================================== */
+
+extern void UTI_SetSystemCleanupHandler(void (*cleanup_handler)(void))
+{
+  system_cleanup_handler = cleanup_handler;
+}
+
+/* ================================================== */
diff --git a/util.h b/util.h
index 843b9fc..9682784 100644
--- a/util.h
+++ b/util.h
@@ -267,6 +267,9 @@ extern int UTI_SplitString(char *string, char **words, int max_saved_words);
    creating a timing side channel */
 extern int UTI_IsMemoryEqual(const void *s1, const void *s2, unsigned int len);
 
+extern void UTI_SetSystemCleanupHandler(void (*)(void));
+extern void UTI_CallSystemCleanupHandler(void);
+
 /* Macros to get maximum and minimum of two values */
 #ifdef MAX
 #undef MAX
-- 
2.34.1


-- 
To unsubscribe email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "unsubscribe" in the subject.
For help email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "help" in the subject.
Trouble?  Email listmaster@xxxxxxxxxxxxxxxxxxxx.


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/