From bba1b266f83615ab70f71c0151b2c11877feec3e Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 14 Aug 2019 16:34:25 +0900 Subject: [PATCH] Add a configuration file to mofidy memory status key-value pairs Change-Id: Iae9639085294824841d73b18ee2ba6c2dfece364 Signed-off-by: Hwankyu Jhun --- CMakeLists.txt | 5 ++ conf/launchpad.conf.in | 5 ++ inc/launchpad_config.h | 35 ++++++++++ packaging/launchpad.spec | 2 + src/launchpad.c | 74 ++++++++++++++++---- src/launchpad_config.c | 178 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 287 insertions(+), 12 deletions(-) create mode 100644 conf/launchpad.conf.in create mode 100644 inc/launchpad_config.h create mode 100644 src/launchpad_config.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 573fc00..a557924 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ PKG_CHECK_MODULES(${this_target_pool} REQUIRED libtzplatform-config libcap dbus-1 + iniparser ) FOREACH(flag ${${this_target_pool}_CFLAGS}) @@ -120,6 +121,7 @@ SET(${LAUNCHPAD_PROCESS_POOL}_SOURCE_FILES src/debugger_info.c src/launchpad_debug.c src/launchpad_signal.c + src/launchpad_config.c ) ADD_EXECUTABLE(${LAUNCHPAD_PROCESS_POOL} ${${LAUNCHPAD_PROCESS_POOL}_SOURCE_FILES}) @@ -136,6 +138,9 @@ INSTALL(TARGETS ${LAUNCHPAD_PROCESS_POOL} DESTINATION bin) CONFIGURE_FILE(packaging/default.loader.in packaging/default.loader @ONLY) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/packaging/default.loader DESTINATION share/aul) +CONFIGURE_FILE(conf/launchpad.conf.in conf/launchpad.conf @ONLY) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/conf/launchpad.conf DESTINATION share/aul) + # liblaunchpad SET(LAUNCHPAD_LIB "launchpad") ADD_LIBRARY(${LAUNCHPAD_LIB} SHARED diff --git a/conf/launchpad.conf.in b/conf/launchpad.conf.in new file mode 100644 index 0000000..91fed8c --- /dev/null +++ b/conf/launchpad.conf.in @@ -0,0 +1,5 @@ +[MemoryStatus] +LowKey=memory/sysman/low_memory +LowValue=2 +NormalKey=memory/sysman/low_memory +NormalValue=1 diff --git a/inc/launchpad_config.h b/inc/launchpad_config.h new file mode 100644 index 0000000..3b819da --- /dev/null +++ b/inc/launchpad_config.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019 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 __LAUNCHPAD_CONFIG_H__ +#define __LAUNCHPAD_CONFIG_H__ + +typedef enum { + CONFIG_TYPE_MEMORY_STATUS_LOW_KEY, + CONFIG_TYPE_MEMORY_STATUS_LOW_VALUE, + CONFIG_TYPE_MEMORY_STATUS_NORMAL_KEY, + CONFIG_TYPE_MEMORY_STATUS_NORMAL_VALUE, +} config_type_e; + +const char *_config_get_string_value(config_type_e type); + +int _config_get_int_value(config_type_e type); + +int _config_init(void); + +void _config_fini(void); + +#endif /* __LAUNCHPAD_CONFIG_H__ */ diff --git a/packaging/launchpad.spec b/packaging/launchpad.spec index e03a7c8..c699b2c 100644 --- a/packaging/launchpad.spec +++ b/packaging/launchpad.spec @@ -27,6 +27,7 @@ BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(libcap) BuildRequires: pkgconfig(tanchor) BuildRequires: pkgconfig(dbus-1) +BuildRequires: pkgconfig(iniparser) Requires(post): /sbin/ldconfig Requires(post): /usr/bin/systemctl @@ -139,6 +140,7 @@ ln -sf ../launchpad-process-pool@.service %{buildroot}%{_unitdir}/user-basic@.ta %{_unitdir}/user-sockets@.target.wants/launchpad-process-pool@.socket %{_unitdir}/user-basic@.target.wants/launchpad-process-pool@.service %{_bindir}/launchpad-process-pool +%{_prefix}/share/aul/launchpad.conf %files devel %{_includedir}/launchpad/*.h diff --git a/src/launchpad.c b/src/launchpad.c index c5d097f..17c7449 100755 --- a/src/launchpad.c +++ b/src/launchpad.c @@ -46,6 +46,7 @@ #include "loader_info.h" #include "launcher_info.h" #include "launchpad_debug.h" +#include "launchpad_config.h" #define AUL_PR_NAME 16 #define EXEC_CANDIDATE_EXPIRED 5 @@ -137,8 +138,11 @@ static GList *candidate_slot_list; static app_labels_monitor *label_monitor; static GList *launcher_info_list; static GHashTable *__pid_table; -static int __memory_status; +static int __memory_status_low; +static int __memory_status_normal; static sequencer __sequencer; +static int MEMORY_STATUS_LOW; +static int MEMORY_STATUS_NORMAL; static candidate_process_context_t *__add_slot(int type, int loader_id, int caller_pid, const char *loader_path, const char *extra, @@ -2176,7 +2180,7 @@ static int __add_default_slots(void) static bool __is_low_memory(void) { - if (__memory_status >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) + if (__memory_status_low >= MEMORY_STATUS_LOW) return true; return false; } @@ -2195,21 +2199,32 @@ static void __update_lang(keynode_t *node, void *user_data) setenv("LANG", lang, 1); } -static void __memory_status_changed_cb(keynode_t *node, void *data) +static void __memory_status_low_changed_cb(keynode_t *node, void *data) { candidate_process_context_t *cpc; - GList *iter = candidate_slot_list; + GList *iter; - __memory_status = vconf_keynode_get_int(node); - if (__memory_status >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) { + __memory_status_low = vconf_keynode_get_int(node); + if (__memory_status_low >= MEMORY_STATUS_LOW) { _W("Low memory"); + iter = candidate_slot_list; while (iter) { cpc = (candidate_process_context_t *)iter->data; __update_slot_state(cpc, METHOD_OUT_OF_MEMORY); iter = g_list_next(iter); } - } else { + } +} + +static void __memory_status_normal_changed_cb(keynode_t *node, void *data) +{ + candidate_process_context_t *cpc; + GList *iter; + + __memory_status_normal = vconf_keynode_get_int(node); + if (__memory_status_normal == MEMORY_STATUS_NORMAL) { _W("Normal"); + iter = candidate_slot_list; while (iter) { cpc = (candidate_process_context_t *)iter->data; __update_slot_state(cpc, METHOD_AVAILABLE_MEMORY); @@ -2220,8 +2235,17 @@ static void __memory_status_changed_cb(keynode_t *node, void *data) static void __unregister_vconf_events(void) { - vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, - __memory_status_changed_cb); + const char *key; + config_type_e type; + + type = CONFIG_TYPE_MEMORY_STATUS_NORMAL_KEY; + key = _config_get_string_value(type); + vconf_ignore_key_changed(key, __memory_status_normal_changed_cb); + + type = CONFIG_TYPE_MEMORY_STATUS_LOW_KEY; + key = _config_get_string_value(type); + vconf_ignore_key_changed(key, __memory_status_low_changed_cb); + vconf_ignore_key_changed(VCONFKEY_LANGSET, __update_lang); vconf_ignore_key_changed(VCONFKEY_SETAPPL_APP_HW_ACCELERATION, @@ -2232,6 +2256,8 @@ static int __register_vconf_events(void) { int r; char *lang; + const char *key; + config_type_e type; r = vconf_get_int(VCONFKEY_SETAPPL_APP_HW_ACCELERATION, &__sys_hwacc); if (r != VCONF_OK) @@ -2254,15 +2280,34 @@ static int __register_vconf_events(void) if (r != VCONF_OK) _E("Failed to register callback for langset. err = %d", r); - r = vconf_get_int(VCONFKEY_SYSMAN_LOW_MEMORY, &__memory_status); + type = CONFIG_TYPE_MEMORY_STATUS_LOW_KEY; + key = _config_get_string_value(type); + type = CONFIG_TYPE_MEMORY_STATUS_LOW_VALUE; + MEMORY_STATUS_LOW = _config_get_int_value(type); + + r = vconf_get_int(key, &__memory_status_low); if (r != VCONF_OK) _E("Failed to get vconf low memory. err = %d", r); - r = vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, - __memory_status_changed_cb, NULL); + r = vconf_notify_key_changed(key, + __memory_status_low_changed_cb, NULL); if (r != 0) _E("Failed to register callback for low memory. err = %d", r); + type = CONFIG_TYPE_MEMORY_STATUS_NORMAL_KEY; + key = _config_get_string_value(type); + type = CONFIG_TYPE_MEMORY_STATUS_NORMAL_VALUE; + MEMORY_STATUS_NORMAL = _config_get_int_value(type); + + r = vconf_get_int(key, &__memory_status_normal); + if (r != VCONF_OK) + _E("Failed to get vconf normal memory. err = %d", r); + + r = vconf_notify_key_changed(key, + __memory_status_normal_changed_cb, NULL); + if (r != 0) + _E("Failed to register callback for normal memory. err = %d", r); + return 0; } @@ -2353,6 +2398,10 @@ static int __before_loop(int argc, char **argv) if (ret != 0) _W("Failed to initialize label monitor"); + ret = _config_init(); + if (ret != 0) + _W("Failed to initialize config"); + __add_default_slots(); launcher_info_list = _launcher_info_load(LAUNCHER_INFO_PATH); @@ -2383,6 +2432,7 @@ static void __after_loop(void) _debug_fini(); _launcher_info_unload(launcher_info_list); + _config_fini(); if (label_monitor) security_manager_app_labels_monitor_finish(label_monitor); diff --git a/src/launchpad_config.c b/src/launchpad_config.c new file mode 100644 index 0000000..5c459e8 --- /dev/null +++ b/src/launchpad_config.c @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2019 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. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "launchpad_common.h" +#include "launchpad_config.h" + +#define PATH_LAUNCHPAD_CONF "/usr/share/aul/launchpad.conf" +#define TAG_MEMORY_STATUS "MemoryStatus" +#define KEY_MEMORY_STATUS_LOW_KEY "LowKey" +#define KEY_MEMORY_STATUS_LOW_VALUE "LowValue" +#define KEY_MEMORY_STATUS_NORMAL_KEY "NormalKey" +#define KEY_MEMORY_STATUS_NORMAL_VALUE "NormalValue" + +struct memory_status_s { + char *low_key; + int low_value; + char *normal_key; + int normal_value; +}; + +static struct memory_status_s __memory_status; + +const char *_config_get_string_value(config_type_e type) +{ + const char *value; + + switch (type) { + case CONFIG_TYPE_MEMORY_STATUS_LOW_KEY: + value = __memory_status.low_key; + break; + case CONFIG_TYPE_MEMORY_STATUS_NORMAL_KEY: + value = __memory_status.normal_key; + break; + default: + _E("Unknown type"); + value = NULL; + break; + } + + return value; +} + +int _config_get_int_value(config_type_e type) +{ + int value; + + switch (type) { + case CONFIG_TYPE_MEMORY_STATUS_LOW_VALUE: + value = __memory_status.low_value; + break; + case CONFIG_TYPE_MEMORY_STATUS_NORMAL_VALUE: + value = __memory_status.normal_value; + break; + default: + _E("Unknown type"); + value = INT_MIN; + } + + return value; +} + +static char *__get_string_value(dictionary *d, const char *tag, + const char *key) +{ + char buf[128]; + + snprintf(buf, sizeof(buf), "%s:%s", tag, key); + return iniparser_getstring(d, buf, NULL); +} + +static int __get_int_value(dictionary *d, const char *tag, + const char *key) +{ + char buf[128]; + + snprintf(buf, sizeof(buf), "%s:%s", tag, key); + return iniparser_getint(d, buf, INT_MIN); +} + +int _config_init(void) +{ + dictionary *d; + char *val; + char *str; + int ret; + + _D("config init"); + + __memory_status.low_key = strdup(VCONFKEY_SYSMAN_LOW_MEMORY); + __memory_status.low_value = VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING; + __memory_status.normal_key = strdup(VCONFKEY_SYSMAN_LOW_MEMORY); + __memory_status.normal_value = VCONFKEY_SYSMAN_LOW_MEMORY_NORMAL; + + ret = access(PATH_LAUNCHPAD_CONF, F_OK); + if (ret != 0) { + _W("The file doesn't exist. errno(%d)", errno); + return 0; + } + + d = iniparser_load(PATH_LAUNCHPAD_CONF); + if (!d) { + _E("Failed to load the config file"); + return -1; + } + + str = __get_string_value(d, TAG_MEMORY_STATUS, + KEY_MEMORY_STATUS_LOW_KEY); + if (str) { + val = strdup(str); + if (val) { + free(__memory_status.low_key); + __memory_status.low_key = val; + } + } + + ret = __get_int_value(d, TAG_MEMORY_STATUS, + KEY_MEMORY_STATUS_LOW_VALUE); + if (ret != INT_MIN) + __memory_status.low_value = ret; + + str = __get_string_value(d, TAG_MEMORY_STATUS, + KEY_MEMORY_STATUS_NORMAL_KEY); + if (str) { + val = strdup(str); + if (val) { + free(__memory_status.normal_key); + __memory_status.normal_key = val; + } + } + + ret = __get_int_value(d, TAG_MEMORY_STATUS, + KEY_MEMORY_STATUS_NORMAL_VALUE); + if (ret != INT_MIN) + __memory_status.normal_value = ret; + + _W("Memory Status Low(%s:%d), Normal(%s:%d)", + __memory_status.low_key, + __memory_status.low_value, + __memory_status.normal_key, + __memory_status.normal_value); + + iniparser_freedict(d); + + return 0; +} + +void _config_fini(void) +{ + _D("config fini"); + + free(__memory_status.normal_key); + free(__memory_status.low_key); +} -- 2.7.4