rename tdm_backend to tdm_module
[platform/core/uifw/libtdm.git] / src / tdm_config.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
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>
13  *
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:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
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.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <iniparser.h>
41
42 #include "tdm_log.h"
43 #include "tdm_macro.h"
44 #include "tdm_list.h"
45 #include "tdm.h"
46 #include "tdm_private.h"
47 #include "tdm_config.h"
48
49 #define TDM_CONFIG_FILENAME "tdm.ini"
50 #define TDM_CONFIG_GENERAL_SECTION "general"
51
52 static pthread_mutex_t g_dic_lock = PTHREAD_MUTEX_INITIALIZER;
53 static dictionary *g_dic = NULL;
54
55 static int
56 _tdm_config_check_file_owner(const char *filepath)
57 {
58         struct stat sb;
59
60         if (stat(filepath, &sb) < 0) {
61                 TDM_WRN("%s: %m", filepath);
62                 return -1;
63         }
64
65         if (sb.st_uid != getuid()) {
66                 TDM_WRN("'%s': not owned by %u", filepath, getuid());
67                 return -1;
68         }
69
70         return 0;
71 }
72
73 static dictionary *
74 _tdm_config_load_file(const char *dir, const char *filename)
75 {
76         char filepath[TDM_PATH_LEN];
77         dictionary *dic;
78
79         snprintf(filepath, sizeof filepath, "%s/%s", dir, filename);
80
81         if (_tdm_config_check_file_owner(filepath) < 0)
82                 return NULL;
83
84         dic = iniparser_load(filepath);
85         TDM_RETURN_VAL_IF_FAIL(dic != NULL, NULL);
86
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);
90                 return NULL;
91         }
92
93         TDM_INFO("opened successed: '%s'", filepath);
94
95         return dic;
96 }
97
98 static void
99 _tdm_config_check_logs(void)
100 {
101         const char *path;
102         int level;
103
104         pthread_mutex_unlock(&g_dic_lock);
105
106         level = tdm_config_get_int(TDM_CONFIG_KEY_DEBUG_LOG_LEVEL, 3);
107         tdm_log_set_debug_level(level);
108
109         level = tdm_config_get_int(TDM_CONFIG_KEY_DEBUG_ASSERT_LEVEL, 0);
110         tdm_log_set_assert_level(level);
111
112         /* if TDM_CONFIG_KEY_DEBUG_LOG_PATH is setted, TDM_CONFIG_KEY_DEBUG_DLOG will be ignored. */
113         path = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_LOG_PATH, NULL);
114         if (path) {
115                 tdm_log_enable_dlog(0);
116                 tdm_log_set_path(path);
117         } else {
118                 int dlog = tdm_config_get_int(TDM_CONFIG_KEY_DEBUG_DLOG, 1);
119                 tdm_log_enable_dlog(dlog);
120         }
121
122         pthread_mutex_lock(&g_dic_lock);
123 }
124
125 static unsigned int
126 _tdm_config_check_init(void)
127 {
128         if (g_dic)
129                 return 1;
130
131         g_dic = _tdm_config_load_file(TDM_DATA_PATH, TDM_CONFIG_FILENAME);
132
133         _tdm_config_check_logs();
134
135         TDM_INFO("tdm config init %s (%p)", (g_dic) ? "successed" : "failed", g_dic);
136
137         return (g_dic) ? 1 : 0;
138 }
139
140 INTERN void
141 tdm_config_deinit(void)
142 {
143         pthread_mutex_lock(&g_dic_lock);
144
145         if (!g_dic) {
146                 pthread_mutex_unlock(&g_dic_lock);
147                 return;
148         }
149
150         iniparser_freedict(g_dic);
151         g_dic = NULL;
152
153         TDM_INFO("tdm config deinit done");
154
155         pthread_mutex_unlock(&g_dic_lock);
156 }
157
158 static const char*
159 _tdm_config_get_string_internal(dictionary *dic, const char *key, const char *default_value)
160 {
161         char *temp = NULL;
162         const char *result;
163
164         if (default_value) {
165                 temp = strdup(default_value);
166                 if (!temp) {
167                         TDM_ERR("strdup failed: %m");
168                         return default_value;
169                 }
170         }
171
172         result = (const char *)iniparser_getstring(dic, key, temp);
173         if (!result || strlen(result) == 0) {
174                 free(temp);
175                 return default_value;
176         }
177
178         free(temp);
179
180         return result;
181 }
182
183 EXTERN int
184 tdm_config_get_int(const char *key, int default_value)
185 {
186         const char *result;
187         int value;
188
189         TDM_RETURN_VAL_IF_FAIL(key != NULL, default_value);
190
191         pthread_mutex_lock(&g_dic_lock);
192
193         if (!_tdm_config_check_init()) {
194                 TDM_INFO("%s = %d: default", key, default_value);
195                 pthread_mutex_unlock(&g_dic_lock);
196                 return default_value;
197         }
198
199         result = _tdm_config_get_string_internal(g_dic, key, NULL);
200         pthread_mutex_unlock(&g_dic_lock);
201
202         if (!result) {
203                 TDM_INFO("%s = %d: no key", key, default_value);
204                 return default_value;
205         }
206
207         value = (int)strtol(result, NULL, 0);
208
209         TDM_INFO("%s = %d", key, value);
210
211         return value;
212 }
213
214 EXTERN const char*
215 tdm_config_get_string(const char *key, const char *default_value)
216 {
217         const char *result;
218
219         TDM_RETURN_VAL_IF_FAIL(key != NULL, default_value);
220
221         pthread_mutex_lock(&g_dic_lock);
222
223         if (!_tdm_config_check_init()) {
224                 TDM_INFO("%s = %s: default", key, default_value);
225                 pthread_mutex_unlock(&g_dic_lock);
226                 return default_value;
227         }
228
229         result = _tdm_config_get_string_internal(g_dic, key, default_value);
230         pthread_mutex_unlock(&g_dic_lock);
231
232         TDM_INFO("%s = %s", key, result);
233
234         return result;
235 }
236
237 EXTERN tdm_error
238 tdm_config_set_int(const char *key, int value)
239 {
240         char temp[TDM_NAME_LEN];
241         int ret;
242
243         TDM_RETURN_VAL_IF_FAIL(key != NULL, TDM_ERROR_INVALID_PARAMETER);
244
245         snprintf(temp, sizeof temp, "%d", value);
246
247         pthread_mutex_lock(&g_dic_lock);
248
249         if (!_tdm_config_check_init()) {
250                 TDM_INFO("%s = %d set failed", key, value);
251                 pthread_mutex_unlock(&g_dic_lock);
252                 return TDM_ERROR_BAD_REQUEST;
253         }
254
255         ret = iniparser_set(g_dic, key, (const char*)temp);
256
257         pthread_mutex_unlock(&g_dic_lock);
258
259         TDM_RETURN_VAL_IF_FAIL(ret == 0, TDM_ERROR_OPERATION_FAILED);
260
261         TDM_INFO("%s = %d set done", key, value);
262
263         return TDM_ERROR_NONE;
264 }
265
266 EXTERN tdm_error
267 tdm_config_set_string(const char *key, const char *value)
268 {
269         int ret;
270
271         TDM_RETURN_VAL_IF_FAIL(key != NULL, TDM_ERROR_INVALID_PARAMETER);
272
273         pthread_mutex_lock(&g_dic_lock);
274
275         if (!_tdm_config_check_init()) {
276                 TDM_INFO("%s = %s set failed", key, value);
277                 pthread_mutex_unlock(&g_dic_lock);
278                 return TDM_ERROR_BAD_REQUEST;
279         }
280
281         ret = iniparser_set(g_dic, key, value);
282
283         pthread_mutex_unlock(&g_dic_lock);
284
285         TDM_RETURN_VAL_IF_FAIL(ret == 0, TDM_ERROR_OPERATION_FAILED);
286
287         TDM_INFO("%s = %s set done", key, value);
288
289         return TDM_ERROR_NONE;
290 }