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-exception.h"
30 #include "stc-monitor.h"
31 #include "stc-manager-plugin.h"
33 #define MAX_INT_LENGTH 128
34 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
37 stc_app_key_s *app_key;
38 stc_process_key_s *proc_key;
39 gboolean entry_removed;
40 } remove_pid_context_s;
43 struct nfacct_rule *counter;
45 gboolean data_limit_reached;
46 } classid_bytes_context_s;
48 static stc_system_s *g_system = NULL;
50 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
52 if (counter->intend == NFACCT_WARN)
53 return NFACCT_JUMP_ACCEPT;
54 else if (counter->intend == NFACCT_BLOCK)
55 return NFACCT_JUMP_REJECT;
57 return NFACCT_JUMP_UNKNOWN;
60 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
63 return STC_ERROR_INVALID_PARAMETER;
65 counter->action = NFACCT_ACTION_INSERT;
66 counter->iotype = NFACCT_COUNTER_IN;
67 counter->jump = __get_jump_by_intend(counter);
68 counter->iptype = NFACCT_TYPE_IPV4;
69 counter->send_limit = 0;
70 counter->rcv_limit = 0;
72 return produce_net_rule(counter);
75 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
78 return STC_ERROR_INVALID_PARAMETER;
80 counter->action = NFACCT_ACTION_INSERT;
81 counter->iotype = NFACCT_COUNTER_OUT;
82 counter->jump = __get_jump_by_intend(counter);
83 counter->iptype = NFACCT_TYPE_IPV4;
84 counter->send_limit = 0;
85 counter->rcv_limit = 0;
87 return produce_net_rule(counter);
90 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
93 return STC_ERROR_INVALID_PARAMETER;
95 counter->action = NFACCT_ACTION_DELETE;
96 counter->iotype = NFACCT_COUNTER_IN;
97 counter->jump = __get_jump_by_intend(counter);
98 counter->iptype = NFACCT_TYPE_IPV4;
99 counter->send_limit = 0;
100 counter->rcv_limit = 0;
102 return produce_net_rule(counter);
105 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
108 return STC_ERROR_INVALID_PARAMETER;
110 counter->action = NFACCT_ACTION_DELETE;
111 counter->iotype = NFACCT_COUNTER_OUT;
112 counter->jump = __get_jump_by_intend(counter);
113 counter->iptype = NFACCT_TYPE_IPV4;
114 counter->send_limit = 0;
115 counter->rcv_limit = 0;
117 return produce_net_rule(counter);
120 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
123 return STC_ERROR_INVALID_PARAMETER;
125 counter->action = NFACCT_ACTION_INSERT;
126 counter->iotype = NFACCT_COUNTER_IN;
127 counter->jump = __get_jump_by_intend(counter);
128 counter->iptype = NFACCT_TYPE_IPV6;
129 counter->send_limit = 0;
130 counter->rcv_limit = 0;
132 return produce_net_rule(counter);
135 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
138 return STC_ERROR_INVALID_PARAMETER;
140 counter->action = NFACCT_ACTION_INSERT;
141 counter->iotype = NFACCT_COUNTER_OUT;
142 counter->jump = __get_jump_by_intend(counter);
143 counter->iptype = NFACCT_TYPE_IPV6;
144 counter->send_limit = 0;
145 counter->rcv_limit = 0;
147 return produce_net_rule(counter);
150 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
153 return STC_ERROR_INVALID_PARAMETER;
155 counter->action = NFACCT_ACTION_DELETE;
156 counter->iotype = NFACCT_COUNTER_IN;
157 counter->jump = __get_jump_by_intend(counter);
158 counter->iptype = NFACCT_TYPE_IPV6;
159 counter->send_limit = 0;
160 counter->rcv_limit = 0;
162 return produce_net_rule(counter);
165 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
168 return STC_ERROR_INVALID_PARAMETER;
170 counter->action = NFACCT_ACTION_DELETE;
171 counter->iotype = NFACCT_COUNTER_OUT;
172 counter->jump = __get_jump_by_intend(counter);
173 counter->iptype = NFACCT_TYPE_IPV6;
174 counter->send_limit = 0;
175 counter->rcv_limit = 0;
177 return produce_net_rule(counter);
180 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
181 gpointer UNUSED user_data)
183 stc_process_key_s *key_a = (stc_process_key_s *)a;
184 stc_process_key_s *key_b = (stc_process_key_s *)b;
186 return key_a->pid - key_b->pid;
189 static void __processes_tree_value_free(gpointer data)
191 stc_process_value_s *value = (stc_process_value_s *)data;
196 static void __processes_tree_key_free(gpointer data)
198 stc_process_key_s *key = (stc_process_key_s *)data;
203 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
204 gpointer UNUSED user_data)
206 stc_app_key_s *key_a = (stc_app_key_s *)a;
207 stc_app_key_s *key_b = (stc_app_key_s *)b;
210 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
214 return g_strcmp0(key_a->app_id, key_b->app_id);
217 static void __apps_tree_value_free(gpointer data)
219 stc_app_value_s *value = (stc_app_value_s *)data;
221 g_tree_destroy(value->processes);
222 value->processes = NULL;
227 static void __apps_tree_key_free(gpointer data)
229 stc_app_key_s *key = (stc_app_key_s *)data;
236 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
237 gpointer UNUSED user_data)
239 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
240 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
243 ret = g_strcmp0(key_a->app_id, key_b->app_id);
247 ret = g_strcmp0(key_a->ifname, key_b->ifname);
251 ret = g_strcmp0(key_a->imsi, key_b->imsi);
255 ret = key_a->iftype - key_b->iftype;
262 static void __rstns_tree_value_free(gpointer data)
264 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
269 static void __rstns_tree_key_free(gpointer data)
271 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
279 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
282 stc_process_key_s *proc_key = (stc_process_key_s *)key;
283 stc_process_value_s *proc_value = (stc_process_value_s *)value;
285 STC_LOGD("Process entry => PID [%d], Ground state [%d]",
286 proc_key->pid, proc_value->ground);
290 static void __processes_tree_printall(GTree *processes)
292 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
295 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
298 stc_app_key_s *app_key = (stc_app_key_s *)key;
299 stc_app_value_s *app_value = (stc_app_value_s *)value;
301 STC_LOGD("Application info => Pkg ID [%s], App ID [%s],"
302 " Type [%d], classid [%d],"
303 " counter [ in (%lld), out (%lld)]",
304 app_key->pkg_id, app_key->app_id,
305 app_value->type, app_value->classid,
306 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
308 __processes_tree_printall(app_value->processes);
313 static void __apps_tree_printall(void)
315 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
319 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
322 remove_pid_context_s *context = (remove_pid_context_s *)data;
323 stc_app_value_s *app_value = (stc_app_value_s *)value;
325 if (!g_tree_remove(app_value->processes, context->proc_key)) {
326 STC_LOGD("key not found");
330 context->entry_removed = TRUE;
331 context->app_key = (stc_app_key_s *)key;
336 static stc_app_value_s * __application_lookup(GTree *apps,
337 const stc_app_key_s *key)
339 stc_app_value_s *lookup;
341 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
343 lookup = g_tree_lookup(apps, key);
348 static stc_process_value_s * __process_lookup(GTree *processes,
349 const stc_process_key_s *key)
351 stc_process_value_s *lookup;
353 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
355 lookup = g_tree_lookup(processes, key);
360 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
363 guint *pid_count = (guint *)data;
368 static gboolean __add_application_monitor(gpointer key, gpointer value,
371 stc_app_value_s *app_value = (stc_app_value_s *)value;
372 default_connection_s *connection = (default_connection_s *)data;
373 stc_s *stc = stc_get_manager();
375 if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
376 app_value->classid == STC_TOTAL_WIFI_CLASSID ||
377 app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
380 if (stc && connection && connection->ifname) {
381 struct nfacct_rule counter;
384 stc->carg = MALLOC0(counter_arg_s, 1);
385 if (stc->carg == NULL)
388 stc->carg->sock = stc_monitor_get_counter_socket();
391 memset(&counter, 0, sizeof(struct nfacct_rule));
393 counter.carg = stc->carg;
394 counter.classid = app_value->classid;
395 counter.intend = NFACCT_COUNTER;
396 counter.iftype = connection->type;
397 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
399 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
400 __add_iptables_in(&counter);
401 __add_iptables_out(&counter);
402 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
403 __add_ip6tables_in(&counter);
404 __add_ip6tables_out(&counter);
406 __add_iptables_in(&counter);
407 __add_iptables_out(&counter);
408 __add_ip6tables_in(&counter);
409 __add_ip6tables_out(&counter);
416 static gboolean __remove_application_monitor(gpointer key, gpointer value,
419 stc_app_value_s *app_value = (stc_app_value_s *)value;
420 default_connection_s *connection = (default_connection_s *)data;
421 stc_s *stc = stc_get_manager();
423 if (stc && connection && connection->ifname) {
424 struct nfacct_rule counter;
427 stc->carg = MALLOC0(counter_arg_s, 1);
428 if (stc->carg == NULL)
431 stc->carg->sock = stc_monitor_get_counter_socket();
434 memset(&counter, 0, sizeof(struct nfacct_rule));
436 counter.carg = stc->carg;
437 counter.classid = app_value->classid;
438 counter.intend = NFACCT_COUNTER;
439 counter.iftype = connection->type;
440 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
442 __del_iptables_in(&counter);
443 __del_iptables_out(&counter);
444 __del_ip6tables_in(&counter);
445 __del_ip6tables_out(&counter);
451 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
453 STC_LOGI("rstn info => rstn_id [%llu], "
454 "app_id [%s], classid [%lu], ifname [%s], "
455 "iftype [%d], rst_state [%d], "
456 "limit [ (%lld) bytes], "
457 "warn_limit [ (%lld) bytes], "
458 "counter [ (%lld) bytes], "
459 "roaming [%d], imsi [%s]",
460 rstn_value->restriction_id,
461 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
462 rstn_key->iftype, rstn_value->rst_state,
463 rstn_value->data_limit,
464 rstn_value->data_warn_limit,
465 rstn_value->data_counter,
466 rstn_key->roaming, rstn_key->imsi);
469 static void __process_restriction(enum traffic_restriction_type rst_type,
470 stc_rstn_key_s *rstn_key,
471 stc_rstn_value_s *rstn_value, void *data)
473 int64_t effective_data_limit, effective_data_warn_limit;
474 default_connection_s *old_connection = (default_connection_s *)data;
475 default_connection_s *connection = NULL;
477 if (old_connection != NULL)
478 connection = old_connection;
480 connection = stc_get_default_connection();
482 /* no default ifname */
483 if (connection->ifname == NULL)
486 /* rstn not applicable for this interface */
487 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
488 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
491 /* classid is invalid */
492 if (rstn_value->classid == STC_UNKNOWN_CLASSID)
495 effective_data_limit = rstn_value->data_limit;
496 effective_data_warn_limit = rstn_value->data_warn_limit;
498 if (rst_type == RST_SET) {
499 /* TODO: Change this to runtime memory */
500 table_counters_info info;
502 memset(&info, 0, sizeof(table_counters_info));
503 table_counters_get(rstn_value->restriction_id, &info);
505 effective_data_limit -= info.data_counter;
506 effective_data_warn_limit -= info.data_counter;
508 if (effective_data_limit < 0) {
509 effective_data_limit = 0;
510 rstn_value->data_limit_reached = TRUE;
513 if (effective_data_warn_limit < 0)
514 effective_data_warn_limit = 0;
516 STC_LOGD("datausage [%lld] bytes", info.data_counter);
519 STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
520 "effective_data_warn_limit [%lld] bytes",
521 rstn_value->restriction_id, effective_data_limit,
522 effective_data_warn_limit);
526 if (effective_data_limit <= 0) {
527 char *default_ifname = stc_default_connection_get_ifname();
528 struct nfacct_rule counter;
529 stc_s *stc = stc_get_manager();
532 stc->carg = MALLOC0(counter_arg_s, 1);
533 if (stc->carg == NULL) {
534 g_free(default_ifname);
539 stc_monitor_get_counter_socket();
542 counter.carg = stc->carg;
543 counter.classid = rstn_value->classid;
544 counter.intend = NFACCT_BLOCK;
545 counter.iftype = rstn_key->iftype;
546 g_strlcpy(counter.ifname, default_ifname,
549 generate_counter_name(&counter);
550 g_free(default_ifname);
553 __add_iptables_in(&counter);
554 __add_iptables_out(&counter);
557 __add_ip6tables_in(&counter);
558 __add_ip6tables_out(&counter);
561 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
562 rstn_value->data_limit_reached = FALSE;
569 char *default_ifname = stc_default_connection_get_ifname();
570 struct nfacct_rule counter;
571 stc_s *stc = stc_get_manager();
574 stc->carg = MALLOC0(counter_arg_s, 1);
575 if (stc->carg == NULL) {
576 g_free(default_ifname);
581 stc_monitor_get_counter_socket();
584 counter.carg = stc->carg;
585 counter.classid = rstn_value->classid;
586 counter.intend = NFACCT_BLOCK;
587 counter.iftype = rstn_key->iftype;
588 g_strlcpy(counter.ifname, default_ifname,
591 generate_counter_name(&counter);
592 g_free(default_ifname);
595 __del_iptables_in(&counter);
596 __del_iptables_out(&counter);
599 __del_ip6tables_in(&counter);
600 __del_ip6tables_out(&counter);
602 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
603 rstn_value->data_limit_reached = FALSE;
611 static gboolean __remove_rstns_foreach_application(gpointer key,
615 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
616 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
617 gchar *app_id = (gchar *)data;
619 /* rstn rule is not for applications */
620 if (rstn_key->app_id == NULL)
623 /* rstn rule is not for this application */
624 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
627 /* rstn rule is already removed */
628 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
631 /* remove restriction from system */
632 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
634 __print_rstn(rstn_key, rstn_value);
639 static void __remove_rstns_for_application(gchar *app_id)
641 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
645 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
647 stc_error_e ret = STC_ERROR_NONE;
649 stc_app_value_s *lookup;
651 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
653 lookup = __application_lookup(g_system->apps, app_key);
655 STC_LOGE("app_key not found");
656 return STC_ERROR_NO_DATA;
659 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
663 /* remove nfacct rule for this classid */
664 __remove_application_monitor((gpointer) app_key, lookup,
665 stc_get_default_connection());
666 __remove_rstns_for_application(app_key->app_id);
669 if (!g_tree_remove(g_system->apps, app_key)) {
670 ret = STC_ERROR_NO_DATA;
671 STC_LOGE("key not found");
677 static stc_error_e __close_contr_sock(stc_system_s *system)
679 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
681 /* close netlink socket for updating kernel counters */
682 if (system->contr_sock != -1) {
683 close(system->contr_sock);
684 system->contr_sock = -1;
687 if (system->contr_gsource_id != 0) {
688 g_source_remove(system->contr_gsource_id);
689 system->contr_gsource_id = 0;
692 return STC_ERROR_NONE;
695 static gboolean __process_contr_reply(GIOChannel *source,
696 GIOCondition condition,
699 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
701 GIOChannel *gio = NULL;
702 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
704 /* close netlink socket for updating kernel counters */
705 if (system->contr_sock != -1) {
706 close(system->contr_sock);
707 system->contr_sock = -1;
710 if (system->contr_gsource_id != 0) {
711 g_source_remove(system->contr_gsource_id);
712 system->contr_gsource_id = 0;
715 /* create netlink socket for updating kernel counters */
716 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
717 if (!(system->contr_sock)) {
718 STC_LOGE("failed to open socket");
720 return STC_ERROR_FAIL;
723 gio = g_io_channel_unix_new(system->contr_sock);
724 system->contr_gsource_id =
725 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
726 (GIOFunc) __process_contr_reply,
728 g_io_channel_unref(gio);
730 return STC_ERROR_NONE;
733 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
734 stc_rstn_value_s *rstn_value,
735 classid_bytes_context_s *context)
737 switch (context->counter->iotype) {
738 case NFACCT_COUNTER_IN:
739 case NFACCT_COUNTER_OUT:
740 rstn_value->data_counter += context->bytes;
742 if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
743 rstn_value->warn_limit_crossed_notified == FALSE) {
746 char iftype[MAX_INT_LENGTH];
747 char byte[MAX_INT_LENGTH];
748 stc_s *stc = (stc_s *)stc_get_manager();
749 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
752 rv = stc_manager_dbus_emit_signal(stc->connection,
753 STC_DBUS_SERVICE_RESTRICTION_PATH,
754 STC_DBUS_INTERFACE_RESTRICTION,
755 "WarnThresholdCrossed",
756 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
758 rstn_value->warn_limit_crossed_notified = TRUE;
760 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
761 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
762 stc_send_warn_message_to_net_popup("warn threshold crossed",
768 if (rstn_value->data_counter >= rstn_value->data_limit &&
769 rstn_value->rstn_limit_crossed_notified == FALSE) {
772 char iftype[MAX_INT_LENGTH];
773 char byte[MAX_INT_LENGTH];
774 stc_s *stc = (stc_s *)stc_get_manager();
775 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
777 /* block immediately */
778 context->counter->intend = NFACCT_BLOCK;
779 __del_iptables_in(context->counter);
780 __del_iptables_out(context->counter);
781 __add_iptables_in(context->counter);
782 __add_iptables_out(context->counter);
784 __del_ip6tables_in(context->counter);
785 __del_ip6tables_out(context->counter);
786 __add_ip6tables_in(context->counter);
787 __add_ip6tables_out(context->counter);
788 context->counter->intend = NFACCT_COUNTER;
790 rstn_value->data_limit_reached = TRUE;
793 rv = stc_manager_dbus_emit_signal(stc->connection,
794 STC_DBUS_SERVICE_RESTRICTION_PATH,
795 STC_DBUS_INTERFACE_RESTRICTION,
796 "RestrictionThresholdCrossed",
797 g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
799 rstn_value->rstn_limit_crossed_notified = TRUE;
801 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
802 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
803 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
804 "restriction_noti", rstn_key->app_id,
808 g_system->rstns_tree_updated = TRUE;
809 __print_rstn(rstn_key, rstn_value);
812 STC_LOGE("unknown iotype");
818 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
819 stc_rstn_value_s *rstn_value,
820 classid_bytes_context_s *context)
822 if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
823 context->counter->iftype == STC_IFACE_DATACALL) ||
824 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
825 context->counter->iftype == STC_IFACE_WIFI) ||
826 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
827 context->counter->iftype == STC_IFACE_BLUETOOTH)) {
828 context->counter->classid = rstn_value->classid;
829 return __rstn_counter_update(rstn_key, rstn_value, context);
835 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
840 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
841 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
842 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
845 if (context->counter->intend != NFACCT_COUNTER)
846 goto try_next_callback;
848 if (rstn_value->data_limit_reached == TRUE) {
849 context->data_limit_reached = TRUE;
850 goto try_next_callback;
853 classid = context->counter->classid;
854 rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
856 context->counter->classid = classid;
857 if (rstn_value->classid != context->counter->classid)
858 goto try_next_callback;
860 rv = __rstn_counter_update(rstn_key, rstn_value, context);
866 static gboolean __update_app_statistics(gpointer key, gpointer value,
869 stc_app_key_s *app_key = (stc_app_key_s *)key;
870 stc_app_value_s *app_value = (stc_app_value_s *)value;
871 time_t *touch_time = (time_t *)data;
872 stc_db_classid_iftype_key stat_key;
873 stc_db_app_stats stat;
874 default_connection_s *default_connection = stc_get_default_connection();
876 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
877 memset(&stat, 0 , sizeof(stc_db_app_stats));
879 stat_key.classid = app_value->classid;
880 stat_key.iftype = default_connection->type;
882 if (STC_IFACE_DATACALL == stat_key.iftype)
883 stat_key.imsi = g_strdup(default_connection->imsi);
885 stat_key.imsi = g_strdup("noneimsi");
887 g_strlcpy(stat_key.ifname, default_connection->ifname,
890 stat.app_id = g_strdup(app_key->app_id);
891 stat.snd_count = app_value->counter.out_bytes;
892 stat.rcv_count = app_value->counter.in_bytes;
893 stat.is_roaming = default_connection->roaming;
894 stat.ground = STC_APP_STATE_UNKNOWN;
896 table_statistics_insert(&stat_key, &stat, *touch_time);
898 app_value->counter.out_bytes = 0;
899 app_value->counter.in_bytes = 0;
907 static gboolean __flush_apps_stats_to_database(gpointer user_data)
909 time_t current_time = time(0);
911 if (g_system->apps_tree_updated == FALSE)
912 return G_SOURCE_REMOVE;
914 g_system->apps_tree_updated = FALSE;
917 g_tree_foreach(g_system->apps,
918 __update_app_statistics,
921 STC_LOGI("Flushed app stats to database");
922 return G_SOURCE_REMOVE;
925 static gboolean __update_counter_statistics(gpointer key, gpointer value,
928 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
929 table_counters_info info = {
930 .restriction_id = rstn_value->restriction_id,
931 .data_counter = rstn_value->data_counter
934 table_counters_update_counters(&info);
939 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
941 time_t current_time = time(0);
943 if (g_system->rstns_tree_updated == FALSE)
944 return G_SOURCE_REMOVE;
946 g_system->rstns_tree_updated = FALSE;
949 g_tree_foreach(g_system->rstns,
950 __update_counter_statistics,
953 STC_LOGI("Flushed rstns counters to database");
954 return G_SOURCE_REMOVE;
957 static void __app_counter_update(stc_app_key_s *app_key,
958 stc_app_value_s *app_value,
959 classid_bytes_context_s *context)
961 switch (context->counter->iotype) {
962 case NFACCT_COUNTER_IN:
963 app_value->data_usage.in_bytes += context->bytes;
964 app_value->counter.in_bytes = context->bytes;
965 g_system->apps_tree_updated = TRUE;
967 __apps_tree_foreach_print(app_key, app_value, NULL);
969 case NFACCT_COUNTER_OUT:
970 app_value->data_usage.out_bytes += context->bytes;
971 app_value->counter.out_bytes = context->bytes;
972 g_system->apps_tree_updated = TRUE;
974 __apps_tree_foreach_print(app_key, app_value, NULL);
977 STC_LOGE("unknown iotype");
981 static void __interface_counter_update(stc_app_key_s *app_key,
982 stc_app_value_s *app_value,
983 classid_bytes_context_s *context)
985 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
986 context->counter->iftype == STC_IFACE_DATACALL) ||
987 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
988 context->counter->iftype == STC_IFACE_WIFI) ||
989 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
990 context->counter->iftype == STC_IFACE_BLUETOOTH))
991 __app_counter_update(app_key, app_value, context);
995 static gboolean __apps_counter_update_foreach_classid(gpointer key,
999 stc_app_key_s *app_key = (stc_app_key_s *)key;
1000 stc_app_value_s *app_value = (stc_app_value_s *)value;
1001 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1003 if (context->counter->intend != NFACCT_COUNTER)
1004 goto try_next_callback;
1006 __interface_counter_update(app_key, app_value, context);
1008 if (app_value->classid != context->counter->classid)
1009 goto try_next_callback;
1011 __app_counter_update(app_key, app_value, context);
1017 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1018 struct counter_arg *carg)
1020 struct nfacct_rule counter = {
1027 classid_bytes_context_s context = {
1028 .counter = &counter,
1030 .data_limit_reached = FALSE,
1033 STC_LOGD("cnt_name %s", cnt_name);
1035 if (!recreate_counter_by_name(cnt_name, &counter)) {
1036 STC_LOGE("Can't parse counter name %s", cnt_name);
1040 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1041 context.counter->classid, context.counter->iftype,
1042 context.counter->iotype, context.counter->intend,
1043 context.counter->ifname, context.bytes);
1045 if (g_system->rstns)
1046 g_tree_foreach(g_system->rstns,
1047 __rstn_counter_update_foreach_classid,
1051 g_tree_foreach(g_system->apps,
1052 __apps_counter_update_foreach_classid,
1056 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1059 struct counter_arg *carg = user_data;
1060 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1061 if (carg->initiate) {
1063 * TODO: this will be used when daemon starts to update existing
1064 * counter data if present.
1066 populate_counters(cnt_name, carg);
1070 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1071 int bytes = be64toh(*bytes_p);
1073 ++carg->serialized_counters;
1074 __fill_nfacct_result(cnt_name, bytes, carg);
1081 static int __post_fill_counters(void *user_data)
1083 struct counter_arg *carg = user_data;
1091 static void __process_network_counter(struct genl *ans,
1092 struct counter_arg *carg)
1094 struct netlink_serialization_params ser_params = {
1097 .eval_attr = __fill_counters,
1098 .post_eval_attr = __post_fill_counters,
1101 netlink_serialization_command *netlink =
1102 netlink_create_command(&ser_params);
1104 STC_LOGE("Can not create command");
1108 netlink->deserialize_answer(&(netlink->params));
1111 static gboolean __process_contr_reply(GIOChannel *source,
1112 GIOCondition condition,
1115 int sock = g_io_channel_unix_get_fd(source);
1118 stc_s *stc = stc_get_manager();
1120 if ((condition & G_IO_ERR) ||
1121 (condition & G_IO_HUP) ||
1122 (condition & G_IO_NVAL)) {
1123 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1125 STC_LOGE("Counter socket received G_IO event, closing socket."
1126 "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
1127 (condition & G_IO_ERR), (condition & G_IO_HUP),
1128 (condition & G_IO_NVAL));
1129 __close_and_reopen_contr_sock(g_system);
1134 STC_LOGE("Can't get stc data");
1138 ret = read_netlink(sock,
1139 &ans, sizeof(struct genl));
1140 /* STC_LOGD("Counter data received ret [%d]", ret); */
1144 stc->carg->ans_len = ret;
1145 __process_network_counter(&ans, stc->carg);
1147 g_idle_add(__flush_apps_stats_to_database, NULL);
1148 g_idle_add(__flush_rstns_counter_to_database, NULL);
1153 static gboolean __update_contr_cb(void *user_data)
1155 /* Here we just sent command, answer we receive in another callback */
1156 stc_s *stc = stc_get_manager();
1157 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1159 stc->carg = MALLOC0(counter_arg_s, 1);
1160 if (stc->carg == NULL)
1161 return TRUE; /* we need to continue the timer */
1163 stc->carg->sock = stc_monitor_get_counter_socket();
1166 /* STC_LOGD("Get all counters"); */
1167 nfacct_send_get_all(stc->carg);
1169 /* we need to continue the timer */
1173 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1176 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1177 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1179 __print_rstn(rstn_key, rstn_value);
1183 static void __rstn_tree_printall(void)
1185 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1188 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1189 const stc_rstn_key_s *key)
1191 stc_rstn_value_s *lookup;
1193 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1195 lookup = g_tree_lookup(rstns_tree, key);
1200 static gboolean __remove_restriction(gpointer key, gpointer value,
1203 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1204 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1206 /* rstn rule is already removed */
1207 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
1210 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1211 __print_rstn(rstn_key, rstn_value);
1215 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1218 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1219 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1221 /* rstn rule is activated */
1222 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1225 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1226 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1228 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1230 __print_rstn(rstn_key, rstn_value);
1235 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1237 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1238 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1240 /* rstn rule is activated */
1241 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1244 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1245 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1247 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1252 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1254 stc_rstn_value_s *lookup_value;
1256 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1258 lookup_value = __rstn_lookup(g_system->rstns, key);
1259 if (!lookup_value) {
1260 STC_LOGE("key not found");
1261 return STC_ERROR_NO_DATA;
1264 __remove_restriction(key, lookup_value, NULL);
1266 /* remove counter also */
1267 table_counters_delete(lookup_value->restriction_id);
1269 if (!g_tree_remove(g_system->rstns, key)) {
1270 STC_LOGD("key not found");
1271 return STC_ERROR_NO_DATA;
1274 return STC_ERROR_NONE;
1277 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1278 stc_rstn_value_s *value, gboolean debug)
1280 stc_rstn_value_s *rstn_value;
1282 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1284 rstn_value = __rstn_lookup(g_system->rstns, key);
1286 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1288 STC_LOGE("rstn_key allocation failed");
1289 return STC_ERROR_OUT_OF_MEMORY;
1292 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1294 STC_LOGE("rstn_value allocation failed");
1296 return STC_ERROR_OUT_OF_MEMORY;
1299 rstn_key->app_id = g_strdup(key->app_id);
1300 rstn_key->ifname = g_strdup(key->ifname);
1301 rstn_key->imsi = g_strdup(key->imsi);
1302 rstn_key->iftype = key->iftype;
1303 rstn_key->roaming = key->roaming;
1305 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1308 rstn_value->restriction_id = value->restriction_id;
1309 rstn_value->rst_state = value->rst_state;
1310 rstn_value->classid = value->classid;
1311 rstn_value->data_limit = value->data_limit;
1312 rstn_value->data_warn_limit = value->data_warn_limit;
1313 rstn_value->data_counter = 0;
1314 rstn_value->warn_limit_crossed_notified = FALSE;
1315 rstn_value->rstn_limit_crossed_notified = FALSE;
1318 __add_restriction_debug(key, rstn_value, NULL);
1320 __add_restriction(key, rstn_value, NULL);
1322 return STC_ERROR_NONE;
1325 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1328 stc_cb_ret_e ret = STC_CONTINUE;
1331 stc_rstn_value_s value;
1333 memset(&key, 0, sizeof(stc_rstn_key_s));
1334 memset(&value, 0, sizeof(stc_rstn_value_s));
1336 key.app_id = g_strdup(info->app_id);
1337 key.ifname = g_strdup(info->ifname);
1338 key.imsi = g_strdup(info->imsi);
1339 key.iftype = info->iftype;
1340 key.roaming = info->roaming;
1342 value.rst_state = info->rst_state;
1343 value.restriction_id = info->restriction_id;
1345 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1346 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1348 value.classid = STC_UNKNOWN_CLASSID;
1350 value.data_limit = info->data_limit;
1351 value.data_warn_limit = info->data_warn_limit;
1353 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1362 static void __fill_restritions_list(void)
1364 table_restrictions_foreach(__insert_restriction_cb, NULL);
1365 //__rstn_tree_printall();
1368 static gboolean __add_rstn_foreach_application(gpointer key,
1372 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1373 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1374 gchar *app_id = (gchar *)data;
1376 /* rstn rule is not for applications */
1377 if (rstn_key->app_id == NULL)
1380 /* rstn rule is not for this application */
1381 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1384 /* rstn rule is already applied */
1385 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1388 /* add restriction to system */
1389 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1390 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1392 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1394 __print_rstn(rstn_key, rstn_value);
1399 static void __add_rstns_for_application(gchar *app_id)
1401 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1405 static void __add_application_by_interface(const char *app_id)
1407 stc_app_key_s app_key;
1408 stc_app_value_s app_value;
1413 memset(&app_key, 0, sizeof(stc_app_key_s));
1414 memset(&app_value, 0, sizeof(stc_app_value_s));
1416 app_key.pkg_id = g_strdup(app_id);
1417 app_key.app_id = g_strdup(app_id);
1419 app_value.type = STC_APP_TYPE_NONE;
1420 app_value.processes = NULL;
1421 app_value.counter.in_bytes = 0;
1422 app_value.counter.out_bytes = 0;
1424 stc_monitor_application_add(app_key, app_value);
1426 FREE(app_key.pkg_id);
1427 FREE(app_key.app_id);
1430 static int __vconf_get_int(const char *key, int *value)
1434 ret = vconf_get_int(key, value);
1435 if (ret != VCONF_OK) {
1436 STC_LOGE("Failed to get vconfkey [%s] value", key);
1443 static int __vconf_set_int(const char *key, int value)
1447 ret = vconf_set_int(key, value);
1448 if (ret != VCONF_OK) {
1449 STC_LOGE("Failed to set vconfkey [%s] value", key);
1456 static guint __get_background_state(void)
1458 return g_system->background_state;;
1461 static void __set_background_state(guint state)
1463 g_system->background_state = state;
1466 static gboolean __processes_tree_foreach_background(gpointer key,
1470 stc_process_key_s *proc_key = (stc_process_key_s *)key;
1471 stc_app_key_s *app_key = (stc_app_key_s *)data;
1473 if (g_system->background_state)
1474 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1476 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1481 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1484 stc_app_key_s *app_key = (stc_app_key_s *)key;
1485 stc_app_value_s *app_value = (stc_app_value_s *)value;
1487 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1488 g_tree_foreach(app_value->processes,
1489 __processes_tree_foreach_background, app_key);
1494 static stc_error_e __process_update_background(void)
1496 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1498 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1500 return STC_ERROR_NONE;
1503 static void __excn_hash_foreach_print(gpointer key, gpointer value,
1506 const char *process_name = key;
1507 const char *exe_type = value;
1509 STC_LOGI("excn info => process_name [%s] exe_type [%s]",
1510 process_name, exe_type);
1513 static void __excn_hash_printall(void)
1515 g_hash_table_foreach(g_system->excns_hash,
1516 __excn_hash_foreach_print, NULL);
1519 static gboolean __remove_exception_app(gpointer key, gpointer value,
1522 const char *exe_type = value;
1524 if (g_strcmp0(exe_type, EXE_TYPE_APPLICATION) == 0)
1530 static void __remove_exception_appall(void)
1532 g_hash_table_foreach_remove(g_system->excns_hash,
1533 __remove_exception_app, NULL);
1536 static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info,
1539 stc_cb_ret_e ret = STC_CONTINUE;
1541 if (g_hash_table_insert(g_system->excns_hash,
1542 g_strdup(info->process_name),
1543 g_strdup(info->exe_type)) != TRUE)
1549 static void __fill_exceptions_list(void)
1551 table_exceptions_foreach(__insert_exception_cb, NULL);
1552 pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
1554 /* __excn_hash_printall(); */
1557 static gboolean __update_exceptions_app_list(void *user_data)
1559 __remove_exception_appall();
1560 pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
1562 /* __excn_hash_printall(); */
1567 stc_error_e stc_monitor_init(void)
1569 stc_system_s *system = MALLOC0(stc_system_s, 1);
1570 GIOChannel *gio = NULL;
1572 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1574 /* initializing cgroups */
1577 /* creating monitored application tree */
1578 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1579 __apps_tree_key_free,
1580 __apps_tree_value_free);
1582 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1583 __rstns_tree_key_free,
1584 __rstns_tree_value_free);
1586 system->excns_hash = g_hash_table_new_full(g_str_hash,
1587 g_str_equal, g_free, g_free);
1589 /* create netlink socket for updating kernel counters */
1590 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1591 if (!(system->contr_sock)) {
1592 STC_LOGE("failed to open socket");
1594 return STC_ERROR_FAIL;
1597 gio = g_io_channel_unix_new(system->contr_sock);
1598 system->contr_gsource_id =
1599 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1600 (GIOFunc) __process_contr_reply,
1602 g_io_channel_unref(gio);
1606 __add_application_by_interface(STC_TOTAL_DATACALL);
1607 __add_application_by_interface(STC_TOTAL_WIFI);
1608 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1609 __add_application_by_interface(STC_TOTAL_IPV4);
1610 __add_application_by_interface(STC_TOTAL_IPV6);
1612 /* creating restriction rules tree */
1613 __update_contr_cb(NULL);
1615 /* registering periodic kernel counters update callback */
1616 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1619 if (g_system->contr_timer_id == 0) {
1620 STC_LOGE("Failed to register kernel counters update timer");
1621 __close_contr_sock(g_system);
1622 return STC_ERROR_FAIL;
1625 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1626 (int *)&g_system->background_state);
1628 __fill_exceptions_list();
1629 __fill_restritions_list();
1631 g_system->excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL,
1632 __update_exceptions_app_list,
1635 return STC_ERROR_NONE;
1638 stc_error_e stc_monitor_deinit(void)
1640 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1642 /* close netlink socket for updating kernel counters */
1643 __close_contr_sock(g_system);
1645 /* remove kernel counters update timer */
1646 if (g_system->contr_timer_id > 0) {
1647 g_source_remove(g_system->contr_timer_id);
1648 g_system->contr_timer_id = 0;
1651 /* remove exceptions app list update timer */
1652 if (g_system->excns_timer_id > 0) {
1653 g_source_remove(g_system->excns_timer_id);
1654 g_system->excns_timer_id = 0;
1657 /* destroy monitored application tree */
1658 g_tree_destroy(g_system->apps);
1659 g_system->apps = NULL;
1661 /* destroy restriction rules tree */
1662 g_tree_destroy(g_system->rstns);
1663 g_system->rstns = NULL;
1665 /* destroy exception hash table */
1666 g_hash_table_destroy(g_system->excns_hash);
1667 g_system->excns_hash = NULL;
1671 return STC_ERROR_NONE;
1674 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1675 const stc_app_value_s app_value)
1677 stc_error_e ret = STC_ERROR_NONE;
1679 stc_app_value_s *value;
1680 stc_app_value_s *lookup;
1682 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1684 lookup = __application_lookup(g_system->apps, &app_key);
1686 STC_LOGD("app_key already present");
1687 return STC_ERROR_NONE;
1690 key = MALLOC0(stc_app_key_s, 1);
1692 STC_LOGE("key allocation failed");
1693 return STC_ERROR_OUT_OF_MEMORY;
1696 value = MALLOC0(stc_app_value_s, 1);
1698 STC_LOGE("value allocation failed");
1700 return STC_ERROR_OUT_OF_MEMORY;
1703 key->app_id = g_strdup(app_key.app_id);
1704 key->pkg_id = g_strdup(app_key.pkg_id);
1706 value->type = app_value.type;
1707 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1708 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1710 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1711 __processes_tree_key_free,
1712 __processes_tree_value_free);
1714 /* create cgroup and update classid */
1715 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1717 g_tree_insert(g_system->apps, key, value);
1719 /* add nfacct rule for this classid */
1720 __add_application_monitor(key, value, stc_get_default_connection());
1721 __add_rstns_for_application(app_key.app_id);
1726 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1727 const stc_process_key_s proc_key,
1728 const stc_process_value_s proc_value)
1730 stc_error_e ret = STC_ERROR_NONE;
1731 stc_app_value_s *app_lookup;
1732 stc_process_key_s *key;
1733 stc_process_value_s *value;
1734 stc_process_value_s *proc_lookup;
1736 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1738 app_lookup = __application_lookup(g_system->apps, &app_key);
1740 STC_LOGD("app_key not found");
1741 return STC_ERROR_FAIL;
1744 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1746 STC_LOGD("proc_key already present");
1747 return STC_ERROR_NONE;
1750 key = MALLOC0(stc_process_key_s, 1);
1752 STC_LOGE("key allocation failed");
1753 return STC_ERROR_OUT_OF_MEMORY;
1756 value = MALLOC0(stc_process_value_s, 1);
1758 STC_LOGE("value allocation failed");
1760 return STC_ERROR_OUT_OF_MEMORY;
1763 key->pid = proc_key.pid;
1765 value->ground = proc_value.ground;
1767 g_tree_insert(app_lookup->processes, key, value);
1769 /* add pid to application cgroup */
1770 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1775 stc_error_e stc_monitor_process_remove(pid_t pid)
1777 stc_error_e ret = STC_ERROR_NONE;
1778 stc_process_key_s proc_key = {
1782 remove_pid_context_s context = {
1784 .proc_key = &proc_key,
1785 .entry_removed = FALSE,
1788 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1790 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1793 if (context.entry_removed)
1794 __application_remove_if_empty(context.app_key);
1799 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1800 const stc_process_key_s proc_key,
1801 stc_app_state_e ground)
1803 stc_error_e ret = STC_ERROR_NONE;
1804 stc_app_value_s *app_lookup;
1805 stc_process_value_s *proc_lookup;
1807 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1809 app_lookup = __application_lookup(g_system->apps, &app_key);
1811 STC_LOGD("app_key not found");
1812 return STC_ERROR_FAIL;
1815 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1817 STC_LOGD("proc_key not found");
1818 return STC_ERROR_FAIL;
1821 if (proc_lookup->ground != ground)
1822 proc_lookup->ground = ground;
1824 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1825 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1827 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1832 void stc_monitor_update_rstn_by_default_connection(void *data)
1834 static default_connection_s old_connection;
1835 default_connection_s *new_connection = (default_connection_s *)data;
1837 if (old_connection.path != NULL) {
1839 g_tree_foreach(g_system->apps,
1840 __remove_application_monitor,
1841 (gpointer)&old_connection);
1843 if (g_system->rstns)
1844 g_tree_foreach(g_system->rstns,
1845 __remove_restriction,
1846 (gpointer)&old_connection);
1849 FREE(old_connection.path);
1850 FREE(old_connection.ifname);
1851 old_connection.type = 0;
1852 old_connection.roaming = 0;
1854 if (new_connection != NULL && new_connection->path != NULL) {
1856 g_tree_foreach(g_system->apps,
1857 __add_application_monitor,
1858 (gpointer)new_connection);
1860 if (g_system->rstns)
1861 g_tree_foreach(g_system->rstns, __add_restriction,
1864 old_connection.path = g_strdup(new_connection->path);
1865 old_connection.ifname = g_strdup(new_connection->ifname);
1866 old_connection.type = new_connection->type;
1867 old_connection.roaming = new_connection->roaming;
1871 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1876 stc_rstn_value_s value;
1878 memset(&key, 0, sizeof(stc_rstn_key_s));
1879 memset(&value, 0, sizeof(stc_rstn_value_s));
1881 key.app_id = g_strdup(info->app_id);
1882 key.ifname = g_strdup(info->ifname);
1883 key.imsi = g_strdup(info->imsi);
1884 key.iftype = info->iftype;
1885 key.roaming = info->roaming;
1887 value.rst_state = info->rst_state;
1888 value.restriction_id = info->restriction_id;
1890 if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1891 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1893 value.classid = STC_UNKNOWN_CLASSID;
1895 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1896 __set_background_state(TRUE);
1897 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
1898 __process_update_background();
1901 value.data_limit = info->data_limit;
1902 value.data_warn_limit = info->data_warn_limit;
1904 ret = __rstn_tree_add(&key, &value, TRUE);
1912 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1916 stc_rstn_key_s key = {
1917 .app_id = g_strdup(info->app_id),
1918 .ifname = g_strdup(info->ifname),
1919 .imsi = g_strdup(info->imsi),
1920 .iftype = info->iftype,
1921 .roaming = info->roaming,
1924 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
1925 __set_background_state(FALSE);
1926 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
1927 __process_update_background();
1930 ret = __rstn_tree_remove(&key);
1938 stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
1940 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1942 char *exe_type = NULL;
1944 exe_type = g_hash_table_lookup(g_system->excns_hash, cmdline);
1946 return STC_ERROR_NO_DATA;
1948 return STC_ERROR_NONE;
1951 int stc_monitor_get_counter_socket(void)
1953 return g_system->contr_sock;