#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 */
}
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;
} proc_key_s;
typedef struct {
- char *app_id;
+ char cmdline[PROC_NAME_MAX];
char status[PROC_STATUS_CNT][PROC_BUF_MAX];
} proc_value_s;
{
proc_value_s *value = (proc_value_s *)data;
- FREE(value->app_id);
FREE(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;
}
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]);
}
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);
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)
__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
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");
}
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]);
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");
}
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]);