1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
7 * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8 * JinYoung Jeon <jy0.jeon@samsung.com>,
9 * Taeheon Kim <th908.kim@samsung.com>,
10 * YoungJun Cho <yj44.cho@samsung.com>,
11 * SooChan Lim <sc1.lim@samsung.com>,
12 * Boram Park <boram1288.park@samsung.com>
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the
16 * "Software"), to deal in the Software without restriction, including
17 * without limitation the rights to use, copy, modify, merge, publish,
18 * distribute, sub license, and/or sell copies of the Software, and to
19 * permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 **************************************************************************/
40 #include <iniparser.h>
43 #include "tdm_macro.h"
46 #include "tdm_private.h"
47 #include "tdm_config.h"
49 #define TDM_CONFIG_FILENAME "tdm.ini"
50 #define TDM_CONFIG_GENERAL_SECTION "general"
52 static pthread_mutex_t g_dic_lock = PTHREAD_MUTEX_INITIALIZER;
53 static dictionary *g_dic = NULL;
54 static int init_dic = 0;
57 _tdm_config_check_file_owner(const char *filepath)
61 if (stat(filepath, &sb) < 0) {
62 TDM_WRN("%s: %m", filepath);
66 if (sb.st_uid != getuid()) {
67 TDM_WRN("'%s': not owned by %u", filepath, getuid());
75 _tdm_config_load_file(const char *dir, const char *filename)
77 char filepath[TDM_PATH_LEN];
80 snprintf(filepath, sizeof filepath, "%s/%s", dir, filename);
82 if (_tdm_config_check_file_owner(filepath) < 0)
85 dic = iniparser_load(filepath);
86 TDM_RETURN_VAL_IF_FAIL(dic != NULL, NULL);
88 if (!iniparser_find_entry(dic, TDM_CONFIG_GENERAL_SECTION)) {
89 TDM_ERR("no '%s' section: '%s'", TDM_CONFIG_GENERAL_SECTION, filepath);
90 iniparser_freedict(dic);
94 TDM_INFO("opened successed: '%s'", filepath);
100 _tdm_config_check_logs(void)
105 pthread_mutex_unlock(&g_dic_lock);
107 level = tdm_config_get_int(TDM_CONFIG_KEY_DEBUG_LOG_LEVEL, 3);
108 tdm_log_set_debug_level(level);
110 level = tdm_config_get_int(TDM_CONFIG_KEY_DEBUG_ASSERT_LEVEL, 0);
111 tdm_log_set_assert_level(level);
113 /* if TDM_CONFIG_KEY_DEBUG_LOG_PATH is setted, TDM_CONFIG_KEY_DEBUG_DLOG will be ignored. */
114 path = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_LOG_PATH, NULL);
116 tdm_log_enable_dlog(0);
117 tdm_log_set_path(path);
119 int dlog = tdm_config_get_int(TDM_CONFIG_KEY_DEBUG_DLOG, 1);
120 tdm_log_enable_dlog(dlog);
123 pthread_mutex_lock(&g_dic_lock);
127 _tdm_config_check_init(void)
134 g_dic = _tdm_config_load_file(TDM_DATA_PATH, TDM_CONFIG_FILENAME);
136 _tdm_config_check_logs();
138 TDM_INFO("tdm config init %s (%p)", (g_dic) ? "successed" : "failed", g_dic);
142 tdm_config_deinit(void)
144 pthread_mutex_lock(&g_dic_lock);
147 pthread_mutex_unlock(&g_dic_lock);
151 iniparser_freedict(g_dic);
155 TDM_INFO("tdm config deinit done");
157 pthread_mutex_unlock(&g_dic_lock);
161 _tdm_config_get_string_internal(dictionary *dic, const char *key, const char *default_value)
167 temp = strdup(default_value);
169 TDM_ERR("strdup failed: %m");
170 return default_value;
174 result = (const char *)iniparser_getstring(dic, key, temp);
175 if (!result || strlen(result) == 0) {
177 return default_value;
186 tdm_config_get_int(const char *key, int default_value)
191 TDM_RETURN_VAL_IF_FAIL(key != NULL, default_value);
193 pthread_mutex_lock(&g_dic_lock);
195 _tdm_config_check_init();
197 TDM_INFO("%s = %d: default", key, default_value);
198 pthread_mutex_unlock(&g_dic_lock);
199 return default_value;
202 result = _tdm_config_get_string_internal(g_dic, key, NULL);
203 pthread_mutex_unlock(&g_dic_lock);
206 TDM_INFO("%s = %d: no key", key, default_value);
207 return default_value;
210 value = (int)strtol(result, NULL, 0);
212 TDM_INFO("%s = %d", key, value);
218 tdm_config_get_string(const char *key, const char *default_value)
222 TDM_RETURN_VAL_IF_FAIL(key != NULL, default_value);
224 pthread_mutex_lock(&g_dic_lock);
226 _tdm_config_check_init();
228 TDM_INFO("%s = %s: default", key, default_value);
229 pthread_mutex_unlock(&g_dic_lock);
230 return default_value;
233 result = _tdm_config_get_string_internal(g_dic, key, default_value);
234 pthread_mutex_unlock(&g_dic_lock);
236 TDM_INFO("%s = %s", key, result);
242 tdm_config_set_int(const char *key, int value)
244 char temp[TDM_NAME_LEN];
247 TDM_RETURN_VAL_IF_FAIL(key != NULL, TDM_ERROR_INVALID_PARAMETER);
249 snprintf(temp, sizeof temp, "%d", value);
251 pthread_mutex_lock(&g_dic_lock);
253 _tdm_config_check_init();
255 TDM_INFO("%s = %d set failed", key, value);
256 pthread_mutex_unlock(&g_dic_lock);
257 return TDM_ERROR_BAD_REQUEST;
260 ret = iniparser_set(g_dic, key, (const char*)temp);
262 pthread_mutex_unlock(&g_dic_lock);
264 TDM_RETURN_VAL_IF_FAIL(ret == 0, TDM_ERROR_OPERATION_FAILED);
266 TDM_INFO("%s = %d set done", key, value);
268 return TDM_ERROR_NONE;
272 tdm_config_set_string(const char *key, const char *value)
276 TDM_RETURN_VAL_IF_FAIL(key != NULL, TDM_ERROR_INVALID_PARAMETER);
278 pthread_mutex_lock(&g_dic_lock);
280 _tdm_config_check_init();
282 TDM_INFO("%s = %s set failed", key, value);
283 pthread_mutex_unlock(&g_dic_lock);
284 return TDM_ERROR_BAD_REQUEST;
287 ret = iniparser_set(g_dic, key, value);
289 pthread_mutex_unlock(&g_dic_lock);
291 TDM_RETURN_VAL_IF_FAIL(ret == 0, TDM_ERROR_OPERATION_FAILED);
293 TDM_INFO("%s = %s set done", key, value);
295 return TDM_ERROR_NONE;