From 463d77c457fe6d7e09182f474a5c465d7c0b9595 Mon Sep 17 00:00:00 2001 From: Wonnam Jang Date: Thu, 8 Mar 2018 09:02:04 +0900 Subject: [PATCH] Load default config file when user config is invalid Change-Id: Ia34d37a7799c923b1e6127f1bd127868a59a380d Signed-off-by: Wonnam Jang --- common/stt_config_parser.c | 58 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/common/stt_config_parser.c b/common/stt_config_parser.c index 6786c71..77d0c79 100644 --- a/common/stt_config_parser.c +++ b/common/stt_config_parser.c @@ -12,6 +12,8 @@ */ #include +#include +#include #include #include "stt_defs.h" @@ -319,14 +321,37 @@ int stt_parser_load_config(stt_config_s** config_info) xmlChar *key; bool is_default_open = false; - doc = xmlParseFile(STT_CONFIG); - if (doc == NULL) { + if (0 != access(STT_CONFIG, F_OK)) { doc = xmlParseFile(STT_DEFAULT_CONFIG); if (doc == NULL) { SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_DEFAULT_CONFIG); + xmlCleanupParser(); return -1; } is_default_open = true; + } else { + int retry_count = 0; + + while (NULL == doc) { + doc = xmlParseFile(STT_CONFIG); + if (NULL != doc) { + break; + } + retry_count++; + usleep(10000); + + if (STT_RETRY_COUNT == retry_count) { + SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_CONFIG); + doc = xmlParseFile(STT_DEFAULT_CONFIG); + if (NULL == doc) { + SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_DEFAULT_CONFIG); + xmlCleanupParser(); + return -1; + } + is_default_open = true; + break; + } + } } cur = xmlDocGetRootElement(doc); @@ -450,8 +475,33 @@ int stt_parser_load_config(stt_config_s** config_info) *config_info = temp; g_config_doc = doc; - if (is_default_open) - xmlSaveFile(STT_CONFIG, g_config_doc); + if (is_default_open) { + int retry_count = 0; + int ret = -1; + do { + ret = xmlSaveFile(STT_CONFIG, g_config_doc); + if (0 < ret) + break; + retry_count++; + usleep(10000); + + if (STT_RETRY_COUNT == retry_count) { + SLOG(LOG_ERROR, stt_tag(), "[ERROR] Save result : %d", ret); + return -1; + } + } while (0 != ret); + + /* Set mode */ + if (0 > chmod(STT_CONFIG, 0600)) { + SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to change file mode : %d", ret); + } + + /* Set owner */ + if (0 > chown(STT_CONFIG, 5000, 5000)) { + SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to change file owner : %d", ret); + } + SLOG(LOG_DEBUG, stt_tag(), "Default config is changed : pid(%d)", getpid()); + } return 0; } -- 2.7.4