Parse 'PerProcess' section
[platform/core/system/resourced.git] / src / common / config-parser.c
index 6551dff..b6b85b5 100644 (file)
 #include "util.h"
 #include "trace.h"
 #include "config-parser.h"
+#include "proc-common.h"
 
-#define MAX_SECTION    64
+#define MAX_SECTION            64
+#define CPU_INIT_PRIO  100
+
+static GSList *app_conf_info_list;
+static GSList *service_conf_info_list;
+
+static struct proc_conf_info* process_exist(const char *name, enum proc_type proc_type)
+{
+       GSList *iter;
+       bool found = false;
+       struct proc_conf_info *pci = NULL;
+
+       if (proc_type != APP_TYPE && proc_type != SERVICE_TYPE)
+               return pci;
+
+       gslist_for_each_item(iter,
+               proc_type == APP_TYPE ? app_conf_info_list : service_conf_info_list) {
+               pci     = (struct proc_conf_info *)iter->data;
+               if (!strncmp(pci->name, name, strlen(name)+1)) {
+                       found = true;
+                       break;
+               }
+       }
+
+       if (!found)
+               pci = NULL;
+
+       return pci;
+}
+
+static int vendor_config(struct parse_result *result, void *user_data)
+{
+       int *config_type = (int *)user_data;
+       static struct proc_conf_info *pci = NULL;
+
+       if (!result || !user_data)
+               return RESOURCED_ERROR_INVALID_PARAMETER;
+
+       if (strncmp(result->section, PER_PROCESS_SECTION_CONF, strlen(PER_PROCESS_SECTION_CONF)+1))
+               return RESOURCED_ERROR_NONE;
+
+       if (!strncmp(result->name, SERVICE_NAME_CONF, strlen(SERVICE_NAME_CONF)+1) ||
+               !strncmp(result->name, APP_NAME_CONF, strlen(APP_NAME_CONF)+1)) {
+               pci = process_exist(result->value, result->name[0] == 'A' ? APP_TYPE : SERVICE_TYPE);
+               if (pci == NULL) {
+                       pci = (struct proc_conf_info *)calloc(1, sizeof(struct proc_conf_info));
+                       if (pci == NULL) {
+                               _E("Failed to allocate memory during parsing vendor configurations");
+                               return RESOURCED_ERROR_OUT_OF_MEMORY;
+                       }
+                       if (result->name[0] == 'A') {
+                               pci->proc_type = APP_TYPE;
+                               app_conf_info_list = g_slist_prepend(app_conf_info_list, (gpointer)pci);
+                       }
+                       else {
+                               pci->proc_type = SERVICE_TYPE;
+                               service_conf_info_list = g_slist_prepend(service_conf_info_list, (gpointer)pci);
+                       }
+                       pci->mem_type = CGROUP_TOP;
+                       pci->cpu_type = CGROUP_TOP;
+                       pci->cpu_priority = CPU_INIT_PRIO;
+                       strncpy(pci->name, result->value, sizeof(pci->name)-1);
+               }
+       }
+       else if (!strncmp(result->name, CPU_CGROUP_NAME_CONF, strlen(CPU_CGROUP_NAME_CONF)+1) &&
+                       *config_type == LIMITER_CONFIG) {
+               if (!pci) {
+                       _E("process configuration information pointer should not be NULL");
+                       return RESOURCED_ERROR_FAIL;
+               }
+
+               if (!strncmp(result->value, CGROUP_VIP_VALUE_CONF,
+                       strlen(CGROUP_VIP_VALUE_CONF) +1)) {
+                       pci->cpu_type = CGROUP_VIP;
+               }
+               else if (!strncmp(result->value, CGROUP_HIGH_VALUE_CONF,
+                       strlen(CGROUP_HIGH_VALUE_CONF) +1)) {
+                       pci->cpu_type = CGROUP_HIGH;
+               }
+               else if (!strncmp(result->value, CGROUP_MEDIUM_VALUE_CONF,
+                       strlen(CGROUP_MEDIUM_VALUE_CONF) +1)) {
+                       pci->cpu_type = CGROUP_MEDIUM;
+               }
+               else if (!strncmp(result->value, CGROUP_LOW_VALUE_CONF,
+                       strlen(CGROUP_LOW_VALUE_CONF) +1)) {
+                       pci->cpu_type = CGROUP_LOW;
+               }
+               else {
+                       _E("invalid parameter (%s)", result->value);
+                       return RESOURCED_ERROR_INVALID_PARAMETER;
+               }
+       }
+       else if (!strncmp(result->name, MEM_CGROUP_NAME_CONF, strlen(MEM_CGROUP_NAME_CONF)+1) &&
+                       *config_type == LIMITER_CONFIG) {
+               if (!pci) {
+                       _E("process configuration information pointer should not be NULL");
+                       return RESOURCED_ERROR_FAIL;
+               }
+
+               if (!strncmp(result->value, CGROUP_VIP_VALUE_CONF,
+                       strlen(CGROUP_VIP_VALUE_CONF) +1)) {
+                       pci->mem_type = CGROUP_VIP;
+               }
+               else if (!strncmp(result->value, CGROUP_HIGH_VALUE_CONF,
+                       strlen(CGROUP_HIGH_VALUE_CONF) +1)) {
+                       pci->mem_type = CGROUP_HIGH;
+               }
+               else if (!strncmp(result->value, CGROUP_MEDIUM_VALUE_CONF,
+                       strlen(CGROUP_MEDIUM_VALUE_CONF) +1)) {
+                       pci->mem_type = CGROUP_MEDIUM;
+               }
+               else if (!strncmp(result->value, CGROUP_LOW_VALUE_CONF,
+                       strlen(CGROUP_LOW_VALUE_CONF) +1)) {
+                       pci->mem_type = CGROUP_LOW;
+               }
+               else {
+                       _E("invalid parameter (%s)", result->value);
+                       return RESOURCED_ERROR_INVALID_PARAMETER;
+               }
+       }
+       else if (!strncmp(result->name, MEM_LIMIT_ACTION_NAME_CONF,
+               strlen(MEM_LIMIT_ACTION_NAME_CONF)+1) && *config_type == LIMITER_CONFIG) {
+               if (!pci) {
+                       _E("process configuration information pointer should not be NULL");
+                       return RESOURCED_ERROR_FAIL;
+               }
+
+               char *ptr = strchr(result->value, ',');
+               if (ptr == NULL) {
+                       _E("Cannot find ',' in the string (%s)", result->value);
+                       return RESOURCED_ERROR_FAIL;
+               }
+
+               char *second_value = ptr + 1;
+               char temp;
+
+               if (result->value > (ptr - 2)) {
+                       _E("Size of string should be larger than 2");
+                       return RESOURCED_ERROR_FAIL;
+               }
+
+               if (*(ptr - 1) == 'B') {
+                       temp = *(ptr - 2);
+                       *(ptr - 2) = '\0';
+
+                       if (temp == 'G') {
+                               pci->mem_action.memory = GBYTE_TO_MBYTE(atoi(result->value));
+                       }
+                       else if (temp == 'M') {
+                               pci->mem_action.memory = atoi(result->value);
+                       }
+                       else if (temp == 'K') {
+                               pci->mem_action.memory = KBYTE_TO_MBYTE(atoi(result->value));
+                       }
+                       else if (temp == ' ') {
+                               pci->mem_action.memory = BYTE_TO_MBYTE(atoi(result->value));
+                       }
+                       else {
+                               _E("Memory size unit should be GB or MB or KB or B");
+                               return RESOURCED_ERROR_FAIL;
+                       }
+
+                       if (!strncmp(second_value, ACTION_BROADCAST_VALUE_CONF,
+                                               strlen(ACTION_BROADCAST_VALUE_CONF)+1))
+                               pci->mem_action.action = PROC_ACTION_BROADCAST;
+                       else if (!strncmp(second_value, ACTION_RECLAIM_VALUE_CONF,
+                                               strlen(ACTION_RECLAIM_VALUE_CONF)+1))
+                               pci->mem_action.action = PROC_ACTION_RECLAIM;
+                       else if (!strncmp(second_value, ACTION_KILL_VALUE_CONF,
+                                               strlen(ACTION_KILL_VALUE_CONF)+1))
+                               pci->mem_action.action = PROC_ACTION_KILL;
+                       else {
+                               _E("action (%s) is not supported", second_value);
+                               return RESOURCED_ERROR_FAIL;
+                       }
+               }
+               else {
+                       _E("Memory size unit should be XB");
+                       return RESOURCED_ERROR_FAIL;
+               }
+       }
+       else if (!strncmp(result->name, CPU_PRIORITY_NAME_CONF, strlen(CPU_PRIORITY_NAME_CONF)+1) &&
+                       *config_type == LIMITER_CONFIG) {
+               if (!pci) {
+                       _E("process configuration information pointer should not be NULL");
+                       return RESOURCED_ERROR_FAIL;
+               }
+               pci->cpu_priority = atoi(result->value);
+       }
+       else if (!strncmp(result->name, ACTION_ON_FAILURE_NAME_CONF,
+                               strlen(ACTION_ON_FAILURE_NAME_CONF)+1) && *config_type == PROCESS_CONFIG) {
+               if (!pci) {
+                       _E("process configuration information pointer should not be NULL");
+                       return RESOURCED_ERROR_FAIL;
+               }
+
+               if (!strncmp(result->value, ACTION_REBOOT_VALUE_CONF,
+                                       strlen(ACTION_REBOOT_VALUE_CONF) +1)) {
+                       pci->fail_action = PROC_ACTION_REBOOT;
+               }
+               else {
+                       _E("invalid parameter (%s)", result->value);
+                       return RESOURCED_ERROR_INVALID_PARAMETER;
+               }
+       }
+       else if (!strncmp(result->name, WATCHDOG_ACTION_NAME_CONF,
+                               strlen(WATCHDOG_ACTION_NAME_CONF)+1) && *config_type == PROCESS_CONFIG) {
+               if (!pci) {
+                       _E("process configuration information pointer should not be NULL");
+                       return RESOURCED_ERROR_FAIL;
+               }
+
+               if (!strncmp(result->value, ACTION_IGNORE_VALUE_CONF,
+                                       strlen(ACTION_IGNORE_VALUE_CONF) +1)) {
+                       pci->watchdog_action = PROC_ACTION_IGNORE;
+               }
+               else if (!strncmp(result->value, ACTION_KILL_VALUE_CONF,
+                                       strlen(ACTION_KILL_VALUE_CONF) +1)) {
+                       pci->watchdog_action = PROC_ACTION_KILL;
+               }
+               else {
+                       _E("invalid parameter (%s)", result->value);
+                       return RESOURCED_ERROR_INVALID_PARAMETER;
+               }
+       }
+       else {
+               _E("Unknown configuration name (%s) and value (%s) on section (%s)",
+                               result->name, result->value, result->section);
+       }
+
+       return RESOURCED_ERROR_NONE;
+}
+
+static void load_per_vendor_configs(const char *dir, int func(struct parse_result *result,
+                                                               void *user_data), void *user_data)
+{
+       int count;
+       int idx;
+       struct dirent **namelist;
+
+       if ((count = scandir(dir, &namelist, NULL, alphasort)) == -1) {
+               _W("failed to opendir (%s)", dir);
+               return;
+       }
+
+       for (idx = 0; idx < count; idx++) {
+               char path[PATH_MAX] = {0, };
+
+               if (!strstr(namelist[idx]->d_name, CONF_FILE_SUFFIX))
+                       continue;
+
+               snprintf(path, sizeof(path), "%s/%s", dir, namelist[idx]->d_name);
+               config_parse(path, func, user_data);
+               free(namelist[idx]);
+       }
+
+       free(namelist);
+}
+
+void resourced_parse_vendor_configs(void)
+{
+       int config_type;
+
+       config_type = LIMITER_CONFIG;
+       load_per_vendor_configs(CGROUP_VIP_LIST_DIR, vendor_config, &config_type);
+
+       config_type = PROCESS_CONFIG;
+       load_per_vendor_configs(PROC_CONF_DIR, vendor_config, &config_type);
+
+       //DEBUG
+       GSList *iter;
+       gslist_for_each_item(iter, service_conf_info_list) {
+               struct proc_conf_info *pci = (struct proc_conf_info *)iter->data;
+               _I("[CONFIG] name: %s", pci->name);
+               _I("[CONFIG] mem cgroup: %d", pci->mem_type);
+               _I("[CONFIG] cpu cgroup: %d", pci->cpu_type);
+               _I("[CONFIG] cpu priority: %d", pci->cpu_priority);
+               _I("[CONFIG] watchdog_flag: %x", pci->watchdog_action);
+               _I("[CONFIG] fail_flag: %x", pci->fail_action);
+               _I("[CONFIG] memory: %lu MB", pci->mem_action.memory);
+               _I("[CONFIG] action: %d", pci->mem_action.action);
+       }
+
+       gslist_for_each_item(iter, app_conf_info_list) {
+               struct proc_conf_info *pci = (struct proc_conf_info *)iter->data;
+               _I("[CONFIG] name: %s", pci->name);
+               _I("[CONFIG] mem cgroup: %d", pci->mem_type);
+               _I("[CONFIG] cpu cgroup: %d", pci->cpu_type);
+               _I("[CONFIG] cpu priority: %d", pci->cpu_priority);
+               _I("[CONFIG] watchdog_flag: %x", pci->watchdog_action);
+               _I("[CONFIG] fail_flag: %x", pci->fail_action);
+               _I("[CONFIG] memory: %lu MB", pci->mem_action.memory);
+               _I("[CONFIG] action: %d", pci->mem_action.action);
+       }
+}
 
 int config_parse(const char *file_name, int cb(struct parse_result *result,
                        void *user_data), void *user_data)
@@ -47,7 +342,7 @@ int config_parse(const char *file_name, int cb(struct parse_result *result,
        /* open conf file */
        f = fopen(file_name, "r");
        if (!f) {
-               _E("[DEBUG] Failed to open file %s", file_name);
+               _E("Failed to open file %s", file_name);
                ret = -EIO;
                goto error;
        }
@@ -100,14 +395,12 @@ int config_parse(const char *file_name, int cb(struct parse_result *result,
                        }
                }
        }
-       _D("[DEBUG] Success to load %s", file_name);
-       fclose(f);
        return 0;
 
 error:
        if (f)
                fclose(f);
-       _E("[DEBUG] Failed to read %s:%d!", file_name, lineno);
+       _E("Failed to read %s:%d!", file_name, lineno);
        return ret;
 }