Parse 'PerProcess' section
[platform/core/system/resourced.git] / src / common / config-parser.c
index fe3b28b..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 inline char *trim_str(char *s)
-{
-       char *t;
-       /* left trim */
-       s += strspn(s, WHITESPACE);
+static GSList *app_conf_info_list;
+static GSList *service_conf_info_list;
 
-       /* right trim */
-       for (t = strchr(s, 0); t > s; t--)
-               if (!strchr(WHITESPACE, t[-1]))
+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;
-       *t = 0;
-       return s;
+               }
+       }
+
+       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)
+                       void *user_data), void *user_data)
 {
        FILE *f = NULL;
        struct parse_result result;
@@ -71,8 +352,8 @@ int config_parse(const char *file_name, int cb(struct parse_result *result,
                lineno++;
 
                start = line;
-               start[strcspn(start, NEWLINE)] = '\0';
-               start = trim_str(start);
+               truncate_nl(start);
+               start = strstrip(start);
 
                if (*start == COMMENT) {
                        continue;
@@ -95,12 +376,12 @@ int config_parse(const char *file_name, int cb(struct parse_result *result,
                                goto error;
                        }
                        *end = '\0';
-                       name = trim_str(start);
-                       value = trim_str(end + 1);
+                       name = strstrip(start);
+                       value = strstrip(end + 1);
                        end = strchr(value, COMMENT);
                        if (end && *end == COMMENT) {
                                *end = '\0';
-                               value = trim_str(value);
+                               value = strstrip(value);
                        }
 
                        result.section = section;
@@ -114,8 +395,6 @@ int config_parse(const char *file_name, int cb(struct parse_result *result,
                        }
                }
        }
-       _D("Success to load %s", file_name);
-       fclose(f);
        return 0;
 
 error:
@@ -210,8 +489,9 @@ int config_parse_new(const char *filename, void *table)
 
        f = fopen(filename, "r");
        if (!f) {
-               _E("Failed to open file %s", filename);
-               return -errno;
+               const int saved_errno = errno;
+               _E("Failed to open file %s", filename); // can modify errno
+               return -saved_errno;
        }
 
        while (!feof(f)) {
@@ -282,10 +562,20 @@ int config_parse_new(const char *filename, void *table)
                }
 
                lvalue = strndup(l, e-l);
+               if (!lvalue) {
+                       r = -ENOMEM;
+                       goto finish;
+               }
+
                strstrip(lvalue);
 
                rs = strstrip(e+1);
-               rvalue = strndup(rs, strlen(rs));
+               rvalue = strdup(rs);
+               if (!rvalue) {
+                       r = -ENOMEM;
+                       goto finish;
+               }
+
                strstrip(rvalue);
 
                r = config_parse_table(filename,
@@ -301,7 +591,7 @@ int config_parse_new(const char *filename, void *table)
        r = 0;
 
 finish:
-       for (i=0; i<num_section; i++)
+       for (i = 0; i < num_section; i++)
                if (sections[i])
                        free(sections[i]);
 
@@ -311,8 +601,7 @@ finish:
 int config_parse_dir(const char *dir, ConfigParseFunc fp, void *data)
 {
        _cleanup_closedir_ DIR *d = NULL;
-       struct dirent de;
-       struct dirent *result;
+       struct dirent *de;
 
        d = opendir(dir);
        if (!d) {
@@ -320,14 +609,22 @@ int config_parse_dir(const char *dir, ConfigParseFunc fp, void *data)
                return errno;
        }
 
-       FOREACH_DIRENT(de, d, result, return -errno) {
+       for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) {
+               if (!de) {
+                       if (errno > 0)
+                               return -errno;
+                       break;
+               } else if (streq(de->d_name, ".") || streq(de->d_name, "..")) {
+                       continue;
+               }
+
                _cleanup_free_ char *path = NULL;
                int r;
 
-               if (de.d_type != DT_REG)
+               if (de->d_type != DT_REG)
                        continue;
 
-               r = asprintf(&path, "%s/%s", dir, de.d_name);
+               r = asprintf(&path, "%s/%s", dir, de->d_name);
                if (r < 0)
                        return -ENOMEM;
 
@@ -335,7 +632,7 @@ int config_parse_dir(const char *dir, ConfigParseFunc fp, void *data)
                /* Do not just break loop until parse all file of
                 * dir. Just only put log */
                if (r < 0)
-                       _D("Failed to parse config: %s", de.d_name);
+                       _D("Failed to parse config: %s", de->d_name);
        }
 
        return 0;
@@ -368,26 +665,30 @@ int config_parse_bool(const char *filename,
        return 0;
 }
 
+#define DEFINE_PARSER(type, vartype, conv_func)          \
+       int config_parse_##type(                                        \
+                               const char *filename,                   \
+                               unsigned line,                          \
+                               const char *section,                    \
+                               const char *lvalue,                     \
+                               int ltype,                              \
+                               const char *rvalue,                     \
+                               void *data) {                           \
+                                                                                                               \
+               vartype *i = data;                                      \
+                                                                       \
+               assert(filename);                                       \
+               assert(lvalue);                                         \
+               assert(rvalue);                                         \
+               assert(data);                                           \
+                                                                       \
+               *i = conv_func(rvalue);                          \
+               return 0;                                               \
+               }
 
-int config_parse_int(const char *filename,
-                    unsigned line,
-                    const char *section,
-                    const char *lvalue,
-                    int ltype,
-                    const char *rvalue,
-                    void *data)
-{
-       int *i = data;
-
-       assert(filename);
-       assert(lvalue);
-       assert(rvalue);
-       assert(data);
-
-       *i = atoi(rvalue);
-
-       return 0;
-}
+DEFINE_PARSER(int, int, atoi)
+DEFINE_PARSER(float, float, atof)
+DEFINE_PARSER(long, long, atol)
 
 int config_parse_string(const char *filename,
                        unsigned line,
@@ -407,7 +708,7 @@ int config_parse_string(const char *filename,
        if (is_empty(rvalue))
                n = NULL;
        else {
-               n = strndup(rvalue, strlen(rvalue));
+               n = strdup(rvalue);
                if (!n)
                        return -ENOMEM;
        }