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