--- /dev/null
+/**************************************************************************
+ *
+ * libtdm
+ *
+ * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
+ *
+ * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
+ * JinYoung Jeon <jy0.jeon@samsung.com>,
+ * Taeheon Kim <th908.kim@samsung.com>,
+ * YoungJun Cho <yj44.cho@samsung.com>,
+ * SooChan Lim <sc1.lim@samsung.com>,
+ * Boram Park <sc1.lim@samsung.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+**************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <iniparser.h>
+
+#include "tdm_log.h"
+#include "tdm_macro.h"
+#include "tdm_list.h"
+#include "tdm.h"
+#include "tdm_private.h"
+#include "tdm_config.h"
+
+#define TDM_CONFIG_FILENAME "tdm.ini"
+#define TDM_CONFIG_GENERAL_SECTION "general"
+
+static dictionary *g_dic = NULL;
+static pthread_mutex_t g_dic_lock;
+
+static int
+_tdm_config_check_file_owner(const char *filepath)
+{
+ struct stat sb;
+
+ if (stat(filepath, &sb) < 0) {
+ TDM_WRN("%s: %m", filepath);
+ return -1;
+ }
+
+ if (sb.st_uid != getuid()) {
+ TDM_WRN("'%s': not owned by %u", filepath, getuid());
+ return -1;
+ }
+
+ return 0;
+}
+
+static dictionary *
+_tdm_config_load_file(const char *dir, const char *filename)
+{
+ char filepath[TDM_PATH_LEN];
+ dictionary *dic;
+
+ snprintf(filepath, sizeof filepath, "%s/%s", dir, filename);
+
+ if (_tdm_config_check_file_owner(filepath) < 0)
+ return NULL;
+
+ dic = iniparser_load(filepath);
+ TDM_RETURN_VAL_IF_FAIL(dic != NULL, NULL);
+
+ if (!iniparser_find_entry(dic, TDM_CONFIG_GENERAL_SECTION)) {
+ TDM_ERR("no '%s' section: '%s'", TDM_CONFIG_GENERAL_SECTION, filepath);
+ iniparser_freedict(dic);
+ return NULL;
+ }
+
+ TDM_INFO("opened successed: '%s'", filepath);
+
+ return dic;
+}
+
+static dictionary *
+_tdm_config_load(void)
+{
+ dictionary *dic = NULL;
+
+#if 0
+ /* not allowed: try to read from RW directory */
+ dic = _tdm_config_load_file(TDM_SYSCONF_PATH, TDM_CONFIG_FILENAME);
+#endif
+
+ if (!dic) {
+ /* try to read from RO directory */
+ dic = _tdm_config_load_file(TDM_DATA_PATH, TDM_CONFIG_FILENAME);
+ }
+
+ return dic;
+}
+
+INTERN tdm_error
+tdm_config_init(void)
+{
+ if (g_dic) {
+ TDM_ERR("init failed: twice");
+ return TDM_ERROR_OPERATION_FAILED;
+ }
+
+ if (pthread_mutex_init(&g_dic_lock, NULL)) {
+ TDM_ERR("mutex init failed: %m");
+ return TDM_ERROR_OUT_OF_MEMORY;
+ }
+
+ g_dic = _tdm_config_load();
+ if (!g_dic) {
+ if (!getenv("TDM_NO_CONFIG")) {
+ TDM_ERR("Loading config file failed!!");
+ pthread_mutex_destroy(&g_dic_lock);
+ return TDM_ERROR_NONE;
+ }
+ }
+
+ TDM_INFO("tdm config init done (%p)", g_dic);
+
+ return TDM_ERROR_NONE;
+}
+
+INTERN void
+tdm_config_deinit(void)
+{
+ if (!g_dic) {
+ return;
+ }
+
+ iniparser_freedict(g_dic);
+ g_dic = NULL;
+
+ /* we don't need to lock/unlock here because we come here
+ * after tdm_thread has been destroyed
+ */
+ pthread_mutex_destroy(&g_dic_lock);
+
+ TDM_INFO("tdm config deinit done");
+}
+
+static const char*
+_tdm_config_get_string_internal(dictionary *dic, const char *key, const char *default_value)
+{
+ char *temp = NULL;
+ const char *result;
+
+ if (default_value) {
+ temp = strdup(default_value);
+ if (!temp) {
+ TDM_ERR("strdup failed: %m");
+ return default_value;
+ }
+ }
+
+ result = (const char *)iniparser_getstring(dic, key, temp);
+ if (!result || strlen(result) == 0) {
+ free(temp);
+ return default_value;
+ }
+
+ free(temp);
+
+ return result;
+}
+
+INTERN int
+tdm_config_get_int(const char *key, int default_value)
+{
+ const char *result;
+ int value;
+
+ TDM_RETURN_VAL_IF_FAIL(key != NULL, default_value);
+
+ if (!g_dic) {
+ TDM_INFO("%s = %d: default", key, default_value);
+ return default_value;
+ }
+
+ pthread_mutex_lock(&g_dic_lock);
+ result = _tdm_config_get_string_internal(g_dic, key, NULL);
+ pthread_mutex_unlock(&g_dic_lock);
+
+ if (!result) {
+ TDM_INFO("%s = %d: no key", key, default_value);
+ return default_value;
+ }
+
+ value = (int)strtol(result, NULL, 0);
+
+ TDM_INFO("%s = %d", key, value);
+
+ return value;
+}
+
+INTERN const char*
+tdm_config_get_string(const char *key, const char *default_value)
+{
+ const char *result;
+
+ TDM_RETURN_VAL_IF_FAIL(key != NULL, default_value);
+
+ if (!g_dic) {
+ TDM_INFO("%s = %s: default", key, default_value);
+ return default_value;
+ }
+
+ pthread_mutex_lock(&g_dic_lock);
+ result = _tdm_config_get_string_internal(g_dic, key, default_value);
+ pthread_mutex_unlock(&g_dic_lock);
+
+ TDM_INFO("%s = %s", key, result);
+
+ return result;
+}
+
+INTERN tdm_error
+tdm_config_set_int(const char *key, int value)
+{
+ char temp[TDM_NAME_LEN];
+ int ret;
+
+ TDM_RETURN_VAL_IF_FAIL(key != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+ if (!g_dic) {
+ TDM_INFO("%s = %d set failed", key, value);
+ return TDM_ERROR_BAD_REQUEST;
+ }
+
+ snprintf(temp, sizeof temp, "%d", value);
+
+ pthread_mutex_lock(&g_dic_lock);
+ ret = iniparser_set(g_dic, key, (const char*)temp);
+ pthread_mutex_unlock(&g_dic_lock);
+
+ TDM_RETURN_VAL_IF_FAIL(ret == 0, TDM_ERROR_OPERATION_FAILED);
+
+ TDM_INFO("%s = %d set done", key, value);
+
+ return TDM_ERROR_NONE;
+}
+
+INTERN tdm_error
+tdm_config_set_string(const char *key, const char *value)
+{
+ int ret;
+
+ TDM_RETURN_VAL_IF_FAIL(key != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+ if (!g_dic) {
+ TDM_INFO("%s = %s set failed", key, value);
+ return TDM_ERROR_BAD_REQUEST;
+ }
+
+ pthread_mutex_lock(&g_dic_lock);
+ ret = iniparser_set(g_dic, key, value);
+ pthread_mutex_unlock(&g_dic_lock);
+
+ TDM_RETURN_VAL_IF_FAIL(ret == 0, TDM_ERROR_OPERATION_FAILED);
+
+ TDM_INFO("%s = %s set done", key, value);
+
+ return TDM_ERROR_NONE;
+}
--- /dev/null
+/**************************************************************************
+ *
+ * libtdm
+ *
+ * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
+ *
+ * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
+ * JinYoung Jeon <jy0.jeon@samsung.com>,
+ * Taeheon Kim <th908.kim@samsung.com>,
+ * YoungJun Cho <yj44.cho@samsung.com>,
+ * SooChan Lim <sc1.lim@samsung.com>,
+ * Boram Park <sc1.lim@samsung.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+**************************************************************************/
+
+#ifndef _TDM_CONFIG_H_
+#define _TDM_CONFIG_H_
+
+#include "tdm_common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "tdm_common.h"
+
+/**
+ * @file tdm_config.h
+ * @brief The config header file for a frontend library
+ */
+
+tdm_error
+tdm_config_init(void);
+void
+tdm_config_deinit(void);
+
+tdm_error
+tdm_config_set_int(const char *key, int value);
+tdm_error
+tdm_config_set_string(const char *key, const char *value);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _TDM_CONFIG_H_ */