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;
260 ret = key_a->roaming - key_b->roaming;
267 static void __rstns_tree_value_free(gpointer data)
269 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
274 static void __rstns_tree_key_free(gpointer data)
276 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
280 FREE(key->subscriber_id);
285 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
288 stc_process_key_s *proc_key = (stc_process_key_s *)key;
289 stc_process_value_s *proc_value = (stc_process_value_s *)value;
291 STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
292 proc_key->pid, proc_value->ground);
296 static void __processes_tree_printall(GTree *processes)
298 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
301 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
304 stc_app_key_s *app_key = (stc_app_key_s *)key;
305 stc_app_value_s *app_value = (stc_app_value_s *)value;
307 STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
308 "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
309 " counter [ in (%lld), out (%lld)]",
310 app_key->pkg_id, app_key->app_id,
311 app_value->type, app_value->classid,
312 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
314 __processes_tree_printall(app_value->processes);
318 static void __apps_tree_printall(void)
320 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
324 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
327 remove_pid_context_s *context = (remove_pid_context_s *)data;
328 stc_app_value_s *app_value = (stc_app_value_s *)value;
330 if (!g_tree_remove(app_value->processes, context->proc_key)) {
332 STC_LOGD("key not found"); //LCOV_EXCL_LINE
336 context->entry_removed = TRUE;
337 context->app_key = (stc_app_key_s *)key;
342 static stc_app_value_s * __application_lookup(GTree *apps,
343 const stc_app_key_s *key)
345 stc_app_value_s *lookup;
347 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
349 lookup = g_tree_lookup(apps, key);
354 static stc_process_value_s * __process_lookup(GTree *processes,
355 const stc_process_key_s *key)
357 stc_process_value_s *lookup;
359 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
361 lookup = g_tree_lookup(processes, key);
367 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
370 guint *pid_count = (guint *)data;
376 static gboolean __add_application_monitor(gpointer key, gpointer value,
379 stc_app_value_s *app_value = (stc_app_value_s *)value;
380 default_connection_s *connection = (default_connection_s *)data;
381 stc_s *stc = stc_get_manager();
383 if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
384 app_value->classid == STC_TOTAL_WIFI_CLASSID ||
385 app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
388 if (stc && connection && connection->ifname) {
389 struct nfacct_rule counter;
392 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
393 if (stc->carg == NULL) //LCOV_EXCL_LINE
394 return FALSE; //LCOV_EXCL_LINE
396 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
399 memset(&counter, 0, sizeof(struct nfacct_rule));
401 counter.carg = stc->carg;
402 counter.classid = app_value->classid;
403 counter.intend = NFACCT_COUNTER;
404 counter.iftype = connection->type;
405 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
407 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
408 __add_iptables_in(&counter);
409 __add_iptables_out(&counter);
410 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
411 __add_ip6tables_in(&counter);
412 __add_ip6tables_out(&counter);
414 __add_iptables_in(&counter);
415 __add_iptables_out(&counter);
416 __add_ip6tables_in(&counter);
417 __add_ip6tables_out(&counter);
424 static gboolean __remove_application_monitor(gpointer key, gpointer value,
427 stc_app_value_s *app_value = (stc_app_value_s *)value;
428 default_connection_s *connection = (default_connection_s *)data;
429 stc_s *stc = stc_get_manager();
431 if (stc && connection && connection->ifname) {
432 struct nfacct_rule counter;
435 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
436 if (stc->carg == NULL) //LCOV_EXCL_LINE
437 return FALSE; //LCOV_EXCL_LINE
439 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
442 memset(&counter, 0, sizeof(struct nfacct_rule));
444 counter.carg = stc->carg;
445 counter.classid = app_value->classid;
446 counter.intend = NFACCT_COUNTER;
447 counter.iftype = connection->type;
448 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
450 __del_iptables_in(&counter);
451 __del_iptables_out(&counter);
452 __del_ip6tables_in(&counter);
453 __del_ip6tables_out(&counter);
459 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
461 STC_LOGI("rstn info => rstn_id [%llu], "
462 "app_id [%s], classid [%lu], ifname [%s], "
463 "iftype [%d], rst_state [%d], "
464 "limit [ (%lld) bytes], "
465 "warn_limit [ (%lld) bytes], "
466 "counter [ (%lld) bytes], "
467 "roaming [%d], subscriber_id [%s]",
468 rstn_value->restriction_id,
469 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
470 rstn_key->iftype, rstn_value->rst_state,
471 rstn_value->data_limit,
472 rstn_value->data_warn_limit,
473 rstn_value->data_counter,
474 rstn_key->roaming, rstn_key->subscriber_id);
477 static void __process_restriction(enum traffic_restriction_type rst_type,
478 stc_rstn_key_s *rstn_key,
479 stc_rstn_value_s *rstn_value, void *data)
481 int64_t effective_data_limit, effective_data_warn_limit;
482 default_connection_s *old_connection = (default_connection_s *)data;
483 default_connection_s *connection = NULL;
485 if (old_connection != NULL)
486 connection = old_connection;
488 connection = stc_get_default_connection();
490 /* no default ifname */
491 if (connection->ifname == NULL)
494 /* rstn not applicable for this interface */
495 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
496 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
499 /* classid is invalid */
500 if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
503 effective_data_limit = rstn_value->data_limit;
504 effective_data_warn_limit = rstn_value->data_warn_limit;
506 if (rst_type == RST_SET) {
507 /* TODO: Change this to runtime memory */
508 table_counters_info info;
510 memset(&info, 0, sizeof(table_counters_info));
511 table_counters_get(rstn_value->restriction_id, &info);
513 effective_data_limit -= info.data_counter;
514 effective_data_warn_limit -= info.data_counter;
516 if (effective_data_limit < 0) {
517 effective_data_limit = 0; //LCOV_EXCL_LINE
518 rstn_value->data_limit_reached = TRUE; //LCOV_EXCL_LINE
521 if (effective_data_warn_limit < 0)
522 effective_data_warn_limit = 0; //LCOV_EXCL_LINE
524 STC_LOGD("datausage [%lld] bytes", info.data_counter);
527 STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
528 "effective_data_warn_limit [%lld] bytes",
529 rstn_value->restriction_id, effective_data_limit,
530 effective_data_warn_limit);
534 if (effective_data_limit <= 0) {
535 char *default_ifname = stc_default_connection_get_ifname();
536 struct nfacct_rule counter;
537 stc_s *stc = stc_get_manager();
539 g_free(default_ifname); //LCOV_EXCL_LINE
540 return; //LCOV_EXCL_LINE
544 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
545 if (stc->carg == NULL) { //LCOV_EXCL_LINE
546 g_free(default_ifname); //LCOV_EXCL_LINE
547 return; //LCOV_EXCL_LINE
550 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
553 counter.carg = stc->carg;
554 counter.classid = rstn_value->classid;
555 counter.intend = NFACCT_BLOCK;
556 counter.iftype = rstn_key->iftype;
557 g_strlcpy(counter.ifname, default_ifname,
559 g_free(default_ifname);
562 __add_iptables_in(&counter);
563 __add_iptables_out(&counter);
566 __add_ip6tables_in(&counter);
567 __add_ip6tables_out(&counter);
570 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
571 rstn_value->data_limit_reached = FALSE;
578 char *default_ifname = stc_default_connection_get_ifname();
579 struct nfacct_rule counter;
580 stc_s *stc = stc_get_manager();
582 g_free(default_ifname); //LCOV_EXCL_LINE
583 return; //LCOV_EXCL_LINE
587 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
588 if (stc->carg == NULL) { //LCOV_EXCL_LINE
589 g_free(default_ifname); //LCOV_EXCL_LINE
590 return; //LCOV_EXCL_LINE
593 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
596 counter.carg = stc->carg;
597 counter.classid = rstn_value->classid;
598 counter.intend = NFACCT_BLOCK;
599 counter.iftype = rstn_key->iftype;
600 g_strlcpy(counter.ifname, default_ifname,
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_plugin_appstatus_send_warn_message("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_plugin_appstatus_send_restriction_message("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 */
1217 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1220 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1221 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1223 __print_rstn(rstn_key, rstn_value);
1227 static void __rstn_tree_printall(void)
1229 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1233 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1234 const stc_rstn_key_s *key)
1236 stc_rstn_value_s *lookup;
1238 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1240 lookup = g_tree_lookup(rstns_tree, key);
1245 static gboolean __remove_restriction(gpointer key, gpointer value,
1248 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1249 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1251 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1252 __print_rstn(rstn_key, rstn_value);
1256 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1259 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1260 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1262 /* rstn rule is activated */
1263 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1266 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1267 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1269 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1271 __print_rstn(rstn_key, rstn_value);
1277 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1279 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1280 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1282 /* rstn rule is activated */
1283 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1286 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1287 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1289 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1295 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1297 stc_rstn_value_s *lookup_value;
1299 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1301 lookup_value = __rstn_lookup(g_system->rstns, key);
1302 if (!lookup_value) {
1303 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1304 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1307 __remove_restriction(key, lookup_value, NULL);
1309 /* remove counter also */
1310 table_counters_delete(lookup_value->restriction_id);
1312 if (!g_tree_remove(g_system->rstns, key)) {
1313 STC_LOGD("key not found"); //LCOV_EXCL_LINE
1314 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1317 return STC_ERROR_NONE;
1320 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1321 stc_rstn_value_s *value, gboolean debug)
1323 stc_rstn_value_s *rstn_value;
1325 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1327 rstn_value = __rstn_lookup(g_system->rstns, key);
1329 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1331 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
1332 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1335 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1337 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
1338 FREE(rstn_key); //LCOV_EXCL_LINE
1339 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1342 rstn_key->app_id = g_strdup(key->app_id);
1343 rstn_key->ifname = g_strdup(key->ifname);
1344 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1345 rstn_key->iftype = key->iftype;
1346 rstn_key->roaming = key->roaming;
1348 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1351 rstn_value->restriction_id = value->restriction_id;
1352 rstn_value->rst_state = value->rst_state;
1353 rstn_value->rst_type = value->rst_type;
1354 rstn_value->classid = value->classid;
1355 rstn_value->data_limit = value->data_limit;
1356 rstn_value->data_warn_limit = value->data_warn_limit;
1357 rstn_value->data_counter = 0;
1358 rstn_value->warn_limit_crossed_notified = FALSE;
1359 rstn_value->rstn_limit_crossed_notified = FALSE;
1362 __add_restriction_debug(key, rstn_value, NULL);
1364 __add_restriction(key, rstn_value, NULL);
1366 return STC_ERROR_NONE;
1370 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1373 stc_cb_ret_e ret = STC_CONTINUE;
1376 stc_rstn_value_s value;
1378 memset(&key, 0, sizeof(stc_rstn_key_s));
1379 memset(&value, 0, sizeof(stc_rstn_value_s));
1381 key.app_id = g_strdup(info->app_id);
1382 key.ifname = g_strdup(info->ifname);
1383 key.subscriber_id = g_strdup(info->subscriber_id);
1384 key.iftype = info->iftype;
1385 key.roaming = info->roaming;
1387 value.rst_type = info->rst_type;
1388 value.rst_state = STC_RESTRICTION_UNKNOWN;
1389 value.restriction_id = info->restriction_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);
1413 __rstn_tree_printall();
1416 static gboolean __add_rstn_foreach_application(gpointer key,
1420 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1421 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1422 gchar *app_id = (gchar *)data;
1424 /* rstn rule is not for applications */
1425 if (rstn_key->app_id == NULL)
1428 /* rstn rule is not for this application */
1429 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1432 /* rstn rule is already applied */
1433 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1436 /* add restriction to system */
1437 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1438 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
1440 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
1442 __print_rstn(rstn_key, rstn_value);
1448 static void __add_rstns_for_application(gchar *app_id)
1450 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1454 static void __add_application_by_interface(const char *app_id)
1456 stc_app_key_s app_key;
1457 stc_app_value_s app_value;
1460 return; //LCOV_EXCL_LINE
1462 memset(&app_key, 0, sizeof(stc_app_key_s));
1463 memset(&app_value, 0, sizeof(stc_app_value_s));
1465 app_key.pkg_id = g_strdup(app_id);
1466 app_key.app_id = g_strdup(app_id);
1468 app_value.type = STC_APP_TYPE_NONE;
1469 app_value.processes = NULL;
1470 app_value.counter.in_bytes = 0;
1471 app_value.counter.out_bytes = 0;
1473 stc_monitor_application_add(app_key, app_value);
1475 FREE(app_key.pkg_id);
1476 FREE(app_key.app_id);
1479 static int __vconf_get_int(const char *key, int *value)
1483 ret = vconf_get_int(key, value);
1484 if (ret != VCONF_OK) {
1485 STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
1486 return -1; //LCOV_EXCL_LINE
1493 static int __vconf_set_int(const char *key, int value)
1497 ret = vconf_set_int(key, value);
1498 if (ret != VCONF_OK) {
1499 STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
1500 return -1; //LCOV_EXCL_LINE
1506 static guint __get_background_state(void)
1508 return g_system->background_state;;
1511 static void __set_background_state(guint state)
1513 g_system->background_state = state;
1516 static gboolean __processes_tree_foreach_background(gpointer key,
1520 stc_process_key_s *proc_key = (stc_process_key_s *)key;
1521 stc_app_key_s *app_key = (stc_app_key_s *)data;
1523 if (g_system->background_state)
1524 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1526 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1531 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1534 stc_app_key_s *app_key = (stc_app_key_s *)key;
1535 stc_app_value_s *app_value = (stc_app_value_s *)value;
1537 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1538 g_tree_foreach(app_value->processes,
1539 __processes_tree_foreach_background, app_key);
1544 static stc_error_e __process_update_background(void)
1546 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1548 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1550 return STC_ERROR_NONE;
1554 static void __fill_exceptions_list(void)
1556 stc_plugin_fill_exception_list();
1559 stc_error_e stc_monitor_init(void)
1561 stc_system_s *system = MALLOC0(stc_system_s, 1);
1562 GIOChannel *gio = NULL;
1564 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1566 /* initializing current classid */
1567 init_current_classid();
1569 /* initializing cgroups */
1572 /* creating monitored application tree */
1573 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1574 __apps_tree_key_free,
1575 __apps_tree_value_free);
1577 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1578 __rstns_tree_key_free,
1579 __rstns_tree_value_free);
1581 /* create netlink socket for updating kernel counters */
1582 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1583 if (system->contr_sock < 0) {
1584 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
1585 FREE(system); //LCOV_EXCL_LINE
1586 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1589 gio = g_io_channel_unix_new(system->contr_sock);
1590 system->contr_gsource_id =
1591 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1592 (GIOFunc) __process_contr_reply,
1594 g_io_channel_unref(gio);
1598 __add_application_by_interface(STC_TOTAL_DATACALL);
1599 __add_application_by_interface(STC_TOTAL_WIFI);
1600 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1601 __add_application_by_interface(STC_TOTAL_IPV4);
1602 __add_application_by_interface(STC_TOTAL_IPV6);
1604 /* creating restriction rules tree */
1605 __update_contr_cb(NULL);
1607 /* registering periodic kernel counters update callback */
1608 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1611 if (g_system->contr_timer_id == 0) {
1612 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
1613 __close_contr_sock(g_system); //LCOV_EXCL_LINE
1614 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1617 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1618 (int *)&g_system->background_state);
1620 __fill_exceptions_list();
1621 __fill_restritions_list();
1623 return STC_ERROR_NONE;
1626 stc_error_e stc_monitor_deinit(void)
1628 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1630 /* close netlink socket for updating kernel counters */
1631 __close_contr_sock(g_system);
1633 /* remove kernel counters update timer */
1634 if (g_system->contr_timer_id > 0) {
1635 g_source_remove(g_system->contr_timer_id);
1636 g_system->contr_timer_id = 0;
1639 /* destroy monitored application tree */
1640 g_tree_destroy(g_system->apps);
1641 g_system->apps = NULL;
1643 /* destroy restriction rules tree */
1644 g_tree_destroy(g_system->rstns);
1645 g_system->rstns = NULL;
1649 return STC_ERROR_NONE;
1652 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1653 const stc_app_value_s app_value)
1655 stc_error_e ret = STC_ERROR_NONE;
1657 stc_app_value_s *value;
1658 stc_app_value_s *lookup;
1660 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1662 lookup = __application_lookup(g_system->apps, &app_key);
1664 STC_LOGD("app_key already present"); //LCOV_EXCL_LINE
1665 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1668 key = MALLOC0(stc_app_key_s, 1);
1670 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1671 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1674 value = MALLOC0(stc_app_value_s, 1);
1676 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1677 FREE(key); //LCOV_EXCL_LINE
1678 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1681 key->app_id = g_strdup(app_key.app_id);
1682 key->pkg_id = g_strdup(app_key.pkg_id);
1684 value->type = app_value.type;
1685 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1686 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1688 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1689 __processes_tree_key_free,
1690 __processes_tree_value_free);
1692 /* create cgroup and update classid */
1693 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1695 g_tree_insert(g_system->apps, key, value);
1697 /* add nfacct rule for this classid */
1698 __add_application_monitor(key, value, stc_get_default_connection());
1699 __add_rstns_for_application(app_key.app_id);
1704 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1705 const stc_process_key_s proc_key,
1706 const stc_process_value_s proc_value)
1708 stc_error_e ret = STC_ERROR_NONE;
1709 stc_app_value_s *app_lookup;
1710 stc_process_key_s *key;
1711 stc_process_value_s *value;
1712 stc_process_value_s *proc_lookup;
1714 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1716 app_lookup = __application_lookup(g_system->apps, &app_key);
1718 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
1719 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1722 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1724 STC_LOGD("proc_key already present"); //LCOV_EXCL_LINE
1725 return STC_ERROR_NONE; //LCOV_EXCL_LINE
1728 key = MALLOC0(stc_process_key_s, 1);
1730 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
1731 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1734 value = MALLOC0(stc_process_value_s, 1);
1736 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
1737 FREE(key); //LCOV_EXCL_LINE
1738 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1741 key->pid = proc_key.pid;
1743 value->ground = proc_value.ground;
1745 g_tree_insert(app_lookup->processes, key, value);
1747 /* add pid to application cgroup */
1748 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1751 __apps_tree_printall(); //LCOV_EXCL_LINE
1756 API stc_error_e stc_monitor_process_remove(pid_t pid)
1758 stc_error_e ret = STC_ERROR_NONE;
1759 stc_process_key_s proc_key = {
1763 remove_pid_context_s context = {
1765 .proc_key = &proc_key,
1766 .entry_removed = FALSE,
1769 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1771 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1774 if (context.entry_removed)
1775 __application_remove_if_empty(context.app_key);
1778 __apps_tree_printall(); //LCOV_EXCL_LINE
1784 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1785 const stc_process_key_s proc_key,
1786 stc_app_state_e ground)
1788 stc_error_e ret = STC_ERROR_NONE;
1789 stc_app_value_s *app_lookup;
1790 stc_process_value_s *proc_lookup;
1792 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1794 app_lookup = __application_lookup(g_system->apps, &app_key);
1796 STC_LOGD("app_key not found");
1797 return STC_ERROR_FAIL;
1800 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1802 STC_LOGD("proc_key not found");
1803 return STC_ERROR_FAIL;
1806 if (proc_lookup->ground != ground)
1807 proc_lookup->ground = ground;
1809 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1810 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1812 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1818 void stc_monitor_update_rstn_by_default_connection(void *data)
1820 static default_connection_s old_connection;
1821 default_connection_s *new_connection = (default_connection_s *)data;
1823 if (old_connection.path != NULL) {
1826 g_tree_foreach(g_system->apps,
1827 __remove_application_monitor,
1828 (gpointer)&old_connection);
1830 if (g_system->rstns)
1831 g_tree_foreach(g_system->rstns,
1832 __remove_restriction,
1833 (gpointer)&old_connection);
1835 iptables_flush_chains();
1839 FREE(old_connection.path);
1840 FREE(old_connection.ifname);
1841 old_connection.type = 0;
1842 old_connection.roaming = 0;
1844 if (new_connection != NULL && new_connection->path != NULL) {
1846 g_tree_foreach(g_system->apps,
1847 __add_application_monitor,
1848 (gpointer)new_connection);
1850 if (g_system->rstns)
1851 g_tree_foreach(g_system->rstns, __add_restriction,
1854 old_connection.path = g_strdup(new_connection->path);
1855 old_connection.ifname = g_strdup(new_connection->ifname);
1856 old_connection.type = new_connection->type;
1857 old_connection.roaming = new_connection->roaming;
1861 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1866 stc_rstn_value_s value;
1868 memset(&key, 0, sizeof(stc_rstn_key_s));
1869 memset(&value, 0, sizeof(stc_rstn_value_s));
1871 key.app_id = g_strdup(info->app_id);
1872 key.ifname = g_strdup(info->ifname);
1873 key.subscriber_id = g_strdup(info->subscriber_id);
1874 key.iftype = info->iftype;
1875 key.roaming = info->roaming;
1877 value.rst_type = info->rst_type;
1878 value.rst_state = STC_RESTRICTION_UNKNOWN;
1879 value.restriction_id = info->restriction_id;
1882 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1884 value.classid = STC_UNKNOWN_CLASSID;
1886 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1887 __set_background_state(TRUE); //LCOV_EXCL_LINE
1888 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1889 __process_update_background(); //LCOV_EXCL_LINE
1892 value.data_limit = info->data_limit;
1893 value.data_warn_limit = info->data_warn_limit;
1895 ret = __rstn_tree_add(&key, &value, TRUE);
1899 FREE(key.subscriber_id);
1903 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1907 stc_rstn_key_s key = {
1908 .app_id = g_strdup(info->app_id),
1909 .ifname = g_strdup(info->ifname),
1910 .subscriber_id = g_strdup(info->subscriber_id),
1911 .iftype = info->iftype,
1912 .roaming = info->roaming,
1915 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
1916 __set_background_state(FALSE); //LCOV_EXCL_LINE
1917 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
1918 __process_update_background(); //LCOV_EXCL_LINE
1921 ret = __rstn_tree_remove(&key);
1925 FREE(key.subscriber_id);
1929 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
1931 return stc_plugin_check_exception_by_cmdline(cmdline);
1934 int stc_monitor_get_counter_socket(void)
1936 return g_system->contr_sock;