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