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-exception.h"
30 #include "stc-monitor.h"
31 #include "stc-manager-plugin.h"
33 #define MAX_INT_LENGTH 128
34 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
37 stc_app_key_s *app_key;
38 stc_process_key_s *proc_key;
39 gboolean entry_removed;
40 } remove_pid_context_s;
43 struct nfacct_rule *counter;
45 gboolean data_limit_reached;
46 } classid_bytes_context_s;
48 static stc_system_s *g_system = NULL;
50 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
52 if (counter->intend == NFACCT_WARN)
53 return NFACCT_JUMP_ACCEPT;
54 else if (counter->intend == NFACCT_BLOCK)
55 return NFACCT_JUMP_REJECT;
57 return NFACCT_JUMP_UNKNOWN;
60 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
63 return STC_ERROR_INVALID_PARAMETER;
65 counter->action = NFACCT_ACTION_INSERT;
66 counter->iotype = NFACCT_COUNTER_IN;
67 counter->jump = __get_jump_by_intend(counter);
68 counter->iptype = NFACCT_TYPE_IPV4;
69 counter->send_limit = 0;
70 counter->rcv_limit = 0;
72 return produce_net_rule(counter);
75 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
78 return STC_ERROR_INVALID_PARAMETER;
80 counter->action = NFACCT_ACTION_INSERT;
81 counter->iotype = NFACCT_COUNTER_OUT;
82 counter->jump = __get_jump_by_intend(counter);
83 counter->iptype = NFACCT_TYPE_IPV4;
84 counter->send_limit = 0;
85 counter->rcv_limit = 0;
87 return produce_net_rule(counter);
90 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
93 return STC_ERROR_INVALID_PARAMETER;
95 counter->action = NFACCT_ACTION_DELETE;
96 counter->iotype = NFACCT_COUNTER_IN;
97 counter->jump = __get_jump_by_intend(counter);
98 counter->iptype = NFACCT_TYPE_IPV4;
99 counter->send_limit = 0;
100 counter->rcv_limit = 0;
102 return produce_net_rule(counter);
105 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
108 return STC_ERROR_INVALID_PARAMETER;
110 counter->action = NFACCT_ACTION_DELETE;
111 counter->iotype = NFACCT_COUNTER_OUT;
112 counter->jump = __get_jump_by_intend(counter);
113 counter->iptype = NFACCT_TYPE_IPV4;
114 counter->send_limit = 0;
115 counter->rcv_limit = 0;
117 return produce_net_rule(counter);
120 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
123 return STC_ERROR_INVALID_PARAMETER;
125 counter->action = NFACCT_ACTION_INSERT;
126 counter->iotype = NFACCT_COUNTER_IN;
127 counter->jump = __get_jump_by_intend(counter);
128 counter->iptype = NFACCT_TYPE_IPV6;
129 counter->send_limit = 0;
130 counter->rcv_limit = 0;
132 return produce_net_rule(counter);
135 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
138 return STC_ERROR_INVALID_PARAMETER;
140 counter->action = NFACCT_ACTION_INSERT;
141 counter->iotype = NFACCT_COUNTER_OUT;
142 counter->jump = __get_jump_by_intend(counter);
143 counter->iptype = NFACCT_TYPE_IPV6;
144 counter->send_limit = 0;
145 counter->rcv_limit = 0;
147 return produce_net_rule(counter);
150 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
153 return STC_ERROR_INVALID_PARAMETER;
155 counter->action = NFACCT_ACTION_DELETE;
156 counter->iotype = NFACCT_COUNTER_IN;
157 counter->jump = __get_jump_by_intend(counter);
158 counter->iptype = NFACCT_TYPE_IPV6;
159 counter->send_limit = 0;
160 counter->rcv_limit = 0;
162 return produce_net_rule(counter);
165 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
168 return STC_ERROR_INVALID_PARAMETER;
170 counter->action = NFACCT_ACTION_DELETE;
171 counter->iotype = NFACCT_COUNTER_OUT;
172 counter->jump = __get_jump_by_intend(counter);
173 counter->iptype = NFACCT_TYPE_IPV6;
174 counter->send_limit = 0;
175 counter->rcv_limit = 0;
177 return produce_net_rule(counter);
180 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
181 gpointer UNUSED user_data)
183 stc_process_key_s *key_a = (stc_process_key_s *)a;
184 stc_process_key_s *key_b = (stc_process_key_s *)b;
186 return key_a->pid - key_b->pid;
189 static void __processes_tree_value_free(gpointer data)
191 stc_process_value_s *value = (stc_process_value_s *)data;
196 static void __processes_tree_key_free(gpointer data)
198 stc_process_key_s *key = (stc_process_key_s *)data;
203 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
204 gpointer UNUSED user_data)
206 stc_app_key_s *key_a = (stc_app_key_s *)a;
207 stc_app_key_s *key_b = (stc_app_key_s *)b;
210 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
214 return g_strcmp0(key_a->app_id, key_b->app_id);
217 static void __apps_tree_value_free(gpointer data)
219 stc_app_value_s *value = (stc_app_value_s *)data;
221 g_tree_destroy(value->processes);
222 value->processes = NULL;
227 static void __apps_tree_key_free(gpointer data)
229 stc_app_key_s *key = (stc_app_key_s *)data;
236 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
237 gpointer UNUSED user_data)
239 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
240 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
243 ret = g_strcmp0(key_a->app_id, key_b->app_id);
247 ret = g_strcmp0(key_a->ifname, key_b->ifname);
251 ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
255 ret = key_a->iftype - key_b->iftype;
262 static void __rstns_tree_value_free(gpointer data)
264 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
269 static void __rstns_tree_key_free(gpointer data)
271 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
275 FREE(key->subscriber_id);
280 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
283 stc_process_key_s *proc_key = (stc_process_key_s *)key;
284 stc_process_value_s *proc_value = (stc_process_value_s *)value;
286 STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
287 proc_key->pid, proc_value->ground);
291 static void __processes_tree_printall(GTree *processes)
293 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
296 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
299 stc_app_key_s *app_key = (stc_app_key_s *)key;
300 stc_app_value_s *app_value = (stc_app_value_s *)value;
302 STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
303 "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
304 " counter [ in (%lld), out (%lld)]",
305 app_key->pkg_id, app_key->app_id,
306 app_value->type, app_value->classid,
307 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
309 __processes_tree_printall(app_value->processes);
313 static void __apps_tree_printall(void)
315 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
319 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
322 remove_pid_context_s *context = (remove_pid_context_s *)data;
323 stc_app_value_s *app_value = (stc_app_value_s *)value;
325 if (!g_tree_remove(app_value->processes, context->proc_key)) {
327 STC_LOGD("key not found");
332 context->entry_removed = TRUE;
333 context->app_key = (stc_app_key_s *)key;
338 static stc_app_value_s * __application_lookup(GTree *apps,
339 const stc_app_key_s *key)
341 stc_app_value_s *lookup;
343 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
345 lookup = g_tree_lookup(apps, key);
350 static stc_process_value_s * __process_lookup(GTree *processes,
351 const stc_process_key_s *key)
353 stc_process_value_s *lookup;
355 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
357 lookup = g_tree_lookup(processes, key);
362 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
365 guint *pid_count = (guint *)data;
370 static gboolean __add_application_monitor(gpointer key, gpointer value,
373 stc_app_value_s *app_value = (stc_app_value_s *)value;
374 default_connection_s *connection = (default_connection_s *)data;
375 stc_s *stc = stc_get_manager();
377 if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
378 app_value->classid == STC_TOTAL_WIFI_CLASSID ||
379 app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
382 if (stc && connection && connection->ifname) {
383 struct nfacct_rule counter;
386 stc->carg = MALLOC0(counter_arg_s, 1);
387 if (stc->carg == NULL)
390 stc->carg->sock = stc_monitor_get_counter_socket();
393 memset(&counter, 0, sizeof(struct nfacct_rule));
395 counter.carg = stc->carg;
396 counter.classid = app_value->classid;
397 counter.intend = NFACCT_COUNTER;
398 counter.iftype = connection->type;
399 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
401 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
402 __add_iptables_in(&counter);
403 __add_iptables_out(&counter);
404 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
405 __add_ip6tables_in(&counter);
406 __add_ip6tables_out(&counter);
408 __add_iptables_in(&counter);
409 __add_iptables_out(&counter);
410 __add_ip6tables_in(&counter);
411 __add_ip6tables_out(&counter);
418 static gboolean __remove_application_monitor(gpointer key, gpointer value,
421 stc_app_value_s *app_value = (stc_app_value_s *)value;
422 default_connection_s *connection = (default_connection_s *)data;
423 stc_s *stc = stc_get_manager();
425 if (stc && connection && connection->ifname) {
426 struct nfacct_rule counter;
429 stc->carg = MALLOC0(counter_arg_s, 1);
430 if (stc->carg == NULL)
433 stc->carg->sock = stc_monitor_get_counter_socket();
436 memset(&counter, 0, sizeof(struct nfacct_rule));
438 counter.carg = stc->carg;
439 counter.classid = app_value->classid;
440 counter.intend = NFACCT_COUNTER;
441 counter.iftype = connection->type;
442 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
444 __del_iptables_in(&counter);
445 __del_iptables_out(&counter);
446 __del_ip6tables_in(&counter);
447 __del_ip6tables_out(&counter);
453 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
455 STC_LOGI("rstn info => rstn_id [%llu], "
456 "app_id [%s], classid [%lu], ifname [%s], "
457 "iftype [%d], rst_state [%d], "
458 "limit [ (%lld) bytes], "
459 "warn_limit [ (%lld) bytes], "
460 "counter [ (%lld) bytes], "
461 "roaming [%d], subscriber_id [%s]",
462 rstn_value->restriction_id,
463 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
464 rstn_key->iftype, rstn_value->rst_state,
465 rstn_value->data_limit,
466 rstn_value->data_warn_limit,
467 rstn_value->data_counter,
468 rstn_key->roaming, rstn_key->subscriber_id);
471 static void __process_restriction(enum traffic_restriction_type rst_type,
472 stc_rstn_key_s *rstn_key,
473 stc_rstn_value_s *rstn_value, void *data)
475 int64_t effective_data_limit, effective_data_warn_limit;
476 default_connection_s *old_connection = (default_connection_s *)data;
477 default_connection_s *connection = NULL;
479 if (old_connection != NULL)
480 connection = old_connection;
482 connection = stc_get_default_connection();
484 /* no default ifname */
485 if (connection->ifname == NULL)
488 /* rstn not applicable for this interface */
489 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
490 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
493 /* classid is invalid */
494 if (rstn_value->classid == STC_UNKNOWN_CLASSID)
497 effective_data_limit = rstn_value->data_limit;
498 effective_data_warn_limit = rstn_value->data_warn_limit;
500 if (rst_type == RST_SET) {
501 /* TODO: Change this to runtime memory */
502 table_counters_info info;
504 memset(&info, 0, sizeof(table_counters_info));
505 table_counters_get(rstn_value->restriction_id, &info);
507 effective_data_limit -= info.data_counter;
508 effective_data_warn_limit -= info.data_counter;
510 if (effective_data_limit < 0) {
511 effective_data_limit = 0;
512 rstn_value->data_limit_reached = TRUE;
515 if (effective_data_warn_limit < 0)
516 effective_data_warn_limit = 0;
518 STC_LOGD("datausage [%lld] bytes", info.data_counter);
521 STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
522 "effective_data_warn_limit [%lld] bytes",
523 rstn_value->restriction_id, effective_data_limit,
524 effective_data_warn_limit);
528 if (effective_data_limit <= 0) {
529 char *default_ifname = stc_default_connection_get_ifname();
530 struct nfacct_rule counter;
531 stc_s *stc = stc_get_manager();
534 stc->carg = MALLOC0(counter_arg_s, 1);
535 if (stc->carg == NULL) {
536 g_free(default_ifname);
541 stc_monitor_get_counter_socket();
544 counter.carg = stc->carg;
545 counter.classid = rstn_value->classid;
546 counter.intend = NFACCT_BLOCK;
547 counter.iftype = rstn_key->iftype;
548 g_strlcpy(counter.ifname, default_ifname,
551 generate_counter_name(&counter);
552 g_free(default_ifname);
555 __add_iptables_in(&counter);
556 __add_iptables_out(&counter);
559 __add_ip6tables_in(&counter);
560 __add_ip6tables_out(&counter);
563 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
564 rstn_value->data_limit_reached = FALSE;
571 char *default_ifname = stc_default_connection_get_ifname();
572 struct nfacct_rule counter;
573 stc_s *stc = stc_get_manager();
576 stc->carg = MALLOC0(counter_arg_s, 1);
577 if (stc->carg == NULL) {
578 g_free(default_ifname);
583 stc_monitor_get_counter_socket();
586 counter.carg = stc->carg;
587 counter.classid = rstn_value->classid;
588 counter.intend = NFACCT_BLOCK;
589 counter.iftype = rstn_key->iftype;
590 g_strlcpy(counter.ifname, default_ifname,
593 generate_counter_name(&counter);
594 g_free(default_ifname);
597 __del_iptables_in(&counter);
598 __del_iptables_out(&counter);
601 __del_ip6tables_in(&counter);
602 __del_ip6tables_out(&counter);
604 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
605 rstn_value->data_limit_reached = FALSE;
613 static gboolean __remove_rstns_foreach_application(gpointer key,
617 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
618 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
619 gchar *app_id = (gchar *)data;
621 /* rstn rule is not for applications */
622 if (rstn_key->app_id == NULL)
625 /* rstn rule is not for this application */
626 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
629 /* rstn rule is already removed */
630 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
633 /* remove restriction from system */
634 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
636 __print_rstn(rstn_key, rstn_value);
641 static void __remove_rstns_for_application(gchar *app_id)
643 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
647 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
649 stc_error_e ret = STC_ERROR_NONE;
651 stc_app_value_s *lookup;
653 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
655 lookup = __application_lookup(g_system->apps, app_key);
657 STC_LOGE("app_key not found");
658 return STC_ERROR_NO_DATA;
661 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
665 /* remove nfacct rule for this classid */
666 __remove_application_monitor((gpointer) app_key, lookup,
667 stc_get_default_connection());
668 __remove_rstns_for_application(app_key->app_id);
671 if (!g_tree_remove(g_system->apps, app_key)) {
672 ret = STC_ERROR_NO_DATA;
673 STC_LOGE("key not found");
679 static stc_error_e __close_contr_sock(stc_system_s *system)
681 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
683 /* close netlink socket for updating kernel counters */
684 if (system->contr_sock != -1) {
685 close(system->contr_sock);
686 system->contr_sock = -1;
689 if (system->contr_gsource_id != 0) {
690 g_source_remove(system->contr_gsource_id);
691 system->contr_gsource_id = 0;
694 return STC_ERROR_NONE;
697 static gboolean __process_contr_reply(GIOChannel *source,
698 GIOCondition condition,
701 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
703 GIOChannel *gio = NULL;
704 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
706 /* close netlink socket for updating kernel counters */
707 if (system->contr_sock != -1) {
708 close(system->contr_sock);
709 system->contr_sock = -1;
712 if (system->contr_gsource_id != 0) {
713 g_source_remove(system->contr_gsource_id);
714 system->contr_gsource_id = 0;
717 /* create netlink socket for updating kernel counters */
718 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
719 if (!(system->contr_sock)) {
720 STC_LOGE("failed to open socket");
722 return STC_ERROR_FAIL;
725 gio = g_io_channel_unix_new(system->contr_sock);
726 system->contr_gsource_id =
727 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
728 (GIOFunc) __process_contr_reply,
730 g_io_channel_unref(gio);
732 return STC_ERROR_NONE;
735 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
736 stc_rstn_value_s *rstn_value,
737 classid_bytes_context_s *context)
739 switch (context->counter->iotype) {
740 case NFACCT_COUNTER_IN:
741 case NFACCT_COUNTER_OUT:
742 rstn_value->data_counter += context->bytes;
744 if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
745 rstn_value->warn_limit_crossed_notified == FALSE) {
748 char iftype[MAX_INT_LENGTH];
749 char byte[MAX_INT_LENGTH];
750 stc_s *stc = (stc_s *)stc_get_manager();
751 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
754 rv = stc_manager_dbus_emit_signal(stc->connection,
755 STC_DBUS_SERVICE_RESTRICTION_PATH,
756 STC_DBUS_INTERFACE_RESTRICTION,
757 "WarnThresholdCrossed",
758 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
760 rstn_value->warn_limit_crossed_notified = TRUE;
762 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
763 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
764 stc_send_warn_message_to_net_popup("warn threshold crossed",
770 if (rstn_value->data_counter >= rstn_value->data_limit &&
771 rstn_value->rstn_limit_crossed_notified == FALSE) {
774 char iftype[MAX_INT_LENGTH];
775 char byte[MAX_INT_LENGTH];
776 stc_s *stc = (stc_s *)stc_get_manager();
777 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
779 /* block immediately */
780 context->counter->intend = NFACCT_BLOCK;
781 __del_iptables_in(context->counter);
782 __del_iptables_out(context->counter);
783 __add_iptables_in(context->counter);
784 __add_iptables_out(context->counter);
786 __del_ip6tables_in(context->counter);
787 __del_ip6tables_out(context->counter);
788 __add_ip6tables_in(context->counter);
789 __add_ip6tables_out(context->counter);
790 context->counter->intend = NFACCT_COUNTER;
792 rstn_value->data_limit_reached = TRUE;
795 rv = stc_manager_dbus_emit_signal(stc->connection,
796 STC_DBUS_SERVICE_RESTRICTION_PATH,
797 STC_DBUS_INTERFACE_RESTRICTION,
798 "RestrictionThresholdCrossed",
799 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
801 rstn_value->rstn_limit_crossed_notified = TRUE;
803 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
804 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
805 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
806 "restriction_noti", rstn_key->app_id,
810 g_system->rstns_tree_updated = TRUE;
811 __print_rstn(rstn_key, rstn_value);
814 STC_LOGE("unknown iotype");
820 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
821 stc_rstn_value_s *rstn_value,
822 classid_bytes_context_s *context)
824 if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
825 context->counter->iftype == STC_IFACE_DATACALL) ||
826 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
827 context->counter->iftype == STC_IFACE_WIFI) ||
828 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
829 context->counter->iftype == STC_IFACE_BLUETOOTH)) {
830 context->counter->classid = rstn_value->classid;
831 return __rstn_counter_update(rstn_key, rstn_value, context);
837 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
842 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
843 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
844 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
847 if (context->counter->intend != NFACCT_COUNTER)
848 goto try_next_callback;
850 if (rstn_value->data_limit_reached == TRUE) {
851 context->data_limit_reached = TRUE;
852 goto try_next_callback;
855 classid = context->counter->classid;
856 rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
858 context->counter->classid = classid;
859 if (rstn_value->classid != context->counter->classid)
860 goto try_next_callback;
862 rv = __rstn_counter_update(rstn_key, rstn_value, context);
868 static gboolean __update_app_statistics(gpointer key, gpointer value,
871 stc_app_key_s *app_key = (stc_app_key_s *)key;
872 stc_app_value_s *app_value = (stc_app_value_s *)value;
873 time_t *touch_time = (time_t *)data;
874 stc_db_classid_iftype_key stat_key;
875 stc_db_app_stats stat;
876 default_connection_s *default_connection = stc_get_default_connection();
878 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
879 memset(&stat, 0 , sizeof(stc_db_app_stats));
881 stat_key.classid = app_value->classid;
882 stat_key.iftype = default_connection->type;
884 if (STC_IFACE_DATACALL == stat_key.iftype)
885 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
887 stat_key.subscriber_id = g_strdup("none_subid");
889 g_strlcpy(stat_key.ifname, default_connection->ifname,
892 stat.app_id = g_strdup(app_key->app_id);
893 stat.snd_count = app_value->counter.out_bytes;
894 stat.rcv_count = app_value->counter.in_bytes;
895 stat.is_roaming = default_connection->roaming;
896 stat.ground = STC_APP_STATE_UNKNOWN;
898 table_statistics_insert(&stat_key, &stat, *touch_time);
900 app_value->counter.out_bytes = 0;
901 app_value->counter.in_bytes = 0;
904 FREE(stat_key.subscriber_id);
909 static gboolean __flush_apps_stats_to_database(gpointer user_data)
911 time_t current_time = time(0);
913 if (g_system->apps_tree_updated == FALSE)
914 return G_SOURCE_REMOVE;
916 g_system->apps_tree_updated = FALSE;
919 g_tree_foreach(g_system->apps,
920 __update_app_statistics,
923 STC_LOGI("Flushed app stats to database");
924 return G_SOURCE_REMOVE;
927 static gboolean __update_counter_statistics(gpointer key, gpointer value,
930 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
931 table_counters_info info = {
932 .restriction_id = rstn_value->restriction_id,
933 .data_counter = rstn_value->data_counter
936 table_counters_update_counters(&info);
941 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
943 time_t current_time = time(0);
945 if (g_system->rstns_tree_updated == FALSE)
946 return G_SOURCE_REMOVE;
948 g_system->rstns_tree_updated = FALSE;
951 g_tree_foreach(g_system->rstns,
952 __update_counter_statistics,
955 STC_LOGI("Flushed rstns counters to database");
956 return G_SOURCE_REMOVE;
959 static void __app_counter_update(stc_app_key_s *app_key,
960 stc_app_value_s *app_value,
961 classid_bytes_context_s *context)
963 switch (context->counter->iotype) {
964 case NFACCT_COUNTER_IN:
965 app_value->data_usage.in_bytes += context->bytes;
966 app_value->counter.in_bytes = context->bytes;
967 g_system->apps_tree_updated = TRUE;
970 __apps_tree_foreach_print(app_key, app_value, NULL);
973 case NFACCT_COUNTER_OUT:
974 app_value->data_usage.out_bytes += context->bytes;
975 app_value->counter.out_bytes = context->bytes;
976 g_system->apps_tree_updated = TRUE;
979 __apps_tree_foreach_print(app_key, app_value, NULL);
983 STC_LOGE("unknown iotype");
987 static void __interface_counter_update(stc_app_key_s *app_key,
988 stc_app_value_s *app_value,
989 classid_bytes_context_s *context)
991 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
992 context->counter->iftype == STC_IFACE_DATACALL) ||
993 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
994 context->counter->iftype == STC_IFACE_WIFI) ||
995 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
996 context->counter->iftype == STC_IFACE_BLUETOOTH))
997 __app_counter_update(app_key, app_value, context);
1001 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1005 stc_app_key_s *app_key = (stc_app_key_s *)key;
1006 stc_app_value_s *app_value = (stc_app_value_s *)value;
1007 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1009 if (context->counter->intend != NFACCT_COUNTER)
1010 goto try_next_callback;
1012 __interface_counter_update(app_key, app_value, context);
1014 if (app_value->classid != context->counter->classid)
1015 goto try_next_callback;
1017 __app_counter_update(app_key, app_value, context);
1023 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1024 struct counter_arg *carg)
1026 struct nfacct_rule counter = {
1033 classid_bytes_context_s context = {
1034 .counter = &counter,
1036 .data_limit_reached = FALSE,
1039 STC_LOGD("cnt_name %s", cnt_name);
1041 if (!recreate_counter_by_name(cnt_name, &counter)) {
1042 STC_LOGE("Can't parse counter name %s", cnt_name);
1046 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1047 context.counter->classid, context.counter->iftype,
1048 context.counter->iotype, context.counter->intend,
1049 context.counter->ifname, context.bytes);
1051 if (g_system->rstns)
1052 g_tree_foreach(g_system->rstns,
1053 __rstn_counter_update_foreach_classid,
1057 g_tree_foreach(g_system->apps,
1058 __apps_counter_update_foreach_classid,
1062 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1065 struct counter_arg *carg = user_data;
1066 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1067 if (carg->initiate) {
1069 * TODO: this will be used when daemon starts to update existing
1070 * counter data if present.
1072 populate_counters(cnt_name, carg);
1076 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1077 int bytes = be64toh(*bytes_p);
1079 ++carg->serialized_counters;
1080 __fill_nfacct_result(cnt_name, bytes, carg);
1087 static int __post_fill_counters(void *user_data)
1089 struct counter_arg *carg = user_data;
1097 static void __process_network_counter(struct genl *ans,
1098 struct counter_arg *carg)
1100 struct netlink_serialization_params ser_params = {
1103 .eval_attr = __fill_counters,
1104 .post_eval_attr = __post_fill_counters,
1107 netlink_serialization_command *netlink =
1108 netlink_create_command(&ser_params);
1110 STC_LOGE("Can not create command");
1114 netlink->deserialize_answer(&(netlink->params));
1117 static gboolean __process_contr_reply(GIOChannel *source,
1118 GIOCondition condition,
1121 int sock = g_io_channel_unix_get_fd(source);
1124 stc_s *stc = stc_get_manager();
1126 if ((condition & G_IO_ERR) ||
1127 (condition & G_IO_HUP) ||
1128 (condition & G_IO_NVAL)) {
1129 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1131 STC_LOGE("Counter socket received G_IO event, closing socket."
1132 "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
1133 (condition & G_IO_ERR), (condition & G_IO_HUP),
1134 (condition & G_IO_NVAL));
1135 __close_and_reopen_contr_sock(g_system);
1140 STC_LOGE("Can't get stc data");
1144 ret = read_netlink(sock,
1145 &ans, sizeof(struct genl));
1146 /* STC_LOGD("Counter data received ret [%d]", ret); */
1150 stc->carg->ans_len = ret;
1151 __process_network_counter(&ans, stc->carg);
1153 g_idle_add(__flush_apps_stats_to_database, NULL);
1154 g_idle_add(__flush_rstns_counter_to_database, NULL);
1159 static gboolean __update_contr_cb(void *user_data)
1161 /* Here we just sent command, answer we receive in another callback */
1162 stc_s *stc = stc_get_manager();
1163 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1165 stc->carg = MALLOC0(counter_arg_s, 1);
1166 if (stc->carg == NULL)
1167 return TRUE; /* we need to continue the timer */
1169 stc->carg->sock = stc_monitor_get_counter_socket();
1172 /* STC_LOGD("Get all counters"); */
1173 nfacct_send_get_all(stc->carg);
1175 /* we need to continue the timer */
1179 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1182 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1183 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1185 __print_rstn(rstn_key, rstn_value);
1189 static void __rstn_tree_printall(void)
1191 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1194 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1195 const stc_rstn_key_s *key)
1197 stc_rstn_value_s *lookup;
1199 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1201 lookup = g_tree_lookup(rstns_tree, key);
1206 static gboolean __remove_restriction(gpointer key, gpointer value,
1209 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1210 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1212 /* rstn rule is already removed */
1213 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
1216 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1217 __print_rstn(rstn_key, rstn_value);
1221 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1224 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1225 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1227 /* rstn rule is activated */
1228 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1231 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1232 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1234 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1236 __print_rstn(rstn_key, rstn_value);
1241 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1243 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1244 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1246 /* rstn rule is activated */
1247 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1250 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1251 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1253 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1258 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1260 stc_rstn_value_s *lookup_value;
1262 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1264 lookup_value = __rstn_lookup(g_system->rstns, key);
1265 if (!lookup_value) {
1266 STC_LOGE("key not found");
1267 return STC_ERROR_NO_DATA;
1270 __remove_restriction(key, lookup_value, NULL);
1272 /* remove counter also */
1273 table_counters_delete(lookup_value->restriction_id);
1275 if (!g_tree_remove(g_system->rstns, key)) {
1276 STC_LOGD("key not found");
1277 return STC_ERROR_NO_DATA;
1280 return STC_ERROR_NONE;
1283 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1284 stc_rstn_value_s *value, gboolean debug)
1286 stc_rstn_value_s *rstn_value;
1288 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1290 rstn_value = __rstn_lookup(g_system->rstns, key);
1292 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1294 STC_LOGE("rstn_key allocation failed");
1295 return STC_ERROR_OUT_OF_MEMORY;
1298 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1300 STC_LOGE("rstn_value allocation failed");
1302 return STC_ERROR_OUT_OF_MEMORY;
1305 rstn_key->app_id = g_strdup(key->app_id);
1306 rstn_key->ifname = g_strdup(key->ifname);
1307 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1308 rstn_key->iftype = key->iftype;
1309 rstn_key->roaming = key->roaming;
1311 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1314 rstn_value->restriction_id = value->restriction_id;
1315 rstn_value->rst_state = value->rst_state;
1316 rstn_value->classid = value->classid;
1317 rstn_value->data_limit = value->data_limit;
1318 rstn_value->data_warn_limit = value->data_warn_limit;
1319 rstn_value->data_counter = 0;
1320 rstn_value->warn_limit_crossed_notified = FALSE;
1321 rstn_value->rstn_limit_crossed_notified = FALSE;
1324 __add_restriction_debug(key, rstn_value, NULL);
1326 __add_restriction(key, rstn_value, NULL);
1328 return STC_ERROR_NONE;
1331 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1334 stc_cb_ret_e ret = STC_CONTINUE;
1337 stc_rstn_value_s value;
1339 memset(&key, 0, sizeof(stc_rstn_key_s));
1340 memset(&value, 0, sizeof(stc_rstn_value_s));
1342 key.app_id = g_strdup(info->app_id);
1343 key.ifname = g_strdup(info->ifname);
1344 key.subscriber_id = g_strdup(info->subscriber_id);
1345 key.iftype = info->iftype;
1346 key.roaming = info->roaming;
1348 value.rst_state = info->rst_state;
1349 value.restriction_id = info->restriction_id;
1351 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1352 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1354 value.classid = STC_UNKNOWN_CLASSID;
1356 value.data_limit = info->data_limit;
1357 value.data_warn_limit = info->data_warn_limit;
1359 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1364 FREE(key.subscriber_id);
1368 static void __fill_restritions_list(void)
1370 table_restrictions_foreach(__insert_restriction_cb, NULL);
1371 //__rstn_tree_printall();
1374 static gboolean __add_rstn_foreach_application(gpointer key,
1378 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1379 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1380 gchar *app_id = (gchar *)data;
1382 /* rstn rule is not for applications */
1383 if (rstn_key->app_id == NULL)
1386 /* rstn rule is not for this application */
1387 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1390 /* rstn rule is already applied */
1391 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1394 /* add restriction to system */
1395 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1396 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1398 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1400 __print_rstn(rstn_key, rstn_value);
1405 static void __add_rstns_for_application(gchar *app_id)
1407 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1411 static void __add_application_by_interface(const char *app_id)
1413 stc_app_key_s app_key;
1414 stc_app_value_s app_value;
1419 memset(&app_key, 0, sizeof(stc_app_key_s));
1420 memset(&app_value, 0, sizeof(stc_app_value_s));
1422 app_key.pkg_id = g_strdup(app_id);
1423 app_key.app_id = g_strdup(app_id);
1425 app_value.type = STC_APP_TYPE_NONE;
1426 app_value.processes = NULL;
1427 app_value.counter.in_bytes = 0;
1428 app_value.counter.out_bytes = 0;
1430 stc_monitor_application_add(app_key, app_value);
1432 FREE(app_key.pkg_id);
1433 FREE(app_key.app_id);
1436 static int __vconf_get_int(const char *key, int *value)
1440 ret = vconf_get_int(key, value);
1441 if (ret != VCONF_OK) {
1442 STC_LOGE("Failed to get vconfkey [%s] value", key);
1449 static int __vconf_set_int(const char *key, int value)
1453 ret = vconf_set_int(key, value);
1454 if (ret != VCONF_OK) {
1455 STC_LOGE("Failed to set vconfkey [%s] value", key);
1462 static guint __get_background_state(void)
1464 return g_system->background_state;;
1467 static void __set_background_state(guint state)
1469 g_system->background_state = state;
1472 static gboolean __processes_tree_foreach_background(gpointer key,
1476 stc_process_key_s *proc_key = (stc_process_key_s *)key;
1477 stc_app_key_s *app_key = (stc_app_key_s *)data;
1479 if (g_system->background_state)
1480 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1482 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1487 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1490 stc_app_key_s *app_key = (stc_app_key_s *)key;
1491 stc_app_value_s *app_value = (stc_app_value_s *)value;
1493 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1494 g_tree_foreach(app_value->processes,
1495 __processes_tree_foreach_background, app_key);
1500 static stc_error_e __process_update_background(void)
1502 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1504 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1506 return STC_ERROR_NONE;
1510 static void __excn_hash_foreach_print(gpointer key, gpointer value,
1513 const char *process_name = key;
1514 const char *exe_type = value;
1516 STC_LOGI("excn info => process_name [%s] exe_type [%s]",
1517 process_name, exe_type);
1520 static void __excn_hash_printall(void)
1522 g_hash_table_foreach(g_system->excns_hash,
1523 __excn_hash_foreach_print, NULL);
1527 static gboolean __remove_exception_app(gpointer key, gpointer value,
1530 const char *exe_type = value;
1532 if (g_strcmp0(exe_type, EXE_TYPE_APPLICATION) == 0)
1538 static void __remove_exception_appall(void)
1540 g_hash_table_foreach_remove(g_system->excns_hash,
1541 __remove_exception_app, NULL);
1544 static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info,
1547 stc_cb_ret_e ret = STC_CONTINUE;
1549 if (g_hash_table_insert(g_system->excns_hash,
1550 g_strdup(info->process_name),
1551 g_strdup(info->exe_type)) != TRUE)
1557 static void __fill_exceptions_list(void)
1559 table_exceptions_foreach(__insert_exception_cb, NULL);
1560 pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
1562 /* __excn_hash_printall(); */
1565 static gboolean __update_exceptions_app_list(void *user_data)
1567 __remove_exception_appall();
1568 pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
1570 /* __excn_hash_printall(); */
1575 stc_error_e stc_monitor_init(void)
1577 stc_system_s *system = MALLOC0(stc_system_s, 1);
1578 GIOChannel *gio = NULL;
1580 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1582 /* initializing current classid */
1583 init_current_classid();
1585 /* initializing cgroups */
1588 /* creating monitored application tree */
1589 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1590 __apps_tree_key_free,
1591 __apps_tree_value_free);
1593 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1594 __rstns_tree_key_free,
1595 __rstns_tree_value_free);
1597 system->excns_hash = g_hash_table_new_full(g_str_hash,
1598 g_str_equal, g_free, g_free);
1600 /* create netlink socket for updating kernel counters */
1601 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1602 if (!(system->contr_sock)) {
1603 STC_LOGE("failed to open socket");
1605 return STC_ERROR_FAIL;
1608 gio = g_io_channel_unix_new(system->contr_sock);
1609 system->contr_gsource_id =
1610 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1611 (GIOFunc) __process_contr_reply,
1613 g_io_channel_unref(gio);
1617 __add_application_by_interface(STC_TOTAL_DATACALL);
1618 __add_application_by_interface(STC_TOTAL_WIFI);
1619 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1620 __add_application_by_interface(STC_TOTAL_IPV4);
1621 __add_application_by_interface(STC_TOTAL_IPV6);
1623 /* creating restriction rules tree */
1624 __update_contr_cb(NULL);
1626 /* registering periodic kernel counters update callback */
1627 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1630 if (g_system->contr_timer_id == 0) {
1631 STC_LOGE("Failed to register kernel counters update timer");
1632 __close_contr_sock(g_system);
1633 return STC_ERROR_FAIL;
1636 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1637 (int *)&g_system->background_state);
1639 __fill_exceptions_list();
1640 __fill_restritions_list();
1642 g_system->excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL,
1643 __update_exceptions_app_list,
1646 return STC_ERROR_NONE;
1649 stc_error_e stc_monitor_deinit(void)
1651 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1653 /* close netlink socket for updating kernel counters */
1654 __close_contr_sock(g_system);
1656 /* remove kernel counters update timer */
1657 if (g_system->contr_timer_id > 0) {
1658 g_source_remove(g_system->contr_timer_id);
1659 g_system->contr_timer_id = 0;
1662 /* remove exceptions app list update timer */
1663 if (g_system->excns_timer_id > 0) {
1664 g_source_remove(g_system->excns_timer_id);
1665 g_system->excns_timer_id = 0;
1668 /* destroy monitored application tree */
1669 g_tree_destroy(g_system->apps);
1670 g_system->apps = NULL;
1672 /* destroy restriction rules tree */
1673 g_tree_destroy(g_system->rstns);
1674 g_system->rstns = NULL;
1676 /* destroy exception hash table */
1677 g_hash_table_destroy(g_system->excns_hash);
1678 g_system->excns_hash = NULL;
1682 return STC_ERROR_NONE;
1685 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1686 const stc_app_value_s app_value)
1688 stc_error_e ret = STC_ERROR_NONE;
1690 stc_app_value_s *value;
1691 stc_app_value_s *lookup;
1693 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1695 lookup = __application_lookup(g_system->apps, &app_key);
1697 STC_LOGD("app_key already present");
1698 return STC_ERROR_NONE;
1701 key = MALLOC0(stc_app_key_s, 1);
1703 STC_LOGE("key allocation failed");
1704 return STC_ERROR_OUT_OF_MEMORY;
1707 value = MALLOC0(stc_app_value_s, 1);
1709 STC_LOGE("value allocation failed");
1711 return STC_ERROR_OUT_OF_MEMORY;
1714 key->app_id = g_strdup(app_key.app_id);
1715 key->pkg_id = g_strdup(app_key.pkg_id);
1717 value->type = app_value.type;
1718 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1719 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1721 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1722 __processes_tree_key_free,
1723 __processes_tree_value_free);
1725 /* create cgroup and update classid */
1726 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1728 g_tree_insert(g_system->apps, key, value);
1730 /* add nfacct rule for this classid */
1731 __add_application_monitor(key, value, stc_get_default_connection());
1732 __add_rstns_for_application(app_key.app_id);
1737 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1738 const stc_process_key_s proc_key,
1739 const stc_process_value_s proc_value)
1741 stc_error_e ret = STC_ERROR_NONE;
1742 stc_app_value_s *app_lookup;
1743 stc_process_key_s *key;
1744 stc_process_value_s *value;
1745 stc_process_value_s *proc_lookup;
1747 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1749 app_lookup = __application_lookup(g_system->apps, &app_key);
1751 STC_LOGD("app_key not found");
1752 return STC_ERROR_FAIL;
1755 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1757 STC_LOGD("proc_key already present");
1758 return STC_ERROR_NONE;
1761 key = MALLOC0(stc_process_key_s, 1);
1763 STC_LOGE("key allocation failed");
1764 return STC_ERROR_OUT_OF_MEMORY;
1767 value = MALLOC0(stc_process_value_s, 1);
1769 STC_LOGE("value allocation failed");
1771 return STC_ERROR_OUT_OF_MEMORY;
1774 key->pid = proc_key.pid;
1776 value->ground = proc_value.ground;
1778 g_tree_insert(app_lookup->processes, key, value);
1780 /* add pid to application cgroup */
1781 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1784 __apps_tree_printall();
1790 stc_error_e stc_monitor_process_remove(pid_t pid)
1792 stc_error_e ret = STC_ERROR_NONE;
1793 stc_process_key_s proc_key = {
1797 remove_pid_context_s context = {
1799 .proc_key = &proc_key,
1800 .entry_removed = FALSE,
1803 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1805 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1808 if (context.entry_removed)
1809 __application_remove_if_empty(context.app_key);
1812 __apps_tree_printall();
1818 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1819 const stc_process_key_s proc_key,
1820 stc_app_state_e ground)
1822 stc_error_e ret = STC_ERROR_NONE;
1823 stc_app_value_s *app_lookup;
1824 stc_process_value_s *proc_lookup;
1826 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1828 app_lookup = __application_lookup(g_system->apps, &app_key);
1830 STC_LOGD("app_key not found");
1831 return STC_ERROR_FAIL;
1834 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1836 STC_LOGD("proc_key not found");
1837 return STC_ERROR_FAIL;
1840 if (proc_lookup->ground != ground)
1841 proc_lookup->ground = ground;
1843 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1844 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1846 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1851 void stc_monitor_update_rstn_by_default_connection(void *data)
1853 static default_connection_s old_connection;
1854 default_connection_s *new_connection = (default_connection_s *)data;
1856 if (old_connection.path != NULL) {
1858 g_tree_foreach(g_system->apps,
1859 __remove_application_monitor,
1860 (gpointer)&old_connection);
1862 if (g_system->rstns)
1863 g_tree_foreach(g_system->rstns,
1864 __remove_restriction,
1865 (gpointer)&old_connection);
1868 FREE(old_connection.path);
1869 FREE(old_connection.ifname);
1870 old_connection.type = 0;
1871 old_connection.roaming = 0;
1873 if (new_connection != NULL && new_connection->path != NULL) {
1875 g_tree_foreach(g_system->apps,
1876 __add_application_monitor,
1877 (gpointer)new_connection);
1879 if (g_system->rstns)
1880 g_tree_foreach(g_system->rstns, __add_restriction,
1883 old_connection.path = g_strdup(new_connection->path);
1884 old_connection.ifname = g_strdup(new_connection->ifname);
1885 old_connection.type = new_connection->type;
1886 old_connection.roaming = new_connection->roaming;
1890 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1895 stc_rstn_value_s value;
1897 memset(&key, 0, sizeof(stc_rstn_key_s));
1898 memset(&value, 0, sizeof(stc_rstn_value_s));
1900 key.app_id = g_strdup(info->app_id);
1901 key.ifname = g_strdup(info->ifname);
1902 key.subscriber_id = g_strdup(info->subscriber_id);
1903 key.iftype = info->iftype;
1904 key.roaming = info->roaming;
1906 value.rst_state = info->rst_state;
1907 value.restriction_id = info->restriction_id;
1909 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1910 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1912 value.classid = STC_UNKNOWN_CLASSID;
1914 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1915 __set_background_state(TRUE);
1916 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
1917 __process_update_background();
1920 value.data_limit = info->data_limit;
1921 value.data_warn_limit = info->data_warn_limit;
1923 ret = __rstn_tree_add(&key, &value, TRUE);
1927 FREE(key.subscriber_id);
1931 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1935 stc_rstn_key_s key = {
1936 .app_id = g_strdup(info->app_id),
1937 .ifname = g_strdup(info->ifname),
1938 .subscriber_id = g_strdup(info->subscriber_id),
1939 .iftype = info->iftype,
1940 .roaming = info->roaming,
1943 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
1944 __set_background_state(FALSE);
1945 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
1946 __process_update_background();
1949 ret = __rstn_tree_remove(&key);
1953 FREE(key.subscriber_id);
1957 stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
1959 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1961 char *exe_type = NULL;
1963 exe_type = g_hash_table_lookup(g_system->excns_hash, cmdline);
1965 return STC_ERROR_NO_DATA;
1967 return STC_ERROR_NONE;
1970 int stc_monitor_get_counter_socket(void)
1972 return g_system->contr_sock;