Organize log for print tree
[platform/core/connectivity/stc-manager.git] / src / monitor / stc-monitor.c
index b73a889..cbd6f11 100755 (executable)
@@ -283,6 +283,7 @@ static void __rstns_tree_key_free(gpointer data)
        FREE(key);
 }
 
+/*
 //LCOV_EXCL_START
 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
                                               gpointer data)
@@ -322,6 +323,7 @@ static void __apps_tree_printall(void)
        g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
 }
 //LCOV_EXCL_STOP
+*/
 
 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
                                               gpointer data)
@@ -329,11 +331,8 @@ static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
        remove_pid_context_s *context = (remove_pid_context_s *)data;
        stc_app_value_s *app_value = (stc_app_value_s *)value;
 
-       if (!g_tree_remove(app_value->processes, context->proc_key)) {
-               if (STC_DEBUG_LOG)
-                       STC_LOGD("key not found"); //LCOV_EXCL_LINE
+       if (!g_tree_remove(app_value->processes, context->proc_key))
                return FALSE;
-       }
 
        context->entry_removed = TRUE;
        context->app_key = (stc_app_key_s *)key;
@@ -403,8 +402,16 @@ static gboolean __add_application_monitor(gpointer key, gpointer value,
                counter.carg = stc->carg;
                counter.classid = app_value->classid;
                counter.intend = NFACCT_COUNTER;
-               counter.iftype = connection->type;
-               g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
+
+               if (connection->tether_state == TRUE &&
+                       connection->tether_iface.ifname != NULL &&
+                       app_value->classid == STC_TETHERING_APP_CLASSID) {
+                       counter.iftype = connection->tether_iface.type;
+                       g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
+               } else {
+                       counter.iftype = connection->type;
+                       g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
+               }
 
                if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
                        __add_iptables_in(&counter);
@@ -446,8 +453,16 @@ static gboolean __remove_application_monitor(gpointer key, gpointer value,
                counter.carg = stc->carg;
                counter.classid = app_value->classid;
                counter.intend = NFACCT_COUNTER;
-               counter.iftype = connection->type;
-               g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
+
+               if (connection->tether_state == FALSE &&
+                       connection->tether_iface.ifname != NULL &&
+                       app_value->classid == STC_TETHERING_APP_CLASSID) {
+                       counter.iftype = connection->tether_iface.type;
+                       g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
+               } else {
+                       counter.iftype = connection->type;
+                       g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
+               }
 
                __del_iptables_in(&counter);
                __del_iptables_out(&counter);
@@ -461,7 +476,7 @@ static gboolean __remove_application_monitor(gpointer key, gpointer value,
 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
 {
        STC_LOGI("rstn info => rstn_id [%llu], "
-                "app_id [%s], classid [%lu], ifname [%s], "
+                "app_id [%s], classid [%u], ifname [%s], "
                 "iftype [%d], rstn_state [%d], rstn_type [%d], "
                 "limit [ (%lld) bytes], "
                 "warn_limit [ (%lld) bytes], "
@@ -480,6 +495,7 @@ static void __add_iptables_rule(int64_t classid, nfacct_rule_intend intend,
                                stc_iface_type_e iftype)
 {
        char *default_ifname = stc_default_connection_get_ifname();
+       default_connection_s *connection = stc_get_default_connection();
        struct nfacct_rule counter;
        stc_s *stc = stc_get_manager();
        if (!stc) {
@@ -500,8 +516,16 @@ static void __add_iptables_rule(int64_t classid, nfacct_rule_intend intend,
        counter.carg = stc->carg;
        counter.classid = classid;
        counter.intend = intend;
-       counter.iftype = iftype;
-       g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
+
+       if (connection && connection->tether_iface.ifname != NULL &&
+               classid == STC_TETHERING_APP_CLASSID) {
+               counter.iftype = connection->tether_iface.type;
+               g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
+       } else {
+               counter.iftype = iftype;
+               g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
+       }
+
        g_free(default_ifname);
 
        /* iptables rule */
@@ -517,6 +541,7 @@ static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend,
                                stc_iface_type_e iftype)
 {
        char *default_ifname = stc_default_connection_get_ifname();
+       default_connection_s *connection = stc_get_default_connection();
        struct nfacct_rule counter;
        stc_s *stc = stc_get_manager();
        if (!stc) {
@@ -537,8 +562,16 @@ static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend,
        counter.carg = stc->carg;
        counter.classid = classid;
        counter.intend = intend;
-       counter.iftype = iftype;
-       g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
+
+       if (connection && connection->tether_iface.ifname != NULL &&
+               classid == STC_TETHERING_APP_CLASSID) {
+               counter.iftype = connection->tether_iface.type;
+               g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
+       } else {
+               counter.iftype = iftype;
+               g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
+       }
+
        g_free(default_ifname);
 
        /* iptables rule */
@@ -569,7 +602,8 @@ static void __process_restriction(enum traffic_restriction_type rstn_type,
 
        /* rstn not applicable for this interface */
        if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
-           g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
+           (g_strcmp0(connection->ifname, rstn_key->ifname) != 0) &&
+               (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
                return;
 
        /* classid is invalid */
@@ -622,8 +656,12 @@ static void __process_restriction(enum traffic_restriction_type rstn_type,
                rstn_value->data_limit_reached = TRUE;
                break;
        case RST_UNSET:
-               __del_iptables_rule(rstn_value->classid, rstn_value->rstn_type,
-                                   rstn_key->iftype);
+               if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
+                       __del_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
+                                           rstn_key->iftype);
+               else
+                       __del_iptables_rule(rstn_value->classid, rstn_value->rstn_type,
+                                           rstn_key->iftype);
 
                rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
                rstn_value->data_limit_reached = FALSE;
@@ -848,11 +886,19 @@ static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
                                                classid_bytes_context_s *context)
 {
        if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
-            context->counter->iftype == STC_IFACE_DATACALL) ||
-           (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
-            context->counter->iftype == STC_IFACE_WIFI) ||
-           (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
-            context->counter->iftype == STC_IFACE_BLUETOOTH)) {
+               context->counter->iftype == STC_IFACE_DATACALL) ||
+               (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
+               context->counter->iftype == STC_IFACE_WIFI) ||
+               (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
+               context->counter->iftype == STC_IFACE_BLUETOOTH) ||
+               (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
+                context->counter->iftype == STC_IFACE_WIFI) ||
+               (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
+                context->counter->iftype == STC_IFACE_BLUETOOTH) ||
+               (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
+                context->counter->iftype == STC_IFACE_USB) ||
+               (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
+                context->counter->iftype == STC_IFACE_P2P)) {
                context->counter->classid = rstn_value->classid;
                return __rstn_counter_update(rstn_key, rstn_value, context);
        }
@@ -905,16 +951,40 @@ static gboolean __update_app_statistics(gpointer key, gpointer value,
        memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
        memset(&stat, 0 , sizeof(stc_db_app_stats));
 
+       /* Do not update statistics for Tethering
+        * if tethering is in-active found */
+       if (default_connection &&
+               default_connection->tether_state == FALSE &&
+               !strcmp(app_key->app_id, STC_TOTAL_TETHERING))
+               return FALSE;
+
+       /* Do not update statistics for Wi-Fi
+        * if tethering is active on wlan0 iface */
+       if (default_connection && default_connection->tether_state &&
+               default_connection->tether_iface.type == STC_IFACE_WIFI &&
+               !strcmp(app_key->app_id, STC_TOTAL_WIFI))
+               return FALSE;
+
        stat_key.classid = app_value->classid;
-       stat_key.iftype = default_connection->type;
+
+       if (app_value->classid == STC_TETHERING_APP_CLASSID &&
+               default_connection->tether_state == TRUE)
+               stat_key.iftype = default_connection->tether_iface.type;
+       else
+               stat_key.iftype = default_connection->type;
 
        if (STC_IFACE_DATACALL == stat_key.iftype)
                stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
        else
                stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
 
-       g_strlcpy(stat_key.ifname, default_connection->ifname,
-                 MAX_IFACE_LENGTH);
+       if (app_value->classid == STC_TETHERING_APP_CLASSID &&
+               default_connection->tether_state == TRUE)
+               g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
+                         MAX_IFACE_LENGTH);
+       else
+               g_strlcpy(stat_key.ifname, default_connection->ifname,
+                         MAX_IFACE_LENGTH);
 
        stat.app_id = g_strdup(app_key->app_id);
        stat.snd_count = app_value->counter.out_bytes;
@@ -1003,16 +1073,18 @@ static void __app_counter_update(stc_app_key_s *app_key,
                app_value->counter.in_bytes = context->bytes;
                g_system->apps_tree_updated = TRUE;
 
-               if (STC_DEBUG_LOG)
-                       __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
+               /*
+               __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
+               */
                break;
        case NFACCT_COUNTER_OUT:
                app_value->data_usage.out_bytes += context->bytes;
                app_value->counter.out_bytes = context->bytes;
                g_system->apps_tree_updated = TRUE;
 
-               if (STC_DEBUG_LOG)
-                       __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
+               /*
+               __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
+               */
                break;
        default:
                STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
@@ -1028,7 +1100,15 @@ static void __interface_counter_update(stc_app_key_s *app_key,
            (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
             context->counter->iftype == STC_IFACE_WIFI) ||
            (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
-            context->counter->iftype == STC_IFACE_BLUETOOTH))
+            context->counter->iftype == STC_IFACE_BLUETOOTH) ||
+               (app_value->classid == STC_TETHERING_APP_CLASSID &&
+                context->counter->iftype == STC_IFACE_WIFI) ||
+               (app_value->classid == STC_TETHERING_APP_CLASSID &&
+                context->counter->iftype == STC_IFACE_BLUETOOTH) ||
+               (app_value->classid == STC_TETHERING_APP_CLASSID &&
+                context->counter->iftype == STC_IFACE_USB) ||
+               (app_value->classid == STC_TETHERING_APP_CLASSID &&
+                context->counter->iftype == STC_IFACE_P2P))
                __app_counter_update(app_key, app_value, context);
 }
 
@@ -1079,10 +1159,11 @@ static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
                return; //LCOV_EXCL_LINE
        }
 
-       STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
-                context.counter->classid, context.counter->iftype,
-                context.counter->iotype, context.counter->intend,
-                context.counter->ifname, context.bytes);
+       if (STC_DEBUG_LOG)
+               STC_LOGI("classid %u, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
+                       context.counter->classid, context.counter->iftype,
+                       context.counter->iotype, context.counter->intend,
+                       context.counter->ifname, context.bytes);
 
        if (g_system->rstns)
                g_tree_foreach(g_system->rstns,
@@ -1169,7 +1250,7 @@ static gboolean __process_contr_reply(GIOChannel *source,
                /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
 
                STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
-                        "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
+                        "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
                         (condition & G_IO_ERR), (condition & G_IO_HUP),
                         (condition & G_IO_NVAL));
                __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
@@ -1228,6 +1309,7 @@ static gboolean __update_contr_cb(void *user_data)
        return TRUE;
 }
 
+/*
 //LCOV_EXCL_START
 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
                                          gpointer data)
@@ -1243,7 +1325,8 @@ static void __rstn_tree_printall(void)
 {
        g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
 }
-//LCOV_EXCL_END
+//LCOV_EXCL_STOP
+*/
 
 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
                                        const stc_rstn_key_s *key)
@@ -1426,8 +1509,7 @@ static void __fill_restritions_list(void)
 {
        table_restrictions_foreach(__insert_restriction_cb, NULL);
 
-       if (STC_DEBUG_LOG)
-               __rstn_tree_printall();
+       /* __rstn_tree_printall(); */
 }
 
 static gboolean __add_rstn_foreach_application(gpointer key,
@@ -1617,6 +1699,7 @@ stc_error_e stc_monitor_init(void)
        __add_application_by_interface(STC_TOTAL_BLUETOOTH);
        __add_application_by_interface(STC_TOTAL_IPV4);
        __add_application_by_interface(STC_TOTAL_IPV6);
+       __add_application_by_interface(STC_TOTAL_TETHERING);
 
        /* creating restriction rules tree */
        __update_contr_cb(NULL);
@@ -1678,7 +1761,8 @@ API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
 
        lookup = __application_lookup(g_system->apps, &app_key);
        if (lookup) {
-               STC_LOGD("app_key already present"); //LCOV_EXCL_LINE
+               if (STC_DEBUG_LOG)
+                       STC_LOGD("app_key already present"); //LCOV_EXCL_LINE
                return STC_ERROR_NONE; //LCOV_EXCL_LINE
        }
 
@@ -1732,13 +1816,15 @@ API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
 
        app_lookup = __application_lookup(g_system->apps, &app_key);
        if (!app_lookup) {
-               STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
+               if (STC_DEBUG_LOG)
+                       STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
                return STC_ERROR_FAIL; //LCOV_EXCL_LINE
        }
 
        proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
        if (proc_lookup) {
-               STC_LOGD("proc_key already present"); //LCOV_EXCL_LINE
+               if (STC_DEBUG_LOG)
+                       STC_LOGD("proc_key already present"); //LCOV_EXCL_LINE
                return STC_ERROR_NONE; //LCOV_EXCL_LINE
        }
 
@@ -1764,8 +1850,9 @@ API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
        /* add pid to application cgroup */
        place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
 
-       if (STC_DEBUG_LOG)
-               __apps_tree_printall(); //LCOV_EXCL_LINE
+       /*
+       __apps_tree_printall(); //LCOV_EXCL_LINE
+       */
 
        return ret;
 }
@@ -1791,8 +1878,9 @@ API stc_error_e stc_monitor_process_remove(pid_t pid)
        if (context.entry_removed)
                __application_remove_if_empty(context.app_key);
 
-       if (STC_DEBUG_LOG)
-               __apps_tree_printall(); //LCOV_EXCL_LINE
+       /*
+       __apps_tree_printall(); //LCOV_EXCL_LINE
+       */
 
        return ret;
 }
@@ -1855,8 +1943,11 @@ void stc_monitor_update_rstn_by_default_connection(void *data)
 
        FREE(old_connection.path);
        FREE(old_connection.ifname);
+       FREE(old_connection.tether_iface.ifname);
        old_connection.type = 0;
        old_connection.roaming = 0;
+       old_connection.tether_state = FALSE;
+       old_connection.tether_iface.type = 0;
 
        if (new_connection != NULL && new_connection->path != NULL) {
                if (g_system->apps)
@@ -1870,8 +1961,11 @@ void stc_monitor_update_rstn_by_default_connection(void *data)
 
                old_connection.path = g_strdup(new_connection->path);
                old_connection.ifname = g_strdup(new_connection->ifname);
+               old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
                old_connection.type = new_connection->type;
                old_connection.roaming = new_connection->roaming;
+               old_connection.tether_state = new_connection->tether_state;
+               old_connection.tether_iface.type = new_connection->tether_iface.type;
        }
 }