From 1793fa4cbbdb014706bdbdc58e84c400c5f52a2b Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 2 May 2018 17:05:10 +0900 Subject: [PATCH 01/16] Removed exclude restriction method Change-Id: Id61aa10111a725ce19197896a982496c4ad0f75e --- include/stc-restriction.h | 5 --- interfaces/stcmanager-iface-restriction.xml | 5 --- packaging/stc-manager.spec | 2 +- src/stc-manager-gdbus.c | 3 -- src/stc-restriction.c | 56 ++++------------------------- 5 files changed, 8 insertions(+), 63 deletions(-) diff --git a/include/stc-restriction.h b/include/stc-restriction.h index 9fcc7e3..9f5560a 100755 --- a/include/stc-restriction.h +++ b/include/stc-restriction.h @@ -47,11 +47,6 @@ gboolean handle_restriction_set(StcRestriction *object, GVariant *parameters, void *user_data); -gboolean handle_restriction_exclude(StcRestriction *object, - GDBusMethodInvocation *invocation, - GVariant *parameters, - void *user_data); - gboolean handle_restriction_unset(StcRestriction *object, GDBusMethodInvocation *invocation, GVariant *parameters, diff --git a/interfaces/stcmanager-iface-restriction.xml b/interfaces/stcmanager-iface-restriction.xml index 3ecc542..1fbfc3f 100644 --- a/interfaces/stcmanager-iface-restriction.xml +++ b/interfaces/stcmanager-iface-restriction.xml @@ -5,11 +5,6 @@ - - - - - diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index afafa95..65d6175 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.58 +Version: 0.0.59 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index 3170c42..a31deb0 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -125,9 +125,6 @@ static gboolean __stc_manager_gdbus_restriction_init(stc_s *stc) g_signal_connect(restriction, "handle-set", G_CALLBACK(handle_restriction_set), stc); - g_signal_connect(restriction, "handle-exclude", - G_CALLBACK(handle_restriction_exclude), stc); - g_signal_connect(restriction, "handle-get", G_CALLBACK(handle_restriction_get), stc); diff --git a/src/stc-restriction.c b/src/stc-restriction.c index 05d7544..242cdcb 100755 --- a/src/stc-restriction.c +++ b/src/stc-restriction.c @@ -52,8 +52,7 @@ void __initialize_rstn_rule(table_restrictions_info *rule) rule->subscriber_id = NULL; } -gboolean __validate_rstn_rule(table_restrictions_info *rule, - enum traffic_restriction_type rstn_type) +gboolean __validate_rstn_rule(table_restrictions_info *rule) { __STC_LOG_FUNC_ENTER__; @@ -62,11 +61,6 @@ gboolean __validate_rstn_rule(table_restrictions_info *rule, return FALSE; //LCOV_EXCL_LINE } - if (rstn_type <= RST_UNDEFINDED || rstn_type >= RST_MAX_VALUE) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return FALSE; //LCOV_EXCL_LINE - } - if (rule->iftype <= STC_IFACE_UNKNOWN || rule->iftype >= STC_IFACE_LAST_ELEM) { __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE @@ -214,6 +208,10 @@ static void __stc_extract_restriction_rule(const char *key, GVariant *value, rule->subscriber_id = g_strdup(str); STC_LOGD("subscriber_id: [%s]", rule->subscriber_id); + } else if (!g_strcmp0(key, "type")) { + rule->rstn_type = g_variant_get_uint16(value); + STC_LOGD("type: [%u]", (unsigned int) rule->rstn_type); + } else { STC_LOGD("Unknown select rule"); //LCOV_EXCL_LINE } @@ -241,47 +239,7 @@ gboolean handle_restriction_set(StcRestriction *object, g_variant_iter_free(iter); } - rule.rstn_type = STC_RSTN_TYPE_DROP; - - if (__validate_rstn_rule(&rule, RST_SET) == FALSE) { - STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE - STC_ERROR_INVALID_PARAMETER); - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return TRUE; - } - - table_restrictions_update(&rule); - /* update restriction rule in runtime structure */ - stc_monitor_rstns_tree_add(&rule); - - STC_DBUS_REPLY_ERROR_NONE(invocation); - __STC_LOG_FUNC_EXIT__; - return TRUE; -} - -gboolean handle_restriction_exclude(StcRestriction *object, - GDBusMethodInvocation *invocation, - GVariant *parameters, - void *user_data) -{ - __STC_LOG_FUNC_ENTER__; - GVariantIter *iter = NULL; - table_restrictions_info rule; - - memset(&rule, 0, sizeof(table_restrictions_info)); - __initialize_rstn_rule(&rule); - - g_variant_get(parameters, "a{sv}", &iter); - if (iter != NULL) { - stc_manager_gdbus_dict_foreach(iter, - __stc_extract_restriction_rule, - &rule); - g_variant_iter_free(iter); - } - - rule.rstn_type = STC_RSTN_TYPE_ACCEPT; - - if (__validate_rstn_rule(&rule, RST_EXCLUDE) == FALSE) { + if (__validate_rstn_rule(&rule) == FALSE) { STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE STC_ERROR_INVALID_PARAMETER); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE @@ -317,7 +275,7 @@ gboolean handle_restriction_unset(StcRestriction *object, g_variant_iter_free(iter); } - if (__validate_rstn_rule(&rule, RST_UNSET) == FALSE) { + if (__validate_rstn_rule(&rule) == FALSE) { STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE STC_ERROR_INVALID_PARAMETER); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE -- 2.7.4 From 2456ee39ff1f11990fc371f51d9e6a0de505bbb6 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Fri, 4 May 2018 13:09:03 +0900 Subject: [PATCH 02/16] Replaced rule type to direction Change-Id: Ia158c9e54167d1da971d79d14940c6464b97abd3 Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- src/helper/helper-firewall.c | 2 +- src/helper/helper-iptables.c | 14 +++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 65d6175..b85d523 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.59 +Version: 0.0.60 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-firewall.c b/src/helper/helper-firewall.c index aab377f..1df9621 100755 --- a/src/helper/helper-firewall.c +++ b/src/helper/helper-firewall.c @@ -38,7 +38,7 @@ #define BUF_SIZE_FOR_IP 64 #define RULE_CHAIN "chain" -#define RULE_DIRECTION "type" +#define RULE_DIRECTION "direction" #define RULE_IFNAME "ifname" #define RULE_PROTOCOL "protocol" #define RULE_TARGET "target" diff --git a/src/helper/helper-iptables.c b/src/helper/helper-iptables.c index cfd8aaf..b1ca634 100755 --- a/src/helper/helper-iptables.c +++ b/src/helper/helper-iptables.c @@ -33,12 +33,12 @@ #define STC_IPTABLES_DBUS_METHOD_IP6T_ADD_RULE "Ip6tAddRule" #define STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_RULE "Ip6tRemoveRule" -#define RULE_CHAIN "chain" -#define RULE_TYPE "type" -#define RULE_IFNAME "ifname" -#define RULE_CGROUP "cgroup" -#define RULE_NFACCT "nfacct" -#define RULE_TARGET "target" +#define RULE_CHAIN "chain" +#define RULE_DIRECTION "direction" +#define RULE_IFNAME "ifname" +#define RULE_CGROUP "cgroup" +#define RULE_NFACCT "nfacct" +#define RULE_TARGET "target" static void __add_rule_info_to_builder(GVariantBuilder *builder, iptables_rule_s *rule) @@ -49,7 +49,7 @@ static void __add_rule_info_to_builder(GVariantBuilder *builder, g_variant_builder_add(builder, "{sv}", RULE_CHAIN, g_variant_new_string(rule->chain)); - g_variant_builder_add(builder, "{sv}", RULE_TYPE, + g_variant_builder_add(builder, "{sv}", RULE_DIRECTION, g_variant_new_uint16(rule->direction)); if (rule->ifname) -- 2.7.4 From ed321635cef7090eda3fdd32bced083c2e6b8d05 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 9 May 2018 17:33:45 +0900 Subject: [PATCH 03/16] Added dbus method to commit ip6tables Change-Id: Ic446d4575fc9fc901f7a609377475f1be64657ea Signed-off-by: hyunuktak --- include/stc-manager-gdbus.h | 4 ++++ interfaces/stcmanager-iface-manager.xml | 6 ++++++ src/helper/helper-iptables.c | 12 +++++++---- src/stc-manager-gdbus.c | 38 ++++++++++++++++++++++++++++++++- 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/include/stc-manager-gdbus.h b/include/stc-manager-gdbus.h index b422b62..860b766 100755 --- a/include/stc-manager-gdbus.h +++ b/include/stc-manager-gdbus.h @@ -87,5 +87,9 @@ gboolean handle_manager_commit_iptables(StcManager *object, GDBusMethodInvocation *invocation, const gchar *option, void *user_data); +gboolean handle_manager_commit_ip6tables(StcManager *object, + GDBusMethodInvocation *invocation, + const gchar *option, + void *user_data); #endif /* __STC_MANAGER_GDBUS_H__ */ diff --git a/interfaces/stcmanager-iface-manager.xml b/interfaces/stcmanager-iface-manager.xml index 33697b4..6ac32d3 100644 --- a/interfaces/stcmanager-iface-manager.xml +++ b/interfaces/stcmanager-iface-manager.xml @@ -9,5 +9,11 @@ + + + + + + diff --git a/src/helper/helper-iptables.c b/src/helper/helper-iptables.c index b1ca634..6ca96af 100755 --- a/src/helper/helper-iptables.c +++ b/src/helper/helper-iptables.c @@ -96,7 +96,8 @@ static int __iptables_rule_add(GDBusConnection *connection, } g_variant_get(message, "(i)", &result); - STC_LOGD("Successfully Add Rule [%d:%s]", result, rule->nfacct_name); + if (STC_DEBUG_LOG) + STC_LOGD("Successfully Add Rule [%d:%s]", result, rule->nfacct_name); g_variant_unref(message); return STC_ERROR_NONE; @@ -128,7 +129,8 @@ static int __iptables_rule_remove(GDBusConnection *connection, } g_variant_get(message, "(i)", &result); - STC_LOGD("Successfully Remove Rule [%d:%s]", result, rule->nfacct_name); + if (STC_DEBUG_LOG) + STC_LOGD("Successfully Remove Rule [%d:%s]", result, rule->nfacct_name); g_variant_unref(message); return STC_ERROR_NONE; @@ -160,7 +162,8 @@ static int __ip6tables_rule_add(GDBusConnection *connection, } g_variant_get(message, "(i)", &result); - STC_LOGD("Successfully Add 6 Rule [%d:%s]", result, rule->nfacct_name); + if (STC_DEBUG_LOG) + STC_LOGD("Successfully Add 6 Rule [%d:%s]", result, rule->nfacct_name); g_variant_unref(message); return STC_ERROR_NONE; @@ -192,7 +195,8 @@ static int __ip6tables_rule_remove(GDBusConnection *connection, } g_variant_get(message, "(i)", &result); - STC_LOGD("Successfully Remove 6 Rule [%d:%s]", result, rule->nfacct_name); + if (STC_DEBUG_LOG) + STC_LOGD("Successfully Remove 6 Rule [%d:%s]", result, rule->nfacct_name); g_variant_unref(message); return STC_ERROR_NONE; diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index a31deb0..ddad1ef 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -255,6 +255,9 @@ static gboolean __stc_manager_gdbus_manager_init(stc_s *stc) g_signal_connect(manager, "handle-commit-iptables", G_CALLBACK(handle_manager_commit_iptables), stc); + g_signal_connect(manager, "handle-commit-ip6tables", + G_CALLBACK(handle_manager_commit_ip6tables), stc); + g_dbus_object_manager_server_export(stc->obj_mgr, G_DBUS_OBJECT_SKELETON(object)); g_object_unref(object); @@ -389,7 +392,7 @@ GVariant *stc_manager_gdbus_call_sync(GDBusConnection *connection, if (reply == NULL) { if (error != NULL) { STC_LOGE("g_dbus_connection_call_sync() failed" //LCOV_EXCL_LINE - "error [%d: %s]", error->code, error->message); + " error [%d: %s]", error->code, error->message); g_error_free(error); //LCOV_EXCL_LINE } else { STC_LOGE("g_dbus_connection_call_sync() failed"); //LCOV_EXCL_LINE @@ -548,3 +551,36 @@ gboolean handle_manager_commit_iptables(StcManager *object, __STC_LOG_FUNC_EXIT__; return TRUE; } + +gboolean handle_manager_commit_ip6tables(StcManager *object, + GDBusMethodInvocation *invocation, + const gchar *option, + void *user_data) +{ + __STC_LOG_FUNC_ENTER__; + GVariant *return_parameters = NULL; + int ret = STC_ERROR_NONE; + int err_num = 0; + char *err_str = NULL; + char cmd[STC_CMD_SIZE] = { 0, }; + + if (option == NULL) { + STC_MANAGER_DBUS_REPLY_ERROR(invocation, + STC_ERROR_INVALID_PARAMETER); + __STC_LOG_FUNC_EXIT__; + return TRUE; + } + + STC_LOGD("[%s]", option); + g_snprintf(cmd, STC_CMD_SIZE, "%s %s", STC_IP6TABLES, option); + + ret = stc_commit_iptables(cmd, &err_num, &err_str); + + return_parameters = g_variant_new("(iis)", ret, err_num, err_str); + + DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters); + STC_DBUS_REPLY(invocation, return_parameters); + + __STC_LOG_FUNC_EXIT__; + return TRUE; +} -- 2.7.4 From 0fe1e778742b3d158666e144254fcfb70b65a29e Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 10 May 2018 16:00:48 +0900 Subject: [PATCH 04/16] Updated enum value for app state Change-Id: I9cd032ac4482968f43678fdb8c4773ef2253cdbb Signed-off-by: hyunuktak --- include/stc-manager.h | 8 ++++---- packaging/stc-manager.spec | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/stc-manager.h b/include/stc-manager.h index f3cee74..f43c92d 100755 --- a/include/stc-manager.h +++ b/include/stc-manager.h @@ -121,10 +121,10 @@ typedef enum { * @brief State of the statisticsed process */ typedef enum { - STC_APP_STATE_UNKNOWN = 0, - STC_APP_STATE_FOREGROUND = 1 << 1, /** < foreground state */ - STC_APP_STATE_BACKGROUND = 1 << 2, /** < background state */ - STC_APP_STATE_LAST_ELEM = 1 << 3 + STC_APP_STATE_UNKNOWN, + STC_APP_STATE_FOREGROUND, /** < foreground state */ + STC_APP_STATE_BACKGROUND, /** < background state */ + STC_APP_STATE_LAST_ELEM } stc_app_state_e; /** diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index b85d523..95ff39a 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.60 +Version: 0.0.61 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 -- 2.7.4 From faa15ef08b6a44305108696f98e44c4db1674f5c Mon Sep 17 00:00:00 2001 From: Milind Murhekar Date: Fri, 4 May 2018 15:48:28 +0530 Subject: [PATCH 05/16] Add support for Tethering data monitoring and data restriction rules Description: This patch applies the tethering interface monitoring, when Hotspot is enabled/disabled, Also it supports data restriction rules for tethering interface including default network connection. Change-Id: I2b79de433abde7b5ae46d1fb9d7b537975398dab Signed-off-by: Milind Murhekar --- include/stc-manager.h | 3 + packaging/stc-manager.spec | 2 +- src/helper/helper-net-cls.c | 3 + src/monitor/include/stc-default-connection.h | 19 ++++ src/monitor/stc-default-connection.c | 88 +++++++++++++++++++ src/monitor/stc-monitor.c | 126 ++++++++++++++++++++++----- 6 files changed, 220 insertions(+), 21 deletions(-) diff --git a/include/stc-manager.h b/include/stc-manager.h index f43c92d..14d5cc6 100755 --- a/include/stc-manager.h +++ b/include/stc-manager.h @@ -30,6 +30,7 @@ #define STC_TOTAL_BLUETOOTH "TOTAL_BLUETOOTH" #define STC_TOTAL_IPV4 "TOTAL_IPV4" #define STC_TOTAL_IPV6 "TOTAL_IPV6" +#define STC_TOTAL_TETHERING "TOTAL_TETHERING" #define STC_IPTABLES "/usr/sbin/iptables" #define STC_IP6TABLES "/usr/sbin/ip6tables" @@ -158,6 +159,8 @@ typedef enum { STC_IFACE_BLUETOOTH, /**< bluetooth interface */ STC_IFACE_IPV4, /**< ipv4 interface */ STC_IFACE_IPV6, /**< ipv6 interface */ + STC_IFACE_USB, /**< usb interface */ + STC_IFACE_P2P, /**< p2p interface */ STC_IFACE_ALL, /**< enumerate all network interface types */ STC_IFACE_LAST_ELEM } stc_iface_type_e; diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 95ff39a..bb6a9c1 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.61 +Version: 0.0.63 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-net-cls.c b/src/helper/helper-net-cls.c index c1460dc..e13c729 100755 --- a/src/helper/helper-net-cls.c +++ b/src/helper/helper-net-cls.c @@ -132,6 +132,9 @@ uint32_t get_classid_by_app_id(const char *app_id, int create) if (!strcmp(app_id, STC_TOTAL_IPV6)) return STC_TOTAL_IPV6_CLASSID; + if (!strcmp(app_id, STC_TOTAL_TETHERING)) + return STC_TETHERING_APP_CLASSID; + if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX)) path_to_net_cgroup_dir = BACKGROUND_CGROUP_NETWORK; else diff --git a/src/monitor/include/stc-default-connection.h b/src/monitor/include/stc-default-connection.h index 230671b..222e77b 100755 --- a/src/monitor/include/stc-default-connection.h +++ b/src/monitor/include/stc-default-connection.h @@ -25,6 +25,19 @@ #define IMSI_LENGTH 16 #define SHA256_DIGEST_LENGTH 32 +#define TETHERING_USB_IF "usb0" +#define TETHERING_WIFI_IF "wlan0" +#define TETHERING_BT_IF "bnep0" +#define TETHERING_P2P_IF "p2p0" + +/** + * @brief Tethering interface info + */ +typedef struct { + gchar *ifname; + stc_iface_type_e type; +} tether_iface_s; + /** * @brief default connection information will be fetched from net-config */ @@ -44,6 +57,12 @@ typedef struct { /* hardware network protocol type */ stc_hw_net_protocol_type_e hw_net_protocol_type; + + /* tethering status */ + gboolean tether_state; + + /* tethering interface */ + tether_iface_s tether_iface; } default_connection_s; stc_error_e stc_default_connection_monitor_init(stc_s *stc); diff --git a/src/monitor/stc-default-connection.c b/src/monitor/stc-default-connection.c index 9a359d4..244a8b9 100755 --- a/src/monitor/stc-default-connection.c +++ b/src/monitor/stc-default-connection.c @@ -193,12 +193,24 @@ static void __print_default_connection_info(void) STC_LOGI("=================================================="); } +static void __print_tether_connection_info(void) +{ + STC_LOGI("============= tethering connection info ============"); + STC_LOGI("mode [%u]", g_default_connection.tether_state ? TRUE : FALSE); + STC_LOGI("type [%d]", g_default_connection.tether_iface.type); + STC_LOGI("ifname [%s]", g_default_connection.tether_iface.ifname); + STC_LOGI("===================================================="); +} + static void __reset_default_connection_data(void) { FREE(g_default_connection.path); FREE(g_default_connection.ifname); + FREE(g_default_connection.tether_iface.ifname); g_default_connection.type = STC_IFACE_UNKNOWN; g_default_connection.roaming = FALSE; + g_default_connection.tether_iface.type = STC_IFACE_UNKNOWN; + g_default_connection.tether_state = FALSE; } static gboolean __is_cellular_internet_profile(const char *profile) @@ -482,8 +494,80 @@ done: return; } +static void __vconf_key_callback(keynode_t *node, void *user_data) +{ + int vconf_key; + + if (node == NULL) { + STC_LOGE("Invalid parameter"); + return; + } + + if (vconf_keynode_get_type(node) != VCONF_TYPE_INT) { + STC_LOGE("Invalid vconf key type"); + return; + } + + vconf_key = vconf_keynode_get_int(node); + + /* Check the tethering type */ + switch (vconf_key) { + case VCONFKEY_MOBILE_HOTSPOT_MODE_USB: + STC_LOGI("Hotspot mode USB type !"); + g_default_connection.tether_state = TRUE; + g_default_connection.tether_iface.ifname = g_strdup(TETHERING_USB_IF); + g_default_connection.tether_iface.type = STC_IFACE_USB; + break; + case VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI: + STC_LOGI("Hotspot mode Wi-Fi type !"); + g_default_connection.tether_state = TRUE; + g_default_connection.tether_iface.ifname = g_strdup(TETHERING_WIFI_IF); + g_default_connection.tether_iface.type = STC_IFACE_WIFI; + break; + case VCONFKEY_MOBILE_HOTSPOT_MODE_BT: + STC_LOGI("Hotspot mode Bluetooth type !"); + g_default_connection.tether_state = TRUE; + g_default_connection.tether_iface.ifname = g_strdup(TETHERING_BT_IF); + g_default_connection.tether_iface.type = STC_IFACE_BLUETOOTH; + break; + case VCONFKEY_MOBILE_HOTSPOT_MODE_P2P: + STC_LOGI("Hotspot mode P2P type !"); + g_default_connection.tether_state = TRUE; + g_default_connection.tether_iface.ifname = g_strdup(TETHERING_P2P_IF); + g_default_connection.tether_iface.type = STC_IFACE_P2P; + break; + case VCONFKEY_MOBILE_HOTSPOT_MODE_NONE: + STC_LOGI("Hotspot mode none"); + g_default_connection.tether_state = FALSE; + break; + default: + STC_LOGE("Unknown Hotspot mode type !"); + break; + } + + /* add monitoring for tethering if active found */ + if (g_default_connection.tether_state == TRUE && g_default_connection.tether_iface.ifname) { + __print_tether_connection_info(); + stc_monitor_update_rstn_by_default_connection(&g_default_connection); + stc_firewall_update(); + STC_LOGI("Data monitoring started for tethering iface !"); + return; + } + + /* remove monitoring for tethering if in-active found */ + if (g_default_connection.tether_state == FALSE && g_default_connection.tether_iface.ifname) { + stc_monitor_update_rstn_by_default_connection(&g_default_connection); + g_free(g_default_connection.tether_iface.ifname); + g_default_connection.tether_iface.ifname = NULL; + g_default_connection.tether_iface.type = STC_IFACE_UNKNOWN; + STC_LOGI("Data monitoring stopped for tethering iface !"); + return; + } +} + stc_error_e stc_default_connection_monitor_init(stc_s *stc) { + int ret; ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); __get_default_profile(stc->connection); @@ -497,6 +581,10 @@ stc_error_e stc_default_connection_monitor_init(stc_s *stc) _service_signal_cb, NULL, NULL); + ret = vconf_notify_key_changed(VCONFKEY_MOBILE_HOTSPOT_MODE, __vconf_key_callback, NULL); + if (ret < 0) + STC_LOGE("vconf_notify_key_changed failed: %d", ret); + STC_LOGI("Successfully subscribed connman [%s] signal", CONNMAN_SIGNAL_PROPERTY_CHANGED); return STC_ERROR_NONE; } diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index b73a889..62a0c20 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -403,8 +403,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 +454,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); @@ -480,6 +496,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 +517,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 +542,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 +563,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 +603,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 +657,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 +887,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 +952,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; @@ -1028,7 +1099,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); } @@ -1617,6 +1696,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); @@ -1855,8 +1935,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 +1953,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; } } -- 2.7.4 From 2a3073365d410df5aa0d6d5a16e0b7a6f4ae5782 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 17 May 2018 18:19:39 +0530 Subject: [PATCH 06/16] [Wformat] Fix Wformat build error Change-Id: I418e75c7f403d1ee1d54f8236b39f20977513aab Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- plugin/exception/stc-plugin-exception.c | 3 ++- plugin/procfs/stc-plugin-procfs.c | 2 +- src/monitor/stc-monitor.c | 6 +++--- src/stc-restriction.c | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index bb6a9c1..de1f0c8 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.63 +Version: 0.0.64 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/plugin/exception/stc-plugin-exception.c b/plugin/exception/stc-plugin-exception.c index 783bc4f..db22b2f 100755 --- a/plugin/exception/stc-plugin-exception.c +++ b/plugin/exception/stc-plugin-exception.c @@ -205,7 +205,8 @@ static stc_error_e table_exceptions_foreach(const stc_exceptions_info_cb excepti char buf[EXCEPTION_BUF_MAX] = {0, }; fp = fopen(EXCEPTION_STORAGE, "r"); - ret_value_msg_if(!fp, STC_ERROR_FAIL, "Failed to open %s file"); + ret_value_msg_if(!fp, STC_ERROR_FAIL, "Failed to open %s file", + EXCEPTION_STORAGE); while (fgets(buf, sizeof(buf), fp) != NULL) { char *process_name, *exe_type; diff --git a/plugin/procfs/stc-plugin-procfs.c b/plugin/procfs/stc-plugin-procfs.c index df69301..df2c195 100755 --- a/plugin/procfs/stc-plugin-procfs.c +++ b/plugin/procfs/stc-plugin-procfs.c @@ -406,7 +406,7 @@ static gboolean __process_nl_connector_message(GIOChannel *source, /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */ STC_LOGE("Netlink Connector socket received G_IO event, closing" - " socket. G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]", + " socket. G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]", (condition & G_IO_ERR), (condition & G_IO_HUP), (condition & G_IO_NVAL)); __reopen_nl_connector_sock(); diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 62a0c20..adb79a2 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -477,7 +477,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], " @@ -1158,7 +1158,7 @@ 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", + 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); @@ -1248,7 +1248,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 diff --git a/src/stc-restriction.c b/src/stc-restriction.c index 242cdcb..d4353ea 100755 --- a/src/stc-restriction.c +++ b/src/stc-restriction.c @@ -178,7 +178,7 @@ static void __stc_extract_restriction_rule(const char *key, GVariant *value, guint str_length; const gchar *str = g_variant_get_string(value, &str_length); rule->app_id = g_strdup(str); - STC_LOGD("app_id: [%s]", (unsigned int) rule->app_id); + STC_LOGD("app_id: [%s]", rule->app_id); } else if (!g_strcmp0(key, "ifname")) { guint str_length; -- 2.7.4 From ba9186f949073543fcecf5f1d517a21b5141e7f0 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 3 May 2018 20:20:30 +0530 Subject: [PATCH 07/16] Corrected file permissions Change-Id: Ic0392ac48d765bf5f7c9291bac4f573c84d78a1b Signed-off-by: Nishant Chaprana --- include/stc-error.h | 0 include/stc-firewall.h | 0 include/stc-manager-gdbus.h | 0 include/stc-manager-plugin-appstatus.h | 0 include/stc-manager-plugin-exception.h | 0 include/stc-manager-plugin-procfs.h | 0 include/stc-manager-util.h | 0 include/stc-manager.h | 0 include/stc-restriction.h | 0 include/stc-statistics.h | 0 include/transmission.h | 0 plugin/CMakeLists.txt | 0 plugin/appstatus/CMakeLists.txt | 0 plugin/appstatus/data/stc_noti_datausage.png | Bin plugin/appstatus/include/stc-plugin-appstatus.h | 0 plugin/appstatus/stc-plugin-appstatus.c | 0 plugin/exception/CMakeLists.txt | 0 plugin/exception/include/stc-plugin-exception.h | 0 plugin/exception/stc-plugin-exception.c | 0 plugin/procfs/CMakeLists.txt | 0 plugin/procfs/include/stc-plugin-procfs.h | 0 plugin/procfs/stc-plugin-procfs.c | 0 resources/systemd/stc-manager.service | 0 src/configure/include/configure_stub.h | 0 src/configure/include/counter.h | 0 src/database/db-common.c | 0 src/database/db-guard.c | 0 src/database/include/db-internal.h | 0 src/database/include/stc-db.h | 0 src/database/include/table-counters.h | 0 src/database/include/table-firewall.h | 0 src/database/include/table-restrictions.h | 0 src/database/include/table-statistics.h | 0 src/database/tables/table-counters.c | 0 src/database/tables/table-firewall.c | 0 src/database/tables/table-restrictions.c | 0 src/database/tables/table-statistics.c | 0 src/helper/helper-cgroup.c | 0 src/helper/helper-cgroup.h | 0 src/helper/helper-file.c | 0 src/helper/helper-file.h | 0 src/helper/helper-firewall.c | 0 src/helper/helper-firewall.h | 0 src/helper/helper-iptables.c | 0 src/helper/helper-iptables.h | 0 src/helper/helper-net-cls.c | 0 src/helper/helper-net-cls.h | 0 src/helper/helper-nfacct-rule.c | 0 src/helper/helper-nfacct-rule.h | 0 src/helper/helper-nl.c | 0 src/helper/helper-nl.h | 0 src/helper/helper-procfs.c | 0 src/helper/helper-procfs.h | 0 src/monitor/include/stc-default-connection.h | 0 src/monitor/include/stc-emulator.h | 0 src/monitor/include/stc-monitor.h | 0 src/monitor/stc-default-connection.c | 0 src/monitor/stc-emulator.c | 0 src/monitor/stc-monitor.c | 0 src/stc-firewall.c | 0 src/stc-manager-gdbus.c | 0 src/stc-manager-plugin-appstatus.c | 0 src/stc-manager-plugin-exception.c | 0 src/stc-manager-plugin-procfs.c | 0 src/stc-manager-util.c | 0 src/stc-manager.c | 0 src/stc-restriction.c | 0 src/stc-statistics.c | 0 src/utils/net-cls-release.c | 0 unittest/CMakeLists.txt | 0 unittest/gdbus.h | 0 unittest/manager.h | 0 unittest/restriction.h | 0 unittest/statistics.h | 0 unittest/stcmgr.h | 0 unittest/unittest.h | 0 76 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 include/stc-error.h mode change 100755 => 100644 include/stc-firewall.h mode change 100755 => 100644 include/stc-manager-gdbus.h mode change 100755 => 100644 include/stc-manager-plugin-appstatus.h mode change 100755 => 100644 include/stc-manager-plugin-exception.h mode change 100755 => 100644 include/stc-manager-plugin-procfs.h mode change 100755 => 100644 include/stc-manager-util.h mode change 100755 => 100644 include/stc-manager.h mode change 100755 => 100644 include/stc-restriction.h mode change 100755 => 100644 include/stc-statistics.h mode change 100755 => 100644 include/transmission.h mode change 100755 => 100644 plugin/CMakeLists.txt mode change 100755 => 100644 plugin/appstatus/CMakeLists.txt mode change 100755 => 100644 plugin/appstatus/data/stc_noti_datausage.png mode change 100755 => 100644 plugin/appstatus/include/stc-plugin-appstatus.h mode change 100755 => 100644 plugin/appstatus/stc-plugin-appstatus.c mode change 100755 => 100644 plugin/exception/CMakeLists.txt mode change 100755 => 100644 plugin/exception/include/stc-plugin-exception.h mode change 100755 => 100644 plugin/exception/stc-plugin-exception.c mode change 100755 => 100644 plugin/procfs/CMakeLists.txt mode change 100755 => 100644 plugin/procfs/include/stc-plugin-procfs.h mode change 100755 => 100644 plugin/procfs/stc-plugin-procfs.c mode change 100755 => 100644 resources/systemd/stc-manager.service mode change 100755 => 100644 src/configure/include/configure_stub.h mode change 100755 => 100644 src/configure/include/counter.h mode change 100755 => 100644 src/database/db-common.c mode change 100755 => 100644 src/database/db-guard.c mode change 100755 => 100644 src/database/include/db-internal.h mode change 100755 => 100644 src/database/include/stc-db.h mode change 100755 => 100644 src/database/include/table-counters.h mode change 100755 => 100644 src/database/include/table-firewall.h mode change 100755 => 100644 src/database/include/table-restrictions.h mode change 100755 => 100644 src/database/include/table-statistics.h mode change 100755 => 100644 src/database/tables/table-counters.c mode change 100755 => 100644 src/database/tables/table-firewall.c mode change 100755 => 100644 src/database/tables/table-restrictions.c mode change 100755 => 100644 src/database/tables/table-statistics.c mode change 100755 => 100644 src/helper/helper-cgroup.c mode change 100755 => 100644 src/helper/helper-cgroup.h mode change 100755 => 100644 src/helper/helper-file.c mode change 100755 => 100644 src/helper/helper-file.h mode change 100755 => 100644 src/helper/helper-firewall.c mode change 100755 => 100644 src/helper/helper-firewall.h mode change 100755 => 100644 src/helper/helper-iptables.c mode change 100755 => 100644 src/helper/helper-iptables.h mode change 100755 => 100644 src/helper/helper-net-cls.c mode change 100755 => 100644 src/helper/helper-net-cls.h mode change 100755 => 100644 src/helper/helper-nfacct-rule.c mode change 100755 => 100644 src/helper/helper-nfacct-rule.h mode change 100755 => 100644 src/helper/helper-nl.c mode change 100755 => 100644 src/helper/helper-nl.h mode change 100755 => 100644 src/helper/helper-procfs.c mode change 100755 => 100644 src/helper/helper-procfs.h mode change 100755 => 100644 src/monitor/include/stc-default-connection.h mode change 100755 => 100644 src/monitor/include/stc-emulator.h mode change 100755 => 100644 src/monitor/include/stc-monitor.h mode change 100755 => 100644 src/monitor/stc-default-connection.c mode change 100755 => 100644 src/monitor/stc-emulator.c mode change 100755 => 100644 src/monitor/stc-monitor.c mode change 100755 => 100644 src/stc-firewall.c mode change 100755 => 100644 src/stc-manager-gdbus.c mode change 100755 => 100644 src/stc-manager-plugin-appstatus.c mode change 100755 => 100644 src/stc-manager-plugin-exception.c mode change 100755 => 100644 src/stc-manager-plugin-procfs.c mode change 100755 => 100644 src/stc-manager-util.c mode change 100755 => 100644 src/stc-manager.c mode change 100755 => 100644 src/stc-restriction.c mode change 100755 => 100644 src/stc-statistics.c mode change 100755 => 100644 src/utils/net-cls-release.c mode change 100755 => 100644 unittest/CMakeLists.txt mode change 100755 => 100644 unittest/gdbus.h mode change 100755 => 100644 unittest/manager.h mode change 100755 => 100644 unittest/restriction.h mode change 100755 => 100644 unittest/statistics.h mode change 100755 => 100644 unittest/stcmgr.h mode change 100755 => 100644 unittest/unittest.h diff --git a/include/stc-error.h b/include/stc-error.h old mode 100755 new mode 100644 diff --git a/include/stc-firewall.h b/include/stc-firewall.h old mode 100755 new mode 100644 diff --git a/include/stc-manager-gdbus.h b/include/stc-manager-gdbus.h old mode 100755 new mode 100644 diff --git a/include/stc-manager-plugin-appstatus.h b/include/stc-manager-plugin-appstatus.h old mode 100755 new mode 100644 diff --git a/include/stc-manager-plugin-exception.h b/include/stc-manager-plugin-exception.h old mode 100755 new mode 100644 diff --git a/include/stc-manager-plugin-procfs.h b/include/stc-manager-plugin-procfs.h old mode 100755 new mode 100644 diff --git a/include/stc-manager-util.h b/include/stc-manager-util.h old mode 100755 new mode 100644 diff --git a/include/stc-manager.h b/include/stc-manager.h old mode 100755 new mode 100644 diff --git a/include/stc-restriction.h b/include/stc-restriction.h old mode 100755 new mode 100644 diff --git a/include/stc-statistics.h b/include/stc-statistics.h old mode 100755 new mode 100644 diff --git a/include/transmission.h b/include/transmission.h old mode 100755 new mode 100644 diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/plugin/appstatus/CMakeLists.txt b/plugin/appstatus/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/plugin/appstatus/data/stc_noti_datausage.png b/plugin/appstatus/data/stc_noti_datausage.png old mode 100755 new mode 100644 diff --git a/plugin/appstatus/include/stc-plugin-appstatus.h b/plugin/appstatus/include/stc-plugin-appstatus.h old mode 100755 new mode 100644 diff --git a/plugin/appstatus/stc-plugin-appstatus.c b/plugin/appstatus/stc-plugin-appstatus.c old mode 100755 new mode 100644 diff --git a/plugin/exception/CMakeLists.txt b/plugin/exception/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/plugin/exception/include/stc-plugin-exception.h b/plugin/exception/include/stc-plugin-exception.h old mode 100755 new mode 100644 diff --git a/plugin/exception/stc-plugin-exception.c b/plugin/exception/stc-plugin-exception.c old mode 100755 new mode 100644 diff --git a/plugin/procfs/CMakeLists.txt b/plugin/procfs/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/plugin/procfs/include/stc-plugin-procfs.h b/plugin/procfs/include/stc-plugin-procfs.h old mode 100755 new mode 100644 diff --git a/plugin/procfs/stc-plugin-procfs.c b/plugin/procfs/stc-plugin-procfs.c old mode 100755 new mode 100644 diff --git a/resources/systemd/stc-manager.service b/resources/systemd/stc-manager.service old mode 100755 new mode 100644 diff --git a/src/configure/include/configure_stub.h b/src/configure/include/configure_stub.h old mode 100755 new mode 100644 diff --git a/src/configure/include/counter.h b/src/configure/include/counter.h old mode 100755 new mode 100644 diff --git a/src/database/db-common.c b/src/database/db-common.c old mode 100755 new mode 100644 diff --git a/src/database/db-guard.c b/src/database/db-guard.c old mode 100755 new mode 100644 diff --git a/src/database/include/db-internal.h b/src/database/include/db-internal.h old mode 100755 new mode 100644 diff --git a/src/database/include/stc-db.h b/src/database/include/stc-db.h old mode 100755 new mode 100644 diff --git a/src/database/include/table-counters.h b/src/database/include/table-counters.h old mode 100755 new mode 100644 diff --git a/src/database/include/table-firewall.h b/src/database/include/table-firewall.h old mode 100755 new mode 100644 diff --git a/src/database/include/table-restrictions.h b/src/database/include/table-restrictions.h old mode 100755 new mode 100644 diff --git a/src/database/include/table-statistics.h b/src/database/include/table-statistics.h old mode 100755 new mode 100644 diff --git a/src/database/tables/table-counters.c b/src/database/tables/table-counters.c old mode 100755 new mode 100644 diff --git a/src/database/tables/table-firewall.c b/src/database/tables/table-firewall.c old mode 100755 new mode 100644 diff --git a/src/database/tables/table-restrictions.c b/src/database/tables/table-restrictions.c old mode 100755 new mode 100644 diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c old mode 100755 new mode 100644 diff --git a/src/helper/helper-cgroup.c b/src/helper/helper-cgroup.c old mode 100755 new mode 100644 diff --git a/src/helper/helper-cgroup.h b/src/helper/helper-cgroup.h old mode 100755 new mode 100644 diff --git a/src/helper/helper-file.c b/src/helper/helper-file.c old mode 100755 new mode 100644 diff --git a/src/helper/helper-file.h b/src/helper/helper-file.h old mode 100755 new mode 100644 diff --git a/src/helper/helper-firewall.c b/src/helper/helper-firewall.c old mode 100755 new mode 100644 diff --git a/src/helper/helper-firewall.h b/src/helper/helper-firewall.h old mode 100755 new mode 100644 diff --git a/src/helper/helper-iptables.c b/src/helper/helper-iptables.c old mode 100755 new mode 100644 diff --git a/src/helper/helper-iptables.h b/src/helper/helper-iptables.h old mode 100755 new mode 100644 diff --git a/src/helper/helper-net-cls.c b/src/helper/helper-net-cls.c old mode 100755 new mode 100644 diff --git a/src/helper/helper-net-cls.h b/src/helper/helper-net-cls.h old mode 100755 new mode 100644 diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c old mode 100755 new mode 100644 diff --git a/src/helper/helper-nfacct-rule.h b/src/helper/helper-nfacct-rule.h old mode 100755 new mode 100644 diff --git a/src/helper/helper-nl.c b/src/helper/helper-nl.c old mode 100755 new mode 100644 diff --git a/src/helper/helper-nl.h b/src/helper/helper-nl.h old mode 100755 new mode 100644 diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c old mode 100755 new mode 100644 diff --git a/src/helper/helper-procfs.h b/src/helper/helper-procfs.h old mode 100755 new mode 100644 diff --git a/src/monitor/include/stc-default-connection.h b/src/monitor/include/stc-default-connection.h old mode 100755 new mode 100644 diff --git a/src/monitor/include/stc-emulator.h b/src/monitor/include/stc-emulator.h old mode 100755 new mode 100644 diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h old mode 100755 new mode 100644 diff --git a/src/monitor/stc-default-connection.c b/src/monitor/stc-default-connection.c old mode 100755 new mode 100644 diff --git a/src/monitor/stc-emulator.c b/src/monitor/stc-emulator.c old mode 100755 new mode 100644 diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c old mode 100755 new mode 100644 diff --git a/src/stc-firewall.c b/src/stc-firewall.c old mode 100755 new mode 100644 diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c old mode 100755 new mode 100644 diff --git a/src/stc-manager-plugin-appstatus.c b/src/stc-manager-plugin-appstatus.c old mode 100755 new mode 100644 diff --git a/src/stc-manager-plugin-exception.c b/src/stc-manager-plugin-exception.c old mode 100755 new mode 100644 diff --git a/src/stc-manager-plugin-procfs.c b/src/stc-manager-plugin-procfs.c old mode 100755 new mode 100644 diff --git a/src/stc-manager-util.c b/src/stc-manager-util.c old mode 100755 new mode 100644 diff --git a/src/stc-manager.c b/src/stc-manager.c old mode 100755 new mode 100644 diff --git a/src/stc-restriction.c b/src/stc-restriction.c old mode 100755 new mode 100644 diff --git a/src/stc-statistics.c b/src/stc-statistics.c old mode 100755 new mode 100644 diff --git a/src/utils/net-cls-release.c b/src/utils/net-cls-release.c old mode 100755 new mode 100644 diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/unittest/gdbus.h b/unittest/gdbus.h old mode 100755 new mode 100644 diff --git a/unittest/manager.h b/unittest/manager.h old mode 100755 new mode 100644 diff --git a/unittest/restriction.h b/unittest/restriction.h old mode 100755 new mode 100644 diff --git a/unittest/statistics.h b/unittest/statistics.h old mode 100755 new mode 100644 diff --git a/unittest/stcmgr.h b/unittest/stcmgr.h old mode 100755 new mode 100644 diff --git a/unittest/unittest.h b/unittest/unittest.h old mode 100755 new mode 100644 -- 2.7.4 From ccdf22a5861f7ce5b4b72810c03e632e1e2e5e92 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Fri, 18 May 2018 20:18:09 +0530 Subject: [PATCH 08/16] Fix: Malloc only when making process entry in tree Description: For some processes both FORK and EXEC events are emitted, however we were allocating mamory in both methods, but not unallocating the memory in failure case when entry was already present. So we should malloc only when we are creating an entry in process tree. Change-Id: Iedc3c10eabb41ad4da854ab8d0cf45759bbedcc7 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- plugin/procfs/stc-plugin-procfs.c | 110 +++++++++++++++++++++----------------- 2 files changed, 62 insertions(+), 50 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index de1f0c8..f01d2a5 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.64 +Version: 0.0.65 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/plugin/procfs/stc-plugin-procfs.c b/plugin/procfs/stc-plugin-procfs.c index df2c195..36b8f78 100644 --- a/plugin/procfs/stc-plugin-procfs.c +++ b/plugin/procfs/stc-plugin-procfs.c @@ -145,8 +145,7 @@ static proc_value_s * __proc_tree_find_parent(proc_value_s *value) return parent; } -static void __proc_tree_add(proc_key_s *key, - proc_value_s *value) +static void __proc_tree_add(proc_key_s *key, proc_value_s *value) { proc_value_s *lookup; proc_value_s *parent; @@ -156,32 +155,54 @@ static void __proc_tree_add(proc_key_s *key, return; } + if (key == NULL || value == NULL) { + if (STC_DEBUG_LOG) + STC_LOGE("invalid parameters"); + return; + } + lookup = g_tree_lookup(proc_tree, key); if (lookup) { if (STC_DEBUG_LOG) STC_LOGD("LOOKUP: tgid[\033[1;33m%s\033[0;m] pid[%s] ppid[\033[1;35m%s\033[0;m] " - "cmdline[\033[0;34m%s\033[0;m] name[%s]", lookup->status[PROC_STATUS_TGID], - lookup->status[PROC_STATUS_PID], lookup->status[PROC_STATUS_PPID], - lookup->cmdline, lookup->status[PROC_STATUS_NAME]); + "cmdline[\033[0;34m%s\033[0;m] name[%s]", lookup->status[PROC_STATUS_TGID], + lookup->status[PROC_STATUS_PID], lookup->status[PROC_STATUS_PPID], + lookup->cmdline, lookup->status[PROC_STATUS_NAME]); return; } + proc_key_s *proc_key = MALLOC0(proc_key_s, 1); + if (proc_key == NULL) { + STC_LOGE("memory allocation failed"); + return; + } + + proc_value_s *proc_value = MALLOC0(proc_value_s, 1); + if (proc_value == NULL) { + STC_LOGE("memory allocation failed"); + FREE(proc_key); + return; + } + + memcpy(proc_key, key, sizeof(proc_key_s)); + memcpy(proc_value, value, sizeof(proc_value_s)); + if (STC_DEBUG_LOG) STC_LOGD("cmdline [%s] pid[%s] ppid[%s]", value->cmdline, - value->status[PROC_STATUS_PID], value->status[PROC_STATUS_PPID]); + value->status[PROC_STATUS_PID], value->status[PROC_STATUS_PPID]); - g_tree_insert(proc_tree, key, value); + g_tree_insert(proc_tree, proc_key, proc_value); if (STC_DEBUG_LOG) __proc_tree_printall(); - parent = __proc_tree_find_parent(value); + parent = __proc_tree_find_parent(proc_value); if (parent != NULL) - stc_plugin_procfs_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid, + stc_plugin_procfs_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, proc_key->pid, parent->cmdline, parent->cmdline, STC_APP_TYPE_SERVICE); else - stc_plugin_procfs_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid, - value->cmdline, value->cmdline, STC_APP_TYPE_SERVICE); + stc_plugin_procfs_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, proc_key->pid, + proc_value->cmdline, proc_value->cmdline, STC_APP_TYPE_SERVICE); } static void __proc_tree_remove(const proc_key_s *key) @@ -278,6 +299,10 @@ static void __process_event_fork(int tgid, int pid) char cmdline[PROC_NAME_MAX] = {0, }; char status[PROC_STATUS_CNT][PROC_BUF_MAX]; + /* TODO: Add newly created thread to the process tasks */ + if (tgid != pid) + return; + memset(status, 0x0, sizeof(status)); if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && @@ -290,26 +315,16 @@ static void __process_event_fork(int tgid, int pid) } unsigned int i; - proc_key_s *key; - proc_value_s *value; - - key = MALLOC0(proc_key_s, 1); - if (key == NULL) { - STC_LOGE("memory allocation failed"); - return; - } + proc_key_s key; + proc_value_s value; - value = MALLOC0(proc_value_s, 1); - if (value == NULL) { - STC_LOGE("memory allocation failed"); - FREE(key); - return; - } + memset(&key, 0x0, sizeof(proc_key_s)); + memset(&value, 0x0, sizeof(proc_value_s)); - key->pid = tgid; + key.pid = tgid; for (i = 0; i < PROC_STATUS_CNT; ++i) - g_strlcpy(value->status[i], status[i], sizeof(value->status[i])); - g_strlcpy(value->cmdline, cmdline, sizeof(value->cmdline)); + g_strlcpy(value.status[i], status[i], sizeof(value.status[i])); + g_strlcpy(value.cmdline, cmdline, sizeof(value.cmdline)); if (STC_DEBUG_LOG) { STC_LOGD("\033[1;34mFORK\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] " @@ -319,7 +334,7 @@ static void __process_event_fork(int tgid, int pid) status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]); } - __proc_tree_add(key, value); + __proc_tree_add(&key, &value); } } @@ -328,6 +343,10 @@ static void __process_event_exec(int tgid, int pid) char cmdline[PROC_NAME_MAX] = {0, }; char status[PROC_STATUS_CNT][PROC_BUF_MAX]; + /* TODO: Add newly created thread to the process tasks */ + if (tgid != pid) + return; + memset(status, 0x0, sizeof(status)); if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && @@ -340,26 +359,17 @@ static void __process_event_exec(int tgid, int pid) } unsigned int i; - proc_key_s *key; - proc_value_s *value; - - key = MALLOC0(proc_key_s, 1); - if (key == NULL) { - STC_LOGE("memory allocation failed"); - return; - } + proc_key_s key; + proc_value_s value; - value = MALLOC0(proc_value_s, 1); - if (value == NULL) { - STC_LOGE("memory allocation failed"); - FREE(key); - return; - } + memset(&key, 0x0, sizeof(proc_key_s)); + memset(&value, 0x0, sizeof(proc_value_s)); - key->pid = tgid; + key.pid = tgid; for (i = 0; i < PROC_STATUS_CNT; ++i) - g_strlcpy(value->status[i], status[i], sizeof(value->status[i])); - g_strlcpy(value->cmdline, cmdline, sizeof(value->cmdline)); + g_strlcpy(value.status[i], status[i], + sizeof(value.status[i])); + g_strlcpy(value.cmdline, cmdline, sizeof(value.cmdline)); if (STC_DEBUG_LOG) { STC_LOGD("\033[1;32mEXEC\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] " @@ -369,7 +379,7 @@ static void __process_event_exec(int tgid, int pid) status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]); } - __proc_tree_add(key, value); + __proc_tree_add(&key, &value); } } @@ -548,8 +558,10 @@ int stc_plugin_procfs_deinitialize(void) return STC_ERROR_NONE; } -stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd, - pid_t pid, const gchar *app_id, const gchar *pkg_id, stc_app_type_e app_type) +stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd, pid_t pid, + const gchar *app_id, + const gchar *pkg_id, + stc_app_type_e app_type) { stc_error_e ret = STC_ERROR_NONE; -- 2.7.4 From 3fe2d2fa0830c3abc50a913934100592d9c745fb Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 23 May 2018 13:20:27 +0900 Subject: [PATCH 09/16] Removed secure dlog for sql query Change-Id: I1505bd691ed8a28e6e552eda033e864b4012b592 Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- src/database/tables/table-restrictions.c | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) mode change 100644 => 100755 src/database/tables/table-restrictions.c diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index f01d2a5..7d6decf 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.65 +Version: 0.0.66 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/database/tables/table-restrictions.c b/src/database/tables/table-restrictions.c old mode 100644 new mode 100755 index 5c7e4ee..12fa67d --- a/src/database/tables/table-restrictions.c +++ b/src/database/tables/table-restrictions.c @@ -410,8 +410,7 @@ stc_error_e table_restrictions_get_restriction_type_subscriber_id(const char *ap break; case SQLITE_ERROR: default: - STC_LOGE("Can't perform sql query: %s \n%s", //LCOV_EXCL_LINE - SELECT_RESTRICTION_TYPE, + STC_LOGE("Can't perform sql query: %s\n", //LCOV_EXCL_LINE sqlite3_errmsg(stc_db_get_database())); error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE } -- 2.7.4 From 941d2b993313976d42a0908980024ca5a4b428ac Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 30 May 2018 16:24:44 +0900 Subject: [PATCH 10/16] Load procfs on booting time Change-Id: If68c6428cfb364aff9fbfd2a904ff6fee82a6a4d Signed-off-by: hyunuktak --- include/stc-manager-plugin-procfs.h | 1 + packaging/stc-manager.spec | 2 +- plugin/procfs/include/stc-plugin-procfs.h | 2 ++ plugin/procfs/stc-plugin-procfs.c | 43 +++++++++++++++++++++++++++++++ src/helper/helper-procfs.c | 29 +++++++++++++++++++++ src/helper/helper-procfs.h | 7 +++++ src/stc-manager-plugin-procfs.c | 11 ++++++++ src/stc-manager.c | 1 + 8 files changed, 95 insertions(+), 1 deletion(-) mode change 100644 => 100755 include/stc-manager-plugin-procfs.h mode change 100644 => 100755 plugin/procfs/include/stc-plugin-procfs.h mode change 100644 => 100755 plugin/procfs/stc-plugin-procfs.c mode change 100644 => 100755 src/helper/helper-procfs.c mode change 100644 => 100755 src/helper/helper-procfs.h mode change 100644 => 100755 src/stc-manager-plugin-procfs.c mode change 100644 => 100755 src/stc-manager.c diff --git a/include/stc-manager-plugin-procfs.h b/include/stc-manager-plugin-procfs.h old mode 100644 new mode 100755 index 83e7332..9e7b565 --- a/include/stc-manager-plugin-procfs.h +++ b/include/stc-manager-plugin-procfs.h @@ -24,6 +24,7 @@ int stc_plugin_procfs_init(void); int stc_plugin_procfs_deinit(void); +stc_error_e stc_plugin_procfs_load_pid(void); stc_error_e stc_plugin_procfs_app_status_changed(stc_cmd_type_e cmd, pid_t pid, const gchar *app_id, const gchar *pkg_id, stc_app_type_e app_type); diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 7d6decf..52b858d 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.66 +Version: 0.0.67 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/plugin/procfs/include/stc-plugin-procfs.h b/plugin/procfs/include/stc-plugin-procfs.h old mode 100644 new mode 100755 index 3a02bfe..7375041 --- a/plugin/procfs/include/stc-plugin-procfs.h +++ b/plugin/procfs/include/stc-plugin-procfs.h @@ -24,6 +24,7 @@ typedef struct { int (*initialize_plugin) (void); int (*deinitialize_plugin) (void); + int (*procfs_load) (void); int (*procfs_status_changed) (stc_cmd_type_e cmd, pid_t pid, const gchar *app_id, const gchar *pkg_id, stc_app_type_e app_type); } stc_plugin_procfs_s; @@ -31,6 +32,7 @@ typedef struct { int stc_plugin_procfs_initialize(void); int stc_plugin_procfs_deinitialize(void); +int stc_plugin_procfs_load(void); stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd, pid_t pid, const gchar *app_id, const gchar *pkg_id, stc_app_type_e app_type); diff --git a/plugin/procfs/stc-plugin-procfs.c b/plugin/procfs/stc-plugin-procfs.c old mode 100644 new mode 100755 index 36b8f78..80cf9b1 --- a/plugin/procfs/stc-plugin-procfs.c +++ b/plugin/procfs/stc-plugin-procfs.c @@ -523,6 +523,37 @@ static int __unsubscribe_proc_events(void) return 0; } +static bool __process_pid_cb(pid_t pid, void *user_data) +{ + char cmdline[PROC_NAME_MAX] = {0, }; + char status[PROC_STATUS_CNT][PROC_BUF_MAX]; + + memset(status, 0x0, sizeof(status)); + + if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && + STC_ERROR_NONE == proc_get_status(pid, status)) { + + if (__check_excn(cmdline)) + return true; + + unsigned int i; + proc_key_s key; + proc_value_s value; + + memset(&key, 0x0, sizeof(proc_key_s)); + memset(&value, 0x0, sizeof(proc_value_s)); + + key.pid = pid; + for (i = 0; i < PROC_STATUS_CNT; ++i) + g_strlcpy(value.status[i], status[i], sizeof(value.status[i])); + g_strlcpy(value.cmdline, cmdline, sizeof(value.cmdline)); + + __proc_tree_add(&key, &value); + } + + return true; +} + int stc_plugin_procfs_initialize(void) { __STC_LOG_FUNC_ENTER__; @@ -558,6 +589,16 @@ int stc_plugin_procfs_deinitialize(void) return STC_ERROR_NONE; } +stc_error_e stc_plugin_procfs_load(void) +{ + __STC_LOG_FUNC_ENTER__; + + proc_foreach_pid(__process_pid_cb, NULL); + + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_NONE; +} + stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd, pid_t pid, const gchar *app_id, const gchar *pkg_id, @@ -682,6 +723,8 @@ API stc_plugin_procfs_s stc_plugin_procfs = { stc_plugin_procfs_initialize, .deinitialize_plugin = stc_plugin_procfs_deinitialize, + .procfs_load = + stc_plugin_procfs_load, .procfs_status_changed = stc_plugin_procfs_status_changed }; diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c old mode 100644 new mode 100755 index 020987d..8cd0461 --- a/src/helper/helper-procfs.c +++ b/src/helper/helper-procfs.c @@ -118,6 +118,35 @@ pid_t find_pid_from_cmdline(char *cmdline) return foundpid; } +API void proc_foreach_pid(proc_pid_cb cb, void *user_data) +{ + pid_t pid = -1; + int ret = 0; + DIR *dp; + struct dirent *dentry; + + dp = opendir("/proc"); + if (!dp) { + STC_LOGE("failed to open /proc"); + return; + } + + while ((dentry = readdir(dp)) != NULL) { + if (!isdigit(dentry->d_name[0])) + continue; + + pid = atoi(dentry->d_name); + if (!pid) + continue; + + ret = cb(pid, user_data); + if (ret == false) + break; + } + + closedir(dp); +} + int proc_get_label(pid_t pid, char *label) { char buf[PROC_BUF_MAX]; diff --git a/src/helper/helper-procfs.h b/src/helper/helper-procfs.h old mode 100644 new mode 100755 index 55c6a03..26839c6 --- a/src/helper/helper-procfs.h +++ b/src/helper/helper-procfs.h @@ -22,6 +22,8 @@ #define PROC_BUF_MAX 64 +typedef bool (*proc_pid_cb)(pid_t pid, void *user_data); + /** * @desc get command line from /proc/{pid}/cmdline * @return negative value if error @@ -36,6 +38,11 @@ int proc_get_cmdline(pid_t pid, char *cmdline); pid_t find_pid_from_cmdline(char *cmdline); /** + * @desc find pid from /proc + */ +void proc_foreach_pid(proc_pid_cb cb, void *user_data); + +/** * @desc get smack subject label from /proc/{pid}/attr/current * this label can indicate package name about child processes * @return negative value if error or pid doesn't exist diff --git a/src/stc-manager-plugin-procfs.c b/src/stc-manager-plugin-procfs.c old mode 100644 new mode 100755 index b83a7e8..1067f5e --- a/src/stc-manager-plugin-procfs.c +++ b/src/stc-manager-plugin-procfs.c @@ -67,6 +67,17 @@ int stc_plugin_procfs_deinit(void) return STC_ERROR_NONE; } +stc_error_e stc_plugin_procfs_load_pid(void) +{ + if (!stc_plugin_enabled) + return STC_ERROR_UNINITIALIZED; + + if (!stc_plugin) + return STC_ERROR_UNINITIALIZED; + + return stc_plugin->procfs_load(); +} + stc_error_e stc_plugin_procfs_app_status_changed(stc_cmd_type_e cmd, pid_t pid, const gchar *app_id, const gchar *pkg_id, stc_app_type_e app_type) { diff --git a/src/stc-manager.c b/src/stc-manager.c old mode 100644 new mode 100755 index 799839e..00854ea --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -110,6 +110,7 @@ static stc_s *__stc_manager_init(void) if (err != STC_ERROR_NONE) goto handle_error; + stc_plugin_procfs_load_pid(); stc_manager_gdbus_init((gpointer)stc); STC_LOGI("stc manager initialized"); -- 2.7.4 From cab8bbff4b7610ecbe70a90a980ebe1579e08c21 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 31 May 2018 16:06:51 +0900 Subject: [PATCH 11/16] Organize log for print tree Change-Id: Ie27476ac28f8939df71dd0739adbcfccad427fb1 Signed-off-by: hyunuktak --- plugin/procfs/stc-plugin-procfs.c | 12 +++++--- src/database/tables/table-statistics.c | 3 +- src/monitor/stc-monitor.c | 52 ++++++++++++++++++++-------------- 3 files changed, 40 insertions(+), 27 deletions(-) mode change 100644 => 100755 src/database/tables/table-statistics.c mode change 100644 => 100755 src/monitor/stc-monitor.c diff --git a/plugin/procfs/stc-plugin-procfs.c b/plugin/procfs/stc-plugin-procfs.c index 80cf9b1..e889b7d 100755 --- a/plugin/procfs/stc-plugin-procfs.c +++ b/plugin/procfs/stc-plugin-procfs.c @@ -102,6 +102,7 @@ static proc_value_s * __proc_tree_lookup(const proc_key_s *key) return lookup; } +/* static gboolean __proc_tree_foreach_print(gpointer key, gpointer value, gpointer data) { @@ -119,6 +120,7 @@ static void __proc_tree_printall(void) { g_tree_foreach(proc_tree, __proc_tree_foreach_print, NULL); } +*/ static proc_value_s * __proc_tree_find_parent(proc_value_s *value) { @@ -193,8 +195,9 @@ static void __proc_tree_add(proc_key_s *key, proc_value_s *value) g_tree_insert(proc_tree, proc_key, proc_value); - if (STC_DEBUG_LOG) - __proc_tree_printall(); + /* + __proc_tree_printall(); + */ parent = __proc_tree_find_parent(proc_value); if (parent != NULL) @@ -217,8 +220,9 @@ static void __proc_tree_remove(const proc_key_s *key) g_tree_remove(proc_tree, key); - if (STC_DEBUG_LOG) - __proc_tree_printall(); + /* + __proc_tree_printall(); + */ } static gboolean __check_excn(char *cmdline) diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c old mode 100644 new mode 100755 index 6f2fe03..436c55d --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -628,7 +628,8 @@ stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key, goto handle_error; //LCOV_EXCL_LINE } - STC_LOGD("App stat recorded [%s]", stat->app_id); + if (STC_DEBUG_LOG) + STC_LOGD("App stat recorded [%s]", stat->app_id); handle_error: sqlite3_reset(stmt); diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c old mode 100644 new mode 100755 index adb79a2..cbd6f11 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -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; @@ -1074,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 @@ -1158,10 +1159,11 @@ static void __fill_nfacct_result(char *cnt_name, int64_t bytes, return; //LCOV_EXCL_LINE } - 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 (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, @@ -1307,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) @@ -1322,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) @@ -1505,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, @@ -1758,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 } @@ -1812,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 } @@ -1844,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; } @@ -1871,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; } -- 2.7.4 From ff0aeaa7ac6d41945081a35dc0a704f6578be342 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Fri, 8 Jun 2018 14:51:25 +0900 Subject: [PATCH 12/16] Add inotify for information config Change-Id: I2d7a54bb54390442702dfe78ab30cd3434a898df Signed-off-by: hyunuktak --- plugin/procfs/stc-plugin-procfs.c | 24 +----- src/helper/helper-inotify.c | 175 ++++++++++++++++++++++++++++++++++++++ src/helper/helper-inotify.h | 31 +++++++ src/helper/helper-net-cls.c | 5 +- src/helper/helper-nfacct-rule.c | 6 -- src/helper/helper-procfs.c | 0 src/helper/helper-procfs.h | 0 src/monitor/stc-monitor.c | 13 +-- src/stc-manager.c | 25 +++++- 9 files changed, 235 insertions(+), 44 deletions(-) create mode 100755 src/helper/helper-inotify.c create mode 100755 src/helper/helper-inotify.h mode change 100644 => 100755 src/helper/helper-net-cls.c mode change 100644 => 100755 src/helper/helper-nfacct-rule.c mode change 100755 => 100644 src/helper/helper-procfs.c mode change 100755 => 100644 src/helper/helper-procfs.h diff --git a/plugin/procfs/stc-plugin-procfs.c b/plugin/procfs/stc-plugin-procfs.c index e889b7d..82b301c 100755 --- a/plugin/procfs/stc-plugin-procfs.c +++ b/plugin/procfs/stc-plugin-procfs.c @@ -164,14 +164,8 @@ static void __proc_tree_add(proc_key_s *key, proc_value_s *value) } lookup = g_tree_lookup(proc_tree, key); - if (lookup) { - if (STC_DEBUG_LOG) - STC_LOGD("LOOKUP: tgid[\033[1;33m%s\033[0;m] pid[%s] ppid[\033[1;35m%s\033[0;m] " - "cmdline[\033[0;34m%s\033[0;m] name[%s]", lookup->status[PROC_STATUS_TGID], - lookup->status[PROC_STATUS_PID], lookup->status[PROC_STATUS_PPID], - lookup->cmdline, lookup->status[PROC_STATUS_NAME]); + if (lookup) return; - } proc_key_s *proc_key = MALLOC0(proc_key_s, 1); if (proc_key == NULL) { @@ -189,10 +183,6 @@ static void __proc_tree_add(proc_key_s *key, proc_value_s *value) memcpy(proc_key, key, sizeof(proc_key_s)); memcpy(proc_value, value, sizeof(proc_value_s)); - if (STC_DEBUG_LOG) - STC_LOGD("cmdline [%s] pid[%s] ppid[%s]", value->cmdline, - value->status[PROC_STATUS_PID], value->status[PROC_STATUS_PPID]); - g_tree_insert(proc_tree, proc_key, proc_value); /* @@ -330,13 +320,9 @@ static void __process_event_fork(int tgid, int pid) g_strlcpy(value.status[i], status[i], sizeof(value.status[i])); g_strlcpy(value.cmdline, cmdline, sizeof(value.cmdline)); - if (STC_DEBUG_LOG) { + if (STC_DEBUG_LOG) STC_LOGD("\033[1;34mFORK\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] " "cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, pid); - STC_LOGD("STATUS: tgid[%s] pid[%s] ppid[%s] name[%s] state[%s] tracerpid[%s]", - status[PROC_STATUS_TGID], status[PROC_STATUS_PID], status[PROC_STATUS_PPID], - status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]); - } __proc_tree_add(&key, &value); } @@ -375,13 +361,9 @@ static void __process_event_exec(int tgid, int pid) sizeof(value.status[i])); g_strlcpy(value.cmdline, cmdline, sizeof(value.cmdline)); - if (STC_DEBUG_LOG) { + if (STC_DEBUG_LOG) STC_LOGD("\033[1;32mEXEC\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] " "cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, pid); - STC_LOGD("STATUS: tgid[%s] pid[%s] ppid[%s] name[%s] state[%s] tracerpid[%s]", - status[PROC_STATUS_TGID], status[PROC_STATUS_PID], status[PROC_STATUS_PPID], - status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]); - } __proc_tree_add(&key, &value); } diff --git a/src/helper/helper-inotify.c b/src/helper/helper-inotify.c new file mode 100755 index 0000000..05d10db --- /dev/null +++ b/src/helper/helper-inotify.c @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "helper-inotify.h" +#include "stc-manager-util.h" + +typedef struct { + GIOChannel *channel; + uint watch; + int wd; + + inotify_event_cb cb; +} stc_inotify_s; + +static GHashTable *g_inotify_hash; + +static void __inotify_destroy(gpointer user_data) +{ + int fd; + stc_inotify_s *inotify = user_data; + + if (!inotify->channel) + return; + + if (inotify->watch > 0) + g_source_remove(inotify->watch); + + if (inotify->wd >= 0) { + fd = g_io_channel_unix_get_fd(inotify->channel); + inotify_rm_watch(fd, inotify->wd); + } + + g_io_channel_unref(inotify->channel); +} + +static gboolean __inotify_data(GIOChannel *channel, + GIOCondition cond, gpointer user_data) +{ + stc_inotify_s *inotify = user_data; + char buf[256]; + char *next_event = buf; + gsize bytes_read; + GIOStatus status; + + if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) { + inotify->watch = 0; + return false; + } + + status = g_io_channel_read_chars(channel, buf, + sizeof(buf), &bytes_read, NULL); + if (status != G_IO_STATUS_NORMAL) { + if (status == G_IO_STATUS_AGAIN) + return true; + else { + STC_LOGE("Failed to read from inotify channel"); + inotify->watch = 0; + return false; + } + } + + while (bytes_read > 0) { + struct inotify_event *event = next_event; + gchar *ident = NULL; + gsize len = 0; + + len = sizeof(*event) + event->len; + if (bytes_read < len) + break; + + if (event->len) + ident = next_event + sizeof(*event); + + next_event += len; + bytes_read -= len; + + (inotify->cb)(event, ident); + } + + return true; +} + +int inotify_register(const char *path, inotify_event_cb cb) +{ + int fd; + stc_inotify_s *inotify; + + if (!cb) + return -EINVAL; + + inotify = g_hash_table_lookup(g_inotify_hash, path); + if (inotify) { + inotify->cb = cb; + return 0; + } + + inotify = g_try_new0(stc_inotify_s, 1); + if (!inotify) + return -ENOMEM; + + fd = inotify_init(); + if (fd < 0) { + FREE(inotify); + return -EIO; + } + + inotify->wd = inotify_add_watch(fd, path, IN_MODIFY); + if (inotify->wd < 0) { + STC_LOGE("Failed to create watch [%s]", path); + FREE(inotify); + close(fd); + return -EIO; + } + + inotify->channel = g_io_channel_unix_new(fd); + if (!inotify->channel) { + STC_LOGE("Failed to create channel"); + inotify_rm_watch(fd, inotify->wd); + FREE(inotify); + close(fd); + return -EIO; + } + + g_io_channel_set_close_on_unref(inotify->channel, TRUE); + g_io_channel_set_encoding(inotify->channel, NULL, NULL); + g_io_channel_set_buffered(inotify->channel, FALSE); + + inotify->watch = g_io_add_watch(inotify->channel, + G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR, + __inotify_data, inotify); + + inotify->cb = cb; + + g_hash_table_insert(g_inotify_hash, g_strdup(path), inotify); + return 0; +} + +void inotify_deregister(const char *path) +{ + stc_inotify_s *inotify; + + inotify = g_hash_table_lookup(g_inotify_hash, path); + if (!inotify) + return; + + g_hash_table_remove(g_inotify_hash, path); +} + +int inotify_initialize(void) +{ + g_inotify_hash = g_hash_table_new_full(g_str_hash, g_str_equal, + g_free, __inotify_destroy); + + return 0; +} + +void inotify_deinitialize(void) +{ + g_hash_table_destroy(g_inotify_hash); +} diff --git a/src/helper/helper-inotify.h b/src/helper/helper-inotify.h new file mode 100755 index 0000000..aaadafc --- /dev/null +++ b/src/helper/helper-inotify.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __STC_HELPER_INOTIFY_H__ +#define __STC_HELPER_INOTIFY_H__ + +#include + +struct inotify_event; +typedef void (* inotify_event_cb) (struct inotify_event *event, const char *ident); + +int inotify_register(const char *path, inotify_event_cb cb); +void inotify_deregister(const char *path); + +int inotify_initialize(void); +void inotify_deinitialize(void); + +#endif /*__STC_HELPER_INOTIFY_H__*/ diff --git a/src/helper/helper-net-cls.c b/src/helper/helper-net-cls.c old mode 100644 new mode 100755 index e13c729..f3a2ef5 --- a/src/helper/helper-net-cls.c +++ b/src/helper/helper-net-cls.c @@ -189,11 +189,8 @@ stc_error_e place_pids_to_net_cgroup(const int pid, const char *app_id) else path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK; //LCOV_EXCL_LINE - if (access(child_buf, F_OK)) { - if (STC_DEBUG_LOG) - STC_LOGD("%s of %s is not existed", child_buf, app_id); //LCOV_EXCL_LINE + if (access(child_buf, F_OK)) return cgroup_write_pid(path_to_net_cgroup_dir, app_id, pid); - } return cgroup_write_pidtree(path_to_net_cgroup_dir, app_id, pid); //LCOV_EXCL_LINE } diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c old mode 100644 new mode 100755 index 3923daf..caf1174 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -142,9 +142,6 @@ static stc_error_e nfacct_send_new(nfacct_rule_s *counter) prepare_netlink_msg(req, NFNL_MSG_ACCT_NEW, NLM_F_CREATE | NLM_F_ACK); add_string_attr(req, counter->name, NFACCT_NAME); - if (STC_DEBUG_LOG) - STC_LOGD("counter name %s", counter->name); //LCOV_EXCL_LINE - /* padding */ add_uint64_attr(req, 0, NFACCT_PKTS); add_uint64_attr(req, 0, NFACCT_BYTES); @@ -172,9 +169,6 @@ stc_error_e nfacct_send_del(nfacct_rule_s *counter) return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } - if (STC_DEBUG_LOG) - STC_LOGD("send remove request for %s", counter->name); //LCOV_EXCL_LINE - prepare_netlink_msg(req, NFNL_MSG_ACCT_DEL, NLM_F_ACK); add_string_attr(req, counter->name, NFACCT_NAME); diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c old mode 100755 new mode 100644 diff --git a/src/helper/helper-procfs.h b/src/helper/helper-procfs.h old mode 100755 new mode 100644 diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index cbd6f11..8ee3533 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -1151,9 +1151,6 @@ static void __fill_nfacct_result(char *cnt_name, int64_t bytes, .data_limit_reached = FALSE, }; - if (STC_DEBUG_LOG) - STC_LOGD("cnt_name %s", cnt_name); //LCOV_EXCL_LINE - if (!recreate_counter_by_name(cnt_name, &counter)) { STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE return; //LCOV_EXCL_LINE @@ -1760,11 +1757,8 @@ API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key, ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!"); lookup = __application_lookup(g_system->apps, &app_key); - if (lookup) { - if (STC_DEBUG_LOG) - STC_LOGD("app_key already present"); //LCOV_EXCL_LINE + if (lookup) return STC_ERROR_NONE; //LCOV_EXCL_LINE - } key = MALLOC0(stc_app_key_s, 1); if (!key) { @@ -1822,11 +1816,8 @@ API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key, } proc_lookup = __process_lookup(app_lookup->processes, &proc_key); - if (proc_lookup) { - if (STC_DEBUG_LOG) - STC_LOGD("proc_key already present"); //LCOV_EXCL_LINE + if (proc_lookup) return STC_ERROR_NONE; //LCOV_EXCL_LINE - } key = MALLOC0(stc_process_key_s, 1); if (!key) { diff --git a/src/stc-manager.c b/src/stc-manager.c index 00854ea..d83f079 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -26,6 +26,7 @@ #include "helper-cgroup.h" #include "helper-nfacct-rule.h" #include "helper-iptables.h" +#include "helper-inotify.h" #include "stc-monitor.h" #include "stc-firewall.h" #include "stc-manager-plugin-appstatus.h" @@ -36,7 +37,6 @@ static stc_s *g_stc = NULL; -/* static gboolean __validate_ident(const char *ident) { unsigned int i; @@ -50,7 +50,22 @@ static gboolean __validate_ident(const char *ident) return TRUE; } -*/ + +static void __stc_inotify_handler(struct inotify_event *event, const char *ident) +{ + if (!ident) + return; + + if (!__validate_ident(ident)) { + STC_LOGE("Invalid ident [%s]", ident); + return; + } + + if (!g_strcmp0(ident, INFO_CONFIG)) { + int debug = stc_util_get_config_int(INFO_DEBUGLOG); + stc_util_set_debuglog(debug); + } +} static void __stc_manager_deinit(void) { @@ -76,6 +91,9 @@ static void __stc_manager_deinit(void) stc_plugin_exception_deinit(); stc_plugin_procfs_deinit(); + inotify_deregister(INFO_STORAGE_DIR); + inotify_deinitialize(); + STC_LOGI("stc manager deinitialized"); FREE(g_stc); __STC_LOG_FUNC_EXIT__; @@ -96,6 +114,9 @@ static stc_s *__stc_manager_init(void) stc_util_initialize_config(); + inotify_initialize(); + inotify_register(INFO_STORAGE_DIR, __stc_inotify_handler); + cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT); EXEC(STC_ERROR_NONE, stc_db_initialize()); -- 2.7.4 From d56633f1127e88f7744282e9feaf247d580cf31a Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Fri, 15 Jun 2018 13:33:54 +0900 Subject: [PATCH 13/16] Fix a build warning for initialization from incompatible pointer type Change-Id: I780bcde31ee66a14e1b9d18521b8c1ef8d6350b8 Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- src/helper/helper-inotify.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 52b858d..857db2c 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.67 +Version: 0.0.68 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-inotify.c b/src/helper/helper-inotify.c index 05d10db..1283cc6 100755 --- a/src/helper/helper-inotify.c +++ b/src/helper/helper-inotify.c @@ -75,7 +75,8 @@ static gboolean __inotify_data(GIOChannel *channel, } while (bytes_read > 0) { - struct inotify_event *event = next_event; + struct inotify_event *event = + (struct inotify_event *)next_event; gchar *ident = NULL; gsize len = 0; -- 2.7.4 From f1241cfe85a7d99d8cf188b86f018e130cb376fd Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Mon, 25 Jun 2018 16:58:38 +0900 Subject: [PATCH 14/16] Fix some security issues Change-Id: I2b31052aa20c028f0a4b496ec50b54641cce8bd9 Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- src/helper/helper-cgroup.c | 2 +- src/helper/helper-file.c | 6 ++++++ src/helper/helper-net-cls.c | 2 +- src/helper/helper-nfacct-rule.c | 4 ++-- 5 files changed, 11 insertions(+), 5 deletions(-) mode change 100644 => 100755 src/helper/helper-cgroup.c mode change 100644 => 100755 src/helper/helper-file.c diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 857db2c..9fe0c3c 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.68 +Version: 0.0.69 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-cgroup.c b/src/helper/helper-cgroup.c old mode 100644 new mode 100755 index dc51231..1f195ba --- a/src/helper/helper-cgroup.c +++ b/src/helper/helper-cgroup.c @@ -76,7 +76,7 @@ stc_error_e cgroup_write_pidtree(const char *cgroup_subsystem, char buf[MAX_PATH_LENGTH]; /*/proc/%d/task/%d/children */ - char child_buf[21 + MAX_DEC_SIZE(int) + MAX_DEC_SIZE(int)]; + char child_buf[21 + MAX_DEC_SIZE(int) + MAX_DEC_SIZE(int) + 1]; char pidbuf[MAX_DEC_SIZE(int)]; stc_error_e ret; diff --git a/src/helper/helper-file.c b/src/helper/helper-file.c old mode 100644 new mode 100755 index 63ad2bd..636ef5d --- a/src/helper/helper-file.c +++ b/src/helper/helper-file.c @@ -23,10 +23,16 @@ int fwrite_str(const char *path, const char *str) { _cleanup_fclose_ FILE *f = NULL; int ret; + char * t; assert(path); assert(str); + t = realpath(path, NULL); + ret_value_errno_msg_if(!t, -errno, + "Fail to get realpath %s", path); + free(t); + f = fopen(path, "w"); ret_value_errno_msg_if(!f, -errno, "Fail to open file %s", path); diff --git a/src/helper/helper-net-cls.c b/src/helper/helper-net-cls.c index f3a2ef5..748f95f 100755 --- a/src/helper/helper-net-cls.c +++ b/src/helper/helper-net-cls.c @@ -172,7 +172,7 @@ handle_error: stc_error_e place_pids_to_net_cgroup(const int pid, const char *app_id) { - char child_buf[21 + MAX_DEC_SIZE(int) + MAX_DEC_SIZE(int)]; + char child_buf[21 + MAX_DEC_SIZE(int) + MAX_DEC_SIZE(int) + 1]; const char *path_to_net_cgroup_dir = NULL; snprintf(child_buf, sizeof(child_buf), PROC_TASK_CHILDREN, pid, pid); diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index caf1174..1a2d188 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -478,7 +478,7 @@ static stc_error_e produce_app_rule(nfacct_rule_s *rule) char *set_cmd = get_iptables_cmd(rule->action); char *jump_cmd = get_iptables_jump(rule->jump); char nfacct_buf[sizeof(NFACCT_NAME_MOD) + - 3*MAX_DEC_SIZE(int) + 4]; + 3*MAX_DEC_SIZE(int) + 4 + 1]; stc_error_e ret = STC_ERROR_NONE; /* income part */ @@ -581,7 +581,7 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule) char *set_cmd = get_iptables_cmd(rule->action); char *jump_cmd = get_iptables_jump(rule->jump); char nfacct_buf[sizeof(NFACCT_NAME_MOD) + - 3*MAX_DEC_SIZE(int) + 4]; + 3*MAX_DEC_SIZE(int) + 4 + 1]; uint32_t classid = rule->classid; stc_error_e ret; -- 2.7.4 From 146aa9755afe0e2a9fd85e63d9390e66122f0a73 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 4 Jul 2018 15:13:27 +0900 Subject: [PATCH 15/16] Sync current time Change-Id: I0b741ffd37d3848a6ce01c260908e3acd8b80cf2 Signed-off-by: hyunuktak --- src/monitor/stc-monitor.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 8ee3533..9db1cfb 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -1013,7 +1013,8 @@ static gboolean __update_app_statistics(gpointer key, gpointer value, static gboolean __flush_apps_stats_to_database(gpointer user_data) { - time_t current_time = time(0); + time_t current_time = 0; + time(¤t_time); if (g_system->apps_tree_updated == FALSE) return G_SOURCE_REMOVE; @@ -1109,7 +1110,7 @@ static void __interface_counter_update(stc_app_key_s *app_key, 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); + __app_counter_update(app_key, app_value, context); } -- 2.7.4 From 1b21d2dd6e6a51192bad23b595cebdbd810bffd2 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 11 Jul 2018 18:11:42 +0900 Subject: [PATCH 16/16] Added data limits monitoring for daily, weekly and monthly Change-Id: Ibc94757d36210c9167ebe4ddb7c36053d13f60fd Signed-off-by: hyunuktak --- data/traffic_db.sql | 14 +- packaging/stc-manager.spec | 2 +- src/database/include/table-counters.h | 13 +- src/database/include/table-restrictions.h | 4 + src/database/tables/table-counters.c | 214 +++++++++++- src/database/tables/table-restrictions.c | 32 +- src/database/tables/table-statistics.c | 3 +- src/helper/helper-nfacct-rule.c | 2 +- src/monitor/include/stc-monitor.h | 31 +- src/monitor/include/stc-time.h | 24 ++ src/monitor/stc-monitor.c | 533 +++++++++++++++++++++++------- src/monitor/stc-time.c | 104 ++++++ src/stc-manager-gdbus.c | 2 +- src/stc-restriction.c | 41 ++- 14 files changed, 867 insertions(+), 152 deletions(-) create mode 100644 src/monitor/include/stc-time.h create mode 100644 src/monitor/stc-time.c diff --git a/data/traffic_db.sql b/data/traffic_db.sql index 1444598..cd06f8d 100644 --- a/data/traffic_db.sql +++ b/data/traffic_db.sql @@ -24,7 +24,11 @@ CREATE TABLE IF NOT EXISTS restrictions ( roaming INT, subscriber_id TEXT NOT NULL, data_limit BIGINT, - data_warn_limit BIGINT + data_warn_limit BIGINT, + monthly_limit BIGINT, + weekly_limit BIGINT, + daily_limit BIGINT, + month_start_date INT ); CREATE INDEX IF NOT EXISTS restrictions_index ON restrictions (binpath, iftype, ifname); @@ -32,5 +36,13 @@ CREATE INDEX IF NOT EXISTS restrictions_index ON restrictions (binpath, iftype, CREATE TABLE IF NOT EXISTS counters ( restriction_id INTEGER NOT NULL, data_counter BIGINT, + warn_counter BIGINT, + monthly_counter BIGINT, + weekly_counter BIGINT, + daily_counter BIGINT, + month_start_date INT, + month_start_ts BIGINT, + week_start_ts BIGINT, + day_start_ts BIGINT, PRIMARY KEY (restriction_id) ); diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 9fe0c3c..bcad233 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.69 +Version: 0.0.70 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/database/include/table-counters.h b/src/database/include/table-counters.h index a0ef60d..5faff83 100644 --- a/src/database/include/table-counters.h +++ b/src/database/include/table-counters.h @@ -20,6 +20,14 @@ typedef struct { uint64_t restriction_id; int64_t data_counter; + int64_t warn_counter; + int64_t monthly_counter; + int64_t weekly_counter; + int64_t daily_counter; + int32_t month_start_date; + int64_t month_start_ts; + int64_t week_start_ts; + int64_t day_start_ts; } table_counters_info; typedef stc_cb_ret_e(*table_counters_info_cb)(const table_counters_info *info, @@ -30,7 +38,10 @@ stc_error_e table_counters_get(uint64_t restriction_id, stc_error_e table_counters_update_counters(const table_counters_info *info); -stc_error_e table_counters_insert(const table_counters_info *info); +stc_error_e table_counters_get_timestamps(uint64_t restriction_id, + table_counters_info *info); + +stc_error_e table_counters_update_timestamps(const table_counters_info *info); stc_error_e table_counters_delete(uint64_t restriction_id); diff --git a/src/database/include/table-restrictions.h b/src/database/include/table-restrictions.h index 77793d1..ea36988 100644 --- a/src/database/include/table-restrictions.h +++ b/src/database/include/table-restrictions.h @@ -26,6 +26,10 @@ typedef struct { stc_roaming_type_e roaming; int64_t data_limit; int64_t data_warn_limit; + int64_t monthly_limit; + int64_t weekly_limit; + int64_t daily_limit; + int month_start_date; uint64_t restriction_id; } table_restrictions_info; diff --git a/src/database/tables/table-counters.c b/src/database/tables/table-counters.c index 11c99a4..9aec545 100644 --- a/src/database/tables/table-counters.c +++ b/src/database/tables/table-counters.c @@ -26,13 +26,30 @@ #define DELETE_COUNTER "DELETE FROM counters WHERE restriction_id=?" -#define SELECT_COUNTER "SELECT data_counter FROM counters WHERE restriction_id=?" +#define SELECT_RESTRICTION_ID "SELECT restriction_id FROM counters " \ + " WHERE restriction_id = ?" -#define INSERT_COUNTER "INSERT INTO counters (restriction_id, data_counter) " \ - " VALUES (?, ?)" +#define SELECT_COUNTER "SELECT data_counter, warn_counter, monthly_counter, weekly_counter, daily_counter " \ + " FROM counters WHERE restriction_id = ?" -#define UPDATE_COUNTER "REPLACE INTO counters (restriction_id, data_counter) " \ - " VALUES (?, ?)" +#define UPDATE_COUNTER "UPDATE counters " \ + " SET data_counter = ?, warn_counter = ?, monthly_counter = ?, weekly_counter = ?, daily_counter = ? " \ + " WHERE restriction_id = ?" + +#define INSERT_COUNTER "INSERT INTO counters " \ + " (restriction_id, data_counter, warn_counter, monthly_counter, weekly_counter, daily_counter) " \ + " VALUES (?, ?, ?, ?, ?, ?)" + +#define SELECT_TIMESTAMP "SELECT month_start_date, month_start_ts, week_start_ts, day_start_ts " \ + " FROM counters WHERE restriction_id = ?" + +#define UPDATE_TIMESTAMP "UPDATE counters " \ + " SET month_start_date = ?, month_start_ts = ?, week_start_ts = ?, day_start_ts = ? " \ + " WHERE restriction_id = ?" + +#define INSERT_TIMESTAMP "INSERT INTO counters " \ + " (restriction_id, month_start_date, month_start_ts, week_start_ts, day_start_ts) " \ + " VALUES (?, ?, ?, ?, ?)" static void __finalize_delete(void); @@ -73,6 +90,21 @@ static void __finalize_update(void); } \ } while (0) +static void __finalize_insert(void); + +#define PREPARE_INSERT(stm, query) do { \ + rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \ + if (rc != SQLITE_OK) { \ + stm = NULL; \ + __finalize_insert(); \ + STC_LOGE("Failed to prepare \"%s\"query" \ + , query); \ + return rc; \ + } \ + } while (0) + + + #define FINALIZE(stm) do { \ if (stm) { \ sqlite3_finalize(stm); \ @@ -81,8 +113,13 @@ static void __finalize_update(void); } while (0) static sqlite3_stmt *delete_counter; +static sqlite3_stmt *select_restriction_id; static sqlite3_stmt *select_counter; static sqlite3_stmt *update_counter; +static sqlite3_stmt *insert_counter; +static sqlite3_stmt *select_timestamp; +static sqlite3_stmt *update_timestamp; +static sqlite3_stmt *insert_timestamp; static int __prepare_delete(sqlite3 *db) { @@ -123,6 +160,8 @@ static int __prepare_select(sqlite3 *db) } PREPARE_SELECT(select_counter, SELECT_COUNTER); + PREPARE_SELECT(select_timestamp, SELECT_TIMESTAMP); + PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID); initialized = 1; __STC_LOG_FUNC_EXIT__; @@ -134,6 +173,8 @@ static void __finalize_select(void) __STC_LOG_FUNC_ENTER__; FINALIZE(select_counter); + FINALIZE(select_timestamp); + FINALIZE(select_restriction_id); __STC_LOG_FUNC_EXIT__; } @@ -150,6 +191,7 @@ static int __prepare_update(sqlite3 *db) } PREPARE_UPDATE(update_counter, UPDATE_COUNTER); + PREPARE_UPDATE(update_timestamp, UPDATE_TIMESTAMP); initialized = 1; __STC_LOG_FUNC_EXIT__; @@ -161,10 +203,74 @@ static void __finalize_update(void) __STC_LOG_FUNC_ENTER__; FINALIZE(update_counter); + FINALIZE(update_timestamp); + + __STC_LOG_FUNC_EXIT__; +} + +static int __prepare_insert(sqlite3 *db) +{ + __STC_LOG_FUNC_ENTER__; + int rc; + static int initialized; + + if (initialized) { + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return SQLITE_OK; //LCOV_EXCL_LINE + } + + PREPARE_INSERT(insert_counter, INSERT_COUNTER); + PREPARE_INSERT(insert_timestamp, INSERT_TIMESTAMP); + + initialized = 1; + __STC_LOG_FUNC_EXIT__; + return rc; +} + +static void __finalize_insert(void) +{ + __STC_LOG_FUNC_ENTER__; + + FINALIZE(insert_counter); + FINALIZE(insert_timestamp); __STC_LOG_FUNC_EXIT__; } +static bool __table_counters_is_entry_present(uint64_t restriction_id) +{ + bool ret = FALSE; + int rc; + uint64_t l_restriction_id = -1; + sqlite3_stmt *stmt = select_restriction_id; + + if (sqlite3_bind_int(stmt, 1, restriction_id) != SQLITE_OK) { + ret = FALSE; + __STC_LOG_FUNC_EXIT__; + goto handle_error; + } + + rc = sqlite3_step(stmt); + + switch (rc) { + case SQLITE_DONE: + break; + case SQLITE_ROW: + l_restriction_id = sqlite3_column_int64(stmt, 0); + STC_LOGD("restriction id [%llu]", l_restriction_id); + ret = TRUE; + break; + case SQLITE_ERROR: + default: + STC_LOGE("Failed to get restriction id : %s", + sqlite3_errmsg(stc_db_get_database())); + } + +handle_error: + sqlite3_reset(stmt); + return ret; +} + stc_error_e table_counters_get(uint64_t restriction_id, table_counters_info *info) { @@ -188,6 +294,10 @@ stc_error_e table_counters_get(uint64_t restriction_id, break; case SQLITE_ROW: info->data_counter = sqlite3_column_int64(stmt, 0); + info->warn_counter = sqlite3_column_int64(stmt, 1); + info->monthly_counter = sqlite3_column_int64(stmt, 2); + info->weekly_counter = sqlite3_column_int64(stmt, 3); + info->daily_counter = sqlite3_column_int64(stmt, 4); STC_LOGD("rstn_id [%llu] data_counters [%lld] bytes", restriction_id, info->data_counter); @@ -198,6 +308,7 @@ stc_error_e table_counters_get(uint64_t restriction_id, sqlite3_errmsg(stc_db_get_database())); error_code = STC_ERROR_DB_FAILED; + __STC_LOG_FUNC_EXIT__; } //LCOV_EXCL_STOP } while (rc == SQLITE_ROW); @@ -218,8 +329,15 @@ stc_error_e table_counters_update_counters(const table_counters_info *info) goto handle_error; } + if (__table_counters_is_entry_present(info->restriction_id) == FALSE) + stmt = insert_counter; + DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id)); DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_counter)); + DB_ACTION(sqlite3_bind_int64(stmt, 3, info->warn_counter)); + DB_ACTION(sqlite3_bind_int64(stmt, 4, info->monthly_counter)); + DB_ACTION(sqlite3_bind_int64(stmt, 5, info->weekly_counter)); + DB_ACTION(sqlite3_bind_int64(stmt, 6, info->daily_counter)); if (sqlite3_step(stmt) != SQLITE_DONE) { STC_LOGE("Failed to update counter: %s\n", @@ -238,6 +356,90 @@ handle_error: } //LCOV_EXCL_STOP +stc_error_e table_counters_get_timestamps(uint64_t restriction_id, + table_counters_info *info) +{ + stc_error_e error_code = STC_ERROR_NONE; + sqlite3_stmt *stmt = select_timestamp; + int rc; + + if (info == NULL) { + __STC_LOG_FUNC_EXIT__; + goto handle_error; + } + + info->restriction_id = restriction_id; + + DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id)); + + do { + rc = sqlite3_step(stmt); + + //LCOV_EXCL_START + switch (rc) { + case SQLITE_DONE: + break; + case SQLITE_ROW: + info->month_start_date = sqlite3_column_int(stmt, 0); + info->month_start_ts = sqlite3_column_int64(stmt, 1); + info->week_start_ts = sqlite3_column_int64(stmt, 2); + info->day_start_ts = sqlite3_column_int64(stmt, 3); + + STC_LOGD("rstn_id [%llu] month_start_date [%d], " + "month_start_ts [%lld], week_start_ts [%lld], " + "day_start_ts [%lld]", restriction_id, + info->month_start_date, info->month_start_ts, + info->week_start_ts, info->day_start_ts); + break; + case SQLITE_ERROR: + default: + STC_LOGE("Failed to enumerate counters: %s\n", + sqlite3_errmsg(stc_db_get_database())); + + error_code = STC_ERROR_DB_FAILED; + __STC_LOG_FUNC_EXIT__; + } + //LCOV_EXCL_STOP + } while (rc == SQLITE_ROW); + +handle_error: + sqlite3_reset(stmt); + return error_code; +} + +//LCOV_EXCL_START +stc_error_e table_counters_update_timestamps(const table_counters_info *info) +{ + stc_error_e error_code = STC_ERROR_NONE; + sqlite3_stmt *stmt = update_timestamp; + + if (__table_counters_is_entry_present(info->restriction_id) == FALSE) + stmt = insert_timestamp; + + DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id)); + DB_ACTION(sqlite3_bind_int(stmt, 2, info->month_start_date)); + DB_ACTION(sqlite3_bind_int64(stmt, 3, info->month_start_ts)); + DB_ACTION(sqlite3_bind_int64(stmt, 4, info->week_start_ts)); + DB_ACTION(sqlite3_bind_int64(stmt, 5, info->day_start_ts)); + + if (sqlite3_step(stmt) != SQLITE_DONE) { + STC_LOGE("Failed to update timestamps: %s\n", + sqlite3_errmsg(stc_db_get_database())); + error_code = STC_ERROR_DB_FAILED; + __STC_LOG_FUNC_EXIT__; + goto handle_error; + } + + STC_LOGD("Timestamps updated for restriction_id [%llu]", + info->restriction_id); + +handle_error: + sqlite3_reset(stmt); + return error_code; +} +//LCOV_EXCL_STOP + + stc_error_e table_counters_delete(uint64_t restriction_id) { stc_error_e error_code = STC_ERROR_NONE; @@ -273,6 +475,7 @@ stc_error_e table_counters_prepare(sqlite3 *db) DB_ACTION(__prepare_delete(db)); DB_ACTION(__prepare_select(db)); DB_ACTION(__prepare_update(db)); + DB_ACTION(__prepare_insert(db)); handle_error: __STC_LOG_FUNC_EXIT__; @@ -285,5 +488,6 @@ void table_counters_finalize(void) __finalize_delete(); __finalize_select(); __finalize_update(); + __finalize_insert(); __STC_LOG_FUNC_EXIT__; } diff --git a/src/database/tables/table-restrictions.c b/src/database/tables/table-restrictions.c index 12fa67d..bbbede0 100755 --- a/src/database/tables/table-restrictions.c +++ b/src/database/tables/table-restrictions.c @@ -31,11 +31,13 @@ /* SELECT statements */ #define SELECT_RESTRICTIONS "SELECT binpath, data_limit, " \ " iftype, rstn_type, roaming, ifname, subscriber_id, " \ - " data_warn_limit, restriction_id FROM restrictions" + " data_warn_limit, monthly_limit, weekly_limit, daily_limit, month_start_date, " \ + " restriction_id FROM restrictions" #define SELECT_RESTRICTIONS_PER_APP "SELECT binpath, data_limit, " \ " iftype, rstn_type, roaming, ifname, subscriber_id, " \ - " data_warn_limit, restriction_id " \ + " data_warn_limit, monthly_limit, weekly_limit, daily_limit, month_start_date, " \ + " restriction_id " \ " FROM restrictions INDEXED BY restrictions_index " \ " WHERE binpath = ?" @@ -54,14 +56,16 @@ /* UPDATE statement */ #define UPDATE_NET_RESTRICTIONS "UPDATE restrictions " \ " SET binpath = ?, data_limit = ?, iftype = ?, rstn_type = ?, " \ - " roaming = ?, ifname = ?, subscriber_id = ?, data_warn_limit = ? " \ + " roaming = ?, ifname = ?, subscriber_id = ?, data_warn_limit = ?, " \ + " monthly_limit = ?, weekly_limit = ?, daily_limit = ? " \ " WHERE restriction_id = ?" /* INSERT statement */ #define INSERT_NET_RESTRICTIONS "INSERT INTO restrictions " \ " (binpath, data_limit, iftype, rstn_type, " \ - " roaming, ifname, subscriber_id, data_warn_limit) " \ - " VALUES (?, ?, ?, ?, ?, ?, ?, ?)" + " roaming, ifname, subscriber_id, data_warn_limit, " \ + " monthly_limit, weekly_limit, daily_limit, month_start_date) " \ + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" static void __finalize_delete(void); @@ -291,7 +295,10 @@ stc_error_e table_restrictions_per_app(const gchar* app_id, data.ifname = (char *)sqlite3_column_text(stmt, 5); data.subscriber_id = (char *)sqlite3_column_text(stmt, 6); data.data_warn_limit = sqlite3_column_int64(stmt, 7); - data.restriction_id = sqlite3_column_int64(stmt, 8); + data.monthly_limit = sqlite3_column_int64(stmt, 8); + data.weekly_limit = sqlite3_column_int64(stmt, 9); + data.daily_limit = sqlite3_column_int64(stmt, 10); + data.restriction_id = sqlite3_column_int64(stmt, 11); if (restriction_cb(&data, user_data) == STC_CANCEL) rc = SQLITE_DONE; //LCOV_EXCL_LINE @@ -339,7 +346,11 @@ stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restrict data.ifname = (char *)sqlite3_column_text(stmt, 5); data.subscriber_id = (char *)sqlite3_column_text(stmt, 6); data.data_warn_limit = sqlite3_column_int64(stmt, 7); - data.restriction_id = sqlite3_column_int64(stmt, 8); + data.monthly_limit = sqlite3_column_int64(stmt, 8); + data.weekly_limit = sqlite3_column_int64(stmt, 9); + data.daily_limit = sqlite3_column_int64(stmt, 10); + data.month_start_date = sqlite3_column_int(stmt, 11); + data.restriction_id = sqlite3_column_int64(stmt, 12); if (restriction_cb(&data, user_data) == STC_CANCEL) rc = SQLITE_DONE; //LCOV_EXCL_LINE @@ -524,9 +535,14 @@ stc_error_e table_restrictions_update(table_restrictions_info *info) DB_ACTION(sqlite3_bind_text(stmt, 7, info->subscriber_id ? info->subscriber_id : "", -1, SQLITE_TRANSIENT)); DB_ACTION(sqlite3_bind_int64(stmt, 8, info->data_warn_limit)); + DB_ACTION(sqlite3_bind_int64(stmt, 9, info->monthly_limit)); + DB_ACTION(sqlite3_bind_int64(stmt, 10, info->weekly_limit)); + DB_ACTION(sqlite3_bind_int64(stmt, 11, info->daily_limit)); if (info->restriction_id) - DB_ACTION(sqlite3_bind_int64(stmt, 9, info->restriction_id)); + DB_ACTION(sqlite3_bind_int64(stmt, 12, info->restriction_id)); + else + DB_ACTION(sqlite3_bind_int64(stmt, 12, info->month_start_date)); if (sqlite3_step(stmt) != SQLITE_DONE) { STC_LOGE("Failed to set network restriction: %s\n", //LCOV_EXCL_LINE diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c index 436c55d..6355f60 100755 --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -605,8 +605,7 @@ stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key, SQLITE_STATIC)); DB_ACTION(sqlite3_bind_int(stmt, 2, stat->rcv_count)); DB_ACTION(sqlite3_bind_int(stmt, 3, stat->snd_count)); - DB_ACTION(sqlite3_bind_int64(stmt, 4, - (sqlite3_int64)(last_touch_time))); + DB_ACTION(sqlite3_bind_int64(stmt, 4, (sqlite3_int64)last_touch_time)); DB_ACTION(sqlite3_bind_int(stmt, 5, (int)(stat_key->iftype))); DB_ACTION(sqlite3_bind_int(stmt, 6, (int)(stat->is_roaming))); DB_ACTION(sqlite3_bind_int(stmt, 7, (int)hw_net_protocol_type)); diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index 1a2d188..90364d3 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -249,7 +249,7 @@ static nfacct_rule_direction convert_to_iotype(int type) static stc_iface_type_e convert_to_iftype(int type) { return (type < STC_IFACE_LAST_ELEM && - type > STC_IFACE_UNKNOWN) ? type : STC_IFACE_UNKNOWN; + type > STC_IFACE_UNKNOWN) ? type : STC_IFACE_UNKNOWN; } bool recreate_counter_by_name(char *cnt_name, nfacct_rule_s *cnt) diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index a102ea3..3414c06 100644 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -29,6 +29,19 @@ #define CONTR_TIMER_INTERVAL 1 /** + * @brief enumeration for data limit types + */ +typedef enum { + STC_RSTN_LIMIT_TYPE_DATA_WARN, + STC_RSTN_LIMIT_TYPE_DATA, + STC_RSTN_LIMIT_TYPE_MONTHLY, + STC_RSTN_LIMIT_TYPE_WEEKLY, + STC_RSTN_LIMIT_TYPE_DAILY, + STC_RSTN_LIMIT_TYPE_MAX +} stc_rstn_limit_type_e; + + +/** * @brief key for processes tree */ typedef struct { @@ -80,12 +93,14 @@ typedef struct { uint32_t classid; stc_rstn_state_e rstn_state; stc_rstn_type_e rstn_type; - int64_t data_limit; - int64_t data_warn_limit; - int64_t data_counter; - gboolean data_limit_reached; - gboolean warn_limit_crossed_notified; - gboolean rstn_limit_crossed_notified; + + int64_t counter[STC_RSTN_LIMIT_TYPE_MAX]; + int64_t limit[STC_RSTN_LIMIT_TYPE_MAX]; + int32_t limit_exceeded; + int32_t limit_notified; + + int month_start_date; + time_t month_start_ts; } stc_rstn_value_s; /** @@ -101,6 +116,10 @@ typedef struct { GTree *apps; /**< monitored applications */ gboolean apps_tree_updated; guint background_state; + time_t last_month_ts; + time_t last_week_ts; + time_t last_day_ts; + int month_start_date; } stc_system_s; /** diff --git a/src/monitor/include/stc-time.h b/src/monitor/include/stc-time.h new file mode 100644 index 0000000..d331bce --- /dev/null +++ b/src/monitor/include/stc-time.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __STC_MANAGER_TIME_H__ +#define __STC_MANAGER_TIME_H__ + +time_t stc_time_get_day_start(time_t now); +time_t stc_time_get_week_start(time_t now); +time_t stc_time_get_month_start(time_t now, int month_start_date); + +#endif /* __STC_MANAGER_TIME_H__ */ diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 9db1cfb..4210441 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -28,13 +28,23 @@ #include "table-statistics.h" #include "table-counters.h" #include "stc-monitor.h" +#include "stc-time.h" #include "stc-manager-plugin-appstatus.h" #include "stc-manager-plugin-exception.h" +#define GRANULARITY 10 #define MAX_INT_LENGTH 128 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state" typedef struct { + time_t now; + time_t month_start_ts; + time_t week_start_ts; + time_t day_start_ts; + int is_updated; +} reset_time_limits_context_s; + +typedef struct { stc_app_key_s *app_key; stc_process_key_s *proc_key; gboolean entry_removed; @@ -43,7 +53,7 @@ typedef struct { typedef struct { struct nfacct_rule *counter; int64_t bytes; - gboolean data_limit_reached; + gboolean data_limit_exceeded; } classid_bytes_context_s; static stc_system_s *g_system = NULL; @@ -478,16 +488,31 @@ 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 [%u], ifname [%s], " "iftype [%d], rstn_state [%d], rstn_type [%d], " - "limit [ (%lld) bytes], " + "month_start_date [%d], limit [ (%lld) bytes], " "warn_limit [ (%lld) bytes], " - "counter [ (%lld) bytes], " + "monthly_limit [ (%lld) bytes], " + "weekly_limit [ (%lld) bytes], " + "daily_limit [ (%lld) bytes], " + "data_counter [ (%lld) bytes], " + "warn_counter [ (%lld) bytes], " + "monthly_counter [ (%lld) bytes], " + "weekly_counter [ (%lld) bytes], " + "daily_counter [ (%lld) bytes], " "roaming [%d], subscriber_id [%s]", rstn_value->restriction_id, rstn_key->app_id, rstn_value->classid , rstn_key->ifname, rstn_key->iftype, rstn_value->rstn_state, rstn_value->rstn_type, - rstn_value->data_limit, - rstn_value->data_warn_limit, - rstn_value->data_counter, + rstn_value->month_start_date, + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA], + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN], + rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY], + rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY], + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY], + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA], + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN], + rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY], + rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY], + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY], rstn_key->roaming, rstn_key->subscriber_id); } @@ -583,11 +608,36 @@ static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend, __del_ip6tables_out(&counter); } +typedef struct { + time_t month_start_ts; + time_t week_start_ts; + time_t day_start_ts; + int64_t monthly_stat; + int64_t weekly_stat; + int64_t daily_stat; +} cumulative_data_s; + +static stc_cb_ret_e __statistics_info_cb(const table_statistics_info *info, + void *user_data) +{ + cumulative_data_s *stat = (cumulative_data_s *)user_data; + int64_t counters = 0; + + counters = info->cnt.in_bytes + info->cnt.out_bytes; + + stat->monthly_stat += counters; + if (stat->week_start_ts <= info->interval->from) + stat->weekly_stat += counters; + if (stat->day_start_ts <= info->interval->from) + stat->daily_stat += counters; + + return STC_CONTINUE; +} + static void __process_restriction(enum traffic_restriction_type rstn_type, stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value, void *data) { - int64_t effective_data_limit, effective_data_warn_limit; default_connection_s *old_connection = (default_connection_s *)data; default_connection_s *connection = NULL; @@ -610,52 +660,85 @@ static void __process_restriction(enum traffic_restriction_type rstn_type, if (rstn_value->classid <= STC_UNKNOWN_CLASSID) return; - effective_data_limit = rstn_value->data_limit; - effective_data_warn_limit = rstn_value->data_warn_limit; - - if (rstn_type == RST_SET) { - /* TODO: Change this to runtime memory */ + switch (rstn_type) { + case RST_SET: + { + int i; table_counters_info info; + int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, }; memset(&info, 0, sizeof(table_counters_info)); - table_counters_get(rstn_value->restriction_id, &info); - - effective_data_limit -= info.data_counter; - effective_data_warn_limit -= info.data_counter; - - if (effective_data_limit < 0) { - effective_data_limit = 0; //LCOV_EXCL_LINE - rstn_value->data_limit_reached = TRUE; //LCOV_EXCL_LINE + rstn_value->limit_exceeded = 0; + + if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) { + table_counters_get(rstn_value->restriction_id, &info); + + time_t current_time = 0; + cumulative_data_s stat; + table_statistics_select_rule rule; + + memset(&stat, 0, sizeof(cumulative_data_s)); + stat.month_start_ts = rstn_value->month_start_ts; + stat.week_start_ts = g_system->last_week_ts; + stat.week_start_ts = g_system->last_day_ts; + + memset(&rule, 0, sizeof(table_statistics_select_rule)); + rule.from = rstn_value->month_start_ts; + time(¤t_time); + rule.to = current_time; + rule.iftype = rstn_key->iftype; + rule.granularity = GRANULARITY; + + table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat); + + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat; } - if (effective_data_warn_limit < 0) - effective_data_warn_limit = 0; //LCOV_EXCL_LINE + for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) { + if (rstn_value->limit[i] >= 0) { + effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i]; - STC_LOGD("datausage [%lld] bytes", info.data_counter); - } + if (effective_limit[i] < 0) + rstn_value->limit_exceeded |= (1 << i); + } + } - STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, " - "effective_data_warn_limit [%lld] bytes", - rstn_value->restriction_id, effective_data_limit, - effective_data_warn_limit); + STC_LOGD("rstn_id [%llu], datausage [%llu] bytes", + rstn_value->restriction_id, info.data_counter); - switch (rstn_type) { - case RST_SET: - if (effective_data_limit <= 0) - __add_iptables_rule(rstn_value->classid, NFACCT_BLOCK, - rstn_key->iftype); + if (rstn_value->limit_exceeded != 0 && + rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) { + __add_iptables_rule(rstn_value->classid, NFACCT_BLOCK, rstn_key->iftype); + } rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED; - rstn_value->data_limit_reached = FALSE; - break; + } + break; case RST_EXCLUDE: __add_iptables_rule(rstn_value->classid, NFACCT_ALLOW, rstn_key->iftype); rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED; - rstn_value->data_limit_reached = TRUE; + rstn_value->limit_exceeded = 0; + rstn_value->limit_notified = 0; break; case RST_UNSET: + { + int i; + if (rstn_value->classid == STC_TETHERING_APP_CLASSID) __del_iptables_rule(rstn_value->classid, NFACCT_BLOCK, rstn_key->iftype); @@ -664,8 +747,14 @@ static void __process_restriction(enum traffic_restriction_type rstn_type, rstn_key->iftype); rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED; - rstn_value->data_limit_reached = FALSE; - break; + rstn_value->limit_exceeded = 0; + rstn_value->limit_notified = 0; + + for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) + if (rstn_value->limit[i] >= 0) + rstn_value->counter[i] = 0; + } + break; default: ;//Do Nothing } @@ -796,79 +885,121 @@ static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system) return STC_ERROR_NONE; } +static void __action_when_rstn_limit_exceeded(stc_rstn_limit_type_e limit_type, + stc_rstn_key_s *rstn_key, + stc_rstn_value_s *rstn_value, + classid_bytes_context_s *context) +{ + gboolean rv; + char iftype[MAX_INT_LENGTH]; + char byte[MAX_INT_LENGTH]; + const char *signal_name; + stc_s *stc = (stc_s *)stc_get_manager(); + + if (stc == NULL) { + STC_LOGE("Failed to get stc data"); + return; + } + + switch (limit_type) { + case STC_RSTN_LIMIT_TYPE_DATA_WARN: + { + signal_name = "WarnThresholdCrossed"; + + snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); + snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit[limit_type]); + stc_plugin_appstatus_send_warn_message("warn threshold crossed", + "warning_noti", + rstn_key->app_id, + iftype, byte); + } + break; + case STC_RSTN_LIMIT_TYPE_DATA: + case STC_RSTN_LIMIT_TYPE_MONTHLY: + case STC_RSTN_LIMIT_TYPE_WEEKLY: + case STC_RSTN_LIMIT_TYPE_DAILY: + { + signal_name = "RestrictionThresholdCrossed"; + + /* block immediately */ + context->counter->intend = NFACCT_BLOCK; + __del_iptables_in(context->counter); + __del_iptables_out(context->counter); + __add_iptables_in(context->counter); + __add_iptables_out(context->counter); + + __del_ip6tables_in(context->counter); + __del_ip6tables_out(context->counter); + __add_ip6tables_in(context->counter); + __add_ip6tables_out(context->counter); + context->counter->intend = NFACCT_COUNTER; + + rstn_value->limit_exceeded |= (1 << limit_type); + + snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); + snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit[limit_type]); + stc_plugin_appstatus_send_restriction_message("restriction threshold crossed", + "restriction_noti", + rstn_key->app_id, + iftype, byte); + } + break; + default: + break; + } + + /* emit signal */ + rv = stc_manager_dbus_emit_signal(stc->connection, + STC_DBUS_SERVICE_RESTRICTION_PATH, + STC_DBUS_INTERFACE_RESTRICTION, + signal_name, + g_variant_new("(si)", + rstn_key->app_id, + rstn_key->iftype)); + + if (rv == TRUE) + rstn_value->limit_notified |= (1 << limit_type); +} + static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value, classid_bytes_context_s *context) { + int i; switch (context->counter->iotype) { case NFACCT_COUNTER_IN: case NFACCT_COUNTER_OUT: - rstn_value->data_counter += context->bytes; - - if (rstn_value->data_counter >= rstn_value->data_warn_limit && - rstn_value->warn_limit_crossed_notified == FALSE) { - - gboolean rv; - char iftype[MAX_INT_LENGTH]; - char byte[MAX_INT_LENGTH]; - stc_s *stc = (stc_s *)stc_get_manager(); - ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data"); - - /* emit signal */ - rv = stc_manager_dbus_emit_signal(stc->connection, - STC_DBUS_SERVICE_RESTRICTION_PATH, - STC_DBUS_INTERFACE_RESTRICTION, - "WarnThresholdCrossed", - g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype)); - if (rv == TRUE) - rstn_value->warn_limit_crossed_notified = TRUE; - - snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); - snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit); - stc_plugin_appstatus_send_warn_message("warn threshold crossed", - "warning_noti", - rstn_key->app_id, - iftype, byte); + if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) || + (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 && + rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) { + table_counters_info info; + memset(&info, 0, sizeof(table_counters_info)); + table_counters_get(rstn_value->restriction_id, &info); + + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter; + rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter; } - if (rstn_value->data_counter >= rstn_value->data_limit && - rstn_value->rstn_limit_crossed_notified == FALSE) { - - gboolean rv; - char iftype[MAX_INT_LENGTH]; - char byte[MAX_INT_LENGTH]; - stc_s *stc = (stc_s *)stc_get_manager(); - ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data"); - - /* block immediately */ - context->counter->intend = NFACCT_BLOCK; - __del_iptables_in(context->counter); - __del_iptables_out(context->counter); - __add_iptables_in(context->counter); - __add_iptables_out(context->counter); - - __del_ip6tables_in(context->counter); - __del_ip6tables_out(context->counter); - __add_ip6tables_in(context->counter); - __add_ip6tables_out(context->counter); - context->counter->intend = NFACCT_COUNTER; - - rstn_value->data_limit_reached = TRUE; - - /* emit signal */ - rv = stc_manager_dbus_emit_signal(stc->connection, - STC_DBUS_SERVICE_RESTRICTION_PATH, - STC_DBUS_INTERFACE_RESTRICTION, - "RestrictionThresholdCrossed", - g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype)); - if (rv == TRUE) - rstn_value->rstn_limit_crossed_notified = TRUE; - - snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); - snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit); - stc_plugin_appstatus_send_restriction_message("restriction threshold crossed", - "restriction_noti", rstn_key->app_id, - iftype, byte); + for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) { + if (rstn_value->limit[i] >= 0 && + !(rstn_value->limit_notified & (1 << i))) { + rstn_value->counter[i] += context->bytes; + if (rstn_value->limit[i] <= rstn_value->counter[i]) + __action_when_rstn_limit_exceeded(i, + rstn_key, + rstn_value, + context); + } } g_system->rstns_tree_updated = TRUE; @@ -919,8 +1050,8 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, if (context->counter->intend != NFACCT_COUNTER) goto try_next_callback; - if (rstn_value->data_limit_reached == TRUE) { - context->data_limit_reached = TRUE; //LCOV_EXCL_LINE + if (rstn_value->limit_exceeded == TRUE) { + context->data_limit_exceeded = TRUE; //LCOV_EXCL_LINE goto try_next_callback; //LCOV_EXCL_LINE } @@ -1014,7 +1145,10 @@ static gboolean __update_app_statistics(gpointer key, gpointer value, static gboolean __flush_apps_stats_to_database(gpointer user_data) { time_t current_time = 0; - time(¤t_time); + stc_s *stc = stc_get_manager(); + + if (stc && stc->carg) + current_time = stc->carg->last_run_time; if (g_system->apps_tree_updated == FALSE) return G_SOURCE_REMOVE; @@ -1037,7 +1171,11 @@ static gboolean __update_counter_statistics(gpointer key, gpointer value, stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value; table_counters_info info = { .restriction_id = rstn_value->restriction_id, - .data_counter = rstn_value->data_counter + .data_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA], + .warn_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN], + .monthly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY], + .weekly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY], + .daily_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] }; table_counters_update_counters(&info); @@ -1047,7 +1185,11 @@ static gboolean __update_counter_statistics(gpointer key, gpointer value, static gboolean __flush_rstns_counter_to_database(gpointer user_data) { - time_t current_time = time(0); + time_t current_time = 0; + stc_s *stc = stc_get_manager(); + + if (stc && stc->carg) + current_time = stc->carg->last_run_time; if (g_system->rstns_tree_updated == FALSE) return G_SOURCE_REMOVE; @@ -1136,9 +1278,146 @@ try_next_callback: return FALSE; } +static gboolean __reset_time_counter_foreach_rstn(gpointer key, + gpointer value, + gpointer data) +{ + stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key; + stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value; + reset_time_limits_context_s *context = (reset_time_limits_context_s *)data; + int i; + time_t now_month_start_ts; + + if (rstn_value->month_start_date == 0) { + table_counters_info info; + memset(&info, 0, sizeof(table_counters_info)); + table_counters_get_timestamps(rstn_value->restriction_id, &info); + + if (info.month_start_date == 0) + rstn_value->month_start_date = 1; + else + rstn_value->month_start_date = info.month_start_date; + rstn_value->month_start_ts = info.month_start_ts; + } + + now_month_start_ts = + stc_time_get_month_start(context->now, + rstn_value->month_start_date); + + if (rstn_value->month_start_ts != now_month_start_ts) { + rstn_value->month_start_ts = now_month_start_ts; + context->month_start_ts = now_month_start_ts; + context->is_updated |= (1 << STC_RSTN_LIMIT_TYPE_MONTHLY); + } + + if (context->is_updated) { + table_counters_info info; + memset(&info, 0, sizeof(table_counters_info)); + + info.restriction_id = rstn_value->restriction_id; + info.month_start_date = rstn_value->month_start_date; + info.month_start_ts = rstn_value->month_start_ts; + info.week_start_ts = context->week_start_ts; + info.day_start_ts = context->day_start_ts; + + table_counters_update_timestamps(&info); + } + + for (i = STC_RSTN_LIMIT_TYPE_MONTHLY; i < STC_RSTN_LIMIT_TYPE_MAX; i++) { + + if ((context->is_updated) & (1 << i)) { + /* reset limit */ + rstn_value->counter[i] = 0; + + if (rstn_value->limit_exceeded & (1 << i)) { + /* remove iptables rule */ + char *default_ifname = stc_default_connection_get_ifname(); + struct nfacct_rule counter; + stc_s *stc = stc_get_manager(); + if (stc == NULL) { + STC_LOGE("Can't get stc data"); + g_free(default_ifname); + goto try_next_callback; + } + + if (!stc->carg) { + stc->carg = MALLOC0(counter_arg_s, 1); + if (stc->carg == NULL) { + g_free(default_ifname); + goto try_next_callback; + } + + stc->carg->sock = + stc_monitor_get_counter_socket(); + } + + counter.carg = stc->carg; + counter.classid = rstn_value->classid; + counter.intend = NFACCT_BLOCK; + counter.iftype = rstn_key->iftype; + g_strlcpy(counter.ifname, default_ifname, + MAX_IFACE_LENGTH); + + g_free(default_ifname); + + /* iptables rule */ + __del_iptables_in(&counter); + __del_iptables_out(&counter); + + /* ip6tables rule */ + __del_ip6tables_in(&counter); + __del_ip6tables_out(&counter); + + rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED; + rstn_value->limit_exceeded &= ~(1 << i); + rstn_value->limit_notified &= ~(1 << i); + } + } + } + +try_next_callback: + return FALSE; +} + +static void __reset_time_counters_if_required(void) +{ + reset_time_limits_context_s context; + + if (g_system == NULL) { + STC_LOGE("stc monitor not initialized!"); + return; + } + + context.now = time(NULL); + context.week_start_ts = stc_time_get_week_start(context.now); + context.day_start_ts = stc_time_get_day_start(context.now); + context.is_updated = 0; + + if (g_system->last_week_ts != context.week_start_ts) { + g_system->last_week_ts = context.week_start_ts; + context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_WEEKLY); + } + + if (g_system->last_day_ts != context.day_start_ts) { + g_system->last_day_ts = context.day_start_ts; + context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_DAILY); + } + + if (g_system->rstns) { + g_tree_foreach(g_system->rstns, + __reset_time_counter_foreach_rstn, + &context); + if (context.is_updated) + STC_LOGD("Counter reset completed month_start [%ld], week_start [%ld], day_start [%ld]", + context.month_start_ts, g_system->last_week_ts, g_system->last_day_ts); + } +} + static void __fill_nfacct_result(char *cnt_name, int64_t bytes, struct counter_arg *carg) { + __reset_time_counters_if_required(); + struct nfacct_rule counter = { .carg = carg, .name = {0}, @@ -1149,7 +1428,7 @@ static void __fill_nfacct_result(char *cnt_name, int64_t bytes, classid_bytes_context_s context = { .counter = &counter, .bytes = bytes, - .data_limit_reached = FALSE, + .data_limit_exceeded = FALSE, }; if (!recreate_counter_by_name(cnt_name, &counter)) { @@ -1272,6 +1551,8 @@ static gboolean __process_contr_reply(GIOChannel *source, goto out; stc->carg->ans_len = ret; + stc->carg->last_run_time = time(NULL); + __process_network_counter(ans, stc->carg); g_idle_add(__flush_apps_stats_to_database, NULL); @@ -1400,10 +1681,9 @@ static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key) return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE } - __remove_restriction(key, lookup_value, NULL); - /* remove counter also */ table_counters_delete(lookup_value->restriction_id); + __remove_restriction(key, lookup_value, NULL); if (!g_tree_remove(g_system->rstns, key)) { STC_LOGD("key not found"); //LCOV_EXCL_LINE @@ -1450,11 +1730,17 @@ static stc_error_e __rstn_tree_add(stc_rstn_key_s *key, rstn_value->rstn_state = value->rstn_state; rstn_value->rstn_type = value->rstn_type; rstn_value->classid = value->classid; - rstn_value->data_limit = value->data_limit; - rstn_value->data_warn_limit = value->data_warn_limit; - rstn_value->data_counter = 0; - rstn_value->warn_limit_crossed_notified = FALSE; - rstn_value->rstn_limit_crossed_notified = FALSE; + + int i; + for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) { + rstn_value->limit[i] = value->limit[i]; + rstn_value->counter[i] = 0; + } + + rstn_value->limit_exceeded = 0; + rstn_value->limit_notified = 0; + rstn_value->month_start_date = value->month_start_date; + rstn_value->month_start_ts = value->month_start_ts; if (debug == TRUE) __add_restriction_debug(key, rstn_value, NULL); @@ -1491,8 +1777,11 @@ static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info, else value.classid = STC_UNKNOWN_CLASSID; - value.data_limit = info->data_limit; - value.data_warn_limit = info->data_warn_limit; + value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit; + value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit; + value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit; + value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit; + value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit; if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE) ret = STC_CANCEL; @@ -1992,8 +2281,14 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info) __process_update_background(); //LCOV_EXCL_LINE } - value.data_limit = info->data_limit; - value.data_warn_limit = info->data_warn_limit; + value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit; + value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit; + value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit; + value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit; + value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit; + value.month_start_date = info->month_start_date; + value.month_start_ts = stc_time_get_month_start(time(NULL), + info->month_start_date); ret = __rstn_tree_add(&key, &value, TRUE); diff --git a/src/monitor/stc-time.c b/src/monitor/stc-time.c new file mode 100644 index 0000000..750b43d --- /dev/null +++ b/src/monitor/stc-time.c @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#define SEC_IN_DAY 24 * 60 * 60 +#define SEC_IN_WEEK 7 * SEC_IN_DAY + +time_t stc_time_get_day_start(time_t now) +{ + struct tm *curr; + + curr = localtime(&now); + + curr->tm_sec = 0; + curr->tm_min = 0; + curr->tm_hour = 0; + + return mktime(curr); +} + +time_t stc_time_get_week_start(time_t now) +{ + struct tm *curr; + int days; + + curr = localtime(&now); + + curr->tm_sec = 0; + curr->tm_min = 0; + curr->tm_hour = 0; + + if (curr->tm_wday > 1) + days = curr->tm_wday - 1; + else + days = 1 - curr->tm_wday; + + return (mktime(curr) - (days * SEC_IN_DAY)); +} + +time_t stc_time_get_month_start(time_t now, int month_start_date) +{ + struct tm *curr; + bool is_leap_year; + + curr = localtime(&now); + + curr->tm_sec = 0; + curr->tm_min = 0; + curr->tm_hour = 0; + + if (curr->tm_mday < month_start_date) { + curr->tm_mon--; + if (curr->tm_mon < 0) { + curr->tm_mon = 11; + curr->tm_year--; + } + } + + is_leap_year = ((curr->tm_year + 1900) % 4 ? 0 : 1); + curr->tm_mday = month_start_date; + + switch (month_start_date) { + case 29: + case 30: + if (curr->tm_mon == 1 && !is_leap_year) + curr->tm_mday = 28; + + else if (curr->tm_mon == 1 && is_leap_year) + curr->tm_mday = 29; + + break; + case 31: + if (curr->tm_mon == 1 && !is_leap_year) + curr->tm_mday = 28; + + else if (curr->tm_mon == 1 && is_leap_year) + curr->tm_mday = 29; + + else if (curr->tm_mon == 3 || curr->tm_mon == 5 || + curr->tm_mon == 8 || curr->tm_mon == 10) + curr->tm_mday = 30; + + break; + default: + ;//Do Nothing + }; + + return mktime(curr); +} diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index ddad1ef..672909b 100644 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -457,7 +457,7 @@ void stc_manager_gdbus_dict_foreach(GVariantIter *iter, dbus_dict_cb cb, } while (g_variant_iter_loop(iter, "{sv}", &key, &value)) { - DEBUG_GDBUS_KEY_VALUE(key, value); + /* DEBUG_GDBUS_KEY_VALUE(key, value); */ if (key && cb) cb(key, value, user_data); } diff --git a/src/stc-restriction.c b/src/stc-restriction.c index d4353ea..2f44459 100644 --- a/src/stc-restriction.c +++ b/src/stc-restriction.c @@ -46,8 +46,11 @@ void __initialize_rstn_rule(table_restrictions_info *rule) rule->ifname = NULL; rule->iftype = STC_IFACE_ALL; rule->rstn_type = STC_RSTN_TYPE_UNKNOWN; - rule->data_limit = 0; - rule->data_warn_limit = 0; + rule->data_limit = -1; + rule->data_warn_limit = -1; + rule->monthly_limit = -1; + rule->weekly_limit = -1; + rule->daily_limit = -1; rule->roaming = STC_ROAMING_DISABLE; rule->subscriber_id = NULL; } @@ -114,6 +117,18 @@ void __stc_restriction_app_info_builder_add(GVariantBuilder *builder, g_variant_builder_add(builder, "{sv}", "data_warn_limit", g_variant_new_int64(info->data_warn_limit)); + g_variant_builder_add(builder, "{sv}", "month_start_date", + g_variant_new_int32(info->month_start_date)); + + g_variant_builder_add(builder, "{sv}", "monthly_limit", + g_variant_new_int64(info->monthly_limit)); + + g_variant_builder_add(builder, "{sv}", "weekly_limit", + g_variant_new_int64(info->weekly_limit)); + + g_variant_builder_add(builder, "{sv}", "daily_limit", + g_variant_new_int64(info->daily_limit)); + g_variant_builder_add(builder, "{sv}", "roaming", g_variant_new_uint16(info->roaming)); @@ -165,8 +180,6 @@ stc_cb_ret_e __table_restrictions_per_app_cb(const table_restrictions_info *info static void __stc_extract_restriction_rule(const char *key, GVariant *value, void *user_data) { - __STC_LOG_FUNC_ENTER__; - table_restrictions_info *rule = (table_restrictions_info *) user_data; if (rule == NULL) { @@ -198,6 +211,22 @@ static void __stc_extract_restriction_rule(const char *key, GVariant *value, rule->data_warn_limit = g_variant_get_int64(value); STC_LOGD("data_warn_limit: [%lld]", rule->data_warn_limit); + } else if (!g_strcmp0(key, "month_start_date")) { + rule->month_start_date = g_variant_get_int32(value); + STC_LOGD("month_start_date: [%d]", rule->month_start_date); + + } else if (!g_strcmp0(key, "monthly_limit")) { + rule->monthly_limit = g_variant_get_int64(value); + STC_LOGD("monthly_limit: [%lld]", rule->monthly_limit); + + } else if (!g_strcmp0(key, "weekly_limit")) { + rule->weekly_limit = g_variant_get_int64(value); + STC_LOGD("weekly_limit: [%lld]", rule->weekly_limit); + + } else if (!g_strcmp0(key, "daily_limit")) { + rule->daily_limit = g_variant_get_int64(value); + STC_LOGD("daily_limit: [%lld]", rule->daily_limit); + } else if (!g_strcmp0(key, "roaming")) { rule->roaming = g_variant_get_uint16(value); STC_LOGD("roaming: [%u]", rule->roaming); @@ -208,15 +237,13 @@ static void __stc_extract_restriction_rule(const char *key, GVariant *value, rule->subscriber_id = g_strdup(str); STC_LOGD("subscriber_id: [%s]", rule->subscriber_id); - } else if (!g_strcmp0(key, "type")) { + } else if (!g_strcmp0(key, "rstn_type")) { rule->rstn_type = g_variant_get_uint16(value); STC_LOGD("type: [%u]", (unsigned int) rule->rstn_type); } else { STC_LOGD("Unknown select rule"); //LCOV_EXCL_LINE } - - __STC_LOG_FUNC_EXIT__; } gboolean handle_restriction_set(StcRestriction *object, -- 2.7.4