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-monitor.h"
31 #include "stc-manager-plugin.h"
33 #define MAX_INT_LENGTH 128
34 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
37 stc_app_key_s *app_key;
38 stc_process_key_s *proc_key;
39 gboolean entry_removed;
40 } remove_pid_context_s;
43 struct nfacct_rule *counter;
45 gboolean data_limit_reached;
46 } classid_bytes_context_s;
48 static stc_system_s *g_system = NULL;
50 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
52 if (counter->intend == NFACCT_WARN)
53 return NFACCT_JUMP_ACCEPT;
54 else if (counter->intend == NFACCT_BLOCK)
55 return NFACCT_JUMP_REJECT;
57 return NFACCT_JUMP_UNKNOWN;
60 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
63 return STC_ERROR_INVALID_PARAMETER;
65 counter->action = NFACCT_ACTION_INSERT;
66 counter->iotype = NFACCT_COUNTER_IN;
67 counter->jump = __get_jump_by_intend(counter);
68 counter->iptype = NFACCT_TYPE_IPV4;
69 counter->send_limit = 0;
70 counter->rcv_limit = 0;
72 return produce_net_rule(counter);
75 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
78 return STC_ERROR_INVALID_PARAMETER;
80 counter->action = NFACCT_ACTION_INSERT;
81 counter->iotype = NFACCT_COUNTER_OUT;
82 counter->jump = __get_jump_by_intend(counter);
83 counter->iptype = NFACCT_TYPE_IPV4;
84 counter->send_limit = 0;
85 counter->rcv_limit = 0;
87 return produce_net_rule(counter);
90 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
93 return STC_ERROR_INVALID_PARAMETER;
95 counter->action = NFACCT_ACTION_DELETE;
96 counter->iotype = NFACCT_COUNTER_IN;
97 counter->jump = __get_jump_by_intend(counter);
98 counter->iptype = NFACCT_TYPE_IPV4;
99 counter->send_limit = 0;
100 counter->rcv_limit = 0;
102 return produce_net_rule(counter);
105 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
108 return STC_ERROR_INVALID_PARAMETER;
110 counter->action = NFACCT_ACTION_DELETE;
111 counter->iotype = NFACCT_COUNTER_OUT;
112 counter->jump = __get_jump_by_intend(counter);
113 counter->iptype = NFACCT_TYPE_IPV4;
114 counter->send_limit = 0;
115 counter->rcv_limit = 0;
117 return produce_net_rule(counter);
120 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
123 return STC_ERROR_INVALID_PARAMETER;
125 counter->action = NFACCT_ACTION_INSERT;
126 counter->iotype = NFACCT_COUNTER_IN;
127 counter->jump = __get_jump_by_intend(counter);
128 counter->iptype = NFACCT_TYPE_IPV6;
129 counter->send_limit = 0;
130 counter->rcv_limit = 0;
132 return produce_net_rule(counter);
135 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
138 return STC_ERROR_INVALID_PARAMETER;
140 counter->action = NFACCT_ACTION_INSERT;
141 counter->iotype = NFACCT_COUNTER_OUT;
142 counter->jump = __get_jump_by_intend(counter);
143 counter->iptype = NFACCT_TYPE_IPV6;
144 counter->send_limit = 0;
145 counter->rcv_limit = 0;
147 return produce_net_rule(counter);
150 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
153 return STC_ERROR_INVALID_PARAMETER;
155 counter->action = NFACCT_ACTION_DELETE;
156 counter->iotype = NFACCT_COUNTER_IN;
157 counter->jump = __get_jump_by_intend(counter);
158 counter->iptype = NFACCT_TYPE_IPV6;
159 counter->send_limit = 0;
160 counter->rcv_limit = 0;
162 return produce_net_rule(counter);
165 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
168 return STC_ERROR_INVALID_PARAMETER;
170 counter->action = NFACCT_ACTION_DELETE;
171 counter->iotype = NFACCT_COUNTER_OUT;
172 counter->jump = __get_jump_by_intend(counter);
173 counter->iptype = NFACCT_TYPE_IPV6;
174 counter->send_limit = 0;
175 counter->rcv_limit = 0;
177 return produce_net_rule(counter);
180 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
181 gpointer UNUSED user_data)
183 stc_process_key_s *key_a = (stc_process_key_s *)a;
184 stc_process_key_s *key_b = (stc_process_key_s *)b;
186 return key_a->pid - key_b->pid;
189 static void __processes_tree_value_free(gpointer data)
191 stc_process_value_s *value = (stc_process_value_s *)data;
196 static void __processes_tree_key_free(gpointer data)
198 stc_process_key_s *key = (stc_process_key_s *)data;
203 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
204 gpointer UNUSED user_data)
206 stc_app_key_s *key_a = (stc_app_key_s *)a;
207 stc_app_key_s *key_b = (stc_app_key_s *)b;
210 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
214 return g_strcmp0(key_a->app_id, key_b->app_id);
217 static void __apps_tree_value_free(gpointer data)
219 stc_app_value_s *value = (stc_app_value_s *)data;
221 g_tree_destroy(value->processes);
222 value->processes = NULL;
227 static void __apps_tree_key_free(gpointer data)
229 stc_app_key_s *key = (stc_app_key_s *)data;
236 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
237 gpointer UNUSED user_data)
239 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
240 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
243 ret = g_strcmp0(key_a->app_id, key_b->app_id);
247 ret = g_strcmp0(key_a->ifname, key_b->ifname);
251 ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
255 ret = key_a->iftype - key_b->iftype;
262 static void __rstns_tree_value_free(gpointer data)
264 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
269 static void __rstns_tree_key_free(gpointer data)
271 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
275 FREE(key->subscriber_id);
280 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
283 stc_process_key_s *proc_key = (stc_process_key_s *)key;
284 stc_process_value_s *proc_value = (stc_process_value_s *)value;
286 STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
287 proc_key->pid, proc_value->ground);
291 static void __processes_tree_printall(GTree *processes)
293 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
296 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
299 stc_app_key_s *app_key = (stc_app_key_s *)key;
300 stc_app_value_s *app_value = (stc_app_value_s *)value;
302 STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
303 "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
304 " counter [ in (%lld), out (%lld)]",
305 app_key->pkg_id, app_key->app_id,
306 app_value->type, app_value->classid,
307 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
309 __processes_tree_printall(app_value->processes);
313 static void __apps_tree_printall(void)
315 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
319 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
322 remove_pid_context_s *context = (remove_pid_context_s *)data;
323 stc_app_value_s *app_value = (stc_app_value_s *)value;
325 if (!g_tree_remove(app_value->processes, context->proc_key)) {
327 STC_LOGD("key not found"); //LCOV_EXCL_LINE
331 context->entry_removed = TRUE;
332 context->app_key = (stc_app_key_s *)key;
337 static stc_app_value_s * __application_lookup(GTree *apps,
338 const stc_app_key_s *key)
340 stc_app_value_s *lookup;
342 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
344 lookup = g_tree_lookup(apps, key);
349 static stc_process_value_s * __process_lookup(GTree *processes,
350 const stc_process_key_s *key)
352 stc_process_value_s *lookup;
354 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
356 lookup = g_tree_lookup(processes, key);
362 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
365 guint *pid_count = (guint *)data;
371 static gboolean __add_application_monitor(gpointer key, gpointer value,
374 stc_app_value_s *app_value = (stc_app_value_s *)value;
375 default_connection_s *connection = (default_connection_s *)data;
376 stc_s *stc = stc_get_manager();
378 if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
379 app_value->classid == STC_TOTAL_WIFI_CLASSID ||
380 app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
383 if (stc && connection && connection->ifname) {
384 struct nfacct_rule counter;
387 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
388 if (stc->carg == NULL) //LCOV_EXCL_LINE
389 return FALSE; //LCOV_EXCL_LINE
391 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
394 memset(&counter, 0, sizeof(struct nfacct_rule));
396 counter.carg = stc->carg;
397 counter.classid = app_value->classid;
398 counter.intend = NFACCT_COUNTER;
399 counter.iftype = connection->type;
400 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
402 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
403 __add_iptables_in(&counter);
404 __add_iptables_out(&counter);
405 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
406 __add_ip6tables_in(&counter);
407 __add_ip6tables_out(&counter);
409 __add_iptables_in(&counter);
410 __add_iptables_out(&counter);
411 __add_ip6tables_in(&counter);
412 __add_ip6tables_out(&counter);
419 static gboolean __remove_application_monitor(gpointer key, gpointer value,
422 stc_app_value_s *app_value = (stc_app_value_s *)value;
423 default_connection_s *connection = (default_connection_s *)data;
424 stc_s *stc = stc_get_manager();
426 if (stc && connection && connection->ifname) {
427 struct nfacct_rule counter;
430 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
431 if (stc->carg == NULL) //LCOV_EXCL_LINE
432 return FALSE; //LCOV_EXCL_LINE
434 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
437 memset(&counter, 0, sizeof(struct nfacct_rule));
439 counter.carg = stc->carg;
440 counter.classid = app_value->classid;
441 counter.intend = NFACCT_COUNTER;
442 counter.iftype = connection->type;
443 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
445 __del_iptables_in(&counter);
446 __del_iptables_out(&counter);
447 __del_ip6tables_in(&counter);
448 __del_ip6tables_out(&counter);
454 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
456 STC_LOGI("rstn info => rstn_id [%llu], "
457 "app_id [%s], classid [%lu], ifname [%s], "
458 "iftype [%d], rst_state [%d], "
459 "limit [ (%lld) bytes], "
460 "warn_limit [ (%lld) bytes], "
461 "counter [ (%lld) bytes], "
462 "roaming [%d], subscriber_id [%s]",
463 rstn_value->restriction_id,
464 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
465 rstn_key->iftype, rstn_value->rst_state,
466 rstn_value->data_limit,
467 rstn_value->data_warn_limit,
468 rstn_value->data_counter,
469 rstn_key->roaming, rstn_key->subscriber_id);
472 static void __process_restriction(enum traffic_restriction_type rst_type,
473 stc_rstn_key_s *rstn_key,
474 stc_rstn_value_s *rstn_value, void *data)
476 int64_t effective_data_limit, effective_data_warn_limit;
477 default_connection_s *old_connection = (default_connection_s *)data;
478 default_connection_s *connection = NULL;
480 if (old_connection != NULL)
481 connection = old_connection;
483 connection = stc_get_default_connection();
485 /* no default ifname */
486 if (connection->ifname == NULL)
489 /* rstn not applicable for this interface */
490 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
491 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
494 /* classid is invalid */
495 if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
498 effective_data_limit = rstn_value->data_limit;
499 effective_data_warn_limit = rstn_value->data_warn_limit;
501 if (rst_type == RST_SET) {
502 /* TODO: Change this to runtime memory */
503 table_counters_info info;
505 memset(&info, 0, sizeof(table_counters_info));
506 table_counters_get(rstn_value->restriction_id, &info);
508 effective_data_limit -= info.data_counter;
509 effective_data_warn_limit -= info.data_counter;
511 if (effective_data_limit < 0) {
512 effective_data_limit = 0; //LCOV_EXCL_LINE
513 rstn_value->data_limit_reached = TRUE; //LCOV_EXCL_LINE
516 if (effective_data_warn_limit < 0)
517 effective_data_warn_limit = 0; //LCOV_EXCL_LINE
519 STC_LOGD("datausage [%lld] bytes", info.data_counter);
522 STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
523 "effective_data_warn_limit [%lld] bytes",
524 rstn_value->restriction_id, effective_data_limit,
525 effective_data_warn_limit);
529 if (effective_data_limit <= 0) {
530 char *default_ifname = stc_default_connection_get_ifname();
531 struct nfacct_rule counter;
532 stc_s *stc = stc_get_manager();
534 g_free(default_ifname); //LCOV_EXCL_LINE
535 return; //LCOV_EXCL_LINE
539 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
540 if (stc->carg == NULL) { //LCOV_EXCL_LINE
541 g_free(default_ifname); //LCOV_EXCL_LINE
542 return; //LCOV_EXCL_LINE
545 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
548 counter.carg = stc->carg;
549 counter.classid = rstn_value->classid;
550 counter.intend = NFACCT_BLOCK;
551 counter.iftype = rstn_key->iftype;
552 g_strlcpy(counter.ifname, default_ifname,
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); //LCOV_EXCL_LINE
578 return; //LCOV_EXCL_LINE
582 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
583 if (stc->carg == NULL) { //LCOV_EXCL_LINE
584 g_free(default_ifname); //LCOV_EXCL_LINE
585 return; //LCOV_EXCL_LINE
588 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
591 counter.carg = stc->carg;
592 counter.classid = rstn_value->classid;
593 counter.intend = NFACCT_BLOCK;
594 counter.iftype = rstn_key->iftype;
595 g_strlcpy(counter.ifname, default_ifname,
597 g_free(default_ifname);
600 __del_iptables_in(&counter);
601 __del_iptables_out(&counter);
604 __del_ip6tables_in(&counter);
605 __del_ip6tables_out(&counter);
607 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
608 rstn_value->data_limit_reached = FALSE;
617 static gboolean __remove_rstns_foreach_application(gpointer key,
621 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
622 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
623 gchar *app_id = (gchar *)data;
625 /* rstn rule is not for applications */
626 if (rstn_key->app_id == NULL)
629 /* rstn rule is not for this application */
630 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
633 /* rstn rule is already removed */
634 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
637 /* remove restriction from system */
638 __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
640 __print_rstn(rstn_key, rstn_value);
646 static void __remove_rstns_for_application(gchar *app_id)
648 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
652 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
654 stc_error_e ret = STC_ERROR_NONE;
656 stc_app_value_s *lookup;
658 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
660 lookup = __application_lookup(g_system->apps, app_key);
662 STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
663 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
666 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
670 /* remove nfacct rule for this classid */
671 __remove_application_monitor((gpointer) app_key, lookup,
672 stc_get_default_connection());
673 __remove_rstns_for_application(app_key->app_id);
676 if (!g_tree_remove(g_system->apps, app_key)) {
677 ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
678 STC_LOGE("key not found"); //LCOV_EXCL_LINE
684 static stc_error_e __close_contr_sock(stc_system_s *system)
686 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
688 /* close netlink socket for updating kernel counters */
689 if (system->contr_sock != -1) {
690 close(system->contr_sock);
691 system->contr_sock = -1;
694 if (system->contr_gsource_id != 0) {
695 g_source_remove(system->contr_gsource_id);
696 system->contr_gsource_id = 0;
699 return STC_ERROR_NONE;
702 static gboolean __process_contr_reply(GIOChannel *source,
703 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_manager_plugin_send_warn_message("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_manager_plugin_send_restriction_message("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; //LCOV_EXCL_LINE
858 goto try_next_callback; //LCOV_EXCL_LINE
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);
875 static gboolean __update_app_statistics(gpointer key, gpointer value,
878 stc_app_key_s *app_key = (stc_app_key_s *)key;
879 stc_app_value_s *app_value = (stc_app_value_s *)value;
880 time_t *touch_time = (time_t *)data;
881 stc_db_classid_iftype_key stat_key;
882 stc_db_app_stats stat;
883 default_connection_s *default_connection = stc_get_default_connection();
885 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
886 memset(&stat, 0 , sizeof(stc_db_app_stats));
888 stat_key.classid = app_value->classid;
889 stat_key.iftype = default_connection->type;
891 if (STC_IFACE_DATACALL == stat_key.iftype)
892 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
894 stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
896 g_strlcpy(stat_key.ifname, default_connection->ifname,
899 stat.app_id = g_strdup(app_key->app_id);
900 stat.snd_count = app_value->counter.out_bytes;
901 stat.rcv_count = app_value->counter.in_bytes;
902 stat.is_roaming = default_connection->roaming;
904 if (strstr(stat.app_id, "_BACKGROUND")) {
905 stat.ground = STC_APP_STATE_BACKGROUND;
907 if (strstr(stat.app_id, "TOTAL_"))
908 stat.ground = STC_APP_STATE_UNKNOWN;
910 stat.ground = STC_APP_STATE_FOREGROUND;
913 table_statistics_insert(&stat_key, &stat, *touch_time);
915 app_value->counter.out_bytes = 0;
916 app_value->counter.in_bytes = 0;
919 FREE(stat_key.subscriber_id);
924 static gboolean __flush_apps_stats_to_database(gpointer user_data)
926 time_t current_time = time(0);
928 if (g_system->apps_tree_updated == FALSE)
929 return G_SOURCE_REMOVE;
931 g_system->apps_tree_updated = FALSE;
934 g_tree_foreach(g_system->apps,
935 __update_app_statistics,
938 STC_LOGI("Flushed app stats to database");
939 return G_SOURCE_REMOVE;
943 static gboolean __update_counter_statistics(gpointer key, gpointer value,
946 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
947 table_counters_info info = {
948 .restriction_id = rstn_value->restriction_id,
949 .data_counter = rstn_value->data_counter
952 table_counters_update_counters(&info);
957 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
959 time_t current_time = time(0);
961 if (g_system->rstns_tree_updated == FALSE)
962 return G_SOURCE_REMOVE;
964 g_system->rstns_tree_updated = FALSE;
967 g_tree_foreach(g_system->rstns,
968 __update_counter_statistics,
971 STC_LOGI("Flushed rstns counters to database");
972 return G_SOURCE_REMOVE;
976 static void __app_counter_update(stc_app_key_s *app_key,
977 stc_app_value_s *app_value,
978 classid_bytes_context_s *context)
980 switch (context->counter->iotype) {
981 case NFACCT_COUNTER_IN:
982 app_value->data_usage.in_bytes += context->bytes;
983 app_value->counter.in_bytes = context->bytes;
984 g_system->apps_tree_updated = TRUE;
987 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
989 case NFACCT_COUNTER_OUT:
990 app_value->data_usage.out_bytes += context->bytes;
991 app_value->counter.out_bytes = context->bytes;
992 g_system->apps_tree_updated = TRUE;
995 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
998 STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
1002 static void __interface_counter_update(stc_app_key_s *app_key,
1003 stc_app_value_s *app_value,
1004 classid_bytes_context_s *context)
1006 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1007 context->counter->iftype == STC_IFACE_DATACALL) ||
1008 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1009 context->counter->iftype == STC_IFACE_WIFI) ||
1010 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1011 context->counter->iftype == STC_IFACE_BLUETOOTH))
1012 __app_counter_update(app_key, app_value, context);
1016 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1020 stc_app_key_s *app_key = (stc_app_key_s *)key;
1021 stc_app_value_s *app_value = (stc_app_value_s *)value;
1022 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1024 if (context->counter->intend != NFACCT_COUNTER)
1025 goto try_next_callback;
1027 __interface_counter_update(app_key, app_value, context);
1029 if (app_value->classid != context->counter->classid)
1030 goto try_next_callback;
1032 __app_counter_update(app_key, app_value, context);
1038 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1039 struct counter_arg *carg)
1041 struct nfacct_rule counter = {
1048 classid_bytes_context_s context = {
1049 .counter = &counter,
1051 .data_limit_reached = FALSE,
1055 STC_LOGD("cnt_name %s", cnt_name); //LCOV_EXCL_LINE
1057 if (!recreate_counter_by_name(cnt_name, &counter)) {
1058 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1059 return; //LCOV_EXCL_LINE
1062 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1063 context.counter->classid, context.counter->iftype,
1064 context.counter->iotype, context.counter->intend,
1065 context.counter->ifname, context.bytes);
1067 if (g_system->rstns)
1068 g_tree_foreach(g_system->rstns,
1069 __rstn_counter_update_foreach_classid,
1073 g_tree_foreach(g_system->apps,
1074 __apps_counter_update_foreach_classid,
1078 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1081 struct counter_arg *carg = user_data;
1082 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1083 if (carg->initiate) {
1085 * TODO: this will be used when daemon starts to update existing
1086 * counter data if present.
1088 populate_counters(cnt_name, carg);
1092 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1093 int bytes = be64toh(*bytes_p);
1095 ++carg->serialized_counters;
1096 __fill_nfacct_result(cnt_name, bytes, carg);
1103 static int __post_fill_counters(void *user_data)
1105 struct counter_arg *carg = user_data;
1113 static void __process_network_counter(struct genl *ans,
1114 struct counter_arg *carg)
1116 struct netlink_serialization_params ser_params = {
1119 .eval_attr = __fill_counters,
1120 .post_eval_attr = __post_fill_counters,
1123 netlink_serialization_command *netlink =
1124 netlink_create_command(&ser_params);
1126 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
1127 return; //LCOV_EXCL_LINE
1130 netlink->deserialize_answer(&(netlink->params));
1133 static gboolean __process_contr_reply(GIOChannel *source,
1134 GIOCondition condition,
1137 int sock = g_io_channel_unix_get_fd(source);
1140 stc_s *stc = stc_get_manager();
1143 void __gcov_flush(void);
1147 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1148 (condition & G_IO_NVAL)) {
1149 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1151 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
1152 "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
1153 (condition & G_IO_ERR), (condition & G_IO_HUP),
1154 (condition & G_IO_NVAL));
1155 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
1156 return FALSE; //LCOV_EXCL_LINE
1159 ans = MALLOC0(struct genl, 1);
1161 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
1162 return TRUE; //LCOV_EXCL_LINE
1166 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
1167 goto out; //LCOV_EXCL_LINE
1170 ret = read_netlink(sock, ans, sizeof(struct genl));
1171 /* STC_LOGD("Counter data received ret [%d]", ret); */
1175 stc->carg->ans_len = ret;
1176 __process_network_counter(ans, stc->carg);
1178 g_idle_add(__flush_apps_stats_to_database, NULL);
1179 g_idle_add(__flush_rstns_counter_to_database, NULL);
1186 static gboolean __update_contr_cb(void *user_data)
1188 /* Here we just sent command, answer we receive in another callback */
1189 stc_s *stc = stc_get_manager();
1190 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1192 stc->carg = MALLOC0(counter_arg_s, 1);
1193 if (stc->carg == NULL)
1194 return TRUE; /* we need to continue the timer */
1196 stc->carg->sock = stc_monitor_get_counter_socket();
1200 void __gcov_flush(void);
1204 /* STC_LOGD("Get all counters"); */
1205 nfacct_send_get_all(stc->carg);
1207 /* we need to continue the timer */
1212 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1215 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1216 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1218 __print_rstn(rstn_key, rstn_value);
1222 static void __rstn_tree_printall(void)
1224 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1228 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1229 const stc_rstn_key_s *key)
1231 stc_rstn_value_s *lookup;
1233 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1235 lookup = g_tree_lookup(rstns_tree, key);
1240 static gboolean __remove_restriction(gpointer key, gpointer value,
1243 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1244 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1246 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1247 __print_rstn(rstn_key, rstn_value);
1251 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1254 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1255 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1257 /* rstn rule is activated */
1258 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1261 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1262 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1264 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1266 __print_rstn(rstn_key, rstn_value);
1272 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1274 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1275 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1277 /* rstn rule is activated */
1278 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1281 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1282 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1284 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1290 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1292 stc_rstn_value_s *lookup_value;
1294 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1296 lookup_value = __rstn_lookup(g_system->rstns, key);
1297 if (!lookup_value) {
1298 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1299 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1302 __remove_restriction(key, lookup_value, NULL);
1304 /* remove counter also */
1305 table_counters_delete(lookup_value->restriction_id);
1307 if (!g_tree_remove(g_system->rstns, key)) {
1308 STC_LOGD("key not found"); //LCOV_EXCL_LINE
1309 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1312 return STC_ERROR_NONE;
1315 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1316 stc_rstn_value_s *value, gboolean debug)
1318 stc_rstn_value_s *rstn_value;
1320 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1322 rstn_value = __rstn_lookup(g_system->rstns, key);
1324 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1326 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
1327 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1330 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1332 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
1333 FREE(rstn_key); //LCOV_EXCL_LINE
1334 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1337 rstn_key->app_id = g_strdup(key->app_id);
1338 rstn_key->ifname = g_strdup(key->ifname);
1339 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1340 rstn_key->iftype = key->iftype;
1341 rstn_key->roaming = key->roaming;
1343 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1346 rstn_value->restriction_id = value->restriction_id;
1347 rstn_value->rst_state = value->rst_state;
1348 rstn_value->classid = value->classid;
1349 rstn_value->data_limit = value->data_limit;
1350 rstn_value->data_warn_limit = value->data_warn_limit;
1351 rstn_value->data_counter = 0;
1352 rstn_value->warn_limit_crossed_notified = FALSE;
1353 rstn_value->rstn_limit_crossed_notified = FALSE;
1356 __add_restriction_debug(key, rstn_value, NULL);
1358 __add_restriction(key, rstn_value, NULL);
1360 return STC_ERROR_NONE;
1364 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1367 stc_cb_ret_e ret = STC_CONTINUE;
1370 stc_rstn_value_s value;
1372 memset(&key, 0, sizeof(stc_rstn_key_s));
1373 memset(&value, 0, sizeof(stc_rstn_value_s));
1375 key.app_id = g_strdup(info->app_id);
1376 key.ifname = g_strdup(info->ifname);
1377 key.subscriber_id = g_strdup(info->subscriber_id);
1378 key.iftype = info->iftype;
1379 key.roaming = info->roaming;
1381 value.rst_state = info->rst_state;
1382 value.restriction_id = info->restriction_id;
1384 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1385 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1387 value.classid = STC_UNKNOWN_CLASSID;
1389 value.data_limit = info->data_limit;
1390 value.data_warn_limit = info->data_warn_limit;
1392 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1397 FREE(key.subscriber_id);
1401 static void __fill_restritions_list(void)
1403 table_restrictions_foreach(__insert_restriction_cb, NULL);
1406 __rstn_tree_printall();
1409 static gboolean __add_rstn_foreach_application(gpointer key,
1413 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1414 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1415 gchar *app_id = (gchar *)data;
1417 /* rstn rule is not for applications */
1418 if (rstn_key->app_id == NULL)
1421 /* rstn rule is not for this application */
1422 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1425 /* rstn rule is already applied */
1426 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1429 /* add restriction to system */
1430 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1431 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
1433 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
1435 __print_rstn(rstn_key, rstn_value);
1441 static void __add_rstns_for_application(gchar *app_id)
1443 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1447 static void __add_application_by_interface(const char *app_id)
1449 stc_app_key_s app_key;
1450 stc_app_value_s app_value;
1453 return; //LCOV_EXCL_LINE
1455 memset(&app_key, 0, sizeof(stc_app_key_s));
1456 memset(&app_value, 0, sizeof(stc_app_value_s));
1458 app_key.pkg_id = g_strdup(app_id);
1459 app_key.app_id = g_strdup(app_id);
1461 app_value.type = STC_APP_TYPE_NONE;
1462 app_value.processes = NULL;
1463 app_value.counter.in_bytes = 0;
1464 app_value.counter.out_bytes = 0;
1466 stc_monitor_application_add(app_key, app_value);
1468 FREE(app_key.pkg_id);
1469 FREE(app_key.app_id);
1472 static int __vconf_get_int(const char *key, int *value)
1476 ret = vconf_get_int(key, value);
1477 if (ret != VCONF_OK) {
1478 STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
1479 return -1; //LCOV_EXCL_LINE
1486 static int __vconf_set_int(const char *key, int value)
1490 ret = vconf_set_int(key, value);
1491 if (ret != VCONF_OK) {
1492 STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
1493 return -1; //LCOV_EXCL_LINE
1499 static guint __get_background_state(void)
1501 return g_system->background_state;;
1504 static void __set_background_state(guint state)
1506 g_system->background_state = state;
1509 static gboolean __processes_tree_foreach_background(gpointer key,
1513 stc_process_key_s *proc_key = (stc_process_key_s *)key;
1514 stc_app_key_s *app_key = (stc_app_key_s *)data;
1516 if (g_system->background_state)
1517 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1519 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1524 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1527 stc_app_key_s *app_key = (stc_app_key_s *)key;
1528 stc_app_value_s *app_value = (stc_app_value_s *)value;
1530 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1531 g_tree_foreach(app_value->processes,
1532 __processes_tree_foreach_background, app_key);
1537 static stc_error_e __process_update_background(void)
1539 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1541 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1543 return STC_ERROR_NONE;
1547 static void __fill_exceptions_list(void)
1549 stc_manager_plugin_fill_exception_list();
1552 stc_error_e stc_monitor_init(void)
1554 stc_system_s *system = MALLOC0(stc_system_s, 1);
1555 GIOChannel *gio = NULL;
1557 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1559 /* initializing current classid */
1560 init_current_classid();
1562 /* initializing cgroups */
1565 /* creating monitored application tree */
1566 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1567 __apps_tree_key_free,
1568 __apps_tree_value_free);
1570 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1571 __rstns_tree_key_free,
1572 __rstns_tree_value_free);
1574 /* create netlink socket for updating kernel counters */
1575 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1576 if (system->contr_sock < 0) {
1577 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
1578 FREE(system); //LCOV_EXCL_LINE
1579 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1582 gio = g_io_channel_unix_new(system->contr_sock);
1583 system->contr_gsource_id =
1584 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1585 (GIOFunc) __process_contr_reply,
1587 g_io_channel_unref(gio);
1591 __add_application_by_interface(STC_TOTAL_DATACALL);
1592 __add_application_by_interface(STC_TOTAL_WIFI);
1593 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1594 __add_application_by_interface(STC_TOTAL_IPV4);
1595 __add_application_by_interface(STC_TOTAL_IPV6);
1597 /* creating restriction rules tree */
1598 __update_contr_cb(NULL);
1600 /* registering periodic kernel counters update callback */
1601 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1604 if (g_system->contr_timer_id == 0) {
1605 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
1606 __close_contr_sock(g_system); //LCOV_EXCL_LINE
1607 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1610 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1611 (int *)&g_system->background_state);
1613 __fill_exceptions_list();
1614 __fill_restritions_list();
1616 return STC_ERROR_NONE;
1619 stc_error_e stc_monitor_deinit(void)
1621 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1623 /* close netlink socket for updating kernel counters */
1624 __close_contr_sock(g_system);
1626 /* remove kernel counters update timer */
1627 if (g_system->contr_timer_id > 0) {
1628 g_source_remove(g_system->contr_timer_id);
1629 g_system->contr_timer_id = 0;
1632 /* destroy monitored application tree */
1633 g_tree_destroy(g_system->apps);
1634 g_system->apps = NULL;
1636 /* destroy restriction rules tree */
1637 g_tree_destroy(g_system->rstns);
1638 g_system->rstns = NULL;
1642 return STC_ERROR_NONE;
1645 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1646 const stc_app_value_s app_value)
1648 stc_error_e ret = STC_ERROR_NONE;
1650 stc_app_value_s *value;
1651 stc_app_value_s *lookup;
1653 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1655 lookup = __application_lookup(g_system->apps, &app_key);
1657 STC_LOGD("app_key already present"); //LCOV_EXCL_LINE
1658 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1661 key = MALLOC0(stc_app_key_s, 1);
1663 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1664 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1667 value = MALLOC0(stc_app_value_s, 1);
1669 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1670 FREE(key); //LCOV_EXCL_LINE
1671 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1674 key->app_id = g_strdup(app_key.app_id);
1675 key->pkg_id = g_strdup(app_key.pkg_id);
1677 value->type = app_value.type;
1678 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1679 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1681 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1682 __processes_tree_key_free,
1683 __processes_tree_value_free);
1685 /* create cgroup and update classid */
1686 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1688 g_tree_insert(g_system->apps, key, value);
1690 /* add nfacct rule for this classid */
1691 __add_application_monitor(key, value, stc_get_default_connection());
1692 __add_rstns_for_application(app_key.app_id);
1697 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1698 const stc_process_key_s proc_key,
1699 const stc_process_value_s proc_value)
1701 stc_error_e ret = STC_ERROR_NONE;
1702 stc_app_value_s *app_lookup;
1703 stc_process_key_s *key;
1704 stc_process_value_s *value;
1705 stc_process_value_s *proc_lookup;
1707 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1709 app_lookup = __application_lookup(g_system->apps, &app_key);
1711 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
1712 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1715 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1717 STC_LOGD("proc_key already present"); //LCOV_EXCL_LINE
1718 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1721 key = MALLOC0(stc_process_key_s, 1);
1723 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1724 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1727 value = MALLOC0(stc_process_value_s, 1);
1729 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1730 FREE(key); //LCOV_EXCL_LINE
1731 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1734 key->pid = proc_key.pid;
1736 value->ground = proc_value.ground;
1738 g_tree_insert(app_lookup->processes, key, value);
1740 /* add pid to application cgroup */
1741 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1744 __apps_tree_printall(); //LCOV_EXCL_LINE
1749 stc_error_e stc_monitor_process_remove(pid_t pid)
1751 stc_error_e ret = STC_ERROR_NONE;
1752 stc_process_key_s proc_key = {
1756 remove_pid_context_s context = {
1758 .proc_key = &proc_key,
1759 .entry_removed = FALSE,
1762 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1764 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1767 if (context.entry_removed)
1768 __application_remove_if_empty(context.app_key);
1771 __apps_tree_printall(); //LCOV_EXCL_LINE
1777 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1778 const stc_process_key_s proc_key,
1779 stc_app_state_e ground)
1781 stc_error_e ret = STC_ERROR_NONE;
1782 stc_app_value_s *app_lookup;
1783 stc_process_value_s *proc_lookup;
1785 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1787 app_lookup = __application_lookup(g_system->apps, &app_key);
1789 STC_LOGD("app_key not found");
1790 return STC_ERROR_FAIL;
1793 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1795 STC_LOGD("proc_key not found");
1796 return STC_ERROR_FAIL;
1799 if (proc_lookup->ground != ground)
1800 proc_lookup->ground = ground;
1802 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1803 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1805 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1811 void stc_monitor_update_rstn_by_default_connection(void *data)
1813 static default_connection_s old_connection;
1814 default_connection_s *new_connection = (default_connection_s *)data;
1816 if (old_connection.path != NULL) {
1819 g_tree_foreach(g_system->apps,
1820 __remove_application_monitor,
1821 (gpointer)&old_connection);
1823 if (g_system->rstns)
1824 g_tree_foreach(g_system->rstns,
1825 __remove_restriction,
1826 (gpointer)&old_connection);
1828 iptables_flush_chains();
1832 FREE(old_connection.path);
1833 FREE(old_connection.ifname);
1834 old_connection.type = 0;
1835 old_connection.roaming = 0;
1837 if (new_connection != NULL && new_connection->path != NULL) {
1839 g_tree_foreach(g_system->apps,
1840 __add_application_monitor,
1841 (gpointer)new_connection);
1843 if (g_system->rstns)
1844 g_tree_foreach(g_system->rstns, __add_restriction,
1847 old_connection.path = g_strdup(new_connection->path);
1848 old_connection.ifname = g_strdup(new_connection->ifname);
1849 old_connection.type = new_connection->type;
1850 old_connection.roaming = new_connection->roaming;
1854 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1859 stc_rstn_value_s value;
1861 memset(&key, 0, sizeof(stc_rstn_key_s));
1862 memset(&value, 0, sizeof(stc_rstn_value_s));
1864 key.app_id = g_strdup(info->app_id);
1865 key.ifname = g_strdup(info->ifname);
1866 key.subscriber_id = g_strdup(info->subscriber_id);
1867 key.iftype = info->iftype;
1868 key.roaming = info->roaming;
1870 value.rst_state = info->rst_state;
1871 value.restriction_id = info->restriction_id;
1873 if (value.rst_state > STC_RESTRICTION_UNKNOWN &&
1874 value.rst_state < STC_RESTRICTION_LAST_ELEM &&
1876 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1878 value.classid = STC_UNKNOWN_CLASSID;
1880 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1881 __set_background_state(TRUE); //LCOV_EXCL_LINE
1882 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1883 __process_update_background(); //LCOV_EXCL_LINE
1886 value.data_limit = info->data_limit;
1887 value.data_warn_limit = info->data_warn_limit;
1889 ret = __rstn_tree_add(&key, &value, TRUE);
1893 FREE(key.subscriber_id);
1897 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1901 stc_rstn_key_s key = {
1902 .app_id = g_strdup(info->app_id),
1903 .ifname = g_strdup(info->ifname),
1904 .subscriber_id = g_strdup(info->subscriber_id),
1905 .iftype = info->iftype,
1906 .roaming = info->roaming,
1909 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
1910 __set_background_state(FALSE); //LCOV_EXCL_LINE
1911 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1912 __process_update_background(); //LCOV_EXCL_LINE
1915 ret = __rstn_tree_remove(&key);
1919 FREE(key.subscriber_id);
1923 stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
1925 return stc_manager_plugin_check_exception_by_cmdline(cmdline);
1928 int stc_monitor_get_counter_socket(void)
1930 return g_system->contr_sock;