Monitor freezer signal in the amd_suspend 67/305567/1
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 5 Feb 2024 06:35:25 +0000 (15:35 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 5 Feb 2024 06:40:14 +0000 (15:40 +0900)
To send the thaw event to the running process, amd listens to the freezer signal.
The listening the freezer signal code is moved to the amd_suspend from
the watchdog module.

Change-Id: I2aa0e4a513e235f0869de6d98ca309b7ae019854
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/lib/amd_suspend.c
src/lib/api/amd_api_noti_msg.h
src/modules/watchdog/src/amd_watchdog.c

index 10dfe4a..0ded37c 100644 (file)
 
 #include "amd_api_app_request_broker.h"
 #include "amd_app_status.h"
+#include "amd_noti.h"
 #include "amd_signal.h"
 #include "amd_suspend.h"
 #include "amd_util.h"
 
+#define RESOURCED_FREEZER_PATH         "/Org/Tizen/ResourceD/Freezer"
+#define RESOURCED_FREEZER_INTERFACE    "org.tizen.resourced.freezer"
+#define RESOURCED_FREEZER_SIGNAL       "FreezerState"
+
 typedef struct proc_info {
        pid_t pid;
        guint timer_id;
@@ -41,8 +46,14 @@ typedef struct network_info {
        guint timer_id;
 } network_info_t;
 
+typedef struct {
+       GDBusConnection *conn;
+       guint subs_id;
+} freezer_listener_t;
+
 static GHashTable *proc_info_tbl;
 static network_info_t __net_info;
+static freezer_listener_t __freezer_listener;
 static guint __init_timer;
 
 static proc_info_t *__create_proc_info(int pid);
@@ -356,6 +367,97 @@ static void __fini_network_info()
        _D("[__SUSPEND__] Network info is finished");
 }
 
+static void __prepare_to_thaw(pid_t pid, uid_t uid)
+{
+       amd_app_request_t request = {
+               .pid = pid,
+               .uid = uid,
+               .cmd = APP_THAW,
+               .data = bundle_create()
+       };
+
+       _D("[__SUSPEND__] pid: %d, uid: %u", pid, uid);
+       amd_app_request_broker_send(&request, NULL, NULL);
+       bundle_free(request.data);
+}
+
+static void __on_freezer_state_changed(
+               GDBusConnection *connection, const gchar *sender_name,
+               const gchar *object_path, const gchar *interface_name,
+               const gchar *signal_name, GVariant *parameters,
+               gpointer user_data)
+{
+       gint pid = -1;
+       gint status = -1;
+       app_status_h app_status;
+
+       if (g_strcmp0(signal_name, RESOURCED_FREEZER_SIGNAL) != 0)
+               return;
+
+       g_variant_get(parameters, "(ii)", &status, &pid);
+       _W("pid(%d), freezer state(%s)", pid, status ? "BACKGRD" : "FOREGRD");
+
+       app_status = _app_status_find(pid);
+       if (!app_status)
+               return;
+
+       if (status == 0)
+               __prepare_to_thaw(pid, _app_status_get_uid(app_status));
+
+       _noti_send(AMD_NOTI_MSG_SUSPEND_FREEZER_STATE_CHANGED, pid, status,
+                       NULL, NULL);
+}
+
+static int __listen_freezer_event(void *user_data)
+{
+       GError *err = NULL;
+
+       if (!__freezer_listener.conn) {
+               __freezer_listener.conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM,
+                               NULL, &err);
+               if (!__freezer_listener.conn) {
+                       _E("g_bus_get_sync() is failed. error=%s",
+                                       err ? err->message : "");
+                       g_clear_error(&err);
+                       return -1;
+               }
+       }
+
+       __freezer_listener.subs_id = g_dbus_connection_signal_subscribe(
+                       __freezer_listener.conn,
+                       NULL,
+                       RESOURCED_FREEZER_INTERFACE,
+                       RESOURCED_FREEZER_SIGNAL,
+                       RESOURCED_FREEZER_PATH,
+                       NULL,
+                       G_DBUS_SIGNAL_FLAGS_NONE,
+                       __on_freezer_state_changed,
+                       NULL,
+                       NULL);
+       if (!__freezer_listener.subs_id) {
+               _E("g_dbus_connection_signal_subscribe() is failed");
+               return -1;
+       }
+
+       _W("Signal subscription is successful");
+       return 0;
+}
+
+static void __ignore_freezer_event(void)
+{
+       if (!__freezer_listener.conn)
+               return;
+
+       if (__freezer_listener.subs_id) {
+               g_dbus_connection_signal_unsubscribe(__freezer_listener.conn,
+                               __freezer_listener.subs_id);
+               __freezer_listener.subs_id = 0;
+       }
+
+       g_object_unref(__freezer_listener.conn);
+       __freezer_listener.conn = NULL;
+}
+
 void _suspend_init(void)
 {
        if (!proc_info_tbl) {
@@ -366,11 +468,14 @@ void _suspend_init(void)
 
        __init_timer = g_timeout_add(500, __init_network_info, NULL);
 
+       _signal_add_ready_cb(__listen_freezer_event, NULL);
+
        _D("_amd_proc_init done");
 }
 
 void _suspend_fini(void)
 {
+       __ignore_freezer_event();
        if (__init_timer)
                g_source_remove(__init_timer);
 
index db282bf..b77ddce 100644 (file)
@@ -1064,6 +1064,15 @@ extern "C" {
 #define AMD_NOTI_MSG_APP_GROUP_SET                                             \
        "app_group.set"
 
+/**
+ * @brief Definition for the notification message: The freezer state changed.
+ * @details Input: arg1(int) The process ID.\n
+ *          Input: arg2(int) The freezer state.\n
+ * @since_tizen 8.0
+ */
+#define AMD_NOTI_MSG_SUSPEND_FREEZER_STATE_CHANGED                             \
+       "suspend.freezer.state.changed"
+
 #ifdef __cplusplus
 }
 #endif
index 25dd54a..c81bda3 100644 (file)
@@ -24,7 +24,6 @@
 #include <sys/time.h>
 #include <errno.h>
 #include <glib.h>
-#include <gio/gio.h>
 #include <aul.h>
 #include <aul_cmd.h>
 #include <aul_sock.h>
 #include "amd_watchdog_logger.h"
 #include "amd_watchdog_private.h"
 
-#define RESOURCED_FREEZER_PATH         "/Org/Tizen/ResourceD/Freezer"
-#define RESOURCED_FREEZER_INTERFACE    "org.tizen.resourced.freezer"
-#define RESOURCED_FREEZER_SIGNAL       "FreezerState"
-
 typedef struct watchdog_s {
-       GDBusConnection *conn;
-       guint listener;
        guint retry_handler;
        unsigned int interval;
        int max_retry_count;
@@ -435,95 +428,31 @@ static amd_request_cmd_dispatch __dispatch_table[] = {
        },
 };
 
-static void __on_freezer_state_changed(GDBusConnection *connection,
-               const gchar *sender_name,
-               const gchar *object_path,
-               const gchar *interface_name,
-               const gchar *signal_name,
-               GVariant *parameters,
-               gpointer user_data)
+static void __on_suspend_freezer_state_changed(const char *msg, int arg1,
+               int arg2, void *arg3, bundle *arg4)
 {
-       gint pid = -1;
-       gint status = -1;
+       int pid = arg1;
+       int status = arg2;
        proc_context *ctx;
 
-       if (!g_strcmp0(signal_name, RESOURCED_FREEZER_SIGNAL)) {
-               g_variant_get(parameters, "(ii)", &status, &pid);
-               ctx = __find_proc_context(pid);
-               if (!ctx) {
-                       _E("Failed to find process(%d) context", pid);
-                       return;
-               }
-
-               /* 0: SET_FOREGRD, 1: SET_BACKGRD, */
-               ctx->frozen = (bool)status;
-               if (ctx->frozen) {
-                       __watchdog_unset_timer(ctx);
-               } else {
-                       if (ctx->watchdog_enable)
-                               __watchdog_set_timer(ctx);
-               }
-
-               _W("pid(%d), freezer state(%s)",
-                               pid, status ? "BACKGRD" : "FOREGRD");
-               _watchdog_logger_print("FREEZER",
-                               "state(%s), pid(%d), appid(%s)",
-                               status ? "BACKGRD" : "FOREGRD",
-                               pid, __get_appid(pid));
-       }
-}
-
-static int __listen_freezer_state(void *user_data)
-{
-       GError *err = NULL;
-
-       if (__watchdog.listener)
-               return 0;
-
-       if (!__watchdog.conn) {
-               __watchdog.conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-               if (!__watchdog.conn) {
-                       _E("Failed to connecto D-Bus daemon. err(%s)",
-                                       err->message);
-                       g_error_free(err);
-                       return -1;
-               }
-       }
-
-       __watchdog.listener = g_dbus_connection_signal_subscribe(
-                       __watchdog.conn,
-                       NULL,
-                       RESOURCED_FREEZER_INTERFACE,
-                       RESOURCED_FREEZER_SIGNAL,
-                       RESOURCED_FREEZER_PATH,
-                       NULL,
-                       G_DBUS_SIGNAL_FLAGS_NONE,
-                       __on_freezer_state_changed,
-                       NULL,
-                       NULL);
-       if (!__watchdog.listener) {
-               _E("Failed to subscribe freezer state");
-               return -1;
-       }
-
-       _I("%s is subscribed", RESOURCED_FREEZER_SIGNAL);
-
-       return 0;
-}
-
-static void __ignore_freezer_state(void)
-{
-       if (!__watchdog.conn)
+       ctx = __find_proc_context(pid);
+       if (!ctx) {
+               _E("Failed to find process(%d) context", pid);
                return;
+       }
 
-       if (__watchdog.listener) {
-               g_dbus_connection_signal_unsubscribe(__watchdog.conn,
-                               __watchdog.listener);
-               __watchdog.listener = 0;
+       /* 0: SET_FOREGRD, 1: SET_BACKGRD, */
+       ctx->frozen = (bool)status;
+       if (ctx->frozen) {
+               __watchdog_unset_timer(ctx);
+       } else {
+               if (ctx->watchdog_enable)
+                       __watchdog_set_timer(ctx);
        }
 
-       g_object_unref(__watchdog.conn);
-       __watchdog.conn = NULL;
+       _W("pid(%d), freezer state(%s)", pid, status ? "BACKGRD" : "FOREGRD");
+       _watchdog_logger_print("FREEZER", "state(%s), pid(%d), appid(%s)",
+                       status ? "BACKGRD" : "FOREGRD", pid, __get_appid(pid));
 }
 
 EXPORT int AMD_MOD_INIT(void)
@@ -553,8 +482,8 @@ EXPORT int AMD_MOD_INIT(void)
                        __on_app_status_cleanup);
        amd_noti_listen(AMD_NOTI_MSG_SIGNAL_SEND_WATCHDOG_START,
                        __on_signal_send_watchdog_start);
-
-       amd_signal_add_ready_cb(__listen_freezer_state, NULL);
+       amd_noti_listen(AMD_NOTI_MSG_SUSPEND_FREEZER_STATE_CHANGED,
+                       __on_suspend_freezer_state_changed);
 
        return 0;
 }
@@ -566,8 +495,6 @@ EXPORT void AMD_MOD_FINI(void)
        if (__watchdog.retry_handler)
                g_source_remove(__watchdog.retry_handler);
 
-       __ignore_freezer_state();
-
        if (__watchdog.proc_contexts) {
                g_list_free_full(__watchdog.proc_contexts,
                                __destroy_proc_context);