| [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
]
- To: chrony-dev@xxxxxxxxxxxxxxxxxxxx
- Subject: [chrony-dev] [RFC PATCH v1 15/17] util, main, logging: Add cleanup code for dynamically allocated auxiliary clocks
- From: Christopher S M Hall <christopher.s.hall@xxxxxxxxx>
- Date: Sat, 6 Dec 2025 06:11:01 +0000
- Cc: christopher.s.hall@xxxxxxxxx, david.zage@xxxxxxxxx, yoong.siang.song@xxxxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1765002157; x=1796538157; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wyB5aIpRzUS4MNacQi4vRFWfR6MagfgbiDDm3Q8c+9E=; b=V2C00evu4QAATORYMuojQNMwyrS3tL2m8Z2MFmWYdJvq8bNmTRXeq6xR RO6G5eHMi1QIeo+E3ro+u6d28Lp4hJjhq7CQUuuFjLMPBIL0d1MJc3g1m dCP1W9VUOPZxgrkH0tp8M46+mKeFwyXujwKvCE3qJA+Dtkid3qcNB7ri4 LWwwEDFxLl/IkPymaNlkRcLRWXDTBzJe+mfJpFsbbiNe0eTnYzWejiSfP uwrQrt96ToFeCeSQrshlbtb3O5BkARUAvjHFZHomNnsVcPX/d589Vad2w FgHkVVpvi1cBYmVth0AgTdhk1n9OHiWaNLBAX6lLXIH07IUdtCeCM2sOG A==;
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.