Update copyright dates
[src/app-framework-binder.git] / src / sig-monitor.c
index 954dcf8..b209dbf 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017-2019 "IoT.bzh"
+ * Copyright (C) 2015-2020 "IoT.bzh"
  * Author José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
 
 /* controls whether to dump stack or not */
-#if !defined(USE_SIG_MONITOR_DUMPSTACK)
-#  define USE_SIG_MONITOR_DUMPSTACK 1
+#if !defined(WITH_SIG_MONITOR_DUMPSTACK)
+#  define WITH_SIG_MONITOR_DUMPSTACK 1
 #endif
 
 /* control whether to monitor signals */
-#if !defined(USE_SIG_MONITOR_SIGNALS)
-#  define USE_SIG_MONITOR_SIGNALS 1
+#if !defined(WITH_SIG_MONITOR_SIGNALS)
+#  define WITH_SIG_MONITOR_SIGNALS 1
 #endif
 
 /* controls whether to monitor calls */
-#if !defined(USE_SIG_MONITOR_FOR_CALL)
-#  define USE_SIG_MONITOR_FOR_CALL 1
+#if !defined(WITH_SIG_MONITOR_FOR_CALL)
+#  define WITH_SIG_MONITOR_FOR_CALL 1
 #endif
 
 /* control whether to monitor timers */
-#if !defined(USE_SIG_MONITOR_TIMERS)
-#  define USE_SIG_MONITOR_TIMERS 1
+#if !defined(WITH_SIG_MONITOR_TIMERS)
+#  define WITH_SIG_MONITOR_TIMERS 1
 #endif
 
-#if !USE_SIG_MONITOR_SIGNALS
-#  undef USE_SIG_MONITOR_FOR_CALL
-#  define USE_SIG_MONITOR_FOR_CALL 0
+#if !WITH_SIG_MONITOR_SIGNALS
+#  undef WITH_SIG_MONITOR_FOR_CALL
+#  define WITH_SIG_MONITOR_FOR_CALL 0
 #endif
 
-#if !USE_SIG_MONITOR_FOR_CALL
-#  undef USE_SIG_MONITOR_TIMERS
-#  define USE_SIG_MONITOR_TIMERS 0
+#if !WITH_SIG_MONITOR_FOR_CALL
+#  undef WITH_SIG_MONITOR_TIMERS
+#  define WITH_SIG_MONITOR_TIMERS 0
 #endif
 
 /******************************************************************************/
@@ -63,7 +63,7 @@
 #include "verbose.h"
 
 /******************************************************************************/
-#if !USE_SIG_MONITOR_DUMPSTACK
+#if !WITH_SIG_MONITOR_DUMPSTACK
 
 static inline void dumpstack(int crop, int signum) {}
 
@@ -109,7 +109,7 @@ static void dumpstack(int crop, int signum)
 
 #endif
 /******************************************************************************/
-#if !USE_SIG_MONITOR_TIMERS
+#if !WITH_SIG_MONITOR_TIMERS
 
 static inline int timeout_create() { return 0; }
 static inline int timeout_arm(int timeout) { return 0; }
@@ -154,7 +154,7 @@ static inline int timeout_create()
                rc = timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &thread_timerid);
                thread_timer_set = !rc;
        }
-       return 0;
+       return rc;
 }
 
 /*
@@ -198,7 +198,7 @@ static inline void timeout_delete()
 }
 #endif
 /******************************************************************************/
-#if !USE_SIG_MONITOR_FOR_CALL
+#if !WITH_SIG_MONITOR_FOR_CALL
 
 static inline void monitor_raise(int signum) {}
 
@@ -240,7 +240,7 @@ static inline void monitor_raise(int signum)
 }
 #endif
 /******************************************************************************/
-#if !USE_SIG_MONITOR_SIGNALS
+#if !WITH_SIG_MONITOR_SIGNALS
 
 static inline int enable_signal_handling() { return 0; }
 
@@ -284,9 +284,9 @@ static void on_rescue_exit(int signum)
 }
 
 /*
- * Do a safe exit
+ * Do a direct safe exit
  */
-static void safe_exit(int code)
+static void direct_safe_exit(int code)
 {
        set_signals_handler(on_rescue_exit, sigerr);
        set_signals_handler(on_rescue_exit, sigterm);
@@ -294,7 +294,29 @@ static void safe_exit(int code)
        exit(code);
 }
 
-#if !USE_SIG_MONITOR_DUMPSTACK
+/*
+ * Do a safe exit
+ */
+#if WITH_SIG_MONITOR_NO_DEFERRED_EXIT
+#  define safe_exit(x) direct_safe_exit(x)
+#else
+#include "jobs.h"
+static void exit_job(int signum, void* arg)
+{
+       exiting = (int)(intptr_t)arg;
+       if (signum)
+               on_rescue_exit(signum);
+       exit(exiting);
+}
+
+static void safe_exit(int code)
+{
+       if (jobs_queue(safe_exit, 0, exit_job, (void*)(intptr_t)code))
+               direct_safe_exit(code);
+}
+#endif
+
+#if !WITH_SIG_MONITOR_DUMPSTACK
 
 static inline void safe_dumpstack(int crop, int signum) {}
 #define in_safe_dumpstack (0)
@@ -387,7 +409,7 @@ void sig_monitor_clean_timeouts()
 
 void sig_monitor(int timeout, void (*function)(int sig, void*), void *arg)
 {
-#if USE_SIG_MONITOR_SIGNALS && USE_SIG_MONITOR_FOR_CALL
+#if WITH_SIG_MONITOR_SIGNALS && WITH_SIG_MONITOR_FOR_CALL
        if (enabled)
                monitor(timeout, function, arg);
        else