Modified to prevent frequent restart of Sticker daemon 18/283218/1
authorInHong Han <inhong1.han@samsung.com>
Thu, 20 Oct 2022 09:48:39 +0000 (18:48 +0900)
committerInHong Han <inhong1.han@samsung.com>
Thu, 20 Oct 2022 09:48:39 +0000 (18:48 +0900)
Change-Id: I64e1a40c250c4a2efcfc78a6b6a5a22a95b5ba9f

server/stickerd_data_manager.c

index 79b65b5..66915b6 100644 (file)
@@ -27,6 +27,7 @@
 #include <errno.h>
 #include <dirent.h>
 #include <fcntl.h>
+#include <glib.h>
 
 #include "stickerd_dbus.h"
 #include "stickerd_data_manager.h"
@@ -40,6 +41,7 @@
 #define LOG_TAG "STICKERD_DATA_MANAGER"
 
 #define MAX_ERROR_BUFFER  256
+#define TERMINATE_TIMER_INTERVAL 3000
 
 enum {
     STICKER_URI_TYPE_NONE,
@@ -50,17 +52,36 @@ enum {
 static GHashTable *_monitoring_hash = NULL;
 static char error_buffer[MAX_ERROR_BUFFER];
 static GList *consumer_list = NULL;
+static guint _terminate_timer_id = 0;
 extern GMainLoop *main_loop;
 
+static void _terminate_daemon()
+{
+    LOGD("Terminate sticker daemon");
+    g_hash_table_destroy(_monitoring_hash);
+    _monitoring_hash = NULL;
+    g_list_free_full(consumer_list, free);
+    consumer_list = NULL;
+    g_main_loop_quit(main_loop);
+}
+
+static gboolean _terminate_timer_handler(gpointer user_data)
+{
+    _terminate_daemon();
+    return TRUE;
+}
+
 static void _check_watcher_exist()
 {
     if (_monitoring_hash != NULL && g_hash_table_size(_monitoring_hash) == 0) {
-        LOGD("Terminate sticker daemon");
-        g_hash_table_destroy(_monitoring_hash);
-        _monitoring_hash = NULL;
-        g_list_free_full(consumer_list, free);
-        consumer_list = NULL;
-        g_main_loop_quit(main_loop);
+        if (_terminate_timer_id != 0) {
+            g_source_remove(_terminate_timer_id);
+            _terminate_timer_id = 0;
+        }
+
+        _terminate_timer_id = g_timeout_add(TERMINATE_TIMER_INTERVAL, _terminate_timer_handler, NULL);
+        if (_terminate_timer_id <= 0)
+            _terminate_daemon();
     }
 }
 
@@ -70,6 +91,10 @@ static void _on_name_appeared(GDBusConnection *connection,
         gpointer         user_data)
 {
     LOGD("name: %s", name);
+    if (_terminate_timer_id != 0) {
+        g_source_remove(_terminate_timer_id);
+        _terminate_timer_id = 0;
+    }
 }
 
 static void _on_name_vanished(GDBusConnection *connection,