adb79a25b1553de618edb7aa662fe565c9c987b6
[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 //LCOV_EXCL_START
287 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
288                                                gpointer data)
289 {
290         stc_process_key_s *proc_key = (stc_process_key_s *)key;
291         stc_process_value_s *proc_value = (stc_process_value_s *)value;
292
293         STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
294                  proc_key->pid, proc_value->ground);
295         return FALSE;
296 }
297
298 static void __processes_tree_printall(GTree *processes)
299 {
300         g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
301 }
302
303 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
304                                           gpointer data)
305 {
306         stc_app_key_s *app_key = (stc_app_key_s *)key;
307         stc_app_value_s *app_value = (stc_app_value_s *)value;
308
309         STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
310                  "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
311                  " counter [ in (%lld), out (%lld)]",
312                  app_key->pkg_id, app_key->app_id,
313                  app_value->type, app_value->classid,
314                  app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
315
316         __processes_tree_printall(app_value->processes);
317         return FALSE;
318 }
319
320 static void __apps_tree_printall(void)
321 {
322         g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
323 }
324 //LCOV_EXCL_STOP
325
326 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
327                                                gpointer data)
328 {
329         remove_pid_context_s *context = (remove_pid_context_s *)data;
330         stc_app_value_s *app_value = (stc_app_value_s *)value;
331
332         if (!g_tree_remove(app_value->processes, context->proc_key)) {
333                 if (STC_DEBUG_LOG)
334                         STC_LOGD("key not found"); //LCOV_EXCL_LINE
335                 return FALSE;
336         }
337
338         context->entry_removed = TRUE;
339         context->app_key = (stc_app_key_s *)key;
340
341         return TRUE;
342 }
343
344 static stc_app_value_s * __application_lookup(GTree *apps,
345                                               const stc_app_key_s *key)
346 {
347         stc_app_value_s *lookup;
348
349         ret_value_msg_if(apps == NULL, NULL, "apps is null!");
350
351         lookup = g_tree_lookup(apps, key);
352
353         return lookup;
354 }
355
356 static stc_process_value_s * __process_lookup(GTree *processes,
357                                               const stc_process_key_s *key)
358 {
359         stc_process_value_s *lookup;
360
361         ret_value_msg_if(processes == NULL, NULL, "processes is null!");
362
363         lookup = g_tree_lookup(processes, key);
364
365         return lookup;
366 }
367
368 //LCOV_EXCL_START
369 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
370                                              gpointer data)
371 {
372         guint *pid_count = (guint *)data;
373         (*pid_count)++;
374         return TRUE;
375 }
376 //LCOV_EXCL_STOP
377
378 static gboolean __add_application_monitor(gpointer key, gpointer value,
379                                           gpointer data)
380 {
381         stc_app_value_s *app_value = (stc_app_value_s *)value;
382         default_connection_s *connection = (default_connection_s *)data;
383         stc_s *stc = stc_get_manager();
384
385         if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
386             app_value->classid == STC_TOTAL_WIFI_CLASSID ||
387             app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
388                 return FALSE;
389
390         if (stc && connection && connection->ifname) {
391                 struct nfacct_rule counter;
392
393                 if (!stc->carg) {
394                         stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
395                         if (stc->carg == NULL) //LCOV_EXCL_LINE
396                                 return FALSE; //LCOV_EXCL_LINE
397
398                         stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
399                 }
400
401                 memset(&counter, 0, sizeof(struct nfacct_rule));
402
403                 counter.carg = stc->carg;
404                 counter.classid = app_value->classid;
405                 counter.intend = NFACCT_COUNTER;
406
407                 if (connection->tether_state == TRUE &&
408                         connection->tether_iface.ifname != NULL &&
409                         app_value->classid == STC_TETHERING_APP_CLASSID) {
410                         counter.iftype = connection->tether_iface.type;
411                         g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
412                 } else {
413                         counter.iftype = connection->type;
414                         g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
415                 }
416
417                 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
418                         __add_iptables_in(&counter);
419                         __add_iptables_out(&counter);
420                 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
421                         __add_ip6tables_in(&counter);
422                         __add_ip6tables_out(&counter);
423                 } else {
424                         __add_iptables_in(&counter);
425                         __add_iptables_out(&counter);
426                         __add_ip6tables_in(&counter);
427                         __add_ip6tables_out(&counter);
428                 }
429         }
430
431         return FALSE;
432 }
433
434 static gboolean __remove_application_monitor(gpointer key, gpointer value,
435                                              gpointer data)
436 {
437         stc_app_value_s *app_value = (stc_app_value_s *)value;
438         default_connection_s *connection = (default_connection_s *)data;
439         stc_s *stc = stc_get_manager();
440
441         if (stc && connection && connection->ifname) {
442                 struct nfacct_rule counter;
443
444                 if (!stc->carg) {
445                         stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
446                         if (stc->carg == NULL) //LCOV_EXCL_LINE
447                                 return FALSE; //LCOV_EXCL_LINE
448
449                         stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
450                 }
451
452                 memset(&counter, 0, sizeof(struct nfacct_rule));
453
454                 counter.carg = stc->carg;
455                 counter.classid = app_value->classid;
456                 counter.intend = NFACCT_COUNTER;
457
458                 if (connection->tether_state == FALSE &&
459                         connection->tether_iface.ifname != NULL &&
460                         app_value->classid == STC_TETHERING_APP_CLASSID) {
461                         counter.iftype = connection->tether_iface.type;
462                         g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
463                 } else {
464                         counter.iftype = connection->type;
465                         g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
466                 }
467
468                 __del_iptables_in(&counter);
469                 __del_iptables_out(&counter);
470                 __del_ip6tables_in(&counter);
471                 __del_ip6tables_out(&counter);
472         }
473
474         return FALSE;
475 }
476
477 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
478 {
479         STC_LOGI("rstn info => rstn_id [%llu], "
480                  "app_id [%s], classid [%u], ifname [%s], "
481                  "iftype [%d], rstn_state [%d], rstn_type [%d], "
482                  "limit [ (%lld) bytes], "
483                  "warn_limit [ (%lld) bytes], "
484                  "counter [ (%lld) bytes], "
485                  "roaming [%d], subscriber_id [%s]",
486                  rstn_value->restriction_id,
487                  rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
488                  rstn_key->iftype, rstn_value->rstn_state, rstn_value->rstn_type,
489                  rstn_value->data_limit,
490                  rstn_value->data_warn_limit,
491                  rstn_value->data_counter,
492                  rstn_key->roaming, rstn_key->subscriber_id);
493 }
494
495 static void __add_iptables_rule(int64_t classid, nfacct_rule_intend intend,
496                                 stc_iface_type_e iftype)
497 {
498         char *default_ifname = stc_default_connection_get_ifname();
499         default_connection_s *connection = stc_get_default_connection();
500         struct nfacct_rule counter;
501         stc_s *stc = stc_get_manager();
502         if (!stc) {
503                 g_free(default_ifname); //LCOV_EXCL_LINE
504                 return; //LCOV_EXCL_LINE
505         }
506
507         if (!stc->carg) {
508                 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
509                 if (stc->carg == NULL) { //LCOV_EXCL_LINE
510                         g_free(default_ifname); //LCOV_EXCL_LINE
511                         return; //LCOV_EXCL_LINE
512                 }
513
514                 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
515         }
516
517         counter.carg = stc->carg;
518         counter.classid = classid;
519         counter.intend = intend;
520
521         if (connection && connection->tether_iface.ifname != NULL &&
522                 classid == STC_TETHERING_APP_CLASSID) {
523                 counter.iftype = connection->tether_iface.type;
524                 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
525         } else {
526                 counter.iftype = iftype;
527                 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
528         }
529
530         g_free(default_ifname);
531
532         /* iptables rule */
533         __add_iptables_in(&counter);
534         __add_iptables_out(&counter);
535
536         /* ip6tables rule */
537         __add_ip6tables_in(&counter);
538         __add_ip6tables_out(&counter);
539 }
540
541 static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend,
542                                 stc_iface_type_e iftype)
543 {
544         char *default_ifname = stc_default_connection_get_ifname();
545         default_connection_s *connection = stc_get_default_connection();
546         struct nfacct_rule counter;
547         stc_s *stc = stc_get_manager();
548         if (!stc) {
549                 g_free(default_ifname); //LCOV_EXCL_LINE
550                 return; //LCOV_EXCL_LINE
551         }
552
553         if (!stc->carg) {
554                 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
555                 if (stc->carg == NULL) { //LCOV_EXCL_LINE
556                         g_free(default_ifname); //LCOV_EXCL_LINE
557                         return; //LCOV_EXCL_LINE
558                 }
559
560                 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
561         }
562
563         counter.carg = stc->carg;
564         counter.classid = classid;
565         counter.intend = intend;
566
567         if (connection && connection->tether_iface.ifname != NULL &&
568                 classid == STC_TETHERING_APP_CLASSID) {
569                 counter.iftype = connection->tether_iface.type;
570                 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
571         } else {
572                 counter.iftype = iftype;
573                 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
574         }
575
576         g_free(default_ifname);
577
578         /* iptables rule */
579         __del_iptables_in(&counter);
580         __del_iptables_out(&counter);
581
582         /* ip6tables rule */
583         __del_ip6tables_in(&counter);
584         __del_ip6tables_out(&counter);
585 }
586
587 static void __process_restriction(enum traffic_restriction_type rstn_type,
588                                   stc_rstn_key_s *rstn_key,
589                                   stc_rstn_value_s *rstn_value, void *data)
590 {
591         int64_t effective_data_limit, effective_data_warn_limit;
592         default_connection_s *old_connection = (default_connection_s *)data;
593         default_connection_s *connection = NULL;
594
595         if (old_connection != NULL)
596                 connection = old_connection;
597         else
598                 connection = stc_get_default_connection();
599
600         /* no default ifname */
601         if (connection->ifname == NULL)
602                 return;
603
604         /* rstn not applicable for this interface */
605         if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
606             (g_strcmp0(connection->ifname, rstn_key->ifname) != 0) &&
607                 (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
608                 return;
609
610         /* classid is invalid */
611         if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
612                 return;
613
614         effective_data_limit = rstn_value->data_limit;
615         effective_data_warn_limit = rstn_value->data_warn_limit;
616
617         if (rstn_type == RST_SET) {
618                 /* TODO: Change this to runtime memory */
619                 table_counters_info info;
620
621                 memset(&info, 0, sizeof(table_counters_info));
622                 table_counters_get(rstn_value->restriction_id, &info);
623
624                 effective_data_limit -= info.data_counter;
625                 effective_data_warn_limit -= info.data_counter;
626
627                 if (effective_data_limit < 0) {
628                         effective_data_limit = 0; //LCOV_EXCL_LINE
629                         rstn_value->data_limit_reached = TRUE; //LCOV_EXCL_LINE
630                 }
631
632                 if (effective_data_warn_limit < 0)
633                         effective_data_warn_limit = 0; //LCOV_EXCL_LINE
634
635                 STC_LOGD("datausage [%lld] bytes", info.data_counter);
636         }
637
638         STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
639                  "effective_data_warn_limit [%lld] bytes",
640                  rstn_value->restriction_id, effective_data_limit,
641                  effective_data_warn_limit);
642
643         switch (rstn_type) {
644         case RST_SET:
645                 if (effective_data_limit <= 0)
646                         __add_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
647                                             rstn_key->iftype);
648
649                 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
650                 rstn_value->data_limit_reached = FALSE;
651                 break;
652         case RST_EXCLUDE:
653                 __add_iptables_rule(rstn_value->classid, NFACCT_ALLOW,
654                                     rstn_key->iftype);
655
656                 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
657                 rstn_value->data_limit_reached = TRUE;
658                 break;
659         case RST_UNSET:
660                 if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
661                         __del_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
662                                             rstn_key->iftype);
663                 else
664                         __del_iptables_rule(rstn_value->classid, rstn_value->rstn_type,
665                                             rstn_key->iftype);
666
667                 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
668                 rstn_value->data_limit_reached = FALSE;
669                 break;
670         default:
671                 ;//Do Nothing
672         }
673 }
674
675 //LCOV_EXCL_START
676 static gboolean __remove_rstns_foreach_application(gpointer key,
677                                                    gpointer value,
678                                                    gpointer data)
679 {
680         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
681         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
682         gchar *app_id = (gchar *)data;
683
684         /* rstn rule is not for applications */
685         if (rstn_key->app_id == NULL)
686                 goto out;
687
688         /* rstn rule is not for this application */
689         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
690                 goto out;
691
692         /* rstn rule is already removed */
693         if (rstn_value->rstn_state == STC_RSTN_STATE_DEACTIVATED)
694                 goto out;
695
696         /* remove restriction from system */
697         __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
698
699         __print_rstn(rstn_key, rstn_value);
700 out:
701         return FALSE;
702 }
703 //LCOV_EXCL_STOP
704
705 static void __remove_rstns_for_application(gchar *app_id)
706 {
707         g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
708                        app_id);
709 }
710
711 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
712 {
713         stc_error_e ret = STC_ERROR_NONE;
714         guint pid_count = 0;
715         stc_app_value_s *lookup;
716
717         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
718
719         lookup = __application_lookup(g_system->apps, app_key);
720         if (!lookup) {
721                 STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
722                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
723         }
724
725         g_tree_foreach(lookup->processes, __processes_tree_check_empty,
726                        &pid_count);
727
728         if (!pid_count) {
729                 /* remove nfacct rule for this classid */
730                 __remove_application_monitor((gpointer) app_key, lookup,
731                                              stc_get_default_connection());
732                 __remove_rstns_for_application(app_key->app_id);
733         }
734
735         if (!g_tree_remove(g_system->apps, app_key)) {
736                 ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
737                 STC_LOGE("key not found"); //LCOV_EXCL_LINE
738         }
739
740         return ret;
741 }
742
743 static stc_error_e __close_contr_sock(stc_system_s *system)
744 {
745         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
746
747         /* close netlink socket for updating kernel counters */
748         if (system->contr_sock != -1) {
749                 close(system->contr_sock);
750                 system->contr_sock = -1;
751         }
752
753         if (system->contr_gsource_id != 0) {
754                 g_source_remove(system->contr_gsource_id);
755                 system->contr_gsource_id = 0;
756         }
757
758         return STC_ERROR_NONE;
759 }
760
761 static gboolean __process_contr_reply(GIOChannel *source,
762                                       GIOCondition condition,
763                                       gpointer user_data);
764
765 //LCOV_EXCL_START
766 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
767 {
768         GIOChannel *gio = NULL;
769         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
770
771         /* close netlink socket for updating kernel counters */
772         if (system->contr_sock != -1) {
773                 close(system->contr_sock);
774                 system->contr_sock = -1;
775         }
776
777         if (system->contr_gsource_id != 0) {
778                 g_source_remove(system->contr_gsource_id);
779                 system->contr_gsource_id = 0;
780         }
781
782         /* create netlink socket for updating kernel counters */
783         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
784         if (system->contr_sock < 0) {
785                 STC_LOGE("failed to open socket");
786                 FREE(system);
787                 return STC_ERROR_FAIL;
788         }
789
790         gio = g_io_channel_unix_new(system->contr_sock);
791         system->contr_gsource_id =
792                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
793                                (GIOFunc) __process_contr_reply,
794                                NULL);
795         g_io_channel_unref(gio);
796
797         return STC_ERROR_NONE;
798 }
799
800 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
801                                       stc_rstn_value_s *rstn_value,
802                                       classid_bytes_context_s *context)
803 {
804         switch (context->counter->iotype) {
805         case NFACCT_COUNTER_IN:
806         case NFACCT_COUNTER_OUT:
807                 rstn_value->data_counter += context->bytes;
808
809                 if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
810                     rstn_value->warn_limit_crossed_notified == FALSE) {
811
812                         gboolean rv;
813                         char iftype[MAX_INT_LENGTH];
814                         char byte[MAX_INT_LENGTH];
815                         stc_s *stc = (stc_s *)stc_get_manager();
816                         ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
817
818                         /* emit signal */
819                         rv = stc_manager_dbus_emit_signal(stc->connection,
820                                                           STC_DBUS_SERVICE_RESTRICTION_PATH,
821                                                           STC_DBUS_INTERFACE_RESTRICTION,
822                                                           "WarnThresholdCrossed",
823                                                           g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
824                         if (rv == TRUE)
825                                 rstn_value->warn_limit_crossed_notified = TRUE;
826
827                         snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
828                         snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
829                         stc_plugin_appstatus_send_warn_message("warn threshold crossed",
830                                                            "warning_noti",
831                                                            rstn_key->app_id,
832                                                            iftype, byte);
833                 }
834
835                 if (rstn_value->data_counter >= rstn_value->data_limit &&
836                     rstn_value->rstn_limit_crossed_notified == FALSE) {
837
838                         gboolean rv;
839                         char iftype[MAX_INT_LENGTH];
840                         char byte[MAX_INT_LENGTH];
841                         stc_s *stc = (stc_s *)stc_get_manager();
842                         ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
843
844                         /* block immediately */
845                         context->counter->intend = NFACCT_BLOCK;
846                         __del_iptables_in(context->counter);
847                         __del_iptables_out(context->counter);
848                         __add_iptables_in(context->counter);
849                         __add_iptables_out(context->counter);
850
851                         __del_ip6tables_in(context->counter);
852                         __del_ip6tables_out(context->counter);
853                         __add_ip6tables_in(context->counter);
854                         __add_ip6tables_out(context->counter);
855                         context->counter->intend = NFACCT_COUNTER;
856
857                         rstn_value->data_limit_reached = TRUE;
858
859                         /* emit signal */
860                         rv = stc_manager_dbus_emit_signal(stc->connection,
861                                                           STC_DBUS_SERVICE_RESTRICTION_PATH,
862                                                           STC_DBUS_INTERFACE_RESTRICTION,
863                                                           "RestrictionThresholdCrossed",
864                                                           g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
865                         if (rv == TRUE)
866                                 rstn_value->rstn_limit_crossed_notified = TRUE;
867
868                         snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
869                         snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
870                         stc_plugin_appstatus_send_restriction_message("restriction threshold crossed",
871                                                                   "restriction_noti", rstn_key->app_id,
872                                                                   iftype, byte);
873                 }
874
875                 g_system->rstns_tree_updated = TRUE;
876                 __print_rstn(rstn_key, rstn_value);
877                 break;
878         default:
879                 STC_LOGE("unknown iotype");
880         }
881
882         return FALSE;
883 }
884
885 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
886                                                 stc_rstn_value_s *rstn_value,
887                                                 classid_bytes_context_s *context)
888 {
889         if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
890                 context->counter->iftype == STC_IFACE_DATACALL) ||
891                 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
892                 context->counter->iftype == STC_IFACE_WIFI) ||
893                 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
894                 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
895                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
896                  context->counter->iftype == STC_IFACE_WIFI) ||
897                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
898                  context->counter->iftype == STC_IFACE_BLUETOOTH) ||
899                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
900                  context->counter->iftype == STC_IFACE_USB) ||
901                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
902                  context->counter->iftype == STC_IFACE_P2P)) {
903                 context->counter->classid = rstn_value->classid;
904                 return __rstn_counter_update(rstn_key, rstn_value, context);
905         }
906
907         return FALSE;
908 }
909
910 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
911                                                       gpointer value,
912                                                       gpointer data)
913 {
914         gboolean rv = FALSE;
915         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
916         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
917         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
918         uint32_t classid;
919
920         if (context->counter->intend != NFACCT_COUNTER)
921                 goto try_next_callback;
922
923         if (rstn_value->data_limit_reached == TRUE) {
924                 context->data_limit_reached = TRUE; //LCOV_EXCL_LINE
925                 goto try_next_callback; //LCOV_EXCL_LINE
926         }
927
928         classid = context->counter->classid;
929         rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
930
931         context->counter->classid = classid;
932         if (rstn_value->classid != context->counter->classid)
933                 goto try_next_callback;
934
935         rv = __rstn_counter_update(rstn_key, rstn_value, context);
936
937 try_next_callback:
938         return rv;
939 }
940 //LCOV_EXCL_STOP
941
942 static gboolean __update_app_statistics(gpointer key, gpointer value,
943                                         gpointer data)
944 {
945         stc_app_key_s *app_key = (stc_app_key_s *)key;
946         stc_app_value_s *app_value = (stc_app_value_s *)value;
947         time_t *touch_time = (time_t *)data;
948         stc_db_classid_iftype_key stat_key;
949         stc_db_app_stats stat;
950         default_connection_s *default_connection = stc_get_default_connection();
951
952         memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
953         memset(&stat, 0 , sizeof(stc_db_app_stats));
954
955         /* Do not update statistics for Tethering
956          * if tethering is in-active found */
957         if (default_connection &&
958                 default_connection->tether_state == FALSE &&
959                 !strcmp(app_key->app_id, STC_TOTAL_TETHERING))
960                 return FALSE;
961
962         /* Do not update statistics for Wi-Fi
963          * if tethering is active on wlan0 iface */
964         if (default_connection && default_connection->tether_state &&
965                 default_connection->tether_iface.type == STC_IFACE_WIFI &&
966                 !strcmp(app_key->app_id, STC_TOTAL_WIFI))
967                 return FALSE;
968
969         stat_key.classid = app_value->classid;
970
971         if (app_value->classid == STC_TETHERING_APP_CLASSID &&
972                 default_connection->tether_state == TRUE)
973                 stat_key.iftype = default_connection->tether_iface.type;
974         else
975                 stat_key.iftype = default_connection->type;
976
977         if (STC_IFACE_DATACALL == stat_key.iftype)
978                 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
979         else
980                 stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
981
982         if (app_value->classid == STC_TETHERING_APP_CLASSID &&
983                 default_connection->tether_state == TRUE)
984                 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
985                           MAX_IFACE_LENGTH);
986         else
987                 g_strlcpy(stat_key.ifname, default_connection->ifname,
988                           MAX_IFACE_LENGTH);
989
990         stat.app_id = g_strdup(app_key->app_id);
991         stat.snd_count = app_value->counter.out_bytes;
992         stat.rcv_count = app_value->counter.in_bytes;
993         stat.is_roaming = default_connection->roaming;
994
995         if (strstr(stat.app_id, "_BACKGROUND")) {
996                 stat.ground = STC_APP_STATE_BACKGROUND;
997         } else {
998                 if (strstr(stat.app_id, "TOTAL_"))
999                         stat.ground = STC_APP_STATE_UNKNOWN;
1000                 else
1001                         stat.ground = STC_APP_STATE_FOREGROUND;
1002         }
1003
1004         table_statistics_insert(&stat_key, &stat, *touch_time);
1005
1006         app_value->counter.out_bytes = 0;
1007         app_value->counter.in_bytes = 0;
1008
1009         FREE(stat.app_id);
1010         FREE(stat_key.subscriber_id);
1011
1012         return FALSE;
1013 }
1014
1015 static gboolean __flush_apps_stats_to_database(gpointer user_data)
1016 {
1017         time_t current_time = time(0);
1018
1019         if (g_system->apps_tree_updated == FALSE)
1020                 return G_SOURCE_REMOVE;
1021
1022         g_system->apps_tree_updated = FALSE;
1023
1024         if (g_system->apps)
1025                 g_tree_foreach(g_system->apps,
1026                                __update_app_statistics,
1027                                &current_time);
1028
1029         STC_LOGI("Flushed app stats to database");
1030         return G_SOURCE_REMOVE;
1031 }
1032
1033 //LCOV_EXCL_START
1034 static gboolean __update_counter_statistics(gpointer key, gpointer value,
1035                                             gpointer data)
1036 {
1037         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1038         table_counters_info info = {
1039                 .restriction_id = rstn_value->restriction_id,
1040                 .data_counter = rstn_value->data_counter
1041         };
1042
1043         table_counters_update_counters(&info);
1044
1045         return FALSE;
1046 }
1047
1048 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
1049 {
1050         time_t current_time = time(0);
1051
1052         if (g_system->rstns_tree_updated == FALSE)
1053                 return G_SOURCE_REMOVE;
1054
1055         g_system->rstns_tree_updated = FALSE;
1056
1057         if (g_system->rstns)
1058                 g_tree_foreach(g_system->rstns,
1059                                __update_counter_statistics,
1060                                &current_time);
1061
1062         STC_LOGI("Flushed rstns counters to database");
1063         return G_SOURCE_REMOVE;
1064 }
1065 //LCOV_EXCL_STOP
1066
1067 static void __app_counter_update(stc_app_key_s *app_key,
1068                                  stc_app_value_s *app_value,
1069                                  classid_bytes_context_s *context)
1070 {
1071         switch (context->counter->iotype) {
1072         case NFACCT_COUNTER_IN:
1073                 app_value->data_usage.in_bytes += context->bytes;
1074                 app_value->counter.in_bytes = context->bytes;
1075                 g_system->apps_tree_updated = TRUE;
1076
1077                 if (STC_DEBUG_LOG)
1078                         __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
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                 if (STC_DEBUG_LOG)
1086                         __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1087                 break;
1088         default:
1089                 STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
1090         }
1091 }
1092
1093 static void __interface_counter_update(stc_app_key_s *app_key,
1094                                        stc_app_value_s *app_value,
1095                                        classid_bytes_context_s *context)
1096 {
1097         if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1098              context->counter->iftype == STC_IFACE_DATACALL) ||
1099             (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1100              context->counter->iftype == STC_IFACE_WIFI) ||
1101             (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1102              context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1103                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1104                  context->counter->iftype == STC_IFACE_WIFI) ||
1105                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1106                  context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1107                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1108                  context->counter->iftype == STC_IFACE_USB) ||
1109                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1110                  context->counter->iftype == STC_IFACE_P2P))
1111                 __app_counter_update(app_key, app_value, context);
1112 }
1113
1114
1115 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1116                                                       gpointer value,
1117                                                       gpointer data)
1118 {
1119         stc_app_key_s *app_key = (stc_app_key_s *)key;
1120         stc_app_value_s *app_value = (stc_app_value_s *)value;
1121         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1122
1123         if (context->counter->intend != NFACCT_COUNTER)
1124                 goto try_next_callback;
1125
1126         __interface_counter_update(app_key, app_value, context);
1127
1128         if (app_value->classid != context->counter->classid)
1129                 goto try_next_callback;
1130
1131         __app_counter_update(app_key, app_value, context);
1132
1133 try_next_callback:
1134         return FALSE;
1135 }
1136
1137 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1138                                  struct counter_arg *carg)
1139 {
1140         struct nfacct_rule counter = {
1141                 .carg = carg,
1142                 .name = {0},
1143                 .ifname = {0},
1144                 0
1145         };
1146
1147         classid_bytes_context_s context = {
1148                 .counter = &counter,
1149                 .bytes = bytes,
1150                 .data_limit_reached = FALSE,
1151         };
1152
1153         if (STC_DEBUG_LOG)
1154                 STC_LOGD("cnt_name %s", cnt_name); //LCOV_EXCL_LINE
1155
1156         if (!recreate_counter_by_name(cnt_name, &counter)) {
1157                 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1158                 return; //LCOV_EXCL_LINE
1159         }
1160
1161         STC_LOGI("classid %u, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1162                  context.counter->classid, context.counter->iftype,
1163                  context.counter->iotype, context.counter->intend,
1164                  context.counter->ifname, context.bytes);
1165
1166         if (g_system->rstns)
1167                 g_tree_foreach(g_system->rstns,
1168                                __rstn_counter_update_foreach_classid,
1169                                &context);
1170
1171         if (g_system->apps)
1172                 g_tree_foreach(g_system->apps,
1173                                __apps_counter_update_foreach_classid,
1174                                &context);
1175 }
1176
1177 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1178                            void *user_data)
1179 {
1180         struct counter_arg *carg = user_data;
1181         char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1182         if (carg->initiate) {
1183                 /**
1184                  * TODO: this will be used when daemon starts to update existing
1185                  * counter data if present.
1186                  *
1187                  populate_counters(cnt_name, carg);
1188                  */
1189         } else {
1190                 int64_t *bytes_p =
1191                         (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1192                 int bytes = be64toh(*bytes_p);
1193                 if (bytes) {
1194                         ++carg->serialized_counters;
1195                         __fill_nfacct_result(cnt_name, bytes, carg);
1196                 }
1197         }
1198
1199         return 0;
1200 }
1201
1202 static int __post_fill_counters(void *user_data)
1203 {
1204         struct counter_arg *carg = user_data;
1205
1206         if (carg->initiate)
1207                 carg->initiate = 0;
1208
1209         return 0;
1210 }
1211
1212 static void __process_network_counter(struct genl *ans,
1213                                       struct counter_arg *carg)
1214 {
1215         struct netlink_serialization_params ser_params = {
1216                 .carg = carg,
1217                 .ans = ans,
1218                 .eval_attr = __fill_counters,
1219                 .post_eval_attr = __post_fill_counters,
1220         };
1221
1222         netlink_serialization_command *netlink =
1223                 netlink_create_command(&ser_params);
1224         if (!netlink) {
1225                 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
1226                 return; //LCOV_EXCL_LINE
1227         }
1228
1229         netlink->deserialize_answer(&(netlink->params));
1230 }
1231
1232 static gboolean __process_contr_reply(GIOChannel *source,
1233                                       GIOCondition condition,
1234                                       gpointer user_data)
1235 {
1236         int sock = g_io_channel_unix_get_fd(source);
1237         struct genl *ans;
1238         int ret;
1239         stc_s *stc = stc_get_manager();
1240
1241 #ifdef TIZEN_GTESTS
1242         void __gcov_flush(void);
1243         __gcov_flush();
1244 #endif
1245
1246         if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1247             (condition & G_IO_NVAL)) {
1248                 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1249
1250                 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
1251                          "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
1252                          (condition & G_IO_ERR), (condition & G_IO_HUP),
1253                          (condition & G_IO_NVAL));
1254                 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
1255                 return FALSE; //LCOV_EXCL_LINE
1256         }
1257
1258         ans = MALLOC0(struct genl, 1);
1259         if (ans == NULL) {
1260                 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
1261                 return TRUE; //LCOV_EXCL_LINE
1262         }
1263
1264         if (stc == NULL) {
1265                 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
1266                 goto out; //LCOV_EXCL_LINE
1267         }
1268
1269         ret = read_netlink(sock, ans, sizeof(struct genl));
1270         /* STC_LOGD("Counter data received ret [%d]", ret); */
1271         if (ret == 0)
1272                 goto out;
1273
1274         stc->carg->ans_len = ret;
1275         __process_network_counter(ans, stc->carg);
1276
1277         g_idle_add(__flush_apps_stats_to_database, NULL);
1278         g_idle_add(__flush_rstns_counter_to_database, NULL);
1279 out:
1280
1281         FREE(ans);
1282         return TRUE;
1283 }
1284
1285 static gboolean __update_contr_cb(void *user_data)
1286 {
1287         /* Here we just sent command, answer we receive in another callback */
1288         stc_s *stc = stc_get_manager();
1289         ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1290         if (!stc->carg) {
1291                 stc->carg = MALLOC0(counter_arg_s, 1);
1292                 if (stc->carg == NULL)
1293                         return TRUE;  /* we need to continue the timer */
1294
1295                 stc->carg->sock = stc_monitor_get_counter_socket();
1296         }
1297
1298 #ifdef TIZEN_GTESTS
1299         void __gcov_flush(void);
1300         __gcov_flush();
1301 #endif
1302
1303         /* STC_LOGD("Get all counters"); */
1304         nfacct_send_get_all(stc->carg);
1305
1306         /* we need to continue the timer */
1307         return TRUE;
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_END
1326
1327 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1328                                         const stc_rstn_key_s *key)
1329 {
1330         stc_rstn_value_s *lookup;
1331
1332         ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1333
1334         lookup = g_tree_lookup(rstns_tree, key);
1335
1336         return lookup;
1337 }
1338
1339 static gboolean __remove_restriction(gpointer key, gpointer value,
1340                                      gpointer data)
1341 {
1342         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1343         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1344
1345         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1346         __print_rstn(rstn_key, rstn_value);
1347         return FALSE;
1348 }
1349
1350 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1351                                         gpointer data)
1352 {
1353         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1354         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1355
1356         /* rstn rule is activated */
1357         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1358                 return FALSE;
1359
1360         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1361                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1362         else
1363                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1364
1365         __print_rstn(rstn_key, rstn_value);
1366
1367         return FALSE;
1368 }
1369
1370 //LCOV_EXCL_START
1371 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1372 {
1373         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1374         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1375
1376         /* rstn rule is activated */
1377         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1378                 return FALSE;
1379
1380         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1381                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1382         else
1383                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1384
1385         return FALSE;
1386 }
1387 //LCOV_EXCL_STOP
1388
1389 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1390 {
1391         stc_rstn_value_s *lookup_value;
1392
1393         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1394
1395         lookup_value = __rstn_lookup(g_system->rstns, key);
1396         if (!lookup_value) {
1397                 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1398                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1399         }
1400
1401         __remove_restriction(key, lookup_value, NULL);
1402
1403         /* remove counter also */
1404         table_counters_delete(lookup_value->restriction_id);
1405
1406         if (!g_tree_remove(g_system->rstns, key)) {
1407                 STC_LOGD("key not found"); //LCOV_EXCL_LINE
1408                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1409         }
1410
1411         return STC_ERROR_NONE;
1412 }
1413
1414 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1415                                    stc_rstn_value_s *value, gboolean debug)
1416 {
1417         stc_rstn_key_s *rstn_key;
1418         stc_rstn_value_s *rstn_value;
1419
1420         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1421
1422         rstn_value = __rstn_lookup(g_system->rstns, key);
1423         if (rstn_value)
1424                 __rstn_tree_remove(key);
1425
1426         rstn_key = MALLOC0(stc_rstn_key_s, 1);
1427         if (!rstn_key) {
1428                 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
1429                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1430         }
1431
1432         rstn_value = MALLOC0(stc_rstn_value_s, 1);
1433         if (!rstn_value) {
1434                 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
1435                 FREE(rstn_key); //LCOV_EXCL_LINE
1436                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1437         }
1438
1439         rstn_key->app_id = g_strdup(key->app_id);
1440         rstn_key->ifname = g_strdup(key->ifname);
1441         rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1442         rstn_key->iftype = key->iftype;
1443         rstn_key->roaming = key->roaming;
1444
1445         g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1446
1447         rstn_value->restriction_id = value->restriction_id;
1448         rstn_value->rstn_state = value->rstn_state;
1449         rstn_value->rstn_type = value->rstn_type;
1450         rstn_value->classid = value->classid;
1451         rstn_value->data_limit = value->data_limit;
1452         rstn_value->data_warn_limit = value->data_warn_limit;
1453         rstn_value->data_counter = 0;
1454         rstn_value->warn_limit_crossed_notified = FALSE;
1455         rstn_value->rstn_limit_crossed_notified = FALSE;
1456
1457         if (debug == TRUE)
1458                 __add_restriction_debug(key, rstn_value, NULL);
1459         else
1460                 __add_restriction(key, rstn_value, NULL);
1461
1462         return STC_ERROR_NONE;
1463 }
1464
1465 //LCOV_EXCL_START
1466 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1467                                             void *user_data)
1468 {
1469         stc_cb_ret_e ret = STC_CONTINUE;
1470
1471         stc_rstn_key_s key;
1472         stc_rstn_value_s value;
1473
1474         memset(&key, 0, sizeof(stc_rstn_key_s));
1475         memset(&value, 0, sizeof(stc_rstn_value_s));
1476
1477         key.app_id = g_strdup(info->app_id);
1478         key.ifname = g_strdup(info->ifname);
1479         key.subscriber_id = g_strdup(info->subscriber_id);
1480         key.iftype = info->iftype;
1481         key.roaming = info->roaming;
1482
1483         value.rstn_type = info->rstn_type;
1484         value.rstn_state = STC_RSTN_STATE_UNKNOWN;
1485         value.restriction_id = info->restriction_id;
1486
1487         if (info->app_id)
1488                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1489         else
1490                 value.classid = STC_UNKNOWN_CLASSID;
1491
1492         value.data_limit = info->data_limit;
1493         value.data_warn_limit = info->data_warn_limit;
1494
1495         if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1496                 ret = STC_CANCEL;
1497
1498         FREE(key.app_id);
1499         FREE(key.ifname);
1500         FREE(key.subscriber_id);
1501         return ret;
1502 }
1503
1504 static void __fill_restritions_list(void)
1505 {
1506         table_restrictions_foreach(__insert_restriction_cb, NULL);
1507
1508         if (STC_DEBUG_LOG)
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                 STC_LOGD("app_key already present"); //LCOV_EXCL_LINE
1762                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1763         }
1764
1765         key = MALLOC0(stc_app_key_s, 1);
1766         if (!key) {
1767                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1768                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1769         }
1770
1771         value = MALLOC0(stc_app_value_s, 1);
1772         if (!value) {
1773                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1774                 FREE(key); //LCOV_EXCL_LINE
1775                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1776         }
1777
1778         key->app_id = g_strdup(app_key.app_id);
1779         key->pkg_id = g_strdup(app_key.pkg_id);
1780
1781         value->type = app_value.type;
1782         value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1783         value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1784
1785         value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1786                                            __processes_tree_key_free,
1787                                            __processes_tree_value_free);
1788
1789         /* create cgroup and update classid */
1790         value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1791
1792         g_tree_insert(g_system->apps, key, value);
1793
1794         /* add nfacct rule for this classid */
1795         __add_application_monitor(key, value, stc_get_default_connection());
1796         __add_rstns_for_application(app_key.app_id);
1797
1798         return ret;
1799 }
1800
1801 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1802                                     const stc_process_key_s proc_key,
1803                                     const stc_process_value_s proc_value)
1804 {
1805         stc_error_e ret = STC_ERROR_NONE;
1806         stc_app_value_s *app_lookup;
1807         stc_process_key_s *key;
1808         stc_process_value_s *value;
1809         stc_process_value_s *proc_lookup;
1810
1811         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1812
1813         app_lookup = __application_lookup(g_system->apps, &app_key);
1814         if (!app_lookup) {
1815                 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
1816                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1817         }
1818
1819         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1820         if (proc_lookup) {
1821                 STC_LOGD("proc_key already present"); //LCOV_EXCL_LINE
1822                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1823         }
1824
1825         key = MALLOC0(stc_process_key_s, 1);
1826         if (!key) {
1827                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1828                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1829         }
1830
1831         value = MALLOC0(stc_process_value_s, 1);
1832         if (!value) {
1833                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1834                 FREE(key); //LCOV_EXCL_LINE
1835                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1836         }
1837
1838         key->pid = proc_key.pid;
1839
1840         value->ground = proc_value.ground;
1841
1842         g_tree_insert(app_lookup->processes, key, value);
1843
1844         /* add pid to application cgroup */
1845         place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1846
1847         if (STC_DEBUG_LOG)
1848                 __apps_tree_printall(); //LCOV_EXCL_LINE
1849
1850         return ret;
1851 }
1852
1853 API stc_error_e stc_monitor_process_remove(pid_t pid)
1854 {
1855         stc_error_e ret = STC_ERROR_NONE;
1856         stc_process_key_s proc_key = {
1857                 .pid = pid
1858         };
1859
1860         remove_pid_context_s context = {
1861                 .app_key = NULL,
1862                 .proc_key = &proc_key,
1863                 .entry_removed = FALSE,
1864         };
1865
1866         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1867
1868         g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1869                        &context);
1870
1871         if (context.entry_removed)
1872                 __application_remove_if_empty(context.app_key);
1873
1874         if (STC_DEBUG_LOG)
1875                 __apps_tree_printall(); //LCOV_EXCL_LINE
1876
1877         return ret;
1878 }
1879
1880 //LCOV_EXCL_START
1881 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1882                                               const stc_process_key_s proc_key,
1883                                               stc_app_state_e ground)
1884 {
1885         stc_error_e ret = STC_ERROR_NONE;
1886         stc_app_value_s *app_lookup;
1887         stc_process_value_s *proc_lookup;
1888
1889         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1890
1891         app_lookup = __application_lookup(g_system->apps, &app_key);
1892         if (!app_lookup) {
1893                 STC_LOGD("app_key not found");
1894                 return STC_ERROR_FAIL;
1895         }
1896
1897         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1898         if (!proc_lookup) {
1899                 STC_LOGD("proc_key not found");
1900                 return STC_ERROR_FAIL;
1901         }
1902
1903         if (proc_lookup->ground != ground)
1904                 proc_lookup->ground = ground;
1905
1906         if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1907                 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1908         else
1909                 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1910
1911         return ret;
1912 }
1913 //LCOV_EXCL_STOP
1914
1915 void stc_monitor_update_rstn_by_default_connection(void *data)
1916 {
1917         static default_connection_s old_connection;
1918         default_connection_s *new_connection = (default_connection_s *)data;
1919
1920         if (old_connection.path != NULL) {
1921                 //LCOV_EXCL_START
1922                 if (g_system->apps)
1923                         g_tree_foreach(g_system->apps,
1924                                        __remove_application_monitor,
1925                                        (gpointer)&old_connection);
1926
1927                 if (g_system->rstns)
1928                         g_tree_foreach(g_system->rstns,
1929                                        __remove_restriction,
1930                                        (gpointer)&old_connection);
1931
1932                 iptables_flush_chains();
1933                 //LCOV_EXCL_STOP
1934         }
1935
1936         FREE(old_connection.path);
1937         FREE(old_connection.ifname);
1938         FREE(old_connection.tether_iface.ifname);
1939         old_connection.type = 0;
1940         old_connection.roaming = 0;
1941         old_connection.tether_state = FALSE;
1942         old_connection.tether_iface.type = 0;
1943
1944         if (new_connection != NULL && new_connection->path != NULL) {
1945                 if (g_system->apps)
1946                         g_tree_foreach(g_system->apps,
1947                                        __add_application_monitor,
1948                                        (gpointer)new_connection);
1949
1950                 if (g_system->rstns)
1951                         g_tree_foreach(g_system->rstns, __add_restriction,
1952                                        NULL);
1953
1954                 old_connection.path = g_strdup(new_connection->path);
1955                 old_connection.ifname = g_strdup(new_connection->ifname);
1956                 old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
1957                 old_connection.type = new_connection->type;
1958                 old_connection.roaming = new_connection->roaming;
1959                 old_connection.tether_state = new_connection->tether_state;
1960                 old_connection.tether_iface.type = new_connection->tether_iface.type;
1961         }
1962 }
1963
1964 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1965 {
1966         stc_error_e ret;
1967
1968         stc_rstn_key_s key;
1969         stc_rstn_value_s value;
1970
1971         memset(&key, 0, sizeof(stc_rstn_key_s));
1972         memset(&value, 0, sizeof(stc_rstn_value_s));
1973
1974         key.app_id = g_strdup(info->app_id);
1975         key.ifname = g_strdup(info->ifname);
1976         key.subscriber_id = g_strdup(info->subscriber_id);
1977         key.iftype = info->iftype;
1978         key.roaming = info->roaming;
1979
1980         value.rstn_type = info->rstn_type;
1981         value.rstn_state = STC_RSTN_STATE_UNKNOWN;
1982         value.restriction_id = info->restriction_id;
1983
1984         if (info->app_id)
1985                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1986         else
1987                 value.classid = STC_UNKNOWN_CLASSID;
1988
1989         if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1990                 __set_background_state(TRUE); //LCOV_EXCL_LINE
1991                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1992                 __process_update_background(); //LCOV_EXCL_LINE
1993         }
1994
1995         value.data_limit = info->data_limit;
1996         value.data_warn_limit = info->data_warn_limit;
1997
1998         ret = __rstn_tree_add(&key, &value, TRUE);
1999
2000         FREE(key.app_id);
2001         FREE(key.ifname);
2002         FREE(key.subscriber_id);
2003         return ret;
2004 }
2005
2006 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
2007 {
2008         stc_error_e ret;
2009
2010         stc_rstn_key_s key = {
2011                 .app_id = g_strdup(info->app_id),
2012                 .ifname = g_strdup(info->ifname),
2013                 .subscriber_id = g_strdup(info->subscriber_id),
2014                 .iftype = info->iftype,
2015                 .roaming = info->roaming,
2016         };
2017
2018         if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
2019                 __set_background_state(FALSE); //LCOV_EXCL_LINE
2020                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2021                 __process_update_background(); //LCOV_EXCL_LINE
2022         }
2023
2024         ret = __rstn_tree_remove(&key);
2025
2026         FREE(key.app_id);
2027         FREE(key.ifname);
2028         FREE(key.subscriber_id);
2029         return ret;
2030 }
2031
2032 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
2033 {
2034         return stc_plugin_check_exception_by_cmdline(cmdline);
2035 }
2036
2037 int stc_monitor_get_counter_socket(void)
2038 {
2039         return g_system->contr_sock;
2040 }