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"
27 #include "table-statistics.h"
28 #include "table-counters.h"
29 #include "stc-monitor.h"
30 #include "stc-manager-plugin.h"
32 #define MAX_INT_LENGTH 128
33 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
36 stc_app_key_s *app_key;
37 stc_process_key_s *proc_key;
38 gboolean entry_removed;
39 } remove_pid_context_s;
42 struct nfacct_rule *counter;
44 gboolean data_limit_reached;
45 } classid_bytes_context_s;
47 static stc_system_s *g_system = NULL;
49 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
51 if (counter->intend == NFACCT_WARN)
52 return NFACCT_JUMP_ACCEPT;
53 else if (counter->intend == NFACCT_BLOCK)
54 return NFACCT_JUMP_REJECT;
56 return NFACCT_JUMP_UNKNOWN;
59 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
62 return STC_ERROR_INVALID_PARAMETER;
64 counter->action = NFACCT_ACTION_INSERT;
65 counter->iotype = NFACCT_COUNTER_IN;
66 counter->jump = __get_jump_by_intend(counter);
67 counter->iptype = NFACCT_TYPE_IPV4;
68 counter->send_limit = 0;
69 counter->rcv_limit = 0;
71 return produce_net_rule(counter);
74 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
77 return STC_ERROR_INVALID_PARAMETER;
79 counter->action = NFACCT_ACTION_INSERT;
80 counter->iotype = NFACCT_COUNTER_OUT;
81 counter->jump = __get_jump_by_intend(counter);
82 counter->iptype = NFACCT_TYPE_IPV4;
83 counter->send_limit = 0;
84 counter->rcv_limit = 0;
86 return produce_net_rule(counter);
89 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
92 return STC_ERROR_INVALID_PARAMETER;
94 counter->action = NFACCT_ACTION_DELETE;
95 counter->iotype = NFACCT_COUNTER_IN;
96 counter->jump = __get_jump_by_intend(counter);
97 counter->iptype = NFACCT_TYPE_IPV4;
98 counter->send_limit = 0;
99 counter->rcv_limit = 0;
101 return produce_net_rule(counter);
104 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
107 return STC_ERROR_INVALID_PARAMETER;
109 counter->action = NFACCT_ACTION_DELETE;
110 counter->iotype = NFACCT_COUNTER_OUT;
111 counter->jump = __get_jump_by_intend(counter);
112 counter->iptype = NFACCT_TYPE_IPV4;
113 counter->send_limit = 0;
114 counter->rcv_limit = 0;
116 return produce_net_rule(counter);
119 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
122 return STC_ERROR_INVALID_PARAMETER;
124 counter->action = NFACCT_ACTION_INSERT;
125 counter->iotype = NFACCT_COUNTER_IN;
126 counter->jump = __get_jump_by_intend(counter);
127 counter->iptype = NFACCT_TYPE_IPV6;
128 counter->send_limit = 0;
129 counter->rcv_limit = 0;
131 return produce_net_rule(counter);
134 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
137 return STC_ERROR_INVALID_PARAMETER;
139 counter->action = NFACCT_ACTION_INSERT;
140 counter->iotype = NFACCT_COUNTER_OUT;
141 counter->jump = __get_jump_by_intend(counter);
142 counter->iptype = NFACCT_TYPE_IPV6;
143 counter->send_limit = 0;
144 counter->rcv_limit = 0;
146 return produce_net_rule(counter);
149 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
152 return STC_ERROR_INVALID_PARAMETER;
154 counter->action = NFACCT_ACTION_DELETE;
155 counter->iotype = NFACCT_COUNTER_IN;
156 counter->jump = __get_jump_by_intend(counter);
157 counter->iptype = NFACCT_TYPE_IPV6;
158 counter->send_limit = 0;
159 counter->rcv_limit = 0;
161 return produce_net_rule(counter);
164 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
167 return STC_ERROR_INVALID_PARAMETER;
169 counter->action = NFACCT_ACTION_DELETE;
170 counter->iotype = NFACCT_COUNTER_OUT;
171 counter->jump = __get_jump_by_intend(counter);
172 counter->iptype = NFACCT_TYPE_IPV6;
173 counter->send_limit = 0;
174 counter->rcv_limit = 0;
176 return produce_net_rule(counter);
179 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
180 gpointer UNUSED user_data)
182 stc_process_key_s *key_a = (stc_process_key_s *)a;
183 stc_process_key_s *key_b = (stc_process_key_s *)b;
185 return key_a->pid - key_b->pid;
188 static void __processes_tree_value_free(gpointer data)
190 stc_process_value_s *value = (stc_process_value_s *)data;
195 static void __processes_tree_key_free(gpointer data)
197 stc_process_key_s *key = (stc_process_key_s *)data;
202 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
203 gpointer UNUSED user_data)
205 stc_app_key_s *key_a = (stc_app_key_s *)a;
206 stc_app_key_s *key_b = (stc_app_key_s *)b;
209 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
213 return g_strcmp0(key_a->app_id, key_b->app_id);
216 static void __apps_tree_value_free(gpointer data)
218 stc_app_value_s *value = (stc_app_value_s *)data;
220 g_tree_destroy(value->processes);
221 value->processes = NULL;
226 static void __apps_tree_key_free(gpointer data)
228 stc_app_key_s *key = (stc_app_key_s *)data;
235 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
236 gpointer UNUSED user_data)
238 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
239 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
242 ret = g_strcmp0(key_a->app_id, key_b->app_id);
246 ret = g_strcmp0(key_a->ifname, key_b->ifname);
250 ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
254 ret = key_a->iftype - key_b->iftype;
261 static void __rstns_tree_value_free(gpointer data)
263 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
268 static void __rstns_tree_key_free(gpointer data)
270 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
274 FREE(key->subscriber_id);
278 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
281 stc_process_key_s *proc_key = (stc_process_key_s *)key;
282 stc_process_value_s *proc_value = (stc_process_value_s *)value;
284 STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
285 proc_key->pid, proc_value->ground);
289 static void __processes_tree_printall(GTree *processes)
291 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
294 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
297 stc_app_key_s *app_key = (stc_app_key_s *)key;
298 stc_app_value_s *app_value = (stc_app_value_s *)value;
300 STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
301 "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
302 " counter [ in (%lld), out (%lld)]",
303 app_key->pkg_id, app_key->app_id,
304 app_value->type, app_value->classid,
305 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
307 __processes_tree_printall(app_value->processes);
311 static void __apps_tree_printall(void)
313 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
316 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
319 remove_pid_context_s *context = (remove_pid_context_s *)data;
320 stc_app_value_s *app_value = (stc_app_value_s *)value;
322 if (!g_tree_remove(app_value->processes, context->proc_key)) {
324 STC_LOGD("key not found");
328 context->entry_removed = TRUE;
329 context->app_key = (stc_app_key_s *)key;
334 static stc_app_value_s * __application_lookup(GTree *apps,
335 const stc_app_key_s *key)
337 stc_app_value_s *lookup;
339 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
341 lookup = g_tree_lookup(apps, key);
346 static stc_process_value_s * __process_lookup(GTree *processes,
347 const stc_process_key_s *key)
349 stc_process_value_s *lookup;
351 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
353 lookup = g_tree_lookup(processes, key);
358 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
361 guint *pid_count = (guint *)data;
366 static gboolean __add_application_monitor(gpointer key, gpointer value,
369 stc_app_value_s *app_value = (stc_app_value_s *)value;
370 default_connection_s *connection = (default_connection_s *)data;
371 stc_s *stc = stc_get_manager();
373 if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
374 app_value->classid == STC_TOTAL_WIFI_CLASSID ||
375 app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
378 if (stc && connection && connection->ifname) {
379 struct nfacct_rule counter;
382 stc->carg = MALLOC0(counter_arg_s, 1);
383 if (stc->carg == NULL)
386 stc->carg->sock = stc_monitor_get_counter_socket();
389 memset(&counter, 0, sizeof(struct nfacct_rule));
391 counter.carg = stc->carg;
392 counter.classid = app_value->classid;
393 counter.intend = NFACCT_COUNTER;
394 counter.iftype = connection->type;
395 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
397 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
398 __add_iptables_in(&counter);
399 __add_iptables_out(&counter);
400 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
401 __add_ip6tables_in(&counter);
402 __add_ip6tables_out(&counter);
404 __add_iptables_in(&counter);
405 __add_iptables_out(&counter);
406 __add_ip6tables_in(&counter);
407 __add_ip6tables_out(&counter);
414 static gboolean __remove_application_monitor(gpointer key, gpointer value,
417 stc_app_value_s *app_value = (stc_app_value_s *)value;
418 default_connection_s *connection = (default_connection_s *)data;
419 stc_s *stc = stc_get_manager();
421 if (stc && connection && connection->ifname) {
422 struct nfacct_rule counter;
425 stc->carg = MALLOC0(counter_arg_s, 1);
426 if (stc->carg == NULL)
429 stc->carg->sock = stc_monitor_get_counter_socket();
432 memset(&counter, 0, sizeof(struct nfacct_rule));
434 counter.carg = stc->carg;
435 counter.classid = app_value->classid;
436 counter.intend = NFACCT_COUNTER;
437 counter.iftype = connection->type;
438 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
440 __del_iptables_in(&counter);
441 __del_iptables_out(&counter);
442 __del_ip6tables_in(&counter);
443 __del_ip6tables_out(&counter);
449 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
451 STC_LOGI("rstn info => rstn_id [%llu], "
452 "app_id [%s], classid [%lu], ifname [%s], "
453 "iftype [%d], rst_state [%d], "
454 "limit [ (%lld) bytes], "
455 "warn_limit [ (%lld) bytes], "
456 "counter [ (%lld) bytes], "
457 "roaming [%d], subscriber_id [%s]",
458 rstn_value->restriction_id,
459 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
460 rstn_key->iftype, rstn_value->rst_state,
461 rstn_value->data_limit,
462 rstn_value->data_warn_limit,
463 rstn_value->data_counter,
464 rstn_key->roaming, rstn_key->subscriber_id);
467 static void __process_restriction(enum traffic_restriction_type rst_type,
468 stc_rstn_key_s *rstn_key,
469 stc_rstn_value_s *rstn_value, void *data)
471 int64_t effective_data_limit, effective_data_warn_limit;
472 default_connection_s *old_connection = (default_connection_s *)data;
473 default_connection_s *connection = NULL;
475 if (old_connection != NULL)
476 connection = old_connection;
478 connection = stc_get_default_connection();
480 /* no default ifname */
481 if (connection->ifname == NULL)
484 /* rstn not applicable for this interface */
485 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
486 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
489 /* classid is invalid */
490 if (rstn_value->classid == STC_UNKNOWN_CLASSID)
493 effective_data_limit = rstn_value->data_limit;
494 effective_data_warn_limit = rstn_value->data_warn_limit;
496 if (rst_type == RST_SET) {
497 /* TODO: Change this to runtime memory */
498 table_counters_info info;
500 memset(&info, 0, sizeof(table_counters_info));
501 table_counters_get(rstn_value->restriction_id, &info);
503 effective_data_limit -= info.data_counter;
504 effective_data_warn_limit -= info.data_counter;
506 if (effective_data_limit < 0) {
507 effective_data_limit = 0;
508 rstn_value->data_limit_reached = TRUE;
511 if (effective_data_warn_limit < 0)
512 effective_data_warn_limit = 0;
514 STC_LOGD("datausage [%lld] bytes", info.data_counter);
517 STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
518 "effective_data_warn_limit [%lld] bytes",
519 rstn_value->restriction_id, effective_data_limit,
520 effective_data_warn_limit);
524 if (effective_data_limit <= 0) {
525 char *default_ifname = stc_default_connection_get_ifname();
526 struct nfacct_rule counter;
527 stc_s *stc = stc_get_manager();
530 stc->carg = MALLOC0(counter_arg_s, 1);
531 if (stc->carg == NULL) {
532 g_free(default_ifname);
537 stc_monitor_get_counter_socket();
540 counter.carg = stc->carg;
541 counter.classid = rstn_value->classid;
542 counter.intend = NFACCT_BLOCK;
543 counter.iftype = rstn_key->iftype;
544 g_strlcpy(counter.ifname, default_ifname,
547 g_free(default_ifname);
550 __add_iptables_in(&counter);
551 __add_iptables_out(&counter);
554 __add_ip6tables_in(&counter);
555 __add_ip6tables_out(&counter);
558 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
559 rstn_value->data_limit_reached = FALSE;
566 char *default_ifname = stc_default_connection_get_ifname();
567 struct nfacct_rule counter;
568 stc_s *stc = stc_get_manager();
571 stc->carg = MALLOC0(counter_arg_s, 1);
572 if (stc->carg == NULL) {
573 g_free(default_ifname);
578 stc_monitor_get_counter_socket();
581 counter.carg = stc->carg;
582 counter.classid = rstn_value->classid;
583 counter.intend = NFACCT_BLOCK;
584 counter.iftype = rstn_key->iftype;
585 g_strlcpy(counter.ifname, default_ifname,
588 g_free(default_ifname);
591 __del_iptables_in(&counter);
592 __del_iptables_out(&counter);
595 __del_ip6tables_in(&counter);
596 __del_ip6tables_out(&counter);
598 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
599 rstn_value->data_limit_reached = FALSE;
607 static gboolean __remove_rstns_foreach_application(gpointer key,
611 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
612 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
613 gchar *app_id = (gchar *)data;
615 /* rstn rule is not for applications */
616 if (rstn_key->app_id == NULL)
619 /* rstn rule is not for this application */
620 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
623 /* rstn rule is already removed */
624 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
627 /* remove restriction from system */
628 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
630 __print_rstn(rstn_key, rstn_value);
635 static void __remove_rstns_for_application(gchar *app_id)
637 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
641 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
643 stc_error_e ret = STC_ERROR_NONE;
645 stc_app_value_s *lookup;
647 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
649 lookup = __application_lookup(g_system->apps, app_key);
651 STC_LOGE("app_key not found");
652 return STC_ERROR_NO_DATA;
655 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
659 /* remove nfacct rule for this classid */
660 __remove_application_monitor((gpointer) app_key, lookup,
661 stc_get_default_connection());
662 __remove_rstns_for_application(app_key->app_id);
665 if (!g_tree_remove(g_system->apps, app_key)) {
666 ret = STC_ERROR_NO_DATA;
667 STC_LOGE("key not found");
673 static stc_error_e __close_contr_sock(stc_system_s *system)
675 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
677 /* close netlink socket for updating kernel counters */
678 if (system->contr_sock != -1) {
679 close(system->contr_sock);
680 system->contr_sock = -1;
683 if (system->contr_gsource_id != 0) {
684 g_source_remove(system->contr_gsource_id);
685 system->contr_gsource_id = 0;
688 return STC_ERROR_NONE;
691 static gboolean __process_contr_reply(GIOChannel *source,
692 GIOCondition condition,
695 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
697 GIOChannel *gio = NULL;
698 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
700 /* close netlink socket for updating kernel counters */
701 if (system->contr_sock != -1) {
702 close(system->contr_sock);
703 system->contr_sock = -1;
706 if (system->contr_gsource_id != 0) {
707 g_source_remove(system->contr_gsource_id);
708 system->contr_gsource_id = 0;
711 /* create netlink socket for updating kernel counters */
712 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
713 if (system->contr_sock < 0) {
714 STC_LOGE("failed to open socket");
716 return STC_ERROR_FAIL;
719 gio = g_io_channel_unix_new(system->contr_sock);
720 system->contr_gsource_id =
721 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
722 (GIOFunc) __process_contr_reply,
724 g_io_channel_unref(gio);
726 return STC_ERROR_NONE;
729 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
730 stc_rstn_value_s *rstn_value,
731 classid_bytes_context_s *context)
733 switch (context->counter->iotype) {
734 case NFACCT_COUNTER_IN:
735 case NFACCT_COUNTER_OUT:
736 rstn_value->data_counter += context->bytes;
738 if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
739 rstn_value->warn_limit_crossed_notified == FALSE) {
742 char iftype[MAX_INT_LENGTH];
743 char byte[MAX_INT_LENGTH];
744 stc_s *stc = (stc_s *)stc_get_manager();
745 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
748 rv = stc_manager_dbus_emit_signal(stc->connection,
749 STC_DBUS_SERVICE_RESTRICTION_PATH,
750 STC_DBUS_INTERFACE_RESTRICTION,
751 "WarnThresholdCrossed",
752 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
754 rstn_value->warn_limit_crossed_notified = TRUE;
756 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
757 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
758 stc_manager_plugin_send_warn_message("warn threshold crossed",
764 if (rstn_value->data_counter >= rstn_value->data_limit &&
765 rstn_value->rstn_limit_crossed_notified == FALSE) {
768 char iftype[MAX_INT_LENGTH];
769 char byte[MAX_INT_LENGTH];
770 stc_s *stc = (stc_s *)stc_get_manager();
771 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
773 /* block immediately */
774 context->counter->intend = NFACCT_BLOCK;
775 __del_iptables_in(context->counter);
776 __del_iptables_out(context->counter);
777 __add_iptables_in(context->counter);
778 __add_iptables_out(context->counter);
780 __del_ip6tables_in(context->counter);
781 __del_ip6tables_out(context->counter);
782 __add_ip6tables_in(context->counter);
783 __add_ip6tables_out(context->counter);
784 context->counter->intend = NFACCT_COUNTER;
786 rstn_value->data_limit_reached = TRUE;
789 rv = stc_manager_dbus_emit_signal(stc->connection,
790 STC_DBUS_SERVICE_RESTRICTION_PATH,
791 STC_DBUS_INTERFACE_RESTRICTION,
792 "RestrictionThresholdCrossed",
793 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
795 rstn_value->rstn_limit_crossed_notified = TRUE;
797 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
798 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
799 stc_manager_plugin_send_restriction_message("restriction threshold crossed",
800 "restriction_noti", rstn_key->app_id,
804 g_system->rstns_tree_updated = TRUE;
805 __print_rstn(rstn_key, rstn_value);
808 STC_LOGE("unknown iotype");
814 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
815 stc_rstn_value_s *rstn_value,
816 classid_bytes_context_s *context)
818 if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
819 context->counter->iftype == STC_IFACE_DATACALL) ||
820 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
821 context->counter->iftype == STC_IFACE_WIFI) ||
822 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
823 context->counter->iftype == STC_IFACE_BLUETOOTH)) {
824 context->counter->classid = rstn_value->classid;
825 return __rstn_counter_update(rstn_key, rstn_value, context);
831 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
836 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
837 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
838 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
841 if (context->counter->intend != NFACCT_COUNTER)
842 goto try_next_callback;
844 if (rstn_value->data_limit_reached == TRUE) {
845 context->data_limit_reached = TRUE;
846 goto try_next_callback;
849 classid = context->counter->classid;
850 rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
852 context->counter->classid = classid;
853 if (rstn_value->classid != context->counter->classid)
854 goto try_next_callback;
856 rv = __rstn_counter_update(rstn_key, rstn_value, context);
862 static gboolean __update_app_statistics(gpointer key, gpointer value,
865 stc_app_key_s *app_key = (stc_app_key_s *)key;
866 stc_app_value_s *app_value = (stc_app_value_s *)value;
867 time_t *touch_time = (time_t *)data;
868 stc_db_classid_iftype_key stat_key;
869 stc_db_app_stats stat;
870 default_connection_s *default_connection = stc_get_default_connection();
872 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
873 memset(&stat, 0 , sizeof(stc_db_app_stats));
875 stat_key.classid = app_value->classid;
876 stat_key.iftype = default_connection->type;
878 if (STC_IFACE_DATACALL == stat_key.iftype)
879 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
881 stat_key.subscriber_id = g_strdup("none_subid");
883 g_strlcpy(stat_key.ifname, default_connection->ifname,
886 stat.app_id = g_strdup(app_key->app_id);
887 stat.snd_count = app_value->counter.out_bytes;
888 stat.rcv_count = app_value->counter.in_bytes;
889 stat.is_roaming = default_connection->roaming;
891 if (strstr(stat.app_id, "_BACKGROUND")) {
892 stat.ground = STC_APP_STATE_BACKGROUND;
894 if (strstr(stat.app_id, "TOTAL_"))
895 stat.ground = STC_APP_STATE_UNKNOWN;
897 stat.ground = STC_APP_STATE_FOREGROUND;
900 table_statistics_insert(&stat_key, &stat, *touch_time);
902 app_value->counter.out_bytes = 0;
903 app_value->counter.in_bytes = 0;
906 FREE(stat_key.subscriber_id);
911 static gboolean __flush_apps_stats_to_database(gpointer user_data)
913 time_t current_time = time(0);
915 if (g_system->apps_tree_updated == FALSE)
916 return G_SOURCE_REMOVE;
918 g_system->apps_tree_updated = FALSE;
921 g_tree_foreach(g_system->apps,
922 __update_app_statistics,
925 STC_LOGI("Flushed app stats to database");
926 return G_SOURCE_REMOVE;
929 static gboolean __update_counter_statistics(gpointer key, gpointer value,
932 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
933 table_counters_info info = {
934 .restriction_id = rstn_value->restriction_id,
935 .data_counter = rstn_value->data_counter
938 table_counters_update_counters(&info);
943 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
945 time_t current_time = time(0);
947 if (g_system->rstns_tree_updated == FALSE)
948 return G_SOURCE_REMOVE;
950 g_system->rstns_tree_updated = FALSE;
953 g_tree_foreach(g_system->rstns,
954 __update_counter_statistics,
957 STC_LOGI("Flushed rstns counters to database");
958 return G_SOURCE_REMOVE;
961 static void __app_counter_update(stc_app_key_s *app_key,
962 stc_app_value_s *app_value,
963 classid_bytes_context_s *context)
965 switch (context->counter->iotype) {
966 case NFACCT_COUNTER_IN:
967 app_value->data_usage.in_bytes += context->bytes;
968 app_value->counter.in_bytes = context->bytes;
969 g_system->apps_tree_updated = TRUE;
972 __apps_tree_foreach_print(app_key, app_value, NULL);
974 case NFACCT_COUNTER_OUT:
975 app_value->data_usage.out_bytes += context->bytes;
976 app_value->counter.out_bytes = context->bytes;
977 g_system->apps_tree_updated = TRUE;
980 __apps_tree_foreach_print(app_key, app_value, NULL);
983 STC_LOGE("unknown iotype");
987 static void __interface_counter_update(stc_app_key_s *app_key,
988 stc_app_value_s *app_value,
989 classid_bytes_context_s *context)
991 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
992 context->counter->iftype == STC_IFACE_DATACALL) ||
993 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
994 context->counter->iftype == STC_IFACE_WIFI) ||
995 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
996 context->counter->iftype == STC_IFACE_BLUETOOTH))
997 __app_counter_update(app_key, app_value, context);
1001 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1005 stc_app_key_s *app_key = (stc_app_key_s *)key;
1006 stc_app_value_s *app_value = (stc_app_value_s *)value;
1007 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1009 if (context->counter->intend != NFACCT_COUNTER)
1010 goto try_next_callback;
1012 __interface_counter_update(app_key, app_value, context);
1014 if (app_value->classid != context->counter->classid)
1015 goto try_next_callback;
1017 __app_counter_update(app_key, app_value, context);
1023 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1024 struct counter_arg *carg)
1026 struct nfacct_rule counter = {
1033 classid_bytes_context_s context = {
1034 .counter = &counter,
1036 .data_limit_reached = FALSE,
1040 STC_LOGD("cnt_name %s", cnt_name);
1042 if (!recreate_counter_by_name(cnt_name, &counter)) {
1043 STC_LOGE("Can't parse counter name %s", cnt_name);
1047 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1048 context.counter->classid, context.counter->iftype,
1049 context.counter->iotype, context.counter->intend,
1050 context.counter->ifname, context.bytes);
1052 if (g_system->rstns)
1053 g_tree_foreach(g_system->rstns,
1054 __rstn_counter_update_foreach_classid,
1058 g_tree_foreach(g_system->apps,
1059 __apps_counter_update_foreach_classid,
1063 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1066 struct counter_arg *carg = user_data;
1067 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1068 if (carg->initiate) {
1070 * TODO: this will be used when daemon starts to update existing
1071 * counter data if present.
1073 populate_counters(cnt_name, carg);
1077 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1078 int bytes = be64toh(*bytes_p);
1080 ++carg->serialized_counters;
1081 __fill_nfacct_result(cnt_name, bytes, carg);
1088 static int __post_fill_counters(void *user_data)
1090 struct counter_arg *carg = user_data;
1098 static void __process_network_counter(struct genl *ans,
1099 struct counter_arg *carg)
1101 struct netlink_serialization_params ser_params = {
1104 .eval_attr = __fill_counters,
1105 .post_eval_attr = __post_fill_counters,
1108 netlink_serialization_command *netlink =
1109 netlink_create_command(&ser_params);
1111 STC_LOGE("Can not create command");
1115 netlink->deserialize_answer(&(netlink->params));
1118 static gboolean __process_contr_reply(GIOChannel *source,
1119 GIOCondition condition,
1122 int sock = g_io_channel_unix_get_fd(source);
1125 stc_s *stc = stc_get_manager();
1127 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1128 (condition & G_IO_NVAL)) {
1129 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1131 STC_LOGE("Counter socket received G_IO event, closing socket."
1132 "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
1133 (condition & G_IO_ERR), (condition & G_IO_HUP),
1134 (condition & G_IO_NVAL));
1135 __close_and_reopen_contr_sock(g_system);
1139 ans = MALLOC0(struct genl, 1);
1141 STC_LOGE("Failed allocate memory to genl reply message");
1146 STC_LOGE("Can't get stc data");
1150 ret = read_netlink(sock, ans, sizeof(struct genl));
1151 /* STC_LOGD("Counter data received ret [%d]", ret); */
1155 stc->carg->ans_len = ret;
1156 __process_network_counter(ans, stc->carg);
1158 g_idle_add(__flush_apps_stats_to_database, NULL);
1159 g_idle_add(__flush_rstns_counter_to_database, NULL);
1165 static gboolean __update_contr_cb(void *user_data)
1167 /* Here we just sent command, answer we receive in another callback */
1168 stc_s *stc = stc_get_manager();
1169 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1171 stc->carg = MALLOC0(counter_arg_s, 1);
1172 if (stc->carg == NULL)
1173 return TRUE; /* we need to continue the timer */
1175 stc->carg->sock = stc_monitor_get_counter_socket();
1178 /* STC_LOGD("Get all counters"); */
1179 nfacct_send_get_all(stc->carg);
1181 /* we need to continue the timer */
1185 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1188 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1189 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1191 __print_rstn(rstn_key, rstn_value);
1195 static void __rstn_tree_printall(void)
1197 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1200 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1201 const stc_rstn_key_s *key)
1203 stc_rstn_value_s *lookup;
1205 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1207 lookup = g_tree_lookup(rstns_tree, key);
1212 static gboolean __remove_restriction(gpointer key, gpointer value,
1215 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1216 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1218 /* rstn rule is already removed */
1219 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
1222 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1223 __print_rstn(rstn_key, rstn_value);
1227 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1230 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1231 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1233 /* rstn rule is activated */
1234 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1237 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1238 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1240 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1242 __print_rstn(rstn_key, rstn_value);
1247 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1249 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1250 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1252 /* rstn rule is activated */
1253 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1256 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1257 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1259 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1264 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1266 stc_rstn_value_s *lookup_value;
1268 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1270 lookup_value = __rstn_lookup(g_system->rstns, key);
1271 if (!lookup_value) {
1272 STC_LOGE("key not found");
1273 return STC_ERROR_NO_DATA;
1276 __remove_restriction(key, lookup_value, NULL);
1278 /* remove counter also */
1279 table_counters_delete(lookup_value->restriction_id);
1281 if (!g_tree_remove(g_system->rstns, key)) {
1282 STC_LOGD("key not found");
1283 return STC_ERROR_NO_DATA;
1286 return STC_ERROR_NONE;
1289 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1290 stc_rstn_value_s *value, gboolean debug)
1292 stc_rstn_value_s *rstn_value;
1294 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1296 rstn_value = __rstn_lookup(g_system->rstns, key);
1298 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1300 STC_LOGE("rstn_key allocation failed");
1301 return STC_ERROR_OUT_OF_MEMORY;
1304 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1306 STC_LOGE("rstn_value allocation failed");
1308 return STC_ERROR_OUT_OF_MEMORY;
1311 rstn_key->app_id = g_strdup(key->app_id);
1312 rstn_key->ifname = g_strdup(key->ifname);
1313 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1314 rstn_key->iftype = key->iftype;
1315 rstn_key->roaming = key->roaming;
1317 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1320 rstn_value->restriction_id = value->restriction_id;
1321 rstn_value->rst_state = value->rst_state;
1322 rstn_value->classid = value->classid;
1323 rstn_value->data_limit = value->data_limit;
1324 rstn_value->data_warn_limit = value->data_warn_limit;
1325 rstn_value->data_counter = 0;
1326 rstn_value->warn_limit_crossed_notified = FALSE;
1327 rstn_value->rstn_limit_crossed_notified = FALSE;
1330 __add_restriction_debug(key, rstn_value, NULL);
1332 __add_restriction(key, rstn_value, NULL);
1334 return STC_ERROR_NONE;
1337 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1340 stc_cb_ret_e ret = STC_CONTINUE;
1343 stc_rstn_value_s value;
1345 memset(&key, 0, sizeof(stc_rstn_key_s));
1346 memset(&value, 0, sizeof(stc_rstn_value_s));
1348 key.app_id = g_strdup(info->app_id);
1349 key.ifname = g_strdup(info->ifname);
1350 key.subscriber_id = g_strdup(info->subscriber_id);
1351 key.iftype = info->iftype;
1352 key.roaming = info->roaming;
1354 value.rst_state = info->rst_state;
1355 value.restriction_id = info->restriction_id;
1357 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1358 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1360 value.classid = STC_UNKNOWN_CLASSID;
1362 value.data_limit = info->data_limit;
1363 value.data_warn_limit = info->data_warn_limit;
1365 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1370 FREE(key.subscriber_id);
1374 static void __fill_restritions_list(void)
1376 table_restrictions_foreach(__insert_restriction_cb, NULL);
1377 //__rstn_tree_printall();
1380 static gboolean __add_rstn_foreach_application(gpointer key,
1384 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1385 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1386 gchar *app_id = (gchar *)data;
1388 /* rstn rule is not for applications */
1389 if (rstn_key->app_id == NULL)
1392 /* rstn rule is not for this application */
1393 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1396 /* rstn rule is already applied */
1397 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1400 /* add restriction to system */
1401 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1402 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1404 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1406 __print_rstn(rstn_key, rstn_value);
1411 static void __add_rstns_for_application(gchar *app_id)
1413 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1417 static void __add_application_by_interface(const char *app_id)
1419 stc_app_key_s app_key;
1420 stc_app_value_s app_value;
1425 memset(&app_key, 0, sizeof(stc_app_key_s));
1426 memset(&app_value, 0, sizeof(stc_app_value_s));
1428 app_key.pkg_id = g_strdup(app_id);
1429 app_key.app_id = g_strdup(app_id);
1431 app_value.type = STC_APP_TYPE_NONE;
1432 app_value.processes = NULL;
1433 app_value.counter.in_bytes = 0;
1434 app_value.counter.out_bytes = 0;
1436 stc_monitor_application_add(app_key, app_value);
1438 FREE(app_key.pkg_id);
1439 FREE(app_key.app_id);
1442 static int __vconf_get_int(const char *key, int *value)
1446 ret = vconf_get_int(key, value);
1447 if (ret != VCONF_OK) {
1448 STC_LOGE("Failed to get vconfkey [%s] value", key);
1455 static int __vconf_set_int(const char *key, int value)
1459 ret = vconf_set_int(key, value);
1460 if (ret != VCONF_OK) {
1461 STC_LOGE("Failed to set vconfkey [%s] value", key);
1468 static guint __get_background_state(void)
1470 return g_system->background_state;;
1473 static void __set_background_state(guint state)
1475 g_system->background_state = state;
1478 static gboolean __processes_tree_foreach_background(gpointer key,
1482 stc_process_key_s *proc_key = (stc_process_key_s *)key;
1483 stc_app_key_s *app_key = (stc_app_key_s *)data;
1485 if (g_system->background_state)
1486 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1488 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1493 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1496 stc_app_key_s *app_key = (stc_app_key_s *)key;
1497 stc_app_value_s *app_value = (stc_app_value_s *)value;
1499 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1500 g_tree_foreach(app_value->processes,
1501 __processes_tree_foreach_background, app_key);
1506 static stc_error_e __process_update_background(void)
1508 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1510 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1512 return STC_ERROR_NONE;
1515 static void __fill_exceptions_list(void)
1517 stc_manager_plugin_fill_exception_list();
1520 stc_error_e stc_monitor_init(void)
1522 stc_system_s *system = MALLOC0(stc_system_s, 1);
1523 GIOChannel *gio = NULL;
1525 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1527 /* initializing current classid */
1528 init_current_classid();
1530 /* initializing cgroups */
1533 /* creating monitored application tree */
1534 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1535 __apps_tree_key_free,
1536 __apps_tree_value_free);
1538 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1539 __rstns_tree_key_free,
1540 __rstns_tree_value_free);
1542 /* create netlink socket for updating kernel counters */
1543 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1544 if (system->contr_sock < 0) {
1545 STC_LOGE("failed to open socket");
1547 return STC_ERROR_FAIL;
1550 gio = g_io_channel_unix_new(system->contr_sock);
1551 system->contr_gsource_id =
1552 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1553 (GIOFunc) __process_contr_reply,
1555 g_io_channel_unref(gio);
1559 __add_application_by_interface(STC_TOTAL_DATACALL);
1560 __add_application_by_interface(STC_TOTAL_WIFI);
1561 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1562 __add_application_by_interface(STC_TOTAL_IPV4);
1563 __add_application_by_interface(STC_TOTAL_IPV6);
1565 /* creating restriction rules tree */
1566 __update_contr_cb(NULL);
1568 /* registering periodic kernel counters update callback */
1569 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1572 if (g_system->contr_timer_id == 0) {
1573 STC_LOGE("Failed to register kernel counters update timer");
1574 __close_contr_sock(g_system);
1575 return STC_ERROR_FAIL;
1578 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1579 (int *)&g_system->background_state);
1581 __fill_exceptions_list();
1582 __fill_restritions_list();
1584 return STC_ERROR_NONE;
1587 stc_error_e stc_monitor_deinit(void)
1589 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1591 /* close netlink socket for updating kernel counters */
1592 __close_contr_sock(g_system);
1594 /* remove kernel counters update timer */
1595 if (g_system->contr_timer_id > 0) {
1596 g_source_remove(g_system->contr_timer_id);
1597 g_system->contr_timer_id = 0;
1600 /* destroy monitored application tree */
1601 g_tree_destroy(g_system->apps);
1602 g_system->apps = NULL;
1604 /* destroy restriction rules tree */
1605 g_tree_destroy(g_system->rstns);
1606 g_system->rstns = NULL;
1610 return STC_ERROR_NONE;
1613 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1614 const stc_app_value_s app_value)
1616 stc_error_e ret = STC_ERROR_NONE;
1618 stc_app_value_s *value;
1619 stc_app_value_s *lookup;
1621 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1623 lookup = __application_lookup(g_system->apps, &app_key);
1625 STC_LOGD("app_key already present");
1626 return STC_ERROR_NONE;
1629 key = MALLOC0(stc_app_key_s, 1);
1631 STC_LOGE("key allocation failed");
1632 return STC_ERROR_OUT_OF_MEMORY;
1635 value = MALLOC0(stc_app_value_s, 1);
1637 STC_LOGE("value allocation failed");
1639 return STC_ERROR_OUT_OF_MEMORY;
1642 key->app_id = g_strdup(app_key.app_id);
1643 key->pkg_id = g_strdup(app_key.pkg_id);
1645 value->type = app_value.type;
1646 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1647 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1649 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1650 __processes_tree_key_free,
1651 __processes_tree_value_free);
1653 /* create cgroup and update classid */
1654 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1656 g_tree_insert(g_system->apps, key, value);
1658 /* add nfacct rule for this classid */
1659 __add_application_monitor(key, value, stc_get_default_connection());
1660 __add_rstns_for_application(app_key.app_id);
1665 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1666 const stc_process_key_s proc_key,
1667 const stc_process_value_s proc_value)
1669 stc_error_e ret = STC_ERROR_NONE;
1670 stc_app_value_s *app_lookup;
1671 stc_process_key_s *key;
1672 stc_process_value_s *value;
1673 stc_process_value_s *proc_lookup;
1675 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1677 app_lookup = __application_lookup(g_system->apps, &app_key);
1679 STC_LOGD("app_key not found");
1680 return STC_ERROR_FAIL;
1683 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1685 STC_LOGD("proc_key already present");
1686 return STC_ERROR_NONE;
1689 key = MALLOC0(stc_process_key_s, 1);
1691 STC_LOGE("key allocation failed");
1692 return STC_ERROR_OUT_OF_MEMORY;
1695 value = MALLOC0(stc_process_value_s, 1);
1697 STC_LOGE("value allocation failed");
1699 return STC_ERROR_OUT_OF_MEMORY;
1702 key->pid = proc_key.pid;
1704 value->ground = proc_value.ground;
1706 g_tree_insert(app_lookup->processes, key, value);
1708 /* add pid to application cgroup */
1709 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1712 __apps_tree_printall();
1717 stc_error_e stc_monitor_process_remove(pid_t pid)
1719 stc_error_e ret = STC_ERROR_NONE;
1720 stc_process_key_s proc_key = {
1724 remove_pid_context_s context = {
1726 .proc_key = &proc_key,
1727 .entry_removed = FALSE,
1730 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1732 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1735 if (context.entry_removed)
1736 __application_remove_if_empty(context.app_key);
1739 __apps_tree_printall();
1744 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1745 const stc_process_key_s proc_key,
1746 stc_app_state_e ground)
1748 stc_error_e ret = STC_ERROR_NONE;
1749 stc_app_value_s *app_lookup;
1750 stc_process_value_s *proc_lookup;
1752 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1754 app_lookup = __application_lookup(g_system->apps, &app_key);
1756 STC_LOGD("app_key not found");
1757 return STC_ERROR_FAIL;
1760 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1762 STC_LOGD("proc_key not found");
1763 return STC_ERROR_FAIL;
1766 if (proc_lookup->ground != ground)
1767 proc_lookup->ground = ground;
1769 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1770 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1772 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1777 void stc_monitor_update_rstn_by_default_connection(void *data)
1779 static default_connection_s old_connection;
1780 default_connection_s *new_connection = (default_connection_s *)data;
1782 if (old_connection.path != NULL) {
1784 g_tree_foreach(g_system->apps,
1785 __remove_application_monitor,
1786 (gpointer)&old_connection);
1788 if (g_system->rstns)
1789 g_tree_foreach(g_system->rstns,
1790 __remove_restriction,
1791 (gpointer)&old_connection);
1794 FREE(old_connection.path);
1795 FREE(old_connection.ifname);
1796 old_connection.type = 0;
1797 old_connection.roaming = 0;
1799 if (new_connection != NULL && new_connection->path != NULL) {
1801 g_tree_foreach(g_system->apps,
1802 __add_application_monitor,
1803 (gpointer)new_connection);
1805 if (g_system->rstns)
1806 g_tree_foreach(g_system->rstns, __add_restriction,
1809 old_connection.path = g_strdup(new_connection->path);
1810 old_connection.ifname = g_strdup(new_connection->ifname);
1811 old_connection.type = new_connection->type;
1812 old_connection.roaming = new_connection->roaming;
1816 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1821 stc_rstn_value_s value;
1823 memset(&key, 0, sizeof(stc_rstn_key_s));
1824 memset(&value, 0, sizeof(stc_rstn_value_s));
1826 key.app_id = g_strdup(info->app_id);
1827 key.ifname = g_strdup(info->ifname);
1828 key.subscriber_id = g_strdup(info->subscriber_id);
1829 key.iftype = info->iftype;
1830 key.roaming = info->roaming;
1832 value.rst_state = info->rst_state;
1833 value.restriction_id = info->restriction_id;
1835 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1836 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1838 value.classid = STC_UNKNOWN_CLASSID;
1840 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1841 __set_background_state(TRUE);
1842 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
1843 __process_update_background();
1846 value.data_limit = info->data_limit;
1847 value.data_warn_limit = info->data_warn_limit;
1849 ret = __rstn_tree_add(&key, &value, TRUE);
1853 FREE(key.subscriber_id);
1857 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1861 stc_rstn_key_s key = {
1862 .app_id = g_strdup(info->app_id),
1863 .ifname = g_strdup(info->ifname),
1864 .subscriber_id = g_strdup(info->subscriber_id),
1865 .iftype = info->iftype,
1866 .roaming = info->roaming,
1869 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
1870 __set_background_state(FALSE);
1871 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
1872 __process_update_background();
1875 ret = __rstn_tree_remove(&key);
1879 FREE(key.subscriber_id);
1883 stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
1885 return stc_manager_plugin_check_exception_by_cmdline(cmdline);
1888 int stc_monitor_get_counter_socket(void)
1890 return g_system->contr_sock;