a4c2a3ddda5265c2d994317f8a483629202b206f
[platform/core/connectivity/stc-manager.git] / src / database / db-common.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "stc-db.h"
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"
23
24 #ifndef DATABASE_FULL_PATH
25 #define DATABASE_FULL_PATH "/opt/usr/dbspace/.stc-manager-datausage.db"
26 #endif
27
28 #define SQLITE_BUSY_TIMEOUT 500000
29
30 static sqlite3 *database;
31
32 //LCOV_EXCL_START
33 static int __stc_db_busy(void *user, int attempts)
34 {
35         __STC_LOG_FUNC_ENTER__;
36         STC_LOGE("DB locked by another process, attempts number %d",
37                  attempts);
38
39         usleep(SQLITE_BUSY_TIMEOUT); /* wait for a half second*/
40         __STC_LOG_FUNC_EXIT__;
41         return 1;
42 }
43 //LCOV_EXCL_STOP
44
45 stc_error_e stc_db_initialize_once(void)
46 {
47         __STC_LOG_FUNC_ENTER__;
48         int res = 0;
49         int retry_count = 0;
50         if (database != NULL) {
51                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
52                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
53         }
54
55         do {
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
61                 } else {
62                         break;
63                 }
64                 usleep(MAX_USLEEP_TIMEOUT);
65         } while (retry_count <= MAX_DB_RETRY_COUNT);
66
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
72         }
73
74         STC_LOGD("Successfully opened database");
75
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
84         }
85
86         /* Set how many times we'll repeat our attempts for sqlite_step */
87         if (sqlite3_busy_handler(database, __stc_db_busy, NULL)
88             != SQLITE_OK)
89                 STC_LOGE("Couldn't set busy handler!"); //LCOV_EXCL_LINE
90
91         __STC_LOG_FUNC_EXIT__;
92         return STC_ERROR_NONE;
93 }
94
95 //LCOV_EXCL_START
96 sqlite3 *stc_db_get_database(void)
97 {
98         if (database == NULL)
99                 stc_db_initialize_once();
100
101         return database;
102 }
103 //LCOV_EXCL_STOP
104
105 stc_error_e stc_db_initialize(void)
106 {
107         __STC_LOG_FUNC_ENTER__;
108         database = NULL;
109
110         stc_db_initialize_once();
111
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());
117
118         __STC_LOG_FUNC_EXIT__;
119         return STC_ERROR_NONE;
120
121 handle_error:
122         stc_db_deinitialize(); //LCOV_EXCL_LINE
123         __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
124         return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
125 }
126
127 gboolean stc_db_deinitialize(void)
128 {
129         __STC_LOG_FUNC_ENTER__;
130         if (database == NULL) {
131                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
132                 return TRUE; //LCOV_EXCL_LINE
133         }
134
135         table_statistics_finalize();
136         table_restrictions_finalize();
137         table_counters_finalize();
138         table_firewall_finalize();
139         sqlite3_close(database);
140
141         __STC_LOG_FUNC_EXIT__;
142         return TRUE;
143 }