Separate monitoring function plugin
[platform/core/connectivity/stc-manager.git] / include / stc-manager-util.h
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 #ifndef __STC_MANAGER_UTIL_H__
18 #define __STC_MANAGER_UTIL_H__
19
20 #include <dirent.h>
21 #include <glib.h>
22 #include <gio/gio.h>
23 #include <stdio.h>
24 #include <stdbool.h>  /* bool */
25 #include <stdint.h> /* uint32_t, uint63_t */
26 #include <string.h>
27 #include <sqlite3.h>  /* database handling */
28 #include <time.h>  /* time function */
29 #include <vconf/vconf.h>
30 #include <unistd.h>
31
32 #ifdef USE_DLOG
33 #include <dlog.h>
34
35 #undef LOG_TAG
36 #define LOG_TAG "STC_MANAGER"
37
38 #define STC_LOGD(format, args...) LOGD(format, ##args)
39 #define STC_LOGI(format, args...) LOGI(format, ##args)
40 #define STC_LOGW(format, args...) LOGW(format, ##args)
41 #define STC_LOGE(format, args...) LOGE(format, ##args)
42
43 #define __STC_LOG_FUNC_ENTER__ LOGD("Enter")
44 #define __STC_LOG_FUNC_EXIT__ LOGD("Quit")
45
46 #else /* USE_DLOG */
47
48 #define STC_LOGD(format, args...)
49 #define STC_LOGI(format, args...)
50 #define STC_LOGW(format, args...)
51 #define STC_LOGE(format, args...)
52
53 #define __STC_LOG_FUNC_ENTER__
54 #define __STC_LOG_FUNC_EXIT__
55
56 #endif /* USE_DLOG */
57
58 #define LOG_RED      "\033[0;31m"
59 #define LOG_GREEN    "\033[0;32m"
60 #define LOG_BROWN    "\033[0;33m"
61 #define LOG_BLUE     "\033[0;34m"
62 #define LOG_MAGENTA      "\033[0;35m"
63 #define LOG_CYAN     "\033[0;36m"
64 #define LOG_YELLOW   "\033[1;33m"
65 #define LOG_LIGHTRED      "\033[1;31m"
66 #define LOG_LIGHTGREEN    "\033[1;32m"
67 #define LOG_LIGHTBLUE     "\033[1;34m"
68 #define LOG_LIGHTMAGENTA  "\033[1;35m"
69 #define LOG_LIGHTCYAN     "\033[1;36m"
70 #define LOG_END      "\033[0;m"
71
72 #define APP_ID_LEN_MAX 1024
73
74 #define GPOINTER_TO_PID(x) (pid_t)GPOINTER_TO_INT((x))
75 #define PID_TO_GPOINTER(x) GINT_TO_POINTER((pid_t)(x))
76
77 #define MALLOC0(t, n) ((t*) g_try_malloc0((n) * sizeof(t)))
78 #define FREE(p) do { \
79         if (p) { \
80                 g_free(p); \
81                 p = NULL; \
82         } \
83 } while (0)
84
85 #define EXEC(error_code, command) do { \
86         if (error_code != command) { \
87                 __STC_LOG_FUNC_EXIT__; \
88                 goto handle_error; \
89         } \
90 } while (0)
91
92 #define DEBUG_GDBUS_VARIANT(str, parameters) do { \
93         gchar *params_str = NULL; \
94         if (parameters) { \
95                 params_str = g_variant_print(parameters, \
96                                              TRUE); \
97         } \
98         STC_LOGD("%s[%s]", str, \
99                  params_str ? params_str : "NULL"); \
100         g_free(params_str); \
101 } while (0)
102
103 #define DEBUG_GDBUS_KEY_VALUE(key, value) do { \
104         if (key) { \
105                 STC_LOGD("Key : [%s]", key); \
106         } \
107         if (value) { \
108                 DEBUG_GDBUS_VARIANT("Value: ", value); \
109         } \
110 } while (0)
111
112 #define _pure_ __attribute__ ((pure))
113 #define _cleanup_(x) __attribute__((cleanup(x)))
114
115 static inline void freep(void *p)
116 {
117         FREE(*(void**) p);
118 }
119
120 static inline void closep(int *fd)
121 {
122         if (*fd >= 0)
123                 close(*fd);
124 }
125
126 static inline void fclosep(FILE **f)
127 {
128         if (*f)
129                 fclose(*f);
130 }
131
132 static inline void pclosep(FILE **f)
133 {
134         if (*f)
135                 pclose(*f);
136 }
137
138 static inline void closedirp(DIR **d)
139 {
140         if (*d)
141                 closedir(*d);
142 }
143
144 #define _cleanup_free_ _cleanup_(freep)
145 #define _cleanup_close_ _cleanup_(closep)
146 #define _cleanup_fclose_ _cleanup_(fclosep)
147 #define _cleanup_pclose_ _cleanup_(pclosep)
148 #define _cleanup_closedir_ _cleanup_(closedirp)
149
150 #define NUM_DIFF(x, y) ((x > y) ? (x - y) : (y -x))
151
152 #define BYTE_TO_KBYTE(b) ((b) >> 10)
153 #define BYTE_TO_MBYTE(b) ((b) >> 20)
154 #define BYTE_TO_GBYTE(b) ((b) >> 30)
155 #define BYTE_TO_PAGE(b) ((b) >> 12)
156
157 #define KBYTE_TO_BYTE(k) ((k) << 10)
158 #define KBYTE_TO_MBYTE(k) ((k) >> 10)
159 #define KBYTE_TO_GBYTE(k) ((k) >> 20)
160
161 #define MBYTE_TO_BYTE(m) ((m) << 20)
162 #define MBYTE_TO_KBYTE(m) ((m) << 10)
163 #define MBYTE_TO_GBYTE(m) ((m) >> 10)
164
165 #define GBYTE_TO_BYTE(g) ((g) << 30)
166 #define GBYTE_TO_KBYTE(g) ((g) << 20)
167 #define GBYTE_TO_MBYTE(g) ((g) << 10)
168
169 #define streq(a, b) (strncmp((a), (b), strlen(b)+1) == 0)
170 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
171 #define strcaseeq(a, b) (strcasecmp((a), (b)) == 0)
172 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
173
174 static inline bool is_empty(const char *p)
175 {
176         return !p || !p[0];
177 }
178
179 static inline bool strstart_with(const char *str, const char *with)
180 {
181         return strncmp(str, with, strlen(with)) == 0;
182 }
183
184 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
185         for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); \
186              (word); \
187              (word) = split((s), &(length), (separator), &(state)))
188
189 #define FOREACH_WORD(word, length, s, state) \
190         FOREACH_WORD_SEPARATOR(word, length, s, WHITESPACE, state)
191
192 #define FOREACH_DIRENT(de, d, result, on_error) \
193         for (errno  = readdir_r(d, &de, &result);; errno = readdir_r(d, &de, &result)) \
194                 if (errno || !result) { \
195                         if (errno) \
196                                 on_error; \
197                         break; \
198                 } else if (streq(de.d_name, ".") || \
199                            streq(de.d_name, "..")) \
200                         continue; \
201                 else
202
203 #define FOREACH_STRV(s, l) \
204         for ((s) = (l); (s) && *(s); (s)++)
205
206 #define execute_once                            \
207         static int __func__##guardian;          \
208         for (;                                  \
209              __func__##guardian == 0;           \
210              __func__##guardian = 1)
211
212 #ifndef API
213 #define API __attribute__((visibility("default")))
214 #endif
215
216 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
217
218 #define DECLARE_WRAPPER(fn_name, inner_fn)                              \
219         static void fn_name(void *data, void __attribute__((__unused__)) *not_used) \
220         {                                                               \
221                 return inner_fn(data);                                  \
222         }
223
224 #define UNUSED __attribute__((__unused__))
225
226 #define MAX_SIZE2(a, b) sizeof(a) + sizeof(b) - 1
227 #define MAX_SIZE3(a, b, c) MAX_SIZE2(a, b) + sizeof(c) - 1
228
229 /*
230  * One byte digit has 3 position in decimal representation
231  * 2 - 5
232  * 4 - 10
233  * 8 - 20
234  * >8 - compile time error
235  * plus 1 null termination byte
236  * plus 1 for negative prefix
237  */
238 #define MAX_DEC_SIZE(type)                              \
239         (2 + (sizeof(type) <= 1 ? 3 :                   \
240               sizeof(type) <= 2 ? 5 :                   \
241               sizeof(type) <= 4 ? 10 :                  \
242               sizeof(type) <= 8 ? 20 :                  \
243               sizeof(int[-2*(sizeof(type) > 8)])))
244
245 #define SET_BIT(a, bit)                         \
246         ((a) |= (bit))
247
248 #define UNSET_BIT(a, bit)                       \
249         ((a) &= ~(bit))
250
251 #define CHECK_BIT(a, bit)                       \
252         ((a) & (bit))
253
254 #define ret_msg_if(expr, fmt, arg...) do {      \
255         if (expr) {                             \
256                 STC_LOGE(fmt, ##arg);           \
257                 return;                         \
258         }                                       \
259 } while (0)
260
261 #define ret_value_if(expr, val) do {                                    \
262         if (expr) {                                                     \
263                 return (val);                                           \
264         }                                                               \
265 } while (0)
266
267 #define ret_value_msg_if(expr, val, fmt, arg...) do {   \
268         if (expr) {                                     \
269                 if (STC_DEBUG_LOG)                              \
270                         STC_LOGE(fmt, ##arg);                   \
271                 return val;                             \
272         }                                               \
273 } while (0)
274
275 #define ret_value_errno_msg_if(expr, val, fmt, arg...) do { \
276         if (expr) {                                         \
277                 if (STC_DEBUG_LOG)                          \
278                         STC_LOGE(fmt, ##arg);                       \
279                 return val;                                 \
280         }                                                   \
281 } while (0)
282
283
284 /*
285  * @brief Copy from source to destination
286  * destination should not be on heap.
287  * Destination will be null terminated
288  */
289 #define STRING_SAVE_COPY(destination, source) do {                      \
290         if (destination && source) {                                    \
291                 size_t null_pos = strlen(source);                       \
292                 strncpy(destination, source, sizeof(destination));      \
293                 null_pos = sizeof(destination) - 1 < null_pos ?         \
294                         sizeof(destination) - 1 : null_pos;             \
295                 destination[null_pos] = '\0';                           \
296         }                                                               \
297 } while (0)
298
299 /* FIXME: Do we really need pointers? */
300 #define array_foreach(key, type, array)                                 \
301                 guint _array_foreach_index;                             \
302                 type *key;                                              \
303                 for (_array_foreach_index = 0;                          \
304                      array && _array_foreach_index < array->len &&      \
305                              (key = &g_array_index(array, type, _array_foreach_index)); \
306                      ++_array_foreach_index)
307
308 #define slist_foreach(key, type, list)                                  \
309         type *key;                                                      \
310         GSList *_slist_foreach_copy_list = list;                        \
311         for (;                                                          \
312              _slist_foreach_copy_list &&                                \
313                      ((key = _slist_foreach_copy_list->data) || 1);     \
314              _slist_foreach_copy_list = _slist_foreach_copy_list->next)
315
316 #define gslist_for_each_item(item, list)                                \
317         for (item = list; item != NULL; item = g_slist_next(item))
318
319 #define gslist_for_each(head, elem, node)                               \
320         for (elem = head, node = NULL; elem && ((node = elem->data) != NULL); elem = elem->next, node = NULL)
321
322 #define gslist_for_each_safe(head, elem, elem_next, node)               \
323         for (elem = head, elem_next = g_slist_next(elem), node = NULL;  \
324              elem && ((node = elem->data) != NULL);                     \
325              elem = elem_next, elem_next = g_slist_next(elem), node = NULL)
326
327 #define TASK_FILE_NAME "tasks"
328 #define CGROUP_FILE_NAME "cgroup.procs"
329 #define UNKNOWN_APP "(unknown)"
330
331 #define INFO_STORAGE_DIR "/var/lib/stc"
332 #define INFO_CONFIG "info.config"
333 #define INFO_DEBUG_LOG "debug_log"
334 #define INFO_STAT_LOG "stat_log"
335 #define INFO_RSTN_LOG "rstn_log"
336 #define INFO_FW_LOG "fw_log"
337 #define INFO_PCAP_LOG "pcap_log"
338
339 #define MAX_PATH_LENGTH 512
340 #define MAX_NAME_LENGTH 256
341 #define MAX_IFACE_LENGTH 32
342 #define MAX_APPID_LENGTH 128
343 #define MAX_PKGNAME_LENGTH 128
344
345 #define PROC_BUF_MAX 64
346 #define PROC_NAME_MAX 1024
347 #define PROC_STATUS_CNT 7
348
349 #define PROC_STATUS_NAME 0
350 #define PROC_STATUS_STATE 1
351 #define PROC_STATUS_TGID 2
352 #define PROC_STATUS_NGID 3
353 #define PROC_STATUS_PID 4
354 #define PROC_STATUS_PPID 5
355 #define PROC_STATUS_TRACERPID 6
356
357 #define PROC_STATUS_NAME_STR "Name:"
358 #define PROC_STATUS_STATE_STR "State:"
359 #define PROC_STATUS_TGID_STR "Tgid:"
360 #define PROC_STATUS_NGID_STR "Ngid:"
361 #define PROC_STATUS_PID_STR "Pid:"
362 #define PROC_STATUS_PPID_STR "PPid:"
363 #define PROC_STATUS_TRACERPID_STR "TracerPid:"
364
365 #define COMMA_DELIMETER ","
366
367 #define COUNTER_UPDATE_PERIOD 60
368 #define COUNTER_FLUSH_PERIOD 60
369
370 #define NONE_QUOTA_ID 0
371
372 #define TIME_TO_SAFE_DATA 1 /* one second */
373
374 /*
375  * @desc reserved classid enums
376  * internal structure, we don't provide it externally
377  */
378 enum stc_reserved_classid {
379         STC_UNKNOWN_CLASSID,
380         STC_ALL_APP_CLASSID,            /**< kernel expects 1 for handling restriction for all applications */
381         STC_TETHERING_APP_CLASSID,      /**< it uses in user space logic for counting tethering traffic */
382         STC_FOREGROUND_APP_CLASSID,     /**< it will used for special cgroup, blocked cgroup */
383         STC_BACKGROUND_APP_CLASSID,     /**< background data monitoring */
384         STC_TOTAL_DATACALL_CLASSID,     /**< Cellular data monitoring */
385         STC_TOTAL_WIFI_CLASSID,         /**< Wi-Fi data monitoring */
386         STC_TOTAL_BLUETOOTH_CLASSID,
387         STC_TOTAL_IPV4_CLASSID,         /**< IPV4 data monitoring */
388         STC_TOTAL_IPV6_CLASSID,         /**< IPV6 data monitoring */
389         STC_NETWORK_RESTRICTION_APP_CLASSID,
390         STC_RESERVED_CLASSID_MAX,
391 };
392
393 enum stc_counter_state {
394         STC_DEFAULT_STATE = 0,
395         STC_FORCIBLY_FLUSH_STATE = 1 << 1,
396         STC_FORCIBLY_QUIT_STATE = 1 << 2,
397         STC_NET_BLOCKED_STATE = 1 << 3,
398         STC_CHECK_QUOTA = 1 << 4,
399         STC_UPDATE_REQUESTED = 1 << 5,
400 };
401
402 typedef enum {
403         DEBUG_LOG_INFO,
404         STAT_LOG_INFO,
405         RSTN_LOG_INFO,
406         FW_LOG_INFO,
407         PCAP_LOG_INFO,
408         MAX_LOG_INFO
409 } log_info_e;
410
411 gboolean stc_util_get_config_bool(char *key);
412 gchar * stc_util_get_config_str(char *key);
413 int stc_util_get_config_int(char *key);
414
415 void stc_util_update_log_state(void);
416 void stc_util_set_log_state(log_info_e info, int state);
417 int stc_util_get_log_state(log_info_e info);
418
419 void stc_util_initialize_config(void);
420
421 #endif /* __STC_MANAGER_UTIL_H__ */