2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "db-internal.h"
19 #include "table-statistics.h"
20 #include "table-restrictions.h"
21 #include "table-counters.h"
22 #include "table-firewall.h"
24 #ifndef DATABASE_FULL_PATH
25 #define DATABASE_FULL_PATH "/opt/usr/dbspace/.stc-manager-datausage.db"
28 #define SQLITE_BUSY_TIMEOUT 500000
30 static sqlite3 *database;
33 static int __stc_db_busy(void *user, int attempts)
35 __STC_LOG_FUNC_ENTER__;
36 STC_LOGE("DB locked by another process, attempts number %d",
39 usleep(SQLITE_BUSY_TIMEOUT); /* wait for a half second*/
40 __STC_LOG_FUNC_EXIT__;
45 stc_error_e stc_db_initialize_once(void)
47 __STC_LOG_FUNC_ENTER__;
50 if (database != NULL) {
51 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
52 return STC_ERROR_NONE; //LCOV_EXCL_LINE
56 res = sqlite3_open(DATABASE_FULL_PATH, &database);
57 if (res != SQLITE_OK) {
58 STC_LOGD("Retry[%d] opening database %s: %s\n", //LCOV_EXCL_LINE
59 ++retry_count, DATABASE_FULL_PATH,
60 sqlite3_errmsg(database)); //LCOV_EXCL_LINE
64 usleep(MAX_USLEEP_TIMEOUT);
65 } while (retry_count <= MAX_DB_RETRY_COUNT);
67 if (res != SQLITE_OK) {
68 STC_LOGE("Can't open database %s: %s\n", DATABASE_FULL_PATH, //LCOV_EXCL_LINE
69 sqlite3_errmsg(database));
70 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
71 return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
74 STC_LOGD("Successfully opened database");
76 res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
77 if (res != SQLITE_OK) {
78 STC_LOGE("Can't set locking mode %s, skip set busy handler.", //LCOV_EXCL_LINE
79 sqlite3_errmsg(database));
80 sqlite3_close(database); //LCOV_EXCL_LINE
81 database = NULL; //LCOV_EXCL_LINE
82 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
83 return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
86 /* Set how many times we'll repeat our attempts for sqlite_step */
87 if (sqlite3_busy_handler(database, __stc_db_busy, NULL)
89 STC_LOGE("Couldn't set busy handler!"); //LCOV_EXCL_LINE
91 __STC_LOG_FUNC_EXIT__;
92 return STC_ERROR_NONE;
96 sqlite3 *stc_db_get_database(void)
99 stc_db_initialize_once();
105 stc_error_e stc_db_initialize(void)
107 __STC_LOG_FUNC_ENTER__;
110 stc_db_initialize_once();
112 EXEC(STC_ERROR_NONE, table_statistics_prepare(database));
113 EXEC(STC_ERROR_NONE, table_restrictions_prepare(database));
114 EXEC(STC_ERROR_NONE, table_counters_prepare(database));
115 EXEC(STC_ERROR_NONE, table_firewall_prepare(database));
116 EXEC(STC_ERROR_NONE, stc_init_db_guard());
118 __STC_LOG_FUNC_EXIT__;
119 return STC_ERROR_NONE;
122 stc_db_deinitialize(); //LCOV_EXCL_LINE
123 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
124 return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
127 gboolean stc_db_deinitialize(void)
129 __STC_LOG_FUNC_ENTER__;
130 if (database == NULL) {
131 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
132 return TRUE; //LCOV_EXCL_LINE
135 table_statistics_finalize();
136 table_restrictions_finalize();
137 table_counters_finalize();
138 table_firewall_finalize();
139 sqlite3_close(database);
141 __STC_LOG_FUNC_EXIT__;