Add inotify for information config
[platform/core/connectivity/stc-manager.git] / src / monitor / stc-monitor.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 <linux/netlink.h>
18 #include <vconf.h>
19 #include <vconf-keys.h>
20
21 #include "stc-default-connection.h"
22 #include "helper-nl.h"
23 #include "helper-nfacct-rule.h"
24 #include "helper-net-cls.h"
25 #include "helper-cgroup.h"
26 #include "helper-iptables.h"
27 #include "counter.h"
28 #include "table-statistics.h"
29 #include "table-counters.h"
30 #include "stc-monitor.h"
31 #include "stc-manager-plugin-appstatus.h"
32 #include "stc-manager-plugin-exception.h"
33
34 #define MAX_INT_LENGTH 128
35 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
36
37 typedef struct {
38         stc_app_key_s *app_key;
39         stc_process_key_s *proc_key;
40         gboolean entry_removed;
41 } remove_pid_context_s;
42
43 typedef struct {
44         struct nfacct_rule *counter;
45         int64_t bytes;
46         gboolean data_limit_reached;
47 } classid_bytes_context_s;
48
49 static stc_system_s *g_system = NULL;
50
51 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
52 {
53         if (counter->intend == NFACCT_WARN)
54                 return NFACCT_JUMP_ACCEPT;
55         else if (counter->intend == NFACCT_BLOCK)
56                 return NFACCT_JUMP_REJECT;
57         else if (counter->intend == NFACCT_ALLOW)
58                 return NFACCT_JUMP_ACCEPT;
59
60         return NFACCT_JUMP_UNKNOWN;
61 }
62
63 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
64 {
65         if (counter == NULL)
66                 return STC_ERROR_INVALID_PARAMETER;
67
68         counter->action = NFACCT_ACTION_INSERT;
69         counter->iotype = NFACCT_COUNTER_IN;
70         counter->jump = __get_jump_by_intend(counter);
71         counter->iptype = NFACCT_TYPE_IPV4;
72         counter->send_limit = 0;
73         counter->rcv_limit = 0;
74
75         return produce_net_rule(counter);
76 }
77
78 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
79 {
80         if (counter == NULL)
81                 return STC_ERROR_INVALID_PARAMETER;
82
83         counter->action = NFACCT_ACTION_INSERT;
84         counter->iotype = NFACCT_COUNTER_OUT;
85         counter->jump = __get_jump_by_intend(counter);
86         counter->iptype = NFACCT_TYPE_IPV4;
87         counter->send_limit = 0;
88         counter->rcv_limit = 0;
89
90         return produce_net_rule(counter);
91 }
92
93 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
94 {
95         if (counter == NULL)
96                 return STC_ERROR_INVALID_PARAMETER;
97
98         counter->action = NFACCT_ACTION_DELETE;
99         counter->iotype = NFACCT_COUNTER_IN;
100         counter->jump = __get_jump_by_intend(counter);
101         counter->iptype = NFACCT_TYPE_IPV4;
102         counter->send_limit = 0;
103         counter->rcv_limit = 0;
104
105         return produce_net_rule(counter);
106 }
107
108 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
109 {
110         if (counter == NULL)
111                 return STC_ERROR_INVALID_PARAMETER;
112
113         counter->action = NFACCT_ACTION_DELETE;
114         counter->iotype = NFACCT_COUNTER_OUT;
115         counter->jump = __get_jump_by_intend(counter);
116         counter->iptype = NFACCT_TYPE_IPV4;
117         counter->send_limit = 0;
118         counter->rcv_limit = 0;
119
120         return produce_net_rule(counter);
121 }
122
123 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
124 {
125         if (counter == NULL)
126                 return STC_ERROR_INVALID_PARAMETER;
127
128         counter->action = NFACCT_ACTION_INSERT;
129         counter->iotype = NFACCT_COUNTER_IN;
130         counter->jump = __get_jump_by_intend(counter);
131         counter->iptype = NFACCT_TYPE_IPV6;
132         counter->send_limit = 0;
133         counter->rcv_limit = 0;
134
135         return produce_net_rule(counter);
136 }
137
138 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
139 {
140         if (counter == NULL)
141                 return STC_ERROR_INVALID_PARAMETER;
142
143         counter->action = NFACCT_ACTION_INSERT;
144         counter->iotype = NFACCT_COUNTER_OUT;
145         counter->jump = __get_jump_by_intend(counter);
146         counter->iptype = NFACCT_TYPE_IPV6;
147         counter->send_limit = 0;
148         counter->rcv_limit = 0;
149
150         return produce_net_rule(counter);
151 }
152
153 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
154 {
155         if (counter == NULL)
156                 return STC_ERROR_INVALID_PARAMETER;
157
158         counter->action = NFACCT_ACTION_DELETE;
159         counter->iotype = NFACCT_COUNTER_IN;
160         counter->jump = __get_jump_by_intend(counter);
161         counter->iptype = NFACCT_TYPE_IPV6;
162         counter->send_limit = 0;
163         counter->rcv_limit = 0;
164
165         return produce_net_rule(counter);
166 }
167
168 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
169 {
170         if (counter == NULL)
171                 return STC_ERROR_INVALID_PARAMETER;
172
173         counter->action = NFACCT_ACTION_DELETE;
174         counter->iotype = NFACCT_COUNTER_OUT;
175         counter->jump = __get_jump_by_intend(counter);
176         counter->iptype = NFACCT_TYPE_IPV6;
177         counter->send_limit = 0;
178         counter->rcv_limit = 0;
179
180         return produce_net_rule(counter);
181 }
182
183 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
184                                         gpointer UNUSED user_data)
185 {
186         stc_process_key_s *key_a = (stc_process_key_s *)a;
187         stc_process_key_s *key_b = (stc_process_key_s *)b;
188
189         return key_a->pid - key_b->pid;
190 }
191
192 static void __processes_tree_value_free(gpointer data)
193 {
194         stc_process_value_s *value = (stc_process_value_s *)data;
195
196         FREE(value);
197 }
198
199 static void __processes_tree_key_free(gpointer data)
200 {
201         stc_process_key_s *key = (stc_process_key_s *)data;
202
203         FREE(key);
204 }
205
206 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
207                                    gpointer UNUSED user_data)
208 {
209         stc_app_key_s *key_a = (stc_app_key_s *)a;
210         stc_app_key_s *key_b = (stc_app_key_s *)b;
211         gint ret;
212
213         ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
214         if (ret)
215                 return ret;
216
217         return g_strcmp0(key_a->app_id, key_b->app_id);
218 }
219
220 static void __apps_tree_value_free(gpointer data)
221 {
222         stc_app_value_s *value = (stc_app_value_s *)data;
223
224         g_tree_destroy(value->processes);
225         value->processes = NULL;
226
227         FREE(value);
228 }
229
230 static void __apps_tree_key_free(gpointer data)
231 {
232         stc_app_key_s *key = (stc_app_key_s *)data;
233
234         g_free(key->pkg_id);
235         g_free(key->app_id);
236         FREE(key);
237 }
238
239 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
240                                     gpointer UNUSED user_data)
241 {
242         stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
243         stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
244         int ret;
245
246         ret = g_strcmp0(key_a->app_id, key_b->app_id);
247         if (ret != 0)
248                 return ret;
249
250         ret = g_strcmp0(key_a->ifname, key_b->ifname);
251         if (ret != 0)
252                 return ret;
253
254         ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
255         if (ret != 0)
256                 return ret;
257
258         ret = key_a->iftype - key_b->iftype;
259         if (ret != 0)
260                 return ret;
261
262         ret = key_a->roaming - key_b->roaming;
263         if (ret != 0)
264                 return ret;
265
266         return 0;
267 }
268
269 static void __rstns_tree_value_free(gpointer data)
270 {
271         stc_rstn_value_s *value = (stc_rstn_value_s *)data;
272
273         FREE(value);
274 }
275
276 static void __rstns_tree_key_free(gpointer data)
277 {
278         stc_rstn_key_s *key = (stc_rstn_key_s *)data;
279
280         FREE(key->app_id);
281         FREE(key->ifname);
282         FREE(key->subscriber_id);
283         FREE(key);
284 }
285
286 /*
287 //LCOV_EXCL_START
288 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
289                                                gpointer data)
290 {
291         stc_process_key_s *proc_key = (stc_process_key_s *)key;
292         stc_process_value_s *proc_value = (stc_process_value_s *)value;
293
294         STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
295                  proc_key->pid, proc_value->ground);
296         return FALSE;
297 }
298
299 static void __processes_tree_printall(GTree *processes)
300 {
301         g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
302 }
303
304 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
305                                           gpointer data)
306 {
307         stc_app_key_s *app_key = (stc_app_key_s *)key;
308         stc_app_value_s *app_value = (stc_app_value_s *)value;
309
310         STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
311                  "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
312                  " counter [ in (%lld), out (%lld)]",
313                  app_key->pkg_id, app_key->app_id,
314                  app_value->type, app_value->classid,
315                  app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
316
317         __processes_tree_printall(app_value->processes);
318         return FALSE;
319 }
320
321 static void __apps_tree_printall(void)
322 {
323         g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
324 }
325 //LCOV_EXCL_STOP
326 */
327
328 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
329                                                gpointer data)
330 {
331         remove_pid_context_s *context = (remove_pid_context_s *)data;
332         stc_app_value_s *app_value = (stc_app_value_s *)value;
333
334         if (!g_tree_remove(app_value->processes, context->proc_key))
335                 return FALSE;
336
337         context->entry_removed = TRUE;
338         context->app_key = (stc_app_key_s *)key;
339
340         return TRUE;
341 }
342
343 static stc_app_value_s * __application_lookup(GTree *apps,
344                                               const stc_app_key_s *key)
345 {
346         stc_app_value_s *lookup;
347
348         ret_value_msg_if(apps == NULL, NULL, "apps is null!");
349
350         lookup = g_tree_lookup(apps, key);
351
352         return lookup;
353 }
354
355 static stc_process_value_s * __process_lookup(GTree *processes,
356                                               const stc_process_key_s *key)
357 {
358         stc_process_value_s *lookup;
359
360         ret_value_msg_if(processes == NULL, NULL, "processes is null!");
361
362         lookup = g_tree_lookup(processes, key);
363
364         return lookup;
365 }
366
367 //LCOV_EXCL_START
368 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
369                                              gpointer data)
370 {
371         guint *pid_count = (guint *)data;
372         (*pid_count)++;
373         return TRUE;
374 }
375 //LCOV_EXCL_STOP
376
377 static gboolean __add_application_monitor(gpointer key, gpointer value,
378                                           gpointer data)
379 {
380         stc_app_value_s *app_value = (stc_app_value_s *)value;
381         default_connection_s *connection = (default_connection_s *)data;
382         stc_s *stc = stc_get_manager();
383
384         if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
385             app_value->classid == STC_TOTAL_WIFI_CLASSID ||
386             app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
387                 return FALSE;
388
389         if (stc && connection && connection->ifname) {
390                 struct nfacct_rule counter;
391
392                 if (!stc->carg) {
393                         stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
394                         if (stc->carg == NULL) //LCOV_EXCL_LINE
395                                 return FALSE; //LCOV_EXCL_LINE
396
397                         stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
398                 }
399
400                 memset(&counter, 0, sizeof(struct nfacct_rule));
401
402                 counter.carg = stc->carg;
403                 counter.classid = app_value->classid;
404                 counter.intend = NFACCT_COUNTER;
405
406                 if (connection->tether_state == TRUE &&
407                         connection->tether_iface.ifname != NULL &&
408                         app_value->classid == STC_TETHERING_APP_CLASSID) {
409                         counter.iftype = connection->tether_iface.type;
410                         g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
411                 } else {
412                         counter.iftype = connection->type;
413                         g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
414                 }
415
416                 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
417                         __add_iptables_in(&counter);
418                         __add_iptables_out(&counter);
419                 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
420                         __add_ip6tables_in(&counter);
421                         __add_ip6tables_out(&counter);
422                 } else {
423                         __add_iptables_in(&counter);
424                         __add_iptables_out(&counter);
425                         __add_ip6tables_in(&counter);
426                         __add_ip6tables_out(&counter);
427                 }
428         }
429
430         return FALSE;
431 }
432
433 static gboolean __remove_application_monitor(gpointer key, gpointer value,
434                                              gpointer data)
435 {
436         stc_app_value_s *app_value = (stc_app_value_s *)value;
437         default_connection_s *connection = (default_connection_s *)data;
438         stc_s *stc = stc_get_manager();
439
440         if (stc && connection && connection->ifname) {
441                 struct nfacct_rule counter;
442
443                 if (!stc->carg) {
444                         stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
445                         if (stc->carg == NULL) //LCOV_EXCL_LINE
446                                 return FALSE; //LCOV_EXCL_LINE
447
448                         stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
449                 }
450
451                 memset(&counter, 0, sizeof(struct nfacct_rule));
452
453                 counter.carg = stc->carg;
454                 counter.classid = app_value->classid;
455                 counter.intend = NFACCT_COUNTER;
456
457                 if (connection->tether_state == FALSE &&
458                         connection->tether_iface.ifname != NULL &&
459                         app_value->classid == STC_TETHERING_APP_CLASSID) {
460                         counter.iftype = connection->tether_iface.type;
461                         g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
462                 } else {
463                         counter.iftype = connection->type;
464                         g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
465                 }
466
467                 __del_iptables_in(&counter);
468                 __del_iptables_out(&counter);
469                 __del_ip6tables_in(&counter);
470                 __del_ip6tables_out(&counter);
471         }
472
473         return FALSE;
474 }
475
476 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
477 {
478         STC_LOGI("rstn info => rstn_id [%llu], "
479                  "app_id [%s], classid [%u], ifname [%s], "
480                  "iftype [%d], rstn_state [%d], rstn_type [%d], "
481                  "limit [ (%lld) bytes], "
482                  "warn_limit [ (%lld) bytes], "
483                  "counter [ (%lld) bytes], "
484                  "roaming [%d], subscriber_id [%s]",
485                  rstn_value->restriction_id,
486                  rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
487                  rstn_key->iftype, rstn_value->rstn_state, rstn_value->rstn_type,
488                  rstn_value->data_limit,
489                  rstn_value->data_warn_limit,
490                  rstn_value->data_counter,
491                  rstn_key->roaming, rstn_key->subscriber_id);
492 }
493
494 static void __add_iptables_rule(int64_t classid, nfacct_rule_intend intend,
495                                 stc_iface_type_e iftype)
496 {
497         char *default_ifname = stc_default_connection_get_ifname();
498         default_connection_s *connection = stc_get_default_connection();
499         struct nfacct_rule counter;
500         stc_s *stc = stc_get_manager();
501         if (!stc) {
502                 g_free(default_ifname); //LCOV_EXCL_LINE
503                 return; //LCOV_EXCL_LINE
504         }
505
506         if (!stc->carg) {
507                 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
508                 if (stc->carg == NULL) { //LCOV_EXCL_LINE
509                         g_free(default_ifname); //LCOV_EXCL_LINE
510                         return; //LCOV_EXCL_LINE
511                 }
512
513                 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
514         }
515
516         counter.carg = stc->carg;
517         counter.classid = classid;
518         counter.intend = intend;
519
520         if (connection && connection->tether_iface.ifname != NULL &&
521                 classid == STC_TETHERING_APP_CLASSID) {
522                 counter.iftype = connection->tether_iface.type;
523                 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
524         } else {
525                 counter.iftype = iftype;
526                 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
527         }
528
529         g_free(default_ifname);
530
531         /* iptables rule */
532         __add_iptables_in(&counter);
533         __add_iptables_out(&counter);
534
535         /* ip6tables rule */
536         __add_ip6tables_in(&counter);
537         __add_ip6tables_out(&counter);
538 }
539
540 static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend,
541                                 stc_iface_type_e iftype)
542 {
543         char *default_ifname = stc_default_connection_get_ifname();
544         default_connection_s *connection = stc_get_default_connection();
545         struct nfacct_rule counter;
546         stc_s *stc = stc_get_manager();
547         if (!stc) {
548                 g_free(default_ifname); //LCOV_EXCL_LINE
549                 return; //LCOV_EXCL_LINE
550         }
551
552         if (!stc->carg) {
553                 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
554                 if (stc->carg == NULL) { //LCOV_EXCL_LINE
555                         g_free(default_ifname); //LCOV_EXCL_LINE
556                         return; //LCOV_EXCL_LINE
557                 }
558
559                 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
560         }
561
562         counter.carg = stc->carg;
563         counter.classid = classid;
564         counter.intend = intend;
565
566         if (connection && connection->tether_iface.ifname != NULL &&
567                 classid == STC_TETHERING_APP_CLASSID) {
568                 counter.iftype = connection->tether_iface.type;
569                 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
570         } else {
571                 counter.iftype = iftype;
572                 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
573         }
574
575         g_free(default_ifname);
576
577         /* iptables rule */
578         __del_iptables_in(&counter);
579         __del_iptables_out(&counter);
580
581         /* ip6tables rule */
582         __del_ip6tables_in(&counter);
583         __del_ip6tables_out(&counter);
584 }
585
586 static void __process_restriction(enum traffic_restriction_type rstn_type,
587                                   stc_rstn_key_s *rstn_key,
588                                   stc_rstn_value_s *rstn_value, void *data)
589 {
590         int64_t effective_data_limit, effective_data_warn_limit;
591         default_connection_s *old_connection = (default_connection_s *)data;
592         default_connection_s *connection = NULL;
593
594         if (old_connection != NULL)
595                 connection = old_connection;
596         else
597                 connection = stc_get_default_connection();
598
599         /* no default ifname */
600         if (connection->ifname == NULL)
601                 return;
602
603         /* rstn not applicable for this interface */
604         if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
605             (g_strcmp0(connection->ifname, rstn_key->ifname) != 0) &&
606                 (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
607                 return;
608
609         /* classid is invalid */
610         if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
611                 return;
612
613         effective_data_limit = rstn_value->data_limit;
614         effective_data_warn_limit = rstn_value->data_warn_limit;
615
616         if (rstn_type == RST_SET) {
617                 /* TODO: Change this to runtime memory */
618                 table_counters_info info;
619
620                 memset(&info, 0, sizeof(table_counters_info));
621                 table_counters_get(rstn_value->restriction_id, &info);
622
623                 effective_data_limit -= info.data_counter;
624                 effective_data_warn_limit -= info.data_counter;
625
626                 if (effective_data_limit < 0) {
627                         effective_data_limit = 0; //LCOV_EXCL_LINE
628                         rstn_value->data_limit_reached = TRUE; //LCOV_EXCL_LINE
629                 }
630
631                 if (effective_data_warn_limit < 0)
632                         effective_data_warn_limit = 0; //LCOV_EXCL_LINE
633
634                 STC_LOGD("datausage [%lld] bytes", info.data_counter);
635         }
636
637         STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
638                  "effective_data_warn_limit [%lld] bytes",
639                  rstn_value->restriction_id, effective_data_limit,
640                  effective_data_warn_limit);
641
642         switch (rstn_type) {
643         case RST_SET:
644                 if (effective_data_limit <= 0)
645                         __add_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
646                                             rstn_key->iftype);
647
648                 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
649                 rstn_value->data_limit_reached = FALSE;
650                 break;
651         case RST_EXCLUDE:
652                 __add_iptables_rule(rstn_value->classid, NFACCT_ALLOW,
653                                     rstn_key->iftype);
654
655                 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
656                 rstn_value->data_limit_reached = TRUE;
657                 break;
658         case RST_UNSET:
659                 if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
660                         __del_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
661                                             rstn_key->iftype);
662                 else
663                         __del_iptables_rule(rstn_value->classid, rstn_value->rstn_type,
664                                             rstn_key->iftype);
665
666                 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
667                 rstn_value->data_limit_reached = FALSE;
668                 break;
669         default:
670                 ;//Do Nothing
671         }
672 }
673
674 //LCOV_EXCL_START
675 static gboolean __remove_rstns_foreach_application(gpointer key,
676                                                    gpointer value,
677                                                    gpointer data)
678 {
679         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
680         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
681         gchar *app_id = (gchar *)data;
682
683         /* rstn rule is not for applications */
684         if (rstn_key->app_id == NULL)
685                 goto out;
686
687         /* rstn rule is not for this application */
688         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
689                 goto out;
690
691         /* rstn rule is already removed */
692         if (rstn_value->rstn_state == STC_RSTN_STATE_DEACTIVATED)
693                 goto out;
694
695         /* remove restriction from system */
696         __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
697
698         __print_rstn(rstn_key, rstn_value);
699 out:
700         return FALSE;
701 }
702 //LCOV_EXCL_STOP
703
704 static void __remove_rstns_for_application(gchar *app_id)
705 {
706         g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
707                        app_id);
708 }
709
710 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
711 {
712         stc_error_e ret = STC_ERROR_NONE;
713         guint pid_count = 0;
714         stc_app_value_s *lookup;
715
716         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
717
718         lookup = __application_lookup(g_system->apps, app_key);
719         if (!lookup) {
720                 STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
721                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
722         }
723
724         g_tree_foreach(lookup->processes, __processes_tree_check_empty,
725                        &pid_count);
726
727         if (!pid_count) {
728                 /* remove nfacct rule for this classid */
729                 __remove_application_monitor((gpointer) app_key, lookup,
730                                              stc_get_default_connection());
731                 __remove_rstns_for_application(app_key->app_id);
732         }
733
734         if (!g_tree_remove(g_system->apps, app_key)) {
735                 ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
736                 STC_LOGE("key not found"); //LCOV_EXCL_LINE
737         }
738
739         return ret;
740 }
741
742 static stc_error_e __close_contr_sock(stc_system_s *system)
743 {
744         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
745
746         /* close netlink socket for updating kernel counters */
747         if (system->contr_sock != -1) {
748                 close(system->contr_sock);
749                 system->contr_sock = -1;
750         }
751
752         if (system->contr_gsource_id != 0) {
753                 g_source_remove(system->contr_gsource_id);
754                 system->contr_gsource_id = 0;
755         }
756
757         return STC_ERROR_NONE;
758 }
759
760 static gboolean __process_contr_reply(GIOChannel *source,
761                                       GIOCondition condition,
762                                       gpointer user_data);
763
764 //LCOV_EXCL_START
765 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
766 {
767         GIOChannel *gio = NULL;
768         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
769
770         /* close netlink socket for updating kernel counters */
771         if (system->contr_sock != -1) {
772                 close(system->contr_sock);
773                 system->contr_sock = -1;
774         }
775
776         if (system->contr_gsource_id != 0) {
777                 g_source_remove(system->contr_gsource_id);
778                 system->contr_gsource_id = 0;
779         }
780
781         /* create netlink socket for updating kernel counters */
782         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
783         if (system->contr_sock < 0) {
784                 STC_LOGE("failed to open socket");
785                 FREE(system);
786                 return STC_ERROR_FAIL;
787         }
788
789         gio = g_io_channel_unix_new(system->contr_sock);
790         system->contr_gsource_id =
791                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
792                                (GIOFunc) __process_contr_reply,
793                                NULL);
794         g_io_channel_unref(gio);
795
796         return STC_ERROR_NONE;
797 }
798
799 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
800                                       stc_rstn_value_s *rstn_value,
801                                       classid_bytes_context_s *context)
802 {
803         switch (context->counter->iotype) {
804         case NFACCT_COUNTER_IN:
805         case NFACCT_COUNTER_OUT:
806                 rstn_value->data_counter += context->bytes;
807
808                 if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
809                     rstn_value->warn_limit_crossed_notified == FALSE) {
810
811                         gboolean rv;
812                         char iftype[MAX_INT_LENGTH];
813                         char byte[MAX_INT_LENGTH];
814                         stc_s *stc = (stc_s *)stc_get_manager();
815                         ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
816
817                         /* emit signal */
818                         rv = stc_manager_dbus_emit_signal(stc->connection,
819                                                           STC_DBUS_SERVICE_RESTRICTION_PATH,
820                                                           STC_DBUS_INTERFACE_RESTRICTION,
821                                                           "WarnThresholdCrossed",
822                                                           g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
823                         if (rv == TRUE)
824                                 rstn_value->warn_limit_crossed_notified = TRUE;
825
826                         snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
827                         snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
828                         stc_plugin_appstatus_send_warn_message("warn threshold crossed",
829                                                            "warning_noti",
830                                                            rstn_key->app_id,
831                                                            iftype, byte);
832                 }
833
834                 if (rstn_value->data_counter >= rstn_value->data_limit &&
835                     rstn_value->rstn_limit_crossed_notified == FALSE) {
836
837                         gboolean rv;
838                         char iftype[MAX_INT_LENGTH];
839                         char byte[MAX_INT_LENGTH];
840                         stc_s *stc = (stc_s *)stc_get_manager();
841                         ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
842
843                         /* block immediately */
844                         context->counter->intend = NFACCT_BLOCK;
845                         __del_iptables_in(context->counter);
846                         __del_iptables_out(context->counter);
847                         __add_iptables_in(context->counter);
848                         __add_iptables_out(context->counter);
849
850                         __del_ip6tables_in(context->counter);
851                         __del_ip6tables_out(context->counter);
852                         __add_ip6tables_in(context->counter);
853                         __add_ip6tables_out(context->counter);
854                         context->counter->intend = NFACCT_COUNTER;
855
856                         rstn_value->data_limit_reached = TRUE;
857
858                         /* emit signal */
859                         rv = stc_manager_dbus_emit_signal(stc->connection,
860                                                           STC_DBUS_SERVICE_RESTRICTION_PATH,
861                                                           STC_DBUS_INTERFACE_RESTRICTION,
862                                                           "RestrictionThresholdCrossed",
863                                                           g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
864                         if (rv == TRUE)
865                                 rstn_value->rstn_limit_crossed_notified = TRUE;
866
867                         snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
868                         snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
869                         stc_plugin_appstatus_send_restriction_message("restriction threshold crossed",
870                                                                   "restriction_noti", rstn_key->app_id,
871                                                                   iftype, byte);
872                 }
873
874                 g_system->rstns_tree_updated = TRUE;
875                 __print_rstn(rstn_key, rstn_value);
876                 break;
877         default:
878                 STC_LOGE("unknown iotype");
879         }
880
881         return FALSE;
882 }
883
884 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
885                                                 stc_rstn_value_s *rstn_value,
886                                                 classid_bytes_context_s *context)
887 {
888         if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
889                 context->counter->iftype == STC_IFACE_DATACALL) ||
890                 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
891                 context->counter->iftype == STC_IFACE_WIFI) ||
892                 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
893                 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
894                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
895                  context->counter->iftype == STC_IFACE_WIFI) ||
896                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
897                  context->counter->iftype == STC_IFACE_BLUETOOTH) ||
898                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
899                  context->counter->iftype == STC_IFACE_USB) ||
900                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
901                  context->counter->iftype == STC_IFACE_P2P)) {
902                 context->counter->classid = rstn_value->classid;
903                 return __rstn_counter_update(rstn_key, rstn_value, context);
904         }
905
906         return FALSE;
907 }
908
909 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
910                                                       gpointer value,
911                                                       gpointer data)
912 {
913         gboolean rv = FALSE;
914         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
915         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
916         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
917         uint32_t classid;
918
919         if (context->counter->intend != NFACCT_COUNTER)
920                 goto try_next_callback;
921
922         if (rstn_value->data_limit_reached == TRUE) {
923                 context->data_limit_reached = TRUE; //LCOV_EXCL_LINE
924                 goto try_next_callback; //LCOV_EXCL_LINE
925         }
926
927         classid = context->counter->classid;
928         rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
929
930         context->counter->classid = classid;
931         if (rstn_value->classid != context->counter->classid)
932                 goto try_next_callback;
933
934         rv = __rstn_counter_update(rstn_key, rstn_value, context);
935
936 try_next_callback:
937         return rv;
938 }
939 //LCOV_EXCL_STOP
940
941 static gboolean __update_app_statistics(gpointer key, gpointer value,
942                                         gpointer data)
943 {
944         stc_app_key_s *app_key = (stc_app_key_s *)key;
945         stc_app_value_s *app_value = (stc_app_value_s *)value;
946         time_t *touch_time = (time_t *)data;
947         stc_db_classid_iftype_key stat_key;
948         stc_db_app_stats stat;
949         default_connection_s *default_connection = stc_get_default_connection();
950
951         memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
952         memset(&stat, 0 , sizeof(stc_db_app_stats));
953
954         /* Do not update statistics for Tethering
955          * if tethering is in-active found */
956         if (default_connection &&
957                 default_connection->tether_state == FALSE &&
958                 !strcmp(app_key->app_id, STC_TOTAL_TETHERING))
959                 return FALSE;
960
961         /* Do not update statistics for Wi-Fi
962          * if tethering is active on wlan0 iface */
963         if (default_connection && default_connection->tether_state &&
964                 default_connection->tether_iface.type == STC_IFACE_WIFI &&
965                 !strcmp(app_key->app_id, STC_TOTAL_WIFI))
966                 return FALSE;
967
968         stat_key.classid = app_value->classid;
969
970         if (app_value->classid == STC_TETHERING_APP_CLASSID &&
971                 default_connection->tether_state == TRUE)
972                 stat_key.iftype = default_connection->tether_iface.type;
973         else
974                 stat_key.iftype = default_connection->type;
975
976         if (STC_IFACE_DATACALL == stat_key.iftype)
977                 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
978         else
979                 stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
980
981         if (app_value->classid == STC_TETHERING_APP_CLASSID &&
982                 default_connection->tether_state == TRUE)
983                 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
984                           MAX_IFACE_LENGTH);
985         else
986                 g_strlcpy(stat_key.ifname, default_connection->ifname,
987                           MAX_IFACE_LENGTH);
988
989         stat.app_id = g_strdup(app_key->app_id);
990         stat.snd_count = app_value->counter.out_bytes;
991         stat.rcv_count = app_value->counter.in_bytes;
992         stat.is_roaming = default_connection->roaming;
993
994         if (strstr(stat.app_id, "_BACKGROUND")) {
995                 stat.ground = STC_APP_STATE_BACKGROUND;
996         } else {
997                 if (strstr(stat.app_id, "TOTAL_"))
998                         stat.ground = STC_APP_STATE_UNKNOWN;
999                 else
1000                         stat.ground = STC_APP_STATE_FOREGROUND;
1001         }
1002
1003         table_statistics_insert(&stat_key, &stat, *touch_time);
1004
1005         app_value->counter.out_bytes = 0;
1006         app_value->counter.in_bytes = 0;
1007
1008         FREE(stat.app_id);
1009         FREE(stat_key.subscriber_id);
1010
1011         return FALSE;
1012 }
1013
1014 static gboolean __flush_apps_stats_to_database(gpointer user_data)
1015 {
1016         time_t current_time = time(0);
1017
1018         if (g_system->apps_tree_updated == FALSE)
1019                 return G_SOURCE_REMOVE;
1020
1021         g_system->apps_tree_updated = FALSE;
1022
1023         if (g_system->apps)
1024                 g_tree_foreach(g_system->apps,
1025                                __update_app_statistics,
1026                                &current_time);
1027
1028         STC_LOGI("Flushed app stats to database");
1029         return G_SOURCE_REMOVE;
1030 }
1031
1032 //LCOV_EXCL_START
1033 static gboolean __update_counter_statistics(gpointer key, gpointer value,
1034                                             gpointer data)
1035 {
1036         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1037         table_counters_info info = {
1038                 .restriction_id = rstn_value->restriction_id,
1039                 .data_counter = rstn_value->data_counter
1040         };
1041
1042         table_counters_update_counters(&info);
1043
1044         return FALSE;
1045 }
1046
1047 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
1048 {
1049         time_t current_time = time(0);
1050
1051         if (g_system->rstns_tree_updated == FALSE)
1052                 return G_SOURCE_REMOVE;
1053
1054         g_system->rstns_tree_updated = FALSE;
1055
1056         if (g_system->rstns)
1057                 g_tree_foreach(g_system->rstns,
1058                                __update_counter_statistics,
1059                                &current_time);
1060
1061         STC_LOGI("Flushed rstns counters to database");
1062         return G_SOURCE_REMOVE;
1063 }
1064 //LCOV_EXCL_STOP
1065
1066 static void __app_counter_update(stc_app_key_s *app_key,
1067                                  stc_app_value_s *app_value,
1068                                  classid_bytes_context_s *context)
1069 {
1070         switch (context->counter->iotype) {
1071         case NFACCT_COUNTER_IN:
1072                 app_value->data_usage.in_bytes += context->bytes;
1073                 app_value->counter.in_bytes = context->bytes;
1074                 g_system->apps_tree_updated = TRUE;
1075
1076                 /*
1077                 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1078                 */
1079                 break;
1080         case NFACCT_COUNTER_OUT:
1081                 app_value->data_usage.out_bytes += context->bytes;
1082                 app_value->counter.out_bytes = context->bytes;
1083                 g_system->apps_tree_updated = TRUE;
1084
1085                 /*
1086                 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1087                 */
1088                 break;
1089         default:
1090                 STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
1091         }
1092 }
1093
1094 static void __interface_counter_update(stc_app_key_s *app_key,
1095                                        stc_app_value_s *app_value,
1096                                        classid_bytes_context_s *context)
1097 {
1098         if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1099              context->counter->iftype == STC_IFACE_DATACALL) ||
1100             (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1101              context->counter->iftype == STC_IFACE_WIFI) ||
1102             (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1103              context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1104                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1105                  context->counter->iftype == STC_IFACE_WIFI) ||
1106                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1107                  context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1108                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1109                  context->counter->iftype == STC_IFACE_USB) ||
1110                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1111                  context->counter->iftype == STC_IFACE_P2P))
1112                 __app_counter_update(app_key, app_value, context);
1113 }
1114
1115
1116 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1117                                                       gpointer value,
1118                                                       gpointer data)
1119 {
1120         stc_app_key_s *app_key = (stc_app_key_s *)key;
1121         stc_app_value_s *app_value = (stc_app_value_s *)value;
1122         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1123
1124         if (context->counter->intend != NFACCT_COUNTER)
1125                 goto try_next_callback;
1126
1127         __interface_counter_update(app_key, app_value, context);
1128
1129         if (app_value->classid != context->counter->classid)
1130                 goto try_next_callback;
1131
1132         __app_counter_update(app_key, app_value, context);
1133
1134 try_next_callback:
1135         return FALSE;
1136 }
1137
1138 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1139                                  struct counter_arg *carg)
1140 {
1141         struct nfacct_rule counter = {
1142                 .carg = carg,
1143                 .name = {0},
1144                 .ifname = {0},
1145                 0
1146         };
1147
1148         classid_bytes_context_s context = {
1149                 .counter = &counter,
1150                 .bytes = bytes,
1151                 .data_limit_reached = FALSE,
1152         };
1153
1154         if (!recreate_counter_by_name(cnt_name, &counter)) {
1155                 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1156                 return; //LCOV_EXCL_LINE
1157         }
1158
1159         if (STC_DEBUG_LOG)
1160                 STC_LOGI("classid %u, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1161                         context.counter->classid, context.counter->iftype,
1162                         context.counter->iotype, context.counter->intend,
1163                         context.counter->ifname, context.bytes);
1164
1165         if (g_system->rstns)
1166                 g_tree_foreach(g_system->rstns,
1167                                __rstn_counter_update_foreach_classid,
1168                                &context);
1169
1170         if (g_system->apps)
1171                 g_tree_foreach(g_system->apps,
1172                                __apps_counter_update_foreach_classid,
1173                                &context);
1174 }
1175
1176 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1177                            void *user_data)
1178 {
1179         struct counter_arg *carg = user_data;
1180         char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1181         if (carg->initiate) {
1182                 /**
1183                  * TODO: this will be used when daemon starts to update existing
1184                  * counter data if present.
1185                  *
1186                  populate_counters(cnt_name, carg);
1187                  */
1188         } else {
1189                 int64_t *bytes_p =
1190                         (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1191                 int bytes = be64toh(*bytes_p);
1192                 if (bytes) {
1193                         ++carg->serialized_counters;
1194                         __fill_nfacct_result(cnt_name, bytes, carg);
1195                 }
1196         }
1197
1198         return 0;
1199 }
1200
1201 static int __post_fill_counters(void *user_data)
1202 {
1203         struct counter_arg *carg = user_data;
1204
1205         if (carg->initiate)
1206                 carg->initiate = 0;
1207
1208         return 0;
1209 }
1210
1211 static void __process_network_counter(struct genl *ans,
1212                                       struct counter_arg *carg)
1213 {
1214         struct netlink_serialization_params ser_params = {
1215                 .carg = carg,
1216                 .ans = ans,
1217                 .eval_attr = __fill_counters,
1218                 .post_eval_attr = __post_fill_counters,
1219         };
1220
1221         netlink_serialization_command *netlink =
1222                 netlink_create_command(&ser_params);
1223         if (!netlink) {
1224                 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
1225                 return; //LCOV_EXCL_LINE
1226         }
1227
1228         netlink->deserialize_answer(&(netlink->params));
1229 }
1230
1231 static gboolean __process_contr_reply(GIOChannel *source,
1232                                       GIOCondition condition,
1233                                       gpointer user_data)
1234 {
1235         int sock = g_io_channel_unix_get_fd(source);
1236         struct genl *ans;
1237         int ret;
1238         stc_s *stc = stc_get_manager();
1239
1240 #ifdef TIZEN_GTESTS
1241         void __gcov_flush(void);
1242         __gcov_flush();
1243 #endif
1244
1245         if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1246             (condition & G_IO_NVAL)) {
1247                 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1248
1249                 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
1250                          "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
1251                          (condition & G_IO_ERR), (condition & G_IO_HUP),
1252                          (condition & G_IO_NVAL));
1253                 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
1254                 return FALSE; //LCOV_EXCL_LINE
1255         }
1256
1257         ans = MALLOC0(struct genl, 1);
1258         if (ans == NULL) {
1259                 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
1260                 return TRUE; //LCOV_EXCL_LINE
1261         }
1262
1263         if (stc == NULL) {
1264                 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
1265                 goto out; //LCOV_EXCL_LINE
1266         }
1267
1268         ret = read_netlink(sock, ans, sizeof(struct genl));
1269         /* STC_LOGD("Counter data received ret [%d]", ret); */
1270         if (ret == 0)
1271                 goto out;
1272
1273         stc->carg->ans_len = ret;
1274         __process_network_counter(ans, stc->carg);
1275
1276         g_idle_add(__flush_apps_stats_to_database, NULL);
1277         g_idle_add(__flush_rstns_counter_to_database, NULL);
1278 out:
1279
1280         FREE(ans);
1281         return TRUE;
1282 }
1283
1284 static gboolean __update_contr_cb(void *user_data)
1285 {
1286         /* Here we just sent command, answer we receive in another callback */
1287         stc_s *stc = stc_get_manager();
1288         ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1289         if (!stc->carg) {
1290                 stc->carg = MALLOC0(counter_arg_s, 1);
1291                 if (stc->carg == NULL)
1292                         return TRUE;  /* we need to continue the timer */
1293
1294                 stc->carg->sock = stc_monitor_get_counter_socket();
1295         }
1296
1297 #ifdef TIZEN_GTESTS
1298         void __gcov_flush(void);
1299         __gcov_flush();
1300 #endif
1301
1302         /* STC_LOGD("Get all counters"); */
1303         nfacct_send_get_all(stc->carg);
1304
1305         /* we need to continue the timer */
1306         return TRUE;
1307 }
1308
1309 /*
1310 //LCOV_EXCL_START
1311 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1312                                           gpointer data)
1313 {
1314         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1315         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1316
1317         __print_rstn(rstn_key, rstn_value);
1318         return FALSE;
1319 }
1320
1321 static void __rstn_tree_printall(void)
1322 {
1323         g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1324 }
1325 //LCOV_EXCL_STOP
1326 */
1327
1328 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1329                                         const stc_rstn_key_s *key)
1330 {
1331         stc_rstn_value_s *lookup;
1332
1333         ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1334
1335         lookup = g_tree_lookup(rstns_tree, key);
1336
1337         return lookup;
1338 }
1339
1340 static gboolean __remove_restriction(gpointer key, gpointer value,
1341                                      gpointer data)
1342 {
1343         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1344         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1345
1346         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1347         __print_rstn(rstn_key, rstn_value);
1348         return FALSE;
1349 }
1350
1351 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1352                                         gpointer data)
1353 {
1354         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1355         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1356
1357         /* rstn rule is activated */
1358         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1359                 return FALSE;
1360
1361         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1362                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1363         else
1364                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1365
1366         __print_rstn(rstn_key, rstn_value);
1367
1368         return FALSE;
1369 }
1370
1371 //LCOV_EXCL_START
1372 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1373 {
1374         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1375         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1376
1377         /* rstn rule is activated */
1378         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1379                 return FALSE;
1380
1381         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1382                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1383         else
1384                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1385
1386         return FALSE;
1387 }
1388 //LCOV_EXCL_STOP
1389
1390 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1391 {
1392         stc_rstn_value_s *lookup_value;
1393
1394         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1395
1396         lookup_value = __rstn_lookup(g_system->rstns, key);
1397         if (!lookup_value) {
1398                 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1399                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1400         }
1401
1402         __remove_restriction(key, lookup_value, NULL);
1403
1404         /* remove counter also */
1405         table_counters_delete(lookup_value->restriction_id);
1406
1407         if (!g_tree_remove(g_system->rstns, key)) {
1408                 STC_LOGD("key not found"); //LCOV_EXCL_LINE
1409                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1410         }
1411
1412         return STC_ERROR_NONE;
1413 }
1414
1415 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1416                                    stc_rstn_value_s *value, gboolean debug)
1417 {
1418         stc_rstn_key_s *rstn_key;
1419         stc_rstn_value_s *rstn_value;
1420
1421         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1422
1423         rstn_value = __rstn_lookup(g_system->rstns, key);
1424         if (rstn_value)
1425                 __rstn_tree_remove(key);
1426
1427         rstn_key = MALLOC0(stc_rstn_key_s, 1);
1428         if (!rstn_key) {
1429                 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
1430                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1431         }
1432
1433         rstn_value = MALLOC0(stc_rstn_value_s, 1);
1434         if (!rstn_value) {
1435                 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
1436                 FREE(rstn_key); //LCOV_EXCL_LINE
1437                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1438         }
1439
1440         rstn_key->app_id = g_strdup(key->app_id);
1441         rstn_key->ifname = g_strdup(key->ifname);
1442         rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1443         rstn_key->iftype = key->iftype;
1444         rstn_key->roaming = key->roaming;
1445
1446         g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1447
1448         rstn_value->restriction_id = value->restriction_id;
1449         rstn_value->rstn_state = value->rstn_state;
1450         rstn_value->rstn_type = value->rstn_type;
1451         rstn_value->classid = value->classid;
1452         rstn_value->data_limit = value->data_limit;
1453         rstn_value->data_warn_limit = value->data_warn_limit;
1454         rstn_value->data_counter = 0;
1455         rstn_value->warn_limit_crossed_notified = FALSE;
1456         rstn_value->rstn_limit_crossed_notified = FALSE;
1457
1458         if (debug == TRUE)
1459                 __add_restriction_debug(key, rstn_value, NULL);
1460         else
1461                 __add_restriction(key, rstn_value, NULL);
1462
1463         return STC_ERROR_NONE;
1464 }
1465
1466 //LCOV_EXCL_START
1467 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1468                                             void *user_data)
1469 {
1470         stc_cb_ret_e ret = STC_CONTINUE;
1471
1472         stc_rstn_key_s key;
1473         stc_rstn_value_s value;
1474
1475         memset(&key, 0, sizeof(stc_rstn_key_s));
1476         memset(&value, 0, sizeof(stc_rstn_value_s));
1477
1478         key.app_id = g_strdup(info->app_id);
1479         key.ifname = g_strdup(info->ifname);
1480         key.subscriber_id = g_strdup(info->subscriber_id);
1481         key.iftype = info->iftype;
1482         key.roaming = info->roaming;
1483
1484         value.rstn_type = info->rstn_type;
1485         value.rstn_state = STC_RSTN_STATE_UNKNOWN;
1486         value.restriction_id = info->restriction_id;
1487
1488         if (info->app_id)
1489                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1490         else
1491                 value.classid = STC_UNKNOWN_CLASSID;
1492
1493         value.data_limit = info->data_limit;
1494         value.data_warn_limit = info->data_warn_limit;
1495
1496         if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1497                 ret = STC_CANCEL;
1498
1499         FREE(key.app_id);
1500         FREE(key.ifname);
1501         FREE(key.subscriber_id);
1502         return ret;
1503 }
1504
1505 static void __fill_restritions_list(void)
1506 {
1507         table_restrictions_foreach(__insert_restriction_cb, NULL);
1508
1509         /* __rstn_tree_printall(); */
1510 }
1511
1512 static gboolean __add_rstn_foreach_application(gpointer key,
1513                                                gpointer value,
1514                                                gpointer data)
1515 {
1516         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1517         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1518         gchar *app_id = (gchar *)data;
1519
1520         /* rstn rule is not for applications */
1521         if (rstn_key->app_id == NULL)
1522                 goto out;
1523
1524         /* rstn rule is not for this application */
1525         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1526                 goto out;
1527
1528         /* rstn rule is already applied */
1529         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1530                 goto out;
1531
1532         /* add restriction to system */
1533         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1534                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
1535         else
1536                 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
1537
1538         __print_rstn(rstn_key, rstn_value);
1539 out:
1540         return FALSE;
1541 }
1542 //LCOV_EXCL_STOP
1543
1544 static void __add_rstns_for_application(gchar *app_id)
1545 {
1546         g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1547                        app_id);
1548 }
1549
1550 static void __add_application_by_interface(const char *app_id)
1551 {
1552         stc_app_key_s app_key;
1553         stc_app_value_s app_value;
1554
1555         if (app_id == NULL)
1556                 return; //LCOV_EXCL_LINE
1557
1558         memset(&app_key, 0, sizeof(stc_app_key_s));
1559         memset(&app_value, 0, sizeof(stc_app_value_s));
1560
1561         app_key.pkg_id = g_strdup(app_id);
1562         app_key.app_id = g_strdup(app_id);
1563
1564         app_value.type = STC_APP_TYPE_NONE;
1565         app_value.processes = NULL;
1566         app_value.counter.in_bytes = 0;
1567         app_value.counter.out_bytes = 0;
1568
1569         stc_monitor_application_add(app_key, app_value);
1570
1571         FREE(app_key.pkg_id);
1572         FREE(app_key.app_id);
1573 }
1574
1575 static int __vconf_get_int(const char *key, int *value)
1576 {
1577         int ret = 0;
1578
1579         ret = vconf_get_int(key, value);
1580         if (ret != VCONF_OK) {
1581                 STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
1582                 return -1; //LCOV_EXCL_LINE
1583         }
1584
1585         return 0;
1586 }
1587
1588 //LCOV_EXCL_START
1589 static int __vconf_set_int(const char *key, int value)
1590 {
1591         int ret = 0;
1592
1593         ret = vconf_set_int(key, value);
1594         if (ret != VCONF_OK) {
1595                 STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
1596                 return -1; //LCOV_EXCL_LINE
1597         }
1598
1599         return 0;
1600 }
1601
1602 static guint __get_background_state(void)
1603 {
1604         return g_system->background_state;;
1605 }
1606
1607 static void __set_background_state(guint state)
1608 {
1609         g_system->background_state = state;
1610 }
1611
1612 static gboolean __processes_tree_foreach_background(gpointer key,
1613                                                     gpointer value,
1614                                                     gpointer data)
1615 {
1616         stc_process_key_s *proc_key = (stc_process_key_s *)key;
1617         stc_app_key_s *app_key = (stc_app_key_s *)data;
1618
1619         if (g_system->background_state)
1620                 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1621         else
1622                 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1623
1624         return FALSE;
1625 }
1626
1627 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1628                                         gpointer data)
1629 {
1630         stc_app_key_s *app_key = (stc_app_key_s *)key;
1631         stc_app_value_s *app_value = (stc_app_value_s *)value;
1632
1633         if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1634                 g_tree_foreach(app_value->processes,
1635                                __processes_tree_foreach_background, app_key);
1636
1637         return FALSE;
1638 }
1639
1640 static stc_error_e __process_update_background(void)
1641 {
1642         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1643
1644         g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1645
1646         return STC_ERROR_NONE;
1647 }
1648 //LCOV_EXCL_STOP
1649
1650 static void __fill_exceptions_list(void)
1651 {
1652         stc_plugin_fill_exception_list();
1653 }
1654
1655 stc_error_e stc_monitor_init(void)
1656 {
1657         stc_system_s *system = MALLOC0(stc_system_s, 1);
1658         GIOChannel *gio = NULL;
1659
1660         ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1661
1662         /* initializing current classid */
1663         init_current_classid();
1664
1665         /* initializing cgroups */
1666         cgroup_init();
1667
1668         /* creating monitored application tree */
1669         system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1670                                        __apps_tree_key_free,
1671                                        __apps_tree_value_free);
1672
1673         system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1674                                         __rstns_tree_key_free,
1675                                         __rstns_tree_value_free);
1676
1677         /* create netlink socket for updating kernel counters */
1678         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1679         if (system->contr_sock < 0) {
1680                 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
1681                 FREE(system); //LCOV_EXCL_LINE
1682                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1683         }
1684
1685         gio = g_io_channel_unix_new(system->contr_sock);
1686         system->contr_gsource_id =
1687                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1688                                (GIOFunc) __process_contr_reply,
1689                                NULL);
1690         g_io_channel_unref(gio);
1691
1692         g_system = system;
1693
1694         __add_application_by_interface(STC_TOTAL_DATACALL);
1695         __add_application_by_interface(STC_TOTAL_WIFI);
1696         __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1697         __add_application_by_interface(STC_TOTAL_IPV4);
1698         __add_application_by_interface(STC_TOTAL_IPV6);
1699         __add_application_by_interface(STC_TOTAL_TETHERING);
1700
1701         /* creating restriction rules tree */
1702         __update_contr_cb(NULL);
1703
1704         /* registering periodic kernel counters update callback */
1705         g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1706                                                          __update_contr_cb,
1707                                                          NULL);
1708         if (g_system->contr_timer_id == 0) {
1709                 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
1710                 __close_contr_sock(g_system); //LCOV_EXCL_LINE
1711                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1712         }
1713
1714         __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1715                         (int *)&g_system->background_state);
1716
1717         __fill_exceptions_list();
1718         __fill_restritions_list();
1719
1720         return STC_ERROR_NONE;
1721 }
1722
1723 stc_error_e stc_monitor_deinit(void)
1724 {
1725         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1726
1727         /* close netlink socket for updating kernel counters */
1728         __close_contr_sock(g_system);
1729
1730         /* remove kernel counters update timer */
1731         if (g_system->contr_timer_id > 0) {
1732                 g_source_remove(g_system->contr_timer_id);
1733                 g_system->contr_timer_id = 0;
1734         }
1735
1736         /* destroy monitored application tree */
1737         g_tree_destroy(g_system->apps);
1738         g_system->apps = NULL;
1739
1740         /* destroy restriction rules tree */
1741         g_tree_destroy(g_system->rstns);
1742         g_system->rstns = NULL;
1743
1744         FREE(g_system);
1745
1746         return STC_ERROR_NONE;
1747 }
1748
1749 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1750                                         const stc_app_value_s app_value)
1751 {
1752         stc_error_e ret = STC_ERROR_NONE;
1753         stc_app_key_s *key;
1754         stc_app_value_s *value;
1755         stc_app_value_s *lookup;
1756
1757         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1758
1759         lookup = __application_lookup(g_system->apps, &app_key);
1760         if (lookup)
1761                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1762
1763         key = MALLOC0(stc_app_key_s, 1);
1764         if (!key) {
1765                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1766                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1767         }
1768
1769         value = MALLOC0(stc_app_value_s, 1);
1770         if (!value) {
1771                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1772                 FREE(key); //LCOV_EXCL_LINE
1773                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1774         }
1775
1776         key->app_id = g_strdup(app_key.app_id);
1777         key->pkg_id = g_strdup(app_key.pkg_id);
1778
1779         value->type = app_value.type;
1780         value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1781         value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1782
1783         value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1784                                            __processes_tree_key_free,
1785                                            __processes_tree_value_free);
1786
1787         /* create cgroup and update classid */
1788         value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1789
1790         g_tree_insert(g_system->apps, key, value);
1791
1792         /* add nfacct rule for this classid */
1793         __add_application_monitor(key, value, stc_get_default_connection());
1794         __add_rstns_for_application(app_key.app_id);
1795
1796         return ret;
1797 }
1798
1799 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1800                                     const stc_process_key_s proc_key,
1801                                     const stc_process_value_s proc_value)
1802 {
1803         stc_error_e ret = STC_ERROR_NONE;
1804         stc_app_value_s *app_lookup;
1805         stc_process_key_s *key;
1806         stc_process_value_s *value;
1807         stc_process_value_s *proc_lookup;
1808
1809         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1810
1811         app_lookup = __application_lookup(g_system->apps, &app_key);
1812         if (!app_lookup) {
1813                 if (STC_DEBUG_LOG)
1814                         STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
1815                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1816         }
1817
1818         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1819         if (proc_lookup)
1820                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1821
1822         key = MALLOC0(stc_process_key_s, 1);
1823         if (!key) {
1824                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1825                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1826         }
1827
1828         value = MALLOC0(stc_process_value_s, 1);
1829         if (!value) {
1830                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1831                 FREE(key); //LCOV_EXCL_LINE
1832                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1833         }
1834
1835         key->pid = proc_key.pid;
1836
1837         value->ground = proc_value.ground;
1838
1839         g_tree_insert(app_lookup->processes, key, value);
1840
1841         /* add pid to application cgroup */
1842         place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1843
1844         /*
1845         __apps_tree_printall(); //LCOV_EXCL_LINE
1846         */
1847
1848         return ret;
1849 }
1850
1851 API stc_error_e stc_monitor_process_remove(pid_t pid)
1852 {
1853         stc_error_e ret = STC_ERROR_NONE;
1854         stc_process_key_s proc_key = {
1855                 .pid = pid
1856         };
1857
1858         remove_pid_context_s context = {
1859                 .app_key = NULL,
1860                 .proc_key = &proc_key,
1861                 .entry_removed = FALSE,
1862         };
1863
1864         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1865
1866         g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1867                        &context);
1868
1869         if (context.entry_removed)
1870                 __application_remove_if_empty(context.app_key);
1871
1872         /*
1873         __apps_tree_printall(); //LCOV_EXCL_LINE
1874         */
1875
1876         return ret;
1877 }
1878
1879 //LCOV_EXCL_START
1880 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1881                                               const stc_process_key_s proc_key,
1882                                               stc_app_state_e ground)
1883 {
1884         stc_error_e ret = STC_ERROR_NONE;
1885         stc_app_value_s *app_lookup;
1886         stc_process_value_s *proc_lookup;
1887
1888         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1889
1890         app_lookup = __application_lookup(g_system->apps, &app_key);
1891         if (!app_lookup) {
1892                 STC_LOGD("app_key not found");
1893                 return STC_ERROR_FAIL;
1894         }
1895
1896         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1897         if (!proc_lookup) {
1898                 STC_LOGD("proc_key not found");
1899                 return STC_ERROR_FAIL;
1900         }
1901
1902         if (proc_lookup->ground != ground)
1903                 proc_lookup->ground = ground;
1904
1905         if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1906                 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1907         else
1908                 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1909
1910         return ret;
1911 }
1912 //LCOV_EXCL_STOP
1913
1914 void stc_monitor_update_rstn_by_default_connection(void *data)
1915 {
1916         static default_connection_s old_connection;
1917         default_connection_s *new_connection = (default_connection_s *)data;
1918
1919         if (old_connection.path != NULL) {
1920                 //LCOV_EXCL_START
1921                 if (g_system->apps)
1922                         g_tree_foreach(g_system->apps,
1923                                        __remove_application_monitor,
1924                                        (gpointer)&old_connection);
1925
1926                 if (g_system->rstns)
1927                         g_tree_foreach(g_system->rstns,
1928                                        __remove_restriction,
1929                                        (gpointer)&old_connection);
1930
1931                 iptables_flush_chains();
1932                 //LCOV_EXCL_STOP
1933         }
1934
1935         FREE(old_connection.path);
1936         FREE(old_connection.ifname);
1937         FREE(old_connection.tether_iface.ifname);
1938         old_connection.type = 0;
1939         old_connection.roaming = 0;
1940         old_connection.tether_state = FALSE;
1941         old_connection.tether_iface.type = 0;
1942
1943         if (new_connection != NULL && new_connection->path != NULL) {
1944                 if (g_system->apps)
1945                         g_tree_foreach(g_system->apps,
1946                                        __add_application_monitor,
1947                                        (gpointer)new_connection);
1948
1949                 if (g_system->rstns)
1950                         g_tree_foreach(g_system->rstns, __add_restriction,
1951                                        NULL);
1952
1953                 old_connection.path = g_strdup(new_connection->path);
1954                 old_connection.ifname = g_strdup(new_connection->ifname);
1955                 old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
1956                 old_connection.type = new_connection->type;
1957                 old_connection.roaming = new_connection->roaming;
1958                 old_connection.tether_state = new_connection->tether_state;
1959                 old_connection.tether_iface.type = new_connection->tether_iface.type;
1960         }
1961 }
1962
1963 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1964 {
1965         stc_error_e ret;
1966
1967         stc_rstn_key_s key;
1968         stc_rstn_value_s value;
1969
1970         memset(&key, 0, sizeof(stc_rstn_key_s));
1971         memset(&value, 0, sizeof(stc_rstn_value_s));
1972
1973         key.app_id = g_strdup(info->app_id);
1974         key.ifname = g_strdup(info->ifname);
1975         key.subscriber_id = g_strdup(info->subscriber_id);
1976         key.iftype = info->iftype;
1977         key.roaming = info->roaming;
1978
1979         value.rstn_type = info->rstn_type;
1980         value.rstn_state = STC_RSTN_STATE_UNKNOWN;
1981         value.restriction_id = info->restriction_id;
1982
1983         if (info->app_id)
1984                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1985         else
1986                 value.classid = STC_UNKNOWN_CLASSID;
1987
1988         if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1989                 __set_background_state(TRUE); //LCOV_EXCL_LINE
1990                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1991                 __process_update_background(); //LCOV_EXCL_LINE
1992         }
1993
1994         value.data_limit = info->data_limit;
1995         value.data_warn_limit = info->data_warn_limit;
1996
1997         ret = __rstn_tree_add(&key, &value, TRUE);
1998
1999         FREE(key.app_id);
2000         FREE(key.ifname);
2001         FREE(key.subscriber_id);
2002         return ret;
2003 }
2004
2005 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
2006 {
2007         stc_error_e ret;
2008
2009         stc_rstn_key_s key = {
2010                 .app_id = g_strdup(info->app_id),
2011                 .ifname = g_strdup(info->ifname),
2012                 .subscriber_id = g_strdup(info->subscriber_id),
2013                 .iftype = info->iftype,
2014                 .roaming = info->roaming,
2015         };
2016
2017         if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
2018                 __set_background_state(FALSE); //LCOV_EXCL_LINE
2019                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2020                 __process_update_background(); //LCOV_EXCL_LINE
2021         }
2022
2023         ret = __rstn_tree_remove(&key);
2024
2025         FREE(key.app_id);
2026         FREE(key.ifname);
2027         FREE(key.subscriber_id);
2028         return ret;
2029 }
2030
2031 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
2032 {
2033         return stc_plugin_check_exception_by_cmdline(cmdline);
2034 }
2035
2036 int stc_monitor_get_counter_socket(void)
2037 {
2038         return g_system->contr_sock;
2039 }