Moved procfs for app lifecycle to plugin and separate plugins
[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         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                         g_free(default_ifname);
556
557                         /* iptables rule */
558                         __add_iptables_in(&counter);
559                         __add_iptables_out(&counter);
560
561                         /* ip6tables rule */
562                         __add_ip6tables_in(&counter);
563                         __add_ip6tables_out(&counter);
564                 }
565
566                 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
567                 rstn_value->data_limit_reached = FALSE;
568                 break;
569         case RST_EXCLUDE:
570                 ;//Do Nothing
571                 break;
572         case RST_UNSET:
573                 {
574                         char *default_ifname = stc_default_connection_get_ifname();
575                         struct nfacct_rule counter;
576                         stc_s *stc = stc_get_manager();
577                         if (!stc) {
578                                 g_free(default_ifname); //LCOV_EXCL_LINE
579                                 return; //LCOV_EXCL_LINE
580                         }
581
582                         if (!stc->carg) {
583                                 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
584                                 if (stc->carg == NULL) { //LCOV_EXCL_LINE
585                                         g_free(default_ifname); //LCOV_EXCL_LINE
586                                         return; //LCOV_EXCL_LINE
587                                 }
588
589                                 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
590                         }
591
592                         counter.carg = stc->carg;
593                         counter.classid = rstn_value->classid;
594                         counter.intend = NFACCT_BLOCK;
595                         counter.iftype = rstn_key->iftype;
596                         g_strlcpy(counter.ifname, default_ifname,
597                                   MAX_IFACE_LENGTH);
598                         g_free(default_ifname);
599
600                         /* iptables rule */
601                         __del_iptables_in(&counter);
602                         __del_iptables_out(&counter);
603
604                         /* ip6tables rule */
605                         __del_ip6tables_in(&counter);
606                         __del_ip6tables_out(&counter);
607
608                         rstn_value->rst_state = STC_RESTRICTION_REMOVED;
609                         rstn_value->data_limit_reached = FALSE;
610                 }
611                 break;
612         default:
613                 ;//Do Nothing
614         }
615 }
616
617 //LCOV_EXCL_START
618 static gboolean __remove_rstns_foreach_application(gpointer key,
619                                                    gpointer value,
620                                                    gpointer data)
621 {
622         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
623         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
624         gchar *app_id = (gchar *)data;
625
626         /* rstn rule is not for applications */
627         if (rstn_key->app_id == NULL)
628                 goto out;
629
630         /* rstn rule is not for this application */
631         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
632                 goto out;
633
634         /* rstn rule is already removed */
635         if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
636                 goto out;
637
638         /* remove restriction from system */
639         __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
640
641         __print_rstn(rstn_key, rstn_value);
642 out:
643         return FALSE;
644 }
645 //LCOV_EXCL_STOP
646
647 static void __remove_rstns_for_application(gchar *app_id)
648 {
649         g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
650                        app_id);
651 }
652
653 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
654 {
655         stc_error_e ret = STC_ERROR_NONE;
656         guint pid_count = 0;
657         stc_app_value_s *lookup;
658
659         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
660
661         lookup = __application_lookup(g_system->apps, app_key);
662         if (!lookup) {
663                 STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
664                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
665         }
666
667         g_tree_foreach(lookup->processes, __processes_tree_check_empty,
668                        &pid_count);
669
670         if (!pid_count) {
671                 /* remove nfacct rule for this classid */
672                 __remove_application_monitor((gpointer) app_key, lookup,
673                                              stc_get_default_connection());
674                 __remove_rstns_for_application(app_key->app_id);
675         }
676
677         if (!g_tree_remove(g_system->apps, app_key)) {
678                 ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
679                 STC_LOGE("key not found"); //LCOV_EXCL_LINE
680         }
681
682         return ret;
683 }
684
685 static stc_error_e __close_contr_sock(stc_system_s *system)
686 {
687         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
688
689         /* close netlink socket for updating kernel counters */
690         if (system->contr_sock != -1) {
691                 close(system->contr_sock);
692                 system->contr_sock = -1;
693         }
694
695         if (system->contr_gsource_id != 0) {
696                 g_source_remove(system->contr_gsource_id);
697                 system->contr_gsource_id = 0;
698         }
699
700         return STC_ERROR_NONE;
701 }
702
703 static gboolean __process_contr_reply(GIOChannel *source,
704                                       GIOCondition condition,
705                                       gpointer user_data);
706
707 //LCOV_EXCL_START
708 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
709 {
710         GIOChannel *gio = NULL;
711         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
712
713         /* close netlink socket for updating kernel counters */
714         if (system->contr_sock != -1) {
715                 close(system->contr_sock);
716                 system->contr_sock = -1;
717         }
718
719         if (system->contr_gsource_id != 0) {
720                 g_source_remove(system->contr_gsource_id);
721                 system->contr_gsource_id = 0;
722         }
723
724         /* create netlink socket for updating kernel counters */
725         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
726         if (system->contr_sock < 0) {
727                 STC_LOGE("failed to open socket");
728                 FREE(system);
729                 return STC_ERROR_FAIL;
730         }
731
732         gio = g_io_channel_unix_new(system->contr_sock);
733         system->contr_gsource_id =
734                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
735                                (GIOFunc) __process_contr_reply,
736                                NULL);
737         g_io_channel_unref(gio);
738
739         return STC_ERROR_NONE;
740 }
741
742 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
743                                       stc_rstn_value_s *rstn_value,
744                                       classid_bytes_context_s *context)
745 {
746         switch (context->counter->iotype) {
747         case NFACCT_COUNTER_IN:
748         case NFACCT_COUNTER_OUT:
749                 rstn_value->data_counter += context->bytes;
750
751                 if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
752                     rstn_value->warn_limit_crossed_notified == FALSE) {
753
754                         gboolean rv;
755                         char iftype[MAX_INT_LENGTH];
756                         char byte[MAX_INT_LENGTH];
757                         stc_s *stc = (stc_s *)stc_get_manager();
758                         ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
759
760                         /* emit signal */
761                         rv = stc_manager_dbus_emit_signal(stc->connection,
762                                                           STC_DBUS_SERVICE_RESTRICTION_PATH,
763                                                           STC_DBUS_INTERFACE_RESTRICTION,
764                                                           "WarnThresholdCrossed",
765                                                           g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
766                         if (rv == TRUE)
767                                 rstn_value->warn_limit_crossed_notified = TRUE;
768
769                         snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
770                         snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
771                         stc_plugin_appstatus_send_warn_message("warn threshold crossed",
772                                                            "warning_noti",
773                                                            rstn_key->app_id,
774                                                            iftype, byte);
775                 }
776
777                 if (rstn_value->data_counter >= rstn_value->data_limit &&
778                     rstn_value->rstn_limit_crossed_notified == FALSE) {
779
780                         gboolean rv;
781                         char iftype[MAX_INT_LENGTH];
782                         char byte[MAX_INT_LENGTH];
783                         stc_s *stc = (stc_s *)stc_get_manager();
784                         ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
785
786                         /* block immediately */
787                         context->counter->intend = NFACCT_BLOCK;
788                         __del_iptables_in(context->counter);
789                         __del_iptables_out(context->counter);
790                         __add_iptables_in(context->counter);
791                         __add_iptables_out(context->counter);
792
793                         __del_ip6tables_in(context->counter);
794                         __del_ip6tables_out(context->counter);
795                         __add_ip6tables_in(context->counter);
796                         __add_ip6tables_out(context->counter);
797                         context->counter->intend = NFACCT_COUNTER;
798
799                         rstn_value->data_limit_reached = TRUE;
800
801                         /* emit signal */
802                         rv = stc_manager_dbus_emit_signal(stc->connection,
803                                                           STC_DBUS_SERVICE_RESTRICTION_PATH,
804                                                           STC_DBUS_INTERFACE_RESTRICTION,
805                                                           "RestrictionThresholdCrossed",
806                                                           g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
807                         if (rv == TRUE)
808                                 rstn_value->rstn_limit_crossed_notified = TRUE;
809
810                         snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
811                         snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
812                         stc_plugin_appstatus_send_restriction_message("restriction threshold crossed",
813                                                                   "restriction_noti", rstn_key->app_id,
814                                                                   iftype, byte);
815                 }
816
817                 g_system->rstns_tree_updated = TRUE;
818                 __print_rstn(rstn_key, rstn_value);
819                 break;
820         default:
821                 STC_LOGE("unknown iotype");
822         }
823
824         return FALSE;
825 }
826
827 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
828                                                 stc_rstn_value_s *rstn_value,
829                                                 classid_bytes_context_s *context)
830 {
831         if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
832              context->counter->iftype == STC_IFACE_DATACALL) ||
833             (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
834              context->counter->iftype == STC_IFACE_WIFI) ||
835             (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
836              context->counter->iftype == STC_IFACE_BLUETOOTH)) {
837                 context->counter->classid = rstn_value->classid;
838                 return __rstn_counter_update(rstn_key, rstn_value, context);
839         }
840
841         return FALSE;
842 }
843
844 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
845                                                       gpointer value,
846                                                       gpointer data)
847 {
848         gboolean rv = FALSE;
849         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
850         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
851         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
852         uint32_t classid;
853
854         if (context->counter->intend != NFACCT_COUNTER)
855                 goto try_next_callback;
856
857         if (rstn_value->data_limit_reached == TRUE) {
858                 context->data_limit_reached = TRUE; //LCOV_EXCL_LINE
859                 goto try_next_callback; //LCOV_EXCL_LINE
860         }
861
862         classid = context->counter->classid;
863         rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
864
865         context->counter->classid = classid;
866         if (rstn_value->classid != context->counter->classid)
867                 goto try_next_callback;
868
869         rv = __rstn_counter_update(rstn_key, rstn_value, context);
870
871 try_next_callback:
872         return rv;
873 }
874 //LCOV_EXCL_STOP
875
876 static gboolean __update_app_statistics(gpointer key, gpointer value,
877                                         gpointer data)
878 {
879         stc_app_key_s *app_key = (stc_app_key_s *)key;
880         stc_app_value_s *app_value = (stc_app_value_s *)value;
881         time_t *touch_time = (time_t *)data;
882         stc_db_classid_iftype_key stat_key;
883         stc_db_app_stats stat;
884         default_connection_s *default_connection = stc_get_default_connection();
885
886         memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
887         memset(&stat, 0 , sizeof(stc_db_app_stats));
888
889         stat_key.classid = app_value->classid;
890         stat_key.iftype = default_connection->type;
891
892         if (STC_IFACE_DATACALL == stat_key.iftype)
893                 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
894         else
895                 stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
896
897         g_strlcpy(stat_key.ifname, default_connection->ifname,
898                   MAX_IFACE_LENGTH);
899
900         stat.app_id = g_strdup(app_key->app_id);
901         stat.snd_count = app_value->counter.out_bytes;
902         stat.rcv_count = app_value->counter.in_bytes;
903         stat.is_roaming = default_connection->roaming;
904
905         if (strstr(stat.app_id, "_BACKGROUND")) {
906                 stat.ground = STC_APP_STATE_BACKGROUND;
907         } else {
908                 if (strstr(stat.app_id, "TOTAL_"))
909                         stat.ground = STC_APP_STATE_UNKNOWN;
910                 else
911                         stat.ground = STC_APP_STATE_FOREGROUND;
912         }
913
914         table_statistics_insert(&stat_key, &stat, *touch_time);
915
916         app_value->counter.out_bytes = 0;
917         app_value->counter.in_bytes = 0;
918
919         FREE(stat.app_id);
920         FREE(stat_key.subscriber_id);
921
922         return FALSE;
923 }
924
925 static gboolean __flush_apps_stats_to_database(gpointer user_data)
926 {
927         time_t current_time = time(0);
928
929         if (g_system->apps_tree_updated == FALSE)
930                 return G_SOURCE_REMOVE;
931
932         g_system->apps_tree_updated = FALSE;
933
934         if (g_system->apps)
935                 g_tree_foreach(g_system->apps,
936                                __update_app_statistics,
937                                &current_time);
938
939         STC_LOGI("Flushed app stats to database");
940         return G_SOURCE_REMOVE;
941 }
942
943 //LCOV_EXCL_START
944 static gboolean __update_counter_statistics(gpointer key, gpointer value,
945                                             gpointer data)
946 {
947         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
948         table_counters_info info = {
949                 .restriction_id = rstn_value->restriction_id,
950                 .data_counter = rstn_value->data_counter
951         };
952
953         table_counters_update_counters(&info);
954
955         return FALSE;
956 }
957
958 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
959 {
960         time_t current_time = time(0);
961
962         if (g_system->rstns_tree_updated == FALSE)
963                 return G_SOURCE_REMOVE;
964
965         g_system->rstns_tree_updated = FALSE;
966
967         if (g_system->rstns)
968                 g_tree_foreach(g_system->rstns,
969                                __update_counter_statistics,
970                                &current_time);
971
972         STC_LOGI("Flushed rstns counters to database");
973         return G_SOURCE_REMOVE;
974 }
975 //LCOV_EXCL_STOP
976
977 static void __app_counter_update(stc_app_key_s *app_key,
978                                  stc_app_value_s *app_value,
979                                  classid_bytes_context_s *context)
980 {
981         switch (context->counter->iotype) {
982         case NFACCT_COUNTER_IN:
983                 app_value->data_usage.in_bytes += context->bytes;
984                 app_value->counter.in_bytes = context->bytes;
985                 g_system->apps_tree_updated = TRUE;
986
987                 if (STC_DEBUG_LOG)
988                         __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
989                 break;
990         case NFACCT_COUNTER_OUT:
991                 app_value->data_usage.out_bytes += context->bytes;
992                 app_value->counter.out_bytes = context->bytes;
993                 g_system->apps_tree_updated = TRUE;
994
995                 if (STC_DEBUG_LOG)
996                         __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
997                 break;
998         default:
999                 STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
1000         }
1001 }
1002
1003 static void __interface_counter_update(stc_app_key_s *app_key,
1004                                        stc_app_value_s *app_value,
1005                                        classid_bytes_context_s *context)
1006 {
1007         if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1008              context->counter->iftype == STC_IFACE_DATACALL) ||
1009             (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1010              context->counter->iftype == STC_IFACE_WIFI) ||
1011             (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1012              context->counter->iftype == STC_IFACE_BLUETOOTH))
1013                 __app_counter_update(app_key, app_value, context);
1014 }
1015
1016
1017 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1018                                                       gpointer value,
1019                                                       gpointer data)
1020 {
1021         stc_app_key_s *app_key = (stc_app_key_s *)key;
1022         stc_app_value_s *app_value = (stc_app_value_s *)value;
1023         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1024
1025         if (context->counter->intend != NFACCT_COUNTER)
1026                 goto try_next_callback;
1027
1028         __interface_counter_update(app_key, app_value, context);
1029
1030         if (app_value->classid != context->counter->classid)
1031                 goto try_next_callback;
1032
1033         __app_counter_update(app_key, app_value, context);
1034
1035 try_next_callback:
1036         return FALSE;
1037 }
1038
1039 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1040                                  struct counter_arg *carg)
1041 {
1042         struct nfacct_rule counter = {
1043                 .carg = carg,
1044                 .name = {0},
1045                 .ifname = {0},
1046                 0
1047         };
1048
1049         classid_bytes_context_s context = {
1050                 .counter = &counter,
1051                 .bytes = bytes,
1052                 .data_limit_reached = FALSE,
1053         };
1054
1055         if (STC_DEBUG_LOG)
1056                 STC_LOGD("cnt_name %s", cnt_name); //LCOV_EXCL_LINE
1057
1058         if (!recreate_counter_by_name(cnt_name, &counter)) {
1059                 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1060                 return; //LCOV_EXCL_LINE
1061         }
1062
1063         STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1064                  context.counter->classid, context.counter->iftype,
1065                  context.counter->iotype, context.counter->intend,
1066                  context.counter->ifname, context.bytes);
1067
1068         if (g_system->rstns)
1069                 g_tree_foreach(g_system->rstns,
1070                                __rstn_counter_update_foreach_classid,
1071                                &context);
1072
1073         if (g_system->apps)
1074                 g_tree_foreach(g_system->apps,
1075                                __apps_counter_update_foreach_classid,
1076                                &context);
1077 }
1078
1079 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1080                            void *user_data)
1081 {
1082         struct counter_arg *carg = user_data;
1083         char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1084         if (carg->initiate) {
1085                 /**
1086                  * TODO: this will be used when daemon starts to update existing
1087                  * counter data if present.
1088                  *
1089                  populate_counters(cnt_name, carg);
1090                  */
1091         } else {
1092                 int64_t *bytes_p =
1093                         (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1094                 int bytes = be64toh(*bytes_p);
1095                 if (bytes) {
1096                         ++carg->serialized_counters;
1097                         __fill_nfacct_result(cnt_name, bytes, carg);
1098                 }
1099         }
1100
1101         return 0;
1102 }
1103
1104 static int __post_fill_counters(void *user_data)
1105 {
1106         struct counter_arg *carg = user_data;
1107
1108         if (carg->initiate)
1109                 carg->initiate = 0;
1110
1111         return 0;
1112 }
1113
1114 static void __process_network_counter(struct genl *ans,
1115                                       struct counter_arg *carg)
1116 {
1117         struct netlink_serialization_params ser_params = {
1118                 .carg = carg,
1119                 .ans = ans,
1120                 .eval_attr = __fill_counters,
1121                 .post_eval_attr = __post_fill_counters,
1122         };
1123
1124         netlink_serialization_command *netlink =
1125                 netlink_create_command(&ser_params);
1126         if (!netlink) {
1127                 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
1128                 return; //LCOV_EXCL_LINE
1129         }
1130
1131         netlink->deserialize_answer(&(netlink->params));
1132 }
1133
1134 static gboolean __process_contr_reply(GIOChannel *source,
1135                                       GIOCondition condition,
1136                                       gpointer user_data)
1137 {
1138         int sock = g_io_channel_unix_get_fd(source);
1139         struct genl *ans;
1140         int ret;
1141         stc_s *stc = stc_get_manager();
1142
1143 #ifdef TIZEN_GTESTS
1144         void __gcov_flush(void);
1145         __gcov_flush();
1146 #endif
1147
1148         if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1149             (condition & G_IO_NVAL)) {
1150                 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1151
1152                 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
1153                          "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
1154                          (condition & G_IO_ERR), (condition & G_IO_HUP),
1155                          (condition & G_IO_NVAL));
1156                 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
1157                 return FALSE; //LCOV_EXCL_LINE
1158         }
1159
1160         ans = MALLOC0(struct genl, 1);
1161         if (ans == NULL) {
1162                 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
1163                 return TRUE; //LCOV_EXCL_LINE
1164         }
1165
1166         if (stc == NULL) {
1167                 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
1168                 goto out; //LCOV_EXCL_LINE
1169         }
1170
1171         ret = read_netlink(sock, ans, sizeof(struct genl));
1172         /* STC_LOGD("Counter data received ret [%d]", ret); */
1173         if (ret == 0)
1174                 goto out;
1175
1176         stc->carg->ans_len = ret;
1177         __process_network_counter(ans, stc->carg);
1178
1179         g_idle_add(__flush_apps_stats_to_database, NULL);
1180         g_idle_add(__flush_rstns_counter_to_database, NULL);
1181 out:
1182
1183         FREE(ans);
1184         return TRUE;
1185 }
1186
1187 static gboolean __update_contr_cb(void *user_data)
1188 {
1189         /* Here we just sent command, answer we receive in another callback */
1190         stc_s *stc = stc_get_manager();
1191         ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1192         if (!stc->carg) {
1193                 stc->carg = MALLOC0(counter_arg_s, 1);
1194                 if (stc->carg == NULL)
1195                         return TRUE;  /* we need to continue the timer */
1196
1197                 stc->carg->sock = stc_monitor_get_counter_socket();
1198         }
1199
1200 #ifdef TIZEN_GTESTS
1201         void __gcov_flush(void);
1202         __gcov_flush();
1203 #endif
1204
1205         /* STC_LOGD("Get all counters"); */
1206         nfacct_send_get_all(stc->carg);
1207
1208         /* we need to continue the timer */
1209         return TRUE;
1210 }
1211
1212 //LCOV_EXCL_START
1213 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1214                                           gpointer data)
1215 {
1216         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1217         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1218
1219         __print_rstn(rstn_key, rstn_value);
1220         return FALSE;
1221 }
1222
1223 static void __rstn_tree_printall(void)
1224 {
1225         g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1226 }
1227 //LCOV_EXCL_END
1228
1229 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1230                                         const stc_rstn_key_s *key)
1231 {
1232         stc_rstn_value_s *lookup;
1233
1234         ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1235
1236         lookup = g_tree_lookup(rstns_tree, key);
1237
1238         return lookup;
1239 }
1240
1241 static gboolean __remove_restriction(gpointer key, gpointer value,
1242                                      gpointer data)
1243 {
1244         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1245         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1246
1247         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1248         __print_rstn(rstn_key, rstn_value);
1249         return FALSE;
1250 }
1251
1252 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1253                                         gpointer data)
1254 {
1255         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1256         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1257
1258         /* rstn rule is activated */
1259         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1260                 return FALSE;
1261
1262         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1263                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1264         else
1265                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1266
1267         __print_rstn(rstn_key, rstn_value);
1268
1269         return FALSE;
1270 }
1271
1272 //LCOV_EXCL_START
1273 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1274 {
1275         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1276         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1277
1278         /* rstn rule is activated */
1279         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1280                 return FALSE;
1281
1282         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1283                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1284         else
1285                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1286
1287         return FALSE;
1288 }
1289 //LCOV_EXCL_STOP
1290
1291 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1292 {
1293         stc_rstn_value_s *lookup_value;
1294
1295         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1296
1297         lookup_value = __rstn_lookup(g_system->rstns, key);
1298         if (!lookup_value) {
1299                 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1300                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1301         }
1302
1303         __remove_restriction(key, lookup_value, NULL);
1304
1305         /* remove counter also */
1306         table_counters_delete(lookup_value->restriction_id);
1307
1308         if (!g_tree_remove(g_system->rstns, key)) {
1309                 STC_LOGD("key not found"); //LCOV_EXCL_LINE
1310                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1311         }
1312
1313         return STC_ERROR_NONE;
1314 }
1315
1316 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1317                                    stc_rstn_value_s *value, gboolean debug)
1318 {
1319         stc_rstn_value_s *rstn_value;
1320
1321         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1322
1323         rstn_value = __rstn_lookup(g_system->rstns, key);
1324         if (!rstn_value) {
1325                 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1326                 if (!rstn_key) {
1327                         STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
1328                         return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1329                 }
1330
1331                 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1332                 if (!rstn_value) {
1333                         STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
1334                         FREE(rstn_key); //LCOV_EXCL_LINE
1335                         return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1336                 }
1337
1338                 rstn_key->app_id = g_strdup(key->app_id);
1339                 rstn_key->ifname = g_strdup(key->ifname);
1340                 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1341                 rstn_key->iftype = key->iftype;
1342                 rstn_key->roaming = key->roaming;
1343
1344                 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1345         }
1346
1347         rstn_value->restriction_id = value->restriction_id;
1348         rstn_value->rst_state = value->rst_state;
1349         rstn_value->classid = value->classid;
1350         rstn_value->data_limit = value->data_limit;
1351         rstn_value->data_warn_limit = value->data_warn_limit;
1352         rstn_value->data_counter = 0;
1353         rstn_value->warn_limit_crossed_notified = FALSE;
1354         rstn_value->rstn_limit_crossed_notified = FALSE;
1355
1356         if (debug == TRUE)
1357                 __add_restriction_debug(key, rstn_value, NULL);
1358         else
1359                 __add_restriction(key, rstn_value, NULL);
1360
1361         return STC_ERROR_NONE;
1362 }
1363
1364 //LCOV_EXCL_START
1365 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1366                                             void *user_data)
1367 {
1368         stc_cb_ret_e ret = STC_CONTINUE;
1369
1370         stc_rstn_key_s key;
1371         stc_rstn_value_s value;
1372
1373         memset(&key, 0, sizeof(stc_rstn_key_s));
1374         memset(&value, 0, sizeof(stc_rstn_value_s));
1375
1376         key.app_id = g_strdup(info->app_id);
1377         key.ifname = g_strdup(info->ifname);
1378         key.subscriber_id = g_strdup(info->subscriber_id);
1379         key.iftype = info->iftype;
1380         key.roaming = info->roaming;
1381
1382         value.rst_state = info->rst_state;
1383         value.restriction_id = info->restriction_id;
1384
1385         if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1386                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1387         else
1388                 value.classid = STC_UNKNOWN_CLASSID;
1389
1390         value.data_limit = info->data_limit;
1391         value.data_warn_limit = info->data_warn_limit;
1392
1393         if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1394                 ret = STC_CANCEL;
1395
1396         FREE(key.app_id);
1397         FREE(key.ifname);
1398         FREE(key.subscriber_id);
1399         return ret;
1400 }
1401
1402 static void __fill_restritions_list(void)
1403 {
1404         table_restrictions_foreach(__insert_restriction_cb, NULL);
1405
1406         if (STC_DEBUG_LOG)
1407                 __rstn_tree_printall();
1408 }
1409
1410 static gboolean __add_rstn_foreach_application(gpointer key,
1411                                                gpointer value,
1412                                                gpointer data)
1413 {
1414         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1415         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1416         gchar *app_id = (gchar *)data;
1417
1418         /* rstn rule is not for applications */
1419         if (rstn_key->app_id == NULL)
1420                 goto out;
1421
1422         /* rstn rule is not for this application */
1423         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1424                 goto out;
1425
1426         /* rstn rule is already applied */
1427         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1428                 goto out;
1429
1430         /* add restriction to system */
1431         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1432                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
1433         else
1434                 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
1435
1436         __print_rstn(rstn_key, rstn_value);
1437 out:
1438         return FALSE;
1439 }
1440 //LCOV_EXCL_STOP
1441
1442 static void __add_rstns_for_application(gchar *app_id)
1443 {
1444         g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1445                        app_id);
1446 }
1447
1448 static void __add_application_by_interface(const char *app_id)
1449 {
1450         stc_app_key_s app_key;
1451         stc_app_value_s app_value;
1452
1453         if (app_id == NULL)
1454                 return; //LCOV_EXCL_LINE
1455
1456         memset(&app_key, 0, sizeof(stc_app_key_s));
1457         memset(&app_value, 0, sizeof(stc_app_value_s));
1458
1459         app_key.pkg_id = g_strdup(app_id);
1460         app_key.app_id = g_strdup(app_id);
1461
1462         app_value.type = STC_APP_TYPE_NONE;
1463         app_value.processes = NULL;
1464         app_value.counter.in_bytes = 0;
1465         app_value.counter.out_bytes = 0;
1466
1467         stc_monitor_application_add(app_key, app_value);
1468
1469         FREE(app_key.pkg_id);
1470         FREE(app_key.app_id);
1471 }
1472
1473 static int __vconf_get_int(const char *key, int *value)
1474 {
1475         int ret = 0;
1476
1477         ret = vconf_get_int(key, value);
1478         if (ret != VCONF_OK) {
1479                 STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
1480                 return -1; //LCOV_EXCL_LINE
1481         }
1482
1483         return 0;
1484 }
1485
1486 //LCOV_EXCL_START
1487 static int __vconf_set_int(const char *key, int value)
1488 {
1489         int ret = 0;
1490
1491         ret = vconf_set_int(key, value);
1492         if (ret != VCONF_OK) {
1493                 STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
1494                 return -1; //LCOV_EXCL_LINE
1495         }
1496
1497         return 0;
1498 }
1499
1500 static guint __get_background_state(void)
1501 {
1502         return g_system->background_state;;
1503 }
1504
1505 static void __set_background_state(guint state)
1506 {
1507         g_system->background_state = state;
1508 }
1509
1510 static gboolean __processes_tree_foreach_background(gpointer key,
1511                                                     gpointer value,
1512                                                     gpointer data)
1513 {
1514         stc_process_key_s *proc_key = (stc_process_key_s *)key;
1515         stc_app_key_s *app_key = (stc_app_key_s *)data;
1516
1517         if (g_system->background_state)
1518                 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1519         else
1520                 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1521
1522         return FALSE;
1523 }
1524
1525 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1526                                         gpointer data)
1527 {
1528         stc_app_key_s *app_key = (stc_app_key_s *)key;
1529         stc_app_value_s *app_value = (stc_app_value_s *)value;
1530
1531         if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1532                 g_tree_foreach(app_value->processes,
1533                                __processes_tree_foreach_background, app_key);
1534
1535         return FALSE;
1536 }
1537
1538 static stc_error_e __process_update_background(void)
1539 {
1540         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1541
1542         g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1543
1544         return STC_ERROR_NONE;
1545 }
1546 //LCOV_EXCL_STOP
1547
1548 static void __fill_exceptions_list(void)
1549 {
1550         stc_plugin_fill_exception_list();
1551 }
1552
1553 stc_error_e stc_monitor_init(void)
1554 {
1555         stc_system_s *system = MALLOC0(stc_system_s, 1);
1556         GIOChannel *gio = NULL;
1557
1558         ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1559
1560         /* initializing current classid */
1561         init_current_classid();
1562
1563         /* initializing cgroups */
1564         cgroup_init();
1565
1566         /* creating monitored application tree */
1567         system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1568                                        __apps_tree_key_free,
1569                                        __apps_tree_value_free);
1570
1571         system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1572                                         __rstns_tree_key_free,
1573                                         __rstns_tree_value_free);
1574
1575         /* create netlink socket for updating kernel counters */
1576         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1577         if (system->contr_sock < 0) {
1578                 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
1579                 FREE(system); //LCOV_EXCL_LINE
1580                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1581         }
1582
1583         gio = g_io_channel_unix_new(system->contr_sock);
1584         system->contr_gsource_id =
1585                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1586                                (GIOFunc) __process_contr_reply,
1587                                NULL);
1588         g_io_channel_unref(gio);
1589
1590         g_system = system;
1591
1592         __add_application_by_interface(STC_TOTAL_DATACALL);
1593         __add_application_by_interface(STC_TOTAL_WIFI);
1594         __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1595         __add_application_by_interface(STC_TOTAL_IPV4);
1596         __add_application_by_interface(STC_TOTAL_IPV6);
1597
1598         /* creating restriction rules tree */
1599         __update_contr_cb(NULL);
1600
1601         /* registering periodic kernel counters update callback */
1602         g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1603                                                          __update_contr_cb,
1604                                                          NULL);
1605         if (g_system->contr_timer_id == 0) {
1606                 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
1607                 __close_contr_sock(g_system); //LCOV_EXCL_LINE
1608                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1609         }
1610
1611         __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1612                         (int *)&g_system->background_state);
1613
1614         __fill_exceptions_list();
1615         __fill_restritions_list();
1616
1617         return STC_ERROR_NONE;
1618 }
1619
1620 stc_error_e stc_monitor_deinit(void)
1621 {
1622         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1623
1624         /* close netlink socket for updating kernel counters */
1625         __close_contr_sock(g_system);
1626
1627         /* remove kernel counters update timer */
1628         if (g_system->contr_timer_id > 0) {
1629                 g_source_remove(g_system->contr_timer_id);
1630                 g_system->contr_timer_id = 0;
1631         }
1632
1633         /* destroy monitored application tree */
1634         g_tree_destroy(g_system->apps);
1635         g_system->apps = NULL;
1636
1637         /* destroy restriction rules tree */
1638         g_tree_destroy(g_system->rstns);
1639         g_system->rstns = NULL;
1640
1641         FREE(g_system);
1642
1643         return STC_ERROR_NONE;
1644 }
1645
1646 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1647                                         const stc_app_value_s app_value)
1648 {
1649         stc_error_e ret = STC_ERROR_NONE;
1650         stc_app_key_s *key;
1651         stc_app_value_s *value;
1652         stc_app_value_s *lookup;
1653
1654         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1655
1656         lookup = __application_lookup(g_system->apps, &app_key);
1657         if (lookup) {
1658                 STC_LOGD("app_key already present"); //LCOV_EXCL_LINE
1659                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1660         }
1661
1662         key = MALLOC0(stc_app_key_s, 1);
1663         if (!key) {
1664                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1665                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1666         }
1667
1668         value = MALLOC0(stc_app_value_s, 1);
1669         if (!value) {
1670                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1671                 FREE(key); //LCOV_EXCL_LINE
1672                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1673         }
1674
1675         key->app_id = g_strdup(app_key.app_id);
1676         key->pkg_id = g_strdup(app_key.pkg_id);
1677
1678         value->type = app_value.type;
1679         value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1680         value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1681
1682         value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1683                                            __processes_tree_key_free,
1684                                            __processes_tree_value_free);
1685
1686         /* create cgroup and update classid */
1687         value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1688
1689         g_tree_insert(g_system->apps, key, value);
1690
1691         /* add nfacct rule for this classid */
1692         __add_application_monitor(key, value, stc_get_default_connection());
1693         __add_rstns_for_application(app_key.app_id);
1694
1695         return ret;
1696 }
1697
1698 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1699                                     const stc_process_key_s proc_key,
1700                                     const stc_process_value_s proc_value)
1701 {
1702         stc_error_e ret = STC_ERROR_NONE;
1703         stc_app_value_s *app_lookup;
1704         stc_process_key_s *key;
1705         stc_process_value_s *value;
1706         stc_process_value_s *proc_lookup;
1707
1708         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1709
1710         app_lookup = __application_lookup(g_system->apps, &app_key);
1711         if (!app_lookup) {
1712                 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
1713                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1714         }
1715
1716         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1717         if (proc_lookup) {
1718                 STC_LOGD("proc_key already present"); //LCOV_EXCL_LINE
1719                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1720         }
1721
1722         key = MALLOC0(stc_process_key_s, 1);
1723         if (!key) {
1724                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1725                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1726         }
1727
1728         value = MALLOC0(stc_process_value_s, 1);
1729         if (!value) {
1730                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1731                 FREE(key); //LCOV_EXCL_LINE
1732                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1733         }
1734
1735         key->pid = proc_key.pid;
1736
1737         value->ground = proc_value.ground;
1738
1739         g_tree_insert(app_lookup->processes, key, value);
1740
1741         /* add pid to application cgroup */
1742         place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1743
1744         if (STC_DEBUG_LOG)
1745                 __apps_tree_printall(); //LCOV_EXCL_LINE
1746
1747         return ret;
1748 }
1749
1750 API stc_error_e stc_monitor_process_remove(pid_t pid)
1751 {
1752         stc_error_e ret = STC_ERROR_NONE;
1753         stc_process_key_s proc_key = {
1754                 .pid = pid
1755         };
1756
1757         remove_pid_context_s context = {
1758                 .app_key = NULL,
1759                 .proc_key = &proc_key,
1760                 .entry_removed = FALSE,
1761         };
1762
1763         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1764
1765         g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1766                        &context);
1767
1768         if (context.entry_removed)
1769                 __application_remove_if_empty(context.app_key);
1770
1771         if (STC_DEBUG_LOG)
1772                 __apps_tree_printall(); //LCOV_EXCL_LINE
1773
1774         return ret;
1775 }
1776
1777 //LCOV_EXCL_START
1778 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1779                                               const stc_process_key_s proc_key,
1780                                               stc_app_state_e ground)
1781 {
1782         stc_error_e ret = STC_ERROR_NONE;
1783         stc_app_value_s *app_lookup;
1784         stc_process_value_s *proc_lookup;
1785
1786         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1787
1788         app_lookup = __application_lookup(g_system->apps, &app_key);
1789         if (!app_lookup) {
1790                 STC_LOGD("app_key not found");
1791                 return STC_ERROR_FAIL;
1792         }
1793
1794         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1795         if (!proc_lookup) {
1796                 STC_LOGD("proc_key not found");
1797                 return STC_ERROR_FAIL;
1798         }
1799
1800         if (proc_lookup->ground != ground)
1801                 proc_lookup->ground = ground;
1802
1803         if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1804                 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1805         else
1806                 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1807
1808         return ret;
1809 }
1810 //LCOV_EXCL_STOP
1811
1812 void stc_monitor_update_rstn_by_default_connection(void *data)
1813 {
1814         static default_connection_s old_connection;
1815         default_connection_s *new_connection = (default_connection_s *)data;
1816
1817         if (old_connection.path != NULL) {
1818                 //LCOV_EXCL_START
1819                 if (g_system->apps)
1820                         g_tree_foreach(g_system->apps,
1821                                        __remove_application_monitor,
1822                                        (gpointer)&old_connection);
1823
1824                 if (g_system->rstns)
1825                         g_tree_foreach(g_system->rstns,
1826                                        __remove_restriction,
1827                                        (gpointer)&old_connection);
1828
1829                 iptables_flush_chains();
1830                 //LCOV_EXCL_STOP
1831         }
1832
1833         FREE(old_connection.path);
1834         FREE(old_connection.ifname);
1835         old_connection.type = 0;
1836         old_connection.roaming = 0;
1837
1838         if (new_connection != NULL && new_connection->path != NULL) {
1839                 if (g_system->apps)
1840                         g_tree_foreach(g_system->apps,
1841                                        __add_application_monitor,
1842                                        (gpointer)new_connection);
1843
1844                 if (g_system->rstns)
1845                         g_tree_foreach(g_system->rstns, __add_restriction,
1846                                        NULL);
1847
1848                 old_connection.path = g_strdup(new_connection->path);
1849                 old_connection.ifname = g_strdup(new_connection->ifname);
1850                 old_connection.type = new_connection->type;
1851                 old_connection.roaming = new_connection->roaming;
1852         }
1853 }
1854
1855 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1856 {
1857         stc_error_e ret;
1858
1859         stc_rstn_key_s key;
1860         stc_rstn_value_s value;
1861
1862         memset(&key, 0, sizeof(stc_rstn_key_s));
1863         memset(&value, 0, sizeof(stc_rstn_value_s));
1864
1865         key.app_id = g_strdup(info->app_id);
1866         key.ifname = g_strdup(info->ifname);
1867         key.subscriber_id = g_strdup(info->subscriber_id);
1868         key.iftype = info->iftype;
1869         key.roaming = info->roaming;
1870
1871         value.rst_state = info->rst_state;
1872         value.restriction_id = info->restriction_id;
1873
1874         if (value.rst_state > STC_RESTRICTION_UNKNOWN &&
1875                 value.rst_state < STC_RESTRICTION_LAST_ELEM &&
1876                 info->app_id)
1877                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1878         else
1879                 value.classid = STC_UNKNOWN_CLASSID;
1880
1881         if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1882                 __set_background_state(TRUE); //LCOV_EXCL_LINE
1883                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1884                 __process_update_background(); //LCOV_EXCL_LINE
1885         }
1886
1887         value.data_limit = info->data_limit;
1888         value.data_warn_limit = info->data_warn_limit;
1889
1890         ret = __rstn_tree_add(&key, &value, TRUE);
1891
1892         FREE(key.app_id);
1893         FREE(key.ifname);
1894         FREE(key.subscriber_id);
1895         return ret;
1896 }
1897
1898 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1899 {
1900         stc_error_e ret;
1901
1902         stc_rstn_key_s key = {
1903                 .app_id = g_strdup(info->app_id),
1904                 .ifname = g_strdup(info->ifname),
1905                 .subscriber_id = g_strdup(info->subscriber_id),
1906                 .iftype = info->iftype,
1907                 .roaming = info->roaming,
1908         };
1909
1910         if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
1911                 __set_background_state(FALSE); //LCOV_EXCL_LINE
1912                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1913                 __process_update_background(); //LCOV_EXCL_LINE
1914         }
1915
1916         ret = __rstn_tree_remove(&key);
1917
1918         FREE(key.app_id);
1919         FREE(key.ifname);
1920         FREE(key.subscriber_id);
1921         return ret;
1922 }
1923
1924 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
1925 {
1926         return stc_plugin_check_exception_by_cmdline(cmdline);
1927 }
1928
1929 int stc_monitor_get_counter_socket(void)
1930 {
1931         return g_system->contr_sock;
1932 }