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