[Fix] Fetch proper app_id for creation of cgroup. 00/153900/3
authorNishant Chaprana <n.chaprana@samsung.com>
Wed, 11 Oct 2017 05:32:29 +0000 (11:02 +0530)
committerNishant Chaprana <n.chaprana@samsung.com>
Wed, 11 Oct 2017 05:33:46 +0000 (11:03 +0530)
Description: The app_id was not extracted properly from procfs.
Some processes run with help of other daemons, for example
org.tizen.browser app is execued using efl_webprocess.

In this patch for resolving above issue,
if /proc/<pid>/attr/current file contains "User::Pkg::" wildcard string,
then we extract app_id from above file.
Otherwise we read /proc/<pid>/cmdline and fetch process_name as app_id.

Change-Id: I0eb46f4f675cec3f2323c0ab05388fec46c7781f
Signed-off-by: Nishant Chaprana <n.chaprana@samsung.com>
include/stc-plugin.h
packaging/stc-manager.spec
plugin/stc-plugin.c
src/helper/helper-inotify.h
src/helper/helper-procfs.c
src/helper/helper-procfs.h
src/monitor/include/stc-monitor.h
src/monitor/stc-app-lifecycle.c
src/monitor/stc-monitor.c

index 4d843b8..53cfaec 100755 (executable)
@@ -21,7 +21,7 @@
 #include "stc-error.h"
 #include "stc-manager.h"
 
-typedef stc_error_e (*stc_plugin_app_state_changed_cb)(stc_cmd_type_e cmd,
+typedef stc_error_e(*stc_plugin_app_state_changed_cb) (stc_cmd_type_e cmd,
                                                       pid_t pid,
                                                       const gchar *app_id,
                                                       const gchar *pkg_id,
index cf69262..9500d61 100644 (file)
@@ -1,6 +1,6 @@
 Name:       stc-manager
 Summary:    STC(Smart Traffic Control) manager
-Version:    0.0.36
+Version:    0.0.37
 Release:    0
 Group:      Network & Connectivity/Other
 License:    Apache-2.0
index 916a75e..3587db1 100755 (executable)
@@ -74,13 +74,12 @@ static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection,
        g_variant_get(parameters, AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE,
                      &pid, &appid, &pkgid, &statstr, &pkgtype);
 
-       if (!strncmp(statstr, "fg", 2)) {
+       if (!strncmp(statstr, "fg", 2))
                status = STC_CMD_SET_FOREGRD;
-       } else if (!strncmp(statstr, "bg", 2)) {
+       else if (!strncmp(statstr, "bg", 2))
                status = STC_CMD_SET_BACKGRD;
-       } else {
+       else
                goto out;
-       }
 
        if (!strncmp(pkgtype, "svc", 3))
                apptype = STC_APP_TYPE_SERVICE;
index 1fe9d66..9ee6995 100755 (executable)
@@ -20,8 +20,8 @@
 #include <sys/inotify.h>
 
 struct inotify_event;
-typedef void (* inotify_event_cb) (struct inotify_event *event,
-                                       const char *ident);
+typedef void (*inotify_event_cb) (struct inotify_event *event,
+                                 const char *ident);
 
 int inotify_register(const char *path, inotify_event_cb callback);
 void inotify_deregister(const char *path);
index 168c34a..9dcd36e 100755 (executable)
 
 #define USRAPPS "/usr/apps/"
 
+static int __proc_get_data(char *path, char *buf, int len)
+{
+       _cleanup_fclose_ FILE *fp = NULL;
+
+       fp = fopen(path, "r");
+       if (fp == NULL)
+               return STC_ERROR_FAIL;
+
+       if (fgets(buf, len - 1, fp) == NULL)
+               return STC_ERROR_FAIL;
+
+       return STC_ERROR_NONE;
+}
+
 int proc_get_cmdline(pid_t pid, char *cmdline)
 {
-       char buf[PROC_BUF_MAX];
-       char cmdline_buf[PROC_NAME_MAX];
        char *filename;
-       FILE *fp;
        char *token = NULL;
        char *saveptr = NULL;
+       char path_buf[PROC_BUF_MAX] = {0, };
+       char cmdline_buf[PROC_NAME_MAX] = {0, };
+       stc_error_e ret = STC_ERROR_NONE;
 
-       snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
-       fp = fopen(buf, "r");
-       if (fp == NULL)
-               return STC_ERROR_FAIL;
+       snprintf(path_buf, sizeof(path_buf), "/proc/%d/cmdline", pid);
 
-       if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
-               fclose(fp);
-               return STC_ERROR_FAIL;
-       }
-       fclose(fp);
+       ret = __proc_get_data(path_buf, cmdline_buf, PROC_NAME_MAX);
+       if (ret != STC_ERROR_NONE)
+               return ret;
 
        if (g_strstr_len(cmdline_buf, strlen(USRAPPS), USRAPPS) != NULL) {
                /* Application */
@@ -80,100 +89,25 @@ int proc_get_cmdline(pid_t pid, char *cmdline)
        }
 
        strncpy(cmdline, filename, PROC_NAME_MAX-1);
-
        return STC_ERROR_NONE;
 }
 
-pid_t find_pid_from_cmdline(char *cmdline)
-{
-       pid_t pid = -1, foundpid = -1;
-       int ret = 0;
-       DIR *dp;
-       struct dirent *dentry;
-       char appname[PROC_NAME_MAX];
-
-       dp = opendir("/proc");
-       if (!dp) {
-               STC_LOGE("BACKGRD MANAGE : fail to open /proc");
-               return STC_ERROR_FAIL;
-       }
-
-       while ((dentry = readdir(dp)) != NULL) {
-               if (!isdigit(dentry->d_name[0]))
-                       continue;
-
-               pid = atoi(dentry->d_name);
-               if (!pid)
-                       continue;
-               ret = proc_get_cmdline(pid, appname);
-               if (ret == STC_ERROR_NONE) {
-                       if (!strncmp(cmdline, appname, strlen(appname)+1)) {
-                               foundpid = pid;
-                               break;
-                       }
-               }
-       }
-       closedir(dp);
-       return foundpid;
-}
-
 int proc_get_label(pid_t pid, char *label)
 {
-       char buf[PROC_BUF_MAX];
-       FILE *fp;
+       char path_buf[PROC_BUF_MAX] = {0, };
+       char label_buf[PROC_NAME_MAX] = {0, };
+       stc_error_e ret = STC_ERROR_NONE;
 
-       snprintf(buf, sizeof(buf), "/proc/%d/attr/current", pid);
-       fp = fopen(buf, "r");
-       if (fp == NULL)
-               return STC_ERROR_FAIL;
+       snprintf(path_buf, sizeof(path_buf), "/proc/%d/attr/current", pid);
 
-       if (fgets(label, PROC_NAME_MAX-1, fp) == NULL) {
-               fclose(fp);
-               return STC_ERROR_FAIL;
-       }
-       fclose(fp);
-       return STC_ERROR_NONE;
-}
+       ret = __proc_get_data(path_buf, label_buf, PROC_NAME_MAX);
+       if (ret != STC_ERROR_NONE)
+               return ret;
 
-int proc_get_exepath(pid_t pid, char *buf, int len)
-{
-       char path[PROC_BUF_MAX];
-       int ret = 0;
-
-       snprintf(path, sizeof(path), "/proc/%d/exe", pid);
-       ret = readlink(path, buf, len-1);
-       if (ret > 0)
-               buf[ret] = '\0';
-       else
-               buf[0] = '\0';
+       strncpy(label, label_buf, PROC_NAME_MAX-1);
        return STC_ERROR_NONE;
 }
 
-static int proc_get_data(char *path, char *buf, int len)
-{
-       _cleanup_close_ int fd = -1;
-       int ret;
-
-       fd = open(path, O_RDONLY);
-       if (fd < 0)
-               return STC_ERROR_FAIL;
-
-       ret = read(fd, buf, len-1);
-       if (ret < 0) {
-               buf[0] = '\0';
-               return STC_ERROR_FAIL;
-       }
-       buf[ret] = '\0';
-       return STC_ERROR_NONE;
-}
-
-int proc_get_raw_cmdline(pid_t pid, char *buf, int len)
-{
-       char path[PROC_BUF_MAX];
-       snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
-       return proc_get_data(path, buf, len);
-}
-
 int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX])
 {
        unsigned int i;
index 55c6a03..c6c9b90 100755 (executable)
 int proc_get_cmdline(pid_t pid, char *cmdline);
 
 /**
- * @desc find pid with /proc/{pid}/cmdline
- * it returns first entry when many pids have same cmdline
- * @return negative value if error
- */
-pid_t find_pid_from_cmdline(char *cmdline);
-
-/**
  * @desc get smack subject label from /proc/{pid}/attr/current
  * this label can indicate package name about child processes
  * @return negative value if error or pid doesn't exist
@@ -43,18 +36,6 @@ pid_t find_pid_from_cmdline(char *cmdline);
 int proc_get_label(pid_t pid, char *label);
 
 /**
- * @desc get command line from /proc/{pid}/cmdline without any truncation
- * @return negative value if error
- */
-int proc_get_raw_cmdline(pid_t pid, char *buf, int len);
-
-/**
- * @desc get symblolic link about /proc/{pid}/exe
- * @return negative value if error
- */
-int proc_get_exepath(pid_t pid, char *buf, int len);
-
-/**
  * @desc get status from /proc/{pid}/status
  * @return negative value if error
  */
index 0ecce65..ee2213f 100755 (executable)
@@ -148,7 +148,7 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info);
 
 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info);
 
-stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline);
+stc_error_e stc_monitor_check_excn_by_app_id(char *app_id);
 
 int stc_monitor_get_counter_socket(void);
 
index eb18816..81b976b 100755 (executable)
@@ -36,7 +36,7 @@ typedef struct {
 } proc_key_s;
 
 typedef struct {
-       char cmdline[PROC_NAME_MAX];
+       char *app_id;
        char status[PROC_STATUS_CNT][PROC_BUF_MAX];
 } proc_value_s;
 
@@ -78,6 +78,7 @@ static void __proc_tree_value_free(gpointer data)
 {
        proc_value_s *value = (proc_value_s *)data;
 
+       FREE(value->app_id);
        FREE(value);
 }
 
@@ -108,8 +109,8 @@ static gboolean __proc_tree_foreach_print(gpointer key, gpointer value,
        proc_value_s *proc_value = (proc_value_s *)value;
 
        STC_LOGD("Proc pid [\033[1;33m%d\033[0;m] ppid [\033[1;35m%s\033[0;m] "
-               "cmdline [\033[0;34m%s\033[0;m]", proc_key->pid,
-               proc_value->status[PROC_STATUS_PPID], proc_value->cmdline);
+               "app_id [\033[0;34m%s\033[0;m]", proc_key->pid,
+               proc_value->status[PROC_STATUS_PPID], proc_value->app_id);
 
        return FALSE;
 }
@@ -135,9 +136,9 @@ static proc_value_s * __proc_tree_find_parent(proc_value_s *value)
        if (STC_DEBUG_LOG) {
                if (parent != NULL)
                        STC_LOGD("\033[0;35mPARENT\033[0;m: tgid[\033[1;33m%s\033[0;m] pid[%s] "
-                               "ppid[\033[1;35m%s\033[0;m] cmdline[\033[0;34m%s\033[0;m] name[%s]",
+                               "ppid[\033[1;35m%s\033[0;m] app_id[\033[0;34m%s\033[0;m] name[%s]",
                                parent->status[PROC_STATUS_TGID], parent->status[PROC_STATUS_PID],
-                               parent->status[PROC_STATUS_PPID], parent->cmdline,
+                               parent->status[PROC_STATUS_PPID], parent->app_id,
                                parent->status[PROC_STATUS_NAME]);
        }
 
@@ -159,13 +160,13 @@ static void __proc_tree_add(proc_key_s *key,
        if (lookup) {
                if (STC_DEBUG_LOG)
                        STC_LOGD("LOOKUP: tgid[\033[1;33m%s\033[0;m] pid[%s] ppid[\033[1;35m%s\033[0;m] "
-                               "cmdline[\033[0;34m%s\033[0;m] name[%s]", lookup->status[PROC_STATUS_TGID],
+                               "app_id[\033[0;34m%s\033[0;m] name[%s]", lookup->status[PROC_STATUS_TGID],
                                lookup->status[PROC_STATUS_PID], lookup->status[PROC_STATUS_PPID],
-                               lookup->cmdline, lookup->status[PROC_STATUS_NAME]);
+                               lookup->app_id, lookup->status[PROC_STATUS_NAME]);
                return;
        }
 
-       STC_LOGD("cmdline [%s] pid[%s] ppid[%s]", value->cmdline,
+       STC_LOGD("app_id [%s] pid[%s] ppid[%s]", value->app_id,
                value->status[PROC_STATUS_PID], value->status[PROC_STATUS_PPID]);
 
        g_tree_insert(proc_tree, key, value);
@@ -176,10 +177,10 @@ static void __proc_tree_add(proc_key_s *key,
        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);
+                       parent->app_id, parent->app_id, 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);
+                       value->app_id, value->app_id, STC_APP_TYPE_SERVICE);
 }
 
 static void __proc_tree_remove(const proc_key_s *key)
@@ -198,14 +199,14 @@ static void __proc_tree_remove(const proc_key_s *key)
                __proc_tree_printall();
 }
 
-static gboolean __check_excn(char *cmdline)
+static gboolean __check_excn(char *app_id)
 {
        stc_error_e ret = STC_ERROR_NONE;
 
-       if (cmdline[0] == '(')
+       if (app_id && app_id[0] == '(')
                return TRUE;
 
-       ret = stc_monitor_check_excn_by_cmdline(cmdline);
+       ret = stc_monitor_check_excn_by_app_id(app_id);
        if (ret == STC_ERROR_NO_DATA)
                return FALSE;
        else
@@ -390,24 +391,41 @@ stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd,
        return ret;
 }
 
-static void __process_event_fork(int tgid, int pid)
+static char * __process_get_app_id(int pid)
 {
+       char label[PROC_NAME_MAX] = {0, };
        char cmdline[PROC_NAME_MAX] = {0, };
+       char *app_id = NULL;
+
+       if (STC_ERROR_NONE == proc_get_label(pid, label)) {
+               app_id = strstr(label, "User::Pkg::");
+               if (app_id != NULL)
+                       return g_strdup(app_id + 11);
+       }
+
+       if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline))
+               return g_strdup(cmdline);
+
+       return NULL;
+}
+
+static void __process_event_fork(int tgid, int pid)
+{
        char status[PROC_STATUS_CNT][PROC_BUF_MAX];
+       char *app_id = __process_get_app_id(pid);
 
-       if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) &&
-           STC_ERROR_NONE == proc_get_status(pid, status)) {
+       if (app_id != NULL && proc_get_status(pid, status) == STC_ERROR_NONE) {
 
-               if (__check_excn(cmdline)) {
+               unsigned int i;
+               proc_key_s *key = NULL;
+               proc_value_s *value = NULL;
+
+               if (__check_excn(app_id)) {
                        if (STC_DEBUG_LOG)
-                               STC_LOGD("[%s] monitoring is excepted", cmdline);
+                               STC_LOGD("[%s] monitoring is excepted", app_id);
                        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");
@@ -422,13 +440,13 @@ static void __process_event_fork(int tgid, int pid)
                }
 
                key->pid = tgid;
+               value->app_id = app_id;
                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));
 
                if (STC_DEBUG_LOG) {
                        STC_LOGD("\033[1;34mFORK\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] "
-                               "cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, pid);
+                               "app_id[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], app_id, pid);
                        STC_LOGD("STATUS: tgid[%s] pid[%s] ppid[%s] name[%s] state[%s] tracerpid[%s]",
                                status[PROC_STATUS_TGID], status[PROC_STATUS_PID], status[PROC_STATUS_PPID],
                                status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]);
@@ -440,22 +458,21 @@ static void __process_event_fork(int tgid, int pid)
 
 static void __process_event_exec(int tgid, int pid)
 {
-       char cmdline[PROC_NAME_MAX] = {0, };
        char status[PROC_STATUS_CNT][PROC_BUF_MAX];
+       char *app_id = __process_get_app_id(pid);
 
-       if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) &&
-           STC_ERROR_NONE == proc_get_status(pid, status)) {
+       if (app_id != NULL && proc_get_status(pid, status) == STC_ERROR_NONE) {
 
-               if (__check_excn(cmdline)) {
+               unsigned int i;
+               proc_key_s *key = NULL;
+               proc_value_s *value = NULL;
+
+               if (__check_excn(app_id)) {
                        if (STC_DEBUG_LOG)
-                               STC_LOGD("[%s] monitoring is excepted", cmdline);
+                               STC_LOGD("[%s] monitoring is excepted", app_id);
                        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");
@@ -470,13 +487,13 @@ static void __process_event_exec(int tgid, int pid)
                }
 
                key->pid = tgid;
+               value->app_id = app_id;
                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));
 
                if (STC_DEBUG_LOG) {
                        STC_LOGD("\033[1;32mEXEC\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] "
-                               "cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, pid);
+                               "app_id[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], app_id, pid);
                        STC_LOGD("STATUS: tgid[%s] pid[%s] ppid[%s] name[%s] state[%s] tracerpid[%s]",
                                status[PROC_STATUS_TGID], status[PROC_STATUS_PID], status[PROC_STATUS_PPID],
                                status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]);
index 1cc0b5e..2530dd0 100755 (executable)
@@ -1948,13 +1948,13 @@ stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
        return ret;
 }
 
-stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
+stc_error_e stc_monitor_check_excn_by_app_id(char *app_id)
 {
        ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
 
        char *exe_type = NULL;
 
-       exe_type = g_hash_table_lookup(g_system->excns_hash, cmdline);
+       exe_type = g_hash_table_lookup(g_system->excns_hash, app_id);
        if (!exe_type)
                return STC_ERROR_NO_DATA;