From 75e80e452edc23a1ea570de8377a1a3c74337cf4 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Wed, 7 Jun 2017 10:16:46 +0530 Subject: [PATCH 01/16] Removed unused preset restrictions list. Change-Id: I8b2c4fab85db417a1831668c6172924d3e9452b5 Signed-off-by: Nishant Chaprana --- data/exception_db.sql | 49 ---------------------------------------------- packaging/stc-manager.spec | 4 +--- src/CMakeLists.txt | 1 - 3 files changed, 1 insertion(+), 53 deletions(-) delete mode 100755 data/exception_db.sql diff --git a/data/exception_db.sql b/data/exception_db.sql deleted file mode 100755 index 7613769..0000000 --- a/data/exception_db.sql +++ /dev/null @@ -1,49 +0,0 @@ -PRAGMA journal_mode = PERSIST; -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.easysignup", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.osmeta.runtime", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.osmeta.runtime.service", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.email", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("email-composer-efl", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("email-viewer-efl", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("email-conversation-efl", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("email-mailbox-efl", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("email-account-efl", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("email-filter-efl", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("email-block-efl", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("email-locker-efl", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("email-setting-efl", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.email-misc_worker", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.email-record-video-icon", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.samsungaccount", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.samsungaccount.samsungaccountservice", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.samsungaccount.samsungaccountpushefl", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.samsungaccount.samsungaccountupdate", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.special-day-app", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.special-day-widget", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("org.tizen.tizenstore.billingagent", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("org.tizen.inapppurchase", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("org.tizen.inapppurchase.iapclient", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("org.tizen.inapppurchase.iapservice", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("org.tizen.tizenstore", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("org.tizen.tizenstoreservice", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.videos-lite", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.video-player-lite", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("org.tizen.webcontainer", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.themestore", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("ACL111OMWW.AclService", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("ACL111OMWW.AclManager", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("ACL111OMWW.AclAudioProxyService", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.gallery-lite", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("gallery-efl-lite", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.gallery-lite.appcontrol", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.image-viewer.appcontrol.slideshow", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.image-viewer", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.image-viewer-subapp", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.image-viewer-subapp-single", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("image-viewer-efl-lite", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.gallery-lite.dbox", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.music-player-lite", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.sound-player-lite", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.music-chooser-lite", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.music-player-lite.widget", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); -INSERT INTO restrictions (binpath, iftype, ifname, rst_state, roaming, imsi, rcv_limit, send_limit, rcv_warn_limit, send_warn_limit) VALUES("com.samsung.cloud-content-sync", 1, "seth_w0", 3, 2, "noneimsi", 0, 0, 1, 1); diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index d575b3b..4852a41 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.12 +Version: 0.0.13 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 @@ -59,8 +59,6 @@ rm -rf %{buildroot} mkdir -p %{buildroot}/opt/usr/dbspace sqlite3 %{buildroot}%{database_full_path} < %{buildroot}/usr/share/traffic_db.sql rm %{buildroot}/usr/share/traffic_db.sql - sqlite3 %{buildroot}%{database_full_path} < %{buildroot}/usr/share/exception_db.sql - rm %{buildroot}/usr/share/exception_db.sql %endif #Systemd service file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 51245d5..4173f82 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -59,7 +59,6 @@ IF("${ENABLE_DATABASE}" STREQUAL "YES") SET(SRCS ${SRCS} ${DATABASE_SRCS} ${DATABASE_TABLES_SRCS}) INSTALL(FILES ${DATA_DIR}/traffic_db.sql DESTINATION /usr/share) - INSTALL(FILES ${DATA_DIR}/exception_db.sql DESTINATION /usr/share) ENDIF() SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fpic -Wall -Werror-implicit-function-declaration -fvisibility=hidden") -- 2.7.4 From c47c751815c1171b8f653adf3bac6b2dfcc6fc97 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Fri, 9 Jun 2017 13:41:44 +0530 Subject: [PATCH 02/16] Monitoring application wise background data usage. Description: This patch updates logic to monitor application wise background data usage. When background data usage is notified, app_id will be appended with "_BACKGROUND". Below is the test result performed using stc_test application. ================================================================= Main >> STC >> [Statistics] >> [Stats] ----------------------------------------------------------------- [ 1] [Set] stats rule > [ 2] [Get] stats [ 3] [Get] all stats [ 4] [Get] total stats --------------------------------------------------------------- [ p ] Previous Menu [ m ] Show Menu [ q ] Quit ================================================================= ( 1354) >> 3 Success to set time interval Success to request stats all info ( 1354) >> ----------------------------------------------------------------- App_id: [org.tizen.browser] Ifname: [] Imsi: [noneimsi] Iftype: [Undefined] Received: [2182136] Sent: [102681] Roaming: [Unknown] Protocol: [Unknown] Process: [Unknown] From: [0] To: [0] ----------------------------------------------------------------- ----------------------------------------------------------------- App_id: [org.tizen.browser_BACKGROUND] Ifname: [] Imsi: [noneimsi] Iftype: [Undefined] Received: [1872] Sent: [1872] Roaming: [Unknown] Protocol: [Unknown] Process: [Unknown] From: [0] To: [0] ----------------------------------------------------------------- Change-Id: Id8175494e559790e2959e5a6e7164de24e00a548 Signed-off-by: Nishant Chaprana --- include/stc-manager.h | 6 +-- src/configure/include/counter.h | 3 -- src/database/tables/table-statistics.c | 4 +- src/helper/helper-cgroup.c | 14 +++++ src/helper/helper-cgroup.h | 17 ++++-- src/helper/helper-net-cls.c | 96 ++++++++------------------------- src/monitor/include/stc-monitor.h | 5 +- src/monitor/stc-application-lifecycle.c | 16 ++++-- src/monitor/stc-monitor.c | 78 +++++++-------------------- src/utils/net-cls-release.c | 9 ++-- 10 files changed, 90 insertions(+), 158 deletions(-) diff --git a/include/stc-manager.h b/include/stc-manager.h index 6df86af..73b7370 100755 --- a/include/stc-manager.h +++ b/include/stc-manager.h @@ -23,11 +23,7 @@ #define NET_RELEASE_AGENT "/usr/bin/net-cls-release" #define NET_CLS_SUBSYS "net_cls" -#define STC_BACKGROUND_APP_NAME "BACKGROUND" -#define NETWORK_RESTRICTION_APP_NAME "NETWORKRESTRICTION" - -#define STC_ALL_APP "STC_ALL_APPLICATION_IDENTIFIER" -#define TETHERING_APP_NAME "STC_TETHERING_APPLICATION_IDENTIFIER" +#define STC_BACKGROUND_APP_SUFFIX "_BACKGROUND" typedef enum { STC_CANCEL = 0, /**< cancel */ diff --git a/src/configure/include/counter.h b/src/configure/include/counter.h index 760a56a..95bc0ce 100755 --- a/src/configure/include/counter.h +++ b/src/configure/include/counter.h @@ -19,9 +19,6 @@ #include "stc-db.h" -#define STC_BACKGROUND_APP_NAME "BACKGROUND" -#define NETWORK_RESTRICTION_APP_NAME "NETWORKRESTRICTION" - struct counter_arg { int sock; int ans_len; diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c index 05e66f6..e16059a 100755 --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -103,7 +103,7 @@ #define SELECT_TOTAL "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ "ifname, imsi, ground from statistics " \ - " where binpath='STC_ALL_APPLICATION_IDENTIFIER' AND (time_stamp between ? and ?) " \ + " where (time_stamp between ? and ?) " \ "group by iftype, ifname, imsi, hw_net_protocol_type, is_roaming " \ "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ "is_roaming" @@ -111,7 +111,7 @@ #define SELECT_TOTAL_IFACE "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ "ifname, imsi, ground from statistics " \ - " where binpath='STC_ALL_APPLICATION_IDENTIFIER' AND (time_stamp between ? and ?) " \ + " where (time_stamp between ? and ?) " \ "and iftype=? " \ "group by hw_net_protocol_type, is_roaming, " \ "iftype, ifname, imsi " \ diff --git a/src/helper/helper-cgroup.c b/src/helper/helper-cgroup.c index fe05ea1..a725dcc 100755 --- a/src/helper/helper-cgroup.c +++ b/src/helper/helper-cgroup.c @@ -273,3 +273,17 @@ int cgroup_set_release_agent(const char *cgroup_subsys, return cgroup_write_node_str(buf, NOTIFY_ON_RELEASE, "1"); } + +void cgroup_init(void) +{ + /* create stc cgroup directory */ + cgroup_make_subdir(CGROUP_NETWORK, STC_CGROUP_NAME, NULL); + + /* create background cgroup directory */ + cgroup_make_subdir(STC_CGROUP_NETWORK, STC_BACKGROUND_CGROUP_NAME, + NULL); + + /* create foreground cgroup directory */ + cgroup_make_subdir(STC_CGROUP_NETWORK, STC_FOREGROUND_CGROUP_NAME, + NULL); +} diff --git a/src/helper/helper-cgroup.h b/src/helper/helper-cgroup.h index e3398af..a358066 100755 --- a/src/helper/helper-cgroup.h +++ b/src/helper/helper-cgroup.h @@ -28,9 +28,15 @@ #include "stc-manager.h" #include "helper-file.h" -#define DEFAULT_CGROUP "/sys/fs/cgroup" -#define CGROUP_NETWORK DEFAULT_CGROUP "/net_cls" -#define PROC_TASK_CHILDREN "/proc/%d/task/%d/children" +#define DEFAULT_CGROUP "/sys/fs/cgroup" +#define CGROUP_NETWORK DEFAULT_CGROUP "/net_cls" +#define STC_CGROUP_NETWORK CGROUP_NETWORK "/stc" +#define BACKGROUND_CGROUP_NETWORK STC_CGROUP_NETWORK "/background" +#define FOREGROUND_CGROUP_NETWORK STC_CGROUP_NETWORK "/foreground" +#define PROC_TASK_CHILDREN "/proc/%d/task/%d/children" +#define STC_CGROUP_NAME "stc" +#define STC_BACKGROUND_CGROUP_NAME "background" +#define STC_FOREGROUND_CGROUP_NAME "foreground" /** * @desc Get one unsigned int32 value from cgroup @@ -116,4 +122,9 @@ int cgroup_get_pids(const char *name, GArray **pids); int cgroup_remove_pid(const char *cgroup_subsystem, const char *cgroup_name, const int pid); +/** + * @desc initializes cgroups. + */ +void cgroup_init(void); + #endif /*__STC_HELPER_CGROUP_H__*/ diff --git a/src/helper/helper-net-cls.c b/src/helper/helper-net-cls.c index d08f559..8f3f5d0 100755 --- a/src/helper/helper-net-cls.c +++ b/src/helper/helper-net-cls.c @@ -82,81 +82,41 @@ static uint32_t __get_classid_from_cgroup(const char *cgroup, return classid; } -/* TODO: background tasks update from list */ -static void __add_background_apps(GSList *background_pid_list) -{ - if (background_pid_list == NULL) - return; - - /* remove old list */ - /* add new list */ -} - -static stc_error_e __make_net_cls_cgroup(const char *pkg_name, uint32_t classid, - check_classid_used_cb cb) -{ - stc_error_e ret = STC_ERROR_NONE; - bool exists = false; - - if (pkg_name == NULL) { - STC_LOGE("package name must be not empty"); - return STC_ERROR_INVALID_PARAMETER; - } - - ret = cgroup_make_subdir(PATH_TO_NET_CGROUP_DIR, (char *)pkg_name, - &exists); - ret_value_if(ret < 0, STC_ERROR_FAIL); - - if (!exists) { - ret = __place_classid_to_cgroup(PATH_TO_NET_CGROUP_DIR, - pkg_name, - classid ? &classid : NULL, cb); - ret_value_if(ret < 0, STC_ERROR_FAIL); - } - return ret; -} - uint32_t get_classid_by_app_id(const char *app_id, int create) { int ret = 0; bool exists; 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"); return STC_UNKNOWN_CLASSID; } - if (!strcmp(app_id, STC_ALL_APP)) - return STC_ALL_APP_CLASSID; - - if (!strcmp(app_id, TETHERING_APP_NAME)) - return STC_TETHERING_APP_CLASSID; - - if (!strcmp(app_id, STC_BACKGROUND_APP_NAME)) - return STC_BACKGROUND_APP_CLASSID; - - if (!strcmp(app_id, NETWORK_RESTRICTION_APP_NAME)) - return STC_NETWORK_RESTRICTION_APP_CLASSID; + if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX)) + path_to_net_cgroup_dir = BACKGROUND_CGROUP_NETWORK; + else + path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK; /* just read */ if (!create) - classid = __get_classid_from_cgroup(PATH_TO_NET_CGROUP_DIR, + classid = __get_classid_from_cgroup(path_to_net_cgroup_dir, app_id); if (classid != STC_UNKNOWN_CLASSID) return classid; - ret = cgroup_make_subdir(PATH_TO_NET_CGROUP_DIR, (char *)app_id, + ret = cgroup_make_subdir(path_to_net_cgroup_dir, (char *)app_id, &exists); if (ret) goto handle_error; if (exists) - classid = __get_classid_from_cgroup(PATH_TO_NET_CGROUP_DIR, + classid = __get_classid_from_cgroup(path_to_net_cgroup_dir, app_id); else - ret = __place_classid_to_cgroup(PATH_TO_NET_CGROUP_DIR, + ret = __place_classid_to_cgroup(path_to_net_cgroup_dir, (char *)app_id, &classid, NULL); if (ret) goto handle_error; @@ -169,41 +129,27 @@ handle_error: return STC_UNKNOWN_CLASSID; } -stc_error_e place_pids_to_net_cgroup(const int pid, const char *pkg_name) +stc_error_e place_pids_to_net_cgroup(const int pid, const char *app_id) { char child_buf[21 + MAX_DEC_SIZE(int) + MAX_DEC_SIZE(int)]; + const char *path_to_net_cgroup_dir = NULL; + snprintf(child_buf, sizeof(child_buf), PROC_TASK_CHILDREN, pid, pid); - if (pkg_name == NULL) { + if (app_id == NULL) { STC_LOGE("package name must be not empty"); return STC_ERROR_INVALID_PARAMETER; } - if (access(child_buf, F_OK)) { - STC_LOGD("%s of %s is not existed", child_buf, pkg_name); - return cgroup_write_pid(PATH_TO_NET_CGROUP_DIR, pkg_name, pid); - } - - return cgroup_write_pidtree(PATH_TO_NET_CGROUP_DIR, pkg_name, pid); -} - -void create_net_background_cgroup(GSList *background_pid_list) -{ - stc_error_e ret = __make_net_cls_cgroup(STC_BACKGROUND_APP_NAME, - STC_BACKGROUND_APP_CLASSID, - NULL); - if (ret == STC_ERROR_NONE) - __add_background_apps(background_pid_list); + if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX)) + path_to_net_cgroup_dir = BACKGROUND_CGROUP_NETWORK; else - STC_LOGD("Could not support quota for background applications"); -} + path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK; -void add_pid_to_background_cgroup(pid_t pid) -{ - place_pids_to_net_cgroup(pid, STC_BACKGROUND_APP_NAME); -} + if (access(child_buf, F_OK)) { + STC_LOGD("%s of %s is not existed", child_buf, app_id); + return cgroup_write_pid(path_to_net_cgroup_dir, app_id, pid); + } -void remove_pid_from_background_cgroup(pid_t pid) -{ - cgroup_remove_pid(PATH_TO_NET_CGROUP_DIR, STC_BACKGROUND_APP_NAME, pid); + return cgroup_write_pidtree(path_to_net_cgroup_dir, app_id, pid); } diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index 385c9fb..feb0f12 100644 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -26,8 +26,8 @@ #include "helper-nl.h" #include "netlink-restriction.h" -/* 10 seconds */ -#define CONTR_TIMER_INTERVAL 10 +/* 1 seconds */ +#define CONTR_TIMER_INTERVAL 1 /** * @brief key for processes tree @@ -102,7 +102,6 @@ typedef struct { gboolean rstns_tree_updated; GTree *apps; /**< monitored applications */ gboolean apps_tree_updated; - GSList *background_pids; /**< list of background pids */ } stc_system_s; /** diff --git a/src/monitor/stc-application-lifecycle.c b/src/monitor/stc-application-lifecycle.c index 2b1243c..a0fb931 100755 --- a/src/monitor/stc-application-lifecycle.c +++ b/src/monitor/stc-application-lifecycle.c @@ -148,11 +148,19 @@ static stc_error_e __stc_manager_app_status_changed(stc_cmd_type_e cmd, proc_value.ground = STC_APP_STATE_UNKNOWN; + /* create foreground cgroup */ stc_monitor_application_add(app_key, app_value); stc_monitor_process_add(app_key, proc_key, proc_value); - FREE(app_key.pkg_id); + /* create background cgroup */ FREE(app_key.app_id); + + app_key.app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, + NULL); + stc_monitor_application_add(app_key, app_value); + + FREE(app_key.pkg_id); + g_free(app_key.app_id); break; } case STC_CMD_SET_SERVICE_LAUNCHED: @@ -168,20 +176,22 @@ static stc_error_e __stc_manager_app_status_changed(stc_cmd_type_e cmd, memset(&proc_value, 0, sizeof(stc_process_value_s)); app_key.pkg_id = g_strdup(pkg_id); - app_key.app_id = g_strdup(app_id); + app_key.app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, + NULL); app_value.type = app_type; app_value.processes = NULL; proc_key.pid = pid; + /* services will run always in background. */ proc_value.ground = STC_APP_STATE_BACKGROUND; stc_monitor_application_add(app_key, app_value); stc_monitor_process_add(app_key, proc_key, proc_value); FREE(app_key.pkg_id); - FREE(app_key.app_id); + g_free(app_key.app_id); break; } case STC_CMD_SET_TERMINATED: diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index b12920e..3015b51 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -20,6 +20,7 @@ #include "helper-nl.h" #include "helper-nfacct-rule.h" #include "helper-net-cls.h" +#include "helper-cgroup.h" #include "counter.h" #include "table-statistics.h" #include "table-counters.h" @@ -210,10 +211,12 @@ static gboolean __apps_tree_foreach_print(gpointer key, gpointer value, return FALSE; } +#if 0 static void __apps_tree_printall(void) { g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL); } +#endif static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value, gpointer data) @@ -1094,10 +1097,8 @@ static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info, value.rst_state = info->rst_state; value.restriction_id = info->restriction_id; - if (value.rst_state != STC_RESTRICTION_EXCLUDED) - value.classid = get_classid_by_app_id(info->app_id ? - info->app_id : - STC_ALL_APP, TRUE); + if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id) + value.classid = get_classid_by_app_id(info->app_id, TRUE); else value.classid = STC_UNKNOWN_CLASSID; @@ -1158,34 +1159,6 @@ static void __add_rstns_for_application(gchar *app_id) app_id); } -/* used only for (STC_ALL_APP/STC_BACKGROUND_APP_NAME) */ -static void __stc_monitor_add_application_by_app_id(const char *app_id) -{ - stc_app_key_s app_key; - stc_app_value_s app_value; - - if (app_id == NULL) - return; - - memset(&app_key, 0, sizeof(stc_app_key_s)); - memset(&app_value, 0, sizeof(stc_app_value_s)); - - app_key.pkg_id = g_strdup(app_id); - app_key.app_id = g_strdup(app_id); - - app_value.type = STC_APP_TYPE_NONE; - app_value.processes = NULL; - app_value.counter.in_bytes = 0; - app_value.counter.out_bytes = 0; - - stc_monitor_application_add(app_key, app_value); - - FREE(app_key.pkg_id); - FREE(app_key.app_id); - - __apps_tree_printall(); -} - stc_error_e stc_monitor_init(void) { stc_system_s *system = MALLOC0(stc_system_s, 1); @@ -1193,6 +1166,9 @@ stc_error_e stc_monitor_init(void) ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!"); + /* initializing cgroups */ + cgroup_init(); + /* creating monitored application tree */ system->apps = g_tree_new_full(__apps_tree_key_compare, NULL, __apps_tree_key_free, @@ -1219,16 +1195,6 @@ stc_error_e stc_monitor_init(void) g_system = system; - /* create entry for STC_ALL_APP */ - __stc_monitor_add_application_by_app_id(STC_ALL_APP); - - /* create entry for STC_BACKGROUND_APP_NAME */ - __stc_monitor_add_application_by_app_id(STC_BACKGROUND_APP_NAME); - - /* create background cgroup */ - g_system->background_pids = NULL; - create_net_background_cgroup(g_system->background_pids); - /* creating restriction rules tree */ __update_contr_cb(NULL); @@ -1260,10 +1226,6 @@ stc_error_e stc_monitor_deinit(void) g_system->contr_timer_id = 0; } - /* free background pid list */ - g_slist_free(g_system->background_pids); - g_system->background_pids = NULL; - /* destroy monitored application tree */ g_tree_destroy(g_system->apps); g_system->apps = NULL; @@ -1375,10 +1337,6 @@ stc_error_e stc_monitor_process_add(const stc_app_key_s app_key, /* add pid to application cgroup */ place_pids_to_net_cgroup(proc_key.pid, app_key.app_id); - /* add pid to background cgroup if gound state is background */ - if (proc_value.ground == STC_APP_STATE_BACKGROUND) - add_pid_to_background_cgroup(proc_key.pid); - return ret; } @@ -1397,9 +1355,6 @@ stc_error_e stc_monitor_process_remove(pid_t pid) ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!"); - /* remove pid from background cgroup */ - remove_pid_from_background_cgroup(pid); - g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid, &context); @@ -1434,10 +1389,15 @@ stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key, if (proc_lookup->ground != ground) proc_lookup->ground = ground; - if (ground == STC_APP_STATE_BACKGROUND) - add_pid_to_background_cgroup(proc_key.pid); - else + if (ground == STC_APP_STATE_BACKGROUND) { + char *background_app_id = g_strconcat(app_key.app_id, + STC_BACKGROUND_APP_SUFFIX, + NULL); + place_pids_to_net_cgroup(proc_key.pid, background_app_id); + g_free(background_app_id); + } else { place_pids_to_net_cgroup(proc_key.pid, app_key.app_id); + } return ret; } @@ -1500,10 +1460,8 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info) value.rst_state = info->rst_state; value.restriction_id = info->restriction_id; - if (value.rst_state != STC_RESTRICTION_EXCLUDED) - value.classid = get_classid_by_app_id(info->app_id ? - info->app_id : - STC_ALL_APP, TRUE); + if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id) + value.classid = get_classid_by_app_id(info->app_id, TRUE); else value.classid = STC_UNKNOWN_CLASSID; diff --git a/src/utils/net-cls-release.c b/src/utils/net-cls-release.c index 11f5040..7d41deb 100755 --- a/src/utils/net-cls-release.c +++ b/src/utils/net-cls-release.c @@ -2,10 +2,11 @@ #include #include -#define MAX_PATH_LENGTH 512 -#define DEFAULT_CGROUP "/sys/fs/cgroup" -#define CGROUP_NETWORK DEFAULT_CGROUP "/net_cls" -#define PATH_TO_NET_CGROUP_DIR CGROUP_NETWORK +#define MAX_PATH_LENGTH 512 +#define DEFAULT_CGROUP "/sys/fs/cgroup" +#define CGROUP_NETWORK DEFAULT_CGROUP "/net_cls" +#define STC_CGROUP_NETWORK CGROUP_NETWORK "/stc" +#define PATH_TO_NET_CGROUP_DIR STC_CGROUP_NETWORK int main(int argc, char *argv[]) { -- 2.7.4 From d7d0c6b61fc56bd65a385606c92d1b16e067f4f8 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Tue, 13 Jun 2017 14:38:06 +0530 Subject: [PATCH 03/16] Blocking incoming and outgoing traffic when specified limits are crosssed. Change-Id: I39e872adf9980b457a2b4427ce06c5798ed88f14 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/monitor/stc-monitor.c | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 4852a41..d62e979 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.13 +Version: 0.0.14 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 3015b51..a0a58ff 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -570,8 +570,10 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, /* block immediately */ if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) { + context->counter->intend = NFACCT_BLOCK; __del_iptables_in(context->counter); __add_iptables_in(context->counter); + context->counter->intend = NFACCT_COUNTER; rstn_value->in_limit_reached = TRUE; if (rstn_value->rstn_limit_crossed_notified == FALSE) { @@ -612,8 +614,10 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, /* block immediately */ if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) { + context->counter->intend = NFACCT_BLOCK; __del_iptables_out(context->counter); __add_iptables_out(context->counter); + context->counter->intend = NFACCT_COUNTER; rstn_value->out_limit_reached = TRUE; if (rstn_value->rstn_limit_crossed_notified == FALSE) { -- 2.7.4 From 0ea7a72fc87120370741c7b054b3ecfa3837c5d7 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Tue, 13 Jun 2017 14:42:11 +0530 Subject: [PATCH 04/16] Removed unwanted function send_net_restriction() and its related code. Change-Id: I68390c4d018778f6811fc1a9f2c0852b7aca1317 Signed-off-by: Nishant Chaprana --- {src/configure/include => include}/transmission.h | 0 src/configure/include/netlink-restriction.h | 53 --------- src/configure/nf-restriction.c | 133 ---------------------- src/monitor/include/stc-monitor.h | 1 - src/monitor/stc-monitor.c | 9 -- 5 files changed, 196 deletions(-) rename {src/configure/include => include}/transmission.h (100%) delete mode 100755 src/configure/include/netlink-restriction.h delete mode 100755 src/configure/nf-restriction.c diff --git a/src/configure/include/transmission.h b/include/transmission.h similarity index 100% rename from src/configure/include/transmission.h rename to include/transmission.h diff --git a/src/configure/include/netlink-restriction.h b/src/configure/include/netlink-restriction.h deleted file mode 100755 index 069a4a3..0000000 --- a/src/configure/include/netlink-restriction.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* - * @file net-restriction.h - * @desc Performance management API. Network restriction. - * @version 1.0 - * - * Created on: Jun 18, 2012 - */ - -#ifndef __STC_NET_RESTRICTION_H__ -#define __STC_NET_RESTRICTION_H__ - -#include - -#include "stc-manager.h" -#include "transmission.h" - -/** - * @brief Send network restriction for specific classid - * rst_type - type of restriction on the basis of which the restriction - * can be applied, removed or excluded. - * classid - id, that generated for each application in the cgroup - * quota_id - quota_id to store in nf_cntr tree - * iftype - network interface type to process restriction - * send_limit - amount number of engress bytes allowed for restriction - * rcv_limit - amount number of ingress bytes allowed for restriction - * snd_warning_limit - threshold for warning notification on engress bytes - * rcv_warning_limit - threshold for warning notification on ingress bytes - */ -int send_net_restriction(const enum traffic_restriction_type rst_type, - const guint32 classid, const int quota_id, - const stc_iface_type_e iftype, - const int64_t send_limit, const int64_t rcv_limit, - const int64_t snd_warning_threshold, - const int64_t rcv_warning_threshold, - const char *ifname); - -#endif /* __STC_NET_RESTRICTION_H__ */ diff --git a/src/configure/nf-restriction.c b/src/configure/nf-restriction.c deleted file mode 100755 index 2423ee0..0000000 --- a/src/configure/nf-restriction.c +++ /dev/null @@ -1,133 +0,0 @@ -/* - * @file nf-restriction.c - * - * @desc Implementation for set up/down restrictions. - * - * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved. - * - */ - -#include "stc-error.h" -#include "stc-manager.h" -#include "netlink-restriction.h" -#include "helper-nfacct-rule.h" -#include "helper-restriction.h" -#include "stc-default-connection.h" -#include "stc-monitor.h" -#include "counter.h" - -static stc_error_e apply_net_restriction(struct nfacct_rule *rule, - const int64_t send_limit, - const int64_t rcv_limit) -{ - nfacct_rule_jump jump; - - /* block immediately */ - if (!rcv_limit) { /* for dual nfacct entity for restriction add || !send_limit */ - return produce_net_rule(rule, 0, 0, NFACCT_ACTION_INSERT, - NFACCT_JUMP_REJECT, NFACCT_COUNTER_OUT); - } - - jump = ((rule->intend == NFACCT_WARN) ? - NFACCT_JUMP_ACCEPT : NFACCT_JUMP_REJECT); - - return produce_net_rule(rule, send_limit, rcv_limit, - NFACCT_ACTION_APPEND, jump, - NFACCT_COUNTER_IN | NFACCT_COUNTER_OUT); -} - -static stc_error_e revert_net_restriction(struct nfacct_rule *rule, - const int64_t send_limit, - const int64_t rcv_limit) -{ - nfacct_rule_jump jump = ((rule->intend == NFACCT_WARN) ? - NFACCT_JUMP_ACCEPT : NFACCT_JUMP_REJECT); - - return produce_net_rule(rule, send_limit, rcv_limit, - NFACCT_ACTION_DELETE, jump, - NFACCT_COUNTER_IN | NFACCT_COUNTER_OUT); - -} - -static stc_error_e exclude_net_restriction(struct nfacct_rule *rule) -{ - /* Idea to remove old counter and insert new one at first position - * iptables has following architecture: it gets all entries from kernel - * modifies this list and returns it back, without iptables it could be - * done for one step, but with iptables cmd 2 steps is necessary */ - rule->intend = NFACCT_COUNTER; - stc_error_e ret = produce_net_rule(rule, 0, 0, NFACCT_ACTION_DELETE, - NFACCT_JUMP_UNKNOWN, - NFACCT_COUNTER_IN | NFACCT_COUNTER_OUT); - - ret_value_msg_if(ret != STC_ERROR_NONE, ret, "Failed to delete"); - - return produce_net_rule(rule, 0, 0, - NFACCT_ACTION_INSERT, NFACCT_JUMP_UNKNOWN, - NFACCT_COUNTER_IN | NFACCT_COUNTER_OUT); -} - -stc_error_e send_net_restriction(const enum traffic_restriction_type rst_type, - const guint32 classid, const int quota_id, - const stc_iface_type_e iftype, - const int64_t send_limit, - const int64_t rcv_limit, - const int64_t snd_warning_threshold, - const int64_t rcv_warning_threshold, - const char *ifname) -{ - int ret; - stc_s *stc = stc_get_manager(); - struct counter_arg *carg; - struct nfacct_rule rule = { - .name = {0}, - .ifname = {0}, - .quota_id = quota_id, - }; - - rule.rst_state = convert_to_restriction_state(rst_type); - - ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data"); - if (!stc->carg) { - stc->carg = MALLOC0(counter_arg_s, 1); - stc->carg->sock = stc_monitor_get_counter_socket(); - } - - carg = stc->carg; - ret_value_msg_if(carg == NULL, STC_ERROR_FAIL, "Empty counter"); - - rule.classid = classid; - rule.iftype = iftype; - rule.carg = carg; - rule.roaming = stc_default_connection_get_roaming(); - STRING_SAVE_COPY(rule.ifname, ifname); - - if (rst_type == RST_SET) { - /* snd_warning_threshold && */ - if (rcv_warning_threshold) { - rule.intend = NFACCT_WARN; - ret = apply_net_restriction(&rule, - snd_warning_threshold, - rcv_warning_threshold); - ret_value_msg_if(ret != STC_ERROR_NONE, ret, - "Can't apply network restriction"); - } - rule.intend = NFACCT_BLOCK; - ret = apply_net_restriction(&rule, send_limit, rcv_limit); - ret_value_msg_if(ret != STC_ERROR_NONE, ret, - "Can't apply network restriction"); - } else if (rst_type == RST_UNSET) { - rule.intend = NFACCT_WARN; - ret = revert_net_restriction(&rule, - snd_warning_threshold, - rcv_warning_threshold); - ret_value_msg_if(ret != STC_ERROR_NONE, ret, - "Can't revert network restriction"); - rule.intend = NFACCT_BLOCK; - return revert_net_restriction(&rule, send_limit, - rcv_limit); - } else if (rst_type == RST_EXCLUDE) - return exclude_net_restriction(&rule); - - return STC_ERROR_NONE; -} diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index feb0f12..07b4715 100644 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -24,7 +24,6 @@ #include "stc-manager-util.h" #include "table-restrictions.h" #include "helper-nl.h" -#include "netlink-restriction.h" /* 1 seconds */ #define CONTR_TIMER_INTERVAL 1 diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index a0a58ff..1dac02f 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -413,15 +413,6 @@ static void __process_restriction(enum traffic_restriction_type rst_type, effective_limit.in_bytes, effective_limit.out_bytes, effective_warn_limit.in_bytes, effective_warn_limit.out_bytes); - send_net_restriction(rst_type, - rstn_value->classid, - 0 /*quota_id*/, - connection->type, - effective_limit.out_bytes, - effective_limit.in_bytes, - effective_warn_limit.out_bytes, - effective_warn_limit.in_bytes, - connection->ifname); switch (rst_type) { case RST_SET: rstn_value->rst_state = STC_RESTRICTION_ACTIVATED; -- 2.7.4 From 4f63fde461236b5559f50cdc6d79b51343eefa8d Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 31 May 2017 17:05:26 +0900 Subject: [PATCH 05/16] Added a plugin for net popup Change-Id: I8ebc30530c1f819832f144fb78528193b48007f6 Signed-off-by: hyunuktak --- CMakeLists.txt | 2 + include/stc-manager-plugin.h | 30 +++++++++++++ include/stc-plugin.h | 29 ++++++++++++ packaging/stc-manager.spec | 13 ++++++ plugin/CMakeLists.txt | 36 +++++++++++++++ plugin/stc-plugin.c | 105 +++++++++++++++++++++++++++++++++++++++++++ src/monitor/stc-monitor.c | 32 +++++++++++++ src/stc-manager-plugin.c | 97 +++++++++++++++++++++++++++++++++++++++ src/stc-manager.c | 3 ++ 9 files changed, 347 insertions(+) create mode 100755 include/stc-manager-plugin.h create mode 100755 include/stc-plugin.h create mode 100755 plugin/CMakeLists.txt create mode 100755 plugin/stc-plugin.c create mode 100755 src/stc-manager-plugin.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 95356ec..4dfaf15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,9 @@ PROJECT(stc-manager C) SET(PACKAGE ${PROJECT_NAME}) SET(INTERFACES "${CMAKE_SOURCE_DIR}/interfaces") SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(LIBDIR ${PREFIX}/${LIB_PATH}) SET(DATA_DIR ${CMAKE_SOURCE_DIR}/data) ADD_SUBDIRECTORY(src) +ADD_SUBDIRECTORY(plugin) diff --git a/include/stc-manager-plugin.h b/include/stc-manager-plugin.h new file mode 100755 index 0000000..6eae115 --- /dev/null +++ b/include/stc-manager-plugin.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __STC_MANAGER_PLUGIN_H__ +#define __STC_MANAGER_PLUGIN_H__ + +#define STC_PLUGIN_FILEPATH "/usr/lib/stc-manager-plugin.so" + +void stc_manager_plugin_init(void); +void stc_manager_plugin_deinit(void); + +int stc_send_warn_message_to_net_popup(const char *content, + const char *type, const char *app_id, const char *iftype, const char *warn); +int stc_send_restriction_message_to_net_popup(const char *content, + const char *type, const char *app_id, const char *iftype, const char *limit); + +#endif /* __STC_MANAGER_PLUGIN_H__ */ diff --git a/include/stc-plugin.h b/include/stc-plugin.h new file mode 100755 index 0000000..3bbcedb --- /dev/null +++ b/include/stc-plugin.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __STC_PLUGIN_H__ +#define __STC_PLUGIN_H__ + +#include + +typedef struct { + int(*send_restriction_message_to_net_popup) (const char *, + const char *, const char *, const char *, const char *); + int(*send_warn_message_to_net_popup) (const char *, + const char *, const char *, const char *, const char *); +} stc_plugin_s; + +#endif /* __STC_PLUGIN_H__ */ diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index d575b3b..83b2561 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -30,6 +30,14 @@ BuildRequires: python-xml %description A smart traffic control manager to manage traffic counting and bandwidth limitation +%package plugin +Summary: STC manager plugin +BuildRequires: pkgconfig(bundle) +BuildRequires: pkgconfig(syspopup-caller) + +%description plugin +A smart traffic control manager extension for plugin + %prep %setup -q chmod 644 %{SOURCE0} @@ -42,6 +50,7 @@ export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE" %cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \ -DBIN_DIR=%{_bindir} \ + -DLIB_PATH=%{_lib} \ -DENABLE_DATABASE=%{enable_database} \ -DDATABASE_FULL_PATH=%{database_full_path} \ -DENABLE_STATISTICS=%{enable_statistics} \ @@ -94,3 +103,7 @@ cp resources/dbus/stc-manager.conf %{buildroot}%{_sysconfdir}/dbus-1/system.d/st %config(noreplace) %attr(660, root, root) %{database_full_path} %config(noreplace) %attr(660, root, root) %{database_full_path}-journal %endif + +%files plugin +%manifest %{name}.manifest +%attr(500,root,root) %{_libdir}/stc-manager-plugin.so diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt new file mode 100755 index 0000000..66ab497 --- /dev/null +++ b/plugin/CMakeLists.txt @@ -0,0 +1,36 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(stc-manager-plugin C) + +# Set required packages +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(pkgs_plugin REQUIRED + dlog + gio-2.0 + gio-unix-2.0 + glib-2.0 + bundle + syspopup-caller + ) + +FOREACH(flag ${pkgs_plugin_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) +# INCLUDE_DIRECTORIES(SRCS include) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -g -Werror -fvisibility=hidden") +SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") +SET(CMAKE_C_FLAGS_RELEASE "-O2") + +SET(SRCS_PLUGIN + stc-plugin.c + ) + +# library build +ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS_PLUGIN}) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_plugin_LDFLAGS}) +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "" OUTPUT_NAME ${PROJECT_NAME}) + +# install +INSTALL(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${LIBDIR}) diff --git a/plugin/stc-plugin.c b/plugin/stc-plugin.c new file mode 100755 index 0000000..13f0306 --- /dev/null +++ b/plugin/stc-plugin.c @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "stc-plugin.h" + +#ifdef USE_DLOG +#include + +#undef LOG_TAG +#define LOG_TAG "STC_MANAGER" + +#define STC_LOGD(format, args...) LOGD(format, ##args) +#define STC_LOGI(format, args...) LOGI(format, ##args) +#define STC_LOGW(format, args...) LOGW(format, ##args) +#define STC_LOGE(format, args...) LOGE(format, ##args) + +#define __STC_LOG_FUNC_ENTER__ LOGD("Enter") +#define __STC_LOG_FUNC_EXIT__ LOGD("Quit") + +#else /* USE_DLOG */ + +#define STC_LOGD(format, args...) +#define STC_LOGI(format, args...) +#define STC_LOGW(format, args...) +#define STC_LOGE(format, args...) + +#define __STC_LOG_FUNC_ENTER__ +#define __STC_LOG_FUNC_EXIT__ + +#endif /* USE_DLOG */ + +static int __stc_send_warn_message_to_net_popup(const char *content, + const char *type, const char *app_id, const char *iftype, const char *warn) +{ + int ret = 0; + bundle *b = bundle_create(); + + STC_LOGD("Warn message : content[%s] type[%s] app_id[%s] limit[%s]", + title, content, type, app_id, limit); + + bundle_add(b, "_SYSPOPUP_CONTENT_", content); + bundle_add(b, "_SYSPOPUP_TYPE_", type); + bundle_add(b, "_APP_ID_", app_id); + bundle_add(b, "_IF_TYPE_", iftype); + bundle_add(b, "_WARN_LIMIT_", warn); + + ret = syspopup_launch("net-popup", b); + + bundle_free(b); + + return ret; +} + +static int __stc_send_restriction_message_to_net_popup(const char *content, + const char *type, const char *app_id, const char *iftype, const char *limit) +{ + int ret = 0; + bundle *b = bundle_create(); + + STC_LOGD("Restriction message : content[%s] type[%s] app_id[%s] limit[%s]", + title, content, type, app_id); + + bundle_add(b, "_SYSPOPUP_CONTENT_", content); + bundle_add(b, "_SYSPOPUP_TYPE_", type); + bundle_add(b, "_APP_ID_", app_id); + bundle_add(b, "_IF_TYPE_", iftype); + bundle_add(b, "_RESTRICTION_LIMIT_", limit); + + ret = syspopup_launch("net-popup", b); + + bundle_free(b); + + return ret; +} + + +API stc_plugin_s stc_plugin = { + .send_warn_message_to_net_popup = + __stc_send_warn_message_to_net_popup, + .send_restriction_message_to_net_popup = + __stc_send_restriction_message_to_net_popup +}; + diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index b12920e..b4f43b3 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -24,6 +24,9 @@ #include "table-statistics.h" #include "table-counters.h" #include "stc-monitor.h" +#include "stc-manager-plugin.h" + +#define MAX_INT_LENGTH 128 typedef struct { stc_app_key_s *app_key; @@ -553,6 +556,8 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, if (rstn_value->counter.in_bytes >= rstn_value->warn_limit.in_bytes && rstn_value->warn_limit_crossed_notified == FALSE) { gboolean rv = FALSE; + char iftype[MAX_INT_LENGTH]; + char byte[MAX_INT_LENGTH]; stc_s *stc = (stc_s *)stc_get_manager(); ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data"); @@ -563,6 +568,11 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, g_variant_new("(s)", rstn_key->app_id)); if (rv == TRUE) rstn_value->warn_limit_crossed_notified = TRUE; + + snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); + snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes); + stc_send_warn_message_to_net_popup("warn threshold crossed", + "warning_noti", rstn_key->app_id, iftype, byte); } /* block immediately */ @@ -573,6 +583,9 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, if (rstn_value->rstn_limit_crossed_notified == FALSE) { gboolean rv = FALSE; + int ret = 0; + char iftype[MAX_INT_LENGTH]; + char byte[MAX_INT_LENGTH]; stc_s *stc = (stc_s *)stc_get_manager(); ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data"); @@ -583,6 +596,11 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, g_variant_new("(s)", rstn_key->app_id)); if (rv == TRUE) rstn_value->rstn_limit_crossed_notified = TRUE; + + snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); + snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes); + stc_send_restriction_message_to_net_popup("restriction threshold crossed", + "restriction_noti", rstn_key->app_id, iftype, byte); } } @@ -595,6 +613,8 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes && rstn_value->warn_limit_crossed_notified == FALSE) { gboolean rv = FALSE; + char iftype[MAX_INT_LENGTH]; + char byte[MAX_INT_LENGTH]; stc_s *stc = (stc_s *)stc_get_manager(); ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data"); @@ -605,6 +625,11 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, g_variant_new("(s)", rstn_key->app_id)); if (rv == TRUE) rstn_value->warn_limit_crossed_notified = TRUE; + + snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); + snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.out_bytes); + stc_send_warn_message_to_net_popup("warn threshold crossed", + "warning_noti", rstn_key->app_id, iftype, byte); } /* block immediately */ @@ -615,6 +640,8 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, if (rstn_value->rstn_limit_crossed_notified == FALSE) { gboolean rv = FALSE; + char iftype[MAX_INT_LENGTH]; + char byte[MAX_INT_LENGTH]; stc_s *stc = (stc_s *)stc_get_manager(); ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data"); @@ -625,6 +652,11 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, g_variant_new("(s)", rstn_key->app_id)); if (rv == TRUE) rstn_value->rstn_limit_crossed_notified = TRUE; + + snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); + snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.out_bytes); + stc_send_restriction_message_to_net_popup("restriction threshold crossed", + "restriction_noti", rstn_key->app_id, iftype, byte); } } diff --git a/src/stc-manager-plugin.c b/src/stc-manager-plugin.c new file mode 100755 index 0000000..d25b1a8 --- /dev/null +++ b/src/stc-manager-plugin.c @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "stc-manager.h" +#include "stc-plugin.h" +#include "stc-manager-plugin.h" + +static gboolean stc_plugin_enabled = FALSE; +static void *handle_plugin; +static stc_plugin_s *stc_plugin; + +void stc_manager_plugin_init(void) +{ + __STC_LOG_FUNC_ENTER__; + + handle_plugin = dlopen(STC_PLUGIN_FILEPATH, RTLD_NOW); + if (!handle_plugin) { + STC_LOGE("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror()); + return; + } + + stc_plugin = dlsym(handle_plugin, "stc_plugin"); + if (!stc_plugin) { + STC_LOGE("Can't load symbol: %s", dlerror()); + dlclose(handle_plugin); + return; + } + + stc_plugin_enabled = TRUE; + + __STC_LOG_FUNC_EXIT__; +} + +void stc_manager_plugin_deinit(void) +{ + __STC_LOG_FUNC_ENTER__; + + if (!stc_plugin_enabled) + return; + + stc_plugin_enabled = FALSE; + dlclose(handle_plugin); + + __STC_LOG_FUNC_EXIT__; +} + +int stc_send_warn_message_to_net_popup(const char *content, + const char *type, const char *app_id, const char *iftype, const char *warn) +{ + __STC_LOG_FUNC_ENTER__; + + if (!stc_plugin_enabled) { + STC_LOGE("Plugin wasn't enabled"); + return 0; + } + + if (!stc_plugin) { + STC_LOGE("Plugin wasn't loaded"); + return 0; + } + + return stc_plugin->send_warn_message_to_net_popup(content, type, app_id, iftype, warn); +} + +int stc_send_restriction_message_to_net_popup(const char *content, + const char *type, const char *app_id, const char *iftype, const char *limit) +{ + __STC_LOG_FUNC_ENTER__; + + if (!stc_plugin_enabled) { + STC_LOGE("Plugin wasn't enabled"); + return 0; + } + + if (!stc_plugin) { + STC_LOGE("Plugin wasn't loaded"); + return 0; + } + + return stc_plugin->send_restriction_message_to_net_popup(content, type, app_id, iftype, limit); +} + diff --git a/src/stc-manager.c b/src/stc-manager.c index bbabfa9..69349ae 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -24,6 +24,7 @@ #include "helper-cgroup.h" #include "helper-nfacct-rule.h" #include "stc-monitor.h" +#include "stc-manager-plugin.h" static stc_s *g_stc = NULL; @@ -40,6 +41,7 @@ static void __stc_manager_deinit(void) stc_deinit_db_guard(); stc_db_deinitialize(); stc_manager_gdbus_deinit((gpointer)g_stc); + stc_manager_plugin_deinit(); STC_LOGI("stc manager deinitialized"); FREE(g_stc); @@ -64,6 +66,7 @@ static stc_s *__stc_manager_init(void) stc_monitor_init(); stc_manager_gdbus_init((gpointer)stc); + stc_manager_plugin_init(); STC_LOGI("stc manager initialized"); __STC_LOG_FUNC_EXIT__; -- 2.7.4 From 125ff4a6c69597383ed28f697c859fdd6c4a5ab4 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 15 Jun 2017 09:40:04 +0530 Subject: [PATCH 06/16] [STC Manager] Support set/get for total data usage(Tx + Rx) in restriction. Description: This patch removes seperate data usage limits for Tx and Rx data and uses total data limit for setting Tx and Rx limits. Change-Id: I6882e936c9cb076dda548ef95a10e105071df767 Signed-off-by: Nishant Chaprana --- data/traffic_db.sql | 9 +- packaging/stc-manager.spec | 2 +- src/database/include/stc-db.h | 6 +- src/database/include/table-counters.h | 3 +- src/database/include/table-restrictions.h | 6 +- src/database/tables/table-counters.c | 25 ++-- src/database/tables/table-restrictions.c | 109 +++++++-------- src/monitor/include/stc-monitor.h | 9 +- src/monitor/stc-monitor.c | 215 +++++++++--------------------- src/stc-restriction.c | 40 ++---- 10 files changed, 153 insertions(+), 271 deletions(-) diff --git a/data/traffic_db.sql b/data/traffic_db.sql index 045586d..fda62f8 100644 --- a/data/traffic_db.sql +++ b/data/traffic_db.sql @@ -23,18 +23,15 @@ CREATE TABLE IF NOT EXISTS restrictions ( rst_state INT, roaming INT, imsi TEXT NOT NULL, - rcv_limit BIGINT, - send_limit BIGINT, - rcv_warn_limit BIGINT, - send_warn_limit BIGINT + data_limit BIGINT, + data_warn_limit BIGINT ); CREATE INDEX IF NOT EXISTS restrictions_index ON restrictions (binpath, iftype, ifname); CREATE TABLE IF NOT EXISTS counters ( restriction_id INTEGER NOT NULL, - sent_bytes BIGINT, - rcv_bytes BIGINT, + data_counter BIGINT, PRIMARY KEY (restriction_id) ); diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 6a00c37..3afe575 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.14 +Version: 0.0.15 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/database/include/stc-db.h b/src/database/include/stc-db.h index a240994..6f718c3 100755 --- a/src/database/include/stc-db.h +++ b/src/database/include/stc-db.h @@ -57,10 +57,8 @@ typedef enum { */ typedef struct { char *app_id; - unsigned int snd_count; - unsigned int rcv_count; - unsigned int delta_snd; - unsigned int delta_rcv; + int64_t rcv_count; + int64_t snd_count; #ifndef CONFIG_DATAUSAGE_NFACCT pid_t pid; diff --git a/src/database/include/table-counters.h b/src/database/include/table-counters.h index 70e545c..a0ef60d 100755 --- a/src/database/include/table-counters.h +++ b/src/database/include/table-counters.h @@ -19,8 +19,7 @@ typedef struct { uint64_t restriction_id; - int64_t sent_bytes; - int64_t rcv_bytes; + int64_t data_counter; } table_counters_info; typedef stc_cb_ret_e(*table_counters_info_cb)(const table_counters_info *info, diff --git a/src/database/include/table-restrictions.h b/src/database/include/table-restrictions.h index 2814199..fe275e1 100755 --- a/src/database/include/table-restrictions.h +++ b/src/database/include/table-restrictions.h @@ -24,10 +24,8 @@ typedef struct { stc_iface_type_e iftype; stc_restriction_state_e rst_state; stc_roaming_type_e roaming; - int64_t rcv_limit; - int64_t send_limit; - int64_t rcv_warn_limit; - int64_t send_warn_limit; + int64_t data_limit; + int64_t data_warn_limit; uint64_t restriction_id; } table_restrictions_info; diff --git a/src/database/tables/table-counters.c b/src/database/tables/table-counters.c index 9fb6fba..f8946f7 100755 --- a/src/database/tables/table-counters.c +++ b/src/database/tables/table-counters.c @@ -26,13 +26,13 @@ #define DELETE_COUNTER "DELETE FROM counters WHERE restriction_id=?" -#define SELECT_COUNTER "SELECT sent_bytes, rcv_bytes FROM counters WHERE restriction_id=?" +#define SELECT_COUNTER "SELECT data_counter FROM counters WHERE restriction_id=?" -#define INSERT_COUNTER "INSERT INTO counters (restriction_id, sent_bytes, rcv_bytes) " \ - " VALUES (?, ?, ?)" +#define INSERT_COUNTER "INSERT INTO counters (restriction_id, data_counter) " \ + " VALUES (?, ?)" -#define UPDATE_COUNTER "REPLACE INTO counters (restriction_id, sent_bytes, rcv_bytes) " \ - " VALUES (?, ?, ?)" +#define UPDATE_COUNTER "REPLACE INTO counters (restriction_id, data_counter) " \ + " VALUES (?, ?)" static void __finalize_delete(void); @@ -175,6 +175,8 @@ stc_error_e table_counters_get(uint64_t restriction_id, if (info == NULL) goto handle_error; + info->restriction_id = restriction_id; + DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id)); do { @@ -184,12 +186,10 @@ stc_error_e table_counters_get(uint64_t restriction_id, case SQLITE_DONE: break; case SQLITE_ROW: - info->sent_bytes = sqlite3_column_int64(stmt, 0); - info->rcv_bytes = sqlite3_column_int64(stmt, 1); + info->data_counter = sqlite3_column_int64(stmt, 0); - STC_LOGD("rstn_id [%llu] counters [sent = %lld rcv = %lld]", - restriction_id, info->sent_bytes, - info->rcv_bytes); + STC_LOGD("rstn_id [%llu] data_counters [%lld] bytes", + restriction_id, info->data_counter); break; case SQLITE_ERROR: default: @@ -210,14 +210,13 @@ stc_error_e table_counters_update_counters(const table_counters_info *info) stc_error_e error_code = STC_ERROR_NONE; sqlite3_stmt *stmt = update_counter; - if (!info->rcv_bytes && !info->sent_bytes) { + if (!info->data_counter) { error_code = STC_ERROR_INVALID_PARAMETER; goto handle_error; } DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id)); - DB_ACTION(sqlite3_bind_int64(stmt, 2, info->sent_bytes)); - DB_ACTION(sqlite3_bind_int64(stmt, 3, info->rcv_bytes)); + DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_counter)); if (sqlite3_step(stmt) != SQLITE_DONE) { STC_LOGE("Failed to update counter: %s\n", diff --git a/src/database/tables/table-restrictions.c b/src/database/tables/table-restrictions.c index aec6f08..a5cb108 100755 --- a/src/database/tables/table-restrictions.c +++ b/src/database/tables/table-restrictions.c @@ -26,43 +26,43 @@ /* DELETE statements */ #define DELETE_RESTRICTIONS "DELETE FROM restrictions " \ - "WHERE binpath=? AND iftype=? AND imsi = ?" + " WHERE binpath = ? AND iftype = ? AND imsi = ?" /* SELECT statements */ -#define SELECT_RESTRICTIONS "SELECT binpath, rcv_limit, " \ - "send_limit, iftype, rst_state, roaming, ifname, imsi, " \ - "rcv_warn_limit, send_warn_limit, restriction_id FROM restrictions" +#define SELECT_RESTRICTIONS "SELECT binpath, data_limit, " \ + " iftype, rst_state, roaming, ifname, imsi, " \ + " data_warn_limit, restriction_id FROM restrictions" -#define SELECT_RESTRICTIONS_PER_APP "SELECT binpath, rcv_limit, " \ - "send_limit, iftype, rst_state, roaming, ifname, imsi, " \ - "rcv_warn_limit, send_warn_limit, restriction_id " \ - "FROM restrictions INDEXED BY restrictions_index " \ - "WHERE binpath = ?" +#define SELECT_RESTRICTIONS_PER_APP "SELECT binpath, data_limit, " \ + " iftype, rst_state, roaming, ifname, imsi, " \ + " data_warn_limit, restriction_id " \ + " FROM restrictions INDEXED BY restrictions_index " \ + " WHERE binpath = ?" #define SELECT_RESTRICTION_STATE "SELECT rst_state " \ - "FROM restrictions INDEXED BY restrictions_index " \ - "WHERE binpath = ? AND iftype = ?" + " FROM restrictions INDEXED BY restrictions_index " \ + " WHERE binpath = ? AND iftype = ?" #define SELECT_RESTRICTION_STATE_IMSI "SELECT rst_state " \ - "FROM restrictions INDEXED BY restrictions_index " \ - "WHERE binpath = ? AND iftype = ? AND imsi = ?" + " FROM restrictions INDEXED BY restrictions_index " \ + " WHERE binpath = ? AND iftype = ? AND imsi = ?" #define SELECT_RESTRICTION_ID "SELECT restriction_id FROM restrictions " \ - "WHERE binpath = ? AND iftype = ? AND imsi = ? AND send_limit=? " \ - "AND rcv_limit=? AND rcv_warn_limit=? AND send_warn_limit=? " \ - "AND rst_state=? AND roaming=? AND ifname=?" + " WHERE binpath = ? AND iftype = ? AND imsi = ? AND " \ + " data_limit = ? AND data_warn_limit = ? AND " \ + " rst_state = ? AND roaming = ? AND ifname = ?" /* UPDATE statement */ #define UPDATE_NET_RESTRICTIONS "UPDATE restrictions " \ - "SET binpath=?, rcv_limit=?, send_limit=?, iftype=?, rst_state=?, " \ - "roaming=?, ifname=?, imsi=?, rcv_warn_limit=?, send_warn_limit=? " \ - "WHERE restriction_id=?" + " SET binpath = ?, data_limit = ?, iftype = ?, rst_state = ?, " \ + " roaming = ?, ifname = ?, imsi = ?, data_warn_limit = ? " \ + " WHERE restriction_id = ?" /* INSERT statement */ #define INSERT_NET_RESTRICTIONS "INSERT INTO restrictions " \ - "(binpath, rcv_limit, send_limit, iftype, rst_state, " \ - "roaming, ifname, imsi, rcv_warn_limit, send_warn_limit) " \ - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" + " (binpath, data_limit, iftype, rst_state, " \ + " roaming, ifname, imsi, data_warn_limit) " \ + " VALUES (?, ?, ?, ?, ?, ?, ?, ?)" static void __finalize_delete(void); @@ -284,17 +284,15 @@ stc_error_e table_restrictions_per_app(const gchar* app_id, break; case SQLITE_ROW: data.app_id = (char *)sqlite3_column_text(stmt, 0); - data.rcv_limit = sqlite3_column_int64(stmt, 1); - data.send_limit = sqlite3_column_int64(stmt, 2); - data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 3); + data.data_limit = sqlite3_column_int64(stmt, 1); + data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2); data.rst_state = - (stc_restriction_state_e)sqlite3_column_int(stmt, 4); - data.roaming = sqlite3_column_int(stmt, 5); - data.ifname = (char *)sqlite3_column_text(stmt, 6); - data.imsi = (char *)sqlite3_column_text(stmt, 7); - data.rcv_warn_limit = sqlite3_column_int64(stmt, 8); - data.send_warn_limit = sqlite3_column_int64(stmt, 9); - data.restriction_id = sqlite3_column_int64(stmt, 10); + (stc_restriction_state_e)sqlite3_column_int(stmt, 3); + data.roaming = sqlite3_column_int(stmt, 4); + data.ifname = (char *)sqlite3_column_text(stmt, 5); + data.imsi = (char *)sqlite3_column_text(stmt, 6); + data.data_warn_limit = sqlite3_column_int64(stmt, 7); + data.restriction_id = sqlite3_column_int64(stmt, 8); if (restriction_cb(&data, user_data) == STC_CANCEL) rc = SQLITE_DONE; @@ -334,17 +332,15 @@ stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restrict break; case SQLITE_ROW: data.app_id = (char *)sqlite3_column_text(stmt, 0); - data.rcv_limit = sqlite3_column_int64(stmt, 1); - data.send_limit = sqlite3_column_int64(stmt, 2); - data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 3); + data.data_limit = sqlite3_column_int64(stmt, 1); + data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2); data.rst_state = - (stc_restriction_state_e)sqlite3_column_int(stmt, 4); - data.roaming = sqlite3_column_int(stmt, 5); - data.ifname = (char *)sqlite3_column_text(stmt, 6); - data.imsi = (char *)sqlite3_column_text(stmt, 7); - data.rcv_warn_limit = sqlite3_column_int64(stmt, 8); - data.send_warn_limit = sqlite3_column_int64(stmt, 9); - data.restriction_id = sqlite3_column_int64(stmt, 10); + (stc_restriction_state_e)sqlite3_column_int(stmt, 3); + data.roaming = sqlite3_column_int(stmt, 4); + data.ifname = (char *)sqlite3_column_text(stmt, 5); + data.imsi = (char *)sqlite3_column_text(stmt, 6); + data.data_warn_limit = sqlite3_column_int64(stmt, 7); + data.restriction_id = sqlite3_column_int64(stmt, 8); if (restriction_cb(&data, user_data) == STC_CANCEL) rc = SQLITE_DONE; @@ -480,14 +476,11 @@ stc_error_e __get_restriction_id(table_restrictions_info *info) DB_ACTION(sqlite3_bind_int(stmt, 2, info->iftype)); DB_ACTION(sqlite3_bind_text(stmt, 3, info->imsi ? info->imsi : "", -1, SQLITE_TRANSIENT)); - DB_ACTION(sqlite3_bind_int64(stmt, 4, info->send_limit)); - DB_ACTION(sqlite3_bind_int64(stmt, 5, info->rcv_limit)); - DB_ACTION(sqlite3_bind_int64(stmt, 6, info->rcv_warn_limit)); - DB_ACTION(sqlite3_bind_int64(stmt, 7, info->send_warn_limit)); - - DB_ACTION(sqlite3_bind_int(stmt, 8, info->rst_state)); - DB_ACTION(sqlite3_bind_int(stmt, 9, info->roaming)); - DB_ACTION(sqlite3_bind_text(stmt, 10, info->ifname ? info->ifname : "", + DB_ACTION(sqlite3_bind_int64(stmt, 4, info->data_limit)); + DB_ACTION(sqlite3_bind_int64(stmt, 5, info->data_warn_limit)); + DB_ACTION(sqlite3_bind_int(stmt, 6, info->rst_state)); + DB_ACTION(sqlite3_bind_int(stmt, 7, info->roaming)); + DB_ACTION(sqlite3_bind_text(stmt, 8, info->ifname ? info->ifname : "", -1, SQLITE_TRANSIENT)); rc = sqlite3_step(stmt); @@ -526,17 +519,15 @@ stc_error_e table_restrictions_update(table_restrictions_info *info) DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "", -1, SQLITE_TRANSIENT)); - DB_ACTION(sqlite3_bind_int64(stmt, 2, info->rcv_limit)); - DB_ACTION(sqlite3_bind_int64(stmt, 3, info->send_limit)); - DB_ACTION(sqlite3_bind_int(stmt, 4, info->iftype)); - DB_ACTION(sqlite3_bind_int(stmt, 5, info->rst_state)); - DB_ACTION(sqlite3_bind_int(stmt, 6, info->roaming)); - DB_ACTION(sqlite3_bind_text(stmt, 7, info->ifname ? info->ifname : "", + DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_limit)); + DB_ACTION(sqlite3_bind_int(stmt, 3, info->iftype)); + DB_ACTION(sqlite3_bind_int(stmt, 4, info->rst_state)); + DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming)); + DB_ACTION(sqlite3_bind_text(stmt, 6, info->ifname ? info->ifname : "", -1, SQLITE_TRANSIENT)); - DB_ACTION(sqlite3_bind_text(stmt, 8, info->imsi ? info->imsi : "", + DB_ACTION(sqlite3_bind_text(stmt, 7, info->imsi ? info->imsi : "", -1, SQLITE_TRANSIENT)); - DB_ACTION(sqlite3_bind_int64(stmt, 9, info->rcv_warn_limit)); - DB_ACTION(sqlite3_bind_int64(stmt, 10, info->send_warn_limit)); + DB_ACTION(sqlite3_bind_int64(stmt, 8, info->data_warn_limit)); if (info->restriction_id) DB_ACTION(sqlite3_bind_int64(stmt, 11, info->restriction_id)); diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index 07b4715..09871d5 100644 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -80,11 +80,10 @@ typedef struct { uint64_t restriction_id; uint32_t classid; stc_restriction_state_e rst_state; - stc_data_counter_s limit; - stc_data_counter_s warn_limit; - stc_data_counter_s counter; - gboolean in_limit_reached; - gboolean out_limit_reached; + int64_t data_limit; + int64_t data_warn_limit; + int64_t data_counter; + gboolean data_limit_reached; gboolean warn_limit_crossed_notified; gboolean rstn_limit_crossed_notified; } stc_rstn_value_s; diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 2674ae5..896e437 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -38,8 +38,7 @@ typedef struct { typedef struct { struct nfacct_rule *counter; int64_t bytes; - gboolean in_limit_reached; - gboolean out_limit_reached; + gboolean data_limit_reached; } classid_bytes_context_s; static stc_system_s *g_system = NULL; @@ -333,17 +332,16 @@ static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value) STC_LOGI("rstn info => rstn_id [%llu], " "app_id [%s], classid [%lu], ifname [%s], " "iftype [%d], rst_state [%d], " - "limit [ in (%lld), out (%lld)], " - "warn_limit [ in (%lld), out (%lld)], " - "counter [ in (%lld), out (%lld)], " + "limit [ (%lld) bytes], " + "warn_limit [ (%lld) bytes], " + "counter [ (%lld) bytes], " "roaming [%d], imsi [%s]", rstn_value->restriction_id, rstn_key->app_id, rstn_value->classid , rstn_key->ifname, rstn_key->iftype, rstn_value->rst_state, - rstn_value->limit.in_bytes, rstn_value->limit.out_bytes, - rstn_value->warn_limit.in_bytes, - rstn_value->warn_limit.out_bytes, - rstn_value->counter.in_bytes, rstn_value->counter.out_bytes, + rstn_value->data_limit, + rstn_value->data_warn_limit, + rstn_value->data_counter, rstn_key->roaming, rstn_key->imsi); } @@ -351,7 +349,7 @@ static void __process_restriction(enum traffic_restriction_type rst_type, stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value, void *data) { - stc_data_counter_s effective_limit, effective_warn_limit; + int64_t effective_data_limit, effective_data_warn_limit; default_connection_s *old_connection = (default_connection_s *)data; default_connection_s *connection = NULL; @@ -373,10 +371,8 @@ static void __process_restriction(enum traffic_restriction_type rst_type, if (rstn_value->classid == STC_UNKNOWN_CLASSID) return; - effective_limit.out_bytes = rstn_value->limit.out_bytes; - effective_limit.in_bytes = rstn_value->limit.in_bytes; - effective_warn_limit.out_bytes = rstn_value->warn_limit.out_bytes; - effective_warn_limit.in_bytes = rstn_value->warn_limit.in_bytes; + effective_data_limit = rstn_value->data_limit; + effective_data_warn_limit = rstn_value->data_warn_limit; if (rst_type == RST_SET) { /* TODO: Change this to runtime memory */ @@ -385,50 +381,36 @@ static void __process_restriction(enum traffic_restriction_type rst_type, memset(&info, 0, sizeof(table_counters_info)); table_counters_get(rstn_value->restriction_id, &info); - effective_limit.out_bytes -= info.sent_bytes; - effective_limit.in_bytes -= info.rcv_bytes; - effective_warn_limit.out_bytes -= info.sent_bytes; - effective_warn_limit.in_bytes -= info.rcv_bytes; + effective_data_limit -= info.data_counter; + effective_data_warn_limit -= info.data_counter; - if (effective_limit.in_bytes < 0) { - effective_limit.in_bytes = 0; - rstn_value->in_limit_reached = TRUE; + if (effective_data_limit < 0) { + effective_data_limit = 0; + rstn_value->data_limit_reached = TRUE; } - if (effective_limit.out_bytes < 0) { - effective_limit.out_bytes = 0; - rstn_value->out_limit_reached = TRUE; - } - - if (effective_warn_limit.in_bytes < 0) - effective_warn_limit.in_bytes = 0; + if (effective_data_warn_limit < 0) + effective_data_warn_limit = 0; - if (effective_warn_limit.out_bytes < 0) - effective_warn_limit.out_bytes = 0; - STC_LOGD("datausage [in: %lld, out: %lld]", - info.rcv_bytes, info.sent_bytes); + STC_LOGD("datausage [%lld] bytes", info.data_counter); } - STC_LOGD("rstn_id [%llu], effective limit [in: %lld, out: %lld], " - "effective warn limit [in: %lld, out: %lld], " - "datausage [in: %lld, out: %lld]", - rstn_value->restriction_id, - effective_limit.in_bytes, effective_limit.out_bytes, - effective_warn_limit.in_bytes, effective_warn_limit.out_bytes); + STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, " + "effective_data_warn_limit [%lld] bytes", + rstn_value->restriction_id, effective_data_limit, + effective_data_warn_limit); switch (rst_type) { case RST_SET: rstn_value->rst_state = STC_RESTRICTION_ACTIVATED; - rstn_value->in_limit_reached = FALSE; - rstn_value->out_limit_reached = FALSE; + rstn_value->data_limit_reached = FALSE; break; case RST_EXCLUDE: ;//Do Nothing break; case RST_UNSET: rstn_value->rst_state = STC_RESTRICTION_REMOVED; - rstn_value->in_limit_reached = FALSE; - rstn_value->out_limit_reached = FALSE; + rstn_value->data_limit_reached = FALSE; break; default: ;//Do Nothing @@ -533,28 +515,26 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, if (rstn_value->classid != context->counter->classid) goto try_next_callback; - if (rstn_value->in_limit_reached == TRUE) { - context->in_limit_reached = TRUE; - goto try_next_callback; - } - - if (rstn_value->out_limit_reached == TRUE) { - context->out_limit_reached = TRUE; + if (rstn_value->data_limit_reached == TRUE) { + context->data_limit_reached = TRUE; goto try_next_callback; } switch (context->counter->iotype) { case NFACCT_COUNTER_IN: - rstn_value->counter.in_bytes += context->bytes; + case NFACCT_COUNTER_OUT: + rstn_value->data_counter += context->bytes; + + if (rstn_value->data_counter >= rstn_value->data_warn_limit && + rstn_value->warn_limit_crossed_notified == FALSE) { - if (rstn_value->counter.in_bytes >= rstn_value->warn_limit.in_bytes - && rstn_value->warn_limit_crossed_notified == FALSE) { gboolean rv = FALSE; char iftype[MAX_INT_LENGTH]; char byte[MAX_INT_LENGTH]; stc_s *stc = (stc_s *)stc_get_manager(); ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data"); + /* emit signal */ rv = stc_manager_dbus_emit_signal(stc->connection, STC_DBUS_SERVICE_RESTRICTION_PATH, STC_DBUS_INTERFACE_RESTRICTION, @@ -564,98 +544,46 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, rstn_value->warn_limit_crossed_notified = TRUE; snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); - snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes); + snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit); stc_send_warn_message_to_net_popup("warn threshold crossed", - "warning_noti", rstn_key->app_id, iftype, byte); - } - - /* block immediately */ - if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) { - context->counter->intend = NFACCT_BLOCK; - __del_iptables_in(context->counter); - __add_iptables_in(context->counter); - context->counter->intend = NFACCT_COUNTER; - rstn_value->in_limit_reached = TRUE; - - if (rstn_value->rstn_limit_crossed_notified == FALSE) { - gboolean rv = FALSE; - int ret = 0; - char iftype[MAX_INT_LENGTH]; - char byte[MAX_INT_LENGTH]; - stc_s *stc = (stc_s *)stc_get_manager(); - ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data"); - - rv = stc_manager_dbus_emit_signal(stc->connection, - STC_DBUS_SERVICE_RESTRICTION_PATH, - STC_DBUS_INTERFACE_RESTRICTION, - "RestrictionThresholdCrossed", - g_variant_new("(s)", rstn_key->app_id)); - if (rv == TRUE) - rstn_value->rstn_limit_crossed_notified = TRUE; - - snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); - snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes); - stc_send_restriction_message_to_net_popup("restriction threshold crossed", - "restriction_noti", rstn_key->app_id, iftype, byte); - } + "warning_noti", + rstn_key->app_id, + iftype, byte); } - g_system->rstns_tree_updated = TRUE; - __print_rstn(rstn_key, rstn_value); - break; - case NFACCT_COUNTER_OUT: - rstn_value->counter.out_bytes += context->bytes; + if (rstn_value->data_counter >= rstn_value->data_limit && + rstn_value->rstn_limit_crossed_notified == FALSE) { - if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes - && rstn_value->warn_limit_crossed_notified == FALSE) { gboolean rv = FALSE; char iftype[MAX_INT_LENGTH]; char byte[MAX_INT_LENGTH]; stc_s *stc = (stc_s *)stc_get_manager(); ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data"); + /* block immediately */ + context->counter->intend = NFACCT_BLOCK; + __del_iptables_in(context->counter); + __del_iptables_out(context->counter); + __add_iptables_in(context->counter); + __add_iptables_out(context->counter); + context->counter->intend = NFACCT_COUNTER; + + rstn_value->data_limit_reached = TRUE; + + /* emit signal */ rv = stc_manager_dbus_emit_signal(stc->connection, STC_DBUS_SERVICE_RESTRICTION_PATH, STC_DBUS_INTERFACE_RESTRICTION, - "WarnThresholdCrossed", + "RestrictionThresholdCrossed", g_variant_new("(s)", rstn_key->app_id)); if (rv == TRUE) - rstn_value->warn_limit_crossed_notified = TRUE; + rstn_value->rstn_limit_crossed_notified = TRUE; snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); - snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.out_bytes); - stc_send_warn_message_to_net_popup("warn threshold crossed", - "warning_noti", rstn_key->app_id, iftype, byte); - } - - /* block immediately */ - if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) { - context->counter->intend = NFACCT_BLOCK; - __del_iptables_out(context->counter); - __add_iptables_out(context->counter); - context->counter->intend = NFACCT_COUNTER; - rstn_value->out_limit_reached = TRUE; - - if (rstn_value->rstn_limit_crossed_notified == FALSE) { - gboolean rv = FALSE; - char iftype[MAX_INT_LENGTH]; - char byte[MAX_INT_LENGTH]; - stc_s *stc = (stc_s *)stc_get_manager(); - ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data"); - - rv = stc_manager_dbus_emit_signal(stc->connection, - STC_DBUS_SERVICE_RESTRICTION_PATH, - STC_DBUS_INTERFACE_RESTRICTION, - "RestrictionThresholdCrossed", - g_variant_new("(s)", rstn_key->app_id)); - if (rv == TRUE) - rstn_value->rstn_limit_crossed_notified = TRUE; - - snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); - snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.out_bytes); - stc_send_restriction_message_to_net_popup("restriction threshold crossed", - "restriction_noti", rstn_key->app_id, iftype, byte); - } + snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit); + stc_send_restriction_message_to_net_popup("restriction threshold crossed", + "restriction_noti", rstn_key->app_id, + iftype, byte); } g_system->rstns_tree_updated = TRUE; @@ -694,8 +622,6 @@ static gboolean __update_app_statistics(gpointer key, gpointer value, stat.app_id = g_strdup(app_key->app_id); stat.snd_count = app_value->counter.out_bytes; stat.rcv_count = app_value->counter.in_bytes; - stat.delta_snd = 0; - stat.delta_rcv = 0; stat.is_roaming = stc_default_connection_get_roaming(); stat.ground = STC_APP_STATE_UNKNOWN; @@ -735,8 +661,7 @@ static gboolean __update_counter_statistics(gpointer key, gpointer value, stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value; table_counters_info info = { .restriction_id = rstn_value->restriction_id, - .sent_bytes = rstn_value->counter.out_bytes, - .rcv_bytes = rstn_value->counter.in_bytes + .data_counter = rstn_value->data_counter }; table_counters_update_counters(&info); @@ -812,8 +737,7 @@ static void __fill_nfacct_result(char *cnt_name, int64_t bytes, classid_bytes_context_s context = { .counter = &counter, .bytes = bytes, - .in_limit_reached = FALSE, - .out_limit_reached = FALSE + .data_limit_reached = FALSE, }; STC_LOGD("cnt_name %s", cnt_name); @@ -1087,12 +1011,9 @@ static stc_error_e __rstn_tree_add(stc_rstn_key_s *key, rstn_value->restriction_id = value->restriction_id; rstn_value->rst_state = value->rst_state; rstn_value->classid = value->classid; - rstn_value->limit.in_bytes = value->limit.in_bytes; - rstn_value->limit.out_bytes = value->limit.out_bytes; - rstn_value->warn_limit.in_bytes = value->warn_limit.in_bytes; - rstn_value->warn_limit.out_bytes = value->warn_limit.out_bytes; - rstn_value->counter.in_bytes = 0; - rstn_value->counter.out_bytes = 0; + rstn_value->data_limit = value->data_limit; + rstn_value->data_warn_limit = value->data_warn_limit; + rstn_value->data_counter = 0; rstn_value->warn_limit_crossed_notified = FALSE; rstn_value->rstn_limit_crossed_notified = FALSE; @@ -1129,10 +1050,8 @@ static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info, else value.classid = STC_UNKNOWN_CLASSID; - value.limit.in_bytes = info->rcv_limit; - value.limit.out_bytes = info->send_limit; - value.warn_limit.in_bytes = info->rcv_warn_limit; - value.warn_limit.out_bytes = info->send_warn_limit; + value.data_limit = info->data_limit; + value.data_warn_limit = info->data_warn_limit; if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE) ret = STC_CANCEL; @@ -1299,8 +1218,8 @@ stc_error_e stc_monitor_application_add(const stc_app_key_s app_key, key->pkg_id = g_strdup(app_key.pkg_id); value->type = app_value.type; - value->counter.in_bytes = app_value.counter.in_bytes; - value->counter.out_bytes = app_value.counter.out_bytes; + value->data_usage.in_bytes = app_value.data_usage.in_bytes; + value->data_usage.out_bytes = app_value.data_usage.out_bytes; value->processes = g_tree_new_full(__processes_tree_key_compare, NULL, __processes_tree_key_free, @@ -1492,10 +1411,8 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info) else value.classid = STC_UNKNOWN_CLASSID; - value.limit.in_bytes = info->rcv_limit; - value.limit.out_bytes = info->send_limit; - value.warn_limit.in_bytes = info->rcv_warn_limit; - value.warn_limit.out_bytes = info->send_warn_limit; + value.data_limit = info->data_limit; + value.data_warn_limit = info->data_warn_limit; ret = __rstn_tree_add(&key, &value, TRUE); diff --git a/src/stc-restriction.c b/src/stc-restriction.c index ba4e9ea..aa8ed4e 100755 --- a/src/stc-restriction.c +++ b/src/stc-restriction.c @@ -44,10 +44,8 @@ void __initialize_rstn_rule(table_restrictions_info *rule) rule->ifname = NULL; rule->iftype = STC_IFACE_ALL; rule->rst_state = STC_RESTRICTION_REMOVED; - rule->rcv_limit = 0; - rule->send_limit = 0; - rule->rcv_warn_limit = 0; - rule->send_warn_limit = 0; + rule->data_limit = 0; + rule->data_warn_limit = 0; rule->roaming = STC_ROAMING_DISABLE; rule->imsi = NULL; } @@ -114,17 +112,11 @@ void __stc_restriction_app_info_builder_add(GVariantBuilder *builder, g_variant_builder_add(builder, "{sv}", "rst_state", g_variant_new_uint16(info->rst_state)); - g_variant_builder_add(builder, "{sv}", "rcv_limit", - g_variant_new_int64(info->rcv_limit)); + g_variant_builder_add(builder, "{sv}", "data_limit", + g_variant_new_int64(info->data_limit)); - g_variant_builder_add(builder, "{sv}", "send_limit", - g_variant_new_int64(info->send_limit)); - - g_variant_builder_add(builder, "{sv}", "rcv_warn_limit", - g_variant_new_int64(info->rcv_warn_limit)); - - g_variant_builder_add(builder, "{sv}", "send_warn_limit", - g_variant_new_int64(info->send_warn_limit)); + g_variant_builder_add(builder, "{sv}", "data_warn_limit", + g_variant_new_int64(info->data_warn_limit)); g_variant_builder_add(builder, "{sv}", "roaming", g_variant_new_uint16(info->roaming)); @@ -202,21 +194,13 @@ static void __stc_extract_restriction_rule(const char *key, GVariant *value, rule->iftype = g_variant_get_uint16(value); STC_LOGD("iftype: [%u]", (unsigned int) rule->iftype); - } else if (!g_strcmp0(key, "rcv_limit")) { - rule->rcv_limit = g_variant_get_int64(value); - STC_LOGD("rcv_limit: [%lld]", rule->rcv_limit); - - } else if (!g_strcmp0(key, "send_limit")) { - rule->send_limit = g_variant_get_int64(value); - STC_LOGD("send_limit: [%lld]", rule->send_limit); - - } else if (!g_strcmp0(key, "rcv_warn_limit")) { - rule->rcv_warn_limit = g_variant_get_int64(value); - STC_LOGD("rcv_warn_limit: [%lld]", rule->rcv_warn_limit); + } else if (!g_strcmp0(key, "data_limit")) { + rule->data_limit = g_variant_get_int64(value); + STC_LOGD("data_limit: [%lld]", rule->data_limit); - } else if (!g_strcmp0(key, "send_warn_limit")) { - rule->send_warn_limit = g_variant_get_int64(value); - STC_LOGD("send_warn_limit: [%lld]", rule->send_warn_limit); + } else if (!g_strcmp0(key, "data_warn_limit")) { + rule->data_warn_limit = g_variant_get_int64(value); + STC_LOGD("data_warn_limit: [%lld]", rule->data_warn_limit); } else if (!g_strcmp0(key, "roaming")) { rule->roaming = g_variant_get_uint16(value); -- 2.7.4 From 401b1e043cc47994aa2f723bf9cda2bad3ae58fc Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Fri, 16 Jun 2017 12:00:46 +0530 Subject: [PATCH 07/16] [STC Manager] Added logic to add and remove iptable rule of restriction. Description: This patch adds logic to add and remove iptable rule of applied restriction, when application starts and exit respectively. Change-Id: I873183cbd94c94b4d4fc8987cdcea66d82aaa7e9 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/monitor/stc-monitor.c | 56 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 3afe575..6eb4d90 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.15 +Version: 0.0.16 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 896e437..32d2a8a 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -402,6 +402,32 @@ static void __process_restriction(enum traffic_restriction_type rst_type, switch (rst_type) { case RST_SET: + if (effective_data_limit <= 0) { + char *default_ifname = stc_default_connection_get_ifname(); + struct nfacct_rule counter; + stc_s *stc = stc_get_manager(); + + if (!stc->carg) { + stc->carg = MALLOC0(counter_arg_s, 1); + stc->carg->sock = + stc_monitor_get_counter_socket(); + } + + counter.carg = stc->carg; + counter.classid = rstn_value->classid; + counter.intend = NFACCT_BLOCK; + counter.iftype = STC_IFACE_UNKNOWN; + g_strlcpy(counter.ifname, default_ifname, + MAX_IFACE_LENGTH); + + generate_counter_name(&counter); + g_free(default_ifname); + + /* iptables rule */ + __add_iptables_in(&counter); + __add_iptables_out(&counter); + } + rstn_value->rst_state = STC_RESTRICTION_ACTIVATED; rstn_value->data_limit_reached = FALSE; break; @@ -409,8 +435,34 @@ static void __process_restriction(enum traffic_restriction_type rst_type, ;//Do Nothing break; case RST_UNSET: - rstn_value->rst_state = STC_RESTRICTION_REMOVED; - rstn_value->data_limit_reached = FALSE; + { + char *default_ifname = stc_default_connection_get_ifname(); + struct nfacct_rule counter; + stc_s *stc = stc_get_manager(); + + if (!stc->carg) { + stc->carg = MALLOC0(counter_arg_s, 1); + stc->carg->sock = + stc_monitor_get_counter_socket(); + } + + counter.carg = stc->carg; + counter.classid = rstn_value->classid; + counter.intend = NFACCT_BLOCK; + counter.iftype = STC_IFACE_UNKNOWN; + g_strlcpy(counter.ifname, default_ifname, + MAX_IFACE_LENGTH); + + generate_counter_name(&counter); + g_free(default_ifname); + + /* iptables rule */ + __del_iptables_in(&counter); + __del_iptables_out(&counter); + + rstn_value->rst_state = STC_RESTRICTION_REMOVED; + rstn_value->data_limit_reached = FALSE; + } break; default: ;//Do Nothing -- 2.7.4 From 22a7c3ea2a9a0d9f9d72731a3b2b650e3f2f33cd Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Fri, 16 Jun 2017 16:56:49 +0900 Subject: [PATCH 08/16] Added a icon for datausage noti in plugin Change-Id: Ib6524cb938f0e5155951123cce092dff403701ab Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 1 + plugin/CMakeLists.txt | 1 + plugin/data/stc_noti_datausage.png | Bin 0 -> 7782 bytes 3 files changed, 2 insertions(+) create mode 100755 plugin/data/stc_noti_datausage.png diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 3afe575..f82f5c7 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -104,4 +104,5 @@ cp resources/dbus/stc-manager.conf %{buildroot}%{_sysconfdir}/dbus-1/system.d/st %files plugin %manifest %{name}.manifest +%attr(644, -,-) %{_datadir}/icons/*.png %attr(500,root,root) %{_libdir}/stc-manager-plugin.so diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt index 66ab497..74cab33 100755 --- a/plugin/CMakeLists.txt +++ b/plugin/CMakeLists.txt @@ -34,3 +34,4 @@ SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "" OUTPUT_NAME ${PROJECT # install INSTALL(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${LIBDIR}) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data/stc_noti_datausage.png DESTINATION /usr/share/icons) diff --git a/plugin/data/stc_noti_datausage.png b/plugin/data/stc_noti_datausage.png new file mode 100755 index 0000000000000000000000000000000000000000..7ac94bb61661fac2508cfe0880b87433520b71a0 GIT binary patch literal 7782 zcmbVxRahL|wk;MUKyV1|8mw`L?#3a|!QC5|#x1ypK!RIv0yGT-3k25?+}(mhNN{&9 z*?a%{-iPy@hg)A&EvY%im~*bV9;)gcL`?zrImL4%BqUrVMOlrf*7NU$iT-qlp?zt2 zYREn0^gJ|OtUbKV-JnQ7D;EnWosyHe4O9baZsqIR50yYdLbbNj((}-Jttw{e;>2zK z*M{523HF3WLXwd7ftg!6Kt1Rzpf+~Sk_?BlHp&xVo&|Q(>x4x|1$A#kYxC`Q+lr< zbYK@ZD4ifTKbIvh06-@s#0?M-6cPe(((&;E_;`4qP9ZLUfS90=7@siRe-4HxH8(43 zF%4Py|7bmpBpGZyJYZrxJl@{k+}`}$E^anF08vrVzZ`sgTu&BU?!L|*=004`?u`F1 z$U@yM-RxiR2IbL!y1RI~Sw5|YHRC_vr`-MTLVpdPyb)7(vwK<;b4OVh zOHU`LvxkzbB*W7aZYw)0F%dagK4C$ykSJJGR*nw<01ARYvhpGTKA^m;06c!fKcSB=b36YEG|${Zg~r)YtM)=n0VG|;AYUcQJtWRrf5F)rsUos5`{lOz@NWObTkXNf8nUk>gwMM^` zv`?{Grl}jpf!`FR%mam=4UNmNRJvaJGXuZFXYwMs=)&Fw+aq}?n-;5fx-!Z)$!Ed4UrVg z6$1R;P6+m(k#n4G6DvNqq6kqn2I)!Uj8Y+6(31Tk3EZ6>vXb91=*rBu=GC^6i)H@FYu&}v^5=T^ZFaL=`RDtm+@Rxmu-NLGgkKHei0x)yR?0gbSjZ6uB=}n!?T3=0>fIH622p|ueN7dOGIJ+D zaO32D=*$g=t-WJ-z(eefWKOa_t&i2R-ul^P8BHUEUY#qz%DJ*?n zdSP6|BC^+%5@eYpY$B?o0`@@kVBqtJ8R*KyLW(8=W$4>BLiWn#~j*;aF00kCj^m;g+y@Ip+wTB?O*6S#O4s+UlSSBv)j zy&%v1KFX4?V)qs#j$(?tG}||cA3D*slJWWn2?_HCynM)GuEjJ|8O@#7bmO$&Aum)K zAMbz$f8-cRzjpQ0vZmQ0St*1a-@jf^i~C`-P}{SczP}Jf&?37u?e5%dINi_1&fGw! zCmH3cKqq9htVim0@S#|G=kdiCFXxy~^ckjoQLQ06oklX9`O9@x?F&V33#0L*Zr2;7 zDecRs$D=jUJX4KrJX5&>hOGHb>#Hmz#MvG+E*QyE9fcF z(ykjBHXK6#*peO6WjVg|M$P$9%holUDAhs&N!cv4guqFSPA4 zERJ$Ch+I2Kg32t)D6o0c>l%Bun&mdYy_obw-cal=pm)r`#zVPCJil$PrXl!dx~&&~ zu7=2@L=RvWE%%7(c?-sd3Q+k+07 z&KC~3G0tk8-?vD2%FA*`mA~;wxoB~a=XfMc#tKoj)x=ch_wXECHg-hiM{AI zv$e^$Fky!+UJS4E_*kr9U`WGd&SxV#I9w-7B7@ZocVVinM{)0b+1*Q2Y4An^Y0z?c z-KdvmLl{IxYdoj&guZekoATn_Og3T|J&!iTWXl#Lp9_d`)6@GpYo?eH^1C{atz!a= za`x3wgPMq}jlYhql|fvXHfNC#O^IB8^qIKI0-LW&jD2`!;q3PdVR)yobAs919-q7n z3XIYeUAG;{xXu|IQcg+RJbq`GMcyQR4^Q>&**{^8}K}nIRjhEd0V?OGckfzXkOOENWL6G z23pEX7_kkg!NCxWpJa?xi*kM0CP)YaC!M^v3b2b&*1{VnYWS(_iywoYLw67FiB7ZJ zvom~;wuK8Zh*`-+IZ{GG$Gl(m8!cXi#oy5w9tZ@-I#1ozA^B!p^D@qE>S?vuMsf?V#bLZzfpRI*H!pwF6H*-bT#iG8TBDEN`d;-n@zY*mP6vU5 zs9E{Y=b=mkAAcZlHb^MY~wW|?~@FcPdoQtQO=BFHfiUH@AH;+ z3=ESCpE9-vF-K|yvhT>LSHc#C1~n6+>RWYUg>#I3XL&En5X$r)y|?-l@65@6KuJ3U zh_v7K6dnFtHYI1dWp3V*c16kc8ZEEE`Y!GD9)Hx%hL^LmJLS_tnv6^zZS~35Lme|W zeFD`Y9O0B)FB|)y7V{d@8gbW$!=sppigQl1qN|?)yjdfJG#_a?*E$G1sJR?N=04{SZje|;-Gy9MjmcRW)b7Q@8B0IFv5Yj5(WpUO85iv|Z@$iBYH(Vg5!`v@3Q^@3 zzJOL(9qQaHZIQpww9_uZB8G++ZiWrfhzSMQA^wNM-DR=dvO zOxYxp_Dt=z;H(7dWJZ@@Xg!w$&wQeGHlQ}8kwO7A4nxcsh6lL=FBfuNUZAB|53@VO zVAq2E4G95_#IFR_VQX}#xck{QA+x5W-dm@F9Px(f&wM$*)t6jJ&P)2q=C%n9cR+SL z;X*COuMT>PT1P6LM}#$n#kBmoDgLujGj74zvK}mBH}}!QTN=(W0z8pK8%rT(gZ_Eq3QeEFWHkl+t2_75g4``=dx*SJ z(RI+Z1wodBg80JzqJGcyV<_f^+2iHvdB&(&m`@@Kv4|qw4|2`;YKlBfP?^4F6twu` zk4%hTkkIvyG`KW;j*4>pz{`C-^5c5k1SX&rQ@dr7khaByFn}taX)$lfK%kbXf=X^6 z&Bd5-Hp@Kih%-)BtD}0ug5L68V3F-MRbnk}f6llMCfNbesb&2{m zd`o<;^dB^MgEX|**r9|R(IUqSs`=d78Ja$%h5rL+*gFv(lQWLYZxvs29$QP(_dyZ% zT|)`{kg)?5b5h0S+m+?eG8LVnlMNU|&>K^W5VFq|1MOo-P9~Lg<$Sz=MK8NESZbf4 z>8sGFklfJD^#akq*#Nsz-pI9ZMt_Q68jU-O#j#@x{cO`ujMtxh5iNKBQ($_mhJvt8 z)IFKpF^}ZWJ%TtynUG1e(b#Om&ym!>VGKo~NUQI&;qBYpMRD4QX?KIxk#qVNqoGc-N!la1_&^~)^hM1OOjJpOogk<6qtBT$P)g(gz|fJTnV6@3T4~E}jHlK+ zBo<_)=O*A6TjrR<(cUNmj5N+mjA^R$<{U5^dg@TDM48zK`T^*?GPezwS}hVHnkX+4 z0op1)p7hvMu6NI0RIVeDg(8@hz{N-@1Of`Wxync)_k1bIkZQ)S?NKmu&BeO>`E)C# z@FBW#3fd556y%Fw5U5~u=q&Z}{j=WIDb5dJGCST$W#LrK7EC@8 zSnAofjBs!^ewN3Q#z#O%XeRlCJi%0q4meR9uwH#HsNJbCmCvo(#D^e^B)4w}cP^zM z`-z{ERZQ^?d1Gq94K+QnDmGkxJ>A7NwfNP=EB1beQfdTAW->7xI~9pGD^-Bz&^49A zvS7_EVk4A-Ed~MPD=`KE8Wwbzovx1tMlrkawO&u6QAj7Vie|4s0t@%W`UtU;8s-sK zYa#}+fya1!U^}|Oeus)frTHV8U8RK+a=T|Z$fm5@rG|(FrI6>A>>&Ij#rNKv1d7V};FZQr@i zn(U*7&#vhe#Z7iF%?qR5#nY64;A_usTG}Et(4-Zg9N8m|LW0egNu8lu%zgDJS-RFC z&os*+Vk2k~9uaV13G3ikGX22Q1voEj&-W)$Biw&XKjyZa&cw$JWOssx+@(Y{5ne9 zs91Z;e_Gb0 z1U73J;yLa8r8PbN(#XI8Aaz`Q4xGbHf4x?n!Yq?ejjOw-v*|Ozl02SL9T`*{YxeDX zz0#lULy~SLe^#{CoXQA1m%FdcDHoX9-swzoC(;&gKJ^k`>h;jQN&$sL%qMABKDak4 znKqD9tBxIAhdQcWuDyJIV3dmIX0w08#x7Y}6iHRwaVzYIiGG(7%eRdch0K=UcJ`wP z)qf0vX}_r&s>q0pBX|+ena0I2-Ii2GJDROAHk#DfT&_(dc2GB<(0ij}6u4PxZ3xxV z$`Ry+OOQCf>^&bJ2+rdZkXw3tI((&sOLcZ?b-?X#9(0tXivohZ2Rc(GnJ~4kTmV6{8+-wfQ&@pqxn~q z>VKB?KEDD_I8a>uu*odGPI;xfUK^_Bv;J)U+wbrvDXYcT_Ej5fT)t}K+=3F)2FZdj zxEDW>)S#e%zy!zk^@6MjSiZq8h z{H~_ya*;XIT!}`O@6+&1j>PH%w=1ND_TfzK%~AW%d(RSE9|&q{KDtfmcp>KN>Fkma z)6Vb~(hYXHYRns=(TZXdNRf)PdV_pwP>NKD*bhD}as=+W0tVrKA!cOENI!qVIr z5r6goh!7S_{pt5q6)%ffKf&C6T&b88ult?FSlewSxd0KP6%Njo{36JG8N%lNo_465>A9-LH0;=(C<|mg>~jBR3L%%ucdZ2?hA&wL(3$=U@R zA1{KMnozDzOfUKj{&dt;HMgrbA(5`s^l z8bmZSqPup#Vv1fLsOm8o^PKh&uAqpOC!;mVsTE-EoSCxYYZxSTUGeSbLW9TdghDmS zhB-$h;v*=M)}B}|GxxH#F|+8%F0PvRd8BI|ff5W>(y5d$1VZ`gx2geKh~%9JX2}~nw#XTHw43 zzP3APLp}0CYjbH?Sy{kA8G^fRidptP>YCE>h=&;=qR zuZMtB_K~K@f@nFy4Ku(})i)~jnKEQ%|1MtUG&tJ=M$FiCRm1hnuoxyYM-})op79N) zc2MOKmx4>wOEb&K^^LbtZ2HChyV7cwnkqU8Or8M98F55C5uYV$N==6?6-M~SI(ibE ziMU`d+q}MJZSBCzx~CU0%-~k%X(JwshhwN6=~LLkNN

P;*arS(F?vGq$P(q%u!6V*3K+~3CaYun^B_A=0vMrHR{9hjUn*csuUkp&XvbV)<3 zL4)c7_r%q|6iM@4G@ct1NJt#Z6qsX$@Rgz?_m*L4U(l~uRTd8{YRTPcIWOI`)wqNi zM!|7(Xw>cC0g3`7jWzBwq)F&Is-Xq%L-J0r5{H0?sz8@|LVU3;Z0WqQ&%!V6vgl3w zkdcSqKfFgzGbk%YoaeFEWirr0!uA)xj~+_mLnD-3y5EVECJ~vw;^tPHqhAj}#VELC zuUSlq{ypj!P32)+2Y;v Date: Wed, 21 Jun 2017 15:48:07 +0900 Subject: [PATCH 09/16] Added check emulator environment Change-Id: Ied3e0a4abe59f93445aa46c8a44868f52c82876c Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 3 +- src/CMakeLists.txt | 1 + src/monitor/include/stc-application-lifecycle.h | 0 src/monitor/include/stc-default-connection.h | 0 src/monitor/include/stc-emulator.h | 25 +++++++++++ src/monitor/include/stc-monitor.h | 0 src/monitor/stc-emulator.c | 57 +++++++++++++++++++++++++ src/monitor/stc-monitor.c | 4 +- src/stc-manager.c | 17 +++++--- 9 files changed, 98 insertions(+), 9 deletions(-) mode change 100644 => 100755 src/monitor/include/stc-application-lifecycle.h mode change 100644 => 100755 src/monitor/include/stc-default-connection.h create mode 100755 src/monitor/include/stc-emulator.h mode change 100644 => 100755 src/monitor/include/stc-monitor.h create mode 100755 src/monitor/stc-emulator.c diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 7247da5..f9ff828 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.16 +Version: 0.0.17 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 @@ -19,6 +19,7 @@ BuildRequires: pkgconfig(gio-unix-2.0) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(vconf) +BuildRequires: pkgconfig(capi-system-info) %if %{?enable_database} == YES BuildRequires: pkgconfig(sqlite3) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4173f82..736cec3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,6 +6,7 @@ SET(REQUIRES_LIST ${REQUIRES_LIST} gio-unix-2.0 dlog vconf + capi-system-info ) IF("${ENABLE_DATABASE}" STREQUAL "YES") diff --git a/src/monitor/include/stc-application-lifecycle.h b/src/monitor/include/stc-application-lifecycle.h old mode 100644 new mode 100755 diff --git a/src/monitor/include/stc-default-connection.h b/src/monitor/include/stc-default-connection.h old mode 100644 new mode 100755 diff --git a/src/monitor/include/stc-emulator.h b/src/monitor/include/stc-emulator.h new file mode 100755 index 0000000..8e06f7c --- /dev/null +++ b/src/monitor/include/stc-emulator.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __STC_EMULATOR_H__ +#define __STC_EMULATOR_H__ + +#include + +gboolean stc_emulator_is_emulated(void); +void stc_emulator_check_environment(void); + +#endif /* __STC_EMULATOR_H__ */ diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h old mode 100644 new mode 100755 diff --git a/src/monitor/stc-emulator.c b/src/monitor/stc-emulator.c new file mode 100755 index 0000000..80bc170 --- /dev/null +++ b/src/monitor/stc-emulator.c @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "stc-emulator.h" +#include "stc-manager-util.h" + +static gboolean stc_is_emulated = FALSE; + +static gboolean __stc_emulator_check_env(void) +{ + int ret; + char *model = NULL; + + ret = system_info_get_platform_string("tizen.org/system/model_name", &model); + if (ret != SYSTEM_INFO_ERROR_NONE) { + STC_LOGE("Failed to get system information(%d)", ret); + return FALSE; + } + + if (model && strncmp(model, "Emulator", strlen("Emulator")) == 0) { + g_free(model); + return TRUE; + } + + g_free(model); + return FALSE; +} + +gboolean stc_emulator_is_emulated(void) +{ + return stc_is_emulated; +} + +void stc_emulator_check_environment(void) +{ + stc_is_emulated = __stc_emulator_check_env(); + + STC_LOGD("Emulation environment : %s", + stc_is_emulated ? "It's emulated" : "Not emulated"); +} + diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 32d2a8a..835e29f 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -895,7 +895,7 @@ static gboolean __process_contr_reply(GIOChannel *source, ret = read_netlink(sock, &ans, sizeof(struct genl)); - STC_LOGD("Counter data received ret [%d]", ret); + /* STC_LOGD("Counter data received ret [%d]", ret); */ if (ret == 0) goto out; @@ -918,7 +918,7 @@ static gboolean __update_contr_cb(void *user_data) stc->carg->sock = stc_monitor_get_counter_socket(); } - STC_LOGD("Get all counters"); + /* STC_LOGD("Get all counters"); */ nfacct_send_get_all(stc->carg); /* we need to continue the timer */ diff --git a/src/stc-manager.c b/src/stc-manager.c index 69349ae..6af6b7b 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -15,6 +15,7 @@ */ #include "stc-manager.h" +#include "stc-emulator.h" #include "stc-statistics.h" #include "stc-restriction.h" #include "stc-manager-gdbus.h" @@ -98,13 +99,16 @@ gint32 main(gint32 argc, gchar *argv[]) g_type_init(); #endif - g_stc = __stc_manager_init(); - if (!g_stc) - goto fail; - /* Crate the GLIB main loop */ main_loop = g_main_loop_new(NULL, FALSE); - g_stc->main_loop = main_loop; + + stc_emulator_check_environment(); + if (stc_emulator_is_emulated() == FALSE) { + g_stc = __stc_manager_init(); + if (!g_stc) + goto fail; + g_stc->main_loop = main_loop; + } /* Run the main loop */ g_main_loop_run(main_loop); @@ -112,7 +116,8 @@ gint32 main(gint32 argc, gchar *argv[]) ret = 0; fail: - __stc_manager_deinit(); + if (stc_emulator_is_emulated() == FALSE) + __stc_manager_deinit(); if (main_loop) g_main_loop_unref(main_loop); -- 2.7.4 From 086cdb8b95c7e336335f31fcdba1f7283805a2c3 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Tue, 27 Jun 2017 14:19:33 +0530 Subject: [PATCH 10/16] Ignoring SIGCHLD signal as exit status if child process is not required. Description: This patch ignores the SIGCHLD event, so that kernel can reap the child process automatically so that process table is properly updated. Change-Id: I09283dbee630359a5daa5c2d25f97a65ac6dda3e Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/stc-manager.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index f9ff828..d9ab976 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.17 +Version: 0.0.18 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/stc-manager.c b/src/stc-manager.c index 6af6b7b..a732b72 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include "stc-manager.h" #include "stc-emulator.h" #include "stc-statistics.h" @@ -89,6 +90,8 @@ gint32 main(gint32 argc, gchar *argv[]) GMainLoop *main_loop = NULL; gint32 ret = -1; + signal(SIGCHLD, SIG_IGN); + STC_LOGI("Smart Traffic Control Manager"); if (daemon(0, 0) != 0) -- 2.7.4 From 2113c9539100f68efcdbd0266084f62e9aca3e6a Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Wed, 28 Jun 2017 16:45:39 +0530 Subject: [PATCH 11/16] Reopening socket if G_IO error is received and updated debug logs. Change-Id: I58ee40fe4b3349dd26117907347893d892fd0a63 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/database/tables/table-statistics.c | 2 +- src/helper/helper-net-cls.c | 2 +- src/helper/helper-nfacct-rule.c | 2 +- src/monitor/stc-default-connection.c | 2 +- src/monitor/stc-monitor.c | 56 +++++++++++++++++++++++++++++----- src/stc-manager.c | 6 +++- 7 files changed, 59 insertions(+), 13 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index d9ab976..cfd59dd 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.18 +Version: 0.0.19 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c index e16059a..73c298d 100755 --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -619,7 +619,7 @@ stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key, goto handle_error; } - STC_LOGE("App stat recorded [%s]", stat->app_id); + STC_LOGD("App stat recorded [%s]", stat->app_id); handle_error: sqlite3_reset(stmt); diff --git a/src/helper/helper-net-cls.c b/src/helper/helper-net-cls.c index 8f3f5d0..826ff7d 100755 --- a/src/helper/helper-net-cls.c +++ b/src/helper/helper-net-cls.c @@ -37,7 +37,7 @@ static uint32_t __produce_classid(check_classid_used_cb check_classid_cb) int classid_test_count = 0; int ret = fread_uint(CUR_CLASSID_PATH, &classid); if (ret < 0) - STC_LOGE("Can not read current classid"); + STC_LOGI("Can not read current classid"); classid += 1; if (check_classid_cb) for (classid_test_count = 0; classid_test_count < INT32_MAX; diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index 1eb140b..315a465 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -126,7 +126,7 @@ static stc_error_e send_nfacct_request(int sock, struct genl *req) int ret = sendto(sock, (char *)(&req->n), req->n.nlmsg_len, 0, (struct sockaddr *)&nladdr, sizeof(nladdr)); ret_value_msg_if(ret < 0, STC_ERROR_FAIL, - "Failed to send command to get outgoing traffic"); + "Failed to send nfacct request, error [%d]", ret); return STC_ERROR_NONE; } diff --git a/src/monitor/stc-default-connection.c b/src/monitor/stc-default-connection.c index 9aad6fb..ad9b74f 100755 --- a/src/monitor/stc-default-connection.c +++ b/src/monitor/stc-default-connection.c @@ -140,7 +140,7 @@ static void __get_default_connection_info(GDBusConnection *connection, gchar *key = NULL; if (object_path == NULL) { - STC_LOGE("Object path is NULL"); + STC_LOGI("Object path is NULL, so information not available."); return; } diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 835e29f..31dadf6 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -540,19 +540,57 @@ static stc_error_e __close_contr_sock(stc_system_s *system) ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter"); /* close netlink socket for updating kernel counters */ - if (g_system->contr_sock != -1) { - close(g_system->contr_sock); - g_system->contr_sock = -1; + if (system->contr_sock != -1) { + close(system->contr_sock); + system->contr_sock = -1; } - if (g_system->contr_gsource_id != 0) { - g_source_remove(g_system->contr_gsource_id); - g_system->contr_gsource_id = 0; + if (system->contr_gsource_id != 0) { + g_source_remove(system->contr_gsource_id); + system->contr_gsource_id = 0; } return STC_ERROR_NONE; } +static gboolean __process_contr_reply(GIOChannel *source, + GIOCondition condition, + gpointer user_data); + +static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system) +{ + GIOChannel *gio = NULL; + ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter"); + + /* close netlink socket for updating kernel counters */ + if (system->contr_sock != -1) { + close(system->contr_sock); + system->contr_sock = -1; + } + + if (system->contr_gsource_id != 0) { + g_source_remove(system->contr_gsource_id); + system->contr_gsource_id = 0; + } + + /* create netlink socket for updating kernel counters */ + system->contr_sock = create_netlink(NETLINK_NETFILTER, 0); + if (!(system->contr_sock)) { + STC_LOGE("failed to open socket"); + FREE(system); + return STC_ERROR_FAIL; + } + + gio = g_io_channel_unix_new(system->contr_sock); + system->contr_gsource_id = + g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP, + (GIOFunc) __process_contr_reply, + NULL); + g_io_channel_unref(gio); + + return STC_ERROR_NONE; +} + static gboolean __rstn_counter_update_foreach_classid(gpointer key, gpointer value, gpointer data) @@ -884,7 +922,11 @@ static gboolean __process_contr_reply(GIOChannel *source, (condition & G_IO_NVAL)) { /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */ - __close_contr_sock(g_system); + STC_LOGE("Counter socket received G_IO event, closing socket." + "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]", + (condition & G_IO_ERR), (condition & G_IO_HUP), + (condition & G_IO_NVAL)); + __close_and_reopen_contr_sock(g_system); return FALSE; } diff --git a/src/stc-manager.c b/src/stc-manager.c index a732b72..1f856ef 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -54,6 +54,7 @@ static stc_s *__stc_manager_init(void) { __STC_LOG_FUNC_ENTER__; stc_s *stc; + stc_error_e err = STC_ERROR_NONE; stc = MALLOC0(stc_s, 1); if (!stc) { @@ -66,7 +67,10 @@ static stc_s *__stc_manager_init(void) EXEC(STC_ERROR_NONE, stc_db_initialize()); - stc_monitor_init(); + err = stc_monitor_init(); + if (err != STC_ERROR_NONE) + goto handle_error; + stc_manager_gdbus_init((gpointer)stc); stc_manager_plugin_init(); -- 2.7.4 From 887245fd8e34706dee927d150baf9fcaa59420d7 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 27 Jun 2017 16:33:14 +0900 Subject: [PATCH 12/16] Added logic to monitor and restrict data usage per interface Change-Id: I597acaa133cec9cd322309201ebf724582810cc4 Signed-off-by: hyunuktak --- include/stc-manager-util.h | 3 + include/stc-manager.h | 3 + packaging/stc-manager.spec | 2 +- src/database/tables/table-statistics.c | 20 ++-- src/helper/helper-net-cls.c | 9 ++ src/helper/helper-net-cls.h | 9 -- src/helper/helper-nfacct-rule.c | 5 +- src/monitor/stc-monitor.c | 161 +++++++++++++++++++++++++-------- src/stc-manager.c | 3 + 9 files changed, 160 insertions(+), 55 deletions(-) diff --git a/include/stc-manager-util.h b/include/stc-manager-util.h index 1f8af9a..7a19fcb 100755 --- a/include/stc-manager-util.h +++ b/include/stc-manager-util.h @@ -345,6 +345,9 @@ enum stc_reserved_classid { STC_FOREGROUND_APP_CLASSID, /* it will used for special cgroup, blocked cgroup */ STC_BACKGROUND_APP_CLASSID, + STC_TOTAL_DATACALL_CLASSID, + STC_TOTAL_WIFI_CLASSID, + STC_TOTAL_BLUETOOTH_CLASSID, STC_NETWORK_RESTRICTION_APP_CLASSID, STC_RESERVED_CLASSID_MAX, }; diff --git a/include/stc-manager.h b/include/stc-manager.h index 73b7370..ac8ecb4 100755 --- a/include/stc-manager.h +++ b/include/stc-manager.h @@ -24,6 +24,9 @@ #define NET_CLS_SUBSYS "net_cls" #define STC_BACKGROUND_APP_SUFFIX "_BACKGROUND" +#define STC_TOTAL_DATACALL "TOTAL_DATACALL" +#define STC_TOTAL_WIFI "TOTAL_WIFI" +#define STC_TOTAL_BLUETOOTH "TOTAL_BLUETOOTH" typedef enum { STC_CANCEL = 0, /**< cancel */ diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index f9ff828..cfd59dd 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.17 +Version: 0.0.19 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c index e16059a..b31d4af 100755 --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -42,26 +42,26 @@ /* SELECT statements */ #define SELECT_FOR_PERIOD "select binpath, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, " \ - "sum(sent) as sent, imsi, ground from statistics " \ + "sum(sent) as sent, imsi, ground, iftype, ifname from statistics " \ "where time_stamp between ? and ? " \ "group by binpath, is_roaming, imsi order by received desc" #define SELECT_FOR_PERIOD_IFACE "select binpath, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, " \ - "sum(sent) as sent, imsi, ground from statistics " \ + "sum(sent) as sent, imsi, ground, iftype, ifname from statistics " \ "where time_stamp between ? and ? " \ "and iftype=? group by binpath, is_roaming, imsi order by received desc" #define SELECT_CHUNKS "select binpath, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, " \ - "sum(sent) as sent, time_stamp - time_stamp % ? as time_stamp, imsi, " \ - "ground " \ + "sum(sent) as sent, imsi, ground, iftype, ifname, " \ + "time_stamp - time_stamp % ? as time_stamp " \ "from statistics where time_stamp between ? and ? " \ "group by binpath, time_stamp, imsi order by time_stamp" #define SELECT_CHUNKS_IFACE "select binpath, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, " \ - "sum(sent) as sent, imsi, ground, " \ + "sum(sent) as sent, imsi, ground, iftype, ifname, " \ "time_stamp - time_stamp % ? as time_stamp " \ "from statistics where time_stamp between ? and ? and iftype=?" \ "group by binpath, time_stamp, imsi order by time_stamp" @@ -103,7 +103,8 @@ #define SELECT_TOTAL "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ "ifname, imsi, ground from statistics " \ - " where (time_stamp between ? and ?) " \ + " where (time_stamp between ? and ?) and binpath != 'TOTAL_DATACALL' " \ + "and binpath != 'TOTAL_WIFI' and binpath != 'TOTAL_BLUETOOTH' " \ "group by iftype, ifname, imsi, hw_net_protocol_type, is_roaming " \ "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ "is_roaming" @@ -469,12 +470,15 @@ stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rul data.app_id = (char *)sqlite3_column_text(stmt, 0); data.hw_net_protocol_type = sqlite3_column_int(stmt, 1); data.roaming = sqlite3_column_int(stmt, 2); - data.ground = sqlite3_column_int(stmt, 6); data.cnt.in_bytes = sqlite3_column_int64(stmt, 3); data.cnt.out_bytes = sqlite3_column_int64(stmt, 4); data.imsi = (char *)sqlite3_column_text(stmt, 5); + data.ground = sqlite3_column_int(stmt, 6); + data.iftype = sqlite3_column_int(stmt, 7); + data.ifname = (char *)sqlite3_column_text(stmt, 8); + if (rule->granularity) { - interval.from = sqlite3_column_int64(stmt, 7); + interval.from = sqlite3_column_int64(stmt, 9); interval.to = interval.from + rule->granularity; } diff --git a/src/helper/helper-net-cls.c b/src/helper/helper-net-cls.c index 8f3f5d0..ba30745 100755 --- a/src/helper/helper-net-cls.c +++ b/src/helper/helper-net-cls.c @@ -94,6 +94,15 @@ uint32_t get_classid_by_app_id(const char *app_id, int create) return STC_UNKNOWN_CLASSID; } + if (!strcmp(app_id, STC_TOTAL_DATACALL)) + return STC_TOTAL_DATACALL_CLASSID; + + if (!strcmp(app_id, STC_TOTAL_WIFI)) + return STC_TOTAL_WIFI_CLASSID; + + if (!strcmp(app_id, STC_TOTAL_BLUETOOTH)) + return STC_TOTAL_BLUETOOTH_CLASSID; + if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX)) path_to_net_cgroup_dir = BACKGROUND_CGROUP_NETWORK; else diff --git a/src/helper/helper-net-cls.h b/src/helper/helper-net-cls.h index b809716..24cd993 100755 --- a/src/helper/helper-net-cls.h +++ b/src/helper/helper-net-cls.h @@ -51,13 +51,4 @@ typedef gboolean(*check_classid_used_cb)(guint32 classid); stc_error_e place_pids_to_net_cgroup(const int pid, const char *pkg_name); -/** - * @desc this function makes net_cls cgroup and put pids into it. - */ -void create_net_background_cgroup(GSList *background_pid_list); - -void add_pid_to_background_cgroup(pid_t pid); - -void remove_pid_from_background_cgroup(pid_t pid); - #endif /*__STC_HELPER_NET_CLS_H__*/ diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index 1eb140b..8ad3a17 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -864,7 +864,10 @@ stc_error_e produce_net_rule(nfacct_rule_s *rule, return STC_ERROR_NONE; if (rule->classid != STC_ALL_APP_CLASSID && - rule->classid != STC_TETHERING_APP_CLASSID) + rule->classid != STC_TETHERING_APP_CLASSID && + rule->classid != STC_TOTAL_DATACALL_CLASSID && + rule->classid != STC_TOTAL_WIFI_CLASSID && + rule->classid != STC_TOTAL_BLUETOOTH_CLASSID) ret = produce_app_rule(rule, send_limit, rcv_limit, action, jump, iotype); else diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 835e29f..f06d679 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -276,6 +276,11 @@ static gboolean __add_application_monitor(gpointer key, gpointer value, default_connection_s *connection = (default_connection_s *)data; stc_s *stc = stc_get_manager(); + if (app_value->classid == STC_TOTAL_DATACALL_CLASSID || + app_value->classid == STC_TOTAL_WIFI_CLASSID || + app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID) + return FALSE; + if (stc && connection && connection->ifname) { struct nfacct_rule counter; @@ -289,6 +294,7 @@ static gboolean __add_application_monitor(gpointer key, gpointer value, counter.carg = stc->carg; counter.classid = app_value->classid; counter.intend = NFACCT_COUNTER; + counter.iftype = connection->type; g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH); __add_iptables_in(&counter); @@ -318,6 +324,7 @@ static gboolean __remove_application_monitor(gpointer key, gpointer value, counter.carg = stc->carg; counter.classid = app_value->classid; counter.intend = NFACCT_COUNTER; + counter.iftype = connection->type; g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH); __del_iptables_in(&counter); @@ -416,7 +423,7 @@ static void __process_restriction(enum traffic_restriction_type rst_type, counter.carg = stc->carg; counter.classid = rstn_value->classid; counter.intend = NFACCT_BLOCK; - counter.iftype = STC_IFACE_UNKNOWN; + counter.iftype = rstn_key->iftype; g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH); @@ -449,7 +456,7 @@ static void __process_restriction(enum traffic_restriction_type rst_type, counter.carg = stc->carg; counter.classid = rstn_value->classid; counter.intend = NFACCT_BLOCK; - counter.iftype = STC_IFACE_UNKNOWN; + counter.iftype = rstn_key->iftype; g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH); @@ -553,25 +560,10 @@ static stc_error_e __close_contr_sock(stc_system_s *system) return STC_ERROR_NONE; } -static gboolean __rstn_counter_update_foreach_classid(gpointer key, - gpointer value, - gpointer data) +static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key, + stc_rstn_value_s *rstn_value, + classid_bytes_context_s *context) { - stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key; - stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value; - classid_bytes_context_s *context = (classid_bytes_context_s *)data; - - if (context->counter->intend != NFACCT_COUNTER) - goto try_next_callback; - - if (rstn_value->classid != context->counter->classid) - goto try_next_callback; - - if (rstn_value->data_limit_reached == TRUE) { - context->data_limit_reached = TRUE; - goto try_next_callback; - } - switch (context->counter->iotype) { case NFACCT_COUNTER_IN: case NFACCT_COUNTER_OUT: @@ -591,7 +583,7 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, STC_DBUS_SERVICE_RESTRICTION_PATH, STC_DBUS_INTERFACE_RESTRICTION, "WarnThresholdCrossed", - g_variant_new("(s)", rstn_key->app_id)); + g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype)); if (rv == TRUE) rstn_value->warn_limit_crossed_notified = TRUE; @@ -627,7 +619,7 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, STC_DBUS_SERVICE_RESTRICTION_PATH, STC_DBUS_INTERFACE_RESTRICTION, "RestrictionThresholdCrossed", - g_variant_new("(s)", rstn_key->app_id)); + g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype)); if (rv == TRUE) rstn_value->rstn_limit_crossed_notified = TRUE; @@ -645,11 +637,57 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, STC_LOGE("unknown iotype"); } + return FALSE; +} + +static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key, + stc_rstn_value_s *rstn_value, + classid_bytes_context_s *context) +{ + if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID && + context->counter->iftype == STC_IFACE_DATACALL) || + (rstn_value->classid == STC_TOTAL_WIFI_CLASSID && + context->counter->iftype == STC_IFACE_WIFI) || + (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID && + context->counter->iftype == STC_IFACE_BLUETOOTH)) { + context->counter->classid = rstn_value->classid; + return __rstn_counter_update(rstn_key, rstn_value, context); + } -try_next_callback: return FALSE; } +static gboolean __rstn_counter_update_foreach_classid(gpointer key, + gpointer value, + gpointer data) +{ + gboolean rv = FALSE; + stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key; + stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value; + classid_bytes_context_s *context = (classid_bytes_context_s *)data; + uint32_t classid; + + if (context->counter->intend != NFACCT_COUNTER) + goto try_next_callback; + + if (rstn_value->data_limit_reached == TRUE) { + context->data_limit_reached = TRUE; + goto try_next_callback; + } + + classid = context->counter->classid; + rv = __interface_rstn_counter_update(rstn_key, rstn_value, context); + + context->counter->classid = classid; + if (rstn_value->classid != context->counter->classid) + goto try_next_callback; + + rv = __rstn_counter_update(rstn_key, rstn_value, context); + +try_next_callback: + return rv; +} + static gboolean __update_app_statistics(gpointer key, gpointer value, gpointer data) { @@ -739,20 +777,10 @@ static gboolean __flush_rstns_counter_to_database(gpointer user_data) return G_SOURCE_REMOVE; } -static gboolean __apps_counter_update_foreach_classid(gpointer key, - gpointer value, - gpointer data) +static void __app_counter_update(stc_app_key_s *app_key, + stc_app_value_s *app_value, + classid_bytes_context_s *context) { - stc_app_key_s *app_key = (stc_app_key_s *)key; - stc_app_value_s *app_value = (stc_app_value_s *)value; - classid_bytes_context_s *context = (classid_bytes_context_s *)data; - - if (context->counter->intend != NFACCT_COUNTER) - goto try_next_callback; - - if (app_value->classid != context->counter->classid) - goto try_next_callback; - switch (context->counter->iotype) { case NFACCT_COUNTER_IN: app_value->data_usage.in_bytes += context->bytes; @@ -771,6 +799,38 @@ static gboolean __apps_counter_update_foreach_classid(gpointer key, default: STC_LOGE("unknown iotype"); } +} + +static void __interface_counter_update(stc_app_key_s *app_key, + stc_app_value_s *app_value, + classid_bytes_context_s *context) +{ + if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID && + context->counter->iftype == STC_IFACE_DATACALL) || + (app_value->classid == STC_TOTAL_WIFI_CLASSID && + context->counter->iftype == STC_IFACE_WIFI) || + (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID && + context->counter->iftype == STC_IFACE_BLUETOOTH)) + __app_counter_update(app_key, app_value, context); +} + +static gboolean __apps_counter_update_foreach_classid(gpointer key, + gpointer value, + gpointer data) +{ + stc_app_key_s *app_key = (stc_app_key_s *)key; + stc_app_value_s *app_value = (stc_app_value_s *)value; + classid_bytes_context_s *context = (classid_bytes_context_s *)data; + + if (context->counter->intend != NFACCT_COUNTER) + goto try_next_callback; + + __interface_counter_update(app_key, app_value, context); + + if (app_value->classid != context->counter->classid) + goto try_next_callback; + + __app_counter_update(app_key, app_value, context); try_next_callback: return FALSE; @@ -1157,6 +1217,31 @@ static void __add_rstns_for_application(gchar *app_id) app_id); } +static void __stc_monitor_add_application_by_interface(const char *app_id) +{ + stc_app_key_s app_key; + stc_app_value_s app_value; + + if (app_id == NULL) + return; + + memset(&app_key, 0, sizeof(stc_app_key_s)); + memset(&app_value, 0, sizeof(stc_app_value_s)); + + app_key.pkg_id = g_strdup(app_id); + app_key.app_id = g_strdup(app_id); + + app_value.type = STC_APP_TYPE_NONE; + app_value.processes = NULL; + app_value.counter.in_bytes = 0; + app_value.counter.out_bytes = 0; + + stc_monitor_application_add(app_key, app_value); + + FREE(app_key.pkg_id); + FREE(app_key.app_id); +} + stc_error_e stc_monitor_init(void) { stc_system_s *system = MALLOC0(stc_system_s, 1); @@ -1193,6 +1278,10 @@ stc_error_e stc_monitor_init(void) g_system = system; + __stc_monitor_add_application_by_interface(STC_TOTAL_DATACALL); + __stc_monitor_add_application_by_interface(STC_TOTAL_WIFI); + __stc_monitor_add_application_by_interface(STC_TOTAL_BLUETOOTH); + /* creating restriction rules tree */ __update_contr_cb(NULL); diff --git a/src/stc-manager.c b/src/stc-manager.c index 6af6b7b..a732b72 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include "stc-manager.h" #include "stc-emulator.h" #include "stc-statistics.h" @@ -89,6 +90,8 @@ gint32 main(gint32 argc, gchar *argv[]) GMainLoop *main_loop = NULL; gint32 ret = -1; + signal(SIGCHLD, SIG_IGN); + STC_LOGI("Smart Traffic Control Manager"); if (daemon(0, 0) != 0) -- 2.7.4 From 65144a1894772f20aea0721241933bda62fa398a Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Fri, 30 Jun 2017 10:47:29 +0530 Subject: [PATCH 13/16] Fixed restriction update logic, when user data limit of a restriction. Change-Id: I1784d6ad9485f2ebc21c26cc1e3941594b12ca04 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/database/tables/table-restrictions.c | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index cfd59dd..0470c0c 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.19 +Version: 0.0.20 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/database/tables/table-restrictions.c b/src/database/tables/table-restrictions.c index a5cb108..e1e7256 100755 --- a/src/database/tables/table-restrictions.c +++ b/src/database/tables/table-restrictions.c @@ -49,7 +49,6 @@ #define SELECT_RESTRICTION_ID "SELECT restriction_id FROM restrictions " \ " WHERE binpath = ? AND iftype = ? AND imsi = ? AND " \ - " data_limit = ? AND data_warn_limit = ? AND " \ " rst_state = ? AND roaming = ? AND ifname = ?" /* UPDATE statement */ @@ -476,11 +475,9 @@ stc_error_e __get_restriction_id(table_restrictions_info *info) DB_ACTION(sqlite3_bind_int(stmt, 2, info->iftype)); DB_ACTION(sqlite3_bind_text(stmt, 3, info->imsi ? info->imsi : "", -1, SQLITE_TRANSIENT)); - DB_ACTION(sqlite3_bind_int64(stmt, 4, info->data_limit)); - DB_ACTION(sqlite3_bind_int64(stmt, 5, info->data_warn_limit)); - DB_ACTION(sqlite3_bind_int(stmt, 6, info->rst_state)); - DB_ACTION(sqlite3_bind_int(stmt, 7, info->roaming)); - DB_ACTION(sqlite3_bind_text(stmt, 8, info->ifname ? info->ifname : "", + DB_ACTION(sqlite3_bind_int(stmt, 4, info->rst_state)); + DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming)); + DB_ACTION(sqlite3_bind_text(stmt, 6, info->ifname ? info->ifname : "", -1, SQLITE_TRANSIENT)); rc = sqlite3_step(stmt); @@ -514,6 +511,7 @@ stc_error_e table_restrictions_update(table_restrictions_info *info) goto handle_error; } + __get_restriction_id(info); if (info->restriction_id) stmt = update_net_restrictions; @@ -530,7 +528,7 @@ stc_error_e table_restrictions_update(table_restrictions_info *info) DB_ACTION(sqlite3_bind_int64(stmt, 8, info->data_warn_limit)); if (info->restriction_id) - DB_ACTION(sqlite3_bind_int64(stmt, 11, info->restriction_id)); + DB_ACTION(sqlite3_bind_int64(stmt, 9, info->restriction_id)); if (sqlite3_step(stmt) != SQLITE_DONE) { STC_LOGE("Failed to set network restriction: %s\n", -- 2.7.4 From 0e4036f2ae7a28b65e7341eb67b3275ac15dab9a Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Mon, 3 Jul 2017 15:59:26 +0900 Subject: [PATCH 14/16] Called appropriately waitpid() function to wait for the child process to terminate. Change-Id: I01778a0270026c0779f3484d31870278a7cd4a58 Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- src/helper/helper-nfacct-rule.c | 19 ++++++++++--------- src/stc-manager.c | 2 -- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 0470c0c..7801d1c 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.20 +Version: 0.0.21 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index 3dcde84..5a677f7 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -539,13 +539,17 @@ static stc_error_e exec_iface_cmd(const char *pattern, const char *cmd, ret_value_msg_if(ret > sizeof(block_buf), STC_ERROR_FAIL, "Not enough buffer"); exec_iptables_cmd(block_buf, pid); + wait_for_rule_cmd(*pid); /* ip6tables rule */ ret = snprintf(block_buf, sizeof(block_buf), pattern, IP6TABLES, cmd, chain, iftype_name, nfacct, jump); ret_value_msg_if(ret > sizeof(block_buf), STC_ERROR_FAIL, "Not enough buffer"); - return exec_ip6tables_cmd(block_buf, pid); + ret = exec_ip6tables_cmd(block_buf, pid); + wait_for_rule_cmd(*pid); + + return ret; } static stc_error_e exec_app_cmd(const char *pattern, const char *cmd, @@ -564,13 +568,17 @@ static stc_error_e exec_app_cmd(const char *pattern, const char *cmd, ret_value_msg_if(ret > sizeof(block_buf), STC_ERROR_FAIL, "Not enough buffer"); exec_iptables_cmd(block_buf, pid); + wait_for_rule_cmd(*pid); /* ip6tables rules */ ret = snprintf(block_buf, sizeof(block_buf), pattern, IP6TABLES, cmd, iftype_name, classid, nfacct, jump); ret_value_msg_if(ret > sizeof(block_buf), STC_ERROR_FAIL, "Not enough buffer"); - return exec_ip6tables_cmd(block_buf, pid); + ret = exec_ip6tables_cmd(block_buf, pid); + wait_for_rule_cmd(*pid); + + return ret; } static char *get_iptables_cmd(const nfacct_rule_action action) @@ -661,7 +669,6 @@ static stc_error_e produce_app_rule(nfacct_rule_s *rule, * set new counter with that value, but it's minor issue, * due it's not clear when actual counters was stored, * and based on which value settings made such decition */ - wait_for_rule_cmd(pid); rule->iptables_rule = nfacct_send_del; set_finalize_flag(rule); nfacct_send_get(rule); @@ -700,7 +707,6 @@ static stc_error_e produce_app_rule(nfacct_rule_s *rule, " traffic, for classid %u, cmd %s, j %s", rule->classid, set_cmd, jump_cmd); if (action == NFACCT_ACTION_DELETE) { - wait_for_rule_cmd(pid); rule->iptables_rule = nfacct_send_del; /* not effective, it's better to replace * set_finalize_flag by set_property, @@ -768,7 +774,6 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule, if (rule->intend == NFACCT_WARN || rule->intend == NFACCT_BLOCK) { /* RULE_IFACE_OUT is not a misprint here */ - wait_for_rule_cmd(pid); ret = exec_iface_cmd(RULE_IFACE_IN, set_cmd, FORWARD_RULE, nfacct_buf, jump_cmd, choose_iftype_name(rule), &pid); @@ -780,7 +785,6 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule, /* tethering */ if (action == NFACCT_ACTION_DELETE) { - wait_for_rule_cmd(pid); rule->iptables_rule = nfacct_send_del; set_finalize_flag(rule); nfacct_send_get(rule); @@ -815,7 +819,6 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule, ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0, STC_ERROR_FAIL, "Not enough buffer"); - wait_for_rule_cmd(pid); ret = exec_iface_cmd(RULE_IFACE_OUT, set_cmd, OUT_RULE, nfacct_buf, jump_cmd, choose_iftype_name(rule), &pid); @@ -826,7 +829,6 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule, /* for tethering */ if (rule->intend == NFACCT_WARN || rule->intend == NFACCT_BLOCK) { - wait_for_rule_cmd(pid); ret = exec_iface_cmd(RULE_IFACE_OUT, set_cmd, FORWARD_RULE, nfacct_buf, jump_cmd, choose_iftype_name(rule), &pid); @@ -838,7 +840,6 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule, /* tethering */ if (action == NFACCT_ACTION_DELETE) { - wait_for_rule_cmd(pid); rule->iptables_rule = nfacct_send_del; set_finalize_flag(rule); nfacct_send_get(rule); diff --git a/src/stc-manager.c b/src/stc-manager.c index 1f856ef..dc923ff 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -94,8 +94,6 @@ gint32 main(gint32 argc, gchar *argv[]) GMainLoop *main_loop = NULL; gint32 ret = -1; - signal(SIGCHLD, SIG_IGN); - STC_LOGI("Smart Traffic Control Manager"); if (daemon(0, 0) != 0) -- 2.7.4 From dbba5d72cbf60aa30b9f54e3ef558a89206d9f41 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Mon, 3 Jul 2017 16:59:16 +0900 Subject: [PATCH 15/16] Removed dbus activation Change-Id: I14f1ffb8979540b4c5829f1e071647adb36cea56 Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- resources/dbus/net.stc.service | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 7801d1c..4cbbf5e 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.21 +Version: 0.0.22 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/resources/dbus/net.stc.service b/resources/dbus/net.stc.service index 68d4a0b..13f4097 100755 --- a/resources/dbus/net.stc.service +++ b/resources/dbus/net.stc.service @@ -1,4 +1,5 @@ [D-BUS Service] Name=net.stc -Exec=/usr/bin/stc-manager +Exec=/bin/false User=root +SystemdService=stc-manager.service -- 2.7.4 From d662fe34a1c0a678ac4dd1e83f0d9361f539744d Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Mon, 3 Jul 2017 16:06:06 +0530 Subject: [PATCH 16/16] Corrected and optimized usage of fork and waitpid. The patch fixes below things:- 1. Do not call fork when arguments not present. 2. Do not call waitpid when fork returned pid as -1 from fork. 3. Corrected wrong english usage in function name by changing name of is_rule_exists => is_rule_present. Change-Id: Ic42233a846df91cc1b7871a52b830fd59433a970 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/helper/helper-nfacct-rule.c | 46 +++++++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 4cbbf5e..292e5c5 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.22 +Version: 0.0.23 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index 5a677f7..b46b3a4 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -390,10 +390,11 @@ static void wait_for_rule_cmd(pid_t pid) pid_t ret_pid; char buf[BUF_SIZE_FOR_ERR] = { 0 }; - if (!pid) { + if (!pid || pid == -1) { STC_LOGD("no need to wait"); return; } + ret_pid = waitpid(pid, &status, 0); if (ret_pid < 0) STC_LOGD("can't wait for a pid %d %d %s", pid, status, @@ -409,7 +410,7 @@ static char* get_cmd_pos(const char *cmd_buf) return cmd_pos; } -static bool is_rule_exists(const char *cmd_buf) +static bool is_rule_present(const char *cmd_buf) { size_t buf_len; char *exec_buf; @@ -436,12 +437,16 @@ static bool is_rule_exists(const char *cmd_buf) stc_error_e exec_iptables_cmd(const char *cmd_buf, pid_t *cmd_pid) { + const size_t args_number = get_args_number(cmd_buf); + *cmd_pid = 0; + + ret_value_msg_if(args_number == 0, STC_ERROR_FAIL, "no arguments"); + pid_t pid = fork(); if (pid == 0) { char *cmd; unsigned int i; - const size_t args_number = get_args_number(cmd_buf); char *args[args_number + 2]; int ret; char *save_ptr = NULL; @@ -450,15 +455,18 @@ stc_error_e exec_iptables_cmd(const char *cmd_buf, pid_t *cmd_pid) STC_LOGD("executing iptables cmd %s in forked process", cmd_buf); - ret_value_msg_if(args_number == 0, STC_ERROR_FAIL, "no arguments"); - - if (is_rule_exists(cmd_buf)) { - STC_LOGD("Rule %s already exists", cmd_buf); + if (is_rule_present(cmd_buf)) { + STC_LOGD("Rule %s already present", cmd_buf); exit(0); } + args[0] = "iptables"; cmd = strtok_r((char *)cmd_buf, " ", &save_ptr); - ret_value_msg_if(cmd == NULL, STC_ERROR_FAIL, "no arguments"); + if (cmd == NULL) { + STC_LOGE("no arguments"); + exit(-EINVAL); + } + for (i = 1; i <= args_number; ++i) args[i] = strtok_r(NULL, " ", &save_ptr); @@ -467,7 +475,8 @@ stc_error_e exec_iptables_cmd(const char *cmd_buf, pid_t *cmd_pid) ret = execv(cmd, args); if (ret) STC_LOGE("Can't execute %s: %s", - cmd_buf, strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); + cmd_buf, strerror_r(errno, buf, + BUF_SIZE_FOR_ERR)); exit(ret); } @@ -477,12 +486,16 @@ stc_error_e exec_iptables_cmd(const char *cmd_buf, pid_t *cmd_pid) stc_error_e exec_ip6tables_cmd(const char *cmd_buf, pid_t *cmd_pid) { + const size_t args_number = get_args_number(cmd_buf); + *cmd_pid = 0; + + ret_value_msg_if(args_number == 0, STC_ERROR_FAIL, "no arguments"); + pid_t pid = fork(); if (pid == 0) { char *cmd; unsigned int i; - const size_t args_number = get_args_number(cmd_buf); char *args[args_number + 2]; int ret; char *save_ptr = NULL; @@ -491,15 +504,18 @@ stc_error_e exec_ip6tables_cmd(const char *cmd_buf, pid_t *cmd_pid) STC_LOGD("executing ip6tables cmd %s in forked process", cmd_buf); - ret_value_msg_if(args_number == 0, STC_ERROR_FAIL, "no arguments"); - - if (is_rule_exists(cmd_buf)) { - STC_LOGD("Rule %s already exists", cmd_buf); + if (is_rule_present(cmd_buf)) { + STC_LOGD("Rule %s already present", cmd_buf); exit(0); } + args[0] = "ip6tables"; cmd = strtok_r((char *)cmd_buf, " ", &save_ptr); - ret_value_msg_if(cmd == NULL, STC_ERROR_FAIL, "no arguments"); + if (cmd == NULL) { + STC_LOGE("no arguments"); + exit(-EINVAL); + } + for (i = 1; i <= args_number; ++i) args[i] = strtok_r(NULL, " ", &save_ptr); -- 2.7.4