From f503660e8a7664f738e81e49f5b8e58c10bed827 Mon Sep 17 00:00:00 2001 From: Kichan Kwon Date: Thu, 26 Jan 2017 14:19:57 +0900 Subject: heart : update DB to modify appid after grouping - After grouping, child will be considered to parent - Therefore, appid in DB should also be changed Change-Id: Ia205631af711f1573b0d5c7fa6363745cfee3b71 Signed-off-by: Kichan Kwon --- src/common/notifier.h | 5 ++ src/heart/heart-cpu.c | 19 ++++++ src/heart/include/logging.h | 1 + src/heart/logging.c | 142 +++++++++++++++++++++++++++++++++++--------- src/proc-stat/proc-main.c | 10 ++++ 5 files changed, 150 insertions(+), 27 deletions(-) diff --git a/src/common/notifier.h b/src/common/notifier.h index 70ffde93..3b55b5f8 100644 --- a/src/common/notifier.h +++ b/src/common/notifier.h @@ -50,6 +50,11 @@ enum notifier_type { RESOURCED_NOTIFIER_APP_SUSPEND_READY, RESOURCED_NOTIFIER_APP_SUSPEND, + /* + * group some applications + */ + RESOURCED_NOTIFIER_APP_GROUP, + /* * service status */ diff --git a/src/heart/heart-cpu.c b/src/heart/heart-cpu.c index 7743890b..71ec5262 100644 --- a/src/heart/heart-cpu.c +++ b/src/heart/heart-cpu.c @@ -1143,6 +1143,23 @@ int heart_cpu_get_appusage_list(int uid, GHashTable *lists, int top) return RESOURCED_ERROR_NONE; } +static int heart_cpu_update_appid(void *data) +{ + int pid; + char old_appid[MAX_APPID_LENGTH], new_appid[MAX_APPID_LENGTH]; + char *info; + + if (!data) { + _E("Invalid parameter"); + return RESOURCED_ERROR_INVALID_PARAMETER; + } + + info = (char*)data; + sscanf(info, "%d %s %s", &pid, old_appid, new_appid); + + return logging_modify_appid(CPU_NAME, old_appid, new_appid, pid); +} + static DBusMessage *edbus_heart_get_cpu_data(E_DBus_Object *obj, DBusMessage *msg) { int period, index, i, ret; @@ -1539,6 +1556,7 @@ static int heart_cpu_init(void *data) register_notifier(RESOURCED_NOTIFIER_SERVICE_LAUNCH, heart_cpu_service_launch); register_notifier(RESOURCED_NOTIFIER_APP_FOREGRD, heart_cpu_foreground_state); register_notifier(RESOURCED_NOTIFIER_APP_BACKGRD, heart_cpu_background_state); + register_notifier(RESOURCED_NOTIFIER_APP_GROUP, heart_cpu_update_appid); register_notifier(RESOURCED_NOTIFIER_DATA_UPDATE, heart_cpu_update_app_list); register_notifier(RESOURCED_NOTIFIER_DATA_RESET, heart_cpu_reset); @@ -1555,6 +1573,7 @@ static int heart_cpu_exit(void *data) unregister_notifier(RESOURCED_NOTIFIER_SERVICE_LAUNCH, heart_cpu_service_launch); unregister_notifier(RESOURCED_NOTIFIER_APP_FOREGRD, heart_cpu_foreground_state); unregister_notifier(RESOURCED_NOTIFIER_APP_BACKGRD, heart_cpu_background_state); + unregister_notifier(RESOURCED_NOTIFIER_APP_GROUP, heart_cpu_update_appid); unregister_notifier(RESOURCED_NOTIFIER_DATA_UPDATE, heart_cpu_update_app_list); unregister_notifier(RESOURCED_NOTIFIER_DATA_RESET, heart_cpu_reset); diff --git a/src/heart/include/logging.h b/src/heart/include/logging.h index d855ff56..80f5780d 100644 --- a/src/heart/include/logging.h +++ b/src/heart/include/logging.h @@ -129,6 +129,7 @@ int logging_write(int pid, char *name, char *appid, char *pkgid, time_t time, ch int logging_delete(int uid, char *name, char *data); int logging_read_foreach(char *name, char *appid, char *pkgid, time_t start_time, time_t end_time, logging_info_cb callback, void *user_data); +int logging_modify_appid(char *module_name, char *old_appid, char *new_appid, int pid); void logging_update(int force); void logging_save_to_storage(int force); int logging_sync(DBusMessage *reply); diff --git a/src/heart/logging.c b/src/heart/logging.c index 6f5c44ea..b0922738 100644 --- a/src/heart/logging.c +++ b/src/heart/logging.c @@ -62,6 +62,7 @@ #define DELETE_QUERY_WITH_DATA "DELETE from %s where data = ?;" #define INSERT_QUERY "INSERT INTO %s values (?, ?, ?, ?, ?);" #define SELECT_QUERY "SELECT * FROM %s WHERE time > %d AND time < %d;" +#define UPDATE_APPID_QUERY "UPDATE %s SET appid = \'%s\' WHERE appid = \'%s\';" #define SELECT_BEGIN_QUERY "SELECT * FROM %s " #define SELECT_WHERE_QUERY "WHERE" @@ -162,6 +163,35 @@ long logging_get_time_ms(void) return (tv.tv_sec * 1000 + tv.tv_usec / 1000); } +static int logging_convert_pid_to_uid(int pid) +{ + char buf[LOGGING_BUF_MAX]; + char key[20]; + FILE *fp = NULL; + int uid = -1; + + if (pid == PID_FOR_ROOT) + return UID_FOR_ROOT; + + snprintf(buf, LOGGING_BUF_MAX, "/proc/%d/status", pid); + fp = fopen(buf, "r"); + if (fp == NULL) { + _E("Fail to open status file"); + return -1; + } + + while (fgets(buf, LOGGING_BUF_MAX, fp) != NULL) { + sscanf(buf, "%s %d", key, &uid); + if (!strncmp(key, "Uid:", sizeof("Uid:") + 1)) { + fclose(fp); + return uid; + } + } + + fclose(fp); + return -1; +} + static struct logging_module *logging_find_module(char *name) { int i; @@ -620,36 +650,12 @@ int logging_operate(int uid, char *name, char *appid, char *pkgid, time_t time, int logging_write(int pid, char *name, char *appid, char *pkgid, time_t time, char *data) { - FILE *fp = NULL; - char buf[LOGGING_BUF_MAX]; - char key[20]; - int uid = -1; - int val; - - if (pid == PID_FOR_ROOT) - return logging_operate(UID_FOR_ROOT, name, appid, pkgid, time, data, INSERT); + int uid = logging_convert_pid_to_uid(pid); - snprintf(buf, LOGGING_BUF_MAX, "/proc/%d/status", pid); - fp = fopen(buf, "r"); - if (fp == NULL) { - _E("Fail to open status file"); + if (uid < 0) { + _E("Fail to get uid"); return RESOURCED_ERROR_FAIL; } - - while (fgets(buf, LOGGING_BUF_MAX, fp) != NULL) { - sscanf(buf, "%s %d", key, &val); - if (!strncmp(key, "Uid:", sizeof("Uid:") + 1)) { - uid = val; - break; - } - } - fclose(fp); - - if (uid == -1) { - _E("There is no uid in the status file"); - return RESOURCED_ERROR_FAIL; - } - return logging_operate(uid, name, appid, pkgid, time, data, INSERT); } @@ -1067,6 +1073,88 @@ static int logging_check_storage_size(const char *db_path) return RESOURCED_ERROR_NONE; } +int logging_modify_appid(char *module_name, char *old_appid, char *new_appid, int pid) +{ + struct logging_module *module = NULL; + int uid = -1; + struct logging_db *db = NULL; + char query[LOGGING_BUF_MAX]; + int i; + struct logging_table_form *entry; + + if (!module_name || !old_appid || !new_appid || pid < 0) { + _E("Invalid parameter"); + return RESOURCED_ERROR_INVALID_PARAMETER; + } + + module = logging_find_module(module_name); + if (!module) { + _E("There is no %s module", module_name); + return RESOURCED_ERROR_NO_DATA; + } + + uid = logging_convert_pid_to_uid(pid); + if (uid < 0) { + _E("Fail to get uid"); + return RESOURCED_ERROR_FAIL; + } + + if (pthread_mutex_lock(&logging_update_mutex)) { + _E("Failed to lock mutex"); + return RESOURCED_ERROR_FAIL; + } + + /* Modify cache */ + if (!g_queue_is_empty(module->cache)) { + pthread_mutex_lock(&(module->cache_mutex)); + for (i = 0; i < g_queue_get_length(module->cache); i++) { + entry = (struct logging_table_form *)g_queue_peek_nth( + module->cache, i); + if (entry->uid == uid && !strncmp(entry->appid, old_appid, strlen(old_appid))) + snprintf(entry->appid, MAX_APPID_LENGTH, "%s", new_appid); + } + pthread_mutex_unlock(&(module->cache_mutex)); + } + + /* Modify DB */ + switch (module->db_type) { + case SYSTEM_DEFAULT: + case SYSTEM_OWN: + db = g_hash_table_lookup(module->db_list, GINT_TO_POINTER(UID_FOR_ROOT)); + break; + case USER_DEFAULT: + case USER_OWN: + db = g_hash_table_lookup(module->db_list, (gpointer)(&uid)); + break; + default: + _E("Unknown db_type"); + pthread_mutex_unlock(&logging_update_mutex); + return RESOURCED_ERROR_FAIL; + } + + if (!db) { + _E("Failed to get DB"); + pthread_mutex_unlock(&logging_update_mutex); + return RESOURCED_ERROR_NO_DATA; + } + + snprintf(query, LOGGING_BUF_MAX, UPDATE_APPID_QUERY, + module_name, new_appid, old_appid); + + if (sqlite3_exec(db->file, query, NULL, NULL, NULL) != SQLITE_OK) { + _E("Failed to apply query"); + pthread_mutex_unlock(&logging_update_mutex); + return RESOURCED_ERROR_DB_FAILED; + } + + if (pthread_mutex_unlock(&logging_update_mutex)) { + _E("Failed to unlock mutex"); + return RESOURCED_ERROR_FAIL; + } + + return RESOURCED_ERROR_NONE; +} + void logging_update(int force) { int i, ret; diff --git a/src/proc-stat/proc-main.c b/src/proc-stat/proc-main.c index 7bce57fe..481a52dc 100644 --- a/src/proc-stat/proc-main.c +++ b/src/proc-stat/proc-main.c @@ -1349,6 +1349,7 @@ void proc_set_group(pid_t ownerpid, pid_t childpid, char *pkgname) struct proc_program_info *ppi; struct proc_app_info *pai, *owner; struct proc_status ps = {0}; + char group_info[512]; if (ownerpid <= 0 || childpid <= 0) return; @@ -1370,6 +1371,15 @@ void proc_set_group(pid_t ownerpid, pid_t childpid, char *pkgname) return; if (pkgname && pai) { + /* + * After grouping, resourced considers child pai with parent pai + * Therefore, child appid in HEART-CPU DB should be changed + * to parent appid + */ + snprintf(group_info, sizeof(group_info), "%d %s %s", + childpid, pai->appid, owner->appid); + resourced_notify(RESOURCED_NOTIFIER_APP_GROUP, group_info); + /* * when child application with appid has owner pai and ppi * check and remove them -- cgit v1.2.3