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