Separately identify restrictions by rst_type
[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
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         ret = key_a->roaming - key_b->roaming;
261         if (ret != 0)
262                 return ret;
263
264         return 0;
265 }
266
267 static void __rstns_tree_value_free(gpointer data)
268 {
269         stc_rstn_value_s *value = (stc_rstn_value_s *)data;
270
271         FREE(value);
272 }
273
274 static void __rstns_tree_key_free(gpointer data)
275 {
276         stc_rstn_key_s *key = (stc_rstn_key_s *)data;
277
278         FREE(key->app_id);
279         FREE(key->ifname);
280         FREE(key->subscriber_id);
281         FREE(key);
282 }
283
284 //LCOV_EXCL_START
285 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
286                                                gpointer data)
287 {
288         stc_process_key_s *proc_key = (stc_process_key_s *)key;
289         stc_process_value_s *proc_value = (stc_process_value_s *)value;
290
291         STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
292                  proc_key->pid, proc_value->ground);
293         return FALSE;
294 }
295
296 static void __processes_tree_printall(GTree *processes)
297 {
298         g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
299 }
300
301 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
302                                           gpointer data)
303 {
304         stc_app_key_s *app_key = (stc_app_key_s *)key;
305         stc_app_value_s *app_value = (stc_app_value_s *)value;
306
307         STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
308                  "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
309                  " counter [ in (%lld), out (%lld)]",
310                  app_key->pkg_id, app_key->app_id,
311                  app_value->type, app_value->classid,
312                  app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
313
314         __processes_tree_printall(app_value->processes);
315         return FALSE;
316 }
317
318 static void __apps_tree_printall(void)
319 {
320         g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
321 }
322 //LCOV_EXCL_STOP
323
324 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
325                                                gpointer data)
326 {
327         remove_pid_context_s *context = (remove_pid_context_s *)data;
328         stc_app_value_s *app_value = (stc_app_value_s *)value;
329
330         if (!g_tree_remove(app_value->processes, context->proc_key)) {
331                 if (STC_DEBUG_LOG)
332                         STC_LOGD("key not found"); //LCOV_EXCL_LINE
333                 return FALSE;
334         }
335
336         context->entry_removed = TRUE;
337         context->app_key = (stc_app_key_s *)key;
338
339         return TRUE;
340 }
341
342 static stc_app_value_s * __application_lookup(GTree *apps,
343                                               const stc_app_key_s *key)
344 {
345         stc_app_value_s *lookup;
346
347         ret_value_msg_if(apps == NULL, NULL, "apps is null!");
348
349         lookup = g_tree_lookup(apps, key);
350
351         return lookup;
352 }
353
354 static stc_process_value_s * __process_lookup(GTree *processes,
355                                               const stc_process_key_s *key)
356 {
357         stc_process_value_s *lookup;
358
359         ret_value_msg_if(processes == NULL, NULL, "processes is null!");
360
361         lookup = g_tree_lookup(processes, key);
362
363         return lookup;
364 }
365
366 //LCOV_EXCL_START
367 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
368                                              gpointer data)
369 {
370         guint *pid_count = (guint *)data;
371         (*pid_count)++;
372         return TRUE;
373 }
374 //LCOV_EXCL_STOP
375
376 static gboolean __add_application_monitor(gpointer key, gpointer value,
377                                           gpointer data)
378 {
379         stc_app_value_s *app_value = (stc_app_value_s *)value;
380         default_connection_s *connection = (default_connection_s *)data;
381         stc_s *stc = stc_get_manager();
382
383         if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
384             app_value->classid == STC_TOTAL_WIFI_CLASSID ||
385             app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
386                 return FALSE;
387
388         if (stc && connection && connection->ifname) {
389                 struct nfacct_rule counter;
390
391                 if (!stc->carg) {
392                         stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
393                         if (stc->carg == NULL) //LCOV_EXCL_LINE
394                                 return FALSE; //LCOV_EXCL_LINE
395
396                         stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
397                 }
398
399                 memset(&counter, 0, sizeof(struct nfacct_rule));
400
401                 counter.carg = stc->carg;
402                 counter.classid = app_value->classid;
403                 counter.intend = NFACCT_COUNTER;
404                 counter.iftype = connection->type;
405                 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
406
407                 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
408                         __add_iptables_in(&counter);
409                         __add_iptables_out(&counter);
410                 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
411                         __add_ip6tables_in(&counter);
412                         __add_ip6tables_out(&counter);
413                 } else {
414                         __add_iptables_in(&counter);
415                         __add_iptables_out(&counter);
416                         __add_ip6tables_in(&counter);
417                         __add_ip6tables_out(&counter);
418                 }
419         }
420
421         return FALSE;
422 }
423
424 static gboolean __remove_application_monitor(gpointer key, gpointer value,
425                                              gpointer data)
426 {
427         stc_app_value_s *app_value = (stc_app_value_s *)value;
428         default_connection_s *connection = (default_connection_s *)data;
429         stc_s *stc = stc_get_manager();
430
431         if (stc && connection && connection->ifname) {
432                 struct nfacct_rule counter;
433
434                 if (!stc->carg) {
435                         stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
436                         if (stc->carg == NULL) //LCOV_EXCL_LINE
437                                 return FALSE; //LCOV_EXCL_LINE
438
439                         stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
440                 }
441
442                 memset(&counter, 0, sizeof(struct nfacct_rule));
443
444                 counter.carg = stc->carg;
445                 counter.classid = app_value->classid;
446                 counter.intend = NFACCT_COUNTER;
447                 counter.iftype = connection->type;
448                 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
449
450                 __del_iptables_in(&counter);
451                 __del_iptables_out(&counter);
452                 __del_ip6tables_in(&counter);
453                 __del_ip6tables_out(&counter);
454         }
455
456         return FALSE;
457 }
458
459 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
460 {
461         STC_LOGI("rstn info => rstn_id [%llu], "
462                  "app_id [%s], classid [%lu], ifname [%s], "
463                  "iftype [%d], rst_state [%d], "
464                  "limit [ (%lld) bytes], "
465                  "warn_limit [ (%lld) bytes], "
466                  "counter [ (%lld) bytes], "
467                  "roaming [%d], subscriber_id [%s]",
468                  rstn_value->restriction_id,
469                  rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
470                  rstn_key->iftype, rstn_value->rst_state,
471                  rstn_value->data_limit,
472                  rstn_value->data_warn_limit,
473                  rstn_value->data_counter,
474                  rstn_key->roaming, rstn_key->subscriber_id);
475 }
476
477 static void __process_restriction(enum traffic_restriction_type rst_type,
478                                   stc_rstn_key_s *rstn_key,
479                                   stc_rstn_value_s *rstn_value, void *data)
480 {
481         int64_t effective_data_limit, effective_data_warn_limit;
482         default_connection_s *old_connection = (default_connection_s *)data;
483         default_connection_s *connection = NULL;
484
485         if (old_connection != NULL)
486                 connection = old_connection;
487         else
488                 connection = stc_get_default_connection();
489
490         /* no default ifname */
491         if (connection->ifname == NULL)
492                 return;
493
494         /* rstn not applicable for this interface */
495         if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
496             g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
497                 return;
498
499         /* classid is invalid */
500         if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
501                 return;
502
503         effective_data_limit = rstn_value->data_limit;
504         effective_data_warn_limit = rstn_value->data_warn_limit;
505
506         if (rst_type == RST_SET) {
507                 /* TODO: Change this to runtime memory */
508                 table_counters_info info;
509
510                 memset(&info, 0, sizeof(table_counters_info));
511                 table_counters_get(rstn_value->restriction_id, &info);
512
513                 effective_data_limit -= info.data_counter;
514                 effective_data_warn_limit -= info.data_counter;
515
516                 if (effective_data_limit < 0) {
517                         effective_data_limit = 0; //LCOV_EXCL_LINE
518                         rstn_value->data_limit_reached = TRUE; //LCOV_EXCL_LINE
519                 }
520
521                 if (effective_data_warn_limit < 0)
522                         effective_data_warn_limit = 0; //LCOV_EXCL_LINE
523
524                 STC_LOGD("datausage [%lld] bytes", info.data_counter);
525         }
526
527         STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
528                  "effective_data_warn_limit [%lld] bytes",
529                  rstn_value->restriction_id, effective_data_limit,
530                  effective_data_warn_limit);
531
532         switch (rst_type) {
533         case RST_SET:
534                 if (effective_data_limit <= 0) {
535                         char *default_ifname = stc_default_connection_get_ifname();
536                         struct nfacct_rule counter;
537                         stc_s *stc = stc_get_manager();
538                         if (!stc) {
539                                 g_free(default_ifname); //LCOV_EXCL_LINE
540                                 return; //LCOV_EXCL_LINE
541                         }
542
543                         if (!stc->carg) {
544                                 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
545                                 if (stc->carg == NULL) { //LCOV_EXCL_LINE
546                                         g_free(default_ifname); //LCOV_EXCL_LINE
547                                         return; //LCOV_EXCL_LINE
548                                 }
549
550                                 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
551                         }
552
553                         counter.carg = stc->carg;
554                         counter.classid = rstn_value->classid;
555                         counter.intend = NFACCT_BLOCK;
556                         counter.iftype = rstn_key->iftype;
557                         g_strlcpy(counter.ifname, default_ifname,
558                                   MAX_IFACE_LENGTH);
559                         g_free(default_ifname);
560
561                         /* iptables rule */
562                         __add_iptables_in(&counter);
563                         __add_iptables_out(&counter);
564
565                         /* ip6tables rule */
566                         __add_ip6tables_in(&counter);
567                         __add_ip6tables_out(&counter);
568                 }
569
570                 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
571                 rstn_value->data_limit_reached = FALSE;
572                 break;
573         case RST_EXCLUDE:
574                 ;//Do Nothing
575                 break;
576         case RST_UNSET:
577                 {
578                         char *default_ifname = stc_default_connection_get_ifname();
579                         struct nfacct_rule counter;
580                         stc_s *stc = stc_get_manager();
581                         if (!stc) {
582                                 g_free(default_ifname); //LCOV_EXCL_LINE
583                                 return; //LCOV_EXCL_LINE
584                         }
585
586                         if (!stc->carg) {
587                                 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
588                                 if (stc->carg == NULL) { //LCOV_EXCL_LINE
589                                         g_free(default_ifname); //LCOV_EXCL_LINE
590                                         return; //LCOV_EXCL_LINE
591                                 }
592
593                                 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
594                         }
595
596                         counter.carg = stc->carg;
597                         counter.classid = rstn_value->classid;
598                         counter.intend = NFACCT_BLOCK;
599                         counter.iftype = rstn_key->iftype;
600                         g_strlcpy(counter.ifname, default_ifname,
601                                   MAX_IFACE_LENGTH);
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_plugin_appstatus_send_warn_message("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_plugin_appstatus_send_restriction_message("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
1216 //LCOV_EXCL_START
1217 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1218                                           gpointer data)
1219 {
1220         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1221         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1222
1223         __print_rstn(rstn_key, rstn_value);
1224         return FALSE;
1225 }
1226
1227 static void __rstn_tree_printall(void)
1228 {
1229         g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1230 }
1231 //LCOV_EXCL_END
1232
1233 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1234                                         const stc_rstn_key_s *key)
1235 {
1236         stc_rstn_value_s *lookup;
1237
1238         ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1239
1240         lookup = g_tree_lookup(rstns_tree, key);
1241
1242         return lookup;
1243 }
1244
1245 static gboolean __remove_restriction(gpointer key, gpointer value,
1246                                      gpointer data)
1247 {
1248         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1249         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1250
1251         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1252         __print_rstn(rstn_key, rstn_value);
1253         return FALSE;
1254 }
1255
1256 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1257                                         gpointer data)
1258 {
1259         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1260         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1261
1262         /* rstn rule is activated */
1263         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1264                 return FALSE;
1265
1266         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1267                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1268         else
1269                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1270
1271         __print_rstn(rstn_key, rstn_value);
1272
1273         return FALSE;
1274 }
1275
1276 //LCOV_EXCL_START
1277 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1278 {
1279         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1280         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1281
1282         /* rstn rule is activated */
1283         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1284                 return FALSE;
1285
1286         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1287                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1288         else
1289                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1290
1291         return FALSE;
1292 }
1293 //LCOV_EXCL_STOP
1294
1295 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1296 {
1297         stc_rstn_value_s *lookup_value;
1298
1299         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1300
1301         lookup_value = __rstn_lookup(g_system->rstns, key);
1302         if (!lookup_value) {
1303                 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1304                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1305         }
1306
1307         __remove_restriction(key, lookup_value, NULL);
1308
1309         /* remove counter also */
1310         table_counters_delete(lookup_value->restriction_id);
1311
1312         if (!g_tree_remove(g_system->rstns, key)) {
1313                 STC_LOGD("key not found"); //LCOV_EXCL_LINE
1314                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1315         }
1316
1317         return STC_ERROR_NONE;
1318 }
1319
1320 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1321                                    stc_rstn_value_s *value, gboolean debug)
1322 {
1323         stc_rstn_value_s *rstn_value;
1324
1325         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1326
1327         rstn_value = __rstn_lookup(g_system->rstns, key);
1328         if (!rstn_value) {
1329                 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1330                 if (!rstn_key) {
1331                         STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
1332                         return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1333                 }
1334
1335                 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1336                 if (!rstn_value) {
1337                         STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
1338                         FREE(rstn_key); //LCOV_EXCL_LINE
1339                         return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1340                 }
1341
1342                 rstn_key->app_id = g_strdup(key->app_id);
1343                 rstn_key->ifname = g_strdup(key->ifname);
1344                 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1345                 rstn_key->iftype = key->iftype;
1346                 rstn_key->roaming = key->roaming;
1347
1348                 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1349         }
1350
1351         rstn_value->restriction_id = value->restriction_id;
1352         rstn_value->rst_state = value->rst_state;
1353         rstn_value->rst_type = value->rst_type;
1354         rstn_value->classid = value->classid;
1355         rstn_value->data_limit = value->data_limit;
1356         rstn_value->data_warn_limit = value->data_warn_limit;
1357         rstn_value->data_counter = 0;
1358         rstn_value->warn_limit_crossed_notified = FALSE;
1359         rstn_value->rstn_limit_crossed_notified = FALSE;
1360
1361         if (debug == TRUE)
1362                 __add_restriction_debug(key, rstn_value, NULL);
1363         else
1364                 __add_restriction(key, rstn_value, NULL);
1365
1366         return STC_ERROR_NONE;
1367 }
1368
1369 //LCOV_EXCL_START
1370 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1371                                             void *user_data)
1372 {
1373         stc_cb_ret_e ret = STC_CONTINUE;
1374
1375         stc_rstn_key_s key;
1376         stc_rstn_value_s value;
1377
1378         memset(&key, 0, sizeof(stc_rstn_key_s));
1379         memset(&value, 0, sizeof(stc_rstn_value_s));
1380
1381         key.app_id = g_strdup(info->app_id);
1382         key.ifname = g_strdup(info->ifname);
1383         key.subscriber_id = g_strdup(info->subscriber_id);
1384         key.iftype = info->iftype;
1385         key.roaming = info->roaming;
1386
1387         value.rst_type = info->rst_type;
1388         value.rst_state = STC_RESTRICTION_UNKNOWN;
1389         value.restriction_id = info->restriction_id;
1390
1391         if (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
1412         if (STC_DEBUG_LOG)
1413                 __rstn_tree_printall();
1414 }
1415
1416 static gboolean __add_rstn_foreach_application(gpointer key,
1417                                                gpointer value,
1418                                                gpointer data)
1419 {
1420         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1421         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1422         gchar *app_id = (gchar *)data;
1423
1424         /* rstn rule is not for applications */
1425         if (rstn_key->app_id == NULL)
1426                 goto out;
1427
1428         /* rstn rule is not for this application */
1429         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1430                 goto out;
1431
1432         /* rstn rule is already applied */
1433         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1434                 goto out;
1435
1436         /* add restriction to system */
1437         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1438                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
1439         else
1440                 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
1441
1442         __print_rstn(rstn_key, rstn_value);
1443 out:
1444         return FALSE;
1445 }
1446 //LCOV_EXCL_STOP
1447
1448 static void __add_rstns_for_application(gchar *app_id)
1449 {
1450         g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1451                        app_id);
1452 }
1453
1454 static void __add_application_by_interface(const char *app_id)
1455 {
1456         stc_app_key_s app_key;
1457         stc_app_value_s app_value;
1458
1459         if (app_id == NULL)
1460                 return; //LCOV_EXCL_LINE
1461
1462         memset(&app_key, 0, sizeof(stc_app_key_s));
1463         memset(&app_value, 0, sizeof(stc_app_value_s));
1464
1465         app_key.pkg_id = g_strdup(app_id);
1466         app_key.app_id = g_strdup(app_id);
1467
1468         app_value.type = STC_APP_TYPE_NONE;
1469         app_value.processes = NULL;
1470         app_value.counter.in_bytes = 0;
1471         app_value.counter.out_bytes = 0;
1472
1473         stc_monitor_application_add(app_key, app_value);
1474
1475         FREE(app_key.pkg_id);
1476         FREE(app_key.app_id);
1477 }
1478
1479 static int __vconf_get_int(const char *key, int *value)
1480 {
1481         int ret = 0;
1482
1483         ret = vconf_get_int(key, value);
1484         if (ret != VCONF_OK) {
1485                 STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
1486                 return -1; //LCOV_EXCL_LINE
1487         }
1488
1489         return 0;
1490 }
1491
1492 //LCOV_EXCL_START
1493 static int __vconf_set_int(const char *key, int value)
1494 {
1495         int ret = 0;
1496
1497         ret = vconf_set_int(key, value);
1498         if (ret != VCONF_OK) {
1499                 STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
1500                 return -1; //LCOV_EXCL_LINE
1501         }
1502
1503         return 0;
1504 }
1505
1506 static guint __get_background_state(void)
1507 {
1508         return g_system->background_state;;
1509 }
1510
1511 static void __set_background_state(guint state)
1512 {
1513         g_system->background_state = state;
1514 }
1515
1516 static gboolean __processes_tree_foreach_background(gpointer key,
1517                                                     gpointer value,
1518                                                     gpointer data)
1519 {
1520         stc_process_key_s *proc_key = (stc_process_key_s *)key;
1521         stc_app_key_s *app_key = (stc_app_key_s *)data;
1522
1523         if (g_system->background_state)
1524                 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1525         else
1526                 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1527
1528         return FALSE;
1529 }
1530
1531 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1532                                         gpointer data)
1533 {
1534         stc_app_key_s *app_key = (stc_app_key_s *)key;
1535         stc_app_value_s *app_value = (stc_app_value_s *)value;
1536
1537         if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1538                 g_tree_foreach(app_value->processes,
1539                                __processes_tree_foreach_background, app_key);
1540
1541         return FALSE;
1542 }
1543
1544 static stc_error_e __process_update_background(void)
1545 {
1546         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1547
1548         g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1549
1550         return STC_ERROR_NONE;
1551 }
1552 //LCOV_EXCL_STOP
1553
1554 static void __fill_exceptions_list(void)
1555 {
1556         stc_plugin_fill_exception_list();
1557 }
1558
1559 stc_error_e stc_monitor_init(void)
1560 {
1561         stc_system_s *system = MALLOC0(stc_system_s, 1);
1562         GIOChannel *gio = NULL;
1563
1564         ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1565
1566         /* initializing current classid */
1567         init_current_classid();
1568
1569         /* initializing cgroups */
1570         cgroup_init();
1571
1572         /* creating monitored application tree */
1573         system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1574                                        __apps_tree_key_free,
1575                                        __apps_tree_value_free);
1576
1577         system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1578                                         __rstns_tree_key_free,
1579                                         __rstns_tree_value_free);
1580
1581         /* create netlink socket for updating kernel counters */
1582         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1583         if (system->contr_sock < 0) {
1584                 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
1585                 FREE(system); //LCOV_EXCL_LINE
1586                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1587         }
1588
1589         gio = g_io_channel_unix_new(system->contr_sock);
1590         system->contr_gsource_id =
1591                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1592                                (GIOFunc) __process_contr_reply,
1593                                NULL);
1594         g_io_channel_unref(gio);
1595
1596         g_system = system;
1597
1598         __add_application_by_interface(STC_TOTAL_DATACALL);
1599         __add_application_by_interface(STC_TOTAL_WIFI);
1600         __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1601         __add_application_by_interface(STC_TOTAL_IPV4);
1602         __add_application_by_interface(STC_TOTAL_IPV6);
1603
1604         /* creating restriction rules tree */
1605         __update_contr_cb(NULL);
1606
1607         /* registering periodic kernel counters update callback */
1608         g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1609                                                          __update_contr_cb,
1610                                                          NULL);
1611         if (g_system->contr_timer_id == 0) {
1612                 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
1613                 __close_contr_sock(g_system); //LCOV_EXCL_LINE
1614                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1615         }
1616
1617         __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1618                         (int *)&g_system->background_state);
1619
1620         __fill_exceptions_list();
1621         __fill_restritions_list();
1622
1623         return STC_ERROR_NONE;
1624 }
1625
1626 stc_error_e stc_monitor_deinit(void)
1627 {
1628         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1629
1630         /* close netlink socket for updating kernel counters */
1631         __close_contr_sock(g_system);
1632
1633         /* remove kernel counters update timer */
1634         if (g_system->contr_timer_id > 0) {
1635                 g_source_remove(g_system->contr_timer_id);
1636                 g_system->contr_timer_id = 0;
1637         }
1638
1639         /* destroy monitored application tree */
1640         g_tree_destroy(g_system->apps);
1641         g_system->apps = NULL;
1642
1643         /* destroy restriction rules tree */
1644         g_tree_destroy(g_system->rstns);
1645         g_system->rstns = NULL;
1646
1647         FREE(g_system);
1648
1649         return STC_ERROR_NONE;
1650 }
1651
1652 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1653                                         const stc_app_value_s app_value)
1654 {
1655         stc_error_e ret = STC_ERROR_NONE;
1656         stc_app_key_s *key;
1657         stc_app_value_s *value;
1658         stc_app_value_s *lookup;
1659
1660         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1661
1662         lookup = __application_lookup(g_system->apps, &app_key);
1663         if (lookup) {
1664                 STC_LOGD("app_key already present"); //LCOV_EXCL_LINE
1665                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1666         }
1667
1668         key = MALLOC0(stc_app_key_s, 1);
1669         if (!key) {
1670                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1671                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1672         }
1673
1674         value = MALLOC0(stc_app_value_s, 1);
1675         if (!value) {
1676                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1677                 FREE(key); //LCOV_EXCL_LINE
1678                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1679         }
1680
1681         key->app_id = g_strdup(app_key.app_id);
1682         key->pkg_id = g_strdup(app_key.pkg_id);
1683
1684         value->type = app_value.type;
1685         value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1686         value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1687
1688         value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1689                                            __processes_tree_key_free,
1690                                            __processes_tree_value_free);
1691
1692         /* create cgroup and update classid */
1693         value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1694
1695         g_tree_insert(g_system->apps, key, value);
1696
1697         /* add nfacct rule for this classid */
1698         __add_application_monitor(key, value, stc_get_default_connection());
1699         __add_rstns_for_application(app_key.app_id);
1700
1701         return ret;
1702 }
1703
1704 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1705                                     const stc_process_key_s proc_key,
1706                                     const stc_process_value_s proc_value)
1707 {
1708         stc_error_e ret = STC_ERROR_NONE;
1709         stc_app_value_s *app_lookup;
1710         stc_process_key_s *key;
1711         stc_process_value_s *value;
1712         stc_process_value_s *proc_lookup;
1713
1714         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1715
1716         app_lookup = __application_lookup(g_system->apps, &app_key);
1717         if (!app_lookup) {
1718                 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
1719                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1720         }
1721
1722         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1723         if (proc_lookup) {
1724                 STC_LOGD("proc_key already present"); //LCOV_EXCL_LINE
1725                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1726         }
1727
1728         key = MALLOC0(stc_process_key_s, 1);
1729         if (!key) {
1730                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1731                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1732         }
1733
1734         value = MALLOC0(stc_process_value_s, 1);
1735         if (!value) {
1736                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1737                 FREE(key); //LCOV_EXCL_LINE
1738                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1739         }
1740
1741         key->pid = proc_key.pid;
1742
1743         value->ground = proc_value.ground;
1744
1745         g_tree_insert(app_lookup->processes, key, value);
1746
1747         /* add pid to application cgroup */
1748         place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1749
1750         if (STC_DEBUG_LOG)
1751                 __apps_tree_printall(); //LCOV_EXCL_LINE
1752
1753         return ret;
1754 }
1755
1756 API stc_error_e stc_monitor_process_remove(pid_t pid)
1757 {
1758         stc_error_e ret = STC_ERROR_NONE;
1759         stc_process_key_s proc_key = {
1760                 .pid = pid
1761         };
1762
1763         remove_pid_context_s context = {
1764                 .app_key = NULL,
1765                 .proc_key = &proc_key,
1766                 .entry_removed = FALSE,
1767         };
1768
1769         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1770
1771         g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1772                        &context);
1773
1774         if (context.entry_removed)
1775                 __application_remove_if_empty(context.app_key);
1776
1777         if (STC_DEBUG_LOG)
1778                 __apps_tree_printall(); //LCOV_EXCL_LINE
1779
1780         return ret;
1781 }
1782
1783 //LCOV_EXCL_START
1784 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1785                                               const stc_process_key_s proc_key,
1786                                               stc_app_state_e ground)
1787 {
1788         stc_error_e ret = STC_ERROR_NONE;
1789         stc_app_value_s *app_lookup;
1790         stc_process_value_s *proc_lookup;
1791
1792         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1793
1794         app_lookup = __application_lookup(g_system->apps, &app_key);
1795         if (!app_lookup) {
1796                 STC_LOGD("app_key not found");
1797                 return STC_ERROR_FAIL;
1798         }
1799
1800         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1801         if (!proc_lookup) {
1802                 STC_LOGD("proc_key not found");
1803                 return STC_ERROR_FAIL;
1804         }
1805
1806         if (proc_lookup->ground != ground)
1807                 proc_lookup->ground = ground;
1808
1809         if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1810                 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1811         else
1812                 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1813
1814         return ret;
1815 }
1816 //LCOV_EXCL_STOP
1817
1818 void stc_monitor_update_rstn_by_default_connection(void *data)
1819 {
1820         static default_connection_s old_connection;
1821         default_connection_s *new_connection = (default_connection_s *)data;
1822
1823         if (old_connection.path != NULL) {
1824                 //LCOV_EXCL_START
1825                 if (g_system->apps)
1826                         g_tree_foreach(g_system->apps,
1827                                        __remove_application_monitor,
1828                                        (gpointer)&old_connection);
1829
1830                 if (g_system->rstns)
1831                         g_tree_foreach(g_system->rstns,
1832                                        __remove_restriction,
1833                                        (gpointer)&old_connection);
1834
1835                 iptables_flush_chains();
1836                 //LCOV_EXCL_STOP
1837         }
1838
1839         FREE(old_connection.path);
1840         FREE(old_connection.ifname);
1841         old_connection.type = 0;
1842         old_connection.roaming = 0;
1843
1844         if (new_connection != NULL && new_connection->path != NULL) {
1845                 if (g_system->apps)
1846                         g_tree_foreach(g_system->apps,
1847                                        __add_application_monitor,
1848                                        (gpointer)new_connection);
1849
1850                 if (g_system->rstns)
1851                         g_tree_foreach(g_system->rstns, __add_restriction,
1852                                        NULL);
1853
1854                 old_connection.path = g_strdup(new_connection->path);
1855                 old_connection.ifname = g_strdup(new_connection->ifname);
1856                 old_connection.type = new_connection->type;
1857                 old_connection.roaming = new_connection->roaming;
1858         }
1859 }
1860
1861 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1862 {
1863         stc_error_e ret;
1864
1865         stc_rstn_key_s key;
1866         stc_rstn_value_s value;
1867
1868         memset(&key, 0, sizeof(stc_rstn_key_s));
1869         memset(&value, 0, sizeof(stc_rstn_value_s));
1870
1871         key.app_id = g_strdup(info->app_id);
1872         key.ifname = g_strdup(info->ifname);
1873         key.subscriber_id = g_strdup(info->subscriber_id);
1874         key.iftype = info->iftype;
1875         key.roaming = info->roaming;
1876
1877         value.rst_type = info->rst_type;
1878         value.rst_state = STC_RESTRICTION_UNKNOWN;
1879         value.restriction_id = info->restriction_id;
1880
1881         if (info->app_id)
1882                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1883         else
1884                 value.classid = STC_UNKNOWN_CLASSID;
1885
1886         if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1887                 __set_background_state(TRUE); //LCOV_EXCL_LINE
1888                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1889                 __process_update_background(); //LCOV_EXCL_LINE
1890         }
1891
1892         value.data_limit = info->data_limit;
1893         value.data_warn_limit = info->data_warn_limit;
1894
1895         ret = __rstn_tree_add(&key, &value, TRUE);
1896
1897         FREE(key.app_id);
1898         FREE(key.ifname);
1899         FREE(key.subscriber_id);
1900         return ret;
1901 }
1902
1903 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1904 {
1905         stc_error_e ret;
1906
1907         stc_rstn_key_s key = {
1908                 .app_id = g_strdup(info->app_id),
1909                 .ifname = g_strdup(info->ifname),
1910                 .subscriber_id = g_strdup(info->subscriber_id),
1911                 .iftype = info->iftype,
1912                 .roaming = info->roaming,
1913         };
1914
1915         if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
1916                 __set_background_state(FALSE); //LCOV_EXCL_LINE
1917                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1918                 __process_update_background(); //LCOV_EXCL_LINE
1919         }
1920
1921         ret = __rstn_tree_remove(&key);
1922
1923         FREE(key.app_id);
1924         FREE(key.ifname);
1925         FREE(key.subscriber_id);
1926         return ret;
1927 }
1928
1929 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
1930 {
1931         return stc_plugin_check_exception_by_cmdline(cmdline);
1932 }
1933
1934 int stc_monitor_get_counter_socket(void)
1935 {
1936         return g_system->contr_sock;
1937 }