[Non-ACR] get app_id/daemon-name in case of power lock/unlock 88/243488/2
authorAbhishek Vijay <abhishek.v@samsung.com>
Mon, 7 Sep 2020 12:39:31 +0000 (18:09 +0530)
committerAbhishek Vijay <abhishek.v@samsung.com>
Tue, 22 Sep 2020 12:42:38 +0000 (18:12 +0530)
Change-Id: Ic9af1bf46a44739de5e1ffd9d2fd33bd07280950
Signed-off-by: Abhishek Vijay <abhishek.v@samsung.com>
include/bm_util.h
src/battery_dump/bm_listeners.c
src/bm_util.c

index bf2c4b4931a129b9953d23ae1c0cdc503f38b60d..06d8aff23a5cb0bf0e666b7089624716f7f93990 100644 (file)
 #include <stdlib.h>
 #include <signal.h>
 #include <glib.h>
+#include <fcntl.h>
 
 #if !GLIB_CHECK_VERSION(2, 31, 0)
 #include <glib/gmacros.h>
 #endif
 
+#include <app_context.h>
+#include <app_manager_extension.h>
+
 #include "../battery_monitor_stub.h"
 #include "bm_power_engine.h"
 #include "bm_private.h"
@@ -55,6 +59,10 @@ int bm_set_dbus_connection_obj(GDBusConnection* conn);
 
 GDBusConnection* bm_get_dbus_connection_obj(void);
 
+void bd_get_focussed_app_id(char **app_name);
+
+void bm_get_name_from_pid(pid_t pid, char **pname);
+
 void bm_data_free(gpointer data);
 
 void bm_atm_free(gpointer data);
index ed2cb491dcea7517b6d6832033490c1d0cf8824b..ed2df5f45948688b46d7e9ab13268bd33bc89055 100644 (file)
@@ -22,8 +22,6 @@
 #include <ITapiNetwork.h>
 #include <glib.h>
 #include <device/battery-internal.h>
-#include <app_context.h>
-#include <app_manager_extension.h>
 
 #include "bm_listeners.h"
 #include "bm_dbg.h"
@@ -51,49 +49,6 @@ static int buff_count = 0;
 
 GHashTable *app_list = NULL;
 
-void bd_get_focussed_app_id(char **app_name)
-{
-       ENTER;
-
-       if (*app_name == NULL) {
-               _ERR("invalid storage for app_id");
-               return;
-       }
-
-       int return_val = APP_MANAGER_ERROR_NONE;
-
-       app_context_h app_context;
-       char *app_id = NULL;
-
-       return_val  = app_manager_get_focused_app_context(&app_context);
-       if (return_val != APP_MANAGER_ERROR_NONE) {
-               _ERR("failed to get app-context");
-               return;
-       }
-
-       return_val = app_context_get_app_id(app_context, &app_id);
-       if(return_val != APP_MANAGER_ERROR_NONE) {
-               _ERR("failed to get app_id");
-               app_context_destroy(app_context);
-               return;
-       }
-
-       _DBG("focussed app_id = %s", app_id);
-
-       *app_name = app_id;
-
-       return_val = app_context_destroy(app_context);
-       if (return_val != APP_MANAGER_ERROR_NONE) {
-               _ERR("failed to destroy app-context");
-               return;
-       }
-
-       app_context = NULL;
-
-       EXIT;
-       return;
-}
-
 static int bd_listener_set_appId_info(char *app_id)
 {
        ENTER;
@@ -1662,10 +1617,11 @@ static void _bd_listener_notify_modem_power(TapiHandle *handle, const char *noti
        return;
 }
 
-static void _bd_listener_create_lock_event(char *lock_type)
+static void _bd_listener_create_lock_event(char *lock_type, pid_t pid)
 {
        int val;
        static int prev_lock = 0;
+       char *pname = NULL;
 
        if (g_strcmp0(lock_type, "lcdoff") == 0)
                val = 0;
@@ -1681,8 +1637,10 @@ static void _bd_listener_create_lock_event(char *lock_type)
        else
                prev_lock = val;
 
+       bm_get_name_from_pid(pid, &pname);
+
        /* get event object */
-       event_pool *event = bd_listener_get_event_obj(LISTEN_POWERLOCKUNLOCK_STATE, val, NULL);
+       event_pool *event = bd_listener_get_event_obj(LISTEN_POWERLOCKUNLOCK_STATE, val, pname);
        if (event == NULL) {
                _ERR("Failed to get event pool object");
                return;
@@ -1706,7 +1664,7 @@ static void _bd_listener_powerlock_signal_cb(GDBusConnection *conn, const gchar
 
        _INFO(" powerlock value changed =%s Pid=%d, timeout=%d", lock_type, pid, timeout);
 
-       _bd_listener_create_lock_event(lock_type);
+       _bd_listener_create_lock_event(lock_type, pid);
 
        EXIT;
        return;
@@ -1724,7 +1682,7 @@ static void _bd_listener_powerunlock_signal_cb(GDBusConnection *conn, const gcha
 
        _INFO(" powerUnlock value changed =%s Pid=%d, timeout=%d", lock_type, pid, timeout);
 
-       _bd_listener_create_lock_event(lock_type);
+       _bd_listener_create_lock_event(lock_type, pid);
 
        EXIT;
        return;
index af272910df6d19823f99a13f456bfdf5a139e353..5665a51465dc514b1ccb94feaffed5ed1ee806fb 100644 (file)
@@ -331,6 +331,88 @@ GDBusConnection* bm_get_dbus_connection_obj(void)
        return connection_obj;
 }
 
+void bd_get_focussed_app_id(char **app_name)
+{
+       ENTER;
+
+       int return_val = APP_MANAGER_ERROR_NONE;
+
+       app_context_h app_context;
+       char *app_id = NULL;
+
+       return_val  = app_manager_get_focused_app_context(&app_context);
+       if (return_val != APP_MANAGER_ERROR_NONE) {
+               _ERR("failed to get app-context");
+               return;
+       }
+
+       return_val = app_context_get_app_id(app_context, &app_id);
+       if(return_val != APP_MANAGER_ERROR_NONE) {
+               _ERR("failed to get app_id");
+               app_context_destroy(app_context);
+               return;
+       }
+
+       _DBG("focussed app_id = %s", app_id);
+
+       *app_name = app_id;
+
+       return_val = app_context_destroy(app_context);
+       if (return_val != APP_MANAGER_ERROR_NONE) {
+               _ERR("failed to destroy app-context");
+               return;
+       }
+
+       app_context = NULL;
+
+       EXIT;
+       return;
+}
+
+void bm_get_name_from_pid(pid_t pid, char **pname)
+{
+       _INFO("pid = [%d]", pid);
+
+       char *app_id = NULL;
+
+       app_manager_get_app_id(pid, &app_id);
+
+       if (app_id == NULL) { /*it might be a daemon, ex-/usr/bin/batterymonitor-svcd*/
+               char buf[PATH_MAX];
+               int fd, r;
+
+               snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid);
+
+               fd = open(buf, O_RDONLY);
+               if (fd < 0) {
+                       _ERR("process(%d) does not exist now.", pid);
+                       return;
+               }
+
+               r = read(fd, buf, PATH_MAX);
+               if ((r >= 0) && (r < PATH_MAX)) {
+                       buf[r] = '\0';
+               } else {
+                       _ERR("read error");
+                       if (close(fd) == -1)
+                               _ERR("fd close error");
+                       return;
+               }
+
+               if (close(fd) == -1) {
+                       _ERR("fd close error");
+               }
+
+               app_id = strdup(buf);
+       }
+
+       *pname = app_id;
+
+       _INFO("pid - [%d], name - [%s]", pid, *pname);
+
+       return;
+}
+
 void bm_data_free(gpointer data)
 {
        ENTER;