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 "stc-default-connection.h"
20 #include "helper-nl.h"
21 #include "helper-nfacct-rule.h"
22 #include "helper-net-cls.h"
23 #include "helper-cgroup.h"
25 #include "table-statistics.h"
26 #include "table-counters.h"
27 #include "stc-monitor.h"
28 #include "stc-manager-plugin.h"
30 #define MAX_INT_LENGTH 128
33 stc_app_key_s *app_key;
34 stc_process_key_s *proc_key;
35 gboolean entry_removed;
36 } remove_pid_context_s;
39 struct nfacct_rule *counter;
41 gboolean data_limit_reached;
42 } classid_bytes_context_s;
44 static stc_system_s *g_system = NULL;
46 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
48 if (counter->intend == NFACCT_WARN)
49 return NFACCT_JUMP_ACCEPT;
50 else if (counter->intend == NFACCT_BLOCK)
51 return NFACCT_JUMP_REJECT;
53 return NFACCT_JUMP_UNKNOWN;
56 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
58 return produce_net_rule(counter, 0, 0,
59 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
63 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
65 return produce_net_rule(counter, 0, 0,
66 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
70 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
72 return produce_net_rule(counter, 0, 0,
73 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
77 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
79 return produce_net_rule(counter, 0, 0,
80 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
84 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
85 gpointer UNUSED user_data)
87 stc_process_key_s *key_a = (stc_process_key_s *)a;
88 stc_process_key_s *key_b = (stc_process_key_s *)b;
90 return key_a->pid - key_b->pid;
93 static void __processes_tree_value_free(gpointer data)
95 stc_process_value_s *value = (stc_process_value_s *)data;
100 static void __processes_tree_key_free(gpointer data)
102 stc_process_key_s *key = (stc_process_key_s *)data;
107 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
108 gpointer UNUSED user_data)
110 stc_app_key_s *key_a = (stc_app_key_s *)a;
111 stc_app_key_s *key_b = (stc_app_key_s *)b;
114 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
118 return g_strcmp0(key_a->app_id, key_b->app_id);
121 static void __apps_tree_value_free(gpointer data)
123 stc_app_value_s *value = (stc_app_value_s *)data;
125 g_tree_destroy(value->processes);
126 value->processes = NULL;
131 static void __apps_tree_key_free(gpointer data)
133 stc_app_key_s *key = (stc_app_key_s *)data;
140 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
141 gpointer UNUSED user_data)
143 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
144 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
147 ret = g_strcmp0(key_a->app_id, key_b->app_id);
151 ret = g_strcmp0(key_a->ifname, key_b->ifname);
155 ret = g_strcmp0(key_a->imsi, key_b->imsi);
159 ret = key_a->iftype - key_b->iftype;
166 static void __rstns_tree_value_free(gpointer data)
168 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
173 static void __rstns_tree_key_free(gpointer data)
175 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
183 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
186 stc_process_key_s *proc_key = (stc_process_key_s *)key;
187 stc_process_value_s *proc_value = (stc_process_value_s *)value;
189 STC_LOGD("Process entry => PID [%d], Ground state [%d]",
190 proc_key->pid, proc_value->ground);
194 static void __processes_tree_printall(GTree *processes)
196 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
199 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
202 stc_app_key_s *app_key = (stc_app_key_s *)key;
203 stc_app_value_s *app_value = (stc_app_value_s *)value;
205 STC_LOGD("Application info => Pkg ID [%s], App ID [%s],"
206 " Type [%d], classid [%d],"
207 " counter [ in (%lld), out (%lld)]",
208 app_key->pkg_id, app_key->app_id,
209 app_value->type, app_value->classid,
210 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
212 __processes_tree_printall(app_value->processes);
217 static void __apps_tree_printall(void)
219 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
223 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
226 remove_pid_context_s *context = (remove_pid_context_s *)data;
227 stc_app_value_s *app_value = (stc_app_value_s *)value;
229 if (!g_tree_remove(app_value->processes, context->proc_key)) {
230 STC_LOGD("key not found");
234 context->entry_removed = TRUE;
235 context->app_key = (stc_app_key_s *)key;
240 static stc_app_value_s * __application_lookup(GTree *apps,
241 const stc_app_key_s *key)
243 stc_app_value_s *lookup;
245 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
247 lookup = g_tree_lookup(apps, key);
252 static stc_process_value_s * __process_lookup(GTree *processes,
253 const stc_process_key_s *key)
255 stc_process_value_s *lookup;
257 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
259 lookup = g_tree_lookup(processes, key);
264 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
267 guint *pid_count = (guint *)data;
272 static gboolean __add_application_monitor(gpointer key, gpointer value,
275 stc_app_value_s *app_value = (stc_app_value_s *)value;
276 default_connection_s *connection = (default_connection_s *)data;
277 stc_s *stc = stc_get_manager();
279 if (stc && connection && connection->ifname) {
280 struct nfacct_rule counter;
283 stc->carg = MALLOC0(counter_arg_s, 1);
284 stc->carg->sock = stc_monitor_get_counter_socket();
287 memset(&counter, 0, sizeof(struct nfacct_rule));
289 counter.carg = stc->carg;
290 counter.classid = app_value->classid;
291 counter.intend = NFACCT_COUNTER;
292 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
294 __add_iptables_in(&counter);
295 __add_iptables_out(&counter);
301 static gboolean __remove_application_monitor(gpointer key, gpointer value,
304 stc_app_value_s *app_value = (stc_app_value_s *)value;
305 default_connection_s *connection = (default_connection_s *)data;
306 stc_s *stc = stc_get_manager();
308 if (stc && connection && connection->ifname) {
309 struct nfacct_rule counter;
312 stc->carg = MALLOC0(counter_arg_s, 1);
313 stc->carg->sock = stc_monitor_get_counter_socket();
316 memset(&counter, 0, sizeof(struct nfacct_rule));
318 counter.carg = stc->carg;
319 counter.classid = app_value->classid;
320 counter.intend = NFACCT_COUNTER;
321 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
323 __del_iptables_in(&counter);
324 __del_iptables_out(&counter);
330 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
332 STC_LOGI("rstn info => rstn_id [%llu], "
333 "app_id [%s], classid [%lu], ifname [%s], "
334 "iftype [%d], rst_state [%d], "
335 "limit [ (%lld) bytes], "
336 "warn_limit [ (%lld) bytes], "
337 "counter [ (%lld) bytes], "
338 "roaming [%d], imsi [%s]",
339 rstn_value->restriction_id,
340 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
341 rstn_key->iftype, rstn_value->rst_state,
342 rstn_value->data_limit,
343 rstn_value->data_warn_limit,
344 rstn_value->data_counter,
345 rstn_key->roaming, rstn_key->imsi);
348 static void __process_restriction(enum traffic_restriction_type rst_type,
349 stc_rstn_key_s *rstn_key,
350 stc_rstn_value_s *rstn_value, void *data)
352 int64_t effective_data_limit, effective_data_warn_limit;
353 default_connection_s *old_connection = (default_connection_s *)data;
354 default_connection_s *connection = NULL;
356 if (old_connection != NULL)
357 connection = old_connection;
359 connection = stc_get_default_connection();
361 /* no default ifname */
362 if (connection->ifname == NULL)
365 /* rstn not applicable for this interface */
366 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
367 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
370 /* classid is invalid */
371 if (rstn_value->classid == STC_UNKNOWN_CLASSID)
374 effective_data_limit = rstn_value->data_limit;
375 effective_data_warn_limit = rstn_value->data_warn_limit;
377 if (rst_type == RST_SET) {
378 /* TODO: Change this to runtime memory */
379 table_counters_info info;
381 memset(&info, 0, sizeof(table_counters_info));
382 table_counters_get(rstn_value->restriction_id, &info);
384 effective_data_limit -= info.data_counter;
385 effective_data_warn_limit -= info.data_counter;
387 if (effective_data_limit < 0) {
388 effective_data_limit = 0;
389 rstn_value->data_limit_reached = TRUE;
392 if (effective_data_warn_limit < 0)
393 effective_data_warn_limit = 0;
395 STC_LOGD("datausage [%lld] bytes", info.data_counter);
398 STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
399 "effective_data_warn_limit [%lld] bytes",
400 rstn_value->restriction_id, effective_data_limit,
401 effective_data_warn_limit);
405 if (effective_data_limit <= 0) {
406 char *default_ifname = stc_default_connection_get_ifname();
407 struct nfacct_rule counter;
408 stc_s *stc = stc_get_manager();
411 stc->carg = MALLOC0(counter_arg_s, 1);
413 stc_monitor_get_counter_socket();
416 counter.carg = stc->carg;
417 counter.classid = rstn_value->classid;
418 counter.intend = NFACCT_BLOCK;
419 counter.iftype = STC_IFACE_UNKNOWN;
420 g_strlcpy(counter.ifname, default_ifname,
423 generate_counter_name(&counter);
424 g_free(default_ifname);
427 __add_iptables_in(&counter);
428 __add_iptables_out(&counter);
431 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
432 rstn_value->data_limit_reached = FALSE;
439 char *default_ifname = stc_default_connection_get_ifname();
440 struct nfacct_rule counter;
441 stc_s *stc = stc_get_manager();
444 stc->carg = MALLOC0(counter_arg_s, 1);
446 stc_monitor_get_counter_socket();
449 counter.carg = stc->carg;
450 counter.classid = rstn_value->classid;
451 counter.intend = NFACCT_BLOCK;
452 counter.iftype = STC_IFACE_UNKNOWN;
453 g_strlcpy(counter.ifname, default_ifname,
456 generate_counter_name(&counter);
457 g_free(default_ifname);
460 __del_iptables_in(&counter);
461 __del_iptables_out(&counter);
463 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
464 rstn_value->data_limit_reached = FALSE;
472 static gboolean __remove_rstns_foreach_application(gpointer key,
476 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
477 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
478 gchar *app_id = (gchar *)data;
480 /* rstn rule is not for applications */
481 if (rstn_key->app_id == NULL)
484 /* rstn rule is not for this application */
485 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
488 /* rstn rule is already removed */
489 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
492 /* remove restriction from system */
493 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
495 __print_rstn(rstn_key, rstn_value);
500 static void __remove_rstns_for_application(gchar *app_id)
502 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
506 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
508 stc_error_e ret = STC_ERROR_NONE;
510 stc_app_value_s *lookup;
512 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
514 lookup = __application_lookup(g_system->apps, app_key);
516 STC_LOGE("app_key not found");
517 return STC_ERROR_NO_DATA;
520 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
524 /* remove nfacct rule for this classid */
525 __remove_application_monitor((gpointer) app_key, lookup,
526 stc_get_default_connection());
527 __remove_rstns_for_application(app_key->app_id);
530 if (!g_tree_remove(g_system->apps, app_key)) {
531 ret = STC_ERROR_NO_DATA;
532 STC_LOGE("key not found");
538 static stc_error_e __close_contr_sock(stc_system_s *system)
540 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
542 /* close netlink socket for updating kernel counters */
543 if (g_system->contr_sock != -1) {
544 close(g_system->contr_sock);
545 g_system->contr_sock = -1;
548 if (g_system->contr_gsource_id != 0) {
549 g_source_remove(g_system->contr_gsource_id);
550 g_system->contr_gsource_id = 0;
553 return STC_ERROR_NONE;
556 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
560 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
561 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
562 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
564 if (context->counter->intend != NFACCT_COUNTER)
565 goto try_next_callback;
567 if (rstn_value->classid != context->counter->classid)
568 goto try_next_callback;
570 if (rstn_value->data_limit_reached == TRUE) {
571 context->data_limit_reached = TRUE;
572 goto try_next_callback;
575 switch (context->counter->iotype) {
576 case NFACCT_COUNTER_IN:
577 case NFACCT_COUNTER_OUT:
578 rstn_value->data_counter += context->bytes;
580 if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
581 rstn_value->warn_limit_crossed_notified == FALSE) {
584 char iftype[MAX_INT_LENGTH];
585 char byte[MAX_INT_LENGTH];
586 stc_s *stc = (stc_s *)stc_get_manager();
587 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
590 rv = stc_manager_dbus_emit_signal(stc->connection,
591 STC_DBUS_SERVICE_RESTRICTION_PATH,
592 STC_DBUS_INTERFACE_RESTRICTION,
593 "WarnThresholdCrossed",
594 g_variant_new("(s)", rstn_key->app_id));
596 rstn_value->warn_limit_crossed_notified = TRUE;
598 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
599 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
600 stc_send_warn_message_to_net_popup("warn threshold crossed",
606 if (rstn_value->data_counter >= rstn_value->data_limit &&
607 rstn_value->rstn_limit_crossed_notified == FALSE) {
610 char iftype[MAX_INT_LENGTH];
611 char byte[MAX_INT_LENGTH];
612 stc_s *stc = (stc_s *)stc_get_manager();
613 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
615 /* block immediately */
616 context->counter->intend = NFACCT_BLOCK;
617 __del_iptables_in(context->counter);
618 __del_iptables_out(context->counter);
619 __add_iptables_in(context->counter);
620 __add_iptables_out(context->counter);
621 context->counter->intend = NFACCT_COUNTER;
623 rstn_value->data_limit_reached = TRUE;
626 rv = stc_manager_dbus_emit_signal(stc->connection,
627 STC_DBUS_SERVICE_RESTRICTION_PATH,
628 STC_DBUS_INTERFACE_RESTRICTION,
629 "RestrictionThresholdCrossed",
630 g_variant_new("(s)", rstn_key->app_id));
632 rstn_value->rstn_limit_crossed_notified = TRUE;
634 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
635 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
636 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
637 "restriction_noti", rstn_key->app_id,
641 g_system->rstns_tree_updated = TRUE;
642 __print_rstn(rstn_key, rstn_value);
645 STC_LOGE("unknown iotype");
653 static gboolean __update_app_statistics(gpointer key, gpointer value,
656 stc_app_key_s *app_key = (stc_app_key_s *)key;
657 stc_app_value_s *app_value = (stc_app_value_s *)value;
658 time_t *touch_time = (time_t *)data;
659 stc_db_classid_iftype_key stat_key;
660 stc_db_app_stats stat;
661 char *default_ifname = stc_default_connection_get_ifname();
663 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
664 memset(&stat, 0 , sizeof(stc_db_app_stats));
666 stat_key.classid = app_value->classid;
667 stat_key.iftype = stc_default_connection_get_type();
668 if (STC_IFACE_DATACALL == stat_key.iftype)
669 stat_key.imsi = g_strdup("unknown");
671 stat_key.imsi = g_strdup("noneimsi");
672 g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH);
674 stat.app_id = g_strdup(app_key->app_id);
675 stat.snd_count = app_value->counter.out_bytes;
676 stat.rcv_count = app_value->counter.in_bytes;
677 stat.is_roaming = stc_default_connection_get_roaming();
678 stat.ground = STC_APP_STATE_UNKNOWN;
680 table_statistics_insert(&stat_key, &stat, *touch_time);
682 app_value->counter.out_bytes = 0;
683 app_value->counter.in_bytes = 0;
687 FREE(default_ifname);
692 static gboolean __flush_apps_stats_to_database(gpointer user_data)
694 time_t current_time = time(0);
696 if (g_system->apps_tree_updated == FALSE)
697 return G_SOURCE_REMOVE;
699 g_system->apps_tree_updated = FALSE;
702 g_tree_foreach(g_system->apps,
703 __update_app_statistics,
706 STC_LOGI("Flushed app stats to database");
707 return G_SOURCE_REMOVE;
710 static gboolean __update_counter_statistics(gpointer key, gpointer value,
713 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
714 table_counters_info info = {
715 .restriction_id = rstn_value->restriction_id,
716 .data_counter = rstn_value->data_counter
719 table_counters_update_counters(&info);
724 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
726 time_t current_time = time(0);
728 if (g_system->rstns_tree_updated == FALSE)
729 return G_SOURCE_REMOVE;
731 g_system->rstns_tree_updated = FALSE;
734 g_tree_foreach(g_system->rstns,
735 __update_counter_statistics,
738 STC_LOGI("Flushed rstns counters to database");
739 return G_SOURCE_REMOVE;
742 static gboolean __apps_counter_update_foreach_classid(gpointer key,
746 stc_app_key_s *app_key = (stc_app_key_s *)key;
747 stc_app_value_s *app_value = (stc_app_value_s *)value;
748 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
750 if (context->counter->intend != NFACCT_COUNTER)
751 goto try_next_callback;
753 if (app_value->classid != context->counter->classid)
754 goto try_next_callback;
756 switch (context->counter->iotype) {
757 case NFACCT_COUNTER_IN:
758 app_value->data_usage.in_bytes += context->bytes;
759 app_value->counter.in_bytes = context->bytes;
760 g_system->apps_tree_updated = TRUE;
762 __apps_tree_foreach_print(app_key, app_value, NULL);
764 case NFACCT_COUNTER_OUT:
765 app_value->data_usage.out_bytes += context->bytes;
766 app_value->counter.out_bytes = context->bytes;
767 g_system->apps_tree_updated = TRUE;
769 __apps_tree_foreach_print(app_key, app_value, NULL);
772 STC_LOGE("unknown iotype");
779 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
780 struct counter_arg *carg)
782 struct nfacct_rule counter = {
789 classid_bytes_context_s context = {
792 .data_limit_reached = FALSE,
795 STC_LOGD("cnt_name %s", cnt_name);
797 if (!recreate_counter_by_name(cnt_name, &counter)) {
798 STC_LOGE("Can't parse counter name %s", cnt_name);
802 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
803 context.counter->classid, context.counter->iftype,
804 context.counter->iotype, context.counter->intend,
805 context.counter->ifname, context.bytes);
808 g_tree_foreach(g_system->rstns,
809 __rstn_counter_update_foreach_classid,
813 g_tree_foreach(g_system->apps,
814 __apps_counter_update_foreach_classid,
818 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
821 struct counter_arg *carg = user_data;
822 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
823 if (carg->initiate) {
825 * TODO: this will be used when daemon starts to update existing
826 * counter data if present.
828 populate_counters(cnt_name, carg);
832 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
833 int bytes = be64toh(*bytes_p);
835 ++carg->serialized_counters;
836 __fill_nfacct_result(cnt_name, bytes, carg);
843 static int __post_fill_counters(void *user_data)
845 struct counter_arg *carg = user_data;
853 static void __process_network_counter(struct genl *ans,
854 struct counter_arg *carg)
856 struct netlink_serialization_params ser_params = {
859 .eval_attr = __fill_counters,
860 .post_eval_attr = __post_fill_counters,
863 netlink_serialization_command *netlink =
864 netlink_create_command(&ser_params);
866 STC_LOGE("Can not create command");
870 netlink->deserialize_answer(&(netlink->params));
873 static gboolean __process_contr_reply(GIOChannel *source,
874 GIOCondition condition,
877 int sock = g_io_channel_unix_get_fd(source);
880 stc_s *stc = stc_get_manager();
882 if ((condition & G_IO_ERR) ||
883 (condition & G_IO_HUP) ||
884 (condition & G_IO_NVAL)) {
885 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
887 __close_contr_sock(g_system);
892 STC_LOGE("Can't get stc data");
896 ret = read_netlink(sock,
897 &ans, sizeof(struct genl));
898 /* STC_LOGD("Counter data received ret [%d]", ret); */
902 stc->carg->ans_len = ret;
903 __process_network_counter(&ans, stc->carg);
905 g_idle_add(__flush_apps_stats_to_database, NULL);
906 g_idle_add(__flush_rstns_counter_to_database, NULL);
911 static gboolean __update_contr_cb(void *user_data)
913 /* Here we just sent command, answer we receive in another callback */
914 stc_s *stc = stc_get_manager();
915 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
917 stc->carg = MALLOC0(counter_arg_s, 1);
918 stc->carg->sock = stc_monitor_get_counter_socket();
921 /* STC_LOGD("Get all counters"); */
922 nfacct_send_get_all(stc->carg);
924 /* we need to continue the timer */
928 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
931 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
932 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
934 __print_rstn(rstn_key, rstn_value);
938 static void __rstn_tree_printall(void)
940 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
943 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
944 const stc_rstn_key_s *key)
946 stc_rstn_value_s *lookup;
948 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
950 lookup = g_tree_lookup(rstns_tree, key);
955 static gboolean __remove_restriction(gpointer key, gpointer value,
958 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
959 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
961 /* rstn rule is already removed */
962 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
965 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
966 __print_rstn(rstn_key, rstn_value);
970 static gboolean __add_restriction_debug(gpointer key, gpointer value,
973 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
974 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
976 /* rstn rule is activated */
977 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
980 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
981 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
983 __process_restriction(RST_SET, rstn_key, rstn_value, data);
985 __print_rstn(rstn_key, rstn_value);
990 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
992 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
993 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
995 /* rstn rule is activated */
996 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
999 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1000 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1002 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1007 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1009 stc_rstn_value_s *lookup_value;
1011 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1013 lookup_value = __rstn_lookup(g_system->rstns, key);
1014 if (!lookup_value) {
1015 STC_LOGE("key not found");
1016 return STC_ERROR_NO_DATA;
1019 __remove_restriction(key, lookup_value, NULL);
1021 /* remove counter also */
1022 table_counters_delete(lookup_value->restriction_id);
1024 if (!g_tree_remove(g_system->rstns, key)) {
1025 STC_LOGD("key not found");
1026 return STC_ERROR_NO_DATA;
1029 return STC_ERROR_NONE;
1032 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1033 stc_rstn_value_s *value, gboolean debug)
1035 stc_rstn_value_s *rstn_value;
1037 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1039 rstn_value = __rstn_lookup(g_system->rstns, key);
1041 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1043 STC_LOGE("rstn_key allocation failed");
1044 return STC_ERROR_OUT_OF_MEMORY;
1047 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1049 STC_LOGE("rstn_value allocation failed");
1051 return STC_ERROR_OUT_OF_MEMORY;
1054 rstn_key->app_id = g_strdup(key->app_id);
1055 rstn_key->ifname = g_strdup(key->ifname);
1056 rstn_key->imsi = g_strdup(key->imsi);
1057 rstn_key->iftype = key->iftype;
1058 rstn_key->roaming = key->roaming;
1060 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1063 rstn_value->restriction_id = value->restriction_id;
1064 rstn_value->rst_state = value->rst_state;
1065 rstn_value->classid = value->classid;
1066 rstn_value->data_limit = value->data_limit;
1067 rstn_value->data_warn_limit = value->data_warn_limit;
1068 rstn_value->data_counter = 0;
1069 rstn_value->warn_limit_crossed_notified = FALSE;
1070 rstn_value->rstn_limit_crossed_notified = FALSE;
1073 __add_restriction_debug(key, rstn_value, NULL);
1075 __add_restriction(key, rstn_value, NULL);
1077 return STC_ERROR_NONE;
1080 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1083 stc_cb_ret_e ret = STC_CONTINUE;
1086 stc_rstn_value_s value;
1088 memset(&key, 0, sizeof(stc_rstn_key_s));
1089 memset(&value, 0, sizeof(stc_rstn_value_s));
1091 key.app_id = g_strdup(info->app_id);
1092 key.ifname = g_strdup(info->ifname);
1093 key.imsi = g_strdup(info->imsi);
1094 key.iftype = info->iftype;
1095 key.roaming = info->roaming;
1097 value.rst_state = info->rst_state;
1098 value.restriction_id = info->restriction_id;
1100 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1101 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1103 value.classid = STC_UNKNOWN_CLASSID;
1105 value.data_limit = info->data_limit;
1106 value.data_warn_limit = info->data_warn_limit;
1108 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1117 static void __fill_restritions_list(void)
1119 table_restrictions_foreach(__insert_restriction_cb, NULL);
1120 //__rstn_tree_printall();
1123 static gboolean __add_rstn_foreach_application(gpointer key,
1127 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1128 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1129 gchar *app_id = (gchar *)data;
1131 /* rstn rule is not for applications */
1132 if (rstn_key->app_id == NULL)
1135 /* rstn rule is not for this application */
1136 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1139 /* rstn rule is already applied */
1140 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1143 /* add restriction to system */
1144 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1145 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1147 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1149 __print_rstn(rstn_key, rstn_value);
1154 static void __add_rstns_for_application(gchar *app_id)
1156 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1160 stc_error_e stc_monitor_init(void)
1162 stc_system_s *system = MALLOC0(stc_system_s, 1);
1163 GIOChannel *gio = NULL;
1165 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1167 /* initializing cgroups */
1170 /* creating monitored application tree */
1171 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1172 __apps_tree_key_free,
1173 __apps_tree_value_free);
1175 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1176 __rstns_tree_key_free,
1177 __rstns_tree_value_free);
1179 /* create netlink socket for updating kernel counters */
1180 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1181 if (!(system->contr_sock)) {
1182 STC_LOGE("failed to open socket");
1184 return STC_ERROR_FAIL;
1187 gio = g_io_channel_unix_new(system->contr_sock);
1188 system->contr_gsource_id =
1189 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1190 (GIOFunc) __process_contr_reply,
1192 g_io_channel_unref(gio);
1196 /* creating restriction rules tree */
1197 __update_contr_cb(NULL);
1199 /* registering periodic kernel counters update callback */
1200 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1203 if (g_system->contr_timer_id == 0) {
1204 STC_LOGE("Failed to register kernel counters update timer");
1205 __close_contr_sock(g_system);
1206 return STC_ERROR_FAIL;
1209 __fill_restritions_list();
1211 return STC_ERROR_NONE;
1214 stc_error_e stc_monitor_deinit(void)
1216 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1218 /* close netlink socket for updating kernel counters */
1219 __close_contr_sock(g_system);
1221 /* remove kernel counters update timer */
1222 if (g_system->contr_timer_id > 0) {
1223 g_source_remove(g_system->contr_timer_id);
1224 g_system->contr_timer_id = 0;
1227 /* destroy monitored application tree */
1228 g_tree_destroy(g_system->apps);
1229 g_system->apps = NULL;
1231 /* destroy restriction rules tree */
1232 g_tree_destroy(g_system->rstns);
1233 g_system->rstns = NULL;
1237 return STC_ERROR_NONE;
1240 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1241 const stc_app_value_s app_value)
1243 stc_error_e ret = STC_ERROR_NONE;
1245 stc_app_value_s *value;
1246 stc_app_value_s *lookup;
1248 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1250 lookup = __application_lookup(g_system->apps, &app_key);
1252 STC_LOGD("app_key already present");
1253 return STC_ERROR_NONE;
1256 key = MALLOC0(stc_app_key_s, 1);
1258 STC_LOGE("key allocation failed");
1259 return STC_ERROR_OUT_OF_MEMORY;
1262 value = MALLOC0(stc_app_value_s, 1);
1264 STC_LOGE("value allocation failed");
1266 return STC_ERROR_OUT_OF_MEMORY;
1269 key->app_id = g_strdup(app_key.app_id);
1270 key->pkg_id = g_strdup(app_key.pkg_id);
1272 value->type = app_value.type;
1273 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1274 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1276 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1277 __processes_tree_key_free,
1278 __processes_tree_value_free);
1280 /* create cgroup and update classid */
1281 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1283 g_tree_insert(g_system->apps, key, value);
1285 /* add nfacct rule for this classid */
1286 __add_application_monitor(key, value, stc_get_default_connection());
1287 __add_rstns_for_application(app_key.app_id);
1292 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1293 const stc_process_key_s proc_key,
1294 const stc_process_value_s proc_value)
1296 stc_error_e ret = STC_ERROR_NONE;
1297 stc_app_value_s *app_lookup;
1298 stc_process_key_s *key;
1299 stc_process_value_s *value;
1300 stc_process_value_s *proc_lookup;
1302 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1304 app_lookup = __application_lookup(g_system->apps, &app_key);
1306 STC_LOGD("app_key not found");
1307 return STC_ERROR_FAIL;
1310 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1312 STC_LOGD("proc_key already present");
1313 return STC_ERROR_NONE;
1316 key = MALLOC0(stc_process_key_s, 1);
1318 STC_LOGE("key allocation failed");
1319 return STC_ERROR_OUT_OF_MEMORY;
1322 value = MALLOC0(stc_process_value_s, 1);
1324 STC_LOGE("value allocation failed");
1326 return STC_ERROR_OUT_OF_MEMORY;
1329 key->pid = proc_key.pid;
1331 value->ground = proc_value.ground;
1333 g_tree_insert(app_lookup->processes, key, value);
1335 /* add pid to application cgroup */
1336 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1341 stc_error_e stc_monitor_process_remove(pid_t pid)
1343 stc_error_e ret = STC_ERROR_NONE;
1344 stc_process_key_s proc_key = {
1348 remove_pid_context_s context = {
1350 .proc_key = &proc_key,
1351 .entry_removed = FALSE,
1354 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1356 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1359 if (context.entry_removed)
1360 __application_remove_if_empty(context.app_key);
1365 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1366 const stc_process_key_s proc_key,
1367 stc_app_state_e ground)
1369 stc_error_e ret = STC_ERROR_NONE;
1370 stc_app_value_s *app_lookup;
1371 stc_process_value_s *proc_lookup;
1373 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1375 app_lookup = __application_lookup(g_system->apps, &app_key);
1377 STC_LOGD("app_key not found");
1378 return STC_ERROR_FAIL;
1381 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1383 STC_LOGD("proc_key not found");
1384 return STC_ERROR_FAIL;
1387 if (proc_lookup->ground != ground)
1388 proc_lookup->ground = ground;
1390 if (ground == STC_APP_STATE_BACKGROUND) {
1391 char *background_app_id = g_strconcat(app_key.app_id,
1392 STC_BACKGROUND_APP_SUFFIX,
1394 place_pids_to_net_cgroup(proc_key.pid, background_app_id);
1395 g_free(background_app_id);
1397 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1403 void stc_monitor_update_rstn_by_default_connection(void *data)
1405 static default_connection_s old_connection;
1406 default_connection_s *new_connection = (default_connection_s *)data;
1408 if (old_connection.path != NULL) {
1410 g_tree_foreach(g_system->apps,
1411 __remove_application_monitor,
1412 (gpointer)&old_connection);
1414 if (g_system->rstns)
1415 g_tree_foreach(g_system->rstns,
1416 __remove_restriction,
1417 (gpointer)&old_connection);
1420 FREE(old_connection.path);
1421 FREE(old_connection.ifname);
1422 old_connection.type = 0;
1423 old_connection.roaming = 0;
1425 if (new_connection != NULL && new_connection->path != NULL) {
1427 g_tree_foreach(g_system->apps,
1428 __add_application_monitor,
1429 (gpointer)new_connection);
1431 if (g_system->rstns)
1432 g_tree_foreach(g_system->rstns, __add_restriction,
1435 old_connection.path = g_strdup(new_connection->path);
1436 old_connection.ifname = g_strdup(new_connection->ifname);
1437 old_connection.type = new_connection->type;
1438 old_connection.roaming = new_connection->roaming;
1442 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1447 stc_rstn_value_s value;
1449 memset(&key, 0, sizeof(stc_rstn_key_s));
1450 memset(&value, 0, sizeof(stc_rstn_value_s));
1452 key.app_id = g_strdup(info->app_id);
1453 key.ifname = g_strdup(info->ifname);
1454 key.imsi = g_strdup(info->imsi);
1455 key.iftype = info->iftype;
1456 key.roaming = info->roaming;
1458 value.rst_state = info->rst_state;
1459 value.restriction_id = info->restriction_id;
1461 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1462 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1464 value.classid = STC_UNKNOWN_CLASSID;
1466 value.data_limit = info->data_limit;
1467 value.data_warn_limit = info->data_warn_limit;
1469 ret = __rstn_tree_add(&key, &value, TRUE);
1477 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1481 stc_rstn_key_s key = {
1482 .app_id = g_strdup(info->app_id),
1483 .ifname = g_strdup(info->ifname),
1484 .imsi = g_strdup(info->imsi),
1485 .iftype = info->iftype,
1486 .roaming = info->roaming,
1489 ret = __rstn_tree_remove(&key);
1497 int stc_monitor_get_counter_socket(void)
1499 return g_system->contr_sock;