]> git.ipfire.org Git - thirdparty/fcron.git/commitdiff
Defer any work outside of the signal handlers.
authorThibault Godouet <yo8192@users.noreply.github.com>
Tue, 31 Dec 2024 23:08:21 +0000 (23:08 +0000)
committerThibault Godouet <yo8192@users.noreply.github.com>
Tue, 31 Dec 2024 23:08:21 +0000 (23:08 +0000)
This is to make sure we don't inadvertently call a non-async-signal safe function (see the signal-safety(7) man page).

fcron.c

diff --git a/fcron.c b/fcron.c
index 7abc6cabd60835dadb7ff4cab6d8c4b423ce7e75..7847f261d5eff45a60a9e583e68ae57fc0a1b28e 100644 (file)
--- a/fcron.c
+++ b/fcron.c
@@ -94,6 +94,7 @@ char sig_conf = 0;              /* is 1 when we got a SIGHUP, 2 for a SIGUSR1 */
 char sig_chld = 0;              /* is 1 when we got a SIGCHLD */
 char sig_debug = 0;             /* is 1 when we got a SIGUSR2 */
 char sig_cont = 0;              /* is 1 when we got a SIGCONT */
+bool sig_term = false;          /* true if we got a SIGTERM */
 
 /* jobs database */
 struct cf_t *file_base;         /* point to the first file of the list */
@@ -478,24 +479,24 @@ create_spooldir(char *dir)
 
 }
 
-
 void
 sigterm_handler(int x)
   /* exit safely */
 {
-    debug("");
-    explain("SIGTERM signal received");
-    xexit(EXIT_OK);
+    /* Defer any work outside of the handler, by fear of inadvertently calling
+     * a non-async-signal safe function (see the signal-safety(7) man page). */
+    sig_term = true;
 }
 
 void
 sighup_handler(int x)
   /* update configuration */
 {
-    /* we don't call the synchronize_dir() function directly,
-     * because it may cause some problems if this signal
-     * is not received during the sleep
-     */
+    /* Defer any work outside of the handler, by fear of inadvertently calling
+     * a non-async-signal safe function (see the signal-safety(7) man page).
+     * In particular, we don't call the synchronize_dir() function directly,
+     * because it may cause some problems if this signal is not received during
+     * the sleep */
     sig_conf = 1;
 }
 
@@ -503,9 +504,9 @@ void
 sigchild_handler(int x)
   /* call wait_chld() to take care of finished jobs */
 {
-
+    /* Defer any work outside of the handler, by fear of inadvertently calling
+     * a non-async-signal safe function (see the signal-safety(7) man page). */
     sig_chld = 1;
-
 }
 
 
@@ -513,10 +514,11 @@ void
 sigusr1_handler(int x)
   /* reload all configurations */
 {
-    /* we don't call the synchronize_dir() function directly,
-     * because it may cause some problems if this signal
-     * is not received during the sleep
-     */
+    /* Defer any work outside of the handler, by fear of inadvertently calling
+     * a non-async-signal safe function (see the signal-safety(7) man page).
+     * In particular, we don't call the synchronize_dir() function directly,
+     * because it may cause some problems if this signal is not received during
+     * the sleep */
     sig_conf = 2;
 }
 
@@ -525,6 +527,8 @@ void
 sigusr2_handler(int x)
   /* print schedule and switch on/off debug mode */
 {
+    /* Defer any work outside of the handler, by fear of inadvertently calling
+     * a non-async-signal safe function (see the signal-safety(7) man page). */
     sig_debug = 1;
 }
 
@@ -533,6 +537,8 @@ sigcont_handler(int x)
   /* used to notify fcron of a system resume after suspend.
    * However this signal could also be received in other cases. */
 {
+    /* Defer any work outside of the handler, by fear of inadvertently calling
+     * a non-async-signal safe function (see the signal-safety(7) man page). */
     sig_cont = 1;
 }
 
@@ -776,6 +782,12 @@ check_signal()
         wait_chld();
         sig_chld = 0;
         reinstall_signal_handler(SIGCHLD, sigchild_handler);
+
+    if (sig_term == true) {
+        sig_term = false;
+        debug("");
+        explain("SIGTERM signal received");
+        xexit(EXIT_OK);
     }
 
     if (sig_conf > 0) {