From: hyunuktak Date: Thu, 8 Feb 2018 00:44:18 +0000 (+0900) Subject: Moved exception list to plugin X-Git-Tag: accepted/tizen/unified/20180222.080217~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1582744d78efda2bc85b7cdf3cb6ece204803e5b;p=platform%2Fcore%2Fconnectivity%2Fstc-manager.git Moved exception list to plugin Change-Id: Iec04f0f7aba1862cde610a9e3c4d7c93f67674e0 Signed-off-by: hyunuktak --- diff --git a/include/stc-manager-plugin.h b/include/stc-manager-plugin.h index f4233da..f0409b2 100755 --- a/include/stc-manager-plugin.h +++ b/include/stc-manager-plugin.h @@ -21,17 +21,21 @@ #include "stc-plugin.h" -void stc_manager_plugin_init(void); -void stc_manager_plugin_deinit(void); +int stc_manager_plugin_init(void); +int stc_manager_plugin_deinit(void); -int stc_send_warn_message_to_net_popup(const char *content, +int stc_manager_plugin_send_warn_message(const char *content, const char *type, const char *app_id, const char *iftype, const char *warn); -int stc_send_restriction_message_to_net_popup(const char *content, +int stc_manager_plugin_send_restriction_message(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, +int stc_manager_plugin_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); +int stc_manager_plugin_deregister_state_changed_cb(stc_s *stc); + +int stc_manager_plugin_fill_exception_list(void); +int stc_manager_plugin_update_exception_list(void); +int stc_manager_plugin_check_exception_by_cmdline(char *cmdline); #endif /* __STC_MANAGER_PLUGIN_H__ */ diff --git a/include/stc-plugin.h b/include/stc-plugin.h index 4d843b8..ffb5767 100755 --- a/include/stc-plugin.h +++ b/include/stc-plugin.h @@ -28,20 +28,49 @@ typedef stc_error_e (*stc_plugin_app_state_changed_cb)(stc_cmd_type_e cmd, stc_app_type_e app_type); typedef struct { - int(*send_restriction_message_to_net_popup) (const char *, + void (*initialize_plugin) (void); + void (*deinitialize_plugin) (void); + /* popup */ + int (*send_restriction_message_to_net_popup) (const char *, const char *, const char *, const char *, const char *); - int(*send_warn_message_to_net_popup) (const char *, + int (*send_warn_message_to_net_popup) (const char *, const char *, const char *, const char *, const char *); + + /* app status */ 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); + + /* exception */ + int (*fill_exception_list) (void); + int (*update_exception_list) (void); + int (*check_exception_by_cmdline) (char *cmdline); } stc_plugin_s; +void stc_plugin_initialize(void); +void stc_plugin_deinitialize(void); + +int stc_plugin_popup_send_restriction_message(const char *content, + const char *type, const char *app_id, const char *iftype, const char *limit); +int stc_plugin_popup_send_restriction_message(const char *content, + const char *type, const char *app_id, const char *iftype, const char *limit); + +int stc_plugin_appstatus_register_changed_cb(stc_s *stc, + stc_plugin_app_state_changed_cb cb, + void *data); +int stc_plugin_appstatus_deregister_changed_cb(stc_s *stc); + +int stc_plugin_exception_init(void); +int stc_plugin_exception_deinit(void); +int stc_plugin_exception_fill_list(void); +int stc_plugin_exception_update_list(void); +int stc_plugin_exception_check_by_cmdline(char *cmdline); + #endif /* __STC_PLUGIN_H__ */ diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index d50d114..3b6e441 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.47 +Version: 0.0.48 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt index afde5f4..bad6651 100755 --- a/plugin/CMakeLists.txt +++ b/plugin/CMakeLists.txt @@ -28,6 +28,9 @@ ADD_DEFINITIONS("-DUSE_DLOG") SET(SRCS_PLUGIN stc-plugin.c + stc-plugin-popup.c + stc-plugin-appstatus.c + stc-plugin-exception.c ) # library build diff --git a/plugin/stc-plugin-appstatus.c b/plugin/stc-plugin-appstatus.c new file mode 100755 index 0000000..d642cd6 --- /dev/null +++ b/plugin/stc-plugin-appstatus.c @@ -0,0 +1,186 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "stc-plugin.h" + +//LCOV_EXCL_START +#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) +{ + 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"); + 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 { + 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 (STC_DEBUG_LOG) { + STC_LOGD("\033[1;36mAPP STATUS\033[0;m: Pkg ID [\033[0;34m%s\033[0;m], " + "App ID [\033[0;32m%s\033[0;m], PID [\033[1;33m%d\033[0;m], Status [%s], Type [%s]", + pkgid, appid, pid, statstr, pkgtype); + } + + if (state_changed_cb) + state_changed_cb(status, pid, appid, pkgid, apptype); + +out: + 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 + } +}; + + +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; +} + +static stc_error_e __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++) { + 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); + } + + return STC_ERROR_NONE; +} + +int stc_plugin_appstatus_register_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 stc_plugin_appstatus_deregister_changed_cb(stc_s *stc) +{ + state_changed_cb = NULL; + __ground_status_monitor_deinit(stc); + return 0; +} +//LCOV_EXCL_STOP diff --git a/plugin/stc-plugin-exception.c b/plugin/stc-plugin-exception.c new file mode 100755 index 0000000..9484d92 --- /dev/null +++ b/plugin/stc-plugin-exception.c @@ -0,0 +1,291 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "stc-plugin.h" + +//LCOV_EXCL_START +#define EXE_TYPE_APPLICATION "app" +#define EXE_TYPE_INSTRUCTION "inst" +#define EXE_TYPE_SYSTEM "sys" +#define EXE_TYPE_SCRIPT "script" + +/* 1 day */ +#define EXCNS_TIMER_INTERVAL 86400 + +#define EXCEPTION_BUF_MAX 64 +#define EXCEPTION_STORAGE "/var/lib/stc/exceptions" + +#define INTERNET_PRIVILEGE "http://tizen.org/privilege/internet" + +typedef struct { + char *process_name; + char *exe_type; +} stc_exceptions_info; + +typedef stc_cb_ret_e +(*stc_exceptions_info_cb)(const stc_exceptions_info *info, + void *user_data); + +static GHashTable *g_excns_hash; /**< exception hash table */ +static GHashTable *g_pkginfo_filter_hash; +static guint g_excns_timer_id; + +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; +} + +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_excns_hash, + __excn_hash_foreach_print, NULL); +} + +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_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; + + if (g_hash_table_insert(g_excns_hash, + g_strdup(info->process_name), + g_strdup(info->exe_type)) != TRUE) + ret = STC_CANCEL; + + return ret; +} + +static gboolean __update_exceptions_app_list(void *user_data) +{ + stc_plugin_exception_update_list(); + return TRUE; +} + +static stc_error_e pkginfo_exceptions_foreach(const stc_exceptions_info_cb exception_cb, + void *user_data) +{ + 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); + + return err; +} + +static stc_error_e table_exceptions_foreach(const stc_exceptions_info_cb exception_cb, + void *user_data) +{ + stc_error_e error_code = STC_ERROR_NONE; + stc_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); + + return error_code; +} + +int stc_plugin_exception_init(void) +{ + g_excns_hash = g_hash_table_new_full(g_str_hash, + g_str_equal, g_free, g_free); + + return STC_ERROR_NONE; +} + +int stc_plugin_exception_deinit(void) +{ + if (g_excns_timer_id > 0) { + g_source_remove(g_excns_timer_id); + g_excns_timer_id = 0; + } + + g_hash_table_destroy(g_excns_hash); + + return STC_ERROR_NONE; +} + +int stc_plugin_exception_fill_list(void) +{ + table_exceptions_foreach(__insert_exception_cb, NULL); + pkginfo_exceptions_foreach(__insert_exception_cb, NULL); + + if (STC_DEBUG_LOG) + __excn_hash_printall(); + + + g_excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL, + __update_exceptions_app_list, + NULL); + + return STC_ERROR_NONE; +} + +int stc_plugin_exception_update_list(void) +{ + __remove_exception_appall(); + pkginfo_exceptions_foreach(__insert_exception_cb, NULL); + + if (STC_DEBUG_LOG) + __excn_hash_printall(); + + return STC_ERROR_NONE; +} + +int stc_plugin_exception_check_by_cmdline(char *cmdline) +{ + char *exe_type = NULL; + + exe_type = g_hash_table_lookup(g_excns_hash, cmdline); + if (!exe_type) + return STC_ERROR_NO_DATA; + + return STC_ERROR_NONE; +} +//LCOV_EXCL_STOP diff --git a/plugin/stc-plugin-popup.c b/plugin/stc-plugin-popup.c new file mode 100755 index 0000000..409c890 --- /dev/null +++ b/plugin/stc-plugin-popup.c @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "stc-plugin.h" + +//LCOV_EXCL_START +int stc_plugin_popup_send_warn_message(const char *content, + const char *type, const char *app_id, const char *iftype, const char *warn) +{ + int ret = 0; + bundle *b = bundle_create(); + + STC_LOGD("Warn message : content[%s] type[%s] app_id[%s] warn[%s]", + content, type, app_id, warn); + + bundle_add(b, "_SYSPOPUP_CONTENT_", content); + bundle_add(b, "_SYSPOPUP_TYPE_", type); + bundle_add(b, "_APP_ID_", app_id); + bundle_add(b, "_IF_TYPE_", iftype); + bundle_add(b, "_WARN_LIMIT_", warn); + + ret = syspopup_launch("net-popup", b); + + bundle_free(b); + + return ret; +} + +int stc_plugin_popup_send_restriction_message(const char *content, + const char *type, const char *app_id, const char *iftype, const char *limit) +{ + int ret = 0; + bundle *b = bundle_create(); + + STC_LOGD("Restriction message : content[%s] type[%s] app_id[%s] limit[%s]", + content, type, app_id, limit); + + bundle_add(b, "_SYSPOPUP_CONTENT_", content); + bundle_add(b, "_SYSPOPUP_TYPE_", type); + bundle_add(b, "_APP_ID_", app_id); + bundle_add(b, "_IF_TYPE_", iftype); + bundle_add(b, "_RESTRICTION_LIMIT_", limit); + + ret = syspopup_launch("net-popup", b); + + bundle_free(b); + + return ret; +} +//LCOV_EXCL_STOP diff --git a/plugin/stc-plugin.c b/plugin/stc-plugin.c index 916a75e..b1b4013 100755 --- a/plugin/stc-plugin.c +++ b/plugin/stc-plugin.c @@ -25,219 +25,36 @@ #include #include "stc-plugin.h" -#include "stc-manager.h" -#include "stc-manager-util.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; - -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) -{ - 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"); - 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 { - 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 (STC_DEBUG_LOG) { - STC_LOGD("\033[1;36mAPP STATUS\033[0;m: Pkg ID [\033[0;34m%s\033[0;m], " - "App ID [\033[0;32m%s\033[0;m], PID [\033[1;33m%d\033[0;m], Status [%s], Type [%s]", - pkgid, appid, pid, statstr, pkgtype); - } - - if (state_changed_cb) - state_changed_cb(status, pid, appid, pkgid, apptype); - -out: - 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 - } -}; - - -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; -} - -static stc_error_e __ground_status_monitor_deinit(stc_s *stc) +//LCOV_EXCL_START +void stc_plugin_initialize(void) { - 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++) { - 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); - } - - return STC_ERROR_NONE; + stc_plugin_exception_init(); } -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) +void stc_plugin_deinitialize(void) { - int ret = 0; - bundle *b = bundle_create(); - - STC_LOGD("Warn message : content[%s] type[%s] app_id[%s] warn[%s]", - content, type, app_id, warn); - - bundle_add(b, "_SYSPOPUP_CONTENT_", content); - bundle_add(b, "_SYSPOPUP_TYPE_", type); - bundle_add(b, "_APP_ID_", app_id); - bundle_add(b, "_IF_TYPE_", iftype); - bundle_add(b, "_WARN_LIMIT_", warn); - - ret = syspopup_launch("net-popup", b); - - bundle_free(b); - - return ret; -} - -static 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 ret = 0; - bundle *b = bundle_create(); - - STC_LOGD("Restriction message : content[%s] type[%s] app_id[%s] limit[%s]", - content, type, app_id, limit); - - bundle_add(b, "_SYSPOPUP_CONTENT_", content); - bundle_add(b, "_SYSPOPUP_TYPE_", type); - bundle_add(b, "_APP_ID_", app_id); - bundle_add(b, "_IF_TYPE_", iftype); - bundle_add(b, "_RESTRICTION_LIMIT_", limit); - - ret = syspopup_launch("net-popup", b); - - bundle_free(b); - - 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; + stc_plugin_exception_deinit(); } API stc_plugin_s stc_plugin = { + .initialize_plugin = + stc_plugin_initialize, + .deinitialize_plugin = + stc_plugin_deinitialize, .send_warn_message_to_net_popup = - __stc_send_warn_message_to_net_popup, + stc_plugin_popup_send_restriction_message, .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 + stc_plugin_popup_send_restriction_message, + .register_state_changed_cb = + stc_plugin_appstatus_register_changed_cb, + .deregister_state_changed_cb = + stc_plugin_appstatus_deregister_changed_cb, + .fill_exception_list = + stc_plugin_exception_fill_list, + .update_exception_list = + stc_plugin_exception_update_list, + .check_exception_by_cmdline = + stc_plugin_exception_check_by_cmdline }; - +//LCOV_EXCL_STOP diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index 0ecce65..b8a7357 100755 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -28,9 +28,6 @@ /* 1 seconds */ #define CONTR_TIMER_INTERVAL 1 -/* 1 day */ -#define EXCNS_TIMER_INTERVAL 86400 - /** * @brief key for processes tree */ @@ -102,8 +99,6 @@ typedef struct { gboolean rstns_tree_updated; 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 d02667f..2fcbfe6 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -208,7 +208,8 @@ static gboolean __check_excn(char *cmdline) return TRUE; ret = stc_monitor_check_excn_by_cmdline(cmdline); - if (ret == STC_ERROR_NO_DATA) + if (ret == STC_ERROR_UNINITIALIZED || + ret == STC_ERROR_NO_DATA) return FALSE; else return TRUE; diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index c8f7b7e..b3edf39 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -27,7 +27,6 @@ #include "counter.h" #include "table-statistics.h" #include "table-counters.h" -#include "stc-exception.h" #include "stc-monitor.h" #include "stc-manager-plugin.h" @@ -552,8 +551,6 @@ static void __process_restriction(enum traffic_restriction_type rst_type, counter.iftype = rstn_key->iftype; g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH); - - generate_counter_name(&counter); g_free(default_ifname); /* iptables rule */ @@ -597,8 +594,6 @@ static void __process_restriction(enum traffic_restriction_type rst_type, counter.iftype = rstn_key->iftype; g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH); - - generate_counter_name(&counter); g_free(default_ifname); /* iptables rule */ @@ -772,7 +767,7 @@ static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key, snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit); - stc_send_warn_message_to_net_popup("warn threshold crossed", + stc_manager_plugin_send_warn_message("warn threshold crossed", "warning_noti", rstn_key->app_id, iftype, byte); @@ -813,7 +808,7 @@ static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key, snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit); - stc_send_restriction_message_to_net_popup("restriction threshold crossed", + stc_manager_plugin_send_restriction_message("restriction threshold crossed", "restriction_noti", rstn_key->app_id, iftype, byte); } @@ -1212,7 +1207,8 @@ static gboolean __update_contr_cb(void *user_data) /* we need to continue the timer */ return TRUE; } -#if 0 + +//LCOV_EXCL_START static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value, gpointer data) { @@ -1227,7 +1223,8 @@ static void __rstn_tree_printall(void) { g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL); } -#endif +//LCOV_EXCL_END + static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree, const stc_rstn_key_s *key) { @@ -1404,7 +1401,9 @@ static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info, static void __fill_restritions_list(void) { table_restrictions_foreach(__insert_restriction_cb, NULL); - //__rstn_tree_printall(); + + if (STC_DEBUG_LOG) + __rstn_tree_printall(); } static gboolean __add_rstn_foreach_application(gpointer key, @@ -1545,75 +1544,10 @@ static stc_error_e __process_update_background(void) } //LCOV_EXCL_STOP -#if 0 -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); -} -#endif - -//LCOV_EXCL_START -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; -} -//LCOV_EXCL_STOP - -static void __remove_exception_appall(void) -{ - g_hash_table_foreach_remove(g_system->excns_hash, //LCOV_EXCL_LINE - __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; - - if (g_hash_table_insert(g_system->excns_hash, - g_strdup(info->process_name), - g_strdup(info->exe_type)) != TRUE) - ret = STC_CANCEL; //LCOV_EXCL_LINE - - return ret; -} - static void __fill_exceptions_list(void) { - table_exceptions_foreach(__insert_exception_cb, NULL); - pkginfo_exceptions_foreach(__insert_exception_cb, NULL); - - /* __excn_hash_printall(); */ -} - -//LCOV_EXCL_START -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_manager_plugin_fill_exception_list(); } -//LCOV_EXCL_STOP stc_error_e stc_monitor_init(void) { @@ -1637,9 +1571,6 @@ 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 < 0) { @@ -1682,10 +1613,6 @@ 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; } @@ -1702,12 +1629,6 @@ 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; @@ -1716,10 +1637,6 @@ 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; @@ -1954,8 +1871,8 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info) value.restriction_id = info->restriction_id; if (value.rst_state > STC_RESTRICTION_UNKNOWN && - value.rst_state < STC_RESTRICTION_LAST_ELEM && - info->app_id) + value.rst_state < STC_RESTRICTION_LAST_ELEM && + info->app_id) value.classid = get_classid_by_app_id(info->app_id, TRUE); else value.classid = STC_UNKNOWN_CLASSID; @@ -2005,15 +1922,7 @@ stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info) 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; + return stc_manager_plugin_check_exception_by_cmdline(cmdline); } int stc_monitor_get_counter_socket(void) diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index df53f9b..46a3d42 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -194,7 +194,8 @@ static void __stc_manager_gdbus_on_bus_acquired(GDBusConnection *connection, iptables_init(); stc_default_connection_monitor_init(stc); - stc_register_state_changed_cb(stc, stc_manager_app_status_changed, NULL); + stc_manager_plugin_register_state_changed_cb(stc, + stc_manager_app_status_changed, NULL); __STC_LOG_FUNC_EXIT__; } @@ -237,7 +238,7 @@ void stc_manager_gdbus_deinit(gpointer stc_data) __STC_LOG_FUNC_ENTER__; stc_s *stc = (stc_s *)stc_data; - stc_deregister_state_changed_cb(stc); + stc_manager_plugin_deregister_state_changed_cb(stc); stc_default_connection_monitor_deinit(stc); g_bus_unown_name(stc->gdbus_owner_id); diff --git a/src/stc-manager-plugin.c b/src/stc-manager-plugin.c index a97ae73..f4aaae9 100755 --- a/src/stc-manager-plugin.c +++ b/src/stc-manager-plugin.c @@ -17,7 +17,6 @@ #include #include "stc-manager.h" -#include "stc-plugin.h" #include "stc-manager-plugin.h" static gboolean stc_plugin_enabled = FALSE; @@ -25,7 +24,7 @@ static void *handle_plugin; static stc_plugin_s *stc_plugin; //LCOV_EXCL_START -void stc_manager_plugin_init(void) +int stc_manager_plugin_init(void) { __STC_LOG_FUNC_ENTER__; @@ -33,7 +32,7 @@ void stc_manager_plugin_init(void) if (!handle_plugin) { STC_LOGE("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror()); __STC_LOG_FUNC_EXIT__; - return; + return STC_ERROR_UNINITIALIZED; } stc_plugin = dlsym(handle_plugin, "stc_plugin"); @@ -41,28 +40,34 @@ void stc_manager_plugin_init(void) STC_LOGE("Can't load symbol: %s", dlerror()); dlclose(handle_plugin); __STC_LOG_FUNC_EXIT__; - return; + return STC_ERROR_UNINITIALIZED; } stc_plugin_enabled = TRUE; + stc_plugin->initialize_plugin(); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_NONE; } -void stc_manager_plugin_deinit(void) +int stc_manager_plugin_deinit(void) { __STC_LOG_FUNC_ENTER__; if (!stc_plugin_enabled) { __STC_LOG_FUNC_EXIT__; - return; + return STC_ERROR_UNINITIALIZED; } + stc_plugin->deinitialize_plugin(); + stc_plugin_enabled = FALSE; dlclose(handle_plugin); __STC_LOG_FUNC_EXIT__; + return STC_ERROR_NONE; } -int stc_send_warn_message_to_net_popup(const char *content, const char *type, +int stc_manager_plugin_send_warn_message(const char *content, const char *type, const char *app_id, const char *iftype, const char *warn) { @@ -71,13 +76,13 @@ int stc_send_warn_message_to_net_popup(const char *content, const char *type, if (!stc_plugin_enabled) { STC_LOGE("Plugin wasn't enabled"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } if (!stc_plugin) { STC_LOGE("Plugin wasn't loaded"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } __STC_LOG_FUNC_EXIT__; @@ -85,7 +90,7 @@ int stc_send_warn_message_to_net_popup(const char *content, const char *type, iftype, warn); } -int stc_send_restriction_message_to_net_popup(const char *content, +int stc_manager_plugin_send_restriction_message(const char *content, const char *type, const char *app_id, const char *iftype, @@ -96,13 +101,13 @@ int stc_send_restriction_message_to_net_popup(const char *content, if (!stc_plugin_enabled) { STC_LOGE("Plugin wasn't enabled"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } if (!stc_plugin) { STC_LOGE("Plugin wasn't loaded"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } __STC_LOG_FUNC_EXIT__; @@ -111,7 +116,7 @@ int stc_send_restriction_message_to_net_popup(const char *content, limit); } -int stc_register_state_changed_cb(stc_s *stc, +int stc_manager_plugin_register_state_changed_cb(stc_s *stc, stc_plugin_app_state_changed_cb cb, void *data) { @@ -120,35 +125,87 @@ int stc_register_state_changed_cb(stc_s *stc, if (!stc_plugin_enabled) { STC_LOGE("Plugin wasn't enabled"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } if (!stc_plugin) { STC_LOGE("Plugin wasn't loaded"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } __STC_LOG_FUNC_EXIT__; return stc_plugin->register_state_changed_cb(stc, cb, data); } -int stc_deregister_state_changed_cb(stc_s *stc) + +int stc_manager_plugin_deregister_state_changed_cb(stc_s *stc) { __STC_LOG_FUNC_ENTER__; if (!stc_plugin_enabled) { STC_LOGE("Plugin wasn't enabled"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } if (!stc_plugin) { STC_LOGE("Plugin wasn't loaded"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } __STC_LOG_FUNC_EXIT__; return stc_plugin->deregister_state_changed_cb(stc); } + +int stc_manager_plugin_fill_exception_list(void) +{ + __STC_LOG_FUNC_ENTER__; + + if (!stc_plugin_enabled) { + STC_LOGE("Plugin wasn't enabled"); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + if (!stc_plugin) { + STC_LOGE("Plugin wasn't loaded"); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + __STC_LOG_FUNC_EXIT__; + return stc_plugin->fill_exception_list(); +} + +int stc_manager_plugin_update_exception_list(void) +{ + __STC_LOG_FUNC_ENTER__; + + if (!stc_plugin_enabled) { + STC_LOGE("Plugin wasn't enabled"); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + if (!stc_plugin) { + STC_LOGE("Plugin wasn't loaded"); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + __STC_LOG_FUNC_EXIT__; + return stc_plugin->update_exception_list(); +} + +int stc_manager_plugin_check_exception_by_cmdline(char *cmdline) +{ + if (!stc_plugin_enabled) + return STC_ERROR_UNINITIALIZED; + + if (!stc_plugin) + return STC_ERROR_UNINITIALIZED; + + return stc_plugin->check_exception_by_cmdline(cmdline); +} //LCOV_EXCL_STOP diff --git a/src/stc-manager.c b/src/stc-manager.c index d9a4d80..b815400 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -88,12 +88,12 @@ static stc_s *__stc_manager_init(void) cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT); EXEC(STC_ERROR_NONE, stc_db_initialize()); + stc_manager_plugin_init(); err = stc_monitor_init(); if (err != STC_ERROR_NONE) goto handle_error; - stc_manager_plugin_init(); stc_app_lifecycle_monitor_init(); stc_manager_gdbus_init((gpointer)stc);