[Non-ACR] hold pid always, change app_id 60/245860/4
authorAbhishek Vijay <abhishek.v@samsung.com>
Fri, 16 Oct 2020 13:05:31 +0000 (18:35 +0530)
committerAbhishek Vijay <abhishek.v@samsung.com>
Tue, 20 Oct 2020 13:41:21 +0000 (19:11 +0530)
Change-Id: I0d2a0f0a834a6dcb991c1761290f5c8db1de6342
Signed-off-by: Abhishek Vijay <abhishek.v@samsung.com>
src/battery_dump/bm_listeners.c

index 5f4632d047f4c2a7bcfe1d83cf90da2d2c86cf62..8e57b40cd608fbeacfa9ce910ff4c7d61e8fed9c 100644 (file)
@@ -87,15 +87,16 @@ static void bd_set_free_data_object(void)
 
 static void bd_get_app_id_from_table(gint pid, char **app_id)
 {
-       if (pid_map == NULL) {
-               _ERR("invalid pid_map");
+       BD_CHECK_VALIDITY_RETURN((pid_map != NULL), {});
+
+       if (g_hash_table_size(pid_map) <= 0) {
+               _WARN("empty app_id-pid map");
                return;
        }
 
-       _DBG("app_id for pid[%d], table size[%d]", pid, g_hash_table_size(pid_map));
+       _DBG("get app_id for pid[%d], table size[%d]", pid, g_hash_table_size(pid_map));
 
-       void *orig_pid = NULL;
-       void *orig_app_id = NULL;
+       void *orig_pid = NULL, *orig_app_id = NULL;
 
        if (g_hash_table_lookup_extended(pid_map, &pid, &orig_pid, &orig_app_id) == true) {
                *app_id = strdup((char *)orig_app_id);
@@ -110,23 +111,53 @@ static void bd_get_app_id_from_table(gint pid, char **app_id)
 
 static void bd_insert_app_id_into_table(gint pid, char *appid)
 {
-       if (appid == NULL) {
-               _ERR("invalid appid");
-               return;
-       }
+       BD_CHECK_VALIDITY_RETURN((appid != NULL), {});
 
-       /* redundant keys are not required */
-       if (g_hash_table_contains(pid_map, &pid) == true) {
-               _DBG("key[%d] already present, no insertion required", pid);
-               return;
-       }
+       gint* lpid = NULL;
+       static bool isEmpty = true;
 
-       gint* lpid = g_new0(gint, 1);
-       if (lpid == NULL) {
-               _ERR("failed to allocate memory");
-               return;
+       _DBG("insert app_id for pid[%d], table size[%d]", pid, g_hash_table_size(pid_map));
+
+       if (!isEmpty) {
+               gpointer orig_pid = NULL, orig_app_id = NULL;
+               /* if not empty then check if pid-app_id is redundant */
+               if (g_hash_table_lookup_extended(pid_map, &pid, &orig_pid, &orig_app_id) == true) {
+                       /* redundant pid, check if redundant app_id */
+                       if (g_strcmp0(appid, (char *)orig_app_id) == 0) {
+                               _DBG("redundant pid[%d], app_id[%s]", pid, appid);
+                               return;
+                       } else {
+                               _DBG("replacing pid[%d], app_id[%s]", pid, appid);
+                               /* app_id is not redundant,
+                                * means this pid is assigned to new app_id
+                                * replace old app_id with new app_id, delete old pid-app_id */
+                               lpid = g_new0(gint, 1);
+                               BD_CHECK_VALIDITY_RETURN((lpid != NULL), {});
+
+                               *lpid = pid;
+
+                               if (g_hash_table_replace(pid_map, lpid, strdup(appid)) == false)
+                                       _ERR("failed to replace old app_id");
+                       }
+               } else {
+                       GHashTableIter iter;
+                       gpointer stale_pid = NULL, stale_app_id = NULL;
+
+                       g_hash_table_iter_init(&iter, pid_map);
+                       /* delete stale pid-app_id */
+                       while (g_hash_table_iter_next(&iter, &stale_pid, &stale_app_id)) {
+                               if (g_strcmp0(appid, (char *)stale_app_id) == 0) {
+                                       _DBG("removed stale - pid[%d], app_id[%s]", \
+                                               *((int *)stale_pid), (char *)stale_app_id);
+                                       g_hash_table_iter_remove(&iter);
+                               }
+                       }
+               }
        }
 
+       lpid = g_new0(gint, 1);
+       BD_CHECK_VALIDITY_RETURN((lpid != NULL), {});
+
        *lpid = pid;
 
        _DBG("creating map - pid[%d], appid[%s]", *lpid, appid);
@@ -134,19 +165,8 @@ static void bd_insert_app_id_into_table(gint pid, char *appid)
        if (g_hash_table_insert(pid_map, lpid, strdup(appid)) == false)
                _WARN("key already available, replacing old value");
 
-       return;
-}
+       isEmpty = false;
 
-static void bd_remove_app_id_from_table(gint pid)
-{
-       if (g_hash_table_contains(pid_map, &pid) != true) {
-               _DBG("key[%d] not present in list", pid);
-               return;
-       }
-
-       g_hash_table_remove(pid_map, &pid);
-
-       _DBG("removed key[%d] from table", pid);
        return;
 }
 
@@ -1969,9 +1989,6 @@ static void _bd_listener_app_terminate_signal_cb(GDBusConnection *conn, const gc
                return;
        }
 
-       /* remove app_id from table as terminate signal is received */
-       bd_remove_app_id_from_table(pid);
-
        /* get event object */
        event_pool *event = bd_listener_get_event_obj(LISTEN_APP_STATUS, val, app_id);
        if (event == NULL) {