From 982b58fcb98e6bfd415a7dc73454e222a971f297 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Mon, 16 Oct 2017 11:44:42 +0530 Subject: [PATCH 01/16] [Fix] Memory leak while fetching data from config file Change-Id: I2126b99837fb53c9a504ba47580c2fcfba5b5fd7 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/stc-manager-util.c | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index cf69262..9500d61 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.36 +Version: 0.0.37 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/stc-manager-util.c b/src/stc-manager-util.c index 1c97e81..17f9d83 100755 --- a/src/stc-manager-util.c +++ b/src/stc-manager-util.c @@ -74,6 +74,7 @@ gboolean stc_util_get_config_bool(char *key) { char path[MAX_PATH_LENGTH]; GKeyFile *keyfile; + gboolean value; snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG); @@ -81,13 +82,17 @@ gboolean stc_util_get_config_bool(char *key) if (!keyfile) keyfile = g_key_file_new(); - return g_key_file_get_boolean(keyfile, path, key, NULL); + value = g_key_file_get_boolean(keyfile, path, key, NULL); + g_key_file_free(keyfile); + + return value; } gchar * stc_util_get_config_str(char *key) { char path[MAX_PATH_LENGTH]; GKeyFile *keyfile; + gchar *value; snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG); @@ -95,13 +100,17 @@ gchar * stc_util_get_config_str(char *key) if (!keyfile) keyfile = g_key_file_new(); - return g_key_file_get_string(keyfile, path, key, NULL); + value = g_key_file_get_string(keyfile, path, key, NULL); + g_key_file_free(keyfile); + + return value; } int stc_util_get_config_int(char *key) { char path[MAX_PATH_LENGTH]; GKeyFile *keyfile; + gint value; snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG); @@ -109,7 +118,11 @@ int stc_util_get_config_int(char *key) if (!keyfile) keyfile = g_key_file_new(); - return g_key_file_get_integer(keyfile, path, key, NULL); + + value = g_key_file_get_integer(keyfile, path, key, NULL); + g_key_file_free(keyfile); + + return value; } API void stc_util_set_debuglog(int debuglog) -- 2.7.4 From 9a2652e780ee9483a15aa35c86c2c60fa7040418 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Mon, 16 Oct 2017 14:46:50 +0530 Subject: [PATCH 02/16] [nfacct-rule] Use heap instead of stack, to aviod large stack usage issue. Change-Id: I5252955200603bc1c21b3b36b150e174447ce20c Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/helper/helper-nfacct-rule.c | 68 +++++++++++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 9500d61..d0d5348 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.37 +Version: 0.0.38 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 ca3a7a9..952e414 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -131,38 +131,53 @@ static stc_error_e send_nfacct_request(int sock, struct genl *req) static stc_error_e nfacct_send_new(nfacct_rule_s *counter) { - struct genl req; + int ret = STC_ERROR_NONE; + struct genl *req = MALLOC0(struct genl, 1); + if (req == NULL) { + STC_LOGE("Failed allocate memory to genl request message"); + return STC_ERROR_OUT_OF_MEMORY; + } - prepare_netlink_msg(&req, NFNL_MSG_ACCT_NEW, NLM_F_CREATE | NLM_F_ACK); - add_string_attr(&req, counter->name, NFACCT_NAME); + prepare_netlink_msg(req, NFNL_MSG_ACCT_NEW, NLM_F_CREATE | NLM_F_ACK); + add_string_attr(req, counter->name, NFACCT_NAME); if (STC_DEBUG_LOG) STC_LOGD("counter name %s", counter->name); /* padding */ - add_uint64_attr(&req, 0, NFACCT_PKTS); - add_uint64_attr(&req, 0, NFACCT_BYTES); + add_uint64_attr(req, 0, NFACCT_PKTS); + add_uint64_attr(req, 0, NFACCT_BYTES); if (counter->quota) { STC_LOGD("quota bytes %"PRId64, counter->quota); - add_uint32_attr(&req, htobe32(NFACCT_F_QUOTA_BYTES), + add_uint32_attr(req, htobe32(NFACCT_F_QUOTA_BYTES), NFACCT_FLAGS); - add_uint64_attr(&req, htobe64(counter->quota), NFACCT_QUOTA); + add_uint64_attr(req, htobe64(counter->quota), NFACCT_QUOTA); } - return send_nfacct_request(counter->carg->sock, &req); + ret = send_nfacct_request(counter->carg->sock, req); + FREE(req); + return ret; } stc_error_e nfacct_send_del(nfacct_rule_s *counter) { - struct genl req; + int ret = STC_ERROR_NONE; + struct genl *req = MALLOC0(struct genl, 1); + if (req == NULL) { + STC_LOGE("Failed allocate memory to genl request message"); + return STC_ERROR_OUT_OF_MEMORY; + } if (STC_DEBUG_LOG) STC_LOGD("send remove request for %s", counter->name); - prepare_netlink_msg(&req, NFNL_MSG_ACCT_DEL, NLM_F_ACK); - add_string_attr(&req, counter->name, NFACCT_NAME); - return send_nfacct_request(counter->carg->sock, &req); + prepare_netlink_msg(req, NFNL_MSG_ACCT_DEL, NLM_F_ACK); + add_string_attr(req, counter->name, NFACCT_NAME); + + ret = send_nfacct_request(counter->carg->sock, req); + FREE(req); + return ret; } #define NFACCT_F_QUOTAS (NFACCT_F_QUOTA_BYTES | NFACCT_F_QUOTA_PKTS) @@ -171,22 +186,29 @@ static stc_error_e internal_nfacct_send_get(struct counter_arg *carg, const char *name, int mask, int filter) { - struct genl req; + int ret = STC_ERROR_NONE; struct nlattr *na; int flag = !name ? NLM_F_DUMP : 0; - prepare_netlink_msg(&req, get_type, - flag); + struct genl *req = MALLOC0(struct genl, 1); + if (req == NULL) { + STC_LOGE("Failed allocate memory to genl request message"); + return STC_ERROR_OUT_OF_MEMORY; + } + + prepare_netlink_msg(req, get_type, flag); /* due we don't get counter with quota any where else, * here we will request just counters by default */ if (name) - add_string_attr(&req, name, NFACCT_NAME); - - na = start_nest_attr(&req, NFACCT_FILTER); - add_uint32_attr(&req, htonl(mask), - NFACCT_FILTER_ATTR_MASK); - add_uint32_attr(&req, htonl(filter), NFACCT_FILTER_ATTR_VALUE); - end_nest_attr(&req, na); - return send_nfacct_request(carg->sock, &req); + add_string_attr(req, name, NFACCT_NAME); + + na = start_nest_attr(req, NFACCT_FILTER); + add_uint32_attr(req, htonl(mask), NFACCT_FILTER_ATTR_MASK); + add_uint32_attr(req, htonl(filter), NFACCT_FILTER_ATTR_VALUE); + end_nest_attr(req, na); + + ret = send_nfacct_request(carg->sock, req); + FREE(req); + return ret; } stc_error_e nfacct_send_get_counters(struct counter_arg *carg, const char *name) -- 2.7.4 From e2644618210b9d01767e94e38bb92cf6415018e9 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Tue, 17 Oct 2017 08:47:50 +0530 Subject: [PATCH 03/16] Use heap instead of stack, to aviod large stack usage issue in __process_contr_reply() Change-Id: I447c0cca6658995f355693f0923c9395cabe0468 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/monitor/stc-monitor.c | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index d0d5348..c1914df 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.38 +Version: 0.0.39 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 2fde42c..8b18657 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -1123,12 +1123,11 @@ static gboolean __process_contr_reply(GIOChannel *source, gpointer user_data) { int sock = g_io_channel_unix_get_fd(source); - struct genl ans; + struct genl *ans; int ret; stc_s *stc = stc_get_manager(); - if ((condition & G_IO_ERR) || - (condition & G_IO_HUP) || + if ((condition & G_IO_ERR) || (condition & G_IO_HUP) || (condition & G_IO_NVAL)) { /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */ @@ -1140,23 +1139,29 @@ static gboolean __process_contr_reply(GIOChannel *source, return FALSE; } + ans = MALLOC0(struct genl, 1); + if (ans == NULL) { + STC_LOGE("Failed allocate memory to genl reply message"); + return TRUE; + } + if (stc == NULL) { STC_LOGE("Can't get stc data"); goto out; } - ret = read_netlink(sock, - &ans, sizeof(struct genl)); + ret = read_netlink(sock, ans, sizeof(struct genl)); /* STC_LOGD("Counter data received ret [%d]", ret); */ if (ret == 0) goto out; stc->carg->ans_len = ret; - __process_network_counter(&ans, stc->carg); + __process_network_counter(ans, stc->carg); g_idle_add(__flush_apps_stats_to_database, NULL); g_idle_add(__flush_rstns_counter_to_database, NULL); out: + FREE(ans); return TRUE; } -- 2.7.4 From fe9d6335ed936bf8995b34efe37ff36dacbef089 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Fri, 20 Oct 2017 13:00:04 +0900 Subject: [PATCH 04/16] Removed inotify helper Change-Id: I4de98b665307d02a75738dd8fd372eb85bfffaff Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- src/helper/helper-inotify.c | 206 -------------------------------------------- src/helper/helper-inotify.h | 32 ------- src/stc-manager.c | 26 ------ 4 files changed, 1 insertion(+), 265 deletions(-) delete mode 100755 src/helper/helper-inotify.c delete mode 100755 src/helper/helper-inotify.h diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index c1914df..51f366d 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.39 +Version: 0.0.40 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-inotify.c b/src/helper/helper-inotify.c deleted file mode 100755 index 71e30e2..0000000 --- a/src/helper/helper-inotify.c +++ /dev/null @@ -1,206 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include "helper-inotify.h" -#include "stc-manager-util.h" - -typedef struct { - GIOChannel *channel; - uint watch; - int wd; - - inotify_event_cb cb; -} stc_inotify_s; - -static GHashTable *g_inotify_hash; - -static gboolean __inotify_data(GIOChannel *channel, GIOCondition cond, - gpointer user_data) -{ - stc_inotify_s *inotify = user_data; - char buffer[sizeof(struct inotify_event) + NAME_MAX + 1]; - char *next_event; - gsize bytes_read; - GIOStatus status; - - if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) { - inotify->watch = 0; - return FALSE; - } - - status = g_io_channel_read_chars(channel, buffer, - sizeof(buffer), &bytes_read, NULL); - - switch (status) { - case G_IO_STATUS_NORMAL: - break; - case G_IO_STATUS_AGAIN: - return TRUE; - default: - STC_LOGE("Reading from inotify channel failed"); - inotify->watch = 0; - return FALSE; - } - - next_event = buffer; - - while (bytes_read > 0) { - struct inotify_event *event; - gchar *ident; - gsize len; - inotify_event_cb callback = inotify->cb; - - event = (struct inotify_event *) next_event; - if (event->len) - ident = next_event + sizeof(struct inotify_event); - else - ident = NULL; - - len = sizeof(struct inotify_event) + event->len; - if (len > bytes_read) - break; - - next_event += len; - bytes_read -= len; - - (*callback)(event, ident); - } - - return TRUE; -} - -static void __remove_watch(stc_inotify_s *inotify) -{ - int fd; - - if (!inotify->channel) - return; - - if (inotify->watch > 0) - g_source_remove(inotify->watch); - - fd = g_io_channel_unix_get_fd(inotify->channel); - - if (inotify->wd >= 0) - inotify_rm_watch(fd, inotify->wd); - - g_io_channel_unref(inotify->channel); -} - -static int __create_watch(const char *path, stc_inotify_s *inotify) -{ - int fd; - - STC_LOGD("Add directory watch for [%s]", path); - - fd = inotify_init(); - if (fd < 0) - return -EIO; - - inotify->wd = inotify_add_watch(fd, path, - IN_MODIFY | IN_CREATE | IN_DELETE | - IN_MOVED_TO | IN_MOVED_FROM); - if (inotify->wd < 0) { - STC_LOGE("Creation of [%s] watch failed", path); - close(fd); - return -EIO; - } - - inotify->channel = g_io_channel_unix_new(fd); - if (!inotify->channel) { - STC_LOGE("Creation of inotify channel failed"); - inotify_rm_watch(fd, inotify->wd); - inotify->wd = 0; - - close(fd); - return -EIO; - } - - g_io_channel_set_close_on_unref(inotify->channel, TRUE); - g_io_channel_set_encoding(inotify->channel, NULL, NULL); - g_io_channel_set_buffered(inotify->channel, FALSE); - - inotify->watch = g_io_add_watch(inotify->channel, - G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR, - __inotify_data, inotify); - - return 0; -} - -static void __inotify_destroy(gpointer user_data) -{ - stc_inotify_s *inotify = user_data; - - __remove_watch(inotify); - FREE(inotify); -} - -int inotify_register(const char *path, inotify_event_cb callback) -{ - int err; - stc_inotify_s *inotify; - - if (!callback) - return -EINVAL; - - inotify = g_hash_table_lookup(g_inotify_hash, path); - if (inotify) - goto update; - - inotify = g_try_new0(stc_inotify_s, 1); - if (!inotify) - return -ENOMEM; - - inotify->wd = -1; - - err = __create_watch(path, inotify); - if (err < 0) { - FREE(inotify); - return err; - } - - g_hash_table_replace(g_inotify_hash, g_strdup(path), inotify); - -update: - inotify->cb = callback; - - return 0; -} - -void inotify_deregister(const char *path) -{ - stc_inotify_s *inotify; - - inotify = g_hash_table_lookup(g_inotify_hash, path); - if (!inotify) - return; - - g_hash_table_remove(g_inotify_hash, path); -} - -int inotify_initialize(void) -{ - g_inotify_hash = g_hash_table_new_full(g_str_hash, g_str_equal, - g_free, __inotify_destroy); - return 0; -} - -void inotify_deinitialize(void) -{ - g_hash_table_destroy(g_inotify_hash); -} diff --git a/src/helper/helper-inotify.h b/src/helper/helper-inotify.h deleted file mode 100755 index 1fe9d66..0000000 --- a/src/helper/helper-inotify.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __STC_HELPER_INOTIFY_H__ -#define __STC_HELPER_INOTIFY_H__ - -#include - -struct inotify_event; -typedef void (* inotify_event_cb) (struct inotify_event *event, - const char *ident); - -int inotify_register(const char *path, inotify_event_cb callback); -void inotify_deregister(const char *path); - -int inotify_initialize(void); -void inotify_deinitialize(void); - -#endif /*__STC_HELPER_INOTIFY_H__*/ diff --git a/src/stc-manager.c b/src/stc-manager.c index d6e8521..fb5d75a 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -23,7 +23,6 @@ #include "table-restrictions.h" #include "helper-cgroup.h" #include "helper-nfacct-rule.h" -#include "helper-inotify.h" #include "stc-monitor.h" #include "stc-manager-plugin.h" #include "stc-app-lifecycle.h" @@ -44,25 +43,6 @@ static gboolean __validate_ident(const char *ident) return TRUE; } -static void __stc_inotify_handler(struct inotify_event *event, const char *ident) -{ - if (!ident) - return; - - if (!__validate_ident(ident)) { - STC_LOGE("Invalid ident [%s]", ident); - return; - } - - if (event->mask & IN_MODIFY) { - if (!g_strcmp0(ident, INFO_CONFIG)) { - int debug = 0; - debug = stc_util_get_config_int(INFO_DEBUGLOG); - stc_util_set_debuglog(debug); - } - } -} - static void __stc_manager_deinit(void) { __STC_LOG_FUNC_ENTER__; @@ -80,9 +60,6 @@ static void __stc_manager_deinit(void) stc_app_lifecycle_monitor_deinit(); stc_manager_plugin_deinit(); - inotify_deregister(INFO_STORAGE_DIR); - inotify_deinitialize(); - STC_LOGI("stc manager deinitialized"); FREE(g_stc); __STC_LOG_FUNC_EXIT__; @@ -103,9 +80,6 @@ static stc_s *__stc_manager_init(void) stc_util_initialize_config(); - inotify_initialize(); - inotify_register(INFO_STORAGE_DIR, __stc_inotify_handler); - cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT); EXEC(STC_ERROR_NONE, stc_db_initialize()); -- 2.7.4 From 71f7bebb11e3b1c00959d20377225b4af46c9e36 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Wed, 25 Oct 2017 11:16:07 +0530 Subject: [PATCH 05/16] Fetching /proc//status entry after validation. Description: This patch adds validation check before storing status entries and assignes proper index for storing value. In TM1 reference device kernel makes below entries in status file. proc entry is like below:- sh-3.2# cat /proc/373/status Name: stc-manager State: S (sleeping) Tgid: 373 Pid: 373 PPid: 1 TracerPid: 0 However in Odroid reference device, kernel makes below entries in status. sh-3.2# cat /proc/3404/status Name: stc-manager State: S (sleeping) Tgid: 3404 Ngid: 0 Pid: 3404 PPid: 1 TracerPid: 0 Odroid kernel has an extra entry "Ngid" which breaks current extraction logic. So added logic to match entry key before extracting value when reading status file. Change-Id: I393ed476919fc8c101a8ce6a11ab835230a39021 Signed-off-by: Nishant Chaprana --- include/stc-manager-util.h | 17 +++++++++++++---- packaging/stc-manager.spec | 2 +- src/helper/helper-procfs.c | 30 +++++++++++++++++++++++++++++- src/monitor/stc-app-lifecycle.c | 4 ++++ 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/include/stc-manager-util.h b/include/stc-manager-util.h index d504cf3..2275b85 100755 --- a/include/stc-manager-util.h +++ b/include/stc-manager-util.h @@ -339,14 +339,23 @@ static inline bool strstart_with(const char *str, const char *with) #define PROC_BUF_MAX 64 #define PROC_NAME_MAX 1024 -#define PROC_STATUS_CNT 6 +#define PROC_STATUS_CNT 7 #define PROC_STATUS_NAME 0 #define PROC_STATUS_STATE 1 #define PROC_STATUS_TGID 2 -#define PROC_STATUS_PID 3 -#define PROC_STATUS_PPID 4 -#define PROC_STATUS_TRACERPID 5 +#define PROC_STATUS_NGID 3 +#define PROC_STATUS_PID 4 +#define PROC_STATUS_PPID 5 +#define PROC_STATUS_TRACERPID 6 + +#define PROC_STATUS_NAME_STR "Name:" +#define PROC_STATUS_STATE_STR "State:" +#define PROC_STATUS_TGID_STR "Tgid:" +#define PROC_STATUS_NGID_STR "Ngid:" +#define PROC_STATUS_PID_STR "Pid:" +#define PROC_STATUS_PPID_STR "PPid:" +#define PROC_STATUS_TRACERPID_STR "TracerPid:" #define COMMA_DELIMETER "," diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 51f366d..b67e5ea 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.40 +Version: 0.0.41 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c index 168c34a..8710fa2 100755 --- a/src/helper/helper-procfs.c +++ b/src/helper/helper-procfs.c @@ -177,8 +177,10 @@ int proc_get_raw_cmdline(pid_t pid, char *buf, int len) int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX]) { unsigned int i; + unsigned int index = 0; char path[PROC_BUF_MAX]; char status_buf[PROC_BUF_MAX]; + bool updated[PROC_STATUS_CNT] = {FALSE, }; FILE *fp; snprintf(path, sizeof(path), "/proc/%d/status", pid); @@ -195,13 +197,39 @@ int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX]) return STC_ERROR_FAIL; } + if (!updated[PROC_STATUS_NAME] && strstr(status_buf, + PROC_STATUS_NAME_STR)) + index = PROC_STATUS_NAME; + else if (!updated[PROC_STATUS_STATE] && strstr(status_buf, + PROC_STATUS_STATE_STR)) + index = PROC_STATUS_STATE; + else if (!updated[PROC_STATUS_TGID] && strstr(status_buf, + PROC_STATUS_TGID_STR)) + index = PROC_STATUS_TGID; + else if (!updated[PROC_STATUS_NGID] && strstr(status_buf, + PROC_STATUS_NGID_STR)) + index = PROC_STATUS_NGID; + else if (!updated[PROC_STATUS_PID] && strstr(status_buf, + PROC_STATUS_PID_STR)) + index = PROC_STATUS_PID; + else if (!updated[PROC_STATUS_PPID] && strstr(status_buf, + PROC_STATUS_PPID_STR)) + index = PROC_STATUS_PPID; + else if (!updated[PROC_STATUS_TRACERPID] && strstr(status_buf, + PROC_STATUS_TRACERPID_STR)) + index = PROC_STATUS_TRACERPID; + else + continue; + token = strtok_r(status_buf, ":", &saveptr); if (token != NULL) { token = strtok_r(NULL, "\n", &saveptr); if (token != NULL) { while (isspace((unsigned char)*token)) token++; - strncpy(status[i], token, sizeof(status[i])); + strncpy(status[index], token, + sizeof(status[index])); + updated[index] = TRUE; } } } diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index eb18816..1cd8e9d 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -395,6 +395,8 @@ static void __process_event_fork(int tgid, int pid) char cmdline[PROC_NAME_MAX] = {0, }; char status[PROC_STATUS_CNT][PROC_BUF_MAX]; + memset(status, 0x0, sizeof(status)); + if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && STC_ERROR_NONE == proc_get_status(pid, status)) { @@ -443,6 +445,8 @@ static void __process_event_exec(int tgid, int pid) char cmdline[PROC_NAME_MAX] = {0, }; char status[PROC_STATUS_CNT][PROC_BUF_MAX]; + memset(status, 0x0, sizeof(status)); + if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && STC_ERROR_NONE == proc_get_status(pid, status)) { -- 2.7.4 From aadaee0ba3ac7ddc866bf9f3327bcd209e4b203a Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Fri, 1 Dec 2017 15:34:56 +0530 Subject: [PATCH 06/16] Replaces fork() and execv() calls with stc-iptables dbus method calls Change-Id: Iea3e6236e39c6747152e91965b785ceb6bc20023 Signed-off-by: Nishant Chaprana --- src/helper/helper-iptables.c | 231 ++++++++++++++++++++++++++++++++++ src/helper/helper-iptables.h | 40 ++++++ src/helper/helper-nfacct-rule.c | 271 +++++++++------------------------------- src/helper/helper-nfacct-rule.h | 1 + src/stc-manager.c | 2 + 5 files changed, 332 insertions(+), 213 deletions(-) create mode 100755 src/helper/helper-iptables.c create mode 100755 src/helper/helper-iptables.h diff --git a/src/helper/helper-iptables.c b/src/helper/helper-iptables.c new file mode 100755 index 0000000..42fa6f6 --- /dev/null +++ b/src/helper/helper-iptables.c @@ -0,0 +1,231 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "stc-manager-gdbus.h" +#include "helper-iptables.h" + +#define STC_IPTABLES_DBUS_SERVICE "net.stc.iptables" +#define STC_IPTABLES_DBUS_RULE_INTERFACE STC_IPTABLES_DBUS_SERVICE ".rule" +#define STC_IPTABLES_DBUS_CHAIN_INTERFACE STC_IPTABLES_DBUS_SERVICE ".chain" +#define STC_IPTABLES_DBUS_RULE_PATH "/net/stc/iptables/rule" +#define STC_IPTABLES_DBUS_CHAIN_PATH "/net/stc/iptables/chain" +#define STC_IPTABLES_DBUS_METHOD_IPT_ADD_CHAIN "IptAddChain" +#define STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_CHAIN "IptRemoveChain" +#define STC_IPTABLES_DBUS_METHOD_IP6T_ADD_CHAIN "Ip6tAddChain" +#define STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_CHAIN "Ip6tRemoveChain" +#define STC_IPTABLES_DBUS_METHOD_IPT_ADD_RULE "IptAddRule" +#define STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_RULE "IptRemoveRule" +#define STC_IPTABLES_DBUS_METHOD_IP6T_ADD_RULE "Ip6tAddRule" +#define STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_RULE "Ip6tRemoveRule" + +#define RULE_CHAIN "chain" +#define RULE_TYPE "type" +#define RULE_IFNAME "ifname" +#define RULE_CGROUP "cgroup" +#define RULE_NFACCT "nfacct" +#define RULE_TARGET "target" + +static void __add_rule_info_to_builder(GVariantBuilder *builder, + iptables_rule_s *rule) +{ + if (builder == NULL || rule == NULL) + return; + + g_variant_builder_add(builder, "{sv}", RULE_CHAIN, + g_variant_new_string(rule->chain)); + + g_variant_builder_add(builder, "{sv}", RULE_TYPE, + g_variant_new_uint32(rule->direction)); + + if (rule->ifname) + g_variant_builder_add(builder, "{sv}", RULE_IFNAME, + g_variant_new_string(rule->ifname)); + + if (rule->classid > 0) + g_variant_builder_add(builder, "{sv}", RULE_CGROUP, + g_variant_new_uint32(rule->classid)); + + if (rule->nfacct_name) + g_variant_builder_add(builder, "{sv}", RULE_NFACCT, + g_variant_new_string(rule->nfacct_name)); + + if (rule->target) + g_variant_builder_add(builder, "{sv}", RULE_TARGET, + g_variant_new_string(rule->target)); + +} + +static int __iptables_rule_add(GDBusConnection *connection, + iptables_rule_s *rule) +{ + int result = 0; + GVariantBuilder *builder = NULL; + GVariant *params = NULL; + GVariant *message = NULL; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + __add_rule_info_to_builder(builder, rule); + params = g_variant_new("(a{sv})", builder); + g_variant_builder_unref(builder); + + message = stc_manager_gdbus_call_sync(connection, + STC_IPTABLES_DBUS_SERVICE, + STC_IPTABLES_DBUS_RULE_PATH, + STC_IPTABLES_DBUS_RULE_INTERFACE, + STC_IPTABLES_DBUS_METHOD_IPT_ADD_RULE, + params); + + if (message == NULL) { + STC_LOGE("Failed to invoke dbus method"); + return STC_ERROR_FAIL; + } + + g_variant_get(message, "(i)", &result); + STC_LOGD("Successfully Add Rule [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +static int __iptables_rule_remove(GDBusConnection *connection, + iptables_rule_s *rule) +{ + int result = 0; + GVariantBuilder *builder = NULL; + GVariant *params = NULL; + GVariant *message = NULL; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + __add_rule_info_to_builder(builder, rule); + params = g_variant_new("(a{sv})", builder); + g_variant_builder_unref(builder); + + message = stc_manager_gdbus_call_sync(connection, + STC_IPTABLES_DBUS_SERVICE, + STC_IPTABLES_DBUS_RULE_PATH, + STC_IPTABLES_DBUS_RULE_INTERFACE, + STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_RULE, + params); + + if (message == NULL) { + STC_LOGE("Failed to invoke dbus method"); + return STC_ERROR_FAIL; + } + + g_variant_get(message, "(i)", &result); + STC_LOGD("Successfully Remove Rule [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +static int __ip6tables_rule_add(GDBusConnection *connection, + iptables_rule_s *rule) +{ + int result = 0; + GVariantBuilder *builder = NULL; + GVariant *params = NULL; + GVariant *message = NULL; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + __add_rule_info_to_builder(builder, rule); + params = g_variant_new("(a{sv})", builder); + g_variant_builder_unref(builder); + + message = stc_manager_gdbus_call_sync(connection, + STC_IPTABLES_DBUS_SERVICE, + STC_IPTABLES_DBUS_RULE_PATH, + STC_IPTABLES_DBUS_RULE_INTERFACE, + STC_IPTABLES_DBUS_METHOD_IP6T_ADD_RULE, + params); + + if (message == NULL) { + STC_LOGE("Failed to invoke dbus method"); + return STC_ERROR_FAIL; + } + + g_variant_get(message, "(i)", &result); + STC_LOGD("Successfully Add 6 Rule [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +static int __ip6tables_rule_remove(GDBusConnection *connection, + iptables_rule_s *rule) +{ + int result = 0; + GVariantBuilder *builder = NULL; + GVariant *params = NULL; + GVariant *message = NULL; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + __add_rule_info_to_builder(builder, rule); + params = g_variant_new("(a{sv})", builder); + g_variant_builder_unref(builder); + + message = stc_manager_gdbus_call_sync(connection, + STC_IPTABLES_DBUS_SERVICE, + STC_IPTABLES_DBUS_RULE_PATH, + STC_IPTABLES_DBUS_RULE_INTERFACE, + STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_RULE, + params); + + if (message == NULL) { + STC_LOGE("Failed to invoke dbus method"); + return STC_ERROR_FAIL; + } + + g_variant_get(message, "(i)", &result); + STC_LOGD("Successfully Remove 6 Rule [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +stc_error_e iptables_add(iptables_rule_s *rule) +{ + stc_error_e ret = STC_ERROR_NONE; + stc_s *stc = stc_get_manager(); + + if (!stc || !stc->connection) + return STC_ERROR_INVALID_PARAMETER; + + ret = __iptables_rule_add(stc->connection, rule); + if (ret != STC_ERROR_NONE) + goto done; + + ret = __ip6tables_rule_add(stc->connection, rule); +done: + return ret; +} + +stc_error_e iptables_remove(iptables_rule_s *rule) +{ + stc_error_e ret = STC_ERROR_NONE; + stc_s *stc = stc_get_manager(); + + if (!stc || !stc->connection) + return STC_ERROR_INVALID_PARAMETER; + + ret = __iptables_rule_remove(stc->connection, rule); + if (ret != STC_ERROR_NONE) + goto done; + + ret = __ip6tables_rule_remove(stc->connection, rule); +done: + return ret; +} diff --git a/src/helper/helper-iptables.h b/src/helper/helper-iptables.h new file mode 100755 index 0000000..fed7650 --- /dev/null +++ b/src/helper/helper-iptables.h @@ -0,0 +1,40 @@ +/* + * 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_HELPER_IPTABLES_H__ +#define __STC_HELPER_IPTABLES_H__ + +#include "stc-manager.h" +#include "stc-error.h" + +typedef enum { + IPTABLES_DIRECTION_IN, + IPTABLES_DIRECTION_OUT +} iptables_rule_direction_e; + +typedef struct { + char *chain; + char *ifname; + char *nfacct_name; + char *target; + iptables_rule_direction_e direction; + uint32_t classid; +} iptables_rule_s; + +stc_error_e iptables_add(iptables_rule_s *rule); +stc_error_e iptables_remove(iptables_rule_s *rule); + +#endif /*__STC_HELPER_IPTABLES_H__*/ diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index 952e414..5b2cde4 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -26,6 +26,7 @@ #include "counter.h" #include "helper-nfacct-rule.h" +#include "helper-iptables.h" #include "configure_stub.h" @@ -37,8 +38,8 @@ #define INSERT "-I" #define NFACCT_NAME_MOD " -m nfacct --nfacct-name %s" -#define REJECT_RULE " -j REJECT" -#define ACCEPT_RULE " -j ACCEPT" +#define REJECT_RULE "REJECT" +#define ACCEPT_RULE "ACCEPT" #define OUT_RULE "OUTPUT" #define IN_RULE "INPUT" #define FORWARD_RULE "FORWARD" @@ -394,192 +395,6 @@ netlink_create_command(struct netlink_serialization_params *params) return &command; } -static unsigned int get_args_number(const char *cmd_buf) -{ - char *str; - unsigned int count = 0; - - for (str = (char *)cmd_buf; *str != '\0'; ++str) { - if (*str == ' ') - ++count; - } - return count; -} - -static void wait_for_rule_cmd(pid_t pid) -{ - int status; - pid_t ret_pid; - - if (!pid || pid == -1) { - STC_LOGD("no need to wait"); - return; - } - - ret_pid = waitpid(pid, &status, 0); - if (ret_pid < 0) { - char buf[BUF_SIZE_FOR_ERR] = { 0 }; - STC_LOGD("can't wait for a pid %d %d %s", pid, status, - strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - } -} - -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; - char *args[args_number + 2]; - int ret; - char *save_ptr = NULL; - - if (STC_DEBUG_LOG) - STC_LOGD("executing iptables cmd %s in forked process", cmd_buf); - - args[0] = "iptables"; - cmd = strtok_r((char *)cmd_buf, " ", &save_ptr); - if (cmd == NULL) { - STC_LOGE("no arguments"); - exit(-EINVAL); - } - - for (i = 1; i <= args_number; ++i) - args[i] = strtok_r(NULL, " ", &save_ptr); - - args[i] = NULL; - - ret = execv(cmd, args); - if (ret) { - char buf[BUF_SIZE_FOR_ERR] = { 0 }; - STC_LOGE("Can't execute %s: %s", - cmd_buf, strerror_r(errno, buf, - BUF_SIZE_FOR_ERR)); - } - exit(ret); - } - - *cmd_pid = pid; - return STC_ERROR_NONE; -} - -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; - char *args[args_number + 2]; - int ret; - char *save_ptr = NULL; - - if (STC_DEBUG_LOG) - STC_LOGD("executing ip6tables cmd %s in forked process", cmd_buf); - - args[0] = "ip6tables"; - cmd = strtok_r((char *)cmd_buf, " ", &save_ptr); - if (cmd == NULL) { - STC_LOGE("no arguments"); - exit(-EINVAL); - } - - for (i = 1; i <= args_number; ++i) - args[i] = strtok_r(NULL, " ", &save_ptr); - - args[i] = NULL; - - ret = execv(cmd, args); - if (ret) { - char buf[BUF_SIZE_FOR_ERR] = { 0 }; - STC_LOGE("Can't execute %s: %s", - cmd_buf, strerror_r(errno, buf, - BUF_SIZE_FOR_ERR)); - } - exit(ret); - } - - *cmd_pid = pid; - return STC_ERROR_NONE; -} - -static char *choose_iftype_name(nfacct_rule_s *rule) -{ - return strlen(rule->ifname) != 0 ? rule->ifname : - get_iftype_name(rule->iftype); -} - -static stc_error_e exec_iface_cmd(const char *pattern, const char *cmd, - const char *chain, const char *nfacct, - const char *jump, char *iftype_name, - pid_t *pid, nfacct_rule_iptype iptype) -{ - char block_buf[MAX_PATH_LENGTH]; - int ret; - const char *iptables_type = IPTABLES; - - ret_value_msg_if(iftype_name == NULL, STC_ERROR_FAIL, - "Invalid network interface name argument"); - - if (iptype == NFACCT_TYPE_IPV6) - iptables_type = IP6TABLES; - - ret = snprintf(block_buf, sizeof(block_buf), pattern, iptables_type, - cmd, chain, iftype_name, nfacct, jump); - ret_value_msg_if(ret > sizeof(block_buf), STC_ERROR_FAIL, - "Not enough buffer"); - - if (iptype == NFACCT_TYPE_IPV6) - exec_ip6tables_cmd(block_buf, pid); - else - exec_iptables_cmd(block_buf, pid); - - wait_for_rule_cmd(*pid); - - return STC_ERROR_NONE; -} - -static stc_error_e exec_app_cmd(const char *pattern, const char *cmd, - const char *nfacct, const char *jump, - const uint32_t classid, char *iftype_name, - pid_t *pid, nfacct_rule_iptype iptype) -{ - char block_buf[MAX_PATH_LENGTH]; - int ret; - const char *iptables_type = IPTABLES; - - ret_value_msg_if(iftype_name == NULL, STC_ERROR_FAIL, - "Invalid network interface name argument"); - - if (iptype == NFACCT_TYPE_IPV6) - iptables_type = IP6TABLES; - - ret = snprintf(block_buf, sizeof(block_buf), pattern, iptables_type, - cmd, iftype_name, classid, nfacct, jump); - ret_value_msg_if(ret > sizeof(block_buf), STC_ERROR_FAIL, - "Not enough buffer"); - - if (iptype == NFACCT_TYPE_IPV6) - exec_ip6tables_cmd(block_buf, pid); - else - exec_iptables_cmd(block_buf, pid); - - wait_for_rule_cmd(*pid); - - return STC_ERROR_NONE; -} - static char *get_iptables_cmd(const nfacct_rule_action action) { if (action == NFACCT_ACTION_APPEND) @@ -598,6 +413,8 @@ static char *get_iptables_chain(const nfacct_rule_direction iotype) return IN_RULE; else if (iotype == NFACCT_COUNTER_OUT) return OUT_RULE; + else if (iotype == NFACCT_COUNTER_FORWARD) + return FORWARD_RULE; return ""; } @@ -612,6 +429,41 @@ static char *get_iptables_jump(const nfacct_rule_jump jump) return ""; } +static char *choose_iftype_name(nfacct_rule_s *rule) +{ + return strlen(rule->ifname) != 0 ? rule->ifname : + get_iftype_name(rule->iftype); +} + +static stc_error_e exec_iptables_cmd(nfacct_rule_s *rule) +{ + stc_error_e ret = STC_ERROR_NONE; + iptables_rule_s iptables_rule; + memset(&iptables_rule, 0, sizeof(iptables_rule_s)); + + iptables_rule.nfacct_name = g_strdup(rule->name); + iptables_rule.ifname = g_strdup(rule->ifname); + iptables_rule.target = g_strdup(get_iptables_jump(rule->jump)); + iptables_rule.chain = g_strdup(get_iptables_chain(rule->iotype)); + iptables_rule.classid = rule->classid; + iptables_rule.direction = (rule->iotype & NFACCT_COUNTER_IN) ? 0 : 1; + + if (rule->action == NFACCT_ACTION_DELETE) { + /* delete interface rule */ + ret = iptables_remove(&iptables_rule); + } else { + /* add interface rule */ + ret = iptables_add(&iptables_rule); + } + + g_free(iptables_rule.nfacct_name); + g_free(iptables_rule.ifname); + g_free(iptables_rule.target); + g_free(iptables_rule.chain); + + return ret; +} + static stc_error_e produce_app_rule(nfacct_rule_s *rule) { if (rule == NULL) @@ -622,7 +474,6 @@ static stc_error_e produce_app_rule(nfacct_rule_s *rule) char nfacct_buf[sizeof(NFACCT_NAME_MOD) + 3*MAX_DEC_SIZE(int) + 4]; stc_error_e ret = STC_ERROR_NONE; - pid_t pid = 0; /* income part */ if (rule->iotype & NFACCT_COUNTER_IN) { @@ -651,9 +502,7 @@ static stc_error_e produce_app_rule(nfacct_rule_s *rule) ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0, STC_ERROR_FAIL, "Not enought buffer"); - ret = exec_app_cmd(RULE_APP_IN, set_cmd, nfacct_buf, jump_cmd, - rule->classid, choose_iftype_name(rule), - &pid, rule->iptype); + ret = exec_iptables_cmd(rule); ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL, "Can't set conditional block for ingress" " traffic, for classid %u, cmd %s, j %s", @@ -696,13 +545,12 @@ static stc_error_e produce_app_rule(nfacct_rule_s *rule) ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0, STC_ERROR_FAIL, "Not enought buffer"); - ret = exec_app_cmd(RULE_APP_OUT, set_cmd, nfacct_buf, jump_cmd, - rule->classid, choose_iftype_name(rule), - &pid, rule->iptype); + ret = exec_iptables_cmd(rule); ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL, "Can't set conditional block for engress" " traffic, for classid %u, cmd %s, j %s", rule->classid, set_cmd, jump_cmd); + if (rule->action == NFACCT_ACTION_DELETE) { rule->iptables_rule = nfacct_send_del; /* not effective, it's better to replace @@ -729,7 +577,8 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule) char nfacct_buf[sizeof(NFACCT_NAME_MOD) + 3*MAX_DEC_SIZE(int) + 4]; stc_error_e ret; - pid_t pid = 0; + + rule->classid = 0; if (rule->iotype & NFACCT_COUNTER_IN) { /* income part */ @@ -756,11 +605,7 @@ 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 enought buffer"); - ret = exec_iface_cmd(RULE_IFACE_IN, set_cmd, - get_iptables_chain(rule->iotype), - nfacct_buf, jump_cmd, - choose_iftype_name(rule), &pid, - rule->iptype); + ret = exec_iptables_cmd(rule); ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL, "Can't set conditional block for ingress" " traffic, for iftype %d, cmd %s, j %s", @@ -770,10 +615,11 @@ 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 */ - ret = exec_iface_cmd(RULE_IFACE_IN, set_cmd, - FORWARD_RULE, nfacct_buf, jump_cmd, - choose_iftype_name(rule), &pid, - rule->iptype); + nfacct_rule_direction temp_iotype = rule->iotype; + + rule->iotype = NFACCT_COUNTER_FORWARD; + ret = exec_iptables_cmd(rule); + rule->iotype = temp_iotype; ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL, "Can't set forward rule for ingress " "traffic, for iftype %d, cmd %s, j %s", @@ -816,21 +662,20 @@ 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"); - ret = exec_iface_cmd(RULE_IFACE_OUT, set_cmd, OUT_RULE, - nfacct_buf, jump_cmd, - choose_iftype_name(rule), &pid, - rule->iptype); + ret = exec_iptables_cmd(rule); ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL, "Can't set conditional block for " "engress traffic, for iftype %d, cmd %s, j %s", rule->iftype, set_cmd, jump_cmd); + /* for tethering */ if (rule->intend == NFACCT_WARN || rule->intend == NFACCT_BLOCK) { - ret = exec_iface_cmd(RULE_IFACE_OUT, set_cmd, - FORWARD_RULE, nfacct_buf, jump_cmd, - choose_iftype_name(rule), &pid, - rule->iptype); + nfacct_rule_direction temp_iotype = rule->iotype; + + rule->iotype = NFACCT_COUNTER_OUT; + ret = exec_iptables_cmd(rule); + rule->iotype = temp_iotype; ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL, "Can't set forward rule for engress " "traffic, for iftype %d, cmd %s, j %s", diff --git a/src/helper/helper-nfacct-rule.h b/src/helper/helper-nfacct-rule.h index 89eb27c..88b33ad 100755 --- a/src/helper/helper-nfacct-rule.h +++ b/src/helper/helper-nfacct-rule.h @@ -31,6 +31,7 @@ typedef enum { NFACCT_COUNTER_UNKNOWN, NFACCT_COUNTER_IN = (1 << 1), NFACCT_COUNTER_OUT = (1 << 2), + NFACCT_COUNTER_FORWARD = (1 << 3), NFACCT_COUNTER_LAST_ELEM } nfacct_rule_direction; diff --git a/src/stc-manager.c b/src/stc-manager.c index fb5d75a..0e6eb8c 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -29,6 +29,7 @@ static stc_s *g_stc = NULL; +/* static gboolean __validate_ident(const char *ident) { unsigned int i; @@ -42,6 +43,7 @@ static gboolean __validate_ident(const char *ident) return TRUE; } +*/ static void __stc_manager_deinit(void) { -- 2.7.4 From 1ef4eafbdedb92720445e0a9e38912a0c46cabaf Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Fri, 8 Dec 2017 16:08:24 +0530 Subject: [PATCH 07/16] Creating seperate chains[STC_IN, STC_OUT, STC_FRWD] for STC Framework's rules. Change-Id: I74b9ce4d13fb9122c4e0ea05226a22a925d1a585 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/helper/helper-iptables.c | 333 ++++++++++++++++++++++++++++++++++++++-- src/helper/helper-iptables.h | 7 + src/helper/helper-nfacct-rule.c | 6 +- src/monitor/stc-monitor.c | 11 ++ src/stc-manager-gdbus.c | 2 + src/stc-manager.c | 2 + 7 files changed, 350 insertions(+), 13 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index b67e5ea..bd37172 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.41 +Version: 0.0.42 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-iptables.c b/src/helper/helper-iptables.c index 42fa6f6..96eacb8 100755 --- a/src/helper/helper-iptables.c +++ b/src/helper/helper-iptables.c @@ -17,17 +17,19 @@ #include "stc-manager-gdbus.h" #include "helper-iptables.h" -#define STC_IPTABLES_DBUS_SERVICE "net.stc.iptables" -#define STC_IPTABLES_DBUS_RULE_INTERFACE STC_IPTABLES_DBUS_SERVICE ".rule" -#define STC_IPTABLES_DBUS_CHAIN_INTERFACE STC_IPTABLES_DBUS_SERVICE ".chain" -#define STC_IPTABLES_DBUS_RULE_PATH "/net/stc/iptables/rule" -#define STC_IPTABLES_DBUS_CHAIN_PATH "/net/stc/iptables/chain" -#define STC_IPTABLES_DBUS_METHOD_IPT_ADD_CHAIN "IptAddChain" -#define STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_CHAIN "IptRemoveChain" +#define STC_IPTABLES_DBUS_SERVICE "net.stc.iptables" +#define STC_IPTABLES_DBUS_RULE_INTERFACE STC_IPTABLES_DBUS_SERVICE ".rule" +#define STC_IPTABLES_DBUS_CHAIN_INTERFACE STC_IPTABLES_DBUS_SERVICE ".chain" +#define STC_IPTABLES_DBUS_RULE_PATH "/net/stc/iptables/rule" +#define STC_IPTABLES_DBUS_CHAIN_PATH "/net/stc/iptables/chain" +#define STC_IPTABLES_DBUS_METHOD_IPT_ADD_CHAIN "IptAddChain" +#define STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_CHAIN "IptRemoveChain" +#define STC_IPTABLES_DBUS_METHOD_IPT_FLUSH_CHAIN "IptFlushChain" #define STC_IPTABLES_DBUS_METHOD_IP6T_ADD_CHAIN "Ip6tAddChain" #define STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_CHAIN "Ip6tRemoveChain" -#define STC_IPTABLES_DBUS_METHOD_IPT_ADD_RULE "IptAddRule" -#define STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_RULE "IptRemoveRule" +#define STC_IPTABLES_DBUS_METHOD_IP6T_FLUSH_CHAIN "Ip6tFlushChain" +#define STC_IPTABLES_DBUS_METHOD_IPT_ADD_RULE "IptAddRule" +#define STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_RULE "IptRemoveRule" #define STC_IPTABLES_DBUS_METHOD_IP6T_ADD_RULE "Ip6tAddRule" #define STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_RULE "Ip6tRemoveRule" @@ -196,6 +198,174 @@ static int __ip6tables_rule_remove(GDBusConnection *connection, return STC_ERROR_NONE; } +static int __iptables_add_chain(GDBusConnection *connection, + const char *chain) +{ + int result = 0; + GVariant *message = NULL; + + message = stc_manager_gdbus_call_sync(connection, + STC_IPTABLES_DBUS_SERVICE, + STC_IPTABLES_DBUS_CHAIN_PATH, + STC_IPTABLES_DBUS_CHAIN_INTERFACE, + STC_IPTABLES_DBUS_METHOD_IPT_ADD_CHAIN, + g_variant_new("(s)", chain)); + + if (message == NULL) { + STC_LOGE("Failed to invoke dbus method"); + return STC_ERROR_FAIL; + } + + g_variant_get(message, "(i)", &result); + STC_LOGD("Successfully added ipv4 chain [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +static int __ip6tables_add_chain(GDBusConnection *connection, + const char *chain) +{ + int result = 0; + GVariant *message = NULL; + + message = stc_manager_gdbus_call_sync(connection, + STC_IPTABLES_DBUS_SERVICE, + STC_IPTABLES_DBUS_CHAIN_PATH, + STC_IPTABLES_DBUS_CHAIN_INTERFACE, + STC_IPTABLES_DBUS_METHOD_IP6T_ADD_CHAIN, + g_variant_new("(s)", chain)); + + if (message == NULL) { + STC_LOGE("Failed to invoke dbus method"); + return STC_ERROR_FAIL; + } + + g_variant_get(message, "(i)", &result); + STC_LOGD("Successfully added ipv6 chain [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +static int __iptables_remove_chain(GDBusConnection *connection, + const char *chain) +{ + int result = 0; + GVariant *message = NULL; + + message = stc_manager_gdbus_call_sync(connection, + STC_IPTABLES_DBUS_SERVICE, + STC_IPTABLES_DBUS_CHAIN_PATH, + STC_IPTABLES_DBUS_CHAIN_INTERFACE, + STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_CHAIN, + g_variant_new("(s)", chain)); + + if (message == NULL) { + STC_LOGE("Failed to invoke dbus method"); + return STC_ERROR_FAIL; + } + + g_variant_get(message, "(i)", &result); + STC_LOGD("Successfully removed ipv4 chain [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +static int __ip6tables_remove_chain(GDBusConnection *connection, + const char *chain) +{ + int result = 0; + GVariant *message = NULL; + + message = stc_manager_gdbus_call_sync(connection, + STC_IPTABLES_DBUS_SERVICE, + STC_IPTABLES_DBUS_CHAIN_PATH, + STC_IPTABLES_DBUS_CHAIN_INTERFACE, + STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_CHAIN, + g_variant_new("(s)", chain)); + + if (message == NULL) { + STC_LOGE("Failed to invoke dbus method"); + return STC_ERROR_FAIL; + } + + g_variant_get(message, "(i)", &result); + STC_LOGD("Successfully removed ipv6 chain [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +static int __iptables_flush_chain(GDBusConnection *connection, + const char *chain) +{ + int result = 0; + GVariant *message = NULL; + + message = stc_manager_gdbus_call_sync(connection, + STC_IPTABLES_DBUS_SERVICE, + STC_IPTABLES_DBUS_CHAIN_PATH, + STC_IPTABLES_DBUS_CHAIN_INTERFACE, + STC_IPTABLES_DBUS_METHOD_IPT_FLUSH_CHAIN, + g_variant_new("(s)", chain)); + + if (message == NULL) { + STC_LOGE("Failed to invoke dbus method"); + return STC_ERROR_FAIL; + } + + g_variant_get(message, "(i)", &result); + STC_LOGD("Successfully flushed ipv4 chain [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +static int __ip6tables_flush_chain(GDBusConnection *connection, + const char *chain) +{ + int result = 0; + GVariant *message = NULL; + + message = stc_manager_gdbus_call_sync(connection, + STC_IPTABLES_DBUS_SERVICE, + STC_IPTABLES_DBUS_CHAIN_PATH, + STC_IPTABLES_DBUS_CHAIN_INTERFACE, + STC_IPTABLES_DBUS_METHOD_IP6T_FLUSH_CHAIN, + g_variant_new("(s)", chain)); + + if (message == NULL) { + STC_LOGE("Failed to invoke dbus method"); + return STC_ERROR_FAIL; + } + + g_variant_get(message, "(i)", &result); + STC_LOGD("Successfully flushed ipv6 chain [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +static int __iptables_add_chain_jump_rule(const char *chain, + const char *target) +{ + stc_error_e ret = STC_ERROR_NONE; + iptables_rule_s iptables_rule; + memset(&iptables_rule, 0, sizeof(iptables_rule_s)); + + iptables_rule.target = g_strdup(target); + iptables_rule.chain = g_strdup(chain); + + ret = iptables_add(&iptables_rule); + + g_free(iptables_rule.target); + g_free(iptables_rule.chain); + + return ret; +} + stc_error_e iptables_add(iptables_rule_s *rule) { stc_error_e ret = STC_ERROR_NONE; @@ -229,3 +399,148 @@ stc_error_e iptables_remove(iptables_rule_s *rule) done: return ret; } + +stc_error_e iptables_flush_chains(void) +{ + stc_error_e ret = STC_ERROR_NONE; + stc_s *stc = stc_get_manager(); + + if (!stc || !stc->connection) + return STC_ERROR_INVALID_PARAMETER; + + ret = __iptables_flush_chain(stc->connection, STC_IN_CHAIN); + if (ret != STC_ERROR_NONE) + goto done; + + ret = __iptables_flush_chain(stc->connection, STC_OUT_CHAIN); + if (ret != STC_ERROR_NONE) + goto done; + + ret = __iptables_flush_chain(stc->connection, STC_FRWD_CHAIN); + if (ret != STC_ERROR_NONE) + goto done; + + ret = __ip6tables_flush_chain(stc->connection, STC_IN_CHAIN); + if (ret != STC_ERROR_NONE) + goto done; + + ret = __ip6tables_flush_chain(stc->connection, STC_OUT_CHAIN); + if (ret != STC_ERROR_NONE) + goto done; + + ret = __ip6tables_flush_chain(stc->connection, STC_FRWD_CHAIN); +done: + return ret; +} + +stc_error_e iptables_init(void) +{ + __STC_LOG_FUNC_ENTER__; + + stc_error_e ret = STC_ERROR_NONE; + stc_s *stc = stc_get_manager(); + + if (!stc || !stc->connection) { + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_INVALID_PARAMETER; + } + + ret = __iptables_add_chain(stc->connection, STC_IN_CHAIN); + if (ret != STC_ERROR_NONE) { + __STC_LOG_FUNC_EXIT__; + goto done; + } + + ret = __iptables_add_chain(stc->connection, STC_OUT_CHAIN); + if (ret != STC_ERROR_NONE) { + __STC_LOG_FUNC_EXIT__; + goto done; + } + + ret = __iptables_add_chain(stc->connection, STC_FRWD_CHAIN); + if (ret != STC_ERROR_NONE) { + __STC_LOG_FUNC_EXIT__; + goto done; + } + + ret = __ip6tables_add_chain(stc->connection, STC_IN_CHAIN); + if (ret != STC_ERROR_NONE) { + __STC_LOG_FUNC_EXIT__; + goto done; + } + + ret = __ip6tables_add_chain(stc->connection, STC_OUT_CHAIN); + if (ret != STC_ERROR_NONE) { + __STC_LOG_FUNC_EXIT__; + goto done; + } + + ret = __ip6tables_add_chain(stc->connection, STC_FRWD_CHAIN); + if (ret != STC_ERROR_NONE) { + __STC_LOG_FUNC_EXIT__; + goto done; + } + + ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_CHAIN); + if (ret != STC_ERROR_NONE) { + __STC_LOG_FUNC_EXIT__; + goto done; + } + + ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_CHAIN); + if (ret != STC_ERROR_NONE) { + __STC_LOG_FUNC_EXIT__; + goto done; + } + + ret = __iptables_add_chain_jump_rule("FORWARD", STC_FRWD_CHAIN); +done: + return ret; +} + +stc_error_e iptables_deinit(void) +{ + __STC_LOG_FUNC_ENTER__; + + stc_error_e ret = STC_ERROR_NONE; + stc_s *stc = stc_get_manager(); + + if (!stc || !stc->connection) { + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_INVALID_PARAMETER; + } + + ret = __iptables_remove_chain(stc->connection, STC_IN_CHAIN); + if (ret != STC_ERROR_NONE) { + __STC_LOG_FUNC_EXIT__; + goto done; + } + + ret = __iptables_remove_chain(stc->connection, STC_OUT_CHAIN); + if (ret != STC_ERROR_NONE) { + __STC_LOG_FUNC_EXIT__; + goto done; + } + + ret = __iptables_remove_chain(stc->connection, STC_FRWD_CHAIN); + if (ret != STC_ERROR_NONE) { + __STC_LOG_FUNC_EXIT__; + goto done; + } + + ret = __ip6tables_remove_chain(stc->connection, STC_IN_CHAIN); + if (ret != STC_ERROR_NONE) { + __STC_LOG_FUNC_EXIT__; + goto done; + } + + ret = __ip6tables_remove_chain(stc->connection, STC_OUT_CHAIN); + if (ret != STC_ERROR_NONE) { + __STC_LOG_FUNC_EXIT__; + goto done; + } + + ret = __ip6tables_remove_chain(stc->connection, STC_FRWD_CHAIN); +done: + return ret; +} diff --git a/src/helper/helper-iptables.h b/src/helper/helper-iptables.h index fed7650..bdfedb1 100755 --- a/src/helper/helper-iptables.h +++ b/src/helper/helper-iptables.h @@ -20,6 +20,10 @@ #include "stc-manager.h" #include "stc-error.h" +#define STC_IN_CHAIN "STC_IN" +#define STC_OUT_CHAIN "STC_OUT" +#define STC_FRWD_CHAIN "STC_FRWD" + typedef enum { IPTABLES_DIRECTION_IN, IPTABLES_DIRECTION_OUT @@ -36,5 +40,8 @@ typedef struct { stc_error_e iptables_add(iptables_rule_s *rule); stc_error_e iptables_remove(iptables_rule_s *rule); +stc_error_e iptables_flush_chains(void); +stc_error_e iptables_init(void); +stc_error_e iptables_deinit(void); #endif /*__STC_HELPER_IPTABLES_H__*/ diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index 5b2cde4..374090a 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -410,11 +410,11 @@ static char *get_iptables_cmd(const nfacct_rule_action action) static char *get_iptables_chain(const nfacct_rule_direction iotype) { if (iotype == NFACCT_COUNTER_IN) - return IN_RULE; + return STC_IN_CHAIN; else if (iotype == NFACCT_COUNTER_OUT) - return OUT_RULE; + return STC_OUT_CHAIN; else if (iotype == NFACCT_COUNTER_FORWARD) - return FORWARD_RULE; + return STC_FRWD_CHAIN; return ""; } diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 8b18657..268c5cf 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -23,6 +23,7 @@ #include "helper-nfacct-rule.h" #include "helper-net-cls.h" #include "helper-cgroup.h" +#include "helper-iptables.h" #include "counter.h" #include "table-statistics.h" #include "table-counters.h" @@ -526,6 +527,10 @@ static void __process_restriction(enum traffic_restriction_type rst_type, char *default_ifname = stc_default_connection_get_ifname(); struct nfacct_rule counter; stc_s *stc = stc_get_manager(); + if (!stc) { + g_free(default_ifname); + return; + } if (!stc->carg) { stc->carg = MALLOC0(counter_arg_s, 1); @@ -568,6 +573,10 @@ static void __process_restriction(enum traffic_restriction_type rst_type, char *default_ifname = stc_default_connection_get_ifname(); struct nfacct_rule counter; stc_s *stc = stc_get_manager(); + if (!stc) { + g_free(default_ifname); + return; + } if (!stc->carg) { stc->carg = MALLOC0(counter_arg_s, 1); @@ -1870,6 +1879,8 @@ void stc_monitor_update_rstn_by_default_connection(void *data) g_tree_foreach(g_system->rstns, __remove_restriction, (gpointer)&old_connection); + + iptables_flush_chains(); } FREE(old_connection.path); diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index aee4c9e..bf556e6 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -20,6 +20,7 @@ #include "stc-default-connection.h" #include "stc-manager-plugin.h" #include "stc-app-lifecycle.h" +#include "helper-iptables.h" static gboolean __stc_manager_gdbus_statistics_init(stc_s *stc) { @@ -154,6 +155,7 @@ static void __stc_manager_gdbus_on_bus_acquired(GDBusConnection *connection, g_dbus_object_manager_server_set_connection(stc->obj_mgr, stc->connection); + iptables_init(); stc_default_connection_monitor_init(stc); stc_register_state_changed_cb(stc, stc_manager_app_status_changed, NULL); diff --git a/src/stc-manager.c b/src/stc-manager.c index 0e6eb8c..5e7ddd6 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -23,6 +23,7 @@ #include "table-restrictions.h" #include "helper-cgroup.h" #include "helper-nfacct-rule.h" +#include "helper-iptables.h" #include "stc-monitor.h" #include "stc-manager-plugin.h" #include "stc-app-lifecycle.h" @@ -58,6 +59,7 @@ static void __stc_manager_deinit(void) stc_deinit_db_guard(); stc_db_deinitialize(); + iptables_deinit(); stc_manager_gdbus_deinit((gpointer)g_stc); stc_app_lifecycle_monitor_deinit(); stc_manager_plugin_deinit(); -- 2.7.4 From 3292a32e4d3621fc117920f5a4aa40c547a980fd Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 24 Jan 2018 09:55:02 +0900 Subject: [PATCH 08/16] Add initial source codes for gtest Change-Id: I41574b7e13b99c1a1fb14ee32b27d4352bbc933a Signed-off-by: hyunuktak --- CMakeLists.txt | 6 +- include/stc-manager-gdbus.h | 5 +- include/stc-manager.h | 4 +- interfaces/stcmanager-iface-manager.xml | 7 + interfaces/stcmanager-iface-restriction.xml | 4 + packaging/stc-manager.spec | 14 +- src/CMakeLists.txt | 10 +- src/database/db-common.c | 40 +-- src/database/db-guard.c | 8 +- src/database/include/stc-db.h | 9 - src/database/include/table-statistics.h | 3 - src/database/tables/table-counters.c | 26 +- src/database/tables/table-restrictions.c | 68 ++--- src/database/tables/table-statistics.c | 66 ++--- src/helper/helper-cgroup.c | 24 +- src/helper/helper-iptables.c | 122 ++++----- src/helper/helper-net-cls.c | 32 +-- src/helper/helper-nfacct-rule.c | 34 ++- src/helper/helper-nl.c | 16 +- src/helper/helper-procfs.c | 12 +- src/monitor/stc-app-lifecycle.c | 120 ++++----- src/monitor/stc-default-connection.c | 73 +++--- src/monitor/stc-emulator.c | 12 +- src/monitor/stc-exception.c | 26 +- src/monitor/stc-monitor.c | 225 ++++++++++------- src/stc-manager-gdbus.c | 91 +++++-- src/stc-manager-plugin.c | 2 + src/stc-manager-util.c | 22 +- src/stc-manager.c | 23 +- src/stc-restriction.c | 74 +++--- src/stc-statistics.c | 86 +++---- unittest/CMakeLists.txt | 32 +++ unittest/gdbus.cpp | 185 ++++++++++++++ unittest/gdbus.h | 70 ++++++ unittest/manager.cpp | 58 +++++ unittest/manager.h | 31 +++ unittest/restriction.cpp | 343 +++++++++++++++++++++++++ unittest/restriction.h | 78 ++++++ unittest/statistics.cpp | 317 +++++++++++++++++++++++ unittest/statistics.h | 66 +++++ unittest/stcmgr.cpp | 17 ++ unittest/stcmgr.h | 72 ++++++ unittest/unittest.cpp | 377 ++++++++++++++++++++++++++++ unittest/unittest.h | 29 +++ 44 files changed, 2382 insertions(+), 557 deletions(-) create mode 100644 interfaces/stcmanager-iface-manager.xml create mode 100755 unittest/CMakeLists.txt create mode 100755 unittest/gdbus.cpp create mode 100755 unittest/gdbus.h create mode 100755 unittest/manager.cpp create mode 100755 unittest/manager.h create mode 100755 unittest/restriction.cpp create mode 100755 unittest/restriction.h create mode 100755 unittest/statistics.cpp create mode 100755 unittest/statistics.h create mode 100755 unittest/stcmgr.cpp create mode 100755 unittest/stcmgr.h create mode 100755 unittest/unittest.cpp create mode 100755 unittest/unittest.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4dfaf15..1e7fe23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -PROJECT(stc-manager C) +PROJECT(stc-manager C CXX) SET(PACKAGE ${PROJECT_NAME}) SET(INTERFACES "${CMAKE_SOURCE_DIR}/interfaces") SET(PREFIX ${CMAKE_INSTALL_PREFIX}) @@ -9,3 +9,7 @@ SET(DATA_DIR ${CMAKE_SOURCE_DIR}/data) ADD_SUBDIRECTORY(src) ADD_SUBDIRECTORY(plugin) + +IF(BUILD_GTESTS) + ADD_SUBDIRECTORY(unittest) +ENDIF(BUILD_GTESTS) diff --git a/include/stc-manager-gdbus.h b/include/stc-manager-gdbus.h index 045a70c..7803c6b 100755 --- a/include/stc-manager-gdbus.h +++ b/include/stc-manager-gdbus.h @@ -25,7 +25,7 @@ #define STC_DBUS_SERVICE_PATH "/net/stc" #define STC_DBUS_SERVICE_STATISTICS_PATH "/net/stc/statistics" #define STC_DBUS_SERVICE_RESTRICTION_PATH "/net/stc/restriction" -#define STC_DBUS_SERVICE_QUOTA_PATH "/net/stc/quota" +#define STC_DBUS_SERVICE_MANAGER_PATH "/net/stc/manager" #define STC_DBUS_REPLY_ERROR_NONE(invocation) \ g_dbus_method_invocation_return_value((invocation), \ @@ -79,4 +79,7 @@ gboolean stc_manager_dbus_emit_signal(GDBusConnection *connection, const gchar *signal_name, GVariant *parameters); +gboolean handle_manager_stop(StcManager *object, + GDBusMethodInvocation *invocation); + #endif /* __STC_MANAGER_GDBUS_H__ */ diff --git a/include/stc-manager.h b/include/stc-manager.h index 2766a60..f2b9c97 100755 --- a/include/stc-manager.h +++ b/include/stc-manager.h @@ -137,7 +137,6 @@ typedef enum { /** * @desc Set of the options. - * version - contains structure version * wifi - enable/disable wifi, STC_DB_OPTION_UNDEF to leave option as is * datacall - enable/disable datacall, STC_DB_OPTION_UNDEF to leave option as is * datausage_timer - set period of the updating data from the kernel, @@ -146,7 +145,6 @@ typedef enum { * STC_DB_OPTION_UNDEF to leave option as is */ typedef struct { - unsigned char version; stc_option_state_e wifi; stc_option_state_e datacall; time_t datausage_timer; @@ -166,6 +164,7 @@ typedef struct { gpointer statistics_obj; gpointer restriction_obj; + gpointer manager_obj; GDBusObjectManagerServer *obj_mgr; GDBusConnection *connection; @@ -176,5 +175,6 @@ typedef struct { } stc_s; stc_s *stc_get_manager(void); +void stc_stop_manager(void); #endif /* __STC_MANAGER__ */ diff --git a/interfaces/stcmanager-iface-manager.xml b/interfaces/stcmanager-iface-manager.xml new file mode 100644 index 0000000..f5a9167 --- /dev/null +++ b/interfaces/stcmanager-iface-manager.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/interfaces/stcmanager-iface-restriction.xml b/interfaces/stcmanager-iface-restriction.xml index f4baf76..93b81af 100644 --- a/interfaces/stcmanager-iface-restriction.xml +++ b/interfaces/stcmanager-iface-restriction.xml @@ -33,6 +33,10 @@ + + + + diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index bd37172..ea01601 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.42 +Version: 0.0.43 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 @@ -30,6 +30,10 @@ BuildRequires: pkgconfig(sqlite3) BuildRequires: python BuildRequires: python-xml +%if 0%{?gtests:1} +BuildRequires: pkgconfig(gmock) +%endif + %description A smart traffic control manager to manage traffic counting and bandwidth limitation @@ -57,7 +61,9 @@ export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE" -DENABLE_DATABASE=%{enable_database} \ -DDATABASE_FULL_PATH=%{database_full_path} \ -DENABLE_STATISTICS=%{enable_statistics} \ - -DENABLE_RESTRICTION=%{enable_restriction} + -DENABLE_RESTRICTION=%{enable_restriction} \ + -DBUILD_GTESTS=%{?gtests:1}%{!?gtests:0} \ + -DBUILD_GCOV=%{?gcov:1}%{!?gcov:0} make %{?_smp_mflags} @@ -108,6 +114,10 @@ cp resources/dbus/stc-manager.conf %{buildroot}%{_sysconfdir}/dbus-1/system.d/st %config(noreplace) %attr(660, root, root) %{database_full_path}-journal %endif +%if 0%{?gtests:1} +%{_bindir}/gtest* +%endif + %files plugin %manifest %{name}.manifest %attr(644, -,-) %{_datadir}/icons/*.png diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 23fd943..d8bc359 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -64,14 +64,21 @@ IF("${ENABLE_DATABASE}" STREQUAL "YES") INSTALL(FILES ${DATA_DIR}/traffic_db.sql DESTINATION /usr/share) ENDIF() +IF(BUILD_GTESTS) +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fpic -Wall -Werror-implicit-function-declaration") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fprofile-arcs -ftest-coverage") +ELSE(BUILD_GTESTS) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fpic -Wall -Werror-implicit-function-declaration -fvisibility=hidden") -SET(ARM_CFLAGS "${ARM_CFLAGS} -mapcs -mabi=aapcs-linux -msoft-float -Uarm -fpic") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") +ENDIF(BUILD_GTESTS) SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") ADD_DEFINITIONS("-DUSE_DLOG") ADD_DEFINITIONS("-DDATABASE_FULL_PATH=\"${DATABASE_FULL_PATH}\"") +IF(BUILD_GTESTS) + ADD_DEFINITIONS(-DTIZEN_GTESTS) +ENDIF(BUILD_GTESTS) ADD_CUSTOM_COMMAND( WORKING_DIRECTORY @@ -81,6 +88,7 @@ ADD_CUSTOM_COMMAND( --c-namespace Stc --c-generate-object-manager --generate-docbook generated-code-docs + ${INTERFACES}/stcmanager-iface-manager.xml ${INTERFACES}/stcmanager-iface-restriction.xml ${INTERFACES}/stcmanager-iface-statistics.xml COMMENT "Generating GDBus .c/.h") diff --git a/src/database/db-common.c b/src/database/db-common.c index 3895e12..f5f1770 100755 --- a/src/database/db-common.c +++ b/src/database/db-common.c @@ -28,6 +28,7 @@ static sqlite3 *database; +//LCOV_EXCL_START static int __stc_db_busy(void *user, int attempts) { __STC_LOG_FUNC_ENTER__; @@ -38,6 +39,7 @@ static int __stc_db_busy(void *user, int attempts) __STC_LOG_FUNC_EXIT__; return 1; } +//LCOV_EXCL_STOP stc_error_e stc_db_initialize_once(void) { @@ -45,16 +47,16 @@ stc_error_e stc_db_initialize_once(void) int res = 0; int retry_count = 0; if (database != NULL) { - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_NONE; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_NONE; //LCOV_EXCL_LINE } do { res = sqlite3_open(DATABASE_FULL_PATH, &database); if (res != SQLITE_OK) { - STC_LOGD("Retry[%d] opening database %s: %s\n", + STC_LOGD("Retry[%d] opening database %s: %s\n", //LCOV_EXCL_LINE ++retry_count, DATABASE_FULL_PATH, - sqlite3_errmsg(database)); + sqlite3_errmsg(database)); //LCOV_EXCL_LINE } else { break; } @@ -62,33 +64,34 @@ stc_error_e stc_db_initialize_once(void) } while (retry_count <= MAX_DB_RETRY_COUNT); if (res != SQLITE_OK) { - STC_LOGE("Can't open database %s: %s\n", DATABASE_FULL_PATH, + STC_LOGE("Can't open database %s: %s\n", DATABASE_FULL_PATH, //LCOV_EXCL_LINE sqlite3_errmsg(database)); - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_DB_FAILED; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE } STC_LOGD("Successfully opened database"); res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0); if (res != SQLITE_OK) { - STC_LOGE("Can't set locking mode %s, skip set busy handler.", + STC_LOGE("Can't set locking mode %s, skip set busy handler.", //LCOV_EXCL_LINE sqlite3_errmsg(database)); - sqlite3_close(database); - database = NULL; - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_DB_FAILED; + sqlite3_close(database); //LCOV_EXCL_LINE + database = NULL; //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE } /* Set how many times we'll repeat our attempts for sqlite_step */ if (sqlite3_busy_handler(database, __stc_db_busy, NULL) != SQLITE_OK) - STC_LOGE("Couldn't set busy handler!"); + STC_LOGE("Couldn't set busy handler!"); //LCOV_EXCL_LINE __STC_LOG_FUNC_EXIT__; return STC_ERROR_NONE; } +//LCOV_EXCL_START sqlite3 *stc_db_get_database(void) { if (database == NULL) @@ -96,6 +99,7 @@ sqlite3 *stc_db_get_database(void) return database; } +//LCOV_EXCL_STOP stc_error_e stc_db_initialize(void) { @@ -113,17 +117,17 @@ stc_error_e stc_db_initialize(void) return STC_ERROR_NONE; handle_error: - stc_db_deinitialize(); - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_DB_FAILED; + stc_db_deinitialize(); //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE } gboolean stc_db_deinitialize(void) { __STC_LOG_FUNC_ENTER__; if (database == NULL) { - __STC_LOG_FUNC_EXIT__; - return TRUE; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return TRUE; //LCOV_EXCL_LINE } table_statistics_finalize(); diff --git a/src/database/db-guard.c b/src/database/db-guard.c index 5d03183..54b7fc5 100755 --- a/src/database/db-guard.c +++ b/src/database/db-guard.c @@ -33,6 +33,7 @@ static guint erase_timer = 0; static int db_entries = 0; +//LCOV_EXCL_START static void __change_db_entries_num_num(int num) { __STC_LOG_FUNC_ENTER__; @@ -115,6 +116,7 @@ static gboolean __erase_func_cb(void *user_data) __STC_LOG_FUNC_EXIT__; return TRUE; } +//LCOV_EXCL_STOP stc_error_e stc_init_db_guard(void) { @@ -123,9 +125,9 @@ stc_error_e stc_init_db_guard(void) erase_timer = g_timeout_add_seconds(ERASE_TIMER_INTERVAL, __erase_func_cb, NULL); if (erase_timer == 0) { - STC_LOGE("Failed to create timer"); - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_FAIL; + STC_LOGE("Failed to create timer"); //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } __STC_LOG_FUNC_EXIT__; diff --git a/src/database/include/stc-db.h b/src/database/include/stc-db.h index 689cb1f..6255433 100755 --- a/src/database/include/stc-db.h +++ b/src/database/include/stc-db.h @@ -37,15 +37,6 @@ typedef struct { time_t to; } stc_db_tm_interval_s; -typedef enum { - STC_DB_CON_PERIOD_UNKNOWN, /**< Undefined period */ - STC_DB_CON_PERIOD_LAST_RECEIVED_DATA, /**< Last received data */ - STC_DB_CON_PERIOD_LAST_SENT_DATA, /**< Last sent data */ - STC_DB_CON_PERIOD_TOTAL_RECEIVED_DATA, /**< Total received data */ - STC_DB_CON_PERIOD_TOTAL_SENT_DATA, /**< Total sent data */ - STC_DB_CON_PERIOD_LAST_ELEM -} stc_db_connection_period_type; - /* * General structure containing information for storing * app_id - package name as unique application identifier diff --git a/src/database/include/table-statistics.h b/src/database/include/table-statistics.h index ec88f5a..284fb48 100755 --- a/src/database/include/table-statistics.h +++ b/src/database/include/table-statistics.h @@ -30,16 +30,13 @@ typedef struct { } table_statistics_info; typedef struct { - unsigned char version; char *app_id; char *subscriber_id; stc_iface_type_e iftype; stc_db_tm_interval_s *interval; - stc_db_connection_period_type connection_state; } table_statistics_reset_rule; typedef struct { - unsigned char version; time_t from; time_t to; stc_iface_type_e iftype; diff --git a/src/database/tables/table-counters.c b/src/database/tables/table-counters.c index f8946f7..11c99a4 100755 --- a/src/database/tables/table-counters.c +++ b/src/database/tables/table-counters.c @@ -91,8 +91,8 @@ static int __prepare_delete(sqlite3 *db) static int initialized; if (initialized) { - __STC_LOG_FUNC_EXIT__; - return SQLITE_OK; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return SQLITE_OK; //LCOV_EXCL_LINE } PREPARE_DELETE(delete_counter, DELETE_COUNTER); @@ -118,8 +118,8 @@ static int __prepare_select(sqlite3 *db) static int initialized; if (initialized) { - __STC_LOG_FUNC_EXIT__; - return SQLITE_OK; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return SQLITE_OK; //LCOV_EXCL_LINE } PREPARE_SELECT(select_counter, SELECT_COUNTER); @@ -145,8 +145,8 @@ static int __prepare_update(sqlite3 *db) static int initialized; if (initialized) { - __STC_LOG_FUNC_EXIT__; - return SQLITE_OK; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return SQLITE_OK; //LCOV_EXCL_LINE } PREPARE_UPDATE(update_counter, UPDATE_COUNTER); @@ -182,6 +182,7 @@ stc_error_e table_counters_get(uint64_t restriction_id, do { rc = sqlite3_step(stmt); + //LCOV_EXCL_START switch (rc) { case SQLITE_DONE: break; @@ -198,6 +199,7 @@ stc_error_e table_counters_get(uint64_t restriction_id, error_code = STC_ERROR_DB_FAILED; } + //LCOV_EXCL_STOP } while (rc == SQLITE_ROW); handle_error: @@ -205,6 +207,7 @@ handle_error: return error_code; } +//LCOV_EXCL_START stc_error_e table_counters_update_counters(const table_counters_info *info) { stc_error_e error_code = STC_ERROR_NONE; @@ -233,6 +236,7 @@ handle_error: sqlite3_reset(stmt); return error_code; } +//LCOV_EXCL_STOP stc_error_e table_counters_delete(uint64_t restriction_id) { @@ -242,10 +246,10 @@ stc_error_e table_counters_delete(uint64_t restriction_id) DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id)); if (sqlite3_step(stmt) != SQLITE_DONE) { - STC_LOGE("Failed to delete counter: %s\n", + STC_LOGE("Failed to delete counter: %s\n", //LCOV_EXCL_LINE sqlite3_errmsg(stc_db_get_database())); - error_code = STC_ERROR_DB_FAILED; - goto handle_error; + error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE + goto handle_error; //LCOV_EXCL_LINE } STC_LOGD("Counter deleted for restriction_id [%llu]", restriction_id); @@ -262,8 +266,8 @@ stc_error_e table_counters_prepare(sqlite3 *db) stc_error_e error_code = STC_ERROR_NONE; if (db == NULL) { - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_FAIL; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } DB_ACTION(__prepare_delete(db)); diff --git a/src/database/tables/table-restrictions.c b/src/database/tables/table-restrictions.c index 9324887..1c717e4 100755 --- a/src/database/tables/table-restrictions.c +++ b/src/database/tables/table-restrictions.c @@ -145,8 +145,8 @@ static int __prepare_delete(sqlite3 *db) static int initialized; if (initialized) { - __STC_LOG_FUNC_EXIT__; - return SQLITE_OK; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return SQLITE_OK; //LCOV_EXCL_LINE } PREPARE_DELETE(delete_restrictions, DELETE_RESTRICTIONS); @@ -172,8 +172,8 @@ static int __prepare_select(sqlite3 *db) static int initialized; if (initialized) { - __STC_LOG_FUNC_EXIT__; - return SQLITE_OK; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return SQLITE_OK; //LCOV_EXCL_LINE } PREPARE_SELECT(select_restriction, SELECT_RESTRICTIONS); @@ -207,8 +207,8 @@ static int __prepare_replace(sqlite3 *db) static int initialized; if (initialized) { - __STC_LOG_FUNC_EXIT__; - return SQLITE_OK; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return SQLITE_OK; //LCOV_EXCL_LINE } PREPARE_UPDATE(update_net_restrictions, UPDATE_NET_RESTRICTIONS); @@ -234,8 +234,8 @@ static int __prepare_insert(sqlite3 *db) static int initialized; if (initialized) { - __STC_LOG_FUNC_EXIT__; - return SQLITE_OK; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return SQLITE_OK; //LCOV_EXCL_LINE } PREPARE_UPDATE(insert_net_restrictions, INSERT_NET_RESTRICTIONS); @@ -265,8 +265,8 @@ stc_error_e table_restrictions_per_app(const gchar* app_id, sqlite3_stmt *stmt = select_restriction_per_app; if (!app_id) { - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_DB_FAILED; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE } DB_ACTION(sqlite3_bind_text(stmt, 1, app_id, -1, @@ -294,15 +294,15 @@ stc_error_e table_restrictions_per_app(const gchar* app_id, data.restriction_id = sqlite3_column_int64(stmt, 8); if (restriction_cb(&data, user_data) == STC_CANCEL) - rc = SQLITE_DONE; + rc = SQLITE_DONE; //LCOV_EXCL_LINE break; case SQLITE_ERROR: default: - STC_LOGE("Failed to enumerate restrictions: %s\n", + STC_LOGE("Failed to enumerate restrictions: %s\n", //LCOV_EXCL_LINE sqlite3_errmsg(stc_db_get_database())); - __STC_LOG_FUNC_EXIT__; - error_code = STC_ERROR_DB_FAILED; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE } } while (rc == SQLITE_ROW); @@ -342,15 +342,15 @@ stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restrict data.restriction_id = sqlite3_column_int64(stmt, 8); if (restriction_cb(&data, user_data) == STC_CANCEL) - rc = SQLITE_DONE; + rc = SQLITE_DONE; //LCOV_EXCL_LINE break; case SQLITE_ERROR: default: - STC_LOGE("Failed to enumerate restrictions: %s\n", + STC_LOGE("Failed to enumerate restrictions: %s\n", //LCOV_EXCL_LINE sqlite3_errmsg(stc_db_get_database())); - __STC_LOG_FUNC_EXIT__; - error_code = STC_ERROR_DB_FAILED; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE } } while (rc == SQLITE_ROW); @@ -370,9 +370,9 @@ stc_error_e table_restrictions_get_restriction_state_subscriber_id(const char *a bool state_subscriber_id = 0; if (state == NULL) { - STC_LOGE("Please provide valid argument!"); - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_INVALID_PARAMETER; + STC_LOGE("Please provide valid argument!"); //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE } *state = STC_RESTRICTION_UNKNOWN; @@ -410,10 +410,10 @@ stc_error_e table_restrictions_get_restriction_state_subscriber_id(const char *a break; case SQLITE_ERROR: default: - STC_LOGE("Can't perform sql query: %s \n%s", + STC_LOGE("Can't perform sql query: %s \n%s", //LCOV_EXCL_LINE SELECT_RESTRICTION_STATE, sqlite3_errmsg(stc_db_get_database())); - error_code = STC_ERROR_DB_FAILED; + error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE } handle_error: @@ -449,10 +449,10 @@ stc_error_e table_restrictions_delete(const char *app_id, SQLITE_TRANSIENT)); if (sqlite3_step(stmt) != SQLITE_DONE) { - STC_LOGE("Failed to remove restrictions by network interface %s\n", + STC_LOGE("Failed to remove restrictions by network interface %s\n", //LCOV_EXCL_LINE sqlite3_errmsg(stc_db_get_database())); - error_code = STC_ERROR_DB_FAILED; - goto handle_error; + error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE + goto handle_error; //LCOV_EXCL_LINE } STC_LOGD("Restriction deleted for app_id [%s]", app_id); @@ -491,7 +491,7 @@ stc_error_e __get_restriction_id(table_restrictions_info *info) break; case SQLITE_ERROR: default: - STC_LOGE("Failed to get restriction id: %s\n", + STC_LOGE("Failed to get restriction id: %s\n", //LCOV_EXCL_LINE sqlite3_errmsg(stc_db_get_database())); } @@ -507,8 +507,8 @@ stc_error_e table_restrictions_update(table_restrictions_info *info) sqlite3_stmt *stmt = insert_net_restrictions; if (!info) { - error_code = STC_ERROR_INVALID_PARAMETER; - goto handle_error; + error_code = STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE + goto handle_error; //LCOV_EXCL_LINE } __get_restriction_id(info); @@ -531,10 +531,10 @@ stc_error_e table_restrictions_update(table_restrictions_info *info) 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", + STC_LOGE("Failed to set network restriction: %s\n", //LCOV_EXCL_LINE sqlite3_errmsg(stc_db_get_database())); - error_code = STC_ERROR_DB_FAILED; - goto handle_error; + error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE + goto handle_error; //LCOV_EXCL_LINE } if (info->restriction_id) { @@ -556,8 +556,8 @@ stc_error_e table_restrictions_prepare(sqlite3 *db) stc_error_e error_code = STC_ERROR_NONE; if (db == NULL) { - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_FAIL; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } DB_ACTION(__prepare_delete(db)); diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c index c14fdb6..49856b4 100755 --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -223,8 +223,8 @@ static int __prepare_delete(sqlite3 *db) static int initialized; if (initialized) { - __STC_LOG_FUNC_EXIT__; - return SQLITE_OK; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return SQLITE_OK; //LCOV_EXCL_LINE } PREPARE_DELETE(delete_query[0], DELETE_ALL); @@ -256,8 +256,8 @@ static int __prepare_select(sqlite3 *db) static int initialized; if (initialized) { - __STC_LOG_FUNC_EXIT__; - return SQLITE_OK; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return SQLITE_OK; //LCOV_EXCL_LINE } PREPARE_SELECT(select_for_period, SELECT_FOR_PERIOD); @@ -305,8 +305,8 @@ static int __prepare_insert(sqlite3 *db) static int initialized; if (initialized) { - __STC_LOG_FUNC_EXIT__; - return SQLITE_OK; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return SQLITE_OK; //LCOV_EXCL_LINE } PREPARE_INSERT(update_statistics_query, INSERT_VALUES); @@ -360,6 +360,7 @@ static sqlite3_stmt *__select_statement(const char *app_id, return *details_stms[stm_index]; } +//LCOV_EXCL_START stc_error_e table_statistics_reset_first_n_entries(int num) { __STC_LOG_FUNC_ENTER__; @@ -381,18 +382,19 @@ handle_error: sqlite3_reset(delete_query[4]); return error_code; } +//LCOV_EXCL_STOP stc_error_e table_statistics_reset(const table_statistics_reset_rule *rule) { __STC_LOG_FUNC_ENTER__; sqlite3_stmt *stmt; stc_error_e error_code = STC_ERROR_NONE; - int pos = 1;/* running through positions where to + int pos = 1; /* running through positions where to bind parameters in the query */ if (!rule || !rule->interval) { - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_INVALID_PARAMETER; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE } /* pick a statement depending on parameters. @@ -413,10 +415,10 @@ stc_error_e table_statistics_reset(const table_statistics_reset_rule *rule) DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->interval->to)); if (sqlite3_step(stmt) != SQLITE_DONE) { - STC_LOGE("Failed to drop collected statistics."); - error_code = STC_ERROR_DB_FAILED; - __STC_LOG_FUNC_EXIT__; - goto handle_error; + STC_LOGE("Failed to drop collected statistics."); //LCOV_EXCL_LINE + error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto handle_error; //LCOV_EXCL_LINE } STC_LOGD("Entry deleted successfully."); @@ -441,8 +443,8 @@ stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rul memset(&data, 0, sizeof(data)); if (!rule || !info_cb) { - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_INVALID_PARAMETER; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE } /* pick a statement depending on parameters */ @@ -485,7 +487,7 @@ stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rul } if (info_cb(&data, user_data) == STC_CANCEL) - rc = SQLITE_DONE;/* emulate end of data */ + rc = SQLITE_DONE; //LCOV_EXCL_LINE __STC_LOG_FUNC_EXIT__; break; case SQLITE_DONE: @@ -493,9 +495,9 @@ stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rul break; case SQLITE_ERROR: default: - error_code = STC_ERROR_DB_FAILED; - __STC_LOG_FUNC_EXIT__; - break; + error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + break; //LCOV_EXCL_LINE } } while (rc == SQLITE_ROW); @@ -515,15 +517,15 @@ stc_error_e table_statistics_per_app(const char *app_id, sqlite3_stmt *stmt; stc_error_e error_code = STC_ERROR_NONE; int rc; - int pos = 1;/* running through positions + int pos = 1; /* running through positions where to bind parameters in the query */ stc_db_tm_interval_s interval; memset(&data, 0, sizeof(data)); if (!rule || !info_cb) { - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_INVALID_PARAMETER; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE } /* pick a statement depending on parameters. @@ -566,7 +568,7 @@ stc_error_e table_statistics_per_app(const char *app_id, } if (info_cb(&data, user_data) == STC_CANCEL) - rc = SQLITE_DONE; /* emulate end of data */ + rc = SQLITE_DONE; //LCOV_EXCL_LINE __STC_LOG_FUNC_EXIT__; break; case SQLITE_DONE: @@ -574,9 +576,9 @@ stc_error_e table_statistics_per_app(const char *app_id, break; case SQLITE_ERROR: default: - error_code = STC_ERROR_DB_FAILED; - __STC_LOG_FUNC_EXIT__; - break; + error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + break; //LCOV_EXCL_LINE } } while (rc == SQLITE_ROW); @@ -619,11 +621,11 @@ stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key, stat->rcv_count = 0; stat->snd_count = 0; if (sqlite3_step(stmt) != SQLITE_DONE) { - STC_LOGE("Failed to record appstat. %s", + STC_LOGE("Failed to record appstat. %s", //LCOV_EXCL_LINE sqlite3_errmsg(stc_db_get_database())); - error_code = STC_ERROR_DB_FAILED; - __STC_LOG_FUNC_EXIT__; - goto handle_error; + error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto handle_error; //LCOV_EXCL_LINE } STC_LOGD("App stat recorded [%s]", stat->app_id); @@ -669,8 +671,8 @@ stc_error_e table_statistics_prepare(sqlite3 *db) stc_error_e error_code = STC_ERROR_NONE; if (db == NULL) { - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_DB_FAILED; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE } DB_ACTION(__prepare_delete(db)); diff --git a/src/helper/helper-cgroup.c b/src/helper/helper-cgroup.c index 8c258a5..dc51231 100755 --- a/src/helper/helper-cgroup.c +++ b/src/helper/helper-cgroup.c @@ -34,7 +34,7 @@ static int cgroup_create(const char *cgroup_full_path) { if (mkdir(cgroup_full_path, S_IRUSR | S_IWUSR | S_IRGRP) < 0) - return -errno; + return -errno; //LCOV_EXCL_LINE return 0; } @@ -49,8 +49,8 @@ stc_error_e cgroup_write_pid_fullpath(const char *cgroup_full_path, int ret; if (pid <= 0) { - STC_LOGE("try to write empty pid to %s", cgroup_full_path); - return STC_ERROR_NO_DATA; + STC_LOGE("try to write empty pid to %s", cgroup_full_path); //LCOV_EXCL_LINE + return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE } ret = cgroup_write_node_uint32(cgroup_full_path, CGROUP_FILE_NAME, @@ -69,6 +69,7 @@ stc_error_e cgroup_write_pid(const char *cgroup_subsystem, return cgroup_write_pid_fullpath(buf, pid); } +//LCOV_EXCL_START stc_error_e cgroup_write_pidtree(const char *cgroup_subsystem, const char *cgroup_name, const int pid) { @@ -110,6 +111,7 @@ stc_error_e cgroup_write_pidtree(const char *cgroup_subsystem, fclose(f); return STC_ERROR_NONE; } +//LCOV_EXCL_STOP int cgroup_write_node_uint32(const char *cgroup_name, const char *file_name, uint32_t value) @@ -118,7 +120,7 @@ int cgroup_write_node_uint32(const char *cgroup_name, snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name); if (STC_DEBUG_LOG) - STC_LOGD("cgroup_buf %s, value %d\n", buf, value); + STC_LOGD("cgroup_buf %s, value %d\n", buf, value); //LCOV_EXCL_LINE return fwrite_uint(buf, value); } @@ -130,7 +132,7 @@ int cgroup_write_node_str(const char *cgroup_name, snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name); if (STC_DEBUG_LOG) - STC_LOGD("cgroup_buf %s, string %s\n", buf, string); + STC_LOGD("cgroup_buf %s, string %s\n", buf, string); //LCOV_EXCL_LINE return fwrite_str(buf, string); } @@ -144,7 +146,7 @@ int cgroup_read_node_uint32(const char *cgroup_name, ret = fread_uint(buf, value); if (STC_DEBUG_LOG) - STC_LOGD("cgroup_buf %s, value %d\n", buf, *value); + STC_LOGD("cgroup_buf %s, value %d\n", buf, *value); //LCOV_EXCL_LINE return ret; } @@ -173,12 +175,12 @@ int cgroup_make_subdir(const char *parentdir, const char *cgroup_name, if (parentdir && !strncmp(parentdir, DEFAULT_CGROUP, sizeof(DEFAULT_CGROUP))) { - ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs", + ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs", //LCOV_EXCL_LINE MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, "mode=755"); if (ret < 0) { - STC_LOGE("Fail to RW mount cgroup directory. Can't make %s cgroup", cgroup_name); - return STC_ERROR_FAIL; + STC_LOGE("Fail to RW mount cgroup directory. Can't make %s cgroup", cgroup_name); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } cgroup_remount = true; } @@ -189,11 +191,11 @@ int cgroup_make_subdir(const char *parentdir, const char *cgroup_name, cgroup_name, errno); if (cgroup_remount) { - ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs", + ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs", //LCOV_EXCL_LINE MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755"); if (ret < 0) - STC_LOGD("Fail to RO mount"); + STC_LOGD("Fail to RO mount"); //LCOV_EXCL_LINE } } diff --git a/src/helper/helper-iptables.c b/src/helper/helper-iptables.c index 96eacb8..06b9378 100755 --- a/src/helper/helper-iptables.c +++ b/src/helper/helper-iptables.c @@ -44,7 +44,7 @@ static void __add_rule_info_to_builder(GVariantBuilder *builder, iptables_rule_s *rule) { if (builder == NULL || rule == NULL) - return; + return; //LCOV_EXCL_LINE g_variant_builder_add(builder, "{sv}", RULE_CHAIN, g_variant_new_string(rule->chain)); @@ -91,8 +91,8 @@ static int __iptables_rule_add(GDBusConnection *connection, params); if (message == NULL) { - STC_LOGE("Failed to invoke dbus method"); - return STC_ERROR_FAIL; + STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } g_variant_get(message, "(i)", &result); @@ -123,8 +123,8 @@ static int __iptables_rule_remove(GDBusConnection *connection, params); if (message == NULL) { - STC_LOGE("Failed to invoke dbus method"); - return STC_ERROR_FAIL; + STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } g_variant_get(message, "(i)", &result); @@ -155,8 +155,8 @@ static int __ip6tables_rule_add(GDBusConnection *connection, params); if (message == NULL) { - STC_LOGE("Failed to invoke dbus method"); - return STC_ERROR_FAIL; + STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } g_variant_get(message, "(i)", &result); @@ -187,8 +187,8 @@ static int __ip6tables_rule_remove(GDBusConnection *connection, params); if (message == NULL) { - STC_LOGE("Failed to invoke dbus method"); - return STC_ERROR_FAIL; + STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } g_variant_get(message, "(i)", &result); @@ -212,8 +212,8 @@ static int __iptables_add_chain(GDBusConnection *connection, g_variant_new("(s)", chain)); if (message == NULL) { - STC_LOGE("Failed to invoke dbus method"); - return STC_ERROR_FAIL; + STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } g_variant_get(message, "(i)", &result); @@ -237,8 +237,8 @@ static int __ip6tables_add_chain(GDBusConnection *connection, g_variant_new("(s)", chain)); if (message == NULL) { - STC_LOGE("Failed to invoke dbus method"); - return STC_ERROR_FAIL; + STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } g_variant_get(message, "(i)", &result); @@ -262,8 +262,8 @@ static int __iptables_remove_chain(GDBusConnection *connection, g_variant_new("(s)", chain)); if (message == NULL) { - STC_LOGE("Failed to invoke dbus method"); - return STC_ERROR_FAIL; + STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } g_variant_get(message, "(i)", &result); @@ -287,8 +287,8 @@ static int __ip6tables_remove_chain(GDBusConnection *connection, g_variant_new("(s)", chain)); if (message == NULL) { - STC_LOGE("Failed to invoke dbus method"); - return STC_ERROR_FAIL; + STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } g_variant_get(message, "(i)", &result); @@ -312,8 +312,8 @@ static int __iptables_flush_chain(GDBusConnection *connection, g_variant_new("(s)", chain)); if (message == NULL) { - STC_LOGE("Failed to invoke dbus method"); - return STC_ERROR_FAIL; + STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } g_variant_get(message, "(i)", &result); @@ -337,8 +337,8 @@ static int __ip6tables_flush_chain(GDBusConnection *connection, g_variant_new("(s)", chain)); if (message == NULL) { - STC_LOGE("Failed to invoke dbus method"); - return STC_ERROR_FAIL; + STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } g_variant_get(message, "(i)", &result); @@ -372,11 +372,11 @@ stc_error_e iptables_add(iptables_rule_s *rule) stc_s *stc = stc_get_manager(); if (!stc || !stc->connection) - return STC_ERROR_INVALID_PARAMETER; + return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE ret = __iptables_rule_add(stc->connection, rule); if (ret != STC_ERROR_NONE) - goto done; + goto done; //LCOV_EXCL_LINE ret = __ip6tables_rule_add(stc->connection, rule); done: @@ -389,11 +389,11 @@ stc_error_e iptables_remove(iptables_rule_s *rule) stc_s *stc = stc_get_manager(); if (!stc || !stc->connection) - return STC_ERROR_INVALID_PARAMETER; + return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE ret = __iptables_rule_remove(stc->connection, rule); if (ret != STC_ERROR_NONE) - goto done; + goto done; //LCOV_EXCL_LINE ret = __ip6tables_rule_remove(stc->connection, rule); done: @@ -406,27 +406,27 @@ stc_error_e iptables_flush_chains(void) stc_s *stc = stc_get_manager(); if (!stc || !stc->connection) - return STC_ERROR_INVALID_PARAMETER; + return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE ret = __iptables_flush_chain(stc->connection, STC_IN_CHAIN); if (ret != STC_ERROR_NONE) - goto done; + goto done; //LCOV_EXCL_LINE ret = __iptables_flush_chain(stc->connection, STC_OUT_CHAIN); if (ret != STC_ERROR_NONE) - goto done; + goto done; //LCOV_EXCL_LINE ret = __iptables_flush_chain(stc->connection, STC_FRWD_CHAIN); if (ret != STC_ERROR_NONE) - goto done; + goto done; //LCOV_EXCL_LINE ret = __ip6tables_flush_chain(stc->connection, STC_IN_CHAIN); if (ret != STC_ERROR_NONE) - goto done; + goto done; //LCOV_EXCL_LINE ret = __ip6tables_flush_chain(stc->connection, STC_OUT_CHAIN); if (ret != STC_ERROR_NONE) - goto done; + goto done; //LCOV_EXCL_LINE ret = __ip6tables_flush_chain(stc->connection, STC_FRWD_CHAIN); done: @@ -441,56 +441,56 @@ stc_error_e iptables_init(void) stc_s *stc = stc_get_manager(); if (!stc || !stc->connection) { - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_INVALID_PARAMETER; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE } ret = __iptables_add_chain(stc->connection, STC_IN_CHAIN); if (ret != STC_ERROR_NONE) { - __STC_LOG_FUNC_EXIT__; - goto done; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } ret = __iptables_add_chain(stc->connection, STC_OUT_CHAIN); if (ret != STC_ERROR_NONE) { - __STC_LOG_FUNC_EXIT__; - goto done; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } ret = __iptables_add_chain(stc->connection, STC_FRWD_CHAIN); if (ret != STC_ERROR_NONE) { - __STC_LOG_FUNC_EXIT__; - goto done; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } ret = __ip6tables_add_chain(stc->connection, STC_IN_CHAIN); if (ret != STC_ERROR_NONE) { - __STC_LOG_FUNC_EXIT__; - goto done; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } ret = __ip6tables_add_chain(stc->connection, STC_OUT_CHAIN); if (ret != STC_ERROR_NONE) { - __STC_LOG_FUNC_EXIT__; - goto done; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } ret = __ip6tables_add_chain(stc->connection, STC_FRWD_CHAIN); if (ret != STC_ERROR_NONE) { - __STC_LOG_FUNC_EXIT__; - goto done; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_CHAIN); if (ret != STC_ERROR_NONE) { - __STC_LOG_FUNC_EXIT__; - goto done; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_CHAIN); if (ret != STC_ERROR_NONE) { - __STC_LOG_FUNC_EXIT__; - goto done; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } ret = __iptables_add_chain_jump_rule("FORWARD", STC_FRWD_CHAIN); @@ -506,38 +506,38 @@ stc_error_e iptables_deinit(void) stc_s *stc = stc_get_manager(); if (!stc || !stc->connection) { - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_INVALID_PARAMETER; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE } ret = __iptables_remove_chain(stc->connection, STC_IN_CHAIN); if (ret != STC_ERROR_NONE) { - __STC_LOG_FUNC_EXIT__; - goto done; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } ret = __iptables_remove_chain(stc->connection, STC_OUT_CHAIN); if (ret != STC_ERROR_NONE) { - __STC_LOG_FUNC_EXIT__; - goto done; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } ret = __iptables_remove_chain(stc->connection, STC_FRWD_CHAIN); if (ret != STC_ERROR_NONE) { - __STC_LOG_FUNC_EXIT__; - goto done; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } ret = __ip6tables_remove_chain(stc->connection, STC_IN_CHAIN); if (ret != STC_ERROR_NONE) { - __STC_LOG_FUNC_EXIT__; - goto done; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } ret = __ip6tables_remove_chain(stc->connection, STC_OUT_CHAIN); if (ret != STC_ERROR_NONE) { - __STC_LOG_FUNC_EXIT__; - goto done; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } ret = __ip6tables_remove_chain(stc->connection, STC_FRWD_CHAIN); diff --git a/src/helper/helper-net-cls.c b/src/helper/helper-net-cls.c index 6be717a..c1460dc 100755 --- a/src/helper/helper-net-cls.c +++ b/src/helper/helper-net-cls.c @@ -36,7 +36,7 @@ static uint32_t __produce_classid(check_classid_used_cb check_classid_cb) uint32_t classid = STC_RESERVED_CLASSID_MAX; int ret = fread_uint(CUR_CLASSID_PATH, &classid); if (ret < 0) - STC_LOGI("Can not read current classid"); + STC_LOGI("Can not read current classid"); //LCOV_EXCL_LINE classid += 1; @@ -51,7 +51,7 @@ static uint32_t __produce_classid(check_classid_used_cb check_classid_cb) ret = fwrite_uint(CUR_CLASSID_PATH, ++classid); if (ret < 0) - STC_LOGE("Can not write classid"); + STC_LOGE("Can not write classid"); //LCOV_EXCL_LINE return classid; } @@ -81,7 +81,7 @@ static uint32_t __get_classid_from_cgroup(const char *cgroup, int ret = cgroup_read_node_uint32(buf, CLASSID_FILE_NAME, &classid); if (ret < 0) - STC_LOGE("Can't read classid from cgroup %s", buf); + STC_LOGE("Can't read classid from cgroup %s", buf); //LCOV_EXCL_LINE return classid; } @@ -94,8 +94,8 @@ stc_error_e init_current_classid(void) uint32_t classid = STC_RESERVED_CLASSID_MAX; ret = fwrite_uint(CUR_CLASSID_PATH, classid); if (ret < 0) { - STC_LOGE("Can not init current classid"); - return STC_ERROR_FAIL; + STC_LOGE("Can not init current classid"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } } @@ -110,8 +110,8 @@ uint32_t get_classid_by_app_id(const char *app_id, int create) const char *path_to_net_cgroup_dir = NULL; if (app_id == NULL) { - STC_LOGE("app_id must be not empty"); - return STC_UNKNOWN_CLASSID; + STC_LOGE("app_id must be not empty"); //LCOV_EXCL_LINE + return STC_UNKNOWN_CLASSID; //LCOV_EXCL_LINE } if (!strcmp(app_id, STC_BACKGROUND_APP_ID)) @@ -139,7 +139,7 @@ uint32_t get_classid_by_app_id(const char *app_id, int create) /* just read */ if (!create) - classid = __get_classid_from_cgroup(path_to_net_cgroup_dir, + classid = __get_classid_from_cgroup(path_to_net_cgroup_dir, //LCOV_EXCL_LINE app_id); if (classid != STC_UNKNOWN_CLASSID) @@ -157,14 +157,14 @@ uint32_t get_classid_by_app_id(const char *app_id, int create) ret = __place_classid_to_cgroup(path_to_net_cgroup_dir, (char *)app_id, &classid, NULL); if (ret) - goto handle_error; + goto handle_error; //LCOV_EXCL_LINE return classid; handle_error: - STC_LOGE("error_code: [%d]", ret); - return STC_UNKNOWN_CLASSID; + STC_LOGE("error_code: [%d]", ret); //LCOV_EXCL_LINE + return STC_UNKNOWN_CLASSID; //LCOV_EXCL_LINE } stc_error_e place_pids_to_net_cgroup(const int pid, const char *app_id) @@ -175,8 +175,8 @@ stc_error_e place_pids_to_net_cgroup(const int pid, const char *app_id) snprintf(child_buf, sizeof(child_buf), PROC_TASK_CHILDREN, pid, pid); if (app_id == NULL) { - STC_LOGE("package name must be not empty"); - return STC_ERROR_INVALID_PARAMETER; + STC_LOGE("package name must be not empty"); //LCOV_EXCL_LINE + return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE } if (!strcmp(app_id, STC_BACKGROUND_APP_ID)) @@ -184,13 +184,13 @@ stc_error_e place_pids_to_net_cgroup(const int pid, const char *app_id) else if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX)) path_to_net_cgroup_dir = BACKGROUND_CGROUP_NETWORK; else - path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK; + path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK; //LCOV_EXCL_LINE if (access(child_buf, F_OK)) { if (STC_DEBUG_LOG) - STC_LOGD("%s of %s is not existed", child_buf, app_id); + STC_LOGD("%s of %s is not existed", child_buf, app_id); //LCOV_EXCL_LINE return cgroup_write_pid(path_to_net_cgroup_dir, app_id, pid); } - return cgroup_write_pidtree(path_to_net_cgroup_dir, app_id, pid); + return cgroup_write_pidtree(path_to_net_cgroup_dir, app_id, pid); //LCOV_EXCL_LINE } diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index 374090a..8f5dfee 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -135,19 +135,20 @@ static stc_error_e nfacct_send_new(nfacct_rule_s *counter) int ret = STC_ERROR_NONE; struct genl *req = MALLOC0(struct genl, 1); if (req == NULL) { - STC_LOGE("Failed allocate memory to genl request message"); - return STC_ERROR_OUT_OF_MEMORY; + STC_LOGE("Failed allocate memory to genl request message"); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } prepare_netlink_msg(req, NFNL_MSG_ACCT_NEW, NLM_F_CREATE | NLM_F_ACK); add_string_attr(req, counter->name, NFACCT_NAME); if (STC_DEBUG_LOG) - STC_LOGD("counter name %s", counter->name); + STC_LOGD("counter name %s", counter->name); //LCOV_EXCL_LINE /* padding */ add_uint64_attr(req, 0, NFACCT_PKTS); add_uint64_attr(req, 0, NFACCT_BYTES); + //LCOV_EXCL_START if (counter->quota) { STC_LOGD("quota bytes %"PRId64, counter->quota); @@ -155,6 +156,7 @@ static stc_error_e nfacct_send_new(nfacct_rule_s *counter) NFACCT_FLAGS); add_uint64_attr(req, htobe64(counter->quota), NFACCT_QUOTA); } + //LCOV_EXCL_STOP ret = send_nfacct_request(counter->carg->sock, req); FREE(req); @@ -166,12 +168,12 @@ stc_error_e nfacct_send_del(nfacct_rule_s *counter) int ret = STC_ERROR_NONE; struct genl *req = MALLOC0(struct genl, 1); if (req == NULL) { - STC_LOGE("Failed allocate memory to genl request message"); - return STC_ERROR_OUT_OF_MEMORY; + STC_LOGE("Failed allocate memory to genl request message"); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } if (STC_DEBUG_LOG) - STC_LOGD("send remove request for %s", counter->name); + STC_LOGD("send remove request for %s", counter->name); //LCOV_EXCL_LINE prepare_netlink_msg(req, NFNL_MSG_ACCT_DEL, NLM_F_ACK); add_string_attr(req, counter->name, NFACCT_NAME); @@ -192,8 +194,8 @@ static stc_error_e internal_nfacct_send_get(struct counter_arg *carg, int flag = !name ? NLM_F_DUMP : 0; struct genl *req = MALLOC0(struct genl, 1); if (req == NULL) { - STC_LOGE("Failed allocate memory to genl request message"); - return STC_ERROR_OUT_OF_MEMORY; + STC_LOGE("Failed allocate memory to genl request message"); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } prepare_netlink_msg(req, get_type, flag); @@ -278,14 +280,15 @@ bool recreate_counter_by_name(char *cnt_name, nfacct_rule_s *cnt) cnt->intend = NFACCT_BLOCK; break; case 't': - cnt->intend = NFACCT_TETH_COUNTER; - break; + cnt->intend = NFACCT_TETH_COUNTER; //LCOV_EXCL_LINE + break; //LCOV_EXCL_LINE default: return false; } STRING_SAVE_COPY(cnt->name, cnt_name); + //LCOV_EXCL_START if (cnt->intend == NFACCT_TETH_COUNTER) { char ifname_buf[MAX_IFACE_LENGTH]; int ifname_len; @@ -321,6 +324,7 @@ bool recreate_counter_by_name(char *cnt_name, nfacct_rule_s *cnt) cnt->classid = STC_TETHERING_APP_CLASSID; return true; } + //LCOV_EXCL_STOP io_part = strtok_r(name, "_", &save_ptr); if (io_part != NULL) @@ -413,8 +417,8 @@ static char *get_iptables_chain(const nfacct_rule_direction iotype) return STC_IN_CHAIN; else if (iotype == NFACCT_COUNTER_OUT) return STC_OUT_CHAIN; - else if (iotype == NFACCT_COUNTER_FORWARD) - return STC_FRWD_CHAIN; + else if (iotype == NFACCT_COUNTER_FORWARD) //LCOV_EXCL_LINE + return STC_FRWD_CHAIN; //LCOV_EXCL_LINE return ""; } @@ -429,11 +433,13 @@ static char *get_iptables_jump(const nfacct_rule_jump jump) return ""; } +/* static char *choose_iftype_name(nfacct_rule_s *rule) { return strlen(rule->ifname) != 0 ? rule->ifname : get_iftype_name(rule->iftype); } +*/ static stc_error_e exec_iptables_cmd(nfacct_rule_s *rule) { @@ -611,6 +617,7 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule) " traffic, for iftype %d, cmd %s, j %s", rule->iftype, set_cmd, jump_cmd); + //LCOV_EXCL_START /* for tethering */ if (rule->intend == NFACCT_WARN || rule->intend == NFACCT_BLOCK) { @@ -635,6 +642,7 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule) ret_value_msg_if(ret != STC_ERROR_NONE, ret, "can't del quota counter"); } + //LCOV_EXCL_STOP } if (rule->iotype & NFACCT_COUNTER_OUT) { @@ -668,6 +676,7 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule) "engress traffic, for iftype %d, cmd %s, j %s", rule->iftype, set_cmd, jump_cmd); + //LCOV_EXCL_START /* for tethering */ if (rule->intend == NFACCT_WARN || rule->intend == NFACCT_BLOCK) { @@ -691,6 +700,7 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule) ret_value_msg_if(ret != STC_ERROR_NONE, ret, "can't del quota counter"); } + //LCOV_EXCL_STOP } return STC_ERROR_NONE; } diff --git a/src/helper/helper-nl.c b/src/helper/helper-nl.c index b43ff2f..a42e565 100755 --- a/src/helper/helper-nl.c +++ b/src/helper/helper-nl.c @@ -34,7 +34,7 @@ int create_netlink(int protocol, uint32_t groups) int sock; sock = socket(PF_NETLINK, SOCK_RAW, protocol); if (sock < 0) - return -EINVAL; + return -EINVAL; //LCOV_EXCL_LINE struct sockaddr_nl src_addr = { 0, }; @@ -42,8 +42,8 @@ int create_netlink(int protocol, uint32_t groups) src_addr.nl_groups = groups; if (bind(sock, (struct sockaddr *)&src_addr, sizeof(src_addr)) < 0) { - close(sock); - return -1; + close(sock); //LCOV_EXCL_LINE + return -1; //LCOV_EXCL_LINE } return sock; @@ -86,16 +86,16 @@ int read_netlink(int sock, void *buf, size_t len) }; ret = recvmsg(sock, &msg, 0); if (ret == -1) - return ret; + return ret; //LCOV_EXCL_LINE if (msg.msg_flags & MSG_TRUNC) { - errno = ENOSPC; - return -1; + errno = ENOSPC; //LCOV_EXCL_LINE + return -1; //LCOV_EXCL_LINE } if (msg.msg_namelen != sizeof(struct sockaddr_nl)) { - errno = EINVAL; - return -1; + errno = EINVAL; //LCOV_EXCL_LINE + return -1; //LCOV_EXCL_LINE } return ret; diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c index 8710fa2..4cf1bb2 100755 --- a/src/helper/helper-procfs.c +++ b/src/helper/helper-procfs.c @@ -52,11 +52,11 @@ int proc_get_cmdline(pid_t pid, char *cmdline) snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid); fp = fopen(buf, "r"); if (fp == NULL) - return STC_ERROR_FAIL; + return STC_ERROR_FAIL; //LCOV_EXCL_LINE if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) { - fclose(fp); - return STC_ERROR_FAIL; + fclose(fp); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } fclose(fp); @@ -84,6 +84,7 @@ int proc_get_cmdline(pid_t pid, char *cmdline) return STC_ERROR_NONE; } +//LCOV_EXCL_START pid_t find_pid_from_cmdline(char *cmdline) { pid_t pid = -1, foundpid = -1; @@ -173,6 +174,7 @@ int proc_get_raw_cmdline(pid_t pid, char *buf, int len) snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); return proc_get_data(path, buf, len); } +//LCOV_EXCL_STOP int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX]) { @@ -193,8 +195,8 @@ int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX]) char *saveptr = NULL; if (fgets(status_buf, sizeof(status_buf), fp) == NULL) { - fclose(fp); - return STC_ERROR_FAIL; + fclose(fp); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } if (!updated[PROC_STATUS_NAME] && strstr(status_buf, diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index 1cd8e9d..d02667f 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -93,14 +93,15 @@ static proc_value_s * __proc_tree_lookup(const proc_key_s *key) proc_value_s *lookup; if (proc_tree == NULL) { - STC_LOGE("tree is null"); - return NULL; + STC_LOGE("tree is null"); //LCOV_EXCL_LINE + return NULL; //LCOV_EXCL_LINE } lookup = g_tree_lookup(proc_tree, key); return lookup; } +//LCOV_EXCL_START static gboolean __proc_tree_foreach_print(gpointer key, gpointer value, gpointer data) { @@ -113,10 +114,11 @@ static gboolean __proc_tree_foreach_print(gpointer key, gpointer value, return FALSE; } +//LCOV_EXCL_STOP static void __proc_tree_printall(void) { - g_tree_foreach(proc_tree, __proc_tree_foreach_print, NULL); + g_tree_foreach(proc_tree, __proc_tree_foreach_print, NULL); //LCOV_EXCL_LINE } static proc_value_s * __proc_tree_find_parent(proc_value_s *value) @@ -133,8 +135,8 @@ static proc_value_s * __proc_tree_find_parent(proc_value_s *value) } while (lookup); if (STC_DEBUG_LOG) { - if (parent != NULL) - STC_LOGD("\033[0;35mPARENT\033[0;m: tgid[\033[1;33m%s\033[0;m] pid[%s] " + if (parent != NULL) //LCOV_EXCL_LINE + STC_LOGD("\033[0;35mPARENT\033[0;m: tgid[\033[1;33m%s\033[0;m] pid[%s] " //LCOV_EXCL_LINE "ppid[\033[1;35m%s\033[0;m] cmdline[\033[0;34m%s\033[0;m] name[%s]", parent->status[PROC_STATUS_TGID], parent->status[PROC_STATUS_PID], parent->status[PROC_STATUS_PPID], parent->cmdline, @@ -151,14 +153,14 @@ static void __proc_tree_add(proc_key_s *key, proc_value_s *parent; if (proc_tree == NULL) { - STC_LOGE("tree is null"); - return; + STC_LOGE("tree is null"); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } lookup = g_tree_lookup(proc_tree, key); if (lookup) { if (STC_DEBUG_LOG) - STC_LOGD("LOOKUP: tgid[\033[1;33m%s\033[0;m] pid[%s] ppid[\033[1;35m%s\033[0;m] " + STC_LOGD("LOOKUP: tgid[\033[1;33m%s\033[0;m] pid[%s] ppid[\033[1;35m%s\033[0;m] " //LCOV_EXCL_LINE "cmdline[\033[0;34m%s\033[0;m] name[%s]", lookup->status[PROC_STATUS_TGID], lookup->status[PROC_STATUS_PID], lookup->status[PROC_STATUS_PPID], lookup->cmdline, lookup->status[PROC_STATUS_NAME]); @@ -185,8 +187,8 @@ static void __proc_tree_add(proc_key_s *key, static void __proc_tree_remove(const proc_key_s *key) { if (proc_tree == NULL) { - STC_LOGE("tree is null"); - return; + STC_LOGE("tree is null"); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } stc_manager_app_status_changed(STC_CMD_SET_TERMINATED, key->pid, NULL, @@ -195,7 +197,7 @@ static void __proc_tree_remove(const proc_key_s *key) g_tree_remove(proc_tree, key); if (STC_DEBUG_LOG) - __proc_tree_printall(); + __proc_tree_printall(); //LCOV_EXCL_LINE } static gboolean __check_excn(char *cmdline) @@ -218,26 +220,26 @@ static void __open_nl_connector_sock(void) GIOChannel *gio = NULL; if (nl_connector_sock != -1 && - nl_connector_gsource_id != 0) { - STC_LOGE("Socket is already open"); - __STC_LOG_FUNC_EXIT__; - return; + nl_connector_gsource_id != 0) { //LCOV_EXCL_LINE + STC_LOGE("Socket is already open"); //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } if (nl_connector_sock != -1) { - close(nl_connector_sock); - nl_connector_sock = -1; + close(nl_connector_sock); //LCOV_EXCL_LINE + nl_connector_sock = -1; //LCOV_EXCL_LINE } if (nl_connector_gsource_id != 0) { - g_source_remove(nl_connector_gsource_id); - nl_connector_gsource_id = 0; + g_source_remove(nl_connector_gsource_id); //LCOV_EXCL_LINE + nl_connector_gsource_id = 0; //LCOV_EXCL_LINE } nl_connector_sock = create_netlink(NETLINK_CONNECTOR, CN_IDX_PROC); if (nl_connector_sock == -1) { - __STC_LOG_FUNC_EXIT__; - return; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } gio = g_io_channel_unix_new(nl_connector_sock); @@ -266,10 +268,11 @@ static void __close_nl_connector_sock(void) static void __reopen_nl_connector_sock(void) { - __close_nl_connector_sock(); - __open_nl_connector_sock(); + __close_nl_connector_sock(); //LCOV_EXCL_LINE + __open_nl_connector_sock(); //LCOV_EXCL_LINE } +//LCOV_EXCL_START stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, pid_t pid, const gchar *app_id, @@ -389,6 +392,7 @@ stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, return ret; } +//LCOV_EXCL_STOP static void __process_event_fork(int tgid, int pid) { @@ -402,7 +406,7 @@ static void __process_event_fork(int tgid, int pid) if (__check_excn(cmdline)) { if (STC_DEBUG_LOG) - STC_LOGD("[%s] monitoring is excepted", cmdline); + STC_LOGD("[%s] monitoring is excepted", cmdline); //LCOV_EXCL_LINE return; } @@ -412,15 +416,15 @@ static void __process_event_fork(int tgid, int pid) key = MALLOC0(proc_key_s, 1); if (key == NULL) { - STC_LOGE("memory allocation failed"); - return; + STC_LOGE("memory allocation failed"); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } value = MALLOC0(proc_value_s, 1); if (value == NULL) { - STC_LOGE("memory allocation failed"); - FREE(key); - return; + STC_LOGE("memory allocation failed"); //LCOV_EXCL_LINE + FREE(key); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } key->pid = tgid; @@ -429,9 +433,9 @@ static void __process_event_fork(int tgid, int pid) g_strlcpy(value->cmdline, cmdline, sizeof(value->cmdline)); if (STC_DEBUG_LOG) { - STC_LOGD("\033[1;34mFORK\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] " + STC_LOGD("\033[1;34mFORK\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] " //LCOV_EXCL_LINE "cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, pid); - STC_LOGD("STATUS: tgid[%s] pid[%s] ppid[%s] name[%s] state[%s] tracerpid[%s]", + STC_LOGD("STATUS: tgid[%s] pid[%s] ppid[%s] name[%s] state[%s] tracerpid[%s]", //LCOV_EXCL_LINE status[PROC_STATUS_TGID], status[PROC_STATUS_PID], status[PROC_STATUS_PPID], status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]); } @@ -452,7 +456,7 @@ static void __process_event_exec(int tgid, int pid) if (__check_excn(cmdline)) { if (STC_DEBUG_LOG) - STC_LOGD("[%s] monitoring is excepted", cmdline); + STC_LOGD("[%s] monitoring is excepted", cmdline); //LCOV_EXCL_LINE return; } @@ -462,15 +466,15 @@ static void __process_event_exec(int tgid, int pid) key = MALLOC0(proc_key_s, 1); if (key == NULL) { - STC_LOGE("memory allocation failed"); - return; + STC_LOGE("memory allocation failed"); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } value = MALLOC0(proc_value_s, 1); if (value == NULL) { - STC_LOGE("memory allocation failed"); - FREE(key); - return; + STC_LOGE("memory allocation failed"); //LCOV_EXCL_LINE + FREE(key); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } key->pid = tgid; @@ -479,9 +483,9 @@ static void __process_event_exec(int tgid, int pid) g_strlcpy(value->cmdline, cmdline, sizeof(value->cmdline)); if (STC_DEBUG_LOG) { - STC_LOGD("\033[1;32mEXEC\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] " + STC_LOGD("\033[1;32mEXEC\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] " //LCOV_EXCL_LINE "cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, pid); - STC_LOGD("STATUS: tgid[%s] pid[%s] ppid[%s] name[%s] state[%s] tracerpid[%s]", + STC_LOGD("STATUS: tgid[%s] pid[%s] ppid[%s] name[%s] state[%s] tracerpid[%s]", //LCOV_EXCL_LINE status[PROC_STATUS_TGID], status[PROC_STATUS_PID], status[PROC_STATUS_PPID], status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]); } @@ -504,7 +508,7 @@ static void __process_event_exit(int tgid, int pid, int exit_code) return; if (STC_DEBUG_LOG) - STC_LOGD("\033[1;31mEXIT\033[0;m: tgid[\033[1;33m%d\033[0;m] " + STC_LOGD("\033[1;31mEXIT\033[0;m: tgid[\033[1;33m%d\033[0;m] " //LCOV_EXCL_LINE "pid[%d] exitcode[\033[0;31m%d\033[0;m]", tgid, pid, exit_code); __proc_tree_remove(&key); @@ -522,21 +526,21 @@ static gboolean __process_nl_connector_message(GIOChannel *source, (condition & G_IO_NVAL)) { /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */ - STC_LOGE("Netlink Connector socket received G_IO event, closing" + STC_LOGE("Netlink Connector socket received G_IO event, closing" //LCOV_EXCL_LINE " socket. G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]", (condition & G_IO_ERR), (condition & G_IO_HUP), (condition & G_IO_NVAL)); - __reopen_nl_connector_sock(); - __STC_LOG_FUNC_EXIT__; - return FALSE; + __reopen_nl_connector_sock(); //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE } memset(&msg, 0, sizeof(nl_connector_proc_event_s)); ret = read(sock, &msg, sizeof(nl_connector_proc_event_s)); if (ret == 0) { - __STC_LOG_FUNC_EXIT__; - return TRUE; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return TRUE; //LCOV_EXCL_LINE } switch (msg.proc_ev.what) { @@ -568,8 +572,8 @@ static int __subscribe_proc_events(void) int sock = nl_connector_sock; if (sock == -1) { - __STC_LOG_FUNC_EXIT__; - return -1; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return -1; //LCOV_EXCL_LINE } memset(&msg, 0, sizeof(nl_connector_msg_s)); @@ -586,9 +590,9 @@ static int __subscribe_proc_events(void) ret = send(sock, &msg, sizeof(nl_connector_msg_s), 0); if (ret == -1) { - STC_LOGE("Error sending netlink connector message"); - __STC_LOG_FUNC_EXIT__; - return -1; + STC_LOGE("Error sending netlink connector message"); //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return -1; //LCOV_EXCL_LINE } __STC_LOG_FUNC_EXIT__; @@ -603,8 +607,8 @@ static int __unsubscribe_proc_events(void) int sock = nl_connector_sock; if (sock == -1) { - __STC_LOG_FUNC_EXIT__; - return -1; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return -1; //LCOV_EXCL_LINE } memset(&msg, 0, sizeof(nl_connector_msg_s)); @@ -621,9 +625,9 @@ static int __unsubscribe_proc_events(void) ret = send(sock, &msg, sizeof(nl_connector_msg_s), 0); if (ret == -1) { - STC_LOGE("Error sending netlink connector message"); - __STC_LOG_FUNC_EXIT__; - return -1; + STC_LOGE("Error sending netlink connector message"); //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return -1; //LCOV_EXCL_LINE } __STC_LOG_FUNC_EXIT__; @@ -650,8 +654,8 @@ void stc_app_lifecycle_monitor_deinit(void) __STC_LOG_FUNC_ENTER__; if (nl_connector_sock == -1) { - STC_LOGE("socket already closed"); - return; + STC_LOGE("socket already closed"); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } __unsubscribe_proc_events(); diff --git a/src/monitor/stc-default-connection.c b/src/monitor/stc-default-connection.c index aa8e1e6..9a43408 100755 --- a/src/monitor/stc-default-connection.c +++ b/src/monitor/stc-default-connection.c @@ -59,19 +59,19 @@ static int __telephony_get_current_sim(void) int current_sim = 0; if (vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT_COUNT, &sim_slot_count) != 0) { - STC_LOGD("failed to get sim slot count"); - return -1; + STC_LOGD("failed to get sim slot count"); //LCOV_EXCL_LINE + return -1; //LCOV_EXCL_LINE } if (sim_slot_count == SIM_SLOT_SINGLE) { - STC_LOGD("It's single sim model"); - return current_sim; + STC_LOGD("It's single sim model"); //LCOV_EXCL_LINE + return current_sim; //LCOV_EXCL_LINE } if (vconf_get_int(VCONF_TELEPHONY_DEFAULT_DATA_SERVICE, ¤t_sim) != 0) { - STC_LOGD("failed to get default data service = %d\n", + STC_LOGD("failed to get default data service = %d\n", //LCOV_EXCL_LINE current_sim); - return -1; + return -1; //LCOV_EXCL_LINE } return current_sim; @@ -111,8 +111,8 @@ static void __telephony_get_modem_subscriber_id(GDBusConnection *connection, TELEPHONY_GET_IMSI, NULL); if (message == NULL) { - STC_LOGE("Failed to get services informations"); - goto done; + STC_LOGE("Failed to get services informations"); //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } DEBUG_PARAMS(message); @@ -122,8 +122,8 @@ static void __telephony_get_modem_subscriber_id(GDBusConnection *connection, msin_len = strlen(msin); if (msin_len + plmn_len >= IMSI_LENGTH) { - STC_LOGD("Incorrect length of mobile subscriber identifier + net id"); - goto done; + STC_LOGD("Incorrect length of mobile subscriber identifier + net id"); //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } snprintf(imsi, IMSI_LENGTH, "%s%s", plmn, msin); @@ -143,8 +143,8 @@ static void __telephony_update_default_modem_subscriber_id(GDBusConnection *conn int current_sim = __telephony_get_current_sim(); if (current_sim < 0) { - STC_LOGI("Sim not found"); - return; + STC_LOGI("Sim not found"); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } message = stc_manager_gdbus_call_sync(connection, @@ -154,8 +154,8 @@ static void __telephony_update_default_modem_subscriber_id(GDBusConnection *conn TELEPHONY_GET_MODEMS, NULL); if (message == NULL) { - STC_LOGE("Failed to get services informations"); - return; + STC_LOGE("Failed to get services informations"); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } g_variant_get(message, "(as)", &iter); @@ -167,7 +167,7 @@ static void __telephony_update_default_modem_subscriber_id(GDBusConnection *conn FREE(modem_name); break; } - current_sim--; + current_sim--; //LCOV_EXCL_LINE } __telephony_get_modem_subscriber_id(connection, default_modem_name); @@ -220,7 +220,7 @@ static gboolean __is_cellular_profile(const char *profile) if (profile == NULL) return FALSE; - return g_str_has_prefix(profile, + return g_str_has_prefix(profile, //LCOV_EXCL_LINE CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX); } @@ -229,7 +229,8 @@ static gboolean __is_wifi_profile(const char *profile) if (profile == NULL) return FALSE; - return g_str_has_prefix(profile, CONNMAN_WIFI_SERVICE_PROFILE_PREFIX); + return g_str_has_prefix(profile, //LCOV_EXCL_LINE + CONNMAN_WIFI_SERVICE_PROFILE_PREFIX); } static gboolean __is_ethernet_profile(const char *profile) @@ -237,7 +238,7 @@ static gboolean __is_ethernet_profile(const char *profile) if (profile == NULL) return FALSE; - return g_str_has_prefix(profile, + return g_str_has_prefix(profile, //LCOV_EXCL_LINE CONNMAN_ETHERNET_SERVICE_PROFILE_PREFIX); } @@ -246,7 +247,7 @@ static gboolean __is_bluetooth_profile(const char *profile) if (profile == NULL) return FALSE; - return g_str_has_prefix(profile, + return g_str_has_prefix(profile, //LCOV_EXCL_LINE CONNMAN_BLUETOOTH_SERVICE_PROFILE_PREFIX); } @@ -296,14 +297,14 @@ static void __get_default_connection_info(GDBusConnection *connection, CONNMAN_SERVICE_INTERFACE, "GetProperties", NULL); if (message == NULL) { - STC_LOGE("Failed to get services informations"); - goto done; + STC_LOGE("Failed to get services informations"); //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } g_variant_get(message, "(a{sv})", &iter); if (iter == NULL) { - STC_LOGE("Profile %s doesn't exist", object_path); - goto done; + STC_LOGE("Profile %s doesn't exist", object_path); //LCOV_EXCL_LINE + goto done; //LCOV_EXCL_LINE } while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) { @@ -314,7 +315,7 @@ static void __get_default_connection_info(GDBusConnection *connection, g_variant_get(variant, "a{sv}", &iter1); if (iter1 == NULL) - continue; + continue; //LCOV_EXCL_LINE while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant1)) { @@ -363,14 +364,14 @@ static stc_error_e __get_default_profile(GDBusConnection *connection) CONNMAN_MANAGER_INTERFACE, "GetServices", NULL); if (message == NULL) { - STC_LOGE("Failed to get profiles"); - return STC_ERROR_FAIL; + STC_LOGE("Failed to get profiles"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } g_variant_get(message, "(a(oa{sv}))", &iter); while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) { if (object_path == NULL) - continue; + continue; //LCOV_EXCL_LINE if (__is_cellular_profile(object_path) && !__is_cellular_internet_profile(object_path)) @@ -394,16 +395,16 @@ static stc_error_e __get_default_profile(GDBusConnection *connection) g_variant_unref(message); if (__is_cellular_profile(g_default_connection.path)) { - g_default_connection.type = STC_IFACE_DATACALL; - __telephony_update_default_modem_subscriber_id(connection); + g_default_connection.type = STC_IFACE_DATACALL; //LCOV_EXCL_LINE + __telephony_update_default_modem_subscriber_id(connection); //LCOV_EXCL_LINE } else if (__is_wifi_profile(g_default_connection.path)) { - g_default_connection.type = STC_IFACE_WIFI; + g_default_connection.type = STC_IFACE_WIFI; //LCOV_EXCL_LINE } else if (__is_ethernet_profile(g_default_connection.path)) { - g_default_connection.type = STC_IFACE_WIRED; + g_default_connection.type = STC_IFACE_WIRED; //LCOV_EXCL_LINE } else if (__is_bluetooth_profile(g_default_connection.path)) { - g_default_connection.type = STC_IFACE_BLUETOOTH; + g_default_connection.type = STC_IFACE_BLUETOOTH; //LCOV_EXCL_LINE } else { - g_default_connection.type = STC_IFACE_UNKNOWN; + g_default_connection.type = STC_IFACE_UNKNOWN; //LCOV_EXCL_LINE } __get_default_connection_info(connection, g_default_connection.path); @@ -448,11 +449,12 @@ static void _service_signal_cb(GDBusConnection *conn, } } else { if (g_strcmp0(g_default_connection.path, path) == 0) { - __reset_default_connection_data(); - __get_default_profile(stc->connection); + __reset_default_connection_data(); //LCOV_EXCL_LINE + __get_default_profile(stc->connection); //LCOV_EXCL_LINE } } } else if (g_strcmp0(sigvalue, "Roaming") == 0) { + //LCOV_EXCL_START if (g_strcmp0(g_default_connection.path, path) == 0) { gboolean roaming = 0; @@ -462,6 +464,7 @@ static void _service_signal_cb(GDBusConnection *conn, g_default_connection.roaming = roaming; } } + //LCOV_EXCL_STOP } else { ;//Do nothing } diff --git a/src/monitor/stc-emulator.c b/src/monitor/stc-emulator.c index 80bc170..6ccbe4f 100755 --- a/src/monitor/stc-emulator.c +++ b/src/monitor/stc-emulator.c @@ -29,17 +29,17 @@ static gboolean __stc_emulator_check_env(void) 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; + STC_LOGE("Failed to get system information(%d)", ret); //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE } if (model && strncmp(model, "Emulator", strlen("Emulator")) == 0) { - g_free(model); - return TRUE; + g_free(model); //LCOV_EXCL_LINE + return TRUE; //LCOV_EXCL_LINE } - g_free(model); - return FALSE; + g_free(model); //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE } gboolean stc_emulator_is_emulated(void) diff --git a/src/monitor/stc-exception.c b/src/monitor/stc-exception.c index 30579fc..a9e3523 100755 --- a/src/monitor/stc-exception.c +++ b/src/monitor/stc-exception.c @@ -54,13 +54,13 @@ stc_error_e table_exceptions_foreach(const stc_exceptions_info_cb exception_cb, if (process_name != NULL) data.process_name = process_name; else - data.process_name = "none"; + data.process_name = "none"; //LCOV_EXCL_LINE exe_type = strtok_r(NULL, "\n", &save_ptr); if (exe_type != NULL) data.exe_type = exe_type; else - data.exe_type = "none"; + data.exe_type = "none"; //LCOV_EXCL_LINE if (exception_cb(&data, user_data) == STC_CANCEL) break; @@ -80,7 +80,7 @@ static int __pkginfo_filter_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data if (ret == PMINFO_R_OK) { if (g_hash_table_insert(g_pkginfo_filter_hash, g_strdup(pkgname), g_strdup(EXE_TYPE_APPLICATION)) != TRUE) - STC_LOGE("Failed to insert hash table"); + STC_LOGE("Failed to insert hash table"); //LCOV_EXCL_LINE } return STC_CONTINUE; @@ -104,7 +104,7 @@ static int __pkginfo_pkg_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data) data.exe_type = EXE_TYPE_APPLICATION; if (excn_cb(&data, NULL) == STC_CANCEL) - STC_LOGE("Failed to insert hash table"); + STC_LOGE("Failed to insert hash table"); //LCOV_EXCL_LINE } return STC_CONTINUE; @@ -130,24 +130,24 @@ stc_error_e pkginfo_exceptions_foreach(const stc_exceptions_info_cb exception_cb PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE, INTERNET_PRIVILEGE); if (ret != PMINFO_R_OK) { - STC_LOGE("Failed to add pkginfo filter string"); - err = STC_ERROR_FAIL; - goto out; + STC_LOGE("Failed to add pkginfo filter string"); //LCOV_EXCL_LINE + err = STC_ERROR_FAIL; //LCOV_EXCL_LINE + goto out; //LCOV_EXCL_LINE } ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle, __pkginfo_filter_list_cb, NULL); if (ret != PMINFO_R_OK) { - STC_LOGE("Failed to foreach pkginfo filter"); - err = STC_ERROR_FAIL; - goto out; + STC_LOGE("Failed to foreach pkginfo filter"); //LCOV_EXCL_LINE + err = STC_ERROR_FAIL; //LCOV_EXCL_LINE + goto out; //LCOV_EXCL_LINE } ret = pkgmgrinfo_pkginfo_get_list(__pkginfo_pkg_list_cb, exception_cb); if (ret != PMINFO_R_OK) { - STC_LOGE("Failed to get pkginfo list"); - err = STC_ERROR_FAIL; - goto out; + STC_LOGE("Failed to get pkginfo list"); //LCOV_EXCL_LINE + err = STC_ERROR_FAIL; //LCOV_EXCL_LINE + goto out; //LCOV_EXCL_LINE } out: diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 268c5cf..62a2c5c 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -277,6 +277,7 @@ static void __rstns_tree_key_free(gpointer data) FREE(key); } +//LCOV_EXCL_START static gboolean __processes_tree_foreach_print(gpointer key, gpointer value, gpointer data) { @@ -314,6 +315,7 @@ static void __apps_tree_printall(void) { g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL); } +//LCOV_EXCL_STOP static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value, gpointer data) @@ -323,7 +325,7 @@ static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value, if (!g_tree_remove(app_value->processes, context->proc_key)) { if (STC_DEBUG_LOG) - STC_LOGD("key not found"); + STC_LOGD("key not found"); //LCOV_EXCL_LINE return FALSE; } @@ -357,6 +359,7 @@ static stc_process_value_s * __process_lookup(GTree *processes, return lookup; } +//LCOV_EXCL_START static gboolean __processes_tree_check_empty(gpointer key, gpointer value, gpointer data) { @@ -364,6 +367,7 @@ static gboolean __processes_tree_check_empty(gpointer key, gpointer value, (*pid_count)++; return TRUE; } +//LCOV_EXCL_STOP static gboolean __add_application_monitor(gpointer key, gpointer value, gpointer data) @@ -381,11 +385,11 @@ static gboolean __add_application_monitor(gpointer key, gpointer value, struct nfacct_rule counter; if (!stc->carg) { - stc->carg = MALLOC0(counter_arg_s, 1); - if (stc->carg == NULL) - return FALSE; + stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE + if (stc->carg == NULL) //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE - stc->carg->sock = stc_monitor_get_counter_socket(); + stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE } memset(&counter, 0, sizeof(struct nfacct_rule)); @@ -424,11 +428,11 @@ static gboolean __remove_application_monitor(gpointer key, gpointer value, struct nfacct_rule counter; if (!stc->carg) { - stc->carg = MALLOC0(counter_arg_s, 1); - if (stc->carg == NULL) - return FALSE; + stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE + if (stc->carg == NULL) //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE - stc->carg->sock = stc_monitor_get_counter_socket(); + stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE } memset(&counter, 0, sizeof(struct nfacct_rule)); @@ -506,12 +510,12 @@ static void __process_restriction(enum traffic_restriction_type rst_type, effective_data_warn_limit -= info.data_counter; if (effective_data_limit < 0) { - effective_data_limit = 0; - rstn_value->data_limit_reached = TRUE; + effective_data_limit = 0; //LCOV_EXCL_LINE + rstn_value->data_limit_reached = TRUE; //LCOV_EXCL_LINE } if (effective_data_warn_limit < 0) - effective_data_warn_limit = 0; + effective_data_warn_limit = 0; //LCOV_EXCL_LINE STC_LOGD("datausage [%lld] bytes", info.data_counter); } @@ -528,19 +532,18 @@ static void __process_restriction(enum traffic_restriction_type rst_type, struct nfacct_rule counter; stc_s *stc = stc_get_manager(); if (!stc) { - g_free(default_ifname); - return; + g_free(default_ifname); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } if (!stc->carg) { - stc->carg = MALLOC0(counter_arg_s, 1); - if (stc->carg == NULL) { - g_free(default_ifname); - return; + stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE + if (stc->carg == NULL) { //LCOV_EXCL_LINE + g_free(default_ifname); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } - stc->carg->sock = - stc_monitor_get_counter_socket(); + stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE } counter.carg = stc->carg; @@ -574,19 +577,18 @@ static void __process_restriction(enum traffic_restriction_type rst_type, struct nfacct_rule counter; stc_s *stc = stc_get_manager(); if (!stc) { - g_free(default_ifname); - return; + g_free(default_ifname); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } if (!stc->carg) { - stc->carg = MALLOC0(counter_arg_s, 1); - if (stc->carg == NULL) { - g_free(default_ifname); - return; + stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE + if (stc->carg == NULL) { //LCOV_EXCL_LINE + g_free(default_ifname); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } - stc->carg->sock = - stc_monitor_get_counter_socket(); + stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE } counter.carg = stc->carg; @@ -616,6 +618,7 @@ static void __process_restriction(enum traffic_restriction_type rst_type, } } +//LCOV_EXCL_START static gboolean __remove_rstns_foreach_application(gpointer key, gpointer value, gpointer data) @@ -643,6 +646,7 @@ static gboolean __remove_rstns_foreach_application(gpointer key, out: return FALSE; } +//LCOV_EXCL_STOP static void __remove_rstns_for_application(gchar *app_id) { @@ -660,8 +664,8 @@ static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key) lookup = __application_lookup(g_system->apps, app_key); if (!lookup) { - STC_LOGE("app_key not found"); - return STC_ERROR_NO_DATA; + STC_LOGE("app_key not found"); //LCOV_EXCL_LINE + return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE } g_tree_foreach(lookup->processes, __processes_tree_check_empty, @@ -675,8 +679,8 @@ static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key) } if (!g_tree_remove(g_system->apps, app_key)) { - ret = STC_ERROR_NO_DATA; - STC_LOGE("key not found"); + ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE + STC_LOGE("key not found"); //LCOV_EXCL_LINE } return ret; @@ -704,6 +708,7 @@ static gboolean __process_contr_reply(GIOChannel *source, GIOCondition condition, gpointer user_data); +//LCOV_EXCL_START static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system) { GIOChannel *gio = NULL; @@ -854,8 +859,8 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, goto try_next_callback; if (rstn_value->data_limit_reached == TRUE) { - context->data_limit_reached = TRUE; - goto try_next_callback; + context->data_limit_reached = TRUE; //LCOV_EXCL_LINE + goto try_next_callback; //LCOV_EXCL_LINE } classid = context->counter->classid; @@ -870,6 +875,7 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, try_next_callback: return rv; } +//LCOV_EXCL_STOP static gboolean __update_app_statistics(gpointer key, gpointer value, gpointer data) @@ -890,7 +896,7 @@ static gboolean __update_app_statistics(gpointer key, gpointer value, if (STC_IFACE_DATACALL == stat_key.iftype) stat_key.subscriber_id = g_strdup(default_connection->subscriber_id); else - stat_key.subscriber_id = g_strdup("none_subid"); + stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE g_strlcpy(stat_key.ifname, default_connection->ifname, MAX_IFACE_LENGTH); @@ -938,6 +944,7 @@ static gboolean __flush_apps_stats_to_database(gpointer user_data) return G_SOURCE_REMOVE; } +//LCOV_EXCL_START static gboolean __update_counter_statistics(gpointer key, gpointer value, gpointer data) { @@ -969,6 +976,7 @@ static gboolean __flush_rstns_counter_to_database(gpointer user_data) STC_LOGI("Flushed rstns counters to database"); return G_SOURCE_REMOVE; } +//LCOV_EXCL_STOP static void __app_counter_update(stc_app_key_s *app_key, stc_app_value_s *app_value, @@ -981,7 +989,7 @@ static void __app_counter_update(stc_app_key_s *app_key, g_system->apps_tree_updated = TRUE; if (STC_DEBUG_LOG) - __apps_tree_foreach_print(app_key, app_value, NULL); + __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE break; case NFACCT_COUNTER_OUT: app_value->data_usage.out_bytes += context->bytes; @@ -989,10 +997,10 @@ static void __app_counter_update(stc_app_key_s *app_key, g_system->apps_tree_updated = TRUE; if (STC_DEBUG_LOG) - __apps_tree_foreach_print(app_key, app_value, NULL); + __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE break; default: - STC_LOGE("unknown iotype"); + STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE } } @@ -1049,11 +1057,11 @@ static void __fill_nfacct_result(char *cnt_name, int64_t bytes, }; if (STC_DEBUG_LOG) - STC_LOGD("cnt_name %s", cnt_name); + STC_LOGD("cnt_name %s", cnt_name); //LCOV_EXCL_LINE if (!recreate_counter_by_name(cnt_name, &counter)) { - STC_LOGE("Can't parse counter name %s", cnt_name); - return; + STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld", @@ -1120,8 +1128,8 @@ static void __process_network_counter(struct genl *ans, netlink_serialization_command *netlink = netlink_create_command(&ser_params); if (!netlink) { - STC_LOGE("Can not create command"); - return; + STC_LOGE("Can not create command"); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } netlink->deserialize_answer(&(netlink->params)); @@ -1136,27 +1144,32 @@ static gboolean __process_contr_reply(GIOChannel *source, int ret; stc_s *stc = stc_get_manager(); +#ifdef TIZEN_GTESTS + void __gcov_flush(void); + __gcov_flush(); +#endif + if ((condition & G_IO_ERR) || (condition & G_IO_HUP) || (condition & G_IO_NVAL)) { /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */ - STC_LOGE("Counter socket received G_IO event, closing socket." + STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE "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; + __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE } ans = MALLOC0(struct genl, 1); if (ans == NULL) { - STC_LOGE("Failed allocate memory to genl reply message"); - return TRUE; + STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE + return TRUE; //LCOV_EXCL_LINE } if (stc == NULL) { - STC_LOGE("Can't get stc data"); - goto out; + STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE + goto out; //LCOV_EXCL_LINE } ret = read_netlink(sock, ans, sizeof(struct genl)); @@ -1170,6 +1183,7 @@ static gboolean __process_contr_reply(GIOChannel *source, g_idle_add(__flush_apps_stats_to_database, NULL); g_idle_add(__flush_rstns_counter_to_database, NULL); out: + FREE(ans); return TRUE; } @@ -1187,6 +1201,11 @@ static gboolean __update_contr_cb(void *user_data) stc->carg->sock = stc_monitor_get_counter_socket(); } +#ifdef TIZEN_GTESTS + void __gcov_flush(void); + __gcov_flush(); +#endif + /* STC_LOGD("Get all counters"); */ nfacct_send_get_all(stc->carg); @@ -1256,6 +1275,7 @@ static gboolean __add_restriction_debug(gpointer key, gpointer value, return FALSE; } +//LCOV_EXCL_START static gboolean __add_restriction(gpointer key, gpointer value, gpointer data) { stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key; @@ -1272,6 +1292,7 @@ static gboolean __add_restriction(gpointer key, gpointer value, gpointer data) return FALSE; } +//LCOV_EXCL_STOP static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key) { @@ -1281,8 +1302,8 @@ static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key) lookup_value = __rstn_lookup(g_system->rstns, key); if (!lookup_value) { - STC_LOGE("key not found"); - return STC_ERROR_NO_DATA; + STC_LOGE("key not found"); //LCOV_EXCL_LINE + return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE } __remove_restriction(key, lookup_value, NULL); @@ -1291,8 +1312,8 @@ static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key) table_counters_delete(lookup_value->restriction_id); if (!g_tree_remove(g_system->rstns, key)) { - STC_LOGD("key not found"); - return STC_ERROR_NO_DATA; + STC_LOGD("key not found"); //LCOV_EXCL_LINE + return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE } return STC_ERROR_NONE; @@ -1309,15 +1330,15 @@ static stc_error_e __rstn_tree_add(stc_rstn_key_s *key, if (!rstn_value) { stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1); if (!rstn_key) { - STC_LOGE("rstn_key allocation failed"); - return STC_ERROR_OUT_OF_MEMORY; + STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } rstn_value = MALLOC0(stc_rstn_value_s, 1); if (!rstn_value) { - STC_LOGE("rstn_value allocation failed"); - FREE(rstn_key); - return STC_ERROR_OUT_OF_MEMORY; + STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE + FREE(rstn_key); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } rstn_key->app_id = g_strdup(key->app_id); @@ -1346,6 +1367,7 @@ static stc_error_e __rstn_tree_add(stc_rstn_key_s *key, return STC_ERROR_NONE; } +//LCOV_EXCL_START static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info, void *user_data) { @@ -1419,6 +1441,7 @@ static gboolean __add_rstn_foreach_application(gpointer key, out: return FALSE; } +//LCOV_EXCL_STOP static void __add_rstns_for_application(gchar *app_id) { @@ -1432,7 +1455,7 @@ static void __add_application_by_interface(const char *app_id) stc_app_value_s app_value; if (app_id == NULL) - return; + return; //LCOV_EXCL_LINE memset(&app_key, 0, sizeof(stc_app_key_s)); memset(&app_value, 0, sizeof(stc_app_value_s)); @@ -1457,21 +1480,22 @@ static int __vconf_get_int(const char *key, int *value) ret = vconf_get_int(key, value); if (ret != VCONF_OK) { - STC_LOGE("Failed to get vconfkey [%s] value", key); - return -1; + STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE + return -1; //LCOV_EXCL_LINE } return 0; } +//LCOV_EXCL_START static int __vconf_set_int(const char *key, int value) { int ret = 0; ret = vconf_set_int(key, value); if (ret != VCONF_OK) { - STC_LOGE("Failed to set vconfkey [%s] value", key); - return -1; + STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE + return -1; //LCOV_EXCL_LINE } return 0; @@ -1523,6 +1547,7 @@ static stc_error_e __process_update_background(void) return STC_ERROR_NONE; } +//LCOV_EXCL_STOP #if 0 static void __excn_hash_foreach_print(gpointer key, gpointer value, @@ -1542,6 +1567,7 @@ static void __excn_hash_printall(void) } #endif +//LCOV_EXCL_START static gboolean __remove_exception_app(gpointer key, gpointer value, gpointer data) { @@ -1552,10 +1578,11 @@ static gboolean __remove_exception_app(gpointer key, gpointer value, return FALSE; } +//LCOV_EXCL_STOP static void __remove_exception_appall(void) { - g_hash_table_foreach_remove(g_system->excns_hash, + g_hash_table_foreach_remove(g_system->excns_hash, //LCOV_EXCL_LINE __remove_exception_app, NULL); } @@ -1567,7 +1594,7 @@ static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info, if (g_hash_table_insert(g_system->excns_hash, g_strdup(info->process_name), g_strdup(info->exe_type)) != TRUE) - ret = STC_CANCEL; + ret = STC_CANCEL; //LCOV_EXCL_LINE return ret; } @@ -1580,6 +1607,7 @@ static void __fill_exceptions_list(void) /* __excn_hash_printall(); */ } +//LCOV_EXCL_START static gboolean __update_exceptions_app_list(void *user_data) { __remove_exception_appall(); @@ -1589,6 +1617,7 @@ static gboolean __update_exceptions_app_list(void *user_data) return TRUE; } +//LCOV_EXCL_STOP stc_error_e stc_monitor_init(void) { @@ -1618,9 +1647,9 @@ stc_error_e stc_monitor_init(void) /* create netlink socket for updating kernel counters */ system->contr_sock = create_netlink(NETLINK_NETFILTER, 0); if (system->contr_sock < 0) { - STC_LOGE("failed to open socket"); - FREE(system); - return STC_ERROR_FAIL; + STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE + FREE(system); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } gio = g_io_channel_unix_new(system->contr_sock); @@ -1646,9 +1675,9 @@ stc_error_e stc_monitor_init(void) __update_contr_cb, NULL); if (g_system->contr_timer_id == 0) { - STC_LOGE("Failed to register kernel counters update timer"); - __close_contr_sock(g_system); - return STC_ERROR_FAIL; + STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE + __close_contr_sock(g_system); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE, @@ -1712,21 +1741,21 @@ stc_error_e stc_monitor_application_add(const stc_app_key_s app_key, lookup = __application_lookup(g_system->apps, &app_key); if (lookup) { - STC_LOGD("app_key already present"); - return STC_ERROR_NONE; + STC_LOGD("app_key already present"); //LCOV_EXCL_LINE + return STC_ERROR_NONE; //LCOV_EXCL_LINE } key = MALLOC0(stc_app_key_s, 1); if (!key) { - STC_LOGE("key allocation failed"); - return STC_ERROR_OUT_OF_MEMORY; + STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } value = MALLOC0(stc_app_value_s, 1); if (!value) { - STC_LOGE("value allocation failed"); - FREE(key); - return STC_ERROR_OUT_OF_MEMORY; + STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE + FREE(key); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } key->app_id = g_strdup(app_key.app_id); @@ -1766,27 +1795,27 @@ stc_error_e stc_monitor_process_add(const stc_app_key_s app_key, app_lookup = __application_lookup(g_system->apps, &app_key); if (!app_lookup) { - STC_LOGD("app_key not found"); - return STC_ERROR_FAIL; + STC_LOGD("app_key not found"); //LCOV_EXCL_LINE + return STC_ERROR_FAIL; //LCOV_EXCL_LINE } proc_lookup = __process_lookup(app_lookup->processes, &proc_key); if (proc_lookup) { - STC_LOGD("proc_key already present"); - return STC_ERROR_NONE; + STC_LOGD("proc_key already present"); //LCOV_EXCL_LINE + return STC_ERROR_NONE; //LCOV_EXCL_LINE } key = MALLOC0(stc_process_key_s, 1); if (!key) { - STC_LOGE("key allocation failed"); - return STC_ERROR_OUT_OF_MEMORY; + STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } value = MALLOC0(stc_process_value_s, 1); if (!value) { - STC_LOGE("value allocation failed"); - FREE(key); - return STC_ERROR_OUT_OF_MEMORY; + STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE + FREE(key); //LCOV_EXCL_LINE + return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } key->pid = proc_key.pid; @@ -1799,7 +1828,7 @@ stc_error_e stc_monitor_process_add(const stc_app_key_s app_key, place_pids_to_net_cgroup(proc_key.pid, app_key.app_id); if (STC_DEBUG_LOG) - __apps_tree_printall(); + __apps_tree_printall(); //LCOV_EXCL_LINE return ret; } @@ -1826,11 +1855,12 @@ stc_error_e stc_monitor_process_remove(pid_t pid) __application_remove_if_empty(context.app_key); if (STC_DEBUG_LOG) - __apps_tree_printall(); + __apps_tree_printall(); //LCOV_EXCL_LINE return ret; } +//LCOV_EXCL_START stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key, const stc_process_key_s proc_key, stc_app_state_e ground) @@ -1863,6 +1893,7 @@ stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key, return ret; } +//LCOV_EXCL_STOP void stc_monitor_update_rstn_by_default_connection(void *data) { @@ -1870,6 +1901,7 @@ void stc_monitor_update_rstn_by_default_connection(void *data) default_connection_s *new_connection = (default_connection_s *)data; if (old_connection.path != NULL) { + //LCOV_EXCL_START if (g_system->apps) g_tree_foreach(g_system->apps, __remove_application_monitor, @@ -1881,6 +1913,7 @@ void stc_monitor_update_rstn_by_default_connection(void *data) (gpointer)&old_connection); iptables_flush_chains(); + //LCOV_EXCL_STOP } FREE(old_connection.path); @@ -1930,9 +1963,9 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info) value.classid = STC_UNKNOWN_CLASSID; if (value.classid == STC_BACKGROUND_APP_CLASSID) { - __set_background_state(TRUE); - __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); - __process_update_background(); + __set_background_state(TRUE); //LCOV_EXCL_LINE + __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE + __process_update_background(); //LCOV_EXCL_LINE } value.data_limit = info->data_limit; @@ -1959,9 +1992,9 @@ stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info) }; if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) { - __set_background_state(FALSE); - __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); - __process_update_background(); + __set_background_state(FALSE); //LCOV_EXCL_LINE + __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE + __process_update_background(); //LCOV_EXCL_LINE } ret = __rstn_tree_remove(&key); diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index bf556e6..df53f9b 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -15,6 +15,7 @@ */ #include "stc-manager-gdbus.h" +#include "stc-manager.h" #include "stc-statistics.h" #include "stc-restriction.h" #include "stc-default-connection.h" @@ -129,6 +130,37 @@ static gboolean __stc_manager_gdbus_restriction_init(stc_s *stc) return ret; } +static gboolean __stc_manager_gdbus_manager_init(stc_s *stc) +{ + __STC_LOG_FUNC_ENTER__; + gboolean ret = TRUE; + gchar *s = NULL; + + StcObjectSkeleton *object = NULL; + StcManager *manager = NULL; + s = g_strdup_printf(STC_DBUS_SERVICE_MANAGER_PATH); + + object = stc_object_skeleton_new(s); + g_free(s); + + manager = stc_manager_skeleton_new(); + stc_object_skeleton_set_manager(object, manager); + g_object_unref(manager); + + g_signal_connect(manager, "handle-stop", + G_CALLBACK(handle_manager_stop), stc); + + g_dbus_object_manager_server_export(stc->obj_mgr, + G_DBUS_OBJECT_SKELETON(object)); + g_object_unref(object); + + stc->manager_obj = (gpointer)manager; + + __STC_LOG_FUNC_EXIT__; + return ret; +} + + static void __stc_manager_gdbus_on_bus_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data) @@ -143,12 +175,17 @@ static void __stc_manager_gdbus_on_bus_acquired(GDBusConnection *connection, stc->connection = connection; if (__stc_manager_gdbus_statistics_init(stc) == FALSE) { - STC_LOGE("Can not signal connect to statistics"); + STC_LOGE("Can not signal connect to statistics"); //LCOV_EXCL_LINE /* Deinitialize and quit manager */ } if (__stc_manager_gdbus_restriction_init(stc) == FALSE) { - STC_LOGE("Cannot signal connect to restriction"); + STC_LOGE("Cannot signal connect to restriction"); //LCOV_EXCL_LINE + /* Deinitialize and quit manager */ + } + + if (__stc_manager_gdbus_manager_init(stc) == FALSE) { + STC_LOGE("Cannot signal connect to manager"); //LCOV_EXCL_LINE /* Deinitialize and quit manager */ } @@ -169,12 +206,14 @@ static void __stc_manager_gdbus_on_name_acquired(GDBusConnection *connection, STC_LOGD("name : %s", name); } +//LCOV_EXCL_START static void __stc_manager_gdbus_on_name_lost(GDBusConnection *connection, const gchar *name, gpointer user_data) { STC_LOGD("name : %s", name); } +//LCOV_EXCL_STOP void stc_manager_gdbus_init(gpointer stc_data) { @@ -205,6 +244,7 @@ void stc_manager_gdbus_deinit(gpointer stc_data) stc->statistics_obj = NULL; stc->restriction_obj = NULL; + stc->manager_obj = NULL; __STC_LOG_FUNC_EXIT__; } @@ -217,8 +257,8 @@ GVariant *stc_manager_gdbus_call_sync(GDBusConnection *connection, GVariant *reply = NULL; if (connection == NULL) { - STC_LOGE("Failed to get GDBusconnection"); - return reply; + STC_LOGE("Failed to get GDBusconnection"); //LCOV_EXCL_LINE + return reply; //LCOV_EXCL_LINE } reply = g_dbus_connection_call_sync(connection, @@ -235,11 +275,11 @@ GVariant *stc_manager_gdbus_call_sync(GDBusConnection *connection, if (reply == NULL) { if (error != NULL) { - STC_LOGE("g_dbus_connection_call_sync() failed" + STC_LOGE("g_dbus_connection_call_sync() failed" //LCOV_EXCL_LINE "error [%d: %s]", error->code, error->message); - g_error_free(error); + g_error_free(error); //LCOV_EXCL_LINE } else { - STC_LOGE("g_dbus_connection_call_sync() failed"); + STC_LOGE("g_dbus_connection_call_sync() failed"); //LCOV_EXCL_LINE } return NULL; @@ -260,8 +300,8 @@ guint stc_manager_gdbus_subscribe_signal(GDBusConnection *connection, GDestroyNotify user_data_free_func) { if (connection == NULL) { - STC_LOGE("Failed to get GDBusconnection"); - return 0; + STC_LOGE("Failed to get GDBusconnection"); //LCOV_EXCL_LINE + return 0; //LCOV_EXCL_LINE } return g_dbus_connection_signal_subscribe(connection, @@ -280,8 +320,8 @@ void stc_manager_gdbus_unsubscribe_signal(GDBusConnection *connection, guint subscription_id) { if (connection == NULL) { - STC_LOGE("Failed to get GDBusconnection"); - return; + STC_LOGE("Failed to get GDBusconnection"); //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } g_dbus_connection_signal_unsubscribe(connection, subscription_id); @@ -296,8 +336,8 @@ void stc_manager_gdbus_dict_foreach(GVariantIter *iter, dbus_dict_cb cb, GVariant *value = NULL; if (!cb) { - __STC_LOG_FUNC_EXIT__; - return; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } while (g_variant_iter_loop(iter, "{sv}", &key, &value)) { @@ -319,7 +359,7 @@ gboolean stc_manager_dbus_emit_signal(GDBusConnection *connection, GError *error = NULL; if (connection == NULL) { - STC_LOGE("GDBusconnection is NULL"); + STC_LOGE("GDBusconnection is NULL"); //LCOV_EXCL_LINE return 0; } @@ -333,9 +373,9 @@ gboolean stc_manager_dbus_emit_signal(GDBusConnection *connection, parameters, &error); if (rv != TRUE) { - STC_LOGE("Failed to emit signal [%s] interface [%s] Error [%s]", + STC_LOGE("Failed to emit signal [%s] interface [%s] Error [%s]", //LCOV_EXCL_LINE signal_name, interface_name, error->message); - g_error_free(error); + g_error_free(error); //LCOV_EXCL_LINE } else { STC_LOGD("[%s] signal sent on [%s] interface", signal_name, interface_name); @@ -343,3 +383,22 @@ gboolean stc_manager_dbus_emit_signal(GDBusConnection *connection, return rv; } + +gboolean handle_manager_stop(StcManager *object, + GDBusMethodInvocation *invocation) +{ + __STC_LOG_FUNC_ENTER__; + GVariant *return_parameters = NULL; + + STC_LOGI("stc manager stop"); + + return_parameters = g_variant_new("(i)", STC_ERROR_NONE); + + DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters); + STC_DBUS_REPLY(invocation, return_parameters); + + stc_stop_manager(); + + __STC_LOG_FUNC_EXIT__; + return TRUE; +} diff --git a/src/stc-manager-plugin.c b/src/stc-manager-plugin.c index d1e6d30..a97ae73 100755 --- a/src/stc-manager-plugin.c +++ b/src/stc-manager-plugin.c @@ -24,6 +24,7 @@ static gboolean stc_plugin_enabled = FALSE; static void *handle_plugin; static stc_plugin_s *stc_plugin; +//LCOV_EXCL_START void stc_manager_plugin_init(void) { __STC_LOG_FUNC_ENTER__; @@ -150,3 +151,4 @@ int stc_deregister_state_changed_cb(stc_s *stc) __STC_LOG_FUNC_EXIT__; return stc_plugin->deregister_state_changed_cb(stc); } +//LCOV_EXCL_STOP diff --git a/src/stc-manager-util.c b/src/stc-manager-util.c index 17f9d83..b929427 100755 --- a/src/stc-manager-util.c +++ b/src/stc-manager-util.c @@ -40,10 +40,10 @@ static GKeyFile *__load_key_file(const char *path) keyfile = g_key_file_new(); if (!g_key_file_load_from_file(keyfile, path, 0, &error)) { - STC_LOGD("Unable to load [%s] : %s", path, error->message); - g_clear_error(&error); - g_key_file_free(keyfile); - keyfile = NULL; + STC_LOGD("Unable to load [%s] : %s", path, error->message); //LCOV_EXCL_LINE + g_clear_error(&error); //LCOV_EXCL_LINE + g_key_file_free(keyfile); //LCOV_EXCL_LINE + keyfile = NULL; //LCOV_EXCL_LINE } return keyfile; @@ -59,9 +59,9 @@ static int __save_key_file(GKeyFile *keyfile, char *path) data = g_key_file_to_data(keyfile, &length, NULL); if (!g_file_set_contents(path, data, length, &error)) { - STC_LOGD("Failed to save information : %s", error->message); - g_error_free(error); - ret = -EIO; + STC_LOGD("Failed to save information : %s", error->message); //LCOV_EXCL_LINE + g_error_free(error); //LCOV_EXCL_LINE + ret = -EIO; //LCOV_EXCL_LINE } __sync_file_to_disk(path); @@ -70,6 +70,7 @@ static int __save_key_file(GKeyFile *keyfile, char *path) return ret; } +//LCOV_EXCL_START gboolean stc_util_get_config_bool(char *key) { char path[MAX_PATH_LENGTH]; @@ -105,6 +106,7 @@ gchar * stc_util_get_config_str(char *key) return value; } +//LCOV_EXCL_STOP int stc_util_get_config_int(char *key) { @@ -116,7 +118,7 @@ int stc_util_get_config_int(char *key) keyfile = __load_key_file(path); if (!keyfile) - keyfile = g_key_file_new(); + keyfile = g_key_file_new(); //LCOV_EXCL_LINE value = g_key_file_get_integer(keyfile, path, key, NULL); @@ -125,10 +127,12 @@ int stc_util_get_config_int(char *key) return value; } +//LCOV_EXCL_START API void stc_util_set_debuglog(int debuglog) { g_debuglog = debuglog; } +//LCOV_EXCL_STOP API int stc_util_get_debuglog(void) { @@ -147,7 +151,7 @@ void stc_util_initialize_config(void) keyfile = __load_key_file(path); if (!keyfile) - keyfile = g_key_file_new(); + keyfile = g_key_file_new(); //LCOV_EXCL_LINE g_key_file_set_integer(keyfile, path, INFO_DEBUGLOG, 0); diff --git a/src/stc-manager.c b/src/stc-manager.c index 5e7ddd6..d9a4d80 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -59,6 +59,7 @@ static void __stc_manager_deinit(void) stc_deinit_db_guard(); stc_db_deinitialize(); + iptables_flush_chains(); iptables_deinit(); stc_manager_gdbus_deinit((gpointer)g_stc); stc_app_lifecycle_monitor_deinit(); @@ -77,8 +78,8 @@ static stc_s *__stc_manager_init(void) stc = MALLOC0(stc_s, 1); if (!stc) { - STC_LOGE("Failed to allocate memory for manager structure"); - return NULL; + STC_LOGE("Failed to allocate memory for manager structure"); //LCOV_EXCL_LINE + return NULL; //LCOV_EXCL_LINE } g_stc = stc; @@ -101,9 +102,9 @@ static stc_s *__stc_manager_init(void) return stc; handle_error: - STC_LOGD("Failed to initialize stc manager"); - __stc_manager_deinit(); - return NULL; + STC_LOGD("Failed to initialize stc manager"); //LCOV_EXCL_LINE + __stc_manager_deinit(); //LCOV_EXCL_LINE + return NULL; //LCOV_EXCL_LINE } stc_s *stc_get_manager(void) @@ -111,6 +112,12 @@ stc_s *stc_get_manager(void) return g_stc; } +void stc_stop_manager(void) +{ + if (g_stc && g_stc->main_loop) + g_main_loop_quit(g_stc->main_loop); +} + gint32 main(gint32 argc, gchar *argv[]) { GMainLoop *main_loop = NULL; @@ -118,8 +125,12 @@ gint32 main(gint32 argc, gchar *argv[]) STC_LOGI("Smart Traffic Control Manager"); +#ifdef TIZEN_GTESTS + setenv("GCOV_PREFIX", "/tmp/daemon", 1); +#endif + if (daemon(0, 0) != 0) - STC_LOGE("Can't start daemon"); + STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE /* Initialize required subsystems */ #if !GLIB_CHECK_VERSION(2, 35, 0) diff --git a/src/stc-restriction.c b/src/stc-restriction.c index ab3d350..e8a8a7d 100755 --- a/src/stc-restriction.c +++ b/src/stc-restriction.c @@ -56,34 +56,34 @@ gboolean __validate_rstn_rule(table_restrictions_info *rule, __STC_LOG_FUNC_ENTER__; if (rule == NULL) { - __STC_LOG_FUNC_EXIT__; - return FALSE; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE } if (rst_type <= RST_UNDEFINDED || rst_type >= RST_MAX_VALUE) { - __STC_LOG_FUNC_EXIT__; - return FALSE; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE } if (rule->iftype <= STC_IFACE_UNKNOWN || rule->iftype >= STC_IFACE_LAST_ELEM) { - __STC_LOG_FUNC_EXIT__; - return FALSE; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE } if (rule->roaming >= STC_ROAMING_LAST_ELEM) { - __STC_LOG_FUNC_EXIT__; - return FALSE; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE } if (rule->subscriber_id == NULL) { - __STC_LOG_FUNC_EXIT__; - return FALSE; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE } if (rule->app_id == NULL) { - __STC_LOG_FUNC_EXIT__; - return FALSE; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return FALSE; //LCOV_EXCL_LINE } __STC_LOG_FUNC_EXIT__; @@ -96,8 +96,8 @@ void __stc_restriction_app_info_builder_add(GVariantBuilder *builder, __STC_LOG_FUNC_ENTER__; if (!builder || !info) { - __STC_LOG_FUNC_EXIT__; - return; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } g_variant_builder_add(builder, "{sv}", "app_id", @@ -135,8 +135,8 @@ stc_cb_ret_e __table_restrictions_foreach_app_cb(const table_restrictions_info * GVariantBuilder sub_builder; if (!info || !builder) { - __STC_LOG_FUNC_EXIT__; - return STC_CANCEL; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_CANCEL; //LCOV_EXCL_LINE } g_variant_builder_init(&sub_builder, G_VARIANT_TYPE("a{sv}")); @@ -156,8 +156,8 @@ stc_cb_ret_e __table_restrictions_per_app_cb(const table_restrictions_info *info GVariantBuilder *builder = (GVariantBuilder *)user_data; if (!info || !builder) { - __STC_LOG_FUNC_EXIT__; - return STC_CANCEL; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_CANCEL; //LCOV_EXCL_LINE } __stc_restriction_app_info_builder_add(builder, info); @@ -174,8 +174,8 @@ static void __stc_extract_restriction_rule(const char *key, GVariant *value, table_restrictions_info *rule = (table_restrictions_info *) user_data; if (rule == NULL) { - __STC_LOG_FUNC_EXIT__; - return; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } if (!g_strcmp0(key, "app_id")) { @@ -213,7 +213,7 @@ static void __stc_extract_restriction_rule(const char *key, GVariant *value, STC_LOGD("subscriber_id: [%s]", rule->subscriber_id); } else { - STC_LOGD("Unknown select rule"); + STC_LOGD("Unknown select rule"); //LCOV_EXCL_LINE } __STC_LOG_FUNC_EXIT__; @@ -242,9 +242,9 @@ gboolean handle_restriction_set(StcRestriction *object, rule.rst_state = STC_RESTRICTION_REMOVED; if (__validate_rstn_rule(&rule, RST_SET) == FALSE) { - STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, + STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE STC_ERROR_INVALID_PARAMETER); - __STC_LOG_FUNC_EXIT__; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } @@ -280,9 +280,9 @@ gboolean handle_restriction_exclude(StcRestriction *object, rule.rst_state = STC_RESTRICTION_EXCLUDED; if (__validate_rstn_rule(&rule, RST_EXCLUDE) == FALSE) { - STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, + STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE STC_ERROR_INVALID_PARAMETER); - __STC_LOG_FUNC_EXIT__; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } @@ -316,9 +316,9 @@ gboolean handle_restriction_unset(StcRestriction *object, } if (__validate_rstn_rule(&rule, RST_UNSET) == FALSE) { - STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, + STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE STC_ERROR_INVALID_PARAMETER); - __STC_LOG_FUNC_EXIT__; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } @@ -347,10 +347,10 @@ gboolean handle_restriction_get(StcRestriction *object, __table_restrictions_per_app_cb, builder); if (ret < STC_ERROR_NONE) { - g_variant_builder_unref(builder); - STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); - __STC_LOG_FUNC_EXIT__; - return TRUE; + g_variant_builder_unref(builder); //LCOV_EXCL_LINE + STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return TRUE; //LCOV_EXCL_LINE } return_parameters = g_variant_new("(ia{sv})", STC_ERROR_NONE, builder); @@ -376,10 +376,10 @@ gboolean handle_restriction_get_all(StcRestriction *object, ret = table_restrictions_foreach(__table_restrictions_foreach_app_cb, builder); if (ret < STC_ERROR_NONE) { - g_variant_builder_unref(builder); - STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); - __STC_LOG_FUNC_EXIT__; - return TRUE; + g_variant_builder_unref(builder); //LCOV_EXCL_LINE + STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return TRUE; //LCOV_EXCL_LINE } return_parameters = g_variant_new("(iaa{sv})", STC_ERROR_NONE, builder); @@ -404,8 +404,8 @@ gboolean handle_restriction_get_state(StcRestriction *object, ret = table_restrictions_get_restriction_state(app_id, iftype, &state); if (ret < STC_ERROR_NONE) { - STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); - __STC_LOG_FUNC_EXIT__; + STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; } diff --git a/src/stc-statistics.c b/src/stc-statistics.c index fd4544d..70cf48c 100755 --- a/src/stc-statistics.c +++ b/src/stc-statistics.c @@ -61,15 +61,11 @@ void __stc_extract_select_rule(const char *key, GVariant *value, table_statistics_select_rule *rule = (table_statistics_select_rule *) user_data; if (rule == NULL) { - __STC_LOG_FUNC_EXIT__; - return; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } - if (!g_strcmp0(key, "version")) { - rule->version = g_variant_get_byte(value); - STC_LOGD("version: [%u]", (unsigned int) rule->version); - - } else if (!g_strcmp0(key, "from")) { + if (!g_strcmp0(key, "from")) { rule->from = g_variant_get_uint64(value); STC_LOGD("from: [%lu]", rule->from); @@ -81,12 +77,8 @@ void __stc_extract_select_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, "granularity")) { - rule->granularity = g_variant_get_int32(value); - STC_LOGD("granularity: [%d]", rule->granularity); - } else { - STC_LOGD("Unknown select rule"); + STC_LOGD("Unknown select rule"); //LCOV_EXCL_LINE } __STC_LOG_FUNC_EXIT__; @@ -100,15 +92,11 @@ void __stc_extract_reset_rule(const char *key, GVariant *value, table_statistics_reset_rule *rule = (table_statistics_reset_rule *) user_data; if (rule == NULL) { - __STC_LOG_FUNC_EXIT__; - return; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } - if (!g_strcmp0(key, "version")) { - rule->version = g_variant_get_byte(value); - STC_LOGD("version: [%u]", (unsigned int) rule->version); - - } else if (!g_strcmp0(key, "app_id")) { + if (!g_strcmp0(key, "app_id")) { gsize len = 0; rule->app_id = g_variant_dup_string(value, &len); STC_LOGD("app_id: [%s]", rule->app_id); @@ -124,10 +112,10 @@ void __stc_extract_reset_rule(const char *key, GVariant *value, } else if (!g_strcmp0(key, "from")) { if (!(rule->interval)) { - rule->interval = MALLOC0(stc_db_tm_interval_s, 1); - if (!(rule->interval)) { - __STC_LOG_FUNC_EXIT__; - return; + rule->interval = MALLOC0(stc_db_tm_interval_s, 1); //LCOV_EXCL_LINE + if (!(rule->interval)) { //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } } @@ -136,22 +124,18 @@ void __stc_extract_reset_rule(const char *key, GVariant *value, } else if (!g_strcmp0(key, "to")) { if (!(rule->interval)) { - rule->interval = MALLOC0(stc_db_tm_interval_s, 1); - if (!(rule->interval)) { - __STC_LOG_FUNC_EXIT__; - return; + rule->interval = MALLOC0(stc_db_tm_interval_s, 1); //LCOV_EXCL_LINE + if (!(rule->interval)) { //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } } rule->interval->to = g_variant_get_uint64(value); STC_LOGD("to: [%lu]", rule->interval->to); - } else if (!g_strcmp0(key, "connection_state")) { - rule->connection_state = g_variant_get_int32(value); - STC_LOGD("connection_state: [%d]", rule->connection_state); - } else { - STC_LOGD("Unknown reset rule"); + STC_LOGD("Unknown reset rule"); //LCOV_EXCL_LINE } __STC_LOG_FUNC_EXIT__; @@ -163,8 +147,8 @@ void __stc_statistics_app_info_builder_add(GVariantBuilder *builder, __STC_LOG_FUNC_ENTER__; if (!builder || !info) { - __STC_LOG_FUNC_EXIT__; - return; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return; //LCOV_EXCL_LINE } if (info->app_id) @@ -215,8 +199,8 @@ stc_cb_ret_e __table_statistics_foreach_app_cb(const table_statistics_info *info GVariantBuilder sub_builder; if (!info || !builder) { - __STC_LOG_FUNC_EXIT__; - return STC_CANCEL; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_CANCEL; //LCOV_EXCL_LINE } g_variant_builder_init(&sub_builder, G_VARIANT_TYPE("a{sv}")); @@ -237,8 +221,8 @@ stc_cb_ret_e __table_statistics_per_app_cb(const table_statistics_info *info, GVariantBuilder sub_builder; if (!info || !builder) { - __STC_LOG_FUNC_EXIT__; - return STC_CANCEL; + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_CANCEL; //LCOV_EXCL_LINE } g_variant_builder_init(&sub_builder, G_VARIANT_TYPE("a{sv}")); @@ -281,7 +265,7 @@ gboolean handle_statistics_get_all(StcStatistics *object, g_variant_iter_free(iter); } } else { - STC_LOGD("No selection rule, using default selection rule."); + STC_LOGD("No selection rule, using default selection rule."); //LCOV_EXCL_LINE } builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}")); @@ -290,10 +274,10 @@ gboolean handle_statistics_get_all(StcStatistics *object, __table_statistics_foreach_app_cb, builder); if (ret < STC_ERROR_NONE) { - g_variant_builder_unref(builder); - STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret); - __STC_LOG_FUNC_EXIT__; - return TRUE; + g_variant_builder_unref(builder); //LCOV_EXCL_LINE + STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return TRUE; //LCOV_EXCL_LINE } return_parameters = g_variant_new("(iaa{sv})", STC_ERROR_NONE, builder); @@ -349,7 +333,7 @@ gboolean handle_statistics_get(StcStatistics *object, g_variant_iter_free(iter); } } else { - STC_LOGD("No selection rule, using default selection rule."); + STC_LOGD("No selection rule, using default selection rule."); //LCOV_EXCL_LINE } builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}")); @@ -358,10 +342,10 @@ gboolean handle_statistics_get(StcStatistics *object, __table_statistics_per_app_cb, builder); if (ret < STC_ERROR_NONE) { - g_variant_builder_unref(builder); - STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret); - __STC_LOG_FUNC_EXIT__; - return TRUE; + g_variant_builder_unref(builder); //LCOV_EXCL_LINE + STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return TRUE; //LCOV_EXCL_LINE } return_parameters = g_variant_new("(iaa{sv})", STC_ERROR_NONE, builder); @@ -396,13 +380,13 @@ gboolean handle_statistics_reset(StcStatistics *object, g_variant_iter_free(iter); } } else { - STC_LOGD("No selection rule, using default selection rule."); + STC_LOGD("No selection rule, using default selection rule."); //LCOV_EXCL_LINE } ret = table_statistics_reset(&rule); if (ret < STC_ERROR_NONE) { - STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret); - goto handle_error; + STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE + goto handle_error; //LCOV_EXCL_LINE } return_parameters = g_variant_new("(i)", STC_ERROR_NONE); diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt new file mode 100755 index 0000000..7cb1f4f --- /dev/null +++ b/unittest/CMakeLists.txt @@ -0,0 +1,32 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(gtest-stc-manager C CXX) + +SET(GTEST_TEST "gtest-stc-manager") +ADD_DEFINITIONS("-DUSE_DLOG") + +SET(REQUIRES_LIST ${REQUIRES_LIST} + glib-2.0 + gio-2.0 + gmock + dlog +) + +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(GTEST_TEST_PKG REQUIRED ${REQUIRES_LIST}) + +FOREACH(flag ${GTEST_TEST_PKG_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wall -fPIE") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS}") +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") + +FILE(GLOB GTEST_TEST_SRCS *.cpp) +SET(GTEST_TEST_SRCS ${GTEST_TEST_SRCS}) + +ADD_EXECUTABLE(${GTEST_TEST} ${GTEST_TEST_SRCS}) +TARGET_LINK_LIBRARIES(${GTEST_TEST} ${GTEST_TEST_LDFLAGS} ${GTEST_TEST_PKG_LDFLAGS} -ldl -lgcov) + +INSTALL(TARGETS ${GTEST_TEST} RUNTIME DESTINATION ${BIN_DIR}) diff --git a/unittest/gdbus.cpp b/unittest/gdbus.cpp new file mode 100755 index 0000000..e882fea --- /dev/null +++ b/unittest/gdbus.cpp @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include "gdbus.h" + +GDbus::GDbus() +{ + this->m_pConnection = NULL; + this->m_pCancellable = NULL; +} + +GDbus::~GDbus() +{ + GDBusConnection *conn = this->m_pConnection; + GCancellable *cancel = this->m_pCancellable; + + if (cancel) { + g_cancellable_cancel(cancel); + g_object_unref(cancel); + cancel = NULL; + } + + if (conn) { + g_object_unref(conn); + conn = NULL; + } +} + +error_e GDbus::Create(void) +{ + GError *err = NULL; + +#if !GLIB_CHECK_VERSION(2, 36, 0) + g_type_init(); +#endif + + this->m_pConnection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err); + if (this->m_pConnection == NULL) { + if (err != NULL) { + GLOGD("Failed to connect to the D-BUS daemon [%s]", err->message); + g_error_free(err); + } + + return ERROR_OPERATION_FAILED; + } + + this->m_pCancellable = g_cancellable_new(); + + return ERROR_NONE; +} + +error_e GDbus::Destroy(void) +{ + g_cancellable_cancel(this->m_pCancellable); + g_object_unref(this->m_pCancellable); + this->m_pCancellable = NULL; + + g_object_unref(this->m_pConnection); + this->m_pConnection = NULL; + + return ERROR_NONE; +} + +GDBusConnection *GDbus::GetConnection(void) +{ + return this->m_pConnection; +} + +GCancellable *GDbus::GetCancellable(void) +{ + return this->m_pCancellable; +} + +error_e GDbus::ConvertErrorStringToEnum(const char *error) +{ + if (NULL != strstr(error, "NoReply")) + return ERROR_INVALID_OPERATION; + else if (NULL != strstr(error, "Failed")) + return ERROR_OPERATION_FAILED; + else if (NULL != strstr(error, "UnknownMethod")) + return ERROR_INVALID_OPERATION; + else if (NULL != strstr(error, "InvalidArguments")) + return ERROR_INVALID_PARAMETER; + else if (NULL != strstr(error, "AccessDenied")) + return ERROR_PERMISSION_DENIED; + else if (NULL != strstr(error, "PermissionDenied")) + return ERROR_PERMISSION_DENIED; + else if (NULL != strstr(error, "NotSupported")) + return ERROR_NOT_SUPPORTED; + else if (NULL != strstr(error, "InProgress")) + return ERROR_IN_PROGRESS; + + return ERROR_OPERATION_FAILED; +} + +GVariant *GDbus::InvokeMethod(const char *dest, const char *path, + const char *iface_name, const char *method, GVariant *params, error_e *dbus_error) +{ + GError *error = NULL; + GVariant *reply = NULL; + GDBusConnection *connection = NULL; + *dbus_error = ERROR_NONE; + + connection = GetConnection(); + if (connection == NULL) { + GLOGD("GDBusconnection is NULL"); + *dbus_error = ERROR_NOT_INITIALIZED; + return reply; + } + + reply = g_dbus_connection_call_sync(connection, + dest, + path, + iface_name, + method, + params, + NULL, + G_DBUS_CALL_FLAGS_NONE, + DBUS_REPLY_TIMEOUT, + GetCancellable(), + &error); + + if (reply == NULL) { + if (error != NULL) { + GLOGD("g_dbus_connection_call_sync() failed " + "error [%d: %s]", error->code, error->message); + *dbus_error = ConvertErrorStringToEnum(error->message); + g_error_free(error); + } else { + GLOGD("g_dbus_connection_call_sync() failed"); + *dbus_error = ERROR_OPERATION_FAILED; + } + + return NULL; + } + + return reply; +} + +error_e GDbus::InvokeMethodNonblock(const char *dest, const char *path, + const char *iface_name, const char *method, GVariant *params, int timeout, + GAsyncReadyCallback notify_func, void *user_data) +{ + GDBusConnection *connection = NULL; + + connection = GetConnection(); + if (connection == NULL) { + GLOGD("GDBusconnection is NULL"); + return ERROR_NOT_INITIALIZED; + } + + g_dbus_connection_call(connection, + dest, + path, + iface_name, + method, + params, + NULL, + G_DBUS_CALL_FLAGS_NONE, + timeout, + GetCancellable(), + (GAsyncReadyCallback) notify_func, + (gpointer)user_data); + + return ERROR_NONE; +} diff --git a/unittest/gdbus.h b/unittest/gdbus.h new file mode 100755 index 0000000..91596a9 --- /dev/null +++ b/unittest/gdbus.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __STC_MGR_GDBUS_H__ +#define __STC_MGR_GDBUS_H__ + +#include +#include + +#include "stcmgr.h" + +#define GMAINTIMEOUT 10000 +#define DBUS_REPLY_TIMEOUT (120 * 1000) + +#define STC_MGR_SERVICE "net.stc" +#define STC_MGR_STATISTICS_INTERFACE STC_MGR_SERVICE ".statistics" +#define STC_MGR_RESTRICTION_INTERFACE STC_MGR_SERVICE ".restriction" +#define STC_MGR_MANAGER_INTERFACE STC_MGR_SERVICE ".manager" + +#define STC_MGR_STATISTICS_PATH "/net/stc/statistics" +#define STC_MGR_RESTRICTION_PATH "/net/stc/restriction" +#define STC_MGR_MANAGER_PATH "/net/stc/manager" + +#define STC_MGR_METHOD_STATISTICS_INIT "Init" +#define STC_MGR_METHOD_STATISTICS_GET_PER_APP_ID "Get" +#define STC_MGR_METHOD_STATISTICS_GET_ALL "GetAll" +#define STC_MGR_METHOD_STATISTICS_GET_TOTAL "Get" +#define STC_MGR_METHOD_STATISTICS_RESET "Reset" + +#define STC_MGR_METHOD_RESTRICTION_SET "Set" +#define STC_MGR_METHOD_RESTRICTION_GET_STATUS "GetState" +#define STC_MGR_METHOD_RESTRICTION_GET_PER_APP_ID "Get" +#define STC_MGR_METHOD_RESTRICTION_GET_ALL "GetAll" +#define STC_MGR_METHOD_RESTRICTION_UNSET "Unset" +#define STC_MGR_METHOD_RESTRICTION_EXCLUDE "Exclude" + +#define STC_MGR_METHOD_MANAGER_STOP "Stop" + +class GDbus { +private: + GDBusConnection *m_pConnection; + GCancellable *m_pCancellable; +public: + GDbus(); + ~GDbus(); + error_e Create(void); + error_e Destroy(void); + GDBusConnection *GetConnection(void); + GCancellable *GetCancellable(void); + error_e ConvertErrorStringToEnum(const char *error); + GVariant *InvokeMethod(const char *dest, const char *path, + const char *iface_name, const char *method, GVariant *params, error_e *dbus_error); + error_e InvokeMethodNonblock(const char *dest, const char *path, + const char *iface_name, const char *method, GVariant *params, int timeout, + GAsyncReadyCallback notify_func, void *user_data); +}; + +#endif /* __STC_MGR_GDBUS_H__ */ diff --git a/unittest/manager.cpp b/unittest/manager.cpp new file mode 100755 index 0000000..a99149c --- /dev/null +++ b/unittest/manager.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include "manager.h" + +Manager::Manager() +{ + Create(); +} + +Manager::~Manager() +{ + Destroy(); +} + +error_e Manager::StopManager(void) +{ + GVariant *message = NULL; + error_e error = ERROR_NONE; + int result = 0; + + message = InvokeMethod(STC_MGR_SERVICE, + STC_MGR_MANAGER_PATH, + STC_MGR_MANAGER_INTERFACE, + STC_MGR_METHOD_MANAGER_STOP, + NULL, + &error); + + if (message == NULL) { + GLOGD("Failed to invoke dbus method"); + return error; + } + + g_variant_get(message, "(i)", &result); + GLOGD("Successfully stop manager [%d]", result); + g_variant_unref(message); + + return ERROR_NONE; +} diff --git a/unittest/manager.h b/unittest/manager.h new file mode 100755 index 0000000..61b31ca --- /dev/null +++ b/unittest/manager.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __STC_MGR_MANAGER_H__ +#define __STC_MGR_MANAGER_H__ + +#include "stcmgr.h" +#include "gdbus.h" + +class Manager : public GDbus { +private: +public: + Manager(); + ~Manager(); + error_e StopManager(void); +}; + + +#endif /* __STC_MGR_MANAGER_H__ */ diff --git a/unittest/restriction.cpp b/unittest/restriction.cpp new file mode 100755 index 0000000..0c09ddc --- /dev/null +++ b/unittest/restriction.cpp @@ -0,0 +1,343 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include "restriction.h" + +static GMainLoop *g_pMainLoop = NULL; +static guint g_nTimeoutId = 0; + +static gboolean CallbackTimeut(gpointer data) +{ + if (g_pMainLoop) + g_main_loop_quit(g_pMainLoop); + + return false; +} + +static void RunMainLoop(void) +{ + g_pMainLoop = g_main_loop_new(NULL, false); + g_nTimeoutId = g_timeout_add(GMAINTIMEOUT, CallbackTimeut, g_pMainLoop); + g_main_loop_run(g_pMainLoop); + + g_source_remove(g_nTimeoutId); + g_pMainLoop = NULL; +} + +static void QuitMainLoop(void) +{ + if (g_pMainLoop) + g_main_loop_quit(g_pMainLoop); +} + +static void AsyncReadyCallback(GObject *source_object, + GAsyncResult *res, gpointer user_data) +{ + GLOGD("Succeeded to response async callback"); + QuitMainLoop(); +} + +RRule::RRule() +{ + this->m_AppID[0] = '\0'; + this->m_IfaceName[0] = '\0'; + this->m_IfaceType = IFACE_ALL; + this->m_DataLimit = 0; + this->m_DataWarnLimit = 0; + this->m_RoamingType = ROAMING_DISABLED; + this->m_SubscriberID[0] = '\0'; +} + +RRule::~RRule() +{ +} + +Restriction::Restriction() +{ + Create(); +} + +Restriction::~Restriction() +{ + Destroy(); +} + +error_e Restriction::SetRule(const char *app_id, const char *iface_name, + const char *subscriber_id, iface_type_e iface_type, + int64_t data_limit, int64_t data_warn_limit, + roaming_type_e roaming_type) +{ + if (app_id == NULL || strlen(app_id) == 0) + this->m_Rule.m_AppID[0] = '\0'; + else + g_strlcpy(this->m_Rule.m_AppID, app_id, APP_ID_LEN); + + if (iface_name == NULL || strlen(iface_name) == 0) + this->m_Rule.m_IfaceName[0] = '\0'; + else + g_strlcpy(this->m_Rule.m_IfaceName, iface_name, IFNAME_LEN); + + if (subscriber_id == NULL || strlen(subscriber_id) == 0) + this->m_Rule.m_SubscriberID[0] = '\0'; + else + g_strlcpy(this->m_Rule.m_SubscriberID, subscriber_id, SUBSCRIBER_ID_LEN); + + switch(iface_type) { + case IFACE_UNKNOWN: + case IFACE_DATACALL: + case IFACE_WIFI: + case IFACE_WIRED: + case IFACE_BLUETOOTH: + case IFACE_ALL: + this->m_Rule.m_IfaceType = iface_type; + break; + default: + return ERROR_INVALID_PARAMETER; + } + + switch(roaming_type) { + case ROAMING_UNKNOWN: + case ROAMING_ENABLED: + case ROAMING_DISABLED: + this->m_Rule.m_RoamingType = roaming_type; + break; + default: + return ERROR_INVALID_PARAMETER; + } + + this->m_Rule.m_DataLimit = data_limit; + this->m_Rule.m_DataWarnLimit = data_warn_limit; + + return ERROR_NONE; +} + +void Restriction::MakeRuleParams(GVariant **params) +{ + GVariantBuilder *builder; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + + g_variant_builder_add(builder, "{sv}", + RESTRICTION_RULE_APP_ID, + g_variant_new_string(this->m_Rule.m_AppID)); + + g_variant_builder_add(builder, "{sv}", + RESTRICTION_RULE_IFNAME, + g_variant_new_string(this->m_Rule.m_IfaceName)); + + g_variant_builder_add(builder, "{sv}", + RESTRICTION_RULE_IFTYPE, + g_variant_new_uint16(this->m_Rule.m_IfaceType)); + + g_variant_builder_add(builder, "{sv}", + RESTRICTION_RULE_ROAMING, + g_variant_new_uint16(this->m_Rule.m_RoamingType)); + + g_variant_builder_add(builder, "{sv}", + RESTRICTION_RULE_SUBSCRIBER_ID, + g_variant_new_string(this->m_Rule.m_SubscriberID)); + + g_variant_builder_add(builder, "{sv}", + RESTRICTION_RULE_DATA_LIMIT, + g_variant_new_int64(this->m_Rule.m_DataLimit)); + + g_variant_builder_add(builder, "{sv}", + RESTRICTION_RULE_DATA_WARN_LIMIT, + g_variant_new_int64(this->m_Rule.m_DataWarnLimit)); + + *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder)); + g_variant_builder_unref(builder); +} + +void Restriction::MakeGetParams(GVariant **params) +{ + *params = g_variant_new("(s)", this->m_Rule.m_AppID); +} + +void Restriction::MakeStatusParams(GVariant **params) +{ + *params = g_variant_new("(si)", + this->m_Rule.m_AppID, this->m_Rule.m_IfaceType); +} + +error_e Restriction::SetRstriction(void) +{ + GVariant *message = NULL; + GVariant *params = NULL; + error_e error = ERROR_NONE; + int result = 0; + + MakeRuleParams(¶ms); + + message = InvokeMethod(STC_MGR_SERVICE, + STC_MGR_RESTRICTION_PATH, + STC_MGR_RESTRICTION_INTERFACE, + STC_MGR_METHOD_RESTRICTION_SET, + params, + &error); + + if (message == NULL) { + GLOGD("Failed to invoke dbus method"); + return error; + } + + g_variant_get(message, "(i)", &result); + GLOGD("Succeeded to set restriction [%d]", result); + g_variant_unref(message); + + return ERROR_NONE; +} + +error_e Restriction::ExcludeRstriction(void) +{ + GVariant *message = NULL; + GVariant *params = NULL; + error_e error = ERROR_NONE; + int result = 0; + + MakeRuleParams(¶ms); + + message = InvokeMethod(STC_MGR_SERVICE, + STC_MGR_RESTRICTION_PATH, + STC_MGR_RESTRICTION_INTERFACE, + STC_MGR_METHOD_RESTRICTION_EXCLUDE, + params, + &error); + + if (message == NULL) { + GLOGD("Failed to invoke dbus method"); + return error; + } + + g_variant_get(message, "(i)", &result); + GLOGD("Succeeded to exclude restriction [%d]", result); + g_variant_unref(message); + + return ERROR_NONE; +} + +error_e Restriction::UnsetRstriction(void) +{ + GVariant *message = NULL; + GVariant *params = NULL; + error_e error = ERROR_NONE; + int result = 0; + + MakeRuleParams(¶ms); + + message = InvokeMethod(STC_MGR_SERVICE, + STC_MGR_RESTRICTION_PATH, + STC_MGR_RESTRICTION_INTERFACE, + STC_MGR_METHOD_RESTRICTION_UNSET, + params, + &error); + + if (message == NULL) { + GLOGD("Failed to invoke dbus method"); + return error; + } + + g_variant_get(message, "(i)", &result); + GLOGD("Succeeded to unset restriction [%d]", result); + g_variant_unref(message); + + return ERROR_NONE; +} + +error_e Restriction::GetRstrictionStatus(restriction_status_e *status) +{ + GVariant *message = NULL; + GVariant *params = NULL; + error_e error = ERROR_NONE; + int result = 0; + + MakeStatusParams(¶ms); + + message = InvokeMethod(STC_MGR_SERVICE, + STC_MGR_RESTRICTION_PATH, + STC_MGR_RESTRICTION_INTERFACE, + STC_MGR_METHOD_RESTRICTION_GET_STATUS, + params, + &error); + + if (message == NULL) { + GLOGD("Failed to invoke dbus method"); + return error; + } + + g_variant_get(message, "(ii)", &result, status); + GLOGD("Succeeded to get restriction status [%d:%d]", result, *status); + g_variant_unref(message); + + return ERROR_NONE; +} + +error_e Restriction::GetRstriction(void) +{ + GVariant *params = NULL; + error_e error = ERROR_NONE; + + MakeGetParams(¶ms); + + error = InvokeMethodNonblock(STC_MGR_SERVICE, + STC_MGR_RESTRICTION_PATH, + STC_MGR_RESTRICTION_INTERFACE, + STC_MGR_METHOD_RESTRICTION_GET_PER_APP_ID, + params, + DBUS_REPLY_TIMEOUT, + AsyncReadyCallback, + NULL); + + if (error != ERROR_NONE) { + GLOGD("Failed to invoke dbus method nonblock"); + return error; + } + + GLOGD("Succeeded to get restriction per app ID"); + + RunMainLoop(); + return ERROR_NONE; +} + +error_e Restriction::GetAllRstriction(void) +{ + error_e error = ERROR_NONE; + + error = InvokeMethodNonblock(STC_MGR_SERVICE, + STC_MGR_RESTRICTION_PATH, + STC_MGR_RESTRICTION_INTERFACE, + STC_MGR_METHOD_RESTRICTION_GET_ALL, + NULL, + DBUS_REPLY_TIMEOUT, + AsyncReadyCallback, + NULL); + + if (error != ERROR_NONE) { + GLOGD("Failed to invoke dbus method nonblock"); + return error; + } + + GLOGD("Succeeded to get all restriction"); + + RunMainLoop(); + return ERROR_NONE; +} \ No newline at end of file diff --git a/unittest/restriction.h b/unittest/restriction.h new file mode 100755 index 0000000..c0b0f2d --- /dev/null +++ b/unittest/restriction.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __STC_MGR_RESTRICTION_H__ +#define __STC_MGR_RESTRICTION_H__ + +#include +#include + +#include "stcmgr.h" +#include "gdbus.h" + +#define RESTRICTION_RULE_APP_ID "app_id" +#define RESTRICTION_RULE_IFNAME "ifname" +#define RESTRICTION_RULE_IFTYPE "iftype" +#define RESTRICTION_RULE_ROAMING "roaming" +#define RESTRICTION_RULE_SUBSCRIBER_ID "subscriber_id" +#define RESTRICTION_RULE_DATA_LIMIT "data_limit" +#define RESTRICTION_RULE_DATA_WARN_LIMIT "data_warn_limit" + +typedef enum { + RESTRICTION_UNKNOWN, + RESTRICTION_ACTIVATED, + RESTRICTION_REMOVED, + RESTRICTION_EXCLUDED, + RESTRICTION_BACKGROUND, + RESTRICTION_LAST_ELEM, +} restriction_status_e; + +class RRule { +private: +public: + char m_AppID[APP_ID_LEN]; + char m_IfaceName[IFNAME_LEN]; + char m_SubscriberID[SUBSCRIBER_ID_LEN]; + iface_type_e m_IfaceType; + int64_t m_DataLimit; + int64_t m_DataWarnLimit; + roaming_type_e m_RoamingType; + + RRule(); + ~RRule(); +}; + +class Restriction : public GDbus { +private: + RRule m_Rule; +public: + Restriction(); + ~Restriction(); + error_e SetRule(const char *app_id, const char *iface_name, + const char *subscriber_id, iface_type_e iface_type, + int64_t data_limit, int64_t data_warn_limit, + roaming_type_e roaming_type); + void MakeRuleParams(GVariant **params); + void MakeGetParams(GVariant **params); + void MakeStatusParams(GVariant **params); + error_e SetRstriction(void); + error_e ExcludeRstriction(void); + error_e UnsetRstriction(void); + error_e GetRstrictionStatus(restriction_status_e *status); + error_e GetRstriction(void); + error_e GetAllRstriction(void); +}; + +#endif /* __STC_MGR_RESTRICTION_H__ */ diff --git a/unittest/statistics.cpp b/unittest/statistics.cpp new file mode 100755 index 0000000..49ebd6a --- /dev/null +++ b/unittest/statistics.cpp @@ -0,0 +1,317 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include "statistics.h" + +static GMainLoop *g_pMainLoop = NULL; +static guint g_nTimeoutId = 0; + +static gboolean CallbackTimeut(gpointer data) +{ + if (g_pMainLoop) + g_main_loop_quit(g_pMainLoop); + + return false; +} + +static void RunMainLoop(void) +{ + g_pMainLoop = g_main_loop_new(NULL, false); + g_nTimeoutId = g_timeout_add(GMAINTIMEOUT, CallbackTimeut, g_pMainLoop); + g_main_loop_run(g_pMainLoop); + + g_source_remove(g_nTimeoutId); + g_pMainLoop = NULL; +} + +static void QuitMainLoop(void) +{ + if (g_pMainLoop) + g_main_loop_quit(g_pMainLoop); +} + +static void AsyncReadyCallback(GObject *source_object, + GAsyncResult *res, gpointer user_data) +{ + GLOGD("Succeeded to response async callback"); + QuitMainLoop(); +} + +SRule::SRule() +{ + this->m_IfaceType = IFACE_UNKNOWN; + this->m_TimePeriod = TIME_PERIOD_UNKNOWN; + this->m_Interval.from = 0; + this->m_Interval.to = 0; +} + +SRule::~SRule() +{ +} + +Statistics::Statistics() +{ + Create(); +} + +Statistics::~Statistics() +{ + Destroy(); +} + +error_e Statistics::SetRule(const char *app_id, const char *subscriber_id, + iface_type_e iface_type, time_t from, time_t to, time_period_e time_period) +{ + if (app_id == NULL || strlen(app_id) == 0) + this->m_Rule.m_AppID[0] = '\0'; + else + g_strlcpy(this->m_Rule.m_AppID, app_id, APP_ID_LEN); + + if (subscriber_id == NULL || strlen(subscriber_id) == 0) + this->m_Rule.m_SubscriberID[0] = '\0'; + else + g_strlcpy(this->m_Rule.m_SubscriberID, subscriber_id, SUBSCRIBER_ID_LEN); + + switch(iface_type) { + case IFACE_UNKNOWN: + case IFACE_DATACALL: + case IFACE_WIFI: + case IFACE_WIRED: + case IFACE_BLUETOOTH: + case IFACE_ALL: + this->m_Rule.m_IfaceType = iface_type; + break; + default: + return ERROR_INVALID_PARAMETER; + } + + if (from < 0 || to < 0) + return ERROR_INVALID_PARAMETER; + + this->m_Rule.m_Interval.from = from; + this->m_Rule.m_Interval.to = to; + + switch(time_period) { + case TIME_PERIOD_UNKNOWN: + case TIME_PERIOD_HOUR: + case TIME_PERIOD_DAY: + case TIME_PERIOD_WEEK: + case TIME_PERIOD_MONTH: + this->m_Rule.m_TimePeriod = time_period; + break; + default: + return ERROR_INVALID_PARAMETER; + } + + return ERROR_NONE; +} + +time_t Statistics::MakeTime(int year, int mon, int day, int hour, int min) +{ + struct tm curr = { 0, }; + curr.tm_year = year - 1900; + curr.tm_mon = mon - 1; + curr.tm_mday = day; + curr.tm_hour = hour; + curr.tm_min = min; + return mktime(&curr); +} + +void Statistics::MakeRuleParams(GVariant **params, int mode) +{ + GVariantBuilder *builder; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + + g_variant_builder_add(builder, "{sv}", + STATISTICS_RULE_INTERVAL_FROM, + g_variant_new_uint64(this->m_Rule.m_Interval.from)); + + g_variant_builder_add(builder, "{sv}", + STATISTICS_RULE_INTERVAL_TO, + g_variant_new_uint64(this->m_Rule.m_Interval.to)); + + g_variant_builder_add(builder, "{sv}", + STATISTICS_RULE_IFTYPE, + g_variant_new_uint16(this->m_Rule.m_IfaceType)); + + g_variant_builder_add(builder, "{sv}", + STATISTICS_RULE_TIME_PERIOD, + g_variant_new_int32(this->m_Rule.m_TimePeriod)); + + switch (mode) { + case 0: /* reset */ + g_variant_builder_add(builder, "{sv}", + STATISTICS_RULE_APP_ID, + g_variant_new_string(this->m_Rule.m_AppID)); + + g_variant_builder_add(builder, "{sv}", + RESET_RULE_SUBSCRIBER_ID, + g_variant_new_string(this->m_Rule.m_SubscriberID)); + + *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder)); + break; + case 1: /* get app */ + *params = g_variant_new("(s@a{sv})", this->m_Rule.m_AppID, g_variant_builder_end(builder)); + break; + case 2: /* get total */ + *params = g_variant_new("(s@a{sv})", "", g_variant_builder_end(builder)); + break; + case 3: /* get all */ + default: + *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder)); + break; + } + + g_variant_builder_unref(builder); +} + +error_e Statistics::InitStatistics(void) +{ + GVariant *message = NULL; + error_e error = ERROR_NONE; + + message = InvokeMethod(STC_MGR_SERVICE, + STC_MGR_STATISTICS_PATH, + STC_MGR_STATISTICS_INTERFACE, + STC_MGR_METHOD_STATISTICS_INIT, + NULL, + &error); + + if (message == NULL) { + GLOGD("Failed to invoke dbus method"); + return error; + } + + GLOGD("Succeeded to init statistics"); + + return ERROR_NONE; +} + +error_e Statistics::GetStatistics(void) +{ + GVariant *params = NULL; + error_e error = ERROR_NONE; + + MakeRuleParams(¶ms, 1); + + error = InvokeMethodNonblock(STC_MGR_SERVICE, + STC_MGR_STATISTICS_PATH, + STC_MGR_STATISTICS_INTERFACE, + STC_MGR_METHOD_STATISTICS_GET_PER_APP_ID, + params, + DBUS_REPLY_TIMEOUT, + AsyncReadyCallback, + NULL); + + if (error != ERROR_NONE) { + GLOGD("Failed to invoke dbus method nonblock"); + return error; + } + + RunMainLoop(); + GLOGD("Succeeded to get statistics per app ID"); + + return ERROR_NONE; +} + +error_e Statistics::GetTotalStatistics(void) +{ + GVariant *params = NULL; + error_e error = ERROR_NONE; + + MakeRuleParams(¶ms, 2); + + error = InvokeMethodNonblock(STC_MGR_SERVICE, + STC_MGR_STATISTICS_PATH, + STC_MGR_STATISTICS_INTERFACE, + STC_MGR_METHOD_STATISTICS_GET_TOTAL, + params, + DBUS_REPLY_TIMEOUT, + AsyncReadyCallback, + NULL); + + if (error != ERROR_NONE) { + GLOGD("Failed to invoke dbus method nonblock"); + return error; + } + + RunMainLoop(); + GLOGD("Succeeded to get total statistics"); + + return ERROR_NONE; +} + +error_e Statistics::GetAllStatistics(void) +{ + GVariant *params = NULL; + error_e error = ERROR_NONE; + + MakeRuleParams(¶ms, 3); + + error = InvokeMethodNonblock(STC_MGR_SERVICE, + STC_MGR_STATISTICS_PATH, + STC_MGR_STATISTICS_INTERFACE, + STC_MGR_METHOD_STATISTICS_GET_ALL, + params, + DBUS_REPLY_TIMEOUT, + AsyncReadyCallback, + NULL); + + if (error != ERROR_NONE) { + GLOGD("Failed to invoke dbus method nonblock"); + return error; + } + + RunMainLoop(); + GLOGD("Succeeded to get all statistics"); + + return ERROR_NONE; +} + +error_e Statistics::ResetStatistics(void) +{ + GVariant *message = NULL; + GVariant *params = NULL; + error_e error = ERROR_NONE; + int result = 0; + + MakeRuleParams(¶ms, 0); + + message = InvokeMethod(STC_MGR_SERVICE, + STC_MGR_STATISTICS_PATH, + STC_MGR_STATISTICS_INTERFACE, + STC_MGR_METHOD_STATISTICS_RESET, + params, + &error); + + if (message == NULL) { + GLOGD("Failed to invoke dbus method"); + return error; + } + + g_variant_get(message, "(i)", &result); + GLOGD("Succeeded to reset statistics", result); + g_variant_unref(message); + + return ERROR_NONE; +} diff --git a/unittest/statistics.h b/unittest/statistics.h new file mode 100755 index 0000000..5f3ebcc --- /dev/null +++ b/unittest/statistics.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __STC_MGR_STATISTICS_H__ +#define __STC_MGR_STATISTICS_H__ + +#include "stcmgr.h" +#include "gdbus.h" + +#define STATISTICS_RULE_APP_ID "app_id" +#define STATISTICS_RULE_INTERVAL_FROM "from" +#define STATISTICS_RULE_INTERVAL_TO "to" +#define STATISTICS_RULE_IFTYPE "iftype" +#define STATISTICS_RULE_TIME_PERIOD "granularity" + +#define RESET_RULE_SUBSCRIBER_ID "subscriber_id" + +typedef struct { + time_t from; + time_t to; +} time_interval_s; + +class SRule { +private: +public: + char m_AppID[APP_ID_LEN]; + char m_SubscriberID[SUBSCRIBER_ID_LEN]; + iface_type_e m_IfaceType; + time_period_e m_TimePeriod; + time_interval_s m_Interval; + + SRule(); + ~SRule(); +}; + +class Statistics : public GDbus { +private: + SRule m_Rule; +public: + Statistics(); + ~Statistics(); + error_e SetRule(const char *app_id, const char *subscriber_id, + iface_type_e iface_type, time_t from, time_t to, time_period_e time_period); + time_t MakeTime(int year, int mon, int day, int hour, int min); + void MakeRuleParams(GVariant **params, int mode); + error_e InitStatistics(void); + error_e GetStatistics(void); + error_e GetTotalStatistics(void); + error_e GetAllStatistics(void); + error_e ResetStatistics(void); +}; + + +#endif /* __STC_MGR_STATISTICS_H__ */ diff --git a/unittest/stcmgr.cpp b/unittest/stcmgr.cpp new file mode 100755 index 0000000..c3f401f --- /dev/null +++ b/unittest/stcmgr.cpp @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "stcmgr.h" diff --git a/unittest/stcmgr.h b/unittest/stcmgr.h new file mode 100755 index 0000000..79f9c2c --- /dev/null +++ b/unittest/stcmgr.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __STC_MGR_H__ +#define __STC_MGR_H__ + +#include + +#define APP_ID_LEN 128 +#define IFNAME_LEN 16 +#define SUBSCRIBER_ID_LEN 128 + +#ifdef USE_DLOG +#include +#undef LOG_TAG +#define LOG_TAG "STC_GTEST" +#define GLOGD(format, args...) LOGD(format, ##args) +#else +#define GLOGD(format, args...) +#endif + +typedef enum { + ERROR_NONE = 0, + ERROR_NOT_PERMITTED = -1, + ERROR_OUT_OF_MEMORY = -2, + ERROR_PERMISSION_DENIED = -3, + ERROR_RESOURCE_BUSY = -4, + ERROR_INVALID_OPERATION = -5, + ERROR_INVALID_PARAMETER = -6, + ERROR_NOT_SUPPORTED = -7, + ERROR_OPERATION_FAILED = -8, + ERROR_NOT_INITIALIZED = -9, + ERROR_ALREADY_INITIALIZED = -10, + ERROR_IN_PROGRESS = -11, +} error_e; + +typedef enum { + IFACE_UNKNOWN, + IFACE_DATACALL, + IFACE_WIFI, + IFACE_WIRED, + IFACE_BLUETOOTH, + IFACE_ALL, +} iface_type_e; + +typedef enum { + ROAMING_UNKNOWN, + ROAMING_ENABLED, + ROAMING_DISABLED, +} roaming_type_e; + +typedef enum { + TIME_PERIOD_UNKNOWN = 0, + TIME_PERIOD_HOUR = 3600, + TIME_PERIOD_DAY = 86400, + TIME_PERIOD_WEEK = 604800, + TIME_PERIOD_MONTH = 2419200, +} time_period_e; + +#endif /* __STC_MGR_H__ */ diff --git a/unittest/unittest.cpp b/unittest/unittest.cpp new file mode 100755 index 0000000..1141021 --- /dev/null +++ b/unittest/unittest.cpp @@ -0,0 +1,377 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include + +#include "unittest.h" +#include "restriction.h" +#include "statistics.h" +#include "manager.h" + +using ::testing::InitGoogleTest; +using ::testing::Test; +using ::testing::TestCase; + +TEST(StcManager_Statistics, Init_p) +{ + error_e ret = ERROR_NONE; + Statistics stat; + + ret = stat.InitStatistics(); + EXPECT_EQ(ERROR_NONE, ret); +} + +TEST(StcManager_Statistics, Get_p) +{ + error_e ret = ERROR_NONE; + Statistics stat; + time_t from = 0; + time_t to = 0; + + from = stat.MakeTime(2000, 1, 1, 1, 1); + time(&to); + + ret = stat.SetRule("TOTAL_DATACALL", + NULL, + IFACE_DATACALL, + from, + to, + TIME_PERIOD_DAY); + EXPECT_EQ(ERROR_NONE, ret); + + ret = stat.GetStatistics(); + EXPECT_EQ(ERROR_NONE, ret); + + ret = stat.SetRule("TOTAL_WIFI", + NULL, + IFACE_WIFI, + from, + to, + TIME_PERIOD_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = stat.GetStatistics(); + EXPECT_EQ(ERROR_NONE, ret); +} + +TEST(StcManager_Statistics, GetAll_p) +{ + error_e ret = ERROR_NONE; + Statistics stat; + time_t from = 0; + time_t to = 0; + + from = stat.MakeTime(2000, 1, 1, 1, 1); + time(&to); + + ret = stat.SetRule(NULL, + NULL, + IFACE_UNKNOWN, + from, + to, + TIME_PERIOD_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = stat.GetAllStatistics(); + EXPECT_EQ(ERROR_NONE, ret); +} + +TEST(StcManager_Statistics, GetTotal_p) +{ + error_e ret = ERROR_NONE; + Statistics stat; + time_t from = 0; + time_t to = 0; + + from = stat.MakeTime(2000, 1, 1, 1, 1); + time(&to); + + ret = stat.SetRule(NULL, + NULL, + IFACE_DATACALL, + from, + to, + TIME_PERIOD_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = stat.GetTotalStatistics(); + EXPECT_EQ(ERROR_NONE, ret); + + ret = stat.SetRule(NULL, + NULL, + IFACE_WIFI, + from, + to, + TIME_PERIOD_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = stat.GetTotalStatistics(); + EXPECT_EQ(ERROR_NONE, ret); +} + +TEST(StcManager_Restriction, Set_p) +{ + error_e ret = ERROR_NONE; + Restriction rest; + + ret = rest.SetRule("Test_tel", + "seth_w0", + "1234567890abcdefg", + IFACE_DATACALL, + 0, + 0, + ROAMING_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.SetRstriction(); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.SetRule("TOTAL_WIFI", + "wlan0", + NULL, + IFACE_WIFI, + 2, + 1, + ROAMING_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.SetRstriction(); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.SetRule("Test_eth", + "eth0", + NULL, + IFACE_WIRED, + 100000, + 80000, + ROAMING_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.SetRstriction(); + EXPECT_EQ(ERROR_NONE, ret); +} + +TEST(StcManager_Restriction, Exclude_p) + +{ + error_e ret = ERROR_NONE; + Restriction rest; + + ret = rest.SetRule("TOTAL_DATACALL", + "seth_w0", + NULL, + IFACE_DATACALL, + 0, + 0, + ROAMING_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.ExcludeRstriction(); + EXPECT_EQ(ERROR_NONE, ret); +} + +TEST(StcManager_Restriction, Get_p) +{ + error_e ret = ERROR_NONE; + Restriction rest; + + ret = rest.SetRule("Test_tel", + "seth_w0", + "1234567890abcdefg", + IFACE_DATACALL, + 0, + 0, + ROAMING_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.GetRstriction(); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.SetRule("TOTAL_WIFI", + "wlan0", + NULL, + IFACE_WIFI, + 0, + 0, + ROAMING_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.GetRstriction(); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.SetRule("TOTAL_IPV4", + "seth_w0", + NULL, + IFACE_DATACALL, + 0, + 0, + ROAMING_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.GetRstriction(); + EXPECT_EQ(ERROR_NONE, ret); +} + +TEST(StcManager_Restriction, GetAll_p) +{ + error_e ret = ERROR_NONE; + Restriction rest; + + ret = rest.GetAllRstriction(); + EXPECT_EQ(ERROR_NONE, ret); +} + +TEST(StcManager_Restriction, GetRst_p) +{ + error_e ret = ERROR_NONE; + Restriction rest; + restriction_status_e status = RESTRICTION_UNKNOWN; + + ret = rest.SetRule("Test_tel", + "seth_w0", + NULL, + IFACE_DATACALL, + 0, + 0, + ROAMING_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.GetRstrictionStatus(&status); + EXPECT_EQ(ERROR_NONE, ret); + EXPECT_LE(RESTRICTION_UNKNOWN, status); + EXPECT_GE(RESTRICTION_LAST_ELEM, status); + + ret = rest.SetRule("Test_eth", + "eth0", + NULL, + IFACE_WIRED, + 100000, + 80000, + ROAMING_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.GetRstrictionStatus(&status); + EXPECT_EQ(ERROR_NONE, ret); + EXPECT_LE(RESTRICTION_UNKNOWN, status); + EXPECT_GE(RESTRICTION_LAST_ELEM, status); +} + +TEST(StcManager_Restriction, Unset_p) +{ + error_e ret = ERROR_NONE; + Restriction rest; + + ret = rest.SetRule("Test_tel", + "seth_w0", + "1234567890abcdefg", + IFACE_DATACALL, + 0, + 0, + ROAMING_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.UnsetRstriction(); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.SetRule("TOTAL_WIFI", + "wlan0", + NULL, + IFACE_WIFI, + 2, + 1, + ROAMING_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.UnsetRstriction(); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.SetRule("Test_eth", + "eth0", + NULL, + IFACE_WIRED, + 100000, + 80000, + ROAMING_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.UnsetRstriction(); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.SetRule("TOTAL_DATACALL", + "seth_w0", + NULL, + IFACE_DATACALL, + 0, + 0, + ROAMING_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = rest.UnsetRstriction(); + EXPECT_EQ(ERROR_NONE, ret); +} + +TEST(StcManager_Statistics, Reset_p) +{ + error_e ret = ERROR_NONE; + Statistics stat; + time_t from = 0; + time_t to = 0; + + from = stat.MakeTime(2000, 1, 1, 1, 1); + time(&to); + + ret = stat.SetRule("TOTAL_DATACALL", + "1234567890abcdefg", + IFACE_DATACALL, + from, + to, + TIME_PERIOD_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = stat.ResetStatistics(); + EXPECT_EQ(ERROR_NONE, ret); + + ret = stat.SetRule(NULL, + NULL, + IFACE_UNKNOWN, + from, + to, + TIME_PERIOD_UNKNOWN); + EXPECT_EQ(ERROR_NONE, ret); + + ret = stat.ResetStatistics(); + EXPECT_EQ(ERROR_NONE, ret); +} + +TEST(StcManager_Manager, Stop_p) +{ + error_e ret = ERROR_NONE; + Manager mgr; + + ret = mgr.StopManager(); + EXPECT_EQ(ERROR_NONE, ret); +} + +int main(int argc, char **argv) +{ + InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/unittest/unittest.h b/unittest/unittest.h new file mode 100755 index 0000000..2ad100d --- /dev/null +++ b/unittest/unittest.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __STC_MGR_UNITTEST_H__ +#define __STC_MGR_UNITTEST_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#ifdef __cplusplus +} +#endif + +#endif /* __STC_MGR_UNITTEST_H__ */ \ No newline at end of file -- 2.7.4 From 2495420eae5f9e32ee0381f920940820e642e355 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 25 Jan 2018 09:59:39 +0900 Subject: [PATCH 09/16] Fixed crash for wrong struct type cast Change-Id: If2d9679b345d2253ea26db22710590c86a93dadc Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- src/monitor/stc-monitor.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index ea01601..4f25986 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.43 +Version: 0.0.44 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 62a2c5c..633b334 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -640,7 +640,7 @@ static gboolean __remove_rstns_foreach_application(gpointer key, goto out; /* remove restriction from system */ - __process_restriction(RST_UNSET, rstn_key, rstn_value, data); + __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL); __print_rstn(rstn_key, rstn_value); out: @@ -1433,9 +1433,9 @@ static gboolean __add_rstn_foreach_application(gpointer key, /* add restriction to system */ if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED) - __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data); + __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL); else - __process_restriction(RST_SET, rstn_key, rstn_value, data); + __process_restriction(RST_SET, rstn_key, rstn_value, NULL); __print_rstn(rstn_key, rstn_value); out: -- 2.7.4 From 864075363de0a4c7fe5de0bd0e1d055c2edf18df Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 25 Jan 2018 17:45:56 +0530 Subject: [PATCH 10/16] Update classid for all valid restriction states Change-Id: I440ca17881dbf92954dfffbf57a18a64216c348f Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/monitor/stc-monitor.c | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 4f25986..0147c03 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.44 +Version: 0.0.45 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 633b334..d9b1b7e 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -493,7 +493,7 @@ static void __process_restriction(enum traffic_restriction_type rst_type, return; /* classid is invalid */ - if (rstn_value->classid == STC_UNKNOWN_CLASSID) + if (rstn_value->classid <= STC_UNKNOWN_CLASSID) return; effective_data_limit = rstn_value->data_limit; @@ -1957,7 +1957,9 @@ 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 && info->app_id) + if (value.rst_state > STC_RESTRICTION_UNKNOWN && + value.rst_state < STC_RESTRICTION_LAST_ELEM && + info->app_id) value.classid = get_classid_by_app_id(info->app_id, TRUE); else value.classid = STC_UNKNOWN_CLASSID; -- 2.7.4 From 047e74dde29c3e688e86d0cd4b3938047b092bcc Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 25 Jan 2018 18:23:41 +0530 Subject: [PATCH 11/16] [Fix] Remove restriction from tree when user initiated remove operation Change-Id: I96cff6077c12d0484643e63fc7da772285579e7f Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/monitor/stc-monitor.c | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 0147c03..9e2c1af 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.45 +Version: 0.0.46 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 d9b1b7e..c8f7b7e 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -1246,10 +1246,6 @@ static gboolean __remove_restriction(gpointer key, gpointer value, stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key; stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value; - /* rstn rule is already removed */ - if (rstn_value->rst_state == STC_RESTRICTION_REMOVED) - return FALSE; - __process_restriction(RST_UNSET, rstn_key, rstn_value, data); __print_rstn(rstn_key, rstn_value); return FALSE; -- 2.7.4 From e9cf7157200314f7da28d42f86bf3f04b197d56c Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 7 Feb 2018 11:17:27 +0900 Subject: [PATCH 12/16] Fixed some svaces WGID : 307350, 307352 Change-Id: I517541c5c088f2a0f30ea71186fcf9d2df332d9e Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- src/monitor/stc-default-connection.c | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 9e2c1af..d50d114 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.46 +Version: 0.0.47 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/monitor/stc-default-connection.c b/src/monitor/stc-default-connection.c index 9a43408..6128e02 100755 --- a/src/monitor/stc-default-connection.c +++ b/src/monitor/stc-default-connection.c @@ -118,8 +118,10 @@ static void __telephony_get_modem_subscriber_id(GDBusConnection *connection, DEBUG_PARAMS(message); DEBUG_PARAM_TYPE(message); g_variant_get(message, "(&s&s)", &plmn, &msin); - plmn_len = strlen(plmn); - msin_len = strlen(msin); + if (plmn) + plmn_len = strlen(plmn); + if (msin) + msin_len = strlen(msin); if (msin_len + plmn_len >= IMSI_LENGTH) { STC_LOGD("Incorrect length of mobile subscriber identifier + net id"); //LCOV_EXCL_LINE -- 2.7.4 From 1582744d78efda2bc85b7cdf3cb6ece204803e5b Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 8 Feb 2018 09:44:18 +0900 Subject: [PATCH 13/16] Moved exception list to plugin Change-Id: Iec04f0f7aba1862cde610a9e3c4d7c93f67674e0 Signed-off-by: hyunuktak --- include/stc-manager-plugin.h | 16 ++- include/stc-plugin.h | 33 ++++- packaging/stc-manager.spec | 2 +- plugin/CMakeLists.txt | 3 + plugin/stc-plugin-appstatus.c | 186 ++++++++++++++++++++++++ plugin/stc-plugin-exception.c | 291 ++++++++++++++++++++++++++++++++++++++ plugin/stc-plugin-popup.c | 73 ++++++++++ plugin/stc-plugin.c | 227 +++-------------------------- src/monitor/include/stc-monitor.h | 5 - src/monitor/stc-app-lifecycle.c | 3 +- src/monitor/stc-monitor.c | 117 ++------------- src/stc-manager-gdbus.c | 5 +- src/stc-manager-plugin.c | 93 +++++++++--- src/stc-manager.c | 2 +- 14 files changed, 711 insertions(+), 345 deletions(-) create mode 100755 plugin/stc-plugin-appstatus.c create mode 100755 plugin/stc-plugin-exception.c create mode 100755 plugin/stc-plugin-popup.c diff --git a/include/stc-manager-plugin.h b/include/stc-manager-plugin.h index f4233da..f0409b2 100755 --- a/include/stc-manager-plugin.h +++ b/include/stc-manager-plugin.h @@ -21,17 +21,21 @@ #include "stc-plugin.h" -void stc_manager_plugin_init(void); -void stc_manager_plugin_deinit(void); +int stc_manager_plugin_init(void); +int stc_manager_plugin_deinit(void); -int stc_send_warn_message_to_net_popup(const char *content, +int stc_manager_plugin_send_warn_message(const char *content, const char *type, const char *app_id, const char *iftype, const char *warn); -int stc_send_restriction_message_to_net_popup(const char *content, +int stc_manager_plugin_send_restriction_message(const char *content, const char *type, const char *app_id, const char *iftype, const char *limit); -int stc_register_state_changed_cb(stc_s *stc, +int stc_manager_plugin_register_state_changed_cb(stc_s *stc, stc_plugin_app_state_changed_cb cb, void *data); -int stc_deregister_state_changed_cb(stc_s *stc); +int stc_manager_plugin_deregister_state_changed_cb(stc_s *stc); + +int stc_manager_plugin_fill_exception_list(void); +int stc_manager_plugin_update_exception_list(void); +int stc_manager_plugin_check_exception_by_cmdline(char *cmdline); #endif /* __STC_MANAGER_PLUGIN_H__ */ diff --git a/include/stc-plugin.h b/include/stc-plugin.h index 4d843b8..ffb5767 100755 --- a/include/stc-plugin.h +++ b/include/stc-plugin.h @@ -28,20 +28,49 @@ typedef stc_error_e (*stc_plugin_app_state_changed_cb)(stc_cmd_type_e cmd, stc_app_type_e app_type); typedef struct { - int(*send_restriction_message_to_net_popup) (const char *, + void (*initialize_plugin) (void); + void (*deinitialize_plugin) (void); + /* popup */ + int (*send_restriction_message_to_net_popup) (const char *, const char *, const char *, const char *, const char *); - int(*send_warn_message_to_net_popup) (const char *, + int (*send_warn_message_to_net_popup) (const char *, const char *, const char *, const char *, const char *); + + /* app status */ int (*register_state_changed_cb) (stc_s *stc, stc_plugin_app_state_changed_cb cb, void *data); int (*deregister_state_changed_cb) (stc_s *stc); + + /* exception */ + int (*fill_exception_list) (void); + int (*update_exception_list) (void); + int (*check_exception_by_cmdline) (char *cmdline); } stc_plugin_s; +void stc_plugin_initialize(void); +void stc_plugin_deinitialize(void); + +int stc_plugin_popup_send_restriction_message(const char *content, + const char *type, const char *app_id, const char *iftype, const char *limit); +int stc_plugin_popup_send_restriction_message(const char *content, + const char *type, const char *app_id, const char *iftype, const char *limit); + +int stc_plugin_appstatus_register_changed_cb(stc_s *stc, + stc_plugin_app_state_changed_cb cb, + void *data); +int stc_plugin_appstatus_deregister_changed_cb(stc_s *stc); + +int stc_plugin_exception_init(void); +int stc_plugin_exception_deinit(void); +int stc_plugin_exception_fill_list(void); +int stc_plugin_exception_update_list(void); +int stc_plugin_exception_check_by_cmdline(char *cmdline); + #endif /* __STC_PLUGIN_H__ */ diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index d50d114..3b6e441 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.47 +Version: 0.0.48 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt index afde5f4..bad6651 100755 --- a/plugin/CMakeLists.txt +++ b/plugin/CMakeLists.txt @@ -28,6 +28,9 @@ ADD_DEFINITIONS("-DUSE_DLOG") SET(SRCS_PLUGIN stc-plugin.c + stc-plugin-popup.c + stc-plugin-appstatus.c + stc-plugin-exception.c ) # library build diff --git a/plugin/stc-plugin-appstatus.c b/plugin/stc-plugin-appstatus.c new file mode 100755 index 0000000..d642cd6 --- /dev/null +++ b/plugin/stc-plugin-appstatus.c @@ -0,0 +1,186 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "stc-plugin.h" + +//LCOV_EXCL_START +#define AUL_APP_STATUS_DBUS_PATH "/Org/Tizen/Aul/AppStatus" +#define AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE "org.tizen.aul.AppStatus" +#define AUL_APP_STATUS_BUS_NAME AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE + +#define AUL_APP_STATUS_DBUS_STATUS_CHANGE "AppStatusChange" +#define AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE "(issss)" + +typedef struct { + guint sub_id; + const gchar *path; + const gchar *interface; + const gchar *member; + const gchar *param_type; + GDBusSignalCallback callback; + gpointer user_data; +} signal_map_s; + +stc_error_e(*state_changed_cb)(stc_cmd_type_e cmd, pid_t pid, + const gchar *app_id, const gchar *pkg_id, + stc_app_type_e app_type); + +static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + pid_t pid; + stc_cmd_type_e status; + stc_app_type_e apptype; + gchar *appid, *pkgid, *statstr, *pkgtype; + + if (g_strcmp0(g_variant_get_type_string(parameters), + AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE)) { + STC_LOGE("Dbus type not matching, do not process"); + return; + } + + g_variant_get(parameters, AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE, + &pid, &appid, &pkgid, &statstr, &pkgtype); + + if (!strncmp(statstr, "fg", 2)) { + status = STC_CMD_SET_FOREGRD; + } else if (!strncmp(statstr, "bg", 2)) { + status = STC_CMD_SET_BACKGRD; + } else { + goto out; + } + + if (!strncmp(pkgtype, "svc", 3)) + apptype = STC_APP_TYPE_SERVICE; + else if (!strncmp(pkgtype, "widget", 6)) + apptype = STC_APP_TYPE_WIDGET; + else if (!strncmp(pkgtype, "watch", 5)) + apptype = STC_APP_TYPE_WATCH; + else + apptype = STC_APP_TYPE_GUI; + + if (STC_DEBUG_LOG) { + STC_LOGD("\033[1;36mAPP STATUS\033[0;m: Pkg ID [\033[0;34m%s\033[0;m], " + "App ID [\033[0;32m%s\033[0;m], PID [\033[1;33m%d\033[0;m], Status [%s], Type [%s]", + pkgid, appid, pid, statstr, pkgtype); + } + + if (state_changed_cb) + state_changed_cb(status, pid, appid, pkgid, apptype); + +out: + FREE(appid); + FREE(pkgid); + FREE(statstr); + FREE(pkgtype); +} + +signal_map_s signal_map[] = { + + /* AMD DBUS */ + { + 0, + AUL_APP_STATUS_DBUS_PATH, + AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE, + AUL_APP_STATUS_DBUS_STATUS_CHANGE, + AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE, + __stc_gdbus_handle_aul_changestate, + NULL + }, + { + 0, + NULL, + NULL, + NULL, + NULL + } +}; + + +static stc_error_e __ground_status_monitor_init(stc_s *stc) +{ + guint i = 0; + + ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); + + for (i = 0; signal_map[i].member != NULL; i++) { + signal_map[i].sub_id = + g_dbus_connection_signal_subscribe(stc->connection, + NULL, + signal_map[i].interface, + signal_map[i].member, + signal_map[i].path, + NULL, + G_DBUS_SIGNAL_FLAGS_NONE, + signal_map[i].callback, + signal_map[i].user_data, + NULL); + STC_LOGI("Successfully subscribed [%s] signal", + signal_map[i].member); + } + + return STC_ERROR_NONE; +} + +static stc_error_e __ground_status_monitor_deinit(stc_s *stc) +{ + guint i = 0; + + ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); + + for (i = 0; signal_map[i].member != NULL; i++) { + g_dbus_connection_signal_unsubscribe(stc->connection, + signal_map[i].sub_id); + signal_map[i].sub_id = 0; + STC_LOGD("Successfully unsubscribed [%s] signal", + signal_map[i].member); + } + + return STC_ERROR_NONE; +} + +int stc_plugin_appstatus_register_changed_cb(stc_s *stc, + stc_plugin_app_state_changed_cb cb, + void *data) +{ + state_changed_cb = cb; + __ground_status_monitor_init(stc); + + return 0; +} + +int stc_plugin_appstatus_deregister_changed_cb(stc_s *stc) +{ + state_changed_cb = NULL; + __ground_status_monitor_deinit(stc); + return 0; +} +//LCOV_EXCL_STOP diff --git a/plugin/stc-plugin-exception.c b/plugin/stc-plugin-exception.c new file mode 100755 index 0000000..9484d92 --- /dev/null +++ b/plugin/stc-plugin-exception.c @@ -0,0 +1,291 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "stc-plugin.h" + +//LCOV_EXCL_START +#define EXE_TYPE_APPLICATION "app" +#define EXE_TYPE_INSTRUCTION "inst" +#define EXE_TYPE_SYSTEM "sys" +#define EXE_TYPE_SCRIPT "script" + +/* 1 day */ +#define EXCNS_TIMER_INTERVAL 86400 + +#define EXCEPTION_BUF_MAX 64 +#define EXCEPTION_STORAGE "/var/lib/stc/exceptions" + +#define INTERNET_PRIVILEGE "http://tizen.org/privilege/internet" + +typedef struct { + char *process_name; + char *exe_type; +} stc_exceptions_info; + +typedef stc_cb_ret_e +(*stc_exceptions_info_cb)(const stc_exceptions_info *info, + void *user_data); + +static GHashTable *g_excns_hash; /**< exception hash table */ +static GHashTable *g_pkginfo_filter_hash; +static guint g_excns_timer_id; + +static int __pkginfo_filter_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data) +{ + int ret = 0; + char *pkgname = NULL; + + ret = pkgmgrinfo_pkginfo_get_pkgname(handle, &pkgname); + if (ret == PMINFO_R_OK) { + if (g_hash_table_insert(g_pkginfo_filter_hash, + g_strdup(pkgname), g_strdup(EXE_TYPE_APPLICATION)) != TRUE) + STC_LOGE("Failed to insert hash table"); + } + + return STC_CONTINUE; +} + +static int __pkginfo_pkg_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data) +{ + int ret = 0; + char *pkgname = NULL; + char *exe_type = NULL; + stc_exceptions_info data; + const stc_exceptions_info_cb excn_cb = user_data; + + ret = pkgmgrinfo_pkginfo_get_pkgname(handle, &pkgname); + if (ret == PMINFO_R_OK) { + exe_type = g_hash_table_lookup(g_pkginfo_filter_hash, pkgname); + if (exe_type) + return STC_CONTINUE; + + data.process_name = pkgname; + data.exe_type = EXE_TYPE_APPLICATION; + + if (excn_cb(&data, NULL) == STC_CANCEL) + STC_LOGE("Failed to insert hash table"); + } + + return STC_CONTINUE; +} + +static void __excn_hash_foreach_print(gpointer key, gpointer value, + gpointer data) +{ + const char *process_name = key; + const char *exe_type = value; + + STC_LOGI("excn info => process_name [%s] exe_type [%s]", + process_name, exe_type); +} + +static void __excn_hash_printall(void) +{ + g_hash_table_foreach(g_excns_hash, + __excn_hash_foreach_print, NULL); +} + +static gboolean __remove_exception_app(gpointer key, gpointer value, + gpointer data) +{ + const char *exe_type = value; + + if (g_strcmp0(exe_type, EXE_TYPE_APPLICATION) == 0) + return TRUE; + + return FALSE; +} + +static void __remove_exception_appall(void) +{ + g_hash_table_foreach_remove(g_excns_hash, + __remove_exception_app, NULL); +} + +static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info, + void *user_data) +{ + stc_cb_ret_e ret = STC_CONTINUE; + + if (g_hash_table_insert(g_excns_hash, + g_strdup(info->process_name), + g_strdup(info->exe_type)) != TRUE) + ret = STC_CANCEL; + + return ret; +} + +static gboolean __update_exceptions_app_list(void *user_data) +{ + stc_plugin_exception_update_list(); + return TRUE; +} + +static stc_error_e pkginfo_exceptions_foreach(const stc_exceptions_info_cb exception_cb, + void *user_data) +{ + int ret = 0; + int err = STC_ERROR_NONE; + pkgmgrinfo_pkginfo_filter_h handle; + + g_pkginfo_filter_hash = g_hash_table_new_full(g_str_hash, + g_str_equal, g_free, g_free); + + ret = pkgmgrinfo_pkginfo_filter_create(&handle); + ret_value_msg_if(ret != PMINFO_R_OK, STC_ERROR_FAIL, + "Failed to create pkginfo filter"); + + ret = pkgmgrinfo_pkginfo_filter_add_string(handle, + PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE, + INTERNET_PRIVILEGE); + if (ret != PMINFO_R_OK) { + STC_LOGE("Failed to add pkginfo filter string"); + err = STC_ERROR_FAIL; + goto out; + } + + ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle, + __pkginfo_filter_list_cb, NULL); + if (ret != PMINFO_R_OK) { + STC_LOGE("Failed to foreach pkginfo filter"); + err = STC_ERROR_FAIL; + goto out; + } + + ret = pkgmgrinfo_pkginfo_get_list(__pkginfo_pkg_list_cb, exception_cb); + if (ret != PMINFO_R_OK) { + STC_LOGE("Failed to get pkginfo list"); + err = STC_ERROR_FAIL; + goto out; + } + +out: + if (g_pkginfo_filter_hash) { + g_hash_table_destroy(g_pkginfo_filter_hash); + g_pkginfo_filter_hash = NULL; + } + + if (handle) + pkgmgrinfo_pkginfo_filter_destroy(handle); + + return err; +} + +static stc_error_e table_exceptions_foreach(const stc_exceptions_info_cb exception_cb, + void *user_data) +{ + stc_error_e error_code = STC_ERROR_NONE; + stc_exceptions_info data; + + FILE *fp = NULL; + char buf[EXCEPTION_BUF_MAX] = {0, }; + + fp = fopen(EXCEPTION_STORAGE, "r"); + ret_value_msg_if(!fp, STC_ERROR_FAIL, "Failed to open %s file"); + + while (fgets(buf, sizeof(buf), fp) != NULL) { + char *process_name, *exe_type; + char *save_ptr = NULL; + + process_name = strtok_r(buf, ":", &save_ptr); + if (process_name != NULL) + data.process_name = process_name; + else + data.process_name = "none"; + + exe_type = strtok_r(NULL, "\n", &save_ptr); + if (exe_type != NULL) + data.exe_type = exe_type; + else + data.exe_type = "none"; + + if (exception_cb(&data, user_data) == STC_CANCEL) + break; + } + fclose(fp); + + return error_code; +} + +int stc_plugin_exception_init(void) +{ + g_excns_hash = g_hash_table_new_full(g_str_hash, + g_str_equal, g_free, g_free); + + return STC_ERROR_NONE; +} + +int stc_plugin_exception_deinit(void) +{ + if (g_excns_timer_id > 0) { + g_source_remove(g_excns_timer_id); + g_excns_timer_id = 0; + } + + g_hash_table_destroy(g_excns_hash); + + return STC_ERROR_NONE; +} + +int stc_plugin_exception_fill_list(void) +{ + table_exceptions_foreach(__insert_exception_cb, NULL); + pkginfo_exceptions_foreach(__insert_exception_cb, NULL); + + if (STC_DEBUG_LOG) + __excn_hash_printall(); + + + g_excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL, + __update_exceptions_app_list, + NULL); + + return STC_ERROR_NONE; +} + +int stc_plugin_exception_update_list(void) +{ + __remove_exception_appall(); + pkginfo_exceptions_foreach(__insert_exception_cb, NULL); + + if (STC_DEBUG_LOG) + __excn_hash_printall(); + + return STC_ERROR_NONE; +} + +int stc_plugin_exception_check_by_cmdline(char *cmdline) +{ + char *exe_type = NULL; + + exe_type = g_hash_table_lookup(g_excns_hash, cmdline); + if (!exe_type) + return STC_ERROR_NO_DATA; + + return STC_ERROR_NONE; +} +//LCOV_EXCL_STOP diff --git a/plugin/stc-plugin-popup.c b/plugin/stc-plugin-popup.c new file mode 100755 index 0000000..409c890 --- /dev/null +++ b/plugin/stc-plugin-popup.c @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "stc-plugin.h" + +//LCOV_EXCL_START +int stc_plugin_popup_send_warn_message(const char *content, + const char *type, const char *app_id, const char *iftype, const char *warn) +{ + int ret = 0; + bundle *b = bundle_create(); + + STC_LOGD("Warn message : content[%s] type[%s] app_id[%s] warn[%s]", + content, type, app_id, warn); + + bundle_add(b, "_SYSPOPUP_CONTENT_", content); + bundle_add(b, "_SYSPOPUP_TYPE_", type); + bundle_add(b, "_APP_ID_", app_id); + bundle_add(b, "_IF_TYPE_", iftype); + bundle_add(b, "_WARN_LIMIT_", warn); + + ret = syspopup_launch("net-popup", b); + + bundle_free(b); + + return ret; +} + +int stc_plugin_popup_send_restriction_message(const char *content, + const char *type, const char *app_id, const char *iftype, const char *limit) +{ + int ret = 0; + bundle *b = bundle_create(); + + STC_LOGD("Restriction message : content[%s] type[%s] app_id[%s] limit[%s]", + content, type, app_id, limit); + + bundle_add(b, "_SYSPOPUP_CONTENT_", content); + bundle_add(b, "_SYSPOPUP_TYPE_", type); + bundle_add(b, "_APP_ID_", app_id); + bundle_add(b, "_IF_TYPE_", iftype); + bundle_add(b, "_RESTRICTION_LIMIT_", limit); + + ret = syspopup_launch("net-popup", b); + + bundle_free(b); + + return ret; +} +//LCOV_EXCL_STOP diff --git a/plugin/stc-plugin.c b/plugin/stc-plugin.c index 916a75e..b1b4013 100755 --- a/plugin/stc-plugin.c +++ b/plugin/stc-plugin.c @@ -25,219 +25,36 @@ #include #include "stc-plugin.h" -#include "stc-manager.h" -#include "stc-manager-util.h" -/* - * AUL - */ -#define AUL_APP_STATUS_DBUS_PATH "/Org/Tizen/Aul/AppStatus" -#define AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE "org.tizen.aul.AppStatus" -#define AUL_APP_STATUS_BUS_NAME AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE - -#define AUL_APP_STATUS_DBUS_STATUS_CHANGE "AppStatusChange" -#define AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE "(issss)" - -typedef struct { - guint sub_id; - const gchar *path; - const gchar *interface; - const gchar *member; - const gchar *param_type; - GDBusSignalCallback callback; - gpointer user_data; -} signal_map_s; - -stc_error_e(*state_changed_cb)(stc_cmd_type_e cmd, pid_t pid, - const gchar *app_id, const gchar *pkg_id, - stc_app_type_e app_type); - -static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - pid_t pid; - stc_cmd_type_e status; - stc_app_type_e apptype; - gchar *appid, *pkgid, *statstr, *pkgtype; - - if (g_strcmp0(g_variant_get_type_string(parameters), - AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE)) { - STC_LOGE("Dbus type not matching, do not process"); - return; - } - - g_variant_get(parameters, AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE, - &pid, &appid, &pkgid, &statstr, &pkgtype); - - if (!strncmp(statstr, "fg", 2)) { - status = STC_CMD_SET_FOREGRD; - } else if (!strncmp(statstr, "bg", 2)) { - status = STC_CMD_SET_BACKGRD; - } else { - goto out; - } - - if (!strncmp(pkgtype, "svc", 3)) - apptype = STC_APP_TYPE_SERVICE; - else if (!strncmp(pkgtype, "widget", 6)) - apptype = STC_APP_TYPE_WIDGET; - else if (!strncmp(pkgtype, "watch", 5)) - apptype = STC_APP_TYPE_WATCH; - else - apptype = STC_APP_TYPE_GUI; - - if (STC_DEBUG_LOG) { - STC_LOGD("\033[1;36mAPP STATUS\033[0;m: Pkg ID [\033[0;34m%s\033[0;m], " - "App ID [\033[0;32m%s\033[0;m], PID [\033[1;33m%d\033[0;m], Status [%s], Type [%s]", - pkgid, appid, pid, statstr, pkgtype); - } - - if (state_changed_cb) - state_changed_cb(status, pid, appid, pkgid, apptype); - -out: - FREE(appid); - FREE(pkgid); - FREE(statstr); - FREE(pkgtype); -} - -signal_map_s signal_map[] = { - - /* AMD DBUS */ - { - 0, - AUL_APP_STATUS_DBUS_PATH, - AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE, - AUL_APP_STATUS_DBUS_STATUS_CHANGE, - AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE, - __stc_gdbus_handle_aul_changestate, - NULL - }, - { - 0, - NULL, - NULL, - NULL, - NULL - } -}; - - -static stc_error_e __ground_status_monitor_init(stc_s *stc) -{ - guint i = 0; - - ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); - - for (i = 0; signal_map[i].member != NULL; i++) { - signal_map[i].sub_id = - g_dbus_connection_signal_subscribe(stc->connection, - NULL, - signal_map[i].interface, - signal_map[i].member, - signal_map[i].path, - NULL, - G_DBUS_SIGNAL_FLAGS_NONE, - signal_map[i].callback, - signal_map[i].user_data, - NULL); - STC_LOGI("Successfully subscribed [%s] signal", - signal_map[i].member); - } - - return STC_ERROR_NONE; -} - -static stc_error_e __ground_status_monitor_deinit(stc_s *stc) +//LCOV_EXCL_START +void stc_plugin_initialize(void) { - guint i = 0; - - ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); - - for (i = 0; signal_map[i].member != NULL; i++) { - g_dbus_connection_signal_unsubscribe(stc->connection, - signal_map[i].sub_id); - signal_map[i].sub_id = 0; - STC_LOGD("Successfully unsubscribed [%s] signal", - signal_map[i].member); - } - - return STC_ERROR_NONE; + stc_plugin_exception_init(); } -static int __stc_send_warn_message_to_net_popup(const char *content, - const char *type, const char *app_id, const char *iftype, const char *warn) +void stc_plugin_deinitialize(void) { - int ret = 0; - bundle *b = bundle_create(); - - STC_LOGD("Warn message : content[%s] type[%s] app_id[%s] warn[%s]", - content, type, app_id, warn); - - bundle_add(b, "_SYSPOPUP_CONTENT_", content); - bundle_add(b, "_SYSPOPUP_TYPE_", type); - bundle_add(b, "_APP_ID_", app_id); - bundle_add(b, "_IF_TYPE_", iftype); - bundle_add(b, "_WARN_LIMIT_", warn); - - ret = syspopup_launch("net-popup", b); - - bundle_free(b); - - return ret; -} - -static int __stc_send_restriction_message_to_net_popup(const char *content, - const char *type, const char *app_id, const char *iftype, const char *limit) -{ - int ret = 0; - bundle *b = bundle_create(); - - STC_LOGD("Restriction message : content[%s] type[%s] app_id[%s] limit[%s]", - content, type, app_id, limit); - - bundle_add(b, "_SYSPOPUP_CONTENT_", content); - bundle_add(b, "_SYSPOPUP_TYPE_", type); - bundle_add(b, "_APP_ID_", app_id); - bundle_add(b, "_IF_TYPE_", iftype); - bundle_add(b, "_RESTRICTION_LIMIT_", limit); - - ret = syspopup_launch("net-popup", b); - - bundle_free(b); - - return ret; -} - -static int __register_state_changed_cb(stc_s *stc, - stc_plugin_app_state_changed_cb cb, - void *data) -{ - state_changed_cb = cb; - __ground_status_monitor_init(stc); - - return 0; -} - -int __deregister_state_changed_cb(stc_s *stc) -{ - state_changed_cb = NULL; - __ground_status_monitor_deinit(stc); - return 0; + stc_plugin_exception_deinit(); } API stc_plugin_s stc_plugin = { + .initialize_plugin = + stc_plugin_initialize, + .deinitialize_plugin = + stc_plugin_deinitialize, .send_warn_message_to_net_popup = - __stc_send_warn_message_to_net_popup, + stc_plugin_popup_send_restriction_message, .send_restriction_message_to_net_popup = - __stc_send_restriction_message_to_net_popup, - .register_state_changed_cb = __register_state_changed_cb, - .deregister_state_changed_cb = __deregister_state_changed_cb + stc_plugin_popup_send_restriction_message, + .register_state_changed_cb = + stc_plugin_appstatus_register_changed_cb, + .deregister_state_changed_cb = + stc_plugin_appstatus_deregister_changed_cb, + .fill_exception_list = + stc_plugin_exception_fill_list, + .update_exception_list = + stc_plugin_exception_update_list, + .check_exception_by_cmdline = + stc_plugin_exception_check_by_cmdline }; - +//LCOV_EXCL_STOP diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index 0ecce65..b8a7357 100755 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -28,9 +28,6 @@ /* 1 seconds */ #define CONTR_TIMER_INTERVAL 1 -/* 1 day */ -#define EXCNS_TIMER_INTERVAL 86400 - /** * @brief key for processes tree */ @@ -102,8 +99,6 @@ typedef struct { gboolean rstns_tree_updated; GTree *apps; /**< monitored applications */ gboolean apps_tree_updated; - GHashTable *excns_hash; /**< exception hash table */ - guint excns_timer_id; guint background_state; } stc_system_s; diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index d02667f..2fcbfe6 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -208,7 +208,8 @@ static gboolean __check_excn(char *cmdline) return TRUE; ret = stc_monitor_check_excn_by_cmdline(cmdline); - if (ret == STC_ERROR_NO_DATA) + if (ret == STC_ERROR_UNINITIALIZED || + ret == STC_ERROR_NO_DATA) return FALSE; else return TRUE; diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index c8f7b7e..b3edf39 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -27,7 +27,6 @@ #include "counter.h" #include "table-statistics.h" #include "table-counters.h" -#include "stc-exception.h" #include "stc-monitor.h" #include "stc-manager-plugin.h" @@ -552,8 +551,6 @@ static void __process_restriction(enum traffic_restriction_type rst_type, counter.iftype = rstn_key->iftype; g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH); - - generate_counter_name(&counter); g_free(default_ifname); /* iptables rule */ @@ -597,8 +594,6 @@ static void __process_restriction(enum traffic_restriction_type rst_type, counter.iftype = rstn_key->iftype; g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH); - - generate_counter_name(&counter); g_free(default_ifname); /* iptables rule */ @@ -772,7 +767,7 @@ static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key, snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit); - stc_send_warn_message_to_net_popup("warn threshold crossed", + stc_manager_plugin_send_warn_message("warn threshold crossed", "warning_noti", rstn_key->app_id, iftype, byte); @@ -813,7 +808,7 @@ static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key, snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit); - stc_send_restriction_message_to_net_popup("restriction threshold crossed", + stc_manager_plugin_send_restriction_message("restriction threshold crossed", "restriction_noti", rstn_key->app_id, iftype, byte); } @@ -1212,7 +1207,8 @@ static gboolean __update_contr_cb(void *user_data) /* we need to continue the timer */ return TRUE; } -#if 0 + +//LCOV_EXCL_START static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value, gpointer data) { @@ -1227,7 +1223,8 @@ static void __rstn_tree_printall(void) { g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL); } -#endif +//LCOV_EXCL_END + static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree, const stc_rstn_key_s *key) { @@ -1404,7 +1401,9 @@ static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info, static void __fill_restritions_list(void) { table_restrictions_foreach(__insert_restriction_cb, NULL); - //__rstn_tree_printall(); + + if (STC_DEBUG_LOG) + __rstn_tree_printall(); } static gboolean __add_rstn_foreach_application(gpointer key, @@ -1545,75 +1544,10 @@ static stc_error_e __process_update_background(void) } //LCOV_EXCL_STOP -#if 0 -static void __excn_hash_foreach_print(gpointer key, gpointer value, - gpointer data) -{ - const char *process_name = key; - const char *exe_type = value; - - STC_LOGI("excn info => process_name [%s] exe_type [%s]", - process_name, exe_type); -} - -static void __excn_hash_printall(void) -{ - g_hash_table_foreach(g_system->excns_hash, - __excn_hash_foreach_print, NULL); -} -#endif - -//LCOV_EXCL_START -static gboolean __remove_exception_app(gpointer key, gpointer value, - gpointer data) -{ - const char *exe_type = value; - - if (g_strcmp0(exe_type, EXE_TYPE_APPLICATION) == 0) - return TRUE; - - return FALSE; -} -//LCOV_EXCL_STOP - -static void __remove_exception_appall(void) -{ - g_hash_table_foreach_remove(g_system->excns_hash, //LCOV_EXCL_LINE - __remove_exception_app, NULL); -} - -static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info, - void *user_data) -{ - stc_cb_ret_e ret = STC_CONTINUE; - - if (g_hash_table_insert(g_system->excns_hash, - g_strdup(info->process_name), - g_strdup(info->exe_type)) != TRUE) - ret = STC_CANCEL; //LCOV_EXCL_LINE - - return ret; -} - static void __fill_exceptions_list(void) { - table_exceptions_foreach(__insert_exception_cb, NULL); - pkginfo_exceptions_foreach(__insert_exception_cb, NULL); - - /* __excn_hash_printall(); */ -} - -//LCOV_EXCL_START -static gboolean __update_exceptions_app_list(void *user_data) -{ - __remove_exception_appall(); - pkginfo_exceptions_foreach(__insert_exception_cb, NULL); - - /* __excn_hash_printall(); */ - - return TRUE; + stc_manager_plugin_fill_exception_list(); } -//LCOV_EXCL_STOP stc_error_e stc_monitor_init(void) { @@ -1637,9 +1571,6 @@ stc_error_e stc_monitor_init(void) __rstns_tree_key_free, __rstns_tree_value_free); - system->excns_hash = g_hash_table_new_full(g_str_hash, - g_str_equal, g_free, g_free); - /* create netlink socket for updating kernel counters */ system->contr_sock = create_netlink(NETLINK_NETFILTER, 0); if (system->contr_sock < 0) { @@ -1682,10 +1613,6 @@ stc_error_e stc_monitor_init(void) __fill_exceptions_list(); __fill_restritions_list(); - g_system->excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL, - __update_exceptions_app_list, - NULL); - return STC_ERROR_NONE; } @@ -1702,12 +1629,6 @@ stc_error_e stc_monitor_deinit(void) g_system->contr_timer_id = 0; } - /* remove exceptions app list update timer */ - if (g_system->excns_timer_id > 0) { - g_source_remove(g_system->excns_timer_id); - g_system->excns_timer_id = 0; - } - /* destroy monitored application tree */ g_tree_destroy(g_system->apps); g_system->apps = NULL; @@ -1716,10 +1637,6 @@ stc_error_e stc_monitor_deinit(void) g_tree_destroy(g_system->rstns); g_system->rstns = NULL; - /* destroy exception hash table */ - g_hash_table_destroy(g_system->excns_hash); - g_system->excns_hash = NULL; - FREE(g_system); return STC_ERROR_NONE; @@ -1954,8 +1871,8 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info) value.restriction_id = info->restriction_id; if (value.rst_state > STC_RESTRICTION_UNKNOWN && - value.rst_state < STC_RESTRICTION_LAST_ELEM && - info->app_id) + value.rst_state < STC_RESTRICTION_LAST_ELEM && + info->app_id) value.classid = get_classid_by_app_id(info->app_id, TRUE); else value.classid = STC_UNKNOWN_CLASSID; @@ -2005,15 +1922,7 @@ stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info) stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline) { - ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!"); - - char *exe_type = NULL; - - exe_type = g_hash_table_lookup(g_system->excns_hash, cmdline); - if (!exe_type) - return STC_ERROR_NO_DATA; - - return STC_ERROR_NONE; + return stc_manager_plugin_check_exception_by_cmdline(cmdline); } int stc_monitor_get_counter_socket(void) diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index df53f9b..46a3d42 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -194,7 +194,8 @@ static void __stc_manager_gdbus_on_bus_acquired(GDBusConnection *connection, iptables_init(); stc_default_connection_monitor_init(stc); - stc_register_state_changed_cb(stc, stc_manager_app_status_changed, NULL); + stc_manager_plugin_register_state_changed_cb(stc, + stc_manager_app_status_changed, NULL); __STC_LOG_FUNC_EXIT__; } @@ -237,7 +238,7 @@ void stc_manager_gdbus_deinit(gpointer stc_data) __STC_LOG_FUNC_ENTER__; stc_s *stc = (stc_s *)stc_data; - stc_deregister_state_changed_cb(stc); + stc_manager_plugin_deregister_state_changed_cb(stc); stc_default_connection_monitor_deinit(stc); g_bus_unown_name(stc->gdbus_owner_id); diff --git a/src/stc-manager-plugin.c b/src/stc-manager-plugin.c index a97ae73..f4aaae9 100755 --- a/src/stc-manager-plugin.c +++ b/src/stc-manager-plugin.c @@ -17,7 +17,6 @@ #include #include "stc-manager.h" -#include "stc-plugin.h" #include "stc-manager-plugin.h" static gboolean stc_plugin_enabled = FALSE; @@ -25,7 +24,7 @@ static void *handle_plugin; static stc_plugin_s *stc_plugin; //LCOV_EXCL_START -void stc_manager_plugin_init(void) +int stc_manager_plugin_init(void) { __STC_LOG_FUNC_ENTER__; @@ -33,7 +32,7 @@ void stc_manager_plugin_init(void) if (!handle_plugin) { STC_LOGE("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror()); __STC_LOG_FUNC_EXIT__; - return; + return STC_ERROR_UNINITIALIZED; } stc_plugin = dlsym(handle_plugin, "stc_plugin"); @@ -41,28 +40,34 @@ void stc_manager_plugin_init(void) STC_LOGE("Can't load symbol: %s", dlerror()); dlclose(handle_plugin); __STC_LOG_FUNC_EXIT__; - return; + return STC_ERROR_UNINITIALIZED; } stc_plugin_enabled = TRUE; + stc_plugin->initialize_plugin(); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_NONE; } -void stc_manager_plugin_deinit(void) +int stc_manager_plugin_deinit(void) { __STC_LOG_FUNC_ENTER__; if (!stc_plugin_enabled) { __STC_LOG_FUNC_EXIT__; - return; + return STC_ERROR_UNINITIALIZED; } + stc_plugin->deinitialize_plugin(); + stc_plugin_enabled = FALSE; dlclose(handle_plugin); __STC_LOG_FUNC_EXIT__; + return STC_ERROR_NONE; } -int stc_send_warn_message_to_net_popup(const char *content, const char *type, +int stc_manager_plugin_send_warn_message(const char *content, const char *type, const char *app_id, const char *iftype, const char *warn) { @@ -71,13 +76,13 @@ int stc_send_warn_message_to_net_popup(const char *content, const char *type, if (!stc_plugin_enabled) { STC_LOGE("Plugin wasn't enabled"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } if (!stc_plugin) { STC_LOGE("Plugin wasn't loaded"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } __STC_LOG_FUNC_EXIT__; @@ -85,7 +90,7 @@ int stc_send_warn_message_to_net_popup(const char *content, const char *type, iftype, warn); } -int stc_send_restriction_message_to_net_popup(const char *content, +int stc_manager_plugin_send_restriction_message(const char *content, const char *type, const char *app_id, const char *iftype, @@ -96,13 +101,13 @@ int stc_send_restriction_message_to_net_popup(const char *content, if (!stc_plugin_enabled) { STC_LOGE("Plugin wasn't enabled"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } if (!stc_plugin) { STC_LOGE("Plugin wasn't loaded"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } __STC_LOG_FUNC_EXIT__; @@ -111,7 +116,7 @@ int stc_send_restriction_message_to_net_popup(const char *content, limit); } -int stc_register_state_changed_cb(stc_s *stc, +int stc_manager_plugin_register_state_changed_cb(stc_s *stc, stc_plugin_app_state_changed_cb cb, void *data) { @@ -120,35 +125,87 @@ int stc_register_state_changed_cb(stc_s *stc, if (!stc_plugin_enabled) { STC_LOGE("Plugin wasn't enabled"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } if (!stc_plugin) { STC_LOGE("Plugin wasn't loaded"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } __STC_LOG_FUNC_EXIT__; return stc_plugin->register_state_changed_cb(stc, cb, data); } -int stc_deregister_state_changed_cb(stc_s *stc) + +int stc_manager_plugin_deregister_state_changed_cb(stc_s *stc) { __STC_LOG_FUNC_ENTER__; if (!stc_plugin_enabled) { STC_LOGE("Plugin wasn't enabled"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } if (!stc_plugin) { STC_LOGE("Plugin wasn't loaded"); __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_UNINITIALIZED; } __STC_LOG_FUNC_EXIT__; return stc_plugin->deregister_state_changed_cb(stc); } + +int stc_manager_plugin_fill_exception_list(void) +{ + __STC_LOG_FUNC_ENTER__; + + if (!stc_plugin_enabled) { + STC_LOGE("Plugin wasn't enabled"); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + if (!stc_plugin) { + STC_LOGE("Plugin wasn't loaded"); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + __STC_LOG_FUNC_EXIT__; + return stc_plugin->fill_exception_list(); +} + +int stc_manager_plugin_update_exception_list(void) +{ + __STC_LOG_FUNC_ENTER__; + + if (!stc_plugin_enabled) { + STC_LOGE("Plugin wasn't enabled"); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + if (!stc_plugin) { + STC_LOGE("Plugin wasn't loaded"); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + __STC_LOG_FUNC_EXIT__; + return stc_plugin->update_exception_list(); +} + +int stc_manager_plugin_check_exception_by_cmdline(char *cmdline) +{ + if (!stc_plugin_enabled) + return STC_ERROR_UNINITIALIZED; + + if (!stc_plugin) + return STC_ERROR_UNINITIALIZED; + + return stc_plugin->check_exception_by_cmdline(cmdline); +} //LCOV_EXCL_STOP diff --git a/src/stc-manager.c b/src/stc-manager.c index d9a4d80..b815400 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -88,12 +88,12 @@ static stc_s *__stc_manager_init(void) cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT); EXEC(STC_ERROR_NONE, stc_db_initialize()); + stc_manager_plugin_init(); err = stc_monitor_init(); if (err != STC_ERROR_NONE) goto handle_error; - stc_manager_plugin_init(); stc_app_lifecycle_monitor_init(); stc_manager_gdbus_init((gpointer)stc); -- 2.7.4 From baaf917c0bd196a0df92c9666e9157043128cdda Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 8 Feb 2018 10:30:35 +0900 Subject: [PATCH 14/16] Moved procfs for app lifecycle to plugin and separate plugins Change-Id: I0310a1761c6b5da28fb991dee7cd03cfdbb49d7b Signed-off-by: hyunuktak --- CMakeLists.txt | 1 + ...ger-plugin.h => stc-manager-plugin-appstatus.h} | 29 +- .../stc-manager-plugin-exception.h | 28 +- include/stc-manager-plugin-procfs.h | 30 ++ interfaces/CMakeLists.txt | 14 + packaging/stc-manager.spec | 36 +- plugin/CMakeLists.txt | 51 +-- plugin/appstatus/CMakeLists.txt | 38 ++ plugin/{ => appstatus}/data/stc_noti_datausage.png | Bin .../appstatus/include/stc-plugin-appstatus.h | 41 +-- plugin/{ => appstatus}/stc-plugin-appstatus.c | 57 ++- plugin/exception/CMakeLists.txt | 36 ++ plugin/exception/include/stc-plugin-exception.h | 39 +++ plugin/{ => exception}/stc-plugin-exception.c | 22 +- plugin/procfs/CMakeLists.txt | 36 ++ .../procfs/include/stc-plugin-procfs.h | 33 +- .../procfs/stc-plugin-procfs.c | 384 +++++++++++---------- plugin/stc-plugin-popup.c | 73 ---- plugin/stc-plugin.c | 60 ---- src/CMakeLists.txt | 27 +- src/helper/helper-nl.c | 2 +- src/helper/helper-nl.h | 2 + src/helper/helper-procfs.c | 4 +- src/monitor/stc-exception.c | 164 --------- src/monitor/stc-monitor.c | 21 +- src/stc-manager-gdbus.c | 11 +- ...ger-plugin.c => stc-manager-plugin-appstatus.c} | 96 +----- src/stc-manager-plugin-exception.c | 120 +++++++ src/stc-manager-plugin-procfs.c | 81 +++++ src/stc-manager.c | 18 +- 30 files changed, 817 insertions(+), 737 deletions(-) rename include/{stc-manager-plugin.h => stc-manager-plugin-appstatus.h} (51%) rename src/monitor/include/stc-app-lifecycle.h => include/stc-manager-plugin-exception.h (52%) create mode 100755 include/stc-manager-plugin-procfs.h create mode 100644 interfaces/CMakeLists.txt create mode 100755 plugin/appstatus/CMakeLists.txt rename plugin/{ => appstatus}/data/stc_noti_datausage.png (100%) rename include/stc-plugin.h => plugin/appstatus/include/stc-plugin-appstatus.h (62%) rename plugin/{ => appstatus}/stc-plugin-appstatus.c (75%) create mode 100755 plugin/exception/CMakeLists.txt create mode 100755 plugin/exception/include/stc-plugin-exception.h rename plugin/{ => exception}/stc-plugin-exception.c (93%) create mode 100755 plugin/procfs/CMakeLists.txt rename src/monitor/include/stc-exception.h => plugin/procfs/include/stc-plugin-procfs.h (50%) rename src/monitor/stc-app-lifecycle.c => plugin/procfs/stc-plugin-procfs.c (82%) delete mode 100755 plugin/stc-plugin-popup.c delete mode 100755 plugin/stc-plugin.c delete mode 100755 src/monitor/stc-exception.c rename src/{stc-manager-plugin.c => stc-manager-plugin-appstatus.c} (56%) create mode 100755 src/stc-manager-plugin-exception.c create mode 100755 src/stc-manager-plugin-procfs.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e7fe23..3386c69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ SET(LIBDIR ${PREFIX}/${LIB_PATH}) SET(DATA_DIR ${CMAKE_SOURCE_DIR}/data) +ADD_SUBDIRECTORY(interfaces) ADD_SUBDIRECTORY(src) ADD_SUBDIRECTORY(plugin) diff --git a/include/stc-manager-plugin.h b/include/stc-manager-plugin-appstatus.h similarity index 51% rename from include/stc-manager-plugin.h rename to include/stc-manager-plugin-appstatus.h index f0409b2..b177bd7 100755 --- a/include/stc-manager-plugin.h +++ b/include/stc-manager-plugin-appstatus.h @@ -14,28 +14,23 @@ * limitations under the License. */ -#ifndef __STC_MANAGER_PLUGIN_H__ -#define __STC_MANAGER_PLUGIN_H__ +#ifndef __STC_MANAGER_PLUGIN_APPSTATUS_H__ +#define __STC_MANAGER_PLUGIN_APPSTATUS_H__ -#define STC_PLUGIN_FILEPATH "/usr/lib/stc-manager-plugin.so" +#define STC_PLUGIN_APPSTATUS_FILEPATH "/usr/lib/stc-plugin-appstatus.so" -#include "stc-plugin.h" +#include "stc-plugin-appstatus.h" -int stc_manager_plugin_init(void); -int stc_manager_plugin_deinit(void); +int stc_plugin_appstatus_init(void); +int stc_plugin_appstatus_deinit(void); -int stc_manager_plugin_send_warn_message(const char *content, +int stc_plugin_appstatus_send_warn_message(const char *content, const char *type, const char *app_id, const char *iftype, const char *warn); -int stc_manager_plugin_send_restriction_message(const char *content, +int stc_plugin_appstatus_send_restriction_message(const char *content, const char *type, const char *app_id, const char *iftype, const char *limit); -int stc_manager_plugin_register_state_changed_cb(stc_s *stc, - stc_plugin_app_state_changed_cb cb, - void *data); -int stc_manager_plugin_deregister_state_changed_cb(stc_s *stc); +int stc_plugin_appstatus_register_state_changed_cb(stc_s *stc, + stc_plugin_app_state_changed_cb cb, void *data); +int stc_plugin_appstatus_deregister_state_changed_cb(stc_s *stc); -int stc_manager_plugin_fill_exception_list(void); -int stc_manager_plugin_update_exception_list(void); -int stc_manager_plugin_check_exception_by_cmdline(char *cmdline); - -#endif /* __STC_MANAGER_PLUGIN_H__ */ +#endif /* __STC_MANAGER_PLUGIN_APPSTATUS_H__ */ diff --git a/src/monitor/include/stc-app-lifecycle.h b/include/stc-manager-plugin-exception.h similarity index 52% rename from src/monitor/include/stc-app-lifecycle.h rename to include/stc-manager-plugin-exception.h index cb5c39e..d826e3c 100755 --- a/src/monitor/include/stc-app-lifecycle.h +++ b/include/stc-manager-plugin-exception.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * 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. @@ -14,20 +14,18 @@ * limitations under the License. */ -#ifndef __STC_APP_LIFECYCLE_H__ -#define __STC_APP_LIFECYCLE_H__ +#ifndef __STC_MANAGER_PLUGIN_EXCEPTION_H__ +#define __STC_MANAGER_PLUGIN_EXCEPTION_H__ -#include "stc-error.h" -#include "stc-manager.h" -#include "stc-manager-util.h" -#include "stc-monitor.h" +#define STC_PLUGIN_EXCEPTION_FILEPATH "/usr/lib/stc-plugin-exception.so" -stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, - pid_t pid, - const gchar *app_id, - const gchar *pkg_id, - stc_app_type_e app_type); -void stc_app_lifecycle_monitor_init(void); -void stc_app_lifecycle_monitor_deinit(void); +#include "stc-plugin-exception.h" -#endif /* __STC_APP_LIFECYCLE_H__ */ +int stc_plugin_exception_init(void); +int stc_plugin_exception_deinit(void); + +int stc_plugin_fill_exception_list(void); +int stc_plugin_update_exception_list(void); +int stc_plugin_check_exception_by_cmdline(char *cmdline); + +#endif /* __STC_MANAGER_PLUGIN_EXCEPTION_H__ */ diff --git a/include/stc-manager-plugin-procfs.h b/include/stc-manager-plugin-procfs.h new file mode 100755 index 0000000..83e7332 --- /dev/null +++ b/include/stc-manager-plugin-procfs.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_PROCFS_H__ +#define __STC_MANAGER_PLUGIN_PROCFS_H__ + +#define STC_PLUGIN_PROCFS_FILEPATH "/usr/lib/stc-plugin-procfs.so" + +#include "stc-plugin-procfs.h" + +int stc_plugin_procfs_init(void); +int stc_plugin_procfs_deinit(void); + +stc_error_e stc_plugin_procfs_app_status_changed(stc_cmd_type_e cmd, + pid_t pid, const gchar *app_id, const gchar *pkg_id, stc_app_type_e app_type); + +#endif /* __STC_MANAGER_PLUGIN_PROCFS_H__ */ diff --git a/interfaces/CMakeLists.txt b/interfaces/CMakeLists.txt new file mode 100644 index 0000000..2e61a9e --- /dev/null +++ b/interfaces/CMakeLists.txt @@ -0,0 +1,14 @@ +ADD_CUSTOM_COMMAND( + WORKING_DIRECTORY + OUTPUT dbus + COMMAND gdbus-codegen --interface-prefix net.stc. + --generate-c-code generated-code + --c-namespace Stc + --c-generate-object-manager + --generate-docbook generated-code-docs + ${INTERFACES}/stcmanager-iface-manager.xml + ${INTERFACES}/stcmanager-iface-restriction.xml + ${INTERFACES}/stcmanager-iface-statistics.xml + COMMENT "Generating GDBus .c/.h") + +ADD_CUSTOM_TARGET(GENERATED_DBUS_CODE DEPENDS dbus) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 3b6e441..40a18d8 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.48 +Version: 0.0.49 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 @@ -20,7 +20,6 @@ BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(capi-system-info) -BuildRequires: pkgconfig(pkgmgr-info) BuildRequires: pkgconfig(openssl) %if %{?enable_database} == YES @@ -37,13 +36,26 @@ BuildRequires: pkgconfig(gmock) %description A smart traffic control manager to manage traffic counting and bandwidth limitation -%package plugin -Summary: STC manager plugin +%package plugin-appstatus +Summary: STC manager application status plugin BuildRequires: pkgconfig(bundle) BuildRequires: pkgconfig(syspopup-caller) -%description plugin -A smart traffic control manager extension for plugin +%description plugin-appstatus +A smart traffic control manager extension for application status plugin + +%package plugin-exception +Summary: STC manager exception list plugin +BuildRequires: pkgconfig(pkgmgr-info) + +%description plugin-exception +A smart traffic control manager extension for exception list plugin + +%package plugin-procfs +Summary: STC manager exception proc file system plugin + +%description plugin-procfs +A smart traffic control manager extension for proc file system plugin %prep %setup -q @@ -118,7 +130,15 @@ cp resources/dbus/stc-manager.conf %{buildroot}%{_sysconfdir}/dbus-1/system.d/st %{_bindir}/gtest* %endif -%files plugin +%files plugin-appstatus %manifest %{name}.manifest %attr(644, -,-) %{_datadir}/icons/*.png -%attr(500,root,root) %{_libdir}/stc-manager-plugin.so +%attr(500,root,root) %{_libdir}/stc-plugin-appstatus.so + +%files plugin-exception +%manifest %{name}.manifest +%attr(500,root,root) %{_libdir}/stc-plugin-exception.so + +%files plugin-procfs +%manifest %{name}.manifest +%attr(500,root,root) %{_libdir}/stc-plugin-procfs.so diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt index bad6651..ce92a27 100755 --- a/plugin/CMakeLists.txt +++ b/plugin/CMakeLists.txt @@ -1,43 +1,20 @@ -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) +SET(SOURCE_DIR ${CMAKE_SOURCE_DIR}/src) +SET(HELPER_SOURCE_DIR ${SOURCE_DIR}/helper) +SET(DATABASE_SOURCE_DIR ${SOURCE_DIR}/database) +SET(MONITOR_SOURCE_DIR ${SOURCE_DIR}/monitor) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) -INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/monitor/include) -# INCLUDE_DIRECTORIES(SRCS include) - -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -g -Werror -fvisibility=hidden") -SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") -SET(CMAKE_C_FLAGS_RELEASE "-O2") +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/interfaces) -ADD_DEFINITIONS("-DUSE_DLOG") +INCLUDE_DIRECTORIES(${SOURCE_DIR}) +INCLUDE_DIRECTORIES(${HELPER_SOURCE_DIR}) -SET(SRCS_PLUGIN - stc-plugin.c - stc-plugin-popup.c - stc-plugin-appstatus.c - stc-plugin-exception.c - ) +INCLUDE_DIRECTORIES(${DATABASE_SOURCE_DIR}) +INCLUDE_DIRECTORIES(${DATABASE_SOURCE_DIR}/include) -# 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}) +INCLUDE_DIRECTORIES(${MONITOR_SOURCE_DIR}) +INCLUDE_DIRECTORIES(${MONITOR_SOURCE_DIR}/include) -# install -INSTALL(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${LIBDIR}) -INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data/stc_noti_datausage.png DESTINATION /usr/share/icons) +ADD_SUBDIRECTORY(appstatus) +ADD_SUBDIRECTORY(exception) +ADD_SUBDIRECTORY(procfs) diff --git a/plugin/appstatus/CMakeLists.txt b/plugin/appstatus/CMakeLists.txt new file mode 100755 index 0000000..d4d7428 --- /dev/null +++ b/plugin/appstatus/CMakeLists.txt @@ -0,0 +1,38 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(stc-plugin-appstatus C) + +# Set required packages +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(appstatus_plugin REQUIRED + dlog + gio-2.0 + gio-unix-2.0 + glib-2.0 + bundle + syspopup-caller + ) + +FOREACH(flag ${appstatus_plugin_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/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") + +ADD_DEFINITIONS("-DUSE_DLOG") + +SET(SRCS_PLUGIN + stc-plugin-appstatus.c + ) + +# library build +ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS_PLUGIN}) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${appstatus_plugin_LDFLAGS}) +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "" OUTPUT_NAME ${PROJECT_NAME}) + +# 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/appstatus/data/stc_noti_datausage.png similarity index 100% rename from plugin/data/stc_noti_datausage.png rename to plugin/appstatus/data/stc_noti_datausage.png diff --git a/include/stc-plugin.h b/plugin/appstatus/include/stc-plugin-appstatus.h similarity index 62% rename from include/stc-plugin.h rename to plugin/appstatus/include/stc-plugin-appstatus.h index ffb5767..ed4a7b4 100755 --- a/include/stc-plugin.h +++ b/plugin/appstatus/include/stc-plugin-appstatus.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef __STC_PLUGIN_H__ -#define __STC_PLUGIN_H__ +#ifndef __STC_PLUGIN_APPSTATUS_H__ +#define __STC_PLUGIN_APPSTATUS_H__ #include #include "stc-error.h" @@ -28,34 +28,16 @@ typedef stc_error_e (*stc_plugin_app_state_changed_cb)(stc_cmd_type_e cmd, stc_app_type_e app_type); typedef struct { - void (*initialize_plugin) (void); - void (*deinitialize_plugin) (void); /* popup */ int (*send_restriction_message_to_net_popup) (const char *, - const char *, - const char *, - const char *, - const char *); + const char *, const char *, const char *, const char *); int (*send_warn_message_to_net_popup) (const char *, - const char *, - const char *, - const char *, - const char *); - + const char *, const char *, const char *, const char *); /* app status */ int (*register_state_changed_cb) (stc_s *stc, - stc_plugin_app_state_changed_cb cb, - void *data); + stc_plugin_app_state_changed_cb cb, void *data); int (*deregister_state_changed_cb) (stc_s *stc); - - /* exception */ - int (*fill_exception_list) (void); - int (*update_exception_list) (void); - int (*check_exception_by_cmdline) (char *cmdline); -} stc_plugin_s; - -void stc_plugin_initialize(void); -void stc_plugin_deinitialize(void); +} stc_plugin_appstatus_s; int stc_plugin_popup_send_restriction_message(const char *content, const char *type, const char *app_id, const char *iftype, const char *limit); @@ -63,14 +45,7 @@ int stc_plugin_popup_send_restriction_message(const char *content, const char *type, const char *app_id, const char *iftype, const char *limit); int stc_plugin_appstatus_register_changed_cb(stc_s *stc, - stc_plugin_app_state_changed_cb cb, - void *data); + stc_plugin_app_state_changed_cb cb, void *data); int stc_plugin_appstatus_deregister_changed_cb(stc_s *stc); -int stc_plugin_exception_init(void); -int stc_plugin_exception_deinit(void); -int stc_plugin_exception_fill_list(void); -int stc_plugin_exception_update_list(void); -int stc_plugin_exception_check_by_cmdline(char *cmdline); - -#endif /* __STC_PLUGIN_H__ */ +#endif /* __STC_PLUGIN_APPSTATUS_H__ */ diff --git a/plugin/stc-plugin-appstatus.c b/plugin/appstatus/stc-plugin-appstatus.c similarity index 75% rename from plugin/stc-plugin-appstatus.c rename to plugin/appstatus/stc-plugin-appstatus.c index d642cd6..fb85dea 100755 --- a/plugin/stc-plugin-appstatus.c +++ b/plugin/appstatus/stc-plugin-appstatus.c @@ -24,7 +24,7 @@ #include #include -#include "stc-plugin.h" +#include "stc-plugin-appstatus.h" //LCOV_EXCL_START #define AUL_APP_STATUS_DBUS_PATH "/Org/Tizen/Aul/AppStatus" @@ -183,4 +183,59 @@ int stc_plugin_appstatus_deregister_changed_cb(stc_s *stc) __ground_status_monitor_deinit(stc); return 0; } + +int stc_plugin_popup_send_warn_message(const char *content, + const char *type, const char *app_id, const char *iftype, const char *warn) +{ + int ret = 0; + bundle *b = bundle_create(); + + STC_LOGD("Warn message : content[%s] type[%s] app_id[%s] warn[%s]", + content, type, app_id, warn); + + bundle_add(b, "_SYSPOPUP_CONTENT_", content); + bundle_add(b, "_SYSPOPUP_TYPE_", type); + bundle_add(b, "_APP_ID_", app_id); + bundle_add(b, "_IF_TYPE_", iftype); + bundle_add(b, "_WARN_LIMIT_", warn); + + ret = syspopup_launch("net-popup", b); + + bundle_free(b); + + return ret; +} + +int stc_plugin_popup_send_restriction_message(const char *content, + const char *type, const char *app_id, const char *iftype, const char *limit) +{ + int ret = 0; + bundle *b = bundle_create(); + + STC_LOGD("Restriction message : content[%s] type[%s] app_id[%s] limit[%s]", + content, type, app_id, limit); + + bundle_add(b, "_SYSPOPUP_CONTENT_", content); + bundle_add(b, "_SYSPOPUP_TYPE_", type); + bundle_add(b, "_APP_ID_", app_id); + bundle_add(b, "_IF_TYPE_", iftype); + bundle_add(b, "_RESTRICTION_LIMIT_", limit); + + ret = syspopup_launch("net-popup", b); + + bundle_free(b); + + return ret; +} + +API stc_plugin_appstatus_s stc_plugin_appstatus = { + .send_warn_message_to_net_popup = + stc_plugin_popup_send_warn_message, + .send_restriction_message_to_net_popup = + stc_plugin_popup_send_restriction_message, + .register_state_changed_cb = + stc_plugin_appstatus_register_changed_cb, + .deregister_state_changed_cb = + stc_plugin_appstatus_deregister_changed_cb +}; //LCOV_EXCL_STOP diff --git a/plugin/exception/CMakeLists.txt b/plugin/exception/CMakeLists.txt new file mode 100755 index 0000000..1ccf391 --- /dev/null +++ b/plugin/exception/CMakeLists.txt @@ -0,0 +1,36 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(stc-plugin-exception C) + +# Set required packages +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(exception_plugin REQUIRED + dlog + gio-2.0 + gio-unix-2.0 + glib-2.0 + pkgmgr-info + ) + +FOREACH(flag ${exception_plugin_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/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") + +ADD_DEFINITIONS("-DUSE_DLOG") + +SET(SRCS_PLUGIN + stc-plugin-exception.c + ) + +# library build +ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS_PLUGIN}) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${exception_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/exception/include/stc-plugin-exception.h b/plugin/exception/include/stc-plugin-exception.h new file mode 100755 index 0000000..70e38e0 --- /dev/null +++ b/plugin/exception/include/stc-plugin-exception.h @@ -0,0 +1,39 @@ +/* + * 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_EXCEPTION_H__ +#define __STC_PLUGIN_EXCEPTION_H__ + +#include +#include "stc-error.h" +#include "stc-manager.h" + +typedef struct { + int (*initialize_plugin) (void); + int (*deinitialize_plugin) (void); + + int (*fill_exception_list) (void); + int (*update_exception_list) (void); + int (*check_exception_by_cmdline) (char *cmdline); +} stc_plugin_exception_s; + +int stc_plugin_exception_initialize(void); +int stc_plugin_exception_deinitialize(void); +int stc_plugin_exception_fill_list(void); +int stc_plugin_exception_update_list(void); +int stc_plugin_exception_check_by_cmdline(char *cmdline); + +#endif /* __STC_PLUGIN_EXCEPTION_H__ */ diff --git a/plugin/stc-plugin-exception.c b/plugin/exception/stc-plugin-exception.c similarity index 93% rename from plugin/stc-plugin-exception.c rename to plugin/exception/stc-plugin-exception.c index 9484d92..783bc4f 100755 --- a/plugin/stc-plugin-exception.c +++ b/plugin/exception/stc-plugin-exception.c @@ -25,7 +25,7 @@ #include #include -#include "stc-plugin.h" +#include "stc-plugin-exception.h" //LCOV_EXCL_START #define EXE_TYPE_APPLICATION "app" @@ -231,7 +231,7 @@ static stc_error_e table_exceptions_foreach(const stc_exceptions_info_cb excepti return error_code; } -int stc_plugin_exception_init(void) +int stc_plugin_exception_initialize(void) { g_excns_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); @@ -239,7 +239,7 @@ int stc_plugin_exception_init(void) return STC_ERROR_NONE; } -int stc_plugin_exception_deinit(void) +int stc_plugin_exception_deinitialize(void) { if (g_excns_timer_id > 0) { g_source_remove(g_excns_timer_id); @@ -259,7 +259,6 @@ int stc_plugin_exception_fill_list(void) if (STC_DEBUG_LOG) __excn_hash_printall(); - g_excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL, __update_exceptions_app_list, NULL); @@ -268,7 +267,7 @@ int stc_plugin_exception_fill_list(void) } int stc_plugin_exception_update_list(void) -{ +{ __remove_exception_appall(); pkginfo_exceptions_foreach(__insert_exception_cb, NULL); @@ -288,4 +287,17 @@ int stc_plugin_exception_check_by_cmdline(char *cmdline) return STC_ERROR_NONE; } + +API stc_plugin_exception_s stc_plugin_exception = { + .initialize_plugin = + stc_plugin_exception_initialize, + .deinitialize_plugin = + stc_plugin_exception_deinitialize, + .fill_exception_list = + stc_plugin_exception_fill_list, + .update_exception_list = + stc_plugin_exception_update_list, + .check_exception_by_cmdline = + stc_plugin_exception_check_by_cmdline +}; //LCOV_EXCL_STOP diff --git a/plugin/procfs/CMakeLists.txt b/plugin/procfs/CMakeLists.txt new file mode 100755 index 0000000..dc04ca8 --- /dev/null +++ b/plugin/procfs/CMakeLists.txt @@ -0,0 +1,36 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(stc-plugin-procfs C) + +# Set required packages +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(procfs_plugin REQUIRED + dlog + gio-2.0 + gio-unix-2.0 + glib-2.0 + ) + +FOREACH(flag ${procfs_plugin_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/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") + +ADD_DEFINITIONS("-DUSE_DLOG") + +SET(SRCS_PLUGIN + stc-plugin-procfs.c + ) + +# library build +ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS_PLUGIN}) +ADD_DEPENDENCIES(${PROJECT_NAME} GENERATED_DBUS_CODE) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${procfs_plugin_LDFLAGS}) +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "" OUTPUT_NAME ${PROJECT_NAME}) + +# install +INSTALL(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${LIBDIR}) diff --git a/src/monitor/include/stc-exception.h b/plugin/procfs/include/stc-plugin-procfs.h similarity index 50% rename from src/monitor/include/stc-exception.h rename to plugin/procfs/include/stc-plugin-procfs.h index 38a2fd4..3a02bfe 100755 --- a/src/monitor/include/stc-exception.h +++ b/plugin/procfs/include/stc-plugin-procfs.h @@ -14,27 +14,24 @@ * limitations under the License. */ -#ifndef __STC_EXCEPTION_H__ -#define __STC_EXCEPTION_H__ +#ifndef __STC_PLUGIN_PROCFS_H__ +#define __STC_PLUGIN_PROCFS_H__ -#define EXE_TYPE_APPLICATION "app" -#define EXE_TYPE_INSTRUCTION "inst" -#define EXE_TYPE_SYSTEM "sys" -#define EXE_TYPE_SCRIPT "script" +#include +#include "stc-error.h" +#include "stc-manager.h" typedef struct { - char *process_name; - char *exe_type; -} stc_exceptions_info; + int (*initialize_plugin) (void); + int (*deinitialize_plugin) (void); + int (*procfs_status_changed) (stc_cmd_type_e cmd, pid_t pid, + const gchar *app_id, const gchar *pkg_id, stc_app_type_e app_type); +} stc_plugin_procfs_s; -typedef stc_cb_ret_e -(*stc_exceptions_info_cb)(const stc_exceptions_info *info, - void *user_data); +int stc_plugin_procfs_initialize(void); +int stc_plugin_procfs_deinitialize(void); -stc_error_e table_exceptions_foreach(const stc_exceptions_info_cb exception_cb, - void *user_data); +stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd, + pid_t pid, const gchar *app_id, const gchar *pkg_id, stc_app_type_e app_type); -stc_error_e pkginfo_exceptions_foreach(const stc_exceptions_info_cb exception_cb, - void *user_data); - -#endif /*__STC_EXCEPTION_H__ */ +#endif /* __STC_PLUGIN_PROCFS_H__ */ diff --git a/src/monitor/stc-app-lifecycle.c b/plugin/procfs/stc-plugin-procfs.c similarity index 82% rename from src/monitor/stc-app-lifecycle.c rename to plugin/procfs/stc-plugin-procfs.c index 2fcbfe6..28c5c9f 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/plugin/procfs/stc-plugin-procfs.c @@ -27,10 +27,11 @@ #include #include -#include "stc-manager-gdbus.h" -#include "stc-app-lifecycle.h" +#include "stc-plugin-procfs.h" +#include "stc-monitor.h" #include "helper-procfs.h" +//LCOV_EXCL_START typedef struct { pid_t pid; } proc_key_s; @@ -93,15 +94,14 @@ static proc_value_s * __proc_tree_lookup(const proc_key_s *key) proc_value_s *lookup; if (proc_tree == NULL) { - STC_LOGE("tree is null"); //LCOV_EXCL_LINE - return NULL; //LCOV_EXCL_LINE + STC_LOGE("tree is null"); + return NULL; } lookup = g_tree_lookup(proc_tree, key); return lookup; } -//LCOV_EXCL_START static gboolean __proc_tree_foreach_print(gpointer key, gpointer value, gpointer data) { @@ -114,11 +114,10 @@ static gboolean __proc_tree_foreach_print(gpointer key, gpointer value, return FALSE; } -//LCOV_EXCL_STOP static void __proc_tree_printall(void) { - g_tree_foreach(proc_tree, __proc_tree_foreach_print, NULL); //LCOV_EXCL_LINE + g_tree_foreach(proc_tree, __proc_tree_foreach_print, NULL); } static proc_value_s * __proc_tree_find_parent(proc_value_s *value) @@ -135,8 +134,8 @@ static proc_value_s * __proc_tree_find_parent(proc_value_s *value) } while (lookup); if (STC_DEBUG_LOG) { - if (parent != NULL) //LCOV_EXCL_LINE - STC_LOGD("\033[0;35mPARENT\033[0;m: tgid[\033[1;33m%s\033[0;m] pid[%s] " //LCOV_EXCL_LINE + if (parent != NULL) + STC_LOGD("\033[0;35mPARENT\033[0;m: tgid[\033[1;33m%s\033[0;m] pid[%s] " "ppid[\033[1;35m%s\033[0;m] cmdline[\033[0;34m%s\033[0;m] name[%s]", parent->status[PROC_STATUS_TGID], parent->status[PROC_STATUS_PID], parent->status[PROC_STATUS_PPID], parent->cmdline, @@ -153,14 +152,14 @@ static void __proc_tree_add(proc_key_s *key, proc_value_s *parent; if (proc_tree == NULL) { - STC_LOGE("tree is null"); //LCOV_EXCL_LINE - return; //LCOV_EXCL_LINE + STC_LOGE("tree is null"); + return; } lookup = g_tree_lookup(proc_tree, key); if (lookup) { if (STC_DEBUG_LOG) - STC_LOGD("LOOKUP: tgid[\033[1;33m%s\033[0;m] pid[%s] ppid[\033[1;35m%s\033[0;m] " //LCOV_EXCL_LINE + STC_LOGD("LOOKUP: tgid[\033[1;33m%s\033[0;m] pid[%s] ppid[\033[1;35m%s\033[0;m] " "cmdline[\033[0;34m%s\033[0;m] name[%s]", lookup->status[PROC_STATUS_TGID], lookup->status[PROC_STATUS_PID], lookup->status[PROC_STATUS_PPID], lookup->cmdline, lookup->status[PROC_STATUS_NAME]); @@ -177,27 +176,27 @@ static void __proc_tree_add(proc_key_s *key, parent = __proc_tree_find_parent(value); if (parent != NULL) - stc_manager_app_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid, + stc_plugin_procfs_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid, parent->cmdline, parent->cmdline, STC_APP_TYPE_SERVICE); else - stc_manager_app_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid, + stc_plugin_procfs_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid, value->cmdline, value->cmdline, STC_APP_TYPE_SERVICE); } static void __proc_tree_remove(const proc_key_s *key) { if (proc_tree == NULL) { - STC_LOGE("tree is null"); //LCOV_EXCL_LINE - return; //LCOV_EXCL_LINE + STC_LOGE("tree is null"); + return; } - stc_manager_app_status_changed(STC_CMD_SET_TERMINATED, key->pid, NULL, + stc_plugin_procfs_status_changed(STC_CMD_SET_TERMINATED, key->pid, NULL, NULL, STC_APP_TYPE_NONE); g_tree_remove(proc_tree, key); if (STC_DEBUG_LOG) - __proc_tree_printall(); //LCOV_EXCL_LINE + __proc_tree_printall(); } static gboolean __check_excn(char *cmdline) @@ -221,26 +220,26 @@ static void __open_nl_connector_sock(void) GIOChannel *gio = NULL; if (nl_connector_sock != -1 && - nl_connector_gsource_id != 0) { //LCOV_EXCL_LINE - STC_LOGE("Socket is already open"); //LCOV_EXCL_LINE - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return; //LCOV_EXCL_LINE + nl_connector_gsource_id != 0) { + STC_LOGE("Socket is already open"); + __STC_LOG_FUNC_EXIT__; + return; } if (nl_connector_sock != -1) { - close(nl_connector_sock); //LCOV_EXCL_LINE - nl_connector_sock = -1; //LCOV_EXCL_LINE + close(nl_connector_sock); + nl_connector_sock = -1; } if (nl_connector_gsource_id != 0) { - g_source_remove(nl_connector_gsource_id); //LCOV_EXCL_LINE - nl_connector_gsource_id = 0; //LCOV_EXCL_LINE + g_source_remove(nl_connector_gsource_id); + nl_connector_gsource_id = 0; } nl_connector_sock = create_netlink(NETLINK_CONNECTOR, CN_IDX_PROC); if (nl_connector_sock == -1) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return; //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; + return; } gio = g_io_channel_unix_new(nl_connector_sock); @@ -269,131 +268,9 @@ static void __close_nl_connector_sock(void) static void __reopen_nl_connector_sock(void) { - __close_nl_connector_sock(); //LCOV_EXCL_LINE - __open_nl_connector_sock(); //LCOV_EXCL_LINE -} - -//LCOV_EXCL_START -stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, - pid_t pid, - const gchar *app_id, - const gchar *pkg_id, - stc_app_type_e app_type) -{ - stc_error_e ret = STC_ERROR_NONE; - - if (pkg_id && app_id) - STC_LOGD("cmd [%d] pkgid [%s] appid [%s] pid[%d] type [%d]", - cmd, pkg_id, app_id, pid, app_type); - - switch (cmd) { - case STC_CMD_SET_FOREGRD: - { - stc_app_key_s app_key; - stc_app_value_s app_value; - stc_process_key_s proc_key; - stc_process_value_s proc_value; - - memset(&app_key, 0, sizeof(stc_app_key_s)); - memset(&app_value, 0, sizeof(stc_app_value_s)); - memset(&proc_key, 0, sizeof(stc_process_key_s)); - memset(&proc_value, 0, sizeof(stc_process_value_s)); - - app_key.pkg_id = g_strdup(pkg_id); - app_key.app_id = g_strdup(app_id); - - app_value.type = app_type; - app_value.processes = NULL; - - proc_key.pid = pid; - - proc_value.ground = STC_APP_STATE_FOREGROUND; - - stc_monitor_application_add(app_key, app_value); - stc_monitor_process_add(app_key, proc_key, proc_value); - stc_monitor_process_update_ground(app_key, proc_key, - STC_APP_STATE_FOREGROUND); - - FREE(app_key.pkg_id); - FREE(app_key.app_id); - break; - } - case STC_CMD_SET_BACKGRD: - { - stc_app_key_s app_key; - stc_app_value_s app_value; - stc_process_key_s proc_key; - stc_process_value_s proc_value; - - memset(&app_key, 0, sizeof(stc_app_key_s)); - memset(&app_value, 0, sizeof(stc_app_value_s)); - memset(&proc_key, 0, sizeof(stc_process_key_s)); - memset(&proc_value, 0, sizeof(stc_process_value_s)); - - app_key.pkg_id = g_strdup(pkg_id); - app_key.app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, - NULL); - - app_value.type = app_type; - app_value.processes = NULL; - - proc_key.pid = pid; - - proc_value.ground = STC_APP_STATE_BACKGROUND; - - stc_monitor_application_add(app_key, app_value); - stc_monitor_process_add(app_key, proc_key, proc_value); - stc_monitor_process_update_ground(app_key, proc_key, - STC_APP_STATE_BACKGROUND); - - FREE(app_key.pkg_id); - FREE(app_key.app_id); - break; - } - case STC_CMD_SET_SERVICE_LAUNCHED: - { - stc_app_key_s app_key; - stc_app_value_s app_value; - stc_process_key_s proc_key; - stc_process_value_s proc_value; - - memset(&app_key, 0, sizeof(stc_app_key_s)); - memset(&app_value, 0, sizeof(stc_app_value_s)); - memset(&proc_key, 0, sizeof(stc_process_key_s)); - memset(&proc_value, 0, sizeof(stc_process_value_s)); - - app_key.pkg_id = g_strdup(pkg_id); - app_key.app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, - NULL); - - app_value.type = app_type; - app_value.processes = NULL; - - proc_key.pid = pid; - - /* services will run always in background. */ - proc_value.ground = STC_APP_STATE_BACKGROUND; - - stc_monitor_application_add(app_key, app_value); - stc_monitor_process_add(app_key, proc_key, proc_value); - - FREE(app_key.pkg_id); - g_free(app_key.app_id); - break; - } - case STC_CMD_SET_TERMINATED: - { - stc_monitor_process_remove(pid); - break; - } - default: - STC_LOGE("Unhandled command"); - ret = STC_ERROR_INVALID_PARAMETER; - } - - return ret; + __close_nl_connector_sock(); + __open_nl_connector_sock(); } -//LCOV_EXCL_STOP static void __process_event_fork(int tgid, int pid) { @@ -407,7 +284,7 @@ static void __process_event_fork(int tgid, int pid) if (__check_excn(cmdline)) { if (STC_DEBUG_LOG) - STC_LOGD("[%s] monitoring is excepted", cmdline); //LCOV_EXCL_LINE + STC_LOGD("[%s] monitoring is excepted", cmdline); return; } @@ -417,15 +294,15 @@ static void __process_event_fork(int tgid, int pid) key = MALLOC0(proc_key_s, 1); if (key == NULL) { - STC_LOGE("memory allocation failed"); //LCOV_EXCL_LINE - return; //LCOV_EXCL_LINE + STC_LOGE("memory allocation failed"); + return; } value = MALLOC0(proc_value_s, 1); if (value == NULL) { - STC_LOGE("memory allocation failed"); //LCOV_EXCL_LINE - FREE(key); //LCOV_EXCL_LINE - return; //LCOV_EXCL_LINE + STC_LOGE("memory allocation failed"); + FREE(key); + return; } key->pid = tgid; @@ -434,9 +311,9 @@ static void __process_event_fork(int tgid, int pid) g_strlcpy(value->cmdline, cmdline, sizeof(value->cmdline)); if (STC_DEBUG_LOG) { - STC_LOGD("\033[1;34mFORK\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] " //LCOV_EXCL_LINE + STC_LOGD("\033[1;34mFORK\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] " "cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, pid); - STC_LOGD("STATUS: tgid[%s] pid[%s] ppid[%s] name[%s] state[%s] tracerpid[%s]", //LCOV_EXCL_LINE + STC_LOGD("STATUS: tgid[%s] pid[%s] ppid[%s] name[%s] state[%s] tracerpid[%s]", status[PROC_STATUS_TGID], status[PROC_STATUS_PID], status[PROC_STATUS_PPID], status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]); } @@ -457,7 +334,7 @@ static void __process_event_exec(int tgid, int pid) if (__check_excn(cmdline)) { if (STC_DEBUG_LOG) - STC_LOGD("[%s] monitoring is excepted", cmdline); //LCOV_EXCL_LINE + STC_LOGD("[%s] monitoring is excepted", cmdline); return; } @@ -467,15 +344,15 @@ static void __process_event_exec(int tgid, int pid) key = MALLOC0(proc_key_s, 1); if (key == NULL) { - STC_LOGE("memory allocation failed"); //LCOV_EXCL_LINE - return; //LCOV_EXCL_LINE + STC_LOGE("memory allocation failed"); + return; } value = MALLOC0(proc_value_s, 1); if (value == NULL) { - STC_LOGE("memory allocation failed"); //LCOV_EXCL_LINE - FREE(key); //LCOV_EXCL_LINE - return; //LCOV_EXCL_LINE + STC_LOGE("memory allocation failed"); + FREE(key); + return; } key->pid = tgid; @@ -484,9 +361,9 @@ static void __process_event_exec(int tgid, int pid) g_strlcpy(value->cmdline, cmdline, sizeof(value->cmdline)); if (STC_DEBUG_LOG) { - STC_LOGD("\033[1;32mEXEC\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] " //LCOV_EXCL_LINE + STC_LOGD("\033[1;32mEXEC\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] " "cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, pid); - STC_LOGD("STATUS: tgid[%s] pid[%s] ppid[%s] name[%s] state[%s] tracerpid[%s]", //LCOV_EXCL_LINE + STC_LOGD("STATUS: tgid[%s] pid[%s] ppid[%s] name[%s] state[%s] tracerpid[%s]", status[PROC_STATUS_TGID], status[PROC_STATUS_PID], status[PROC_STATUS_PPID], status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]); } @@ -509,7 +386,7 @@ static void __process_event_exit(int tgid, int pid, int exit_code) return; if (STC_DEBUG_LOG) - STC_LOGD("\033[1;31mEXIT\033[0;m: tgid[\033[1;33m%d\033[0;m] " //LCOV_EXCL_LINE + STC_LOGD("\033[1;31mEXIT\033[0;m: tgid[\033[1;33m%d\033[0;m] " "pid[%d] exitcode[\033[0;31m%d\033[0;m]", tgid, pid, exit_code); __proc_tree_remove(&key); @@ -527,21 +404,21 @@ static gboolean __process_nl_connector_message(GIOChannel *source, (condition & G_IO_NVAL)) { /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */ - STC_LOGE("Netlink Connector socket received G_IO event, closing" //LCOV_EXCL_LINE + STC_LOGE("Netlink Connector socket received G_IO event, closing" " socket. G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]", (condition & G_IO_ERR), (condition & G_IO_HUP), (condition & G_IO_NVAL)); - __reopen_nl_connector_sock(); //LCOV_EXCL_LINE - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return FALSE; //LCOV_EXCL_LINE + __reopen_nl_connector_sock(); + __STC_LOG_FUNC_EXIT__; + return FALSE; } memset(&msg, 0, sizeof(nl_connector_proc_event_s)); ret = read(sock, &msg, sizeof(nl_connector_proc_event_s)); if (ret == 0) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return TRUE; //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; + return TRUE; } switch (msg.proc_ev.what) { @@ -573,8 +450,8 @@ static int __subscribe_proc_events(void) int sock = nl_connector_sock; if (sock == -1) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return -1; //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; + return -1; } memset(&msg, 0, sizeof(nl_connector_msg_s)); @@ -591,9 +468,9 @@ static int __subscribe_proc_events(void) ret = send(sock, &msg, sizeof(nl_connector_msg_s), 0); if (ret == -1) { - STC_LOGE("Error sending netlink connector message"); //LCOV_EXCL_LINE - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return -1; //LCOV_EXCL_LINE + STC_LOGE("Error sending netlink connector message"); + __STC_LOG_FUNC_EXIT__; + return -1; } __STC_LOG_FUNC_EXIT__; @@ -608,8 +485,8 @@ static int __unsubscribe_proc_events(void) int sock = nl_connector_sock; if (sock == -1) { - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return -1; //LCOV_EXCL_LINE + __STC_LOG_FUNC_EXIT__; + return -1; } memset(&msg, 0, sizeof(nl_connector_msg_s)); @@ -626,16 +503,16 @@ static int __unsubscribe_proc_events(void) ret = send(sock, &msg, sizeof(nl_connector_msg_s), 0); if (ret == -1) { - STC_LOGE("Error sending netlink connector message"); //LCOV_EXCL_LINE - __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE - return -1; //LCOV_EXCL_LINE + STC_LOGE("Error sending netlink connector message"); + __STC_LOG_FUNC_EXIT__; + return -1; } __STC_LOG_FUNC_EXIT__; return 0; } -void stc_app_lifecycle_monitor_init(void) +int stc_plugin_procfs_initialize(void) { __STC_LOG_FUNC_ENTER__; @@ -648,15 +525,16 @@ void stc_app_lifecycle_monitor_init(void) __open_nl_connector_sock(); __subscribe_proc_events(); __STC_LOG_FUNC_EXIT__; + return STC_ERROR_NONE; } -void stc_app_lifecycle_monitor_deinit(void) +int stc_plugin_procfs_deinitialize(void) { __STC_LOG_FUNC_ENTER__; if (nl_connector_sock == -1) { - STC_LOGE("socket already closed"); //LCOV_EXCL_LINE - return; //LCOV_EXCL_LINE + STC_LOGD("socket already closed"); + return STC_ERROR_NONE; } __unsubscribe_proc_events(); @@ -666,4 +544,132 @@ void stc_app_lifecycle_monitor_deinit(void) proc_tree = NULL; __STC_LOG_FUNC_EXIT__; + return STC_ERROR_NONE; } + +stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd, + pid_t pid, const gchar *app_id, const gchar *pkg_id, stc_app_type_e app_type) +{ + stc_error_e ret = STC_ERROR_NONE; + + if (pkg_id && app_id) + STC_LOGD("cmd [%d] pkgid [%s] appid [%s] pid[%d] type [%d]", + cmd, pkg_id, app_id, pid, app_type); + + switch (cmd) { + case STC_CMD_SET_FOREGRD: + { + stc_app_key_s app_key; + stc_app_value_s app_value; + stc_process_key_s proc_key; + stc_process_value_s proc_value; + + memset(&app_key, 0, sizeof(stc_app_key_s)); + memset(&app_value, 0, sizeof(stc_app_value_s)); + memset(&proc_key, 0, sizeof(stc_process_key_s)); + memset(&proc_value, 0, sizeof(stc_process_value_s)); + + app_key.pkg_id = g_strdup(pkg_id); + app_key.app_id = g_strdup(app_id); + + app_value.type = app_type; + app_value.processes = NULL; + + proc_key.pid = pid; + + proc_value.ground = STC_APP_STATE_FOREGROUND; + + stc_monitor_application_add(app_key, app_value); + stc_monitor_process_add(app_key, proc_key, proc_value); + stc_monitor_process_update_ground(app_key, proc_key, + STC_APP_STATE_FOREGROUND); + + FREE(app_key.pkg_id); + FREE(app_key.app_id); + break; + } + case STC_CMD_SET_BACKGRD: + { + stc_app_key_s app_key; + stc_app_value_s app_value; + stc_process_key_s proc_key; + stc_process_value_s proc_value; + + memset(&app_key, 0, sizeof(stc_app_key_s)); + memset(&app_value, 0, sizeof(stc_app_value_s)); + memset(&proc_key, 0, sizeof(stc_process_key_s)); + memset(&proc_value, 0, sizeof(stc_process_value_s)); + + app_key.pkg_id = g_strdup(pkg_id); + app_key.app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, + NULL); + + app_value.type = app_type; + app_value.processes = NULL; + + proc_key.pid = pid; + + proc_value.ground = STC_APP_STATE_BACKGROUND; + + stc_monitor_application_add(app_key, app_value); + stc_monitor_process_add(app_key, proc_key, proc_value); + stc_monitor_process_update_ground(app_key, proc_key, + STC_APP_STATE_BACKGROUND); + + FREE(app_key.pkg_id); + FREE(app_key.app_id); + break; + } + case STC_CMD_SET_SERVICE_LAUNCHED: + { + stc_app_key_s app_key; + stc_app_value_s app_value; + stc_process_key_s proc_key; + stc_process_value_s proc_value; + + memset(&app_key, 0, sizeof(stc_app_key_s)); + memset(&app_value, 0, sizeof(stc_app_value_s)); + memset(&proc_key, 0, sizeof(stc_process_key_s)); + memset(&proc_value, 0, sizeof(stc_process_value_s)); + + app_key.pkg_id = g_strdup(pkg_id); + app_key.app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, + NULL); + + app_value.type = app_type; + app_value.processes = NULL; + + proc_key.pid = pid; + + /* services will run always in background. */ + proc_value.ground = STC_APP_STATE_BACKGROUND; + + stc_monitor_application_add(app_key, app_value); + stc_monitor_process_add(app_key, proc_key, proc_value); + + FREE(app_key.pkg_id); + g_free(app_key.app_id); + break; + } + case STC_CMD_SET_TERMINATED: + { + stc_monitor_process_remove(pid); + break; + } + default: + STC_LOGE("Unhandled command"); + ret = STC_ERROR_INVALID_PARAMETER; + } + + return ret; +} + +API stc_plugin_procfs_s stc_plugin_procfs = { + .initialize_plugin = + stc_plugin_procfs_initialize, + .deinitialize_plugin = + stc_plugin_procfs_deinitialize, + .procfs_status_changed = + stc_plugin_procfs_status_changed +}; +//LCOV_EXCL_STOP diff --git a/plugin/stc-plugin-popup.c b/plugin/stc-plugin-popup.c deleted file mode 100755 index 409c890..0000000 --- a/plugin/stc-plugin-popup.c +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "stc-plugin.h" - -//LCOV_EXCL_START -int stc_plugin_popup_send_warn_message(const char *content, - const char *type, const char *app_id, const char *iftype, const char *warn) -{ - int ret = 0; - bundle *b = bundle_create(); - - STC_LOGD("Warn message : content[%s] type[%s] app_id[%s] warn[%s]", - content, type, app_id, warn); - - bundle_add(b, "_SYSPOPUP_CONTENT_", content); - bundle_add(b, "_SYSPOPUP_TYPE_", type); - bundle_add(b, "_APP_ID_", app_id); - bundle_add(b, "_IF_TYPE_", iftype); - bundle_add(b, "_WARN_LIMIT_", warn); - - ret = syspopup_launch("net-popup", b); - - bundle_free(b); - - return ret; -} - -int stc_plugin_popup_send_restriction_message(const char *content, - const char *type, const char *app_id, const char *iftype, const char *limit) -{ - int ret = 0; - bundle *b = bundle_create(); - - STC_LOGD("Restriction message : content[%s] type[%s] app_id[%s] limit[%s]", - content, type, app_id, limit); - - bundle_add(b, "_SYSPOPUP_CONTENT_", content); - bundle_add(b, "_SYSPOPUP_TYPE_", type); - bundle_add(b, "_APP_ID_", app_id); - bundle_add(b, "_IF_TYPE_", iftype); - bundle_add(b, "_RESTRICTION_LIMIT_", limit); - - ret = syspopup_launch("net-popup", b); - - bundle_free(b); - - return ret; -} -//LCOV_EXCL_STOP diff --git a/plugin/stc-plugin.c b/plugin/stc-plugin.c deleted file mode 100755 index b1b4013..0000000 --- a/plugin/stc-plugin.c +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "stc-plugin.h" - -//LCOV_EXCL_START -void stc_plugin_initialize(void) -{ - stc_plugin_exception_init(); -} - -void stc_plugin_deinitialize(void) -{ - stc_plugin_exception_deinit(); -} - -API stc_plugin_s stc_plugin = { - .initialize_plugin = - stc_plugin_initialize, - .deinitialize_plugin = - stc_plugin_deinitialize, - .send_warn_message_to_net_popup = - stc_plugin_popup_send_restriction_message, - .send_restriction_message_to_net_popup = - stc_plugin_popup_send_restriction_message, - .register_state_changed_cb = - stc_plugin_appstatus_register_changed_cb, - .deregister_state_changed_cb = - stc_plugin_appstatus_deregister_changed_cb, - .fill_exception_list = - stc_plugin_exception_fill_list, - .update_exception_list = - stc_plugin_exception_update_list, - .check_exception_by_cmdline = - stc_plugin_exception_check_by_cmdline -}; -//LCOV_EXCL_STOP diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d8bc359..f01f292 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,7 +7,6 @@ SET(REQUIRES_LIST ${REQUIRES_LIST} dlog vconf capi-system-info - pkgmgr-info openssl ) @@ -28,6 +27,10 @@ SET(DATABASE_SOURCE_DIR ${SOURCE_DIR}/database) SET(MONITOR_SOURCE_DIR ${SOURCE_DIR}/monitor) SET(CONFIGURE_SOURCE_DIR ${SOURCE_DIR}/configure) SET(LIMITATION_SOURCE_DIR ${SOURCE_DIR}/limitation) +SET(PLUGIN_DIR ${CMAKE_SOURCE_DIR}/plugin) +SET(APPSTATUS_SOURCE_DIR ${PLUGIN_DIR}/appstatus) +SET(EXCEPTION_SOURCE_DIR ${PLUGIN_DIR}/exception) +SET(PROCFS_SOURCE_DIR ${PLUGIN_DIR}/procfs) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/interfaces) @@ -47,6 +50,10 @@ INCLUDE_DIRECTORIES(${CONFIGURE_SOURCE_DIR}/include) INCLUDE_DIRECTORIES(${LIMITATION_SOURCE_DIR}) INCLUDE_DIRECTORIES(${LIMITATION_SOURCE_DIR}/include) +INCLUDE_DIRECTORIES(${APPSTATUS_SOURCE_DIR}/include) +INCLUDE_DIRECTORIES(${EXCEPTION_SOURCE_DIR}/include) +INCLUDE_DIRECTORIES(${PROCFS_SOURCE_DIR}/include) + FILE(GLOB SOURCE_SRCS ${SOURCE_DIR}/*.c) FILE(GLOB HELPER_SRCS ${HELPER_SOURCE_DIR}/*.c) FILE(GLOB MONITOR_SRCS ${MONITOR_SOURCE_DIR}/*.c) @@ -80,22 +87,12 @@ IF(BUILD_GTESTS) ADD_DEFINITIONS(-DTIZEN_GTESTS) ENDIF(BUILD_GTESTS) -ADD_CUSTOM_COMMAND( - WORKING_DIRECTORY - OUTPUT ${CMAKE_SOURCE_DIR}/src/generated-code.c - COMMAND gdbus-codegen --interface-prefix net.stc. - --generate-c-code generated-code - --c-namespace Stc - --c-generate-object-manager - --generate-docbook generated-code-docs - ${INTERFACES}/stcmanager-iface-manager.xml - ${INTERFACES}/stcmanager-iface-restriction.xml - ${INTERFACES}/stcmanager-iface-statistics.xml - COMMENT "Generating GDBus .c/.h") - -SET(SRCS ${SRCS} ${SOURCE_DIR}/generated-code.c) +SET(SRCS ${SRCS} ${INTERFACES}/generated-code.c) +SET_SOURCE_FILES_PROPERTIES(${INTERFACES}/generated-code.c PROPERTIES GENERATED TRUE) + ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${stc_pkgs_LDFLAGS} -ldl) INSTALL(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${BIN_DIR}) +ADD_DEPENDENCIES(${PROJECT_NAME} GENERATED_DBUS_CODE) ADD_SUBDIRECTORY(utils) diff --git a/src/helper/helper-nl.c b/src/helper/helper-nl.c index a42e565..2c3c8f9 100755 --- a/src/helper/helper-nl.c +++ b/src/helper/helper-nl.c @@ -25,7 +25,7 @@ * create_netlink(): Create netlink socket and returns it. * Returns: Created socket on success and -1 on failure. */ -int create_netlink(int protocol, uint32_t groups) +API int create_netlink(int protocol, uint32_t groups) { /** * TODO it's one socket, in future make set of sockets diff --git a/src/helper/helper-nl.h b/src/helper/helper-nl.h index fddb2aa..3df7da1 100755 --- a/src/helper/helper-nl.h +++ b/src/helper/helper-nl.h @@ -26,6 +26,8 @@ #include #include +#include "stc-manager.h" + #define NLA_BUF_MAX 65560 /*(65 * 1024) - used in tc_common, we'll do the same */ diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c index 4cf1bb2..020987d 100755 --- a/src/helper/helper-procfs.c +++ b/src/helper/helper-procfs.c @@ -40,7 +40,7 @@ #define USRAPPS "/usr/apps/" -int proc_get_cmdline(pid_t pid, char *cmdline) +API int proc_get_cmdline(pid_t pid, char *cmdline) { char buf[PROC_BUF_MAX]; char cmdline_buf[PROC_NAME_MAX]; @@ -176,7 +176,7 @@ int proc_get_raw_cmdline(pid_t pid, char *buf, int len) } //LCOV_EXCL_STOP -int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX]) +API int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX]) { unsigned int i; unsigned int index = 0; diff --git a/src/monitor/stc-exception.c b/src/monitor/stc-exception.c deleted file mode 100755 index a9e3523..0000000 --- a/src/monitor/stc-exception.c +++ /dev/null @@ -1,164 +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. - */ - -/** - * This file implements exceptions entity handler methods. - * - * @file stc-exception.c - */ - -#include - -#include "stc-manager.h" -#include "stc-exception.h" - -#define EXCEPTION_BUF_MAX 64 -#define EXCEPTION_STORAGE "/var/lib/stc/exceptions" - -#define INTERNET_PRIVILEGE "http://tizen.org/privilege/internet" - -static GHashTable *g_pkginfo_filter_hash; - -stc_error_e table_exceptions_foreach(const stc_exceptions_info_cb exception_cb, - void *user_data) -{ - __STC_LOG_FUNC_ENTER__; - - stc_error_e error_code = STC_ERROR_NONE; - stc_exceptions_info data; - - FILE *fp = NULL; - char buf[EXCEPTION_BUF_MAX] = {0, }; - - fp = fopen(EXCEPTION_STORAGE, "r"); - ret_value_msg_if(!fp, STC_ERROR_FAIL, "Failed to open %s file"); - - while (fgets(buf, sizeof(buf), fp) != NULL) { - char *process_name, *exe_type; - char *save_ptr = NULL; - - process_name = strtok_r(buf, ":", &save_ptr); - if (process_name != NULL) - data.process_name = process_name; - else - data.process_name = "none"; //LCOV_EXCL_LINE - - exe_type = strtok_r(NULL, "\n", &save_ptr); - if (exe_type != NULL) - data.exe_type = exe_type; - else - data.exe_type = "none"; //LCOV_EXCL_LINE - - if (exception_cb(&data, user_data) == STC_CANCEL) - break; - } - fclose(fp); - - __STC_LOG_FUNC_EXIT__; - return error_code; -} - -static int __pkginfo_filter_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data) -{ - int ret = 0; - char *pkgname = NULL; - - ret = pkgmgrinfo_pkginfo_get_pkgname(handle, &pkgname); - if (ret == PMINFO_R_OK) { - if (g_hash_table_insert(g_pkginfo_filter_hash, - g_strdup(pkgname), g_strdup(EXE_TYPE_APPLICATION)) != TRUE) - STC_LOGE("Failed to insert hash table"); //LCOV_EXCL_LINE - } - - return STC_CONTINUE; -} - -static int __pkginfo_pkg_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data) -{ - int ret = 0; - char *pkgname = NULL; - char *exe_type = NULL; - stc_exceptions_info data; - const stc_exceptions_info_cb excn_cb = user_data; - - ret = pkgmgrinfo_pkginfo_get_pkgname(handle, &pkgname); - if (ret == PMINFO_R_OK) { - exe_type = g_hash_table_lookup(g_pkginfo_filter_hash, pkgname); - if (exe_type) - return STC_CONTINUE; - - data.process_name = pkgname; - data.exe_type = EXE_TYPE_APPLICATION; - - if (excn_cb(&data, NULL) == STC_CANCEL) - STC_LOGE("Failed to insert hash table"); //LCOV_EXCL_LINE - } - - return STC_CONTINUE; -} - -stc_error_e pkginfo_exceptions_foreach(const stc_exceptions_info_cb exception_cb, - void *user_data) -{ - __STC_LOG_FUNC_ENTER__; - - int ret = 0; - int err = STC_ERROR_NONE; - pkgmgrinfo_pkginfo_filter_h handle; - - g_pkginfo_filter_hash = g_hash_table_new_full(g_str_hash, - g_str_equal, g_free, g_free); - - ret = pkgmgrinfo_pkginfo_filter_create(&handle); - ret_value_msg_if(ret != PMINFO_R_OK, STC_ERROR_FAIL, - "Failed to create pkginfo filter"); - - ret = pkgmgrinfo_pkginfo_filter_add_string(handle, - PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE, - INTERNET_PRIVILEGE); - if (ret != PMINFO_R_OK) { - STC_LOGE("Failed to add pkginfo filter string"); //LCOV_EXCL_LINE - err = STC_ERROR_FAIL; //LCOV_EXCL_LINE - goto out; //LCOV_EXCL_LINE - } - - ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle, - __pkginfo_filter_list_cb, NULL); - if (ret != PMINFO_R_OK) { - STC_LOGE("Failed to foreach pkginfo filter"); //LCOV_EXCL_LINE - err = STC_ERROR_FAIL; //LCOV_EXCL_LINE - goto out; //LCOV_EXCL_LINE - } - - ret = pkgmgrinfo_pkginfo_get_list(__pkginfo_pkg_list_cb, exception_cb); - if (ret != PMINFO_R_OK) { - STC_LOGE("Failed to get pkginfo list"); //LCOV_EXCL_LINE - err = STC_ERROR_FAIL; //LCOV_EXCL_LINE - goto out; //LCOV_EXCL_LINE - } - -out: - if (g_pkginfo_filter_hash) { - g_hash_table_destroy(g_pkginfo_filter_hash); - g_pkginfo_filter_hash = NULL; - } - - if (handle) - pkgmgrinfo_pkginfo_filter_destroy(handle); - - __STC_LOG_FUNC_EXIT__; - return err; -} diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index b3edf39..b27c97f 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -28,7 +28,8 @@ #include "table-statistics.h" #include "table-counters.h" #include "stc-monitor.h" -#include "stc-manager-plugin.h" +#include "stc-manager-plugin-appstatus.h" +#include "stc-manager-plugin-exception.h" #define MAX_INT_LENGTH 128 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state" @@ -767,7 +768,7 @@ static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key, snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit); - stc_manager_plugin_send_warn_message("warn threshold crossed", + stc_plugin_appstatus_send_warn_message("warn threshold crossed", "warning_noti", rstn_key->app_id, iftype, byte); @@ -808,7 +809,7 @@ static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key, snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit); - stc_manager_plugin_send_restriction_message("restriction threshold crossed", + stc_plugin_appstatus_send_restriction_message("restriction threshold crossed", "restriction_noti", rstn_key->app_id, iftype, byte); } @@ -1546,7 +1547,7 @@ static stc_error_e __process_update_background(void) static void __fill_exceptions_list(void) { - stc_manager_plugin_fill_exception_list(); + stc_plugin_fill_exception_list(); } stc_error_e stc_monitor_init(void) @@ -1642,7 +1643,7 @@ stc_error_e stc_monitor_deinit(void) return STC_ERROR_NONE; } -stc_error_e stc_monitor_application_add(const stc_app_key_s app_key, +API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key, const stc_app_value_s app_value) { stc_error_e ret = STC_ERROR_NONE; @@ -1694,7 +1695,7 @@ stc_error_e stc_monitor_application_add(const stc_app_key_s app_key, return ret; } -stc_error_e stc_monitor_process_add(const stc_app_key_s app_key, +API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key, const stc_process_key_s proc_key, const stc_process_value_s proc_value) { @@ -1746,7 +1747,7 @@ stc_error_e stc_monitor_process_add(const stc_app_key_s app_key, return ret; } -stc_error_e stc_monitor_process_remove(pid_t pid) +API stc_error_e stc_monitor_process_remove(pid_t pid) { stc_error_e ret = STC_ERROR_NONE; stc_process_key_s proc_key = { @@ -1774,7 +1775,7 @@ stc_error_e stc_monitor_process_remove(pid_t pid) } //LCOV_EXCL_START -stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key, +API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key, const stc_process_key_s proc_key, stc_app_state_e ground) { @@ -1920,9 +1921,9 @@ stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info) return ret; } -stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline) +API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline) { - return stc_manager_plugin_check_exception_by_cmdline(cmdline); + return stc_plugin_check_exception_by_cmdline(cmdline); } int stc_monitor_get_counter_socket(void) diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index 46a3d42..a321a4c 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -19,8 +19,8 @@ #include "stc-statistics.h" #include "stc-restriction.h" #include "stc-default-connection.h" -#include "stc-manager-plugin.h" -#include "stc-app-lifecycle.h" +#include "stc-manager-plugin-appstatus.h" +#include "stc-manager-plugin-procfs.h" #include "helper-iptables.h" static gboolean __stc_manager_gdbus_statistics_init(stc_s *stc) @@ -194,8 +194,9 @@ static void __stc_manager_gdbus_on_bus_acquired(GDBusConnection *connection, iptables_init(); stc_default_connection_monitor_init(stc); - stc_manager_plugin_register_state_changed_cb(stc, - stc_manager_app_status_changed, NULL); + + stc_plugin_appstatus_register_state_changed_cb(stc, + stc_plugin_procfs_app_status_changed, NULL); __STC_LOG_FUNC_EXIT__; } @@ -238,7 +239,7 @@ void stc_manager_gdbus_deinit(gpointer stc_data) __STC_LOG_FUNC_ENTER__; stc_s *stc = (stc_s *)stc_data; - stc_manager_plugin_deregister_state_changed_cb(stc); + stc_plugin_appstatus_deregister_state_changed_cb(stc); stc_default_connection_monitor_deinit(stc); g_bus_unown_name(stc->gdbus_owner_id); diff --git a/src/stc-manager-plugin.c b/src/stc-manager-plugin-appstatus.c similarity index 56% rename from src/stc-manager-plugin.c rename to src/stc-manager-plugin-appstatus.c index f4aaae9..8bc067b 100755 --- a/src/stc-manager-plugin.c +++ b/src/stc-manager-plugin-appstatus.c @@ -17,25 +17,25 @@ #include #include "stc-manager.h" -#include "stc-manager-plugin.h" +#include "stc-manager-plugin-appstatus.h" static gboolean stc_plugin_enabled = FALSE; static void *handle_plugin; -static stc_plugin_s *stc_plugin; +static stc_plugin_appstatus_s *stc_plugin; //LCOV_EXCL_START -int stc_manager_plugin_init(void) +int stc_plugin_appstatus_init(void) { __STC_LOG_FUNC_ENTER__; - handle_plugin = dlopen(STC_PLUGIN_FILEPATH, RTLD_NOW); + handle_plugin = dlopen(STC_PLUGIN_APPSTATUS_FILEPATH, RTLD_NOW); if (!handle_plugin) { - STC_LOGE("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror()); + STC_LOGE("Can't load %s: %s", STC_PLUGIN_APPSTATUS_FILEPATH, dlerror()); __STC_LOG_FUNC_EXIT__; return STC_ERROR_UNINITIALIZED; } - stc_plugin = dlsym(handle_plugin, "stc_plugin"); + stc_plugin = dlsym(handle_plugin, "stc_plugin_appstatus"); if (!stc_plugin) { STC_LOGE("Can't load symbol: %s", dlerror()); dlclose(handle_plugin); @@ -44,13 +44,12 @@ int stc_manager_plugin_init(void) } stc_plugin_enabled = TRUE; - stc_plugin->initialize_plugin(); __STC_LOG_FUNC_EXIT__; return STC_ERROR_NONE; } -int stc_manager_plugin_deinit(void) +int stc_plugin_appstatus_deinit(void) { __STC_LOG_FUNC_ENTER__; @@ -59,17 +58,14 @@ int stc_manager_plugin_deinit(void) return STC_ERROR_UNINITIALIZED; } - stc_plugin->deinitialize_plugin(); - stc_plugin_enabled = FALSE; dlclose(handle_plugin); __STC_LOG_FUNC_EXIT__; return STC_ERROR_NONE; } -int stc_manager_plugin_send_warn_message(const char *content, const char *type, - const char *app_id, const char *iftype, - const char *warn) +int stc_plugin_appstatus_send_warn_message(const char *content, + const char *type, const char *app_id, const char *iftype, const char *warn) { __STC_LOG_FUNC_ENTER__; @@ -86,15 +82,12 @@ int stc_manager_plugin_send_warn_message(const char *content, const char *type, } __STC_LOG_FUNC_EXIT__; - return stc_plugin->send_warn_message_to_net_popup(content, type, app_id, - iftype, warn); + return stc_plugin->send_warn_message_to_net_popup(content, + type, app_id, iftype, warn); } -int stc_manager_plugin_send_restriction_message(const char *content, - const char *type, - const char *app_id, - const char *iftype, - const char *limit) +int stc_plugin_appstatus_send_restriction_message(const char *content, + const char *type, const char *app_id, const char *iftype, const char *limit) { __STC_LOG_FUNC_ENTER__; @@ -111,14 +104,12 @@ int stc_manager_plugin_send_restriction_message(const char *content, } __STC_LOG_FUNC_EXIT__; - return stc_plugin->send_restriction_message_to_net_popup(content, type, - app_id, iftype, - limit); + return stc_plugin->send_restriction_message_to_net_popup(content, + type, app_id, iftype, limit); } -int stc_manager_plugin_register_state_changed_cb(stc_s *stc, - stc_plugin_app_state_changed_cb cb, - void *data) +int stc_plugin_appstatus_register_state_changed_cb(stc_s *stc, + stc_plugin_app_state_changed_cb cb, void *data) { __STC_LOG_FUNC_ENTER__; @@ -138,7 +129,7 @@ int stc_manager_plugin_register_state_changed_cb(stc_s *stc, return stc_plugin->register_state_changed_cb(stc, cb, data); } -int stc_manager_plugin_deregister_state_changed_cb(stc_s *stc) +int stc_plugin_appstatus_deregister_state_changed_cb(stc_s *stc) { __STC_LOG_FUNC_ENTER__; @@ -157,55 +148,4 @@ int stc_manager_plugin_deregister_state_changed_cb(stc_s *stc) __STC_LOG_FUNC_EXIT__; return stc_plugin->deregister_state_changed_cb(stc); } - -int stc_manager_plugin_fill_exception_list(void) -{ - __STC_LOG_FUNC_ENTER__; - - if (!stc_plugin_enabled) { - STC_LOGE("Plugin wasn't enabled"); - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_UNINITIALIZED; - } - - if (!stc_plugin) { - STC_LOGE("Plugin wasn't loaded"); - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_UNINITIALIZED; - } - - __STC_LOG_FUNC_EXIT__; - return stc_plugin->fill_exception_list(); -} - -int stc_manager_plugin_update_exception_list(void) -{ - __STC_LOG_FUNC_ENTER__; - - if (!stc_plugin_enabled) { - STC_LOGE("Plugin wasn't enabled"); - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_UNINITIALIZED; - } - - if (!stc_plugin) { - STC_LOGE("Plugin wasn't loaded"); - __STC_LOG_FUNC_EXIT__; - return STC_ERROR_UNINITIALIZED; - } - - __STC_LOG_FUNC_EXIT__; - return stc_plugin->update_exception_list(); -} - -int stc_manager_plugin_check_exception_by_cmdline(char *cmdline) -{ - if (!stc_plugin_enabled) - return STC_ERROR_UNINITIALIZED; - - if (!stc_plugin) - return STC_ERROR_UNINITIALIZED; - - return stc_plugin->check_exception_by_cmdline(cmdline); -} //LCOV_EXCL_STOP diff --git a/src/stc-manager-plugin-exception.c b/src/stc-manager-plugin-exception.c new file mode 100755 index 0000000..bc0078d --- /dev/null +++ b/src/stc-manager-plugin-exception.c @@ -0,0 +1,120 @@ +/* + * 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-manager-plugin-exception.h" + +static gboolean stc_plugin_enabled = FALSE; +static void *handle_plugin; +static stc_plugin_exception_s *stc_plugin; + +//LCOV_EXCL_START +int stc_plugin_exception_init(void) +{ + __STC_LOG_FUNC_ENTER__; + + handle_plugin = dlopen(STC_PLUGIN_EXCEPTION_FILEPATH, RTLD_NOW); + if (!handle_plugin) { + STC_LOGE("Can't load %s: %s", STC_PLUGIN_EXCEPTION_FILEPATH, dlerror()); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + stc_plugin = dlsym(handle_plugin, "stc_plugin_exception"); + if (!stc_plugin) { + STC_LOGE("Can't load symbol: %s", dlerror()); + dlclose(handle_plugin); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + stc_plugin->initialize_plugin(); + stc_plugin_enabled = TRUE; + + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_NONE; +} + +int stc_plugin_exception_deinit(void) +{ + __STC_LOG_FUNC_ENTER__; + + if (!stc_plugin_enabled) { + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + stc_plugin->deinitialize_plugin(); + stc_plugin_enabled = FALSE; + dlclose(handle_plugin); + + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_NONE; +} + +int stc_plugin_fill_exception_list(void) +{ + __STC_LOG_FUNC_ENTER__; + + if (!stc_plugin_enabled) { + STC_LOGE("Plugin wasn't enabled"); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + if (!stc_plugin) { + STC_LOGE("Plugin wasn't loaded"); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + __STC_LOG_FUNC_EXIT__; + return stc_plugin->fill_exception_list(); +} + +int stc_plugin_update_exception_list(void) +{ + __STC_LOG_FUNC_ENTER__; + + if (!stc_plugin_enabled) { + STC_LOGE("Plugin wasn't enabled"); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + if (!stc_plugin) { + STC_LOGE("Plugin wasn't loaded"); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + __STC_LOG_FUNC_EXIT__; + return stc_plugin->update_exception_list(); +} + +int stc_plugin_check_exception_by_cmdline(char *cmdline) +{ + if (!stc_plugin_enabled) + return STC_ERROR_UNINITIALIZED; + + if (!stc_plugin) + return STC_ERROR_UNINITIALIZED; + + return stc_plugin->check_exception_by_cmdline(cmdline); +} +//LCOV_EXCL_STOP diff --git a/src/stc-manager-plugin-procfs.c b/src/stc-manager-plugin-procfs.c new file mode 100755 index 0000000..b83a7e8 --- /dev/null +++ b/src/stc-manager-plugin-procfs.c @@ -0,0 +1,81 @@ +/* + * 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-manager-plugin-procfs.h" + +static gboolean stc_plugin_enabled = FALSE; +static void *handle_plugin; +static stc_plugin_procfs_s *stc_plugin; + +//LCOV_EXCL_START +int stc_plugin_procfs_init(void) +{ + __STC_LOG_FUNC_ENTER__; + + handle_plugin = dlopen(STC_PLUGIN_PROCFS_FILEPATH, RTLD_NOW); + if (!handle_plugin) { + STC_LOGE("Can't load %s: %s", STC_PLUGIN_PROCFS_FILEPATH, dlerror()); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + stc_plugin = dlsym(handle_plugin, "stc_plugin_procfs"); + if (!stc_plugin) { + STC_LOGE("Can't load symbol: %s", dlerror()); + dlclose(handle_plugin); + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + stc_plugin->initialize_plugin(); + stc_plugin_enabled = TRUE; + + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_NONE; +} + +int stc_plugin_procfs_deinit(void) +{ + __STC_LOG_FUNC_ENTER__; + + if (!stc_plugin_enabled) { + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_UNINITIALIZED; + } + + stc_plugin->deinitialize_plugin(); + stc_plugin_enabled = FALSE; + dlclose(handle_plugin); + + __STC_LOG_FUNC_EXIT__; + return STC_ERROR_NONE; +} + +stc_error_e stc_plugin_procfs_app_status_changed(stc_cmd_type_e cmd, + pid_t pid, const gchar *app_id, const gchar *pkg_id, stc_app_type_e app_type) +{ + if (!stc_plugin_enabled) + return STC_ERROR_UNINITIALIZED; + + if (!stc_plugin) + return STC_ERROR_UNINITIALIZED; + + return stc_plugin->procfs_status_changed(cmd, pid, app_id, pkg_id, app_type); +} +//LCOV_EXCL_STOP diff --git a/src/stc-manager.c b/src/stc-manager.c index b815400..8317d4f 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -25,8 +25,9 @@ #include "helper-nfacct-rule.h" #include "helper-iptables.h" #include "stc-monitor.h" -#include "stc-manager-plugin.h" -#include "stc-app-lifecycle.h" +#include "stc-manager-plugin-appstatus.h" +#include "stc-manager-plugin-exception.h" +#include "stc-manager-plugin-procfs.h" static stc_s *g_stc = NULL; @@ -61,9 +62,12 @@ static void __stc_manager_deinit(void) iptables_flush_chains(); iptables_deinit(); + stc_manager_gdbus_deinit((gpointer)g_stc); - stc_app_lifecycle_monitor_deinit(); - stc_manager_plugin_deinit(); + + stc_plugin_appstatus_deinit(); + stc_plugin_exception_deinit(); + stc_plugin_procfs_deinit(); STC_LOGI("stc manager deinitialized"); FREE(g_stc); @@ -88,13 +92,15 @@ static stc_s *__stc_manager_init(void) cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT); EXEC(STC_ERROR_NONE, stc_db_initialize()); - stc_manager_plugin_init(); + + stc_plugin_appstatus_init(); + stc_plugin_exception_init(); + stc_plugin_procfs_init(); err = stc_monitor_init(); if (err != STC_ERROR_NONE) goto handle_error; - stc_app_lifecycle_monitor_init(); stc_manager_gdbus_init((gpointer)stc); STC_LOGI("stc manager initialized"); -- 2.7.4 From 1a06acb1cd107bbb4e36163fd8164d1e6907ccd0 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 8 Feb 2018 16:29:17 +0900 Subject: [PATCH 15/16] Modified ip type to be properly monitored Change-Id: Ib08671b5c73a61418c6ad1643799f01efe97916b Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- src/helper/helper-iptables.c | 54 +++++++++++++++++++++++++---------------- src/helper/helper-iptables.h | 12 +++++++-- src/helper/helper-nfacct-rule.c | 6 +++-- src/helper/helper-nfacct-rule.h | 1 + 5 files changed, 49 insertions(+), 26 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 40a18d8..98a493a 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.49 +Version: 0.0.50 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-iptables.c b/src/helper/helper-iptables.c index 06b9378..81cab4a 100755 --- a/src/helper/helper-iptables.c +++ b/src/helper/helper-iptables.c @@ -96,7 +96,7 @@ static int __iptables_rule_add(GDBusConnection *connection, } g_variant_get(message, "(i)", &result); - STC_LOGD("Successfully Add Rule [%d]", result); + STC_LOGD("Successfully Add Rule [%d:%s]", result, rule->nfacct_name); g_variant_unref(message); return STC_ERROR_NONE; @@ -128,7 +128,7 @@ static int __iptables_rule_remove(GDBusConnection *connection, } g_variant_get(message, "(i)", &result); - STC_LOGD("Successfully Remove Rule [%d]", result); + STC_LOGD("Successfully Remove Rule [%d:%s]", result, rule->nfacct_name); g_variant_unref(message); return STC_ERROR_NONE; @@ -160,7 +160,7 @@ static int __ip6tables_rule_add(GDBusConnection *connection, } g_variant_get(message, "(i)", &result); - STC_LOGD("Successfully Add 6 Rule [%d]", result); + STC_LOGD("Successfully Add 6 Rule [%d:%s]", result, rule->nfacct_name); g_variant_unref(message); return STC_ERROR_NONE; @@ -192,7 +192,7 @@ static int __ip6tables_rule_remove(GDBusConnection *connection, } g_variant_get(message, "(i)", &result); - STC_LOGD("Successfully Remove 6 Rule [%d]", result); + STC_LOGD("Successfully Remove 6 Rule [%d:%s]", result, rule->nfacct_name); g_variant_unref(message); return STC_ERROR_NONE; @@ -217,7 +217,7 @@ static int __iptables_add_chain(GDBusConnection *connection, } g_variant_get(message, "(i)", &result); - STC_LOGD("Successfully added ipv4 chain [%d]", result); + STC_LOGD("Successfully added ipv4 chain [%d:%s]", result, chain); g_variant_unref(message); return STC_ERROR_NONE; @@ -242,7 +242,7 @@ static int __ip6tables_add_chain(GDBusConnection *connection, } g_variant_get(message, "(i)", &result); - STC_LOGD("Successfully added ipv6 chain [%d]", result); + STC_LOGD("Successfully added ipv6 chain [%d:%s]", result, chain); g_variant_unref(message); return STC_ERROR_NONE; @@ -267,7 +267,7 @@ static int __iptables_remove_chain(GDBusConnection *connection, } g_variant_get(message, "(i)", &result); - STC_LOGD("Successfully removed ipv4 chain [%d]", result); + STC_LOGD("Successfully removed ipv4 chain [%d:%s]", result, chain); g_variant_unref(message); return STC_ERROR_NONE; @@ -292,7 +292,7 @@ static int __ip6tables_remove_chain(GDBusConnection *connection, } g_variant_get(message, "(i)", &result); - STC_LOGD("Successfully removed ipv6 chain [%d]", result); + STC_LOGD("Successfully removed ipv6 chain [%d:%s]", result, chain); g_variant_unref(message); return STC_ERROR_NONE; @@ -317,7 +317,7 @@ static int __iptables_flush_chain(GDBusConnection *connection, } g_variant_get(message, "(i)", &result); - STC_LOGD("Successfully flushed ipv4 chain [%d]", result); + STC_LOGD("Successfully flushed ipv4 chain [%d:%s]", result, chain); g_variant_unref(message); return STC_ERROR_NONE; @@ -342,7 +342,7 @@ static int __ip6tables_flush_chain(GDBusConnection *connection, } g_variant_get(message, "(i)", &result); - STC_LOGD("Successfully flushed ipv6 chain [%d]", result); + STC_LOGD("Successfully flushed ipv6 chain [%d:%s]", result, chain); g_variant_unref(message); return STC_ERROR_NONE; @@ -358,7 +358,7 @@ static int __iptables_add_chain_jump_rule(const char *chain, iptables_rule.target = g_strdup(target); iptables_rule.chain = g_strdup(chain); - ret = iptables_add(&iptables_rule); + ret = iptables_add(&iptables_rule, IP_TYPE_IPV4_IPV6); g_free(iptables_rule.target); g_free(iptables_rule.chain); @@ -366,7 +366,7 @@ static int __iptables_add_chain_jump_rule(const char *chain, return ret; } -stc_error_e iptables_add(iptables_rule_s *rule) +stc_error_e iptables_add(iptables_rule_s *rule, iptables_ip_type_e iptype) { stc_error_e ret = STC_ERROR_NONE; stc_s *stc = stc_get_manager(); @@ -374,16 +374,22 @@ stc_error_e iptables_add(iptables_rule_s *rule) if (!stc || !stc->connection) return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE - ret = __iptables_rule_add(stc->connection, rule); - if (ret != STC_ERROR_NONE) - goto done; //LCOV_EXCL_LINE + if (iptype == IP_TYPE_IPV4 || + iptype == IP_TYPE_IPV4_IPV6) { + ret = __iptables_rule_add(stc->connection, rule); + if (ret != STC_ERROR_NONE) + goto done; //LCOV_EXCL_LINE + } + + if (iptype == IP_TYPE_IPV6 || + iptype == IP_TYPE_IPV4_IPV6) + ret = __ip6tables_rule_add(stc->connection, rule); - ret = __ip6tables_rule_add(stc->connection, rule); done: return ret; } -stc_error_e iptables_remove(iptables_rule_s *rule) +stc_error_e iptables_remove(iptables_rule_s *rule, iptables_ip_type_e iptype) { stc_error_e ret = STC_ERROR_NONE; stc_s *stc = stc_get_manager(); @@ -391,11 +397,17 @@ stc_error_e iptables_remove(iptables_rule_s *rule) if (!stc || !stc->connection) return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE - ret = __iptables_rule_remove(stc->connection, rule); - if (ret != STC_ERROR_NONE) - goto done; //LCOV_EXCL_LINE + if (iptype == IP_TYPE_IPV4 || + iptype == IP_TYPE_IPV4_IPV6) { + ret = __iptables_rule_remove(stc->connection, rule); + if (ret != STC_ERROR_NONE) + goto done; //LCOV_EXCL_LINE + } + + if (iptype == IP_TYPE_IPV6 || + iptype == IP_TYPE_IPV4_IPV6) + ret = __ip6tables_rule_remove(stc->connection, rule); - ret = __ip6tables_rule_remove(stc->connection, rule); done: return ret; } diff --git a/src/helper/helper-iptables.h b/src/helper/helper-iptables.h index bdfedb1..b73e29f 100755 --- a/src/helper/helper-iptables.h +++ b/src/helper/helper-iptables.h @@ -29,6 +29,14 @@ typedef enum { IPTABLES_DIRECTION_OUT } iptables_rule_direction_e; +typedef enum { + IP_TYPE_UNKNOWN, + IP_TYPE_IPV4, + IP_TYPE_IPV6, + IP_TYPE_IPV4_IPV6, + IP_TYPE_LAST_ELEM +} iptables_ip_type_e; + typedef struct { char *chain; char *ifname; @@ -38,8 +46,8 @@ typedef struct { uint32_t classid; } iptables_rule_s; -stc_error_e iptables_add(iptables_rule_s *rule); -stc_error_e iptables_remove(iptables_rule_s *rule); +stc_error_e iptables_add(iptables_rule_s *rule, iptables_ip_type_e iptype); +stc_error_e iptables_remove(iptables_rule_s *rule, iptables_ip_type_e iptype); stc_error_e iptables_flush_chains(void); stc_error_e iptables_init(void); stc_error_e iptables_deinit(void); diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index 8f5dfee..5944967 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -444,6 +444,7 @@ static char *choose_iftype_name(nfacct_rule_s *rule) static stc_error_e exec_iptables_cmd(nfacct_rule_s *rule) { stc_error_e ret = STC_ERROR_NONE; + iptables_ip_type_e iptype; iptables_rule_s iptables_rule; memset(&iptables_rule, 0, sizeof(iptables_rule_s)); @@ -453,13 +454,14 @@ static stc_error_e exec_iptables_cmd(nfacct_rule_s *rule) iptables_rule.chain = g_strdup(get_iptables_chain(rule->iotype)); iptables_rule.classid = rule->classid; iptables_rule.direction = (rule->iotype & NFACCT_COUNTER_IN) ? 0 : 1; + iptype = (iptables_ip_type_e)rule->iptype; if (rule->action == NFACCT_ACTION_DELETE) { /* delete interface rule */ - ret = iptables_remove(&iptables_rule); + ret = iptables_remove(&iptables_rule, iptype); } else { /* add interface rule */ - ret = iptables_add(&iptables_rule); + ret = iptables_add(&iptables_rule, iptype); } g_free(iptables_rule.nfacct_name); diff --git a/src/helper/helper-nfacct-rule.h b/src/helper/helper-nfacct-rule.h index 88b33ad..472cf29 100755 --- a/src/helper/helper-nfacct-rule.h +++ b/src/helper/helper-nfacct-rule.h @@ -62,6 +62,7 @@ typedef enum { NFACCT_TYPE_UNKNOWN, NFACCT_TYPE_IPV4, NFACCT_TYPE_IPV6, + NFACCT_TYPE_IPV4_IPV6, NFACCT_TYPE_LAST_ELEM } nfacct_rule_iptype; -- 2.7.4 From 03072c2b78962135ef801df23a52e32bf91cf7d0 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 8 Feb 2018 18:24:24 +0900 Subject: [PATCH 16/16] Fixed IPv4 and IPv6 monitoring issue Change-Id: I3fa0c4c550e21cbd87b5ef0e3f57de2487cf92c5 Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- src/helper/helper-nfacct-rule.c | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 98a493a..a41f2c0 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.50 +Version: 0.0.51 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 5944967..87d2270 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -584,10 +584,9 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule) char *jump_cmd = get_iptables_jump(rule->jump); char nfacct_buf[sizeof(NFACCT_NAME_MOD) + 3*MAX_DEC_SIZE(int) + 4]; + uint32_t classid = rule->classid; stc_error_e ret; - rule->classid = 0; - if (rule->iotype & NFACCT_COUNTER_IN) { /* income part */ rule->iotype = NFACCT_COUNTER_IN; @@ -613,6 +612,9 @@ 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 enought buffer"); + classid = rule->classid; + rule->classid = 0; + ret = exec_iptables_cmd(rule); ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL, "Can't set conditional block for ingress" @@ -647,6 +649,8 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule) //LCOV_EXCL_STOP } + rule->classid = classid; + if (rule->iotype & NFACCT_COUNTER_OUT) { /* outcome part */ rule->iotype = NFACCT_COUNTER_OUT; @@ -672,6 +676,9 @@ 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"); + classid = rule->classid; + rule->classid = 0; + ret = exec_iptables_cmd(rule); ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL, "Can't set conditional block for " -- 2.7.4