Modify sigterm handling method 95/291095/4
authorChanggyu Choi <changyu.choi@samsung.com>
Mon, 10 Apr 2023 01:43:40 +0000 (10:43 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Mon, 10 Apr 2023 04:03:20 +0000 (13:03 +0900)
When handling signals using the signal() API, there can be issues where the previous behavior is not properly finished and interrupted.
This patch switches to using g_unix_signal_add() for safer signal handling.

Change-Id: I73caf0d88b7fbbfd5171d32322eb81d681ec8b31
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
tizen-cpp/app-core-cpp/app_core_base.cc

index 5e8803a..9d22bb1 100644 (file)
 #include <errno.h>
 #include <execinfo.h>
 #include <gio/gio.h>
+#include <glib-unix.h>
 #include <glib.h>
 #include <libintl.h>
 #include <linux/limits.h>
 #include <locale.h>
 #include <malloc.h>
 #include <sensor_internal.h>
-#include <signal.h>
 #include <stdbool.h>
 #include <sys/stat.h>
 #include <sys/time.h>
@@ -258,6 +258,7 @@ class AppCoreBase::Impl {
   int feature_ = 0;
   IAppCore* core_delegator_ = nullptr;
   IMainLoop* loop_delegator_ = nullptr;
+  guint signal_handler_source_ = 0;
 };
 
 AppCoreBase::EventBase::EventBase(Type type)
@@ -1232,11 +1233,19 @@ void AppCoreBase::Init(int argc, char** argv) {
   else
     OnLoopInit(argc, argv);
 
-  signal(SIGTERM, [](int n) {
-    _W("sigterm handler");
-    if (context_ != nullptr)
-      context_->Exit();
-  });
+  impl_->signal_handler_source_ = g_unix_signal_add(SIGTERM,
+      [](gpointer data) -> gboolean {
+        _W("sigterm handler");
+        if (context_ != nullptr) {
+          context_->Exit();
+          context_->impl_->signal_handler_source_ = 0;
+        }
+
+        return G_SOURCE_REMOVE;
+      }, nullptr);
+
+  if (impl_->signal_handler_source_ == 0)
+    _E("Failed to add sigterm handler.");
 
   traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
 
@@ -1290,6 +1299,11 @@ void AppCoreBase::Init(int argc, char** argv) {
 }
 
 void AppCoreBase::Fini() {
+  if (impl_->signal_handler_source_) {
+    g_source_remove(impl_->signal_handler_source_);
+    impl_->signal_handler_source_ = 0;
+  }
+
   Dispose();
 }