Revert "[Fix] Fetch proper app_id for creation of cgroup." 56/155756/1
authortaesub kim <taesub.kim@samsung.com>
Mon, 16 Oct 2017 05:31:20 +0000 (05:31 +0000)
committertaesub kim <taesub.kim@samsung.com>
Mon, 16 Oct 2017 05:31:20 +0000 (05:31 +0000)
This reverts commit 951a7b00437655c92d1a998c4e1ed4353010eb0b.

Change-Id: I10c8ea979a54ca189b0eab6c565e7320307e85e0

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 53cfaec..4d843b8 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 9500d61..cf69262 100644 (file)
@@ -1,6 +1,6 @@
 Name:       stc-manager
 Summary:    STC(Smart Traffic Control) manager
-Version:    0.0.37
+Version:    0.0.36
 Release:    0
 Group:      Network & Connectivity/Other
 License:    Apache-2.0
index 3587db1..916a75e 100755 (executable)
@@ -74,12 +74,13 @@ 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 9ee6995..1fe9d66 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 9dcd36e..168c34a 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(path_buf, sizeof(path_buf), "/proc/%d/cmdline", pid);
+       snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
+       fp = fopen(buf, "r");
+       if (fp == NULL)
+               return STC_ERROR_FAIL;
 
-       ret = __proc_get_data(path_buf, cmdline_buf, PROC_NAME_MAX);
-       if (ret != STC_ERROR_NONE)
-               return ret;
+       if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
+               fclose(fp);
+               return STC_ERROR_FAIL;
+       }
+       fclose(fp);
 
        if (g_strstr_len(cmdline_buf, strlen(USRAPPS), USRAPPS) != NULL) {
                /* Application */
@@ -89,25 +80,100 @@ 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 path_buf[PROC_BUF_MAX] = {0, };
-       char label_buf[PROC_NAME_MAX] = {0, };
-       stc_error_e ret = STC_ERROR_NONE;
+       char buf[PROC_BUF_MAX];
+       FILE *fp;
 
-       snprintf(path_buf, sizeof(path_buf), "/proc/%d/attr/current", pid);
+       snprintf(buf, sizeof(buf), "/proc/%d/attr/current", pid);
+       fp = fopen(buf, "r");
+       if (fp == NULL)
+               return STC_ERROR_FAIL;
 
-       ret = __proc_get_data(path_buf, label_buf, PROC_NAME_MAX);
-       if (ret != STC_ERROR_NONE)
-               return ret;
+       if (fgets(label, PROC_NAME_MAX-1, fp) == NULL) {
+               fclose(fp);
+               return STC_ERROR_FAIL;
+       }
+       fclose(fp);
+       return STC_ERROR_NONE;
+}
 
-       strncpy(label, label_buf, PROC_NAME_MAX-1);
+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';
        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 c6c9b90..55c6a03 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
@@ -36,6 +43,18 @@ int proc_get_cmdline(pid_t pid, 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 ee2213f..0ecce65 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_app_id(char *app_id);
+stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline);
 
 int stc_monitor_get_counter_socket(void);
 
index 81b976b..eb18816 100755 (executable)
@@ -36,7 +36,7 @@ typedef struct {
 } proc_key_s;
 
 typedef struct {
-       char *app_id;
+       char cmdline[PROC_NAME_MAX];
        char status[PROC_STATUS_CNT][PROC_BUF_MAX];
 } proc_value_s;
 
@@ -78,7 +78,6 @@ static void __proc_tree_value_free(gpointer data)
 {
        proc_value_s *value = (proc_value_s *)data;
 
-       FREE(value->app_id);
        FREE(value);
 }
 
@@ -109,8 +108,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] "
-               "app_id [\033[0;34m%s\033[0;m]", proc_key->pid,
-               proc_value->status[PROC_STATUS_PPID], proc_value->app_id);
+               "cmdline [\033[0;34m%s\033[0;m]", proc_key->pid,
+               proc_value->status[PROC_STATUS_PPID], proc_value->cmdline);
 
        return FALSE;
 }
@@ -136,9 +135,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] app_id[\033[0;34m%s\033[0;m] name[%s]",
+                               "ppid[\033[1;35m%s\033[0;m] cmdline[\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->app_id,
+                               parent->status[PROC_STATUS_PPID], parent->cmdline,
                                parent->status[PROC_STATUS_NAME]);
        }
 
@@ -160,13 +159,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] "
-                               "app_id[\033[0;34m%s\033[0;m] name[%s]", lookup->status[PROC_STATUS_TGID],
+                               "cmdline[\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->app_id, lookup->status[PROC_STATUS_NAME]);
+                               lookup->cmdline, lookup->status[PROC_STATUS_NAME]);
                return;
        }
 
-       STC_LOGD("app_id [%s] pid[%s] ppid[%s]", value->app_id,
+       STC_LOGD("cmdline [%s] pid[%s] ppid[%s]", value->cmdline,
                value->status[PROC_STATUS_PID], value->status[PROC_STATUS_PPID]);
 
        g_tree_insert(proc_tree, key, value);
@@ -177,10 +176,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->app_id, parent->app_id, STC_APP_TYPE_SERVICE);
+                       parent->cmdline, parent->cmdline, STC_APP_TYPE_SERVICE);
        else
                stc_manager_app_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid,
-                       value->app_id, value->app_id, STC_APP_TYPE_SERVICE);
+                       value->cmdline, value->cmdline, STC_APP_TYPE_SERVICE);
 }
 
 static void __proc_tree_remove(const proc_key_s *key)
@@ -199,14 +198,14 @@ static void __proc_tree_remove(const proc_key_s *key)
                __proc_tree_printall();
 }
 
-static gboolean __check_excn(char *app_id)
+static gboolean __check_excn(char *cmdline)
 {
        stc_error_e ret = STC_ERROR_NONE;
 
-       if (app_id && app_id[0] == '(')
+       if (cmdline[0] == '(')
                return TRUE;
 
-       ret = stc_monitor_check_excn_by_app_id(app_id);
+       ret = stc_monitor_check_excn_by_cmdline(cmdline);
        if (ret == STC_ERROR_NO_DATA)
                return FALSE;
        else
@@ -391,41 +390,24 @@ stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd,
        return ret;
 }
 
-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 cmdline[PROC_NAME_MAX] = {0, };
        char status[PROC_STATUS_CNT][PROC_BUF_MAX];
-       char *app_id = __process_get_app_id(pid);
 
-       if (app_id != NULL && proc_get_status(pid, status) == STC_ERROR_NONE) {
+       if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) &&
+           STC_ERROR_NONE == proc_get_status(pid, status)) {
 
-               unsigned int i;
-               proc_key_s *key = NULL;
-               proc_value_s *value = NULL;
-
-               if (__check_excn(app_id)) {
+               if (__check_excn(cmdline)) {
                        if (STC_DEBUG_LOG)
-                               STC_LOGD("[%s] monitoring is excepted", app_id);
+                               STC_LOGD("[%s] monitoring is excepted", 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");
@@ -440,13 +422,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] "
-                               "app_id[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], app_id, pid);
+                               "cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, 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]);
@@ -458,21 +440,22 @@ 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 (app_id != NULL && proc_get_status(pid, status) == STC_ERROR_NONE) {
+       if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) &&
+           STC_ERROR_NONE == proc_get_status(pid, status)) {
 
-               unsigned int i;
-               proc_key_s *key = NULL;
-               proc_value_s *value = NULL;
-
-               if (__check_excn(app_id)) {
+               if (__check_excn(cmdline)) {
                        if (STC_DEBUG_LOG)
-                               STC_LOGD("[%s] monitoring is excepted", app_id);
+                               STC_LOGD("[%s] monitoring is excepted", 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");
@@ -487,13 +470,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] "
-                               "app_id[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], app_id, pid);
+                               "cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, 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 2530dd0..1cc0b5e 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_app_id(char *app_id)
+stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
 {
        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, app_id);
+       exe_type = g_hash_table_lookup(g_system->excns_hash, cmdline);
        if (!exe_type)
                return STC_ERROR_NO_DATA;