Modify slot addition function 88/284488/3
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 17 Nov 2022 23:51:04 +0000 (23:51 +0000)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 17 Nov 2022 23:54:20 +0000 (23:54 +0000)
To reduce the number of parameters of the __add_slot() function,
the slot_info_t is added.

Change-Id: I487e1eb1172ff667531c7ad2e9c6a1ff3fdf3296
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/launchpad-process-pool/inc/slot_info.h [new file with mode: 0644]
src/launchpad-process-pool/src/launchpad.c

diff --git a/src/launchpad-process-pool/inc/slot_info.h b/src/launchpad-process-pool/inc/slot_info.h
new file mode 100644 (file)
index 0000000..fdb08b7
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __SLOT_INFO_H__
+#define __SLOT_INFO_H__
+
+#include <stdbool.h>
+
+typedef struct slot_info_s {
+       int type;
+       int loader_id;
+       int caller_pid;
+       const char *loader_name;
+       const char *loader_path;
+       const char *loader_extra;
+       int detection_method;
+       int activation_method;
+       int deactivation_method;
+       unsigned int ttl;
+       int timeout_val;
+       int threshold_max;
+       int threshold_min;
+       bool on_boot;
+       bool app_exists;
+       bool is_hydra;
+       bool app_check;
+} slot_info_t;
+
+#endif /* __SLOT_INFO_H__ */
index 1d2eb62..22edbea 100644 (file)
@@ -57,6 +57,7 @@
 #include "launchpad_worker.h"
 #include "loader_info.h"
 #include "perf.h"
+#include "slot_info.h"
 
 #define AUL_PR_NAME         16
 #define EXEC_CANDIDATE_EXPIRED 5
@@ -214,13 +215,7 @@ static int __client_fd = -1;
 static worker_h __cleaner;
 static worker_h __forker;
 
-static candidate_process_context_t *__add_slot(int type, int loader_id,
-               int caller_pid, const char *loader_name,
-               const char *loader_path, const char *extra,
-               int detection_method, int activation_method,
-               int deactivation_method, unsigned int ttl, int timeout_val,
-               int threshold_max, int threshold_min, bool on_boot,
-               bool app_exists, bool is_hydra, bool app_check);
+static candidate_process_context_t *__add_slot(slot_info_t *info);
 static int __remove_slot(int type, int loader_id);
 static int __add_default_slots(void);
 static gboolean __handle_idle_checker(gpointer data);
@@ -1907,44 +1902,51 @@ static int __dispatch_cmd_add_loader(bundle *kb)
        int lid, size;
        char *loader_name;
        candidate_process_context_t *cpc;
+       slot_info_t slot_info;
 
        _W("cmd add loader");
        add_slot_str = bundle_get_val(kb, AUL_K_LOADER_PATH);
        caller_pid = bundle_get_val(kb, AUL_K_CALLER_PID);
        extra = bundle_get_val(kb, AUL_K_LOADER_EXTRA);
 
-       if (add_slot_str && caller_pid) {
-               lid = __make_loader_id();
-
-               size = snprintf(0, 0, "%s%s%d", add_slot_str, caller_pid, lid);
-               loader_name = (char *)malloc(size + 1);
-               if (loader_name == NULL) {
-                       _E("Out of memory");
-                       return -1;
-               }
+       if (add_slot_str == NULL || caller_pid == NULL)
+               return -1;
 
-               snprintf(loader_name, size, "%s%s%d", add_slot_str, caller_pid, lid);
-
-               cpc = __add_slot(LAUNCHPAD_LOADER_TYPE_DYNAMIC, lid,
-                               atoi(caller_pid), loader_name,
-                               add_slot_str, extra,
-                               METHOD_TIMEOUT | METHOD_VISIBILITY,
-                               METHOD_REQUEST | METHOD_AVAILABLE_MEMORY,
-                               METHOD_TTL | METHOD_OUT_OF_MEMORY,
-                               600,
-                               2000,
-                               DEFAULT_CPU_THRESHOLD_MAX,
-                               DEFAULT_CPU_THRESHOLD_MIN,
-                               false,
-                               true,
-                               false,
-                               true);
-               __set_timer(cpc);
-               free(loader_name);
-               return lid;
+       lid = __make_loader_id();
+       size = snprintf(0, 0, "%s%s%d", add_slot_str, caller_pid, lid);
+       loader_name = (char *)malloc(size + 1);
+       if (loader_name == NULL) {
+               _E("Out of memory");
+               return -1;
        }
 
-       return -1;
+       snprintf(loader_name, size, "%s%s%d", add_slot_str, caller_pid, lid);
+
+       slot_info.type = LAUNCHPAD_LOADER_TYPE_DYNAMIC;
+       slot_info.loader_id = lid;
+       slot_info.caller_pid = atoi(caller_pid);
+       slot_info.loader_name = loader_name;
+       slot_info.loader_path = add_slot_str;
+       slot_info.loader_extra = extra;
+       slot_info.detection_method = METHOD_TIMEOUT | METHOD_VISIBILITY;
+       slot_info.activation_method = METHOD_REQUEST | METHOD_AVAILABLE_MEMORY;
+       slot_info.deactivation_method = METHOD_TTL | METHOD_OUT_OF_MEMORY;
+       slot_info.ttl = 600;
+       slot_info.timeout_val = 2000;
+       slot_info.threshold_max = DEFAULT_CPU_THRESHOLD_MAX;
+       slot_info.threshold_min = DEFAULT_CPU_THRESHOLD_MIN;
+       slot_info.on_boot = false;
+       slot_info.app_exists = true;
+       slot_info.is_hydra = false;
+       slot_info.app_check = true;
+
+       cpc = __add_slot(&slot_info);
+       free(loader_name);
+       if (cpc == NULL)
+               return -1;
+
+       __set_timer(cpc);
+       return lid;
 }
 
 static int __dispatch_cmd_add_app_defined_loader(bundle *kb)
@@ -1954,6 +1956,7 @@ static int __dispatch_cmd_add_app_defined_loader(bundle *kb)
        candidate_process_context_t *cpc;
        loader_info_t *info;
        bundle_raw *extra;
+       slot_info_t slot_info;
 
        _W("cmd add defined loader");
        loader_name = bundle_get_val(kb, AUL_K_LOADER_NAME);
@@ -1975,20 +1978,27 @@ static int __dispatch_cmd_add_app_defined_loader(bundle *kb)
        if (cpc == NULL) {
                lid = __make_loader_id();
                bundle_encode(info->extra, &extra, &len);
-               cpc = __add_slot(LAUNCHPAD_LOADER_TYPE_DYNAMIC, lid, 0,
-                               loader_name, "/usr/bin/app-defined-loader", (const char *)extra,
-                               METHOD_TIMEOUT | METHOD_VISIBILITY,
-                               METHOD_REQUEST | METHOD_AVAILABLE_MEMORY,
-                               METHOD_TTL | METHOD_OUT_OF_MEMORY,
-                               info->ttl,
-                               2000,
-                               DEFAULT_CPU_THRESHOLD_MAX,
-                               DEFAULT_CPU_THRESHOLD_MIN,
-                               false,
-                               true,
-                               false,
-                               true);
-
+               slot_info.type = LAUNCHPAD_LOADER_TYPE_DYNAMIC;
+               slot_info.loader_id = lid;
+               slot_info.caller_pid = 0;
+               slot_info.loader_name = loader_name;
+               slot_info.loader_path = "/usr/bin/app-defined-loader";
+               slot_info.loader_extra = (const char *)extra;
+               slot_info.detection_method = METHOD_TIMEOUT | METHOD_VISIBILITY;
+               slot_info.activation_method =
+                       METHOD_REQUEST | METHOD_AVAILABLE_MEMORY;
+               slot_info.deactivation_method =
+                       METHOD_TTL | METHOD_OUT_OF_MEMORY;
+               slot_info.ttl = info->ttl;
+               slot_info.timeout_val = 2000;
+               slot_info.threshold_max = DEFAULT_CPU_THRESHOLD_MAX;
+               slot_info.threshold_min = DEFAULT_CPU_THRESHOLD_MIN;
+               slot_info.on_boot = false;
+               slot_info.app_exists = true;
+               slot_info.is_hydra = false;
+               slot_info.app_check = true;
+
+               cpc = __add_slot(&slot_info);
                bundle_free_encoded_rawdata(&extra);
                if (cpc == NULL) {
                        _E("cpc is NULL");
@@ -2635,13 +2645,7 @@ static void __destroy_slot(candidate_process_context_t *cpc)
        free(cpc);
 }
 
-static candidate_process_context_t *__create_slot(int type, int loader_id,
-               int caller_pid, const char *loader_name, const char *loader_path,
-               const char *loader_extra, int detection_method,
-               int activation_method, int deactivation_method,
-               unsigned int ttl, int timeout_val,
-               int threshold_max, int threshold_min,
-               bool on_boot, bool app_exists, bool is_hydra, bool app_check)
+static candidate_process_context_t *__create_slot(slot_info_t *info)
 {
        candidate_process_context_t *cpc;
 
@@ -2651,55 +2655,55 @@ static candidate_process_context_t *__create_slot(int type, int loader_id,
                return NULL;
        }
 
-       cpc->loader_name = strdup(loader_name);
+       cpc->loader_name = strdup(info->loader_name);
        if (cpc->loader_name == NULL) {
-               _E("Failed to duplicate loader name(%s)", loader_name);
+               _E("Failed to duplicate loader name(%s)", info->loader_name);
                __destroy_slot(cpc);
                return NULL;
        }
 
-       cpc->loader_path = strdup(loader_path);
+       cpc->loader_path = strdup(info->loader_path);
        if (cpc->loader_path == NULL) {
-               _E("Failed to duplicate loader path(%s)", loader_path);
+               _E("Failed to duplicate loader path(%s)", info->loader_path);
                __destroy_slot(cpc);
                return NULL;
        }
 
-       cpc->loader_extra = loader_extra ? strdup(loader_extra) : strdup("");
+       cpc->loader_extra =
+               info->loader_extra ? strdup(info->loader_extra) : strdup("");
        if (cpc->loader_extra == NULL) {
-               _E("Failed to duplicate loader extra(%s)",
-                               loader_extra ? loader_extra : "null");
+               _E("Failed to duplicate loader extra(%s)", info->loader_extra);
                __destroy_slot(cpc);
                return NULL;
        }
 
-       cpc->type = type;
+       cpc->type = info->type;
        cpc->prepared = false;
        cpc->pid = CANDIDATE_NONE;
        cpc->hydra_pid = HYDRA_NONE;
-       cpc->caller_pid = caller_pid;
-       cpc->loader_id = loader_id;
+       cpc->caller_pid = info->caller_pid;
+       cpc->loader_id = info->loader_id;
        cpc->send_fd = -1;
        cpc->hydra_fd = -1;
        cpc->last_exec_time = 0;
        cpc->timer = 0;
-       cpc->detection_method = detection_method;
-       cpc->timeout_val = timeout_val;
+       cpc->detection_method = info->detection_method;
+       cpc->timeout_val = info->timeout_val;
        cpc->cpu_total_time = 0;
        cpc->cpu_idle_time = 0;
-       cpc->threshold = threshold_max;
-       cpc->threshold_max = threshold_max;
-       cpc->threshold_min = threshold_min;
-       cpc->on_boot = on_boot;
-       cpc->app_exists = app_exists;
+       cpc->threshold = info->threshold_max;
+       cpc->threshold_max = info->threshold_max;
+       cpc->threshold_min = info->threshold_min;
+       cpc->on_boot = info->on_boot;
+       cpc->app_exists = info->app_exists;
        cpc->touched = false;
        cpc->cur_event = 0;
-       cpc->activation_method = activation_method;
-       cpc->deactivation_method = deactivation_method;
-       cpc->ttl = ttl;
+       cpc->activation_method = info->activation_method;
+       cpc->deactivation_method = info->deactivation_method;
+       cpc->ttl = info->ttl;
        cpc->live_timer = 0;
-       cpc->is_hydra = is_hydra;
-       cpc->app_check = app_check;
+       cpc->is_hydra = info->is_hydra;
+       cpc->app_check = info->app_check;
        cpc->score = WIN_SCORE;
        cpc->pss = 0;
        cpc->cpu_check_count = 0;
@@ -2715,13 +2719,7 @@ static candidate_process_context_t *__create_slot(int type, int loader_id,
        return cpc;
 }
 
-static candidate_process_context_t *__add_slot(int type, int loader_id,
-               int caller_pid, const char *loader_name, const char *loader_path,
-               const char *loader_extra, int detection_method,
-               int activation_method, int deactivation_method,
-               unsigned int ttl, int timeout_val,
-               int threshold_max, int threshold_min,
-               bool on_boot, bool app_exists, bool is_hydra, bool app_check)
+static candidate_process_context_t *__add_slot(slot_info_t *info)
 {
        candidate_process_context_t *cpc;
        int fd;
@@ -2729,16 +2727,13 @@ static candidate_process_context_t *__add_slot(int type, int loader_id,
        int hydra_fd;
        io_channel_h hydra_channel;
 
-       if (__find_slot(type, loader_id) != NULL)
+       if (info == NULL)
+               return NULL;
+
+       if (__find_slot(info->type, info->loader_id) != NULL)
                return NULL;
 
-       cpc = __create_slot(type, loader_id,
-                       caller_pid, loader_name, loader_path,
-                       loader_extra, detection_method,
-                       activation_method, deactivation_method,
-                       ttl, timeout_val,
-                       threshold_max, threshold_min,
-                       on_boot, app_exists, is_hydra, app_check);
+       cpc = __create_slot(info);
        if (cpc == NULL)
                return NULL;
 
@@ -2760,7 +2755,7 @@ static candidate_process_context_t *__add_slot(int type, int loader_id,
 
        cpc->channel = channel;
 
-       if (is_hydra) {
+       if (info->is_hydra) {
                hydra_fd = __listen_hydra_process(cpc->type, cpc->loader_id);
                if (hydra_fd == -1) {
                        _E("[launchpad] Listening the socket to " \
@@ -2781,7 +2776,6 @@ static candidate_process_context_t *__add_slot(int type, int loader_id,
                cpc->hydra_channel = hydra_channel;
        }
 
-
        candidate_slot_list = g_list_append(candidate_slot_list, cpc);
 
        return cpc;
@@ -3029,18 +3023,21 @@ static void __add_slot_from_info(gpointer data, gpointer user_data)
        bundle_raw *extra = NULL;
        int len;
        char buf[2048] = {0, };
+       slot_info_t slot_info = {
+               .type = LAUNCHPAD_LOADER_TYPE_USER + user_slot_offset,
+               .loader_name = info->name,
+               .loader_path = info->exe,
+               .threshold_max = info->cpu_threshold_max,
+               .threshold_min = info->cpu_threshold_min,
+               .app_exists = info->app_exists,
+               .is_hydra = info->is_hydra,
+               .app_check = info->app_check,
+       };
 
        if (!strcmp(info->exe, "null")) {
-               cpc = __add_slot(LAUNCHPAD_LOADER_TYPE_USER + user_slot_offset,
-                               PAD_LOADER_ID_DIRECT,
-                               0, info->name, info->exe, NULL,
-                               0, 0, 0, 0, 0,
-                               info->cpu_threshold_max,
-                               info->cpu_threshold_min,
-                               false,
-                               info->app_exists,
-                               info->is_hydra,
-                               info->app_check);
+               slot_info.loader_id = PAD_LOADER_ID_DIRECT;
+
+               cpc = __add_slot(&slot_info);
                if (cpc == NULL)
                        return;
 
@@ -3056,21 +3053,16 @@ static void __add_slot_from_info(gpointer data, gpointer user_data)
                if (info->extra)
                        bundle_encode(info->extra, &extra, &len);
 
-               cpc = __add_slot(LAUNCHPAD_LOADER_TYPE_USER + user_slot_offset,
-                               PAD_LOADER_ID_STATIC,
-                               0, info->name, info->exe, (char *)extra,
-                               info->detection_method,
-                               info->activation_method,
-                               info->deactivation_method,
-                               info->ttl,
-                               info->timeout_val,
-                               info->cpu_threshold_max,
-                               info->cpu_threshold_min,
-                               info->on_boot,
-                               info->app_exists,
-                               info->is_hydra,
-                               info->app_check);
+               slot_info.loader_id = PAD_LOADER_ID_STATIC;
+               slot_info.loader_extra = (const char *)extra;
+               slot_info.detection_method = info->detection_method;
+               slot_info.activation_method = info->activation_method;
+               slot_info.deactivation_method = info->deactivation_method;
+               slot_info.ttl = info->ttl;
+               slot_info.timeout_val = info->timeout_val;
+               slot_info.on_boot = info->on_boot;
 
+               cpc = __add_slot(&slot_info);
                bundle_free_encoded_rawdata(&extra);
                if (cpc == NULL)
                        return;