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 f07bb8230efc901a599cc37e2546d807dbb7603a Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Fri, 31 Jan 2020 11:00:03 +0900 Subject: [PATCH 05/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 6762a707cbcdcd4e837faedec4f36ea97be262b7 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Tue, 24 Mar 2020 16:49:15 +0900 Subject: [PATCH 06/16] Modify to check roaming type Change-Id: I4dbcb24640737f659aa6806e3ba116f08f21c10a 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 080367ecd99ea727ff50e8b97a6f72f3b5d74241 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Mon, 18 May 2020 13:08:54 +0900 Subject: [PATCH 07/16] Fix not to use app-status plugin from monitor plugin Change-Id: Ia29a74d576d346955be220290a890a0aae1e8a10 Signed-off-by: hyunuk.tak --- packaging/stc-manager.spec | 2 ++ plugin/monitor/CMakeLists.txt | 2 ++ plugin/monitor/stc-plugin-monitor-rstn.c | 39 +++++++++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 69cbe48..c183fde 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -78,6 +78,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..ab88ae3 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,35 @@ 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 +1416,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 2a2692e147a0c403f826ff33bab6ec6d9ddca64f Mon Sep 17 00:00:00 2001 From: Semun Lee Date: Tue, 19 May 2020 08:30:47 +0900 Subject: [PATCH 08/16] Fix to delete iptables rule while unsetting restrictions We should delete block rules Change-Id: I4c3db4db451f034a9257ebda234dbfa275c19813 Signed-off-by: Semun Lee (cherry picked from commit ab517e17e80fec38739dffe83e8f14af5487b586) --- 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 ab88ae3..60e75ae 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 d5ed099cea71b345fe85e1f768473f338c94920e Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Mon, 18 May 2020 16:57:23 +0900 Subject: [PATCH 09/16] Modify database query appropriately Change-Id: I71690be6c07a3133941835adab11dac80b243d2b 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 7d13a3f..ef1f1f0 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 ebd33f27cfb111480f25b2cd08d1d5518cd5131c Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 11 Jun 2020 18:35:30 +0530 Subject: [PATCH 10/16] Add retry mechanism for netlink socket creation Change-Id: I92f7fc647fbe3262bad1d6962e296ff97a2a68f4 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/helper/helper-nl.c | 35 +++++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index c183fde..d2a29fa 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/src/helper/helper-nl.c b/src/helper/helper-nl.c index ff9d1af..6d54e8f 100755 --- a/src/helper/helper-nl.c +++ b/src/helper/helper-nl.c @@ -20,35 +20,54 @@ #include #include #include +#include -/** - * create_netlink(): Create netlink socket and returns it. - * Returns: Created socket on success and -1 on failure. - */ -API int create_netlink(int protocol, uint32_t groups) +#define NETLINK_SOCK_RETRY_COUNT 3 + +int __create_netlink(int protocol, uint32_t groups, int retry) { /** * TODO it's one socket, in future make set of sockets * unique for protocol and groups */ int sock; - sock = socket(PF_NETLINK, SOCK_RAW, protocol); - if (sock < 0) + + if (retry <= 0) return -EINVAL; //LCOV_EXCL_LINE + errno = 0; + sock = socket(PF_NETLINK, SOCK_RAW, protocol); + if (sock < 0) { + STC_LOGE("failed to open socket errno [%d], retry [%d]", + errno, NETLINK_SOCK_RETRY_COUNT - retry); //LCOV_EXCL_LINE + return __create_netlink(protocol, groups, --retry); //LCOV_EXCL_LINE + } + struct sockaddr_nl src_addr = { 0, }; src_addr.nl_family = AF_NETLINK; src_addr.nl_groups = groups; + errno = 0; if (bind(sock, (struct sockaddr *)&src_addr, sizeof(src_addr)) < 0) { + STC_LOGE("failed to bind socket errno [%d], retry [%d]", + errno, NETLINK_SOCK_RETRY_COUNT - retry); //LCOV_EXCL_LINE close(sock); //LCOV_EXCL_LINE - return -1; //LCOV_EXCL_LINE + return __create_netlink(protocol, groups, --retry); //LCOV_EXCL_LINE } return sock; } +/** + * create_netlink(): Create netlink socket and returns it. + * Returns: Created socket on success and -1 on failure. + */ +API int create_netlink(int protocol, uint32_t groups) +{ + return __create_netlink(protocol, groups, NETLINK_SOCK_RETRY_COUNT); +} + void fill_attribute_list(struct rtattr **atb, const int max_len, struct rtattr *rt_na, int rt_len) { -- 2.7.4 From 760f5e86beda1413210908e72b27ef8871e3030b Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 11 Jun 2020 21:32:36 +0530 Subject: [PATCH 11/16] Add callback in monitor plugin to stop stc-manager Change-Id: I32510bf9ec26958f83deac9863ef431e623007cf Signed-off-by: Nishant Chaprana --- include/stc-manager-plugin-monitor.h | 2 +- packaging/stc-manager.spec | 2 +- plugin/monitor/include/stc-plugin-monitor.h | 6 ++++-- plugin/monitor/stc-plugin-monitor.c | 11 ++++++++++- src/stc-manager-plugin-monitor.c | 4 ++-- src/stc-manager.c | 18 +++++++++++++++++- 6 files changed, 35 insertions(+), 8 deletions(-) diff --git a/include/stc-manager-plugin-monitor.h b/include/stc-manager-plugin-monitor.h index 3830608..76a65bd 100755 --- a/include/stc-manager-plugin-monitor.h +++ b/include/stc-manager-plugin-monitor.h @@ -21,7 +21,7 @@ #include "stc-plugin-monitor.h" -int stc_plugin_monitor_init(void); +int stc_plugin_monitor_init(stc_manager_stop_cb stop_cb); int stc_plugin_monitor_deinit(void); int stc_plugin_monitor_add_app(uint32_t classid, diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index d2a29fa..e9eaf3f 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 diff --git a/plugin/monitor/include/stc-plugin-monitor.h b/plugin/monitor/include/stc-plugin-monitor.h index 35509c8..0cc8c42 100755 --- a/plugin/monitor/include/stc-plugin-monitor.h +++ b/plugin/monitor/include/stc-plugin-monitor.h @@ -38,8 +38,10 @@ #define VCONFKEY_SETAPPL_DATA_RESTRICTION_INT "db/setting/data_restriction" #endif +typedef void (*stc_manager_stop_cb)(void); + typedef struct { - int (*initialize_plugin) (void); + int (*initialize_plugin) (stc_manager_stop_cb stop_cb); int (*deinitialize_plugin) (void); int (*add_application) (uint32_t classid, @@ -68,7 +70,7 @@ typedef struct { int (*check_excn_by_cmdline) (char *cmdline); } stc_plugin_monitor_s; -stc_error_e stc_plugin_monitor_initialize(void); +stc_error_e stc_plugin_monitor_initialize(stc_manager_stop_cb stop_cb); stc_error_e stc_plugin_monitor_deinitialize(void); GHashTable *stc_monitor_get_system_apps(void); diff --git a/plugin/monitor/stc-plugin-monitor.c b/plugin/monitor/stc-plugin-monitor.c index fc71c8c..b278e94 100755 --- a/plugin/monitor/stc-plugin-monitor.c +++ b/plugin/monitor/stc-plugin-monitor.c @@ -32,6 +32,7 @@ #include "stc-manager-plugin-exception.h" static stc_system_s *g_system = NULL; +static stc_manager_stop_cb g_stop_cb = NULL; static int __vconf_get_int(const char *key, int *value) { @@ -89,6 +90,7 @@ static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system) if (system->contr_sock < 0) { STC_LOGE("failed to open socket"); FREE(system); + g_stop_cb(); return STC_ERROR_FAIL; } @@ -325,7 +327,7 @@ static void __fill_exceptions_list(void) stc_plugin_fill_exception_list(); } -stc_error_e stc_plugin_monitor_initialize(void) +stc_error_e stc_plugin_monitor_initialize(stc_manager_stop_cb stop_cb) { __STC_LOG_FUNC_ENTER__; @@ -335,6 +337,11 @@ stc_error_e stc_plugin_monitor_initialize(void) ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!"); + ret_value_msg_if(stop_cb == NULL, STC_ERROR_INVALID_PARAMETER, + "stop_cb callback is NULL"); + + g_stop_cb = stop_cb; + /* initializing current classid */ init_current_classid(); @@ -350,6 +357,7 @@ stc_error_e stc_plugin_monitor_initialize(void) if (system->contr_sock < 0) { STC_LOGE("failed to open socket"); FREE(system); + g_stop_cb(); return STC_ERROR_FAIL; } @@ -412,6 +420,7 @@ stc_error_e stc_plugin_monitor_deinitialize(void) g_system->rstns = NULL; FREE(g_system); + g_stop_cb = NULL; return STC_ERROR_NONE; } diff --git a/src/stc-manager-plugin-monitor.c b/src/stc-manager-plugin-monitor.c index 209b1a1..efdf759 100755 --- a/src/stc-manager-plugin-monitor.c +++ b/src/stc-manager-plugin-monitor.c @@ -24,7 +24,7 @@ static void *handle_plugin; static stc_plugin_monitor_s *stc_plugin; //LCOV_EXCL_START -API int stc_plugin_monitor_init(void) +API int stc_plugin_monitor_init(stc_manager_stop_cb stop_cb) { __STC_LOG_FUNC_ENTER__; @@ -43,7 +43,7 @@ API int stc_plugin_monitor_init(void) return STC_ERROR_UNINITIALIZED; } - stc_plugin->initialize_plugin(); + stc_plugin->initialize_plugin(stop_cb); stc_plugin_enabled = TRUE; __STC_LOG_FUNC_EXIT__; diff --git a/src/stc-manager.c b/src/stc-manager.c index a376e7f..0011c97 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -38,6 +38,7 @@ #define BUF_SIZE_FOR_ERR 100 static stc_s *g_stc = NULL; +static gboolean g_is_fail_exit = FALSE; static gboolean __validate_ident(const char *ident) { @@ -100,6 +101,15 @@ static void __stc_manager_deinit(void) __STC_LOG_FUNC_EXIT__; } +void __stc_manager_stop_with_fail(void) +{ + STC_LOGI("plugin needs stc-manager to exit"); + + g_is_fail_exit = TRUE; + + stc_stop_manager(); +} + static stc_s *__stc_manager_init(void) { __STC_LOG_FUNC_ENTER__; @@ -134,7 +144,7 @@ static stc_s *__stc_manager_init(void) stc_plugin_tether_init(); if (stc_plugin_pcap_init() == STC_ERROR_NONE) g_stc->ondemand_mode = FALSE; - if (stc_plugin_monitor_init() == STC_ERROR_NONE) + if (stc_plugin_monitor_init(__stc_manager_stop_with_fail) == STC_ERROR_NONE) g_stc->ondemand_mode = FALSE; stc_plugin_firewall_init(); @@ -265,6 +275,9 @@ gint32 main(gint32 argc, gchar *argv[]) if (!g_stc) goto fail; + if (g_is_fail_exit == TRUE) + goto fail; + /* Crate the GLIB main loop */ main_loop = g_main_loop_new(NULL, FALSE); g_stc->main_loop = main_loop; @@ -283,5 +296,8 @@ fail: if (main_loop) g_main_loop_unref(main_loop); + if (g_is_fail_exit == TRUE) + exit(-1); + return 0; } -- 2.7.4 From 49bd69e235bd22a9ba879d5a341882d0684236f4 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 11 Jun 2020 22:28:39 +0530 Subject: [PATCH 12/16] Restart stc-manager at non-zero exit code after 5 seconds Change-Id: I2081d8328d09075e7130a803d921b06dc0399469 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- resources/systemd/stc-manager.service | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index e9eaf3f..02a8b04 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/resources/systemd/stc-manager.service b/resources/systemd/stc-manager.service index e20402f..899db52 100644 --- a/resources/systemd/stc-manager.service +++ b/resources/systemd/stc-manager.service @@ -6,6 +6,8 @@ Type=dbus BusName=net.stc SmackProcessLabel=System ExecStart=/usr/bin/stc-manager +Restart=on-failure +RestartSec=5 [Install] WantedBy=multi-user.target -- 2.7.4 From 26cfbd948434b794b3a4aa68c042b221ae0f324e Mon Sep 17 00:00:00 2001 From: Jaehyun Kim Date: Mon, 22 Jun 2020 15:34:16 +0900 Subject: [PATCH 13/16] Fix memory leak in stc_plugin_monitor_initialize Variable system going out of scope leaks the memory it points to. Change-Id: I9d2c5dc3cc3b951954e793cf3864f270810d846c Signed-off-by: Jaehyun Kim --- plugin/monitor/stc-plugin-monitor.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/monitor/stc-plugin-monitor.c b/plugin/monitor/stc-plugin-monitor.c index b278e94..1143990 100755 --- a/plugin/monitor/stc-plugin-monitor.c +++ b/plugin/monitor/stc-plugin-monitor.c @@ -331,15 +331,15 @@ stc_error_e stc_plugin_monitor_initialize(stc_manager_stop_cb stop_cb) { __STC_LOG_FUNC_ENTER__; + ret_value_msg_if(stop_cb == NULL, STC_ERROR_INVALID_PARAMETER, + "stop_cb callback is NULL"); + stc_system_s *system = MALLOC0(stc_system_s, 1); GIOChannel *gio = NULL; ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!"); - ret_value_msg_if(stop_cb == NULL, STC_ERROR_INVALID_PARAMETER, - "stop_cb callback is NULL"); - g_stop_cb = stop_cb; /* initializing current classid */ -- 2.7.4 From 22025b40ab2c07a5b773b12c71c14f42aa15d7e2 Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Thu, 25 Jun 2020 18:38:43 +0900 Subject: [PATCH 14/16] Free all the unpacked values if breaking out of g_variant_iter_loop Change-Id: Ib5e67dfa852f38431f19ed66a540275165073724 --- plugin/monitor/stc-plugin-monitor-connection.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin/monitor/stc-plugin-monitor-connection.c b/plugin/monitor/stc-plugin-monitor-connection.c index a305002..02a12ac 100755 --- a/plugin/monitor/stc-plugin-monitor-connection.c +++ b/plugin/monitor/stc-plugin-monitor-connection.c @@ -489,6 +489,8 @@ static stc_error_e __get_default_connection(GDBusConnection *connection) if (comp && comp->data) g_default_connection = comp->data; + g_free(object_path); + g_variant_iter_free(next); break; } } -- 2.7.4 From 664157d35cd3f6a2e9f9dc501b7a3f94b66c2f25 Mon Sep 17 00:00:00 2001 From: Cheoleun Moon Date: Thu, 25 Jun 2020 18:42:14 +0900 Subject: [PATCH 15/16] Free before return Change-Id: I951213a634605c561d9061c45134c6d7ef924d67 --- src/stc-manager-util.c | 1 + src/stc-pcap.c | 6 ++++++ src/stc-restriction.c | 20 ++++++++++++++++++++ src/stc-statistics.c | 2 ++ 4 files changed, 29 insertions(+) diff --git a/src/stc-manager-util.c b/src/stc-manager-util.c index 1f25927..3e77610 100755 --- a/src/stc-manager-util.c +++ b/src/stc-manager-util.c @@ -182,4 +182,5 @@ void stc_util_initialize_config(void) } __save_key_file(keyfile, path); + g_key_file_free(keyfile); } diff --git a/src/stc-pcap.c b/src/stc-pcap.c index 3faa7ec..29f5037 100755 --- a/src/stc-pcap.c +++ b/src/stc-pcap.c @@ -106,6 +106,7 @@ gboolean handle_pcap_start(StcPcap *object, if (__validate_pcap(&pcap) == FALSE) { STC_PCAP_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE STC_ERROR_INVALID_PARAMETER); + g_free(pcap.ifname); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } @@ -113,11 +114,13 @@ gboolean handle_pcap_start(StcPcap *object, ret = stc_plugin_pcap_register_loop(pcap.ifname, pcap.nflog_group); if (ret != STC_ERROR_NONE) { STC_PCAP_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE + g_free(pcap.ifname); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } STC_DBUS_REPLY_ERROR_NONE(invocation); + g_free(pcap.ifname); __STC_LOG_FUNC_EXIT__; return TRUE; } @@ -147,6 +150,7 @@ gboolean handle_pcap_stop(StcPcap *object, if (__validate_pcap(&pcap) == FALSE) { STC_PCAP_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE STC_ERROR_INVALID_PARAMETER); + g_free(pcap.ifname); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } @@ -154,11 +158,13 @@ gboolean handle_pcap_stop(StcPcap *object, ret = stc_plugin_pcap_unregister_loop(pcap.ifname, pcap.nflog_group); if (ret != STC_ERROR_NONE) { STC_PCAP_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE + g_free(pcap.ifname); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } STC_DBUS_REPLY_ERROR_NONE(invocation); + g_free(pcap.ifname); __STC_LOG_FUNC_EXIT__; return TRUE; } diff --git a/src/stc-restriction.c b/src/stc-restriction.c index d47aaf0..47bfee8 100755 --- a/src/stc-restriction.c +++ b/src/stc-restriction.c @@ -259,6 +259,14 @@ static void __stc_extract_restriction_rule(const char *key, GVariant *value, } } +static void __stc_free_restriction_rule_members(table_restrictions_info *rule) +{ + FREE(rule->app_id); + FREE(rule->ifname); + FREE(rule->subscriber_id); + FREE(rule->mac); +} + gboolean handle_restriction_set(StcRestriction *object, GDBusMethodInvocation *invocation, GVariant *parameters, @@ -285,6 +293,7 @@ gboolean handle_restriction_set(StcRestriction *object, if (__validate_rstn_rule(&rule) == FALSE) { STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE STC_ERROR_INVALID_PARAMETER); + __stc_free_restriction_rule_members(&rule); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } @@ -293,12 +302,14 @@ gboolean handle_restriction_set(StcRestriction *object, if (ret != STC_ERROR_NONE) { STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE STC_ERROR_DB_FAILED); + __stc_free_restriction_rule_members(&rule); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } stc_plugin_monitor_add_rstn(&rule); STC_DBUS_REPLY_ERROR_NONE(invocation); + __stc_free_restriction_rule_members(&rule); __STC_LOG_FUNC_EXIT__; return TRUE; } @@ -329,6 +340,7 @@ gboolean handle_restriction_unset(StcRestriction *object, if (__validate_rstn_rule(&rule) == FALSE) { STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE STC_ERROR_INVALID_PARAMETER); + __stc_free_restriction_rule_members(&rule); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } @@ -338,12 +350,14 @@ gboolean handle_restriction_unset(StcRestriction *object, if (ret != STC_ERROR_NONE) { STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE STC_ERROR_DB_FAILED); + __stc_free_restriction_rule_members(&rule); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } stc_plugin_monitor_remove_rstn(&rule); STC_DBUS_REPLY_ERROR_NONE(invocation); + __stc_free_restriction_rule_members(&rule); __STC_LOG_FUNC_EXIT__; return TRUE; } @@ -376,6 +390,7 @@ gboolean handle_restriction_set_list(StcRestriction *object, STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE g_variant_iter_free(iter_row); g_variant_iter_free(iter); + __stc_free_restriction_rule_members(&rule); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } @@ -385,11 +400,13 @@ gboolean handle_restriction_set_list(StcRestriction *object, STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE g_variant_iter_free(iter_row); g_variant_iter_free(iter); + __stc_free_restriction_rule_members(&rule); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } } + __stc_free_restriction_rule_members(&rule); g_variant_iter_free(iter_row); } g_variant_iter_free(iter); @@ -428,6 +445,7 @@ gboolean handle_restriction_unset_list(StcRestriction *object, STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE g_variant_iter_free(iter_row); g_variant_iter_free(iter); + __stc_free_restriction_rule_members(&rule); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } @@ -437,11 +455,13 @@ gboolean handle_restriction_unset_list(StcRestriction *object, STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE g_variant_iter_free(iter_row); g_variant_iter_free(iter); + __stc_free_restriction_rule_members(&rule); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } } + __stc_free_restriction_rule_members(&rule); g_variant_iter_free(iter_row); } g_variant_iter_free(iter); diff --git a/src/stc-statistics.c b/src/stc-statistics.c index a0b5a07..feee566 100755 --- a/src/stc-statistics.c +++ b/src/stc-statistics.c @@ -360,6 +360,7 @@ gboolean handle_statistics_get(StcStatistics *object, if (ret < STC_ERROR_NONE) { g_variant_builder_unref(builder); //LCOV_EXCL_LINE STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE + g_free(rule.app_id); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; //LCOV_EXCL_LINE } @@ -370,6 +371,7 @@ gboolean handle_statistics_get(StcStatistics *object, DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters); STC_DBUS_REPLY(invocation, return_parameters); + g_free(rule.app_id); __STC_LOG_FUNC_EXIT__; return TRUE; } -- 2.7.4 From a29e3b479122e4a8d4dc40a39c6a3c39a635879e Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Wed, 29 Apr 2020 10:10:30 +0900 Subject: [PATCH 16/16] Add dbus async call for add/remove ipt rule Change-Id: I30394b30614b51e259e554c3631abdbd5d17af54 Signed-off-by: hyunuk.tak --- include/stc-manager-gdbus.h | 6 ++ src/helper/helper-iptables.c | 144 +++++++++++++++++++++++++++---------------- src/stc-manager-gdbus.c | 28 +++++++++ 3 files changed, 126 insertions(+), 52 deletions(-) diff --git a/include/stc-manager-gdbus.h b/include/stc-manager-gdbus.h index 263077d..b535ed2 100755 --- a/include/stc-manager-gdbus.h +++ b/include/stc-manager-gdbus.h @@ -61,6 +61,12 @@ GVariant *stc_manager_gdbus_call_sync(GDBusConnection *connection, const char *dest, const char *path, const char *interface_name, const char *method, GVariant *params); +int stc_manager_gdbus_call_async(GDBusConnection *connection, + const char *dest, const char *path, + const char *interface_name, + const char *method, GVariant *params, + GAsyncReadyCallback notify_func, + void *user_data); guint stc_manager_gdbus_subscribe_signal(GDBusConnection *connection, const gchar *sender, const gchar *interface_name, diff --git a/src/helper/helper-iptables.c b/src/helper/helper-iptables.c index 1e140b7..519d898 100755 --- a/src/helper/helper-iptables.c +++ b/src/helper/helper-iptables.c @@ -97,136 +97,176 @@ static void __add_rule_info_to_builder(GVariantBuilder *builder, g_variant_new_uint32(rule->d_ip2.s_addr)); } +static void __add_rule_reply( + GObject *source_object, GAsyncResult *res, gpointer user_data) +{ + GDBusConnection *conn = NULL; + GVariant *dbus_data = NULL; + GError *dbus_error = NULL; + int result = 0; + char *nfacct_name = user_data; + + conn = G_DBUS_CONNECTION(source_object); + dbus_data = g_dbus_connection_call_finish(conn, res, &dbus_error); + if (dbus_error != NULL) { + STC_LOGE("Dbus reply error [%s]", dbus_error->message); + g_error_free(dbus_error); + } else { + g_variant_get(dbus_data, "(i)", &result); + STC_LOGI("Added rule [%d:%s]", result, nfacct_name); + } + + g_free(nfacct_name); +} + +static void __remove_rule_reply( + GObject *source_object, GAsyncResult *res, gpointer user_data) +{ + GDBusConnection *conn = NULL; + GVariant *dbus_data = NULL; + GError *dbus_error = NULL; + int result = 0; + char *nfacct_name = user_data; + + conn = G_DBUS_CONNECTION(source_object); + dbus_data = g_dbus_connection_call_finish(conn, res, &dbus_error); + if (dbus_error != NULL) { + STC_LOGE("Dbus reply error [%s]", dbus_error->message); + g_error_free(dbus_error); + } else { + g_variant_get(dbus_data, "(i)", &result); + STC_LOGI("Removed rule [%d:%s]", result, nfacct_name); + } + + g_free(nfacct_name); +} + static int __iptables_rule_add(GDBusConnection *connection, iptables_rule_s *rule) { - int result = 0; + int result = STC_ERROR_NONE; GVariantBuilder *builder = NULL; GVariant *params = NULL; - GVariant *message = NULL; + char *nfacct_name = NULL; builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); __add_rule_info_to_builder(builder, rule); params = g_variant_new("(a{sv})", builder); g_variant_builder_unref(builder); - message = stc_manager_gdbus_call_sync(connection, + nfacct_name = g_strdup_printf("4:%s", rule->nfacct_name); + + result = stc_manager_gdbus_call_async(connection, STC_IPTABLES_DBUS_SERVICE, STC_IPTABLES_DBUS_RULE_PATH, STC_IPTABLES_DBUS_RULE_INTERFACE, STC_IPTABLES_DBUS_METHOD_IPT_ADD_RULE, - params); + params, + __add_rule_reply, + nfacct_name); - if (message == NULL) { - STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE - return STC_ERROR_FAIL; //LCOV_EXCL_LINE + if (result != STC_ERROR_NONE) { + STC_LOGE("Failed to invoke dbus method async"); + g_free(nfacct_name); } - g_variant_get(message, "(i)", &result); - if (STC_DEBUG_LOG) - STC_LOGD("Successfully Add Rule [%d:%s]", result, rule->nfacct_name); - g_variant_unref(message); - - return STC_ERROR_NONE; + return result; } static int __iptables_rule_remove(GDBusConnection *connection, iptables_rule_s *rule) { - int result = 0; + int result = STC_ERROR_NONE; GVariantBuilder *builder = NULL; GVariant *params = NULL; - GVariant *message = NULL; + char *nfacct_name = NULL; builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); __add_rule_info_to_builder(builder, rule); params = g_variant_new("(a{sv})", builder); g_variant_builder_unref(builder); - message = stc_manager_gdbus_call_sync(connection, + nfacct_name = g_strdup_printf("4:%s", rule->nfacct_name); + + result = stc_manager_gdbus_call_async(connection, STC_IPTABLES_DBUS_SERVICE, STC_IPTABLES_DBUS_RULE_PATH, STC_IPTABLES_DBUS_RULE_INTERFACE, STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_RULE, - params); + params, + __remove_rule_reply, + nfacct_name); - if (message == NULL) { - STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE - return STC_ERROR_FAIL; //LCOV_EXCL_LINE + if (result != STC_ERROR_NONE) { + STC_LOGE("Failed to invoke dbus method async"); + g_free(nfacct_name); } - g_variant_get(message, "(i)", &result); - if (STC_DEBUG_LOG) - STC_LOGD("Successfully Remove Rule [%d:%s]", result, rule->nfacct_name); - g_variant_unref(message); - - return STC_ERROR_NONE; + return result; } static int __ip6tables_rule_add(GDBusConnection *connection, iptables_rule_s *rule) { - int result = 0; + int result = STC_ERROR_NONE; GVariantBuilder *builder = NULL; GVariant *params = NULL; - GVariant *message = NULL; + char *nfacct_name = NULL; builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); __add_rule_info_to_builder(builder, rule); params = g_variant_new("(a{sv})", builder); g_variant_builder_unref(builder); - message = stc_manager_gdbus_call_sync(connection, + nfacct_name = g_strdup_printf("6:%s", rule->nfacct_name); + + result = stc_manager_gdbus_call_async(connection, STC_IPTABLES_DBUS_SERVICE, STC_IPTABLES_DBUS_RULE_PATH, STC_IPTABLES_DBUS_RULE_INTERFACE, STC_IPTABLES_DBUS_METHOD_IP6T_ADD_RULE, - params); + params, + __add_rule_reply, + nfacct_name); - if (message == NULL) { - STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE - return STC_ERROR_FAIL; //LCOV_EXCL_LINE + if (result != STC_ERROR_NONE) { + STC_LOGE("Failed to invoke dbus method async"); + g_free(nfacct_name); } - g_variant_get(message, "(i)", &result); - if (STC_DEBUG_LOG) - STC_LOGD("Successfully Add 6 Rule [%d:%s]", result, rule->nfacct_name); - g_variant_unref(message); - - return STC_ERROR_NONE; + return result; } static int __ip6tables_rule_remove(GDBusConnection *connection, iptables_rule_s *rule) { - int result = 0; + int result = STC_ERROR_NONE; GVariantBuilder *builder = NULL; GVariant *params = NULL; - GVariant *message = NULL; + char *nfacct_name = NULL; builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); __add_rule_info_to_builder(builder, rule); params = g_variant_new("(a{sv})", builder); g_variant_builder_unref(builder); - message = stc_manager_gdbus_call_sync(connection, + nfacct_name = g_strdup_printf("6:%s", rule->nfacct_name); + + result = stc_manager_gdbus_call_async(connection, STC_IPTABLES_DBUS_SERVICE, STC_IPTABLES_DBUS_RULE_PATH, STC_IPTABLES_DBUS_RULE_INTERFACE, STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_RULE, - params); + params, + __remove_rule_reply, + nfacct_name); - if (message == NULL) { - STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE - return STC_ERROR_FAIL; //LCOV_EXCL_LINE + if (result != STC_ERROR_NONE) { + STC_LOGE("Failed to invoke dbus method async"); + g_free(nfacct_name); } - g_variant_get(message, "(i)", &result); - if (STC_DEBUG_LOG) - STC_LOGD("Successfully Remove 6 Rule [%d:%s]", result, rule->nfacct_name); - g_variant_unref(message); - - return STC_ERROR_NONE; + return result; } static int __iptables_add_chain(GDBusConnection *connection, diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index 943f466..a5cb068 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -466,6 +466,34 @@ API GVariant *stc_manager_gdbus_call_sync(GDBusConnection *connection, return reply; } +API int stc_manager_gdbus_call_async(GDBusConnection *connection, + const char *dest, const char *path, + const char *interface_name, + const char *method, GVariant *params, + GAsyncReadyCallback notify_func, + void *user_data) +{ + if (connection == NULL) { + STC_LOGE("Failed to get GDBusconnection"); //LCOV_EXCL_LINE + return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE + } + + g_dbus_connection_call(connection, + dest, + path, + interface_name, + method, + params, + NULL, + G_DBUS_CALL_FLAGS_NONE, + (5 * 1000), /* 5 seconds timeout */ + NULL, + (GAsyncReadyCallback)notify_func, + (gpointer)user_data); + + return STC_ERROR_NONE; +} + API guint stc_manager_gdbus_subscribe_signal(GDBusConnection *connection, const gchar *sender, const gchar *interface_name, -- 2.7.4