Seperated net-popup plugin and wearable-popup plugin
[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 #include <vconf.h>
19 #include <vconf-keys.h>
20
21 #include "stc-default-connection.h"
22 #include "helper-nl.h"
23 #include "helper-nfacct-rule.h"
24 #include "helper-net-cls.h"
25 #include "helper-cgroup.h"
26 #include "counter.h"
27 #include "table-statistics.h"
28 #include "table-counters.h"
29 #include "stc-monitor.h"
30 #include "stc-manager-plugin-appstatus.h"
31 #include "stc-manager-plugin-exception.h"
32 #include "stc-manager-plugin-popup.h"
33 #include "stc-time.h"
34
35 #define MAX_INT_LENGTH 128
36 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
37
38 typedef struct {
39         time_t now;
40         time_t month_start_ts;
41         time_t week_start_ts;
42         time_t day_start_ts;
43         int is_updated;
44 } reset_time_limits_context_s;
45
46 typedef struct {
47         stc_app_key_s *app_key;
48         stc_process_key_s *proc_key;
49         gboolean entry_removed;
50 } remove_pid_context_s;
51
52 typedef struct {
53         struct nfacct_rule *counter;
54         int64_t bytes;
55         gboolean data_limit_exceeded;
56 } classid_bytes_context_s;
57
58 static stc_system_s *g_system = NULL;
59
60 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
61 {
62         if (counter->intend == NFACCT_WARN)
63                 return NFACCT_JUMP_ACCEPT;
64         else if (counter->intend == NFACCT_BLOCK)
65                 return NFACCT_JUMP_REJECT;
66
67         return NFACCT_JUMP_UNKNOWN;
68 }
69
70 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
71 {
72         if (counter == NULL)
73                 return STC_ERROR_INVALID_PARAMETER;
74
75         counter->action = NFACCT_ACTION_INSERT;
76         counter->iotype = NFACCT_COUNTER_IN;
77         counter->jump = __get_jump_by_intend(counter);
78         counter->iptype = NFACCT_TYPE_IPV4;
79         counter->send_limit = 0;
80         counter->rcv_limit = 0;
81
82         return produce_net_rule(counter);
83 }
84
85 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
86 {
87         if (counter == NULL)
88                 return STC_ERROR_INVALID_PARAMETER;
89
90         counter->action = NFACCT_ACTION_INSERT;
91         counter->iotype = NFACCT_COUNTER_OUT;
92         counter->jump = __get_jump_by_intend(counter);
93         counter->iptype = NFACCT_TYPE_IPV4;
94         counter->send_limit = 0;
95         counter->rcv_limit = 0;
96
97         return produce_net_rule(counter);
98 }
99
100 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
101 {
102         if (counter == NULL)
103                 return STC_ERROR_INVALID_PARAMETER;
104
105         counter->action = NFACCT_ACTION_DELETE;
106         counter->iotype = NFACCT_COUNTER_IN;
107         counter->jump = __get_jump_by_intend(counter);
108         counter->iptype = NFACCT_TYPE_IPV4;
109         counter->send_limit = 0;
110         counter->rcv_limit = 0;
111
112         return produce_net_rule(counter);
113 }
114
115 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
116 {
117         if (counter == NULL)
118                 return STC_ERROR_INVALID_PARAMETER;
119
120         counter->action = NFACCT_ACTION_DELETE;
121         counter->iotype = NFACCT_COUNTER_OUT;
122         counter->jump = __get_jump_by_intend(counter);
123         counter->iptype = NFACCT_TYPE_IPV4;
124         counter->send_limit = 0;
125         counter->rcv_limit = 0;
126
127         return produce_net_rule(counter);
128 }
129
130 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
131 {
132         if (counter == NULL)
133                 return STC_ERROR_INVALID_PARAMETER;
134
135         counter->action = NFACCT_ACTION_INSERT;
136         counter->iotype = NFACCT_COUNTER_IN;
137         counter->jump = __get_jump_by_intend(counter);
138         counter->iptype = NFACCT_TYPE_IPV6;
139         counter->send_limit = 0;
140         counter->rcv_limit = 0;
141
142         return produce_net_rule(counter);
143 }
144
145 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
146 {
147         if (counter == NULL)
148                 return STC_ERROR_INVALID_PARAMETER;
149
150         counter->action = NFACCT_ACTION_INSERT;
151         counter->iotype = NFACCT_COUNTER_OUT;
152         counter->jump = __get_jump_by_intend(counter);
153         counter->iptype = NFACCT_TYPE_IPV6;
154         counter->send_limit = 0;
155         counter->rcv_limit = 0;
156
157         return produce_net_rule(counter);
158 }
159
160 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
161 {
162         if (counter == NULL)
163                 return STC_ERROR_INVALID_PARAMETER;
164
165         counter->action = NFACCT_ACTION_DELETE;
166         counter->iotype = NFACCT_COUNTER_IN;
167         counter->jump = __get_jump_by_intend(counter);
168         counter->iptype = NFACCT_TYPE_IPV6;
169         counter->send_limit = 0;
170         counter->rcv_limit = 0;
171
172         return produce_net_rule(counter);
173 }
174
175 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
176 {
177         if (counter == NULL)
178                 return STC_ERROR_INVALID_PARAMETER;
179
180         counter->action = NFACCT_ACTION_DELETE;
181         counter->iotype = NFACCT_COUNTER_OUT;
182         counter->jump = __get_jump_by_intend(counter);
183         counter->iptype = NFACCT_TYPE_IPV6;
184         counter->send_limit = 0;
185         counter->rcv_limit = 0;
186
187         return produce_net_rule(counter);
188 }
189
190 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
191                                         gpointer UNUSED user_data)
192 {
193         stc_process_key_s *key_a = (stc_process_key_s *)a;
194         stc_process_key_s *key_b = (stc_process_key_s *)b;
195
196         return key_a->pid - key_b->pid;
197 }
198
199 static void __processes_tree_value_free(gpointer data)
200 {
201         stc_process_value_s *value = (stc_process_value_s *)data;
202
203         FREE(value);
204 }
205
206 static void __processes_tree_key_free(gpointer data)
207 {
208         stc_process_key_s *key = (stc_process_key_s *)data;
209
210         FREE(key);
211 }
212
213 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
214                                    gpointer UNUSED user_data)
215 {
216         stc_app_key_s *key_a = (stc_app_key_s *)a;
217         stc_app_key_s *key_b = (stc_app_key_s *)b;
218         gint ret;
219
220         ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
221         if (ret)
222                 return ret;
223
224         return g_strcmp0(key_a->app_id, key_b->app_id);
225 }
226
227 static void __apps_tree_value_free(gpointer data)
228 {
229         stc_app_value_s *value = (stc_app_value_s *)data;
230
231         g_tree_destroy(value->processes);
232         value->processes = NULL;
233
234         FREE(value);
235 }
236
237 static void __apps_tree_key_free(gpointer data)
238 {
239         stc_app_key_s *key = (stc_app_key_s *)data;
240
241         g_free(key->pkg_id);
242         g_free(key->app_id);
243         FREE(key);
244 }
245
246 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
247                                     gpointer UNUSED user_data)
248 {
249         stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
250         stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
251         int ret;
252
253         ret = g_strcmp0(key_a->app_id, key_b->app_id);
254         if (ret != 0)
255                 return ret;
256
257         ret = g_strcmp0(key_a->ifname, key_b->ifname);
258         if (ret != 0)
259                 return ret;
260
261         ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
262         if (ret != 0)
263                 return ret;
264
265         ret = key_a->iftype - key_b->iftype;
266         if (ret != 0)
267                 return ret;
268
269         return 0;
270 }
271
272 static void __rstns_tree_value_free(gpointer data)
273 {
274         stc_rstn_value_s *value = (stc_rstn_value_s *)data;
275
276         FREE(value);
277 }
278
279 static void __rstns_tree_key_free(gpointer data)
280 {
281         stc_rstn_key_s *key = (stc_rstn_key_s *)data;
282
283         FREE(key->app_id);
284         FREE(key->ifname);
285         FREE(key->subscriber_id);
286         FREE(key);
287 }
288
289 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
290                                                gpointer data)
291 {
292         stc_process_key_s *proc_key = (stc_process_key_s *)key;
293         stc_process_value_s *proc_value = (stc_process_value_s *)value;
294
295         STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
296                  proc_key->pid, proc_value->ground);
297         return FALSE;
298 }
299
300 static void __processes_tree_printall(GTree *processes)
301 {
302         g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
303 }
304
305 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
306                                           gpointer data)
307 {
308         stc_app_key_s *app_key = (stc_app_key_s *)key;
309         stc_app_value_s *app_value = (stc_app_value_s *)value;
310
311         STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
312                  "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
313                  " counter [ in (%lld), out (%lld)]",
314                  app_key->pkg_id, app_key->app_id,
315                  app_value->type, app_value->classid,
316                  app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
317
318         __processes_tree_printall(app_value->processes);
319         return FALSE;
320 }
321
322 static void __apps_tree_printall(void)
323 {
324         g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
325 }
326
327 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
328                                                gpointer data)
329 {
330         remove_pid_context_s *context = (remove_pid_context_s *)data;
331         stc_app_value_s *app_value = (stc_app_value_s *)value;
332
333         if (!g_tree_remove(app_value->processes, context->proc_key)) {
334                 if (STC_DEBUG_LOG)
335                         STC_LOGD("key not found");
336                 return FALSE;
337         }
338
339         context->entry_removed = TRUE;
340         context->app_key = (stc_app_key_s *)key;
341
342         return TRUE;
343 }
344
345 static stc_app_value_s * __application_lookup(GTree *apps,
346                                               const stc_app_key_s *key)
347 {
348         stc_app_value_s *lookup;
349
350         ret_value_msg_if(apps == NULL, NULL, "apps is null!");
351
352         lookup = g_tree_lookup(apps, key);
353
354         return lookup;
355 }
356
357 static stc_process_value_s * __process_lookup(GTree *processes,
358                                               const stc_process_key_s *key)
359 {
360         stc_process_value_s *lookup;
361
362         ret_value_msg_if(processes == NULL, NULL, "processes is null!");
363
364         lookup = g_tree_lookup(processes, key);
365
366         return lookup;
367 }
368
369 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
370                                              gpointer data)
371 {
372         guint *pid_count = (guint *)data;
373         (*pid_count)++;
374         return TRUE;
375 }
376
377 static gboolean __add_application_monitor(gpointer key, gpointer value,
378                                           gpointer data)
379 {
380         stc_app_value_s *app_value = (stc_app_value_s *)value;
381         default_connection_s *connection = (default_connection_s *)data;
382         stc_s *stc = stc_get_manager();
383
384         if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
385              connection->type != STC_IFACE_DATACALL) ||
386             (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
387              connection->type != STC_IFACE_WIFI) ||
388             (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
389              connection->type != STC_IFACE_BLUETOOTH))
390                 return FALSE;
391
392         if (stc && connection && connection->ifname) {
393                 struct nfacct_rule counter;
394
395                 if (!stc->carg) {
396                         stc->carg = MALLOC0(counter_arg_s, 1);
397                         if (stc->carg == NULL)
398                                 return FALSE;
399
400                         stc->carg->sock = stc_monitor_get_counter_socket();
401                 }
402
403                 memset(&counter, 0, sizeof(struct nfacct_rule));
404
405                 counter.carg = stc->carg;
406                 counter.classid = app_value->classid;
407                 counter.intend = NFACCT_COUNTER;
408                 counter.iftype = connection->type;
409                 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
410
411                 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
412                         __add_iptables_in(&counter);
413                         __add_iptables_out(&counter);
414                 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
415                         __add_ip6tables_in(&counter);
416                         __add_ip6tables_out(&counter);
417                 } else {
418                         __add_iptables_in(&counter);
419                         __add_iptables_out(&counter);
420                         __add_ip6tables_in(&counter);
421                         __add_ip6tables_out(&counter);
422                 }
423         }
424
425         return FALSE;
426 }
427
428 static gboolean __remove_application_monitor(gpointer key, gpointer value,
429                                              gpointer data)
430 {
431         stc_app_value_s *app_value = (stc_app_value_s *)value;
432         default_connection_s *connection = (default_connection_s *)data;
433         stc_s *stc = stc_get_manager();
434
435         if (stc && connection && connection->ifname) {
436                 struct nfacct_rule counter;
437
438                 if (!stc->carg) {
439                         stc->carg = MALLOC0(counter_arg_s, 1);
440                         if (stc->carg == NULL)
441                                 return FALSE;
442
443                         stc->carg->sock = stc_monitor_get_counter_socket();
444                 }
445
446                 memset(&counter, 0, sizeof(struct nfacct_rule));
447
448                 counter.carg = stc->carg;
449                 counter.classid = app_value->classid;
450                 counter.intend = NFACCT_COUNTER;
451                 counter.iftype = connection->type;
452                 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
453
454                 __del_iptables_in(&counter);
455                 __del_iptables_out(&counter);
456                 __del_ip6tables_in(&counter);
457                 __del_ip6tables_out(&counter);
458         }
459
460         return FALSE;
461 }
462
463 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
464 {
465         STC_LOGI("rstn info => rstn_id [%llu], "
466                  "app_id [%s], classid [%lu], ifname [%s], "
467                  "iftype [%d], rst_state [%d], "
468                  "limit [ (%lld) bytes], "
469                  "warn_limit [ (%lld) bytes], "
470                  "monthly_limit [ (%lld) bytes], "
471                  "weekly_limit [ (%lld) bytes], "
472                  "daily_limit [ (%lld) bytes], "
473                  "data_counter [ (%lld) bytes], "
474                  "warn_counter [ (%lld) bytes], "
475                  "monthly_counter [ (%lld) bytes], "
476                  "weekly_counter [ (%lld) bytes], "
477                  "daily_counter [ (%lld) bytes], "
478                  "roaming [%d], subscriber_id [%s]",
479                  rstn_value->restriction_id,
480                  rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
481                  rstn_key->iftype, rstn_value->rst_state,
482                  rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA],
483                  rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN],
484                  rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY],
485                  rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY],
486                  rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY],
487                  rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
488                  rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
489                  rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
490                  rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
491                  rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY],
492                  rstn_key->roaming, rstn_key->subscriber_id);
493 }
494
495 static void __process_restriction(enum traffic_restriction_type rst_type,
496                                   stc_rstn_key_s *rstn_key,
497                                   stc_rstn_value_s *rstn_value, void *data)
498 {
499         default_connection_s *old_connection = (default_connection_s *)data;
500         default_connection_s *connection = NULL;
501
502         if (old_connection != NULL)
503                 connection = old_connection;
504         else
505                 connection = stc_get_default_connection();
506
507         /* no default ifname */
508         if (connection->ifname == NULL)
509                 return;
510
511         /* rstn not applicable for this interface */
512         if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
513             g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
514                 return;
515
516         /* classid is invalid */
517         if (rstn_value->classid == STC_UNKNOWN_CLASSID)
518                 return;
519
520         switch (rst_type) {
521         case RST_SET:
522         {
523                 /* TODO: Change this to runtime memory */
524                 int i;
525                 table_counters_info info;
526                 int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = {0, };
527
528                 memset(&info, 0, sizeof(table_counters_info));
529                 rstn_value->limit_exceeded = 0;
530
531                 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
532                      rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
533                     (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
534                      rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
535                     (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
536                      rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
537                     (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
538                      rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
539                     (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
540                      rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
541
542                         table_counters_get(rstn_value->restriction_id, &info);
543
544                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
545                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
546                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter;
547                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter;
548                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter;
549                 }
550
551                 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
552
553                         if (rstn_value->limit[i] >= 0) {
554                                 effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i];
555
556                                 if (effective_limit[i] < 0) {
557                                         rstn_value->limit_exceeded |= (1 << i);
558                                         rstn_value->limit_notified |= (1 << i);
559                                 }
560                         }
561                 }
562
563                 STC_LOGD("rstn_id [%llu], datausage [%lld] bytes, ",
564                          rstn_value->restriction_id, info.data_counter);
565
566                 if (rstn_value->limit_exceeded != 0 &&
567                     rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
568
569                         char *default_ifname = stc_default_connection_get_ifname();
570                         struct nfacct_rule counter;
571                         stc_s *stc = stc_get_manager();
572                         if (stc == NULL) {
573                                 STC_LOGE("Can't get stc data");
574                                 g_free(default_ifname);
575                                 return;
576                         }
577
578                         if (!stc->carg) {
579                                 stc->carg = MALLOC0(counter_arg_s, 1);
580                                 if (stc->carg == NULL) {
581                                         g_free(default_ifname);
582                                         return;
583                                 }
584
585                                 stc->carg->sock =
586                                         stc_monitor_get_counter_socket();
587                         }
588
589                         counter.carg = stc->carg;
590                         counter.classid = rstn_value->classid;
591                         counter.intend = NFACCT_BLOCK;
592                         counter.iftype = rstn_key->iftype;
593                         g_strlcpy(counter.ifname, default_ifname,
594                                   MAX_IFACE_LENGTH);
595
596                         g_free(default_ifname);
597
598                         /* iptables rule */
599                         __add_iptables_in(&counter);
600                         __add_iptables_out(&counter);
601
602                         /* ip6tables rule */
603                         __add_ip6tables_in(&counter);
604                         __add_ip6tables_out(&counter);
605                 }
606
607                 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
608         }
609         break;
610         case RST_EXCLUDE:
611                 ;//Do Nothing
612                 break;
613         case RST_UNSET:
614         {
615                 char *default_ifname = stc_default_connection_get_ifname();
616                 struct nfacct_rule counter;
617                 stc_s *stc = stc_get_manager();
618                 if (stc == NULL) {
619                         STC_LOGE("Can't get stc data");
620                         g_free(default_ifname);
621                         return;
622                 }
623
624                 if (!stc->carg) {
625                         stc->carg = MALLOC0(counter_arg_s, 1);
626                         if (stc->carg == NULL) {
627                                 g_free(default_ifname);
628                                 return;
629                         }
630
631                         stc->carg->sock =
632                                 stc_monitor_get_counter_socket();
633                 }
634
635                 counter.carg = stc->carg;
636                 counter.classid = rstn_value->classid;
637                 counter.intend = NFACCT_BLOCK;
638                 counter.iftype = rstn_key->iftype;
639                 g_strlcpy(counter.ifname, default_ifname,
640                           MAX_IFACE_LENGTH);
641
642                 g_free(default_ifname);
643
644                 /* iptables rule */
645                 __del_iptables_in(&counter);
646                 __del_iptables_out(&counter);
647
648                 /* ip6tables rule */
649                 __del_ip6tables_in(&counter);
650                 __del_ip6tables_out(&counter);
651
652                 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
653                 rstn_value->limit_exceeded = 0;
654                 rstn_value->limit_notified = 0;
655
656                 int i;
657                 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
658                         if (rstn_value->limit[i] >= 0)
659                                 rstn_value->counter[i] = 0;
660         }
661         break;
662         default:
663                 ;//Do Nothing
664         }
665 }
666
667 static gboolean __remove_rstns_foreach_application(gpointer key,
668                                                    gpointer value,
669                                                    gpointer data)
670 {
671         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
672         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
673         gchar *app_id = (gchar *)data;
674
675         /* rstn rule is not for applications */
676         if (rstn_key->app_id == NULL)
677                 goto out;
678
679         /* rstn rule is not for this application */
680         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
681                 goto out;
682
683         /* rstn rule is already removed */
684         if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
685                 goto out;
686
687         /* remove restriction from system */
688         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
689
690         __print_rstn(rstn_key, rstn_value);
691 out:
692         return FALSE;
693 }
694
695 static void __remove_rstns_for_application(gchar *app_id)
696 {
697         g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
698                        app_id);
699 }
700
701 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
702 {
703         stc_error_e ret = STC_ERROR_NONE;
704         guint pid_count = 0;
705         stc_app_value_s *lookup;
706
707         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
708
709         lookup = __application_lookup(g_system->apps, app_key);
710         if (!lookup) {
711                 STC_LOGE("app_key not found");
712                 return STC_ERROR_NO_DATA;
713         }
714
715         g_tree_foreach(lookup->processes, __processes_tree_check_empty,
716                        &pid_count);
717
718         if (!pid_count) {
719                 /* remove nfacct rule for this classid */
720                 __remove_application_monitor((gpointer) app_key, lookup,
721                                              stc_get_default_connection());
722                 __remove_rstns_for_application(app_key->app_id);
723         }
724
725         if (!g_tree_remove(g_system->apps, app_key)) {
726                 ret = STC_ERROR_NO_DATA;
727                 STC_LOGE("key not found");
728         }
729
730         return ret;
731 }
732
733 static stc_error_e __close_contr_sock(stc_system_s *system)
734 {
735         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
736
737         /* close netlink socket for updating kernel counters */
738         if (system->contr_sock != -1) {
739                 close(system->contr_sock);
740                 system->contr_sock = -1;
741         }
742
743         if (system->contr_gsource_id != 0) {
744                 g_source_remove(system->contr_gsource_id);
745                 system->contr_gsource_id = 0;
746         }
747
748         return STC_ERROR_NONE;
749 }
750
751 static gboolean __process_contr_reply(GIOChannel *source,
752                                       GIOCondition condition,
753                                       gpointer user_data);
754
755 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
756 {
757         GIOChannel *gio = NULL;
758         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
759
760         /* close netlink socket for updating kernel counters */
761         if (system->contr_sock != -1) {
762                 close(system->contr_sock);
763                 system->contr_sock = -1;
764         }
765
766         if (system->contr_gsource_id != 0) {
767                 g_source_remove(system->contr_gsource_id);
768                 system->contr_gsource_id = 0;
769         }
770
771         /* create netlink socket for updating kernel counters */
772         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
773         if (system->contr_sock < 0) {
774                 STC_LOGE("failed to open socket");
775                 FREE(system);
776                 return STC_ERROR_FAIL;
777         }
778
779         gio = g_io_channel_unix_new(system->contr_sock);
780         system->contr_gsource_id =
781                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
782                                (GIOFunc) __process_contr_reply,
783                                NULL);
784         g_io_channel_unref(gio);
785
786         return STC_ERROR_NONE;
787 }
788
789 static void __action_when_rstn_limit_exceeded(stc_rstn_limit_type_e limit_type,
790                                               stc_rstn_key_s *rstn_key,
791                                               stc_rstn_value_s *rstn_value,
792                                               classid_bytes_context_s *context)
793 {
794         gboolean rv;
795         char iftype[MAX_INT_LENGTH];
796         char byte[MAX_INT_LENGTH];
797         const char *signal_name = NULL;
798         const char *net_popup_content = NULL;
799         const char *net_popup_type = NULL;
800         stc_s *stc = (stc_s *)stc_get_manager();
801
802         if (stc == NULL) {
803                 STC_LOGE("failed to get stc data");
804                 return;
805         }
806
807         switch (limit_type) {
808         case STC_RSTN_LIMIT_TYPE_DATA_WARN:
809         {
810                 signal_name = "WarnThresholdCrossed";
811                 net_popup_content = "warn threshold crossed";
812                 net_popup_type = "warning_noti";
813         }
814         break;
815         case STC_RSTN_LIMIT_TYPE_DATA:
816         case STC_RSTN_LIMIT_TYPE_MONTHLY:
817         case STC_RSTN_LIMIT_TYPE_WEEKLY:
818         case STC_RSTN_LIMIT_TYPE_DAILY:
819         {
820                 signal_name = "RestrictionThresholdCrossed";
821                 net_popup_content = "restriction threshold crossed";
822                 net_popup_type = "restriction_noti";
823
824                 /* block immediately */
825                 context->counter->intend = NFACCT_BLOCK;
826                 __del_iptables_in(context->counter);
827                 __del_iptables_out(context->counter);
828                 __add_iptables_in(context->counter);
829                 __add_iptables_out(context->counter);
830
831                 __del_ip6tables_in(context->counter);
832                 __del_ip6tables_out(context->counter);
833                 __add_ip6tables_in(context->counter);
834                 __add_ip6tables_out(context->counter);
835                 context->counter->intend = NFACCT_COUNTER;
836
837                 rstn_value->limit_exceeded |= (1 << limit_type);
838         }
839         break;
840         default:
841                 ;// Do nothing
842         }
843
844         /* emit signal */
845         rv = stc_manager_dbus_emit_signal(stc->connection,
846                                           STC_DBUS_SERVICE_RESTRICTION_PATH,
847                                           STC_DBUS_INTERFACE_RESTRICTION,
848                                           signal_name,
849                                           g_variant_new("(si)",
850                                                         rstn_key->app_id,
851                                                         rstn_key->iftype));
852         if (rv == TRUE)
853                 rstn_value->limit_notified |= (1 << limit_type);
854
855         snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
856         snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit[limit_type]);
857
858         stc_plugin_popup_show(net_popup_content, net_popup_type,
859                               rstn_key->app_id, iftype, byte);
860 }
861
862 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
863                                       stc_rstn_value_s *rstn_value,
864                                       classid_bytes_context_s *context)
865 {
866         int i;
867         switch (context->counter->iotype) {
868         case NFACCT_COUNTER_IN:
869         case NFACCT_COUNTER_OUT:
870                 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
871                      rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
872                     (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
873                      rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
874                     (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
875                      rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
876                     (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
877                      rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
878                     (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
879                      rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
880
881                         table_counters_info info;
882                         memset(&info, 0, sizeof(table_counters_info));
883                         table_counters_get(rstn_value->restriction_id, &info);
884
885                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
886                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
887                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter;
888                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter;
889                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter;
890                 }
891
892                 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
893
894                         if (rstn_value->limit[i] >= 0 &&
895                             !(rstn_value->limit_notified & (1 << i))) {
896
897                                 rstn_value->counter[i] += context->bytes;
898
899                                 if (rstn_value->limit[i] <= rstn_value->counter[i])
900                                         __action_when_rstn_limit_exceeded(i, rstn_key,
901                                                                           rstn_value,
902                                                                           context);
903                         }
904                 }
905
906                 g_system->rstns_tree_updated = TRUE;
907                 __print_rstn(rstn_key, rstn_value);
908                 break;
909         default:
910                 STC_LOGE("unknown iotype");
911         }
912
913         return FALSE;
914 }
915
916 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
917                                                 stc_rstn_value_s *rstn_value,
918                                                 classid_bytes_context_s *context)
919 {
920         if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
921              context->counter->iftype == STC_IFACE_DATACALL) ||
922             (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
923              context->counter->iftype == STC_IFACE_WIFI) ||
924             (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
925              context->counter->iftype == STC_IFACE_BLUETOOTH)) {
926                 context->counter->classid = rstn_value->classid;
927                 return __rstn_counter_update(rstn_key, rstn_value, context);
928         }
929
930         return FALSE;
931 }
932
933 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
934                                                       gpointer value,
935                                                       gpointer data)
936 {
937         gboolean rv = FALSE;
938         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
939         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
940         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
941         uint32_t classid;
942
943         if (context->counter->intend != NFACCT_COUNTER)
944                 goto try_next_callback;
945
946         if (rstn_value->limit_exceeded) {
947                 context->data_limit_exceeded = TRUE;
948                 goto try_next_callback;
949         }
950
951         classid = context->counter->classid;
952         rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
953
954         context->counter->classid = classid;
955         if (rstn_value->classid != context->counter->classid)
956                 goto try_next_callback;
957
958         rv = __rstn_counter_update(rstn_key, rstn_value, context);
959
960 try_next_callback:
961         return rv;
962 }
963
964 static gboolean __update_app_statistics(gpointer key, gpointer value,
965                                         gpointer data)
966 {
967         stc_app_key_s *app_key = (stc_app_key_s *)key;
968         stc_app_value_s *app_value = (stc_app_value_s *)value;
969         time_t *touch_time = (time_t *)data;
970         stc_db_classid_iftype_key stat_key;
971         stc_db_app_stats stat;
972         default_connection_s *default_connection = stc_get_default_connection();
973
974         memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
975         memset(&stat, 0 , sizeof(stc_db_app_stats));
976
977         stat_key.classid = app_value->classid;
978         stat_key.iftype = default_connection->type;
979
980         if (STC_IFACE_DATACALL == stat_key.iftype)
981                 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
982         else
983                 stat_key.subscriber_id = g_strdup("none_subid");
984
985         g_strlcpy(stat_key.ifname, default_connection->ifname,
986                   MAX_IFACE_LENGTH);
987
988         stat.app_id = g_strdup(app_key->app_id);
989         stat.snd_count = app_value->counter.out_bytes;
990         stat.rcv_count = app_value->counter.in_bytes;
991         stat.is_roaming = default_connection->roaming;
992
993         if (strstr(stat.app_id, "_BACKGROUND")) {
994                 stat.ground = STC_APP_STATE_BACKGROUND;
995         } else {
996                 if (strstr(stat.app_id, "TOTAL_"))
997                         stat.ground = STC_APP_STATE_UNKNOWN;
998                 else
999                         stat.ground = STC_APP_STATE_FOREGROUND;
1000         }
1001
1002         table_statistics_insert(&stat_key, &stat, *touch_time);
1003
1004         app_value->counter.out_bytes = 0;
1005         app_value->counter.in_bytes = 0;
1006
1007         FREE(stat.app_id);
1008         FREE(stat_key.subscriber_id);
1009
1010         return FALSE;
1011 }
1012
1013 static gboolean __flush_apps_stats_to_database(gpointer user_data)
1014 {
1015         time_t current_time = 0;
1016         stc_s *stc = stc_get_manager();
1017
1018         if (stc && stc->carg)
1019                 current_time = stc->carg->last_run_time;
1020
1021         if (g_system->apps_tree_updated == FALSE)
1022                 return G_SOURCE_REMOVE;
1023
1024         g_system->apps_tree_updated = FALSE;
1025
1026         if (g_system->apps)
1027                 g_tree_foreach(g_system->apps,
1028                                __update_app_statistics,
1029                                &current_time);
1030
1031         STC_LOGI("Flushed app stats to database");
1032         return G_SOURCE_REMOVE;
1033 }
1034
1035 static gboolean __update_counter_statistics(gpointer key, gpointer value,
1036                                             gpointer data)
1037 {
1038         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1039         table_counters_info info = {
1040                 .restriction_id = rstn_value->restriction_id,
1041                 .data_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
1042                 .warn_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
1043                 .monthly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
1044                 .weekly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
1045                 .daily_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY]
1046         };
1047
1048         table_counters_update_counters(&info);
1049
1050         return FALSE;
1051 }
1052
1053 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
1054 {
1055         time_t current_time = 0;
1056         stc_s *stc = stc_get_manager();
1057
1058         if (stc && stc->carg)
1059                 current_time = stc->carg->last_run_time;
1060
1061         if (g_system->rstns_tree_updated == FALSE)
1062                 return G_SOURCE_REMOVE;
1063
1064         g_system->rstns_tree_updated = FALSE;
1065
1066         if (g_system->rstns)
1067                 g_tree_foreach(g_system->rstns,
1068                                __update_counter_statistics,
1069                                &current_time);
1070
1071         STC_LOGI("Flushed rstns counters to database");
1072         return G_SOURCE_REMOVE;
1073 }
1074
1075 static void __app_counter_update(stc_app_key_s *app_key,
1076                                  stc_app_value_s *app_value,
1077                                  classid_bytes_context_s *context)
1078 {
1079         switch (context->counter->iotype) {
1080         case NFACCT_COUNTER_IN:
1081                 app_value->data_usage.in_bytes += context->bytes;
1082                 app_value->counter.in_bytes = context->bytes;
1083                 g_system->apps_tree_updated = TRUE;
1084
1085                 if (STC_DEBUG_LOG)
1086                         __apps_tree_foreach_print(app_key, app_value, NULL);
1087                 break;
1088         case NFACCT_COUNTER_OUT:
1089                 app_value->data_usage.out_bytes += context->bytes;
1090                 app_value->counter.out_bytes = context->bytes;
1091                 g_system->apps_tree_updated = TRUE;
1092
1093                 if (STC_DEBUG_LOG)
1094                         __apps_tree_foreach_print(app_key, app_value, NULL);
1095                 break;
1096         default:
1097                 STC_LOGE("unknown iotype");
1098         }
1099 }
1100
1101 static void __interface_counter_update(stc_app_key_s *app_key,
1102                                        stc_app_value_s *app_value,
1103                                        classid_bytes_context_s *context)
1104 {
1105         if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1106              context->counter->iftype == STC_IFACE_DATACALL) ||
1107             (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1108              context->counter->iftype == STC_IFACE_WIFI) ||
1109             (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1110              context->counter->iftype == STC_IFACE_BLUETOOTH))
1111                 __app_counter_update(app_key, app_value, context);
1112 }
1113
1114
1115 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1116                                                       gpointer value,
1117                                                       gpointer data)
1118 {
1119         stc_app_key_s *app_key = (stc_app_key_s *)key;
1120         stc_app_value_s *app_value = (stc_app_value_s *)value;
1121         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1122
1123         if (context->counter->intend != NFACCT_COUNTER)
1124                 goto try_next_callback;
1125
1126         __interface_counter_update(app_key, app_value, context);
1127
1128         if (app_value->classid != context->counter->classid)
1129                 goto try_next_callback;
1130
1131         __app_counter_update(app_key, app_value, context);
1132
1133 try_next_callback:
1134         return FALSE;
1135 }
1136
1137 static gboolean __reset_time_counter_foreach_rstn(gpointer key,
1138                                                   gpointer value,
1139                                                   gpointer data)
1140 {
1141         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1142         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1143         reset_time_limits_context_s *context = (reset_time_limits_context_s *)data;
1144         int i;
1145         time_t now_month_start_ts;
1146
1147         if (rstn_value->month_start_date == 0) {
1148                 table_counters_info info;
1149                 memset(&info, 0, sizeof(table_counters_info));
1150                 table_counters_get_timestamps(rstn_value->restriction_id, &info);
1151
1152                 if (info.month_start_date == 0)
1153                         rstn_value->month_start_date = 1;
1154                 else
1155                         rstn_value->month_start_date = info.month_start_date;
1156                 rstn_value->month_start_ts = info.month_start_ts;
1157         }
1158
1159         now_month_start_ts =
1160                 stc_time_get_month_start(context->now,
1161                                          rstn_value->month_start_date);
1162
1163         if (rstn_value->month_start_ts != now_month_start_ts) {
1164                 rstn_value->month_start_ts = now_month_start_ts;
1165                 context->month_start_ts = now_month_start_ts;
1166                 context->is_updated |= (1 << STC_RSTN_LIMIT_TYPE_MONTHLY);
1167         }
1168
1169         if (context->is_updated) {
1170                 table_counters_info info;
1171                 memset(&info, 0, sizeof(table_counters_info));
1172
1173                 info.restriction_id = rstn_value->restriction_id;
1174                 info.month_start_date = rstn_value->month_start_date;
1175                 info.month_start_ts = rstn_value->month_start_ts;
1176                 info.week_start_ts = context->week_start_ts;
1177                 info.day_start_ts = context->day_start_ts;
1178
1179                 table_counters_update_timestamps(&info);
1180         }
1181
1182         for (i = STC_RSTN_LIMIT_TYPE_MONTHLY; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1183
1184                 if ((context->is_updated) & (1 << i)) {
1185                         /* reset limit */
1186                         rstn_value->counter[i] = 0;
1187
1188                         if (rstn_value->limit_exceeded & (1 << i)) {
1189                                 /* remove iptables rule */
1190                                 char *default_ifname = stc_default_connection_get_ifname();
1191                                 struct nfacct_rule counter;
1192                                 stc_s *stc = stc_get_manager();
1193                                 if (stc == NULL) {
1194                                         STC_LOGE("Can't get stc data");
1195                                         g_free(default_ifname);
1196                                         goto try_next_callback;
1197                                 }
1198
1199                                 if (!stc->carg) {
1200                                         stc->carg = MALLOC0(counter_arg_s, 1);
1201                                         if (stc->carg == NULL) {
1202                                                 g_free(default_ifname);
1203                                                 goto try_next_callback;
1204                                         }
1205
1206                                         stc->carg->sock =
1207                                                 stc_monitor_get_counter_socket();
1208                                 }
1209
1210                                 counter.carg = stc->carg;
1211                                 counter.classid = rstn_value->classid;
1212                                 counter.intend = NFACCT_BLOCK;
1213                                 counter.iftype = rstn_key->iftype;
1214                                 g_strlcpy(counter.ifname, default_ifname,
1215                                           MAX_IFACE_LENGTH);
1216
1217                                 g_free(default_ifname);
1218
1219                                 /* iptables rule */
1220                                 __del_iptables_in(&counter);
1221                                 __del_iptables_out(&counter);
1222
1223                                 /* ip6tables rule */
1224                                 __del_ip6tables_in(&counter);
1225                                 __del_ip6tables_out(&counter);
1226
1227                                 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
1228                                 rstn_value->limit_exceeded &= ~(1 << i);
1229                                 rstn_value->limit_notified &= ~(1 << i);
1230                         }
1231                 }
1232         }
1233
1234 try_next_callback:
1235         return FALSE;
1236 }
1237
1238 static void __reset_time_counters_if_required(void)
1239 {
1240         reset_time_limits_context_s context;
1241
1242         if (g_system == NULL) {
1243                 STC_LOGE("stc monitor not initialized!");
1244                 return;
1245         }
1246
1247         context.now = time(NULL);
1248         context.week_start_ts = stc_time_get_week_start(context.now);
1249         context.day_start_ts = stc_time_get_day_start(context.now);
1250         context.is_updated = 0;
1251
1252         if (g_system->last_week_ts != context.week_start_ts) {
1253                 g_system->last_week_ts = context.week_start_ts;
1254                 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_WEEKLY);
1255         }
1256
1257         if (g_system->last_day_ts != context.day_start_ts) {
1258                 g_system->last_day_ts = context.day_start_ts;
1259                 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_DAILY);
1260         }
1261
1262         if (g_system->rstns) {
1263                 g_tree_foreach(g_system->rstns,
1264                                __reset_time_counter_foreach_rstn,
1265                                &context);
1266                 if (context.is_updated)
1267                         STC_LOGD("Counter reset completed month_start [%ld], week_start [%ld], day_start [%ld]",
1268                                  context.month_start_ts, g_system->last_week_ts, g_system->last_day_ts);
1269         }
1270 }
1271
1272 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1273                                  struct counter_arg *carg)
1274 {
1275         __reset_time_counters_if_required();
1276
1277         struct nfacct_rule counter = {
1278                 .carg = carg,
1279                 .name = {0},
1280                 .ifname = {0},
1281                 0
1282         };
1283
1284         classid_bytes_context_s context = {
1285                 .counter = &counter,
1286                 .bytes = bytes,
1287                 .data_limit_exceeded = FALSE,
1288         };
1289
1290         if (STC_DEBUG_LOG)
1291                 STC_LOGD("cnt_name %s", cnt_name);
1292
1293         if (!recreate_counter_by_name(cnt_name, &counter)) {
1294                 STC_LOGE("Can't parse counter name %s", cnt_name);
1295                 return;
1296         }
1297
1298         STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1299                  context.counter->classid, context.counter->iftype,
1300                  context.counter->iotype, context.counter->intend,
1301                  context.counter->ifname, context.bytes);
1302
1303         if (g_system->rstns)
1304                 g_tree_foreach(g_system->rstns,
1305                                __rstn_counter_update_foreach_classid,
1306                                &context);
1307
1308         if (g_system->apps)
1309                 g_tree_foreach(g_system->apps,
1310                                __apps_counter_update_foreach_classid,
1311                                &context);
1312 }
1313
1314 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1315                            void *user_data)
1316 {
1317         struct counter_arg *carg = user_data;
1318         char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1319         if (carg->initiate) {
1320                 /**
1321                  * TODO: this will be used when daemon starts to update existing
1322                  * counter data if present.
1323                  *
1324                  populate_counters(cnt_name, carg);
1325                  */
1326         } else {
1327                 int64_t *bytes_p =
1328                         (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1329                 int bytes = be64toh(*bytes_p);
1330                 if (bytes) {
1331                         ++carg->serialized_counters;
1332                         __fill_nfacct_result(cnt_name, bytes, carg);
1333                 }
1334         }
1335
1336         return 0;
1337 }
1338
1339 static int __post_fill_counters(void *user_data)
1340 {
1341         struct counter_arg *carg = user_data;
1342
1343         if (carg->initiate)
1344                 carg->initiate = 0;
1345
1346         return 0;
1347 }
1348
1349 static void __process_network_counter(struct genl *ans,
1350                                       struct counter_arg *carg)
1351 {
1352         struct netlink_serialization_params ser_params = {
1353                 .carg = carg,
1354                 .ans = ans,
1355                 .eval_attr = __fill_counters,
1356                 .post_eval_attr = __post_fill_counters,
1357         };
1358
1359         netlink_serialization_command *netlink =
1360                 netlink_create_command(&ser_params);
1361         if (!netlink) {
1362                 STC_LOGE("Can not create command");
1363                 return;
1364         }
1365
1366         netlink->deserialize_answer(&(netlink->params));
1367 }
1368
1369 static gboolean __process_contr_reply(GIOChannel *source,
1370                                       GIOCondition condition,
1371                                       gpointer user_data)
1372 {
1373         int sock = g_io_channel_unix_get_fd(source);
1374         struct genl *ans;
1375         int ret;
1376         stc_s *stc = stc_get_manager();
1377
1378         if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1379             (condition & G_IO_NVAL)) {
1380                 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1381
1382                 STC_LOGE("Counter socket received G_IO event, closing socket."
1383                          "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
1384                          (condition & G_IO_ERR), (condition & G_IO_HUP),
1385                          (condition & G_IO_NVAL));
1386                 __close_and_reopen_contr_sock(g_system);
1387                 return FALSE;
1388         }
1389
1390         ans = MALLOC0(struct genl, 1);
1391         if (ans == NULL) {
1392                 STC_LOGE("Failed allocate memory to genl reply message");
1393                 return TRUE;
1394         }
1395
1396         if (stc == NULL) {
1397                 STC_LOGE("Can't get stc data");
1398                 goto out;
1399         }
1400
1401         ret = read_netlink(sock, ans, sizeof(struct genl));
1402         /* STC_LOGD("Counter data received ret [%d]", ret); */
1403         if (ret == 0)
1404                 goto out;
1405
1406         stc->carg->ans_len = ret;
1407         stc->carg->last_run_time = time(NULL);
1408
1409         __process_network_counter(ans, stc->carg);
1410
1411         g_idle_add(__flush_apps_stats_to_database, NULL);
1412         g_idle_add(__flush_rstns_counter_to_database, NULL);
1413 out:
1414         FREE(ans);
1415         return TRUE;
1416 }
1417
1418 static gboolean __update_contr_cb(void *user_data)
1419 {
1420         /* Here we just sent command, answer we receive in another callback */
1421         stc_s *stc = stc_get_manager();
1422         ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1423         if (!stc->carg) {
1424                 stc->carg = MALLOC0(counter_arg_s, 1);
1425                 if (stc->carg == NULL)
1426                         return TRUE;  /* we need to continue the timer */
1427
1428                 stc->carg->sock = stc_monitor_get_counter_socket();
1429         }
1430
1431         /* STC_LOGD("Get all counters"); */
1432         nfacct_send_get_all(stc->carg);
1433
1434         /* we need to continue the timer */
1435         return TRUE;
1436 }
1437 #if 0
1438 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1439                                           gpointer data)
1440 {
1441         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1442         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1443
1444         __print_rstn(rstn_key, rstn_value);
1445         return FALSE;
1446 }
1447
1448 static void __rstn_tree_printall(void)
1449 {
1450         g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1451 }
1452 #endif
1453 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1454                                         const stc_rstn_key_s *key)
1455 {
1456         stc_rstn_value_s *lookup;
1457
1458         ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1459
1460         lookup = g_tree_lookup(rstns_tree, key);
1461
1462         return lookup;
1463 }
1464
1465 static gboolean __remove_restriction(gpointer key, gpointer value,
1466                                      gpointer data)
1467 {
1468         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1469         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1470
1471         /* rstn rule is already removed */
1472         if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
1473                 return FALSE;
1474
1475         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1476         __print_rstn(rstn_key, rstn_value);
1477         return FALSE;
1478 }
1479
1480 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1481                                         gpointer data)
1482 {
1483         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1484         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1485
1486         /* rstn rule is activated */
1487         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1488                 return FALSE;
1489
1490         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1491                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1492         else
1493                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1494
1495         __print_rstn(rstn_key, rstn_value);
1496
1497         return FALSE;
1498 }
1499
1500 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1501 {
1502         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1503         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1504
1505         /* rstn rule is activated */
1506         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1507                 return FALSE;
1508
1509         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1510                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1511         else
1512                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1513
1514         return FALSE;
1515 }
1516
1517 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1518 {
1519         stc_rstn_value_s *lookup_value;
1520
1521         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1522
1523         lookup_value = __rstn_lookup(g_system->rstns, key);
1524         if (!lookup_value) {
1525                 STC_LOGE("key not found");
1526                 return STC_ERROR_NO_DATA;
1527         }
1528
1529         /* remove counter also */
1530         table_counters_delete(lookup_value->restriction_id);
1531         __remove_restriction(key, lookup_value, NULL);
1532
1533         if (!g_tree_remove(g_system->rstns, key)) {
1534                 STC_LOGD("key not found");
1535                 return STC_ERROR_NO_DATA;
1536         }
1537
1538         return STC_ERROR_NONE;
1539 }
1540
1541 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1542                                    stc_rstn_value_s *value, gboolean debug)
1543 {
1544         stc_rstn_value_s *rstn_value;
1545
1546         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1547
1548         rstn_value = __rstn_lookup(g_system->rstns, key);
1549         if (!rstn_value) {
1550                 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1551                 if (!rstn_key) {
1552                         STC_LOGE("rstn_key allocation failed");
1553                         return STC_ERROR_OUT_OF_MEMORY;
1554                 }
1555
1556                 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1557                 if (!rstn_value) {
1558                         STC_LOGE("rstn_value allocation failed");
1559                         FREE(rstn_key);
1560                         return STC_ERROR_OUT_OF_MEMORY;
1561                 }
1562
1563                 rstn_key->app_id = g_strdup(key->app_id);
1564                 rstn_key->ifname = g_strdup(key->ifname);
1565                 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1566                 rstn_key->iftype = key->iftype;
1567                 rstn_key->roaming = key->roaming;
1568
1569                 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1570         }
1571
1572         rstn_value->restriction_id = value->restriction_id;
1573         rstn_value->rst_state = value->rst_state;
1574         rstn_value->classid = value->classid;
1575
1576         int i;
1577         for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1578                 rstn_value->limit[i] = value->limit[i];
1579                 rstn_value->counter[i] = 0;
1580         }
1581
1582         rstn_value->limit_exceeded = 0;
1583         rstn_value->limit_notified = 0;
1584         rstn_value->month_start_date = value->month_start_date;
1585         rstn_value->month_start_ts = value->month_start_ts;
1586
1587         if (debug == TRUE)
1588                 __add_restriction_debug(key, rstn_value, NULL);
1589         else
1590                 __add_restriction(key, rstn_value, NULL);
1591
1592         return STC_ERROR_NONE;
1593 }
1594
1595 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1596                                             void *user_data)
1597 {
1598         stc_cb_ret_e ret = STC_CONTINUE;
1599
1600         stc_rstn_key_s key;
1601         stc_rstn_value_s value;
1602
1603         memset(&key, 0, sizeof(stc_rstn_key_s));
1604         memset(&value, 0, sizeof(stc_rstn_value_s));
1605
1606         key.app_id = g_strdup(info->app_id);
1607         key.ifname = g_strdup(info->ifname);
1608         key.subscriber_id = g_strdup(info->subscriber_id);
1609         key.iftype = info->iftype;
1610         key.roaming = info->roaming;
1611
1612         value.rst_state = info->rst_state;
1613         value.restriction_id = info->restriction_id;
1614
1615         if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1616                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1617         else
1618                 value.classid = STC_UNKNOWN_CLASSID;
1619
1620         value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
1621         value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
1622         value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
1623         value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
1624         value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
1625
1626         if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1627                 ret = STC_CANCEL;
1628
1629         FREE(key.app_id);
1630         FREE(key.ifname);
1631         FREE(key.subscriber_id);
1632         return ret;
1633 }
1634
1635 static void __fill_restritions_list(void)
1636 {
1637         table_restrictions_foreach(__insert_restriction_cb, NULL);
1638         //__rstn_tree_printall();
1639 }
1640
1641 static gboolean __add_rstn_foreach_application(gpointer key,
1642                                                gpointer value,
1643                                                gpointer data)
1644 {
1645         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1646         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1647         gchar *app_id = (gchar *)data;
1648
1649         /* rstn rule is not for applications */
1650         if (rstn_key->app_id == NULL)
1651                 goto out;
1652
1653         /* rstn rule is not for this application */
1654         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1655                 goto out;
1656
1657         /* rstn rule is already applied */
1658         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1659                 goto out;
1660
1661         /* add restriction to system */
1662         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1663                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1664         else
1665                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1666
1667         __print_rstn(rstn_key, rstn_value);
1668 out:
1669         return FALSE;
1670 }
1671
1672 static void __add_rstns_for_application(gchar *app_id)
1673 {
1674         g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1675                        app_id);
1676 }
1677
1678 static void __add_application_by_interface(const char *app_id)
1679 {
1680         stc_app_key_s app_key;
1681         stc_app_value_s app_value;
1682
1683         if (app_id == NULL)
1684                 return;
1685
1686         memset(&app_key, 0, sizeof(stc_app_key_s));
1687         memset(&app_value, 0, sizeof(stc_app_value_s));
1688
1689         app_key.pkg_id = g_strdup(app_id);
1690         app_key.app_id = g_strdup(app_id);
1691
1692         app_value.type = STC_APP_TYPE_NONE;
1693         app_value.processes = NULL;
1694         app_value.counter.in_bytes = 0;
1695         app_value.counter.out_bytes = 0;
1696
1697         stc_monitor_application_add(app_key, app_value);
1698
1699         FREE(app_key.pkg_id);
1700         FREE(app_key.app_id);
1701 }
1702
1703 static int __vconf_get_int(const char *key, int *value)
1704 {
1705         int ret = 0;
1706
1707         ret = vconf_get_int(key, value);
1708         if (ret != VCONF_OK) {
1709                 STC_LOGE("Failed to get vconfkey [%s] value", key);
1710                 return -1;
1711         }
1712
1713         return 0;
1714 }
1715
1716 static int __vconf_set_int(const char *key, int value)
1717 {
1718         int ret = 0;
1719
1720         ret = vconf_set_int(key, value);
1721         if (ret != VCONF_OK) {
1722                 STC_LOGE("Failed to set vconfkey [%s] value", key);
1723                 return -1;
1724         }
1725
1726         return 0;
1727 }
1728
1729 static guint __get_background_state(void)
1730 {
1731         return g_system->background_state;;
1732 }
1733
1734 static void __set_background_state(guint state)
1735 {
1736         g_system->background_state = state;
1737 }
1738
1739 static gboolean __processes_tree_foreach_background(gpointer key,
1740                                                     gpointer value,
1741                                                     gpointer data)
1742 {
1743         stc_process_key_s *proc_key = (stc_process_key_s *)key;
1744         stc_app_key_s *app_key = (stc_app_key_s *)data;
1745
1746         if (g_system->background_state)
1747                 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1748         else
1749                 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1750
1751         return FALSE;
1752 }
1753
1754 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1755                                         gpointer data)
1756 {
1757         stc_app_key_s *app_key = (stc_app_key_s *)key;
1758         stc_app_value_s *app_value = (stc_app_value_s *)value;
1759
1760         if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1761                 g_tree_foreach(app_value->processes,
1762                                __processes_tree_foreach_background, app_key);
1763
1764         return FALSE;
1765 }
1766
1767 static stc_error_e __process_update_background(void)
1768 {
1769         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1770
1771         g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1772
1773         return STC_ERROR_NONE;
1774 }
1775
1776 static void __fill_exceptions_list(void)
1777 {
1778         stc_plugin_fill_exception_list();
1779 }
1780
1781 stc_error_e stc_monitor_init(void)
1782 {
1783         stc_system_s *system = MALLOC0(stc_system_s, 1);
1784         GIOChannel *gio = NULL;
1785
1786         ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1787
1788         /* initializing current classid */
1789         init_current_classid();
1790
1791         /* initializing cgroups */
1792         cgroup_init();
1793
1794         /* creating monitored application tree */
1795         system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1796                                        __apps_tree_key_free,
1797                                        __apps_tree_value_free);
1798
1799         system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1800                                         __rstns_tree_key_free,
1801                                         __rstns_tree_value_free);
1802
1803         /* create netlink socket for updating kernel counters */
1804         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1805         if (system->contr_sock < 0) {
1806                 STC_LOGE("failed to open socket");
1807                 FREE(system);
1808                 return STC_ERROR_FAIL;
1809         }
1810
1811         gio = g_io_channel_unix_new(system->contr_sock);
1812         system->contr_gsource_id =
1813                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1814                                (GIOFunc) __process_contr_reply,
1815                                NULL);
1816         g_io_channel_unref(gio);
1817
1818         g_system = system;
1819
1820         __add_application_by_interface(STC_TOTAL_DATACALL);
1821         __add_application_by_interface(STC_TOTAL_WIFI);
1822         __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1823         __add_application_by_interface(STC_TOTAL_IPV4);
1824         __add_application_by_interface(STC_TOTAL_IPV6);
1825
1826         /* creating restriction rules tree */
1827         __update_contr_cb(NULL);
1828
1829         /* registering periodic kernel counters update callback */
1830         g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1831                                                          __update_contr_cb,
1832                                                          NULL);
1833         if (g_system->contr_timer_id == 0) {
1834                 STC_LOGE("Failed to register kernel counters update timer");
1835                 __close_contr_sock(g_system);
1836                 return STC_ERROR_FAIL;
1837         }
1838
1839         __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1840                         (int *)&g_system->background_state);
1841
1842         __fill_exceptions_list();
1843         __fill_restritions_list();
1844
1845         return STC_ERROR_NONE;
1846 }
1847
1848 stc_error_e stc_monitor_deinit(void)
1849 {
1850         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1851
1852         /* close netlink socket for updating kernel counters */
1853         __close_contr_sock(g_system);
1854
1855         /* remove kernel counters update timer */
1856         if (g_system->contr_timer_id > 0) {
1857                 g_source_remove(g_system->contr_timer_id);
1858                 g_system->contr_timer_id = 0;
1859         }
1860
1861         /* destroy monitored application tree */
1862         g_tree_destroy(g_system->apps);
1863         g_system->apps = NULL;
1864
1865         /* destroy restriction rules tree */
1866         g_tree_destroy(g_system->rstns);
1867         g_system->rstns = NULL;
1868
1869         FREE(g_system);
1870
1871         return STC_ERROR_NONE;
1872 }
1873
1874 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1875                                         const stc_app_value_s app_value)
1876 {
1877         stc_error_e ret = STC_ERROR_NONE;
1878         stc_app_key_s *key;
1879         stc_app_value_s *value;
1880         stc_app_value_s *lookup;
1881
1882         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1883
1884         lookup = __application_lookup(g_system->apps, &app_key);
1885         if (lookup) {
1886                 STC_LOGD("app_key already present");
1887                 return STC_ERROR_NONE;
1888         }
1889
1890         key = MALLOC0(stc_app_key_s, 1);
1891         if (!key) {
1892                 STC_LOGE("key allocation failed");
1893                 return STC_ERROR_OUT_OF_MEMORY;
1894         }
1895
1896         value = MALLOC0(stc_app_value_s, 1);
1897         if (!value) {
1898                 STC_LOGE("value allocation failed");
1899                 FREE(key);
1900                 return STC_ERROR_OUT_OF_MEMORY;
1901         }
1902
1903         key->app_id = g_strdup(app_key.app_id);
1904         key->pkg_id = g_strdup(app_key.pkg_id);
1905
1906         value->type = app_value.type;
1907         value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1908         value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1909
1910         value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1911                                            __processes_tree_key_free,
1912                                            __processes_tree_value_free);
1913
1914         /* create cgroup and update classid */
1915         value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1916
1917         g_tree_insert(g_system->apps, key, value);
1918
1919         /* add nfacct rule for this classid */
1920         __add_application_monitor(key, value, stc_get_default_connection());
1921         __add_rstns_for_application(app_key.app_id);
1922
1923         return ret;
1924 }
1925
1926 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1927                                     const stc_process_key_s proc_key,
1928                                     const stc_process_value_s proc_value)
1929 {
1930         stc_error_e ret = STC_ERROR_NONE;
1931         stc_app_value_s *app_lookup;
1932         stc_process_key_s *key;
1933         stc_process_value_s *value;
1934         stc_process_value_s *proc_lookup;
1935
1936         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1937
1938         app_lookup = __application_lookup(g_system->apps, &app_key);
1939         if (!app_lookup) {
1940                 STC_LOGD("app_key not found");
1941                 return STC_ERROR_FAIL;
1942         }
1943
1944         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1945         if (proc_lookup) {
1946                 STC_LOGD("proc_key already present");
1947                 return STC_ERROR_NONE;
1948         }
1949
1950         key = MALLOC0(stc_process_key_s, 1);
1951         if (!key) {
1952                 STC_LOGE("key allocation failed");
1953                 return STC_ERROR_OUT_OF_MEMORY;
1954         }
1955
1956         value = MALLOC0(stc_process_value_s, 1);
1957         if (!value) {
1958                 STC_LOGE("value allocation failed");
1959                 FREE(key);
1960                 return STC_ERROR_OUT_OF_MEMORY;
1961         }
1962
1963         key->pid = proc_key.pid;
1964
1965         value->ground = proc_value.ground;
1966
1967         g_tree_insert(app_lookup->processes, key, value);
1968
1969         /* add pid to application cgroup */
1970         place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1971
1972         if (STC_DEBUG_LOG)
1973                 __apps_tree_printall();
1974
1975         return ret;
1976 }
1977
1978 API stc_error_e stc_monitor_process_remove(pid_t pid)
1979 {
1980         stc_error_e ret = STC_ERROR_NONE;
1981         stc_process_key_s proc_key = {
1982                 .pid = pid
1983         };
1984
1985         remove_pid_context_s context = {
1986                 .app_key = NULL,
1987                 .proc_key = &proc_key,
1988                 .entry_removed = FALSE,
1989         };
1990
1991         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1992
1993         g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1994                        &context);
1995
1996         if (context.entry_removed)
1997                 __application_remove_if_empty(context.app_key);
1998
1999         if (STC_DEBUG_LOG)
2000                 __apps_tree_printall();
2001
2002         return ret;
2003 }
2004
2005 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
2006                                               const stc_process_key_s proc_key,
2007                                               stc_app_state_e ground)
2008 {
2009         stc_error_e ret = STC_ERROR_NONE;
2010         stc_app_value_s *app_lookup;
2011         stc_process_value_s *proc_lookup;
2012
2013         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2014
2015         app_lookup = __application_lookup(g_system->apps, &app_key);
2016         if (!app_lookup) {
2017                 STC_LOGD("app_key not found");
2018                 return STC_ERROR_FAIL;
2019         }
2020
2021         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2022         if (!proc_lookup) {
2023                 STC_LOGD("proc_key not found");
2024                 return STC_ERROR_FAIL;
2025         }
2026
2027         if (proc_lookup->ground != ground)
2028                 proc_lookup->ground = ground;
2029
2030         if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
2031                 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
2032         else
2033                 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2034
2035         return ret;
2036 }
2037
2038 void stc_monitor_update_rstn_by_default_connection(void *data)
2039 {
2040         static default_connection_s old_connection;
2041         default_connection_s *new_connection = (default_connection_s *)data;
2042
2043         if (old_connection.path != NULL) {
2044                 if (g_system->apps)
2045                         g_tree_foreach(g_system->apps,
2046                                        __remove_application_monitor,
2047                                        (gpointer)&old_connection);
2048
2049                 if (g_system->rstns)
2050                         g_tree_foreach(g_system->rstns,
2051                                        __remove_restriction,
2052                                        (gpointer)&old_connection);
2053         }
2054
2055         FREE(old_connection.path);
2056         FREE(old_connection.ifname);
2057         old_connection.type = 0;
2058         old_connection.roaming = 0;
2059
2060         if (new_connection != NULL && new_connection->path != NULL) {
2061                 if (g_system->apps)
2062                         g_tree_foreach(g_system->apps,
2063                                        __add_application_monitor,
2064                                        (gpointer)new_connection);
2065
2066                 if (g_system->rstns)
2067                         g_tree_foreach(g_system->rstns, __add_restriction,
2068                                        NULL);
2069
2070                 old_connection.path = g_strdup(new_connection->path);
2071                 old_connection.ifname = g_strdup(new_connection->ifname);
2072                 old_connection.type = new_connection->type;
2073                 old_connection.roaming = new_connection->roaming;
2074         }
2075 }
2076
2077 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
2078 {
2079         stc_error_e ret;
2080
2081         stc_rstn_key_s key;
2082         stc_rstn_value_s value;
2083
2084         memset(&key, 0, sizeof(stc_rstn_key_s));
2085         memset(&value, 0, sizeof(stc_rstn_value_s));
2086
2087         key.app_id = g_strdup(info->app_id);
2088         key.ifname = g_strdup(info->ifname);
2089         key.subscriber_id = g_strdup(info->subscriber_id);
2090         key.iftype = info->iftype;
2091         key.roaming = info->roaming;
2092
2093         value.rst_state = info->rst_state;
2094         value.restriction_id = info->restriction_id;
2095
2096         if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
2097                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
2098         else
2099                 value.classid = STC_UNKNOWN_CLASSID;
2100
2101         if (value.classid == STC_BACKGROUND_APP_CLASSID) {
2102                 __set_background_state(TRUE);
2103                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
2104                 __process_update_background();
2105         }
2106
2107         value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
2108         value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
2109         value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
2110         value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
2111         value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
2112         value.month_start_date = info->month_start_date;
2113         value.month_start_ts = stc_time_get_month_start(time(NULL),
2114                                                         info->month_start_date);
2115
2116         ret = __rstn_tree_add(&key, &value, TRUE);
2117
2118         FREE(key.app_id);
2119         FREE(key.ifname);
2120         FREE(key.subscriber_id);
2121         return ret;
2122 }
2123
2124 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
2125 {
2126         stc_error_e ret;
2127
2128         stc_rstn_key_s key = {
2129                 .app_id = g_strdup(info->app_id),
2130                 .ifname = g_strdup(info->ifname),
2131                 .subscriber_id = g_strdup(info->subscriber_id),
2132                 .iftype = info->iftype,
2133                 .roaming = info->roaming,
2134         };
2135
2136         if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
2137                 __set_background_state(FALSE);
2138                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
2139                 __process_update_background();
2140         }
2141
2142         ret = __rstn_tree_remove(&key);
2143
2144         FREE(key.app_id);
2145         FREE(key.ifname);
2146         FREE(key.subscriber_id);
2147         return ret;
2148 }
2149
2150 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
2151 {
2152         return stc_plugin_check_exception_by_cmdline(cmdline);
2153 }
2154
2155 int stc_monitor_get_counter_socket(void)
2156 {
2157         return g_system->contr_sock;
2158 }