conf: Extract functions to reduce duplicate code 46/295746/7
authorUnsung Lee <unsung.lee@samsung.com>
Wed, 12 Jul 2023 00:34:38 +0000 (09:34 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Mon, 31 Jul 2023 02:35:06 +0000 (11:35 +0900)
Find configuration parsing codes that are predicted to be commonly used and
extract them as functions.

'proc_conf_info' structure is used to store configurations of
per process information. In addition, this structure can be used
for all kind of configurations in resourced
(e.g., limiter.conf, optimizer.conf, and process.conf).
Therefore, initialization and inserting of 'proc_conf_info'
are predicted to be commonly used.

Change-Id: I161ffce5c6b28ee4acf9610af7c984f2323ea07e
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/common/conf/config-parser.c

index 5027245..a26e0f6 100644 (file)
@@ -287,6 +287,69 @@ parse_cpuset_fail:
        return RESOURCED_ERROR_FAIL;
 }
 
+static int insert_proc_conf_info_in_list(struct proc_conf_info *pci,
+               enum proc_type process_type)
+{
+       if (!pci) {
+               _E("proc_conf_info cannot be NULL");
+               return RESOURCED_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (process_type) {
+       case APP_TYPE:
+               fixed_app_list_insert(pci);
+               break;
+       case SERVICE_TYPE:
+               fixed_service_list_insert(pci);
+               break;
+       case PROCESS_TYPE:
+               fixed_process_list_insert(pci);
+               break;
+       default:
+               _E("Unkown process type");
+               return RESOURCED_ERROR_INVALID_PARAMETER;
+       }
+
+       return RESOURCED_ERROR_NONE;
+}
+
+static int init_proc_conf_info(struct proc_conf_info *pci, const char *name)
+{
+       if (!pci || !name) {
+               _E("proc_conf_info and process name cannot be NULL");
+               return RESOURCED_ERROR_INVALID_PARAMETER;
+       }
+
+       pci->cpu_sched_info.cpu_sched_type = CPU_SCHED_NONE;
+       pci->cpu_sched_info.cpu_rt_priority = CPU_INIT_PRIO;
+       pci->cpu_sched_info.cpu_nice = CPU_INIT_NICE;
+       pci->watchdog_action = PROC_ACTION_KILL;
+       pci->fail_action = PROC_ACTION_IGNORE;
+       pci->cpu_boosting_level = CPU_BOOSTING_LEVEL_NONE;
+       pci->oom_score = OOMADJ_APP_MAX + 10;
+       strncpy(pci->name, name, sizeof(pci->name)-1);
+
+       return RESOURCED_ERROR_NONE;
+}
+
+static int register_proc_conf_info(struct proc_conf_info *pci,
+               const char *process_name, enum proc_type process_type)
+{
+       assert(pci);
+
+       if (init_proc_conf_info(pci, process_name) < 0) {
+               free(pci);
+               return RESOURCED_ERROR_FAIL;
+       }
+
+       if (insert_proc_conf_info_in_list(pci, process_type) < 0) {
+               free(pci);
+               return RESOURCED_ERROR_FAIL;
+       }
+
+       return RESOURCED_ERROR_NONE;
+}
+
 static int optimizer_config(struct parse_result *result, void *user_data)
 {
        if (!result)
@@ -757,35 +820,42 @@ static int vendor_config(struct parse_result *result, void *user_data)
        }
 
        /* common(App or Service or Process) */
-       if (!strncmp(result->name, APP_NAME_CONF, strlen(APP_NAME_CONF)+1)                 ||
-               !strncmp(result->name, SERVICE_NAME_CONF, strlen(SERVICE_NAME_CONF)+1)         ||
-           !strncmp(result->name, OLD_SERVICE_NAME_CONF, strlen(OLD_SERVICE_NAME_CONF)+1) ||
-               !strncmp(result->name, PROCESS_NAME_CONF, strlen(PROCESS_NAME_CONF)+1)         ||
-               !strncmp(result->name, OLD_PROCESS_NAME_CONF, strlen(OLD_PROCESS_NAME_CONF)+1)) {
-               pci = fixed_app_and_service_exist_check(result->value,
-                               result->name[0] == 'A' ? APP_TYPE :
-                               result->name[0] == 'S' ? SERVICE_TYPE : PROCESS_TYPE);
+       if (!strncmp(result->name, APP_NAME_CONF, strlen(APP_NAME_CONF) + 1)) {
+               pci = fixed_app_and_service_exist_check(result->value, APP_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");
+                               _E("Failed to allocate memory during parsing configurations");
                                return RESOURCED_ERROR_OUT_OF_MEMORY;
                        }
-                       pci->cpu_sched_info.cpu_sched_type = CPU_SCHED_NONE;
-                       pci->cpu_sched_info.cpu_rt_priority = CPU_INIT_PRIO;
-                       pci->cpu_sched_info.cpu_nice = CPU_INIT_NICE;
-                       pci->watchdog_action = PROC_ACTION_KILL;
-                       pci->fail_action = PROC_ACTION_IGNORE;
-                       pci->cpu_boosting_level = CPU_BOOSTING_LEVEL_NONE;
-                       pci->oom_score = OOMADJ_APP_MAX + 10;
-                       strncpy(pci->name, result->value, sizeof(pci->name)-1);
-
-                       if (result->name[0] == 'A')
-                               fixed_app_list_insert(pci);
-                       else if (result->name[0] == 'S')
-                               fixed_service_list_insert(pci);
-                       else
-                               fixed_process_list_insert(pci);
+
+                       return register_proc_conf_info(pci, result->value, APP_TYPE);
+               }
+       } else if (!strncmp(result->name, SERVICE_NAME_CONF, strlen(SERVICE_NAME_CONF) + 1)
+                       || !strncmp(result->name, OLD_SERVICE_NAME_CONF,
+                               strlen(OLD_SERVICE_NAME_CONF) + 1)) {
+               pci = fixed_app_and_service_exist_check(result->value, 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 configurations");
+                               return RESOURCED_ERROR_OUT_OF_MEMORY;
+                       }
+
+                       return register_proc_conf_info(pci, result->value, SERVICE_TYPE);
+               }
+       } else if (!strncmp(result->name, PROCESS_NAME_CONF, strlen(PROCESS_NAME_CONF) + 1)
+                       || !strncmp(result->name, OLD_PROCESS_NAME_CONF,
+                               strlen(OLD_PROCESS_NAME_CONF) + 1)) {
+               pci = fixed_app_and_service_exist_check(result->value, PROCESS_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 configurations");
+                               return RESOURCED_ERROR_OUT_OF_MEMORY;
+                       }
+
+                       return register_proc_conf_info(pci, result->value, PROCESS_TYPE);
                }
        }
        /* limiter.conf.d */