Removed unwanted function send_net_restriction() and its related code.
[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         switch (rst_type) {
417         case RST_SET:
418                 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
419                 rstn_value->in_limit_reached = FALSE;
420                 rstn_value->out_limit_reached = FALSE;
421                 break;
422         case RST_EXCLUDE:
423                 ;//Do Nothing
424                 break;
425         case RST_UNSET:
426                 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
427                 rstn_value->in_limit_reached = FALSE;
428                 rstn_value->out_limit_reached = FALSE;
429                 break;
430         default:
431                 ;//Do Nothing
432         }
433 }
434
435 static gboolean __remove_rstns_foreach_application(gpointer key,
436                                                    gpointer value,
437                                                    gpointer data)
438 {
439         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
440         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
441         gchar *app_id = (gchar *)data;
442
443         /* rstn rule is not for applications */
444         if (rstn_key->app_id == NULL)
445                 goto out;
446
447         /* rstn rule is not for this application */
448         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
449                 goto out;
450
451         /* rstn rule is already removed */
452         if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
453                 goto out;
454
455         /* remove restriction from system */
456         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
457
458         __print_rstn(rstn_key, rstn_value);
459 out:
460         return FALSE;
461 }
462
463 static void __remove_rstns_for_application(gchar *app_id)
464 {
465         g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
466                        app_id);
467 }
468
469 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
470 {
471         stc_error_e ret = STC_ERROR_NONE;
472         guint pid_count = 0;
473         stc_app_value_s *lookup;
474
475         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
476
477         lookup = __application_lookup(g_system->apps, app_key);
478         if (!lookup) {
479                 STC_LOGE("app_key not found");
480                 return STC_ERROR_NO_DATA;
481         }
482
483         g_tree_foreach(lookup->processes, __processes_tree_check_empty,
484                        &pid_count);
485
486         if (!pid_count) {
487                 /* remove nfacct rule for this classid */
488                 __remove_application_monitor((gpointer) app_key, lookup,
489                                              stc_get_default_connection());
490                 __remove_rstns_for_application(app_key->app_id);
491         }
492
493         if (!g_tree_remove(g_system->apps, app_key)) {
494                 ret = STC_ERROR_NO_DATA;
495                 STC_LOGE("key not found");
496         }
497
498         return ret;
499 }
500
501 static stc_error_e __close_contr_sock(stc_system_s *system)
502 {
503         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
504
505         /* close netlink socket for updating kernel counters */
506         if (g_system->contr_sock != -1) {
507                 close(g_system->contr_sock);
508                 g_system->contr_sock = -1;
509         }
510
511         if (g_system->contr_gsource_id != 0) {
512                 g_source_remove(g_system->contr_gsource_id);
513                 g_system->contr_gsource_id = 0;
514         }
515
516         return STC_ERROR_NONE;
517 }
518
519 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
520                                                       gpointer value,
521                                                       gpointer data)
522 {
523         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
524         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
525         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
526
527         if (context->counter->intend != NFACCT_COUNTER)
528                 goto try_next_callback;
529
530         if (rstn_value->classid != context->counter->classid)
531                 goto try_next_callback;
532
533         if (rstn_value->in_limit_reached == TRUE) {
534                 context->in_limit_reached = TRUE;
535                 goto try_next_callback;
536         }
537
538         if (rstn_value->out_limit_reached == TRUE) {
539                 context->out_limit_reached = TRUE;
540                 goto try_next_callback;
541         }
542
543         switch (context->counter->iotype) {
544         case NFACCT_COUNTER_IN:
545                 rstn_value->counter.in_bytes += context->bytes;
546
547                 if (rstn_value->counter.in_bytes >= rstn_value->warn_limit.in_bytes
548                     && rstn_value->warn_limit_crossed_notified == FALSE) {
549                         gboolean rv = FALSE;
550                         stc_s *stc = (stc_s *)stc_get_manager();
551                         ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
552
553                         rv = stc_manager_dbus_emit_signal(stc->connection,
554                                                           STC_DBUS_SERVICE_RESTRICTION_PATH,
555                                                           STC_DBUS_INTERFACE_RESTRICTION,
556                                                           "WarnThresholdCrossed",
557                                                           g_variant_new("(s)", rstn_key->app_id));
558                         if (rv == TRUE)
559                                 rstn_value->warn_limit_crossed_notified = TRUE;
560                 }
561
562                 /* block immediately */
563                 if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) {
564                         context->counter->intend = NFACCT_BLOCK;
565                         __del_iptables_in(context->counter);
566                         __add_iptables_in(context->counter);
567                         context->counter->intend = NFACCT_COUNTER;
568                         rstn_value->in_limit_reached = TRUE;
569
570                         if (rstn_value->rstn_limit_crossed_notified == FALSE) {
571                                 gboolean rv = FALSE;
572                                 stc_s *stc = (stc_s *)stc_get_manager();
573                                 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
574
575                                 rv = stc_manager_dbus_emit_signal(stc->connection,
576                                                                   STC_DBUS_SERVICE_RESTRICTION_PATH,
577                                                                   STC_DBUS_INTERFACE_RESTRICTION,
578                                                                   "RestrictionThresholdCrossed",
579                                                                   g_variant_new("(s)", rstn_key->app_id));
580                                 if (rv == TRUE)
581                                         rstn_value->rstn_limit_crossed_notified = TRUE;
582                         }
583                 }
584
585                 g_system->rstns_tree_updated = TRUE;
586                 __print_rstn(rstn_key, rstn_value);
587                 break;
588         case NFACCT_COUNTER_OUT:
589                 rstn_value->counter.out_bytes += context->bytes;
590
591                 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes
592                     && rstn_value->warn_limit_crossed_notified == FALSE) {
593                         gboolean rv = FALSE;
594                         stc_s *stc = (stc_s *)stc_get_manager();
595                         ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
596
597                         rv = stc_manager_dbus_emit_signal(stc->connection,
598                                                           STC_DBUS_SERVICE_RESTRICTION_PATH,
599                                                           STC_DBUS_INTERFACE_RESTRICTION,
600                                                           "WarnThresholdCrossed",
601                                                           g_variant_new("(s)", rstn_key->app_id));
602                         if (rv == TRUE)
603                                 rstn_value->warn_limit_crossed_notified = TRUE;
604                 }
605
606                 /* block immediately */
607                 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) {
608                         context->counter->intend = NFACCT_BLOCK;
609                         __del_iptables_out(context->counter);
610                         __add_iptables_out(context->counter);
611                         context->counter->intend = NFACCT_COUNTER;
612                         rstn_value->out_limit_reached = TRUE;
613
614                         if (rstn_value->rstn_limit_crossed_notified == FALSE) {
615                                 gboolean rv = FALSE;
616                                 stc_s *stc = (stc_s *)stc_get_manager();
617                                 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
618
619                                 rv = stc_manager_dbus_emit_signal(stc->connection,
620                                                                   STC_DBUS_SERVICE_RESTRICTION_PATH,
621                                                                   STC_DBUS_INTERFACE_RESTRICTION,
622                                                                   "RestrictionThresholdCrossed",
623                                                                   g_variant_new("(s)", rstn_key->app_id));
624                                 if (rv == TRUE)
625                                         rstn_value->rstn_limit_crossed_notified = TRUE;
626                         }
627                 }
628
629                 g_system->rstns_tree_updated = TRUE;
630                 __print_rstn(rstn_key, rstn_value);
631                 break;
632         default:
633                 STC_LOGE("unknown iotype");
634         }
635
636
637 try_next_callback:
638         return FALSE;
639 }
640
641 static gboolean __update_app_statistics(gpointer key, gpointer value,
642                                         gpointer data)
643 {
644         stc_app_key_s *app_key = (stc_app_key_s *)key;
645         stc_app_value_s *app_value = (stc_app_value_s *)value;
646         time_t *touch_time = (time_t *)data;
647         stc_db_classid_iftype_key stat_key;
648         stc_db_app_stats stat;
649         char *default_ifname = stc_default_connection_get_ifname();
650
651         memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
652         memset(&stat, 0 , sizeof(stc_db_app_stats));
653
654         stat_key.classid = app_value->classid;
655         stat_key.iftype = stc_default_connection_get_type();
656         if (STC_IFACE_DATACALL == stat_key.iftype)
657                 stat_key.imsi = g_strdup("unknown");
658         else
659                 stat_key.imsi = g_strdup("noneimsi");
660         g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH);
661
662         stat.app_id = g_strdup(app_key->app_id);
663         stat.snd_count = app_value->counter.out_bytes;
664         stat.rcv_count = app_value->counter.in_bytes;
665         stat.delta_snd = 0;
666         stat.delta_rcv = 0;
667         stat.is_roaming = stc_default_connection_get_roaming();
668         stat.ground = STC_APP_STATE_UNKNOWN;
669
670         table_statistics_insert(&stat_key, &stat, *touch_time);
671
672         app_value->counter.out_bytes = 0;
673         app_value->counter.in_bytes = 0;
674
675         FREE(stat.app_id);
676         FREE(stat_key.imsi);
677         FREE(default_ifname);
678
679         return FALSE;
680 }
681
682 static gboolean __flush_apps_stats_to_database(gpointer user_data)
683 {
684         time_t current_time = time(0);
685
686         if (g_system->apps_tree_updated == FALSE)
687                 return G_SOURCE_REMOVE;
688
689         g_system->apps_tree_updated = FALSE;
690
691         if (g_system->apps)
692                 g_tree_foreach(g_system->apps,
693                                __update_app_statistics,
694                                &current_time);
695
696         STC_LOGI("Flushed app stats to database");
697         return G_SOURCE_REMOVE;
698 }
699
700 static gboolean __update_counter_statistics(gpointer key, gpointer value,
701                                             gpointer data)
702 {
703         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
704         table_counters_info info = {
705                 .restriction_id = rstn_value->restriction_id,
706                 .sent_bytes = rstn_value->counter.out_bytes,
707                 .rcv_bytes = rstn_value->counter.in_bytes
708         };
709
710         table_counters_update_counters(&info);
711
712         return FALSE;
713 }
714
715 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
716 {
717         time_t current_time = time(0);
718
719         if (g_system->rstns_tree_updated == FALSE)
720                 return G_SOURCE_REMOVE;
721
722         g_system->rstns_tree_updated = FALSE;
723
724         if (g_system->rstns)
725                 g_tree_foreach(g_system->rstns,
726                                __update_counter_statistics,
727                                &current_time);
728
729         STC_LOGI("Flushed rstns counters to database");
730         return G_SOURCE_REMOVE;
731 }
732
733 static gboolean __apps_counter_update_foreach_classid(gpointer key,
734                                                       gpointer value,
735                                                       gpointer data)
736 {
737         stc_app_key_s *app_key = (stc_app_key_s *)key;
738         stc_app_value_s *app_value = (stc_app_value_s *)value;
739         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
740
741         if (context->counter->intend != NFACCT_COUNTER)
742                 goto try_next_callback;
743
744         if (app_value->classid != context->counter->classid)
745                 goto try_next_callback;
746
747         switch (context->counter->iotype) {
748         case NFACCT_COUNTER_IN:
749                 app_value->data_usage.in_bytes += context->bytes;
750                 app_value->counter.in_bytes = context->bytes;
751                 g_system->apps_tree_updated = TRUE;
752
753                 __apps_tree_foreach_print(app_key, app_value, NULL);
754                 break;
755         case NFACCT_COUNTER_OUT:
756                 app_value->data_usage.out_bytes += context->bytes;
757                 app_value->counter.out_bytes = context->bytes;
758                 g_system->apps_tree_updated = TRUE;
759
760                 __apps_tree_foreach_print(app_key, app_value, NULL);
761                 break;
762         default:
763                 STC_LOGE("unknown iotype");
764         }
765
766 try_next_callback:
767         return FALSE;
768 }
769
770 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
771                                  struct counter_arg *carg)
772 {
773         struct nfacct_rule counter = {
774                 .carg = carg,
775                 .name = {0},
776                 .ifname = {0},
777                 0
778         };
779
780         classid_bytes_context_s context = {
781                 .counter = &counter,
782                 .bytes = bytes,
783                 .in_limit_reached = FALSE,
784                 .out_limit_reached = FALSE
785         };
786
787         STC_LOGD("cnt_name %s", cnt_name);
788
789         if (!recreate_counter_by_name(cnt_name, &counter)) {
790                 STC_LOGE("Can't parse counter name %s", cnt_name);
791                 return;
792         }
793
794         STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
795                  context.counter->classid, context.counter->iftype,
796                  context.counter->iotype, context.counter->intend,
797                  context.counter->ifname, context.bytes);
798
799         if (g_system->rstns)
800                 g_tree_foreach(g_system->rstns,
801                                __rstn_counter_update_foreach_classid,
802                                &context);
803
804         if (g_system->apps)
805                 g_tree_foreach(g_system->apps,
806                                __apps_counter_update_foreach_classid,
807                                &context);
808 }
809
810 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
811                            void *user_data)
812 {
813         struct counter_arg *carg = user_data;
814         char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
815         if (carg->initiate) {
816                 /**
817                  * TODO: this will be used when daemon starts to update existing
818                  * counter data if present.
819                  *
820                  populate_counters(cnt_name, carg);
821                  */
822         } else {
823                 int64_t *bytes_p =
824                         (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
825                 int bytes = be64toh(*bytes_p);
826                 if (bytes) {
827                         ++carg->serialized_counters;
828                         __fill_nfacct_result(cnt_name, bytes, carg);
829                 }
830         }
831
832         return 0;
833 }
834
835 static int __post_fill_counters(void *user_data)
836 {
837         struct counter_arg *carg = user_data;
838
839         if (carg->initiate)
840                 carg->initiate = 0;
841
842         return 0;
843 }
844
845 static void __process_network_counter(struct genl *ans,
846                                       struct counter_arg *carg)
847 {
848         struct netlink_serialization_params ser_params = {
849                 .carg = carg,
850                 .ans = ans,
851                 .eval_attr = __fill_counters,
852                 .post_eval_attr = __post_fill_counters,
853         };
854
855         netlink_serialization_command *netlink =
856                 netlink_create_command(&ser_params);
857         if (!netlink) {
858                 STC_LOGE("Can not create command");
859                 return;
860         }
861
862         netlink->deserialize_answer(&(netlink->params));
863 }
864
865 static gboolean __process_contr_reply(GIOChannel *source,
866                                       GIOCondition condition,
867                                       gpointer user_data)
868 {
869         int sock = g_io_channel_unix_get_fd(source);
870         struct genl ans;
871         int ret;
872         stc_s *stc = stc_get_manager();
873
874         if ((condition & G_IO_ERR) ||
875             (condition & G_IO_HUP) ||
876             (condition & G_IO_NVAL)) {
877                 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
878
879                 __close_contr_sock(g_system);
880                 return FALSE;
881         }
882
883         if (stc == NULL) {
884                 STC_LOGE("Can't get stc data");
885                 goto out;
886         }
887
888         ret = read_netlink(sock,
889                            &ans, sizeof(struct genl));
890         STC_LOGD("Counter data received ret [%d]", ret);
891         if (ret == 0)
892                 goto out;
893
894         stc->carg->ans_len = ret;
895         __process_network_counter(&ans, stc->carg);
896
897         g_idle_add(__flush_apps_stats_to_database, NULL);
898         g_idle_add(__flush_rstns_counter_to_database, NULL);
899 out:
900         return TRUE;
901 }
902
903 static gboolean __update_contr_cb(void *user_data)
904 {
905         /* Here we just sent command, answer we receive in another callback */
906         stc_s *stc = stc_get_manager();
907         ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
908         if (!stc->carg) {
909                 stc->carg = MALLOC0(counter_arg_s, 1);
910                 stc->carg->sock = stc_monitor_get_counter_socket();
911         }
912
913         STC_LOGD("Get all counters");
914         nfacct_send_get_all(stc->carg);
915
916         /* we need to continue the timer */
917         return TRUE;
918 }
919 #if 0
920 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
921                                           gpointer data)
922 {
923         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
924         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
925
926         __print_rstn(rstn_key, rstn_value);
927         return FALSE;
928 }
929
930 static void __rstn_tree_printall(void)
931 {
932         g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
933 }
934 #endif
935 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
936                                         const stc_rstn_key_s *key)
937 {
938         stc_rstn_value_s *lookup;
939
940         ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
941
942         lookup = g_tree_lookup(rstns_tree, key);
943
944         return lookup;
945 }
946
947 static gboolean __remove_restriction(gpointer key, gpointer value,
948                                      gpointer data)
949 {
950         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
951         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
952
953         /* rstn rule is already removed */
954         if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
955                 return FALSE;
956
957         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
958         __print_rstn(rstn_key, rstn_value);
959         return FALSE;
960 }
961
962 static gboolean __add_restriction_debug(gpointer key, gpointer value,
963                                         gpointer data)
964 {
965         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
966         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
967
968         /* rstn rule is activated */
969         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
970                 return FALSE;
971
972         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
973                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
974         else
975                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
976
977         __print_rstn(rstn_key, rstn_value);
978
979         return FALSE;
980 }
981
982 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
983 {
984         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
985         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
986
987         /* rstn rule is activated */
988         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
989                 return FALSE;
990
991         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
992                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
993         else
994                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
995
996         return FALSE;
997 }
998
999 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1000 {
1001         stc_rstn_value_s *lookup_value;
1002
1003         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1004
1005         lookup_value = __rstn_lookup(g_system->rstns, key);
1006         if (!lookup_value) {
1007                 STC_LOGE("key not found");
1008                 return STC_ERROR_NO_DATA;
1009         }
1010
1011         __remove_restriction(key, lookup_value, NULL);
1012
1013         /* remove counter also */
1014         table_counters_delete(lookup_value->restriction_id);
1015
1016         if (!g_tree_remove(g_system->rstns, key)) {
1017                 STC_LOGD("key not found");
1018                 return STC_ERROR_NO_DATA;
1019         }
1020
1021         return STC_ERROR_NONE;
1022 }
1023
1024 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1025                                    stc_rstn_value_s *value, gboolean debug)
1026 {
1027         stc_rstn_value_s *rstn_value;
1028
1029         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1030
1031         rstn_value = __rstn_lookup(g_system->rstns, key);
1032         if (!rstn_value) {
1033                 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1034                 if (!rstn_key) {
1035                         STC_LOGE("rstn_key allocation failed");
1036                         return STC_ERROR_OUT_OF_MEMORY;
1037                 }
1038
1039                 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1040                 if (!rstn_value) {
1041                         STC_LOGE("rstn_value allocation failed");
1042                         FREE(rstn_key);
1043                         return STC_ERROR_OUT_OF_MEMORY;
1044                 }
1045
1046                 rstn_key->app_id = g_strdup(key->app_id);
1047                 rstn_key->ifname = g_strdup(key->ifname);
1048                 rstn_key->imsi = g_strdup(key->imsi);
1049                 rstn_key->iftype = key->iftype;
1050                 rstn_key->roaming = key->roaming;
1051
1052                 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1053         }
1054
1055         rstn_value->restriction_id = value->restriction_id;
1056         rstn_value->rst_state = value->rst_state;
1057         rstn_value->classid = value->classid;
1058         rstn_value->limit.in_bytes = value->limit.in_bytes;
1059         rstn_value->limit.out_bytes = value->limit.out_bytes;
1060         rstn_value->warn_limit.in_bytes = value->warn_limit.in_bytes;
1061         rstn_value->warn_limit.out_bytes = value->warn_limit.out_bytes;
1062         rstn_value->counter.in_bytes = 0;
1063         rstn_value->counter.out_bytes = 0;
1064         rstn_value->warn_limit_crossed_notified = FALSE;
1065         rstn_value->rstn_limit_crossed_notified = FALSE;
1066
1067         if (debug == TRUE)
1068                 __add_restriction_debug(key, rstn_value, NULL);
1069         else
1070                 __add_restriction(key, rstn_value, NULL);
1071
1072         return STC_ERROR_NONE;
1073 }
1074
1075 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1076                                             void *user_data)
1077 {
1078         stc_cb_ret_e ret = STC_CONTINUE;
1079
1080         stc_rstn_key_s key;
1081         stc_rstn_value_s value;
1082
1083         memset(&key, 0, sizeof(stc_rstn_key_s));
1084         memset(&value, 0, sizeof(stc_rstn_value_s));
1085
1086         key.app_id = g_strdup(info->app_id);
1087         key.ifname = g_strdup(info->ifname);
1088         key.imsi = g_strdup(info->imsi);
1089         key.iftype = info->iftype;
1090         key.roaming = info->roaming;
1091
1092         value.rst_state = info->rst_state;
1093         value.restriction_id = info->restriction_id;
1094
1095         if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1096                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1097         else
1098                 value.classid = STC_UNKNOWN_CLASSID;
1099
1100         value.limit.in_bytes = info->rcv_limit;
1101         value.limit.out_bytes = info->send_limit;
1102         value.warn_limit.in_bytes = info->rcv_warn_limit;
1103         value.warn_limit.out_bytes = info->send_warn_limit;
1104
1105         if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1106                 ret = STC_CANCEL;
1107
1108         FREE(key.app_id);
1109         FREE(key.ifname);
1110         FREE(key.imsi);
1111         return ret;
1112 }
1113
1114 static void __fill_restritions_list(void)
1115 {
1116         table_restrictions_foreach(__insert_restriction_cb, NULL);
1117         //__rstn_tree_printall();
1118 }
1119
1120 static gboolean __add_rstn_foreach_application(gpointer key,
1121                                                gpointer value,
1122                                                gpointer data)
1123 {
1124         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1125         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1126         gchar *app_id = (gchar *)data;
1127
1128         /* rstn rule is not for applications */
1129         if (rstn_key->app_id == NULL)
1130                 goto out;
1131
1132         /* rstn rule is not for this application */
1133         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1134                 goto out;
1135
1136         /* rstn rule is already applied */
1137         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1138                 goto out;
1139
1140         /* add restriction to system */
1141         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1142                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1143         else
1144                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1145
1146         __print_rstn(rstn_key, rstn_value);
1147 out:
1148         return FALSE;
1149 }
1150
1151 static void __add_rstns_for_application(gchar *app_id)
1152 {
1153         g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1154                        app_id);
1155 }
1156
1157 stc_error_e stc_monitor_init(void)
1158 {
1159         stc_system_s *system = MALLOC0(stc_system_s, 1);
1160         GIOChannel *gio = NULL;
1161
1162         ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1163
1164         /* initializing cgroups */
1165         cgroup_init();
1166
1167         /* creating monitored application tree */
1168         system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1169                                        __apps_tree_key_free,
1170                                        __apps_tree_value_free);
1171
1172         system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1173                                         __rstns_tree_key_free,
1174                                         __rstns_tree_value_free);
1175
1176         /* create netlink socket for updating kernel counters */
1177         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1178         if (!(system->contr_sock)) {
1179                 STC_LOGE("failed to open socket");
1180                 FREE(system);
1181                 return STC_ERROR_FAIL;
1182         }
1183
1184         gio = g_io_channel_unix_new(system->contr_sock);
1185         system->contr_gsource_id =
1186                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1187                                (GIOFunc) __process_contr_reply,
1188                                NULL);
1189         g_io_channel_unref(gio);
1190
1191         g_system = system;
1192
1193         /* creating restriction rules tree */
1194         __update_contr_cb(NULL);
1195
1196         /* registering periodic kernel counters update callback */
1197         g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1198                                                          __update_contr_cb,
1199                                                          NULL);
1200         if (g_system->contr_timer_id == 0) {
1201                 STC_LOGE("Failed to register kernel counters update timer");
1202                 __close_contr_sock(g_system);
1203                 return STC_ERROR_FAIL;
1204         }
1205
1206         __fill_restritions_list();
1207
1208         return STC_ERROR_NONE;
1209 }
1210
1211 stc_error_e stc_monitor_deinit(void)
1212 {
1213         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1214
1215         /* close netlink socket for updating kernel counters */
1216         __close_contr_sock(g_system);
1217
1218         /* remove kernel counters update timer */
1219         if (g_system->contr_timer_id > 0) {
1220                 g_source_remove(g_system->contr_timer_id);
1221                 g_system->contr_timer_id = 0;
1222         }
1223
1224         /* destroy monitored application tree */
1225         g_tree_destroy(g_system->apps);
1226         g_system->apps = NULL;
1227
1228         /* destroy restriction rules tree */
1229         g_tree_destroy(g_system->rstns);
1230         g_system->rstns = NULL;
1231
1232         FREE(g_system);
1233
1234         return STC_ERROR_NONE;
1235 }
1236
1237 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1238                                         const stc_app_value_s app_value)
1239 {
1240         stc_error_e ret = STC_ERROR_NONE;
1241         stc_app_key_s *key;
1242         stc_app_value_s *value;
1243         stc_app_value_s *lookup;
1244
1245         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1246
1247         lookup = __application_lookup(g_system->apps, &app_key);
1248         if (lookup) {
1249                 STC_LOGD("app_key already present");
1250                 return STC_ERROR_NONE;
1251         }
1252
1253         key = MALLOC0(stc_app_key_s, 1);
1254         if (!key) {
1255                 STC_LOGE("key allocation failed");
1256                 return STC_ERROR_OUT_OF_MEMORY;
1257         }
1258
1259         value = MALLOC0(stc_app_value_s, 1);
1260         if (!value) {
1261                 STC_LOGE("value allocation failed");
1262                 FREE(key);
1263                 return STC_ERROR_OUT_OF_MEMORY;
1264         }
1265
1266         key->app_id = g_strdup(app_key.app_id);
1267         key->pkg_id = g_strdup(app_key.pkg_id);
1268
1269         value->type = app_value.type;
1270         value->counter.in_bytes = app_value.counter.in_bytes;
1271         value->counter.out_bytes = app_value.counter.out_bytes;
1272
1273         value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1274                                            __processes_tree_key_free,
1275                                            __processes_tree_value_free);
1276
1277         /* create cgroup and update classid */
1278         value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1279
1280         g_tree_insert(g_system->apps, key, value);
1281
1282         /* add nfacct rule for this classid */
1283         __add_application_monitor(key, value, stc_get_default_connection());
1284         __add_rstns_for_application(app_key.app_id);
1285
1286         return ret;
1287 }
1288
1289 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1290                                     const stc_process_key_s proc_key,
1291                                     const stc_process_value_s proc_value)
1292 {
1293         stc_error_e ret = STC_ERROR_NONE;
1294         stc_app_value_s *app_lookup;
1295         stc_process_key_s *key;
1296         stc_process_value_s *value;
1297         stc_process_value_s *proc_lookup;
1298
1299         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1300
1301         app_lookup = __application_lookup(g_system->apps, &app_key);
1302         if (!app_lookup) {
1303                 STC_LOGD("app_key not found");
1304                 return STC_ERROR_FAIL;
1305         }
1306
1307         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1308         if (proc_lookup) {
1309                 STC_LOGD("proc_key already present");
1310                 return STC_ERROR_NONE;
1311         }
1312
1313         key = MALLOC0(stc_process_key_s, 1);
1314         if (!key) {
1315                 STC_LOGE("key allocation failed");
1316                 return STC_ERROR_OUT_OF_MEMORY;
1317         }
1318
1319         value = MALLOC0(stc_process_value_s, 1);
1320         if (!value) {
1321                 STC_LOGE("value allocation failed");
1322                 FREE(key);
1323                 return STC_ERROR_OUT_OF_MEMORY;
1324         }
1325
1326         key->pid = proc_key.pid;
1327
1328         value->ground = proc_value.ground;
1329
1330         g_tree_insert(app_lookup->processes, key, value);
1331
1332         /* add pid to application cgroup */
1333         place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1334
1335         return ret;
1336 }
1337
1338 stc_error_e stc_monitor_process_remove(pid_t pid)
1339 {
1340         stc_error_e ret = STC_ERROR_NONE;
1341         stc_process_key_s proc_key = {
1342                 .pid = pid
1343         };
1344
1345         remove_pid_context_s context = {
1346                 .app_key = NULL,
1347                 .proc_key = &proc_key,
1348                 .entry_removed = FALSE,
1349         };
1350
1351         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1352
1353         g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1354                        &context);
1355
1356         if (context.entry_removed)
1357                 __application_remove_if_empty(context.app_key);
1358
1359         return ret;
1360 }
1361
1362 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1363                                               const stc_process_key_s proc_key,
1364                                               stc_app_state_e ground)
1365 {
1366         stc_error_e ret = STC_ERROR_NONE;
1367         stc_app_value_s *app_lookup;
1368         stc_process_value_s *proc_lookup;
1369
1370         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1371
1372         app_lookup = __application_lookup(g_system->apps, &app_key);
1373         if (!app_lookup) {
1374                 STC_LOGD("app_key not found");
1375                 return STC_ERROR_FAIL;
1376         }
1377
1378         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1379         if (!proc_lookup) {
1380                 STC_LOGD("proc_key not found");
1381                 return STC_ERROR_FAIL;
1382         }
1383
1384         if (proc_lookup->ground != ground)
1385                 proc_lookup->ground = ground;
1386
1387         if (ground == STC_APP_STATE_BACKGROUND) {
1388                 char *background_app_id = g_strconcat(app_key.app_id,
1389                                                      STC_BACKGROUND_APP_SUFFIX,
1390                                                      NULL);
1391                 place_pids_to_net_cgroup(proc_key.pid, background_app_id);
1392                 g_free(background_app_id);
1393         } else {
1394                 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1395         }
1396
1397         return ret;
1398 }
1399
1400 void stc_monitor_update_rstn_by_default_connection(void *data)
1401 {
1402         static default_connection_s old_connection;
1403         default_connection_s *new_connection = (default_connection_s *)data;
1404
1405         if (old_connection.path != NULL) {
1406                 if (g_system->apps)
1407                         g_tree_foreach(g_system->apps,
1408                                        __remove_application_monitor,
1409                                        (gpointer)&old_connection);
1410
1411                 if (g_system->rstns)
1412                         g_tree_foreach(g_system->rstns,
1413                                        __remove_restriction,
1414                                        (gpointer)&old_connection);
1415         }
1416
1417         FREE(old_connection.path);
1418         FREE(old_connection.ifname);
1419         old_connection.type = 0;
1420         old_connection.roaming = 0;
1421
1422         if (new_connection != NULL && new_connection->path != NULL) {
1423                 if (g_system->apps)
1424                         g_tree_foreach(g_system->apps,
1425                                        __add_application_monitor,
1426                                        (gpointer)new_connection);
1427
1428                 if (g_system->rstns)
1429                         g_tree_foreach(g_system->rstns, __add_restriction,
1430                                        NULL);
1431
1432                 old_connection.path = g_strdup(new_connection->path);
1433                 old_connection.ifname = g_strdup(new_connection->ifname);
1434                 old_connection.type = new_connection->type;
1435                 old_connection.roaming = new_connection->roaming;
1436         }
1437 }
1438
1439 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1440 {
1441         stc_error_e ret;
1442
1443         stc_rstn_key_s key;
1444         stc_rstn_value_s value;
1445
1446         memset(&key, 0, sizeof(stc_rstn_key_s));
1447         memset(&value, 0, sizeof(stc_rstn_value_s));
1448
1449         key.app_id = g_strdup(info->app_id);
1450         key.ifname = g_strdup(info->ifname);
1451         key.imsi = g_strdup(info->imsi);
1452         key.iftype = info->iftype;
1453         key.roaming = info->roaming;
1454
1455         value.rst_state = info->rst_state;
1456         value.restriction_id = info->restriction_id;
1457
1458         if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1459                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1460         else
1461                 value.classid = STC_UNKNOWN_CLASSID;
1462
1463         value.limit.in_bytes = info->rcv_limit;
1464         value.limit.out_bytes = info->send_limit;
1465         value.warn_limit.in_bytes = info->rcv_warn_limit;
1466         value.warn_limit.out_bytes = info->send_warn_limit;
1467
1468         ret = __rstn_tree_add(&key, &value, TRUE);
1469
1470         FREE(key.app_id);
1471         FREE(key.ifname);
1472         FREE(key.imsi);
1473         return ret;
1474 }
1475
1476 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1477 {
1478         stc_error_e ret;
1479
1480         stc_rstn_key_s key = {
1481                 .app_id = g_strdup(info->app_id),
1482                 .ifname = g_strdup(info->ifname),
1483                 .imsi = g_strdup(info->imsi),
1484                 .iftype = info->iftype,
1485                 .roaming = info->roaming,
1486         };
1487
1488         ret = __rstn_tree_remove(&key);
1489
1490         FREE(key.app_id);
1491         FREE(key.ifname);
1492         FREE(key.imsi);
1493         return ret;
1494 }
1495
1496 int stc_monitor_get_counter_socket(void)
1497 {
1498         return g_system->contr_sock;
1499 }