[STC Manager] Added logic to add and remove iptable rule of restriction.
[platform/core/connectivity/stc-manager.git] / src / database / db-guard.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 <errno.h>
18 #include <sys/stat.h>
19
20 #include "stc-db.h"
21 #include "table-statistics.h"
22
23 #define VCONF_KEY_DB_ENTRIES_COUNT "db/stc-manager/datausage_timer"
24 #define ENTRY_SIZE 128
25
26 /* one hour */
27 #define ERASE_TIMER_INTERVAL 3600
28 /* 40 days */
29 #define ERASE_INTERVAL 3600 * 24 * 40
30 /* 50 Mb */
31 #define DB_SIZE_THRESHOLD 1048576 * 50
32
33 static guint erase_timer = 0;
34 static int db_entries = 0;
35
36 static void __change_db_entries_num_num(int num)
37 {
38         __STC_LOG_FUNC_ENTER__;
39
40         db_entries += num;
41         if (vconf_set_int(VCONF_KEY_DB_ENTRIES_COUNT, db_entries))
42                 STC_LOGE("Failed to set new db entries number");
43
44         __STC_LOG_FUNC_EXIT__;
45 }
46
47 static void __check_erase_db_oversize(void)
48 {
49         __STC_LOG_FUNC_ENTER__;
50
51         struct stat db_stat = {0};
52         int del_entry = 0;
53
54         if (stat(DATABASE_FULL_PATH, &db_stat)) {
55                 STC_LOGE("Failed to get statistics for %s errno %d",
56                          DATABASE_FULL_PATH, errno);
57                 __STC_LOG_FUNC_EXIT__;
58                 return;
59         }
60
61         if (db_stat.st_size < DB_SIZE_THRESHOLD) {
62                 STC_LOGD("Db truncation isn't required!");
63                 __STC_LOG_FUNC_EXIT__;
64                 return;
65         }
66
67         /* get approximate number of entries for removing */
68         del_entry = (db_stat.st_size - DB_SIZE_THRESHOLD) / ENTRY_SIZE;
69         if (STC_ERROR_NONE !=
70             table_statistics_reset_first_n_entries(del_entry)) {
71                 STC_LOGE("Failed to remove first %d entries", del_entry);
72                 __STC_LOG_FUNC_EXIT__;
73                 return;
74         }
75
76         __change_db_entries_num_num(-del_entry);
77
78         __STC_LOG_FUNC_EXIT__;
79 }
80
81 static void __erase_old_entries(void)
82 {
83         __STC_LOG_FUNC_ENTER__;
84         char buffer[80] = {0, };
85         table_statistics_reset_rule rule = {
86                 .iftype = STC_IFACE_LAST_ELEM,
87         };
88         stc_db_tm_interval_s interval;
89         time_t until = time(0);
90         struct tm result = {0, };
91
92         until -= ERASE_INTERVAL;
93
94         interval.from = 0;
95         interval.to = until;
96         rule.interval = &interval;
97
98         strftime(buffer, 80, "%x - %I:%M%p", localtime_r(&until, &result));
99         STC_LOGD("Reset statistics till %s", buffer);
100
101         if (table_statistics_reset(&rule) != STC_ERROR_NONE)
102                 STC_LOGE("Failed to reset statistics");
103
104         __STC_LOG_FUNC_EXIT__;
105 }
106
107 static gboolean __erase_func_cb(void *user_data)
108 {
109         __STC_LOG_FUNC_ENTER__;
110
111         __check_erase_db_oversize();
112         __erase_old_entries();
113
114         /* we need to continue the timer */
115         __STC_LOG_FUNC_EXIT__;
116         return TRUE;
117 }
118
119 stc_error_e stc_init_db_guard(void)
120 {
121         __STC_LOG_FUNC_ENTER__;
122
123         erase_timer = g_timeout_add_seconds(ERASE_TIMER_INTERVAL,
124                                             __erase_func_cb, NULL);
125         if (erase_timer == 0) {
126                 STC_LOGE("Failed to create timer");
127                 __STC_LOG_FUNC_EXIT__;
128                 return STC_ERROR_FAIL;
129         }
130
131         __STC_LOG_FUNC_EXIT__;
132         return STC_ERROR_NONE;
133 }
134
135 void stc_deinit_db_guard(void)
136 {
137         __STC_LOG_FUNC_ENTER__;
138
139         if (erase_timer > 0) {
140                 g_source_remove(erase_timer);
141                 erase_timer = 0;
142         }
143
144         __STC_LOG_FUNC_EXIT__;
145 }