From ff13ab088da23745b48825037e92fae637f03427 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 12 Jul 2017 13:53:50 +0900 Subject: [PATCH 01/16] Added background restriction for data saver Change-Id: I8bb5ae5c54affe6cf1c6bd35e4952b45cac04173 Signed-off-by: hyunuktak --- include/stc-manager.h | 1 + include/stc-restriction.h | 2 +- interfaces/stcmanager-iface-restriction.xml | 2 +- src/helper/helper-cgroup.c | 5 ++ src/helper/helper-cgroup.h | 8 +- src/helper/helper-net-cls.c | 7 +- src/monitor/include/stc-monitor.h | 1 + src/monitor/stc-application-lifecycle.c | 4 +- src/monitor/stc-monitor.c | 110 +++++++++++++++++++++++++--- src/stc-manager-gdbus.c | 4 +- src/stc-restriction.c | 2 +- 11 files changed, 126 insertions(+), 20 deletions(-) diff --git a/include/stc-manager.h b/include/stc-manager.h index ac8ecb4..c82c3af 100755 --- a/include/stc-manager.h +++ b/include/stc-manager.h @@ -24,6 +24,7 @@ #define NET_CLS_SUBSYS "net_cls" #define STC_BACKGROUND_APP_SUFFIX "_BACKGROUND" +#define STC_BACKGROUND_APP_ID "BACKGROUND" #define STC_TOTAL_DATACALL "TOTAL_DATACALL" #define STC_TOTAL_WIFI "TOTAL_WIFI" #define STC_TOTAL_BLUETOOTH "TOTAL_BLUETOOTH" diff --git a/include/stc-restriction.h b/include/stc-restriction.h index 219dad8..acd4eab 100755 --- a/include/stc-restriction.h +++ b/include/stc-restriction.h @@ -67,7 +67,7 @@ gboolean handle_restriction_get_state(StcRestriction *object, int iftype, void *user_data); -gboolean handle_restriction_remove(StcRestriction *object, +gboolean handle_restriction_unset(StcRestriction *object, GDBusMethodInvocation *invocation, GVariant *parameters, void *user_data); diff --git a/interfaces/stcmanager-iface-restriction.xml b/interfaces/stcmanager-iface-restriction.xml index 6ccc381..f4baf76 100644 --- a/interfaces/stcmanager-iface-restriction.xml +++ b/interfaces/stcmanager-iface-restriction.xml @@ -28,7 +28,7 @@ - + diff --git a/src/helper/helper-cgroup.c b/src/helper/helper-cgroup.c index a725dcc..af9f1f1 100755 --- a/src/helper/helper-cgroup.c +++ b/src/helper/helper-cgroup.c @@ -20,6 +20,8 @@ #define NOTIFY_ON_RELEASE "notify_on_release" #define CGROUP_FILE_NAME "cgroup.procs" +#define CLASSID_FILE_NAME "net_cls.classid" + #define MAX_PATH_LENGTH 512 static int read_uint(FILE *handler, uint32_t *out) @@ -283,6 +285,9 @@ void cgroup_init(void) cgroup_make_subdir(STC_CGROUP_NETWORK, STC_BACKGROUND_CGROUP_NAME, NULL); + cgroup_write_node_uint32(BACKGROUND_CGROUP_NETWORK, + CLASSID_FILE_NAME, STC_BACKGROUND_APP_CLASSID); + /* create foreground cgroup directory */ cgroup_make_subdir(STC_CGROUP_NETWORK, STC_FOREGROUND_CGROUP_NAME, NULL); diff --git a/src/helper/helper-cgroup.h b/src/helper/helper-cgroup.h index a358066..273036c 100755 --- a/src/helper/helper-cgroup.h +++ b/src/helper/helper-cgroup.h @@ -31,12 +31,12 @@ #define DEFAULT_CGROUP "/sys/fs/cgroup" #define CGROUP_NETWORK DEFAULT_CGROUP "/net_cls" #define STC_CGROUP_NETWORK CGROUP_NETWORK "/stc" -#define BACKGROUND_CGROUP_NETWORK STC_CGROUP_NETWORK "/background" -#define FOREGROUND_CGROUP_NETWORK STC_CGROUP_NETWORK "/foreground" +#define BACKGROUND_CGROUP_NETWORK STC_CGROUP_NETWORK "/BACKGROUND" +#define FOREGROUND_CGROUP_NETWORK STC_CGROUP_NETWORK "/FOREGROUND" #define PROC_TASK_CHILDREN "/proc/%d/task/%d/children" #define STC_CGROUP_NAME "stc" -#define STC_BACKGROUND_CGROUP_NAME "background" -#define STC_FOREGROUND_CGROUP_NAME "foreground" +#define STC_BACKGROUND_CGROUP_NAME "BACKGROUND" +#define STC_FOREGROUND_CGROUP_NAME "FOREGROUND" /** * @desc Get one unsigned int32 value from cgroup diff --git a/src/helper/helper-net-cls.c b/src/helper/helper-net-cls.c index 5f653fc..1c22608 100755 --- a/src/helper/helper-net-cls.c +++ b/src/helper/helper-net-cls.c @@ -94,6 +94,9 @@ uint32_t get_classid_by_app_id(const char *app_id, int create) return STC_UNKNOWN_CLASSID; } + if (!strcmp(app_id, STC_BACKGROUND_APP_ID)) + return STC_BACKGROUND_APP_CLASSID; + if (!strcmp(app_id, STC_TOTAL_DATACALL)) return STC_TOTAL_DATACALL_CLASSID; @@ -150,7 +153,9 @@ stc_error_e place_pids_to_net_cgroup(const int pid, const char *app_id) return STC_ERROR_INVALID_PARAMETER; } - if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX)) + if (!strcmp(app_id, STC_BACKGROUND_APP_ID)) + path_to_net_cgroup_dir = STC_CGROUP_NETWORK; + else if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX)) path_to_net_cgroup_dir = BACKGROUND_CGROUP_NETWORK; else path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK; diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index 09871d5..56d87e2 100755 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -100,6 +100,7 @@ typedef struct { gboolean rstns_tree_updated; GTree *apps; /**< monitored applications */ gboolean apps_tree_updated; + guint background_state; } stc_system_s; /** diff --git a/src/monitor/stc-application-lifecycle.c b/src/monitor/stc-application-lifecycle.c index a0fb931..ee82320 100755 --- a/src/monitor/stc-application-lifecycle.c +++ b/src/monitor/stc-application-lifecycle.c @@ -91,6 +91,7 @@ static stc_error_e __stc_manager_app_status_changed(stc_cmd_type_e cmd, stc_monitor_process_add(app_key, proc_key, proc_value); stc_monitor_process_update_ground(app_key, proc_key, STC_APP_STATE_FOREGROUND); + FREE(app_key.pkg_id); FREE(app_key.app_id); break; @@ -108,7 +109,8 @@ static stc_error_e __stc_manager_app_status_changed(stc_cmd_type_e cmd, memset(&proc_value, 0, sizeof(stc_process_value_s)); app_key.pkg_id = g_strdup(pkg_id); - app_key.app_id = g_strdup(app_id); + app_key.app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, + NULL); app_value.type = app_type; app_value.processes = NULL; diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index a65f6d2..2bb1777 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -15,6 +15,8 @@ */ #include +#include +#include #include "stc-default-connection.h" #include "helper-nl.h" @@ -28,6 +30,7 @@ #include "stc-manager-plugin.h" #define MAX_INT_LENGTH 128 +#define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state" typedef struct { stc_app_key_s *app_key; @@ -1259,7 +1262,7 @@ static void __add_rstns_for_application(gchar *app_id) app_id); } -static void __stc_monitor_add_application_by_interface(const char *app_id) +static void __add_application_by_interface(const char *app_id) { stc_app_key_s app_key; stc_app_value_s app_value; @@ -1284,6 +1287,78 @@ static void __stc_monitor_add_application_by_interface(const char *app_id) FREE(app_key.app_id); } +static int __vconf_get_int(const char *key, int *value) +{ + int ret = 0; + + ret = vconf_get_int(key, value); + if (ret != VCONF_OK) { + STC_LOGE("Failed to get vconfkey [%s] value", key); + return -1; + } + + return 0; +} + +static int __vconf_set_int(const char *key, int value) +{ + int ret = 0; + + ret = vconf_set_int(key, value); + if (ret != VCONF_OK) { + STC_LOGE("Failed to set vconfkey [%s] value", key); + return -1; + } + + return 0; +} + +static guint __get_background_state(void) +{ + return g_system->background_state;; +} + +static void __set_background_state(guint state) +{ + g_system->background_state = state; +} + +static gboolean __processes_tree_foreach_background(gpointer key, gpointer value, + gpointer data) +{ + stc_process_key_s *proc_key = (stc_process_key_s *)key; + stc_process_value_s *proc_value = (stc_process_value_s *)value; + stc_app_key_s *app_key = (stc_app_key_s *)data; + + if (g_system->background_state) + place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID); + else + place_pids_to_net_cgroup(proc_key->pid, app_key->app_id); + + return FALSE; +} + +static gboolean __apps_tree_foreach_background(gpointer key, gpointer value, + gpointer data) +{ + stc_app_key_s *app_key = (stc_app_key_s *)key; + stc_app_value_s *app_value = (stc_app_value_s *)value; + + if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX)) + g_tree_foreach(app_value->processes, __processes_tree_foreach_background, app_key); + + return FALSE; +} + +static stc_error_e __process_update_background(void) +{ + ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!"); + + g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL); + + return STC_ERROR_NONE; +} + stc_error_e stc_monitor_init(void) { stc_system_s *system = MALLOC0(stc_system_s, 1); @@ -1320,9 +1395,9 @@ stc_error_e stc_monitor_init(void) g_system = system; - __stc_monitor_add_application_by_interface(STC_TOTAL_DATACALL); - __stc_monitor_add_application_by_interface(STC_TOTAL_WIFI); - __stc_monitor_add_application_by_interface(STC_TOTAL_BLUETOOTH); + __add_application_by_interface(STC_TOTAL_DATACALL); + __add_application_by_interface(STC_TOTAL_WIFI); + __add_application_by_interface(STC_TOTAL_BLUETOOTH); /* creating restriction rules tree */ __update_contr_cb(NULL); @@ -1337,6 +1412,8 @@ stc_error_e stc_monitor_init(void) return STC_ERROR_FAIL; } + __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE, &g_system->background_state); + __fill_restritions_list(); return STC_ERROR_NONE; @@ -1519,11 +1596,14 @@ stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key, proc_lookup->ground = ground; if (ground == STC_APP_STATE_BACKGROUND) { - char *background_app_id = g_strconcat(app_key.app_id, - STC_BACKGROUND_APP_SUFFIX, - NULL); - place_pids_to_net_cgroup(proc_key.pid, background_app_id); - g_free(background_app_id); + if (__get_background_state()) { + place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID); + } else { + char *background_app_id = g_strconcat(app_key.app_id, + STC_BACKGROUND_APP_SUFFIX, NULL); + place_pids_to_net_cgroup(proc_key.pid, background_app_id); + g_free(background_app_id); + } } else { place_pids_to_net_cgroup(proc_key.pid, app_key.app_id); } @@ -1594,6 +1674,12 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info) else value.classid = STC_UNKNOWN_CLASSID; + if (value.classid == STC_BACKGROUND_APP_CLASSID) { + __set_background_state(TRUE); + __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); + __process_update_background(); + } + value.data_limit = info->data_limit; value.data_warn_limit = info->data_warn_limit; @@ -1617,6 +1703,12 @@ stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info) .roaming = info->roaming, }; + if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) { + __set_background_state(FALSE); + __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); + __process_update_background(); + } + ret = __rstn_tree_remove(&key); FREE(key.app_id); diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index 5b3cc71..3bd6659 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -113,8 +113,8 @@ static gboolean __stc_manager_gdbus_restriction_init(stc_s *stc) G_CALLBACK(handle_restriction_get_state), stc); - g_signal_connect(restriction, "handle-remove", - G_CALLBACK(handle_restriction_remove), stc); + g_signal_connect(restriction, "handle-unset", + G_CALLBACK(handle_restriction_unset), stc); /* Export the object (@manager takes its own reference to @object) */ g_dbus_object_manager_server_export(stc->obj_mgr, diff --git a/src/stc-restriction.c b/src/stc-restriction.c index aa8ed4e..8550e41 100755 --- a/src/stc-restriction.c +++ b/src/stc-restriction.c @@ -295,7 +295,7 @@ gboolean handle_restriction_exclude(StcRestriction *object, return TRUE; } -gboolean handle_restriction_remove(StcRestriction *object, +gboolean handle_restriction_unset(StcRestriction *object, GDBusMethodInvocation *invocation, GVariant *parameters, void *user_data) -- 2.7.4 From ba0534e9b40f4899e6acc0b76c1456ab6ed024ce Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 20 Jul 2017 10:42:45 +0530 Subject: [PATCH 02/16] Added default sim's IMSI fetch logic. Change-Id: I9040fb2bec433e828c4289e3ce7841a901989dc8 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/monitor/include/stc-default-connection.h | 17 ++- src/monitor/stc-default-connection.c | 150 ++++++++++++++++++++++++--- src/monitor/stc-monitor.c | 45 ++++++-- 4 files changed, 183 insertions(+), 31 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index e82aec1..4c005ca 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.23 +Version: 0.0.24 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/monitor/include/stc-default-connection.h b/src/monitor/include/stc-default-connection.h index ec5b2dc..861e58d 100755 --- a/src/monitor/include/stc-default-connection.h +++ b/src/monitor/include/stc-default-connection.h @@ -23,24 +23,33 @@ #include "stc-manager-gdbus.h" #include "stc-manager-util.h" +#define IMSI_LENGTH 16 + /** * @brief default connection information will be fetched from net-config */ typedef struct { - gchar *path; /* to identify each connection uniquely */ + /* to identify each connection uniquely */ + gchar *path; /* profile info */ stc_iface_type_e type; gchar *ifname; - gboolean roaming; /* cellular profile only else it is always false */ + + /* cellular profile only else it is always false */ + gboolean roaming; + + /* only present when default profile is cellular */ + char imsi[IMSI_LENGTH]; + + /* hardware network protocol type */ + stc_hw_net_protocol_type_e hw_net_protocol_type; } default_connection_s; stc_error_e stc_default_connection_monitor_init(stc_s *stc); stc_error_e stc_default_connection_monitor_deinit(stc_s *stc); -stc_iface_type_e stc_default_connection_get_type(void); gchar *stc_default_connection_get_ifname(void); -gboolean stc_default_connection_get_roaming(void); default_connection_s *stc_get_default_connection(void); #endif /* __STC_DEFAULT_CONNECTION_H__ */ diff --git a/src/monitor/stc-default-connection.c b/src/monitor/stc-default-connection.c index ad9b74f..8b32a3e 100755 --- a/src/monitor/stc-default-connection.c +++ b/src/monitor/stc-default-connection.c @@ -14,9 +14,12 @@ * limitations under the License. */ +#include + #include "stc-monitor.h" #include "stc-default-connection.h" +/* connman service dbus details */ #define CONNMAN_SERVICE "net.connman" #define CONNMAN_PATH "/net/connman" @@ -31,9 +34,132 @@ #define CONNMAN_SIGNAL_PROPERTY_CHANGED "PropertyChanged" +/* telephony service dbus details */ +#define TELEPHONY_SERVICE "org.tizen.telephony" +#define TELEPHONY_DEFAULT_PATH "/org/tizen/telephony" + +#define TELEPHONY_SERVICE_MANAGER TELEPHONY_SERVICE".Manager" +#define TELEPHONY_SIM_INTERFACE TELEPHONY_SERVICE".Sim" + +#define TELEPHONY_GET_MODEMS "GetModems" +#define TELEPHONY_GET_IMSI "GetIMSI" + +#define SIM_SLOT_SINGLE 1 + +#define VCONF_TELEPHONY_DEFAULT_DATA_SERVICE "db/telephony/dualsim/default_data_service" + default_connection_s g_default_connection; guint g_default_connection_sub_id = 0; +static int __telephony_get_current_sim(void) +{ + int sim_slot_count = 0; + int current_sim = 0; + + if (vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT_COUNT, &sim_slot_count) != 0) { + STC_LOGD("failed to get sim slot count"); + return -1; + } + + if (sim_slot_count == SIM_SLOT_SINGLE) { + STC_LOGD("It's single sim model"); + return current_sim; + } + + if (vconf_get_int(VCONF_TELEPHONY_DEFAULT_DATA_SERVICE, ¤t_sim) != 0) { + STC_LOGD("failed to get default data service = %d\n", + current_sim); + return -1; + } + + return current_sim; +} + +static void __telephony_get_modem_imsi(GDBusConnection *connection, + const char *default_modem_name) +{ + GVariant *message = NULL; + char tel_path[MAX_PATH_LENGTH]; + const char *plmn = NULL; + int plmn_len = 0; + const char *msin = NULL; + int msin_len = 0; + + snprintf(tel_path, sizeof(tel_path), "%s/%s", TELEPHONY_DEFAULT_PATH, + default_modem_name); + message = stc_manager_gdbus_call_sync(connection, + TELEPHONY_SERVICE, + tel_path, + TELEPHONY_SIM_INTERFACE, + TELEPHONY_GET_IMSI, + NULL); + if (message == NULL) { + STC_LOGE("Failed to get services informations"); + goto done; + } + + DEBUG_PARAMS(message); + DEBUG_PARAM_TYPE(message); + g_variant_get(message, "(&s&s)", &plmn, &msin); + plmn_len = strlen(plmn); + msin_len = strlen(msin); + + if (msin_len + plmn_len >= IMSI_LENGTH) { + STC_LOGD("Incorrect length of mobile subscriber identifier + net id"); + goto done; + } + + snprintf(g_default_connection.imsi, IMSI_LENGTH, "%s%s", plmn, msin); + +done: + g_variant_unref(message); + return; +} + +static void __telephony_update_default_modem_imsi(GDBusConnection *connection) +{ + GVariant *message = NULL; + GVariantIter *iter = NULL; + gchar *default_modem_name = NULL; + gchar *modem_name = NULL; + int current_sim = __telephony_get_current_sim(); + + if (current_sim < 0) { + STC_LOGI("Sim not found"); + return; + } + + message = stc_manager_gdbus_call_sync(connection, + TELEPHONY_SERVICE, + TELEPHONY_DEFAULT_PATH, + TELEPHONY_SERVICE_MANAGER, + TELEPHONY_GET_MODEMS, + NULL); + if (message == NULL) { + STC_LOGE("Failed to get services informations"); + return; + } + + g_variant_get(message, "(as)", &iter); + DEBUG_PARAMS(message); + DEBUG_PARAM_TYPE(message); + while (g_variant_iter_loop(iter, "s", &modem_name)) { + if (current_sim == 0) { + default_modem_name = g_strdup(modem_name); + FREE(modem_name); + break; + } + current_sim--; + } + + __telephony_get_modem_imsi(connection, default_modem_name); + + FREE(default_modem_name); + g_variant_iter_free(iter); + g_variant_unref(message); + return; +} + static void __print_default_connection_info(void) { STC_LOGI("============= default connection info ============"); @@ -41,6 +167,8 @@ static void __print_default_connection_info(void) STC_LOGI("type [%d]", g_default_connection.type); STC_LOGI("ifname [%s]", g_default_connection.ifname); STC_LOGI("roaming [%u]", g_default_connection.roaming ? TRUE : FALSE); + if (g_default_connection.type == STC_IFACE_DATACALL) + STC_LOGI("imsi [%s]", g_default_connection.imsi); STC_LOGI("=================================================="); } @@ -247,16 +375,18 @@ static stc_error_e __get_default_profile(GDBusConnection *connection) g_variant_iter_free(iter); g_variant_unref(message); - if (__is_cellular_profile(g_default_connection.path)) + if (__is_cellular_profile(g_default_connection.path)) { g_default_connection.type = STC_IFACE_DATACALL; - else if (__is_wifi_profile(g_default_connection.path)) + __telephony_update_default_modem_imsi(connection); + } else if (__is_wifi_profile(g_default_connection.path)) { g_default_connection.type = STC_IFACE_WIFI; - else if (__is_ethernet_profile(g_default_connection.path)) + } else if (__is_ethernet_profile(g_default_connection.path)) { g_default_connection.type = STC_IFACE_WIRED; - else if (__is_bluetooth_profile(g_default_connection.path)) + } else if (__is_bluetooth_profile(g_default_connection.path)) { g_default_connection.type = STC_IFACE_BLUETOOTH; - else + } else { g_default_connection.type = STC_IFACE_UNKNOWN; + } __get_default_connection_info(connection, g_default_connection.path); @@ -357,21 +487,11 @@ stc_error_e stc_default_connection_monitor_deinit(stc_s *stc) return STC_ERROR_NONE; } -stc_iface_type_e stc_default_connection_get_type(void) -{ - return g_default_connection.type; -} - gchar *stc_default_connection_get_ifname(void) { return g_strdup(g_default_connection.ifname); } -gboolean stc_default_connection_get_roaming(void) -{ - return g_default_connection.roaming; -} - default_connection_s *stc_get_default_connection(void) { return &g_default_connection; diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 2bb1777..5ff6adb 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -289,6 +289,9 @@ static gboolean __add_application_monitor(gpointer key, gpointer value, if (!stc->carg) { stc->carg = MALLOC0(counter_arg_s, 1); + if (stc->carg == NULL) + return FALSE; + stc->carg->sock = stc_monitor_get_counter_socket(); } @@ -319,6 +322,9 @@ static gboolean __remove_application_monitor(gpointer key, gpointer value, if (!stc->carg) { stc->carg = MALLOC0(counter_arg_s, 1); + if (stc->carg == NULL) + return FALSE; + stc->carg->sock = stc_monitor_get_counter_socket(); } @@ -419,6 +425,11 @@ static void __process_restriction(enum traffic_restriction_type rst_type, if (!stc->carg) { stc->carg = MALLOC0(counter_arg_s, 1); + if (stc->carg == NULL) { + g_free(default_ifname); + return; + } + stc->carg->sock = stc_monitor_get_counter_socket(); } @@ -452,6 +463,11 @@ static void __process_restriction(enum traffic_restriction_type rst_type, if (!stc->carg) { stc->carg = MALLOC0(counter_arg_s, 1); + if (stc->carg == NULL) { + g_free(default_ifname); + return; + } + stc->carg->sock = stc_monitor_get_counter_socket(); } @@ -737,23 +753,26 @@ static gboolean __update_app_statistics(gpointer key, gpointer value, time_t *touch_time = (time_t *)data; stc_db_classid_iftype_key stat_key; stc_db_app_stats stat; - char *default_ifname = stc_default_connection_get_ifname(); + default_connection_s *default_connection = stc_get_default_connection(); memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key)); memset(&stat, 0 , sizeof(stc_db_app_stats)); stat_key.classid = app_value->classid; - stat_key.iftype = stc_default_connection_get_type(); + stat_key.iftype = default_connection->type; + if (STC_IFACE_DATACALL == stat_key.iftype) - stat_key.imsi = g_strdup("unknown"); + stat_key.imsi = g_strdup(default_connection->imsi); else stat_key.imsi = g_strdup("noneimsi"); - g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH); + + 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; stat.rcv_count = app_value->counter.in_bytes; - stat.is_roaming = stc_default_connection_get_roaming(); + stat.is_roaming = default_connection->roaming; stat.ground = STC_APP_STATE_UNKNOWN; table_statistics_insert(&stat_key, &stat, *touch_time); @@ -763,7 +782,6 @@ static gboolean __update_app_statistics(gpointer key, gpointer value, FREE(stat.app_id); FREE(stat_key.imsi); - FREE(default_ifname); return FALSE; } @@ -1020,6 +1038,9 @@ static gboolean __update_contr_cb(void *user_data) ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data"); if (!stc->carg) { stc->carg = MALLOC0(counter_arg_s, 1); + if (stc->carg == NULL) + return TRUE; /* we need to continue the timer */ + stc->carg->sock = stc_monitor_get_counter_socket(); } @@ -1323,11 +1344,11 @@ static void __set_background_state(guint state) g_system->background_state = state; } -static gboolean __processes_tree_foreach_background(gpointer key, gpointer value, - gpointer data) +static gboolean __processes_tree_foreach_background(gpointer key, + gpointer value, + gpointer data) { stc_process_key_s *proc_key = (stc_process_key_s *)key; - stc_process_value_s *proc_value = (stc_process_value_s *)value; stc_app_key_s *app_key = (stc_app_key_s *)data; if (g_system->background_state) @@ -1345,7 +1366,8 @@ static gboolean __apps_tree_foreach_background(gpointer key, gpointer value, stc_app_value_s *app_value = (stc_app_value_s *)value; if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX)) - g_tree_foreach(app_value->processes, __processes_tree_foreach_background, app_key); + g_tree_foreach(app_value->processes, + __processes_tree_foreach_background, app_key); return FALSE; } @@ -1412,7 +1434,8 @@ stc_error_e stc_monitor_init(void) return STC_ERROR_FAIL; } - __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE, &g_system->background_state); + __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE, + (int *)&g_system->background_state); __fill_restritions_list(); -- 2.7.4 From f599780803f66fa9be773d8ea1c4f1278a9cb695 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 20 Jul 2017 11:47:43 +0530 Subject: [PATCH 03/16] Fix: Extra "_BACKGROUND" was added in background cgroups. Description: An extra "_BACKGROUN" keywork was added when any update in ground status was performed. Change-Id: Ib297853b08c7516483d88674535b032f1ccd932c Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/monitor/stc-monitor.c | 14 +++----------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 4c005ca..8f96ff2 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.24 +Version: 0.0.25 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 5ff6adb..19e4b90 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -1618,18 +1618,10 @@ stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key, if (proc_lookup->ground != ground) proc_lookup->ground = ground; - if (ground == STC_APP_STATE_BACKGROUND) { - if (__get_background_state()) { - place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID); - } else { - char *background_app_id = g_strconcat(app_key.app_id, - STC_BACKGROUND_APP_SUFFIX, NULL); - place_pids_to_net_cgroup(proc_key.pid, background_app_id); - g_free(background_app_id); - } - } else { + if (ground == STC_APP_STATE_BACKGROUND && __get_background_state()) + place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID); + else place_pids_to_net_cgroup(proc_key.pid, app_key.app_id); - } return ret; } -- 2.7.4 From 1e43de3cef3def735d5bf304900d34154f3cf2bc Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 20 Jul 2017 16:23:51 +0530 Subject: [PATCH 04/16] Monitoring system wide IPv4 and IPv6 data usage. Change-Id: I66e8b7af58f7e4fc79b4cbf94581738218726311 Signed-off-by: Nishant Chaprana --- include/stc-manager-util.h | 18 ++-- include/stc-manager.h | 4 + packaging/stc-manager.spec | 2 +- src/database/tables/table-statistics.c | 1 + src/helper/helper-cgroup.c | 65 ----------- src/helper/helper-cgroup.h | 7 -- src/helper/helper-net-cls.c | 15 ++- src/helper/helper-nfacct-rule.c | 174 +++++++++++++++--------------- src/helper/helper-nfacct-rule.h | 33 +++--- src/helper/helper-restriction.c | 39 ------- src/helper/helper-restriction.h | 32 ------ src/monitor/stc-monitor.c | 192 ++++++++++++++++++++++++++------- 12 files changed, 285 insertions(+), 297 deletions(-) delete mode 100755 src/helper/helper-restriction.c delete mode 100755 src/helper/helper-restriction.h diff --git a/include/stc-manager-util.h b/include/stc-manager-util.h index 7a19fcb..d5ee410 100755 --- a/include/stc-manager-util.h +++ b/include/stc-manager-util.h @@ -337,17 +337,15 @@ static inline bool strstart_with(const char *str, const char *with) */ enum stc_reserved_classid { STC_UNKNOWN_CLASSID, - STC_ALL_APP_CLASSID, /**< kernel expects 1 for - handling restriction for all - applications */ - STC_TETHERING_APP_CLASSID, /**< it uses in user space logic - for counting tethering traffic */ - STC_FOREGROUND_APP_CLASSID, /* it will used for special cgroup, - blocked cgroup */ - STC_BACKGROUND_APP_CLASSID, - STC_TOTAL_DATACALL_CLASSID, - STC_TOTAL_WIFI_CLASSID, + STC_ALL_APP_CLASSID, /**< kernel expects 1 for handling restriction for all applications */ + STC_TETHERING_APP_CLASSID, /**< it uses in user space logic for counting tethering traffic */ + STC_FOREGROUND_APP_CLASSID, /**< it will used for special cgroup, blocked cgroup */ + STC_BACKGROUND_APP_CLASSID, /**< background data monitoring */ + STC_TOTAL_DATACALL_CLASSID, /**< Cellular data monitoring */ + STC_TOTAL_WIFI_CLASSID, /**< Wi-Fi data monitoring */ STC_TOTAL_BLUETOOTH_CLASSID, + STC_TOTAL_IPV4_CLASSID, /**< IPV4 data monitoring */ + STC_TOTAL_IPV6_CLASSID, /**< IPV6 data monitoring */ STC_NETWORK_RESTRICTION_APP_CLASSID, STC_RESERVED_CLASSID_MAX, }; diff --git a/include/stc-manager.h b/include/stc-manager.h index c82c3af..160da59 100755 --- a/include/stc-manager.h +++ b/include/stc-manager.h @@ -28,6 +28,8 @@ #define STC_TOTAL_DATACALL "TOTAL_DATACALL" #define STC_TOTAL_WIFI "TOTAL_WIFI" #define STC_TOTAL_BLUETOOTH "TOTAL_BLUETOOTH" +#define STC_TOTAL_IPV4 "TOTAL_IPV4" +#define STC_TOTAL_IPV6 "TOTAL_IPV6" typedef enum { STC_CANCEL = 0, /**< cancel */ @@ -78,6 +80,8 @@ typedef enum { STC_IFACE_WIFI, /**< wifi data */ STC_IFACE_WIRED, /**< wired interface */ STC_IFACE_BLUETOOTH, /**< bluetooth interface */ + STC_IFACE_IPV4, /**< ipv4 interface */ + STC_IFACE_IPV6, /**< ipv6 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 8f96ff2..4668ee5 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.25 +Version: 0.0.26 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c index d28ec86..0f5db95 100755 --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -105,6 +105,7 @@ "ifname, imsi, ground from statistics " \ " where (time_stamp between ? and ?) and binpath != 'TOTAL_DATACALL' " \ "and binpath != 'TOTAL_WIFI' and binpath != 'TOTAL_BLUETOOTH' " \ + "and binpath != 'TOTAL_IPV4' and binpath != 'TOTAL_IPV6' " \ "group by iftype, ifname, imsi, hw_net_protocol_type, is_roaming " \ "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ "is_roaming" diff --git a/src/helper/helper-cgroup.c b/src/helper/helper-cgroup.c index af9f1f1..aa01d16 100755 --- a/src/helper/helper-cgroup.c +++ b/src/helper/helper-cgroup.c @@ -24,26 +24,6 @@ #define MAX_PATH_LENGTH 512 -static int read_uint(FILE *handler, uint32_t *out) -{ - return fscanf(handler, "%u", out); -} - -static int write_uint(FILE *handler, uint32_t number) -{ - _cleanup_free_ char *digit_buf = NULL; - int ret; - - ret = asprintf(&digit_buf, "%u\n", number); - ret_value_errno_msg_if(ret < 0, -ENOMEM, "asprintf failed\n"); - - ret = fputs(digit_buf, handler); - ret_value_errno_msg_if(ret == EOF, errno ? -errno : -EIO, - "Fail to write file"); - - return 0; -} - static bool cgroup_is_exists(const char *cgroup_full_path) { struct stat stat_buf; @@ -214,51 +194,6 @@ int cgroup_make_subdir(const char *parentdir, const char *cgroup_name, return STC_ERROR_NONE; } -/* FIXME: tasks is not removed from tasks list */ -int cgroup_remove_pid(const char *cgroup_subsystem, const char *cgroup_name, - const int pid) -{ - char cgroup_tasks_file_path[MAX_PATH_LENGTH]; - FILE *handler = 0; - guint i = 0; - pid_t pid_for_read = 0; - GArray *pids = NULL; - guint pid_count = 0;; - - snprintf(cgroup_tasks_file_path, sizeof(cgroup_tasks_file_path), - "%s/%s/tasks", cgroup_subsystem, cgroup_name); - - handler = fopen(cgroup_tasks_file_path, "r"); - if (!handler) { - STC_LOGE("Read file open failed"); - return -1; - } - - pids = g_array_new(FALSE, FALSE, sizeof(pid_t)); - - while (read_uint(handler, (uint32_t *)&pid_for_read) >= 0) { - if (pid_for_read != pid) { - pids = g_array_append_val(pids, pid_for_read); - ++pid_count; - } - } - - fclose(handler); - - handler = fopen(cgroup_tasks_file_path, "w"); - if (!handler) { - STC_LOGE("Write file open failed"); - return -1; - } - - for (i = 0; i < pid_count; i++) - write_uint(handler, g_array_index(pids, pid_t, i)); - - fclose(handler); - g_array_free(pids, TRUE); - return 0; -} - int cgroup_set_release_agent(const char *cgroup_subsys, const char *release_agent) { diff --git a/src/helper/helper-cgroup.h b/src/helper/helper-cgroup.h index 273036c..725cad4 100755 --- a/src/helper/helper-cgroup.h +++ b/src/helper/helper-cgroup.h @@ -116,13 +116,6 @@ int cgroup_set_release_agent(const char *cgroup_subsys, int cgroup_get_pids(const char *name, GArray **pids); /** - * @desc remove PID from a certain cgroup tasks file. - * @return 0 if pid removed - */ -int cgroup_remove_pid(const char *cgroup_subsystem, const char *cgroup_name, - const int pid); - -/** * @desc initializes cgroups. */ void cgroup_init(void); diff --git a/src/helper/helper-net-cls.c b/src/helper/helper-net-cls.c index 1c22608..0f2ae30 100755 --- a/src/helper/helper-net-cls.c +++ b/src/helper/helper-net-cls.c @@ -34,17 +34,20 @@ typedef GArray task_classid_array; static uint32_t __produce_classid(check_classid_used_cb check_classid_cb) { uint32_t classid = STC_RESERVED_CLASSID_MAX; - int classid_test_count = 0; int ret = fread_uint(CUR_CLASSID_PATH, &classid); if (ret < 0) STC_LOGI("Can not read current classid"); + classid += 1; - if (check_classid_cb) + + if (check_classid_cb) { + int classid_test_count = 0; for (classid_test_count = 0; classid_test_count < INT32_MAX; ++classid) { if (!check_classid_cb(classid)) break; } + } ret = fwrite_uint(CUR_CLASSID_PATH, ++classid); if (ret < 0) @@ -90,7 +93,7 @@ uint32_t get_classid_by_app_id(const char *app_id, int create) const char *path_to_net_cgroup_dir = NULL; if (app_id == NULL) { - STC_LOGE("app_id must be not empty"); + STC_LOGE("app_id must be not empty"); return STC_UNKNOWN_CLASSID; } @@ -106,6 +109,12 @@ uint32_t get_classid_by_app_id(const char *app_id, int create) if (!strcmp(app_id, STC_TOTAL_BLUETOOTH)) return STC_TOTAL_BLUETOOTH_CLASSID; + if (!strcmp(app_id, STC_TOTAL_IPV4)) + return STC_TOTAL_IPV4_CLASSID; + + if (!strcmp(app_id, STC_TOTAL_IPV6)) + return STC_TOTAL_IPV6_CLASSID; + if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX)) path_to_net_cgroup_dir = BACKGROUND_CGROUP_NETWORK; else diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index b46b3a4..3c365ee 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -47,13 +47,11 @@ #define RULE_APP_OUT "%s -w %s OUTPUT -o %s -m cgroup --cgroup %u %s %s" #define RULE_APP_IN "%s -w %s INPUT -i %s -m cgroup --cgroup %u %s %s" - /* iptables -w [I/A/D] [OUTPUT/FORWARD/INPUT] -o/-i iface -m nfacct --nfacct-name name -j ACCEPT/REJECT */ #define RULE_IFACE_OUT "%s -w %s %s -o %s %s %s" #define RULE_IFACE_IN "%s -w %s %s -i %s %s %s" - #define NFNL_SUBSYS_ACCT 7 #define BUF_SIZE_FOR_ERR 100 @@ -388,7 +386,6 @@ static void wait_for_rule_cmd(pid_t pid) { int status; pid_t ret_pid; - char buf[BUF_SIZE_FOR_ERR] = { 0 }; if (!pid || pid == -1) { STC_LOGD("no need to wait"); @@ -396,9 +393,11 @@ static void wait_for_rule_cmd(pid_t pid) } ret_pid = waitpid(pid, &status, 0); - if (ret_pid < 0) + if (ret_pid < 0) { + char buf[BUF_SIZE_FOR_ERR] = { 0 }; STC_LOGD("can't wait for a pid %d %d %s", pid, status, strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); + } } static char* get_cmd_pos(const char *cmd_buf) @@ -450,7 +449,6 @@ stc_error_e exec_iptables_cmd(const char *cmd_buf, pid_t *cmd_pid) char *args[args_number + 2]; int ret; char *save_ptr = NULL; - char buf[BUF_SIZE_FOR_ERR] = { 0 }; STC_LOGD("executing iptables cmd %s in forked process", cmd_buf); @@ -473,10 +471,12 @@ stc_error_e exec_iptables_cmd(const char *cmd_buf, pid_t *cmd_pid) args[i] = NULL; ret = execv(cmd, args); - if (ret) + if (ret) { + char buf[BUF_SIZE_FOR_ERR] = { 0 }; STC_LOGE("Can't execute %s: %s", cmd_buf, strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); + } exit(ret); } @@ -499,7 +499,6 @@ stc_error_e exec_ip6tables_cmd(const char *cmd_buf, pid_t *cmd_pid) char *args[args_number + 2]; int ret; char *save_ptr = NULL; - char buf[BUF_SIZE_FOR_ERR] = { 0 }; STC_LOGD("executing ip6tables cmd %s in forked process", cmd_buf); @@ -522,9 +521,12 @@ stc_error_e exec_ip6tables_cmd(const char *cmd_buf, pid_t *cmd_pid) args[i] = NULL; ret = execv(cmd, args); - if (ret) + if (ret) { + char buf[BUF_SIZE_FOR_ERR] = { 0 }; STC_LOGE("Can't execute %s: %s", - cmd_buf, strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); + cmd_buf, strerror_r(errno, buf, + BUF_SIZE_FOR_ERR)); + } exit(ret); } @@ -541,60 +543,61 @@ static char *choose_iftype_name(nfacct_rule_s *rule) static stc_error_e exec_iface_cmd(const char *pattern, const char *cmd, const char *chain, const char *nfacct, const char *jump, char *iftype_name, - pid_t *pid) + pid_t *pid, nfacct_rule_iptype iptype) { char block_buf[MAX_PATH_LENGTH]; int ret; + const char *iptables_type = IPTABLES; ret_value_msg_if(iftype_name == NULL, STC_ERROR_FAIL, "Invalid network interface name argument"); - /* iptables rule */ - ret = snprintf(block_buf, sizeof(block_buf), pattern, IPTABLES, cmd, chain, - iftype_name, nfacct, jump); - ret_value_msg_if(ret > sizeof(block_buf), STC_ERROR_FAIL, - "Not enough buffer"); - exec_iptables_cmd(block_buf, pid); - wait_for_rule_cmd(*pid); + if (iptype == NFACCT_TYPE_IPV6) + iptables_type = IP6TABLES; - /* ip6tables rule */ - ret = snprintf(block_buf, sizeof(block_buf), pattern, IP6TABLES, cmd, chain, - iftype_name, nfacct, jump); + ret = snprintf(block_buf, sizeof(block_buf), pattern, iptables_type, + cmd, chain, iftype_name, nfacct, jump); ret_value_msg_if(ret > sizeof(block_buf), STC_ERROR_FAIL, "Not enough buffer"); - ret = exec_ip6tables_cmd(block_buf, pid); + + if (iptype == NFACCT_TYPE_IPV6) + exec_ip6tables_cmd(block_buf, pid); + else + exec_iptables_cmd(block_buf, pid); + wait_for_rule_cmd(*pid); - return ret; + return STC_ERROR_NONE; } static stc_error_e exec_app_cmd(const char *pattern, const char *cmd, const char *nfacct, const char *jump, const uint32_t classid, char *iftype_name, - pid_t *pid) + pid_t *pid, nfacct_rule_iptype iptype) { char block_buf[MAX_PATH_LENGTH]; int ret; + const char *iptables_type = IPTABLES; + ret_value_msg_if(iftype_name == NULL, STC_ERROR_FAIL, "Invalid network interface name argument"); - /* iptables rules */ - ret = snprintf(block_buf, sizeof(block_buf), pattern, IPTABLES, cmd, - iftype_name, classid, nfacct, jump); - ret_value_msg_if(ret > sizeof(block_buf), STC_ERROR_FAIL, - "Not enough buffer"); - exec_iptables_cmd(block_buf, pid); - wait_for_rule_cmd(*pid); + if (iptype == NFACCT_TYPE_IPV6) + iptables_type = IP6TABLES; - /* ip6tables rules */ - ret = snprintf(block_buf, sizeof(block_buf), pattern, IP6TABLES, cmd, - iftype_name, classid, nfacct, jump); + ret = snprintf(block_buf, sizeof(block_buf), pattern, iptables_type, + cmd, iftype_name, classid, nfacct, jump); ret_value_msg_if(ret > sizeof(block_buf), STC_ERROR_FAIL, "Not enough buffer"); - ret = exec_ip6tables_cmd(block_buf, pid); + + if (iptype == NFACCT_TYPE_IPV6) + exec_ip6tables_cmd(block_buf, pid); + else + exec_iptables_cmd(block_buf, pid); + wait_for_rule_cmd(*pid); - return ret; + return STC_ERROR_NONE; } static char *get_iptables_cmd(const nfacct_rule_action action) @@ -629,30 +632,28 @@ static char *get_iptables_jump(const nfacct_rule_jump jump) return ""; } -static stc_error_e produce_app_rule(nfacct_rule_s *rule, - const int64_t send_limit, - const int64_t rcv_limit, - const nfacct_rule_action action, - const nfacct_rule_jump jump, - const nfacct_rule_direction iotype) +static stc_error_e produce_app_rule(nfacct_rule_s *rule) { - char *set_cmd = get_iptables_cmd(action); - char *jump_cmd = get_iptables_jump(jump); + if (rule == NULL) + return STC_ERROR_INVALID_PARAMETER; + + 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]; stc_error_e ret = STC_ERROR_NONE; pid_t pid = 0; /* income part */ - if (iotype & NFACCT_COUNTER_IN) { - rule->quota = rcv_limit; + if (rule->iotype & NFACCT_COUNTER_IN) { + rule->quota = rule->rcv_limit; rule->iotype = NFACCT_COUNTER_IN; generate_counter_name(rule); /* to support quated counter we need nfacct, * don't use it in case of just block without a limit * iow, send_limit = 0 and rcv_limit 0 */ - if (action != NFACCT_ACTION_DELETE) { + if (rule->action != NFACCT_ACTION_DELETE) { ret = nfacct_send_del(rule); ret_value_msg_if(ret != STC_ERROR_NONE, ret, "can't del quota counter"); @@ -672,14 +673,14 @@ static stc_error_e produce_app_rule(nfacct_rule_s *rule, ret = exec_app_cmd(RULE_APP_IN, set_cmd, nfacct_buf, jump_cmd, rule->classid, choose_iftype_name(rule), - &pid); + &pid, rule->iptype); ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL, "Can't set conditional block for ingress" " traffic, for classid %u, cmd %s, j %s", rule->classid, set_cmd, jump_cmd); /* remove in any case */ - if (action == NFACCT_ACTION_DELETE) { + if (rule->action == NFACCT_ACTION_DELETE) { /* TODO here and everywhere should be not just a del, * here should be get counted value and than * set new counter with that value, but it's minor issue, @@ -694,12 +695,12 @@ static stc_error_e produce_app_rule(nfacct_rule_s *rule, } } - if (iotype & NFACCT_COUNTER_OUT) { + if (rule->iotype & NFACCT_COUNTER_OUT) { /* outcome part */ rule->iotype = NFACCT_COUNTER_OUT; - rule->quota = send_limit; + rule->quota = rule->send_limit; generate_counter_name(rule); - if (action != NFACCT_ACTION_DELETE) { + if (rule->action != NFACCT_ACTION_DELETE) { ret = nfacct_send_del(rule); ret_value_msg_if(ret != STC_ERROR_NONE, ret, "can't del quota counter"); @@ -717,12 +718,12 @@ static stc_error_e produce_app_rule(nfacct_rule_s *rule, ret = exec_app_cmd(RULE_APP_OUT, set_cmd, nfacct_buf, jump_cmd, rule->classid, choose_iftype_name(rule), - &pid); + &pid, rule->iptype); ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL, "Can't set conditional block for engress" " traffic, for classid %u, cmd %s, j %s", rule->classid, set_cmd, jump_cmd); - if (action == NFACCT_ACTION_DELETE) { + if (rule->action == NFACCT_ACTION_DELETE) { rule->iptables_rule = nfacct_send_del; /* not effective, it's better to replace * set_finalize_flag by set_property, @@ -738,27 +739,25 @@ static stc_error_e produce_app_rule(nfacct_rule_s *rule, return STC_ERROR_NONE; } -static stc_error_e produce_iface_rule(nfacct_rule_s *rule, - const int64_t send_limit, - const int64_t rcv_limit, - const nfacct_rule_action action, - const nfacct_rule_jump jump, - const nfacct_rule_direction iotype) +static stc_error_e produce_iface_rule(nfacct_rule_s *rule) { - char *set_cmd = get_iptables_cmd(action); - char *jump_cmd = get_iptables_jump(jump); + if (rule == NULL) + return STC_ERROR_INVALID_PARAMETER; + + 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]; stc_error_e ret; pid_t pid = 0; - if (iotype & NFACCT_COUNTER_IN) { + if (rule->iotype & NFACCT_COUNTER_IN) { /* income part */ rule->iotype = NFACCT_COUNTER_IN; - rule->quota = rcv_limit; + rule->quota = rule->rcv_limit; generate_counter_name(rule); - if (action != NFACCT_ACTION_DELETE) { + if (rule->action != NFACCT_ACTION_DELETE) { /* send delete comman in case of creation, * because nfacct doesn't reset value for nfacct quota * in case of quota existing */ @@ -780,7 +779,8 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule, ret = exec_iface_cmd(RULE_IFACE_IN, set_cmd, get_iptables_chain(rule->iotype), nfacct_buf, jump_cmd, - choose_iftype_name(rule), &pid); + choose_iftype_name(rule), &pid, + rule->iptype); ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL, "Can't set conditional block for ingress" " traffic, for iftype %d, cmd %s, j %s", @@ -792,7 +792,8 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule, /* RULE_IFACE_OUT is not a misprint here */ ret = exec_iface_cmd(RULE_IFACE_IN, set_cmd, FORWARD_RULE, nfacct_buf, jump_cmd, - choose_iftype_name(rule), &pid); + choose_iftype_name(rule), &pid, + rule->iptype); ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL, "Can't set forward rule for ingress " "traffic, for iftype %d, cmd %s, j %s", @@ -800,7 +801,7 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule, } /* tethering */ - if (action == NFACCT_ACTION_DELETE) { + if (rule->action == NFACCT_ACTION_DELETE) { rule->iptables_rule = nfacct_send_del; set_finalize_flag(rule); nfacct_send_get(rule); @@ -810,13 +811,13 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule, } } - if (iotype & NFACCT_COUNTER_OUT) { + if (rule->iotype & NFACCT_COUNTER_OUT) { /* outcome part */ rule->iotype = NFACCT_COUNTER_OUT; - rule->quota = send_limit; + rule->quota = rule->send_limit; generate_counter_name(rule); - if (action != NFACCT_ACTION_DELETE) { + if (rule->action != NFACCT_ACTION_DELETE) { /* send delete comman in case of creation, * because nfacct doesn't reset value for nfacct quota * in case of quota existing */ @@ -837,7 +838,8 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule, ret = exec_iface_cmd(RULE_IFACE_OUT, set_cmd, OUT_RULE, nfacct_buf, jump_cmd, - choose_iftype_name(rule), &pid); + choose_iftype_name(rule), &pid, + rule->iptype); ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL, "Can't set conditional block for " "engress traffic, for iftype %d, cmd %s, j %s", @@ -847,7 +849,8 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule, rule->intend == NFACCT_BLOCK) { ret = exec_iface_cmd(RULE_IFACE_OUT, set_cmd, FORWARD_RULE, nfacct_buf, jump_cmd, - choose_iftype_name(rule), &pid); + choose_iftype_name(rule), &pid, + rule->iptype); ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL, "Can't set forward rule for engress " "traffic, for iftype %d, cmd %s, j %s", @@ -855,7 +858,7 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule, } /* tethering */ - if (action == NFACCT_ACTION_DELETE) { + if (rule->action == NFACCT_ACTION_DELETE) { rule->iptables_rule = nfacct_send_del; set_finalize_flag(rule); nfacct_send_get(rule); @@ -867,29 +870,28 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule, return STC_ERROR_NONE; } -stc_error_e produce_net_rule(nfacct_rule_s *rule, - const int64_t send_limit, - const int64_t rcv_limit, - const nfacct_rule_action action, - const nfacct_rule_jump jump, - const nfacct_rule_direction iotype) +stc_error_e produce_net_rule(nfacct_rule_s *rule) { stc_error_e ret = STC_ERROR_NONE; - if (action == NFACCT_ACTION_APPEND && rule->intend == NFACCT_WARN - && !send_limit && !rcv_limit) + if (rule == NULL) + return STC_ERROR_INVALID_PARAMETER; + + if (rule->action == NFACCT_ACTION_APPEND && + rule->intend == NFACCT_WARN && + !rule->send_limit && !rule->rcv_limit) return STC_ERROR_NONE; if (rule->classid != STC_ALL_APP_CLASSID && rule->classid != STC_TETHERING_APP_CLASSID && rule->classid != STC_TOTAL_DATACALL_CLASSID && rule->classid != STC_TOTAL_WIFI_CLASSID && - rule->classid != STC_TOTAL_BLUETOOTH_CLASSID) - ret = produce_app_rule(rule, send_limit, - rcv_limit, action, jump, iotype); + rule->classid != STC_TOTAL_BLUETOOTH_CLASSID && + rule->classid != STC_TOTAL_IPV4_CLASSID && + rule->classid != STC_TOTAL_IPV6_CLASSID) + ret = produce_app_rule(rule); else - ret = produce_iface_rule(rule, send_limit, rcv_limit, - action, jump, iotype); + ret = produce_iface_rule(rule); return ret; } diff --git a/src/helper/helper-nfacct-rule.h b/src/helper/helper-nfacct-rule.h index 2220868..89eb27c 100755 --- a/src/helper/helper-nfacct-rule.h +++ b/src/helper/helper-nfacct-rule.h @@ -57,6 +57,13 @@ typedef enum { NFACCT_RULE_LAST_ELEM, } nfacct_rule_intend; +typedef enum { + NFACCT_TYPE_UNKNOWN, + NFACCT_TYPE_IPV4, + NFACCT_TYPE_IPV6, + NFACCT_TYPE_LAST_ELEM +} nfacct_rule_iptype; + enum nfnl_acct_flags { NFACCT_F_QUOTA_PKTS = (1 << 0), NFACCT_F_QUOTA_BYTES = (1 << 1), @@ -81,20 +88,21 @@ struct nfacct_rule { pid_t pid; uint32_t classid; stc_iface_type_e iftype; + nfacct_rule_action action; nfacct_rule_direction iotype; nfacct_rule_intend intend; + nfacct_rule_jump jump; /* in most cases jump is evalutation based on intend, but not always */ + stc_restriction_state_e rst_state; + nfacct_rule_iptype iptype; + struct counter_arg *carg; stc_error_e(*iptables_rule)(struct nfacct_rule *counter); int64_t quota; int quota_id; stc_roaming_type_e roaming; - stc_restriction_state_e rst_state; - /** - * in most cases jump is evalutation based - * on intend, but not always - */ - nfacct_rule_jump jump; + int64_t send_limit; + int64_t rcv_limit; }; typedef struct nfacct_rule nfacct_rule_s; @@ -105,18 +113,7 @@ void generate_counter_name(nfacct_rule_s *counter); bool recreate_counter_by_name(char *cnt_name, nfacct_rule_s *counter); stc_error_e nfacct_send_get_all(struct counter_arg *carg); -stc_error_e nfacct_send_get_counters(struct counter_arg *carg, - const char *name); -stc_error_e nfacct_send_get(nfacct_rule_s *rule); -stc_error_e nfacct_send_del(nfacct_rule_s *counter); - -stc_error_e exec_iptables_cmd(const char *cmd_buf, pid_t *pid); -stc_error_e produce_net_rule(nfacct_rule_s *rule, - const int64_t send_limit, - const int64_t rcv_limit, - const nfacct_rule_action action, - const nfacct_rule_jump jump, - const nfacct_rule_direction iotype); +stc_error_e produce_net_rule(nfacct_rule_s *rule); netlink_serialization_command * netlink_create_command(struct netlink_serialization_params *params); diff --git a/src/helper/helper-restriction.c b/src/helper/helper-restriction.c deleted file mode 100755 index fb15856..0000000 --- a/src/helper/helper-restriction.c +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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. - */ - -/* - * @file restriction-helper.c - * @desc Helper restriction functions - */ - -#include "stc-manager.h" -#include "stc-restriction.h" -#include "transmission.h" - -stc_restriction_state_e -convert_to_restriction_state(const enum traffic_restriction_type rst_type) -{ - switch (rst_type) { - case RST_SET: - return STC_RESTRICTION_ACTIVATED; - case RST_UNSET: - return STC_RESTRICTION_REMOVED; - case RST_EXCLUDE: - return STC_RESTRICTION_EXCLUDED; - default: - return STC_RESTRICTION_UNKNOWN; - } -} diff --git a/src/helper/helper-restriction.h b/src/helper/helper-restriction.h deleted file mode 100755 index 51b959e..0000000 --- a/src/helper/helper-restriction.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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. - */ - -/** - * @file helper-restriction.h - * @desc Helper restriction functions - */ - -#ifndef __STC_HELPER_RESTRICTION_H__ -#define __STC_HELPER_RESTRICTION_H__ - -#include "stc-manager.h" -#include "stc-restriction.h" -#include "transmission.h" - -stc_restriction_state_e -convert_to_restriction_state(const enum traffic_restriction_type rst_type); - -#endif /* __STC_HELPER_RESTRICTION_H__ */ diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 19e4b90..08f1065 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -58,30 +58,122 @@ static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter) static stc_error_e __add_iptables_in(struct nfacct_rule *counter) { - return produce_net_rule(counter, 0, 0, - NFACCT_ACTION_INSERT, __get_jump_by_intend(counter), - NFACCT_COUNTER_IN); + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_INSERT; + counter->iotype = NFACCT_COUNTER_IN; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV4; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); } static stc_error_e __add_iptables_out(struct nfacct_rule *counter) { - return produce_net_rule(counter, 0, 0, - NFACCT_ACTION_INSERT, __get_jump_by_intend(counter), - NFACCT_COUNTER_OUT); + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_INSERT; + counter->iotype = NFACCT_COUNTER_OUT; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV4; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); } static stc_error_e __del_iptables_in(struct nfacct_rule *counter) { - return produce_net_rule(counter, 0, 0, - NFACCT_ACTION_DELETE, __get_jump_by_intend(counter), - NFACCT_COUNTER_IN); + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_DELETE; + counter->iotype = NFACCT_COUNTER_IN; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV4; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); } static stc_error_e __del_iptables_out(struct nfacct_rule *counter) { - return produce_net_rule(counter, 0, 0, - NFACCT_ACTION_DELETE, __get_jump_by_intend(counter), - NFACCT_COUNTER_OUT); + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_DELETE; + counter->iotype = NFACCT_COUNTER_OUT; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV4; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); +} + +static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter) +{ + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_INSERT; + counter->iotype = NFACCT_COUNTER_IN; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV6; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); +} + +static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter) +{ + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_INSERT; + counter->iotype = NFACCT_COUNTER_OUT; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV6; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); +} + +static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter) +{ + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_DELETE; + counter->iotype = NFACCT_COUNTER_IN; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV6; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); +} + +static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter) +{ + if (counter == NULL) + return STC_ERROR_INVALID_PARAMETER; + + counter->action = NFACCT_ACTION_DELETE; + counter->iotype = NFACCT_COUNTER_OUT; + counter->jump = __get_jump_by_intend(counter); + counter->iptype = NFACCT_TYPE_IPV6; + counter->send_limit = 0; + counter->rcv_limit = 0; + + return produce_net_rule(counter); } static int __processes_tree_key_compare(gconstpointer a, gconstpointer b, @@ -280,8 +372,8 @@ static gboolean __add_application_monitor(gpointer key, gpointer value, stc_s *stc = stc_get_manager(); if (app_value->classid == STC_TOTAL_DATACALL_CLASSID || - app_value->classid == STC_TOTAL_WIFI_CLASSID || - app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID) + app_value->classid == STC_TOTAL_WIFI_CLASSID || + app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID) return FALSE; if (stc && connection && connection->ifname) { @@ -303,8 +395,18 @@ static gboolean __add_application_monitor(gpointer key, gpointer value, counter.iftype = connection->type; g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH); - __add_iptables_in(&counter); - __add_iptables_out(&counter); + if (app_value->classid == STC_TOTAL_IPV4_CLASSID) { + __add_iptables_in(&counter); + __add_iptables_out(&counter); + } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) { + __add_ip6tables_in(&counter); + __add_ip6tables_out(&counter); + } else { + __add_iptables_in(&counter); + __add_iptables_out(&counter); + __add_ip6tables_in(&counter); + __add_ip6tables_out(&counter); + } } return FALSE; @@ -338,6 +440,8 @@ static gboolean __remove_application_monitor(gpointer key, gpointer value, __del_iptables_in(&counter); __del_iptables_out(&counter); + __del_ip6tables_in(&counter); + __del_ip6tables_out(&counter); } return FALSE; @@ -447,6 +551,10 @@ static void __process_restriction(enum traffic_restriction_type rst_type, /* iptables rule */ __add_iptables_in(&counter); __add_iptables_out(&counter); + + /* ip6tables rule */ + __add_ip6tables_in(&counter); + __add_ip6tables_out(&counter); } rstn_value->rst_state = STC_RESTRICTION_ACTIVATED; @@ -486,6 +594,10 @@ static void __process_restriction(enum traffic_restriction_type rst_type, __del_iptables_in(&counter); __del_iptables_out(&counter); + /* ip6tables rule */ + __del_ip6tables_in(&counter); + __del_ip6tables_out(&counter); + rstn_value->rst_state = STC_RESTRICTION_REMOVED; rstn_value->data_limit_reached = FALSE; } @@ -618,8 +730,8 @@ static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system) } static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key, - stc_rstn_value_s *rstn_value, - classid_bytes_context_s *context) + stc_rstn_value_s *rstn_value, + classid_bytes_context_s *context) { switch (context->counter->iotype) { case NFACCT_COUNTER_IN: @@ -629,7 +741,7 @@ static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key, if (rstn_value->data_counter >= rstn_value->data_warn_limit && rstn_value->warn_limit_crossed_notified == FALSE) { - gboolean rv = FALSE; + gboolean rv; char iftype[MAX_INT_LENGTH]; char byte[MAX_INT_LENGTH]; stc_s *stc = (stc_s *)stc_get_manager(); @@ -655,7 +767,7 @@ static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key, if (rstn_value->data_counter >= rstn_value->data_limit && rstn_value->rstn_limit_crossed_notified == FALSE) { - gboolean rv = FALSE; + gboolean rv; char iftype[MAX_INT_LENGTH]; char byte[MAX_INT_LENGTH]; stc_s *stc = (stc_s *)stc_get_manager(); @@ -667,6 +779,11 @@ static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key, __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; @@ -698,15 +815,15 @@ static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key, } static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key, - stc_rstn_value_s *rstn_value, - classid_bytes_context_s *context) + stc_rstn_value_s *rstn_value, + 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)) { context->counter->classid = rstn_value->classid; return __rstn_counter_update(rstn_key, rstn_value, context); } @@ -837,8 +954,8 @@ static gboolean __flush_rstns_counter_to_database(gpointer user_data) } static void __app_counter_update(stc_app_key_s *app_key, - stc_app_value_s *app_value, - classid_bytes_context_s *context) + stc_app_value_s *app_value, + classid_bytes_context_s *context) { switch (context->counter->iotype) { case NFACCT_COUNTER_IN: @@ -861,18 +978,19 @@ static void __app_counter_update(stc_app_key_s *app_key, } static void __interface_counter_update(stc_app_key_s *app_key, - stc_app_value_s *app_value, - classid_bytes_context_s *context) + stc_app_value_s *app_value, + classid_bytes_context_s *context) { if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID && - context->counter->iftype == STC_IFACE_DATACALL) || - (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_DATACALL) || + (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)) __app_counter_update(app_key, app_value, context); } + static gboolean __apps_counter_update_foreach_classid(gpointer key, gpointer value, gpointer data) @@ -1420,6 +1538,8 @@ stc_error_e stc_monitor_init(void) __add_application_by_interface(STC_TOTAL_DATACALL); __add_application_by_interface(STC_TOTAL_WIFI); __add_application_by_interface(STC_TOTAL_BLUETOOTH); + __add_application_by_interface(STC_TOTAL_IPV4); + __add_application_by_interface(STC_TOTAL_IPV6); /* creating restriction rules tree */ __update_contr_cb(NULL); -- 2.7.4 From d4e3bde7ecdff26045d9e11c4e1aa4b2022d7bbd Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Tue, 25 Jul 2017 19:01:31 +0530 Subject: [PATCH 05/16] Fix: Fetching granularized data usage issue. Description: This patch fixes the scenario where multiple entries are sent to upper layer because of granularity, also this patch fixes faulty fill operation when data is fetched from database. Change-Id: Ifbd7fcd646f461277bdd8bc90379f452032b05fe Signed-off-by: Nishant Chaprana --- interfaces/stcmanager-iface-statistics.xml | 2 +- packaging/stc-manager.spec | 2 +- src/database/tables/table-statistics.c | 11 ++++++----- src/stc-statistics.c | 20 +++++++++++++------- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/interfaces/stcmanager-iface-statistics.xml b/interfaces/stcmanager-iface-statistics.xml index 42b5c80..825da54 100644 --- a/interfaces/stcmanager-iface-statistics.xml +++ b/interfaces/stcmanager-iface-statistics.xml @@ -12,7 +12,7 @@ - + diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 4668ee5..fafa1dd 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.26 +Version: 0.0.27 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c index 0f5db95..9564d8e 100755 --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -55,16 +55,16 @@ #define SELECT_CHUNKS "select binpath, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, " \ "sum(sent) as sent, imsi, ground, iftype, ifname, " \ - "time_stamp - time_stamp % ? as time_stamp " \ + "time_stamp - time_stamp % ? as timestamp " \ "from statistics where time_stamp between ? and ? " \ - "group by binpath, time_stamp, imsi order by time_stamp" + "group by binpath, timestamp, imsi order by timestamp" #define SELECT_CHUNKS_IFACE "select binpath, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, " \ "sum(sent) as sent, imsi, ground, iftype, ifname, " \ - "time_stamp - time_stamp % ? as time_stamp " \ + "time_stamp - time_stamp % ? as timestamp " \ "from statistics where time_stamp between ? and ? and iftype=?" \ - "group by binpath, time_stamp, imsi order by time_stamp" + "group by binpath, timestamp, imsi order by timestamp" #define SELECT_APP_DETAILS "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ @@ -557,9 +557,10 @@ stc_error_e table_statistics_per_app(const char *app_id, data.cnt.out_bytes = sqlite3_column_int64(stmt, 4); data.ifname = (char *)sqlite3_column_text(stmt, 5); data.imsi = (char *)sqlite3_column_text(stmt, 6); + data.ground = sqlite3_column_int(stmt, 7); if (rule->granularity) { - interval.from = sqlite3_column_int64(stmt, 7); + interval.from = sqlite3_column_int64(stmt, 8); interval.to = interval.from + rule->granularity; } diff --git a/src/stc-statistics.c b/src/stc-statistics.c index ec52aeb..b20e3bb 100755 --- a/src/stc-statistics.c +++ b/src/stc-statistics.c @@ -167,11 +167,13 @@ void __stc_statistics_app_info_builder_add(GVariantBuilder *builder, return; } - g_variant_builder_add(builder, "{sv}", "app_id", - g_variant_new_string(info->app_id)); + if (info->app_id) + g_variant_builder_add(builder, "{sv}", "app_id", + g_variant_new_string(info->app_id)); - g_variant_builder_add(builder, "{sv}", "ifname", - g_variant_new_string(info->ifname)); + if (info->ifname) + g_variant_builder_add(builder, "{sv}", "ifname", + g_variant_new_string(info->ifname)); g_variant_builder_add(builder, "{sv}", "imsi", g_variant_new_string(info->imsi)); @@ -232,14 +234,18 @@ stc_cb_ret_e __table_statistics_per_app_cb(const table_statistics_info *info, { __STC_LOG_FUNC_ENTER__; GVariantBuilder *builder = (GVariantBuilder *)user_data; + GVariantBuilder sub_builder; if (!info || !builder) { __STC_LOG_FUNC_EXIT__; return STC_CANCEL; } - __stc_statistics_app_info_builder_add(builder, info); + g_variant_builder_init(&sub_builder, G_VARIANT_TYPE("a{sv}")); + __stc_statistics_app_info_builder_add(&sub_builder, info); __stc_statistics_print_app_info(info); + g_variant_builder_add_value(builder, + g_variant_builder_end(&sub_builder)); __STC_LOG_FUNC_EXIT__; return STC_CONTINUE; @@ -346,7 +352,7 @@ gboolean handle_statistics_get(StcStatistics *object, STC_LOGD("No selection rule, using default selection rule."); } - builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}")); ret = table_statistics_per_app(app_id, &rule, __table_statistics_per_app_cb, @@ -358,7 +364,7 @@ gboolean handle_statistics_get(StcStatistics *object, return TRUE; } - return_parameters = g_variant_new("(ia{sv})", STC_ERROR_NONE, builder); + return_parameters = g_variant_new("(iaa{sv})", STC_ERROR_NONE, builder); g_variant_builder_unref(builder); DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters); -- 2.7.4 From c570781b44cc2e72ee0c770769ce01d51f7d9361 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Wed, 26 Jul 2017 10:31:13 +0530 Subject: [PATCH 06/16] Ignoring values of TOTAL_* details when fetching interface data. Description: When fetching interface related data and chunk data TOTAL_IPV4, TOTAL_IPV6, TOTAL_WIFI, TOTAL_BLUETOOTH, TOTAL_DATACALL app_id were increasing redundant data in fetch query. Change-Id: Icc074219dfd9e83832d7f193040969fd23a30d43 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/database/tables/table-statistics.c | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index fafa1dd..a15bc75 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.27 +Version: 0.0.28 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c index 9564d8e..1b6d288 100755 --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -103,9 +103,8 @@ #define SELECT_TOTAL "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ "ifname, imsi, ground from statistics " \ - " where (time_stamp between ? and ?) and binpath != 'TOTAL_DATACALL' " \ - "and binpath != 'TOTAL_WIFI' and binpath != 'TOTAL_BLUETOOTH' " \ - "and binpath != 'TOTAL_IPV4' and binpath != 'TOTAL_IPV6' " \ + "where (time_stamp between ? and ?) " \ + "and binpath NOT LIKE 'TOTAL_%' " \ "group by iftype, ifname, imsi, hw_net_protocol_type, is_roaming " \ "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ "is_roaming" @@ -113,8 +112,8 @@ #define SELECT_TOTAL_IFACE "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ "ifname, imsi, ground from statistics " \ - " where (time_stamp between ? and ?) " \ - "and iftype=? " \ + "where (time_stamp between ? and ?) and iftype=? " \ + "and binpath NOT LIKE 'TOTAL_%' " \ "group by hw_net_protocol_type, is_roaming, " \ "iftype, ifname, imsi " \ "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ @@ -124,6 +123,7 @@ "is_roaming, sum(received) as received, sum(sent) as sent, " \ "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \ "from statistics where time_stamp between ? and ? " \ + "and binpath NOT LIKE 'TOTAL_%' " \ "group by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ "is_roaming " \ "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ @@ -134,6 +134,7 @@ "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \ "from statistics where time_stamp between ? and ? " \ "and iftype = ? " \ + "and binpath NOT LIKE 'TOTAL_%' " \ "group by time_stamp, hw_net_protocol_type, is_roaming, iftype, ifname, imsi " \ "order by time_stamp, hw_net_protocol_type, is_roaming, iftype, " \ "ifname, imsi" -- 2.7.4 From fd6810bf5dfc73e813a4c56f2b22a71c643bd5c5 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 8 Aug 2017 14:37:56 +0900 Subject: [PATCH 07/16] Fixed a issue for using heap after free Change-Id: I6d79e47857211945aa29de2de71072dc98835bd4 Signed-off-by: hyunuktak --- src/database/db-common.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/database/db-common.c b/src/database/db-common.c index d2ad1ea..3895e12 100755 --- a/src/database/db-common.c +++ b/src/database/db-common.c @@ -75,6 +75,7 @@ stc_error_e stc_db_initialize_once(void) STC_LOGE("Can't set locking mode %s, skip set busy handler.", sqlite3_errmsg(database)); sqlite3_close(database); + database = NULL; __STC_LOG_FUNC_EXIT__; return STC_ERROR_DB_FAILED; } -- 2.7.4 From d447d111f225aec6526ee94b1fab5afa9f0242de Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 17 Aug 2017 19:38:03 +0530 Subject: [PATCH 08/16] Added netlink connector for monitoring processes Change-Id: I43335ff9d4daf2ca0d01decb29d7837a8f76af9c Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/helper/helper-procfs.c | 165 +++++++ src/helper/helper-procfs.h | 63 +++ ...ication-lifecycle.h => stc-app-ground-status.h} | 10 +- src/monitor/include/stc-app-lifecycle.h | 44 ++ src/monitor/stc-app-ground-status.c | 147 ++++++ src/monitor/stc-app-lifecycle.c | 508 +++++++++++++++++++++ src/monitor/stc-application-lifecycle.c | 458 ------------------- src/stc-manager-gdbus.c | 6 +- src/stc-manager.c | 3 + 10 files changed, 939 insertions(+), 467 deletions(-) create mode 100644 src/helper/helper-procfs.c create mode 100644 src/helper/helper-procfs.h rename src/monitor/include/{stc-application-lifecycle.h => stc-app-ground-status.h} (74%) create mode 100755 src/monitor/include/stc-app-lifecycle.h create mode 100755 src/monitor/stc-app-ground-status.c create mode 100755 src/monitor/stc-app-lifecycle.c delete mode 100755 src/monitor/stc-application-lifecycle.c diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index a15bc75..66b9d54 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.28 +Version: 0.0.29 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c new file mode 100644 index 0000000..0974f2a --- /dev/null +++ b/src/helper/helper-procfs.c @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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. + * + */ + +/** + * @file procfs.c + * @desc wrapper for reading profs information. + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "stc-error.h" +#include "stc-manager-util.h" +#include "helper-procfs.h" + +int proc_get_cmdline(pid_t pid, char *cmdline) +{ + char buf[PROC_BUF_MAX]; + char cmdline_buf[PROC_NAME_MAX]; + char *filename; + FILE *fp; + + snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid); + fp = fopen(buf, "r"); + if (fp == NULL) + return STC_ERROR_FAIL; + + if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) { + fclose(fp); + return STC_ERROR_FAIL; + } + fclose(fp); + + filename = strrchr(cmdline_buf, '/'); + if (filename == NULL) + filename = cmdline_buf; + else + filename = filename + 1; + + strncpy(cmdline, filename, PROC_NAME_MAX-1); + + return STC_ERROR_NONE; +} + +pid_t find_pid_from_cmdline(char *cmdline) +{ + pid_t pid = -1, foundpid = -1; + int ret = 0; + DIR *dp; + struct dirent *dentry; + char appname[PROC_NAME_MAX]; + + dp = opendir("/proc"); + if (!dp) { + STC_LOGE("BACKGRD MANAGE : fail to open /proc"); + return STC_ERROR_FAIL; + } + + while ((dentry = readdir(dp)) != NULL) { + if (!isdigit(dentry->d_name[0])) + continue; + + pid = atoi(dentry->d_name); + if (!pid) + continue; + ret = proc_get_cmdline(pid, appname); + if (ret == STC_ERROR_NONE) { + if (!strncmp(cmdline, appname, strlen(appname)+1)) { + foundpid = pid; + break; + } + } + } + closedir(dp); + return foundpid; +} + +int proc_get_label(pid_t pid, char *label) +{ + char buf[PROC_BUF_MAX]; + FILE *fp; + + snprintf(buf, sizeof(buf), "/proc/%d/attr/current", pid); + fp = fopen(buf, "r"); + if (fp == NULL) + return STC_ERROR_FAIL; + + if (fgets(label, PROC_NAME_MAX-1, fp) == NULL) { + fclose(fp); + return STC_ERROR_FAIL; + } + fclose(fp); + return STC_ERROR_NONE; +} + +int proc_get_exepath(pid_t pid, char *buf, int len) +{ + char path[PROC_BUF_MAX]; + int ret = 0; + + snprintf(path, sizeof(path), "/proc/%d/exe", pid); + ret = readlink(path, buf, len-1); + if (ret > 0) + buf[ret] = '\0'; + else + buf[0] = '\0'; + return STC_ERROR_NONE; +} + +static int proc_get_data(char *path, char *buf, int len) +{ + _cleanup_close_ int fd = -1; + int ret; + + fd = open(path, O_RDONLY); + if (fd < 0) + return STC_ERROR_FAIL; + + ret = read(fd, buf, len-1); + if (ret < 0) { + buf[0] = '\0'; + return STC_ERROR_FAIL; + } + buf[ret] = '\0'; + return STC_ERROR_NONE; +} + +int proc_get_raw_cmdline(pid_t pid, char *buf, int len) +{ + char path[PROC_BUF_MAX]; + snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); + return proc_get_data(path, buf, len); +} + +int proc_get_status(pid_t pid, char *buf, int len) +{ + char path[PROC_BUF_MAX]; + snprintf(path, sizeof(path), "/proc/%d/status", pid); + return proc_get_data(path, buf, len); +} diff --git a/src/helper/helper-procfs.h b/src/helper/helper-procfs.h new file mode 100644 index 0000000..4f0fd02 --- /dev/null +++ b/src/helper/helper-procfs.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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 __HELPER_PROCFS_H__ +#define __HELPER_PROCFS_H__ + +#include + +#define PROC_BUF_MAX 64 + +/** + * @desc get command line from /proc/{pid}/cmdline + * @return negative value if error + */ +int proc_get_cmdline(pid_t pid, char *cmdline); + +/** + * @desc find pid with /proc/{pid}/cmdline + * it returns first entry when many pids have same cmdline + * @return negative value if error + */ +pid_t find_pid_from_cmdline(char *cmdline); + +/** + * @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 + */ +int proc_get_label(pid_t pid, char *label); + +/** + * @desc get command line from /proc/{pid}/cmdline without any truncation + * @return negative value if error + */ +int proc_get_raw_cmdline(pid_t pid, char *buf, int len); + +/** + * @desc get symblolic link about /proc/{pid}/exe + * @return negative value if error + */ +int proc_get_exepath(pid_t pid, char *buf, int len); + +/** + * @desc get status from /proc/{pid}/status + * @return negative value if error + */ +int proc_get_status(pid_t pid, char *buf, int len); + +#endif /*__HELPER_PROCFS_H__*/ diff --git a/src/monitor/include/stc-application-lifecycle.h b/src/monitor/include/stc-app-ground-status.h similarity index 74% rename from src/monitor/include/stc-application-lifecycle.h rename to src/monitor/include/stc-app-ground-status.h index 8fa34af..fb02abc 100755 --- a/src/monitor/include/stc-application-lifecycle.h +++ b/src/monitor/include/stc-app-ground-status.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef __STC_APPLICATION_LIFECYCLE_H__ -#define __STC_APPLICATION_LIFECYCLE_H__ +#ifndef __STC_APP_GROUND_STATUS_H__ +#define __STC_APP_GROUND_STATUS_H__ #include "stc-error.h" #include "stc-manager.h" @@ -23,7 +23,7 @@ #include "stc-manager-util.h" #include "stc-monitor.h" -stc_error_e stc_application_lifecycle_monitor_init(stc_s *stc); -stc_error_e stc_application_lifecycle_monitor_deinit(stc_s *stc); +stc_error_e stc_app_ground_status_monitor_init(stc_s *stc); +stc_error_e stc_app_ground_status_monitor_deinit(stc_s *stc); -#endif /* __STC_APPLICATION_LIFECYCLE_H__ */ +#endif /* __STC_APP_GROUND_STATUS_H__ */ diff --git a/src/monitor/include/stc-app-lifecycle.h b/src/monitor/include/stc-app-lifecycle.h new file mode 100755 index 0000000..fae72ec --- /dev/null +++ b/src/monitor/include/stc-app-lifecycle.h @@ -0,0 +1,44 @@ +/* + * 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_APP_LIFECYCLE_H__ +#define __STC_APP_LIFECYCLE_H__ + +#include "stc-error.h" +#include "stc-manager.h" +#include "stc-manager-gdbus.h" +#include "stc-manager-util.h" +#include "stc-monitor.h" + +typedef enum { + STC_CMD_NONE, + STC_CMD_SET_FOREGRD, + STC_CMD_SET_BACKGRD, + STC_CMD_SET_APP_LAUNCHED, + STC_CMD_SET_SERVICE_LAUNCHED, + STC_CMD_SET_TERMINATED, + STC_CMD_MAX_ELEM +} stc_cmd_type_e; + +stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, + pid_t pid, + gchar *app_id, + gchar *pkg_id, + stc_app_type_e app_type); +void stc_app_lifecycle_monitor_init(void); +void stc_app_lifecycle_monitor_deinit(void); + +#endif /* __STC_APP_LIFECYCLE_H__ */ diff --git a/src/monitor/stc-app-ground-status.c b/src/monitor/stc-app-ground-status.c new file mode 100755 index 0000000..a5c24ec --- /dev/null +++ b/src/monitor/stc-app-ground-status.c @@ -0,0 +1,147 @@ +/* + * 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 "stc-app-lifecycle.h" +#include "stc-app-ground-status.h" + +/* + * AUL + */ +#define AUL_APP_STATUS_DBUS_PATH "/Org/Tizen/Aul/AppStatus" +#define AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE "org.tizen.aul.AppStatus" +#define AUL_APP_STATUS_BUS_NAME AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE + +#define AUL_APP_STATUS_DBUS_STATUS_CHANGE "AppStatusChange" +#define AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE "(issss)" + +typedef struct { + guint sub_id; + const gchar *path; + const gchar *interface; + const gchar *member; + const gchar *param_type; + GDBusSignalCallback callback; + gpointer user_data; +} signal_map_s; + +static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + __STC_LOG_FUNC_ENTER__; + pid_t pid; + stc_cmd_type_e status; + stc_app_type_e apptype; + gchar *appid, *pkgid, *statstr, *pkgtype; + + RETURN_IF_DBUS_TYPE_MISMATCH(parameters, + AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE); + + g_variant_get(parameters, AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE, + &pid, &appid, &pkgid, &statstr, &pkgtype); + + if (!strncmp(statstr, "fg", 2)) { + status = STC_CMD_SET_FOREGRD; + } else if (!strncmp(statstr, "bg", 2)) { + status = STC_CMD_SET_BACKGRD; + } else { + __STC_LOG_FUNC_EXIT__; + goto handle_error; + } + + if (!strncmp(pkgtype, "svc", 3)) + apptype = STC_APP_TYPE_SERVICE; + else if (!strncmp(pkgtype, "widget", 6)) + apptype = STC_APP_TYPE_WIDGET; + else if (!strncmp(pkgtype, "watch", 5)) + apptype = STC_APP_TYPE_WATCH; + else + apptype = STC_APP_TYPE_GUI; + + stc_manager_app_status_changed(status, pid, appid, pkgid, apptype); + + __STC_LOG_FUNC_EXIT__; +handle_error: + FREE(appid); + FREE(pkgid); + FREE(statstr); + FREE(pkgtype); +} + +signal_map_s signal_map[] = { + + /* AMD DBUS */ + { + 0, + AUL_APP_STATUS_DBUS_PATH, + AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE, + AUL_APP_STATUS_DBUS_STATUS_CHANGE, + AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE, + __stc_gdbus_handle_aul_changestate, + NULL + }, + { + 0, + NULL, + NULL, + NULL, + NULL + } +}; + +stc_error_e stc_app_ground_status_monitor_init(stc_s *stc) +{ + guint i = 0; + ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); + + for (i = 0; signal_map[i].member != NULL; i++) { + signal_map[i].sub_id = + stc_manager_gdbus_subscribe_signal(stc->connection, + NULL, + signal_map[i].interface, + signal_map[i].member, + signal_map[i].path, + NULL, + G_DBUS_SIGNAL_FLAGS_NONE, + signal_map[i].callback, + signal_map[i].user_data, + NULL); + STC_LOGI("Successfully subscribed [%s] signal", + signal_map[i].member); + } + + return STC_ERROR_NONE; +} + +stc_error_e stc_app_ground_status_monitor_deinit(stc_s *stc) +{ + guint i = 0; + ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); + + for (i = 0; signal_map[i].member != NULL; i++) { + stc_manager_gdbus_unsubscribe_signal(stc->connection, + signal_map[i].sub_id); + signal_map[i].sub_id = 0; + STC_LOGD("Successfully unsubscribed [%s] signal", + signal_map[i].member); + } + + return STC_ERROR_NONE; +} diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c new file mode 100755 index 0000000..6036098 --- /dev/null +++ b/src/monitor/stc-app-lifecycle.c @@ -0,0 +1,508 @@ +/* + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "stc-app-lifecycle.h" +#include "helper-procfs.h" + +typedef struct { + pid_t pid; +} proc_key_s; + +typedef struct { + char cmdline[PROC_NAME_MAX]; + char status[PROC_BUF_MAX]; +} proc_value_s; + +typedef struct __attribute__ ((aligned(NLMSG_ALIGNTO))) { + struct nlmsghdr nl_hdr; + struct __attribute__ ((__packed__)) { + struct cn_msg cn_msg; + enum proc_cn_mcast_op cn_mcast; + }; +} nl_connector_msg_s; + +typedef struct __attribute__ ((aligned(NLMSG_ALIGNTO))) { + struct nlmsghdr nl_hdr; + struct __attribute__ ((__packed__)) { + struct cn_msg cn_msg; + struct proc_event proc_ev; + }; +} nl_connector_proc_event_s; + +static int nl_connector_sock = -1; +static guint nl_connector_gsource_id = 0; +static GTree *proc_tree; + + +static gboolean __process_nl_connector_message(GIOChannel *source, + GIOCondition condition, + gpointer user_data); + +static int __proc_tree_key_compare(gconstpointer a, gconstpointer b, + gpointer UNUSED user_data) +{ + proc_key_s *key_a = (proc_key_s *)a; + proc_key_s *key_b = (proc_key_s *)b; + + return key_a->pid - key_b->pid; +} + +static void __proc_tree_value_free(gpointer data) +{ + proc_value_s *value = (proc_value_s *)data; + + FREE(value); +} + +static void __proc_tree_key_free(gpointer data) +{ + proc_key_s *key = (proc_key_s *)data; + + FREE(key); +} + +static proc_value_s * __proc_tree_lookup(const proc_key_s *key) +{ + proc_value_s *lookup; + + if (proc_tree == NULL) { + STC_LOGE("tree is null"); + return NULL; + } + + lookup = g_tree_lookup(proc_tree, key); + return lookup; +} + +static void __proc_tree_add(proc_key_s *key, + proc_value_s *value) +{ + if (proc_tree == NULL) { + STC_LOGE("tree is null"); + return; + } + + g_tree_insert(proc_tree, key, value); + + stc_manager_app_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid, + value->cmdline, value->cmdline, + STC_APP_TYPE_SERVICE); +} + +static void __proc_tree_remove(const proc_key_s *key) +{ + if (proc_tree == NULL) { + STC_LOGE("tree is null"); + return; + } + + stc_manager_app_status_changed(STC_CMD_SET_TERMINATED, key->pid, NULL, + NULL, STC_APP_TYPE_NONE); + g_tree_remove(proc_tree, key); +} + +static void __open_nl_connector_sock(void) +{ + __STC_LOG_FUNC_ENTER__; + GIOChannel *gio = NULL; + + if (nl_connector_sock != -1 && + nl_connector_gsource_id != 0) { + STC_LOGE("Socket is already open"); + __STC_LOG_FUNC_EXIT__; + return; + } + + if (nl_connector_sock != -1) { + close(nl_connector_sock); + nl_connector_sock = -1; + } + + if (nl_connector_gsource_id != 0) { + g_source_remove(nl_connector_gsource_id); + nl_connector_gsource_id = 0; + } + + nl_connector_sock = create_netlink(NETLINK_CONNECTOR, CN_IDX_PROC); + if (nl_connector_sock == -1) { + __STC_LOG_FUNC_EXIT__; + return; + } + + gio = g_io_channel_unix_new(nl_connector_sock); + nl_connector_gsource_id = + g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP, + (GIOFunc) __process_nl_connector_message, + NULL); + g_io_channel_unref(gio); + __STC_LOG_FUNC_EXIT__; +} + +static void __close_nl_connector_sock(void) +{ + __STC_LOG_FUNC_ENTER__; + if (nl_connector_sock != -1) { + close(nl_connector_sock); + nl_connector_sock = -1; + } + + if (nl_connector_gsource_id != 0) { + g_source_remove(nl_connector_gsource_id); + nl_connector_gsource_id = 0; + } + __STC_LOG_FUNC_EXIT__; +} + +static void __reopen_nl_connector_sock(void) +{ + __close_nl_connector_sock(); + __open_nl_connector_sock(); +} + +stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, + pid_t pid, + gchar *app_id, + gchar *pkg_id, + stc_app_type_e app_type) +{ + __STC_LOG_FUNC_ENTER__; + stc_error_e ret = STC_ERROR_NONE; + + switch (cmd) { + case STC_CMD_SET_FOREGRD: + { + stc_app_key_s app_key; + stc_app_value_s app_value; + stc_process_key_s proc_key; + stc_process_value_s proc_value; + + memset(&app_key, 0, sizeof(stc_app_key_s)); + memset(&app_value, 0, sizeof(stc_app_value_s)); + memset(&proc_key, 0, sizeof(stc_process_key_s)); + memset(&proc_value, 0, sizeof(stc_process_value_s)); + + app_key.pkg_id = g_strdup(pkg_id); + app_key.app_id = g_strdup(app_id); + + app_value.type = app_type; + app_value.processes = NULL; + + proc_key.pid = pid; + + proc_value.ground = STC_APP_STATE_FOREGROUND; + + stc_monitor_application_add(app_key, app_value); + stc_monitor_process_add(app_key, proc_key, proc_value); + stc_monitor_process_update_ground(app_key, proc_key, + STC_APP_STATE_FOREGROUND); + + FREE(app_key.pkg_id); + FREE(app_key.app_id); + break; + } + case STC_CMD_SET_BACKGRD: + { + stc_app_key_s app_key; + stc_app_value_s app_value; + stc_process_key_s proc_key; + stc_process_value_s proc_value; + + memset(&app_key, 0, sizeof(stc_app_key_s)); + memset(&app_value, 0, sizeof(stc_app_value_s)); + memset(&proc_key, 0, sizeof(stc_process_key_s)); + memset(&proc_value, 0, sizeof(stc_process_value_s)); + + app_key.pkg_id = g_strdup(pkg_id); + app_key.app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, + NULL); + + app_value.type = app_type; + app_value.processes = NULL; + + proc_key.pid = pid; + + proc_value.ground = STC_APP_STATE_BACKGROUND; + + stc_monitor_application_add(app_key, app_value); + stc_monitor_process_add(app_key, proc_key, proc_value); + stc_monitor_process_update_ground(app_key, proc_key, + STC_APP_STATE_BACKGROUND); + + FREE(app_key.pkg_id); + FREE(app_key.app_id); + break; + } + case STC_CMD_SET_SERVICE_LAUNCHED: + { + stc_app_key_s app_key; + stc_app_value_s app_value; + stc_process_key_s proc_key; + stc_process_value_s proc_value; + + memset(&app_key, 0, sizeof(stc_app_key_s)); + memset(&app_value, 0, sizeof(stc_app_value_s)); + memset(&proc_key, 0, sizeof(stc_process_key_s)); + memset(&proc_value, 0, sizeof(stc_process_value_s)); + + app_key.pkg_id = g_strdup(pkg_id); + app_key.app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, + NULL); + + app_value.type = app_type; + app_value.processes = NULL; + + proc_key.pid = pid; + + /* services will run always in background. */ + proc_value.ground = STC_APP_STATE_BACKGROUND; + + stc_monitor_application_add(app_key, app_value); + stc_monitor_process_add(app_key, proc_key, proc_value); + + FREE(app_key.pkg_id); + g_free(app_key.app_id); + break; + } + case STC_CMD_SET_TERMINATED: + { + stc_monitor_process_remove(pid); + break; + } + default: + STC_LOGE("Unhandled command"); + ret = STC_ERROR_INVALID_PARAMETER; + } + + __STC_LOG_FUNC_EXIT__; + return ret; +} + +static void __process_event_exec(int tid, int pid) +{ + char cmdline[PROC_NAME_MAX] = {0, }; + char status[PROC_BUF_MAX] = {0, }; + + if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && + STC_ERROR_NONE == proc_get_status(pid, status, PROC_BUF_MAX)) { + + if (!g_strcmp0(cmdline, "iptables") || !g_strcmp0(cmdline, "ip6tables") || + !g_strcmp0(cmdline, "modprobe") || !g_strcmp0(cmdline, "net-cls-release")) { + return; + } + + proc_key_s *key; + proc_value_s *value; + + key = MALLOC0(proc_key_s, 1); + if (key == NULL) { + STC_LOGE("memory allocation failed"); + return; + } + + value = MALLOC0(proc_value_s, 1); + if (value == NULL) { + STC_LOGE("memory allocation failed"); + FREE(key); + return; + } + + key->pid = pid; + g_strlcpy(value->status, status, sizeof(value->status)); + g_strlcpy(value->cmdline, cmdline, sizeof(value->cmdline)); + + __proc_tree_add(key, value); + + STC_LOGD("EXEC:pid=%d,tgid=%d\t[%s]\t[%s]", pid, tid, status, + cmdline); + } +} + +static void __process_event_exit(int tid, int pid, int exit_code) +{ + proc_key_s key; + proc_value_s *lookup; + + key.pid = pid; + lookup = __proc_tree_lookup(&key); + if (lookup == NULL) /* unmonitored process */ + return; + + __proc_tree_remove(&key); + + STC_LOGD("EXIT:pid=%d,%d ruid=%d,euid=%d", pid, tid, exit_code); +} + +static gboolean __process_nl_connector_message(GIOChannel *source, + GIOCondition condition, + gpointer user_data) +{ + int ret; + int sock = g_io_channel_unix_get_fd(source); + nl_connector_proc_event_s msg; + + if ((condition & G_IO_ERR) || (condition & G_IO_HUP) || + (condition & G_IO_NVAL)) { + /* 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]", + (condition & G_IO_ERR), (condition & G_IO_HUP), + (condition & G_IO_NVAL)); + __reopen_nl_connector_sock(); + __STC_LOG_FUNC_EXIT__; + return FALSE; + } + + memset(&msg, 0, sizeof(nl_connector_proc_event_s)); + + ret = read(sock, &msg, sizeof(nl_connector_proc_event_s)); + if (ret == 0) { + __STC_LOG_FUNC_EXIT__; + return TRUE; + } + + switch (msg.proc_ev.what) { + case PROC_EVENT_EXEC: + __process_event_exec(msg.proc_ev.event_data.exec.process_pid, + msg.proc_ev.event_data.exec.process_tgid); + break; + case PROC_EVENT_EXIT: + __process_event_exit(msg.proc_ev.event_data.exit.process_pid, + msg.proc_ev.event_data.exit.process_tgid, + msg.proc_ev.event_data.exit.exit_code); + break; + default: + ; /* Do nothing */ + } + + return TRUE; +} + +static int __subscribe_proc_events(void) +{ + __STC_LOG_FUNC_ENTER__; + nl_connector_msg_s msg; + int ret; + int sock = nl_connector_sock; + + if (sock == -1) { + __STC_LOG_FUNC_EXIT__; + return -1; + } + + memset(&msg, 0, sizeof(nl_connector_msg_s)); + + msg.nl_hdr.nlmsg_len = sizeof(nl_connector_msg_s); + msg.nl_hdr.nlmsg_pid = getpid(); + msg.nl_hdr.nlmsg_type = NLMSG_DONE; + + msg.cn_msg.id.idx = CN_IDX_PROC; + msg.cn_msg.id.val = CN_VAL_PROC; + msg.cn_msg.len = sizeof(enum proc_cn_mcast_op); + + msg.cn_mcast = PROC_CN_MCAST_LISTEN; + + ret = send(sock, &msg, sizeof(nl_connector_msg_s), 0); + if (ret == -1) { + STC_LOGE("Error sending netlink connector message"); + __STC_LOG_FUNC_EXIT__; + return -1; + } + + __STC_LOG_FUNC_EXIT__; + return 0; +} + +static int __unsubscribe_proc_events(void) +{ + __STC_LOG_FUNC_ENTER__; + nl_connector_msg_s msg; + int ret; + int sock = nl_connector_sock; + + if (sock == -1) { + __STC_LOG_FUNC_EXIT__; + return -1; + } + + memset(&msg, 0, sizeof(nl_connector_msg_s)); + + msg.nl_hdr.nlmsg_len = sizeof(nl_connector_msg_s); + msg.nl_hdr.nlmsg_pid = getpid(); + msg.nl_hdr.nlmsg_type = NLMSG_DONE; + + msg.cn_msg.id.idx = CN_IDX_PROC; + msg.cn_msg.id.val = CN_VAL_PROC; + msg.cn_msg.len = sizeof(enum proc_cn_mcast_op); + + msg.cn_mcast = PROC_CN_MCAST_IGNORE; + + ret = send(sock, &msg, sizeof(nl_connector_msg_s), 0); + if (ret == -1) { + STC_LOGE("Error sending netlink connector message"); + __STC_LOG_FUNC_EXIT__; + return -1; + } + + __STC_LOG_FUNC_EXIT__; + return 0; +} + +void stc_app_lifecycle_monitor_init(void) +{ + __STC_LOG_FUNC_ENTER__; + + proc_tree = g_tree_new_full(__proc_tree_key_compare, NULL, + __proc_tree_key_free, + __proc_tree_value_free); + + /* TODO: Fill proc tree with current procfs state */ + + __open_nl_connector_sock(); + __subscribe_proc_events(); + __STC_LOG_FUNC_EXIT__; +} + +void stc_app_lifecycle_monitor_deinit(void) +{ + __STC_LOG_FUNC_ENTER__; + + if (nl_connector_sock == -1) { + STC_LOGE("socket already closed"); + return; + } + + __unsubscribe_proc_events(); + __close_nl_connector_sock(); + + g_tree_destroy(proc_tree); + proc_tree = NULL; + + __STC_LOG_FUNC_EXIT__; +} diff --git a/src/monitor/stc-application-lifecycle.c b/src/monitor/stc-application-lifecycle.c deleted file mode 100755 index ee82320..0000000 --- a/src/monitor/stc-application-lifecycle.c +++ /dev/null @@ -1,458 +0,0 @@ -/* - * 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 "stc-application-lifecycle.h" - -/* - * AUL - */ -#define AUL_APP_STATUS_DBUS_PATH "/Org/Tizen/Aul/AppStatus" -#define AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE "org.tizen.aul.AppStatus" -#define AUL_APP_STATUS_BUS_NAME AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE - -#define AUL_APP_STATUS_DBUS_LAUNCH_REQUEST "AppLaunch" -#define AUL_APP_STATUS_DBUS_LAUNCH_REQUEST_TYPE "(isss)" - -#define AUL_APP_STATUS_DBUS_STATUS_CHANGE "AppStatusChange" -#define AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE "(issss)" - -#define AUL_APP_STATUS_DBUS_GROUP "AppGroup" -#define AUL_APP_STATUS_DBUS_GROUP_TYPE "(iis)" - -#define AUL_APP_STATUS_DBUS_TERMINATED "AppTerminated" -#define AUL_APP_STATUS_DBUS_TERMINATED_TYPE "(i)" - -typedef enum { - STC_CMD_NONE, - STC_CMD_SET_FOREGRD, - STC_CMD_SET_BACKGRD, - STC_CMD_SET_APP_LAUNCHED, - STC_CMD_SET_SERVICE_LAUNCHED, - STC_CMD_SET_TERMINATED, - STC_CMD_MAX_ELEM -} stc_cmd_type_e; - -typedef struct { - guint sub_id; - const gchar *path; - const gchar *interface; - const gchar *member; - const gchar *param_type; - GDBusSignalCallback callback; - gpointer user_data; -} signal_map_s; - -static stc_error_e __stc_manager_app_status_changed(stc_cmd_type_e cmd, - pid_t pid, - gchar *app_id, - gchar *pkg_id, - stc_app_type_e app_type) -{ - __STC_LOG_FUNC_ENTER__; - stc_error_e ret = STC_ERROR_NONE; - - switch (cmd) { - case STC_CMD_SET_FOREGRD: - { - stc_app_key_s app_key; - stc_app_value_s app_value; - stc_process_key_s proc_key; - stc_process_value_s proc_value; - - memset(&app_key, 0, sizeof(stc_app_key_s)); - memset(&app_value, 0, sizeof(stc_app_value_s)); - memset(&proc_key, 0, sizeof(stc_process_key_s)); - memset(&proc_value, 0, sizeof(stc_process_value_s)); - - app_key.pkg_id = g_strdup(pkg_id); - app_key.app_id = g_strdup(app_id); - - app_value.type = app_type; - app_value.processes = NULL; - - proc_key.pid = pid; - - proc_value.ground = STC_APP_STATE_FOREGROUND; - - stc_monitor_application_add(app_key, app_value); - stc_monitor_process_add(app_key, proc_key, proc_value); - stc_monitor_process_update_ground(app_key, proc_key, - STC_APP_STATE_FOREGROUND); - - FREE(app_key.pkg_id); - FREE(app_key.app_id); - break; - } - case STC_CMD_SET_BACKGRD: - { - stc_app_key_s app_key; - stc_app_value_s app_value; - stc_process_key_s proc_key; - stc_process_value_s proc_value; - - memset(&app_key, 0, sizeof(stc_app_key_s)); - memset(&app_value, 0, sizeof(stc_app_value_s)); - memset(&proc_key, 0, sizeof(stc_process_key_s)); - memset(&proc_value, 0, sizeof(stc_process_value_s)); - - app_key.pkg_id = g_strdup(pkg_id); - app_key.app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, - NULL); - - app_value.type = app_type; - app_value.processes = NULL; - - proc_key.pid = pid; - - proc_value.ground = STC_APP_STATE_BACKGROUND; - - stc_monitor_application_add(app_key, app_value); - stc_monitor_process_add(app_key, proc_key, proc_value); - stc_monitor_process_update_ground(app_key, proc_key, - STC_APP_STATE_BACKGROUND); - - FREE(app_key.pkg_id); - FREE(app_key.app_id); - break; - } - case STC_CMD_SET_APP_LAUNCHED: - { - stc_app_key_s app_key; - stc_app_value_s app_value; - stc_process_key_s proc_key; - stc_process_value_s proc_value; - - memset(&app_key, 0, sizeof(stc_app_key_s)); - memset(&app_value, 0, sizeof(stc_app_value_s)); - memset(&proc_key, 0, sizeof(stc_process_key_s)); - memset(&proc_value, 0, sizeof(stc_process_value_s)); - - app_key.pkg_id = g_strdup(pkg_id); - app_key.app_id = g_strdup(app_id); - - app_value.type = app_type; - app_value.processes = NULL; - - proc_key.pid = pid; - - proc_value.ground = STC_APP_STATE_UNKNOWN; - - /* create foreground cgroup */ - stc_monitor_application_add(app_key, app_value); - stc_monitor_process_add(app_key, proc_key, proc_value); - - /* create background cgroup */ - FREE(app_key.app_id); - - app_key.app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, - NULL); - stc_monitor_application_add(app_key, app_value); - - FREE(app_key.pkg_id); - g_free(app_key.app_id); - break; - } - case STC_CMD_SET_SERVICE_LAUNCHED: - { - stc_app_key_s app_key; - stc_app_value_s app_value; - stc_process_key_s proc_key; - stc_process_value_s proc_value; - - memset(&app_key, 0, sizeof(stc_app_key_s)); - memset(&app_value, 0, sizeof(stc_app_value_s)); - memset(&proc_key, 0, sizeof(stc_process_key_s)); - memset(&proc_value, 0, sizeof(stc_process_value_s)); - - app_key.pkg_id = g_strdup(pkg_id); - app_key.app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, - NULL); - - app_value.type = app_type; - app_value.processes = NULL; - - proc_key.pid = pid; - - /* services will run always in background. */ - proc_value.ground = STC_APP_STATE_BACKGROUND; - - stc_monitor_application_add(app_key, app_value); - stc_monitor_process_add(app_key, proc_key, proc_value); - - FREE(app_key.pkg_id); - g_free(app_key.app_id); - break; - } - case STC_CMD_SET_TERMINATED: - { - stc_monitor_process_remove(pid); - break; - } - default: - STC_LOGE("Unhandled command"); - ret = STC_ERROR_INVALID_PARAMETER; - } - - __STC_LOG_FUNC_EXIT__; - return ret; -} - -static void __stc_gdbus_handle_aul_launch(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - __STC_LOG_FUNC_ENTER__; - pid_t pid; - stc_cmd_type_e status; - stc_app_type_e apptype; - gchar *appid, *pkgid, *pkgtype; - - RETURN_IF_DBUS_TYPE_MISMATCH(parameters, - AUL_APP_STATUS_DBUS_LAUNCH_REQUEST_TYPE); - - g_variant_get(parameters, AUL_APP_STATUS_DBUS_LAUNCH_REQUEST_TYPE, - &pid, &appid, &pkgid, &pkgtype); - - if (!strncmp(pkgtype, "svc", 3)) { - apptype = STC_APP_TYPE_SERVICE; - status = STC_CMD_SET_SERVICE_LAUNCHED; - - } else if (!strncmp(pkgtype, "ui", 2)) { - apptype = STC_APP_TYPE_GUI; - status = STC_CMD_SET_APP_LAUNCHED; - - } else if (!strncmp(pkgtype, "widget", 6)) { - apptype = STC_APP_TYPE_WIDGET; - status = STC_CMD_SET_APP_LAUNCHED; - - } else if (!strncmp(pkgtype, "watch", 5)) { - apptype = STC_APP_TYPE_WATCH; - status = STC_CMD_SET_APP_LAUNCHED; - - } else { - __STC_LOG_FUNC_EXIT__; - goto handle_error; - } - - __stc_manager_app_status_changed(status, pid, appid, pkgid, apptype); - - __STC_LOG_FUNC_EXIT__; -handle_error: - FREE(appid); - FREE(pkgid); - FREE(pkgtype); -} - -static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - __STC_LOG_FUNC_ENTER__; - pid_t pid; - stc_cmd_type_e status; - stc_app_type_e apptype; - gchar *appid, *pkgid, *statstr, *pkgtype; - - RETURN_IF_DBUS_TYPE_MISMATCH(parameters, - AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE); - - g_variant_get(parameters, AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE, - &pid, &appid, &pkgid, &statstr, &pkgtype); - - if (!strncmp(statstr, "fg", 2)) { - status = STC_CMD_SET_FOREGRD; - } else if (!strncmp(statstr, "bg", 2)) { - status = STC_CMD_SET_BACKGRD; - } else { - __STC_LOG_FUNC_EXIT__; - goto handle_error; - } - - if (!strncmp(pkgtype, "svc", 3)) - apptype = STC_APP_TYPE_SERVICE; - else if (!strncmp(pkgtype, "widget", 6)) - apptype = STC_APP_TYPE_WIDGET; - else if (!strncmp(pkgtype, "watch", 5)) - apptype = STC_APP_TYPE_WATCH; - else - apptype = STC_APP_TYPE_GUI; - - __stc_manager_app_status_changed(status, pid, appid, pkgid, apptype); - - __STC_LOG_FUNC_EXIT__; -handle_error: - FREE(appid); - FREE(pkgid); - FREE(statstr); - FREE(pkgtype); -} - -static void __stc_gdbus_handle_aul_group(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - __STC_LOG_FUNC_ENTER__; - pid_t ownerpid, childpid; - gchar *appid; - - RETURN_IF_DBUS_TYPE_MISMATCH(parameters, - AUL_APP_STATUS_DBUS_GROUP_TYPE); - - g_variant_get(parameters, AUL_APP_STATUS_DBUS_GROUP_TYPE, - &ownerpid, &childpid, &appid); - - if (ownerpid == childpid) { - STC_LOGD("Skip merge, when one app %d makes multiple window.", - ownerpid); - __STC_LOG_FUNC_EXIT__; - goto handle_error; - } - - STC_LOGD("received process grouping : owner %d, child %d, previous appid %s", - ownerpid, childpid, appid); - - /** - * TODO: app_set_group(ownerpid, childpid, appid) - */ - - __STC_LOG_FUNC_EXIT__; -handle_error: - FREE(appid); -} - -static void __stc_gdbus_handle_aul_terminated(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - __STC_LOG_FUNC_ENTER__; - pid_t pid; - stc_cmd_type_e status = STC_CMD_SET_TERMINATED; - - RETURN_IF_DBUS_TYPE_MISMATCH(parameters, - AUL_APP_STATUS_DBUS_TERMINATED_TYPE); - - g_variant_get(parameters, AUL_APP_STATUS_DBUS_TERMINATED_TYPE, - &pid); - - __stc_manager_app_status_changed(status, pid, NULL, NULL, - STC_APP_TYPE_NONE); - - __STC_LOG_FUNC_EXIT__; -} - -signal_map_s signal_map[] = { - - /* AMD DBUS */ - { - 0, - AUL_APP_STATUS_DBUS_PATH, - AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE, - AUL_APP_STATUS_DBUS_LAUNCH_REQUEST, - AUL_APP_STATUS_DBUS_LAUNCH_REQUEST_TYPE, - __stc_gdbus_handle_aul_launch, - NULL - }, - { - 0, - AUL_APP_STATUS_DBUS_PATH, - AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE, - AUL_APP_STATUS_DBUS_STATUS_CHANGE, - AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE, - __stc_gdbus_handle_aul_changestate, - NULL - }, - { - 0, - AUL_APP_STATUS_DBUS_PATH, - AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE, - AUL_APP_STATUS_DBUS_GROUP, - AUL_APP_STATUS_DBUS_GROUP_TYPE, - __stc_gdbus_handle_aul_group, - NULL - }, - { - 0, - AUL_APP_STATUS_DBUS_PATH, - AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE, - AUL_APP_STATUS_DBUS_TERMINATED, - AUL_APP_STATUS_DBUS_TERMINATED_TYPE, - __stc_gdbus_handle_aul_terminated, - NULL - }, - { - 0, - NULL, - NULL, - NULL, - NULL - } -}; - -stc_error_e stc_application_lifecycle_monitor_init(stc_s *stc) -{ - guint i = 0; - ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); - - for (i = 0; signal_map[i].member != NULL; i++) { - signal_map[i].sub_id = - stc_manager_gdbus_subscribe_signal(stc->connection, - NULL, - signal_map[i].interface, - signal_map[i].member, - signal_map[i].path, - NULL, - G_DBUS_SIGNAL_FLAGS_NONE, - signal_map[i].callback, - signal_map[i].user_data, - NULL); - STC_LOGI("Successfully subscribed [%s] signal", - signal_map[i].member); - } - - return STC_ERROR_NONE; -} - -stc_error_e stc_application_lifecycle_monitor_deinit(stc_s *stc) -{ - guint i = 0; - ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); - - for (i = 0; signal_map[i].member != NULL; i++) { - stc_manager_gdbus_unsubscribe_signal(stc->connection, - signal_map[i].sub_id); - signal_map[i].sub_id = 0; - STC_LOGD("Successfully unsubscribed [%s] signal", - signal_map[i].member); - } - - return STC_ERROR_NONE; -} diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index 3bd6659..31a808d 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -18,7 +18,7 @@ #include "stc-statistics.h" #include "stc-restriction.h" #include "stc-default-connection.h" -#include "stc-application-lifecycle.h" +#include "stc-app-ground-status.h" static gboolean __stc_manager_gdbus_statistics_init(stc_s *stc) { @@ -153,7 +153,7 @@ static void __stc_manager_gdbus_on_bus_acquired(GDBusConnection *connection, g_dbus_object_manager_server_set_connection(stc->obj_mgr, stc->connection); - stc_application_lifecycle_monitor_init(stc); + stc_app_ground_status_monitor_init(stc); stc_default_connection_monitor_init(stc); __STC_LOG_FUNC_EXIT__; @@ -194,7 +194,7 @@ void stc_manager_gdbus_deinit(gpointer stc_data) { __STC_LOG_FUNC_ENTER__; stc_s *stc = (stc_s *)stc_data; - stc_application_lifecycle_monitor_deinit(stc); + stc_app_ground_status_monitor_deinit(stc); stc_default_connection_monitor_deinit(stc); g_bus_unown_name(stc->gdbus_owner_id); stc->statistics_obj = NULL; diff --git a/src/stc-manager.c b/src/stc-manager.c index dc923ff..0b91ce4 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -27,6 +27,7 @@ #include "helper-nfacct-rule.h" #include "stc-monitor.h" #include "stc-manager-plugin.h" +#include "stc-app-lifecycle.h" static stc_s *g_stc = NULL; @@ -39,6 +40,7 @@ static void __stc_manager_deinit(void) return; } + stc_app_lifecycle_monitor_deinit(); stc_monitor_deinit(); stc_deinit_db_guard(); stc_db_deinitialize(); @@ -73,6 +75,7 @@ static stc_s *__stc_manager_init(void) stc_manager_gdbus_init((gpointer)stc); stc_manager_plugin_init(); + stc_app_lifecycle_monitor_init(); STC_LOGI("stc manager initialized"); __STC_LOG_FUNC_EXIT__; -- 2.7.4 From d72639ea51475f7c789d45016d77eed2b917473b Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 14 Sep 2017 09:31:10 +0530 Subject: [PATCH 09/16] Moved application ground status monitor to plugin. Change-Id: Ie9680b6d25b73b8262529d02a7885e86396dfc32 Signed-off-by: Nishant Chaprana --- include/stc-manager-gdbus.h | 10 -- include/stc-manager-plugin.h | 7 ++ include/stc-manager.h | 10 ++ include/stc-plugin.h | 22 +++- packaging/stc-manager.spec | 2 +- plugin/CMakeLists.txt | 1 + plugin/stc-plugin.c | 174 ++++++++++++++++++++++++--- src/monitor/include/stc-app-ground-status.h | 29 ----- src/monitor/include/stc-app-lifecycle.h | 11 -- src/monitor/include/stc-default-connection.h | 1 - src/monitor/stc-app-ground-status.c | 147 ---------------------- src/monitor/stc-app-lifecycle.c | 1 + src/monitor/stc-default-connection.c | 1 + src/stc-manager-gdbus.c | 10 +- src/stc-manager-plugin.c | 70 +++++++++-- src/stc-manager.c | 7 +- 16 files changed, 268 insertions(+), 235 deletions(-) delete mode 100755 src/monitor/include/stc-app-ground-status.h delete mode 100755 src/monitor/stc-app-ground-status.c diff --git a/include/stc-manager-gdbus.h b/include/stc-manager-gdbus.h index cafca77..045a70c 100755 --- a/include/stc-manager-gdbus.h +++ b/include/stc-manager-gdbus.h @@ -50,16 +50,6 @@ } while (0) -#define RETURN_IF_DBUS_TYPE_MISMATCH(parameters, str) do { \ - DEBUG_PARAM_TYPE(parameters); \ - DEBUG_PARAMS(parameters); \ - if (g_strcmp0(g_variant_get_type_string(parameters), str)) { \ - STC_LOGE("Dbus type not matching, do not process"); \ - __STC_LOG_FUNC_EXIT__; \ - return; \ - } \ -} while (0) - typedef void(*dbus_dict_cb)(const char *key, GVariant *value, void *user_data); diff --git a/include/stc-manager-plugin.h b/include/stc-manager-plugin.h index 6eae115..f4233da 100755 --- a/include/stc-manager-plugin.h +++ b/include/stc-manager-plugin.h @@ -19,6 +19,8 @@ #define STC_PLUGIN_FILEPATH "/usr/lib/stc-manager-plugin.so" +#include "stc-plugin.h" + void stc_manager_plugin_init(void); void stc_manager_plugin_deinit(void); @@ -27,4 +29,9 @@ int stc_send_warn_message_to_net_popup(const char *content, int stc_send_restriction_message_to_net_popup(const char *content, const char *type, const char *app_id, const char *iftype, const char *limit); +int stc_register_state_changed_cb(stc_s *stc, + stc_plugin_app_state_changed_cb cb, + void *data); +int stc_deregister_state_changed_cb(stc_s *stc); + #endif /* __STC_MANAGER_PLUGIN_H__ */ diff --git a/include/stc-manager.h b/include/stc-manager.h index 160da59..990db68 100755 --- a/include/stc-manager.h +++ b/include/stc-manager.h @@ -36,6 +36,16 @@ typedef enum { STC_CONTINUE = 1, /**< continue */ } stc_cb_ret_e; +typedef enum { + STC_CMD_NONE, + STC_CMD_SET_FOREGRD, + STC_CMD_SET_BACKGRD, + STC_CMD_SET_APP_LAUNCHED, + STC_CMD_SET_SERVICE_LAUNCHED, + STC_CMD_SET_TERMINATED, + STC_CMD_MAX_ELEM +} stc_cmd_type_e; + /** * @brief Monitored application types */ diff --git a/include/stc-plugin.h b/include/stc-plugin.h index 3bbcedb..4d843b8 100755 --- a/include/stc-plugin.h +++ b/include/stc-plugin.h @@ -18,12 +18,30 @@ #define __STC_PLUGIN_H__ #include +#include "stc-error.h" +#include "stc-manager.h" + +typedef stc_error_e (*stc_plugin_app_state_changed_cb)(stc_cmd_type_e cmd, + pid_t pid, + const gchar *app_id, + const gchar *pkg_id, + stc_app_type_e app_type); typedef struct { int(*send_restriction_message_to_net_popup) (const char *, - const char *, const char *, const char *, const char *); + const char *, + const char *, + const char *, + const char *); int(*send_warn_message_to_net_popup) (const char *, - const char *, const char *, const char *, const char *); + const char *, + const char *, + const char *, + const char *); + int (*register_state_changed_cb) (stc_s *stc, + stc_plugin_app_state_changed_cb cb, + void *data); + int (*deregister_state_changed_cb) (stc_s *stc); } stc_plugin_s; #endif /* __STC_PLUGIN_H__ */ diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 66b9d54..e08e1cb 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.29 +Version: 0.0.30 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt index 74cab33..3e5dcda 100755 --- a/plugin/CMakeLists.txt +++ b/plugin/CMakeLists.txt @@ -17,6 +17,7 @@ FOREACH(flag ${pkgs_plugin_CFLAGS}) ENDFOREACH(flag) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/monitor/include) # INCLUDE_DIRECTORIES(SRCS include) SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -g -Werror -fvisibility=hidden") diff --git a/plugin/stc-plugin.c b/plugin/stc-plugin.c index 13f0306..a566388 100755 --- a/plugin/stc-plugin.c +++ b/plugin/stc-plugin.c @@ -22,34 +22,152 @@ #include #include #include +#include #include "stc-plugin.h" +#include "stc-manager.h" +#include "stc-manager-util.h" -#ifdef USE_DLOG -#include +/* + * AUL + */ +#define AUL_APP_STATUS_DBUS_PATH "/Org/Tizen/Aul/AppStatus" +#define AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE "org.tizen.aul.AppStatus" +#define AUL_APP_STATUS_BUS_NAME AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE + +#define AUL_APP_STATUS_DBUS_STATUS_CHANGE "AppStatusChange" +#define AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE "(issss)" + +typedef struct { + guint sub_id; + const gchar *path; + const gchar *interface; + const gchar *member; + const gchar *param_type; + GDBusSignalCallback callback; + gpointer user_data; +} signal_map_s; + +stc_error_e(*state_changed_cb)(stc_cmd_type_e cmd, pid_t pid, + const gchar *app_id, const gchar *pkg_id, + stc_app_type_e app_type); + +static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + __STC_LOG_FUNC_ENTER__; + pid_t pid; + stc_cmd_type_e status; + stc_app_type_e apptype; + gchar *appid, *pkgid, *statstr, *pkgtype; + + if (g_strcmp0(g_variant_get_type_string(parameters), + AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE)) { + STC_LOGE("Dbus type not matching, do not process"); + __STC_LOG_FUNC_EXIT__; + return; + } + + g_variant_get(parameters, AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE, + &pid, &appid, &pkgid, &statstr, &pkgtype); + + if (!strncmp(statstr, "fg", 2)) { + status = STC_CMD_SET_FOREGRD; + } else if (!strncmp(statstr, "bg", 2)) { + status = STC_CMD_SET_BACKGRD; + } else { + __STC_LOG_FUNC_EXIT__; + goto out; + } + + if (!strncmp(pkgtype, "svc", 3)) + apptype = STC_APP_TYPE_SERVICE; + else if (!strncmp(pkgtype, "widget", 6)) + apptype = STC_APP_TYPE_WIDGET; + else if (!strncmp(pkgtype, "watch", 5)) + apptype = STC_APP_TYPE_WATCH; + else + apptype = STC_APP_TYPE_GUI; + + if (state_changed_cb) + state_changed_cb(status, pid, appid, pkgid, apptype); + + __STC_LOG_FUNC_EXIT__; +out: + FREE(appid); + FREE(pkgid); + FREE(statstr); + FREE(pkgtype); +} -#undef LOG_TAG -#define LOG_TAG "STC_MANAGER" +signal_map_s signal_map[] = { + + /* AMD DBUS */ + { + 0, + AUL_APP_STATUS_DBUS_PATH, + AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE, + AUL_APP_STATUS_DBUS_STATUS_CHANGE, + AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE, + __stc_gdbus_handle_aul_changestate, + NULL + }, + { + 0, + NULL, + NULL, + NULL, + NULL + } +}; -#define STC_LOGD(format, args...) LOGD(format, ##args) -#define STC_LOGI(format, args...) LOGI(format, ##args) -#define STC_LOGW(format, args...) LOGW(format, ##args) -#define STC_LOGE(format, args...) LOGE(format, ##args) -#define __STC_LOG_FUNC_ENTER__ LOGD("Enter") -#define __STC_LOG_FUNC_EXIT__ LOGD("Quit") +static stc_error_e __ground_status_monitor_init(stc_s *stc) +{ + guint i = 0; + + ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); + + for (i = 0; signal_map[i].member != NULL; i++) { + signal_map[i].sub_id = + g_dbus_connection_signal_subscribe(stc->connection, + NULL, + signal_map[i].interface, + signal_map[i].member, + signal_map[i].path, + NULL, + G_DBUS_SIGNAL_FLAGS_NONE, + signal_map[i].callback, + signal_map[i].user_data, + NULL); + STC_LOGI("Successfully subscribed [%s] signal", + signal_map[i].member); + } + + return STC_ERROR_NONE; +} -#else /* USE_DLOG */ +static stc_error_e __ground_status_monitor_deinit(stc_s *stc) +{ + guint i = 0; -#define STC_LOGD(format, args...) -#define STC_LOGI(format, args...) -#define STC_LOGW(format, args...) -#define STC_LOGE(format, args...) + ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); -#define __STC_LOG_FUNC_ENTER__ -#define __STC_LOG_FUNC_EXIT__ + for (i = 0; signal_map[i].member != NULL; i++) { + g_dbus_connection_signal_unsubscribe(stc->connection, + signal_map[i].sub_id); + signal_map[i].sub_id = 0; + STC_LOGD("Successfully unsubscribed [%s] signal", + signal_map[i].member); + } -#endif /* USE_DLOG */ + return STC_ERROR_NONE; +} static int __stc_send_warn_message_to_net_popup(const char *content, const char *type, const char *app_id, const char *iftype, const char *warn) @@ -95,11 +213,29 @@ static int __stc_send_restriction_message_to_net_popup(const char *content, return ret; } +static int __register_state_changed_cb(stc_s *stc, + stc_plugin_app_state_changed_cb cb, + void *data) +{ + state_changed_cb = cb; + __ground_status_monitor_init(stc); + + return 0; +} + +int __deregister_state_changed_cb(stc_s *stc) +{ + state_changed_cb = NULL; + __ground_status_monitor_deinit(stc); + return 0; +} API stc_plugin_s stc_plugin = { .send_warn_message_to_net_popup = __stc_send_warn_message_to_net_popup, .send_restriction_message_to_net_popup = - __stc_send_restriction_message_to_net_popup + __stc_send_restriction_message_to_net_popup, + .register_state_changed_cb = __register_state_changed_cb, + .deregister_state_changed_cb = __deregister_state_changed_cb }; diff --git a/src/monitor/include/stc-app-ground-status.h b/src/monitor/include/stc-app-ground-status.h deleted file mode 100755 index fb02abc..0000000 --- a/src/monitor/include/stc-app-ground-status.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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_APP_GROUND_STATUS_H__ -#define __STC_APP_GROUND_STATUS_H__ - -#include "stc-error.h" -#include "stc-manager.h" -#include "stc-manager-gdbus.h" -#include "stc-manager-util.h" -#include "stc-monitor.h" - -stc_error_e stc_app_ground_status_monitor_init(stc_s *stc); -stc_error_e stc_app_ground_status_monitor_deinit(stc_s *stc); - -#endif /* __STC_APP_GROUND_STATUS_H__ */ diff --git a/src/monitor/include/stc-app-lifecycle.h b/src/monitor/include/stc-app-lifecycle.h index fae72ec..3946c3e 100755 --- a/src/monitor/include/stc-app-lifecycle.h +++ b/src/monitor/include/stc-app-lifecycle.h @@ -19,20 +19,9 @@ #include "stc-error.h" #include "stc-manager.h" -#include "stc-manager-gdbus.h" #include "stc-manager-util.h" #include "stc-monitor.h" -typedef enum { - STC_CMD_NONE, - STC_CMD_SET_FOREGRD, - STC_CMD_SET_BACKGRD, - STC_CMD_SET_APP_LAUNCHED, - STC_CMD_SET_SERVICE_LAUNCHED, - STC_CMD_SET_TERMINATED, - STC_CMD_MAX_ELEM -} stc_cmd_type_e; - stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, pid_t pid, gchar *app_id, diff --git a/src/monitor/include/stc-default-connection.h b/src/monitor/include/stc-default-connection.h index 861e58d..b3326de 100755 --- a/src/monitor/include/stc-default-connection.h +++ b/src/monitor/include/stc-default-connection.h @@ -20,7 +20,6 @@ #include #include "stc-error.h" #include "stc-manager.h" -#include "stc-manager-gdbus.h" #include "stc-manager-util.h" #define IMSI_LENGTH 16 diff --git a/src/monitor/stc-app-ground-status.c b/src/monitor/stc-app-ground-status.c deleted file mode 100755 index a5c24ec..0000000 --- a/src/monitor/stc-app-ground-status.c +++ /dev/null @@ -1,147 +0,0 @@ -/* - * 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 "stc-app-lifecycle.h" -#include "stc-app-ground-status.h" - -/* - * AUL - */ -#define AUL_APP_STATUS_DBUS_PATH "/Org/Tizen/Aul/AppStatus" -#define AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE "org.tizen.aul.AppStatus" -#define AUL_APP_STATUS_BUS_NAME AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE - -#define AUL_APP_STATUS_DBUS_STATUS_CHANGE "AppStatusChange" -#define AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE "(issss)" - -typedef struct { - guint sub_id; - const gchar *path; - const gchar *interface; - const gchar *member; - const gchar *param_type; - GDBusSignalCallback callback; - gpointer user_data; -} signal_map_s; - -static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - __STC_LOG_FUNC_ENTER__; - pid_t pid; - stc_cmd_type_e status; - stc_app_type_e apptype; - gchar *appid, *pkgid, *statstr, *pkgtype; - - RETURN_IF_DBUS_TYPE_MISMATCH(parameters, - AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE); - - g_variant_get(parameters, AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE, - &pid, &appid, &pkgid, &statstr, &pkgtype); - - if (!strncmp(statstr, "fg", 2)) { - status = STC_CMD_SET_FOREGRD; - } else if (!strncmp(statstr, "bg", 2)) { - status = STC_CMD_SET_BACKGRD; - } else { - __STC_LOG_FUNC_EXIT__; - goto handle_error; - } - - if (!strncmp(pkgtype, "svc", 3)) - apptype = STC_APP_TYPE_SERVICE; - else if (!strncmp(pkgtype, "widget", 6)) - apptype = STC_APP_TYPE_WIDGET; - else if (!strncmp(pkgtype, "watch", 5)) - apptype = STC_APP_TYPE_WATCH; - else - apptype = STC_APP_TYPE_GUI; - - stc_manager_app_status_changed(status, pid, appid, pkgid, apptype); - - __STC_LOG_FUNC_EXIT__; -handle_error: - FREE(appid); - FREE(pkgid); - FREE(statstr); - FREE(pkgtype); -} - -signal_map_s signal_map[] = { - - /* AMD DBUS */ - { - 0, - AUL_APP_STATUS_DBUS_PATH, - AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE, - AUL_APP_STATUS_DBUS_STATUS_CHANGE, - AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE, - __stc_gdbus_handle_aul_changestate, - NULL - }, - { - 0, - NULL, - NULL, - NULL, - NULL - } -}; - -stc_error_e stc_app_ground_status_monitor_init(stc_s *stc) -{ - guint i = 0; - ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); - - for (i = 0; signal_map[i].member != NULL; i++) { - signal_map[i].sub_id = - stc_manager_gdbus_subscribe_signal(stc->connection, - NULL, - signal_map[i].interface, - signal_map[i].member, - signal_map[i].path, - NULL, - G_DBUS_SIGNAL_FLAGS_NONE, - signal_map[i].callback, - signal_map[i].user_data, - NULL); - STC_LOGI("Successfully subscribed [%s] signal", - signal_map[i].member); - } - - return STC_ERROR_NONE; -} - -stc_error_e stc_app_ground_status_monitor_deinit(stc_s *stc) -{ - guint i = 0; - ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); - - for (i = 0; signal_map[i].member != NULL; i++) { - stc_manager_gdbus_unsubscribe_signal(stc->connection, - signal_map[i].sub_id); - signal_map[i].sub_id = 0; - STC_LOGD("Successfully unsubscribed [%s] signal", - signal_map[i].member); - } - - return STC_ERROR_NONE; -} diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index 6036098..3192c28 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -27,6 +27,7 @@ #include #include +#include "stc-manager-gdbus.h" #include "stc-app-lifecycle.h" #include "helper-procfs.h" diff --git a/src/monitor/stc-default-connection.c b/src/monitor/stc-default-connection.c index 8b32a3e..e524970 100755 --- a/src/monitor/stc-default-connection.c +++ b/src/monitor/stc-default-connection.c @@ -17,6 +17,7 @@ #include #include "stc-monitor.h" +#include "stc-manager-gdbus.h" #include "stc-default-connection.h" /* connman service dbus details */ diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index 31a808d..aee4c9e 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -18,7 +18,8 @@ #include "stc-statistics.h" #include "stc-restriction.h" #include "stc-default-connection.h" -#include "stc-app-ground-status.h" +#include "stc-manager-plugin.h" +#include "stc-app-lifecycle.h" static gboolean __stc_manager_gdbus_statistics_init(stc_s *stc) { @@ -153,8 +154,8 @@ static void __stc_manager_gdbus_on_bus_acquired(GDBusConnection *connection, g_dbus_object_manager_server_set_connection(stc->obj_mgr, stc->connection); - stc_app_ground_status_monitor_init(stc); stc_default_connection_monitor_init(stc); + stc_register_state_changed_cb(stc, stc_manager_app_status_changed, NULL); __STC_LOG_FUNC_EXIT__; } @@ -194,9 +195,12 @@ void stc_manager_gdbus_deinit(gpointer stc_data) { __STC_LOG_FUNC_ENTER__; stc_s *stc = (stc_s *)stc_data; - stc_app_ground_status_monitor_deinit(stc); + + stc_deregister_state_changed_cb(stc); stc_default_connection_monitor_deinit(stc); + g_bus_unown_name(stc->gdbus_owner_id); + stc->statistics_obj = NULL; stc->restriction_obj = NULL; __STC_LOG_FUNC_EXIT__; diff --git a/src/stc-manager-plugin.c b/src/stc-manager-plugin.c index d25b1a8..9a8d96b 100755 --- a/src/stc-manager-plugin.c +++ b/src/stc-manager-plugin.c @@ -31,6 +31,7 @@ void stc_manager_plugin_init(void) handle_plugin = dlopen(STC_PLUGIN_FILEPATH, RTLD_NOW); if (!handle_plugin) { STC_LOGE("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror()); + __STC_LOG_FUNC_EXIT__; return; } @@ -38,11 +39,11 @@ void stc_manager_plugin_init(void) if (!stc_plugin) { STC_LOGE("Can't load symbol: %s", dlerror()); dlclose(handle_plugin); + __STC_LOG_FUNC_EXIT__; return; } stc_plugin_enabled = TRUE; - __STC_LOG_FUNC_EXIT__; } @@ -50,48 +51,101 @@ void stc_manager_plugin_deinit(void) { __STC_LOG_FUNC_ENTER__; - if (!stc_plugin_enabled) + if (!stc_plugin_enabled) { + __STC_LOG_FUNC_EXIT__; return; + } stc_plugin_enabled = FALSE; dlclose(handle_plugin); - __STC_LOG_FUNC_EXIT__; } -int stc_send_warn_message_to_net_popup(const char *content, - const char *type, const char *app_id, const char *iftype, const char *warn) +int stc_send_warn_message_to_net_popup(const char *content, const char *type, + const char *app_id, const char *iftype, + const char *warn) { __STC_LOG_FUNC_ENTER__; if (!stc_plugin_enabled) { STC_LOGE("Plugin wasn't enabled"); + __STC_LOG_FUNC_EXIT__; return 0; } if (!stc_plugin) { STC_LOGE("Plugin wasn't loaded"); + __STC_LOG_FUNC_EXIT__; return 0; } - return stc_plugin->send_warn_message_to_net_popup(content, type, app_id, iftype, warn); + __STC_LOG_FUNC_EXIT__; + return stc_plugin->send_warn_message_to_net_popup(content, type, app_id, + iftype, warn); } int stc_send_restriction_message_to_net_popup(const char *content, - const char *type, const char *app_id, const char *iftype, const char *limit) + const char *type, + const char *app_id, + const char *iftype, + const char *limit) { __STC_LOG_FUNC_ENTER__; if (!stc_plugin_enabled) { STC_LOGE("Plugin wasn't enabled"); + __STC_LOG_FUNC_EXIT__; return 0; } if (!stc_plugin) { STC_LOGE("Plugin wasn't loaded"); + __STC_LOG_FUNC_EXIT__; return 0; } - return stc_plugin->send_restriction_message_to_net_popup(content, type, app_id, iftype, limit); + __STC_LOG_FUNC_EXIT__; + return stc_plugin->send_restriction_message_to_net_popup(content, type, + app_id, iftype, + limit); } +int stc_register_state_changed_cb(stc_s *stc, + stc_plugin_app_state_changed_cb cb, + void *data) +{ + __STC_LOG_FUNC_ENTER__; + + if (!stc_plugin_enabled) { + __STC_LOG_FUNC_EXIT__; + STC_LOGE("Plugin wasn't enabled"); + return 0; + } + + if (!stc_plugin) { + STC_LOGE("Plugin wasn't loaded"); + __STC_LOG_FUNC_EXIT__; + return 0; + } + + return stc_plugin->register_state_changed_cb(stc, cb, data); +} +int stc_deregister_state_changed_cb(stc_s *stc) +{ + __STC_LOG_FUNC_ENTER__; + + if (!stc_plugin_enabled) { + __STC_LOG_FUNC_EXIT__; + STC_LOGE("Plugin wasn't enabled"); + return 0; + } + + if (!stc_plugin) { + __STC_LOG_FUNC_EXIT__; + STC_LOGE("Plugin wasn't loaded"); + return 0; + } + __STC_LOG_FUNC_EXIT__; + + return stc_plugin->deregister_state_changed_cb(stc); +} diff --git a/src/stc-manager.c b/src/stc-manager.c index 0b91ce4..cfee380 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -17,8 +17,6 @@ #include #include "stc-manager.h" #include "stc-emulator.h" -#include "stc-statistics.h" -#include "stc-restriction.h" #include "stc-manager-gdbus.h" #include "stc-db.h" #include "counter.h" @@ -40,11 +38,12 @@ static void __stc_manager_deinit(void) return; } - stc_app_lifecycle_monitor_deinit(); stc_monitor_deinit(); stc_deinit_db_guard(); stc_db_deinitialize(); + stc_manager_gdbus_deinit((gpointer)g_stc); + stc_app_lifecycle_monitor_deinit(); stc_manager_plugin_deinit(); STC_LOGI("stc manager deinitialized"); @@ -73,9 +72,9 @@ static stc_s *__stc_manager_init(void) if (err != STC_ERROR_NONE) goto handle_error; - stc_manager_gdbus_init((gpointer)stc); stc_manager_plugin_init(); stc_app_lifecycle_monitor_init(); + stc_manager_gdbus_init((gpointer)stc); STC_LOGI("stc manager initialized"); __STC_LOG_FUNC_EXIT__; -- 2.7.4 From 9750c77626856b02614dee2dabb0be60c6e56067 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 6 Sep 2017 15:54:56 +0900 Subject: [PATCH 10/16] Added exception lists for monitoring Change-Id: I4f010db523cc8421a2cabcc62f868db976138597 Signed-off-by: hyunuktak --- data/exceptions | 31 +++++++++++++++++ data/traffic_db.sql | 1 - packaging/stc-manager.spec | 9 ++++- src/CMakeLists.txt | 4 +-- src/monitor/include/stc-exception.h | 32 ++++++++++++++++++ src/monitor/include/stc-monitor.h | 3 ++ src/monitor/stc-app-lifecycle.c | 14 ++++++-- src/monitor/stc-exception.c | 66 +++++++++++++++++++++++++++++++++++++ src/monitor/stc-monitor.c | 57 ++++++++++++++++++++++++++++++++ 9 files changed, 211 insertions(+), 6 deletions(-) create mode 100644 data/exceptions create mode 100755 src/monitor/include/stc-exception.h create mode 100755 src/monitor/stc-exception.c diff --git a/data/exceptions b/data/exceptions new file mode 100644 index 0000000..67ab00e --- /dev/null +++ b/data/exceptions @@ -0,0 +1,31 @@ +bash:inst +cd:inst +cp:inst +mv:inst +rm:inst +mkdir:inst +rmdir:inst +cat:inst +more:inst +touch:inst +find:inst +ls:inst +sed:inst +rpm:inst +sh:inst +sleep:inst +grep:inst +killall:inst +systemctl:inst +wlan.sh:script +wpa_supp.sh:script +dlogutil:sys +wifi-loader:sys +launchpad-loader:sys +wrt-loader:sys +dotnet-launcher:sys +volume:app +iptables:app +ip6tables:app +modprobe:inst +net-cls-release:app diff --git a/data/traffic_db.sql b/data/traffic_db.sql index fda62f8..3fae3f6 100644 --- a/data/traffic_db.sql +++ b/data/traffic_db.sql @@ -34,4 +34,3 @@ CREATE TABLE IF NOT EXISTS counters ( data_counter BIGINT, PRIMARY KEY (restriction_id) ); - diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index e08e1cb..d1befbe 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.30 +Version: 0.0.31 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 @@ -71,6 +71,10 @@ rm -rf %{buildroot} rm %{buildroot}/usr/share/traffic_db.sql %endif +#Exceptions file +mkdir -p %{buildroot}/%{_localstatedir}/lib/stc +cp data/exceptions %{buildroot}/%{_localstatedir}/lib/stc/exceptions + #Systemd service file mkdir -p %{buildroot}%{_libdir}/systemd/system/ cp resources/systemd/stc-manager.service %{buildroot}%{_libdir}/systemd/system/stc-manager.service @@ -91,6 +95,9 @@ cp resources/dbus/stc-manager.conf %{buildroot}%{_sysconfdir}/dbus-1/system.d/st %attr(644,root,root) %{_libdir}/systemd/system/stc-manager.service %attr(644,root,root) %{_libdir}/systemd/system/multi-user.target.wants/stc-manager.service +%attr(755,root,root) /%{_localstatedir}/lib/stc +%attr(600,root,root) /%{_localstatedir}/lib/stc/exceptions + #DBus DAC %attr(644,root,root) %{_sysconfdir}/dbus-1/system.d/* diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 736cec3..1ea3658 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -46,12 +46,12 @@ INCLUDE_DIRECTORIES(${LIMITATION_SOURCE_DIR}) INCLUDE_DIRECTORIES(${LIMITATION_SOURCE_DIR}/include) FILE(GLOB SOURCE_SRCS ${SOURCE_DIR}/*.c) -FILE(GLOB HELPER_SOURCE_SRCS ${HELPER_SOURCE_DIR}/*.c) +FILE(GLOB HELPER_SRCS ${HELPER_SOURCE_DIR}/*.c) FILE(GLOB MONITOR_SRCS ${MONITOR_SOURCE_DIR}/*.c) FILE(GLOB CONFIGURE_SRCS ${CONFIGURE_SOURCE_DIR}/*.c) FILE(GLOB LIMITATION_SRCS ${LIMITATION_SOURCE_DIR}/*.c) -SET(SRCS ${SRCS} ${SOURCE_SRCS} ${HELPER_SOURCE_SRCS} ${MONITOR_SRCS} ${CONFIGURE_SRCS} ${LIMITATION_SRCS}) +SET(SRCS ${SRCS} ${SOURCE_SRCS} ${HELPER_SRCS} ${MONITOR_SRCS} ${CONFIGURE_SRCS} ${LIMITATION_SRCS}) IF("${ENABLE_DATABASE}" STREQUAL "YES") FILE(GLOB DATABASE_SRCS ${DATABASE_SOURCE_DIR}/*.c) diff --git a/src/monitor/include/stc-exception.h b/src/monitor/include/stc-exception.h new file mode 100755 index 0000000..da951ae --- /dev/null +++ b/src/monitor/include/stc-exception.h @@ -0,0 +1,32 @@ +/* + * 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_EXCEPTION_H__ +#define __STC_EXCEPTION_H__ + +typedef struct { + char *process_name; + char *exe_type; +} table_exceptions_info; + +typedef stc_cb_ret_e +(*table_exceptions_info_cb)(const table_exceptions_info *info, + void *user_data); + +stc_error_e table_exceptions_foreach(table_exceptions_info_cb info_cb, + void *user_data); + +#endif /*__STC_EXCEPTION_H__ */ diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index 56d87e2..6601489 100755 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -100,6 +100,7 @@ typedef struct { gboolean rstns_tree_updated; GTree *apps; /**< monitored applications */ gboolean apps_tree_updated; + GHashTable *excns_hash; /**< exception hash table */ guint background_state; } stc_system_s; @@ -144,6 +145,8 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info); stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info); +stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline); + int stc_monitor_get_counter_socket(void); #endif /* __STC_MONITOR_H__ */ diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index 3192c28..829c989 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -128,6 +128,16 @@ static void __proc_tree_remove(const proc_key_s *key) g_tree_remove(proc_tree, key); } +static gboolean __check_excn(char *cmdline) +{ + stc_error_e ret = stc_monitor_check_excn_by_cmdline(cmdline); + + if (ret == STC_ERROR_NO_DATA) + return FALSE; + else + return TRUE; +} + static void __open_nl_connector_sock(void) { __STC_LOG_FUNC_ENTER__; @@ -312,8 +322,8 @@ static void __process_event_exec(int tid, int pid) if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && STC_ERROR_NONE == proc_get_status(pid, status, PROC_BUF_MAX)) { - if (!g_strcmp0(cmdline, "iptables") || !g_strcmp0(cmdline, "ip6tables") || - !g_strcmp0(cmdline, "modprobe") || !g_strcmp0(cmdline, "net-cls-release")) { + if (__check_excn(cmdline)) { + STC_LOGI("[%s] monitoring is excepted", cmdline); return; } diff --git a/src/monitor/stc-exception.c b/src/monitor/stc-exception.c new file mode 100755 index 0000000..87c3eeb --- /dev/null +++ b/src/monitor/stc-exception.c @@ -0,0 +1,66 @@ +/* + * 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. + */ + +/** + * This file implements exceptions entity handler methods. + * + * @file stc-exception.c + */ + +#include "stc-manager.h" +#include "stc-exception.h" + +#define EXCEPTION_BUF_MAX 64 +#define EXCEPTION_STORAGE "/var/lib/stc/exceptions" + +stc_error_e table_exceptions_foreach(const table_exceptions_info_cb exception_cb, + void *user_data) +{ + __STC_LOG_FUNC_ENTER__; + + stc_error_e error_code = STC_ERROR_NONE; + table_exceptions_info data; + + FILE *fp = NULL; + char buf[EXCEPTION_BUF_MAX] = {0, }; + + fp = fopen(EXCEPTION_STORAGE, "r"); + ret_value_msg_if(!fp, STC_ERROR_FAIL, "Failed to open %s file"); + + while (fgets(buf, sizeof(buf), fp) != NULL) { + char *process_name, *exe_type; + char *save_ptr = NULL; + + process_name = strtok_r(buf, ":", &save_ptr); + if (process_name != NULL) + data.process_name = process_name; + else + data.process_name = "none"; + + exe_type = strtok_r(NULL, "\n", &save_ptr); + if (exe_type != NULL) + data.exe_type = exe_type; + else + data.exe_type = "none"; + + if (exception_cb(&data, user_data) == STC_CANCEL) + break; + } + fclose(fp); + + __STC_LOG_FUNC_EXIT__; + return error_code; +} diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 08f1065..8bcff31 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -26,6 +26,7 @@ #include "counter.h" #include "table-statistics.h" #include "table-counters.h" +#include "stc-exception.h" #include "stc-monitor.h" #include "stc-manager-plugin.h" @@ -1499,6 +1500,41 @@ static stc_error_e __process_update_background(void) return STC_ERROR_NONE; } +static void __excn_hash_foreach_print(gpointer key, gpointer value, + gpointer data) +{ + const char *process_name = key; + const char *exe_type = value; + + STC_LOGI("excn info => process_name [%s] exe_type [%s]", + process_name, exe_type); +} + +static void __excn_hash_printall(void) +{ + g_hash_table_foreach(g_system->excns_hash, + __excn_hash_foreach_print, NULL); +} + +static stc_cb_ret_e __insert_exception_cb(const table_exceptions_info *info, + void *user_data) +{ + stc_cb_ret_e ret = STC_CONTINUE; + + if (g_hash_table_insert(g_system->excns_hash, + g_strdup(info->process_name), + g_strdup(info->exe_type)) != TRUE) + ret = STC_CANCEL; + + return ret; +} + +static void __fill_exceptions_list(void) +{ + table_exceptions_foreach(__insert_exception_cb, NULL); + __excn_hash_printall(); +} + stc_error_e stc_monitor_init(void) { stc_system_s *system = MALLOC0(stc_system_s, 1); @@ -1518,6 +1554,9 @@ stc_error_e stc_monitor_init(void) __rstns_tree_key_free, __rstns_tree_value_free); + system->excns_hash = g_hash_table_new_full(g_str_hash, + g_str_equal, g_free, g_free); + /* create netlink socket for updating kernel counters */ system->contr_sock = create_netlink(NETLINK_NETFILTER, 0); if (!(system->contr_sock)) { @@ -1557,6 +1596,7 @@ stc_error_e stc_monitor_init(void) __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE, (int *)&g_system->background_state); + __fill_exceptions_list(); __fill_restritions_list(); return STC_ERROR_NONE; @@ -1583,6 +1623,10 @@ stc_error_e stc_monitor_deinit(void) g_tree_destroy(g_system->rstns); g_system->rstns = NULL; + /* destroy exception hash table */ + g_hash_table_destroy(g_system->excns_hash); + g_system->excns_hash = NULL; + FREE(g_system); return STC_ERROR_NONE; @@ -1852,6 +1896,19 @@ stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info) return ret; } +stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline) +{ + ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!"); + + char *exe_type = NULL; + + exe_type = g_hash_table_lookup(g_system->excns_hash, cmdline); + if (!exe_type) + return STC_ERROR_NO_DATA; + + return STC_ERROR_NONE; +} + int stc_monitor_get_counter_socket(void) { return g_system->contr_sock; -- 2.7.4 From 832d05fa4a9c9583cde9105623cc59d0e0fcc712 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Fri, 15 Sep 2017 11:04:23 +0900 Subject: [PATCH 11/16] Get process status from procfs Change-Id: I8c9916ce129ec35f0f93d4d3c35afc0e67f3b7f1 Signed-off-by: hyunuktak --- include/stc-manager-util.h | 8 ++++++++ src/helper/helper-procfs.c | 33 +++++++++++++++++++++++++++++++-- src/helper/helper-procfs.h | 2 +- src/monitor/stc-app-lifecycle.c | 16 ++++++++++------ 4 files changed, 50 insertions(+), 9 deletions(-) mode change 100644 => 100755 src/helper/helper-procfs.c mode change 100644 => 100755 src/helper/helper-procfs.h diff --git a/include/stc-manager-util.h b/include/stc-manager-util.h index d5ee410..32b1cde 100755 --- a/include/stc-manager-util.h +++ b/include/stc-manager-util.h @@ -321,6 +321,14 @@ static inline bool strstart_with(const char *str, const char *with) #define PROC_BUF_MAX 64 #define PROC_NAME_MAX 1024 +#define PROC_STATUS_CNT 6 + +#define PROC_STATUS_NAME 0 +#define PROC_STATUS_STATE 1 +#define PROC_STATUS_TGID 2 +#define PROC_STATUS_PID 3 +#define PROC_STATUS_PPID 4 +#define PROC_STATUS_TRACERPID 5 #define COMMA_DELIMETER "," diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c old mode 100644 new mode 100755 index 0974f2a..ca291ac --- a/src/helper/helper-procfs.c +++ b/src/helper/helper-procfs.c @@ -157,9 +157,38 @@ int proc_get_raw_cmdline(pid_t pid, char *buf, int len) return proc_get_data(path, buf, len); } -int proc_get_status(pid_t pid, char *buf, int len) +int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX]) { + unsigned int i; char path[PROC_BUF_MAX]; + char status_buf[PROC_BUF_MAX]; + FILE *fp; + snprintf(path, sizeof(path), "/proc/%d/status", pid); - return proc_get_data(path, buf, len); + fp = fopen(path, "r"); + if (fp == NULL) + return STC_ERROR_FAIL; + + for (i = 0; i < PROC_STATUS_CNT; ++i) { + char *token = NULL; + char *saveptr = NULL; + + if (fgets(status_buf, sizeof(status_buf), fp) == NULL) { + fclose(fp); + return STC_ERROR_FAIL; + } + + token = strtok_r(status_buf, ":", &saveptr); + if (token != NULL) { + token = strtok_r(NULL, "\n", &saveptr); + if (token != NULL) { + while (isspace((unsigned char)*token)) + token++; + strncpy(status[i], token, sizeof(status[i])); + } + } + } + fclose(fp); + + return STC_ERROR_NONE; } diff --git a/src/helper/helper-procfs.h b/src/helper/helper-procfs.h old mode 100644 new mode 100755 index 4f0fd02..55c6a03 --- a/src/helper/helper-procfs.h +++ b/src/helper/helper-procfs.h @@ -58,6 +58,6 @@ int proc_get_exepath(pid_t pid, char *buf, int len); * @desc get status from /proc/{pid}/status * @return negative value if error */ -int proc_get_status(pid_t pid, char *buf, int len); +int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX]); #endif /*__HELPER_PROCFS_H__*/ diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index 829c989..7f5e035 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -37,7 +37,7 @@ typedef struct { typedef struct { char cmdline[PROC_NAME_MAX]; - char status[PROC_BUF_MAX]; + char status[PROC_STATUS_CNT][PROC_BUF_MAX]; } proc_value_s; typedef struct __attribute__ ((aligned(NLMSG_ALIGNTO))) { @@ -317,16 +317,17 @@ stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, static void __process_event_exec(int tid, int pid) { char cmdline[PROC_NAME_MAX] = {0, }; - char status[PROC_BUF_MAX] = {0, }; + char status[PROC_STATUS_CNT][PROC_BUF_MAX]; if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && - STC_ERROR_NONE == proc_get_status(pid, status, PROC_BUF_MAX)) { + STC_ERROR_NONE == proc_get_status(pid, status)) { if (__check_excn(cmdline)) { STC_LOGI("[%s] monitoring is excepted", cmdline); return; } + unsigned int i; proc_key_s *key; proc_value_s *value; @@ -344,13 +345,16 @@ static void __process_event_exec(int tid, int pid) } key->pid = pid; - g_strlcpy(value->status, status, sizeof(value->status)); + 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); - STC_LOGD("EXEC:pid=%d,tgid=%d\t[%s]\t[%s]", pid, tid, status, - cmdline); + STC_LOGD("EXEC: pid[%d] tgid=[%d] cmdline[%s]", pid, tid, cmdline); + STC_LOGD("STATUS: name[%s] state[%s] tgid[%s] pid[%s] ppid[%s] tracerpid[%s]", + status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TGID], + status[PROC_STATUS_PID], status[PROC_STATUS_PPID], status[PROC_STATUS_TRACERPID]); } } -- 2.7.4 From 6ddbb97984e78a666ec56faacdc702c8776e7fb3 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Mon, 18 Sep 2017 18:29:20 +0900 Subject: [PATCH 12/16] Fixed a svace issue for 293218 Change-Id: I0dff5c0cf7f228243c090a1213aee280ddc6bb63 Signed-off-by: hyunuktak --- src/helper/helper-nfacct-rule.c | 48 ++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index 3c365ee..a2a598f 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -411,26 +411,44 @@ static char* get_cmd_pos(const char *cmd_buf) static bool is_rule_present(const char *cmd_buf) { - size_t buf_len; - char *exec_buf; - char *cmd_pos = get_cmd_pos(cmd_buf); bool ret = false; - if (!cmd_pos) - return false; + pid_t pid = fork(); - buf_len = strlen(cmd_buf) + 1; - exec_buf = (char *)malloc(buf_len); - if (!exec_buf) - return false; + if (pid == 0) { + gchar **args = NULL; + size_t buf_len; + char *exec_buf; + char *cmd_pos = get_cmd_pos(cmd_buf); + + if (!cmd_pos) + exit(1); + + buf_len = strlen(cmd_buf) + 1; + exec_buf = (char *)malloc(buf_len); + if (!exec_buf) + exit(1); + + strncpy(exec_buf, cmd_buf, buf_len); + strncpy(exec_buf + (cmd_pos - cmd_buf), IPTABLES_CHECK, + sizeof(IPTABLES_CHECK) - 1); + + STC_LOGD("check rule %s", exec_buf); - strncpy(exec_buf, cmd_buf, buf_len); - strncpy(exec_buf + (cmd_pos - cmd_buf), IPTABLES_CHECK, - sizeof(IPTABLES_CHECK) - 1); + args = g_strsplit_set(exec_buf, " ", -1); - STC_LOGD("check rule %s", exec_buf); + ret = execv(args[0], args); + if (ret) { + char buf[BUF_SIZE_FOR_ERR] = { 0 }; + STC_LOGE("Can't execute %s: %s", + cmd_buf, strerror_r(errno, buf, + BUF_SIZE_FOR_ERR)); + } + + free(exec_buf); + g_strfreev(args); + exit(ret); + } - ret = system(exec_buf) == 0; - free(exec_buf); return ret; } -- 2.7.4 From 224a785c0b719023cf92da6d226af609bc246062 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 19 Sep 2017 16:44:25 +0900 Subject: [PATCH 13/16] Added application exception list it is checking internet privilege it has used pkginfo package it has a cycle as one day Change-Id: I4dd8f2d0a8eec709790ea6929f8233c84cbd43a8 Signed-off-by: hyunuktak --- data/exceptions | 7 +-- packaging/stc-manager.spec | 3 +- src/CMakeLists.txt | 1 + src/monitor/include/stc-app-lifecycle.h | 4 +- src/monitor/include/stc-exception.h | 14 ++++- src/monitor/include/stc-monitor.h | 4 ++ src/monitor/stc-app-lifecycle.c | 4 +- src/monitor/stc-exception.c | 102 +++++++++++++++++++++++++++++++- src/monitor/stc-monitor.c | 43 +++++++++++++- src/stc-manager-plugin.c | 9 +-- 10 files changed, 171 insertions(+), 20 deletions(-) diff --git a/data/exceptions b/data/exceptions index 67ab00e..0429338 100644 --- a/data/exceptions +++ b/data/exceptions @@ -24,8 +24,7 @@ wifi-loader:sys launchpad-loader:sys wrt-loader:sys dotnet-launcher:sys -volume:app -iptables:app -ip6tables:app +iptables:sys +ip6tables:sys modprobe:inst -net-cls-release:app +net-cls-release:sys diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index d1befbe..3ed74db 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.31 +Version: 0.0.32 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 @@ -20,6 +20,7 @@ BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(capi-system-info) +BuildRequires: pkgconfig(pkgmgr-info) %if %{?enable_database} == YES BuildRequires: pkgconfig(sqlite3) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1ea3658..295314b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,7 @@ SET(REQUIRES_LIST ${REQUIRES_LIST} dlog vconf capi-system-info + pkgmgr-info ) IF("${ENABLE_DATABASE}" STREQUAL "YES") diff --git a/src/monitor/include/stc-app-lifecycle.h b/src/monitor/include/stc-app-lifecycle.h index 3946c3e..cb5c39e 100755 --- a/src/monitor/include/stc-app-lifecycle.h +++ b/src/monitor/include/stc-app-lifecycle.h @@ -24,8 +24,8 @@ stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, pid_t pid, - gchar *app_id, - gchar *pkg_id, + const gchar *app_id, + const gchar *pkg_id, stc_app_type_e app_type); void stc_app_lifecycle_monitor_init(void); void stc_app_lifecycle_monitor_deinit(void); diff --git a/src/monitor/include/stc-exception.h b/src/monitor/include/stc-exception.h index da951ae..38a2fd4 100755 --- a/src/monitor/include/stc-exception.h +++ b/src/monitor/include/stc-exception.h @@ -17,16 +17,24 @@ #ifndef __STC_EXCEPTION_H__ #define __STC_EXCEPTION_H__ +#define EXE_TYPE_APPLICATION "app" +#define EXE_TYPE_INSTRUCTION "inst" +#define EXE_TYPE_SYSTEM "sys" +#define EXE_TYPE_SCRIPT "script" + typedef struct { char *process_name; char *exe_type; -} table_exceptions_info; +} stc_exceptions_info; typedef stc_cb_ret_e -(*table_exceptions_info_cb)(const table_exceptions_info *info, +(*stc_exceptions_info_cb)(const stc_exceptions_info *info, void *user_data); -stc_error_e table_exceptions_foreach(table_exceptions_info_cb info_cb, +stc_error_e table_exceptions_foreach(const stc_exceptions_info_cb exception_cb, + void *user_data); + +stc_error_e pkginfo_exceptions_foreach(const stc_exceptions_info_cb exception_cb, void *user_data); #endif /*__STC_EXCEPTION_H__ */ diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index 6601489..ffb64c0 100755 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -28,6 +28,9 @@ /* 1 seconds */ #define CONTR_TIMER_INTERVAL 1 +/* 1 day */ +#define EXCNS_TIMER_INTERVAL 86400 + /** * @brief key for processes tree */ @@ -101,6 +104,7 @@ typedef struct { GTree *apps; /**< monitored applications */ gboolean apps_tree_updated; GHashTable *excns_hash; /**< exception hash table */ + guint excns_timer_id; guint background_state; } stc_system_s; diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index 7f5e035..1787043 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -198,8 +198,8 @@ static void __reopen_nl_connector_sock(void) stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, pid_t pid, - gchar *app_id, - gchar *pkg_id, + const gchar *app_id, + const gchar *pkg_id, stc_app_type_e app_type) { __STC_LOG_FUNC_ENTER__; diff --git a/src/monitor/stc-exception.c b/src/monitor/stc-exception.c index 87c3eeb..30579fc 100755 --- a/src/monitor/stc-exception.c +++ b/src/monitor/stc-exception.c @@ -20,19 +20,25 @@ * @file stc-exception.c */ +#include + #include "stc-manager.h" #include "stc-exception.h" #define EXCEPTION_BUF_MAX 64 #define EXCEPTION_STORAGE "/var/lib/stc/exceptions" -stc_error_e table_exceptions_foreach(const table_exceptions_info_cb exception_cb, +#define INTERNET_PRIVILEGE "http://tizen.org/privilege/internet" + +static GHashTable *g_pkginfo_filter_hash; + +stc_error_e table_exceptions_foreach(const stc_exceptions_info_cb exception_cb, void *user_data) { __STC_LOG_FUNC_ENTER__; stc_error_e error_code = STC_ERROR_NONE; - table_exceptions_info data; + stc_exceptions_info data; FILE *fp = NULL; char buf[EXCEPTION_BUF_MAX] = {0, }; @@ -64,3 +70,95 @@ stc_error_e table_exceptions_foreach(const table_exceptions_info_cb exception_cb __STC_LOG_FUNC_EXIT__; return error_code; } + +static int __pkginfo_filter_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data) +{ + int ret = 0; + char *pkgname = NULL; + + ret = pkgmgrinfo_pkginfo_get_pkgname(handle, &pkgname); + if (ret == PMINFO_R_OK) { + if (g_hash_table_insert(g_pkginfo_filter_hash, + g_strdup(pkgname), g_strdup(EXE_TYPE_APPLICATION)) != TRUE) + STC_LOGE("Failed to insert hash table"); + } + + return STC_CONTINUE; +} + +static int __pkginfo_pkg_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data) +{ + int ret = 0; + char *pkgname = NULL; + char *exe_type = NULL; + stc_exceptions_info data; + const stc_exceptions_info_cb excn_cb = user_data; + + ret = pkgmgrinfo_pkginfo_get_pkgname(handle, &pkgname); + if (ret == PMINFO_R_OK) { + exe_type = g_hash_table_lookup(g_pkginfo_filter_hash, pkgname); + if (exe_type) + return STC_CONTINUE; + + data.process_name = pkgname; + data.exe_type = EXE_TYPE_APPLICATION; + + if (excn_cb(&data, NULL) == STC_CANCEL) + STC_LOGE("Failed to insert hash table"); + } + + return STC_CONTINUE; +} + +stc_error_e pkginfo_exceptions_foreach(const stc_exceptions_info_cb exception_cb, + void *user_data) +{ + __STC_LOG_FUNC_ENTER__; + + int ret = 0; + int err = STC_ERROR_NONE; + pkgmgrinfo_pkginfo_filter_h handle; + + g_pkginfo_filter_hash = g_hash_table_new_full(g_str_hash, + g_str_equal, g_free, g_free); + + ret = pkgmgrinfo_pkginfo_filter_create(&handle); + ret_value_msg_if(ret != PMINFO_R_OK, STC_ERROR_FAIL, + "Failed to create pkginfo filter"); + + ret = pkgmgrinfo_pkginfo_filter_add_string(handle, + PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE, + INTERNET_PRIVILEGE); + if (ret != PMINFO_R_OK) { + STC_LOGE("Failed to add pkginfo filter string"); + err = STC_ERROR_FAIL; + goto out; + } + + ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle, + __pkginfo_filter_list_cb, NULL); + if (ret != PMINFO_R_OK) { + STC_LOGE("Failed to foreach pkginfo filter"); + err = STC_ERROR_FAIL; + goto out; + } + + ret = pkgmgrinfo_pkginfo_get_list(__pkginfo_pkg_list_cb, exception_cb); + if (ret != PMINFO_R_OK) { + STC_LOGE("Failed to get pkginfo list"); + err = STC_ERROR_FAIL; + goto out; + } + +out: + if (g_pkginfo_filter_hash) { + g_hash_table_destroy(g_pkginfo_filter_hash); + g_pkginfo_filter_hash = NULL; + } + + if (handle) + pkgmgrinfo_pkginfo_filter_destroy(handle); + + __STC_LOG_FUNC_EXIT__; + return err; +} diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 8bcff31..ed8af13 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -1516,7 +1516,24 @@ static void __excn_hash_printall(void) __excn_hash_foreach_print, NULL); } -static stc_cb_ret_e __insert_exception_cb(const table_exceptions_info *info, +static gboolean __remove_exception_app(gpointer key, gpointer value, + gpointer data) +{ + const char *exe_type = value; + + if (g_strcmp0(exe_type, EXE_TYPE_APPLICATION) == 0) + return TRUE; + + return FALSE; +} + +static void __remove_exception_appall(void) +{ + g_hash_table_foreach_remove(g_system->excns_hash, + __remove_exception_app, NULL); +} + +static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info, void *user_data) { stc_cb_ret_e ret = STC_CONTINUE; @@ -1532,7 +1549,19 @@ static stc_cb_ret_e __insert_exception_cb(const table_exceptions_info *info, static void __fill_exceptions_list(void) { table_exceptions_foreach(__insert_exception_cb, NULL); - __excn_hash_printall(); + pkginfo_exceptions_foreach(__insert_exception_cb, NULL); + + /* __excn_hash_printall(); */ +} + +static gboolean __update_exceptions_app_list(void *user_data) +{ + __remove_exception_appall(); + pkginfo_exceptions_foreach(__insert_exception_cb, NULL); + + /* __excn_hash_printall(); */ + + return TRUE; } stc_error_e stc_monitor_init(void) @@ -1599,6 +1628,10 @@ stc_error_e stc_monitor_init(void) __fill_exceptions_list(); __fill_restritions_list(); + g_system->excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL, + __update_exceptions_app_list, + NULL); + return STC_ERROR_NONE; } @@ -1615,6 +1648,12 @@ stc_error_e stc_monitor_deinit(void) g_system->contr_timer_id = 0; } + /* remove exceptions app list update timer */ + if (g_system->excns_timer_id > 0) { + g_source_remove(g_system->excns_timer_id); + g_system->excns_timer_id = 0; + } + /* destroy monitored application tree */ g_tree_destroy(g_system->apps); g_system->apps = NULL; diff --git a/src/stc-manager-plugin.c b/src/stc-manager-plugin.c index 9a8d96b..d1e6d30 100755 --- a/src/stc-manager-plugin.c +++ b/src/stc-manager-plugin.c @@ -117,8 +117,8 @@ int stc_register_state_changed_cb(stc_s *stc, __STC_LOG_FUNC_ENTER__; if (!stc_plugin_enabled) { - __STC_LOG_FUNC_EXIT__; STC_LOGE("Plugin wasn't enabled"); + __STC_LOG_FUNC_EXIT__; return 0; } @@ -128,6 +128,7 @@ int stc_register_state_changed_cb(stc_s *stc, return 0; } + __STC_LOG_FUNC_EXIT__; return stc_plugin->register_state_changed_cb(stc, cb, data); } int stc_deregister_state_changed_cb(stc_s *stc) @@ -135,17 +136,17 @@ int stc_deregister_state_changed_cb(stc_s *stc) __STC_LOG_FUNC_ENTER__; if (!stc_plugin_enabled) { - __STC_LOG_FUNC_EXIT__; STC_LOGE("Plugin wasn't enabled"); + __STC_LOG_FUNC_EXIT__; return 0; } if (!stc_plugin) { - __STC_LOG_FUNC_EXIT__; STC_LOGE("Plugin wasn't loaded"); + __STC_LOG_FUNC_EXIT__; return 0; } - __STC_LOG_FUNC_EXIT__; + __STC_LOG_FUNC_EXIT__; return stc_plugin->deregister_state_changed_cb(stc); } -- 2.7.4 From c1a66d6d01e44a4d64232748d0701174b035bb7c Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 20 Sep 2017 16:35:35 +0900 Subject: [PATCH 14/16] Replaced imsi to subscriber ID Change-Id: Ie137cb045ab279416b3d3711f0cd55a1fe4543bd Signed-off-by: hyunuktak --- data/traffic_db.sql | 4 +- include/stc-restriction.h | 2 +- packaging/stc-manager.spec | 3 +- src/CMakeLists.txt | 1 + src/database/include/stc-db.h | 4 +- src/database/include/table-restrictions.h | 8 ++-- src/database/include/table-statistics.h | 4 +- src/database/tables/table-restrictions.c | 70 ++++++++++++++-------------- src/database/tables/table-statistics.c | 68 +++++++++++++-------------- src/monitor/include/stc-default-connection.h | 3 +- src/monitor/include/stc-monitor.h | 2 +- src/monitor/stc-default-connection.c | 29 +++++++++--- src/monitor/stc-monitor.c | 30 ++++++------ src/stc-restriction.c | 16 +++---- src/stc-statistics.c | 12 ++--- 15 files changed, 139 insertions(+), 117 deletions(-) diff --git a/data/traffic_db.sql b/data/traffic_db.sql index 3fae3f6..dd83d65 100644 --- a/data/traffic_db.sql +++ b/data/traffic_db.sql @@ -11,7 +11,7 @@ CREATE TABLE IF NOT EXISTS statistics ( is_roaming INT, hw_net_protocol_type INT, ifname TEXT, - imsi TEXT, + subscriber_id TEXT, ground INT ); @@ -22,7 +22,7 @@ CREATE TABLE IF NOT EXISTS restrictions ( ifname TEXT, rst_state INT, roaming INT, - imsi TEXT NOT NULL, + subscriber_id TEXT NOT NULL, data_limit BIGINT, data_warn_limit BIGINT ); diff --git a/include/stc-restriction.h b/include/stc-restriction.h index acd4eab..e804e0f 100755 --- a/include/stc-restriction.h +++ b/include/stc-restriction.h @@ -35,7 +35,7 @@ typedef struct { int64_t rcv_warning_limit; stc_roaming_type_e roaming; char *ifname; - char *imsi; + char *subscriber_id; } stc_restriction_s; /***************************************************************************** diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 3ed74db..7979283 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.32 +Version: 0.0.33 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 @@ -21,6 +21,7 @@ BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(capi-system-info) BuildRequires: pkgconfig(pkgmgr-info) +BuildRequires: pkgconfig(openssl) %if %{?enable_database} == YES BuildRequires: pkgconfig(sqlite3) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 295314b..23fd943 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,7 @@ SET(REQUIRES_LIST ${REQUIRES_LIST} vconf capi-system-info pkgmgr-info + openssl ) IF("${ENABLE_DATABASE}" STREQUAL "YES") diff --git a/src/database/include/stc-db.h b/src/database/include/stc-db.h index 6f718c3..689cb1f 100755 --- a/src/database/include/stc-db.h +++ b/src/database/include/stc-db.h @@ -79,8 +79,8 @@ typedef struct { typedef struct { uint32_t classid; int iftype; - /* pointer to telephony's imsi */ - char *imsi; + /* pointer to telephony's subscriber_id */ + char *subscriber_id; char ifname[MAX_IFACE_LENGTH]; } stc_db_classid_iftype_key; diff --git a/src/database/include/table-restrictions.h b/src/database/include/table-restrictions.h index fe275e1..d972b87 100755 --- a/src/database/include/table-restrictions.h +++ b/src/database/include/table-restrictions.h @@ -20,7 +20,7 @@ typedef struct { char *app_id; char *ifname; - char *imsi; + char *subscriber_id; stc_iface_type_e iftype; stc_restriction_state_e rst_state; stc_roaming_type_e roaming; @@ -41,9 +41,9 @@ stc_error_e table_restrictions_per_app(const gchar *app_id, void *user_data); -stc_error_e table_restrictions_get_restriction_state_imsi(const char *app_id, +stc_error_e table_restrictions_get_restriction_state_subscriber_id(const char *app_id, stc_iface_type_e iftype, - const char *imsi_hash, + const char *subscriber_id, stc_restriction_state_e *state); stc_error_e table_restrictions_get_restriction_state(const char *app_id, @@ -54,7 +54,7 @@ stc_error_e table_restrictions_update(table_restrictions_info *info); stc_error_e table_restrictions_delete(const char *app_id, const stc_iface_type_e iftype, - const char *imsi); + const char *subscriber_id); stc_error_e table_restrictions_prepare(sqlite3 *db); diff --git a/src/database/include/table-statistics.h b/src/database/include/table-statistics.h index 62a5ef6..ec88f5a 100755 --- a/src/database/include/table-statistics.h +++ b/src/database/include/table-statistics.h @@ -20,7 +20,7 @@ typedef struct { char *app_id; char *ifname; - char *imsi; + char *subscriber_id; stc_iface_type_e iftype; stc_db_tm_interval_s *interval; stc_data_counter_s cnt; @@ -32,7 +32,7 @@ typedef struct { typedef struct { unsigned char version; char *app_id; - char *imsi; + char *subscriber_id; stc_iface_type_e iftype; stc_db_tm_interval_s *interval; stc_db_connection_period_type connection_state; diff --git a/src/database/tables/table-restrictions.c b/src/database/tables/table-restrictions.c index e1e7256..9324887 100755 --- a/src/database/tables/table-restrictions.c +++ b/src/database/tables/table-restrictions.c @@ -26,15 +26,15 @@ /* DELETE statements */ #define DELETE_RESTRICTIONS "DELETE FROM restrictions " \ - " WHERE binpath = ? AND iftype = ? AND imsi = ?" + " WHERE binpath = ? AND iftype = ? AND subscriber_id = ?" /* SELECT statements */ #define SELECT_RESTRICTIONS "SELECT binpath, data_limit, " \ - " iftype, rst_state, roaming, ifname, imsi, " \ + " iftype, rst_state, roaming, ifname, subscriber_id, " \ " data_warn_limit, restriction_id FROM restrictions" #define SELECT_RESTRICTIONS_PER_APP "SELECT binpath, data_limit, " \ - " iftype, rst_state, roaming, ifname, imsi, " \ + " iftype, rst_state, roaming, ifname, subscriber_id, " \ " data_warn_limit, restriction_id " \ " FROM restrictions INDEXED BY restrictions_index " \ " WHERE binpath = ?" @@ -43,24 +43,24 @@ " FROM restrictions INDEXED BY restrictions_index " \ " WHERE binpath = ? AND iftype = ?" -#define SELECT_RESTRICTION_STATE_IMSI "SELECT rst_state " \ +#define SELECT_RESTRICTION_STATE_SUBSCRIBER_ID "SELECT rst_state " \ " FROM restrictions INDEXED BY restrictions_index " \ - " WHERE binpath = ? AND iftype = ? AND imsi = ?" + " WHERE binpath = ? AND iftype = ? AND subscriber_id = ?" #define SELECT_RESTRICTION_ID "SELECT restriction_id FROM restrictions " \ - " WHERE binpath = ? AND iftype = ? AND imsi = ? AND " \ + " WHERE binpath = ? AND iftype = ? AND subscriber_id = ? AND " \ " rst_state = ? AND roaming = ? AND ifname = ?" /* UPDATE statement */ #define UPDATE_NET_RESTRICTIONS "UPDATE restrictions " \ " SET binpath = ?, data_limit = ?, iftype = ?, rst_state = ?, " \ - " roaming = ?, ifname = ?, imsi = ?, data_warn_limit = ? " \ + " roaming = ?, ifname = ?, subscriber_id = ?, data_warn_limit = ? " \ " WHERE restriction_id = ?" /* INSERT statement */ #define INSERT_NET_RESTRICTIONS "INSERT INTO restrictions " \ " (binpath, data_limit, iftype, rst_state, " \ - " roaming, ifname, imsi, data_warn_limit) " \ + " roaming, ifname, subscriber_id, data_warn_limit) " \ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)" static void __finalize_delete(void); @@ -129,7 +129,7 @@ static sqlite3_stmt *delete_restrictions; static sqlite3_stmt *select_restriction; static sqlite3_stmt *select_restriction_per_app; static sqlite3_stmt *select_restriction_state; -static sqlite3_stmt *select_restriction_state_imsi; +static sqlite3_stmt *select_restriction_state_subscriber_id; static sqlite3_stmt *select_restriction_id; /* REPLACE statements */ @@ -179,7 +179,7 @@ static int __prepare_select(sqlite3 *db) PREPARE_SELECT(select_restriction, SELECT_RESTRICTIONS); PREPARE_SELECT(select_restriction_per_app, SELECT_RESTRICTIONS_PER_APP); PREPARE_SELECT(select_restriction_state, SELECT_RESTRICTION_STATE); - PREPARE_SELECT(select_restriction_state_imsi, SELECT_RESTRICTION_STATE_IMSI); + PREPARE_SELECT(select_restriction_state_subscriber_id, SELECT_RESTRICTION_STATE_SUBSCRIBER_ID); PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID); initialized = 1; @@ -194,7 +194,7 @@ static void __finalize_select(void) FINALIZE(select_restriction); FINALIZE(select_restriction_per_app); FINALIZE(select_restriction_state); - FINALIZE(select_restriction_state_imsi); + FINALIZE(select_restriction_state_subscriber_id); FINALIZE(select_restriction_id); __STC_LOG_FUNC_EXIT__; @@ -289,7 +289,7 @@ stc_error_e table_restrictions_per_app(const gchar* app_id, (stc_restriction_state_e)sqlite3_column_int(stmt, 3); data.roaming = sqlite3_column_int(stmt, 4); data.ifname = (char *)sqlite3_column_text(stmt, 5); - data.imsi = (char *)sqlite3_column_text(stmt, 6); + 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); @@ -337,7 +337,7 @@ stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restrict (stc_restriction_state_e)sqlite3_column_int(stmt, 3); data.roaming = sqlite3_column_int(stmt, 4); data.ifname = (char *)sqlite3_column_text(stmt, 5); - data.imsi = (char *)sqlite3_column_text(stmt, 6); + 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); @@ -359,15 +359,15 @@ stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restrict return error_code; } -stc_error_e table_restrictions_get_restriction_state_imsi(const char *app_id, +stc_error_e table_restrictions_get_restriction_state_subscriber_id(const char *app_id, stc_iface_type_e iftype, - const char *imsi_hash, + const char *subscriber_id, stc_restriction_state_e *state) { __STC_LOG_FUNC_ENTER__; int error_code = STC_ERROR_NONE; int ret; - bool state_imsi = 0; + bool state_subscriber_id = 0; if (state == NULL) { STC_LOGE("Please provide valid argument!"); @@ -376,11 +376,11 @@ stc_error_e table_restrictions_get_restriction_state_imsi(const char *app_id, } *state = STC_RESTRICTION_UNKNOWN; - sqlite3_reset(select_restriction_state_imsi); + sqlite3_reset(select_restriction_state_subscriber_id); sqlite3_reset(select_restriction_state); - if (imsi_hash == NULL) { - state_imsi = 0; + if (subscriber_id == NULL) { + state_subscriber_id = 0; DB_ACTION(sqlite3_bind_text(select_restriction_state, 1, app_id ? app_id : "", -1, SQLITE_STATIC)); @@ -388,23 +388,23 @@ stc_error_e table_restrictions_get_restriction_state_imsi(const char *app_id, iftype)); ret = sqlite3_step(select_restriction_state); } else { - state_imsi = 1; - DB_ACTION(sqlite3_bind_text(select_restriction_state_imsi, 1, + state_subscriber_id = 1; + DB_ACTION(sqlite3_bind_text(select_restriction_state_subscriber_id, 1, app_id ? app_id : "", -1, SQLITE_STATIC)); - DB_ACTION(sqlite3_bind_int(select_restriction_state_imsi, 2, + DB_ACTION(sqlite3_bind_int(select_restriction_state_subscriber_id, 2, iftype)); - DB_ACTION(sqlite3_bind_text(select_restriction_state_imsi, 3, - imsi_hash, -1, SQLITE_STATIC)); - ret = sqlite3_step(select_restriction_state_imsi); + DB_ACTION(sqlite3_bind_text(select_restriction_state_subscriber_id, 3, + subscriber_id, -1, SQLITE_STATIC)); + ret = sqlite3_step(select_restriction_state_subscriber_id); } switch (ret) { case SQLITE_DONE: break; case SQLITE_ROW: - if (state_imsi) - *state = (stc_restriction_state_e)sqlite3_column_int(select_restriction_state_imsi, 0); + if (state_subscriber_id) + *state = (stc_restriction_state_e)sqlite3_column_int(select_restriction_state_subscriber_id, 0); else *state = (stc_restriction_state_e)sqlite3_column_int(select_restriction_state, 0); break; @@ -418,7 +418,7 @@ stc_error_e table_restrictions_get_restriction_state_imsi(const char *app_id, handle_error: sqlite3_reset(select_restriction_state); - sqlite3_reset(select_restriction_state_imsi); + sqlite3_reset(select_restriction_state_subscriber_id); return error_code; } @@ -428,24 +428,24 @@ stc_error_e table_restrictions_get_restriction_state(const char *app_id, { __STC_LOG_FUNC_ENTER__; __STC_LOG_FUNC_EXIT__; - return table_restrictions_get_restriction_state_imsi(app_id, iftype, + return table_restrictions_get_restriction_state_subscriber_id(app_id, iftype, NULL, state); } stc_error_e table_restrictions_delete(const char *app_id, const stc_iface_type_e iftype, - const char *imsi) + const char *subscriber_id) { stc_error_e error_code = STC_ERROR_NONE; sqlite3_stmt *stmt = delete_restrictions; - STC_LOGD("app_id [%s], iftype [%d], imsi [%s]", - app_id, iftype, imsi); + STC_LOGD("app_id [%s], iftype [%d], subscriber_id [%s]", + app_id, iftype, subscriber_id); DB_ACTION(sqlite3_bind_text(stmt, 1, app_id ? app_id : "", -1, SQLITE_TRANSIENT)); DB_ACTION(sqlite3_bind_int(stmt, 2, iftype)); - DB_ACTION(sqlite3_bind_text(stmt, 3, imsi ? imsi : "", -1, + DB_ACTION(sqlite3_bind_text(stmt, 3, subscriber_id ? subscriber_id : "", -1, SQLITE_TRANSIENT)); if (sqlite3_step(stmt) != SQLITE_DONE) { @@ -473,7 +473,7 @@ stc_error_e __get_restriction_id(table_restrictions_info *info) DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "", -1, SQLITE_TRANSIENT)); DB_ACTION(sqlite3_bind_int(stmt, 2, info->iftype)); - DB_ACTION(sqlite3_bind_text(stmt, 3, info->imsi ? info->imsi : "", + DB_ACTION(sqlite3_bind_text(stmt, 3, info->subscriber_id ? info->subscriber_id : "", -1, SQLITE_TRANSIENT)); DB_ACTION(sqlite3_bind_int(stmt, 4, info->rst_state)); DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming)); @@ -523,7 +523,7 @@ stc_error_e table_restrictions_update(table_restrictions_info *info) DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming)); DB_ACTION(sqlite3_bind_text(stmt, 6, info->ifname ? info->ifname : "", -1, SQLITE_TRANSIENT)); - DB_ACTION(sqlite3_bind_text(stmt, 7, info->imsi ? info->imsi : "", + 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)); diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c index 1b6d288..c14fdb6 100755 --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -42,108 +42,108 @@ /* SELECT statements */ #define SELECT_FOR_PERIOD "select binpath, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, " \ - "sum(sent) as sent, imsi, ground, iftype, ifname from statistics " \ + "sum(sent) as sent, subscriber_id, ground, iftype, ifname from statistics " \ "where time_stamp between ? and ? " \ - "group by binpath, is_roaming, imsi order by received desc" + "group by binpath, is_roaming, subscriber_id order by received desc" #define SELECT_FOR_PERIOD_IFACE "select binpath, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, " \ - "sum(sent) as sent, imsi, ground, iftype, ifname from statistics " \ + "sum(sent) as sent, subscriber_id, ground, iftype, ifname from statistics " \ "where time_stamp between ? and ? " \ - "and iftype=? group by binpath, is_roaming, imsi order by received desc" + "and iftype=? group by binpath, is_roaming, subscriber_id order by received desc" #define SELECT_CHUNKS "select binpath, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, " \ - "sum(sent) as sent, imsi, ground, iftype, ifname, " \ + "sum(sent) as sent, subscriber_id, ground, iftype, ifname, " \ "time_stamp - time_stamp % ? as timestamp " \ "from statistics where time_stamp between ? and ? " \ - "group by binpath, timestamp, imsi order by timestamp" + "group by binpath, timestamp, subscriber_id order by timestamp" #define SELECT_CHUNKS_IFACE "select binpath, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, " \ - "sum(sent) as sent, imsi, ground, iftype, ifname, " \ + "sum(sent) as sent, subscriber_id, ground, iftype, ifname, " \ "time_stamp - time_stamp % ? as timestamp " \ "from statistics where time_stamp between ? and ? and iftype=?" \ - "group by binpath, timestamp, imsi order by timestamp" + "group by binpath, timestamp, subscriber_id order by timestamp" #define SELECT_APP_DETAILS "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground from statistics " \ + "ifname, subscriber_id, ground from statistics " \ "where time_stamp between ? and ? and binpath=? " \ - "group by binpath, iftype, ifname, imsi, hw_net_protocol_type, " \ + "group by binpath, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ "is_roaming " \ - "order by time_stamp, binpath, iftype, ifname, imsi, " \ + "order by time_stamp, binpath, iftype, ifname, subscriber_id, " \ "hw_net_protocol_type, is_roaming" #define SELECT_APP_DETAILS_IFACE "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground from statistics " \ + "ifname, subscriber_id, ground from statistics " \ "where time_stamp between ? and ? and binpath=? and iftype=?" \ - "group by hw_net_protocol_type, is_roaming, iftype, ifname, imsi " \ + "group by hw_net_protocol_type, is_roaming, iftype, ifname, subscriber_id " \ "order by time_stamp, hw_net_protocol_type, is_roaming, iftype, " \ - "ifname, imsi" + "ifname, subscriber_id" #define SELECT_CHUNKS_APP "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \ + "ifname, subscriber_id, ground, time_stamp - time_stamp % ? as time_stamp " \ "from statistics " \ "group by iftype, ifname, time_stamp, hw_net_protocol_type, is_roaming " \ "order by time_stamp, iftype, ifname, hw_net_protocol_type, is_roaming" #define SELECT_CHUNKS_APP_IFACE "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \ + "ifname, subscriber_id, ground, time_stamp - time_stamp % ? as time_stamp " \ "from statistics where time_stamp between ? and ? and binpath = ? " \ "and iftype = ? " \ "group by time_stamp, hw_net_protocol_type, is_roaming, " \ - "iftype, ifname, imsi " \ - "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ + "iftype, ifname, subscriber_id " \ + "order by time_stamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ "is_roaming" #define SELECT_TOTAL "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground from statistics " \ + "ifname, subscriber_id, ground from statistics " \ "where (time_stamp between ? and ?) " \ "and binpath NOT LIKE 'TOTAL_%' " \ - "group by iftype, ifname, imsi, hw_net_protocol_type, is_roaming " \ - "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ + "group by iftype, ifname, subscriber_id, hw_net_protocol_type, is_roaming " \ + "order by time_stamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ "is_roaming" #define SELECT_TOTAL_IFACE "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground from statistics " \ + "ifname, subscriber_id, ground from statistics " \ "where (time_stamp between ? and ?) and iftype=? " \ "and binpath NOT LIKE 'TOTAL_%' " \ "group by hw_net_protocol_type, is_roaming, " \ - "iftype, ifname, imsi " \ - "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ + "iftype, ifname, subscriber_id " \ + "order by time_stamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ "is_roaming" #define SELECT_CHUNKS_TOTAL "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \ + "ifname, subscriber_id, ground, time_stamp - time_stamp % ? as time_stamp " \ "from statistics where time_stamp between ? and ? " \ "and binpath NOT LIKE 'TOTAL_%' " \ - "group by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ + "group by time_stamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ "is_roaming " \ - "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ + "order by time_stamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ "is_roaming" #define SELECT_CHUNKS_TOTAL_IFACE "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \ + "ifname, subscriber_id, ground, time_stamp - time_stamp % ? as time_stamp " \ "from statistics where time_stamp between ? and ? " \ "and iftype = ? " \ "and binpath NOT LIKE 'TOTAL_%' " \ - "group by time_stamp, hw_net_protocol_type, is_roaming, iftype, ifname, imsi " \ + "group by time_stamp, hw_net_protocol_type, is_roaming, iftype, ifname, subscriber_id " \ "order by time_stamp, hw_net_protocol_type, is_roaming, iftype, " \ - "ifname, imsi" + "ifname, subscriber_id" /* INSERT statement */ #define INSERT_VALUES "insert into statistics " \ "(binpath, received, sent, time_stamp, " \ "iftype, is_roaming, hw_net_protocol_type, " \ - "ifname, imsi, ground) " \ + "ifname, subscriber_id, ground) " \ "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" static void __finalize_delete(void); @@ -474,7 +474,7 @@ stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rul data.roaming = sqlite3_column_int(stmt, 2); data.cnt.in_bytes = sqlite3_column_int64(stmt, 3); data.cnt.out_bytes = sqlite3_column_int64(stmt, 4); - data.imsi = (char *)sqlite3_column_text(stmt, 5); + data.subscriber_id = (char *)sqlite3_column_text(stmt, 5); data.ground = sqlite3_column_int(stmt, 6); data.iftype = sqlite3_column_int(stmt, 7); data.ifname = (char *)sqlite3_column_text(stmt, 8); @@ -557,7 +557,7 @@ stc_error_e table_statistics_per_app(const char *app_id, data.cnt.in_bytes = sqlite3_column_int64(stmt, 3); data.cnt.out_bytes = sqlite3_column_int64(stmt, 4); data.ifname = (char *)sqlite3_column_text(stmt, 5); - data.imsi = (char *)sqlite3_column_text(stmt, 6); + data.subscriber_id = (char *)sqlite3_column_text(stmt, 6); data.ground = sqlite3_column_int(stmt, 7); if (rule->granularity) { @@ -611,7 +611,7 @@ stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key, DB_ACTION(sqlite3_bind_text(stmt, 8, stat_key->ifname, -1, SQLITE_STATIC)); DB_ACTION(sqlite3_bind_text(stmt, 9, - stat_key->imsi ? stat_key->imsi : "" , -1, + stat_key->subscriber_id ? stat_key->subscriber_id : "" , -1, SQLITE_STATIC)); DB_ACTION(sqlite3_bind_int(stmt, 10, (int)stat->ground)); diff --git a/src/monitor/include/stc-default-connection.h b/src/monitor/include/stc-default-connection.h index b3326de..230671b 100755 --- a/src/monitor/include/stc-default-connection.h +++ b/src/monitor/include/stc-default-connection.h @@ -23,6 +23,7 @@ #include "stc-manager-util.h" #define IMSI_LENGTH 16 +#define SHA256_DIGEST_LENGTH 32 /** * @brief default connection information will be fetched from net-config @@ -39,7 +40,7 @@ typedef struct { gboolean roaming; /* only present when default profile is cellular */ - char imsi[IMSI_LENGTH]; + char subscriber_id[SHA256_DIGEST_LENGTH * 2 + 1]; /* hardware network protocol type */ stc_hw_net_protocol_type_e hw_net_protocol_type; diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index ffb64c0..74a05b4 100755 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -71,7 +71,7 @@ typedef struct { typedef struct { gchar *app_id; gchar *ifname; - gchar *imsi; + gchar *subscriber_id; stc_iface_type_e iftype; stc_roaming_type_e roaming; } stc_rstn_key_s; diff --git a/src/monitor/stc-default-connection.c b/src/monitor/stc-default-connection.c index e524970..aa8e1e6 100755 --- a/src/monitor/stc-default-connection.c +++ b/src/monitor/stc-default-connection.c @@ -15,6 +15,7 @@ */ #include +#include #include "stc-monitor.h" #include "stc-manager-gdbus.h" @@ -76,11 +77,26 @@ static int __telephony_get_current_sim(void) return current_sim; } -static void __telephony_get_modem_imsi(GDBusConnection *connection, +static void __make_imsi_to_subscriber_id(char *imsi) +{ + int i = 0; + SHA256_CTX ctx; + unsigned char md[SHA256_DIGEST_LENGTH]; + + SHA256_Init(&ctx); + SHA256_Update(&ctx, imsi, strlen(imsi)); + SHA256_Final(md, &ctx); + + for (i = 0; i < SHA256_DIGEST_LENGTH; ++i) + snprintf(g_default_connection.subscriber_id + (i * 2), 3, "%02x", md[i]); +} + +static void __telephony_get_modem_subscriber_id(GDBusConnection *connection, const char *default_modem_name) { GVariant *message = NULL; char tel_path[MAX_PATH_LENGTH]; + char imsi[IMSI_LENGTH]; const char *plmn = NULL; int plmn_len = 0; const char *msin = NULL; @@ -110,14 +126,15 @@ static void __telephony_get_modem_imsi(GDBusConnection *connection, goto done; } - snprintf(g_default_connection.imsi, IMSI_LENGTH, "%s%s", plmn, msin); + snprintf(imsi, IMSI_LENGTH, "%s%s", plmn, msin); + __make_imsi_to_subscriber_id(imsi); done: g_variant_unref(message); return; } -static void __telephony_update_default_modem_imsi(GDBusConnection *connection) +static void __telephony_update_default_modem_subscriber_id(GDBusConnection *connection) { GVariant *message = NULL; GVariantIter *iter = NULL; @@ -153,7 +170,7 @@ static void __telephony_update_default_modem_imsi(GDBusConnection *connection) current_sim--; } - __telephony_get_modem_imsi(connection, default_modem_name); + __telephony_get_modem_subscriber_id(connection, default_modem_name); FREE(default_modem_name); g_variant_iter_free(iter); @@ -169,7 +186,7 @@ static void __print_default_connection_info(void) STC_LOGI("ifname [%s]", g_default_connection.ifname); STC_LOGI("roaming [%u]", g_default_connection.roaming ? TRUE : FALSE); if (g_default_connection.type == STC_IFACE_DATACALL) - STC_LOGI("imsi [%s]", g_default_connection.imsi); + STC_LOGI("sub_id [%s]", g_default_connection.subscriber_id); STC_LOGI("=================================================="); } @@ -378,7 +395,7 @@ static stc_error_e __get_default_profile(GDBusConnection *connection) if (__is_cellular_profile(g_default_connection.path)) { g_default_connection.type = STC_IFACE_DATACALL; - __telephony_update_default_modem_imsi(connection); + __telephony_update_default_modem_subscriber_id(connection); } else if (__is_wifi_profile(g_default_connection.path)) { g_default_connection.type = STC_IFACE_WIFI; } else if (__is_ethernet_profile(g_default_connection.path)) { diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index ed8af13..734620b 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -248,7 +248,7 @@ static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b, if (ret != 0) return ret; - ret = g_strcmp0(key_a->imsi, key_b->imsi); + ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id); if (ret != 0) return ret; @@ -272,7 +272,7 @@ static void __rstns_tree_key_free(gpointer data) FREE(key->app_id); FREE(key->ifname); - FREE(key->imsi); + FREE(key->subscriber_id); FREE(key); } @@ -456,14 +456,14 @@ static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value) "limit [ (%lld) bytes], " "warn_limit [ (%lld) bytes], " "counter [ (%lld) bytes], " - "roaming [%d], imsi [%s]", + "roaming [%d], subscriber_id [%s]", rstn_value->restriction_id, rstn_key->app_id, rstn_value->classid , rstn_key->ifname, rstn_key->iftype, rstn_value->rst_state, rstn_value->data_limit, rstn_value->data_warn_limit, rstn_value->data_counter, - rstn_key->roaming, rstn_key->imsi); + rstn_key->roaming, rstn_key->subscriber_id); } static void __process_restriction(enum traffic_restriction_type rst_type, @@ -880,9 +880,9 @@ static gboolean __update_app_statistics(gpointer key, gpointer value, stat_key.iftype = default_connection->type; if (STC_IFACE_DATACALL == stat_key.iftype) - stat_key.imsi = g_strdup(default_connection->imsi); + stat_key.subscriber_id = g_strdup(default_connection->subscriber_id); else - stat_key.imsi = g_strdup("noneimsi"); + stat_key.subscriber_id = g_strdup("none_subid"); g_strlcpy(stat_key.ifname, default_connection->ifname, MAX_IFACE_LENGTH); @@ -899,7 +899,7 @@ static gboolean __update_app_statistics(gpointer key, gpointer value, app_value->counter.in_bytes = 0; FREE(stat.app_id); - FREE(stat_key.imsi); + FREE(stat_key.subscriber_id); return FALSE; } @@ -1298,7 +1298,7 @@ static stc_error_e __rstn_tree_add(stc_rstn_key_s *key, rstn_key->app_id = g_strdup(key->app_id); rstn_key->ifname = g_strdup(key->ifname); - rstn_key->imsi = g_strdup(key->imsi); + rstn_key->subscriber_id = g_strdup(key->subscriber_id); rstn_key->iftype = key->iftype; rstn_key->roaming = key->roaming; @@ -1335,7 +1335,7 @@ static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info, key.app_id = g_strdup(info->app_id); key.ifname = g_strdup(info->ifname); - key.imsi = g_strdup(info->imsi); + key.subscriber_id = g_strdup(info->subscriber_id); key.iftype = info->iftype; key.roaming = info->roaming; @@ -1355,7 +1355,7 @@ static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info, FREE(key.app_id); FREE(key.ifname); - FREE(key.imsi); + FREE(key.subscriber_id); return ret; } @@ -1510,11 +1510,13 @@ static void __excn_hash_foreach_print(gpointer key, gpointer value, process_name, exe_type); } +#if 0 static void __excn_hash_printall(void) { g_hash_table_foreach(g_system->excns_hash, __excn_hash_foreach_print, NULL); } +#endif static gboolean __remove_exception_app(gpointer key, gpointer value, gpointer data) @@ -1880,7 +1882,7 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info) key.app_id = g_strdup(info->app_id); key.ifname = g_strdup(info->ifname); - key.imsi = g_strdup(info->imsi); + key.subscriber_id = g_strdup(info->subscriber_id); key.iftype = info->iftype; key.roaming = info->roaming; @@ -1905,7 +1907,7 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info) FREE(key.app_id); FREE(key.ifname); - FREE(key.imsi); + FREE(key.subscriber_id); return ret; } @@ -1916,7 +1918,7 @@ stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info) stc_rstn_key_s key = { .app_id = g_strdup(info->app_id), .ifname = g_strdup(info->ifname), - .imsi = g_strdup(info->imsi), + .subscriber_id = g_strdup(info->subscriber_id), .iftype = info->iftype, .roaming = info->roaming, }; @@ -1931,7 +1933,7 @@ stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info) FREE(key.app_id); FREE(key.ifname); - FREE(key.imsi); + FREE(key.subscriber_id); return ret; } diff --git a/src/stc-restriction.c b/src/stc-restriction.c index 8550e41..ab3d350 100755 --- a/src/stc-restriction.c +++ b/src/stc-restriction.c @@ -47,7 +47,7 @@ void __initialize_rstn_rule(table_restrictions_info *rule) rule->data_limit = 0; rule->data_warn_limit = 0; rule->roaming = STC_ROAMING_DISABLE; - rule->imsi = NULL; + rule->subscriber_id = NULL; } gboolean __validate_rstn_rule(table_restrictions_info *rule, @@ -76,7 +76,7 @@ gboolean __validate_rstn_rule(table_restrictions_info *rule, return FALSE; } - if (rule->imsi == NULL) { + if (rule->subscriber_id == NULL) { __STC_LOG_FUNC_EXIT__; return FALSE; } @@ -121,8 +121,8 @@ void __stc_restriction_app_info_builder_add(GVariantBuilder *builder, g_variant_builder_add(builder, "{sv}", "roaming", g_variant_new_uint16(info->roaming)); - g_variant_builder_add(builder, "{sv}", "imsi", - g_variant_new_string(info->imsi)); + g_variant_builder_add(builder, "{sv}", "subscriber_id", + g_variant_new_string(info->subscriber_id)); __STC_LOG_FUNC_EXIT__; } @@ -206,11 +206,11 @@ static void __stc_extract_restriction_rule(const char *key, GVariant *value, rule->roaming = g_variant_get_uint16(value); STC_LOGD("roaming: [%u]", rule->roaming); - } else if (!g_strcmp0(key, "imsi")) { + } else if (!g_strcmp0(key, "subscriber_id")) { guint str_length; const gchar *str = g_variant_get_string(value, &str_length); - rule->imsi = g_strdup(str); - STC_LOGD("imsi: [%s]", rule->imsi); + rule->subscriber_id = g_strdup(str); + STC_LOGD("subscriber_id: [%s]", rule->subscriber_id); } else { STC_LOGD("Unknown select rule"); @@ -322,7 +322,7 @@ gboolean handle_restriction_unset(StcRestriction *object, return TRUE; } - table_restrictions_delete(rule.app_id, rule.iftype, rule.imsi); + table_restrictions_delete(rule.app_id, rule.iftype, rule.subscriber_id); /* remove restriction rule from runtime structure */ stc_monitor_rstns_tree_remove(&rule); diff --git a/src/stc-statistics.c b/src/stc-statistics.c index b20e3bb..fd4544d 100755 --- a/src/stc-statistics.c +++ b/src/stc-statistics.c @@ -113,10 +113,10 @@ void __stc_extract_reset_rule(const char *key, GVariant *value, rule->app_id = g_variant_dup_string(value, &len); STC_LOGD("app_id: [%s]", rule->app_id); - } else if (!g_strcmp0(key, "imsi")) { + } else if (!g_strcmp0(key, "subscriber_id")) { gsize len = 0; - rule->imsi = g_variant_dup_string(value, &len); - STC_LOGD("imsi: [%s]", rule->imsi); + rule->subscriber_id = g_variant_dup_string(value, &len); + STC_LOGD("subscriber_id: [%s]", rule->subscriber_id); } else if (!g_strcmp0(key, "iftype")) { rule->iftype = g_variant_get_uint16(value); @@ -175,8 +175,8 @@ void __stc_statistics_app_info_builder_add(GVariantBuilder *builder, g_variant_builder_add(builder, "{sv}", "ifname", g_variant_new_string(info->ifname)); - g_variant_builder_add(builder, "{sv}", "imsi", - g_variant_new_string(info->imsi)); + g_variant_builder_add(builder, "{sv}", "subscriber_id", + g_variant_new_string(info->subscriber_id)); g_variant_builder_add(builder, "{sv}", "iftype", g_variant_new_uint16(info->iftype)); @@ -412,7 +412,7 @@ gboolean handle_statistics_reset(StcStatistics *object, handle_error: FREE(rule.app_id); - FREE(rule.imsi); + FREE(rule.subscriber_id); FREE(rule.interval); __STC_LOG_FUNC_EXIT__; return TRUE; -- 2.7.4 From f59a400b27ff4da933812d36da7c9a67e34ead10 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 21 Sep 2017 10:30:02 +0900 Subject: [PATCH 15/16] Changed cur classid path and initialize Change-Id: Ib7c6bd5ce363a8079750e66943d287e86224e5c6 Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- src/helper/helper-net-cls.c | 19 ++++++++++++++++++- src/helper/helper-net-cls.h | 5 +---- src/monitor/stc-monitor.c | 5 ++++- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 7979283..4a256b3 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.33 +Version: 0.0.34 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 0f2ae30..ef5e4b3 100755 --- a/src/helper/helper-net-cls.c +++ b/src/helper/helper-net-cls.c @@ -26,7 +26,7 @@ #include "helper-file.h" #include "helper-net-cls.h" -#define CUR_CLASSID_PATH "/tmp/cur_classid" +#define CUR_CLASSID_PATH "/var/lib/stc/cur_classid" #define CLASSID_FILE_NAME "net_cls.classid" typedef GArray task_classid_array; @@ -85,6 +85,23 @@ static uint32_t __get_classid_from_cgroup(const char *cgroup, return classid; } +stc_error_e init_current_classid(void) +{ + int ret = 0; + struct stat stat_buf; + + if (stat(STC_CGROUP_NETWORK, &stat_buf) != 0) { + uint32_t classid = STC_RESERVED_CLASSID_MAX; + ret = fwrite_uint(CUR_CLASSID_PATH, classid); + if (ret < 0) { + STC_LOGE("Can not init current classid"); + return STC_ERROR_FAIL; + } + } + + return STC_ERROR_NONE; +} + uint32_t get_classid_by_app_id(const char *app_id, int create) { int ret = 0; diff --git a/src/helper/helper-net-cls.h b/src/helper/helper-net-cls.h index 24cd993..d27a5b2 100755 --- a/src/helper/helper-net-cls.h +++ b/src/helper/helper-net-cls.h @@ -33,10 +33,7 @@ enum { typedef GArray int_array; -/** - * @desc Get appid from classid task table. At present it is package name. - */ -char *get_app_id_by_pid(const pid_t pid); +stc_error_e init_current_classid(void); /** * @desc take classid from net_cls cgroup by appid diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 734620b..fc7b7bb 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -1500,6 +1500,7 @@ static stc_error_e __process_update_background(void) return STC_ERROR_NONE; } +#if 0 static void __excn_hash_foreach_print(gpointer key, gpointer value, gpointer data) { @@ -1510,7 +1511,6 @@ static void __excn_hash_foreach_print(gpointer key, gpointer value, process_name, exe_type); } -#if 0 static void __excn_hash_printall(void) { g_hash_table_foreach(g_system->excns_hash, @@ -1573,6 +1573,9 @@ stc_error_e stc_monitor_init(void) ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!"); + /* initializing current classid */ + init_current_classid(); + /* initializing cgroups */ cgroup_init(); -- 2.7.4 From 773f22751fe2d65f358533a0ef203ab984dca872 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 21 Sep 2017 14:15:51 +0900 Subject: [PATCH 16/16] Set STC_DEBUG_LOG flag Change-Id: Ic4cfd5f7eb3fa1014db9486479d567e092486e1c Signed-off-by: hyunuktak --- include/stc-manager.h | 2 ++ packaging/stc-manager.spec | 2 +- src/helper/helper-cgroup.c | 6 ++++++ src/helper/helper-nfacct-rule.c | 14 ++++++++++---- src/monitor/stc-monitor.c | 8 ++++++++ 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/include/stc-manager.h b/include/stc-manager.h index 990db68..37e5d97 100755 --- a/include/stc-manager.h +++ b/include/stc-manager.h @@ -31,6 +31,8 @@ #define STC_TOTAL_IPV4 "TOTAL_IPV4" #define STC_TOTAL_IPV6 "TOTAL_IPV6" +#define STC_DEBUG_LOG 0 + typedef enum { STC_CANCEL = 0, /**< cancel */ STC_CONTINUE = 1, /**< continue */ diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 4a256b3..d0e6065 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.34 +Version: 0.0.35 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-cgroup.c b/src/helper/helper-cgroup.c index aa01d16..c636bc2 100755 --- a/src/helper/helper-cgroup.c +++ b/src/helper/helper-cgroup.c @@ -116,7 +116,9 @@ int cgroup_write_node_uint32(const char *cgroup_name, { char buf[MAX_PATH_LENGTH]; snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name); +#if STC_DEBUG_LOG STC_LOGD("cgroup_buf %s, value %d\n", buf, value); +#endif return fwrite_uint(buf, value); } @@ -125,7 +127,9 @@ int cgroup_write_node_str(const char *cgroup_name, { char buf[MAX_PATH_LENGTH]; snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name); +#if STC_DEBUG_LOG STC_LOGD("cgroup_buf %s, string %s\n", buf, string); +#endif return fwrite_str(buf, string); } @@ -136,7 +140,9 @@ int cgroup_read_node_uint32(const char *cgroup_name, int ret; snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name); ret = fread_uint(buf, value); +#if STC_DEBUG_LOG STC_LOGD("cgroup_buf %s, value %d\n", buf, *value); +#endif return ret; } diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index a2a598f..a8474a1 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -156,7 +156,9 @@ stc_error_e nfacct_send_del(nfacct_rule_s *counter) { struct genl req; +#if STC_DEBUG_LOG STC_LOGD("send remove request for %s", counter->name); +#endif prepare_netlink_msg(&req, NFNL_MSG_ACCT_DEL, NLM_F_ACK); add_string_attr(&req, counter->name, NFACCT_NAME); @@ -432,7 +434,9 @@ static bool is_rule_present(const char *cmd_buf) strncpy(exec_buf + (cmd_pos - cmd_buf), IPTABLES_CHECK, sizeof(IPTABLES_CHECK) - 1); +#if STC_DEBUG_LOG STC_LOGD("check rule %s", exec_buf); +#endif args = g_strsplit_set(exec_buf, " ", -1); @@ -468,8 +472,9 @@ stc_error_e exec_iptables_cmd(const char *cmd_buf, pid_t *cmd_pid) int ret; char *save_ptr = NULL; - STC_LOGD("executing iptables cmd %s in forked process", - cmd_buf); +#if STC_DEBUG_LOG + STC_LOGD("executing iptables cmd %s in forked process", cmd_buf); +#endif if (is_rule_present(cmd_buf)) { STC_LOGD("Rule %s already present", cmd_buf); @@ -518,8 +523,9 @@ stc_error_e exec_ip6tables_cmd(const char *cmd_buf, pid_t *cmd_pid) int ret; char *save_ptr = NULL; - STC_LOGD("executing ip6tables cmd %s in forked process", - cmd_buf); +#if STC_DEBUG_LOG + STC_LOGD("executing ip6tables cmd %s in forked process", cmd_buf); +#endif if (is_rule_present(cmd_buf)) { STC_LOGD("Rule %s already present", cmd_buf); diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index fc7b7bb..0e34821 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -292,6 +292,7 @@ static void __processes_tree_printall(GTree *processes) g_tree_foreach(processes, __processes_tree_foreach_print, NULL); } +#if STC_DEBUG_LOG static gboolean __apps_tree_foreach_print(gpointer key, gpointer value, gpointer data) { @@ -308,6 +309,7 @@ static gboolean __apps_tree_foreach_print(gpointer key, gpointer value, __processes_tree_printall(app_value->processes); return FALSE; } +#endif #if 0 static void __apps_tree_printall(void) @@ -323,7 +325,9 @@ static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value, 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"); +#endif return FALSE; } @@ -964,14 +968,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); +#endif 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); +#endif break; default: STC_LOGE("unknown iotype"); -- 2.7.4