core: Change to register signal handler with glib 62/293062/1
authorTaeminYeom <taemin.yeom@samsung.com>
Fri, 19 May 2023 04:22:16 +0000 (13:22 +0900)
committerTaeminYeom <taemin.yeom@samsung.com>
Fri, 19 May 2023 04:23:47 +0000 (13:23 +0900)
Using standard signal handler can make deadlock in terminating progress.
When standard signal handler is called dispatching g_main_context,
glib attempts to lock g_main_context again.
To prevent this situation, it is needed to use glib signal handler function.

Change-Id: I437bfab15b593d17d3e21ed899ae98175edfc115
Signed-off-by: TaeminYeom <taemin.yeom@samsung.com>
src/core/main.c

index 346212b..f36de3c 100644 (file)
 
 static GMainLoop *loop;
 
-static void sig_quit(int signo)
+static gboolean handle_signal(gpointer data)
 {
-       _D("Received SIGTERM signal(%d).", signo);
-       g_main_loop_quit(loop);
-}
+       long signo = (long) data;
 
-static void sig_usr1(int signo)
-{
-       _D("Received SIGUSR1 signal %d. Storaged'll be finished.", signo);
+       _D("Received signal(%ld), Storaged'll be finished.", signo);
+
+       if (loop && g_main_loop_is_running(loop))
+               g_main_loop_quit(loop);
 
-       g_main_loop_quit(loop);
+       return G_SOURCE_REMOVE;
 }
 
 void watchdog_notify(void)
@@ -106,8 +105,8 @@ int main(int argc, char **argv)
                gdbus_check_name_owner(NULL, STORAGED_BUS_NAME);
        }
 
-       signal(SIGTERM, sig_quit);
-       signal(SIGUSR1, sig_usr1);
+       g_unix_signal_add(SIGTERM, handle_signal, (gpointer) SIGTERM);
+       g_unix_signal_add(SIGUSR1, handle_signal, (gpointer) SIGUSR1);
 
        timer = g_timeout_add(WATCHDOG_TIMEOUT * 1000, watchdog_cb, NULL);
        if (timer > 0) {