Make db related functions to be thread-safe 20/166020/3
authorJunghoon Park <jh9216.park@samsung.com>
Fri, 5 Jan 2018 07:59:15 +0000 (16:59 +0900)
committerJunghoon Park <jh9216.park@samsung.com>
Mon, 8 Jan 2018 00:21:36 +0000 (09:21 +0900)
Change-Id: I3487e739faec8d63b3d329ff510ecc86ab876da6
Signed-off-by: Junghoon Park <jh9216.park@samsung.com>
src/service_db.c

index 5538fbd..b5a1cb4 100755 (executable)
@@ -74,10 +74,6 @@ struct allowed_info_s {
        char *allowed_appid;
 };
 
-static sqlite3 *svc_db;
-static sqlite3 *app_info_db;
-static sqlite3 *global_app_info_db;
-
 static char *__get_svc_db(uid_t uid)
 {
        char db_path[PATH_MAX];
@@ -96,12 +92,12 @@ static char *__get_svc_db(uid_t uid)
 /**
  * db initialize
  */
-static int __init(uid_t uid, bool readonly)
+static int __init(uid_t uid, bool readonly, sqlite3 **svc_db)
 {
        int rc;
        char *db_path;
 
-       if (svc_db) {
+       if (*svc_db) {
                _D("Already initialized");
                return 0;
        }
@@ -112,17 +108,17 @@ static int __init(uid_t uid, bool readonly)
                return -1;
        }
 
-       rc = sqlite3_open_v2(db_path, &svc_db,
+       rc = sqlite3_open_v2(db_path, svc_db,
                        readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE,
                        NULL);
        free(db_path);
        if (rc) {
                _E("Can't open database: %d, %s, extended: %d",
-                               rc, sqlite3_errmsg(svc_db),
-                               sqlite3_extended_errcode(svc_db));
-               if (svc_db) {
-                       sqlite3_close(svc_db);
-                       svc_db = NULL;
+                               rc, sqlite3_errmsg(*svc_db),
+                               sqlite3_extended_errcode(*svc_db));
+               if (*svc_db) {
+                       sqlite3_close(*svc_db);
+                       *svc_db = NULL;
                }
                return -1;
        }
@@ -204,12 +200,12 @@ static int __collate_appsvc(void *ucol, int str1_len, const void *str1,
        return -1;
 }
 
-static int __init_app_info_db(uid_t uid)
+static int __init_app_info_db(uid_t uid, sqlite3 **app_info_db, sqlite3 **global_app_info_db)
 {
        int rc;
        char *db_path;
 
-       if (app_info_db && global_app_info_db) {
+       if (*app_info_db && *global_app_info_db) {
                _D("Already initialized");
                return 0;
        }
@@ -220,77 +216,77 @@ static int __init_app_info_db(uid_t uid)
                return -1;
        }
 
-       rc = sqlite3_open_v2(db_path, &app_info_db, SQLITE_OPEN_READONLY, NULL);
+       rc = sqlite3_open_v2(db_path, app_info_db, SQLITE_OPEN_READONLY, NULL);
        free(db_path);
        if (rc) {
                _E("Can't open database: %d, %s, extended: %d",
-                               rc, sqlite3_errmsg(app_info_db),
-                               sqlite3_extended_errcode(app_info_db));
+                               rc, sqlite3_errmsg(*app_info_db),
+                               sqlite3_extended_errcode(*app_info_db));
                goto err;
        }
 
-       rc = sqlite3_exec(app_info_db, "PRAGMA journal_mode = PERSIST",
+       rc = sqlite3_exec(*app_info_db, "PRAGMA journal_mode = PERSIST",
                                        NULL, NULL, NULL);
        if (SQLITE_OK != rc) {
                _D("Fail to change journal mode");
                goto err;
        }
 
-       sqlite3_create_collation(app_info_db, SVC_COLLATION, SQLITE_UTF8,
+       sqlite3_create_collation(*app_info_db, SVC_COLLATION, SQLITE_UTF8,
                        NULL, __collate_appsvc);
 
-       rc = sqlite3_open_v2(APP_INFO_DB_PATH, &global_app_info_db,
+       rc = sqlite3_open_v2(APP_INFO_DB_PATH, global_app_info_db,
                        SQLITE_OPEN_READONLY, NULL);
        if (rc) {
                _E("Can't open database: %d, %s, extended: %d",
-                               rc, sqlite3_errmsg(global_app_info_db),
-                               sqlite3_extended_errcode(global_app_info_db));
+                               rc, sqlite3_errmsg(*global_app_info_db),
+                               sqlite3_extended_errcode(*global_app_info_db));
                goto err;
        }
 
-       rc = sqlite3_exec(global_app_info_db, "PRAGMA journal_mode = PERSIST",
+       rc = sqlite3_exec(*global_app_info_db, "PRAGMA journal_mode = PERSIST",
                        NULL, NULL, NULL);
        if (SQLITE_OK != rc) {
                _D("Fail to change journal mode");
                goto err;
        }
 
-       sqlite3_create_collation(global_app_info_db, SVC_COLLATION, SQLITE_UTF8,
+       sqlite3_create_collation(*global_app_info_db, SVC_COLLATION, SQLITE_UTF8,
                        NULL, __collate_appsvc);
 
        return 0;
 err:
-       if (app_info_db) {
-               sqlite3_close(app_info_db);
-               app_info_db = NULL;
+       if (*app_info_db) {
+               sqlite3_close(*app_info_db);
+               *app_info_db = NULL;
        }
 
-       if (global_app_info_db) {
-               sqlite3_close(global_app_info_db);
-               global_app_info_db = NULL;
+       if (*global_app_info_db) {
+               sqlite3_close(*global_app_info_db);
+               *global_app_info_db = NULL;
        }
 
        return -1;
 }
 
-static void __fini_app_info_db(void)
+static void __fini_app_info_db(sqlite3 **app_info_db, sqlite3 **global_app_info_db)
 {
-       if (app_info_db) {
-               sqlite3_close(app_info_db);
-               app_info_db = NULL;
+       if (*app_info_db) {
+               sqlite3_close(*app_info_db);
+               *app_info_db = NULL;
        }
 
-       if (global_app_info_db) {
-               sqlite3_close(global_app_info_db);
-               global_app_info_db = NULL;
+       if (*global_app_info_db) {
+               sqlite3_close(*global_app_info_db);
+               *global_app_info_db = NULL;
        }
 }
 
-static int __fini(void)
+static int __fini(sqlite3 **svc_db)
 {
-       if (svc_db) {
-               sqlite3_close(svc_db);
-               svc_db = NULL;
+       if (*svc_db) {
+               sqlite3_close(*svc_db);
+               *svc_db = NULL;
        }
        return 0;
 }
@@ -299,16 +295,20 @@ int _svc_db_check_perm(uid_t uid, bool readonly)
 {
        int ret = 0;
        char *db;
+       sqlite3 *svc_db = NULL;
 
-       if (__init(uid, readonly) < 0)
+       if (__init(uid, readonly, &svc_db) < 0)
                return -1;
 
        db = __get_svc_db(uid);
-       if (db == NULL)
+       if (db == NULL) {
+               __fini(&svc_db);
                return -1;
+       }
 
        ret = access(db, readonly ? R_OK : (R_OK | W_OK));
        free(db);
+       __fini(&svc_db);
        return ret;
 }
 
@@ -348,15 +348,16 @@ int _svc_db_add_app(const char *op, const char *mime_type, const char *uri,
                const char *pkg_name, uid_t uid)
 {
        int r;
+       sqlite3 *svc_db = NULL;
 
-       if (__init(uid, false) < 0)
+       if (__init(uid, false, &svc_db) < 0)
                return -1;
 
        if (op == NULL)
                return -1;
 
        r = __insert_info(svc_db, op, mime_type, uri, pkg_name);
-       __fini();
+       __fini(&svc_db);
 
        return r;
 }
@@ -390,17 +391,18 @@ static int __delete_info(sqlite3 *db, const char *appid)
 int _svc_db_delete_with_pkgname(const char *pkg_name, uid_t uid)
 {
        int r;
+       sqlite3 *svc_db = NULL;
 
        if (pkg_name == NULL) {
                _E("Invalid argument: data to delete is NULL");
                return -1;
        }
 
-       if (__init(uid, false) < 0)
+       if (__init(uid, false, &svc_db) < 0)
                return -1;
 
        r = __delete_info(svc_db, pkg_name);
-       __fini();
+       __fini(&svc_db);
 
        return r;
 }
@@ -409,17 +411,18 @@ int _svc_db_delete_all(uid_t uid)
 {
        const char query[] = "DELETE FROM appsvc;";
        int r;
+       sqlite3 *svc_db = NULL;
 
-       if (__init(uid, false) < 0)
+       if (__init(uid, false, &svc_db) < 0)
                return -1;
 
        r = sqlite3_exec(svc_db, query, NULL, NULL, NULL);
        if (r != SQLITE_OK) {
                _E("Exec failed: %s", sqlite3_errmsg(svc_db));
-               __fini();
+               __fini(&svc_db);
                return -1;
        }
-       __fini();
+       __fini(&svc_db);
 
        return 0;
 }
@@ -454,17 +457,18 @@ static int __get_count(sqlite3 *db, const char *appid)
 int _svc_db_is_defapp(const char *pkg_name, uid_t uid)
 {
        int r;
+       sqlite3 *svc_db = NULL;
 
        if (pkg_name == NULL) {
                _E("Invalid argument: data to delete is NULL");
                return 0;
        }
 
-       if (__init(uid, true) < 0)
+       if (__init(uid, true, &svc_db) < 0)
                return 0;
 
        r = __get_count(svc_db, pkg_name);
-       __fini();
+       __fini(&svc_db);
 
        if (r < 1)
                return 0;
@@ -520,19 +524,20 @@ char *_svc_db_get_app(const char *op, const char *mime_type, const char *uri,
 {
        char *appid = NULL;
        int r;
+       sqlite3 *svc_db = NULL;
 
        if (op == NULL)
                return NULL;
 
-       if (__init(uid, true) < 0)
+       if (__init(uid, true, &svc_db) < 0)
                return NULL;
 
        r = __get_appid(svc_db, op, mime_type, uri, &appid);
        if (r != 0) {
-               __fini();
+               __fini(&svc_db);
                return NULL;
        }
-       __fini();
+       __fini(&svc_db);
        SECURE_LOGD("appid: %s", appid);
 
        return appid;
@@ -543,8 +548,8 @@ static int __compare_appid(gconstpointer a, gconstpointer b)
        return strcmp(a, b);
 }
 
-static int __adjust_list_with_submode(sqlite3 *db, int mainapp_mode,
-               const char *win_id, GSList **list)
+static int __adjust_list_with_submode(sqlite3 *db, sqlite3 *app_info_db,
+               int mainapp_mode, const char *win_id, GSList **list)
 {
        const char query[] =
                "SELECT ac.app_id, ai.app_submode_mainid "
@@ -604,24 +609,26 @@ int _svc_db_adjust_list_with_submode(int mainapp_mode, char *win_id,
                GSList **pkg_list, uid_t uid)
 {
        int r;
+       sqlite3 *app_info_db = NULL;
+       sqlite3 *global_app_info_db = NULL;
 
-       if (__init_app_info_db(uid) < 0)
+       if (__init_app_info_db(uid, &app_info_db, &global_app_info_db) < 0)
                return 0;
 
-       r = __adjust_list_with_submode(app_info_db, mainapp_mode, win_id,
+       r = __adjust_list_with_submode(app_info_db, app_info_db, mainapp_mode, win_id,
                        pkg_list);
        if (r < 0) {
-               __fini_app_info_db();
+               __fini_app_info_db(&app_info_db, &global_app_info_db);
                return -1;
        }
 
-       r = __adjust_list_with_submode(global_app_info_db, mainapp_mode, win_id,
+       r = __adjust_list_with_submode(global_app_info_db, app_info_db, mainapp_mode, win_id,
                        pkg_list);
        if (r < 0) {
-               __fini_app_info_db();
+               __fini_app_info_db(&app_info_db, &global_app_info_db);
                return -1;
        }
-       __fini_app_info_db();
+       __fini_app_info_db(&app_info_db, &global_app_info_db);
 
        return 0;
 }
@@ -636,7 +643,7 @@ static int __get_list_with_query(sqlite3 *db, const char *query, GSList **list)
 
        r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
        if (r != SQLITE_OK) {
-               _E("Prepare failed: %s", sqlite3_errmsg(svc_db));
+               _E("Prepare failed: %s", sqlite3_errmsg(db));
                return -1;
        }
 
@@ -666,12 +673,13 @@ int _svc_db_get_list_with_all_defapps(GSList **pkg_list, uid_t uid)
 {
        const char query[] = "SELECT pkg_name FROM appsvc;";
        int r;
+       sqlite3 *svc_db = NULL;
 
-       if (__init(uid, true) < 0)
+       if (__init(uid, true, &svc_db) < 0)
                return -1;
 
        r = __get_list_with_query(svc_db, query, pkg_list);
-       __fini();
+       __fini(&svc_db);
 
        return r;
 }
@@ -750,24 +758,26 @@ char *_svc_db_query_builder_build(char *old_query)
 int _svc_db_exec_query(const char *query, GSList **pkg_list, uid_t uid)
 {
        int r;
+       sqlite3 *app_info_db = NULL;
+       sqlite3 *global_app_info_db = NULL;
 
-       if (__init_app_info_db(uid) < 0)
+       if (__init_app_info_db(uid, &app_info_db, &global_app_info_db) < 0)
                return 0;
 
        SECURE_LOGD("query : %s", query);
 
        r = __get_list_with_query(app_info_db, query, pkg_list);
        if (r < 0) {
-               __fini_app_info_db();
+               __fini_app_info_db(&app_info_db, &global_app_info_db);
                return -1;
        }
 
        r = __get_list_with_query(global_app_info_db, query, pkg_list);
        if (r < 0) {
-               __fini_app_info_db();
+               __fini_app_info_db(&app_info_db, &global_app_info_db);
                return -1;
        }
-       __fini_app_info_db();
+       __fini_app_info_db(&app_info_db, &global_app_info_db);
 
        return 0;
 }
@@ -777,6 +787,7 @@ int _svc_db_add_alias_appid(const char *alias_appid, const char *appid,
 {
        int ret;
        sqlite3_stmt *stmt = NULL;
+       sqlite3 *svc_db = NULL;
        const char *query =
                "INSERT OR REPLACE INTO alias_info(alias_appid, appid) " \
                "values(?,?);";
@@ -787,14 +798,14 @@ int _svc_db_add_alias_appid(const char *alias_appid, const char *appid,
                return -1;
        }
 
-       if (__init(uid, false) < 0)
+       if (__init(uid, false, &svc_db) < 0)
                return -1;
 
        ret = sqlite3_prepare_v2(svc_db, query, strlen(query), &stmt, NULL);
        if (ret != SQLITE_OK) {
                _E("sqlite3_prepare_v2() error: %d(%s)",
                                ret, sqlite3_errmsg(svc_db));
-               __fini();
+               __fini(&svc_db);
                return ret;
        }
 
@@ -824,7 +835,7 @@ int _svc_db_add_alias_appid(const char *alias_appid, const char *appid,
 
 end:
        sqlite3_finalize(stmt);
-       __fini();
+       __fini(&svc_db);
 
        return result;
 }
@@ -835,20 +846,21 @@ int _svc_db_delete_alias_appid(const char *alias_appid, uid_t uid)
        sqlite3_stmt *stmt = NULL;
        const char *query = "DELETE FROM alias_info WHERE alias_appid = ?;";
        int result = 0;
+       sqlite3 *svc_db = NULL;
 
        if (alias_appid == NULL) {
                _E("Invalid parameter");
                return -1;
        }
 
-       if (__init(uid, false) < 0)
+       if (__init(uid, false, &svc_db) < 0)
                return -1;
 
        ret = sqlite3_prepare_v2(svc_db, query, strlen(query), &stmt, NULL);
        if (ret != SQLITE_OK) {
                _E("sqlite3_prepare_v2() error: %d(%s)",
                                ret, sqlite3_errmsg(svc_db));
-               __fini();
+               __fini(&svc_db);
                return -1;
        }
 
@@ -869,7 +881,7 @@ int _svc_db_delete_alias_appid(const char *alias_appid, uid_t uid)
 
 end:
        sqlite3_finalize(stmt);
-       __fini();
+       __fini(&svc_db);
 
        return result;
 }
@@ -882,13 +894,14 @@ int __get_appid_from_alias_info(const char *alias_appid, char **appid,
        sqlite3_stmt *stmt = NULL;
        const char *query;
        const char *real_appid;
+       sqlite3 *svc_db = NULL;
 
        if (appid == NULL || alias_appid == NULL) {
                _E("Invalid parameter");
                return -1;
        }
 
-       if (__init(db_uid, true) < 0)
+       if (__init(db_uid, true, &svc_db) < 0)
                return -1;
 
        if (db_uid == GLOBAL_USER) {
@@ -906,7 +919,7 @@ int __get_appid_from_alias_info(const char *alias_appid, char **appid,
        if (ret != SQLITE_OK) {
                _E("sqlite3_prepare_v2() error: %d(%s)",
                                ret, sqlite3_errmsg(svc_db));
-               __fini();
+               __fini(&svc_db);
                return -1;
        }
 
@@ -950,7 +963,7 @@ int __get_appid_from_alias_info(const char *alias_appid, char **appid,
 
 end:
        sqlite3_finalize(stmt);
-       __fini();
+       __fini(&svc_db);
 
        return result;
 }
@@ -977,8 +990,9 @@ static int __get_alias_info_list(uid_t uid, uid_t db_uid, GHashTable *list)
        const char *alias_id;
        const char *id;
        struct alias_info_s *info;
+       sqlite3 *svc_db = NULL;
 
-       if (__init(db_uid, true) < 0)
+       if (__init(db_uid, true, &svc_db) < 0)
                return -1;
 
        if (db_uid == GLOBAL_USER) {
@@ -995,7 +1009,7 @@ static int __get_alias_info_list(uid_t uid, uid_t db_uid, GHashTable *list)
        if (ret != SQLITE_OK) {
                _E("sqlite3_prepare_v2() error - %s(%d)",
                                sqlite3_errmsg(svc_db), ret);
-               __fini();
+               __fini(&svc_db);
                return -1;
        }
 
@@ -1005,7 +1019,7 @@ static int __get_alias_info_list(uid_t uid, uid_t db_uid, GHashTable *list)
                        _E("sqlite3_bind_int() error - %s(%d)",
                                        sqlite3_errmsg(svc_db), ret);
                        sqlite3_finalize(stmt);
-                       __fini();
+                       __fini(&svc_db);
                        return -1;
                }
        }
@@ -1049,7 +1063,7 @@ static int __get_alias_info_list(uid_t uid, uid_t db_uid, GHashTable *list)
        }
 
        sqlite3_finalize(stmt);
-       __fini();
+       __fini(&svc_db);
 
        return 0;
 }
@@ -1120,13 +1134,14 @@ static int __enable_disable_alias_info(const char *appid, uid_t uid,
        sqlite3_stmt *stmt = NULL;
        const char *query;
        const char *value;
+       sqlite3 *svc_db = NULL;
 
        if (appid == NULL) {
                _E("Invalid parameter");
                return -1;
        }
 
-       if (__init(db_uid, false) < 0)
+       if (__init(db_uid, false, &svc_db) < 0)
                return -1;
 
        if (enable)
@@ -1145,7 +1160,7 @@ static int __enable_disable_alias_info(const char *appid, uid_t uid,
        if (ret != SQLITE_OK) {
                _E("sqlite3_prepare_v2() error - %s(%d)",
                                sqlite3_errmsg(svc_db), ret);
-               __fini();
+               __fini(&svc_db);
                return -1;
        }
 
@@ -1200,7 +1215,7 @@ static int __enable_disable_alias_info(const char *appid, uid_t uid,
 
 end:
        sqlite3_finalize(stmt);
-       __fini();
+       __fini(&svc_db);
 
        return result;
 }
@@ -1240,8 +1255,9 @@ static int __get_alias_info_list_by_appid(const char *appid, uid_t uid,
        const char *alias_id;
        const char *id;
        struct alias_info_s *info;
+       sqlite3 *svc_db = NULL;
 
-       if (__init(db_uid, true) < 0)
+       if (__init(db_uid, true, &svc_db) < 0)
                return -1;
 
        if (db_uid == GLOBAL_USER) {
@@ -1259,7 +1275,7 @@ static int __get_alias_info_list_by_appid(const char *appid, uid_t uid,
        if (ret != SQLITE_OK) {
                _E("sqlite3_prepare_v2() error - %s(%d)",
                                sqlite3_errmsg(svc_db), ret);
-               __fini();
+               __fini(&svc_db);
                return -1;
        }
 
@@ -1268,7 +1284,7 @@ static int __get_alias_info_list_by_appid(const char *appid, uid_t uid,
                _E("sqlite3_bind_text() error - %s(%d)",
                                sqlite3_errmsg(svc_db), ret);
                sqlite3_finalize(stmt);
-               __fini();
+               __fini(&svc_db);
                return -1;
        }
 
@@ -1278,7 +1294,7 @@ static int __get_alias_info_list_by_appid(const char *appid, uid_t uid,
                        _E("sqlite3_bind_int() error - %s(%d)",
                                        sqlite3_errmsg(svc_db), ret);
                        sqlite3_finalize(stmt);
-                       __fini();
+                       __fini(&svc_db);
                        return -1;
                }
        }
@@ -1322,7 +1338,7 @@ static int __get_alias_info_list_by_appid(const char *appid, uid_t uid,
        }
 
        sqlite3_finalize(stmt);
-       __fini();
+       __fini(&svc_db);
 
        return 0;
 }
@@ -1384,8 +1400,9 @@ static int __get_allowed_info_list(const char *appid, uid_t uid,
        struct allowed_info_s *info;
        GList *list;
        GList *iter;
+       sqlite3 *svc_db = NULL;
 
-       if (__init(db_uid, true) < 0)
+       if (__init(db_uid, true, &svc_db) < 0)
                return -1;
 
        if (appid) {
@@ -1399,7 +1416,7 @@ static int __get_allowed_info_list(const char *appid, uid_t uid,
        if (ret != SQLITE_OK) {
                _E("sqlite3_prepare_v2() error - %s(%d)",
                                sqlite3_errmsg(svc_db), ret);
-               __fini();
+               __fini(&svc_db);
                return -1;
        }
 
@@ -1409,7 +1426,7 @@ static int __get_allowed_info_list(const char *appid, uid_t uid,
                        _E("sqlite3_bind_text() error - %s(%d)",
                                        sqlite3_errmsg(svc_db), ret);
                        sqlite3_finalize(stmt);
-                       __fini();
+                       __fini(&svc_db);
                        return -1;
                }
        }
@@ -1468,7 +1485,7 @@ static int __get_allowed_info_list(const char *appid, uid_t uid,
        }
 
        sqlite3_finalize(stmt);
-       __fini();
+       __fini(&svc_db);
 
        return 0;
 }