From 39bd4f4e677bdf2d75d5f8ad5cf9bb5cab9f3a7a Mon Sep 17 00:00:00 2001 From: prasadam-p Date: Mon, 12 Aug 2019 12:57:01 +0530 Subject: [PATCH 01/16] Fixed CID 1038526. Moved the log before the pcap_close call to fix USE_AFTER_FREE error. Change-Id: Ie586c93e538c0297cc3dfa5a18719fa21db137f3 Signed-off-by: prashanth prasadam --- plugin/pcap/stc-plugin-pcap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/pcap/stc-plugin-pcap.c b/plugin/pcap/stc-plugin-pcap.c index 0859700..862a0be 100755 --- a/plugin/pcap/stc-plugin-pcap.c +++ b/plugin/pcap/stc-plugin-pcap.c @@ -1441,8 +1441,8 @@ static gpointer __pcap_thread_func(gpointer data) pcap_loop(pcap_data->handle, 0, __pcap_handler, (u_char *)pcap_data); STC_LOGD("Pcap loop end [%s]", name); + STC_LOGD("Pcap before closing [%p]", pcap_data->handle); pcap_close(pcap_data->handle); - STC_LOGD("Pcap closed [%p]", pcap_data->handle); thread_exit: FREE(name); -- 2.7.4 From 6f2558cb5a4c1e4b7c2d3137cc6d1e67d587f153 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Mon, 12 Aug 2019 10:48:52 +0900 Subject: [PATCH 02/16] Add os upgrade file for database Change-Id: Ia679c169d26d8e9163d385362c08875776abc2ce Signed-off-by: hyunuk.tak --- packaging/stc-manager.spec | 8 ++++++ scripts/500.stc-manager_upgrade.sh | 52 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 scripts/500.stc-manager_upgrade.sh diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 30d4919..2ed5dc0 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -10,6 +10,8 @@ Source0: %{name}-%{version}.tar.gz %define enable_statistics YES %define enable_restriction YES %define database_full_path /opt/usr/dbspace/.stc-manager-datausage.db +%define upgrade_script_filename 500.stc-manager_upgrade.sh +%define upgrade_script_path /usr/share/upgrade/scripts BuildRequires: cmake BuildRequires: pkgconfig(glib-2.0) @@ -143,6 +145,10 @@ cp resources/dbus/stc-manager.conf %{buildroot}%{_sysconfdir}/dbus-1/system.d/st mkdir -p %{buildroot}%{_datadir}/dbus-1/system-services/ cp resources/dbus/net.stc-manager.service %{buildroot}%{_datadir}/dbus-1/system-services/net.stc-manager.service +#OS Upgrade +mkdir -p %{buildroot}%{upgrade_script_path} +cp -f scripts/%{upgrade_script_filename} %{buildroot}%{upgrade_script_path} + %files %manifest %{name}.manifest %license LICENSE @@ -168,6 +174,8 @@ cp resources/dbus/net.stc-manager.service %{buildroot}%{_datadir}/dbus-1/system- %{_bindir}/gtest* %endif +%{upgrade_script_path}/%{upgrade_script_filename} + %files plugin-appstatus %manifest %{name}.manifest %attr(644, -,-) %{_datadir}/icons/*.png diff --git a/scripts/500.stc-manager_upgrade.sh b/scripts/500.stc-manager_upgrade.sh new file mode 100644 index 0000000..ad0c84b --- /dev/null +++ b/scripts/500.stc-manager_upgrade.sh @@ -0,0 +1,52 @@ +#!/bin/sh +PATH=/bin:/usr/bin:/sbin:/usr/sbin + +#----------------------------------------------------------------# +# stc-manager patch script for upgrade (4.0 -> the latest tizen) # +#----------------------------------------------------------------# + +if [ "$OLD_VER" = "4.0.0.0" ]; then + sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "alter table restrictions rename column rst_state to rstn_type;" + + sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "CREATE TABLE IF NOT EXISTS fw_lock ( + name TEXT PRIMARY KEY, + state INT + );" + + sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "CREATE TABLE IF NOT EXISTS fw_chains ( + chain TEXT PRIMARY KEY, + target INT, + priority INT + );" + + sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "CREATE TABLE IF NOT EXISTS fw_rules ( + key BIGINT PRIMARY KEY, + chain TEXT NOT NULL, + direction INT, + s_ip_type INT, + d_ip_type INT, + s_port_type INT, + d_port_type INT, + protocol INT, + family INT, + s_ip1 TEXT NOT NULL, + s_ip2 TEXT NOT NULL, + d_ip1 TEXT NOT NULL, + d_ip2 TEXT NOT NULL, + s_port1 INT, + s_port2 INT, + d_port1 INT, + d_port2 INT, + ifname TEXT NOT NULL, + target INT, + log_level INT, + log_prefix TEXT NOT NULL, + nflog_group INT, + nflog_prefix TEXT NOT NULL, + nflog_range INT, + nflog_threshold INT, + identifier TEXT NOT NULL + );" + + sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "CREATE INDEX IF NOT EXISTS rules_index ON fw_rules (chain, target);" +fi -- 2.7.4 From 40194410f8b1589c1a3a0a6036a2976ab1a564f6 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Tue, 17 Sep 2019 17:12:33 +0900 Subject: [PATCH 03/16] Remove to check old version from upgrade script Change-Id: Ic6e537737db32b00d5ca625d390035e53092423d Signed-off-by: hyunuk.tak --- packaging/stc-manager.spec | 2 +- scripts/500.stc-manager_upgrade.sh | 80 +++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 2dc6918..69cbe48 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.95 +Version: 0.0.96 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/scripts/500.stc-manager_upgrade.sh b/scripts/500.stc-manager_upgrade.sh index ad0c84b..7c1945e 100644 --- a/scripts/500.stc-manager_upgrade.sh +++ b/scripts/500.stc-manager_upgrade.sh @@ -5,48 +5,46 @@ PATH=/bin:/usr/bin:/sbin:/usr/sbin # stc-manager patch script for upgrade (4.0 -> the latest tizen) # #----------------------------------------------------------------# -if [ "$OLD_VER" = "4.0.0.0" ]; then - sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "alter table restrictions rename column rst_state to rstn_type;" +sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "ALTER TABLE restrictions RENAME COLUMN rst_state TO rstn_type;" - sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "CREATE TABLE IF NOT EXISTS fw_lock ( - name TEXT PRIMARY KEY, - state INT - );" +sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "CREATE TABLE IF NOT EXISTS fw_lock ( + name TEXT PRIMARY KEY, + state INT +);" - sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "CREATE TABLE IF NOT EXISTS fw_chains ( - chain TEXT PRIMARY KEY, - target INT, - priority INT - );" +sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "CREATE TABLE IF NOT EXISTS fw_chains ( + chain TEXT PRIMARY KEY, + target INT, + priority INT +);" - sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "CREATE TABLE IF NOT EXISTS fw_rules ( - key BIGINT PRIMARY KEY, - chain TEXT NOT NULL, - direction INT, - s_ip_type INT, - d_ip_type INT, - s_port_type INT, - d_port_type INT, - protocol INT, - family INT, - s_ip1 TEXT NOT NULL, - s_ip2 TEXT NOT NULL, - d_ip1 TEXT NOT NULL, - d_ip2 TEXT NOT NULL, - s_port1 INT, - s_port2 INT, - d_port1 INT, - d_port2 INT, - ifname TEXT NOT NULL, - target INT, - log_level INT, - log_prefix TEXT NOT NULL, - nflog_group INT, - nflog_prefix TEXT NOT NULL, - nflog_range INT, - nflog_threshold INT, - identifier TEXT NOT NULL - );" +sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "CREATE TABLE IF NOT EXISTS fw_rules ( + key BIGINT PRIMARY KEY, + chain TEXT NOT NULL, + direction INT, + s_ip_type INT, + d_ip_type INT, + s_port_type INT, + d_port_type INT, + protocol INT, + family INT, + s_ip1 TEXT NOT NULL, + s_ip2 TEXT NOT NULL, + d_ip1 TEXT NOT NULL, + d_ip2 TEXT NOT NULL, + s_port1 INT, + s_port2 INT, + d_port1 INT, + d_port2 INT, + ifname TEXT NOT NULL, + target INT, + log_level INT, + log_prefix TEXT NOT NULL, + nflog_group INT, + nflog_prefix TEXT NOT NULL, + nflog_range INT, + nflog_threshold INT, + identifier TEXT NOT NULL +);" - sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "CREATE INDEX IF NOT EXISTS rules_index ON fw_rules (chain, target);" -fi +sqlite3 /opt/usr/dbspace/.stc-manager-datausage.db "CREATE INDEX IF NOT EXISTS rules_index ON fw_rules (chain, target);" -- 2.7.4 From f24d823df9518db56e51f8c89bf03df673a1e316 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Thu, 26 Sep 2019 14:43:05 +0900 Subject: [PATCH 04/16] Fix to appropriate variable for default connection Change-Id: I43a8a7aca0c1fb4f62b3c41dc58ef40359bbeabf Signed-off-by: hyunuk.tak --- plugin/monitor/stc-plugin-monitor-connection.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/monitor/stc-plugin-monitor-connection.c b/plugin/monitor/stc-plugin-monitor-connection.c index 1988a43..a305002 100755 --- a/plugin/monitor/stc-plugin-monitor-connection.c +++ b/plugin/monitor/stc-plugin-monitor-connection.c @@ -609,7 +609,7 @@ static void __vconf_key_callback(keynode_t *node, void *user_data) /* add monitoring for tethering if active found */ if (g_default_connection->tether_state == TRUE && g_default_connection->tether_iface.ifname) { __print_tether_connection_info(); - stc_monitor_update_by_connection(&g_default_connection); + stc_monitor_update_by_connection(g_default_connection); stc_plugin_firewall_update(); STC_LOGI("Data monitoring started for tethering iface !"); return; @@ -617,7 +617,7 @@ static void __vconf_key_callback(keynode_t *node, void *user_data) /* remove monitoring for tethering if in-active found */ if (g_default_connection->tether_state == FALSE && g_default_connection->tether_iface.ifname) { - stc_monitor_update_by_connection(&g_default_connection); + stc_monitor_update_by_connection(g_default_connection); g_free(g_default_connection->tether_iface.ifname); g_default_connection->tether_iface.ifname = NULL; g_default_connection->tether_iface.type = STC_IFACE_UNKNOWN; -- 2.7.4 From e9843c98cd3aea6d2a3fd663e8fbae05a01d8fc1 Mon Sep 17 00:00:00 2001 From: Semun Lee Date: Thu, 26 Dec 2019 15:00:50 +0900 Subject: [PATCH 05/16] Fix build warnings for gcc 9 Change-Id: I3bd2818c304e7a77987009d296e813d18bade7bb Signed-off-by: Semun Lee --- plugin/pcap/stc-plugin-pcap.c | 2 +- src/helper/helper-nfacct-rule.h | 2 +- src/helper/helper-procfs.c | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/plugin/pcap/stc-plugin-pcap.c b/plugin/pcap/stc-plugin-pcap.c index 862a0be..018254c 100755 --- a/plugin/pcap/stc-plugin-pcap.c +++ b/plugin/pcap/stc-plugin-pcap.c @@ -1153,7 +1153,7 @@ static void __pcap_nflog_tlv_info(const u_char **packet, GString *value; u_int16_t value_len; u_char *value_pos = NULL; - gchar *value_str = NULL; + gchar *value_str = ""; *pktlen -= SIZE_NFLOG_HDR; diff --git a/src/helper/helper-nfacct-rule.h b/src/helper/helper-nfacct-rule.h index 97c9b5e..80b9f83 100755 --- a/src/helper/helper-nfacct-rule.h +++ b/src/helper/helper-nfacct-rule.h @@ -25,7 +25,7 @@ #include "helper-nl.h" -#define NFACCT_NAME_MAX 32 +#define NFACCT_NAME_MAX 64 typedef enum { NFACCT_COUNTER_UNKNOWN, diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c index 8cd0461..60e5db2 100644 --- a/src/helper/helper-procfs.c +++ b/src/helper/helper-procfs.c @@ -79,7 +79,8 @@ API int proc_get_cmdline(pid_t pid, char *cmdline) filename = filename + 1; } - strncpy(cmdline, filename, PROC_NAME_MAX-1); + strncpy(cmdline, filename, PROC_NAME_MAX - 1); + cmdline[PROC_NAME_MAX - 1] = '\0'; return STC_ERROR_NONE; } -- 2.7.4 From dc036545f577295486ea444743738e0352a885fe Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Fri, 31 Jan 2020 11:00:03 +0900 Subject: [PATCH 06/16] Fix ABI issue for app state and roaming Change-Id: Ib6bf8400e20adfed0fa98781fb19bc131995e0a6 Signed-off-by: hyunuk.tak --- include/stc-manager.h | 7 ++++--- plugin/monitor/stc-plugin-monitor-app.c | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/stc-manager.h b/include/stc-manager.h index bc2047f..cfa2404 100755 --- a/include/stc-manager.h +++ b/include/stc-manager.h @@ -133,8 +133,8 @@ typedef enum { */ typedef enum { STC_APP_STATE_UNKNOWN, - STC_APP_STATE_FOREGROUND, /** < foreground state */ - STC_APP_STATE_BACKGROUND, /** < background state */ + STC_APP_STATE_FOREGROUND = 1 << 1, /** < foreground state */ + STC_APP_STATE_BACKGROUND = 1 << 2, /** < background state */ STC_APP_STATE_LAST_ELEM } stc_app_state_e; @@ -179,8 +179,9 @@ typedef enum { * @brief Network roaming type */ typedef enum { - STC_ROAMING_DISABLE, /**< not in roaming */ + STC_ROAMING_UNKNOWN, STC_ROAMING_ENABLE, /**< in roaming */ + STC_ROAMING_DISABLE, /**< not in roaming */ } stc_roaming_type_e; /** diff --git a/plugin/monitor/stc-plugin-monitor-app.c b/plugin/monitor/stc-plugin-monitor-app.c index d7b3152..ca4a88f 100755 --- a/plugin/monitor/stc-plugin-monitor-app.c +++ b/plugin/monitor/stc-plugin-monitor-app.c @@ -101,7 +101,7 @@ static void __app_update_statistics(gpointer key, stat.app_id = g_strdup(app_value->app_id); stat.snd_count = app_value->counter.out_bytes; stat.rcv_count = app_value->counter.in_bytes; - stat.is_roaming = conn->roaming; + stat.is_roaming = (conn->roaming == TRUE) ? STC_ROAMING_ENABLE : STC_ROAMING_DISABLE; if (strstr(stat.app_id, "_BACKGROUND")) { stat.ground = STC_APP_STATE_BACKGROUND; -- 2.7.4 From 968e1a84eda591be4e85b02902e5455434be53bd Mon Sep 17 00:00:00 2001 From: Jaehyun Kim Date: Mon, 17 Feb 2020 21:52:00 +0900 Subject: [PATCH 07/16] Replaced strncpy() with g_strlcpy() in helper-procfs.c MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit strncpy doesn’t set zero byte implicitly. Change-Id: I45f322916017d2b727902e713284d0e8ffa19fad Signed-off-by: Jaehyun Kim --- src/helper/helper-procfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c index 60e5db2..a0d9f0e 100644 --- a/src/helper/helper-procfs.c +++ b/src/helper/helper-procfs.c @@ -259,7 +259,7 @@ API int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX]) if (token != NULL) { while (isspace((unsigned char)*token)) token++; - strncpy(status[index], token, + g_strlcpy(status[index], token, sizeof(status[index])); updated[index] = TRUE; } -- 2.7.4 From 26d7f3aa3a8b290f6390c4ee20b4ea1ac9983ec8 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Thu, 12 Mar 2020 12:40:24 +0900 Subject: [PATCH 08/16] Measure datausage more accurately Add some signal for app status (launch and terminate) Change in datausage measurement when interface Change-Id: I76656633a357d7feb3361f9eb67aff1655caf6c5 Signed-off-by: hyunuk.tak --- data/exceptions | 1 + packaging/stc-manager.spec | 2 +- plugin/appstatus/stc-plugin-appstatus.c | 151 ++++++++++++++++++++++++------- plugin/monitor/stc-plugin-monitor-app.c | 12 ++- plugin/monitor/stc-plugin-monitor-proc.c | 2 + plugin/procfs/stc-plugin-procfs.c | 53 ++++++++--- src/helper/helper-net-cls.c | 6 +- 7 files changed, 170 insertions(+), 57 deletions(-) diff --git a/data/exceptions b/data/exceptions index c3d4cbe..6438de0 100644 --- a/data/exceptions +++ b/data/exceptions @@ -53,3 +53,4 @@ dbus-daemon:sys dlogsend:sys sync:sys reboot:sys +dlog-log-critical:sys diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 69cbe48..bc30e18 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.96 +Version: 0.0.97 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/plugin/appstatus/stc-plugin-appstatus.c b/plugin/appstatus/stc-plugin-appstatus.c index ced6be6..7036271 100755 --- a/plugin/appstatus/stc-plugin-appstatus.c +++ b/plugin/appstatus/stc-plugin-appstatus.c @@ -31,6 +31,10 @@ #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_TERMINATE_REQUEST "AppTerminate" +#define AUL_APP_STATUS_DBUS_TERMINATE_REQUEST_TYPE "(isss)" #define AUL_APP_STATUS_DBUS_STATUS_CHANGE "AppStatusChange" #define AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE "(issss)" @@ -59,43 +63,106 @@ static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection, 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, + gchar *appid = NULL; + gchar *pkgid = NULL; + gchar *statstr = NULL; + gchar *pkgtype = NULL; + + if (g_strcmp0(signal_name, AUL_APP_STATUS_DBUS_LAUNCH_REQUEST) == 0) { + if (g_strcmp0(g_variant_get_type_string(parameters), + AUL_APP_STATUS_DBUS_LAUNCH_REQUEST_TYPE)) { + STC_LOGE("Dbus type not matching, do not process"); + return; + } + + g_variant_get(parameters, AUL_APP_STATUS_DBUS_LAUNCH_REQUEST_TYPE, + &pid, &appid, &pkgid, &pkgtype); + + status = STC_CMD_SET_APP_LAUNCHED; + + 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_STAT_LOG) { + STC_LOGD("APP STATUS[\033[1;34mlaunch\033[0;m] PkgID[\033[0;34m%s\033[0;m] " + "AppID[\033[0;32m%s\033[0;m] PID[\033[1;33m%d\033[0;m] Type[%s]", + pkgid, appid, pid, pkgtype); + } + + if (state_changed_cb) + state_changed_cb(status, pid, appid, pkgid, apptype); + } else if (g_strcmp0(signal_name, AUL_APP_STATUS_DBUS_TERMINATE_REQUEST) == 0) { + if (g_strcmp0(g_variant_get_type_string(parameters), + AUL_APP_STATUS_DBUS_TERMINATE_REQUEST_TYPE)) { + STC_LOGE("Dbus type not matching, do not process"); + return; + } + + g_variant_get(parameters, AUL_APP_STATUS_DBUS_TERMINATE_REQUEST_TYPE, + &pid, &appid, &pkgid, &pkgtype); + + status = STC_CMD_SET_TERMINATED; + + 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_STAT_LOG) { + STC_LOGD("APP STATUS[\033[1;34mterminate\033[0;m] PkgID[\033[0;34m%s\033[0;m] " + "AppID[\033[0;32m%s\033[0;m] PID[\033[1;33m%d\033[0;m] Type[%s]", + pkgid, appid, pid, pkgtype); + } + + if (state_changed_cb) + state_changed_cb(status, pid, appid, pkgid, apptype); + } else if (g_strcmp0(signal_name, AUL_APP_STATUS_DBUS_STATUS_CHANGE) == 0) { + 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_STAT_LOG) { - STC_LOGD("\033[1;34mAPP STATUS\033[0;m: PkgID[\033[0;34m%s\033[0;m] " - "AppID[\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 (!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_STAT_LOG) { + STC_LOGD("APP STATUS[\033[1;34m%s\033[0;m] PkgID[\033[0;34m%s\033[0;m] " + "AppID[\033[0;32m%s\033[0;m] PID[\033[1;33m%d\033[0;m] Type[%s]", + (status == STC_CMD_SET_FOREGRD) ? "Foregrd" : "Backgrd", pkgid, appid, pid, pkgtype); + } + + if (state_changed_cb) + state_changed_cb(status, pid, appid, pkgid, apptype); } - if (state_changed_cb) - state_changed_cb(status, pid, appid, pkgid, apptype); - out: FREE(appid); FREE(pkgid); @@ -110,6 +177,24 @@ signal_map_s signal_map[] = { 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_changestate, + NULL + }, + { + 0, + AUL_APP_STATUS_DBUS_PATH, + AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE, + AUL_APP_STATUS_DBUS_TERMINATE_REQUEST, + AUL_APP_STATUS_DBUS_TERMINATE_REQUEST_TYPE, + __stc_gdbus_handle_aul_changestate, + 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, diff --git a/plugin/monitor/stc-plugin-monitor-app.c b/plugin/monitor/stc-plugin-monitor-app.c index ca4a88f..a5804da 100755 --- a/plugin/monitor/stc-plugin-monitor-app.c +++ b/plugin/monitor/stc-plugin-monitor-app.c @@ -31,7 +31,7 @@ static void __print_app(gpointer key, gpointer value, { stc_app_value_s *app_value = (stc_app_value_s *)value; - STC_LOGD("PkgID[%s] AppID[\033[0;32m%s\033[0;m] " + STC_LOGD("PkgID[\033[0;34m%s\033[0;m] AppID[\033[0;32m%s\033[0;m] " "type[%d] state[%d] classid[\033[1;36m%d\033[0;m] " "counter[in(%lld) out(%lld)]", app_value->pkg_id, app_value->app_id, @@ -417,15 +417,13 @@ void stc_monitor_app_update_counter(stc_app_value_s *value, switch (context->counter->iotype) { case NFACCT_COUNTER_IN: value->data_usage.in_bytes += context->bytes; - value->counter.in_bytes = context->bytes; + value->counter.in_bytes += context->bytes; stc_monitor_set_apps_updated(TRUE); - break; case NFACCT_COUNTER_OUT: value->data_usage.out_bytes += context->bytes; - value->counter.out_bytes = context->bytes; + value->counter.out_bytes += context->bytes; stc_monitor_set_apps_updated(TRUE); - break; default: STC_LOGE("Unknown iotype"); @@ -434,6 +432,10 @@ void stc_monitor_app_update_counter(stc_app_value_s *value, void stc_monitor_app_update_iface_counter(classid_bytes_context_s *context) { + if (context->counter->classid != STC_TOTAL_IPV4_CLASSID && + context->counter->classid != STC_TOTAL_IPV6_CLASSID) + return; + switch (context->counter->iftype) { case STC_IFACE_DATACALL: __app_update_counter(context, STC_TOTAL_DATACALL_CLASSID); diff --git a/plugin/monitor/stc-plugin-monitor-proc.c b/plugin/monitor/stc-plugin-monitor-proc.c index 12cfe65..b4564c0 100755 --- a/plugin/monitor/stc-plugin-monitor-proc.c +++ b/plugin/monitor/stc-plugin-monitor-proc.c @@ -217,6 +217,8 @@ API stc_error_e stc_plugin_monitor_proc_remove(uint32_t classid, pid_t pid) } pid_count = g_hash_table_size(app_lookup->processes); + if (classid == STC_UNKNOWN_CLASSID) + classid = app_lookup->classid; if (!pid_count) { for (; conn_list != NULL; conn_list = conn_list->next) { diff --git a/plugin/procfs/stc-plugin-procfs.c b/plugin/procfs/stc-plugin-procfs.c index 9538a61..f54794a 100755 --- a/plugin/procfs/stc-plugin-procfs.c +++ b/plugin/procfs/stc-plugin-procfs.c @@ -321,7 +321,7 @@ static void __process_event_fork(int tgid, int pid) if (STC_STAT_LOG) STC_LOGD("\033[1;32mFORK\033[0;m: tgid[\033[1;33m%d\033[0;m] " - "ppid=[%s] cmdline[\033[0;34m%s\033[0;m] pid[%d]", + "ppid[%s] cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, pid); __proc_tree_add(&key, &value); @@ -602,9 +602,9 @@ stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd, switch (cmd) { case STC_CMD_SET_FOREGRD: { - uint32_t fg_classid; - uint32_t bg_classid; - char *bg_app_id; + uint32_t fg_classid = STC_UNKNOWN_CLASSID; + uint32_t bg_classid = STC_UNKNOWN_CLASSID; + char *bg_app_id = NULL; stc_app_value_s app_value; stc_proc_value_s proc_value; @@ -635,9 +635,9 @@ stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd, } case STC_CMD_SET_BACKGRD: { - uint32_t bg_classid; - uint32_t fg_classid; - char *bg_app_id; + uint32_t bg_classid = STC_UNKNOWN_CLASSID; + uint32_t fg_classid = STC_UNKNOWN_CLASSID; + char *bg_app_id = NULL; stc_app_value_s app_value; stc_proc_value_s proc_value; @@ -666,10 +666,33 @@ stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd, FREE(bg_app_id); break; } + case STC_CMD_SET_APP_LAUNCHED: + { + uint32_t classid = STC_UNKNOWN_CLASSID; + stc_app_value_s app_value; + stc_proc_value_s proc_value; + + memset(&app_value, 0, sizeof(stc_app_value_s)); + memset(&proc_value, 0, sizeof(stc_proc_value_s)); + + classid = get_classid_by_app_id(app_id, FALSE); + + app_value.type = app_type; + app_value.state = STC_APP_STATE_FOREGROUND; + app_value.processes = NULL; + + proc_value.pid = pid; + proc_value.ground = STC_APP_STATE_FOREGROUND; + + stc_plugin_monitor_add_app(classid, app_id, pkg_id, app_value); + stc_plugin_monitor_add_proc(classid, app_id, proc_value); + + break; + } case STC_CMD_SET_SERVICE_LAUNCHED: { - uint32_t classid; - char *bg_app_id; + uint32_t classid = STC_UNKNOWN_CLASSID; + char *bg_app_id = NULL; stc_app_value_s app_value; stc_proc_value_s proc_value; gboolean is_exist; @@ -709,14 +732,16 @@ stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd, } case STC_CMD_SET_TERMINATED: { - uint32_t classid; - char *bg_app_id; + uint32_t classid = STC_UNKNOWN_CLASSID; + char *bg_app_id = NULL; - bg_app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, NULL); - classid = get_classid_by_app_id(bg_app_id, FALSE); + if (app_type == STC_APP_TYPE_NONE) { + bg_app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, NULL); + classid = get_classid_by_app_id(bg_app_id, FALSE); + } if (classid == STC_UNKNOWN_CLASSID) - classid = get_classid_by_app_id(app_id, FALSE); + classid = get_classid_by_app_id(bg_app_id, FALSE); stc_plugin_monitor_remove_proc(classid, pid); diff --git a/src/helper/helper-net-cls.c b/src/helper/helper-net-cls.c index 350dfb6..88041cc 100755 --- a/src/helper/helper-net-cls.c +++ b/src/helper/helper-net-cls.c @@ -111,10 +111,8 @@ API uint32_t get_classid_by_app_id(const char *app_id, int create) uint32_t classid = STC_UNKNOWN_CLASSID; const char *path_to_net_cgroup_dir = NULL; - if (app_id == NULL) { - STC_LOGE("app_id must be not empty"); //LCOV_EXCL_LINE - return STC_UNKNOWN_CLASSID; //LCOV_EXCL_LINE - } + if (app_id == NULL) + return STC_UNKNOWN_CLASSID; if (!strcmp(app_id, STC_TOTAL_BACKGROUND)) return STC_BACKGROUND_APP_CLASSID; -- 2.7.4 From 07225bd61cfb201746c4f068573cb873879df99b Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Tue, 17 Mar 2020 10:17:36 +0900 Subject: [PATCH 09/16] Check validity of database Change-Id: Ib412f13d82b5e29b373480ad15cc5310f2be949a Signed-off-by: hyunuk.tak --- include/stc-manager-util.h | 16 +- packaging/stc-manager.spec | 13 +- src/CMakeLists.txt | 1 + src/database/db-common.c | 243 ++++++++++++++++++++++++------- src/database/include/db-internal.h | 1 - src/database/include/stc-db.h | 2 +- src/database/tables/table-counters.c | 48 +----- src/database/tables/table-firewall.c | 40 +---- src/database/tables/table-restrictions.c | 48 +----- src/database/tables/table-statistics.c | 36 +---- 10 files changed, 232 insertions(+), 216 deletions(-) diff --git a/include/stc-manager-util.h b/include/stc-manager-util.h index 7440534..3dd9b38 100755 --- a/include/stc-manager-util.h +++ b/include/stc-manager-util.h @@ -85,7 +85,7 @@ #define EXEC(error_code, command) do { \ if (error_code != command) { \ __STC_LOG_FUNC_EXIT__; \ - goto handle_error; \ + return false; \ } \ } while (0) @@ -253,7 +253,8 @@ static inline bool strstart_with(const char *str, const char *with) #define ret_msg_if(expr, fmt, arg...) do { \ if (expr) { \ - STC_LOGE(fmt, ##arg); \ + if (STC_DEBUG_LOG) \ + STC_LOGD(fmt, ##arg); \ return; \ } \ } while (0) @@ -267,20 +268,11 @@ static inline bool strstart_with(const char *str, const char *with) #define ret_value_msg_if(expr, val, fmt, arg...) do { \ if (expr) { \ if (STC_DEBUG_LOG) \ - STC_LOGE(fmt, ##arg); \ + STC_LOGD(fmt, ##arg); \ return val; \ } \ } while (0) -#define ret_value_errno_msg_if(expr, val, fmt, arg...) do { \ - if (expr) { \ - if (STC_DEBUG_LOG) \ - STC_LOGE(fmt, ##arg); \ - return val; \ - } \ -} while (0) - - /* * @brief Copy from source to destination * destination should not be on heap. diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index bc30e18..7e5f221 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.97 +Version: 0.0.98 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 @@ -9,9 +9,10 @@ Source0: %{name}-%{version}.tar.gz %define enable_database YES %define enable_statistics YES %define enable_restriction YES -%define database_full_path /opt/usr/dbspace/.stc-manager-datausage.db +%define database_full_path %{TZ_SYS_GLOBALUSER_DB}/.stc-manager-datausage.db +%define database_backup_path %{TZ_SYS_RO_SHARE}/stc/.stc-manager-datausage.db %define upgrade_script_filename 500.stc-manager_upgrade.sh -%define upgrade_script_path /usr/share/upgrade/scripts +%define upgrade_script_path %{TZ_SYS_RO_SHARE}/upgrade/scripts BuildRequires: cmake BuildRequires: pkgconfig(glib-2.0) @@ -106,6 +107,7 @@ export CXXFLAGS="$CXXFLAGS -D_GNU_SOURCE" -DLIB_PATH=%{_lib} \ -DENABLE_DATABASE=%{enable_database} \ -DDATABASE_FULL_PATH=%{database_full_path} \ + -DDATABASE_BACKUP_PATH=%{database_backup_path} \ -DENABLE_STATISTICS=%{enable_statistics} \ -DENABLE_RESTRICTION=%{enable_restriction} \ -DBUILD_GTESTS=%{?gtests:1}%{!?gtests:0} \ @@ -120,9 +122,11 @@ rm -rf %{buildroot} #database initialization %if %{?enable_database} == YES - mkdir -p %{buildroot}/opt/usr/dbspace + mkdir -p %{buildroot}%{TZ_SYS_GLOBALUSER_DB} sqlite3 %{buildroot}%{database_full_path} < %{buildroot}/usr/share/traffic_db.sql sqlite3 %{buildroot}%{database_full_path} < %{buildroot}/usr/share/firewall_db.sql + mkdir -p %{buildroot}%{TZ_SYS_RO_SHARE}/stc + cp %{buildroot}%{database_full_path} %{buildroot}%{database_backup_path} rm %{buildroot}/usr/share/traffic_db.sql rm %{buildroot}/usr/share/firewall_db.sql %endif @@ -168,6 +172,7 @@ cp -f scripts/%{upgrade_script_filename} %{buildroot}%{upgrade_script_path} %if %{?enable_database} == YES %config(noreplace) %attr(660, root, root) %{database_full_path} %config(noreplace) %attr(660, root, root) %{database_full_path}-journal +%config(noreplace) %attr(660, root, root) %{database_backup_path} %endif %if 0%{?gtests:1} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 35aaafd..9883098 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -87,6 +87,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") ADD_DEFINITIONS("-DUSE_DLOG") ADD_DEFINITIONS("-DDATABASE_FULL_PATH=\"${DATABASE_FULL_PATH}\"") +ADD_DEFINITIONS("-DDATABASE_BACKUP_PATH=\"${DATABASE_BACKUP_PATH}\"") IF(BUILD_GTESTS) ADD_DEFINITIONS(-DTIZEN_GTESTS) ENDIF(BUILD_GTESTS) diff --git a/src/database/db-common.c b/src/database/db-common.c index a4c2a3d..3519710 100755 --- a/src/database/db-common.c +++ b/src/database/db-common.c @@ -14,6 +14,9 @@ * limitations under the License. */ +#include +#include + #include "stc-db.h" #include "db-internal.h" #include "table-statistics.h" @@ -21,10 +24,6 @@ #include "table-counters.h" #include "table-firewall.h" -#ifndef DATABASE_FULL_PATH -#define DATABASE_FULL_PATH "/opt/usr/dbspace/.stc-manager-datausage.db" -#endif - #define SQLITE_BUSY_TIMEOUT 500000 static sqlite3 *database; @@ -40,54 +39,191 @@ static int __stc_db_busy(void *user, int attempts) __STC_LOG_FUNC_EXIT__; return 1; } -//LCOV_EXCL_STOP -stc_error_e stc_db_initialize_once(void) +static gboolean __stc_db_restore(const char *src, const char *dst) +{ + gchar *buf = NULL; + gsize length = 0; + GError *error = NULL; + gboolean result; + + result = g_file_get_contents(src, &buf, &length, &error); + if (result != TRUE) { + STC_LOGE("Failed to read [%s]", error->message); + g_error_free(error); + return result; + } + + result = g_file_set_contents(dst, buf, length, &error); + if (result != TRUE) { + STC_LOGE("Failed to write [%s]", error->message); + g_error_free(error); + g_free(buf); + return result; + } + + STC_LOGD("Successfully restored database"); + g_free(buf); + + return result; +} + +static gboolean __stc_db_table_init() +{ + EXEC(STC_ERROR_NONE, table_statistics_prepare(database)); + EXEC(STC_ERROR_NONE, table_restrictions_prepare(database)); + EXEC(STC_ERROR_NONE, table_counters_prepare(database)); + EXEC(STC_ERROR_NONE, table_firewall_prepare(database)); + EXEC(STC_ERROR_NONE, stc_init_db_guard()); + + return true; +} + +static int __stc_db_open(void) +{ + int ret = 0; + + ret = sqlite3_open(DATABASE_FULL_PATH, &database); + if (ret != SQLITE_OK) { + STC_LOGD("Failed to open database [%s]", sqlite3_errmsg(database)); + return STC_ERROR_DB_FAILED; + } + + STC_LOGD("Successfully opened database"); + return ret; +} + +static int __stc_db_exec(char *sql, void *cb) +{ + int ret; + char *error = NULL; + + if (database == NULL) + return STC_ERROR_DB_FAILED; + + ret = sqlite3_exec(database, sql, cb, 0, &error); + if (ret != SQLITE_OK) { + STC_LOGE("Failed to execute sql [%d:%s]", ret, error); + sqlite3_free(error); + sqlite3_close(database); + database = NULL; + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_DB_FAILED; + } + + return STC_ERROR_NONE; +} + +static int __stc_db_integrity_cb(void *err, int count, char **data, char **columns) +{ + STC_LOGD("%s [%s]", columns[0], data[0] ? data[0] : "null"); + + if (!g_strcmp0(columns[0], "integrity_check") && !g_strcmp0(data[0], "ok")) + return SQLITE_OK; + + return SQLITE_ERROR; +} + +static int __stc_db_check_integrity(void) +{ + int ret; + char *sql = NULL; + + sql = sqlite3_mprintf("PRAGMA integrity_check"); + ret = __stc_db_exec(sql, __stc_db_integrity_cb); + if (ret == STC_ERROR_NONE) + STC_LOGD("Successfully checked integrity"); + + sqlite3_free(sql); + + return ret; +} + +static int __stc_db_set_locking_mode(void) +{ + int ret; + char *sql = NULL; + + sql = sqlite3_mprintf("PRAGMA locking_mode = NORMAL"); + ret = __stc_db_exec(sql, NULL); + if (ret == STC_ERROR_NONE) + STC_LOGD("Successfully set locking mode"); + + sqlite3_free(sql); + + return ret; +} + +static int __stc_db_stat(void) +{ + struct stat db_stat = { 0 }; + + if (stat(DATABASE_FULL_PATH, &db_stat)) { + STC_LOGD("Db restoration is required [no file]"); + return STC_ERROR_DB_FAILED; + } else if (db_stat.st_size == 0) { + STC_LOGD("Db restoration is required [size is zero]"); + return STC_ERROR_DB_FAILED; + } + + return STC_ERROR_NONE; +} + +static int __stc_db_verify(void) +{ + ret_value_msg_if(__stc_db_check_integrity() != STC_ERROR_NONE, + STC_ERROR_DB_FAILED, "Failed to check integrity"); + + ret_value_msg_if(__stc_db_set_locking_mode() != STC_ERROR_NONE, + STC_ERROR_DB_FAILED, "Failed to set locking mode"); + + return STC_ERROR_NONE; +} + +//LCOV_EXCL_STOP +stc_error_e stc_db_initialize_once() { __STC_LOG_FUNC_ENTER__; - int res = 0; - int retry_count = 0; + int retry_count = MAX_DB_RETRY_COUNT; + if (database != NULL) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return STC_ERROR_NONE; //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_NONE; } - do { - res = sqlite3_open(DATABASE_FULL_PATH, &database); - if (res != SQLITE_OK) { - STC_LOGD("Retry[%d] opening database %s: %s\n", //LCOV_EXCL_LINE - ++retry_count, DATABASE_FULL_PATH, - sqlite3_errmsg(database)); //LCOV_EXCL_LINE - } else { - break; + if (__stc_db_stat() != STC_ERROR_NONE) { + if (!__stc_db_restore(DATABASE_BACKUP_PATH, DATABASE_FULL_PATH)) { + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_DB_FAILED; } - usleep(MAX_USLEEP_TIMEOUT); - } while (retry_count <= MAX_DB_RETRY_COUNT); - - if (res != SQLITE_OK) { - STC_LOGE("Can't open database %s: %s\n", DATABASE_FULL_PATH, //LCOV_EXCL_LINE - sqlite3_errmsg(database)); - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE } - STC_LOGD("Successfully opened database"); + do { + if (__stc_db_open() == SQLITE_OK) { + if (__stc_db_verify() == STC_ERROR_NONE) { + STC_LOGD("Successfully verified database"); + break; + } else { + __stc_db_restore(DATABASE_BACKUP_PATH, DATABASE_FULL_PATH); + } + } + usleep(MAX_USLEEP_TIMEOUT); + STC_LOGD("Retry opening database [%d]", MAX_DB_RETRY_COUNT - retry_count + 1); + } while (retry_count--); - res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0); - if (res != SQLITE_OK) { - STC_LOGE("Can't set locking mode %s, skip set busy handler.", //LCOV_EXCL_LINE - sqlite3_errmsg(database)); - sqlite3_close(database); //LCOV_EXCL_LINE - database = NULL; //LCOV_EXCL_LINE - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE + if (retry_count == 0) { + STC_LOGE("Failed to initialize database"); + sqlite3_close(database); + database = NULL; + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_DB_FAILED; } /* Set how many times we'll repeat our attempts for sqlite_step */ - if (sqlite3_busy_handler(database, __stc_db_busy, NULL) - != SQLITE_OK) - STC_LOGE("Couldn't set busy handler!"); //LCOV_EXCL_LINE + if (sqlite3_busy_handler(database, __stc_db_busy, NULL) != SQLITE_OK) + STC_LOGE("Couldn't set busy handler!"); + STC_LOGD("Successfully initialize database"); __STC_LOG_FUNC_EXIT__; return STC_ERROR_NONE; } @@ -105,31 +241,34 @@ sqlite3 *stc_db_get_database(void) stc_error_e stc_db_initialize(void) { __STC_LOG_FUNC_ENTER__; + int ret; + int retry_count = MAX_DB_RETRY_COUNT; database = NULL; - stc_db_initialize_once(); - - EXEC(STC_ERROR_NONE, table_statistics_prepare(database)); - EXEC(STC_ERROR_NONE, table_restrictions_prepare(database)); - EXEC(STC_ERROR_NONE, table_counters_prepare(database)); - EXEC(STC_ERROR_NONE, table_firewall_prepare(database)); - EXEC(STC_ERROR_NONE, stc_init_db_guard()); + do { + stc_db_initialize_once(); + ret = __stc_db_table_init(); + if (ret) { + STC_LOGD("Successfully initialize database"); + break; + } else { + __stc_db_restore(DATABASE_BACKUP_PATH, DATABASE_FULL_PATH); + sqlite3_close(database); + database = NULL; + } + STC_LOGD("Retry init database [%d]", MAX_DB_RETRY_COUNT - retry_count + 1); + } while (retry_count--); __STC_LOG_FUNC_EXIT__; return STC_ERROR_NONE; - -handle_error: - stc_db_deinitialize(); //LCOV_EXCL_LINE - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE } gboolean stc_db_deinitialize(void) { __STC_LOG_FUNC_ENTER__; if (database == NULL) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return TRUE; //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; + return TRUE; } table_statistics_finalize(); diff --git a/src/database/include/db-internal.h b/src/database/include/db-internal.h index 2d8922d..2b2daae 100755 --- a/src/database/include/db-internal.h +++ b/src/database/include/db-internal.h @@ -35,7 +35,6 @@ extern "C" { #define DB_ACTION(command) do { \ if ((command) != SQLITE_OK) { \ error_code = STC_ERROR_DB_FAILED; \ - __STC_LOG_FUNC_EXIT__; \ goto handle_error; \ } \ } while (0) diff --git a/src/database/include/stc-db.h b/src/database/include/stc-db.h index c15d3fa..e648d21 100755 --- a/src/database/include/stc-db.h +++ b/src/database/include/stc-db.h @@ -19,7 +19,7 @@ #include "stc-manager.h" -#define MAX_DB_RETRY_COUNT 20 +#define MAX_DB_RETRY_COUNT 5 #define MAX_USLEEP_TIMEOUT 500000 #define SQLITE_BUSY_TIMEOUT 500000 #define MAX_IFACE_LENGTH 32 diff --git a/src/database/tables/table-counters.c b/src/database/tables/table-counters.c index fa22cc5..c4c0db1 100755 --- a/src/database/tables/table-counters.c +++ b/src/database/tables/table-counters.c @@ -123,118 +123,86 @@ static sqlite3_stmt *insert_timestamp; static int __prepare_delete(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return SQLITE_OK; //LCOV_EXCL_LINE - } + if (initialized) + return SQLITE_OK; PREPARE_DELETE(delete_counter, DELETE_COUNTER); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_delete(void) { - __STC_LOG_FUNC_ENTER__; - FINALIZE(delete_counter); - - __STC_LOG_FUNC_EXIT__; } static int __prepare_select(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return SQLITE_OK; //LCOV_EXCL_LINE - } + if (initialized) + return SQLITE_OK; PREPARE_SELECT(select_counter, SELECT_COUNTER); PREPARE_SELECT(select_timestamp, SELECT_TIMESTAMP); PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_select(void) { - __STC_LOG_FUNC_ENTER__; - FINALIZE(select_counter); FINALIZE(select_timestamp); FINALIZE(select_restriction_id); - - __STC_LOG_FUNC_EXIT__; } static int __prepare_update(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return SQLITE_OK; //LCOV_EXCL_LINE - } + if (initialized) + return SQLITE_OK; PREPARE_UPDATE(update_counter, UPDATE_COUNTER); PREPARE_UPDATE(update_timestamp, UPDATE_TIMESTAMP); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_update(void) { - __STC_LOG_FUNC_ENTER__; - FINALIZE(update_counter); FINALIZE(update_timestamp); - - __STC_LOG_FUNC_EXIT__; } static int __prepare_insert(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return SQLITE_OK; //LCOV_EXCL_LINE - } + if (initialized) + return SQLITE_OK; PREPARE_INSERT(insert_counter, INSERT_COUNTER); PREPARE_INSERT(insert_timestamp, INSERT_TIMESTAMP); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_insert(void) { - __STC_LOG_FUNC_ENTER__; - FINALIZE(insert_counter); FINALIZE(insert_timestamp); - - __STC_LOG_FUNC_EXIT__; } static bool __table_counters_is_entry_present(long long int restriction_id) diff --git a/src/database/tables/table-firewall.c b/src/database/tables/table-firewall.c index 5e179e3..995e8cd 100755 --- a/src/database/tables/table-firewall.c +++ b/src/database/tables/table-firewall.c @@ -180,45 +180,34 @@ static sqlite3_stmt *insert_fw_rule; static int __prepare_delete(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; + if (initialized) return SQLITE_OK; - } PREPARE_DELETE(delete_fw_chain, DELETE_FIREWALL_CHAIN); PREPARE_DELETE(delete_fw_rule, DELETE_FIREWALL_RULE); PREPARE_DELETE(delete_fw_rule_per_chain, DELETE_FIREWALL_RULE_PER_CHAIN); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_delete(void) { - __STC_LOG_FUNC_ENTER__; - FINALIZE(delete_fw_chain); FINALIZE(delete_fw_rule); FINALIZE(delete_fw_rule_per_chain); - - __STC_LOG_FUNC_EXIT__; } static int __prepare_select(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; + if (initialized) return SQLITE_OK; - } PREPARE_SELECT(select_fw_lock, SELECT_FIREWALL_LOCK); PREPARE_SELECT(select_fw_chain, SELECT_FIREWALL_CHAIN); @@ -226,82 +215,61 @@ static int __prepare_select(sqlite3 *db) PREPARE_SELECT(select_fw_rule_per_chain, SELECT_FIREWALL_RULE_PER_CHAIN); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_select(void) { - __STC_LOG_FUNC_ENTER__; - FINALIZE(select_fw_lock); FINALIZE(select_fw_chain); FINALIZE(select_fw_rule); FINALIZE(select_fw_rule_per_chain); - - __STC_LOG_FUNC_EXIT__; } static int __prepare_update(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; + if (initialized) return SQLITE_OK; - } PREPARE_UPDATE(update_fw_lock, UPDATE_FIREWALL_LOCK); PREPARE_UPDATE(update_fw_chain, UPDATE_FIREWALL_CHAIN); PREPARE_UPDATE(update_fw_rule, UPDATE_FIREWALL_RULE); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_update(void) { - __STC_LOG_FUNC_ENTER__; - FINALIZE(update_fw_lock); FINALIZE(update_fw_chain); FINALIZE(update_fw_rule); - - __STC_LOG_FUNC_EXIT__; } static int __prepare_insert(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; + if (initialized) return SQLITE_OK; - } PREPARE_INSERT(insert_fw_lock, INSERT_FIREWALL_LOCK); PREPARE_INSERT(insert_fw_chain, INSERT_FIREWALL_CHAIN); PREPARE_INSERT(insert_fw_rule, INSERT_FIREWALL_RULE); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_insert(void) { - __STC_LOG_FUNC_ENTER__; - FINALIZE(insert_fw_lock); FINALIZE(insert_fw_chain); FINALIZE(insert_fw_rule); - - __STC_LOG_FUNC_EXIT__; } API stc_error_e table_firewall_insert_lock(char *name, int state) diff --git a/src/database/tables/table-restrictions.c b/src/database/tables/table-restrictions.c index 71167ee..1466a65 100755 --- a/src/database/tables/table-restrictions.c +++ b/src/database/tables/table-restrictions.c @@ -145,41 +145,30 @@ static sqlite3_stmt *insert_net_restrictions; static int __prepare_delete(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return SQLITE_OK; //LCOV_EXCL_LINE - } + if (initialized) + return SQLITE_OK; PREPARE_DELETE(delete_restrictions, DELETE_RESTRICTIONS); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_delete(void) { - __STC_LOG_FUNC_ENTER__; - FINALIZE(delete_restrictions); - - __STC_LOG_FUNC_EXIT__; } static int __prepare_select(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return SQLITE_OK; //LCOV_EXCL_LINE - } + if (initialized) + return SQLITE_OK; PREPARE_SELECT(select_restriction, SELECT_RESTRICTIONS); PREPARE_SELECT(select_restriction_per_app, SELECT_RESTRICTIONS_PER_APP); @@ -188,75 +177,54 @@ static int __prepare_select(sqlite3 *db) PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_select(void) { - __STC_LOG_FUNC_ENTER__; - FINALIZE(select_restriction); FINALIZE(select_restriction_per_app); FINALIZE(select_restriction_type); FINALIZE(select_restriction_type_subscriber_id); FINALIZE(select_restriction_id); - - __STC_LOG_FUNC_EXIT__; } static int __prepare_replace(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return SQLITE_OK; //LCOV_EXCL_LINE - } + if (initialized) + return SQLITE_OK; PREPARE_UPDATE(update_net_restrictions, UPDATE_NET_RESTRICTIONS); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_update(void) { - __STC_LOG_FUNC_ENTER__; - FINALIZE(update_net_restrictions); - - __STC_LOG_FUNC_EXIT__; } static int __prepare_insert(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return SQLITE_OK; //LCOV_EXCL_LINE - } + if (initialized) + return SQLITE_OK; PREPARE_UPDATE(insert_net_restrictions, INSERT_NET_RESTRICTIONS); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_insert(void) { - __STC_LOG_FUNC_ENTER__; - FINALIZE(insert_net_restrictions); - - __STC_LOG_FUNC_EXIT__; } stc_error_e table_restrictions_per_app(const gchar* app_id, diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c index 7d13a3f..b631d47 100755 --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -218,14 +218,11 @@ static sqlite3_stmt *update_statistics_query; static int __prepare_delete(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return SQLITE_OK; //LCOV_EXCL_LINE - } + if (initialized) + return SQLITE_OK; PREPARE_DELETE(delete_query[0], DELETE_ALL); PREPARE_DELETE(delete_query[1], DELETE_APP); @@ -234,31 +231,23 @@ static int __prepare_delete(sqlite3 *db) PREPARE_DELETE(delete_query[4], DELETE_FIRST_BY_NUMBER); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_delete(void) { - __STC_LOG_FUNC_ENTER__; - unsigned int i; for (i = 0; i < sizeof(delete_query) / sizeof(*delete_query); i++) FINALIZE(delete_query[i]); - - __STC_LOG_FUNC_EXIT__; } static int __prepare_select(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return SQLITE_OK; //LCOV_EXCL_LINE - } + if (initialized) + return SQLITE_OK; PREPARE_SELECT(select_for_period, SELECT_FOR_PERIOD); PREPARE_SELECT(select_for_period_iface, SELECT_FOR_PERIOD_IFACE); @@ -274,14 +263,11 @@ static int __prepare_select(sqlite3 *db) PREPARE_SELECT(select_chunks_total_iface, SELECT_CHUNKS_TOTAL_IFACE); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_select(void) { - __STC_LOG_FUNC_ENTER__; - FINALIZE(select_for_period); FINALIZE(select_for_period_iface); FINALIZE(select_chunks); @@ -294,35 +280,25 @@ static void __finalize_select(void) FINALIZE(select_total_iface); FINALIZE(select_chunks_total); FINALIZE(select_chunks_total_iface); - - __STC_LOG_FUNC_EXIT__; } static int __prepare_insert(sqlite3 *db) { - __STC_LOG_FUNC_ENTER__; int rc; static int initialized; - if (initialized) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return SQLITE_OK; //LCOV_EXCL_LINE - } + if (initialized) + return SQLITE_OK; PREPARE_INSERT(update_statistics_query, INSERT_VALUES); initialized = 1; - __STC_LOG_FUNC_EXIT__; return rc; } static void __finalize_insert(void) { - __STC_LOG_FUNC_ENTER__; - FINALIZE(update_statistics_query); - - __STC_LOG_FUNC_EXIT__; } static int __is_iftype_defined(const stc_iface_type_e iftype) -- 2.7.4 From 3321f9aa5ff1562e6693abe8cc3468dc0807c104 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Tue, 24 Mar 2020 16:49:15 +0900 Subject: [PATCH 10/16] Modify to check roaming type Change-Id: I310bffa239f9b030519bf203dc5780a2fc31a1d9 Signed-off-by: hyunuk.tak --- src/stc-restriction.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stc-restriction.c b/src/stc-restriction.c index d6bae55..d47aaf0 100755 --- a/src/stc-restriction.c +++ b/src/stc-restriction.c @@ -77,7 +77,7 @@ gboolean __validate_rstn_rule(table_restrictions_info *rule) return FALSE; //LCOV_EXCL_LINE } - if (rule->roaming > STC_ROAMING_ENABLE) { + if (rule->roaming > STC_ROAMING_DISABLE) { __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return FALSE; //LCOV_EXCL_LINE } -- 2.7.4 From 43182bf91bdcb4f1359713d0dfdc914506fe7c73 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Tue, 7 Apr 2020 15:11:03 +0900 Subject: [PATCH 11/16] Dereference variant object with g_variant_unref Change-Id: Id0211768f574da5c89e025e2e6b5afbcdb9aeedd Signed-off-by: hyunuk.tak --- unittest/statistics.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/unittest/statistics.cpp b/unittest/statistics.cpp index 2b4b5e5..7c816cc 100644 --- a/unittest/statistics.cpp +++ b/unittest/statistics.cpp @@ -178,6 +178,7 @@ error_e Statistics::InitStatistics(void) } GLOGD("Succeeded to init statistics"); + g_variant_unref(message); return ERROR_NONE; } -- 2.7.4 From c13a343a4defe4a9115b2a523808a322036b7f43 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 14 May 2020 11:44:25 +0530 Subject: [PATCH 12/16] Remove redundant log which is also available in dbus call reply log Change-Id: I91b36c9db8071ca83eb2dd076f7bfaa09b8f7975 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/stc-statistics.c | 15 --------------- 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 7e5f221..6dae0e7 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.98 +Version: 0.0.99 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/stc-statistics.c b/src/stc-statistics.c index a0b5a07..0e26464 100755 --- a/src/stc-statistics.c +++ b/src/stc-statistics.c @@ -39,19 +39,6 @@ static const gchar *stc_err_strs[] = { "NOTIMPL" }; -void __stc_statistics_print_app_info(const table_statistics_info *info) -{ - if (!info) - return; - - STC_LOGD("========== App data (Statistics) =========="); - STC_LOGD("app_id : [%s]", info->app_id ? info->app_id : "NULL"); - STC_LOGD("ifname : [%s]", info->ifname ? info->ifname : "NULL"); - STC_LOGD("Sent : [%lld] bytes", info->cnt.out_bytes); - STC_LOGD("Received : [%lld] bytes", info->cnt.in_bytes); - STC_LOGD("==========================================="); -} - void __stc_extract_select_rule(const char *key, GVariant *value, void *user_data) { @@ -213,7 +200,6 @@ stc_cb_ret_e __table_statistics_foreach_app_cb(const table_statistics_info *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)); @@ -235,7 +221,6 @@ stc_cb_ret_e __table_statistics_per_app_cb(const table_statistics_info *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)); -- 2.7.4 From 63027b67b356b4cf9a6b8e3bbb1a8b9e383ad133 Mon Sep 17 00:00:00 2001 From: Semun Lee Date: Sat, 16 May 2020 18:38:18 +0900 Subject: [PATCH 13/16] Fix not to use app-status plugin from monitor plugin Change-Id: I3924302eda2ced3859a296b1c9f61047ce7a2d55 Signed-off-by: hyunuk.tak --- packaging/stc-manager.spec | 2 ++ plugin/monitor/CMakeLists.txt | 2 ++ plugin/monitor/stc-plugin-monitor-rstn.c | 40 +++++++++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 7e5f221..f96b7cf 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -79,6 +79,8 @@ A smart traffic control manager extension for tethering client data usage plugin %package plugin-monitor Summary: Monitor plugin for data usage of clients +BuildRequires: pkgconfig(bundle) +BuildRequires: pkgconfig(syspopup-caller) %description plugin-monitor A smart traffic control manager extension for monitoring client data usage plugin diff --git a/plugin/monitor/CMakeLists.txt b/plugin/monitor/CMakeLists.txt index 63b1b9d..e1e0048 100644 --- a/plugin/monitor/CMakeLists.txt +++ b/plugin/monitor/CMakeLists.txt @@ -10,6 +10,8 @@ PKG_CHECK_MODULES(monitor_plugin REQUIRED glib-2.0 vconf openssl1.1 + bundle + syspopup-caller ) FOREACH(flag ${monitor_plugin_CFLAGS}) diff --git a/plugin/monitor/stc-plugin-monitor-rstn.c b/plugin/monitor/stc-plugin-monitor-rstn.c index 365207c..b985baf 100755 --- a/plugin/monitor/stc-plugin-monitor-rstn.c +++ b/plugin/monitor/stc-plugin-monitor-rstn.c @@ -16,6 +16,9 @@ #include #include +#include +#include +#include #include "counter.h" #include "stc-plugin-monitor.h" @@ -26,7 +29,6 @@ #include "table-restrictions.h" #include "table-statistics.h" #include "helper-net-cls.h" -#include "stc-manager-plugin-appstatus.h" #include "stc-manager-plugin-tether.h" static void __print_rstn(stc_rstn_data_s *rstn_data) @@ -1200,6 +1202,7 @@ void stc_monitor_rstn_update_counter(gpointer data, gpointer user_data) { int i; + int is_roaming = 0; stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data; classid_bytes_context_s *context = (classid_bytes_context_s *)user_data; GSList *conn_list = stc_get_connection_list(); @@ -1220,7 +1223,8 @@ void stc_monitor_rstn_update_counter(gpointer data, g_strcmp0(rstn_data->subscriber_id, conn->subscriber_id) != 0) return; - if (rstn_data->roaming != conn->roaming) + is_roaming = (rstn_data->roaming == STC_ROAMING_ENABLE) ? 1 : 0; + if (is_roaming != conn->roaming) return; if (rstn_data->limit_exceeded != 0) { @@ -1297,6 +1301,36 @@ void stc_monitor_rstn_update_iface_counter(classid_bytes_context_s *context) } } + +static int stc_plugin_monitor_launch_rstn_popup(const char *content, + const char *type, const char *app_id, + const char *iftype, const char *limit) +{ + int ret = 0; + bundle *b = bundle_create(); + + 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); + + if (g_strcmp0(type, "warning_noti") == 0) { + bundle_add(b, "_WARN_LIMIT_", limit); + STC_LOGD("Warn message : content[%s] type[%s] app_id[%s] limit[%s]", + content, type, app_id, limit); + } else { + bundle_add(b, "_RESTRICTION_LIMIT_", limit); + STC_LOGD("Restriction message : content[%s] type[%s] app_id[%s] limit[%s]", + content, type, app_id, limit); + } + + ret = syspopup_launch("net-popup", b); + + bundle_free(b); + + return ret; +} + void stc_monitor_rstn_action_when_limit_exceeded(stc_rstn_limit_type_e limit_type, stc_rstn_data_s *rstn_data, classid_bytes_context_s *context) @@ -1383,7 +1417,7 @@ void stc_monitor_rstn_action_when_limit_exceeded(stc_rstn_limit_type_e limit_typ snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_data->iftype); snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_data->limit[limit_type]); - stc_plugin_appstatus_send_message(net_popup_content, + stc_plugin_monitor_launch_rstn_popup(net_popup_content, net_popup_type, rstn_data->app_id, iftype, byte); if (STC_DEBUG_LOG && STC_RSTN_LOG) -- 2.7.4 From c82a1239deb59784f75b305af7aff69af5d42408 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Mon, 18 May 2020 16:57:23 +0900 Subject: [PATCH 14/16] Modify database query appropriately Change-Id: I45bc4f031cd65252d85fd5de9bba6e3eee00b24c Signed-off-by: hyunuk.tak --- src/database/tables/table-statistics.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c index b631d47..2bfff70 100755 --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -85,19 +85,19 @@ #define SELECT_CHUNKS_APP "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, subscriber_id, ground, time_stamp - time_stamp % ? as time_stamp " \ + "ifname, subscriber_id, ground, time_stamp - time_stamp % ? as timestamp " \ "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" + "group by iftype, ifname, timestamp, hw_net_protocol_type, is_roaming " \ + "order by timestamp, 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, subscriber_id, ground, time_stamp - time_stamp % ? as time_stamp " \ + "ifname, subscriber_id, ground, time_stamp - time_stamp % ? as timestamp " \ "from statistics where time_stamp between ? and ? and binpath = ? " \ "and iftype = ? " \ - "group by time_stamp, hw_net_protocol_type, is_roaming, " \ + "group by timestamp, hw_net_protocol_type, is_roaming, " \ "iftype, ifname, subscriber_id " \ - "order by time_stamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ + "order by timestamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ "is_roaming" #define SELECT_TOTAL "select iftype, hw_net_protocol_type, " \ @@ -121,22 +121,22 @@ #define SELECT_CHUNKS_TOTAL "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, subscriber_id, ground, time_stamp - time_stamp % ? as time_stamp " \ + "ifname, subscriber_id, ground, time_stamp - time_stamp % ? as timestamp " \ "from statistics where time_stamp between ? and ? " \ "and binpath NOT LIKE 'TOTAL_%' " \ - "group by time_stamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ + "group by timestamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ "is_roaming " \ - "order by time_stamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ + "order by timestamp, 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, subscriber_id, ground, time_stamp - time_stamp % ? as time_stamp " \ + "ifname, subscriber_id, ground, time_stamp - time_stamp % ? as timestamp " \ "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, subscriber_id " \ - "order by time_stamp, hw_net_protocol_type, is_roaming, iftype, " \ + "group by timestamp, hw_net_protocol_type, is_roaming, iftype, ifname, subscriber_id " \ + "order by timestamp, hw_net_protocol_type, is_roaming, iftype, " \ "ifname, subscriber_id" /* INSERT statement */ -- 2.7.4 From ab517e17e80fec38739dffe83e8f14af5487b586 Mon Sep 17 00:00:00 2001 From: Semun Lee Date: Tue, 19 May 2020 08:30:47 +0900 Subject: [PATCH 15/16] Fix to delete iptables rule while unsetting restrictions We should delete block rules Change-Id: I4c3db4db451f034a9257ebda234dbfa275c19813 Signed-off-by: Semun Lee --- plugin/monitor/stc-plugin-monitor-rstn.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/monitor/stc-plugin-monitor-rstn.c b/plugin/monitor/stc-plugin-monitor-rstn.c index b985baf..5398c1d 100755 --- a/plugin/monitor/stc-plugin-monitor-rstn.c +++ b/plugin/monitor/stc-plugin-monitor-rstn.c @@ -622,10 +622,10 @@ static void __rstn_process(enum traffic_restriction_type rstn_type, int i; if (rstn_data->classid == STC_TETHERING_APP_CLASSID) - __rstn_del_ipt_rule(rstn_data->classid, NFACCT_BLOCK, + __rstn_del_ipt_rule(rstn_data->classid, NFACCT_TETH_BLOCK, rstn_data->iftype); else - __rstn_del_ipt_rule(rstn_data->classid, rstn_data->rstn_type, + __rstn_del_ipt_rule(rstn_data->classid, NFACCT_BLOCK, rstn_data->iftype); rstn_data->rstn_state = STC_RSTN_STATE_DEACTIVATED; -- 2.7.4 From 73ab8e313957e16854708bc16a717d822f8f4a88 Mon Sep 17 00:00:00 2001 From: Semun Lee Date: Fri, 29 May 2020 19:06:24 +0900 Subject: [PATCH 16/16] Fix to init g_system as NULL in error case Change-Id: I2a70043afafb76f1594313899a16f01679e82586 Signed-off-by: Semun Lee --- plugin/monitor/stc-plugin-monitor.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/plugin/monitor/stc-plugin-monitor.c b/plugin/monitor/stc-plugin-monitor.c index fc71c8c..1f08dc3 100755 --- a/plugin/monitor/stc-plugin-monitor.c +++ b/plugin/monitor/stc-plugin-monitor.c @@ -68,6 +68,17 @@ static gboolean __process_contr_reply(GIOChannel *source, GIOCondition condition, gpointer user_data); +static void __free_system(stc_system_s *system) +{ + /* destroy monitored application tree */ + if (system->apps) + g_hash_table_destroy(system->apps); + /* destroy restriction rules tree */ + if (system->rstns) + g_hash_table_destroy(system->rstns); + FREE(system); +} + static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system) { GIOChannel *gio = NULL; @@ -88,7 +99,8 @@ static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system) system->contr_sock = create_netlink(NETLINK_NETFILTER, 0); if (system->contr_sock < 0) { STC_LOGE("failed to open socket"); - FREE(system); + __free_system(system); + g_system = NULL; return STC_ERROR_FAIL; } @@ -349,7 +361,7 @@ stc_error_e stc_plugin_monitor_initialize(void) system->contr_sock = create_netlink(NETLINK_NETFILTER, 0); if (system->contr_sock < 0) { STC_LOGE("failed to open socket"); - FREE(system); + __free_system(system); return STC_ERROR_FAIL; } @@ -403,15 +415,8 @@ stc_error_e stc_plugin_monitor_deinitialize(void) g_system->contr_timer_id = 0; } - /* destroy monitored application tree */ - g_hash_table_destroy(g_system->apps); - g_system->apps = NULL; - - /* destroy restriction rules tree */ - g_hash_table_destroy(g_system->rstns); - g_system->rstns = NULL; - - FREE(g_system); + __free_system(g_system); + g_system = NULL; return STC_ERROR_NONE; } -- 2.7.4