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);
279 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
282 stc_process_key_s *proc_key = (stc_process_key_s *)key;
283 stc_process_value_s *proc_value = (stc_process_value_s *)value;
285 STC_LOGD("Process entry => PID [%d], Ground state [%d]",
286 proc_key->pid, proc_value->ground);
290 static void __processes_tree_printall(GTree *processes)
292 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 [%s], App ID [%s],"
303 " 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);
315 static void __apps_tree_printall(void)
317 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
321 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
324 remove_pid_context_s *context = (remove_pid_context_s *)data;
325 stc_app_value_s *app_value = (stc_app_value_s *)value;
327 if (!g_tree_remove(app_value->processes, context->proc_key)) {
329 STC_LOGD("key not found");
334 context->entry_removed = TRUE;
335 context->app_key = (stc_app_key_s *)key;
340 static stc_app_value_s * __application_lookup(GTree *apps,
341 const stc_app_key_s *key)
343 stc_app_value_s *lookup;
345 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
347 lookup = g_tree_lookup(apps, key);
352 static stc_process_value_s * __process_lookup(GTree *processes,
353 const stc_process_key_s *key)
355 stc_process_value_s *lookup;
357 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
359 lookup = g_tree_lookup(processes, key);
364 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
367 guint *pid_count = (guint *)data;
372 static gboolean __add_application_monitor(gpointer key, gpointer value,
375 stc_app_value_s *app_value = (stc_app_value_s *)value;
376 default_connection_s *connection = (default_connection_s *)data;
377 stc_s *stc = stc_get_manager();
379 if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
380 app_value->classid == STC_TOTAL_WIFI_CLASSID ||
381 app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
384 if (stc && connection && connection->ifname) {
385 struct nfacct_rule counter;
388 stc->carg = MALLOC0(counter_arg_s, 1);
389 if (stc->carg == NULL)
392 stc->carg->sock = stc_monitor_get_counter_socket();
395 memset(&counter, 0, sizeof(struct nfacct_rule));
397 counter.carg = stc->carg;
398 counter.classid = app_value->classid;
399 counter.intend = NFACCT_COUNTER;
400 counter.iftype = connection->type;
401 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
403 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
404 __add_iptables_in(&counter);
405 __add_iptables_out(&counter);
406 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
407 __add_ip6tables_in(&counter);
408 __add_ip6tables_out(&counter);
410 __add_iptables_in(&counter);
411 __add_iptables_out(&counter);
412 __add_ip6tables_in(&counter);
413 __add_ip6tables_out(&counter);
420 static gboolean __remove_application_monitor(gpointer key, gpointer value,
423 stc_app_value_s *app_value = (stc_app_value_s *)value;
424 default_connection_s *connection = (default_connection_s *)data;
425 stc_s *stc = stc_get_manager();
427 if (stc && connection && connection->ifname) {
428 struct nfacct_rule counter;
431 stc->carg = MALLOC0(counter_arg_s, 1);
432 if (stc->carg == NULL)
435 stc->carg->sock = stc_monitor_get_counter_socket();
438 memset(&counter, 0, sizeof(struct nfacct_rule));
440 counter.carg = stc->carg;
441 counter.classid = app_value->classid;
442 counter.intend = NFACCT_COUNTER;
443 counter.iftype = connection->type;
444 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
446 __del_iptables_in(&counter);
447 __del_iptables_out(&counter);
448 __del_ip6tables_in(&counter);
449 __del_ip6tables_out(&counter);
455 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
457 STC_LOGI("rstn info => rstn_id [%llu], "
458 "app_id [%s], classid [%lu], ifname [%s], "
459 "iftype [%d], rst_state [%d], "
460 "limit [ (%lld) bytes], "
461 "warn_limit [ (%lld) bytes], "
462 "counter [ (%lld) bytes], "
463 "roaming [%d], subscriber_id [%s]",
464 rstn_value->restriction_id,
465 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
466 rstn_key->iftype, rstn_value->rst_state,
467 rstn_value->data_limit,
468 rstn_value->data_warn_limit,
469 rstn_value->data_counter,
470 rstn_key->roaming, rstn_key->subscriber_id);
473 static void __process_restriction(enum traffic_restriction_type rst_type,
474 stc_rstn_key_s *rstn_key,
475 stc_rstn_value_s *rstn_value, void *data)
477 int64_t effective_data_limit, effective_data_warn_limit;
478 default_connection_s *old_connection = (default_connection_s *)data;
479 default_connection_s *connection = NULL;
481 if (old_connection != NULL)
482 connection = old_connection;
484 connection = stc_get_default_connection();
486 /* no default ifname */
487 if (connection->ifname == NULL)
490 /* rstn not applicable for this interface */
491 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
492 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
495 /* classid is invalid */
496 if (rstn_value->classid == STC_UNKNOWN_CLASSID)
499 effective_data_limit = rstn_value->data_limit;
500 effective_data_warn_limit = rstn_value->data_warn_limit;
502 if (rst_type == RST_SET) {
503 /* TODO: Change this to runtime memory */
504 table_counters_info info;
506 memset(&info, 0, sizeof(table_counters_info));
507 table_counters_get(rstn_value->restriction_id, &info);
509 effective_data_limit -= info.data_counter;
510 effective_data_warn_limit -= info.data_counter;
512 if (effective_data_limit < 0) {
513 effective_data_limit = 0;
514 rstn_value->data_limit_reached = TRUE;
517 if (effective_data_warn_limit < 0)
518 effective_data_warn_limit = 0;
520 STC_LOGD("datausage [%lld] bytes", info.data_counter);
523 STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
524 "effective_data_warn_limit [%lld] bytes",
525 rstn_value->restriction_id, effective_data_limit,
526 effective_data_warn_limit);
530 if (effective_data_limit <= 0) {
531 char *default_ifname = stc_default_connection_get_ifname();
532 struct nfacct_rule counter;
533 stc_s *stc = stc_get_manager();
536 stc->carg = MALLOC0(counter_arg_s, 1);
537 if (stc->carg == NULL) {
538 g_free(default_ifname);
543 stc_monitor_get_counter_socket();
546 counter.carg = stc->carg;
547 counter.classid = rstn_value->classid;
548 counter.intend = NFACCT_BLOCK;
549 counter.iftype = rstn_key->iftype;
550 g_strlcpy(counter.ifname, default_ifname,
553 generate_counter_name(&counter);
554 g_free(default_ifname);
557 __add_iptables_in(&counter);
558 __add_iptables_out(&counter);
561 __add_ip6tables_in(&counter);
562 __add_ip6tables_out(&counter);
565 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
566 rstn_value->data_limit_reached = FALSE;
573 char *default_ifname = stc_default_connection_get_ifname();
574 struct nfacct_rule counter;
575 stc_s *stc = stc_get_manager();
578 stc->carg = MALLOC0(counter_arg_s, 1);
579 if (stc->carg == NULL) {
580 g_free(default_ifname);
585 stc_monitor_get_counter_socket();
588 counter.carg = stc->carg;
589 counter.classid = rstn_value->classid;
590 counter.intend = NFACCT_BLOCK;
591 counter.iftype = rstn_key->iftype;
592 g_strlcpy(counter.ifname, default_ifname,
595 generate_counter_name(&counter);
596 g_free(default_ifname);
599 __del_iptables_in(&counter);
600 __del_iptables_out(&counter);
603 __del_ip6tables_in(&counter);
604 __del_ip6tables_out(&counter);
606 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
607 rstn_value->data_limit_reached = FALSE;
615 static gboolean __remove_rstns_foreach_application(gpointer key,
619 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
620 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
621 gchar *app_id = (gchar *)data;
623 /* rstn rule is not for applications */
624 if (rstn_key->app_id == NULL)
627 /* rstn rule is not for this application */
628 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
631 /* rstn rule is already removed */
632 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
635 /* remove restriction from system */
636 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
638 __print_rstn(rstn_key, rstn_value);
643 static void __remove_rstns_for_application(gchar *app_id)
645 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
649 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
651 stc_error_e ret = STC_ERROR_NONE;
653 stc_app_value_s *lookup;
655 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
657 lookup = __application_lookup(g_system->apps, app_key);
659 STC_LOGE("app_key not found");
660 return STC_ERROR_NO_DATA;
663 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
667 /* remove nfacct rule for this classid */
668 __remove_application_monitor((gpointer) app_key, lookup,
669 stc_get_default_connection());
670 __remove_rstns_for_application(app_key->app_id);
673 if (!g_tree_remove(g_system->apps, app_key)) {
674 ret = STC_ERROR_NO_DATA;
675 STC_LOGE("key not found");
681 static stc_error_e __close_contr_sock(stc_system_s *system)
683 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
685 /* close netlink socket for updating kernel counters */
686 if (system->contr_sock != -1) {
687 close(system->contr_sock);
688 system->contr_sock = -1;
691 if (system->contr_gsource_id != 0) {
692 g_source_remove(system->contr_gsource_id);
693 system->contr_gsource_id = 0;
696 return STC_ERROR_NONE;
699 static gboolean __process_contr_reply(GIOChannel *source,
700 GIOCondition condition,
703 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
705 GIOChannel *gio = NULL;
706 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
708 /* close netlink socket for updating kernel counters */
709 if (system->contr_sock != -1) {
710 close(system->contr_sock);
711 system->contr_sock = -1;
714 if (system->contr_gsource_id != 0) {
715 g_source_remove(system->contr_gsource_id);
716 system->contr_gsource_id = 0;
719 /* create netlink socket for updating kernel counters */
720 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
721 if (!(system->contr_sock)) {
722 STC_LOGE("failed to open socket");
724 return STC_ERROR_FAIL;
727 gio = g_io_channel_unix_new(system->contr_sock);
728 system->contr_gsource_id =
729 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
730 (GIOFunc) __process_contr_reply,
732 g_io_channel_unref(gio);
734 return STC_ERROR_NONE;
737 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
738 stc_rstn_value_s *rstn_value,
739 classid_bytes_context_s *context)
741 switch (context->counter->iotype) {
742 case NFACCT_COUNTER_IN:
743 case NFACCT_COUNTER_OUT:
744 rstn_value->data_counter += context->bytes;
746 if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
747 rstn_value->warn_limit_crossed_notified == FALSE) {
750 char iftype[MAX_INT_LENGTH];
751 char byte[MAX_INT_LENGTH];
752 stc_s *stc = (stc_s *)stc_get_manager();
753 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
756 rv = stc_manager_dbus_emit_signal(stc->connection,
757 STC_DBUS_SERVICE_RESTRICTION_PATH,
758 STC_DBUS_INTERFACE_RESTRICTION,
759 "WarnThresholdCrossed",
760 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
762 rstn_value->warn_limit_crossed_notified = TRUE;
764 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
765 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
766 stc_send_warn_message_to_net_popup("warn threshold crossed",
772 if (rstn_value->data_counter >= rstn_value->data_limit &&
773 rstn_value->rstn_limit_crossed_notified == FALSE) {
776 char iftype[MAX_INT_LENGTH];
777 char byte[MAX_INT_LENGTH];
778 stc_s *stc = (stc_s *)stc_get_manager();
779 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
781 /* block immediately */
782 context->counter->intend = NFACCT_BLOCK;
783 __del_iptables_in(context->counter);
784 __del_iptables_out(context->counter);
785 __add_iptables_in(context->counter);
786 __add_iptables_out(context->counter);
788 __del_ip6tables_in(context->counter);
789 __del_ip6tables_out(context->counter);
790 __add_ip6tables_in(context->counter);
791 __add_ip6tables_out(context->counter);
792 context->counter->intend = NFACCT_COUNTER;
794 rstn_value->data_limit_reached = TRUE;
797 rv = stc_manager_dbus_emit_signal(stc->connection,
798 STC_DBUS_SERVICE_RESTRICTION_PATH,
799 STC_DBUS_INTERFACE_RESTRICTION,
800 "RestrictionThresholdCrossed",
801 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
803 rstn_value->rstn_limit_crossed_notified = TRUE;
805 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
806 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
807 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
808 "restriction_noti", rstn_key->app_id,
812 g_system->rstns_tree_updated = TRUE;
813 __print_rstn(rstn_key, rstn_value);
816 STC_LOGE("unknown iotype");
822 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
823 stc_rstn_value_s *rstn_value,
824 classid_bytes_context_s *context)
826 if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
827 context->counter->iftype == STC_IFACE_DATACALL) ||
828 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
829 context->counter->iftype == STC_IFACE_WIFI) ||
830 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
831 context->counter->iftype == STC_IFACE_BLUETOOTH)) {
832 context->counter->classid = rstn_value->classid;
833 return __rstn_counter_update(rstn_key, rstn_value, context);
839 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
844 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
845 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
846 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
849 if (context->counter->intend != NFACCT_COUNTER)
850 goto try_next_callback;
852 if (rstn_value->data_limit_reached == TRUE) {
853 context->data_limit_reached = TRUE;
854 goto try_next_callback;
857 classid = context->counter->classid;
858 rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
860 context->counter->classid = classid;
861 if (rstn_value->classid != context->counter->classid)
862 goto try_next_callback;
864 rv = __rstn_counter_update(rstn_key, rstn_value, context);
870 static gboolean __update_app_statistics(gpointer key, gpointer value,
873 stc_app_key_s *app_key = (stc_app_key_s *)key;
874 stc_app_value_s *app_value = (stc_app_value_s *)value;
875 time_t *touch_time = (time_t *)data;
876 stc_db_classid_iftype_key stat_key;
877 stc_db_app_stats stat;
878 default_connection_s *default_connection = stc_get_default_connection();
880 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
881 memset(&stat, 0 , sizeof(stc_db_app_stats));
883 stat_key.classid = app_value->classid;
884 stat_key.iftype = default_connection->type;
886 if (STC_IFACE_DATACALL == stat_key.iftype)
887 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
889 stat_key.subscriber_id = g_strdup("none_subid");
891 g_strlcpy(stat_key.ifname, default_connection->ifname,
894 stat.app_id = g_strdup(app_key->app_id);
895 stat.snd_count = app_value->counter.out_bytes;
896 stat.rcv_count = app_value->counter.in_bytes;
897 stat.is_roaming = default_connection->roaming;
898 stat.ground = STC_APP_STATE_UNKNOWN;
900 table_statistics_insert(&stat_key, &stat, *touch_time);
902 app_value->counter.out_bytes = 0;
903 app_value->counter.in_bytes = 0;
906 FREE(stat_key.subscriber_id);
911 static gboolean __flush_apps_stats_to_database(gpointer user_data)
913 time_t current_time = time(0);
915 if (g_system->apps_tree_updated == FALSE)
916 return G_SOURCE_REMOVE;
918 g_system->apps_tree_updated = FALSE;
921 g_tree_foreach(g_system->apps,
922 __update_app_statistics,
925 STC_LOGI("Flushed app stats to database");
926 return G_SOURCE_REMOVE;
929 static gboolean __update_counter_statistics(gpointer key, gpointer value,
932 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
933 table_counters_info info = {
934 .restriction_id = rstn_value->restriction_id,
935 .data_counter = rstn_value->data_counter
938 table_counters_update_counters(&info);
943 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
945 time_t current_time = time(0);
947 if (g_system->rstns_tree_updated == FALSE)
948 return G_SOURCE_REMOVE;
950 g_system->rstns_tree_updated = FALSE;
953 g_tree_foreach(g_system->rstns,
954 __update_counter_statistics,
957 STC_LOGI("Flushed rstns counters to database");
958 return G_SOURCE_REMOVE;
961 static void __app_counter_update(stc_app_key_s *app_key,
962 stc_app_value_s *app_value,
963 classid_bytes_context_s *context)
965 switch (context->counter->iotype) {
966 case NFACCT_COUNTER_IN:
967 app_value->data_usage.in_bytes += context->bytes;
968 app_value->counter.in_bytes = context->bytes;
969 g_system->apps_tree_updated = TRUE;
972 __apps_tree_foreach_print(app_key, app_value, NULL);
975 case NFACCT_COUNTER_OUT:
976 app_value->data_usage.out_bytes += context->bytes;
977 app_value->counter.out_bytes = context->bytes;
978 g_system->apps_tree_updated = TRUE;
981 __apps_tree_foreach_print(app_key, app_value, NULL);
985 STC_LOGE("unknown iotype");
989 static void __interface_counter_update(stc_app_key_s *app_key,
990 stc_app_value_s *app_value,
991 classid_bytes_context_s *context)
993 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
994 context->counter->iftype == STC_IFACE_DATACALL) ||
995 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
996 context->counter->iftype == STC_IFACE_WIFI) ||
997 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
998 context->counter->iftype == STC_IFACE_BLUETOOTH))
999 __app_counter_update(app_key, app_value, context);
1003 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1007 stc_app_key_s *app_key = (stc_app_key_s *)key;
1008 stc_app_value_s *app_value = (stc_app_value_s *)value;
1009 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1011 if (context->counter->intend != NFACCT_COUNTER)
1012 goto try_next_callback;
1014 __interface_counter_update(app_key, app_value, context);
1016 if (app_value->classid != context->counter->classid)
1017 goto try_next_callback;
1019 __app_counter_update(app_key, app_value, context);
1025 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1026 struct counter_arg *carg)
1028 struct nfacct_rule counter = {
1035 classid_bytes_context_s context = {
1036 .counter = &counter,
1038 .data_limit_reached = FALSE,
1041 STC_LOGD("cnt_name %s", cnt_name);
1043 if (!recreate_counter_by_name(cnt_name, &counter)) {
1044 STC_LOGE("Can't parse counter name %s", cnt_name);
1048 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1049 context.counter->classid, context.counter->iftype,
1050 context.counter->iotype, context.counter->intend,
1051 context.counter->ifname, context.bytes);
1053 if (g_system->rstns)
1054 g_tree_foreach(g_system->rstns,
1055 __rstn_counter_update_foreach_classid,
1059 g_tree_foreach(g_system->apps,
1060 __apps_counter_update_foreach_classid,
1064 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1067 struct counter_arg *carg = user_data;
1068 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1069 if (carg->initiate) {
1071 * TODO: this will be used when daemon starts to update existing
1072 * counter data if present.
1074 populate_counters(cnt_name, carg);
1078 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1079 int bytes = be64toh(*bytes_p);
1081 ++carg->serialized_counters;
1082 __fill_nfacct_result(cnt_name, bytes, carg);
1089 static int __post_fill_counters(void *user_data)
1091 struct counter_arg *carg = user_data;
1099 static void __process_network_counter(struct genl *ans,
1100 struct counter_arg *carg)
1102 struct netlink_serialization_params ser_params = {
1105 .eval_attr = __fill_counters,
1106 .post_eval_attr = __post_fill_counters,
1109 netlink_serialization_command *netlink =
1110 netlink_create_command(&ser_params);
1112 STC_LOGE("Can not create command");
1116 netlink->deserialize_answer(&(netlink->params));
1119 static gboolean __process_contr_reply(GIOChannel *source,
1120 GIOCondition condition,
1123 int sock = g_io_channel_unix_get_fd(source);
1126 stc_s *stc = stc_get_manager();
1128 if ((condition & G_IO_ERR) ||
1129 (condition & G_IO_HUP) ||
1130 (condition & G_IO_NVAL)) {
1131 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1133 STC_LOGE("Counter socket received G_IO event, closing socket."
1134 "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
1135 (condition & G_IO_ERR), (condition & G_IO_HUP),
1136 (condition & G_IO_NVAL));
1137 __close_and_reopen_contr_sock(g_system);
1142 STC_LOGE("Can't get stc data");
1146 ret = read_netlink(sock,
1147 &ans, sizeof(struct genl));
1148 /* STC_LOGD("Counter data received ret [%d]", ret); */
1152 stc->carg->ans_len = ret;
1153 __process_network_counter(&ans, stc->carg);
1155 g_idle_add(__flush_apps_stats_to_database, NULL);
1156 g_idle_add(__flush_rstns_counter_to_database, NULL);
1161 static gboolean __update_contr_cb(void *user_data)
1163 /* Here we just sent command, answer we receive in another callback */
1164 stc_s *stc = stc_get_manager();
1165 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1167 stc->carg = MALLOC0(counter_arg_s, 1);
1168 if (stc->carg == NULL)
1169 return TRUE; /* we need to continue the timer */
1171 stc->carg->sock = stc_monitor_get_counter_socket();
1174 /* STC_LOGD("Get all counters"); */
1175 nfacct_send_get_all(stc->carg);
1177 /* we need to continue the timer */
1181 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1184 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1185 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1187 __print_rstn(rstn_key, rstn_value);
1191 static void __rstn_tree_printall(void)
1193 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1196 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1197 const stc_rstn_key_s *key)
1199 stc_rstn_value_s *lookup;
1201 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1203 lookup = g_tree_lookup(rstns_tree, key);
1208 static gboolean __remove_restriction(gpointer key, gpointer value,
1211 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1212 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1214 /* rstn rule is already removed */
1215 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
1218 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1219 __print_rstn(rstn_key, rstn_value);
1223 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1226 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1227 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1229 /* rstn rule is activated */
1230 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1233 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1234 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1236 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1238 __print_rstn(rstn_key, rstn_value);
1243 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1245 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1246 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1248 /* rstn rule is activated */
1249 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1252 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1253 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1255 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1260 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1262 stc_rstn_value_s *lookup_value;
1264 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1266 lookup_value = __rstn_lookup(g_system->rstns, key);
1267 if (!lookup_value) {
1268 STC_LOGE("key not found");
1269 return STC_ERROR_NO_DATA;
1272 __remove_restriction(key, lookup_value, NULL);
1274 /* remove counter also */
1275 table_counters_delete(lookup_value->restriction_id);
1277 if (!g_tree_remove(g_system->rstns, key)) {
1278 STC_LOGD("key not found");
1279 return STC_ERROR_NO_DATA;
1282 return STC_ERROR_NONE;
1285 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1286 stc_rstn_value_s *value, gboolean debug)
1288 stc_rstn_value_s *rstn_value;
1290 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1292 rstn_value = __rstn_lookup(g_system->rstns, key);
1294 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1296 STC_LOGE("rstn_key allocation failed");
1297 return STC_ERROR_OUT_OF_MEMORY;
1300 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1302 STC_LOGE("rstn_value allocation failed");
1304 return STC_ERROR_OUT_OF_MEMORY;
1307 rstn_key->app_id = g_strdup(key->app_id);
1308 rstn_key->ifname = g_strdup(key->ifname);
1309 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1310 rstn_key->iftype = key->iftype;
1311 rstn_key->roaming = key->roaming;
1313 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1316 rstn_value->restriction_id = value->restriction_id;
1317 rstn_value->rst_state = value->rst_state;
1318 rstn_value->classid = value->classid;
1319 rstn_value->data_limit = value->data_limit;
1320 rstn_value->data_warn_limit = value->data_warn_limit;
1321 rstn_value->data_counter = 0;
1322 rstn_value->warn_limit_crossed_notified = FALSE;
1323 rstn_value->rstn_limit_crossed_notified = FALSE;
1326 __add_restriction_debug(key, rstn_value, NULL);
1328 __add_restriction(key, rstn_value, NULL);
1330 return STC_ERROR_NONE;
1333 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1336 stc_cb_ret_e ret = STC_CONTINUE;
1339 stc_rstn_value_s value;
1341 memset(&key, 0, sizeof(stc_rstn_key_s));
1342 memset(&value, 0, sizeof(stc_rstn_value_s));
1344 key.app_id = g_strdup(info->app_id);
1345 key.ifname = g_strdup(info->ifname);
1346 key.subscriber_id = g_strdup(info->subscriber_id);
1347 key.iftype = info->iftype;
1348 key.roaming = info->roaming;
1350 value.rst_state = info->rst_state;
1351 value.restriction_id = info->restriction_id;
1353 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1354 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1356 value.classid = STC_UNKNOWN_CLASSID;
1358 value.data_limit = info->data_limit;
1359 value.data_warn_limit = info->data_warn_limit;
1361 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1366 FREE(key.subscriber_id);
1370 static void __fill_restritions_list(void)
1372 table_restrictions_foreach(__insert_restriction_cb, NULL);
1373 //__rstn_tree_printall();
1376 static gboolean __add_rstn_foreach_application(gpointer key,
1380 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1381 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1382 gchar *app_id = (gchar *)data;
1384 /* rstn rule is not for applications */
1385 if (rstn_key->app_id == NULL)
1388 /* rstn rule is not for this application */
1389 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1392 /* rstn rule is already applied */
1393 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1396 /* add restriction to system */
1397 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1398 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1400 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1402 __print_rstn(rstn_key, rstn_value);
1407 static void __add_rstns_for_application(gchar *app_id)
1409 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1413 static void __add_application_by_interface(const char *app_id)
1415 stc_app_key_s app_key;
1416 stc_app_value_s app_value;
1421 memset(&app_key, 0, sizeof(stc_app_key_s));
1422 memset(&app_value, 0, sizeof(stc_app_value_s));
1424 app_key.pkg_id = g_strdup(app_id);
1425 app_key.app_id = g_strdup(app_id);
1427 app_value.type = STC_APP_TYPE_NONE;
1428 app_value.processes = NULL;
1429 app_value.counter.in_bytes = 0;
1430 app_value.counter.out_bytes = 0;
1432 stc_monitor_application_add(app_key, app_value);
1434 FREE(app_key.pkg_id);
1435 FREE(app_key.app_id);
1438 static int __vconf_get_int(const char *key, int *value)
1442 ret = vconf_get_int(key, value);
1443 if (ret != VCONF_OK) {
1444 STC_LOGE("Failed to get vconfkey [%s] value", key);
1451 static int __vconf_set_int(const char *key, int value)
1455 ret = vconf_set_int(key, value);
1456 if (ret != VCONF_OK) {
1457 STC_LOGE("Failed to set vconfkey [%s] value", key);
1464 static guint __get_background_state(void)
1466 return g_system->background_state;;
1469 static void __set_background_state(guint state)
1471 g_system->background_state = state;
1474 static gboolean __processes_tree_foreach_background(gpointer key,
1478 stc_process_key_s *proc_key = (stc_process_key_s *)key;
1479 stc_app_key_s *app_key = (stc_app_key_s *)data;
1481 if (g_system->background_state)
1482 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1484 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1489 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1492 stc_app_key_s *app_key = (stc_app_key_s *)key;
1493 stc_app_value_s *app_value = (stc_app_value_s *)value;
1495 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1496 g_tree_foreach(app_value->processes,
1497 __processes_tree_foreach_background, app_key);
1502 static stc_error_e __process_update_background(void)
1504 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1506 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1508 return STC_ERROR_NONE;
1512 static void __excn_hash_foreach_print(gpointer key, gpointer value,
1515 const char *process_name = key;
1516 const char *exe_type = value;
1518 STC_LOGI("excn info => process_name [%s] exe_type [%s]",
1519 process_name, exe_type);
1522 static void __excn_hash_printall(void)
1524 g_hash_table_foreach(g_system->excns_hash,
1525 __excn_hash_foreach_print, NULL);
1529 static gboolean __remove_exception_app(gpointer key, gpointer value,
1532 const char *exe_type = value;
1534 if (g_strcmp0(exe_type, EXE_TYPE_APPLICATION) == 0)
1540 static void __remove_exception_appall(void)
1542 g_hash_table_foreach_remove(g_system->excns_hash,
1543 __remove_exception_app, NULL);
1546 static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info,
1549 stc_cb_ret_e ret = STC_CONTINUE;
1551 if (g_hash_table_insert(g_system->excns_hash,
1552 g_strdup(info->process_name),
1553 g_strdup(info->exe_type)) != TRUE)
1559 static void __fill_exceptions_list(void)
1561 table_exceptions_foreach(__insert_exception_cb, NULL);
1562 pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
1564 /* __excn_hash_printall(); */
1567 static gboolean __update_exceptions_app_list(void *user_data)
1569 __remove_exception_appall();
1570 pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
1572 /* __excn_hash_printall(); */
1577 stc_error_e stc_monitor_init(void)
1579 stc_system_s *system = MALLOC0(stc_system_s, 1);
1580 GIOChannel *gio = NULL;
1582 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1584 /* initializing current classid */
1585 init_current_classid();
1587 /* initializing cgroups */
1590 /* creating monitored application tree */
1591 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1592 __apps_tree_key_free,
1593 __apps_tree_value_free);
1595 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1596 __rstns_tree_key_free,
1597 __rstns_tree_value_free);
1599 system->excns_hash = g_hash_table_new_full(g_str_hash,
1600 g_str_equal, g_free, g_free);
1602 /* create netlink socket for updating kernel counters */
1603 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1604 if (!(system->contr_sock)) {
1605 STC_LOGE("failed to open socket");
1607 return STC_ERROR_FAIL;
1610 gio = g_io_channel_unix_new(system->contr_sock);
1611 system->contr_gsource_id =
1612 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1613 (GIOFunc) __process_contr_reply,
1615 g_io_channel_unref(gio);
1619 __add_application_by_interface(STC_TOTAL_DATACALL);
1620 __add_application_by_interface(STC_TOTAL_WIFI);
1621 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1622 __add_application_by_interface(STC_TOTAL_IPV4);
1623 __add_application_by_interface(STC_TOTAL_IPV6);
1625 /* creating restriction rules tree */
1626 __update_contr_cb(NULL);
1628 /* registering periodic kernel counters update callback */
1629 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1632 if (g_system->contr_timer_id == 0) {
1633 STC_LOGE("Failed to register kernel counters update timer");
1634 __close_contr_sock(g_system);
1635 return STC_ERROR_FAIL;
1638 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1639 (int *)&g_system->background_state);
1641 __fill_exceptions_list();
1642 __fill_restritions_list();
1644 g_system->excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL,
1645 __update_exceptions_app_list,
1648 return STC_ERROR_NONE;
1651 stc_error_e stc_monitor_deinit(void)
1653 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1655 /* close netlink socket for updating kernel counters */
1656 __close_contr_sock(g_system);
1658 /* remove kernel counters update timer */
1659 if (g_system->contr_timer_id > 0) {
1660 g_source_remove(g_system->contr_timer_id);
1661 g_system->contr_timer_id = 0;
1664 /* remove exceptions app list update timer */
1665 if (g_system->excns_timer_id > 0) {
1666 g_source_remove(g_system->excns_timer_id);
1667 g_system->excns_timer_id = 0;
1670 /* destroy monitored application tree */
1671 g_tree_destroy(g_system->apps);
1672 g_system->apps = NULL;
1674 /* destroy restriction rules tree */
1675 g_tree_destroy(g_system->rstns);
1676 g_system->rstns = NULL;
1678 /* destroy exception hash table */
1679 g_hash_table_destroy(g_system->excns_hash);
1680 g_system->excns_hash = NULL;
1684 return STC_ERROR_NONE;
1687 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1688 const stc_app_value_s app_value)
1690 stc_error_e ret = STC_ERROR_NONE;
1692 stc_app_value_s *value;
1693 stc_app_value_s *lookup;
1695 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1697 lookup = __application_lookup(g_system->apps, &app_key);
1699 STC_LOGD("app_key already present");
1700 return STC_ERROR_NONE;
1703 key = MALLOC0(stc_app_key_s, 1);
1705 STC_LOGE("key allocation failed");
1706 return STC_ERROR_OUT_OF_MEMORY;
1709 value = MALLOC0(stc_app_value_s, 1);
1711 STC_LOGE("value allocation failed");
1713 return STC_ERROR_OUT_OF_MEMORY;
1716 key->app_id = g_strdup(app_key.app_id);
1717 key->pkg_id = g_strdup(app_key.pkg_id);
1719 value->type = app_value.type;
1720 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1721 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1723 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1724 __processes_tree_key_free,
1725 __processes_tree_value_free);
1727 /* create cgroup and update classid */
1728 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1730 g_tree_insert(g_system->apps, key, value);
1732 /* add nfacct rule for this classid */
1733 __add_application_monitor(key, value, stc_get_default_connection());
1734 __add_rstns_for_application(app_key.app_id);
1739 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1740 const stc_process_key_s proc_key,
1741 const stc_process_value_s proc_value)
1743 stc_error_e ret = STC_ERROR_NONE;
1744 stc_app_value_s *app_lookup;
1745 stc_process_key_s *key;
1746 stc_process_value_s *value;
1747 stc_process_value_s *proc_lookup;
1749 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1751 app_lookup = __application_lookup(g_system->apps, &app_key);
1753 STC_LOGD("app_key not found");
1754 return STC_ERROR_FAIL;
1757 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1759 STC_LOGD("proc_key already present");
1760 return STC_ERROR_NONE;
1763 key = MALLOC0(stc_process_key_s, 1);
1765 STC_LOGE("key allocation failed");
1766 return STC_ERROR_OUT_OF_MEMORY;
1769 value = MALLOC0(stc_process_value_s, 1);
1771 STC_LOGE("value allocation failed");
1773 return STC_ERROR_OUT_OF_MEMORY;
1776 key->pid = proc_key.pid;
1778 value->ground = proc_value.ground;
1780 g_tree_insert(app_lookup->processes, key, value);
1782 /* add pid to application cgroup */
1783 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1788 stc_error_e stc_monitor_process_remove(pid_t pid)
1790 stc_error_e ret = STC_ERROR_NONE;
1791 stc_process_key_s proc_key = {
1795 remove_pid_context_s context = {
1797 .proc_key = &proc_key,
1798 .entry_removed = FALSE,
1801 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1803 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1806 if (context.entry_removed)
1807 __application_remove_if_empty(context.app_key);
1812 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1813 const stc_process_key_s proc_key,
1814 stc_app_state_e ground)
1816 stc_error_e ret = STC_ERROR_NONE;
1817 stc_app_value_s *app_lookup;
1818 stc_process_value_s *proc_lookup;
1820 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1822 app_lookup = __application_lookup(g_system->apps, &app_key);
1824 STC_LOGD("app_key not found");
1825 return STC_ERROR_FAIL;
1828 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1830 STC_LOGD("proc_key not found");
1831 return STC_ERROR_FAIL;
1834 if (proc_lookup->ground != ground)
1835 proc_lookup->ground = ground;
1837 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1838 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1840 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1845 void stc_monitor_update_rstn_by_default_connection(void *data)
1847 static default_connection_s old_connection;
1848 default_connection_s *new_connection = (default_connection_s *)data;
1850 if (old_connection.path != NULL) {
1852 g_tree_foreach(g_system->apps,
1853 __remove_application_monitor,
1854 (gpointer)&old_connection);
1856 if (g_system->rstns)
1857 g_tree_foreach(g_system->rstns,
1858 __remove_restriction,
1859 (gpointer)&old_connection);
1862 FREE(old_connection.path);
1863 FREE(old_connection.ifname);
1864 old_connection.type = 0;
1865 old_connection.roaming = 0;
1867 if (new_connection != NULL && new_connection->path != NULL) {
1869 g_tree_foreach(g_system->apps,
1870 __add_application_monitor,
1871 (gpointer)new_connection);
1873 if (g_system->rstns)
1874 g_tree_foreach(g_system->rstns, __add_restriction,
1877 old_connection.path = g_strdup(new_connection->path);
1878 old_connection.ifname = g_strdup(new_connection->ifname);
1879 old_connection.type = new_connection->type;
1880 old_connection.roaming = new_connection->roaming;
1884 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1889 stc_rstn_value_s value;
1891 memset(&key, 0, sizeof(stc_rstn_key_s));
1892 memset(&value, 0, sizeof(stc_rstn_value_s));
1894 key.app_id = g_strdup(info->app_id);
1895 key.ifname = g_strdup(info->ifname);
1896 key.subscriber_id = g_strdup(info->subscriber_id);
1897 key.iftype = info->iftype;
1898 key.roaming = info->roaming;
1900 value.rst_state = info->rst_state;
1901 value.restriction_id = info->restriction_id;
1903 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1904 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1906 value.classid = STC_UNKNOWN_CLASSID;
1908 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1909 __set_background_state(TRUE);
1910 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
1911 __process_update_background();
1914 value.data_limit = info->data_limit;
1915 value.data_warn_limit = info->data_warn_limit;
1917 ret = __rstn_tree_add(&key, &value, TRUE);
1921 FREE(key.subscriber_id);
1925 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1929 stc_rstn_key_s key = {
1930 .app_id = g_strdup(info->app_id),
1931 .ifname = g_strdup(info->ifname),
1932 .subscriber_id = g_strdup(info->subscriber_id),
1933 .iftype = info->iftype,
1934 .roaming = info->roaming,
1937 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
1938 __set_background_state(FALSE);
1939 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
1940 __process_update_background();
1943 ret = __rstn_tree_remove(&key);
1947 FREE(key.subscriber_id);
1951 stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
1953 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1955 char *exe_type = NULL;
1957 exe_type = g_hash_table_lookup(g_system->excns_hash, cmdline);
1959 return STC_ERROR_NO_DATA;
1961 return STC_ERROR_NONE;
1964 int stc_monitor_get_counter_socket(void)
1966 return g_system->contr_sock;