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