Fix a svace issue for negative code error
[platform/core/connectivity/stc-manager.git] / plugin / procfs / stc-plugin-procfs.c
index f04446b..9538a61 100755 (executable)
 #include <glib.h>
 
 #include "stc-plugin-procfs.h"
-#include "stc-monitor.h"
 #include "helper-net-cls.h"
 #include "helper-procfs.h"
+#include "stc-manager-plugin-monitor.h"
+#include "stc-manager-plugin-exception.h"
 
 //LCOV_EXCL_START
 typedef struct {
@@ -62,7 +63,6 @@ static int nl_connector_sock = -1;
 static guint nl_connector_gsource_id = 0;
 static GTree *proc_tree;
 
-
 static gboolean __process_nl_connector_message(GIOChannel *source,
                                               GIOCondition condition,
                                               gpointer user_data);
@@ -136,7 +136,7 @@ static proc_value_s * __proc_tree_find_parent(proc_value_s *value)
                        parent = lookup;
        } while (lookup);
 
-       if (STC_DEBUG_LOG) {
+       if (STC_STAT_LOG) {
                if (parent != NULL)
                        STC_LOGD("\033[0;35mPARENT\033[0;m: tgid[\033[1;33m%s\033[0;m] "
                                "pid[%s] ppid[%s] cmdline[\033[0;34m%s\033[0;m] name[%s]",
@@ -159,7 +159,7 @@ static void __proc_tree_add(proc_key_s *key, proc_value_s *value)
        }
 
        if (key == NULL || value == NULL) {
-               if (STC_DEBUG_LOG)
+               if (STC_STAT_LOG)
                        STC_LOGE("invalid parameters");
                return;
        }
@@ -224,7 +224,7 @@ static gboolean __check_excn(char *cmdline)
        if (cmdline[0] == '(')
                return TRUE;
 
-       ret = stc_monitor_check_excn_by_cmdline(cmdline);
+       ret = stc_plugin_check_exception_by_cmdline(cmdline);
        if (ret == STC_ERROR_UNINITIALIZED ||
                ret == STC_ERROR_NO_DATA)
                return FALSE;
@@ -304,11 +304,8 @@ static void __process_event_fork(int tgid, int pid)
        if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) &&
            STC_ERROR_NONE == proc_get_status(pid, status)) {
 
-               if (__check_excn(cmdline)) {
-                       if (STC_DEBUG_LOG)
-                               STC_LOGD("[%s] monitoring is excepted", cmdline);
+               if (__check_excn(cmdline))
                        return;
-               }
 
                unsigned int i;
                proc_key_s key;
@@ -322,7 +319,7 @@ static void __process_event_fork(int tgid, int pid)
                        g_strlcpy(value.status[i], status[i], sizeof(value.status[i]));
                g_strlcpy(value.cmdline, cmdline, sizeof(value.cmdline));
 
-               if (STC_DEBUG_LOG)
+               if (STC_STAT_LOG)
                        STC_LOGD("\033[1;32mFORK\033[0;m: tgid[\033[1;33m%d\033[0;m] "
                        "ppid=[%s] cmdline[\033[0;34m%s\033[0;m] pid[%d]",
                        tgid, status[PROC_STATUS_PPID], cmdline, pid);
@@ -345,11 +342,8 @@ static void __process_event_exec(int tgid, int pid)
        if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) &&
            STC_ERROR_NONE == proc_get_status(pid, status)) {
 
-               if (__check_excn(cmdline)) {
-                       if (STC_DEBUG_LOG)
-                               STC_LOGD("[%s] monitoring is excepted", cmdline);
+               if (__check_excn(cmdline))
                        return;
-               }
 
                unsigned int i;
                proc_key_s key;
@@ -364,7 +358,7 @@ static void __process_event_exec(int tgid, int pid)
                                  sizeof(value.status[i]));
                g_strlcpy(value.cmdline, cmdline, sizeof(value.cmdline));
 
-               if (STC_DEBUG_LOG)
+               if (STC_STAT_LOG)
                        STC_LOGD("\033[1;32mEXEC\033[0;m: tgid[\033[1;33m%d\033[0;m] "
                        "ppid[%s] cmdline[\033[0;34m%s\033[0;m] pid[%d]",
                        tgid, status[PROC_STATUS_PPID], cmdline, pid);
@@ -386,7 +380,7 @@ static void __process_event_exit(int tgid, int pid, int exit_code)
        if (lookup == NULL) /* unmonitored process */
                return;
 
-       if (STC_DEBUG_LOG)
+       if (STC_STAT_LOG)
                STC_LOGD("\033[1;31mEXIT\033[0;m: tgid[\033[1;33m%d\033[0;m] "
                        "cmdline[\033[0;34m%s\033[0;m] pid[%d] exitcode[%d]",
                        tgid, lookup->cmdline, pid, exit_code);
@@ -453,7 +447,7 @@ static int __subscribe_proc_events(void)
 
        if (sock == -1) {
                __STC_LOG_FUNC_EXIT__;
-               return -1;
+               return STC_ERROR_INVALID_PARAMETER;
        }
 
        memset(&msg, 0, sizeof(nl_connector_msg_s));
@@ -472,11 +466,11 @@ static int __subscribe_proc_events(void)
        if (ret == -1) {
                STC_LOGE("Error sending netlink connector message");
                __STC_LOG_FUNC_EXIT__;
-               return -1;
+               return STC_ERROR_FAIL;
        }
 
        __STC_LOG_FUNC_EXIT__;
-       return 0;
+       return STC_ERROR_NONE;
 }
 
 static int __unsubscribe_proc_events(void)
@@ -488,7 +482,7 @@ static int __unsubscribe_proc_events(void)
 
        if (sock == -1) {
                __STC_LOG_FUNC_EXIT__;
-               return -1;
+               return STC_ERROR_INVALID_PARAMETER;
        }
 
        memset(&msg, 0, sizeof(nl_connector_msg_s));
@@ -507,11 +501,11 @@ static int __unsubscribe_proc_events(void)
        if (ret == -1) {
                STC_LOGE("Error sending netlink connector message");
                __STC_LOG_FUNC_EXIT__;
-               return -1;
+               return STC_ERROR_FAIL;
        }
 
        __STC_LOG_FUNC_EXIT__;
-       return 0;
+       return STC_ERROR_NONE;
 }
 
 static bool __process_pid_cb(pid_t pid, void *user_data)
@@ -548,6 +542,7 @@ static bool __process_pid_cb(pid_t pid, void *user_data)
 int stc_plugin_procfs_initialize(void)
 {
        __STC_LOG_FUNC_ENTER__;
+       int ret = STC_ERROR_NONE;
 
        proc_tree = g_tree_new_full(__proc_tree_key_compare, NULL,
                                    __proc_tree_key_free,
@@ -556,28 +551,30 @@ int stc_plugin_procfs_initialize(void)
        /* TODO: Fill proc tree with current procfs state */
 
        __open_nl_connector_sock();
-       __subscribe_proc_events();
+       ret = __subscribe_proc_events();
+
        __STC_LOG_FUNC_EXIT__;
-       return STC_ERROR_NONE;
+       return ret;
 }
 
 int stc_plugin_procfs_deinitialize(void)
 {
        __STC_LOG_FUNC_ENTER__;
+       int ret = STC_ERROR_NONE;
 
        if (nl_connector_sock == -1) {
                STC_LOGD("socket already closed");
                return STC_ERROR_NONE;
        }
 
-       __unsubscribe_proc_events();
+       ret = __unsubscribe_proc_events();
        __close_nl_connector_sock();
 
        g_tree_destroy(proc_tree);
        proc_tree = NULL;
 
        __STC_LOG_FUNC_EXIT__;
-       return STC_ERROR_NONE;
+       return ret;
 }
 
 stc_error_e stc_plugin_procfs_load(void)
@@ -598,14 +595,15 @@ stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd,
 {
        stc_error_e ret = STC_ERROR_NONE;
 
-       if ((pkg_id && app_id) && STC_DEBUG_LOG)
+       if ((pkg_id && app_id) && STC_STAT_LOG)
                STC_LOGD("cmd[%d] pkgid[%s] appid[%s] pid[%d] type[%d]",
                        cmd, pkg_id, app_id, pid, app_type);
 
        switch (cmd) {
        case STC_CMD_SET_FOREGRD:
        {
-               uint32_t classid;
+               uint32_t fg_classid;
+               uint32_t bg_classid;
                char *bg_app_id;
                stc_app_value_s app_value;
                stc_proc_value_s proc_value;
@@ -616,26 +614,29 @@ stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd,
                bg_app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, NULL);
 
                app_value.type = app_type;
+               app_value.state = STC_APP_STATE_FOREGROUND;
                app_value.processes = NULL;
 
                proc_value.pid = pid;
                proc_value.ground = STC_APP_STATE_FOREGROUND;
 
-               classid = get_classid_by_app_id(bg_app_id, FALSE);
-               stc_monitor_proc_remove(classid, pid);
+               bg_classid = get_classid_by_app_id(bg_app_id, FALSE);
+               fg_classid = get_classid_by_app_id(app_id, TRUE);
 
-               classid = get_classid_by_app_id(app_id, TRUE);
+               stc_plugin_monitor_add_app(fg_classid, app_id, pkg_id, app_value);
 
-               stc_monitor_app_add(classid, app_id, pkg_id, app_value);
-               stc_monitor_proc_add(classid, app_id, proc_value);
-               stc_monitor_proc_update_ground(classid, app_id, proc_value);
+               stc_plugin_monitor_move_proc(bg_classid, fg_classid);
+
+               stc_plugin_monitor_add_proc(fg_classid, app_id, proc_value);
+               stc_plugin_monitor_update_proc_ground(fg_classid, app_id, proc_value);
 
                FREE(bg_app_id);
                break;
        }
        case STC_CMD_SET_BACKGRD:
        {
-               uint32_t classid;
+               uint32_t bg_classid;
+               uint32_t fg_classid;
                char *bg_app_id;
                stc_app_value_s app_value;
                stc_proc_value_s proc_value;
@@ -646,19 +647,21 @@ stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd,
                bg_app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, NULL);
 
                app_value.type = app_type;
+               app_value.state = STC_APP_STATE_BACKGROUND;
                app_value.processes = NULL;
 
                proc_value.pid = pid;
                proc_value.ground = STC_APP_STATE_BACKGROUND;
 
-               classid = get_classid_by_app_id(app_id, FALSE);
-               stc_monitor_proc_remove(classid, pid);
+               fg_classid = get_classid_by_app_id(app_id, FALSE);
+               bg_classid = get_classid_by_app_id(bg_app_id, TRUE);
+
+               stc_plugin_monitor_add_app(bg_classid, bg_app_id, pkg_id, app_value);
 
-               classid = get_classid_by_app_id(bg_app_id, TRUE);
+               stc_plugin_monitor_move_proc(fg_classid, bg_classid);
 
-               stc_monitor_app_add(classid, bg_app_id, pkg_id, app_value);
-               stc_monitor_proc_add(classid, bg_app_id, proc_value);
-               stc_monitor_proc_update_ground(classid, bg_app_id, proc_value);
+               stc_plugin_monitor_add_proc(bg_classid, bg_app_id, proc_value);
+               stc_plugin_monitor_update_proc_ground(bg_classid, bg_app_id, proc_value);
 
                FREE(bg_app_id);
                break;
@@ -669,24 +672,39 @@ stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd,
                char *bg_app_id;
                stc_app_value_s app_value;
                stc_proc_value_s proc_value;
+               gboolean is_exist;
 
                memset(&app_value, 0, sizeof(stc_app_value_s));
                memset(&proc_value, 0, sizeof(stc_proc_value_s));
 
-               bg_app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, NULL);
+               classid = get_classid_by_app_id(app_id, FALSE);
+               is_exist = stc_plugin_monitor_lookup_app(classid);
+               if (is_exist) {
+                       app_value.type = app_type;
+                       app_value.state = STC_APP_STATE_FOREGROUND;
+                       app_value.processes = NULL;
 
-               app_value.type = app_type;
-               app_value.processes = NULL;
+                       proc_value.pid = pid;
+                       proc_value.ground = STC_APP_STATE_FOREGROUND;
 
-               proc_value.pid = pid;
-               proc_value.ground = STC_APP_STATE_BACKGROUND;
+                       stc_plugin_monitor_add_app(classid, app_id, pkg_id, app_value);
+                       stc_plugin_monitor_add_proc(classid, app_id, proc_value);
+               } else {
+                       bg_app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, NULL);
+                       classid = get_classid_by_app_id(bg_app_id, TRUE);
 
-               classid = get_classid_by_app_id(bg_app_id, TRUE);
+                       app_value.type = app_type;
+                       app_value.state = STC_APP_STATE_BACKGROUND;
+                       app_value.processes = NULL;
 
-               stc_monitor_app_add(classid, bg_app_id, pkg_id, app_value);
-               stc_monitor_proc_add(classid, bg_app_id, proc_value);
+                       proc_value.pid = pid;
+                       proc_value.ground = STC_APP_STATE_BACKGROUND;
 
-               FREE(bg_app_id);
+                       stc_plugin_monitor_add_app(classid, bg_app_id, pkg_id, app_value);
+                       stc_plugin_monitor_add_proc(classid, bg_app_id, proc_value);
+
+                       FREE(bg_app_id);
+               }
                break;
        }
        case STC_CMD_SET_TERMINATED:
@@ -700,7 +718,7 @@ stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd,
                if (classid == STC_UNKNOWN_CLASSID)
                        classid = get_classid_by_app_id(app_id, FALSE);
 
-               stc_monitor_proc_remove(classid, pid);
+               stc_plugin_monitor_remove_proc(classid, pid);
 
                FREE(bg_app_id);
                break;