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