2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <linux/netlink.h>
19 #include <vconf-keys.h>
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"
27 #include "table-statistics.h"
28 #include "table-counters.h"
29 #include "stc-monitor.h"
30 #include "stc-manager-plugin.h"
32 #define MAX_INT_LENGTH 128
33 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
36 stc_app_key_s *app_key;
37 stc_process_key_s *proc_key;
38 gboolean entry_removed;
39 } remove_pid_context_s;
42 struct nfacct_rule *counter;
44 gboolean data_limit_reached;
45 } classid_bytes_context_s;
47 static stc_system_s *g_system = NULL;
49 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
51 if (counter->intend == NFACCT_WARN)
52 return NFACCT_JUMP_ACCEPT;
53 else if (counter->intend == NFACCT_BLOCK)
54 return NFACCT_JUMP_REJECT;
56 return NFACCT_JUMP_UNKNOWN;
59 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
62 return STC_ERROR_INVALID_PARAMETER;
64 counter->action = NFACCT_ACTION_INSERT;
65 counter->iotype = NFACCT_COUNTER_IN;
66 counter->jump = __get_jump_by_intend(counter);
67 counter->iptype = NFACCT_TYPE_IPV4;
68 counter->send_limit = 0;
69 counter->rcv_limit = 0;
71 return produce_net_rule(counter);
74 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
77 return STC_ERROR_INVALID_PARAMETER;
79 counter->action = NFACCT_ACTION_INSERT;
80 counter->iotype = NFACCT_COUNTER_OUT;
81 counter->jump = __get_jump_by_intend(counter);
82 counter->iptype = NFACCT_TYPE_IPV4;
83 counter->send_limit = 0;
84 counter->rcv_limit = 0;
86 return produce_net_rule(counter);
89 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
92 return STC_ERROR_INVALID_PARAMETER;
94 counter->action = NFACCT_ACTION_DELETE;
95 counter->iotype = NFACCT_COUNTER_IN;
96 counter->jump = __get_jump_by_intend(counter);
97 counter->iptype = NFACCT_TYPE_IPV4;
98 counter->send_limit = 0;
99 counter->rcv_limit = 0;
101 return produce_net_rule(counter);
104 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
107 return STC_ERROR_INVALID_PARAMETER;
109 counter->action = NFACCT_ACTION_DELETE;
110 counter->iotype = NFACCT_COUNTER_OUT;
111 counter->jump = __get_jump_by_intend(counter);
112 counter->iptype = NFACCT_TYPE_IPV4;
113 counter->send_limit = 0;
114 counter->rcv_limit = 0;
116 return produce_net_rule(counter);
119 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
122 return STC_ERROR_INVALID_PARAMETER;
124 counter->action = NFACCT_ACTION_INSERT;
125 counter->iotype = NFACCT_COUNTER_IN;
126 counter->jump = __get_jump_by_intend(counter);
127 counter->iptype = NFACCT_TYPE_IPV6;
128 counter->send_limit = 0;
129 counter->rcv_limit = 0;
131 return produce_net_rule(counter);
134 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
137 return STC_ERROR_INVALID_PARAMETER;
139 counter->action = NFACCT_ACTION_INSERT;
140 counter->iotype = NFACCT_COUNTER_OUT;
141 counter->jump = __get_jump_by_intend(counter);
142 counter->iptype = NFACCT_TYPE_IPV6;
143 counter->send_limit = 0;
144 counter->rcv_limit = 0;
146 return produce_net_rule(counter);
149 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
152 return STC_ERROR_INVALID_PARAMETER;
154 counter->action = NFACCT_ACTION_DELETE;
155 counter->iotype = NFACCT_COUNTER_IN;
156 counter->jump = __get_jump_by_intend(counter);
157 counter->iptype = NFACCT_TYPE_IPV6;
158 counter->send_limit = 0;
159 counter->rcv_limit = 0;
161 return produce_net_rule(counter);
164 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
167 return STC_ERROR_INVALID_PARAMETER;
169 counter->action = NFACCT_ACTION_DELETE;
170 counter->iotype = NFACCT_COUNTER_OUT;
171 counter->jump = __get_jump_by_intend(counter);
172 counter->iptype = NFACCT_TYPE_IPV6;
173 counter->send_limit = 0;
174 counter->rcv_limit = 0;
176 return produce_net_rule(counter);
179 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
180 gpointer UNUSED user_data)
182 stc_process_key_s *key_a = (stc_process_key_s *)a;
183 stc_process_key_s *key_b = (stc_process_key_s *)b;
185 return key_a->pid - key_b->pid;
188 static void __processes_tree_value_free(gpointer data)
190 stc_process_value_s *value = (stc_process_value_s *)data;
195 static void __processes_tree_key_free(gpointer data)
197 stc_process_key_s *key = (stc_process_key_s *)data;
202 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
203 gpointer UNUSED user_data)
205 stc_app_key_s *key_a = (stc_app_key_s *)a;
206 stc_app_key_s *key_b = (stc_app_key_s *)b;
209 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
213 return g_strcmp0(key_a->app_id, key_b->app_id);
216 static void __apps_tree_value_free(gpointer data)
218 stc_app_value_s *value = (stc_app_value_s *)data;
220 g_tree_destroy(value->processes);
221 value->processes = NULL;
226 static void __apps_tree_key_free(gpointer data)
228 stc_app_key_s *key = (stc_app_key_s *)data;
235 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
236 gpointer UNUSED user_data)
238 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
239 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
242 ret = g_strcmp0(key_a->app_id, key_b->app_id);
246 ret = g_strcmp0(key_a->ifname, key_b->ifname);
250 ret = g_strcmp0(key_a->imsi, key_b->imsi);
254 ret = key_a->iftype - key_b->iftype;
261 static void __rstns_tree_value_free(gpointer data)
263 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
268 static void __rstns_tree_key_free(gpointer data)
270 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
278 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
281 stc_process_key_s *proc_key = (stc_process_key_s *)key;
282 stc_process_value_s *proc_value = (stc_process_value_s *)value;
284 STC_LOGD("Process entry => PID [%d], Ground state [%d]",
285 proc_key->pid, proc_value->ground);
289 static void __processes_tree_printall(GTree *processes)
291 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
294 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
297 stc_app_key_s *app_key = (stc_app_key_s *)key;
298 stc_app_value_s *app_value = (stc_app_value_s *)value;
300 STC_LOGD("Application info => Pkg ID [%s], App ID [%s],"
301 " Type [%d], classid [%d],"
302 " counter [ in (%lld), out (%lld)]",
303 app_key->pkg_id, app_key->app_id,
304 app_value->type, app_value->classid,
305 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
307 __processes_tree_printall(app_value->processes);
312 static void __apps_tree_printall(void)
314 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
318 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
321 remove_pid_context_s *context = (remove_pid_context_s *)data;
322 stc_app_value_s *app_value = (stc_app_value_s *)value;
324 if (!g_tree_remove(app_value->processes, context->proc_key)) {
325 STC_LOGD("key not found");
329 context->entry_removed = TRUE;
330 context->app_key = (stc_app_key_s *)key;
335 static stc_app_value_s * __application_lookup(GTree *apps,
336 const stc_app_key_s *key)
338 stc_app_value_s *lookup;
340 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
342 lookup = g_tree_lookup(apps, key);
347 static stc_process_value_s * __process_lookup(GTree *processes,
348 const stc_process_key_s *key)
350 stc_process_value_s *lookup;
352 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
354 lookup = g_tree_lookup(processes, key);
359 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
362 guint *pid_count = (guint *)data;
367 static gboolean __add_application_monitor(gpointer key, gpointer value,
370 stc_app_value_s *app_value = (stc_app_value_s *)value;
371 default_connection_s *connection = (default_connection_s *)data;
372 stc_s *stc = stc_get_manager();
374 if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
375 app_value->classid == STC_TOTAL_WIFI_CLASSID ||
376 app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
379 if (stc && connection && connection->ifname) {
380 struct nfacct_rule counter;
383 stc->carg = MALLOC0(counter_arg_s, 1);
384 if (stc->carg == NULL)
387 stc->carg->sock = stc_monitor_get_counter_socket();
390 memset(&counter, 0, sizeof(struct nfacct_rule));
392 counter.carg = stc->carg;
393 counter.classid = app_value->classid;
394 counter.intend = NFACCT_COUNTER;
395 counter.iftype = connection->type;
396 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
398 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
399 __add_iptables_in(&counter);
400 __add_iptables_out(&counter);
401 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
402 __add_ip6tables_in(&counter);
403 __add_ip6tables_out(&counter);
405 __add_iptables_in(&counter);
406 __add_iptables_out(&counter);
407 __add_ip6tables_in(&counter);
408 __add_ip6tables_out(&counter);
415 static gboolean __remove_application_monitor(gpointer key, gpointer value,
418 stc_app_value_s *app_value = (stc_app_value_s *)value;
419 default_connection_s *connection = (default_connection_s *)data;
420 stc_s *stc = stc_get_manager();
422 if (stc && connection && connection->ifname) {
423 struct nfacct_rule counter;
426 stc->carg = MALLOC0(counter_arg_s, 1);
427 if (stc->carg == NULL)
430 stc->carg->sock = stc_monitor_get_counter_socket();
433 memset(&counter, 0, sizeof(struct nfacct_rule));
435 counter.carg = stc->carg;
436 counter.classid = app_value->classid;
437 counter.intend = NFACCT_COUNTER;
438 counter.iftype = connection->type;
439 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
441 __del_iptables_in(&counter);
442 __del_iptables_out(&counter);
443 __del_ip6tables_in(&counter);
444 __del_ip6tables_out(&counter);
450 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
452 STC_LOGI("rstn info => rstn_id [%llu], "
453 "app_id [%s], classid [%lu], ifname [%s], "
454 "iftype [%d], rst_state [%d], "
455 "limit [ (%lld) bytes], "
456 "warn_limit [ (%lld) bytes], "
457 "counter [ (%lld) bytes], "
458 "roaming [%d], imsi [%s]",
459 rstn_value->restriction_id,
460 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
461 rstn_key->iftype, rstn_value->rst_state,
462 rstn_value->data_limit,
463 rstn_value->data_warn_limit,
464 rstn_value->data_counter,
465 rstn_key->roaming, rstn_key->imsi);
468 static void __process_restriction(enum traffic_restriction_type rst_type,
469 stc_rstn_key_s *rstn_key,
470 stc_rstn_value_s *rstn_value, void *data)
472 int64_t effective_data_limit, effective_data_warn_limit;
473 default_connection_s *old_connection = (default_connection_s *)data;
474 default_connection_s *connection = NULL;
476 if (old_connection != NULL)
477 connection = old_connection;
479 connection = stc_get_default_connection();
481 /* no default ifname */
482 if (connection->ifname == NULL)
485 /* rstn not applicable for this interface */
486 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
487 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
490 /* classid is invalid */
491 if (rstn_value->classid == STC_UNKNOWN_CLASSID)
494 effective_data_limit = rstn_value->data_limit;
495 effective_data_warn_limit = rstn_value->data_warn_limit;
497 if (rst_type == RST_SET) {
498 /* TODO: Change this to runtime memory */
499 table_counters_info info;
501 memset(&info, 0, sizeof(table_counters_info));
502 table_counters_get(rstn_value->restriction_id, &info);
504 effective_data_limit -= info.data_counter;
505 effective_data_warn_limit -= info.data_counter;
507 if (effective_data_limit < 0) {
508 effective_data_limit = 0;
509 rstn_value->data_limit_reached = TRUE;
512 if (effective_data_warn_limit < 0)
513 effective_data_warn_limit = 0;
515 STC_LOGD("datausage [%lld] bytes", info.data_counter);
518 STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
519 "effective_data_warn_limit [%lld] bytes",
520 rstn_value->restriction_id, effective_data_limit,
521 effective_data_warn_limit);
525 if (effective_data_limit <= 0) {
526 char *default_ifname = stc_default_connection_get_ifname();
527 struct nfacct_rule counter;
528 stc_s *stc = stc_get_manager();
531 stc->carg = MALLOC0(counter_arg_s, 1);
532 if (stc->carg == NULL) {
533 g_free(default_ifname);
538 stc_monitor_get_counter_socket();
541 counter.carg = stc->carg;
542 counter.classid = rstn_value->classid;
543 counter.intend = NFACCT_BLOCK;
544 counter.iftype = rstn_key->iftype;
545 g_strlcpy(counter.ifname, default_ifname,
548 generate_counter_name(&counter);
549 g_free(default_ifname);
552 __add_iptables_in(&counter);
553 __add_iptables_out(&counter);
556 __add_ip6tables_in(&counter);
557 __add_ip6tables_out(&counter);
560 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
561 rstn_value->data_limit_reached = FALSE;
568 char *default_ifname = stc_default_connection_get_ifname();
569 struct nfacct_rule counter;
570 stc_s *stc = stc_get_manager();
573 stc->carg = MALLOC0(counter_arg_s, 1);
574 if (stc->carg == NULL) {
575 g_free(default_ifname);
580 stc_monitor_get_counter_socket();
583 counter.carg = stc->carg;
584 counter.classid = rstn_value->classid;
585 counter.intend = NFACCT_BLOCK;
586 counter.iftype = rstn_key->iftype;
587 g_strlcpy(counter.ifname, default_ifname,
590 generate_counter_name(&counter);
591 g_free(default_ifname);
594 __del_iptables_in(&counter);
595 __del_iptables_out(&counter);
598 __del_ip6tables_in(&counter);
599 __del_ip6tables_out(&counter);
601 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
602 rstn_value->data_limit_reached = FALSE;
610 static gboolean __remove_rstns_foreach_application(gpointer key,
614 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
615 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
616 gchar *app_id = (gchar *)data;
618 /* rstn rule is not for applications */
619 if (rstn_key->app_id == NULL)
622 /* rstn rule is not for this application */
623 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
626 /* rstn rule is already removed */
627 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
630 /* remove restriction from system */
631 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
633 __print_rstn(rstn_key, rstn_value);
638 static void __remove_rstns_for_application(gchar *app_id)
640 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
644 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
646 stc_error_e ret = STC_ERROR_NONE;
648 stc_app_value_s *lookup;
650 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
652 lookup = __application_lookup(g_system->apps, app_key);
654 STC_LOGE("app_key not found");
655 return STC_ERROR_NO_DATA;
658 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
662 /* remove nfacct rule for this classid */
663 __remove_application_monitor((gpointer) app_key, lookup,
664 stc_get_default_connection());
665 __remove_rstns_for_application(app_key->app_id);
668 if (!g_tree_remove(g_system->apps, app_key)) {
669 ret = STC_ERROR_NO_DATA;
670 STC_LOGE("key not found");
676 static stc_error_e __close_contr_sock(stc_system_s *system)
678 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
680 /* close netlink socket for updating kernel counters */
681 if (system->contr_sock != -1) {
682 close(system->contr_sock);
683 system->contr_sock = -1;
686 if (system->contr_gsource_id != 0) {
687 g_source_remove(system->contr_gsource_id);
688 system->contr_gsource_id = 0;
691 return STC_ERROR_NONE;
694 static gboolean __process_contr_reply(GIOChannel *source,
695 GIOCondition condition,
698 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
700 GIOChannel *gio = NULL;
701 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
703 /* close netlink socket for updating kernel counters */
704 if (system->contr_sock != -1) {
705 close(system->contr_sock);
706 system->contr_sock = -1;
709 if (system->contr_gsource_id != 0) {
710 g_source_remove(system->contr_gsource_id);
711 system->contr_gsource_id = 0;
714 /* create netlink socket for updating kernel counters */
715 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
716 if (!(system->contr_sock)) {
717 STC_LOGE("failed to open socket");
719 return STC_ERROR_FAIL;
722 gio = g_io_channel_unix_new(system->contr_sock);
723 system->contr_gsource_id =
724 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
725 (GIOFunc) __process_contr_reply,
727 g_io_channel_unref(gio);
729 return STC_ERROR_NONE;
732 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
733 stc_rstn_value_s *rstn_value,
734 classid_bytes_context_s *context)
736 switch (context->counter->iotype) {
737 case NFACCT_COUNTER_IN:
738 case NFACCT_COUNTER_OUT:
739 rstn_value->data_counter += context->bytes;
741 if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
742 rstn_value->warn_limit_crossed_notified == FALSE) {
745 char iftype[MAX_INT_LENGTH];
746 char byte[MAX_INT_LENGTH];
747 stc_s *stc = (stc_s *)stc_get_manager();
748 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
751 rv = stc_manager_dbus_emit_signal(stc->connection,
752 STC_DBUS_SERVICE_RESTRICTION_PATH,
753 STC_DBUS_INTERFACE_RESTRICTION,
754 "WarnThresholdCrossed",
755 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
757 rstn_value->warn_limit_crossed_notified = TRUE;
759 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
760 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
761 stc_send_warn_message_to_net_popup("warn threshold crossed",
767 if (rstn_value->data_counter >= rstn_value->data_limit &&
768 rstn_value->rstn_limit_crossed_notified == FALSE) {
771 char iftype[MAX_INT_LENGTH];
772 char byte[MAX_INT_LENGTH];
773 stc_s *stc = (stc_s *)stc_get_manager();
774 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
776 /* block immediately */
777 context->counter->intend = NFACCT_BLOCK;
778 __del_iptables_in(context->counter);
779 __del_iptables_out(context->counter);
780 __add_iptables_in(context->counter);
781 __add_iptables_out(context->counter);
783 __del_ip6tables_in(context->counter);
784 __del_ip6tables_out(context->counter);
785 __add_ip6tables_in(context->counter);
786 __add_ip6tables_out(context->counter);
787 context->counter->intend = NFACCT_COUNTER;
789 rstn_value->data_limit_reached = TRUE;
792 rv = stc_manager_dbus_emit_signal(stc->connection,
793 STC_DBUS_SERVICE_RESTRICTION_PATH,
794 STC_DBUS_INTERFACE_RESTRICTION,
795 "RestrictionThresholdCrossed",
796 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
798 rstn_value->rstn_limit_crossed_notified = TRUE;
800 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
801 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
802 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
803 "restriction_noti", rstn_key->app_id,
807 g_system->rstns_tree_updated = TRUE;
808 __print_rstn(rstn_key, rstn_value);
811 STC_LOGE("unknown iotype");
817 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
818 stc_rstn_value_s *rstn_value,
819 classid_bytes_context_s *context)
821 if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
822 context->counter->iftype == STC_IFACE_DATACALL) ||
823 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
824 context->counter->iftype == STC_IFACE_WIFI) ||
825 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
826 context->counter->iftype == STC_IFACE_BLUETOOTH)) {
827 context->counter->classid = rstn_value->classid;
828 return __rstn_counter_update(rstn_key, rstn_value, context);
834 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
839 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
840 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
841 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
844 if (context->counter->intend != NFACCT_COUNTER)
845 goto try_next_callback;
847 if (rstn_value->data_limit_reached == TRUE) {
848 context->data_limit_reached = TRUE;
849 goto try_next_callback;
852 classid = context->counter->classid;
853 rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
855 context->counter->classid = classid;
856 if (rstn_value->classid != context->counter->classid)
857 goto try_next_callback;
859 rv = __rstn_counter_update(rstn_key, rstn_value, context);
865 static gboolean __update_app_statistics(gpointer key, gpointer value,
868 stc_app_key_s *app_key = (stc_app_key_s *)key;
869 stc_app_value_s *app_value = (stc_app_value_s *)value;
870 time_t *touch_time = (time_t *)data;
871 stc_db_classid_iftype_key stat_key;
872 stc_db_app_stats stat;
873 default_connection_s *default_connection = stc_get_default_connection();
875 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
876 memset(&stat, 0 , sizeof(stc_db_app_stats));
878 stat_key.classid = app_value->classid;
879 stat_key.iftype = default_connection->type;
881 if (STC_IFACE_DATACALL == stat_key.iftype)
882 stat_key.imsi = g_strdup(default_connection->imsi);
884 stat_key.imsi = g_strdup("noneimsi");
886 g_strlcpy(stat_key.ifname, default_connection->ifname,
889 stat.app_id = g_strdup(app_key->app_id);
890 stat.snd_count = app_value->counter.out_bytes;
891 stat.rcv_count = app_value->counter.in_bytes;
892 stat.is_roaming = default_connection->roaming;
893 stat.ground = STC_APP_STATE_UNKNOWN;
895 table_statistics_insert(&stat_key, &stat, *touch_time);
897 app_value->counter.out_bytes = 0;
898 app_value->counter.in_bytes = 0;
906 static gboolean __flush_apps_stats_to_database(gpointer user_data)
908 time_t current_time = time(0);
910 if (g_system->apps_tree_updated == FALSE)
911 return G_SOURCE_REMOVE;
913 g_system->apps_tree_updated = FALSE;
916 g_tree_foreach(g_system->apps,
917 __update_app_statistics,
920 STC_LOGI("Flushed app stats to database");
921 return G_SOURCE_REMOVE;
924 static gboolean __update_counter_statistics(gpointer key, gpointer value,
927 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
928 table_counters_info info = {
929 .restriction_id = rstn_value->restriction_id,
930 .data_counter = rstn_value->data_counter
933 table_counters_update_counters(&info);
938 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
940 time_t current_time = time(0);
942 if (g_system->rstns_tree_updated == FALSE)
943 return G_SOURCE_REMOVE;
945 g_system->rstns_tree_updated = FALSE;
948 g_tree_foreach(g_system->rstns,
949 __update_counter_statistics,
952 STC_LOGI("Flushed rstns counters to database");
953 return G_SOURCE_REMOVE;
956 static void __app_counter_update(stc_app_key_s *app_key,
957 stc_app_value_s *app_value,
958 classid_bytes_context_s *context)
960 switch (context->counter->iotype) {
961 case NFACCT_COUNTER_IN:
962 app_value->data_usage.in_bytes += context->bytes;
963 app_value->counter.in_bytes = context->bytes;
964 g_system->apps_tree_updated = TRUE;
966 __apps_tree_foreach_print(app_key, app_value, NULL);
968 case NFACCT_COUNTER_OUT:
969 app_value->data_usage.out_bytes += context->bytes;
970 app_value->counter.out_bytes = context->bytes;
971 g_system->apps_tree_updated = TRUE;
973 __apps_tree_foreach_print(app_key, app_value, NULL);
976 STC_LOGE("unknown iotype");
980 static void __interface_counter_update(stc_app_key_s *app_key,
981 stc_app_value_s *app_value,
982 classid_bytes_context_s *context)
984 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
985 context->counter->iftype == STC_IFACE_DATACALL) ||
986 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
987 context->counter->iftype == STC_IFACE_WIFI) ||
988 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
989 context->counter->iftype == STC_IFACE_BLUETOOTH))
990 __app_counter_update(app_key, app_value, context);
994 static gboolean __apps_counter_update_foreach_classid(gpointer key,
998 stc_app_key_s *app_key = (stc_app_key_s *)key;
999 stc_app_value_s *app_value = (stc_app_value_s *)value;
1000 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1002 if (context->counter->intend != NFACCT_COUNTER)
1003 goto try_next_callback;
1005 __interface_counter_update(app_key, app_value, context);
1007 if (app_value->classid != context->counter->classid)
1008 goto try_next_callback;
1010 __app_counter_update(app_key, app_value, context);
1016 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1017 struct counter_arg *carg)
1019 struct nfacct_rule counter = {
1026 classid_bytes_context_s context = {
1027 .counter = &counter,
1029 .data_limit_reached = FALSE,
1032 STC_LOGD("cnt_name %s", cnt_name);
1034 if (!recreate_counter_by_name(cnt_name, &counter)) {
1035 STC_LOGE("Can't parse counter name %s", cnt_name);
1039 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1040 context.counter->classid, context.counter->iftype,
1041 context.counter->iotype, context.counter->intend,
1042 context.counter->ifname, context.bytes);
1044 if (g_system->rstns)
1045 g_tree_foreach(g_system->rstns,
1046 __rstn_counter_update_foreach_classid,
1050 g_tree_foreach(g_system->apps,
1051 __apps_counter_update_foreach_classid,
1055 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1058 struct counter_arg *carg = user_data;
1059 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1060 if (carg->initiate) {
1062 * TODO: this will be used when daemon starts to update existing
1063 * counter data if present.
1065 populate_counters(cnt_name, carg);
1069 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1070 int bytes = be64toh(*bytes_p);
1072 ++carg->serialized_counters;
1073 __fill_nfacct_result(cnt_name, bytes, carg);
1080 static int __post_fill_counters(void *user_data)
1082 struct counter_arg *carg = user_data;
1090 static void __process_network_counter(struct genl *ans,
1091 struct counter_arg *carg)
1093 struct netlink_serialization_params ser_params = {
1096 .eval_attr = __fill_counters,
1097 .post_eval_attr = __post_fill_counters,
1100 netlink_serialization_command *netlink =
1101 netlink_create_command(&ser_params);
1103 STC_LOGE("Can not create command");
1107 netlink->deserialize_answer(&(netlink->params));
1110 static gboolean __process_contr_reply(GIOChannel *source,
1111 GIOCondition condition,
1114 int sock = g_io_channel_unix_get_fd(source);
1117 stc_s *stc = stc_get_manager();
1119 if ((condition & G_IO_ERR) ||
1120 (condition & G_IO_HUP) ||
1121 (condition & G_IO_NVAL)) {
1122 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1124 STC_LOGE("Counter socket received G_IO event, closing socket."
1125 "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
1126 (condition & G_IO_ERR), (condition & G_IO_HUP),
1127 (condition & G_IO_NVAL));
1128 __close_and_reopen_contr_sock(g_system);
1133 STC_LOGE("Can't get stc data");
1137 ret = read_netlink(sock,
1138 &ans, sizeof(struct genl));
1139 /* STC_LOGD("Counter data received ret [%d]", ret); */
1143 stc->carg->ans_len = ret;
1144 __process_network_counter(&ans, stc->carg);
1146 g_idle_add(__flush_apps_stats_to_database, NULL);
1147 g_idle_add(__flush_rstns_counter_to_database, NULL);
1152 static gboolean __update_contr_cb(void *user_data)
1154 /* Here we just sent command, answer we receive in another callback */
1155 stc_s *stc = stc_get_manager();
1156 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1158 stc->carg = MALLOC0(counter_arg_s, 1);
1159 if (stc->carg == NULL)
1160 return TRUE; /* we need to continue the timer */
1162 stc->carg->sock = stc_monitor_get_counter_socket();
1165 /* STC_LOGD("Get all counters"); */
1166 nfacct_send_get_all(stc->carg);
1168 /* we need to continue the timer */
1172 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1175 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1176 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1178 __print_rstn(rstn_key, rstn_value);
1182 static void __rstn_tree_printall(void)
1184 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1187 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1188 const stc_rstn_key_s *key)
1190 stc_rstn_value_s *lookup;
1192 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1194 lookup = g_tree_lookup(rstns_tree, key);
1199 static gboolean __remove_restriction(gpointer key, gpointer value,
1202 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1203 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1205 /* rstn rule is already removed */
1206 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
1209 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1210 __print_rstn(rstn_key, rstn_value);
1214 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1217 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1218 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1220 /* rstn rule is activated */
1221 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1224 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1225 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1227 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1229 __print_rstn(rstn_key, rstn_value);
1234 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1236 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1237 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1239 /* rstn rule is activated */
1240 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1243 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1244 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1246 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1251 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1253 stc_rstn_value_s *lookup_value;
1255 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1257 lookup_value = __rstn_lookup(g_system->rstns, key);
1258 if (!lookup_value) {
1259 STC_LOGE("key not found");
1260 return STC_ERROR_NO_DATA;
1263 __remove_restriction(key, lookup_value, NULL);
1265 /* remove counter also */
1266 table_counters_delete(lookup_value->restriction_id);
1268 if (!g_tree_remove(g_system->rstns, key)) {
1269 STC_LOGD("key not found");
1270 return STC_ERROR_NO_DATA;
1273 return STC_ERROR_NONE;
1276 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1277 stc_rstn_value_s *value, gboolean debug)
1279 stc_rstn_value_s *rstn_value;
1281 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1283 rstn_value = __rstn_lookup(g_system->rstns, key);
1285 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1287 STC_LOGE("rstn_key allocation failed");
1288 return STC_ERROR_OUT_OF_MEMORY;
1291 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1293 STC_LOGE("rstn_value allocation failed");
1295 return STC_ERROR_OUT_OF_MEMORY;
1298 rstn_key->app_id = g_strdup(key->app_id);
1299 rstn_key->ifname = g_strdup(key->ifname);
1300 rstn_key->imsi = g_strdup(key->imsi);
1301 rstn_key->iftype = key->iftype;
1302 rstn_key->roaming = key->roaming;
1304 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1307 rstn_value->restriction_id = value->restriction_id;
1308 rstn_value->rst_state = value->rst_state;
1309 rstn_value->classid = value->classid;
1310 rstn_value->data_limit = value->data_limit;
1311 rstn_value->data_warn_limit = value->data_warn_limit;
1312 rstn_value->data_counter = 0;
1313 rstn_value->warn_limit_crossed_notified = FALSE;
1314 rstn_value->rstn_limit_crossed_notified = FALSE;
1317 __add_restriction_debug(key, rstn_value, NULL);
1319 __add_restriction(key, rstn_value, NULL);
1321 return STC_ERROR_NONE;
1324 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1327 stc_cb_ret_e ret = STC_CONTINUE;
1330 stc_rstn_value_s value;
1332 memset(&key, 0, sizeof(stc_rstn_key_s));
1333 memset(&value, 0, sizeof(stc_rstn_value_s));
1335 key.app_id = g_strdup(info->app_id);
1336 key.ifname = g_strdup(info->ifname);
1337 key.imsi = g_strdup(info->imsi);
1338 key.iftype = info->iftype;
1339 key.roaming = info->roaming;
1341 value.rst_state = info->rst_state;
1342 value.restriction_id = info->restriction_id;
1344 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1345 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1347 value.classid = STC_UNKNOWN_CLASSID;
1349 value.data_limit = info->data_limit;
1350 value.data_warn_limit = info->data_warn_limit;
1352 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1361 static void __fill_restritions_list(void)
1363 table_restrictions_foreach(__insert_restriction_cb, NULL);
1364 //__rstn_tree_printall();
1367 static gboolean __add_rstn_foreach_application(gpointer key,
1371 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1372 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1373 gchar *app_id = (gchar *)data;
1375 /* rstn rule is not for applications */
1376 if (rstn_key->app_id == NULL)
1379 /* rstn rule is not for this application */
1380 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1383 /* rstn rule is already applied */
1384 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1387 /* add restriction to system */
1388 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1389 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1391 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1393 __print_rstn(rstn_key, rstn_value);
1398 static void __add_rstns_for_application(gchar *app_id)
1400 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1404 static void __add_application_by_interface(const char *app_id)
1406 stc_app_key_s app_key;
1407 stc_app_value_s app_value;
1412 memset(&app_key, 0, sizeof(stc_app_key_s));
1413 memset(&app_value, 0, sizeof(stc_app_value_s));
1415 app_key.pkg_id = g_strdup(app_id);
1416 app_key.app_id = g_strdup(app_id);
1418 app_value.type = STC_APP_TYPE_NONE;
1419 app_value.processes = NULL;
1420 app_value.counter.in_bytes = 0;
1421 app_value.counter.out_bytes = 0;
1423 stc_monitor_application_add(app_key, app_value);
1425 FREE(app_key.pkg_id);
1426 FREE(app_key.app_id);
1429 static int __vconf_get_int(const char *key, int *value)
1433 ret = vconf_get_int(key, value);
1434 if (ret != VCONF_OK) {
1435 STC_LOGE("Failed to get vconfkey [%s] value", key);
1442 static int __vconf_set_int(const char *key, int value)
1446 ret = vconf_set_int(key, value);
1447 if (ret != VCONF_OK) {
1448 STC_LOGE("Failed to set vconfkey [%s] value", key);
1455 static guint __get_background_state(void)
1457 return g_system->background_state;;
1460 static void __set_background_state(guint state)
1462 g_system->background_state = state;
1465 static gboolean __processes_tree_foreach_background(gpointer key,
1469 stc_process_key_s *proc_key = (stc_process_key_s *)key;
1470 stc_app_key_s *app_key = (stc_app_key_s *)data;
1472 if (g_system->background_state)
1473 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1475 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1480 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1483 stc_app_key_s *app_key = (stc_app_key_s *)key;
1484 stc_app_value_s *app_value = (stc_app_value_s *)value;
1486 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1487 g_tree_foreach(app_value->processes,
1488 __processes_tree_foreach_background, app_key);
1493 static stc_error_e __process_update_background(void)
1495 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1497 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1499 return STC_ERROR_NONE;
1502 stc_error_e stc_monitor_init(void)
1504 stc_system_s *system = MALLOC0(stc_system_s, 1);
1505 GIOChannel *gio = NULL;
1507 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1509 /* initializing cgroups */
1512 /* creating monitored application tree */
1513 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1514 __apps_tree_key_free,
1515 __apps_tree_value_free);
1517 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1518 __rstns_tree_key_free,
1519 __rstns_tree_value_free);
1521 /* create netlink socket for updating kernel counters */
1522 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1523 if (!(system->contr_sock)) {
1524 STC_LOGE("failed to open socket");
1526 return STC_ERROR_FAIL;
1529 gio = g_io_channel_unix_new(system->contr_sock);
1530 system->contr_gsource_id =
1531 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1532 (GIOFunc) __process_contr_reply,
1534 g_io_channel_unref(gio);
1538 __add_application_by_interface(STC_TOTAL_DATACALL);
1539 __add_application_by_interface(STC_TOTAL_WIFI);
1540 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1541 __add_application_by_interface(STC_TOTAL_IPV4);
1542 __add_application_by_interface(STC_TOTAL_IPV6);
1544 /* creating restriction rules tree */
1545 __update_contr_cb(NULL);
1547 /* registering periodic kernel counters update callback */
1548 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1551 if (g_system->contr_timer_id == 0) {
1552 STC_LOGE("Failed to register kernel counters update timer");
1553 __close_contr_sock(g_system);
1554 return STC_ERROR_FAIL;
1557 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1558 (int *)&g_system->background_state);
1560 __fill_restritions_list();
1562 return STC_ERROR_NONE;
1565 stc_error_e stc_monitor_deinit(void)
1567 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1569 /* close netlink socket for updating kernel counters */
1570 __close_contr_sock(g_system);
1572 /* remove kernel counters update timer */
1573 if (g_system->contr_timer_id > 0) {
1574 g_source_remove(g_system->contr_timer_id);
1575 g_system->contr_timer_id = 0;
1578 /* destroy monitored application tree */
1579 g_tree_destroy(g_system->apps);
1580 g_system->apps = NULL;
1582 /* destroy restriction rules tree */
1583 g_tree_destroy(g_system->rstns);
1584 g_system->rstns = NULL;
1588 return STC_ERROR_NONE;
1591 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1592 const stc_app_value_s app_value)
1594 stc_error_e ret = STC_ERROR_NONE;
1596 stc_app_value_s *value;
1597 stc_app_value_s *lookup;
1599 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1601 lookup = __application_lookup(g_system->apps, &app_key);
1603 STC_LOGD("app_key already present");
1604 return STC_ERROR_NONE;
1607 key = MALLOC0(stc_app_key_s, 1);
1609 STC_LOGE("key allocation failed");
1610 return STC_ERROR_OUT_OF_MEMORY;
1613 value = MALLOC0(stc_app_value_s, 1);
1615 STC_LOGE("value allocation failed");
1617 return STC_ERROR_OUT_OF_MEMORY;
1620 key->app_id = g_strdup(app_key.app_id);
1621 key->pkg_id = g_strdup(app_key.pkg_id);
1623 value->type = app_value.type;
1624 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1625 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1627 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1628 __processes_tree_key_free,
1629 __processes_tree_value_free);
1631 /* create cgroup and update classid */
1632 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1634 g_tree_insert(g_system->apps, key, value);
1636 /* add nfacct rule for this classid */
1637 __add_application_monitor(key, value, stc_get_default_connection());
1638 __add_rstns_for_application(app_key.app_id);
1643 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1644 const stc_process_key_s proc_key,
1645 const stc_process_value_s proc_value)
1647 stc_error_e ret = STC_ERROR_NONE;
1648 stc_app_value_s *app_lookup;
1649 stc_process_key_s *key;
1650 stc_process_value_s *value;
1651 stc_process_value_s *proc_lookup;
1653 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1655 app_lookup = __application_lookup(g_system->apps, &app_key);
1657 STC_LOGD("app_key not found");
1658 return STC_ERROR_FAIL;
1661 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1663 STC_LOGD("proc_key already present");
1664 return STC_ERROR_NONE;
1667 key = MALLOC0(stc_process_key_s, 1);
1669 STC_LOGE("key allocation failed");
1670 return STC_ERROR_OUT_OF_MEMORY;
1673 value = MALLOC0(stc_process_value_s, 1);
1675 STC_LOGE("value allocation failed");
1677 return STC_ERROR_OUT_OF_MEMORY;
1680 key->pid = proc_key.pid;
1682 value->ground = proc_value.ground;
1684 g_tree_insert(app_lookup->processes, key, value);
1686 /* add pid to application cgroup */
1687 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1692 stc_error_e stc_monitor_process_remove(pid_t pid)
1694 stc_error_e ret = STC_ERROR_NONE;
1695 stc_process_key_s proc_key = {
1699 remove_pid_context_s context = {
1701 .proc_key = &proc_key,
1702 .entry_removed = FALSE,
1705 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1707 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1710 if (context.entry_removed)
1711 __application_remove_if_empty(context.app_key);
1716 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1717 const stc_process_key_s proc_key,
1718 stc_app_state_e ground)
1720 stc_error_e ret = STC_ERROR_NONE;
1721 stc_app_value_s *app_lookup;
1722 stc_process_value_s *proc_lookup;
1724 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1726 app_lookup = __application_lookup(g_system->apps, &app_key);
1728 STC_LOGD("app_key not found");
1729 return STC_ERROR_FAIL;
1732 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1734 STC_LOGD("proc_key not found");
1735 return STC_ERROR_FAIL;
1738 if (proc_lookup->ground != ground)
1739 proc_lookup->ground = ground;
1741 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1742 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1744 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1749 void stc_monitor_update_rstn_by_default_connection(void *data)
1751 static default_connection_s old_connection;
1752 default_connection_s *new_connection = (default_connection_s *)data;
1754 if (old_connection.path != NULL) {
1756 g_tree_foreach(g_system->apps,
1757 __remove_application_monitor,
1758 (gpointer)&old_connection);
1760 if (g_system->rstns)
1761 g_tree_foreach(g_system->rstns,
1762 __remove_restriction,
1763 (gpointer)&old_connection);
1766 FREE(old_connection.path);
1767 FREE(old_connection.ifname);
1768 old_connection.type = 0;
1769 old_connection.roaming = 0;
1771 if (new_connection != NULL && new_connection->path != NULL) {
1773 g_tree_foreach(g_system->apps,
1774 __add_application_monitor,
1775 (gpointer)new_connection);
1777 if (g_system->rstns)
1778 g_tree_foreach(g_system->rstns, __add_restriction,
1781 old_connection.path = g_strdup(new_connection->path);
1782 old_connection.ifname = g_strdup(new_connection->ifname);
1783 old_connection.type = new_connection->type;
1784 old_connection.roaming = new_connection->roaming;
1788 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1793 stc_rstn_value_s value;
1795 memset(&key, 0, sizeof(stc_rstn_key_s));
1796 memset(&value, 0, sizeof(stc_rstn_value_s));
1798 key.app_id = g_strdup(info->app_id);
1799 key.ifname = g_strdup(info->ifname);
1800 key.imsi = g_strdup(info->imsi);
1801 key.iftype = info->iftype;
1802 key.roaming = info->roaming;
1804 value.rst_state = info->rst_state;
1805 value.restriction_id = info->restriction_id;
1807 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1808 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1810 value.classid = STC_UNKNOWN_CLASSID;
1812 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1813 __set_background_state(TRUE);
1814 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
1815 __process_update_background();
1818 value.data_limit = info->data_limit;
1819 value.data_warn_limit = info->data_warn_limit;
1821 ret = __rstn_tree_add(&key, &value, TRUE);
1829 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1833 stc_rstn_key_s key = {
1834 .app_id = g_strdup(info->app_id),
1835 .ifname = g_strdup(info->ifname),
1836 .imsi = g_strdup(info->imsi),
1837 .iftype = info->iftype,
1838 .roaming = info->roaming,
1841 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
1842 __set_background_state(FALSE);
1843 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
1844 __process_update_background();
1847 ret = __rstn_tree_remove(&key);
1855 int stc_monitor_get_counter_socket(void)
1857 return g_system->contr_sock;