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 <sc1.lim@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 dictionary *g_dic = NULL;
53 static pthread_mutex_t g_dic_lock;
56 _tdm_config_check_file_owner(const char *filepath)
60 if (stat(filepath, &sb) < 0) {
61 TDM_WRN("%s: %m", filepath);
65 if (sb.st_uid != getuid()) {
66 TDM_WRN("'%s': not owned by %u", filepath, getuid());
74 _tdm_config_load_file(const char *dir, const char *filename)
76 char filepath[TDM_PATH_LEN];
79 snprintf(filepath, sizeof filepath, "%s/%s", dir, filename);
81 if (_tdm_config_check_file_owner(filepath) < 0)
84 dic = iniparser_load(filepath);
85 TDM_RETURN_VAL_IF_FAIL(dic != NULL, NULL);
87 if (!iniparser_find_entry(dic, TDM_CONFIG_GENERAL_SECTION)) {
88 TDM_ERR("no '%s' section: '%s'", TDM_CONFIG_GENERAL_SECTION, filepath);
89 iniparser_freedict(dic);
93 TDM_INFO("opened successed: '%s'", filepath);
99 _tdm_config_load(void)
101 dictionary *dic = NULL;
104 /* not allowed: try to read from RW directory */
105 dic = _tdm_config_load_file(TDM_SYSCONF_PATH, TDM_CONFIG_FILENAME);
109 /* try to read from RO directory */
110 dic = _tdm_config_load_file(TDM_DATA_PATH, TDM_CONFIG_FILENAME);
117 tdm_config_init(void)
120 TDM_ERR("init failed: twice");
121 return TDM_ERROR_OPERATION_FAILED;
124 if (pthread_mutex_init(&g_dic_lock, NULL)) {
125 TDM_ERR("mutex init failed: %m");
126 return TDM_ERROR_OUT_OF_MEMORY;
129 g_dic = _tdm_config_load();
131 if (!getenv("TDM_NO_CONFIG")) {
132 TDM_ERR("Loading config file failed!!");
133 pthread_mutex_destroy(&g_dic_lock);
134 return TDM_ERROR_NONE;
138 TDM_INFO("tdm config init done (%p)", g_dic);
140 return TDM_ERROR_NONE;
144 tdm_config_deinit(void)
150 iniparser_freedict(g_dic);
153 /* we don't need to lock/unlock here because we come here
154 * after tdm_thread has been destroyed
156 pthread_mutex_destroy(&g_dic_lock);
158 TDM_INFO("tdm config deinit done");
162 _tdm_config_get_string_internal(dictionary *dic, const char *key, const char *default_value)
168 temp = strdup(default_value);
170 TDM_ERR("strdup failed: %m");
171 return default_value;
175 result = (const char *)iniparser_getstring(dic, key, temp);
176 if (!result || strlen(result) == 0) {
178 return default_value;
187 tdm_config_get_int(const char *key, int default_value)
192 TDM_RETURN_VAL_IF_FAIL(key != NULL, default_value);
195 TDM_INFO("%s = %d: default", key, default_value);
196 return default_value;
199 pthread_mutex_lock(&g_dic_lock);
200 result = _tdm_config_get_string_internal(g_dic, key, NULL);
201 pthread_mutex_unlock(&g_dic_lock);
204 TDM_INFO("%s = %d: no key", key, default_value);
205 return default_value;
208 value = (int)strtol(result, NULL, 0);
210 TDM_INFO("%s = %d", key, value);
216 tdm_config_get_string(const char *key, const char *default_value)
220 TDM_RETURN_VAL_IF_FAIL(key != NULL, default_value);
223 TDM_INFO("%s = %s: default", key, default_value);
224 return default_value;
227 pthread_mutex_lock(&g_dic_lock);
228 result = _tdm_config_get_string_internal(g_dic, key, default_value);
229 pthread_mutex_unlock(&g_dic_lock);
231 TDM_INFO("%s = %s", key, result);
237 tdm_config_set_int(const char *key, int value)
239 char temp[TDM_NAME_LEN];
242 TDM_RETURN_VAL_IF_FAIL(key != NULL, TDM_ERROR_INVALID_PARAMETER);
245 TDM_INFO("%s = %d set failed", key, value);
246 return TDM_ERROR_BAD_REQUEST;
249 snprintf(temp, sizeof temp, "%d", value);
251 pthread_mutex_lock(&g_dic_lock);
252 ret = iniparser_set(g_dic, key, (const char*)temp);
253 pthread_mutex_unlock(&g_dic_lock);
255 TDM_RETURN_VAL_IF_FAIL(ret == 0, TDM_ERROR_OPERATION_FAILED);
257 TDM_INFO("%s = %d set done", key, value);
259 return TDM_ERROR_NONE;
263 tdm_config_set_string(const char *key, const char *value)
267 TDM_RETURN_VAL_IF_FAIL(key != NULL, TDM_ERROR_INVALID_PARAMETER);
270 TDM_INFO("%s = %s set failed", key, value);
271 return TDM_ERROR_BAD_REQUEST;
274 pthread_mutex_lock(&g_dic_lock);
275 ret = iniparser_set(g_dic, key, value);
276 pthread_mutex_unlock(&g_dic_lock);
278 TDM_RETURN_VAL_IF_FAIL(ret == 0, TDM_ERROR_OPERATION_FAILED);
280 TDM_INFO("%s = %s set done", key, value);
282 return TDM_ERROR_NONE;