Fix unsafety issue on config_mgr 34/222434/1
authorwn.jang <wn.jang@samsung.com>
Thu, 12 Dec 2019 22:54:48 +0000 (07:54 +0900)
committerWonnam Jang <wn.jang@samsung.com>
Wed, 15 Jan 2020 02:33:51 +0000 (02:33 +0000)
Change-Id: I7e2cc810d0fbddbbf874e7469699ef903dfdfb4b
(cherry picked from commit 8c484a46c8f27348ac92aec42afdd61cb89e6c30)

common/vc_config_mgr.c
common/vc_config_parser.c

index 4edf45b..ca4895e 100644 (file)
@@ -336,10 +336,14 @@ int __vc_config_mgr_get_engine_info()
        int filesize;
        vc_engine_info_s* info = NULL;
 
+       SLOG(LOG_DEBUG, vc_config_tag(), "[CONFIG] Release engine and Unregister engine config");
+
        __vc_config_release_engine();
        g_engine_list = NULL;
        __vc_config_mgr_unregister_engine_config_updated_event();
 
+       SLOG(LOG_DEBUG, vc_config_tag(), "[CONFIG] default directory : %s", VC_DEFAULT_ENGINE_INFO);
+
        /* Copy default info directory to download directory */
        dp  = opendir(VC_DEFAULT_ENGINE_INFO);
        if (NULL == dp) {
@@ -425,6 +429,8 @@ int __vc_config_mgr_get_engine_info()
 static Eina_Bool __vc_config_mgr_engine_config_inotify_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
 {
        SLOG(LOG_DEBUG, vc_config_tag(), "@@@ Engine config updated callback event");
+       ecore_thread_main_loop_begin();
+       pthread_mutex_lock(&vc_config_mgr_mutex);
 
        vc_engine_inotify_s *ino = (vc_engine_inotify_s *)data;
        int dir_fd = ino->dir_fd;
@@ -437,6 +443,8 @@ static Eina_Bool __vc_config_mgr_engine_config_inotify_event_callback(void* data
        if (0 > length) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Empty Inotify event");
                SLOG(LOG_DEBUG, vc_config_tag(), "@@@");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
+               ecore_thread_main_loop_end();
                return ECORE_CALLBACK_DONE;
        }
 
@@ -472,6 +480,8 @@ static Eina_Bool __vc_config_mgr_engine_config_inotify_event_callback(void* data
 
        SLOG(LOG_DEBUG, vc_config_tag(), "@@@");
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
+       ecore_thread_main_loop_end();
        return ECORE_CALLBACK_PASS_ON;
 }
 
@@ -557,10 +567,6 @@ static int __vc_config_mgr_unregister_engine_config_updated_event()
        return 0;
 }
 
-
-
-
-
 Eina_Bool vc_config_mgr_inotify_event_cb(void* data, Ecore_Fd_Handler *fd_handler)
 {
        SLOG(LOG_DEBUG, vc_config_tag(), "@@@ Config changed callback event");
@@ -802,11 +808,13 @@ int __vc_config_set_auto_language()
 
 void __vc_config_language_changed_cb(keynode_t *key, void *data)
 {
-       if (true == g_config_info->auto_lang) {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
+       if (g_config_info && true == g_config_info->auto_lang) {
                /* Get voice input vconf key */
                __vc_config_set_auto_language();
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return;
 }
 
@@ -816,11 +824,8 @@ int vc_config_mgr_initialize(int uid)
        int* get_uid;
        vc_config_client_s* temp_client = NULL;
 
-       if (0 != pthread_mutex_init(&vc_config_mgr_mutex, NULL)) {
-               SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to initialize vc_config_mgr_mutex.");
-       }
-
        SLOG(LOG_INFO, vc_config_tag(), "[WARNING] Enter critical section");
+       ecore_thread_main_loop_begin();
        pthread_mutex_lock(&vc_config_mgr_mutex);
 
        if (0 < g_slist_length(g_config_client_list)) {
@@ -833,6 +838,7 @@ int vc_config_mgr_initialize(int uid)
                        if (uid == *get_uid) {
                                SLOG(LOG_WARN, vc_config_tag(), "[CONFIG] uid(%d) has already registered", uid);
                                pthread_mutex_unlock(&vc_config_mgr_mutex);
+                               ecore_thread_main_loop_end();
                                SLOG(LOG_DEBUG, vc_config_tag(), "[WARNING] Leave critical section");
                                return 0;
                        }
@@ -844,6 +850,7 @@ int vc_config_mgr_initialize(int uid)
                if (NULL == temp_client) {
                        SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to allocate memory");
                        pthread_mutex_unlock(&vc_config_mgr_mutex);
+                       ecore_thread_main_loop_end();
                        return VC_ERROR_OUT_OF_MEMORY;
                }
 
@@ -859,6 +866,7 @@ int vc_config_mgr_initialize(int uid)
 
                __vc_config_mgr_print_client_info();
                pthread_mutex_unlock(&vc_config_mgr_mutex);
+               ecore_thread_main_loop_end();
                SLOG(LOG_DEBUG, vc_config_tag(), "[WARNING] Leave critical section");
                return 0;
        }
@@ -871,6 +879,7 @@ int vc_config_mgr_initialize(int uid)
                        SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to make directory : %s", VC_CONFIG_BASE);
                        __vc_config_release_client(uid);
                        pthread_mutex_unlock(&vc_config_mgr_mutex);
+                       ecore_thread_main_loop_end();
                        return -1;
                } else {
                        SLOG(LOG_DEBUG, vc_config_tag(), "Success to make directory : %s", VC_CONFIG_BASE);
@@ -881,6 +890,7 @@ int vc_config_mgr_initialize(int uid)
                        SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to make directory : %s", VC_RUNTIME_INFO_ROOT);
                        __vc_config_release_client(uid);
                        pthread_mutex_unlock(&vc_config_mgr_mutex);
+                       ecore_thread_main_loop_end();
                        return -1;
                } else {
                        SLOG(LOG_DEBUG, vc_config_tag(), "Success to make directory : %s", VC_RUNTIME_INFO_ROOT);
@@ -892,6 +902,7 @@ int vc_config_mgr_initialize(int uid)
                        SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to make directory : %s", VC_DOWNLOAD_BASE);
                        __vc_config_release_client(uid);
                        pthread_mutex_unlock(&vc_config_mgr_mutex);
+                       ecore_thread_main_loop_end();
                        return -1;
                } else {
                        SLOG(LOG_DEBUG, vc_config_tag(), "Success to make directory : %s", VC_DOWNLOAD_BASE);
@@ -902,6 +913,7 @@ int vc_config_mgr_initialize(int uid)
                        SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to make directory : %s", VC_DOWNLOAD_ENGINE_INFO);
                        __vc_config_release_client(uid);
                        pthread_mutex_unlock(&vc_config_mgr_mutex);
+                       ecore_thread_main_loop_end();
                        return -1;
                } else {
                        SLOG(LOG_DEBUG, vc_config_tag(), "Success to make directory : %s", VC_DOWNLOAD_ENGINE_INFO);
@@ -913,6 +925,7 @@ int vc_config_mgr_initialize(int uid)
                __vc_config_release_client(uid);
                __vc_config_release_engine();
                pthread_mutex_unlock(&vc_config_mgr_mutex);
+               ecore_thread_main_loop_end();
                return VC_CONFIG_ERROR_ENGINE_NOT_FOUND;
        }
 
@@ -923,6 +936,7 @@ int vc_config_mgr_initialize(int uid)
                __vc_config_release_client(uid);
                __vc_config_release_engine();
                pthread_mutex_unlock(&vc_config_mgr_mutex);
+               ecore_thread_main_loop_end();
                return -1;
        }
 
@@ -931,7 +945,9 @@ int vc_config_mgr_initialize(int uid)
                __vc_config_release_client(uid);
                __vc_config_release_engine();
                vc_parser_unload_config(g_config_info);
+               g_config_info = NULL;
                pthread_mutex_unlock(&vc_config_mgr_mutex);
+               ecore_thread_main_loop_end();
                return VC_CONFIG_ERROR_ENGINE_NOT_FOUND;
        }
 
@@ -947,7 +963,9 @@ int vc_config_mgr_initialize(int uid)
                                __vc_config_release_client(uid);
                                __vc_config_release_engine();
                                vc_parser_unload_config(g_config_info);
+                               g_config_info = NULL;
                                pthread_mutex_unlock(&vc_config_mgr_mutex);
+                               ecore_thread_main_loop_end();
                                return -1;
                        }
 
@@ -963,7 +981,9 @@ int vc_config_mgr_initialize(int uid)
                                        __vc_config_release_client(uid);
                                        __vc_config_release_engine();
                                        vc_parser_unload_config(g_config_info);
+                                       g_config_info = NULL;
                                        pthread_mutex_unlock(&vc_config_mgr_mutex);
+                                       ecore_thread_main_loop_end();
                                        return -1;
                                }
 
@@ -990,7 +1010,9 @@ int vc_config_mgr_initialize(int uid)
                __vc_config_release_client(uid);
                __vc_config_release_engine();
                vc_parser_unload_config(g_config_info);
+               g_config_info = NULL;
                pthread_mutex_unlock(&vc_config_mgr_mutex);
+               ecore_thread_main_loop_end();
                return VC_ERROR_OUT_OF_MEMORY;
        }
 
@@ -1007,6 +1029,7 @@ int vc_config_mgr_initialize(int uid)
        __vc_config_mgr_print_client_info();
 
        pthread_mutex_unlock(&vc_config_mgr_mutex);
+       ecore_thread_main_loop_end();
        SLOG(LOG_DEBUG, vc_config_tag(), "[WARNING] Leave critical section");
        return 0;
 }
@@ -1020,28 +1043,21 @@ int vc_config_mgr_finalize(int uid)
        if (0 < __vc_config_release_client(uid)) {
                pthread_mutex_unlock(&vc_config_mgr_mutex);
                SLOG(LOG_DEBUG, vc_config_tag(), "[WARNING] Leave critical section");
-               
-               if (0 != pthread_mutex_destroy(&vc_config_mgr_mutex)) {
-                       SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to destroy vc_config_mgr_mutex.");
-               }
+
                return 0;
        }
 
        __vc_config_release_engine();
-       
+
        vconf_ignore_key_changed(VCONFKEY_LANGSET, __vc_config_language_changed_cb);
 
        vc_parser_unload_config(g_config_info);
+       g_config_info = NULL;
 
        pthread_mutex_unlock(&vc_config_mgr_mutex);
        SLOG(LOG_DEBUG, vc_config_tag(), "[WARNING] Leave critical section");
 
-       if (0 != pthread_mutex_destroy(&vc_config_mgr_mutex)) {
-               SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to destroy vc_config_mgr_mutex.");
-       }
-
        SLOG(LOG_DEBUG, vc_config_tag(), "[Success] Finalize config");
-
        return 0;
 }
 
@@ -1108,6 +1124,9 @@ int vc_config_mgr_set_engine_cb(int uid, vc_config_engine_changed_cb engine_cb)
        GSList *iter = NULL;
        vc_config_client_s* temp_client = NULL;
 
+       ecore_thread_main_loop_begin();
+       pthread_mutex_lock(&vc_config_mgr_mutex);
+
        /* Call all callbacks of client*/
        iter = g_slist_nth(g_config_client_list, 0);
 
@@ -1119,6 +1138,7 @@ int vc_config_mgr_set_engine_cb(int uid, vc_config_engine_changed_cb engine_cb)
                                temp_client->engine_cb = engine_cb;
                                if (0 != __vc_config_mgr_register_lang_event()) {
                                        SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to register config event");
+                                       pthread_mutex_unlock(&vc_config_mgr_mutex);
                                        return VC_CONFIG_ERROR_OPERATION_FAILED;
                                }
                        }
@@ -1126,6 +1146,8 @@ int vc_config_mgr_set_engine_cb(int uid, vc_config_engine_changed_cb engine_cb)
                iter = g_slist_next(iter);
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
+       ecore_thread_main_loop_end();
        return 0;
 }
 
@@ -1134,6 +1156,9 @@ int vc_config_mgr_unset_engine_cb(int uid)
        GSList *iter = NULL;
        vc_config_client_s* temp_client = NULL;
 
+       ecore_thread_main_loop_begin();
+       pthread_mutex_lock(&vc_config_mgr_mutex);
+
        /* Call all callbacks of client*/
        iter = g_slist_nth(g_config_client_list, 0);
 
@@ -1149,6 +1174,8 @@ int vc_config_mgr_unset_engine_cb(int uid)
                iter = g_slist_next(iter);
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
+       ecore_thread_main_loop_end();
        return 0;
 }
 
@@ -1158,6 +1185,9 @@ int vc_config_mgr_set_lang_cb(int uid, vc_config_lang_changed_cb lang_cb)
        GSList *iter = NULL;
        vc_config_client_s* temp_client = NULL;
 
+       ecore_thread_main_loop_begin();
+       pthread_mutex_lock(&vc_config_mgr_mutex);
+
        /* Call all callbacks of client*/
        iter = g_slist_nth(g_config_client_list, 0);
 
@@ -1169,6 +1199,7 @@ int vc_config_mgr_set_lang_cb(int uid, vc_config_lang_changed_cb lang_cb)
                                temp_client->lang_cb = lang_cb;
                                if (0 != __vc_config_mgr_register_lang_event()) {
                                        SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to register config event");
+                                       pthread_mutex_unlock(&vc_config_mgr_mutex);
                                        return VC_CONFIG_ERROR_OPERATION_FAILED;
                                }
                        }
@@ -1176,6 +1207,8 @@ int vc_config_mgr_set_lang_cb(int uid, vc_config_lang_changed_cb lang_cb)
                iter = g_slist_next(iter);
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
+       ecore_thread_main_loop_end();
        return 0;
 }
 
@@ -1184,6 +1217,9 @@ int vc_config_mgr_unset_lang_cb(int uid)
        GSList *iter = NULL;
        vc_config_client_s* temp_client = NULL;
 
+       ecore_thread_main_loop_begin();
+       pthread_mutex_lock(&vc_config_mgr_mutex);
+
        /* Call all callbacks of client*/
        iter = g_slist_nth(g_config_client_list, 0);
 
@@ -1199,6 +1235,8 @@ int vc_config_mgr_unset_lang_cb(int uid)
                iter = g_slist_next(iter);
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
+       ecore_thread_main_loop_end();
        return 0;
 }
 
@@ -1212,6 +1250,8 @@ int vc_config_mgr_set_enabled_cb(int uid, vc_config_enabled_cb enabled_cb)
        GSList *iter = NULL;
        vc_config_client_s* temp_client = NULL;
 
+       pthread_mutex_lock(&vc_config_mgr_mutex);
+
        /* Call all callbacks of client*/
        iter = g_slist_nth(g_config_client_list, 0);
 
@@ -1221,12 +1261,14 @@ int vc_config_mgr_set_enabled_cb(int uid, vc_config_enabled_cb enabled_cb)
                if (NULL != temp_client) {
                        if (uid == temp_client->uid) {
                                temp_client->enabled_cb = enabled_cb;
+                               pthread_mutex_unlock(&vc_config_mgr_mutex);
                                return VC_CONFIG_ERROR_NONE;
                        }
                }
                iter = g_slist_next(iter);
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return VC_CONFIG_ERROR_INVALID_PARAMETER;
 }
 
@@ -1235,6 +1277,8 @@ int vc_config_mgr_unset_enabled_cb(int uid)
        GSList *iter = NULL;
        vc_config_client_s* temp_client = NULL;
 
+       pthread_mutex_lock(&vc_config_mgr_mutex);
+
        /* Call all callbacks of client*/
        iter = g_slist_nth(g_config_client_list, 0);
 
@@ -1244,35 +1288,45 @@ int vc_config_mgr_unset_enabled_cb(int uid)
                if (NULL != temp_client) {
                        if (uid == temp_client->uid) {
                                temp_client->enabled_cb = NULL;
+                               pthread_mutex_unlock(&vc_config_mgr_mutex);
                                return 0;
                        }
                }
                iter = g_slist_next(iter);
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return VC_CONFIG_ERROR_INVALID_PARAMETER;
 }
 
 int vc_config_mgr_get_auto_language(bool* value)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
+
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Not initialized");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
        if (NULL == value) {
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
        *value = g_config_info->auto_lang;
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
 
        return 0;
 }
 
 int vc_config_mgr_set_auto_language(bool value)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
+
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Not initialized");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
@@ -1280,8 +1334,10 @@ int vc_config_mgr_set_auto_language(bool value)
                /* Check language is valid */
                if (0 != vc_parser_set_auto_lang(value)) {
                        SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to save engine id");
+                       pthread_mutex_unlock(&vc_config_mgr_mutex);
                        return -1;
                }
+
                g_config_info->auto_lang = value;
 
                if (true == g_config_info->auto_lang) {
@@ -1289,17 +1345,23 @@ int vc_config_mgr_set_auto_language(bool value)
                }
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return 0;
 }
 
 int vc_config_mgr_get_engine_list(vc_config_supported_engine_cb callback, void* user_data)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
+
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "Not initialized");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return VC_CONFIG_ERROR_INVALID_STATE;
        }
 
        if (NULL == callback) {
+               SLOG(LOG_ERROR, vc_config_tag(), " callback is NULL");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return VC_CONFIG_ERROR_INVALID_PARAMETER;
        }
 
@@ -1308,6 +1370,7 @@ int vc_config_mgr_get_engine_list(vc_config_supported_engine_cb callback, void*
 
        if (0 >= g_slist_length(g_engine_list)) {
                SLOG(LOG_WARN, vc_config_tag(), "[ERROR] Engine list is NULL");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return VC_CONFIG_ERROR_ENGINE_NOT_FOUND;
        }
 
@@ -1319,6 +1382,7 @@ int vc_config_mgr_get_engine_list(vc_config_supported_engine_cb callback, void*
 
                if (NULL == engine_info) {
                        SLOG(LOG_ERROR, vc_config_tag(), " Engine info is NULL");
+                       pthread_mutex_unlock(&vc_config_mgr_mutex);
                        return VC_CONFIG_ERROR_OPERATION_FAILED;
                }
 
@@ -1328,18 +1392,23 @@ int vc_config_mgr_get_engine_list(vc_config_supported_engine_cb callback, void*
                iter = g_slist_next(iter);
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return VC_CONFIG_ERROR_NONE;
 }
 
 
 int vc_config_mgr_get_engine(char** engine)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "Not initialized"); //LCOV_EXCL_LINE
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return VC_CONFIG_ERROR_INVALID_STATE;
        }
 
        if (NULL == engine) {
+               SLOG(LOG_ERROR, vc_config_tag(), "Parameter is NULL");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return VC_CONFIG_ERROR_INVALID_PARAMETER;
        }
 
@@ -1347,9 +1416,11 @@ int vc_config_mgr_get_engine(char** engine)
                *engine = strdup(g_config_info->engine_id);
        } else {
                SLOG(LOG_ERROR, vc_config_tag(), " Engine id is NULL"); //LCOV_EXCL_LINE
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return VC_CONFIG_ERROR_ENGINE_NOT_FOUND;
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return VC_CONFIG_ERROR_NONE;
 }
 
@@ -1411,21 +1482,26 @@ int __vc_config_set_buxtonkey(const char* engine)
 
 int vc_config_mgr_set_engine(const char* engine)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "Not initialized");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return VC_CONFIG_ERROR_INVALID_STATE;
        }
 
        if (NULL == g_config_info) {
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return VC_CONFIG_ERROR_OPERATION_FAILED;
        }
 
        if (NULL == engine || NULL == g_config_info->engine_id) {
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return VC_CONFIG_ERROR_INVALID_PARAMETER;
        }
 
        /* Check current engine id with new engine id */
        if (0 == strncmp(g_config_info->engine_id, engine, strlen(g_config_info->engine_id))) {
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return 0;
        }
 
@@ -1434,6 +1510,7 @@ int vc_config_mgr_set_engine(const char* engine)
        int ret = __vc_config_set_buxtonkey(engine);
        if (0 != ret) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] set buxtonkey Failed!!!");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return ret;
        }
 
@@ -1521,25 +1598,31 @@ int vc_config_mgr_set_engine(const char* engine)
 
                if (0 != vc_parser_set_engine(g_config_info->engine_id)) {
                                SLOG(LOG_ERROR, vc_config_tag(), " Fail to save config");
+                               pthread_mutex_unlock(&vc_config_mgr_mutex);
                                return VC_CONFIG_ERROR_OPERATION_FAILED;
                }
        } else {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Engine id is not valid");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return VC_CONFIG_ERROR_INVALID_PARAMETER;
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return VC_CONFIG_ERROR_NONE;
 }
 
 int vc_config_mgr_get_language_list(vc_supported_language_cb callback, void* user_data)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Not initialized");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
        if (0 >= g_slist_length(g_engine_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] There is no engine");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
@@ -1554,6 +1637,7 @@ int vc_config_mgr_get_language_list(vc_supported_language_cb callback, void* use
 
                if (NULL == engine_info) {
                        SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] engine info is NULL");
+                       pthread_mutex_unlock(&vc_config_mgr_mutex);
                        return -1;
                }
 
@@ -1584,17 +1668,21 @@ int vc_config_mgr_get_language_list(vc_supported_language_cb callback, void* use
                break;
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return 0;
 }
 
 int vc_config_mgr_get_default_language(char** language)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Not initialized");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
        if (NULL == language) {
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
@@ -1602,20 +1690,25 @@ int vc_config_mgr_get_default_language(char** language)
                *language = strdup(g_config_info->language);
        } else {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] language is NULL");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return 0;
 }
 
 int vc_config_mgr_set_default_language(const char* language)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Not initialized");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
        if (NULL == language) {
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
@@ -1623,65 +1716,80 @@ int vc_config_mgr_set_default_language(const char* language)
        if (NULL != g_config_info->language) {
                if (0 != vc_parser_set_language(language)) {
                        SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to save engine id");
+                       pthread_mutex_unlock(&vc_config_mgr_mutex);
                        return -1;
                }
                free(g_config_info->language);
                g_config_info->language = strdup(language);
        } else {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] language is NULL");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return 0;
 }
 
 int vc_config_mgr_get_enabled(bool* value)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Not initialized");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
        if (NULL == value) {
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
        *value = g_config_info->enabled;
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return 0;
 }
 
 int vc_config_mgr_set_enabled(bool value)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Not initialized");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
        if (0 != vc_parser_set_enabled(value)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to set enabled");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
        g_config_info->enabled = value;
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return 0;
 }
 
 int vc_config_mgr_get_nonfixed_support(bool* value)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Not initialized");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
        if (NULL == value) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Input parameter is NULL");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
        if (0 >= g_slist_length(g_engine_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] There is no engine");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
@@ -1696,6 +1804,7 @@ int vc_config_mgr_get_nonfixed_support(bool* value)
 
                if (NULL == engine_info) {
                        SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] engine info is NULL");
+                       pthread_mutex_unlock(&vc_config_mgr_mutex);
                        return -1;
                }
 
@@ -1709,22 +1818,28 @@ int vc_config_mgr_get_nonfixed_support(bool* value)
                break;
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return 0;
 }
 
 bool vc_config_check_default_engine_is_valid(const char* engine)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Not initialized");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
        if (NULL == engine) {
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return false;
        }
 
-       if (0 >= g_slist_length(g_engine_list))
+       if (0 >= g_slist_length(g_engine_list)) {
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return false;
+       }
 
        GSList *iter = NULL;
        vc_engine_info_s *engine_info = NULL;
@@ -1737,33 +1852,41 @@ bool vc_config_check_default_engine_is_valid(const char* engine)
 
                if (NULL != engine_info) {
                        if (0 == strcmp(engine, engine_info->uuid)) {
+                               pthread_mutex_unlock(&vc_config_mgr_mutex);
                                return true;
                        }
                }
                iter = g_slist_next(iter);
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return false;
 }
 
 bool vc_config_check_default_language_is_valid(const char* language)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Not initialized");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
        if (NULL == language) {
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return false;
        }
 
        if (NULL == g_config_info->engine_id) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Default engine id is NULL");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return false;
        }
 
-       if (0 >= g_slist_length(g_engine_list))
+       if (0 >= g_slist_length(g_engine_list)) {
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return false;
+       }
 
        GSList *iter = NULL;
        vc_engine_info_s *engine_info = NULL;
@@ -1794,8 +1917,10 @@ bool vc_config_check_default_language_is_valid(const char* language)
                while (NULL != iter_lang) {
                        lang = iter_lang->data;
 
-                       if (0 == strcmp(language, lang))
+                       if (0 == strcmp(language, lang)) {
+                               pthread_mutex_unlock(&vc_config_mgr_mutex);
                                return true;
+                       }
 
                        /*Get next item*/
                        iter_lang = g_slist_next(iter_lang);
@@ -1803,26 +1928,33 @@ bool vc_config_check_default_language_is_valid(const char* language)
                break;
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return false;
 }
 
 int vc_config_mgr_set_foreground(int pid, bool value)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Not initialized");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return vc_parser_set_foreground(pid, value);
 }
 
 int vc_config_mgr_get_foreground(int* pid)
 {
+       pthread_mutex_lock(&vc_config_mgr_mutex);
        if (0 >= g_slist_length(g_config_client_list)) {
                SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Not initialized");
+               pthread_mutex_unlock(&vc_config_mgr_mutex);
                return -1;
        }
 
+       pthread_mutex_unlock(&vc_config_mgr_mutex);
        return vc_parser_get_foreground(pid);
 }
 
index c2149a5..e2be6d8 100644 (file)
@@ -474,11 +474,20 @@ int vc_parser_load_config(vc_config_s** config_info)
 
 int vc_parser_unload_config(vc_config_s* config_info)
 {
-       if (NULL != g_config_doc)       xmlFreeDoc(g_config_doc);
+       if (NULL != g_config_doc) {
+               xmlFreeDoc(g_config_doc);
+               g_config_doc = NULL;
+       }
 
        if (NULL != config_info) {
-               if (NULL != config_info->engine_id)     free(config_info->engine_id);
-               if (NULL != config_info->language)      free(config_info->language);
+               if (NULL != config_info->engine_id) {
+                       free(config_info->engine_id);
+                       config_info->engine_id = NULL;
+               }
+               if (NULL != config_info->language) {
+                       free(config_info->language);
+                       config_info->language = NULL;
+               }
                free(config_info);
        }