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);
281 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
284 stc_process_key_s *proc_key = (stc_process_key_s *)key;
285 stc_process_value_s *proc_value = (stc_process_value_s *)value;
287 STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
288 proc_key->pid, proc_value->ground);
292 static void __processes_tree_printall(GTree *processes)
294 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
297 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
300 stc_app_key_s *app_key = (stc_app_key_s *)key;
301 stc_app_value_s *app_value = (stc_app_value_s *)value;
303 STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
304 "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
305 " counter [ in (%lld), out (%lld)]",
306 app_key->pkg_id, app_key->app_id,
307 app_value->type, app_value->classid,
308 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
310 __processes_tree_printall(app_value->processes);
314 static void __apps_tree_printall(void)
316 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
320 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
323 remove_pid_context_s *context = (remove_pid_context_s *)data;
324 stc_app_value_s *app_value = (stc_app_value_s *)value;
326 if (!g_tree_remove(app_value->processes, context->proc_key)) {
328 STC_LOGD("key not found"); //LCOV_EXCL_LINE
332 context->entry_removed = TRUE;
333 context->app_key = (stc_app_key_s *)key;
338 static stc_app_value_s * __application_lookup(GTree *apps,
339 const stc_app_key_s *key)
341 stc_app_value_s *lookup;
343 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
345 lookup = g_tree_lookup(apps, key);
350 static stc_process_value_s * __process_lookup(GTree *processes,
351 const stc_process_key_s *key)
353 stc_process_value_s *lookup;
355 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
357 lookup = g_tree_lookup(processes, key);
363 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
366 guint *pid_count = (guint *)data;
372 static gboolean __add_application_monitor(gpointer key, gpointer value,
375 stc_app_value_s *app_value = (stc_app_value_s *)value;
376 default_connection_s *connection = (default_connection_s *)data;
377 stc_s *stc = stc_get_manager();
379 if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
380 app_value->classid == STC_TOTAL_WIFI_CLASSID ||
381 app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
384 if (stc && connection && connection->ifname) {
385 struct nfacct_rule counter;
388 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
389 if (stc->carg == NULL) //LCOV_EXCL_LINE
390 return FALSE; //LCOV_EXCL_LINE
392 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
395 memset(&counter, 0, sizeof(struct nfacct_rule));
397 counter.carg = stc->carg;
398 counter.classid = app_value->classid;
399 counter.intend = NFACCT_COUNTER;
400 counter.iftype = connection->type;
401 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
403 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
404 __add_iptables_in(&counter);
405 __add_iptables_out(&counter);
406 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
407 __add_ip6tables_in(&counter);
408 __add_ip6tables_out(&counter);
410 __add_iptables_in(&counter);
411 __add_iptables_out(&counter);
412 __add_ip6tables_in(&counter);
413 __add_ip6tables_out(&counter);
420 static gboolean __remove_application_monitor(gpointer key, gpointer value,
423 stc_app_value_s *app_value = (stc_app_value_s *)value;
424 default_connection_s *connection = (default_connection_s *)data;
425 stc_s *stc = stc_get_manager();
427 if (stc && connection && connection->ifname) {
428 struct nfacct_rule counter;
431 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
432 if (stc->carg == NULL) //LCOV_EXCL_LINE
433 return FALSE; //LCOV_EXCL_LINE
435 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
438 memset(&counter, 0, sizeof(struct nfacct_rule));
440 counter.carg = stc->carg;
441 counter.classid = app_value->classid;
442 counter.intend = NFACCT_COUNTER;
443 counter.iftype = connection->type;
444 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
446 __del_iptables_in(&counter);
447 __del_iptables_out(&counter);
448 __del_ip6tables_in(&counter);
449 __del_ip6tables_out(&counter);
455 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
457 STC_LOGI("rstn info => rstn_id [%llu], "
458 "app_id [%s], classid [%lu], ifname [%s], "
459 "iftype [%d], rst_state [%d], "
460 "limit [ (%lld) bytes], "
461 "warn_limit [ (%lld) bytes], "
462 "counter [ (%lld) bytes], "
463 "roaming [%d], subscriber_id [%s]",
464 rstn_value->restriction_id,
465 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
466 rstn_key->iftype, rstn_value->rst_state,
467 rstn_value->data_limit,
468 rstn_value->data_warn_limit,
469 rstn_value->data_counter,
470 rstn_key->roaming, rstn_key->subscriber_id);
473 static void __process_restriction(enum traffic_restriction_type rst_type,
474 stc_rstn_key_s *rstn_key,
475 stc_rstn_value_s *rstn_value, void *data)
477 int64_t effective_data_limit, effective_data_warn_limit;
478 default_connection_s *old_connection = (default_connection_s *)data;
479 default_connection_s *connection = NULL;
481 if (old_connection != NULL)
482 connection = old_connection;
484 connection = stc_get_default_connection();
486 /* no default ifname */
487 if (connection->ifname == NULL)
490 /* rstn not applicable for this interface */
491 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
492 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
495 /* classid is invalid */
496 if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
499 effective_data_limit = rstn_value->data_limit;
500 effective_data_warn_limit = rstn_value->data_warn_limit;
502 if (rst_type == RST_SET) {
503 /* TODO: Change this to runtime memory */
504 table_counters_info info;
506 memset(&info, 0, sizeof(table_counters_info));
507 table_counters_get(rstn_value->restriction_id, &info);
509 effective_data_limit -= info.data_counter;
510 effective_data_warn_limit -= info.data_counter;
512 if (effective_data_limit < 0) {
513 effective_data_limit = 0; //LCOV_EXCL_LINE
514 rstn_value->data_limit_reached = TRUE; //LCOV_EXCL_LINE
517 if (effective_data_warn_limit < 0)
518 effective_data_warn_limit = 0; //LCOV_EXCL_LINE
520 STC_LOGD("datausage [%lld] bytes", info.data_counter);
523 STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
524 "effective_data_warn_limit [%lld] bytes",
525 rstn_value->restriction_id, effective_data_limit,
526 effective_data_warn_limit);
530 if (effective_data_limit <= 0) {
531 char *default_ifname = stc_default_connection_get_ifname();
532 struct nfacct_rule counter;
533 stc_s *stc = stc_get_manager();
535 g_free(default_ifname); //LCOV_EXCL_LINE
536 return; //LCOV_EXCL_LINE
540 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
541 if (stc->carg == NULL) { //LCOV_EXCL_LINE
542 g_free(default_ifname); //LCOV_EXCL_LINE
543 return; //LCOV_EXCL_LINE
546 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
549 counter.carg = stc->carg;
550 counter.classid = rstn_value->classid;
551 counter.intend = NFACCT_BLOCK;
552 counter.iftype = rstn_key->iftype;
553 g_strlcpy(counter.ifname, default_ifname,
556 generate_counter_name(&counter);
557 g_free(default_ifname);
560 __add_iptables_in(&counter);
561 __add_iptables_out(&counter);
564 __add_ip6tables_in(&counter);
565 __add_ip6tables_out(&counter);
568 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
569 rstn_value->data_limit_reached = FALSE;
576 char *default_ifname = stc_default_connection_get_ifname();
577 struct nfacct_rule counter;
578 stc_s *stc = stc_get_manager();
580 g_free(default_ifname); //LCOV_EXCL_LINE
581 return; //LCOV_EXCL_LINE
585 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
586 if (stc->carg == NULL) { //LCOV_EXCL_LINE
587 g_free(default_ifname); //LCOV_EXCL_LINE
588 return; //LCOV_EXCL_LINE
591 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
594 counter.carg = stc->carg;
595 counter.classid = rstn_value->classid;
596 counter.intend = NFACCT_BLOCK;
597 counter.iftype = rstn_key->iftype;
598 g_strlcpy(counter.ifname, default_ifname,
601 generate_counter_name(&counter);
602 g_free(default_ifname);
605 __del_iptables_in(&counter);
606 __del_iptables_out(&counter);
609 __del_ip6tables_in(&counter);
610 __del_ip6tables_out(&counter);
612 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
613 rstn_value->data_limit_reached = FALSE;
622 static gboolean __remove_rstns_foreach_application(gpointer key,
626 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
627 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
628 gchar *app_id = (gchar *)data;
630 /* rstn rule is not for applications */
631 if (rstn_key->app_id == NULL)
634 /* rstn rule is not for this application */
635 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
638 /* rstn rule is already removed */
639 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
642 /* remove restriction from system */
643 __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
645 __print_rstn(rstn_key, rstn_value);
651 static void __remove_rstns_for_application(gchar *app_id)
653 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
657 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
659 stc_error_e ret = STC_ERROR_NONE;
661 stc_app_value_s *lookup;
663 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
665 lookup = __application_lookup(g_system->apps, app_key);
667 STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
668 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
671 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
675 /* remove nfacct rule for this classid */
676 __remove_application_monitor((gpointer) app_key, lookup,
677 stc_get_default_connection());
678 __remove_rstns_for_application(app_key->app_id);
681 if (!g_tree_remove(g_system->apps, app_key)) {
682 ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
683 STC_LOGE("key not found"); //LCOV_EXCL_LINE
689 static stc_error_e __close_contr_sock(stc_system_s *system)
691 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
693 /* close netlink socket for updating kernel counters */
694 if (system->contr_sock != -1) {
695 close(system->contr_sock);
696 system->contr_sock = -1;
699 if (system->contr_gsource_id != 0) {
700 g_source_remove(system->contr_gsource_id);
701 system->contr_gsource_id = 0;
704 return STC_ERROR_NONE;
707 static gboolean __process_contr_reply(GIOChannel *source,
708 GIOCondition condition,
712 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
714 GIOChannel *gio = NULL;
715 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
717 /* close netlink socket for updating kernel counters */
718 if (system->contr_sock != -1) {
719 close(system->contr_sock);
720 system->contr_sock = -1;
723 if (system->contr_gsource_id != 0) {
724 g_source_remove(system->contr_gsource_id);
725 system->contr_gsource_id = 0;
728 /* create netlink socket for updating kernel counters */
729 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
730 if (system->contr_sock < 0) {
731 STC_LOGE("failed to open socket");
733 return STC_ERROR_FAIL;
736 gio = g_io_channel_unix_new(system->contr_sock);
737 system->contr_gsource_id =
738 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
739 (GIOFunc) __process_contr_reply,
741 g_io_channel_unref(gio);
743 return STC_ERROR_NONE;
746 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
747 stc_rstn_value_s *rstn_value,
748 classid_bytes_context_s *context)
750 switch (context->counter->iotype) {
751 case NFACCT_COUNTER_IN:
752 case NFACCT_COUNTER_OUT:
753 rstn_value->data_counter += context->bytes;
755 if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
756 rstn_value->warn_limit_crossed_notified == FALSE) {
759 char iftype[MAX_INT_LENGTH];
760 char byte[MAX_INT_LENGTH];
761 stc_s *stc = (stc_s *)stc_get_manager();
762 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
765 rv = stc_manager_dbus_emit_signal(stc->connection,
766 STC_DBUS_SERVICE_RESTRICTION_PATH,
767 STC_DBUS_INTERFACE_RESTRICTION,
768 "WarnThresholdCrossed",
769 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
771 rstn_value->warn_limit_crossed_notified = TRUE;
773 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
774 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
775 stc_send_warn_message_to_net_popup("warn threshold crossed",
781 if (rstn_value->data_counter >= rstn_value->data_limit &&
782 rstn_value->rstn_limit_crossed_notified == FALSE) {
785 char iftype[MAX_INT_LENGTH];
786 char byte[MAX_INT_LENGTH];
787 stc_s *stc = (stc_s *)stc_get_manager();
788 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
790 /* block immediately */
791 context->counter->intend = NFACCT_BLOCK;
792 __del_iptables_in(context->counter);
793 __del_iptables_out(context->counter);
794 __add_iptables_in(context->counter);
795 __add_iptables_out(context->counter);
797 __del_ip6tables_in(context->counter);
798 __del_ip6tables_out(context->counter);
799 __add_ip6tables_in(context->counter);
800 __add_ip6tables_out(context->counter);
801 context->counter->intend = NFACCT_COUNTER;
803 rstn_value->data_limit_reached = TRUE;
806 rv = stc_manager_dbus_emit_signal(stc->connection,
807 STC_DBUS_SERVICE_RESTRICTION_PATH,
808 STC_DBUS_INTERFACE_RESTRICTION,
809 "RestrictionThresholdCrossed",
810 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
812 rstn_value->rstn_limit_crossed_notified = TRUE;
814 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
815 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
816 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
817 "restriction_noti", rstn_key->app_id,
821 g_system->rstns_tree_updated = TRUE;
822 __print_rstn(rstn_key, rstn_value);
825 STC_LOGE("unknown iotype");
831 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
832 stc_rstn_value_s *rstn_value,
833 classid_bytes_context_s *context)
835 if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
836 context->counter->iftype == STC_IFACE_DATACALL) ||
837 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
838 context->counter->iftype == STC_IFACE_WIFI) ||
839 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
840 context->counter->iftype == STC_IFACE_BLUETOOTH)) {
841 context->counter->classid = rstn_value->classid;
842 return __rstn_counter_update(rstn_key, rstn_value, context);
848 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
853 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
854 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
855 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
858 if (context->counter->intend != NFACCT_COUNTER)
859 goto try_next_callback;
861 if (rstn_value->data_limit_reached == TRUE) {
862 context->data_limit_reached = TRUE; //LCOV_EXCL_LINE
863 goto try_next_callback; //LCOV_EXCL_LINE
866 classid = context->counter->classid;
867 rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
869 context->counter->classid = classid;
870 if (rstn_value->classid != context->counter->classid)
871 goto try_next_callback;
873 rv = __rstn_counter_update(rstn_key, rstn_value, context);
880 static gboolean __update_app_statistics(gpointer key, gpointer value,
883 stc_app_key_s *app_key = (stc_app_key_s *)key;
884 stc_app_value_s *app_value = (stc_app_value_s *)value;
885 time_t *touch_time = (time_t *)data;
886 stc_db_classid_iftype_key stat_key;
887 stc_db_app_stats stat;
888 default_connection_s *default_connection = stc_get_default_connection();
890 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
891 memset(&stat, 0 , sizeof(stc_db_app_stats));
893 stat_key.classid = app_value->classid;
894 stat_key.iftype = default_connection->type;
896 if (STC_IFACE_DATACALL == stat_key.iftype)
897 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
899 stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
901 g_strlcpy(stat_key.ifname, default_connection->ifname,
904 stat.app_id = g_strdup(app_key->app_id);
905 stat.snd_count = app_value->counter.out_bytes;
906 stat.rcv_count = app_value->counter.in_bytes;
907 stat.is_roaming = default_connection->roaming;
909 if (strstr(stat.app_id, "_BACKGROUND")) {
910 stat.ground = STC_APP_STATE_BACKGROUND;
912 if (strstr(stat.app_id, "TOTAL_"))
913 stat.ground = STC_APP_STATE_UNKNOWN;
915 stat.ground = STC_APP_STATE_FOREGROUND;
918 table_statistics_insert(&stat_key, &stat, *touch_time);
920 app_value->counter.out_bytes = 0;
921 app_value->counter.in_bytes = 0;
924 FREE(stat_key.subscriber_id);
929 static gboolean __flush_apps_stats_to_database(gpointer user_data)
931 time_t current_time = time(0);
933 if (g_system->apps_tree_updated == FALSE)
934 return G_SOURCE_REMOVE;
936 g_system->apps_tree_updated = FALSE;
939 g_tree_foreach(g_system->apps,
940 __update_app_statistics,
943 STC_LOGI("Flushed app stats to database");
944 return G_SOURCE_REMOVE;
948 static gboolean __update_counter_statistics(gpointer key, gpointer value,
951 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
952 table_counters_info info = {
953 .restriction_id = rstn_value->restriction_id,
954 .data_counter = rstn_value->data_counter
957 table_counters_update_counters(&info);
962 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
964 time_t current_time = time(0);
966 if (g_system->rstns_tree_updated == FALSE)
967 return G_SOURCE_REMOVE;
969 g_system->rstns_tree_updated = FALSE;
972 g_tree_foreach(g_system->rstns,
973 __update_counter_statistics,
976 STC_LOGI("Flushed rstns counters to database");
977 return G_SOURCE_REMOVE;
981 static void __app_counter_update(stc_app_key_s *app_key,
982 stc_app_value_s *app_value,
983 classid_bytes_context_s *context)
985 switch (context->counter->iotype) {
986 case NFACCT_COUNTER_IN:
987 app_value->data_usage.in_bytes += context->bytes;
988 app_value->counter.in_bytes = context->bytes;
989 g_system->apps_tree_updated = TRUE;
992 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
994 case NFACCT_COUNTER_OUT:
995 app_value->data_usage.out_bytes += context->bytes;
996 app_value->counter.out_bytes = context->bytes;
997 g_system->apps_tree_updated = TRUE;
1000 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1003 STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
1007 static void __interface_counter_update(stc_app_key_s *app_key,
1008 stc_app_value_s *app_value,
1009 classid_bytes_context_s *context)
1011 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1012 context->counter->iftype == STC_IFACE_DATACALL) ||
1013 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1014 context->counter->iftype == STC_IFACE_WIFI) ||
1015 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1016 context->counter->iftype == STC_IFACE_BLUETOOTH))
1017 __app_counter_update(app_key, app_value, context);
1021 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1025 stc_app_key_s *app_key = (stc_app_key_s *)key;
1026 stc_app_value_s *app_value = (stc_app_value_s *)value;
1027 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1029 if (context->counter->intend != NFACCT_COUNTER)
1030 goto try_next_callback;
1032 __interface_counter_update(app_key, app_value, context);
1034 if (app_value->classid != context->counter->classid)
1035 goto try_next_callback;
1037 __app_counter_update(app_key, app_value, context);
1043 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1044 struct counter_arg *carg)
1046 struct nfacct_rule counter = {
1053 classid_bytes_context_s context = {
1054 .counter = &counter,
1056 .data_limit_reached = FALSE,
1060 STC_LOGD("cnt_name %s", cnt_name); //LCOV_EXCL_LINE
1062 if (!recreate_counter_by_name(cnt_name, &counter)) {
1063 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1064 return; //LCOV_EXCL_LINE
1067 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1068 context.counter->classid, context.counter->iftype,
1069 context.counter->iotype, context.counter->intend,
1070 context.counter->ifname, context.bytes);
1072 if (g_system->rstns)
1073 g_tree_foreach(g_system->rstns,
1074 __rstn_counter_update_foreach_classid,
1078 g_tree_foreach(g_system->apps,
1079 __apps_counter_update_foreach_classid,
1083 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1086 struct counter_arg *carg = user_data;
1087 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1088 if (carg->initiate) {
1090 * TODO: this will be used when daemon starts to update existing
1091 * counter data if present.
1093 populate_counters(cnt_name, carg);
1097 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1098 int bytes = be64toh(*bytes_p);
1100 ++carg->serialized_counters;
1101 __fill_nfacct_result(cnt_name, bytes, carg);
1108 static int __post_fill_counters(void *user_data)
1110 struct counter_arg *carg = user_data;
1118 static void __process_network_counter(struct genl *ans,
1119 struct counter_arg *carg)
1121 struct netlink_serialization_params ser_params = {
1124 .eval_attr = __fill_counters,
1125 .post_eval_attr = __post_fill_counters,
1128 netlink_serialization_command *netlink =
1129 netlink_create_command(&ser_params);
1131 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
1132 return; //LCOV_EXCL_LINE
1135 netlink->deserialize_answer(&(netlink->params));
1138 static gboolean __process_contr_reply(GIOChannel *source,
1139 GIOCondition condition,
1142 int sock = g_io_channel_unix_get_fd(source);
1145 stc_s *stc = stc_get_manager();
1148 void __gcov_flush(void);
1152 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1153 (condition & G_IO_NVAL)) {
1154 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1156 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
1157 "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
1158 (condition & G_IO_ERR), (condition & G_IO_HUP),
1159 (condition & G_IO_NVAL));
1160 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
1161 return FALSE; //LCOV_EXCL_LINE
1164 ans = MALLOC0(struct genl, 1);
1166 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
1167 return TRUE; //LCOV_EXCL_LINE
1171 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
1172 goto out; //LCOV_EXCL_LINE
1175 ret = read_netlink(sock, ans, sizeof(struct genl));
1176 /* STC_LOGD("Counter data received ret [%d]", ret); */
1180 stc->carg->ans_len = ret;
1181 __process_network_counter(ans, stc->carg);
1183 g_idle_add(__flush_apps_stats_to_database, NULL);
1184 g_idle_add(__flush_rstns_counter_to_database, NULL);
1191 static gboolean __update_contr_cb(void *user_data)
1193 /* Here we just sent command, answer we receive in another callback */
1194 stc_s *stc = stc_get_manager();
1195 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1197 stc->carg = MALLOC0(counter_arg_s, 1);
1198 if (stc->carg == NULL)
1199 return TRUE; /* we need to continue the timer */
1201 stc->carg->sock = stc_monitor_get_counter_socket();
1205 void __gcov_flush(void);
1209 /* STC_LOGD("Get all counters"); */
1210 nfacct_send_get_all(stc->carg);
1212 /* we need to continue the timer */
1216 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1219 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1220 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1222 __print_rstn(rstn_key, rstn_value);
1226 static void __rstn_tree_printall(void)
1228 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1231 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1232 const stc_rstn_key_s *key)
1234 stc_rstn_value_s *lookup;
1236 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1238 lookup = g_tree_lookup(rstns_tree, key);
1243 static gboolean __remove_restriction(gpointer key, gpointer value,
1246 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1247 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1249 /* rstn rule is already removed */
1250 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
1253 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1254 __print_rstn(rstn_key, rstn_value);
1258 static gboolean __add_restriction_debug(gpointer key, gpointer value,
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);
1273 __print_rstn(rstn_key, rstn_value);
1279 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1281 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1282 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1284 /* rstn rule is activated */
1285 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1288 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1289 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1291 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1297 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1299 stc_rstn_value_s *lookup_value;
1301 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1303 lookup_value = __rstn_lookup(g_system->rstns, key);
1304 if (!lookup_value) {
1305 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1306 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1309 __remove_restriction(key, lookup_value, NULL);
1311 /* remove counter also */
1312 table_counters_delete(lookup_value->restriction_id);
1314 if (!g_tree_remove(g_system->rstns, key)) {
1315 STC_LOGD("key not found"); //LCOV_EXCL_LINE
1316 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1319 return STC_ERROR_NONE;
1322 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1323 stc_rstn_value_s *value, gboolean debug)
1325 stc_rstn_value_s *rstn_value;
1327 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1329 rstn_value = __rstn_lookup(g_system->rstns, key);
1331 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1333 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
1334 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1337 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1339 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
1340 FREE(rstn_key); //LCOV_EXCL_LINE
1341 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1344 rstn_key->app_id = g_strdup(key->app_id);
1345 rstn_key->ifname = g_strdup(key->ifname);
1346 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1347 rstn_key->iftype = key->iftype;
1348 rstn_key->roaming = key->roaming;
1350 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1353 rstn_value->restriction_id = value->restriction_id;
1354 rstn_value->rst_state = value->rst_state;
1355 rstn_value->classid = value->classid;
1356 rstn_value->data_limit = value->data_limit;
1357 rstn_value->data_warn_limit = value->data_warn_limit;
1358 rstn_value->data_counter = 0;
1359 rstn_value->warn_limit_crossed_notified = FALSE;
1360 rstn_value->rstn_limit_crossed_notified = FALSE;
1363 __add_restriction_debug(key, rstn_value, NULL);
1365 __add_restriction(key, rstn_value, NULL);
1367 return STC_ERROR_NONE;
1371 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1374 stc_cb_ret_e ret = STC_CONTINUE;
1377 stc_rstn_value_s value;
1379 memset(&key, 0, sizeof(stc_rstn_key_s));
1380 memset(&value, 0, sizeof(stc_rstn_value_s));
1382 key.app_id = g_strdup(info->app_id);
1383 key.ifname = g_strdup(info->ifname);
1384 key.subscriber_id = g_strdup(info->subscriber_id);
1385 key.iftype = info->iftype;
1386 key.roaming = info->roaming;
1388 value.rst_state = info->rst_state;
1389 value.restriction_id = info->restriction_id;
1391 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1392 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1394 value.classid = STC_UNKNOWN_CLASSID;
1396 value.data_limit = info->data_limit;
1397 value.data_warn_limit = info->data_warn_limit;
1399 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1404 FREE(key.subscriber_id);
1408 static void __fill_restritions_list(void)
1410 table_restrictions_foreach(__insert_restriction_cb, NULL);
1411 //__rstn_tree_printall();
1414 static gboolean __add_rstn_foreach_application(gpointer key,
1418 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1419 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1420 gchar *app_id = (gchar *)data;
1422 /* rstn rule is not for applications */
1423 if (rstn_key->app_id == NULL)
1426 /* rstn rule is not for this application */
1427 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1430 /* rstn rule is already applied */
1431 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1434 /* add restriction to system */
1435 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1436 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
1438 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
1440 __print_rstn(rstn_key, rstn_value);
1446 static void __add_rstns_for_application(gchar *app_id)
1448 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1452 static void __add_application_by_interface(const char *app_id)
1454 stc_app_key_s app_key;
1455 stc_app_value_s app_value;
1458 return; //LCOV_EXCL_LINE
1460 memset(&app_key, 0, sizeof(stc_app_key_s));
1461 memset(&app_value, 0, sizeof(stc_app_value_s));
1463 app_key.pkg_id = g_strdup(app_id);
1464 app_key.app_id = g_strdup(app_id);
1466 app_value.type = STC_APP_TYPE_NONE;
1467 app_value.processes = NULL;
1468 app_value.counter.in_bytes = 0;
1469 app_value.counter.out_bytes = 0;
1471 stc_monitor_application_add(app_key, app_value);
1473 FREE(app_key.pkg_id);
1474 FREE(app_key.app_id);
1477 static int __vconf_get_int(const char *key, int *value)
1481 ret = vconf_get_int(key, value);
1482 if (ret != VCONF_OK) {
1483 STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
1484 return -1; //LCOV_EXCL_LINE
1491 static int __vconf_set_int(const char *key, int value)
1495 ret = vconf_set_int(key, value);
1496 if (ret != VCONF_OK) {
1497 STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
1498 return -1; //LCOV_EXCL_LINE
1504 static guint __get_background_state(void)
1506 return g_system->background_state;;
1509 static void __set_background_state(guint state)
1511 g_system->background_state = state;
1514 static gboolean __processes_tree_foreach_background(gpointer key,
1518 stc_process_key_s *proc_key = (stc_process_key_s *)key;
1519 stc_app_key_s *app_key = (stc_app_key_s *)data;
1521 if (g_system->background_state)
1522 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1524 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1529 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1532 stc_app_key_s *app_key = (stc_app_key_s *)key;
1533 stc_app_value_s *app_value = (stc_app_value_s *)value;
1535 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1536 g_tree_foreach(app_value->processes,
1537 __processes_tree_foreach_background, app_key);
1542 static stc_error_e __process_update_background(void)
1544 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1546 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1548 return STC_ERROR_NONE;
1553 static void __excn_hash_foreach_print(gpointer key, gpointer value,
1556 const char *process_name = key;
1557 const char *exe_type = value;
1559 STC_LOGI("excn info => process_name [%s] exe_type [%s]",
1560 process_name, exe_type);
1563 static void __excn_hash_printall(void)
1565 g_hash_table_foreach(g_system->excns_hash,
1566 __excn_hash_foreach_print, NULL);
1571 static gboolean __remove_exception_app(gpointer key, gpointer value,
1574 const char *exe_type = value;
1576 if (g_strcmp0(exe_type, EXE_TYPE_APPLICATION) == 0)
1583 static void __remove_exception_appall(void)
1585 g_hash_table_foreach_remove(g_system->excns_hash, //LCOV_EXCL_LINE
1586 __remove_exception_app, NULL);
1589 static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info,
1592 stc_cb_ret_e ret = STC_CONTINUE;
1594 if (g_hash_table_insert(g_system->excns_hash,
1595 g_strdup(info->process_name),
1596 g_strdup(info->exe_type)) != TRUE)
1597 ret = STC_CANCEL; //LCOV_EXCL_LINE
1602 static void __fill_exceptions_list(void)
1604 table_exceptions_foreach(__insert_exception_cb, NULL);
1605 pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
1607 /* __excn_hash_printall(); */
1611 static gboolean __update_exceptions_app_list(void *user_data)
1613 __remove_exception_appall();
1614 pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
1616 /* __excn_hash_printall(); */
1622 stc_error_e stc_monitor_init(void)
1624 stc_system_s *system = MALLOC0(stc_system_s, 1);
1625 GIOChannel *gio = NULL;
1627 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1629 /* initializing current classid */
1630 init_current_classid();
1632 /* initializing cgroups */
1635 /* creating monitored application tree */
1636 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1637 __apps_tree_key_free,
1638 __apps_tree_value_free);
1640 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1641 __rstns_tree_key_free,
1642 __rstns_tree_value_free);
1644 system->excns_hash = g_hash_table_new_full(g_str_hash,
1645 g_str_equal, g_free, g_free);
1647 /* create netlink socket for updating kernel counters */
1648 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1649 if (system->contr_sock < 0) {
1650 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
1651 FREE(system); //LCOV_EXCL_LINE
1652 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1655 gio = g_io_channel_unix_new(system->contr_sock);
1656 system->contr_gsource_id =
1657 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1658 (GIOFunc) __process_contr_reply,
1660 g_io_channel_unref(gio);
1664 __add_application_by_interface(STC_TOTAL_DATACALL);
1665 __add_application_by_interface(STC_TOTAL_WIFI);
1666 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1667 __add_application_by_interface(STC_TOTAL_IPV4);
1668 __add_application_by_interface(STC_TOTAL_IPV6);
1670 /* creating restriction rules tree */
1671 __update_contr_cb(NULL);
1673 /* registering periodic kernel counters update callback */
1674 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1677 if (g_system->contr_timer_id == 0) {
1678 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
1679 __close_contr_sock(g_system); //LCOV_EXCL_LINE
1680 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1683 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1684 (int *)&g_system->background_state);
1686 __fill_exceptions_list();
1687 __fill_restritions_list();
1689 g_system->excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL,
1690 __update_exceptions_app_list,
1693 return STC_ERROR_NONE;
1696 stc_error_e stc_monitor_deinit(void)
1698 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1700 /* close netlink socket for updating kernel counters */
1701 __close_contr_sock(g_system);
1703 /* remove kernel counters update timer */
1704 if (g_system->contr_timer_id > 0) {
1705 g_source_remove(g_system->contr_timer_id);
1706 g_system->contr_timer_id = 0;
1709 /* remove exceptions app list update timer */
1710 if (g_system->excns_timer_id > 0) {
1711 g_source_remove(g_system->excns_timer_id);
1712 g_system->excns_timer_id = 0;
1715 /* destroy monitored application tree */
1716 g_tree_destroy(g_system->apps);
1717 g_system->apps = NULL;
1719 /* destroy restriction rules tree */
1720 g_tree_destroy(g_system->rstns);
1721 g_system->rstns = NULL;
1723 /* destroy exception hash table */
1724 g_hash_table_destroy(g_system->excns_hash);
1725 g_system->excns_hash = NULL;
1729 return STC_ERROR_NONE;
1732 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1733 const stc_app_value_s app_value)
1735 stc_error_e ret = STC_ERROR_NONE;
1737 stc_app_value_s *value;
1738 stc_app_value_s *lookup;
1740 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1742 lookup = __application_lookup(g_system->apps, &app_key);
1744 STC_LOGD("app_key already present"); //LCOV_EXCL_LINE
1745 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1748 key = MALLOC0(stc_app_key_s, 1);
1750 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1751 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1754 value = MALLOC0(stc_app_value_s, 1);
1756 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1757 FREE(key); //LCOV_EXCL_LINE
1758 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1761 key->app_id = g_strdup(app_key.app_id);
1762 key->pkg_id = g_strdup(app_key.pkg_id);
1764 value->type = app_value.type;
1765 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1766 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1768 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1769 __processes_tree_key_free,
1770 __processes_tree_value_free);
1772 /* create cgroup and update classid */
1773 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1775 g_tree_insert(g_system->apps, key, value);
1777 /* add nfacct rule for this classid */
1778 __add_application_monitor(key, value, stc_get_default_connection());
1779 __add_rstns_for_application(app_key.app_id);
1784 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1785 const stc_process_key_s proc_key,
1786 const stc_process_value_s proc_value)
1788 stc_error_e ret = STC_ERROR_NONE;
1789 stc_app_value_s *app_lookup;
1790 stc_process_key_s *key;
1791 stc_process_value_s *value;
1792 stc_process_value_s *proc_lookup;
1794 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1796 app_lookup = __application_lookup(g_system->apps, &app_key);
1798 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
1799 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1802 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1804 STC_LOGD("proc_key already present"); //LCOV_EXCL_LINE
1805 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1808 key = MALLOC0(stc_process_key_s, 1);
1810 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1811 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1814 value = MALLOC0(stc_process_value_s, 1);
1816 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1817 FREE(key); //LCOV_EXCL_LINE
1818 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1821 key->pid = proc_key.pid;
1823 value->ground = proc_value.ground;
1825 g_tree_insert(app_lookup->processes, key, value);
1827 /* add pid to application cgroup */
1828 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1831 __apps_tree_printall(); //LCOV_EXCL_LINE
1836 stc_error_e stc_monitor_process_remove(pid_t pid)
1838 stc_error_e ret = STC_ERROR_NONE;
1839 stc_process_key_s proc_key = {
1843 remove_pid_context_s context = {
1845 .proc_key = &proc_key,
1846 .entry_removed = FALSE,
1849 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1851 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1854 if (context.entry_removed)
1855 __application_remove_if_empty(context.app_key);
1858 __apps_tree_printall(); //LCOV_EXCL_LINE
1864 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1865 const stc_process_key_s proc_key,
1866 stc_app_state_e ground)
1868 stc_error_e ret = STC_ERROR_NONE;
1869 stc_app_value_s *app_lookup;
1870 stc_process_value_s *proc_lookup;
1872 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1874 app_lookup = __application_lookup(g_system->apps, &app_key);
1876 STC_LOGD("app_key not found");
1877 return STC_ERROR_FAIL;
1880 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1882 STC_LOGD("proc_key not found");
1883 return STC_ERROR_FAIL;
1886 if (proc_lookup->ground != ground)
1887 proc_lookup->ground = ground;
1889 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1890 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1892 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1898 void stc_monitor_update_rstn_by_default_connection(void *data)
1900 static default_connection_s old_connection;
1901 default_connection_s *new_connection = (default_connection_s *)data;
1903 if (old_connection.path != NULL) {
1906 g_tree_foreach(g_system->apps,
1907 __remove_application_monitor,
1908 (gpointer)&old_connection);
1910 if (g_system->rstns)
1911 g_tree_foreach(g_system->rstns,
1912 __remove_restriction,
1913 (gpointer)&old_connection);
1915 iptables_flush_chains();
1919 FREE(old_connection.path);
1920 FREE(old_connection.ifname);
1921 old_connection.type = 0;
1922 old_connection.roaming = 0;
1924 if (new_connection != NULL && new_connection->path != NULL) {
1926 g_tree_foreach(g_system->apps,
1927 __add_application_monitor,
1928 (gpointer)new_connection);
1930 if (g_system->rstns)
1931 g_tree_foreach(g_system->rstns, __add_restriction,
1934 old_connection.path = g_strdup(new_connection->path);
1935 old_connection.ifname = g_strdup(new_connection->ifname);
1936 old_connection.type = new_connection->type;
1937 old_connection.roaming = new_connection->roaming;
1941 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1946 stc_rstn_value_s value;
1948 memset(&key, 0, sizeof(stc_rstn_key_s));
1949 memset(&value, 0, sizeof(stc_rstn_value_s));
1951 key.app_id = g_strdup(info->app_id);
1952 key.ifname = g_strdup(info->ifname);
1953 key.subscriber_id = g_strdup(info->subscriber_id);
1954 key.iftype = info->iftype;
1955 key.roaming = info->roaming;
1957 value.rst_state = info->rst_state;
1958 value.restriction_id = info->restriction_id;
1960 if (value.rst_state > STC_RESTRICTION_UNKNOWN &&
1961 value.rst_state < STC_RESTRICTION_LAST_ELEM &&
1963 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1965 value.classid = STC_UNKNOWN_CLASSID;
1967 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1968 __set_background_state(TRUE); //LCOV_EXCL_LINE
1969 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1970 __process_update_background(); //LCOV_EXCL_LINE
1973 value.data_limit = info->data_limit;
1974 value.data_warn_limit = info->data_warn_limit;
1976 ret = __rstn_tree_add(&key, &value, TRUE);
1980 FREE(key.subscriber_id);
1984 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1988 stc_rstn_key_s key = {
1989 .app_id = g_strdup(info->app_id),
1990 .ifname = g_strdup(info->ifname),
1991 .subscriber_id = g_strdup(info->subscriber_id),
1992 .iftype = info->iftype,
1993 .roaming = info->roaming,
1996 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
1997 __set_background_state(FALSE); //LCOV_EXCL_LINE
1998 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1999 __process_update_background(); //LCOV_EXCL_LINE
2002 ret = __rstn_tree_remove(&key);
2006 FREE(key.subscriber_id);
2010 stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
2012 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2014 char *exe_type = NULL;
2016 exe_type = g_hash_table_lookup(g_system->excns_hash, cmdline);
2018 return STC_ERROR_NO_DATA;
2020 return STC_ERROR_NONE;
2023 int stc_monitor_get_counter_socket(void)
2025 return g_system->contr_sock;