Monitoring threads of a process using tgid and PROC_EVENT_FORK 27/154327/1
authorhyunuktak <hyunuk.tak@samsung.com>
Tue, 10 Oct 2017 05:17:01 +0000 (14:17 +0900)
committerhyunuktak <hyunuk.tak@samsung.com>
Tue, 10 Oct 2017 05:17:04 +0000 (14:17 +0900)
Change-Id: I7502aa99b04e06e7e7cce0067b43c5f400d99f4d
Signed-off-by: hyunuktak <hyunuk.tak@samsung.com>
src/monitor/include/stc-monitor.h
src/monitor/stc-app-lifecycle.c
src/monitor/stc-monitor.c

index 74a05b4..0ecce65 100755 (executable)
@@ -43,7 +43,6 @@ typedef struct {
  */
 typedef struct {
        pid_t pid;  /**< process id */
-       GArray *childs;  /**< child pids */
 } stc_process_key_s;
 
 /**
index 1787043..d43fe47 100755 (executable)
@@ -101,19 +101,46 @@ static proc_value_s * __proc_tree_lookup(const proc_key_s *key)
        return lookup;
 }
 
+static proc_value_s * __proc_tree_find_parent(proc_value_s *value)
+{
+       proc_value_s *parent = NULL;
+       proc_value_s *lookup = value;
+
+       do {
+               proc_key_s key;
+               key.pid = atoi(lookup->status[PROC_STATUS_PPID]);
+               lookup = __proc_tree_lookup(&key);
+               if (lookup != NULL)
+                       parent = lookup;
+       } while (lookup);
+
+       return parent;
+}
+
 static void __proc_tree_add(proc_key_s *key,
                            proc_value_s *value)
 {
+       proc_value_s *lookup;
+       proc_value_s *parent;
+
        if (proc_tree == NULL) {
                STC_LOGE("tree is null");
                return;
        }
 
+       lookup = g_tree_lookup(proc_tree, key);
+       if (lookup)
+               return;
+
        g_tree_insert(proc_tree, key, value);
 
-       stc_manager_app_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid,
-                                      value->cmdline, value->cmdline,
-                                      STC_APP_TYPE_SERVICE);
+       parent = __proc_tree_find_parent(value);
+       if (parent != NULL)
+               stc_manager_app_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid,
+                       parent->cmdline, parent->cmdline, STC_APP_TYPE_SERVICE);
+       else
+               stc_manager_app_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid,
+                       value->cmdline, value->cmdline, STC_APP_TYPE_SERVICE);
 }
 
 static void __proc_tree_remove(const proc_key_s *key)
@@ -314,7 +341,7 @@ stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd,
        return ret;
 }
 
-static void __process_event_exec(int tid, int pid)
+static void __process_event_fork(int tgid, int pid)
 {
        char cmdline[PROC_NAME_MAX] = {0, };
        char status[PROC_STATUS_CNT][PROC_BUF_MAX];
@@ -322,10 +349,8 @@ static void __process_event_exec(int tid, int pid)
        if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) &&
            STC_ERROR_NONE == proc_get_status(pid, status)) {
 
-               if (__check_excn(cmdline)) {
-                       STC_LOGI("[%s] monitoring is excepted", cmdline);
+               if (__check_excn(cmdline))
                        return;
-               }
 
                unsigned int i;
                proc_key_s *key;
@@ -344,33 +369,66 @@ static void __process_event_exec(int tid, int pid)
                        return;
                }
 
-               key->pid = pid;
+               key->pid = tgid;
                for (i = 0; i < PROC_STATUS_CNT; ++i)
                        g_strlcpy(value->status[i], status[i], sizeof(value->status[i]));
                g_strlcpy(value->cmdline, cmdline, sizeof(value->cmdline));
 
                __proc_tree_add(key, value);
+       }
+}
 
-               STC_LOGD("EXEC: pid[%d] tgid=[%d] cmdline[%s]", pid, tid, cmdline);
-               STC_LOGD("STATUS: name[%s] state[%s] tgid[%s] pid[%s] ppid[%s] tracerpid[%s]",
-                       status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TGID],
-                       status[PROC_STATUS_PID], status[PROC_STATUS_PPID], status[PROC_STATUS_TRACERPID]);
+static void __process_event_exec(int tgid, int pid)
+{
+       char cmdline[PROC_NAME_MAX] = {0, };
+       char status[PROC_STATUS_CNT][PROC_BUF_MAX];
+
+       if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) &&
+           STC_ERROR_NONE == proc_get_status(pid, status)) {
+
+               if (__check_excn(cmdline))
+                       return;
+
+               unsigned int i;
+               proc_key_s *key;
+               proc_value_s *value;
+
+               key = MALLOC0(proc_key_s, 1);
+               if (key == NULL) {
+                       STC_LOGE("memory allocation failed");
+                       return;
+               }
+
+               value = MALLOC0(proc_value_s, 1);
+               if (value == NULL) {
+                       STC_LOGE("memory allocation failed");
+                       FREE(key);
+                       return;
+               }
+
+               key->pid = tgid;
+               for (i = 0; i < PROC_STATUS_CNT; ++i)
+                       g_strlcpy(value->status[i], status[i], sizeof(value->status[i]));
+               g_strlcpy(value->cmdline, cmdline, sizeof(value->cmdline));
+
+               __proc_tree_add(key, value);
        }
 }
 
-static void __process_event_exit(int tid, int pid, int exit_code)
+static void __process_event_exit(int tgid, int pid, int exit_code)
 {
        proc_key_s key;
        proc_value_s *lookup;
 
-       key.pid = pid;
+       if (tgid != pid)
+               return;
+
+       key.pid = tgid;
        lookup = __proc_tree_lookup(&key);
        if (lookup == NULL) /* unmonitored process */
                return;
 
        __proc_tree_remove(&key);
-
-       STC_LOGD("EXIT:pid=%d,%d ruid=%d,euid=%d", pid, tid, exit_code);
 }
 
 static gboolean __process_nl_connector_message(GIOChannel *source,
@@ -403,13 +461,17 @@ static gboolean __process_nl_connector_message(GIOChannel *source,
        }
 
        switch (msg.proc_ev.what) {
+       case PROC_EVENT_FORK:
+               __process_event_fork(msg.proc_ev.event_data.fork.child_tgid,
+                                    msg.proc_ev.event_data.fork.child_pid);
+               break;
        case PROC_EVENT_EXEC:
-               __process_event_exec(msg.proc_ev.event_data.exec.process_pid,
-                                    msg.proc_ev.event_data.exec.process_tgid);
+               __process_event_exec(msg.proc_ev.event_data.exec.process_tgid,
+                                    msg.proc_ev.event_data.exec.process_pid);
                break;
        case PROC_EVENT_EXIT:
-               __process_event_exit(msg.proc_ev.event_data.exit.process_pid,
-                                    msg.proc_ev.event_data.exit.process_tgid,
+               __process_event_exit(msg.proc_ev.event_data.exit.process_tgid,
+                                    msg.proc_ev.event_data.exit.process_pid,
                                     msg.proc_ev.event_data.exit.exit_code);
                break;
        default:
index 0e34821..9fdf529 100755 (executable)
@@ -276,6 +276,7 @@ static void __rstns_tree_key_free(gpointer data)
        FREE(key);
 }
 
+#if STC_DEBUG_LOG
 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
                                               gpointer data)
 {
@@ -292,7 +293,6 @@ static void __processes_tree_printall(GTree *processes)
        g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
 }
 
-#if STC_DEBUG_LOG
 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
                                          gpointer data)
 {