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-appstatus.h"
32 #include "stc-manager-plugin-exception.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,
555 g_free(default_ifname);
558 __add_iptables_in(&counter);
559 __add_iptables_out(&counter);
562 __add_ip6tables_in(&counter);
563 __add_ip6tables_out(&counter);
566 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
567 rstn_value->data_limit_reached = FALSE;
574 char *default_ifname = stc_default_connection_get_ifname();
575 struct nfacct_rule counter;
576 stc_s *stc = stc_get_manager();
578 g_free(default_ifname); //LCOV_EXCL_LINE
579 return; //LCOV_EXCL_LINE
583 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
584 if (stc->carg == NULL) { //LCOV_EXCL_LINE
585 g_free(default_ifname); //LCOV_EXCL_LINE
586 return; //LCOV_EXCL_LINE
589 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
592 counter.carg = stc->carg;
593 counter.classid = rstn_value->classid;
594 counter.intend = NFACCT_BLOCK;
595 counter.iftype = rstn_key->iftype;
596 g_strlcpy(counter.ifname, default_ifname,
598 g_free(default_ifname);
601 __del_iptables_in(&counter);
602 __del_iptables_out(&counter);
605 __del_ip6tables_in(&counter);
606 __del_ip6tables_out(&counter);
608 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
609 rstn_value->data_limit_reached = FALSE;
618 static gboolean __remove_rstns_foreach_application(gpointer key,
622 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
623 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
624 gchar *app_id = (gchar *)data;
626 /* rstn rule is not for applications */
627 if (rstn_key->app_id == NULL)
630 /* rstn rule is not for this application */
631 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
634 /* rstn rule is already removed */
635 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
638 /* remove restriction from system */
639 __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
641 __print_rstn(rstn_key, rstn_value);
647 static void __remove_rstns_for_application(gchar *app_id)
649 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
653 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
655 stc_error_e ret = STC_ERROR_NONE;
657 stc_app_value_s *lookup;
659 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
661 lookup = __application_lookup(g_system->apps, app_key);
663 STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
664 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
667 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
671 /* remove nfacct rule for this classid */
672 __remove_application_monitor((gpointer) app_key, lookup,
673 stc_get_default_connection());
674 __remove_rstns_for_application(app_key->app_id);
677 if (!g_tree_remove(g_system->apps, app_key)) {
678 ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
679 STC_LOGE("key not found"); //LCOV_EXCL_LINE
685 static stc_error_e __close_contr_sock(stc_system_s *system)
687 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
689 /* close netlink socket for updating kernel counters */
690 if (system->contr_sock != -1) {
691 close(system->contr_sock);
692 system->contr_sock = -1;
695 if (system->contr_gsource_id != 0) {
696 g_source_remove(system->contr_gsource_id);
697 system->contr_gsource_id = 0;
700 return STC_ERROR_NONE;
703 static gboolean __process_contr_reply(GIOChannel *source,
704 GIOCondition condition,
708 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
710 GIOChannel *gio = NULL;
711 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
713 /* close netlink socket for updating kernel counters */
714 if (system->contr_sock != -1) {
715 close(system->contr_sock);
716 system->contr_sock = -1;
719 if (system->contr_gsource_id != 0) {
720 g_source_remove(system->contr_gsource_id);
721 system->contr_gsource_id = 0;
724 /* create netlink socket for updating kernel counters */
725 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
726 if (system->contr_sock < 0) {
727 STC_LOGE("failed to open socket");
729 return STC_ERROR_FAIL;
732 gio = g_io_channel_unix_new(system->contr_sock);
733 system->contr_gsource_id =
734 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
735 (GIOFunc) __process_contr_reply,
737 g_io_channel_unref(gio);
739 return STC_ERROR_NONE;
742 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
743 stc_rstn_value_s *rstn_value,
744 classid_bytes_context_s *context)
746 switch (context->counter->iotype) {
747 case NFACCT_COUNTER_IN:
748 case NFACCT_COUNTER_OUT:
749 rstn_value->data_counter += context->bytes;
751 if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
752 rstn_value->warn_limit_crossed_notified == FALSE) {
755 char iftype[MAX_INT_LENGTH];
756 char byte[MAX_INT_LENGTH];
757 stc_s *stc = (stc_s *)stc_get_manager();
758 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
761 rv = stc_manager_dbus_emit_signal(stc->connection,
762 STC_DBUS_SERVICE_RESTRICTION_PATH,
763 STC_DBUS_INTERFACE_RESTRICTION,
764 "WarnThresholdCrossed",
765 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
767 rstn_value->warn_limit_crossed_notified = TRUE;
769 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
770 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
771 stc_plugin_appstatus_send_warn_message("warn threshold crossed",
777 if (rstn_value->data_counter >= rstn_value->data_limit &&
778 rstn_value->rstn_limit_crossed_notified == FALSE) {
781 char iftype[MAX_INT_LENGTH];
782 char byte[MAX_INT_LENGTH];
783 stc_s *stc = (stc_s *)stc_get_manager();
784 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
786 /* block immediately */
787 context->counter->intend = NFACCT_BLOCK;
788 __del_iptables_in(context->counter);
789 __del_iptables_out(context->counter);
790 __add_iptables_in(context->counter);
791 __add_iptables_out(context->counter);
793 __del_ip6tables_in(context->counter);
794 __del_ip6tables_out(context->counter);
795 __add_ip6tables_in(context->counter);
796 __add_ip6tables_out(context->counter);
797 context->counter->intend = NFACCT_COUNTER;
799 rstn_value->data_limit_reached = TRUE;
802 rv = stc_manager_dbus_emit_signal(stc->connection,
803 STC_DBUS_SERVICE_RESTRICTION_PATH,
804 STC_DBUS_INTERFACE_RESTRICTION,
805 "RestrictionThresholdCrossed",
806 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
808 rstn_value->rstn_limit_crossed_notified = TRUE;
810 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
811 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
812 stc_plugin_appstatus_send_restriction_message("restriction threshold crossed",
813 "restriction_noti", rstn_key->app_id,
817 g_system->rstns_tree_updated = TRUE;
818 __print_rstn(rstn_key, rstn_value);
821 STC_LOGE("unknown iotype");
827 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
828 stc_rstn_value_s *rstn_value,
829 classid_bytes_context_s *context)
831 if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
832 context->counter->iftype == STC_IFACE_DATACALL) ||
833 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
834 context->counter->iftype == STC_IFACE_WIFI) ||
835 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
836 context->counter->iftype == STC_IFACE_BLUETOOTH)) {
837 context->counter->classid = rstn_value->classid;
838 return __rstn_counter_update(rstn_key, rstn_value, context);
844 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
849 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
850 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
851 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
854 if (context->counter->intend != NFACCT_COUNTER)
855 goto try_next_callback;
857 if (rstn_value->data_limit_reached == TRUE) {
858 context->data_limit_reached = TRUE; //LCOV_EXCL_LINE
859 goto try_next_callback; //LCOV_EXCL_LINE
862 classid = context->counter->classid;
863 rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
865 context->counter->classid = classid;
866 if (rstn_value->classid != context->counter->classid)
867 goto try_next_callback;
869 rv = __rstn_counter_update(rstn_key, rstn_value, context);
876 static gboolean __update_app_statistics(gpointer key, gpointer value,
879 stc_app_key_s *app_key = (stc_app_key_s *)key;
880 stc_app_value_s *app_value = (stc_app_value_s *)value;
881 time_t *touch_time = (time_t *)data;
882 stc_db_classid_iftype_key stat_key;
883 stc_db_app_stats stat;
884 default_connection_s *default_connection = stc_get_default_connection();
886 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
887 memset(&stat, 0 , sizeof(stc_db_app_stats));
889 stat_key.classid = app_value->classid;
890 stat_key.iftype = default_connection->type;
892 if (STC_IFACE_DATACALL == stat_key.iftype)
893 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
895 stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
897 g_strlcpy(stat_key.ifname, default_connection->ifname,
900 stat.app_id = g_strdup(app_key->app_id);
901 stat.snd_count = app_value->counter.out_bytes;
902 stat.rcv_count = app_value->counter.in_bytes;
903 stat.is_roaming = default_connection->roaming;
905 if (strstr(stat.app_id, "_BACKGROUND")) {
906 stat.ground = STC_APP_STATE_BACKGROUND;
908 if (strstr(stat.app_id, "TOTAL_"))
909 stat.ground = STC_APP_STATE_UNKNOWN;
911 stat.ground = STC_APP_STATE_FOREGROUND;
914 table_statistics_insert(&stat_key, &stat, *touch_time);
916 app_value->counter.out_bytes = 0;
917 app_value->counter.in_bytes = 0;
920 FREE(stat_key.subscriber_id);
925 static gboolean __flush_apps_stats_to_database(gpointer user_data)
927 time_t current_time = time(0);
929 if (g_system->apps_tree_updated == FALSE)
930 return G_SOURCE_REMOVE;
932 g_system->apps_tree_updated = FALSE;
935 g_tree_foreach(g_system->apps,
936 __update_app_statistics,
939 STC_LOGI("Flushed app stats to database");
940 return G_SOURCE_REMOVE;
944 static gboolean __update_counter_statistics(gpointer key, gpointer value,
947 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
948 table_counters_info info = {
949 .restriction_id = rstn_value->restriction_id,
950 .data_counter = rstn_value->data_counter
953 table_counters_update_counters(&info);
958 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
960 time_t current_time = time(0);
962 if (g_system->rstns_tree_updated == FALSE)
963 return G_SOURCE_REMOVE;
965 g_system->rstns_tree_updated = FALSE;
968 g_tree_foreach(g_system->rstns,
969 __update_counter_statistics,
972 STC_LOGI("Flushed rstns counters to database");
973 return G_SOURCE_REMOVE;
977 static void __app_counter_update(stc_app_key_s *app_key,
978 stc_app_value_s *app_value,
979 classid_bytes_context_s *context)
981 switch (context->counter->iotype) {
982 case NFACCT_COUNTER_IN:
983 app_value->data_usage.in_bytes += context->bytes;
984 app_value->counter.in_bytes = context->bytes;
985 g_system->apps_tree_updated = TRUE;
988 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
990 case NFACCT_COUNTER_OUT:
991 app_value->data_usage.out_bytes += context->bytes;
992 app_value->counter.out_bytes = context->bytes;
993 g_system->apps_tree_updated = TRUE;
996 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
999 STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
1003 static void __interface_counter_update(stc_app_key_s *app_key,
1004 stc_app_value_s *app_value,
1005 classid_bytes_context_s *context)
1007 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1008 context->counter->iftype == STC_IFACE_DATACALL) ||
1009 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1010 context->counter->iftype == STC_IFACE_WIFI) ||
1011 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1012 context->counter->iftype == STC_IFACE_BLUETOOTH))
1013 __app_counter_update(app_key, app_value, context);
1017 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1021 stc_app_key_s *app_key = (stc_app_key_s *)key;
1022 stc_app_value_s *app_value = (stc_app_value_s *)value;
1023 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1025 if (context->counter->intend != NFACCT_COUNTER)
1026 goto try_next_callback;
1028 __interface_counter_update(app_key, app_value, context);
1030 if (app_value->classid != context->counter->classid)
1031 goto try_next_callback;
1033 __app_counter_update(app_key, app_value, context);
1039 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1040 struct counter_arg *carg)
1042 struct nfacct_rule counter = {
1049 classid_bytes_context_s context = {
1050 .counter = &counter,
1052 .data_limit_reached = FALSE,
1056 STC_LOGD("cnt_name %s", cnt_name); //LCOV_EXCL_LINE
1058 if (!recreate_counter_by_name(cnt_name, &counter)) {
1059 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1060 return; //LCOV_EXCL_LINE
1063 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1064 context.counter->classid, context.counter->iftype,
1065 context.counter->iotype, context.counter->intend,
1066 context.counter->ifname, context.bytes);
1068 if (g_system->rstns)
1069 g_tree_foreach(g_system->rstns,
1070 __rstn_counter_update_foreach_classid,
1074 g_tree_foreach(g_system->apps,
1075 __apps_counter_update_foreach_classid,
1079 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1082 struct counter_arg *carg = user_data;
1083 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1084 if (carg->initiate) {
1086 * TODO: this will be used when daemon starts to update existing
1087 * counter data if present.
1089 populate_counters(cnt_name, carg);
1093 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1094 int bytes = be64toh(*bytes_p);
1096 ++carg->serialized_counters;
1097 __fill_nfacct_result(cnt_name, bytes, carg);
1104 static int __post_fill_counters(void *user_data)
1106 struct counter_arg *carg = user_data;
1114 static void __process_network_counter(struct genl *ans,
1115 struct counter_arg *carg)
1117 struct netlink_serialization_params ser_params = {
1120 .eval_attr = __fill_counters,
1121 .post_eval_attr = __post_fill_counters,
1124 netlink_serialization_command *netlink =
1125 netlink_create_command(&ser_params);
1127 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
1128 return; //LCOV_EXCL_LINE
1131 netlink->deserialize_answer(&(netlink->params));
1134 static gboolean __process_contr_reply(GIOChannel *source,
1135 GIOCondition condition,
1138 int sock = g_io_channel_unix_get_fd(source);
1141 stc_s *stc = stc_get_manager();
1144 void __gcov_flush(void);
1148 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1149 (condition & G_IO_NVAL)) {
1150 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1152 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
1153 "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
1154 (condition & G_IO_ERR), (condition & G_IO_HUP),
1155 (condition & G_IO_NVAL));
1156 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
1157 return FALSE; //LCOV_EXCL_LINE
1160 ans = MALLOC0(struct genl, 1);
1162 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
1163 return TRUE; //LCOV_EXCL_LINE
1167 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
1168 goto out; //LCOV_EXCL_LINE
1171 ret = read_netlink(sock, ans, sizeof(struct genl));
1172 /* STC_LOGD("Counter data received ret [%d]", ret); */
1176 stc->carg->ans_len = ret;
1177 __process_network_counter(ans, stc->carg);
1179 g_idle_add(__flush_apps_stats_to_database, NULL);
1180 g_idle_add(__flush_rstns_counter_to_database, NULL);
1187 static gboolean __update_contr_cb(void *user_data)
1189 /* Here we just sent command, answer we receive in another callback */
1190 stc_s *stc = stc_get_manager();
1191 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1193 stc->carg = MALLOC0(counter_arg_s, 1);
1194 if (stc->carg == NULL)
1195 return TRUE; /* we need to continue the timer */
1197 stc->carg->sock = stc_monitor_get_counter_socket();
1201 void __gcov_flush(void);
1205 /* STC_LOGD("Get all counters"); */
1206 nfacct_send_get_all(stc->carg);
1208 /* we need to continue the timer */
1213 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1216 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1217 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1219 __print_rstn(rstn_key, rstn_value);
1223 static void __rstn_tree_printall(void)
1225 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1229 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1230 const stc_rstn_key_s *key)
1232 stc_rstn_value_s *lookup;
1234 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1236 lookup = g_tree_lookup(rstns_tree, key);
1241 static gboolean __remove_restriction(gpointer key, gpointer value,
1244 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1245 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1247 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1248 __print_rstn(rstn_key, rstn_value);
1252 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1255 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1256 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1258 /* rstn rule is activated */
1259 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1262 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1263 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1265 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1267 __print_rstn(rstn_key, rstn_value);
1273 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1275 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1276 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1278 /* rstn rule is activated */
1279 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1282 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1283 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1285 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1291 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1293 stc_rstn_value_s *lookup_value;
1295 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1297 lookup_value = __rstn_lookup(g_system->rstns, key);
1298 if (!lookup_value) {
1299 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1300 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1303 __remove_restriction(key, lookup_value, NULL);
1305 /* remove counter also */
1306 table_counters_delete(lookup_value->restriction_id);
1308 if (!g_tree_remove(g_system->rstns, key)) {
1309 STC_LOGD("key not found"); //LCOV_EXCL_LINE
1310 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1313 return STC_ERROR_NONE;
1316 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1317 stc_rstn_value_s *value, gboolean debug)
1319 stc_rstn_value_s *rstn_value;
1321 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1323 rstn_value = __rstn_lookup(g_system->rstns, key);
1325 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1327 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
1328 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1331 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1333 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
1334 FREE(rstn_key); //LCOV_EXCL_LINE
1335 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1338 rstn_key->app_id = g_strdup(key->app_id);
1339 rstn_key->ifname = g_strdup(key->ifname);
1340 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1341 rstn_key->iftype = key->iftype;
1342 rstn_key->roaming = key->roaming;
1344 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1347 rstn_value->restriction_id = value->restriction_id;
1348 rstn_value->rst_state = value->rst_state;
1349 rstn_value->classid = value->classid;
1350 rstn_value->data_limit = value->data_limit;
1351 rstn_value->data_warn_limit = value->data_warn_limit;
1352 rstn_value->data_counter = 0;
1353 rstn_value->warn_limit_crossed_notified = FALSE;
1354 rstn_value->rstn_limit_crossed_notified = FALSE;
1357 __add_restriction_debug(key, rstn_value, NULL);
1359 __add_restriction(key, rstn_value, NULL);
1361 return STC_ERROR_NONE;
1365 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1368 stc_cb_ret_e ret = STC_CONTINUE;
1371 stc_rstn_value_s value;
1373 memset(&key, 0, sizeof(stc_rstn_key_s));
1374 memset(&value, 0, sizeof(stc_rstn_value_s));
1376 key.app_id = g_strdup(info->app_id);
1377 key.ifname = g_strdup(info->ifname);
1378 key.subscriber_id = g_strdup(info->subscriber_id);
1379 key.iftype = info->iftype;
1380 key.roaming = info->roaming;
1382 value.rst_state = info->rst_state;
1383 value.restriction_id = info->restriction_id;
1385 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1386 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1388 value.classid = STC_UNKNOWN_CLASSID;
1390 value.data_limit = info->data_limit;
1391 value.data_warn_limit = info->data_warn_limit;
1393 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1398 FREE(key.subscriber_id);
1402 static void __fill_restritions_list(void)
1404 table_restrictions_foreach(__insert_restriction_cb, NULL);
1407 __rstn_tree_printall();
1410 static gboolean __add_rstn_foreach_application(gpointer key,
1414 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1415 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1416 gchar *app_id = (gchar *)data;
1418 /* rstn rule is not for applications */
1419 if (rstn_key->app_id == NULL)
1422 /* rstn rule is not for this application */
1423 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1426 /* rstn rule is already applied */
1427 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1430 /* add restriction to system */
1431 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1432 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
1434 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
1436 __print_rstn(rstn_key, rstn_value);
1442 static void __add_rstns_for_application(gchar *app_id)
1444 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1448 static void __add_application_by_interface(const char *app_id)
1450 stc_app_key_s app_key;
1451 stc_app_value_s app_value;
1454 return; //LCOV_EXCL_LINE
1456 memset(&app_key, 0, sizeof(stc_app_key_s));
1457 memset(&app_value, 0, sizeof(stc_app_value_s));
1459 app_key.pkg_id = g_strdup(app_id);
1460 app_key.app_id = g_strdup(app_id);
1462 app_value.type = STC_APP_TYPE_NONE;
1463 app_value.processes = NULL;
1464 app_value.counter.in_bytes = 0;
1465 app_value.counter.out_bytes = 0;
1467 stc_monitor_application_add(app_key, app_value);
1469 FREE(app_key.pkg_id);
1470 FREE(app_key.app_id);
1473 static int __vconf_get_int(const char *key, int *value)
1477 ret = vconf_get_int(key, value);
1478 if (ret != VCONF_OK) {
1479 STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
1480 return -1; //LCOV_EXCL_LINE
1487 static int __vconf_set_int(const char *key, int value)
1491 ret = vconf_set_int(key, value);
1492 if (ret != VCONF_OK) {
1493 STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
1494 return -1; //LCOV_EXCL_LINE
1500 static guint __get_background_state(void)
1502 return g_system->background_state;;
1505 static void __set_background_state(guint state)
1507 g_system->background_state = state;
1510 static gboolean __processes_tree_foreach_background(gpointer key,
1514 stc_process_key_s *proc_key = (stc_process_key_s *)key;
1515 stc_app_key_s *app_key = (stc_app_key_s *)data;
1517 if (g_system->background_state)
1518 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1520 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1525 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1528 stc_app_key_s *app_key = (stc_app_key_s *)key;
1529 stc_app_value_s *app_value = (stc_app_value_s *)value;
1531 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1532 g_tree_foreach(app_value->processes,
1533 __processes_tree_foreach_background, app_key);
1538 static stc_error_e __process_update_background(void)
1540 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1542 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1544 return STC_ERROR_NONE;
1548 static void __fill_exceptions_list(void)
1550 stc_plugin_fill_exception_list();
1553 stc_error_e stc_monitor_init(void)
1555 stc_system_s *system = MALLOC0(stc_system_s, 1);
1556 GIOChannel *gio = NULL;
1558 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1560 /* initializing current classid */
1561 init_current_classid();
1563 /* initializing cgroups */
1566 /* creating monitored application tree */
1567 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1568 __apps_tree_key_free,
1569 __apps_tree_value_free);
1571 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1572 __rstns_tree_key_free,
1573 __rstns_tree_value_free);
1575 /* create netlink socket for updating kernel counters */
1576 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1577 if (system->contr_sock < 0) {
1578 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
1579 FREE(system); //LCOV_EXCL_LINE
1580 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1583 gio = g_io_channel_unix_new(system->contr_sock);
1584 system->contr_gsource_id =
1585 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1586 (GIOFunc) __process_contr_reply,
1588 g_io_channel_unref(gio);
1592 __add_application_by_interface(STC_TOTAL_DATACALL);
1593 __add_application_by_interface(STC_TOTAL_WIFI);
1594 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1595 __add_application_by_interface(STC_TOTAL_IPV4);
1596 __add_application_by_interface(STC_TOTAL_IPV6);
1598 /* creating restriction rules tree */
1599 __update_contr_cb(NULL);
1601 /* registering periodic kernel counters update callback */
1602 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1605 if (g_system->contr_timer_id == 0) {
1606 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
1607 __close_contr_sock(g_system); //LCOV_EXCL_LINE
1608 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1611 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1612 (int *)&g_system->background_state);
1614 __fill_exceptions_list();
1615 __fill_restritions_list();
1617 return STC_ERROR_NONE;
1620 stc_error_e stc_monitor_deinit(void)
1622 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1624 /* close netlink socket for updating kernel counters */
1625 __close_contr_sock(g_system);
1627 /* remove kernel counters update timer */
1628 if (g_system->contr_timer_id > 0) {
1629 g_source_remove(g_system->contr_timer_id);
1630 g_system->contr_timer_id = 0;
1633 /* destroy monitored application tree */
1634 g_tree_destroy(g_system->apps);
1635 g_system->apps = NULL;
1637 /* destroy restriction rules tree */
1638 g_tree_destroy(g_system->rstns);
1639 g_system->rstns = NULL;
1643 return STC_ERROR_NONE;
1646 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1647 const stc_app_value_s app_value)
1649 stc_error_e ret = STC_ERROR_NONE;
1651 stc_app_value_s *value;
1652 stc_app_value_s *lookup;
1654 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1656 lookup = __application_lookup(g_system->apps, &app_key);
1658 STC_LOGD("app_key already present"); //LCOV_EXCL_LINE
1659 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1662 key = MALLOC0(stc_app_key_s, 1);
1664 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1665 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1668 value = MALLOC0(stc_app_value_s, 1);
1670 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1671 FREE(key); //LCOV_EXCL_LINE
1672 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1675 key->app_id = g_strdup(app_key.app_id);
1676 key->pkg_id = g_strdup(app_key.pkg_id);
1678 value->type = app_value.type;
1679 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1680 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1682 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1683 __processes_tree_key_free,
1684 __processes_tree_value_free);
1686 /* create cgroup and update classid */
1687 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1689 g_tree_insert(g_system->apps, key, value);
1691 /* add nfacct rule for this classid */
1692 __add_application_monitor(key, value, stc_get_default_connection());
1693 __add_rstns_for_application(app_key.app_id);
1698 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1699 const stc_process_key_s proc_key,
1700 const stc_process_value_s proc_value)
1702 stc_error_e ret = STC_ERROR_NONE;
1703 stc_app_value_s *app_lookup;
1704 stc_process_key_s *key;
1705 stc_process_value_s *value;
1706 stc_process_value_s *proc_lookup;
1708 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1710 app_lookup = __application_lookup(g_system->apps, &app_key);
1712 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
1713 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1716 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1718 STC_LOGD("proc_key already present"); //LCOV_EXCL_LINE
1719 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1722 key = MALLOC0(stc_process_key_s, 1);
1724 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1725 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1728 value = MALLOC0(stc_process_value_s, 1);
1730 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1731 FREE(key); //LCOV_EXCL_LINE
1732 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1735 key->pid = proc_key.pid;
1737 value->ground = proc_value.ground;
1739 g_tree_insert(app_lookup->processes, key, value);
1741 /* add pid to application cgroup */
1742 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1745 __apps_tree_printall(); //LCOV_EXCL_LINE
1750 API stc_error_e stc_monitor_process_remove(pid_t pid)
1752 stc_error_e ret = STC_ERROR_NONE;
1753 stc_process_key_s proc_key = {
1757 remove_pid_context_s context = {
1759 .proc_key = &proc_key,
1760 .entry_removed = FALSE,
1763 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1765 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1768 if (context.entry_removed)
1769 __application_remove_if_empty(context.app_key);
1772 __apps_tree_printall(); //LCOV_EXCL_LINE
1778 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1779 const stc_process_key_s proc_key,
1780 stc_app_state_e ground)
1782 stc_error_e ret = STC_ERROR_NONE;
1783 stc_app_value_s *app_lookup;
1784 stc_process_value_s *proc_lookup;
1786 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1788 app_lookup = __application_lookup(g_system->apps, &app_key);
1790 STC_LOGD("app_key not found");
1791 return STC_ERROR_FAIL;
1794 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1796 STC_LOGD("proc_key not found");
1797 return STC_ERROR_FAIL;
1800 if (proc_lookup->ground != ground)
1801 proc_lookup->ground = ground;
1803 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1804 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1806 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1812 void stc_monitor_update_rstn_by_default_connection(void *data)
1814 static default_connection_s old_connection;
1815 default_connection_s *new_connection = (default_connection_s *)data;
1817 if (old_connection.path != NULL) {
1820 g_tree_foreach(g_system->apps,
1821 __remove_application_monitor,
1822 (gpointer)&old_connection);
1824 if (g_system->rstns)
1825 g_tree_foreach(g_system->rstns,
1826 __remove_restriction,
1827 (gpointer)&old_connection);
1829 iptables_flush_chains();
1833 FREE(old_connection.path);
1834 FREE(old_connection.ifname);
1835 old_connection.type = 0;
1836 old_connection.roaming = 0;
1838 if (new_connection != NULL && new_connection->path != NULL) {
1840 g_tree_foreach(g_system->apps,
1841 __add_application_monitor,
1842 (gpointer)new_connection);
1844 if (g_system->rstns)
1845 g_tree_foreach(g_system->rstns, __add_restriction,
1848 old_connection.path = g_strdup(new_connection->path);
1849 old_connection.ifname = g_strdup(new_connection->ifname);
1850 old_connection.type = new_connection->type;
1851 old_connection.roaming = new_connection->roaming;
1855 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1860 stc_rstn_value_s value;
1862 memset(&key, 0, sizeof(stc_rstn_key_s));
1863 memset(&value, 0, sizeof(stc_rstn_value_s));
1865 key.app_id = g_strdup(info->app_id);
1866 key.ifname = g_strdup(info->ifname);
1867 key.subscriber_id = g_strdup(info->subscriber_id);
1868 key.iftype = info->iftype;
1869 key.roaming = info->roaming;
1871 value.rst_state = info->rst_state;
1872 value.restriction_id = info->restriction_id;
1874 if (value.rst_state > STC_RESTRICTION_UNKNOWN &&
1875 value.rst_state < STC_RESTRICTION_LAST_ELEM &&
1877 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1879 value.classid = STC_UNKNOWN_CLASSID;
1881 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1882 __set_background_state(TRUE); //LCOV_EXCL_LINE
1883 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1884 __process_update_background(); //LCOV_EXCL_LINE
1887 value.data_limit = info->data_limit;
1888 value.data_warn_limit = info->data_warn_limit;
1890 ret = __rstn_tree_add(&key, &value, TRUE);
1894 FREE(key.subscriber_id);
1898 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1902 stc_rstn_key_s key = {
1903 .app_id = g_strdup(info->app_id),
1904 .ifname = g_strdup(info->ifname),
1905 .subscriber_id = g_strdup(info->subscriber_id),
1906 .iftype = info->iftype,
1907 .roaming = info->roaming,
1910 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
1911 __set_background_state(FALSE); //LCOV_EXCL_LINE
1912 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1913 __process_update_background(); //LCOV_EXCL_LINE
1916 ret = __rstn_tree_remove(&key);
1920 FREE(key.subscriber_id);
1924 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
1926 return stc_plugin_check_exception_by_cmdline(cmdline);
1929 int stc_monitor_get_counter_socket(void)
1931 return g_system->contr_sock;