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"
26 #include "helper-iptables.h"
28 #include "table-statistics.h"
29 #include "table-counters.h"
30 #include "stc-exception.h"
31 #include "stc-monitor.h"
32 #include "stc-manager-plugin.h"
34 #define MAX_INT_LENGTH 128
35 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
38 stc_app_key_s *app_key;
39 stc_process_key_s *proc_key;
40 gboolean entry_removed;
41 } remove_pid_context_s;
44 struct nfacct_rule *counter;
46 gboolean data_limit_reached;
47 } classid_bytes_context_s;
49 static stc_system_s *g_system = NULL;
51 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
53 if (counter->intend == NFACCT_WARN)
54 return NFACCT_JUMP_ACCEPT;
55 else if (counter->intend == NFACCT_BLOCK)
56 return NFACCT_JUMP_REJECT;
58 return NFACCT_JUMP_UNKNOWN;
61 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
64 return STC_ERROR_INVALID_PARAMETER;
66 counter->action = NFACCT_ACTION_INSERT;
67 counter->iotype = NFACCT_COUNTER_IN;
68 counter->jump = __get_jump_by_intend(counter);
69 counter->iptype = NFACCT_TYPE_IPV4;
70 counter->send_limit = 0;
71 counter->rcv_limit = 0;
73 return produce_net_rule(counter);
76 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
79 return STC_ERROR_INVALID_PARAMETER;
81 counter->action = NFACCT_ACTION_INSERT;
82 counter->iotype = NFACCT_COUNTER_OUT;
83 counter->jump = __get_jump_by_intend(counter);
84 counter->iptype = NFACCT_TYPE_IPV4;
85 counter->send_limit = 0;
86 counter->rcv_limit = 0;
88 return produce_net_rule(counter);
91 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
94 return STC_ERROR_INVALID_PARAMETER;
96 counter->action = NFACCT_ACTION_DELETE;
97 counter->iotype = NFACCT_COUNTER_IN;
98 counter->jump = __get_jump_by_intend(counter);
99 counter->iptype = NFACCT_TYPE_IPV4;
100 counter->send_limit = 0;
101 counter->rcv_limit = 0;
103 return produce_net_rule(counter);
106 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
109 return STC_ERROR_INVALID_PARAMETER;
111 counter->action = NFACCT_ACTION_DELETE;
112 counter->iotype = NFACCT_COUNTER_OUT;
113 counter->jump = __get_jump_by_intend(counter);
114 counter->iptype = NFACCT_TYPE_IPV4;
115 counter->send_limit = 0;
116 counter->rcv_limit = 0;
118 return produce_net_rule(counter);
121 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
124 return STC_ERROR_INVALID_PARAMETER;
126 counter->action = NFACCT_ACTION_INSERT;
127 counter->iotype = NFACCT_COUNTER_IN;
128 counter->jump = __get_jump_by_intend(counter);
129 counter->iptype = NFACCT_TYPE_IPV6;
130 counter->send_limit = 0;
131 counter->rcv_limit = 0;
133 return produce_net_rule(counter);
136 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
139 return STC_ERROR_INVALID_PARAMETER;
141 counter->action = NFACCT_ACTION_INSERT;
142 counter->iotype = NFACCT_COUNTER_OUT;
143 counter->jump = __get_jump_by_intend(counter);
144 counter->iptype = NFACCT_TYPE_IPV6;
145 counter->send_limit = 0;
146 counter->rcv_limit = 0;
148 return produce_net_rule(counter);
151 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
154 return STC_ERROR_INVALID_PARAMETER;
156 counter->action = NFACCT_ACTION_DELETE;
157 counter->iotype = NFACCT_COUNTER_IN;
158 counter->jump = __get_jump_by_intend(counter);
159 counter->iptype = NFACCT_TYPE_IPV6;
160 counter->send_limit = 0;
161 counter->rcv_limit = 0;
163 return produce_net_rule(counter);
166 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
169 return STC_ERROR_INVALID_PARAMETER;
171 counter->action = NFACCT_ACTION_DELETE;
172 counter->iotype = NFACCT_COUNTER_OUT;
173 counter->jump = __get_jump_by_intend(counter);
174 counter->iptype = NFACCT_TYPE_IPV6;
175 counter->send_limit = 0;
176 counter->rcv_limit = 0;
178 return produce_net_rule(counter);
181 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
182 gpointer UNUSED user_data)
184 stc_process_key_s *key_a = (stc_process_key_s *)a;
185 stc_process_key_s *key_b = (stc_process_key_s *)b;
187 return key_a->pid - key_b->pid;
190 static void __processes_tree_value_free(gpointer data)
192 stc_process_value_s *value = (stc_process_value_s *)data;
197 static void __processes_tree_key_free(gpointer data)
199 stc_process_key_s *key = (stc_process_key_s *)data;
204 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
205 gpointer UNUSED user_data)
207 stc_app_key_s *key_a = (stc_app_key_s *)a;
208 stc_app_key_s *key_b = (stc_app_key_s *)b;
211 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
215 return g_strcmp0(key_a->app_id, key_b->app_id);
218 static void __apps_tree_value_free(gpointer data)
220 stc_app_value_s *value = (stc_app_value_s *)data;
222 g_tree_destroy(value->processes);
223 value->processes = NULL;
228 static void __apps_tree_key_free(gpointer data)
230 stc_app_key_s *key = (stc_app_key_s *)data;
237 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
238 gpointer UNUSED user_data)
240 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
241 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
244 ret = g_strcmp0(key_a->app_id, key_b->app_id);
248 ret = g_strcmp0(key_a->ifname, key_b->ifname);
252 ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
256 ret = key_a->iftype - key_b->iftype;
263 static void __rstns_tree_value_free(gpointer data)
265 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
270 static void __rstns_tree_key_free(gpointer data)
272 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
276 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);
318 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
321 remove_pid_context_s *context = (remove_pid_context_s *)data;
322 stc_app_value_s *app_value = (stc_app_value_s *)value;
324 if (!g_tree_remove(app_value->processes, context->proc_key)) {
326 STC_LOGD("key not found");
330 context->entry_removed = TRUE;
331 context->app_key = (stc_app_key_s *)key;
336 static stc_app_value_s * __application_lookup(GTree *apps,
337 const stc_app_key_s *key)
339 stc_app_value_s *lookup;
341 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
343 lookup = g_tree_lookup(apps, key);
348 static stc_process_value_s * __process_lookup(GTree *processes,
349 const stc_process_key_s *key)
351 stc_process_value_s *lookup;
353 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
355 lookup = g_tree_lookup(processes, key);
360 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
363 guint *pid_count = (guint *)data;
368 static gboolean __add_application_monitor(gpointer key, gpointer value,
371 stc_app_value_s *app_value = (stc_app_value_s *)value;
372 default_connection_s *connection = (default_connection_s *)data;
373 stc_s *stc = stc_get_manager();
375 if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
376 app_value->classid == STC_TOTAL_WIFI_CLASSID ||
377 app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
380 if (stc && connection && connection->ifname) {
381 struct nfacct_rule counter;
384 stc->carg = MALLOC0(counter_arg_s, 1);
385 if (stc->carg == NULL)
388 stc->carg->sock = stc_monitor_get_counter_socket();
391 memset(&counter, 0, sizeof(struct nfacct_rule));
393 counter.carg = stc->carg;
394 counter.classid = app_value->classid;
395 counter.intend = NFACCT_COUNTER;
396 counter.iftype = connection->type;
397 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
399 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
400 __add_iptables_in(&counter);
401 __add_iptables_out(&counter);
402 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
403 __add_ip6tables_in(&counter);
404 __add_ip6tables_out(&counter);
406 __add_iptables_in(&counter);
407 __add_iptables_out(&counter);
408 __add_ip6tables_in(&counter);
409 __add_ip6tables_out(&counter);
416 static gboolean __remove_application_monitor(gpointer key, gpointer value,
419 stc_app_value_s *app_value = (stc_app_value_s *)value;
420 default_connection_s *connection = (default_connection_s *)data;
421 stc_s *stc = stc_get_manager();
423 if (stc && connection && connection->ifname) {
424 struct nfacct_rule counter;
427 stc->carg = MALLOC0(counter_arg_s, 1);
428 if (stc->carg == NULL)
431 stc->carg->sock = stc_monitor_get_counter_socket();
434 memset(&counter, 0, sizeof(struct nfacct_rule));
436 counter.carg = stc->carg;
437 counter.classid = app_value->classid;
438 counter.intend = NFACCT_COUNTER;
439 counter.iftype = connection->type;
440 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
442 __del_iptables_in(&counter);
443 __del_iptables_out(&counter);
444 __del_ip6tables_in(&counter);
445 __del_ip6tables_out(&counter);
451 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
453 STC_LOGI("rstn info => rstn_id [%llu], "
454 "app_id [%s], classid [%lu], ifname [%s], "
455 "iftype [%d], rst_state [%d], "
456 "limit [ (%lld) bytes], "
457 "warn_limit [ (%lld) bytes], "
458 "counter [ (%lld) bytes], "
459 "roaming [%d], subscriber_id [%s]",
460 rstn_value->restriction_id,
461 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
462 rstn_key->iftype, rstn_value->rst_state,
463 rstn_value->data_limit,
464 rstn_value->data_warn_limit,
465 rstn_value->data_counter,
466 rstn_key->roaming, rstn_key->subscriber_id);
469 static void __process_restriction(enum traffic_restriction_type rst_type,
470 stc_rstn_key_s *rstn_key,
471 stc_rstn_value_s *rstn_value, void *data)
473 int64_t effective_data_limit, effective_data_warn_limit;
474 default_connection_s *old_connection = (default_connection_s *)data;
475 default_connection_s *connection = NULL;
477 if (old_connection != NULL)
478 connection = old_connection;
480 connection = stc_get_default_connection();
482 /* no default ifname */
483 if (connection->ifname == NULL)
486 /* rstn not applicable for this interface */
487 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
488 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
491 /* classid is invalid */
492 if (rstn_value->classid == STC_UNKNOWN_CLASSID)
495 effective_data_limit = rstn_value->data_limit;
496 effective_data_warn_limit = rstn_value->data_warn_limit;
498 if (rst_type == RST_SET) {
499 /* TODO: Change this to runtime memory */
500 table_counters_info info;
502 memset(&info, 0, sizeof(table_counters_info));
503 table_counters_get(rstn_value->restriction_id, &info);
505 effective_data_limit -= info.data_counter;
506 effective_data_warn_limit -= info.data_counter;
508 if (effective_data_limit < 0) {
509 effective_data_limit = 0;
510 rstn_value->data_limit_reached = TRUE;
513 if (effective_data_warn_limit < 0)
514 effective_data_warn_limit = 0;
516 STC_LOGD("datausage [%lld] bytes", info.data_counter);
519 STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
520 "effective_data_warn_limit [%lld] bytes",
521 rstn_value->restriction_id, effective_data_limit,
522 effective_data_warn_limit);
526 if (effective_data_limit <= 0) {
527 char *default_ifname = stc_default_connection_get_ifname();
528 struct nfacct_rule counter;
529 stc_s *stc = stc_get_manager();
531 g_free(default_ifname);
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();
577 g_free(default_ifname);
582 stc->carg = MALLOC0(counter_arg_s, 1);
583 if (stc->carg == NULL) {
584 g_free(default_ifname);
589 stc_monitor_get_counter_socket();
592 counter.carg = stc->carg;
593 counter.classid = rstn_value->classid;
594 counter.intend = NFACCT_BLOCK;
595 counter.iftype = rstn_key->iftype;
596 g_strlcpy(counter.ifname, default_ifname,
599 generate_counter_name(&counter);
600 g_free(default_ifname);
603 __del_iptables_in(&counter);
604 __del_iptables_out(&counter);
607 __del_ip6tables_in(&counter);
608 __del_ip6tables_out(&counter);
610 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
611 rstn_value->data_limit_reached = FALSE;
619 static gboolean __remove_rstns_foreach_application(gpointer key,
623 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
624 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
625 gchar *app_id = (gchar *)data;
627 /* rstn rule is not for applications */
628 if (rstn_key->app_id == NULL)
631 /* rstn rule is not for this application */
632 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
635 /* rstn rule is already removed */
636 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
639 /* remove restriction from system */
640 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
642 __print_rstn(rstn_key, rstn_value);
647 static void __remove_rstns_for_application(gchar *app_id)
649 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
653 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
655 stc_error_e ret = STC_ERROR_NONE;
657 stc_app_value_s *lookup;
659 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
661 lookup = __application_lookup(g_system->apps, app_key);
663 STC_LOGE("app_key not found");
664 return STC_ERROR_NO_DATA;
667 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
671 /* remove nfacct rule for this classid */
672 __remove_application_monitor((gpointer) app_key, lookup,
673 stc_get_default_connection());
674 __remove_rstns_for_application(app_key->app_id);
677 if (!g_tree_remove(g_system->apps, app_key)) {
678 ret = STC_ERROR_NO_DATA;
679 STC_LOGE("key not found");
685 static stc_error_e __close_contr_sock(stc_system_s *system)
687 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
689 /* close netlink socket for updating kernel counters */
690 if (system->contr_sock != -1) {
691 close(system->contr_sock);
692 system->contr_sock = -1;
695 if (system->contr_gsource_id != 0) {
696 g_source_remove(system->contr_gsource_id);
697 system->contr_gsource_id = 0;
700 return STC_ERROR_NONE;
703 static gboolean __process_contr_reply(GIOChannel *source,
704 GIOCondition condition,
707 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
709 GIOChannel *gio = NULL;
710 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
712 /* close netlink socket for updating kernel counters */
713 if (system->contr_sock != -1) {
714 close(system->contr_sock);
715 system->contr_sock = -1;
718 if (system->contr_gsource_id != 0) {
719 g_source_remove(system->contr_gsource_id);
720 system->contr_gsource_id = 0;
723 /* create netlink socket for updating kernel counters */
724 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
725 if (system->contr_sock < 0) {
726 STC_LOGE("failed to open socket");
728 return STC_ERROR_FAIL;
731 gio = g_io_channel_unix_new(system->contr_sock);
732 system->contr_gsource_id =
733 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
734 (GIOFunc) __process_contr_reply,
736 g_io_channel_unref(gio);
738 return STC_ERROR_NONE;
741 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
742 stc_rstn_value_s *rstn_value,
743 classid_bytes_context_s *context)
745 switch (context->counter->iotype) {
746 case NFACCT_COUNTER_IN:
747 case NFACCT_COUNTER_OUT:
748 rstn_value->data_counter += context->bytes;
750 if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
751 rstn_value->warn_limit_crossed_notified == FALSE) {
754 char iftype[MAX_INT_LENGTH];
755 char byte[MAX_INT_LENGTH];
756 stc_s *stc = (stc_s *)stc_get_manager();
757 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
760 rv = stc_manager_dbus_emit_signal(stc->connection,
761 STC_DBUS_SERVICE_RESTRICTION_PATH,
762 STC_DBUS_INTERFACE_RESTRICTION,
763 "WarnThresholdCrossed",
764 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
766 rstn_value->warn_limit_crossed_notified = TRUE;
768 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
769 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
770 stc_send_warn_message_to_net_popup("warn threshold crossed",
776 if (rstn_value->data_counter >= rstn_value->data_limit &&
777 rstn_value->rstn_limit_crossed_notified == FALSE) {
780 char iftype[MAX_INT_LENGTH];
781 char byte[MAX_INT_LENGTH];
782 stc_s *stc = (stc_s *)stc_get_manager();
783 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
785 /* block immediately */
786 context->counter->intend = NFACCT_BLOCK;
787 __del_iptables_in(context->counter);
788 __del_iptables_out(context->counter);
789 __add_iptables_in(context->counter);
790 __add_iptables_out(context->counter);
792 __del_ip6tables_in(context->counter);
793 __del_ip6tables_out(context->counter);
794 __add_ip6tables_in(context->counter);
795 __add_ip6tables_out(context->counter);
796 context->counter->intend = NFACCT_COUNTER;
798 rstn_value->data_limit_reached = TRUE;
801 rv = stc_manager_dbus_emit_signal(stc->connection,
802 STC_DBUS_SERVICE_RESTRICTION_PATH,
803 STC_DBUS_INTERFACE_RESTRICTION,
804 "RestrictionThresholdCrossed",
805 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
807 rstn_value->rstn_limit_crossed_notified = TRUE;
809 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
810 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
811 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
812 "restriction_noti", rstn_key->app_id,
816 g_system->rstns_tree_updated = TRUE;
817 __print_rstn(rstn_key, rstn_value);
820 STC_LOGE("unknown iotype");
826 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
827 stc_rstn_value_s *rstn_value,
828 classid_bytes_context_s *context)
830 if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
831 context->counter->iftype == STC_IFACE_DATACALL) ||
832 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
833 context->counter->iftype == STC_IFACE_WIFI) ||
834 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
835 context->counter->iftype == STC_IFACE_BLUETOOTH)) {
836 context->counter->classid = rstn_value->classid;
837 return __rstn_counter_update(rstn_key, rstn_value, context);
843 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
848 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
849 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
850 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
853 if (context->counter->intend != NFACCT_COUNTER)
854 goto try_next_callback;
856 if (rstn_value->data_limit_reached == TRUE) {
857 context->data_limit_reached = TRUE;
858 goto try_next_callback;
861 classid = context->counter->classid;
862 rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
864 context->counter->classid = classid;
865 if (rstn_value->classid != context->counter->classid)
866 goto try_next_callback;
868 rv = __rstn_counter_update(rstn_key, rstn_value, context);
874 static gboolean __update_app_statistics(gpointer key, gpointer value,
877 stc_app_key_s *app_key = (stc_app_key_s *)key;
878 stc_app_value_s *app_value = (stc_app_value_s *)value;
879 time_t *touch_time = (time_t *)data;
880 stc_db_classid_iftype_key stat_key;
881 stc_db_app_stats stat;
882 default_connection_s *default_connection = stc_get_default_connection();
884 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
885 memset(&stat, 0 , sizeof(stc_db_app_stats));
887 stat_key.classid = app_value->classid;
888 stat_key.iftype = default_connection->type;
890 if (STC_IFACE_DATACALL == stat_key.iftype)
891 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
893 stat_key.subscriber_id = g_strdup("none_subid");
895 g_strlcpy(stat_key.ifname, default_connection->ifname,
898 stat.app_id = g_strdup(app_key->app_id);
899 stat.snd_count = app_value->counter.out_bytes;
900 stat.rcv_count = app_value->counter.in_bytes;
901 stat.is_roaming = default_connection->roaming;
903 if (strstr(stat.app_id, "_BACKGROUND")) {
904 stat.ground = STC_APP_STATE_BACKGROUND;
906 if (strstr(stat.app_id, "TOTAL_"))
907 stat.ground = STC_APP_STATE_UNKNOWN;
909 stat.ground = STC_APP_STATE_FOREGROUND;
912 table_statistics_insert(&stat_key, &stat, *touch_time);
914 app_value->counter.out_bytes = 0;
915 app_value->counter.in_bytes = 0;
918 FREE(stat_key.subscriber_id);
923 static gboolean __flush_apps_stats_to_database(gpointer user_data)
925 time_t current_time = time(0);
927 if (g_system->apps_tree_updated == FALSE)
928 return G_SOURCE_REMOVE;
930 g_system->apps_tree_updated = FALSE;
933 g_tree_foreach(g_system->apps,
934 __update_app_statistics,
937 STC_LOGI("Flushed app stats to database");
938 return G_SOURCE_REMOVE;
941 static gboolean __update_counter_statistics(gpointer key, gpointer value,
944 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
945 table_counters_info info = {
946 .restriction_id = rstn_value->restriction_id,
947 .data_counter = rstn_value->data_counter
950 table_counters_update_counters(&info);
955 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
957 time_t current_time = time(0);
959 if (g_system->rstns_tree_updated == FALSE)
960 return G_SOURCE_REMOVE;
962 g_system->rstns_tree_updated = FALSE;
965 g_tree_foreach(g_system->rstns,
966 __update_counter_statistics,
969 STC_LOGI("Flushed rstns counters to database");
970 return G_SOURCE_REMOVE;
973 static void __app_counter_update(stc_app_key_s *app_key,
974 stc_app_value_s *app_value,
975 classid_bytes_context_s *context)
977 switch (context->counter->iotype) {
978 case NFACCT_COUNTER_IN:
979 app_value->data_usage.in_bytes += context->bytes;
980 app_value->counter.in_bytes = context->bytes;
981 g_system->apps_tree_updated = TRUE;
984 __apps_tree_foreach_print(app_key, app_value, NULL);
986 case NFACCT_COUNTER_OUT:
987 app_value->data_usage.out_bytes += context->bytes;
988 app_value->counter.out_bytes = context->bytes;
989 g_system->apps_tree_updated = TRUE;
992 __apps_tree_foreach_print(app_key, app_value, NULL);
995 STC_LOGE("unknown iotype");
999 static void __interface_counter_update(stc_app_key_s *app_key,
1000 stc_app_value_s *app_value,
1001 classid_bytes_context_s *context)
1003 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1004 context->counter->iftype == STC_IFACE_DATACALL) ||
1005 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1006 context->counter->iftype == STC_IFACE_WIFI) ||
1007 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1008 context->counter->iftype == STC_IFACE_BLUETOOTH))
1009 __app_counter_update(app_key, app_value, context);
1013 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1017 stc_app_key_s *app_key = (stc_app_key_s *)key;
1018 stc_app_value_s *app_value = (stc_app_value_s *)value;
1019 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1021 if (context->counter->intend != NFACCT_COUNTER)
1022 goto try_next_callback;
1024 __interface_counter_update(app_key, app_value, context);
1026 if (app_value->classid != context->counter->classid)
1027 goto try_next_callback;
1029 __app_counter_update(app_key, app_value, context);
1035 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1036 struct counter_arg *carg)
1038 struct nfacct_rule counter = {
1045 classid_bytes_context_s context = {
1046 .counter = &counter,
1048 .data_limit_reached = FALSE,
1052 STC_LOGD("cnt_name %s", cnt_name);
1054 if (!recreate_counter_by_name(cnt_name, &counter)) {
1055 STC_LOGE("Can't parse counter name %s", cnt_name);
1059 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1060 context.counter->classid, context.counter->iftype,
1061 context.counter->iotype, context.counter->intend,
1062 context.counter->ifname, context.bytes);
1064 if (g_system->rstns)
1065 g_tree_foreach(g_system->rstns,
1066 __rstn_counter_update_foreach_classid,
1070 g_tree_foreach(g_system->apps,
1071 __apps_counter_update_foreach_classid,
1075 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1078 struct counter_arg *carg = user_data;
1079 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1080 if (carg->initiate) {
1082 * TODO: this will be used when daemon starts to update existing
1083 * counter data if present.
1085 populate_counters(cnt_name, carg);
1089 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1090 int bytes = be64toh(*bytes_p);
1092 ++carg->serialized_counters;
1093 __fill_nfacct_result(cnt_name, bytes, carg);
1100 static int __post_fill_counters(void *user_data)
1102 struct counter_arg *carg = user_data;
1110 static void __process_network_counter(struct genl *ans,
1111 struct counter_arg *carg)
1113 struct netlink_serialization_params ser_params = {
1116 .eval_attr = __fill_counters,
1117 .post_eval_attr = __post_fill_counters,
1120 netlink_serialization_command *netlink =
1121 netlink_create_command(&ser_params);
1123 STC_LOGE("Can not create command");
1127 netlink->deserialize_answer(&(netlink->params));
1130 static gboolean __process_contr_reply(GIOChannel *source,
1131 GIOCondition condition,
1134 int sock = g_io_channel_unix_get_fd(source);
1137 stc_s *stc = stc_get_manager();
1139 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1140 (condition & G_IO_NVAL)) {
1141 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1143 STC_LOGE("Counter socket received G_IO event, closing socket."
1144 "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
1145 (condition & G_IO_ERR), (condition & G_IO_HUP),
1146 (condition & G_IO_NVAL));
1147 __close_and_reopen_contr_sock(g_system);
1151 ans = MALLOC0(struct genl, 1);
1153 STC_LOGE("Failed allocate memory to genl reply message");
1158 STC_LOGE("Can't get stc data");
1162 ret = read_netlink(sock, ans, sizeof(struct genl));
1163 /* STC_LOGD("Counter data received ret [%d]", ret); */
1167 stc->carg->ans_len = ret;
1168 __process_network_counter(ans, stc->carg);
1170 g_idle_add(__flush_apps_stats_to_database, NULL);
1171 g_idle_add(__flush_rstns_counter_to_database, NULL);
1177 static gboolean __update_contr_cb(void *user_data)
1179 /* Here we just sent command, answer we receive in another callback */
1180 stc_s *stc = stc_get_manager();
1181 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1183 stc->carg = MALLOC0(counter_arg_s, 1);
1184 if (stc->carg == NULL)
1185 return TRUE; /* we need to continue the timer */
1187 stc->carg->sock = stc_monitor_get_counter_socket();
1190 /* STC_LOGD("Get all counters"); */
1191 nfacct_send_get_all(stc->carg);
1193 /* we need to continue the timer */
1197 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1200 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1201 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1203 __print_rstn(rstn_key, rstn_value);
1207 static void __rstn_tree_printall(void)
1209 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1212 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1213 const stc_rstn_key_s *key)
1215 stc_rstn_value_s *lookup;
1217 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1219 lookup = g_tree_lookup(rstns_tree, key);
1224 static gboolean __remove_restriction(gpointer key, gpointer value,
1227 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1228 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1230 /* rstn rule is already removed */
1231 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
1234 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1235 __print_rstn(rstn_key, rstn_value);
1239 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1242 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1243 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1245 /* rstn rule is activated */
1246 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1249 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1250 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1252 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1254 __print_rstn(rstn_key, rstn_value);
1259 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1261 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1262 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1264 /* rstn rule is activated */
1265 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1268 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1269 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1271 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1276 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1278 stc_rstn_value_s *lookup_value;
1280 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1282 lookup_value = __rstn_lookup(g_system->rstns, key);
1283 if (!lookup_value) {
1284 STC_LOGE("key not found");
1285 return STC_ERROR_NO_DATA;
1288 __remove_restriction(key, lookup_value, NULL);
1290 /* remove counter also */
1291 table_counters_delete(lookup_value->restriction_id);
1293 if (!g_tree_remove(g_system->rstns, key)) {
1294 STC_LOGD("key not found");
1295 return STC_ERROR_NO_DATA;
1298 return STC_ERROR_NONE;
1301 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1302 stc_rstn_value_s *value, gboolean debug)
1304 stc_rstn_value_s *rstn_value;
1306 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1308 rstn_value = __rstn_lookup(g_system->rstns, key);
1310 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1312 STC_LOGE("rstn_key allocation failed");
1313 return STC_ERROR_OUT_OF_MEMORY;
1316 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1318 STC_LOGE("rstn_value allocation failed");
1320 return STC_ERROR_OUT_OF_MEMORY;
1323 rstn_key->app_id = g_strdup(key->app_id);
1324 rstn_key->ifname = g_strdup(key->ifname);
1325 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1326 rstn_key->iftype = key->iftype;
1327 rstn_key->roaming = key->roaming;
1329 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1332 rstn_value->restriction_id = value->restriction_id;
1333 rstn_value->rst_state = value->rst_state;
1334 rstn_value->classid = value->classid;
1335 rstn_value->data_limit = value->data_limit;
1336 rstn_value->data_warn_limit = value->data_warn_limit;
1337 rstn_value->data_counter = 0;
1338 rstn_value->warn_limit_crossed_notified = FALSE;
1339 rstn_value->rstn_limit_crossed_notified = FALSE;
1342 __add_restriction_debug(key, rstn_value, NULL);
1344 __add_restriction(key, rstn_value, NULL);
1346 return STC_ERROR_NONE;
1349 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1352 stc_cb_ret_e ret = STC_CONTINUE;
1355 stc_rstn_value_s value;
1357 memset(&key, 0, sizeof(stc_rstn_key_s));
1358 memset(&value, 0, sizeof(stc_rstn_value_s));
1360 key.app_id = g_strdup(info->app_id);
1361 key.ifname = g_strdup(info->ifname);
1362 key.subscriber_id = g_strdup(info->subscriber_id);
1363 key.iftype = info->iftype;
1364 key.roaming = info->roaming;
1366 value.rst_state = info->rst_state;
1367 value.restriction_id = info->restriction_id;
1369 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1370 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1372 value.classid = STC_UNKNOWN_CLASSID;
1374 value.data_limit = info->data_limit;
1375 value.data_warn_limit = info->data_warn_limit;
1377 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1382 FREE(key.subscriber_id);
1386 static void __fill_restritions_list(void)
1388 table_restrictions_foreach(__insert_restriction_cb, NULL);
1389 //__rstn_tree_printall();
1392 static gboolean __add_rstn_foreach_application(gpointer key,
1396 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1397 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1398 gchar *app_id = (gchar *)data;
1400 /* rstn rule is not for applications */
1401 if (rstn_key->app_id == NULL)
1404 /* rstn rule is not for this application */
1405 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1408 /* rstn rule is already applied */
1409 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1412 /* add restriction to system */
1413 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1414 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1416 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1418 __print_rstn(rstn_key, rstn_value);
1423 static void __add_rstns_for_application(gchar *app_id)
1425 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1429 static void __add_application_by_interface(const char *app_id)
1431 stc_app_key_s app_key;
1432 stc_app_value_s app_value;
1437 memset(&app_key, 0, sizeof(stc_app_key_s));
1438 memset(&app_value, 0, sizeof(stc_app_value_s));
1440 app_key.pkg_id = g_strdup(app_id);
1441 app_key.app_id = g_strdup(app_id);
1443 app_value.type = STC_APP_TYPE_NONE;
1444 app_value.processes = NULL;
1445 app_value.counter.in_bytes = 0;
1446 app_value.counter.out_bytes = 0;
1448 stc_monitor_application_add(app_key, app_value);
1450 FREE(app_key.pkg_id);
1451 FREE(app_key.app_id);
1454 static int __vconf_get_int(const char *key, int *value)
1458 ret = vconf_get_int(key, value);
1459 if (ret != VCONF_OK) {
1460 STC_LOGE("Failed to get vconfkey [%s] value", key);
1467 static int __vconf_set_int(const char *key, int value)
1471 ret = vconf_set_int(key, value);
1472 if (ret != VCONF_OK) {
1473 STC_LOGE("Failed to set vconfkey [%s] value", key);
1480 static guint __get_background_state(void)
1482 return g_system->background_state;;
1485 static void __set_background_state(guint state)
1487 g_system->background_state = state;
1490 static gboolean __processes_tree_foreach_background(gpointer key,
1494 stc_process_key_s *proc_key = (stc_process_key_s *)key;
1495 stc_app_key_s *app_key = (stc_app_key_s *)data;
1497 if (g_system->background_state)
1498 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1500 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1505 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1508 stc_app_key_s *app_key = (stc_app_key_s *)key;
1509 stc_app_value_s *app_value = (stc_app_value_s *)value;
1511 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1512 g_tree_foreach(app_value->processes,
1513 __processes_tree_foreach_background, app_key);
1518 static stc_error_e __process_update_background(void)
1520 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1522 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1524 return STC_ERROR_NONE;
1528 static void __excn_hash_foreach_print(gpointer key, gpointer value,
1531 const char *process_name = key;
1532 const char *exe_type = value;
1534 STC_LOGI("excn info => process_name [%s] exe_type [%s]",
1535 process_name, exe_type);
1538 static void __excn_hash_printall(void)
1540 g_hash_table_foreach(g_system->excns_hash,
1541 __excn_hash_foreach_print, NULL);
1545 static gboolean __remove_exception_app(gpointer key, gpointer value,
1548 const char *exe_type = value;
1550 if (g_strcmp0(exe_type, EXE_TYPE_APPLICATION) == 0)
1556 static void __remove_exception_appall(void)
1558 g_hash_table_foreach_remove(g_system->excns_hash,
1559 __remove_exception_app, NULL);
1562 static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info,
1565 stc_cb_ret_e ret = STC_CONTINUE;
1567 if (g_hash_table_insert(g_system->excns_hash,
1568 g_strdup(info->process_name),
1569 g_strdup(info->exe_type)) != TRUE)
1575 static void __fill_exceptions_list(void)
1577 table_exceptions_foreach(__insert_exception_cb, NULL);
1578 pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
1580 /* __excn_hash_printall(); */
1583 static gboolean __update_exceptions_app_list(void *user_data)
1585 __remove_exception_appall();
1586 pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
1588 /* __excn_hash_printall(); */
1593 stc_error_e stc_monitor_init(void)
1595 stc_system_s *system = MALLOC0(stc_system_s, 1);
1596 GIOChannel *gio = NULL;
1598 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1600 /* initializing current classid */
1601 init_current_classid();
1603 /* initializing cgroups */
1606 /* creating monitored application tree */
1607 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1608 __apps_tree_key_free,
1609 __apps_tree_value_free);
1611 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1612 __rstns_tree_key_free,
1613 __rstns_tree_value_free);
1615 system->excns_hash = g_hash_table_new_full(g_str_hash,
1616 g_str_equal, g_free, g_free);
1618 /* create netlink socket for updating kernel counters */
1619 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1620 if (system->contr_sock < 0) {
1621 STC_LOGE("failed to open socket");
1623 return STC_ERROR_FAIL;
1626 gio = g_io_channel_unix_new(system->contr_sock);
1627 system->contr_gsource_id =
1628 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1629 (GIOFunc) __process_contr_reply,
1631 g_io_channel_unref(gio);
1635 __add_application_by_interface(STC_TOTAL_DATACALL);
1636 __add_application_by_interface(STC_TOTAL_WIFI);
1637 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1638 __add_application_by_interface(STC_TOTAL_IPV4);
1639 __add_application_by_interface(STC_TOTAL_IPV6);
1641 /* creating restriction rules tree */
1642 __update_contr_cb(NULL);
1644 /* registering periodic kernel counters update callback */
1645 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1648 if (g_system->contr_timer_id == 0) {
1649 STC_LOGE("Failed to register kernel counters update timer");
1650 __close_contr_sock(g_system);
1651 return STC_ERROR_FAIL;
1654 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1655 (int *)&g_system->background_state);
1657 __fill_exceptions_list();
1658 __fill_restritions_list();
1660 g_system->excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL,
1661 __update_exceptions_app_list,
1664 return STC_ERROR_NONE;
1667 stc_error_e stc_monitor_deinit(void)
1669 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1671 /* close netlink socket for updating kernel counters */
1672 __close_contr_sock(g_system);
1674 /* remove kernel counters update timer */
1675 if (g_system->contr_timer_id > 0) {
1676 g_source_remove(g_system->contr_timer_id);
1677 g_system->contr_timer_id = 0;
1680 /* remove exceptions app list update timer */
1681 if (g_system->excns_timer_id > 0) {
1682 g_source_remove(g_system->excns_timer_id);
1683 g_system->excns_timer_id = 0;
1686 /* destroy monitored application tree */
1687 g_tree_destroy(g_system->apps);
1688 g_system->apps = NULL;
1690 /* destroy restriction rules tree */
1691 g_tree_destroy(g_system->rstns);
1692 g_system->rstns = NULL;
1694 /* destroy exception hash table */
1695 g_hash_table_destroy(g_system->excns_hash);
1696 g_system->excns_hash = NULL;
1700 return STC_ERROR_NONE;
1703 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1704 const stc_app_value_s app_value)
1706 stc_error_e ret = STC_ERROR_NONE;
1708 stc_app_value_s *value;
1709 stc_app_value_s *lookup;
1711 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1713 lookup = __application_lookup(g_system->apps, &app_key);
1715 STC_LOGD("app_key already present");
1716 return STC_ERROR_NONE;
1719 key = MALLOC0(stc_app_key_s, 1);
1721 STC_LOGE("key allocation failed");
1722 return STC_ERROR_OUT_OF_MEMORY;
1725 value = MALLOC0(stc_app_value_s, 1);
1727 STC_LOGE("value allocation failed");
1729 return STC_ERROR_OUT_OF_MEMORY;
1732 key->app_id = g_strdup(app_key.app_id);
1733 key->pkg_id = g_strdup(app_key.pkg_id);
1735 value->type = app_value.type;
1736 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1737 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1739 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1740 __processes_tree_key_free,
1741 __processes_tree_value_free);
1743 /* create cgroup and update classid */
1744 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1746 g_tree_insert(g_system->apps, key, value);
1748 /* add nfacct rule for this classid */
1749 __add_application_monitor(key, value, stc_get_default_connection());
1750 __add_rstns_for_application(app_key.app_id);
1755 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1756 const stc_process_key_s proc_key,
1757 const stc_process_value_s proc_value)
1759 stc_error_e ret = STC_ERROR_NONE;
1760 stc_app_value_s *app_lookup;
1761 stc_process_key_s *key;
1762 stc_process_value_s *value;
1763 stc_process_value_s *proc_lookup;
1765 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1767 app_lookup = __application_lookup(g_system->apps, &app_key);
1769 STC_LOGD("app_key not found");
1770 return STC_ERROR_FAIL;
1773 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1775 STC_LOGD("proc_key already present");
1776 return STC_ERROR_NONE;
1779 key = MALLOC0(stc_process_key_s, 1);
1781 STC_LOGE("key allocation failed");
1782 return STC_ERROR_OUT_OF_MEMORY;
1785 value = MALLOC0(stc_process_value_s, 1);
1787 STC_LOGE("value allocation failed");
1789 return STC_ERROR_OUT_OF_MEMORY;
1792 key->pid = proc_key.pid;
1794 value->ground = proc_value.ground;
1796 g_tree_insert(app_lookup->processes, key, value);
1798 /* add pid to application cgroup */
1799 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1802 __apps_tree_printall();
1807 stc_error_e stc_monitor_process_remove(pid_t pid)
1809 stc_error_e ret = STC_ERROR_NONE;
1810 stc_process_key_s proc_key = {
1814 remove_pid_context_s context = {
1816 .proc_key = &proc_key,
1817 .entry_removed = FALSE,
1820 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1822 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1825 if (context.entry_removed)
1826 __application_remove_if_empty(context.app_key);
1829 __apps_tree_printall();
1834 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1835 const stc_process_key_s proc_key,
1836 stc_app_state_e ground)
1838 stc_error_e ret = STC_ERROR_NONE;
1839 stc_app_value_s *app_lookup;
1840 stc_process_value_s *proc_lookup;
1842 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1844 app_lookup = __application_lookup(g_system->apps, &app_key);
1846 STC_LOGD("app_key not found");
1847 return STC_ERROR_FAIL;
1850 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1852 STC_LOGD("proc_key not found");
1853 return STC_ERROR_FAIL;
1856 if (proc_lookup->ground != ground)
1857 proc_lookup->ground = ground;
1859 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1860 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1862 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1867 void stc_monitor_update_rstn_by_default_connection(void *data)
1869 static default_connection_s old_connection;
1870 default_connection_s *new_connection = (default_connection_s *)data;
1872 if (old_connection.path != NULL) {
1874 g_tree_foreach(g_system->apps,
1875 __remove_application_monitor,
1876 (gpointer)&old_connection);
1878 if (g_system->rstns)
1879 g_tree_foreach(g_system->rstns,
1880 __remove_restriction,
1881 (gpointer)&old_connection);
1883 iptables_flush_chains();
1886 FREE(old_connection.path);
1887 FREE(old_connection.ifname);
1888 old_connection.type = 0;
1889 old_connection.roaming = 0;
1891 if (new_connection != NULL && new_connection->path != NULL) {
1893 g_tree_foreach(g_system->apps,
1894 __add_application_monitor,
1895 (gpointer)new_connection);
1897 if (g_system->rstns)
1898 g_tree_foreach(g_system->rstns, __add_restriction,
1901 old_connection.path = g_strdup(new_connection->path);
1902 old_connection.ifname = g_strdup(new_connection->ifname);
1903 old_connection.type = new_connection->type;
1904 old_connection.roaming = new_connection->roaming;
1908 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1913 stc_rstn_value_s value;
1915 memset(&key, 0, sizeof(stc_rstn_key_s));
1916 memset(&value, 0, sizeof(stc_rstn_value_s));
1918 key.app_id = g_strdup(info->app_id);
1919 key.ifname = g_strdup(info->ifname);
1920 key.subscriber_id = g_strdup(info->subscriber_id);
1921 key.iftype = info->iftype;
1922 key.roaming = info->roaming;
1924 value.rst_state = info->rst_state;
1925 value.restriction_id = info->restriction_id;
1927 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1928 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1930 value.classid = STC_UNKNOWN_CLASSID;
1932 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1933 __set_background_state(TRUE);
1934 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
1935 __process_update_background();
1938 value.data_limit = info->data_limit;
1939 value.data_warn_limit = info->data_warn_limit;
1941 ret = __rstn_tree_add(&key, &value, TRUE);
1945 FREE(key.subscriber_id);
1949 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1953 stc_rstn_key_s key = {
1954 .app_id = g_strdup(info->app_id),
1955 .ifname = g_strdup(info->ifname),
1956 .subscriber_id = g_strdup(info->subscriber_id),
1957 .iftype = info->iftype,
1958 .roaming = info->roaming,
1961 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
1962 __set_background_state(FALSE);
1963 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
1964 __process_update_background();
1967 ret = __rstn_tree_remove(&key);
1971 FREE(key.subscriber_id);
1975 stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
1977 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1979 char *exe_type = NULL;
1981 exe_type = g_hash_table_lookup(g_system->excns_hash, cmdline);
1983 return STC_ERROR_NO_DATA;
1985 return STC_ERROR_NONE;
1988 int stc_monitor_get_counter_socket(void)
1990 return g_system->contr_sock;