Organize log for print tree
[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 (STC_DEBUG_LOG)
1155                 STC_LOGD("cnt_name %s", cnt_name); //LCOV_EXCL_LINE
1156
1157         if (!recreate_counter_by_name(cnt_name, &counter)) {
1158                 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1159                 return; //LCOV_EXCL_LINE
1160         }
1161
1162         if (STC_DEBUG_LOG)
1163                 STC_LOGI("classid %u, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1164                         context.counter->classid, context.counter->iftype,
1165                         context.counter->iotype, context.counter->intend,
1166                         context.counter->ifname, context.bytes);
1167
1168         if (g_system->rstns)
1169                 g_tree_foreach(g_system->rstns,
1170                                __rstn_counter_update_foreach_classid,
1171                                &context);
1172
1173         if (g_system->apps)
1174                 g_tree_foreach(g_system->apps,
1175                                __apps_counter_update_foreach_classid,
1176                                &context);
1177 }
1178
1179 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1180                            void *user_data)
1181 {
1182         struct counter_arg *carg = user_data;
1183         char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1184         if (carg->initiate) {
1185                 /**
1186                  * TODO: this will be used when daemon starts to update existing
1187                  * counter data if present.
1188                  *
1189                  populate_counters(cnt_name, carg);
1190                  */
1191         } else {
1192                 int64_t *bytes_p =
1193                         (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1194                 int bytes = be64toh(*bytes_p);
1195                 if (bytes) {
1196                         ++carg->serialized_counters;
1197                         __fill_nfacct_result(cnt_name, bytes, carg);
1198                 }
1199         }
1200
1201         return 0;
1202 }
1203
1204 static int __post_fill_counters(void *user_data)
1205 {
1206         struct counter_arg *carg = user_data;
1207
1208         if (carg->initiate)
1209                 carg->initiate = 0;
1210
1211         return 0;
1212 }
1213
1214 static void __process_network_counter(struct genl *ans,
1215                                       struct counter_arg *carg)
1216 {
1217         struct netlink_serialization_params ser_params = {
1218                 .carg = carg,
1219                 .ans = ans,
1220                 .eval_attr = __fill_counters,
1221                 .post_eval_attr = __post_fill_counters,
1222         };
1223
1224         netlink_serialization_command *netlink =
1225                 netlink_create_command(&ser_params);
1226         if (!netlink) {
1227                 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
1228                 return; //LCOV_EXCL_LINE
1229         }
1230
1231         netlink->deserialize_answer(&(netlink->params));
1232 }
1233
1234 static gboolean __process_contr_reply(GIOChannel *source,
1235                                       GIOCondition condition,
1236                                       gpointer user_data)
1237 {
1238         int sock = g_io_channel_unix_get_fd(source);
1239         struct genl *ans;
1240         int ret;
1241         stc_s *stc = stc_get_manager();
1242
1243 #ifdef TIZEN_GTESTS
1244         void __gcov_flush(void);
1245         __gcov_flush();
1246 #endif
1247
1248         if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1249             (condition & G_IO_NVAL)) {
1250                 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1251
1252                 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
1253                          "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
1254                          (condition & G_IO_ERR), (condition & G_IO_HUP),
1255                          (condition & G_IO_NVAL));
1256                 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
1257                 return FALSE; //LCOV_EXCL_LINE
1258         }
1259
1260         ans = MALLOC0(struct genl, 1);
1261         if (ans == NULL) {
1262                 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
1263                 return TRUE; //LCOV_EXCL_LINE
1264         }
1265
1266         if (stc == NULL) {
1267                 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
1268                 goto out; //LCOV_EXCL_LINE
1269         }
1270
1271         ret = read_netlink(sock, ans, sizeof(struct genl));
1272         /* STC_LOGD("Counter data received ret [%d]", ret); */
1273         if (ret == 0)
1274                 goto out;
1275
1276         stc->carg->ans_len = ret;
1277         __process_network_counter(ans, stc->carg);
1278
1279         g_idle_add(__flush_apps_stats_to_database, NULL);
1280         g_idle_add(__flush_rstns_counter_to_database, NULL);
1281 out:
1282
1283         FREE(ans);
1284         return TRUE;
1285 }
1286
1287 static gboolean __update_contr_cb(void *user_data)
1288 {
1289         /* Here we just sent command, answer we receive in another callback */
1290         stc_s *stc = stc_get_manager();
1291         ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1292         if (!stc->carg) {
1293                 stc->carg = MALLOC0(counter_arg_s, 1);
1294                 if (stc->carg == NULL)
1295                         return TRUE;  /* we need to continue the timer */
1296
1297                 stc->carg->sock = stc_monitor_get_counter_socket();
1298         }
1299
1300 #ifdef TIZEN_GTESTS
1301         void __gcov_flush(void);
1302         __gcov_flush();
1303 #endif
1304
1305         /* STC_LOGD("Get all counters"); */
1306         nfacct_send_get_all(stc->carg);
1307
1308         /* we need to continue the timer */
1309         return TRUE;
1310 }
1311
1312 /*
1313 //LCOV_EXCL_START
1314 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1315                                           gpointer data)
1316 {
1317         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1318         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1319
1320         __print_rstn(rstn_key, rstn_value);
1321         return FALSE;
1322 }
1323
1324 static void __rstn_tree_printall(void)
1325 {
1326         g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1327 }
1328 //LCOV_EXCL_STOP
1329 */
1330
1331 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1332                                         const stc_rstn_key_s *key)
1333 {
1334         stc_rstn_value_s *lookup;
1335
1336         ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1337
1338         lookup = g_tree_lookup(rstns_tree, key);
1339
1340         return lookup;
1341 }
1342
1343 static gboolean __remove_restriction(gpointer key, gpointer value,
1344                                      gpointer data)
1345 {
1346         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1347         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1348
1349         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1350         __print_rstn(rstn_key, rstn_value);
1351         return FALSE;
1352 }
1353
1354 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1355                                         gpointer data)
1356 {
1357         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1358         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1359
1360         /* rstn rule is activated */
1361         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1362                 return FALSE;
1363
1364         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1365                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1366         else
1367                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1368
1369         __print_rstn(rstn_key, rstn_value);
1370
1371         return FALSE;
1372 }
1373
1374 //LCOV_EXCL_START
1375 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1376 {
1377         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1378         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1379
1380         /* rstn rule is activated */
1381         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1382                 return FALSE;
1383
1384         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1385                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1386         else
1387                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1388
1389         return FALSE;
1390 }
1391 //LCOV_EXCL_STOP
1392
1393 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1394 {
1395         stc_rstn_value_s *lookup_value;
1396
1397         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1398
1399         lookup_value = __rstn_lookup(g_system->rstns, key);
1400         if (!lookup_value) {
1401                 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1402                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1403         }
1404
1405         __remove_restriction(key, lookup_value, NULL);
1406
1407         /* remove counter also */
1408         table_counters_delete(lookup_value->restriction_id);
1409
1410         if (!g_tree_remove(g_system->rstns, key)) {
1411                 STC_LOGD("key not found"); //LCOV_EXCL_LINE
1412                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1413         }
1414
1415         return STC_ERROR_NONE;
1416 }
1417
1418 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1419                                    stc_rstn_value_s *value, gboolean debug)
1420 {
1421         stc_rstn_key_s *rstn_key;
1422         stc_rstn_value_s *rstn_value;
1423
1424         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1425
1426         rstn_value = __rstn_lookup(g_system->rstns, key);
1427         if (rstn_value)
1428                 __rstn_tree_remove(key);
1429
1430         rstn_key = MALLOC0(stc_rstn_key_s, 1);
1431         if (!rstn_key) {
1432                 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
1433                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1434         }
1435
1436         rstn_value = MALLOC0(stc_rstn_value_s, 1);
1437         if (!rstn_value) {
1438                 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
1439                 FREE(rstn_key); //LCOV_EXCL_LINE
1440                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1441         }
1442
1443         rstn_key->app_id = g_strdup(key->app_id);
1444         rstn_key->ifname = g_strdup(key->ifname);
1445         rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1446         rstn_key->iftype = key->iftype;
1447         rstn_key->roaming = key->roaming;
1448
1449         g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1450
1451         rstn_value->restriction_id = value->restriction_id;
1452         rstn_value->rstn_state = value->rstn_state;
1453         rstn_value->rstn_type = value->rstn_type;
1454         rstn_value->classid = value->classid;
1455         rstn_value->data_limit = value->data_limit;
1456         rstn_value->data_warn_limit = value->data_warn_limit;
1457         rstn_value->data_counter = 0;
1458         rstn_value->warn_limit_crossed_notified = FALSE;
1459         rstn_value->rstn_limit_crossed_notified = FALSE;
1460
1461         if (debug == TRUE)
1462                 __add_restriction_debug(key, rstn_value, NULL);
1463         else
1464                 __add_restriction(key, rstn_value, NULL);
1465
1466         return STC_ERROR_NONE;
1467 }
1468
1469 //LCOV_EXCL_START
1470 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1471                                             void *user_data)
1472 {
1473         stc_cb_ret_e ret = STC_CONTINUE;
1474
1475         stc_rstn_key_s key;
1476         stc_rstn_value_s value;
1477
1478         memset(&key, 0, sizeof(stc_rstn_key_s));
1479         memset(&value, 0, sizeof(stc_rstn_value_s));
1480
1481         key.app_id = g_strdup(info->app_id);
1482         key.ifname = g_strdup(info->ifname);
1483         key.subscriber_id = g_strdup(info->subscriber_id);
1484         key.iftype = info->iftype;
1485         key.roaming = info->roaming;
1486
1487         value.rstn_type = info->rstn_type;
1488         value.rstn_state = STC_RSTN_STATE_UNKNOWN;
1489         value.restriction_id = info->restriction_id;
1490
1491         if (info->app_id)
1492                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1493         else
1494                 value.classid = STC_UNKNOWN_CLASSID;
1495
1496         value.data_limit = info->data_limit;
1497         value.data_warn_limit = info->data_warn_limit;
1498
1499         if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1500                 ret = STC_CANCEL;
1501
1502         FREE(key.app_id);
1503         FREE(key.ifname);
1504         FREE(key.subscriber_id);
1505         return ret;
1506 }
1507
1508 static void __fill_restritions_list(void)
1509 {
1510         table_restrictions_foreach(__insert_restriction_cb, NULL);
1511
1512         /* __rstn_tree_printall(); */
1513 }
1514
1515 static gboolean __add_rstn_foreach_application(gpointer key,
1516                                                gpointer value,
1517                                                gpointer data)
1518 {
1519         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1520         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1521         gchar *app_id = (gchar *)data;
1522
1523         /* rstn rule is not for applications */
1524         if (rstn_key->app_id == NULL)
1525                 goto out;
1526
1527         /* rstn rule is not for this application */
1528         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1529                 goto out;
1530
1531         /* rstn rule is already applied */
1532         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1533                 goto out;
1534
1535         /* add restriction to system */
1536         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1537                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
1538         else
1539                 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
1540
1541         __print_rstn(rstn_key, rstn_value);
1542 out:
1543         return FALSE;
1544 }
1545 //LCOV_EXCL_STOP
1546
1547 static void __add_rstns_for_application(gchar *app_id)
1548 {
1549         g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1550                        app_id);
1551 }
1552
1553 static void __add_application_by_interface(const char *app_id)
1554 {
1555         stc_app_key_s app_key;
1556         stc_app_value_s app_value;
1557
1558         if (app_id == NULL)
1559                 return; //LCOV_EXCL_LINE
1560
1561         memset(&app_key, 0, sizeof(stc_app_key_s));
1562         memset(&app_value, 0, sizeof(stc_app_value_s));
1563
1564         app_key.pkg_id = g_strdup(app_id);
1565         app_key.app_id = g_strdup(app_id);
1566
1567         app_value.type = STC_APP_TYPE_NONE;
1568         app_value.processes = NULL;
1569         app_value.counter.in_bytes = 0;
1570         app_value.counter.out_bytes = 0;
1571
1572         stc_monitor_application_add(app_key, app_value);
1573
1574         FREE(app_key.pkg_id);
1575         FREE(app_key.app_id);
1576 }
1577
1578 static int __vconf_get_int(const char *key, int *value)
1579 {
1580         int ret = 0;
1581
1582         ret = vconf_get_int(key, value);
1583         if (ret != VCONF_OK) {
1584                 STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
1585                 return -1; //LCOV_EXCL_LINE
1586         }
1587
1588         return 0;
1589 }
1590
1591 //LCOV_EXCL_START
1592 static int __vconf_set_int(const char *key, int value)
1593 {
1594         int ret = 0;
1595
1596         ret = vconf_set_int(key, value);
1597         if (ret != VCONF_OK) {
1598                 STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
1599                 return -1; //LCOV_EXCL_LINE
1600         }
1601
1602         return 0;
1603 }
1604
1605 static guint __get_background_state(void)
1606 {
1607         return g_system->background_state;;
1608 }
1609
1610 static void __set_background_state(guint state)
1611 {
1612         g_system->background_state = state;
1613 }
1614
1615 static gboolean __processes_tree_foreach_background(gpointer key,
1616                                                     gpointer value,
1617                                                     gpointer data)
1618 {
1619         stc_process_key_s *proc_key = (stc_process_key_s *)key;
1620         stc_app_key_s *app_key = (stc_app_key_s *)data;
1621
1622         if (g_system->background_state)
1623                 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1624         else
1625                 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1626
1627         return FALSE;
1628 }
1629
1630 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1631                                         gpointer data)
1632 {
1633         stc_app_key_s *app_key = (stc_app_key_s *)key;
1634         stc_app_value_s *app_value = (stc_app_value_s *)value;
1635
1636         if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1637                 g_tree_foreach(app_value->processes,
1638                                __processes_tree_foreach_background, app_key);
1639
1640         return FALSE;
1641 }
1642
1643 static stc_error_e __process_update_background(void)
1644 {
1645         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1646
1647         g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1648
1649         return STC_ERROR_NONE;
1650 }
1651 //LCOV_EXCL_STOP
1652
1653 static void __fill_exceptions_list(void)
1654 {
1655         stc_plugin_fill_exception_list();
1656 }
1657
1658 stc_error_e stc_monitor_init(void)
1659 {
1660         stc_system_s *system = MALLOC0(stc_system_s, 1);
1661         GIOChannel *gio = NULL;
1662
1663         ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1664
1665         /* initializing current classid */
1666         init_current_classid();
1667
1668         /* initializing cgroups */
1669         cgroup_init();
1670
1671         /* creating monitored application tree */
1672         system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1673                                        __apps_tree_key_free,
1674                                        __apps_tree_value_free);
1675
1676         system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1677                                         __rstns_tree_key_free,
1678                                         __rstns_tree_value_free);
1679
1680         /* create netlink socket for updating kernel counters */
1681         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1682         if (system->contr_sock < 0) {
1683                 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
1684                 FREE(system); //LCOV_EXCL_LINE
1685                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1686         }
1687
1688         gio = g_io_channel_unix_new(system->contr_sock);
1689         system->contr_gsource_id =
1690                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1691                                (GIOFunc) __process_contr_reply,
1692                                NULL);
1693         g_io_channel_unref(gio);
1694
1695         g_system = system;
1696
1697         __add_application_by_interface(STC_TOTAL_DATACALL);
1698         __add_application_by_interface(STC_TOTAL_WIFI);
1699         __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1700         __add_application_by_interface(STC_TOTAL_IPV4);
1701         __add_application_by_interface(STC_TOTAL_IPV6);
1702         __add_application_by_interface(STC_TOTAL_TETHERING);
1703
1704         /* creating restriction rules tree */
1705         __update_contr_cb(NULL);
1706
1707         /* registering periodic kernel counters update callback */
1708         g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1709                                                          __update_contr_cb,
1710                                                          NULL);
1711         if (g_system->contr_timer_id == 0) {
1712                 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
1713                 __close_contr_sock(g_system); //LCOV_EXCL_LINE
1714                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1715         }
1716
1717         __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1718                         (int *)&g_system->background_state);
1719
1720         __fill_exceptions_list();
1721         __fill_restritions_list();
1722
1723         return STC_ERROR_NONE;
1724 }
1725
1726 stc_error_e stc_monitor_deinit(void)
1727 {
1728         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1729
1730         /* close netlink socket for updating kernel counters */
1731         __close_contr_sock(g_system);
1732
1733         /* remove kernel counters update timer */
1734         if (g_system->contr_timer_id > 0) {
1735                 g_source_remove(g_system->contr_timer_id);
1736                 g_system->contr_timer_id = 0;
1737         }
1738
1739         /* destroy monitored application tree */
1740         g_tree_destroy(g_system->apps);
1741         g_system->apps = NULL;
1742
1743         /* destroy restriction rules tree */
1744         g_tree_destroy(g_system->rstns);
1745         g_system->rstns = NULL;
1746
1747         FREE(g_system);
1748
1749         return STC_ERROR_NONE;
1750 }
1751
1752 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1753                                         const stc_app_value_s app_value)
1754 {
1755         stc_error_e ret = STC_ERROR_NONE;
1756         stc_app_key_s *key;
1757         stc_app_value_s *value;
1758         stc_app_value_s *lookup;
1759
1760         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1761
1762         lookup = __application_lookup(g_system->apps, &app_key);
1763         if (lookup) {
1764                 if (STC_DEBUG_LOG)
1765                         STC_LOGD("app_key already present"); //LCOV_EXCL_LINE
1766                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1767         }
1768
1769         key = MALLOC0(stc_app_key_s, 1);
1770         if (!key) {
1771                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1772                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1773         }
1774
1775         value = MALLOC0(stc_app_value_s, 1);
1776         if (!value) {
1777                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1778                 FREE(key); //LCOV_EXCL_LINE
1779                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1780         }
1781
1782         key->app_id = g_strdup(app_key.app_id);
1783         key->pkg_id = g_strdup(app_key.pkg_id);
1784
1785         value->type = app_value.type;
1786         value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1787         value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1788
1789         value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1790                                            __processes_tree_key_free,
1791                                            __processes_tree_value_free);
1792
1793         /* create cgroup and update classid */
1794         value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1795
1796         g_tree_insert(g_system->apps, key, value);
1797
1798         /* add nfacct rule for this classid */
1799         __add_application_monitor(key, value, stc_get_default_connection());
1800         __add_rstns_for_application(app_key.app_id);
1801
1802         return ret;
1803 }
1804
1805 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1806                                     const stc_process_key_s proc_key,
1807                                     const stc_process_value_s proc_value)
1808 {
1809         stc_error_e ret = STC_ERROR_NONE;
1810         stc_app_value_s *app_lookup;
1811         stc_process_key_s *key;
1812         stc_process_value_s *value;
1813         stc_process_value_s *proc_lookup;
1814
1815         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1816
1817         app_lookup = __application_lookup(g_system->apps, &app_key);
1818         if (!app_lookup) {
1819                 if (STC_DEBUG_LOG)
1820                         STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
1821                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1822         }
1823
1824         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1825         if (proc_lookup) {
1826                 if (STC_DEBUG_LOG)
1827                         STC_LOGD("proc_key already present"); //LCOV_EXCL_LINE
1828                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1829         }
1830
1831         key = MALLOC0(stc_process_key_s, 1);
1832         if (!key) {
1833                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1834                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1835         }
1836
1837         value = MALLOC0(stc_process_value_s, 1);
1838         if (!value) {
1839                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1840                 FREE(key); //LCOV_EXCL_LINE
1841                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1842         }
1843
1844         key->pid = proc_key.pid;
1845
1846         value->ground = proc_value.ground;
1847
1848         g_tree_insert(app_lookup->processes, key, value);
1849
1850         /* add pid to application cgroup */
1851         place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1852
1853         /*
1854         __apps_tree_printall(); //LCOV_EXCL_LINE
1855         */
1856
1857         return ret;
1858 }
1859
1860 API stc_error_e stc_monitor_process_remove(pid_t pid)
1861 {
1862         stc_error_e ret = STC_ERROR_NONE;
1863         stc_process_key_s proc_key = {
1864                 .pid = pid
1865         };
1866
1867         remove_pid_context_s context = {
1868                 .app_key = NULL,
1869                 .proc_key = &proc_key,
1870                 .entry_removed = FALSE,
1871         };
1872
1873         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1874
1875         g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1876                        &context);
1877
1878         if (context.entry_removed)
1879                 __application_remove_if_empty(context.app_key);
1880
1881         /*
1882         __apps_tree_printall(); //LCOV_EXCL_LINE
1883         */
1884
1885         return ret;
1886 }
1887
1888 //LCOV_EXCL_START
1889 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1890                                               const stc_process_key_s proc_key,
1891                                               stc_app_state_e ground)
1892 {
1893         stc_error_e ret = STC_ERROR_NONE;
1894         stc_app_value_s *app_lookup;
1895         stc_process_value_s *proc_lookup;
1896
1897         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1898
1899         app_lookup = __application_lookup(g_system->apps, &app_key);
1900         if (!app_lookup) {
1901                 STC_LOGD("app_key not found");
1902                 return STC_ERROR_FAIL;
1903         }
1904
1905         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1906         if (!proc_lookup) {
1907                 STC_LOGD("proc_key not found");
1908                 return STC_ERROR_FAIL;
1909         }
1910
1911         if (proc_lookup->ground != ground)
1912                 proc_lookup->ground = ground;
1913
1914         if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1915                 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1916         else
1917                 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1918
1919         return ret;
1920 }
1921 //LCOV_EXCL_STOP
1922
1923 void stc_monitor_update_rstn_by_default_connection(void *data)
1924 {
1925         static default_connection_s old_connection;
1926         default_connection_s *new_connection = (default_connection_s *)data;
1927
1928         if (old_connection.path != NULL) {
1929                 //LCOV_EXCL_START
1930                 if (g_system->apps)
1931                         g_tree_foreach(g_system->apps,
1932                                        __remove_application_monitor,
1933                                        (gpointer)&old_connection);
1934
1935                 if (g_system->rstns)
1936                         g_tree_foreach(g_system->rstns,
1937                                        __remove_restriction,
1938                                        (gpointer)&old_connection);
1939
1940                 iptables_flush_chains();
1941                 //LCOV_EXCL_STOP
1942         }
1943
1944         FREE(old_connection.path);
1945         FREE(old_connection.ifname);
1946         FREE(old_connection.tether_iface.ifname);
1947         old_connection.type = 0;
1948         old_connection.roaming = 0;
1949         old_connection.tether_state = FALSE;
1950         old_connection.tether_iface.type = 0;
1951
1952         if (new_connection != NULL && new_connection->path != NULL) {
1953                 if (g_system->apps)
1954                         g_tree_foreach(g_system->apps,
1955                                        __add_application_monitor,
1956                                        (gpointer)new_connection);
1957
1958                 if (g_system->rstns)
1959                         g_tree_foreach(g_system->rstns, __add_restriction,
1960                                        NULL);
1961
1962                 old_connection.path = g_strdup(new_connection->path);
1963                 old_connection.ifname = g_strdup(new_connection->ifname);
1964                 old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
1965                 old_connection.type = new_connection->type;
1966                 old_connection.roaming = new_connection->roaming;
1967                 old_connection.tether_state = new_connection->tether_state;
1968                 old_connection.tether_iface.type = new_connection->tether_iface.type;
1969         }
1970 }
1971
1972 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1973 {
1974         stc_error_e ret;
1975
1976         stc_rstn_key_s key;
1977         stc_rstn_value_s value;
1978
1979         memset(&key, 0, sizeof(stc_rstn_key_s));
1980         memset(&value, 0, sizeof(stc_rstn_value_s));
1981
1982         key.app_id = g_strdup(info->app_id);
1983         key.ifname = g_strdup(info->ifname);
1984         key.subscriber_id = g_strdup(info->subscriber_id);
1985         key.iftype = info->iftype;
1986         key.roaming = info->roaming;
1987
1988         value.rstn_type = info->rstn_type;
1989         value.rstn_state = STC_RSTN_STATE_UNKNOWN;
1990         value.restriction_id = info->restriction_id;
1991
1992         if (info->app_id)
1993                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1994         else
1995                 value.classid = STC_UNKNOWN_CLASSID;
1996
1997         if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1998                 __set_background_state(TRUE); //LCOV_EXCL_LINE
1999                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2000                 __process_update_background(); //LCOV_EXCL_LINE
2001         }
2002
2003         value.data_limit = info->data_limit;
2004         value.data_warn_limit = info->data_warn_limit;
2005
2006         ret = __rstn_tree_add(&key, &value, TRUE);
2007
2008         FREE(key.app_id);
2009         FREE(key.ifname);
2010         FREE(key.subscriber_id);
2011         return ret;
2012 }
2013
2014 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
2015 {
2016         stc_error_e ret;
2017
2018         stc_rstn_key_s key = {
2019                 .app_id = g_strdup(info->app_id),
2020                 .ifname = g_strdup(info->ifname),
2021                 .subscriber_id = g_strdup(info->subscriber_id),
2022                 .iftype = info->iftype,
2023                 .roaming = info->roaming,
2024         };
2025
2026         if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
2027                 __set_background_state(FALSE); //LCOV_EXCL_LINE
2028                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2029                 __process_update_background(); //LCOV_EXCL_LINE
2030         }
2031
2032         ret = __rstn_tree_remove(&key);
2033
2034         FREE(key.app_id);
2035         FREE(key.ifname);
2036         FREE(key.subscriber_id);
2037         return ret;
2038 }
2039
2040 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
2041 {
2042         return stc_plugin_check_exception_by_cmdline(cmdline);
2043 }
2044
2045 int stc_monitor_get_counter_socket(void)
2046 {
2047         return g_system->contr_sock;
2048 }