From 9750c77626856b02614dee2dabb0be60c6e56067 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 6 Sep 2017 15:54:56 +0900 Subject: [PATCH 01/16] Added exception lists for monitoring Change-Id: I4f010db523cc8421a2cabcc62f868db976138597 Signed-off-by: hyunuktak --- data/exceptions | 31 +++++++++++++++++ data/traffic_db.sql | 1 - packaging/stc-manager.spec | 9 ++++- src/CMakeLists.txt | 4 +-- src/monitor/include/stc-exception.h | 32 ++++++++++++++++++ src/monitor/include/stc-monitor.h | 3 ++ src/monitor/stc-app-lifecycle.c | 14 ++++++-- src/monitor/stc-exception.c | 66 +++++++++++++++++++++++++++++++++++++ src/monitor/stc-monitor.c | 57 ++++++++++++++++++++++++++++++++ 9 files changed, 211 insertions(+), 6 deletions(-) create mode 100644 data/exceptions create mode 100755 src/monitor/include/stc-exception.h create mode 100755 src/monitor/stc-exception.c diff --git a/data/exceptions b/data/exceptions new file mode 100644 index 0000000..67ab00e --- /dev/null +++ b/data/exceptions @@ -0,0 +1,31 @@ +bash:inst +cd:inst +cp:inst +mv:inst +rm:inst +mkdir:inst +rmdir:inst +cat:inst +more:inst +touch:inst +find:inst +ls:inst +sed:inst +rpm:inst +sh:inst +sleep:inst +grep:inst +killall:inst +systemctl:inst +wlan.sh:script +wpa_supp.sh:script +dlogutil:sys +wifi-loader:sys +launchpad-loader:sys +wrt-loader:sys +dotnet-launcher:sys +volume:app +iptables:app +ip6tables:app +modprobe:inst +net-cls-release:app diff --git a/data/traffic_db.sql b/data/traffic_db.sql index fda62f8..3fae3f6 100644 --- a/data/traffic_db.sql +++ b/data/traffic_db.sql @@ -34,4 +34,3 @@ CREATE TABLE IF NOT EXISTS counters ( data_counter BIGINT, PRIMARY KEY (restriction_id) ); - diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index e08e1cb..d1befbe 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.30 +Version: 0.0.31 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 @@ -71,6 +71,10 @@ rm -rf %{buildroot} rm %{buildroot}/usr/share/traffic_db.sql %endif +#Exceptions file +mkdir -p %{buildroot}/%{_localstatedir}/lib/stc +cp data/exceptions %{buildroot}/%{_localstatedir}/lib/stc/exceptions + #Systemd service file mkdir -p %{buildroot}%{_libdir}/systemd/system/ cp resources/systemd/stc-manager.service %{buildroot}%{_libdir}/systemd/system/stc-manager.service @@ -91,6 +95,9 @@ cp resources/dbus/stc-manager.conf %{buildroot}%{_sysconfdir}/dbus-1/system.d/st %attr(644,root,root) %{_libdir}/systemd/system/stc-manager.service %attr(644,root,root) %{_libdir}/systemd/system/multi-user.target.wants/stc-manager.service +%attr(755,root,root) /%{_localstatedir}/lib/stc +%attr(600,root,root) /%{_localstatedir}/lib/stc/exceptions + #DBus DAC %attr(644,root,root) %{_sysconfdir}/dbus-1/system.d/* diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 736cec3..1ea3658 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -46,12 +46,12 @@ INCLUDE_DIRECTORIES(${LIMITATION_SOURCE_DIR}) INCLUDE_DIRECTORIES(${LIMITATION_SOURCE_DIR}/include) FILE(GLOB SOURCE_SRCS ${SOURCE_DIR}/*.c) -FILE(GLOB HELPER_SOURCE_SRCS ${HELPER_SOURCE_DIR}/*.c) +FILE(GLOB HELPER_SRCS ${HELPER_SOURCE_DIR}/*.c) FILE(GLOB MONITOR_SRCS ${MONITOR_SOURCE_DIR}/*.c) FILE(GLOB CONFIGURE_SRCS ${CONFIGURE_SOURCE_DIR}/*.c) FILE(GLOB LIMITATION_SRCS ${LIMITATION_SOURCE_DIR}/*.c) -SET(SRCS ${SRCS} ${SOURCE_SRCS} ${HELPER_SOURCE_SRCS} ${MONITOR_SRCS} ${CONFIGURE_SRCS} ${LIMITATION_SRCS}) +SET(SRCS ${SRCS} ${SOURCE_SRCS} ${HELPER_SRCS} ${MONITOR_SRCS} ${CONFIGURE_SRCS} ${LIMITATION_SRCS}) IF("${ENABLE_DATABASE}" STREQUAL "YES") FILE(GLOB DATABASE_SRCS ${DATABASE_SOURCE_DIR}/*.c) diff --git a/src/monitor/include/stc-exception.h b/src/monitor/include/stc-exception.h new file mode 100755 index 0000000..da951ae --- /dev/null +++ b/src/monitor/include/stc-exception.h @@ -0,0 +1,32 @@ +/* + * 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_EXCEPTION_H__ +#define __STC_EXCEPTION_H__ + +typedef struct { + char *process_name; + char *exe_type; +} table_exceptions_info; + +typedef stc_cb_ret_e +(*table_exceptions_info_cb)(const table_exceptions_info *info, + void *user_data); + +stc_error_e table_exceptions_foreach(table_exceptions_info_cb info_cb, + void *user_data); + +#endif /*__STC_EXCEPTION_H__ */ diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index 56d87e2..6601489 100755 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -100,6 +100,7 @@ typedef struct { gboolean rstns_tree_updated; GTree *apps; /**< monitored applications */ gboolean apps_tree_updated; + GHashTable *excns_hash; /**< exception hash table */ guint background_state; } stc_system_s; @@ -144,6 +145,8 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info); stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info); +stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline); + int stc_monitor_get_counter_socket(void); #endif /* __STC_MONITOR_H__ */ diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index 3192c28..829c989 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -128,6 +128,16 @@ static void __proc_tree_remove(const proc_key_s *key) g_tree_remove(proc_tree, key); } +static gboolean __check_excn(char *cmdline) +{ + stc_error_e ret = stc_monitor_check_excn_by_cmdline(cmdline); + + if (ret == STC_ERROR_NO_DATA) + return FALSE; + else + return TRUE; +} + static void __open_nl_connector_sock(void) { __STC_LOG_FUNC_ENTER__; @@ -312,8 +322,8 @@ static void __process_event_exec(int tid, int pid) if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && STC_ERROR_NONE == proc_get_status(pid, status, PROC_BUF_MAX)) { - if (!g_strcmp0(cmdline, "iptables") || !g_strcmp0(cmdline, "ip6tables") || - !g_strcmp0(cmdline, "modprobe") || !g_strcmp0(cmdline, "net-cls-release")) { + if (__check_excn(cmdline)) { + STC_LOGI("[%s] monitoring is excepted", cmdline); return; } diff --git a/src/monitor/stc-exception.c b/src/monitor/stc-exception.c new file mode 100755 index 0000000..87c3eeb --- /dev/null +++ b/src/monitor/stc-exception.c @@ -0,0 +1,66 @@ +/* + * 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 "stc-manager.h" +#include "stc-exception.h" + +#define EXCEPTION_BUF_MAX 64 +#define EXCEPTION_STORAGE "/var/lib/stc/exceptions" + +stc_error_e table_exceptions_foreach(const table_exceptions_info_cb exception_cb, + void *user_data) +{ + __STC_LOG_FUNC_ENTER__; + + stc_error_e error_code = STC_ERROR_NONE; + table_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); + + __STC_LOG_FUNC_EXIT__; + return error_code; +} diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 08f1065..8bcff31 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -26,6 +26,7 @@ #include "counter.h" #include "table-statistics.h" #include "table-counters.h" +#include "stc-exception.h" #include "stc-monitor.h" #include "stc-manager-plugin.h" @@ -1499,6 +1500,41 @@ static stc_error_e __process_update_background(void) return STC_ERROR_NONE; } +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); +} + +static stc_cb_ret_e __insert_exception_cb(const table_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; + + return ret; +} + +static void __fill_exceptions_list(void) +{ + table_exceptions_foreach(__insert_exception_cb, NULL); + __excn_hash_printall(); +} + stc_error_e stc_monitor_init(void) { stc_system_s *system = MALLOC0(stc_system_s, 1); @@ -1518,6 +1554,9 @@ 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)) { @@ -1557,6 +1596,7 @@ stc_error_e stc_monitor_init(void) __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE, (int *)&g_system->background_state); + __fill_exceptions_list(); __fill_restritions_list(); return STC_ERROR_NONE; @@ -1583,6 +1623,10 @@ 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; @@ -1852,6 +1896,19 @@ 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) +{ + 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; +} + int stc_monitor_get_counter_socket(void) { return g_system->contr_sock; -- 2.7.4 From 832d05fa4a9c9583cde9105623cc59d0e0fcc712 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Fri, 15 Sep 2017 11:04:23 +0900 Subject: [PATCH 02/16] Get process status from procfs Change-Id: I8c9916ce129ec35f0f93d4d3c35afc0e67f3b7f1 Signed-off-by: hyunuktak --- include/stc-manager-util.h | 8 ++++++++ src/helper/helper-procfs.c | 33 +++++++++++++++++++++++++++++++-- src/helper/helper-procfs.h | 2 +- src/monitor/stc-app-lifecycle.c | 16 ++++++++++------ 4 files changed, 50 insertions(+), 9 deletions(-) mode change 100644 => 100755 src/helper/helper-procfs.c mode change 100644 => 100755 src/helper/helper-procfs.h diff --git a/include/stc-manager-util.h b/include/stc-manager-util.h index d5ee410..32b1cde 100755 --- a/include/stc-manager-util.h +++ b/include/stc-manager-util.h @@ -321,6 +321,14 @@ 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_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 COMMA_DELIMETER "," diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c old mode 100644 new mode 100755 index 0974f2a..ca291ac --- a/src/helper/helper-procfs.c +++ b/src/helper/helper-procfs.c @@ -157,9 +157,38 @@ int proc_get_raw_cmdline(pid_t pid, char *buf, int len) return proc_get_data(path, buf, len); } -int proc_get_status(pid_t pid, char *buf, int len) +int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX]) { + unsigned int i; char path[PROC_BUF_MAX]; + char status_buf[PROC_BUF_MAX]; + FILE *fp; + snprintf(path, sizeof(path), "/proc/%d/status", pid); - return proc_get_data(path, buf, len); + fp = fopen(path, "r"); + if (fp == NULL) + return STC_ERROR_FAIL; + + for (i = 0; i < PROC_STATUS_CNT; ++i) { + char *token = NULL; + char *saveptr = NULL; + + if (fgets(status_buf, sizeof(status_buf), fp) == NULL) { + fclose(fp); + return STC_ERROR_FAIL; + } + + 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])); + } + } + } + fclose(fp); + + return STC_ERROR_NONE; } diff --git a/src/helper/helper-procfs.h b/src/helper/helper-procfs.h old mode 100644 new mode 100755 index 4f0fd02..55c6a03 --- a/src/helper/helper-procfs.h +++ b/src/helper/helper-procfs.h @@ -58,6 +58,6 @@ int proc_get_exepath(pid_t pid, char *buf, int len); * @desc get status from /proc/{pid}/status * @return negative value if error */ -int proc_get_status(pid_t pid, char *buf, int len); +int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX]); #endif /*__HELPER_PROCFS_H__*/ diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index 829c989..7f5e035 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -37,7 +37,7 @@ typedef struct { typedef struct { char cmdline[PROC_NAME_MAX]; - char status[PROC_BUF_MAX]; + char status[PROC_STATUS_CNT][PROC_BUF_MAX]; } proc_value_s; typedef struct __attribute__ ((aligned(NLMSG_ALIGNTO))) { @@ -317,16 +317,17 @@ stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, static void __process_event_exec(int tid, int pid) { char cmdline[PROC_NAME_MAX] = {0, }; - char status[PROC_BUF_MAX] = {0, }; + char status[PROC_STATUS_CNT][PROC_BUF_MAX]; if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && - STC_ERROR_NONE == proc_get_status(pid, status, PROC_BUF_MAX)) { + STC_ERROR_NONE == proc_get_status(pid, status)) { if (__check_excn(cmdline)) { STC_LOGI("[%s] monitoring is excepted", cmdline); return; } + unsigned int i; proc_key_s *key; proc_value_s *value; @@ -344,13 +345,16 @@ static void __process_event_exec(int tid, int pid) } key->pid = pid; - g_strlcpy(value->status, status, sizeof(value->status)); + for (i = 0; i < PROC_STATUS_CNT; ++i) + g_strlcpy(value->status[i], status[i], sizeof(value->status[i])); g_strlcpy(value->cmdline, cmdline, sizeof(value->cmdline)); __proc_tree_add(key, value); - STC_LOGD("EXEC:pid=%d,tgid=%d\t[%s]\t[%s]", pid, tid, status, - cmdline); + STC_LOGD("EXEC: pid[%d] tgid=[%d] cmdline[%s]", pid, tid, cmdline); + STC_LOGD("STATUS: name[%s] state[%s] tgid[%s] pid[%s] ppid[%s] tracerpid[%s]", + status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TGID], + status[PROC_STATUS_PID], status[PROC_STATUS_PPID], status[PROC_STATUS_TRACERPID]); } } -- 2.7.4 From 6ddbb97984e78a666ec56faacdc702c8776e7fb3 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Mon, 18 Sep 2017 18:29:20 +0900 Subject: [PATCH 03/16] Fixed a svace issue for 293218 Change-Id: I0dff5c0cf7f228243c090a1213aee280ddc6bb63 Signed-off-by: hyunuktak --- src/helper/helper-nfacct-rule.c | 48 ++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index 3c365ee..a2a598f 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -411,26 +411,44 @@ static char* get_cmd_pos(const char *cmd_buf) static bool is_rule_present(const char *cmd_buf) { - size_t buf_len; - char *exec_buf; - char *cmd_pos = get_cmd_pos(cmd_buf); bool ret = false; - if (!cmd_pos) - return false; + pid_t pid = fork(); - buf_len = strlen(cmd_buf) + 1; - exec_buf = (char *)malloc(buf_len); - if (!exec_buf) - return false; + if (pid == 0) { + gchar **args = NULL; + size_t buf_len; + char *exec_buf; + char *cmd_pos = get_cmd_pos(cmd_buf); + + if (!cmd_pos) + exit(1); + + buf_len = strlen(cmd_buf) + 1; + exec_buf = (char *)malloc(buf_len); + if (!exec_buf) + exit(1); + + strncpy(exec_buf, cmd_buf, buf_len); + strncpy(exec_buf + (cmd_pos - cmd_buf), IPTABLES_CHECK, + sizeof(IPTABLES_CHECK) - 1); + + STC_LOGD("check rule %s", exec_buf); - strncpy(exec_buf, cmd_buf, buf_len); - strncpy(exec_buf + (cmd_pos - cmd_buf), IPTABLES_CHECK, - sizeof(IPTABLES_CHECK) - 1); + args = g_strsplit_set(exec_buf, " ", -1); - STC_LOGD("check rule %s", exec_buf); + ret = execv(args[0], 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)); + } + + free(exec_buf); + g_strfreev(args); + exit(ret); + } - ret = system(exec_buf) == 0; - free(exec_buf); return ret; } -- 2.7.4 From 224a785c0b719023cf92da6d226af609bc246062 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 19 Sep 2017 16:44:25 +0900 Subject: [PATCH 04/16] Added application exception list it is checking internet privilege it has used pkginfo package it has a cycle as one day Change-Id: I4dd8f2d0a8eec709790ea6929f8233c84cbd43a8 Signed-off-by: hyunuktak --- data/exceptions | 7 +-- packaging/stc-manager.spec | 3 +- src/CMakeLists.txt | 1 + src/monitor/include/stc-app-lifecycle.h | 4 +- src/monitor/include/stc-exception.h | 14 ++++- src/monitor/include/stc-monitor.h | 4 ++ src/monitor/stc-app-lifecycle.c | 4 +- src/monitor/stc-exception.c | 102 +++++++++++++++++++++++++++++++- src/monitor/stc-monitor.c | 43 +++++++++++++- src/stc-manager-plugin.c | 9 +-- 10 files changed, 171 insertions(+), 20 deletions(-) diff --git a/data/exceptions b/data/exceptions index 67ab00e..0429338 100644 --- a/data/exceptions +++ b/data/exceptions @@ -24,8 +24,7 @@ wifi-loader:sys launchpad-loader:sys wrt-loader:sys dotnet-launcher:sys -volume:app -iptables:app -ip6tables:app +iptables:sys +ip6tables:sys modprobe:inst -net-cls-release:app +net-cls-release:sys diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index d1befbe..3ed74db 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.31 +Version: 0.0.32 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 @@ -20,6 +20,7 @@ BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(capi-system-info) +BuildRequires: pkgconfig(pkgmgr-info) %if %{?enable_database} == YES BuildRequires: pkgconfig(sqlite3) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1ea3658..295314b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,7 @@ SET(REQUIRES_LIST ${REQUIRES_LIST} dlog vconf capi-system-info + pkgmgr-info ) IF("${ENABLE_DATABASE}" STREQUAL "YES") diff --git a/src/monitor/include/stc-app-lifecycle.h b/src/monitor/include/stc-app-lifecycle.h index 3946c3e..cb5c39e 100755 --- a/src/monitor/include/stc-app-lifecycle.h +++ b/src/monitor/include/stc-app-lifecycle.h @@ -24,8 +24,8 @@ stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, pid_t pid, - gchar *app_id, - gchar *pkg_id, + 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); diff --git a/src/monitor/include/stc-exception.h b/src/monitor/include/stc-exception.h index da951ae..38a2fd4 100755 --- a/src/monitor/include/stc-exception.h +++ b/src/monitor/include/stc-exception.h @@ -17,16 +17,24 @@ #ifndef __STC_EXCEPTION_H__ #define __STC_EXCEPTION_H__ +#define EXE_TYPE_APPLICATION "app" +#define EXE_TYPE_INSTRUCTION "inst" +#define EXE_TYPE_SYSTEM "sys" +#define EXE_TYPE_SCRIPT "script" + typedef struct { char *process_name; char *exe_type; -} table_exceptions_info; +} stc_exceptions_info; typedef stc_cb_ret_e -(*table_exceptions_info_cb)(const table_exceptions_info *info, +(*stc_exceptions_info_cb)(const stc_exceptions_info *info, void *user_data); -stc_error_e table_exceptions_foreach(table_exceptions_info_cb info_cb, +stc_error_e table_exceptions_foreach(const stc_exceptions_info_cb exception_cb, + void *user_data); + +stc_error_e pkginfo_exceptions_foreach(const stc_exceptions_info_cb exception_cb, void *user_data); #endif /*__STC_EXCEPTION_H__ */ diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index 6601489..ffb64c0 100755 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -28,6 +28,9 @@ /* 1 seconds */ #define CONTR_TIMER_INTERVAL 1 +/* 1 day */ +#define EXCNS_TIMER_INTERVAL 86400 + /** * @brief key for processes tree */ @@ -101,6 +104,7 @@ typedef struct { 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 7f5e035..1787043 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -198,8 +198,8 @@ static void __reopen_nl_connector_sock(void) stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, pid_t pid, - gchar *app_id, - gchar *pkg_id, + const gchar *app_id, + const gchar *pkg_id, stc_app_type_e app_type) { __STC_LOG_FUNC_ENTER__; diff --git a/src/monitor/stc-exception.c b/src/monitor/stc-exception.c index 87c3eeb..30579fc 100755 --- a/src/monitor/stc-exception.c +++ b/src/monitor/stc-exception.c @@ -20,19 +20,25 @@ * @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" -stc_error_e table_exceptions_foreach(const table_exceptions_info_cb exception_cb, +#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; - table_exceptions_info data; + stc_exceptions_info data; FILE *fp = NULL; char buf[EXCEPTION_BUF_MAX] = {0, }; @@ -64,3 +70,95 @@ stc_error_e table_exceptions_foreach(const table_exceptions_info_cb exception_cb __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"); + } + + 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; +} + +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"); + 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); + + __STC_LOG_FUNC_EXIT__; + return err; +} diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 8bcff31..ed8af13 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -1516,7 +1516,24 @@ static void __excn_hash_printall(void) __excn_hash_foreach_print, NULL); } -static stc_cb_ret_e __insert_exception_cb(const table_exceptions_info *info, +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_system->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; @@ -1532,7 +1549,19 @@ static stc_cb_ret_e __insert_exception_cb(const table_exceptions_info *info, static void __fill_exceptions_list(void) { table_exceptions_foreach(__insert_exception_cb, NULL); - __excn_hash_printall(); + pkginfo_exceptions_foreach(__insert_exception_cb, NULL); + + /* __excn_hash_printall(); */ +} + +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_error_e stc_monitor_init(void) @@ -1599,6 +1628,10 @@ 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; } @@ -1615,6 +1648,12 @@ 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; diff --git a/src/stc-manager-plugin.c b/src/stc-manager-plugin.c index 9a8d96b..d1e6d30 100755 --- a/src/stc-manager-plugin.c +++ b/src/stc-manager-plugin.c @@ -117,8 +117,8 @@ int stc_register_state_changed_cb(stc_s *stc, __STC_LOG_FUNC_ENTER__; if (!stc_plugin_enabled) { - __STC_LOG_FUNC_EXIT__; STC_LOGE("Plugin wasn't enabled"); + __STC_LOG_FUNC_EXIT__; return 0; } @@ -128,6 +128,7 @@ int stc_register_state_changed_cb(stc_s *stc, return 0; } + __STC_LOG_FUNC_EXIT__; return stc_plugin->register_state_changed_cb(stc, cb, data); } int stc_deregister_state_changed_cb(stc_s *stc) @@ -135,17 +136,17 @@ int stc_deregister_state_changed_cb(stc_s *stc) __STC_LOG_FUNC_ENTER__; if (!stc_plugin_enabled) { - __STC_LOG_FUNC_EXIT__; STC_LOGE("Plugin wasn't enabled"); + __STC_LOG_FUNC_EXIT__; return 0; } if (!stc_plugin) { - __STC_LOG_FUNC_EXIT__; STC_LOGE("Plugin wasn't loaded"); + __STC_LOG_FUNC_EXIT__; return 0; } - __STC_LOG_FUNC_EXIT__; + __STC_LOG_FUNC_EXIT__; return stc_plugin->deregister_state_changed_cb(stc); } -- 2.7.4 From c1a66d6d01e44a4d64232748d0701174b035bb7c Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 20 Sep 2017 16:35:35 +0900 Subject: [PATCH 05/16] Replaced imsi to subscriber ID Change-Id: Ie137cb045ab279416b3d3711f0cd55a1fe4543bd Signed-off-by: hyunuktak --- data/traffic_db.sql | 4 +- include/stc-restriction.h | 2 +- packaging/stc-manager.spec | 3 +- src/CMakeLists.txt | 1 + src/database/include/stc-db.h | 4 +- src/database/include/table-restrictions.h | 8 ++-- src/database/include/table-statistics.h | 4 +- src/database/tables/table-restrictions.c | 70 ++++++++++++++-------------- src/database/tables/table-statistics.c | 68 +++++++++++++-------------- src/monitor/include/stc-default-connection.h | 3 +- src/monitor/include/stc-monitor.h | 2 +- src/monitor/stc-default-connection.c | 29 +++++++++--- src/monitor/stc-monitor.c | 30 ++++++------ src/stc-restriction.c | 16 +++---- src/stc-statistics.c | 12 ++--- 15 files changed, 139 insertions(+), 117 deletions(-) diff --git a/data/traffic_db.sql b/data/traffic_db.sql index 3fae3f6..dd83d65 100644 --- a/data/traffic_db.sql +++ b/data/traffic_db.sql @@ -11,7 +11,7 @@ CREATE TABLE IF NOT EXISTS statistics ( is_roaming INT, hw_net_protocol_type INT, ifname TEXT, - imsi TEXT, + subscriber_id TEXT, ground INT ); @@ -22,7 +22,7 @@ CREATE TABLE IF NOT EXISTS restrictions ( ifname TEXT, rst_state INT, roaming INT, - imsi TEXT NOT NULL, + subscriber_id TEXT NOT NULL, data_limit BIGINT, data_warn_limit BIGINT ); diff --git a/include/stc-restriction.h b/include/stc-restriction.h index acd4eab..e804e0f 100755 --- a/include/stc-restriction.h +++ b/include/stc-restriction.h @@ -35,7 +35,7 @@ typedef struct { int64_t rcv_warning_limit; stc_roaming_type_e roaming; char *ifname; - char *imsi; + char *subscriber_id; } stc_restriction_s; /***************************************************************************** diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 3ed74db..7979283 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.32 +Version: 0.0.33 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 @@ -21,6 +21,7 @@ BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(capi-system-info) BuildRequires: pkgconfig(pkgmgr-info) +BuildRequires: pkgconfig(openssl) %if %{?enable_database} == YES BuildRequires: pkgconfig(sqlite3) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 295314b..23fd943 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,7 @@ SET(REQUIRES_LIST ${REQUIRES_LIST} vconf capi-system-info pkgmgr-info + openssl ) IF("${ENABLE_DATABASE}" STREQUAL "YES") diff --git a/src/database/include/stc-db.h b/src/database/include/stc-db.h index 6f718c3..689cb1f 100755 --- a/src/database/include/stc-db.h +++ b/src/database/include/stc-db.h @@ -79,8 +79,8 @@ typedef struct { typedef struct { uint32_t classid; int iftype; - /* pointer to telephony's imsi */ - char *imsi; + /* pointer to telephony's subscriber_id */ + char *subscriber_id; char ifname[MAX_IFACE_LENGTH]; } stc_db_classid_iftype_key; diff --git a/src/database/include/table-restrictions.h b/src/database/include/table-restrictions.h index fe275e1..d972b87 100755 --- a/src/database/include/table-restrictions.h +++ b/src/database/include/table-restrictions.h @@ -20,7 +20,7 @@ typedef struct { char *app_id; char *ifname; - char *imsi; + char *subscriber_id; stc_iface_type_e iftype; stc_restriction_state_e rst_state; stc_roaming_type_e roaming; @@ -41,9 +41,9 @@ stc_error_e table_restrictions_per_app(const gchar *app_id, void *user_data); -stc_error_e table_restrictions_get_restriction_state_imsi(const char *app_id, +stc_error_e table_restrictions_get_restriction_state_subscriber_id(const char *app_id, stc_iface_type_e iftype, - const char *imsi_hash, + const char *subscriber_id, stc_restriction_state_e *state); stc_error_e table_restrictions_get_restriction_state(const char *app_id, @@ -54,7 +54,7 @@ stc_error_e table_restrictions_update(table_restrictions_info *info); stc_error_e table_restrictions_delete(const char *app_id, const stc_iface_type_e iftype, - const char *imsi); + const char *subscriber_id); stc_error_e table_restrictions_prepare(sqlite3 *db); diff --git a/src/database/include/table-statistics.h b/src/database/include/table-statistics.h index 62a5ef6..ec88f5a 100755 --- a/src/database/include/table-statistics.h +++ b/src/database/include/table-statistics.h @@ -20,7 +20,7 @@ typedef struct { char *app_id; char *ifname; - char *imsi; + char *subscriber_id; stc_iface_type_e iftype; stc_db_tm_interval_s *interval; stc_data_counter_s cnt; @@ -32,7 +32,7 @@ typedef struct { typedef struct { unsigned char version; char *app_id; - char *imsi; + char *subscriber_id; stc_iface_type_e iftype; stc_db_tm_interval_s *interval; stc_db_connection_period_type connection_state; diff --git a/src/database/tables/table-restrictions.c b/src/database/tables/table-restrictions.c index e1e7256..9324887 100755 --- a/src/database/tables/table-restrictions.c +++ b/src/database/tables/table-restrictions.c @@ -26,15 +26,15 @@ /* DELETE statements */ #define DELETE_RESTRICTIONS "DELETE FROM restrictions " \ - " WHERE binpath = ? AND iftype = ? AND imsi = ?" + " WHERE binpath = ? AND iftype = ? AND subscriber_id = ?" /* SELECT statements */ #define SELECT_RESTRICTIONS "SELECT binpath, data_limit, " \ - " iftype, rst_state, roaming, ifname, imsi, " \ + " iftype, rst_state, roaming, ifname, subscriber_id, " \ " data_warn_limit, restriction_id FROM restrictions" #define SELECT_RESTRICTIONS_PER_APP "SELECT binpath, data_limit, " \ - " iftype, rst_state, roaming, ifname, imsi, " \ + " iftype, rst_state, roaming, ifname, subscriber_id, " \ " data_warn_limit, restriction_id " \ " FROM restrictions INDEXED BY restrictions_index " \ " WHERE binpath = ?" @@ -43,24 +43,24 @@ " FROM restrictions INDEXED BY restrictions_index " \ " WHERE binpath = ? AND iftype = ?" -#define SELECT_RESTRICTION_STATE_IMSI "SELECT rst_state " \ +#define SELECT_RESTRICTION_STATE_SUBSCRIBER_ID "SELECT rst_state " \ " FROM restrictions INDEXED BY restrictions_index " \ - " WHERE binpath = ? AND iftype = ? AND imsi = ?" + " WHERE binpath = ? AND iftype = ? AND subscriber_id = ?" #define SELECT_RESTRICTION_ID "SELECT restriction_id FROM restrictions " \ - " WHERE binpath = ? AND iftype = ? AND imsi = ? AND " \ + " WHERE binpath = ? AND iftype = ? AND subscriber_id = ? AND " \ " rst_state = ? AND roaming = ? AND ifname = ?" /* UPDATE statement */ #define UPDATE_NET_RESTRICTIONS "UPDATE restrictions " \ " SET binpath = ?, data_limit = ?, iftype = ?, rst_state = ?, " \ - " roaming = ?, ifname = ?, imsi = ?, data_warn_limit = ? " \ + " roaming = ?, ifname = ?, subscriber_id = ?, data_warn_limit = ? " \ " WHERE restriction_id = ?" /* INSERT statement */ #define INSERT_NET_RESTRICTIONS "INSERT INTO restrictions " \ " (binpath, data_limit, iftype, rst_state, " \ - " roaming, ifname, imsi, data_warn_limit) " \ + " roaming, ifname, subscriber_id, data_warn_limit) " \ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)" static void __finalize_delete(void); @@ -129,7 +129,7 @@ static sqlite3_stmt *delete_restrictions; static sqlite3_stmt *select_restriction; static sqlite3_stmt *select_restriction_per_app; static sqlite3_stmt *select_restriction_state; -static sqlite3_stmt *select_restriction_state_imsi; +static sqlite3_stmt *select_restriction_state_subscriber_id; static sqlite3_stmt *select_restriction_id; /* REPLACE statements */ @@ -179,7 +179,7 @@ static int __prepare_select(sqlite3 *db) PREPARE_SELECT(select_restriction, SELECT_RESTRICTIONS); PREPARE_SELECT(select_restriction_per_app, SELECT_RESTRICTIONS_PER_APP); PREPARE_SELECT(select_restriction_state, SELECT_RESTRICTION_STATE); - PREPARE_SELECT(select_restriction_state_imsi, SELECT_RESTRICTION_STATE_IMSI); + PREPARE_SELECT(select_restriction_state_subscriber_id, SELECT_RESTRICTION_STATE_SUBSCRIBER_ID); PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID); initialized = 1; @@ -194,7 +194,7 @@ static void __finalize_select(void) FINALIZE(select_restriction); FINALIZE(select_restriction_per_app); FINALIZE(select_restriction_state); - FINALIZE(select_restriction_state_imsi); + FINALIZE(select_restriction_state_subscriber_id); FINALIZE(select_restriction_id); __STC_LOG_FUNC_EXIT__; @@ -289,7 +289,7 @@ stc_error_e table_restrictions_per_app(const gchar* app_id, (stc_restriction_state_e)sqlite3_column_int(stmt, 3); data.roaming = sqlite3_column_int(stmt, 4); data.ifname = (char *)sqlite3_column_text(stmt, 5); - data.imsi = (char *)sqlite3_column_text(stmt, 6); + data.subscriber_id = (char *)sqlite3_column_text(stmt, 6); data.data_warn_limit = sqlite3_column_int64(stmt, 7); data.restriction_id = sqlite3_column_int64(stmt, 8); @@ -337,7 +337,7 @@ stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restrict (stc_restriction_state_e)sqlite3_column_int(stmt, 3); data.roaming = sqlite3_column_int(stmt, 4); data.ifname = (char *)sqlite3_column_text(stmt, 5); - data.imsi = (char *)sqlite3_column_text(stmt, 6); + data.subscriber_id = (char *)sqlite3_column_text(stmt, 6); data.data_warn_limit = sqlite3_column_int64(stmt, 7); data.restriction_id = sqlite3_column_int64(stmt, 8); @@ -359,15 +359,15 @@ stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restrict return error_code; } -stc_error_e table_restrictions_get_restriction_state_imsi(const char *app_id, +stc_error_e table_restrictions_get_restriction_state_subscriber_id(const char *app_id, stc_iface_type_e iftype, - const char *imsi_hash, + const char *subscriber_id, stc_restriction_state_e *state) { __STC_LOG_FUNC_ENTER__; int error_code = STC_ERROR_NONE; int ret; - bool state_imsi = 0; + bool state_subscriber_id = 0; if (state == NULL) { STC_LOGE("Please provide valid argument!"); @@ -376,11 +376,11 @@ stc_error_e table_restrictions_get_restriction_state_imsi(const char *app_id, } *state = STC_RESTRICTION_UNKNOWN; - sqlite3_reset(select_restriction_state_imsi); + sqlite3_reset(select_restriction_state_subscriber_id); sqlite3_reset(select_restriction_state); - if (imsi_hash == NULL) { - state_imsi = 0; + if (subscriber_id == NULL) { + state_subscriber_id = 0; DB_ACTION(sqlite3_bind_text(select_restriction_state, 1, app_id ? app_id : "", -1, SQLITE_STATIC)); @@ -388,23 +388,23 @@ stc_error_e table_restrictions_get_restriction_state_imsi(const char *app_id, iftype)); ret = sqlite3_step(select_restriction_state); } else { - state_imsi = 1; - DB_ACTION(sqlite3_bind_text(select_restriction_state_imsi, 1, + state_subscriber_id = 1; + DB_ACTION(sqlite3_bind_text(select_restriction_state_subscriber_id, 1, app_id ? app_id : "", -1, SQLITE_STATIC)); - DB_ACTION(sqlite3_bind_int(select_restriction_state_imsi, 2, + DB_ACTION(sqlite3_bind_int(select_restriction_state_subscriber_id, 2, iftype)); - DB_ACTION(sqlite3_bind_text(select_restriction_state_imsi, 3, - imsi_hash, -1, SQLITE_STATIC)); - ret = sqlite3_step(select_restriction_state_imsi); + DB_ACTION(sqlite3_bind_text(select_restriction_state_subscriber_id, 3, + subscriber_id, -1, SQLITE_STATIC)); + ret = sqlite3_step(select_restriction_state_subscriber_id); } switch (ret) { case SQLITE_DONE: break; case SQLITE_ROW: - if (state_imsi) - *state = (stc_restriction_state_e)sqlite3_column_int(select_restriction_state_imsi, 0); + if (state_subscriber_id) + *state = (stc_restriction_state_e)sqlite3_column_int(select_restriction_state_subscriber_id, 0); else *state = (stc_restriction_state_e)sqlite3_column_int(select_restriction_state, 0); break; @@ -418,7 +418,7 @@ stc_error_e table_restrictions_get_restriction_state_imsi(const char *app_id, handle_error: sqlite3_reset(select_restriction_state); - sqlite3_reset(select_restriction_state_imsi); + sqlite3_reset(select_restriction_state_subscriber_id); return error_code; } @@ -428,24 +428,24 @@ stc_error_e table_restrictions_get_restriction_state(const char *app_id, { __STC_LOG_FUNC_ENTER__; __STC_LOG_FUNC_EXIT__; - return table_restrictions_get_restriction_state_imsi(app_id, iftype, + return table_restrictions_get_restriction_state_subscriber_id(app_id, iftype, NULL, state); } stc_error_e table_restrictions_delete(const char *app_id, const stc_iface_type_e iftype, - const char *imsi) + const char *subscriber_id) { stc_error_e error_code = STC_ERROR_NONE; sqlite3_stmt *stmt = delete_restrictions; - STC_LOGD("app_id [%s], iftype [%d], imsi [%s]", - app_id, iftype, imsi); + STC_LOGD("app_id [%s], iftype [%d], subscriber_id [%s]", + app_id, iftype, subscriber_id); DB_ACTION(sqlite3_bind_text(stmt, 1, app_id ? app_id : "", -1, SQLITE_TRANSIENT)); DB_ACTION(sqlite3_bind_int(stmt, 2, iftype)); - DB_ACTION(sqlite3_bind_text(stmt, 3, imsi ? imsi : "", -1, + DB_ACTION(sqlite3_bind_text(stmt, 3, subscriber_id ? subscriber_id : "", -1, SQLITE_TRANSIENT)); if (sqlite3_step(stmt) != SQLITE_DONE) { @@ -473,7 +473,7 @@ stc_error_e __get_restriction_id(table_restrictions_info *info) DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "", -1, SQLITE_TRANSIENT)); DB_ACTION(sqlite3_bind_int(stmt, 2, info->iftype)); - DB_ACTION(sqlite3_bind_text(stmt, 3, info->imsi ? info->imsi : "", + DB_ACTION(sqlite3_bind_text(stmt, 3, info->subscriber_id ? info->subscriber_id : "", -1, SQLITE_TRANSIENT)); DB_ACTION(sqlite3_bind_int(stmt, 4, info->rst_state)); DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming)); @@ -523,7 +523,7 @@ stc_error_e table_restrictions_update(table_restrictions_info *info) DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming)); DB_ACTION(sqlite3_bind_text(stmt, 6, info->ifname ? info->ifname : "", -1, SQLITE_TRANSIENT)); - DB_ACTION(sqlite3_bind_text(stmt, 7, info->imsi ? info->imsi : "", + DB_ACTION(sqlite3_bind_text(stmt, 7, info->subscriber_id ? info->subscriber_id : "", -1, SQLITE_TRANSIENT)); DB_ACTION(sqlite3_bind_int64(stmt, 8, info->data_warn_limit)); diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c index 1b6d288..c14fdb6 100755 --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -42,108 +42,108 @@ /* SELECT statements */ #define SELECT_FOR_PERIOD "select binpath, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, " \ - "sum(sent) as sent, imsi, ground, iftype, ifname from statistics " \ + "sum(sent) as sent, subscriber_id, ground, iftype, ifname from statistics " \ "where time_stamp between ? and ? " \ - "group by binpath, is_roaming, imsi order by received desc" + "group by binpath, is_roaming, subscriber_id order by received desc" #define SELECT_FOR_PERIOD_IFACE "select binpath, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, " \ - "sum(sent) as sent, imsi, ground, iftype, ifname from statistics " \ + "sum(sent) as sent, subscriber_id, ground, iftype, ifname from statistics " \ "where time_stamp between ? and ? " \ - "and iftype=? group by binpath, is_roaming, imsi order by received desc" + "and iftype=? group by binpath, is_roaming, subscriber_id order by received desc" #define SELECT_CHUNKS "select binpath, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, " \ - "sum(sent) as sent, imsi, ground, iftype, ifname, " \ + "sum(sent) as sent, subscriber_id, ground, iftype, ifname, " \ "time_stamp - time_stamp % ? as timestamp " \ "from statistics where time_stamp between ? and ? " \ - "group by binpath, timestamp, imsi order by timestamp" + "group by binpath, timestamp, subscriber_id order by timestamp" #define SELECT_CHUNKS_IFACE "select binpath, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, " \ - "sum(sent) as sent, imsi, ground, iftype, ifname, " \ + "sum(sent) as sent, subscriber_id, ground, iftype, ifname, " \ "time_stamp - time_stamp % ? as timestamp " \ "from statistics where time_stamp between ? and ? and iftype=?" \ - "group by binpath, timestamp, imsi order by timestamp" + "group by binpath, timestamp, subscriber_id order by timestamp" #define SELECT_APP_DETAILS "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground from statistics " \ + "ifname, subscriber_id, ground from statistics " \ "where time_stamp between ? and ? and binpath=? " \ - "group by binpath, iftype, ifname, imsi, hw_net_protocol_type, " \ + "group by binpath, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ "is_roaming " \ - "order by time_stamp, binpath, iftype, ifname, imsi, " \ + "order by time_stamp, binpath, iftype, ifname, subscriber_id, " \ "hw_net_protocol_type, is_roaming" #define SELECT_APP_DETAILS_IFACE "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground from statistics " \ + "ifname, subscriber_id, ground from statistics " \ "where time_stamp between ? and ? and binpath=? and iftype=?" \ - "group by hw_net_protocol_type, is_roaming, iftype, ifname, imsi " \ + "group by hw_net_protocol_type, is_roaming, iftype, ifname, subscriber_id " \ "order by time_stamp, hw_net_protocol_type, is_roaming, iftype, " \ - "ifname, imsi" + "ifname, subscriber_id" #define SELECT_CHUNKS_APP "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \ + "ifname, subscriber_id, ground, time_stamp - time_stamp % ? as time_stamp " \ "from statistics " \ "group by iftype, ifname, time_stamp, hw_net_protocol_type, is_roaming " \ "order by time_stamp, iftype, ifname, hw_net_protocol_type, is_roaming" #define SELECT_CHUNKS_APP_IFACE "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \ + "ifname, subscriber_id, ground, time_stamp - time_stamp % ? as time_stamp " \ "from statistics where time_stamp between ? and ? and binpath = ? " \ "and iftype = ? " \ "group by time_stamp, hw_net_protocol_type, is_roaming, " \ - "iftype, ifname, imsi " \ - "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ + "iftype, ifname, subscriber_id " \ + "order by time_stamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ "is_roaming" #define SELECT_TOTAL "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground from statistics " \ + "ifname, subscriber_id, ground from statistics " \ "where (time_stamp between ? and ?) " \ "and binpath NOT LIKE 'TOTAL_%' " \ - "group by iftype, ifname, imsi, hw_net_protocol_type, is_roaming " \ - "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ + "group by iftype, ifname, subscriber_id, hw_net_protocol_type, is_roaming " \ + "order by time_stamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ "is_roaming" #define SELECT_TOTAL_IFACE "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground from statistics " \ + "ifname, subscriber_id, ground from statistics " \ "where (time_stamp between ? and ?) and iftype=? " \ "and binpath NOT LIKE 'TOTAL_%' " \ "group by hw_net_protocol_type, is_roaming, " \ - "iftype, ifname, imsi " \ - "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ + "iftype, ifname, subscriber_id " \ + "order by time_stamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ "is_roaming" #define SELECT_CHUNKS_TOTAL "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \ + "ifname, subscriber_id, ground, time_stamp - time_stamp % ? as time_stamp " \ "from statistics where time_stamp between ? and ? " \ "and binpath NOT LIKE 'TOTAL_%' " \ - "group by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ + "group by time_stamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ "is_roaming " \ - "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \ + "order by time_stamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \ "is_roaming" #define SELECT_CHUNKS_TOTAL_IFACE "select iftype, hw_net_protocol_type, " \ "is_roaming, sum(received) as received, sum(sent) as sent, " \ - "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \ + "ifname, subscriber_id, ground, time_stamp - time_stamp % ? as time_stamp " \ "from statistics where time_stamp between ? and ? " \ "and iftype = ? " \ "and binpath NOT LIKE 'TOTAL_%' " \ - "group by time_stamp, hw_net_protocol_type, is_roaming, iftype, ifname, imsi " \ + "group by time_stamp, hw_net_protocol_type, is_roaming, iftype, ifname, subscriber_id " \ "order by time_stamp, hw_net_protocol_type, is_roaming, iftype, " \ - "ifname, imsi" + "ifname, subscriber_id" /* INSERT statement */ #define INSERT_VALUES "insert into statistics " \ "(binpath, received, sent, time_stamp, " \ "iftype, is_roaming, hw_net_protocol_type, " \ - "ifname, imsi, ground) " \ + "ifname, subscriber_id, ground) " \ "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" static void __finalize_delete(void); @@ -474,7 +474,7 @@ stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rul data.roaming = sqlite3_column_int(stmt, 2); data.cnt.in_bytes = sqlite3_column_int64(stmt, 3); data.cnt.out_bytes = sqlite3_column_int64(stmt, 4); - data.imsi = (char *)sqlite3_column_text(stmt, 5); + data.subscriber_id = (char *)sqlite3_column_text(stmt, 5); data.ground = sqlite3_column_int(stmt, 6); data.iftype = sqlite3_column_int(stmt, 7); data.ifname = (char *)sqlite3_column_text(stmt, 8); @@ -557,7 +557,7 @@ stc_error_e table_statistics_per_app(const char *app_id, data.cnt.in_bytes = sqlite3_column_int64(stmt, 3); data.cnt.out_bytes = sqlite3_column_int64(stmt, 4); data.ifname = (char *)sqlite3_column_text(stmt, 5); - data.imsi = (char *)sqlite3_column_text(stmt, 6); + data.subscriber_id = (char *)sqlite3_column_text(stmt, 6); data.ground = sqlite3_column_int(stmt, 7); if (rule->granularity) { @@ -611,7 +611,7 @@ stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key, DB_ACTION(sqlite3_bind_text(stmt, 8, stat_key->ifname, -1, SQLITE_STATIC)); DB_ACTION(sqlite3_bind_text(stmt, 9, - stat_key->imsi ? stat_key->imsi : "" , -1, + stat_key->subscriber_id ? stat_key->subscriber_id : "" , -1, SQLITE_STATIC)); DB_ACTION(sqlite3_bind_int(stmt, 10, (int)stat->ground)); diff --git a/src/monitor/include/stc-default-connection.h b/src/monitor/include/stc-default-connection.h index b3326de..230671b 100755 --- a/src/monitor/include/stc-default-connection.h +++ b/src/monitor/include/stc-default-connection.h @@ -23,6 +23,7 @@ #include "stc-manager-util.h" #define IMSI_LENGTH 16 +#define SHA256_DIGEST_LENGTH 32 /** * @brief default connection information will be fetched from net-config @@ -39,7 +40,7 @@ typedef struct { gboolean roaming; /* only present when default profile is cellular */ - char imsi[IMSI_LENGTH]; + char subscriber_id[SHA256_DIGEST_LENGTH * 2 + 1]; /* hardware network protocol type */ stc_hw_net_protocol_type_e hw_net_protocol_type; diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index ffb64c0..74a05b4 100755 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -71,7 +71,7 @@ typedef struct { typedef struct { gchar *app_id; gchar *ifname; - gchar *imsi; + gchar *subscriber_id; stc_iface_type_e iftype; stc_roaming_type_e roaming; } stc_rstn_key_s; diff --git a/src/monitor/stc-default-connection.c b/src/monitor/stc-default-connection.c index e524970..aa8e1e6 100755 --- a/src/monitor/stc-default-connection.c +++ b/src/monitor/stc-default-connection.c @@ -15,6 +15,7 @@ */ #include +#include #include "stc-monitor.h" #include "stc-manager-gdbus.h" @@ -76,11 +77,26 @@ static int __telephony_get_current_sim(void) return current_sim; } -static void __telephony_get_modem_imsi(GDBusConnection *connection, +static void __make_imsi_to_subscriber_id(char *imsi) +{ + int i = 0; + SHA256_CTX ctx; + unsigned char md[SHA256_DIGEST_LENGTH]; + + SHA256_Init(&ctx); + SHA256_Update(&ctx, imsi, strlen(imsi)); + SHA256_Final(md, &ctx); + + for (i = 0; i < SHA256_DIGEST_LENGTH; ++i) + snprintf(g_default_connection.subscriber_id + (i * 2), 3, "%02x", md[i]); +} + +static void __telephony_get_modem_subscriber_id(GDBusConnection *connection, const char *default_modem_name) { GVariant *message = NULL; char tel_path[MAX_PATH_LENGTH]; + char imsi[IMSI_LENGTH]; const char *plmn = NULL; int plmn_len = 0; const char *msin = NULL; @@ -110,14 +126,15 @@ static void __telephony_get_modem_imsi(GDBusConnection *connection, goto done; } - snprintf(g_default_connection.imsi, IMSI_LENGTH, "%s%s", plmn, msin); + snprintf(imsi, IMSI_LENGTH, "%s%s", plmn, msin); + __make_imsi_to_subscriber_id(imsi); done: g_variant_unref(message); return; } -static void __telephony_update_default_modem_imsi(GDBusConnection *connection) +static void __telephony_update_default_modem_subscriber_id(GDBusConnection *connection) { GVariant *message = NULL; GVariantIter *iter = NULL; @@ -153,7 +170,7 @@ static void __telephony_update_default_modem_imsi(GDBusConnection *connection) current_sim--; } - __telephony_get_modem_imsi(connection, default_modem_name); + __telephony_get_modem_subscriber_id(connection, default_modem_name); FREE(default_modem_name); g_variant_iter_free(iter); @@ -169,7 +186,7 @@ static void __print_default_connection_info(void) STC_LOGI("ifname [%s]", g_default_connection.ifname); STC_LOGI("roaming [%u]", g_default_connection.roaming ? TRUE : FALSE); if (g_default_connection.type == STC_IFACE_DATACALL) - STC_LOGI("imsi [%s]", g_default_connection.imsi); + STC_LOGI("sub_id [%s]", g_default_connection.subscriber_id); STC_LOGI("=================================================="); } @@ -378,7 +395,7 @@ static stc_error_e __get_default_profile(GDBusConnection *connection) if (__is_cellular_profile(g_default_connection.path)) { g_default_connection.type = STC_IFACE_DATACALL; - __telephony_update_default_modem_imsi(connection); + __telephony_update_default_modem_subscriber_id(connection); } else if (__is_wifi_profile(g_default_connection.path)) { g_default_connection.type = STC_IFACE_WIFI; } else if (__is_ethernet_profile(g_default_connection.path)) { diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index ed8af13..734620b 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -248,7 +248,7 @@ static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b, if (ret != 0) return ret; - ret = g_strcmp0(key_a->imsi, key_b->imsi); + ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id); if (ret != 0) return ret; @@ -272,7 +272,7 @@ static void __rstns_tree_key_free(gpointer data) FREE(key->app_id); FREE(key->ifname); - FREE(key->imsi); + FREE(key->subscriber_id); FREE(key); } @@ -456,14 +456,14 @@ static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value) "limit [ (%lld) bytes], " "warn_limit [ (%lld) bytes], " "counter [ (%lld) bytes], " - "roaming [%d], imsi [%s]", + "roaming [%d], subscriber_id [%s]", rstn_value->restriction_id, rstn_key->app_id, rstn_value->classid , rstn_key->ifname, rstn_key->iftype, rstn_value->rst_state, rstn_value->data_limit, rstn_value->data_warn_limit, rstn_value->data_counter, - rstn_key->roaming, rstn_key->imsi); + rstn_key->roaming, rstn_key->subscriber_id); } static void __process_restriction(enum traffic_restriction_type rst_type, @@ -880,9 +880,9 @@ static gboolean __update_app_statistics(gpointer key, gpointer value, stat_key.iftype = default_connection->type; if (STC_IFACE_DATACALL == stat_key.iftype) - stat_key.imsi = g_strdup(default_connection->imsi); + stat_key.subscriber_id = g_strdup(default_connection->subscriber_id); else - stat_key.imsi = g_strdup("noneimsi"); + stat_key.subscriber_id = g_strdup("none_subid"); g_strlcpy(stat_key.ifname, default_connection->ifname, MAX_IFACE_LENGTH); @@ -899,7 +899,7 @@ static gboolean __update_app_statistics(gpointer key, gpointer value, app_value->counter.in_bytes = 0; FREE(stat.app_id); - FREE(stat_key.imsi); + FREE(stat_key.subscriber_id); return FALSE; } @@ -1298,7 +1298,7 @@ static stc_error_e __rstn_tree_add(stc_rstn_key_s *key, rstn_key->app_id = g_strdup(key->app_id); rstn_key->ifname = g_strdup(key->ifname); - rstn_key->imsi = g_strdup(key->imsi); + rstn_key->subscriber_id = g_strdup(key->subscriber_id); rstn_key->iftype = key->iftype; rstn_key->roaming = key->roaming; @@ -1335,7 +1335,7 @@ static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info, key.app_id = g_strdup(info->app_id); key.ifname = g_strdup(info->ifname); - key.imsi = g_strdup(info->imsi); + key.subscriber_id = g_strdup(info->subscriber_id); key.iftype = info->iftype; key.roaming = info->roaming; @@ -1355,7 +1355,7 @@ static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info, FREE(key.app_id); FREE(key.ifname); - FREE(key.imsi); + FREE(key.subscriber_id); return ret; } @@ -1510,11 +1510,13 @@ static void __excn_hash_foreach_print(gpointer key, gpointer value, process_name, exe_type); } +#if 0 static void __excn_hash_printall(void) { g_hash_table_foreach(g_system->excns_hash, __excn_hash_foreach_print, NULL); } +#endif static gboolean __remove_exception_app(gpointer key, gpointer value, gpointer data) @@ -1880,7 +1882,7 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info) key.app_id = g_strdup(info->app_id); key.ifname = g_strdup(info->ifname); - key.imsi = g_strdup(info->imsi); + key.subscriber_id = g_strdup(info->subscriber_id); key.iftype = info->iftype; key.roaming = info->roaming; @@ -1905,7 +1907,7 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info) FREE(key.app_id); FREE(key.ifname); - FREE(key.imsi); + FREE(key.subscriber_id); return ret; } @@ -1916,7 +1918,7 @@ stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info) stc_rstn_key_s key = { .app_id = g_strdup(info->app_id), .ifname = g_strdup(info->ifname), - .imsi = g_strdup(info->imsi), + .subscriber_id = g_strdup(info->subscriber_id), .iftype = info->iftype, .roaming = info->roaming, }; @@ -1931,7 +1933,7 @@ stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info) FREE(key.app_id); FREE(key.ifname); - FREE(key.imsi); + FREE(key.subscriber_id); return ret; } diff --git a/src/stc-restriction.c b/src/stc-restriction.c index 8550e41..ab3d350 100755 --- a/src/stc-restriction.c +++ b/src/stc-restriction.c @@ -47,7 +47,7 @@ void __initialize_rstn_rule(table_restrictions_info *rule) rule->data_limit = 0; rule->data_warn_limit = 0; rule->roaming = STC_ROAMING_DISABLE; - rule->imsi = NULL; + rule->subscriber_id = NULL; } gboolean __validate_rstn_rule(table_restrictions_info *rule, @@ -76,7 +76,7 @@ gboolean __validate_rstn_rule(table_restrictions_info *rule, return FALSE; } - if (rule->imsi == NULL) { + if (rule->subscriber_id == NULL) { __STC_LOG_FUNC_EXIT__; return FALSE; } @@ -121,8 +121,8 @@ void __stc_restriction_app_info_builder_add(GVariantBuilder *builder, g_variant_builder_add(builder, "{sv}", "roaming", g_variant_new_uint16(info->roaming)); - g_variant_builder_add(builder, "{sv}", "imsi", - g_variant_new_string(info->imsi)); + g_variant_builder_add(builder, "{sv}", "subscriber_id", + g_variant_new_string(info->subscriber_id)); __STC_LOG_FUNC_EXIT__; } @@ -206,11 +206,11 @@ static void __stc_extract_restriction_rule(const char *key, GVariant *value, rule->roaming = g_variant_get_uint16(value); STC_LOGD("roaming: [%u]", rule->roaming); - } else if (!g_strcmp0(key, "imsi")) { + } else if (!g_strcmp0(key, "subscriber_id")) { guint str_length; const gchar *str = g_variant_get_string(value, &str_length); - rule->imsi = g_strdup(str); - STC_LOGD("imsi: [%s]", rule->imsi); + rule->subscriber_id = g_strdup(str); + STC_LOGD("subscriber_id: [%s]", rule->subscriber_id); } else { STC_LOGD("Unknown select rule"); @@ -322,7 +322,7 @@ gboolean handle_restriction_unset(StcRestriction *object, return TRUE; } - table_restrictions_delete(rule.app_id, rule.iftype, rule.imsi); + table_restrictions_delete(rule.app_id, rule.iftype, rule.subscriber_id); /* remove restriction rule from runtime structure */ stc_monitor_rstns_tree_remove(&rule); diff --git a/src/stc-statistics.c b/src/stc-statistics.c index b20e3bb..fd4544d 100755 --- a/src/stc-statistics.c +++ b/src/stc-statistics.c @@ -113,10 +113,10 @@ void __stc_extract_reset_rule(const char *key, GVariant *value, rule->app_id = g_variant_dup_string(value, &len); STC_LOGD("app_id: [%s]", rule->app_id); - } else if (!g_strcmp0(key, "imsi")) { + } else if (!g_strcmp0(key, "subscriber_id")) { gsize len = 0; - rule->imsi = g_variant_dup_string(value, &len); - STC_LOGD("imsi: [%s]", rule->imsi); + rule->subscriber_id = g_variant_dup_string(value, &len); + STC_LOGD("subscriber_id: [%s]", rule->subscriber_id); } else if (!g_strcmp0(key, "iftype")) { rule->iftype = g_variant_get_uint16(value); @@ -175,8 +175,8 @@ void __stc_statistics_app_info_builder_add(GVariantBuilder *builder, g_variant_builder_add(builder, "{sv}", "ifname", g_variant_new_string(info->ifname)); - g_variant_builder_add(builder, "{sv}", "imsi", - g_variant_new_string(info->imsi)); + g_variant_builder_add(builder, "{sv}", "subscriber_id", + g_variant_new_string(info->subscriber_id)); g_variant_builder_add(builder, "{sv}", "iftype", g_variant_new_uint16(info->iftype)); @@ -412,7 +412,7 @@ gboolean handle_statistics_reset(StcStatistics *object, handle_error: FREE(rule.app_id); - FREE(rule.imsi); + FREE(rule.subscriber_id); FREE(rule.interval); __STC_LOG_FUNC_EXIT__; return TRUE; -- 2.7.4 From f59a400b27ff4da933812d36da7c9a67e34ead10 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 21 Sep 2017 10:30:02 +0900 Subject: [PATCH 06/16] Changed cur classid path and initialize Change-Id: Ib7c6bd5ce363a8079750e66943d287e86224e5c6 Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- src/helper/helper-net-cls.c | 19 ++++++++++++++++++- src/helper/helper-net-cls.h | 5 +---- src/monitor/stc-monitor.c | 5 ++++- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 7979283..4a256b3 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.33 +Version: 0.0.34 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-net-cls.c b/src/helper/helper-net-cls.c index 0f2ae30..ef5e4b3 100755 --- a/src/helper/helper-net-cls.c +++ b/src/helper/helper-net-cls.c @@ -26,7 +26,7 @@ #include "helper-file.h" #include "helper-net-cls.h" -#define CUR_CLASSID_PATH "/tmp/cur_classid" +#define CUR_CLASSID_PATH "/var/lib/stc/cur_classid" #define CLASSID_FILE_NAME "net_cls.classid" typedef GArray task_classid_array; @@ -85,6 +85,23 @@ static uint32_t __get_classid_from_cgroup(const char *cgroup, return classid; } +stc_error_e init_current_classid(void) +{ + int ret = 0; + struct stat stat_buf; + + if (stat(STC_CGROUP_NETWORK, &stat_buf) != 0) { + 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; + } + } + + return STC_ERROR_NONE; +} + uint32_t get_classid_by_app_id(const char *app_id, int create) { int ret = 0; diff --git a/src/helper/helper-net-cls.h b/src/helper/helper-net-cls.h index 24cd993..d27a5b2 100755 --- a/src/helper/helper-net-cls.h +++ b/src/helper/helper-net-cls.h @@ -33,10 +33,7 @@ enum { typedef GArray int_array; -/** - * @desc Get appid from classid task table. At present it is package name. - */ -char *get_app_id_by_pid(const pid_t pid); +stc_error_e init_current_classid(void); /** * @desc take classid from net_cls cgroup by appid diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 734620b..fc7b7bb 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -1500,6 +1500,7 @@ static stc_error_e __process_update_background(void) return STC_ERROR_NONE; } +#if 0 static void __excn_hash_foreach_print(gpointer key, gpointer value, gpointer data) { @@ -1510,7 +1511,6 @@ static void __excn_hash_foreach_print(gpointer key, gpointer value, process_name, exe_type); } -#if 0 static void __excn_hash_printall(void) { g_hash_table_foreach(g_system->excns_hash, @@ -1573,6 +1573,9 @@ stc_error_e stc_monitor_init(void) ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!"); + /* initializing current classid */ + init_current_classid(); + /* initializing cgroups */ cgroup_init(); -- 2.7.4 From 773f22751fe2d65f358533a0ef203ab984dca872 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 21 Sep 2017 14:15:51 +0900 Subject: [PATCH 07/16] Set STC_DEBUG_LOG flag Change-Id: Ic4cfd5f7eb3fa1014db9486479d567e092486e1c Signed-off-by: hyunuktak --- include/stc-manager.h | 2 ++ packaging/stc-manager.spec | 2 +- src/helper/helper-cgroup.c | 6 ++++++ src/helper/helper-nfacct-rule.c | 14 ++++++++++---- src/monitor/stc-monitor.c | 8 ++++++++ 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/include/stc-manager.h b/include/stc-manager.h index 990db68..37e5d97 100755 --- a/include/stc-manager.h +++ b/include/stc-manager.h @@ -31,6 +31,8 @@ #define STC_TOTAL_IPV4 "TOTAL_IPV4" #define STC_TOTAL_IPV6 "TOTAL_IPV6" +#define STC_DEBUG_LOG 0 + typedef enum { STC_CANCEL = 0, /**< cancel */ STC_CONTINUE = 1, /**< continue */ diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 4a256b3..d0e6065 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.34 +Version: 0.0.35 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/helper/helper-cgroup.c b/src/helper/helper-cgroup.c index aa01d16..c636bc2 100755 --- a/src/helper/helper-cgroup.c +++ b/src/helper/helper-cgroup.c @@ -116,7 +116,9 @@ int cgroup_write_node_uint32(const char *cgroup_name, { char buf[MAX_PATH_LENGTH]; snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name); +#if STC_DEBUG_LOG STC_LOGD("cgroup_buf %s, value %d\n", buf, value); +#endif return fwrite_uint(buf, value); } @@ -125,7 +127,9 @@ int cgroup_write_node_str(const char *cgroup_name, { char buf[MAX_PATH_LENGTH]; snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name); +#if STC_DEBUG_LOG STC_LOGD("cgroup_buf %s, string %s\n", buf, string); +#endif return fwrite_str(buf, string); } @@ -136,7 +140,9 @@ int cgroup_read_node_uint32(const char *cgroup_name, int ret; snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name); ret = fread_uint(buf, value); +#if STC_DEBUG_LOG STC_LOGD("cgroup_buf %s, value %d\n", buf, *value); +#endif return ret; } diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index a2a598f..a8474a1 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -156,7 +156,9 @@ stc_error_e nfacct_send_del(nfacct_rule_s *counter) { struct genl req; +#if STC_DEBUG_LOG STC_LOGD("send remove request for %s", counter->name); +#endif prepare_netlink_msg(&req, NFNL_MSG_ACCT_DEL, NLM_F_ACK); add_string_attr(&req, counter->name, NFACCT_NAME); @@ -432,7 +434,9 @@ static bool is_rule_present(const char *cmd_buf) strncpy(exec_buf + (cmd_pos - cmd_buf), IPTABLES_CHECK, sizeof(IPTABLES_CHECK) - 1); +#if STC_DEBUG_LOG STC_LOGD("check rule %s", exec_buf); +#endif args = g_strsplit_set(exec_buf, " ", -1); @@ -468,8 +472,9 @@ stc_error_e exec_iptables_cmd(const char *cmd_buf, pid_t *cmd_pid) int ret; char *save_ptr = NULL; - STC_LOGD("executing iptables cmd %s in forked process", - cmd_buf); +#if STC_DEBUG_LOG + STC_LOGD("executing iptables cmd %s in forked process", cmd_buf); +#endif if (is_rule_present(cmd_buf)) { STC_LOGD("Rule %s already present", cmd_buf); @@ -518,8 +523,9 @@ stc_error_e exec_ip6tables_cmd(const char *cmd_buf, pid_t *cmd_pid) int ret; char *save_ptr = NULL; - STC_LOGD("executing ip6tables cmd %s in forked process", - cmd_buf); +#if STC_DEBUG_LOG + STC_LOGD("executing ip6tables cmd %s in forked process", cmd_buf); +#endif if (is_rule_present(cmd_buf)) { STC_LOGD("Rule %s already present", cmd_buf); diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index fc7b7bb..0e34821 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -292,6 +292,7 @@ static void __processes_tree_printall(GTree *processes) g_tree_foreach(processes, __processes_tree_foreach_print, NULL); } +#if STC_DEBUG_LOG static gboolean __apps_tree_foreach_print(gpointer key, gpointer value, gpointer data) { @@ -308,6 +309,7 @@ static gboolean __apps_tree_foreach_print(gpointer key, gpointer value, __processes_tree_printall(app_value->processes); return FALSE; } +#endif #if 0 static void __apps_tree_printall(void) @@ -323,7 +325,9 @@ static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value, stc_app_value_s *app_value = (stc_app_value_s *)value; if (!g_tree_remove(app_value->processes, context->proc_key)) { +#if STC_DEBUG_LOG STC_LOGD("key not found"); +#endif return FALSE; } @@ -964,14 +968,18 @@ static void __app_counter_update(stc_app_key_s *app_key, app_value->counter.in_bytes = context->bytes; g_system->apps_tree_updated = TRUE; +#if STC_DEBUG_LOG __apps_tree_foreach_print(app_key, app_value, NULL); +#endif break; case NFACCT_COUNTER_OUT: app_value->data_usage.out_bytes += context->bytes; app_value->counter.out_bytes = context->bytes; g_system->apps_tree_updated = TRUE; +#if STC_DEBUG_LOG __apps_tree_foreach_print(app_key, app_value, NULL); +#endif break; default: STC_LOGE("unknown iotype"); -- 2.7.4 From 5d21fab5a8447ff76098bdad41f3a133b6359994 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 10 Oct 2017 14:04:47 +0900 Subject: [PATCH 08/16] Removed is_rule_present which is not valid Change-Id: I783916db78b09d9351f2d322be9eb0007d3dfb69 Signed-off-by: hyunuktak --- src/helper/helper-nfacct-rule.c | 64 ----------------------------------------- 1 file changed, 64 deletions(-) diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index a8474a1..aec0ef3 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -402,60 +402,6 @@ static void wait_for_rule_cmd(pid_t pid) } } -static char* get_cmd_pos(const char *cmd_buf) -{ - char *cmd_pos = strstr(cmd_buf, APPEND); - if (!cmd_pos) - cmd_pos = strstr(cmd_buf, INSERT); - - return cmd_pos; -} - -static bool is_rule_present(const char *cmd_buf) -{ - bool ret = false; - pid_t pid = fork(); - - if (pid == 0) { - gchar **args = NULL; - size_t buf_len; - char *exec_buf; - char *cmd_pos = get_cmd_pos(cmd_buf); - - if (!cmd_pos) - exit(1); - - buf_len = strlen(cmd_buf) + 1; - exec_buf = (char *)malloc(buf_len); - if (!exec_buf) - exit(1); - - strncpy(exec_buf, cmd_buf, buf_len); - strncpy(exec_buf + (cmd_pos - cmd_buf), IPTABLES_CHECK, - sizeof(IPTABLES_CHECK) - 1); - -#if STC_DEBUG_LOG - STC_LOGD("check rule %s", exec_buf); -#endif - - args = g_strsplit_set(exec_buf, " ", -1); - - ret = execv(args[0], 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)); - } - - free(exec_buf); - g_strfreev(args); - exit(ret); - } - - return ret; -} - stc_error_e exec_iptables_cmd(const char *cmd_buf, pid_t *cmd_pid) { const size_t args_number = get_args_number(cmd_buf); @@ -476,11 +422,6 @@ stc_error_e exec_iptables_cmd(const char *cmd_buf, pid_t *cmd_pid) STC_LOGD("executing iptables cmd %s in forked process", cmd_buf); #endif - if (is_rule_present(cmd_buf)) { - STC_LOGD("Rule %s already present", cmd_buf); - exit(0); - } - args[0] = "iptables"; cmd = strtok_r((char *)cmd_buf, " ", &save_ptr); if (cmd == NULL) { @@ -527,11 +468,6 @@ stc_error_e exec_ip6tables_cmd(const char *cmd_buf, pid_t *cmd_pid) STC_LOGD("executing ip6tables cmd %s in forked process", cmd_buf); #endif - if (is_rule_present(cmd_buf)) { - STC_LOGD("Rule %s already present", cmd_buf); - exit(0); - } - args[0] = "ip6tables"; cmd = strtok_r((char *)cmd_buf, " ", &save_ptr); if (cmd == NULL) { -- 2.7.4 From 3cec09f294756fa7dbd5145923bde1376f3b3062 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 10 Oct 2017 14:17:01 +0900 Subject: [PATCH 09/16] Monitoring threads of a process using tgid and PROC_EVENT_FORK Change-Id: I7502aa99b04e06e7e7cce0067b43c5f400d99f4d Signed-off-by: hyunuktak --- src/monitor/include/stc-monitor.h | 1 - src/monitor/stc-app-lifecycle.c | 102 ++++++++++++++++++++++++++++++-------- src/monitor/stc-monitor.c | 2 +- 3 files changed, 83 insertions(+), 22 deletions(-) diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index 74a05b4..0ecce65 100755 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -43,7 +43,6 @@ typedef struct { */ typedef struct { pid_t pid; /**< process id */ - GArray *childs; /**< child pids */ } stc_process_key_s; /** diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index 1787043..d43fe47 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -101,19 +101,46 @@ static proc_value_s * __proc_tree_lookup(const proc_key_s *key) return lookup; } +static proc_value_s * __proc_tree_find_parent(proc_value_s *value) +{ + proc_value_s *parent = NULL; + proc_value_s *lookup = value; + + do { + proc_key_s key; + key.pid = atoi(lookup->status[PROC_STATUS_PPID]); + lookup = __proc_tree_lookup(&key); + if (lookup != NULL) + parent = lookup; + } while (lookup); + + return parent; +} + static void __proc_tree_add(proc_key_s *key, proc_value_s *value) { + proc_value_s *lookup; + proc_value_s *parent; + if (proc_tree == NULL) { STC_LOGE("tree is null"); return; } + lookup = g_tree_lookup(proc_tree, key); + if (lookup) + return; + g_tree_insert(proc_tree, key, value); - stc_manager_app_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid, - value->cmdline, value->cmdline, - STC_APP_TYPE_SERVICE); + parent = __proc_tree_find_parent(value); + if (parent != NULL) + stc_manager_app_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, + value->cmdline, value->cmdline, STC_APP_TYPE_SERVICE); } static void __proc_tree_remove(const proc_key_s *key) @@ -314,7 +341,7 @@ stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, return ret; } -static void __process_event_exec(int tid, int pid) +static void __process_event_fork(int tgid, int pid) { char cmdline[PROC_NAME_MAX] = {0, }; char status[PROC_STATUS_CNT][PROC_BUF_MAX]; @@ -322,10 +349,8 @@ static void __process_event_exec(int tid, int pid) if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && STC_ERROR_NONE == proc_get_status(pid, status)) { - if (__check_excn(cmdline)) { - STC_LOGI("[%s] monitoring is excepted", cmdline); + if (__check_excn(cmdline)) return; - } unsigned int i; proc_key_s *key; @@ -344,33 +369,66 @@ static void __process_event_exec(int tid, int pid) return; } - key->pid = pid; + key->pid = tgid; for (i = 0; i < PROC_STATUS_CNT; ++i) g_strlcpy(value->status[i], status[i], sizeof(value->status[i])); g_strlcpy(value->cmdline, cmdline, sizeof(value->cmdline)); __proc_tree_add(key, value); + } +} - STC_LOGD("EXEC: pid[%d] tgid=[%d] cmdline[%s]", pid, tid, cmdline); - STC_LOGD("STATUS: name[%s] state[%s] tgid[%s] pid[%s] ppid[%s] tracerpid[%s]", - status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TGID], - status[PROC_STATUS_PID], status[PROC_STATUS_PPID], status[PROC_STATUS_TRACERPID]); +static void __process_event_exec(int tgid, int pid) +{ + char cmdline[PROC_NAME_MAX] = {0, }; + char status[PROC_STATUS_CNT][PROC_BUF_MAX]; + + if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && + STC_ERROR_NONE == proc_get_status(pid, status)) { + + if (__check_excn(cmdline)) + return; + + unsigned int i; + proc_key_s *key; + proc_value_s *value; + + key = MALLOC0(proc_key_s, 1); + if (key == NULL) { + STC_LOGE("memory allocation failed"); + return; + } + + value = MALLOC0(proc_value_s, 1); + if (value == NULL) { + STC_LOGE("memory allocation failed"); + FREE(key); + return; + } + + key->pid = tgid; + for (i = 0; i < PROC_STATUS_CNT; ++i) + g_strlcpy(value->status[i], status[i], sizeof(value->status[i])); + g_strlcpy(value->cmdline, cmdline, sizeof(value->cmdline)); + + __proc_tree_add(key, value); } } -static void __process_event_exit(int tid, int pid, int exit_code) +static void __process_event_exit(int tgid, int pid, int exit_code) { proc_key_s key; proc_value_s *lookup; - key.pid = pid; + if (tgid != pid) + return; + + key.pid = tgid; lookup = __proc_tree_lookup(&key); if (lookup == NULL) /* unmonitored process */ return; __proc_tree_remove(&key); - - STC_LOGD("EXIT:pid=%d,%d ruid=%d,euid=%d", pid, tid, exit_code); } static gboolean __process_nl_connector_message(GIOChannel *source, @@ -403,13 +461,17 @@ static gboolean __process_nl_connector_message(GIOChannel *source, } switch (msg.proc_ev.what) { + case PROC_EVENT_FORK: + __process_event_fork(msg.proc_ev.event_data.fork.child_tgid, + msg.proc_ev.event_data.fork.child_pid); + break; case PROC_EVENT_EXEC: - __process_event_exec(msg.proc_ev.event_data.exec.process_pid, - msg.proc_ev.event_data.exec.process_tgid); + __process_event_exec(msg.proc_ev.event_data.exec.process_tgid, + msg.proc_ev.event_data.exec.process_pid); break; case PROC_EVENT_EXIT: - __process_event_exit(msg.proc_ev.event_data.exit.process_pid, - msg.proc_ev.event_data.exit.process_tgid, + __process_event_exit(msg.proc_ev.event_data.exit.process_tgid, + msg.proc_ev.event_data.exit.process_pid, msg.proc_ev.event_data.exit.exit_code); break; default: diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 0e34821..9fdf529 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -276,6 +276,7 @@ static void __rstns_tree_key_free(gpointer data) FREE(key); } +#if STC_DEBUG_LOG static gboolean __processes_tree_foreach_print(gpointer key, gpointer value, gpointer data) { @@ -292,7 +293,6 @@ static void __processes_tree_printall(GTree *processes) g_tree_foreach(processes, __processes_tree_foreach_print, NULL); } -#if STC_DEBUG_LOG static gboolean __apps_tree_foreach_print(gpointer key, gpointer value, gpointer data) { -- 2.7.4 From 40a097ddd80e6523c7718ddfff5b53164c7edea0 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 10 Oct 2017 14:27:08 +0900 Subject: [PATCH 10/16] Added extra debug logs under STC_DEBUG_LOG tag Change-Id: I40b7a6eae7e2adbf919b6f0359b162d221fbc124 Signed-off-by: hyunuktak --- include/stc-manager-util.h | 14 +++++++ plugin/CMakeLists.txt | 2 + plugin/stc-plugin.c | 16 ++++---- src/monitor/stc-app-lifecycle.c | 88 ++++++++++++++++++++++++++++++++++++++--- src/monitor/stc-monitor.c | 16 +++++--- 5 files changed, 119 insertions(+), 17 deletions(-) diff --git a/include/stc-manager-util.h b/include/stc-manager-util.h index 32b1cde..708f67c 100755 --- a/include/stc-manager-util.h +++ b/include/stc-manager-util.h @@ -55,6 +55,20 @@ #endif /* USE_DLOG */ +#define LOG_RED "\033[0;31m" +#define LOG_GREEN "\033[0;32m" +#define LOG_BROWN "\033[0;33m" +#define LOG_BLUE "\033[0;34m" +#define LOG_MAGENTA "\033[0;35m" +#define LOG_CYAN "\033[0;36m" +#define LOG_YELLOW "\033[1;33m" +#define LOG_LIGHTRED "\033[1;31m" +#define LOG_LIGHTGREEN "\033[1;32m" +#define LOG_LIGHTBLUE "\033[1;34m" +#define LOG_LIGHTMAGENTA "\033[1;35m" +#define LOG_LIGHTCYAN "\033[1;36m" +#define LOG_END "\033[0;m" + #define APP_ID_LEN_MAX 1024 #define GPOINTER_TO_PID(x) (pid_t)GPOINTER_TO_INT((x)) diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt index 3e5dcda..afde5f4 100755 --- a/plugin/CMakeLists.txt +++ b/plugin/CMakeLists.txt @@ -24,6 +24,8 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -g -Werror -fvisibility=hidd SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") SET(CMAKE_C_FLAGS_RELEASE "-O2") +ADD_DEFINITIONS("-DUSE_DLOG") + SET(SRCS_PLUGIN stc-plugin.c ) diff --git a/plugin/stc-plugin.c b/plugin/stc-plugin.c index a566388..db83094 100755 --- a/plugin/stc-plugin.c +++ b/plugin/stc-plugin.c @@ -60,7 +60,6 @@ static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection, GVariant *parameters, gpointer user_data) { - __STC_LOG_FUNC_ENTER__; pid_t pid; stc_cmd_type_e status; stc_app_type_e apptype; @@ -69,7 +68,6 @@ static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection, 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"); - __STC_LOG_FUNC_EXIT__; return; } @@ -81,7 +79,6 @@ static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection, } else if (!strncmp(statstr, "bg", 2)) { status = STC_CMD_SET_BACKGRD; } else { - __STC_LOG_FUNC_EXIT__; goto out; } @@ -94,10 +91,15 @@ static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection, 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); +#endif + if (state_changed_cb) state_changed_cb(status, pid, appid, pkgid, apptype); - __STC_LOG_FUNC_EXIT__; out: FREE(appid); FREE(pkgid); @@ -175,8 +177,8 @@ static int __stc_send_warn_message_to_net_popup(const char *content, int ret = 0; bundle *b = bundle_create(); - STC_LOGD("Warn message : content[%s] type[%s] app_id[%s] limit[%s]", - title, content, type, app_id, limit); + 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); @@ -198,7 +200,7 @@ static int __stc_send_restriction_message_to_net_popup(const char *content, bundle *b = bundle_create(); STC_LOGD("Restriction message : content[%s] type[%s] app_id[%s] limit[%s]", - title, content, type, app_id); + content, type, app_id, limit); bundle_add(b, "_SYSPOPUP_CONTENT_", content); bundle_add(b, "_SYSPOPUP_TYPE_", type); diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index d43fe47..09fcef0 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -101,6 +101,26 @@ static proc_value_s * __proc_tree_lookup(const proc_key_s *key) return lookup; } +#if STC_DEBUG_LOG +static gboolean __proc_tree_foreach_print(gpointer key, gpointer value, + gpointer data) +{ + proc_key_s *proc_key = (proc_key_s *)key; + proc_value_s *proc_value = (proc_value_s *)value; + + STC_LOGD("Proc pid [\033[1;33m%d\033[0;m] ppid [\033[1;35m%s\033[0;m] " + "cmdline [\033[0;34m%s\033[0;m]", proc_key->pid, + proc_value->status[PROC_STATUS_PPID], proc_value->cmdline); + + return FALSE; +} + +static void __proc_tree_printall(void) +{ + g_tree_foreach(proc_tree, __proc_tree_foreach_print, NULL); +} +#endif + static proc_value_s * __proc_tree_find_parent(proc_value_s *value) { proc_value_s *parent = NULL; @@ -114,6 +134,15 @@ static proc_value_s * __proc_tree_find_parent(proc_value_s *value) parent = lookup; } 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] " + "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, + parent->status[PROC_STATUS_NAME]); +#endif + return parent; } @@ -129,11 +158,25 @@ static void __proc_tree_add(proc_key_s *key, } lookup = g_tree_lookup(proc_tree, key); - if (lookup) + 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] " + "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]); +#endif return; + } + + STC_LOGD("cmdline [%s] pid[%s] ppid[%s]", value->cmdline, + value->status[PROC_STATUS_PID], value->status[PROC_STATUS_PPID]); g_tree_insert(proc_tree, key, value); +#if STC_DEBUG_LOG + __proc_tree_printall(); +#endif + parent = __proc_tree_find_parent(value); if (parent != NULL) stc_manager_app_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid, @@ -153,6 +196,10 @@ static void __proc_tree_remove(const proc_key_s *key) stc_manager_app_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(); +#endif } static gboolean __check_excn(char *cmdline) @@ -229,9 +276,12 @@ stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, const gchar *pkg_id, stc_app_type_e app_type) { - __STC_LOG_FUNC_ENTER__; 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: { @@ -337,7 +387,6 @@ stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, ret = STC_ERROR_INVALID_PARAMETER; } - __STC_LOG_FUNC_EXIT__; return ret; } @@ -349,8 +398,12 @@ static void __process_event_fork(int tgid, int pid) if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && STC_ERROR_NONE == proc_get_status(pid, status)) { - if (__check_excn(cmdline)) + if (__check_excn(cmdline)) { +#if STC_DEBUG_LOG + STC_LOGD("[%s] monitoring is excepted", cmdline); +#endif return; + } unsigned int i; proc_key_s *key; @@ -374,6 +427,14 @@ static void __process_event_fork(int tgid, int pid) g_strlcpy(value->status[i], status[i], sizeof(value->status[i])); 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] " + "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]", + status[PROC_STATUS_TGID], status[PROC_STATUS_PID], status[PROC_STATUS_PPID], + status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]); +#endif + __proc_tree_add(key, value); } } @@ -386,8 +447,12 @@ static void __process_event_exec(int tgid, int pid) if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && STC_ERROR_NONE == proc_get_status(pid, status)) { - if (__check_excn(cmdline)) + if (__check_excn(cmdline)) { +#if STC_DEBUG_LOG + STC_LOGD("[%s] monitoring is excepted", cmdline); +#endif return; + } unsigned int i; proc_key_s *key; @@ -411,6 +476,14 @@ static void __process_event_exec(int tgid, int pid) g_strlcpy(value->status[i], status[i], sizeof(value->status[i])); 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] " + "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]", + status[PROC_STATUS_TGID], status[PROC_STATUS_PID], status[PROC_STATUS_PPID], + status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]); +#endif + __proc_tree_add(key, value); } } @@ -428,6 +501,11 @@ static void __process_event_exit(int tgid, int pid, int exit_code) if (lookup == NULL) /* unmonitored process */ return; +#if STC_DEBUG_LOG + 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); +#endif + __proc_tree_remove(&key); } diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 9fdf529..a50d65b 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -283,7 +283,7 @@ static gboolean __processes_tree_foreach_print(gpointer key, gpointer value, stc_process_key_s *proc_key = (stc_process_key_s *)key; stc_process_value_s *proc_value = (stc_process_value_s *)value; - STC_LOGD("Process entry => PID [%d], Ground state [%d]", + STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]", proc_key->pid, proc_value->ground); return FALSE; } @@ -299,8 +299,8 @@ static gboolean __apps_tree_foreach_print(gpointer key, gpointer value, stc_app_key_s *app_key = (stc_app_key_s *)key; stc_app_value_s *app_value = (stc_app_value_s *)value; - STC_LOGD("Application info => Pkg ID [%s], App ID [%s]," - " Type [%d], classid [%d]," + STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], " + "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d]," " counter [ in (%lld), out (%lld)]", app_key->pkg_id, app_key->app_id, app_value->type, app_value->classid, @@ -309,9 +309,7 @@ static gboolean __apps_tree_foreach_print(gpointer key, gpointer value, __processes_tree_printall(app_value->processes); return FALSE; } -#endif -#if 0 static void __apps_tree_printall(void) { g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL); @@ -1782,6 +1780,10 @@ stc_error_e stc_monitor_process_add(const stc_app_key_s app_key, /* add pid to application cgroup */ place_pids_to_net_cgroup(proc_key.pid, app_key.app_id); +#if STC_DEBUG_LOG + __apps_tree_printall(); +#endif + return ret; } @@ -1806,6 +1808,10 @@ stc_error_e stc_monitor_process_remove(pid_t pid) if (context.entry_removed) __application_remove_if_empty(context.app_key); +#if STC_DEBUG_LOG + __apps_tree_printall(); +#endif + return ret; } -- 2.7.4 From 1551dfc74c889b900b6967bbc76ba83a6edc4ec1 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 10 Oct 2017 14:33:38 +0900 Subject: [PATCH 11/16] Extract file name using /usr/apps keyword And ignore blank space Change-Id: I36813811a9f0ad358d591ae197de0034c326486a Signed-off-by: hyunuktak --- src/helper/helper-procfs.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c index ca291ac..168c34a 100755 --- a/src/helper/helper-procfs.c +++ b/src/helper/helper-procfs.c @@ -38,12 +38,16 @@ #include "stc-manager-util.h" #include "helper-procfs.h" +#define USRAPPS "/usr/apps/" + int proc_get_cmdline(pid_t pid, char *cmdline) { char buf[PROC_BUF_MAX]; char cmdline_buf[PROC_NAME_MAX]; char *filename; FILE *fp; + char *token = NULL; + char *saveptr = NULL; snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid); fp = fopen(buf, "r"); @@ -56,11 +60,24 @@ int proc_get_cmdline(pid_t pid, char *cmdline) } fclose(fp); - filename = strrchr(cmdline_buf, '/'); - if (filename == NULL) - filename = cmdline_buf; - else - filename = filename + 1; + if (g_strstr_len(cmdline_buf, strlen(USRAPPS), USRAPPS) != NULL) { + /* Application */ + filename = cmdline_buf + strlen(USRAPPS); + token = strtok_r(filename, "/", &saveptr); + if (token != NULL) + filename = token; + } else { + token = strtok_r(cmdline_buf, " ", &saveptr); + if (token != NULL) + filename = strrchr(token, '/'); + else + filename = strrchr(cmdline_buf, '/'); + + if (filename == NULL) + filename = cmdline_buf; + else + filename = filename + 1; + } strncpy(cmdline, filename, PROC_NAME_MAX-1); -- 2.7.4 From f32f457577783c55aaee0a86f6cebf34f49a9362 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 10 Oct 2017 14:37:42 +0900 Subject: [PATCH 12/16] Fixed coverity issues Change-Id: I19f27469c22bc1662c8a33afc92f3bebdc2c560d Signed-off-by: hyunuktak --- src/helper/helper-nfacct-rule.c | 6 +++--- src/monitor/stc-monitor.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index aec0ef3..bd86b01 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -278,15 +278,15 @@ bool recreate_counter_by_name(char *cnt_name, nfacct_rule_s *cnt) iface = get_iftype_by_name(ifname_buf); /* check first part is it datacall */ if (iface == STC_IFACE_DATACALL) { - strncpy(cnt->ifname, ifname_buf, MAX_IFACE_LENGTH); + strncpy(cnt->ifname, ifname_buf, MAX_IFACE_LENGTH - 1); cnt->iotype = NFACCT_COUNTER_IN; } else { /* +1, due : symbol and till the end of cnt_name */ - strncpy(ifname_buf, iftype_part + 1, MAX_IFACE_LENGTH); + strncpy(ifname_buf, iftype_part + 1, MAX_IFACE_LENGTH - 1); iface = get_iftype_by_name(ifname_buf); if (iface == STC_IFACE_DATACALL) { cnt->iotype = NFACCT_COUNTER_OUT; - strncpy(cnt->ifname, ifname_buf, MAX_IFACE_LENGTH); + strncpy(cnt->ifname, ifname_buf, MAX_IFACE_LENGTH - 1); } } diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index a50d65b..522a615 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -716,7 +716,7 @@ static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system) /* create netlink socket for updating kernel counters */ system->contr_sock = create_netlink(NETLINK_NETFILTER, 0); - if (!(system->contr_sock)) { + if (system->contr_sock < 0) { STC_LOGE("failed to open socket"); FREE(system); return STC_ERROR_FAIL; @@ -1599,7 +1599,7 @@ 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)) { + if (system->contr_sock < 0) { STC_LOGE("failed to open socket"); FREE(system); return STC_ERROR_FAIL; -- 2.7.4 From a2bb048375d5726c415de33cb5f501b569cf0105 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 10 Oct 2017 14:42:33 +0900 Subject: [PATCH 13/16] Added exceptions for system call and bracket Change-Id: Ib28764520292f29b4378620680d3391ef2308c93 Signed-off-by: hyunuktak --- data/exceptions | 22 ++++++++++++++++++++-- src/monitor/stc-app-lifecycle.c | 6 +++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/data/exceptions b/data/exceptions index 0429338..2ce70b0 100644 --- a/data/exceptions +++ b/data/exceptions @@ -17,14 +17,32 @@ sleep:inst grep:inst killall:inst systemctl:inst +xargs:inst +modprobe:inst +pkgcmd:inst +mount:inst +chmod:inst wlan.sh:script wpa_supp.sh:script +stc-manager:sys dlogutil:sys wifi-loader:sys -launchpad-loader:sys wrt-loader:sys dotnet-launcher:sys iptables:sys ip6tables:sys -modprobe:inst net-cls-release:sys +init:sys +amd:sys +launchpad-loader:sys +launchpad-process-pool:sys +deviced:sys +systemd:sys +systemd-udevd:sys +systemd-user:sys +systemd-cgroups-agent:sys +systemd-journald:sys +sdbd:sys +sdbd-user:sys +pushd:sys +dbus-daemon:sys diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index 09fcef0..235dc04 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -204,8 +204,12 @@ static void __proc_tree_remove(const proc_key_s *key) static gboolean __check_excn(char *cmdline) { - stc_error_e ret = stc_monitor_check_excn_by_cmdline(cmdline); + stc_error_e ret = STC_ERROR_NONE; + + if (cmdline[0] == '(') + return TRUE; + ret = stc_monitor_check_excn_by_cmdline(cmdline); if (ret == STC_ERROR_NO_DATA) return FALSE; else -- 2.7.4 From 754b1ad89442526794acd69a395aa08a5bfc17e1 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 10 Oct 2017 14:48:27 +0900 Subject: [PATCH 14/16] Added information config with inotify Change-Id: If6faf890e69fa7a2de05d362aada815c29778411 Signed-off-by: hyunuktak --- include/stc-manager-util.h | 13 +++ include/stc-manager.h | 2 +- packaging/stc-manager.spec | 2 +- plugin/stc-plugin.c | 4 +- src/helper/helper-cgroup.c | 21 ++-- src/helper/helper-inotify.c | 206 ++++++++++++++++++++++++++++++++++++++++ src/helper/helper-inotify.h | 32 +++++++ src/helper/helper-net-cls.c | 3 +- src/helper/helper-nfacct-rule.c | 18 ++-- src/monitor/stc-app-lifecycle.c | 33 +++---- src/monitor/stc-monitor.c | 26 ++--- src/stc-manager-util.c | 142 +++++++++++++++++++++++++++ src/stc-manager.c | 42 ++++++++ 13 files changed, 484 insertions(+), 60 deletions(-) create mode 100755 src/helper/helper-inotify.c create mode 100755 src/helper/helper-inotify.h create mode 100755 src/stc-manager-util.c diff --git a/include/stc-manager-util.h b/include/stc-manager-util.h index 708f67c..d504cf3 100755 --- a/include/stc-manager-util.h +++ b/include/stc-manager-util.h @@ -327,6 +327,10 @@ static inline bool strstart_with(const char *str, const char *with) #define CGROUP_FILE_NAME "cgroup.procs" #define UNKNOWN_APP "(unknown)" +#define INFO_STORAGE_DIR "/var/lib/stc" +#define INFO_CONFIG "info.config" +#define INFO_DEBUGLOG "debuglog" + #define MAX_PATH_LENGTH 512 #define MAX_NAME_LENGTH 256 #define MAX_IFACE_LENGTH 32 @@ -381,4 +385,13 @@ enum stc_counter_state { STC_UPDATE_REQUESTED = 1 << 5, }; +gboolean stc_util_get_config_bool(char *key); +gchar * stc_util_get_config_str(char *key); +int stc_util_get_config_int(char *key); + +void stc_util_set_debuglog(int debuglog); +int stc_util_get_debuglog(void); + +void stc_util_initialize_config(void); + #endif /* __STC_MANAGER_UTIL_H__ */ diff --git a/include/stc-manager.h b/include/stc-manager.h index 37e5d97..2766a60 100755 --- a/include/stc-manager.h +++ b/include/stc-manager.h @@ -31,7 +31,7 @@ #define STC_TOTAL_IPV4 "TOTAL_IPV4" #define STC_TOTAL_IPV6 "TOTAL_IPV6" -#define STC_DEBUG_LOG 0 +#define STC_DEBUG_LOG (stc_util_get_debuglog()) typedef enum { STC_CANCEL = 0, /**< cancel */ diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index d0e6065..cf69262 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.35 +Version: 0.0.36 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/plugin/stc-plugin.c b/plugin/stc-plugin.c index db83094..916a75e 100755 --- a/plugin/stc-plugin.c +++ b/plugin/stc-plugin.c @@ -91,11 +91,11 @@ static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection, else apptype = STC_APP_TYPE_GUI; -#if STC_DEBUG_LOG + 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); -#endif + } if (state_changed_cb) state_changed_cb(status, pid, appid, pkgid, apptype); diff --git a/src/helper/helper-cgroup.c b/src/helper/helper-cgroup.c index c636bc2..8c258a5 100755 --- a/src/helper/helper-cgroup.c +++ b/src/helper/helper-cgroup.c @@ -116,9 +116,10 @@ int cgroup_write_node_uint32(const char *cgroup_name, { char buf[MAX_PATH_LENGTH]; snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name); -#if STC_DEBUG_LOG - STC_LOGD("cgroup_buf %s, value %d\n", buf, value); -#endif + + if (STC_DEBUG_LOG) + STC_LOGD("cgroup_buf %s, value %d\n", buf, value); + return fwrite_uint(buf, value); } @@ -127,9 +128,10 @@ int cgroup_write_node_str(const char *cgroup_name, { char buf[MAX_PATH_LENGTH]; snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name); -#if STC_DEBUG_LOG - STC_LOGD("cgroup_buf %s, string %s\n", buf, string); -#endif + + if (STC_DEBUG_LOG) + STC_LOGD("cgroup_buf %s, string %s\n", buf, string); + return fwrite_str(buf, string); } @@ -140,9 +142,10 @@ int cgroup_read_node_uint32(const char *cgroup_name, int ret; snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name); ret = fread_uint(buf, value); -#if STC_DEBUG_LOG - STC_LOGD("cgroup_buf %s, value %d\n", buf, *value); -#endif + + if (STC_DEBUG_LOG) + STC_LOGD("cgroup_buf %s, value %d\n", buf, *value); + return ret; } diff --git a/src/helper/helper-inotify.c b/src/helper/helper-inotify.c new file mode 100755 index 0000000..71e30e2 --- /dev/null +++ b/src/helper/helper-inotify.c @@ -0,0 +1,206 @@ +/* + * 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 new file mode 100755 index 0000000..1fe9d66 --- /dev/null +++ b/src/helper/helper-inotify.h @@ -0,0 +1,32 @@ +/* + * 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/helper/helper-net-cls.c b/src/helper/helper-net-cls.c index ef5e4b3..6be717a 100755 --- a/src/helper/helper-net-cls.c +++ b/src/helper/helper-net-cls.c @@ -187,7 +187,8 @@ stc_error_e place_pids_to_net_cgroup(const int pid, const char *app_id) path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK; if (access(child_buf, F_OK)) { - STC_LOGD("%s of %s is not existed", child_buf, app_id); + if (STC_DEBUG_LOG) + STC_LOGD("%s of %s is not existed", child_buf, app_id); return cgroup_write_pid(path_to_net_cgroup_dir, app_id, pid); } diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index bd86b01..ca3a7a9 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -136,7 +136,8 @@ static stc_error_e nfacct_send_new(nfacct_rule_s *counter) prepare_netlink_msg(&req, NFNL_MSG_ACCT_NEW, NLM_F_CREATE | NLM_F_ACK); add_string_attr(&req, counter->name, NFACCT_NAME); - STC_LOGD("counter name %s", counter->name); + if (STC_DEBUG_LOG) + STC_LOGD("counter name %s", counter->name); /* padding */ add_uint64_attr(&req, 0, NFACCT_PKTS); @@ -156,9 +157,8 @@ stc_error_e nfacct_send_del(nfacct_rule_s *counter) { struct genl req; -#if STC_DEBUG_LOG - STC_LOGD("send remove request for %s", counter->name); -#endif + 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); @@ -418,9 +418,8 @@ stc_error_e exec_iptables_cmd(const char *cmd_buf, pid_t *cmd_pid) int ret; char *save_ptr = NULL; -#if STC_DEBUG_LOG - STC_LOGD("executing iptables cmd %s in forked process", cmd_buf); -#endif + 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); @@ -464,9 +463,8 @@ stc_error_e exec_ip6tables_cmd(const char *cmd_buf, pid_t *cmd_pid) int ret; char *save_ptr = NULL; -#if STC_DEBUG_LOG - STC_LOGD("executing ip6tables cmd %s in forked process", cmd_buf); -#endif + 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); diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index 235dc04..eb18816 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -101,7 +101,6 @@ static proc_value_s * __proc_tree_lookup(const proc_key_s *key) return lookup; } -#if STC_DEBUG_LOG static gboolean __proc_tree_foreach_print(gpointer key, gpointer value, gpointer data) { @@ -119,7 +118,6 @@ static void __proc_tree_printall(void) { g_tree_foreach(proc_tree, __proc_tree_foreach_print, NULL); } -#endif static proc_value_s * __proc_tree_find_parent(proc_value_s *value) { @@ -134,14 +132,14 @@ static proc_value_s * __proc_tree_find_parent(proc_value_s *value) parent = lookup; } while (lookup); -#if STC_DEBUG_LOG + 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] " "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, parent->status[PROC_STATUS_NAME]); -#endif + } return parent; } @@ -159,12 +157,11 @@ static void __proc_tree_add(proc_key_s *key, lookup = g_tree_lookup(proc_tree, key); if (lookup) { -#if STC_DEBUG_LOG + 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] " "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]); -#endif return; } @@ -173,9 +170,8 @@ static void __proc_tree_add(proc_key_s *key, g_tree_insert(proc_tree, key, value); -#if STC_DEBUG_LOG + if (STC_DEBUG_LOG) __proc_tree_printall(); -#endif parent = __proc_tree_find_parent(value); if (parent != NULL) @@ -195,11 +191,11 @@ static void __proc_tree_remove(const proc_key_s *key) stc_manager_app_status_changed(STC_CMD_SET_TERMINATED, key->pid, NULL, NULL, STC_APP_TYPE_NONE); + g_tree_remove(proc_tree, key); -#if STC_DEBUG_LOG + if (STC_DEBUG_LOG) __proc_tree_printall(); -#endif } static gboolean __check_excn(char *cmdline) @@ -403,9 +399,8 @@ static void __process_event_fork(int tgid, int pid) STC_ERROR_NONE == proc_get_status(pid, status)) { if (__check_excn(cmdline)) { -#if STC_DEBUG_LOG + if (STC_DEBUG_LOG) STC_LOGD("[%s] monitoring is excepted", cmdline); -#endif return; } @@ -431,13 +426,13 @@ static void __process_event_fork(int tgid, int pid) g_strlcpy(value->status[i], status[i], sizeof(value->status[i])); g_strlcpy(value->cmdline, cmdline, sizeof(value->cmdline)); -#if STC_DEBUG_LOG + 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] " "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]", status[PROC_STATUS_TGID], status[PROC_STATUS_PID], status[PROC_STATUS_PPID], status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]); -#endif + } __proc_tree_add(key, value); } @@ -452,9 +447,8 @@ static void __process_event_exec(int tgid, int pid) STC_ERROR_NONE == proc_get_status(pid, status)) { if (__check_excn(cmdline)) { -#if STC_DEBUG_LOG + if (STC_DEBUG_LOG) STC_LOGD("[%s] monitoring is excepted", cmdline); -#endif return; } @@ -480,13 +474,13 @@ static void __process_event_exec(int tgid, int pid) g_strlcpy(value->status[i], status[i], sizeof(value->status[i])); g_strlcpy(value->cmdline, cmdline, sizeof(value->cmdline)); -#if STC_DEBUG_LOG + 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] " "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]", status[PROC_STATUS_TGID], status[PROC_STATUS_PID], status[PROC_STATUS_PPID], status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]); -#endif + } __proc_tree_add(key, value); } @@ -505,10 +499,9 @@ static void __process_event_exit(int tgid, int pid, int exit_code) if (lookup == NULL) /* unmonitored process */ return; -#if STC_DEBUG_LOG + if (STC_DEBUG_LOG) 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); -#endif __proc_tree_remove(&key); } diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 522a615..1cc0b5e 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -276,7 +276,6 @@ static void __rstns_tree_key_free(gpointer data) FREE(key); } -#if STC_DEBUG_LOG static gboolean __processes_tree_foreach_print(gpointer key, gpointer value, gpointer data) { @@ -314,7 +313,6 @@ static void __apps_tree_printall(void) { g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL); } -#endif static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value, gpointer data) @@ -323,9 +321,8 @@ static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value, stc_app_value_s *app_value = (stc_app_value_s *)value; if (!g_tree_remove(app_value->processes, context->proc_key)) { -#if STC_DEBUG_LOG - STC_LOGD("key not found"); -#endif + if (STC_DEBUG_LOG) + STC_LOGD("key not found"); return FALSE; } @@ -966,18 +963,16 @@ static void __app_counter_update(stc_app_key_s *app_key, app_value->counter.in_bytes = context->bytes; g_system->apps_tree_updated = TRUE; -#if STC_DEBUG_LOG - __apps_tree_foreach_print(app_key, app_value, NULL); -#endif + if (STC_DEBUG_LOG) + __apps_tree_foreach_print(app_key, app_value, NULL); break; case NFACCT_COUNTER_OUT: app_value->data_usage.out_bytes += context->bytes; app_value->counter.out_bytes = context->bytes; g_system->apps_tree_updated = TRUE; -#if STC_DEBUG_LOG - __apps_tree_foreach_print(app_key, app_value, NULL); -#endif + if (STC_DEBUG_LOG) + __apps_tree_foreach_print(app_key, app_value, NULL); break; default: STC_LOGE("unknown iotype"); @@ -1036,7 +1031,8 @@ static void __fill_nfacct_result(char *cnt_name, int64_t bytes, .data_limit_reached = FALSE, }; - STC_LOGD("cnt_name %s", cnt_name); + if (STC_DEBUG_LOG) + STC_LOGD("cnt_name %s", cnt_name); if (!recreate_counter_by_name(cnt_name, &counter)) { STC_LOGE("Can't parse counter name %s", cnt_name); @@ -1780,9 +1776,8 @@ stc_error_e stc_monitor_process_add(const stc_app_key_s app_key, /* add pid to application cgroup */ place_pids_to_net_cgroup(proc_key.pid, app_key.app_id); -#if STC_DEBUG_LOG + if (STC_DEBUG_LOG) __apps_tree_printall(); -#endif return ret; } @@ -1808,9 +1803,8 @@ stc_error_e stc_monitor_process_remove(pid_t pid) if (context.entry_removed) __application_remove_if_empty(context.app_key); -#if STC_DEBUG_LOG + if (STC_DEBUG_LOG) __apps_tree_printall(); -#endif return ret; } diff --git a/src/stc-manager-util.c b/src/stc-manager-util.c new file mode 100755 index 0000000..1c97e81 --- /dev/null +++ b/src/stc-manager-util.c @@ -0,0 +1,142 @@ +/* + * 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-util.h" + +static int g_debuglog = -1; + +static void __sync_file_to_disk(const char *path) +{ + FILE *fp = NULL; + fp = fopen(path, "a+"); + if (fp) { + fflush(fp); + fsync(fp->_fileno); + fclose(fp); + STC_LOGD("Sync the file to disk"); + } +} + +static GKeyFile *__load_key_file(const char *path) +{ + GKeyFile *keyfile = NULL; + GError *error = NULL; + + STC_LOGD("Loading [%s]", 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; + } + + return keyfile; +} + +static int __save_key_file(GKeyFile *keyfile, char *path) +{ + gchar *data = NULL; + gsize length = 0; + GError *error = NULL; + int ret = 0; + + 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; + } + + __sync_file_to_disk(path); + + g_free(data); + return ret; +} + +gboolean stc_util_get_config_bool(char *key) +{ + char path[MAX_PATH_LENGTH]; + GKeyFile *keyfile; + + snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG); + + keyfile = __load_key_file(path); + if (!keyfile) + keyfile = g_key_file_new(); + + return g_key_file_get_boolean(keyfile, path, key, NULL); +} + +gchar * stc_util_get_config_str(char *key) +{ + char path[MAX_PATH_LENGTH]; + GKeyFile *keyfile; + + snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG); + + keyfile = __load_key_file(path); + if (!keyfile) + keyfile = g_key_file_new(); + + return g_key_file_get_string(keyfile, path, key, NULL); +} + +int stc_util_get_config_int(char *key) +{ + char path[MAX_PATH_LENGTH]; + GKeyFile *keyfile; + + snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG); + + keyfile = __load_key_file(path); + if (!keyfile) + keyfile = g_key_file_new(); + + return g_key_file_get_integer(keyfile, path, key, NULL); +} + +API void stc_util_set_debuglog(int debuglog) +{ + g_debuglog = debuglog; +} + +API int stc_util_get_debuglog(void) +{ + if (g_debuglog == -1) + g_debuglog = stc_util_get_config_int(INFO_DEBUGLOG); + + return g_debuglog; +} + +void stc_util_initialize_config(void) +{ + char path[MAX_PATH_LENGTH]; + GKeyFile *keyfile; + + snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG); + + keyfile = __load_key_file(path); + if (!keyfile) + keyfile = g_key_file_new(); + + g_key_file_set_integer(keyfile, path, INFO_DEBUGLOG, 0); + + __save_key_file(keyfile, path); +} diff --git a/src/stc-manager.c b/src/stc-manager.c index cfee380..d6e8521 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -23,12 +23,46 @@ #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" static stc_s *g_stc = NULL; +static gboolean __validate_ident(const char *ident) +{ + unsigned int i; + + if (!ident) + return FALSE; + + for (i = 0; i < strlen(ident); ++i) + if (!g_ascii_isprint(ident[i])) + return FALSE; + + 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__; @@ -46,6 +80,9 @@ 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__; @@ -64,6 +101,11 @@ static stc_s *__stc_manager_init(void) } g_stc = stc; + 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 951a7b00437655c92d1a998c4e1ed4353010eb0b Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Wed, 11 Oct 2017 11:02:29 +0530 Subject: [PATCH 15/16] [Fix] Fetch proper app_id for creation of cgroup. Description: The app_id was not extracted properly from procfs. Some processes run with help of other daemons, for example org.tizen.browser app is execued using efl_webprocess. In this patch for resolving above issue, if /proc//attr/current file contains "User::Pkg::" wildcard string, then we extract app_id from above file. Otherwise we read /proc//cmdline and fetch process_name as app_id. Change-Id: I0eb46f4f675cec3f2323c0ab05388fec46c7781f Signed-off-by: Nishant Chaprana --- include/stc-plugin.h | 2 +- packaging/stc-manager.spec | 2 +- plugin/stc-plugin.c | 7 +-- src/helper/helper-inotify.h | 4 +- src/helper/helper-procfs.c | 124 +++++++++----------------------------- src/helper/helper-procfs.h | 19 ------ src/monitor/include/stc-monitor.h | 2 +- src/monitor/stc-app-lifecycle.c | 87 +++++++++++++++----------- src/monitor/stc-monitor.c | 4 +- 9 files changed, 91 insertions(+), 160 deletions(-) diff --git a/include/stc-plugin.h b/include/stc-plugin.h index 4d843b8..53cfaec 100755 --- a/include/stc-plugin.h +++ b/include/stc-plugin.h @@ -21,7 +21,7 @@ #include "stc-error.h" #include "stc-manager.h" -typedef stc_error_e (*stc_plugin_app_state_changed_cb)(stc_cmd_type_e cmd, +typedef stc_error_e(*stc_plugin_app_state_changed_cb) (stc_cmd_type_e cmd, pid_t pid, const gchar *app_id, const gchar *pkg_id, 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/plugin/stc-plugin.c b/plugin/stc-plugin.c index 916a75e..3587db1 100755 --- a/plugin/stc-plugin.c +++ b/plugin/stc-plugin.c @@ -74,13 +74,12 @@ static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection, g_variant_get(parameters, AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE, &pid, &appid, &pkgid, &statstr, &pkgtype); - if (!strncmp(statstr, "fg", 2)) { + if (!strncmp(statstr, "fg", 2)) status = STC_CMD_SET_FOREGRD; - } else if (!strncmp(statstr, "bg", 2)) { + else if (!strncmp(statstr, "bg", 2)) status = STC_CMD_SET_BACKGRD; - } else { + else goto out; - } if (!strncmp(pkgtype, "svc", 3)) apptype = STC_APP_TYPE_SERVICE; diff --git a/src/helper/helper-inotify.h b/src/helper/helper-inotify.h index 1fe9d66..9ee6995 100755 --- a/src/helper/helper-inotify.h +++ b/src/helper/helper-inotify.h @@ -20,8 +20,8 @@ #include struct inotify_event; -typedef void (* inotify_event_cb) (struct inotify_event *event, - const char *ident); +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); diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c index 168c34a..9dcd36e 100755 --- a/src/helper/helper-procfs.c +++ b/src/helper/helper-procfs.c @@ -40,25 +40,34 @@ #define USRAPPS "/usr/apps/" +static int __proc_get_data(char *path, char *buf, int len) +{ + _cleanup_fclose_ FILE *fp = NULL; + + fp = fopen(path, "r"); + if (fp == NULL) + return STC_ERROR_FAIL; + + if (fgets(buf, len - 1, fp) == NULL) + return STC_ERROR_FAIL; + + return STC_ERROR_NONE; +} + int proc_get_cmdline(pid_t pid, char *cmdline) { - char buf[PROC_BUF_MAX]; - char cmdline_buf[PROC_NAME_MAX]; char *filename; - FILE *fp; char *token = NULL; char *saveptr = NULL; + char path_buf[PROC_BUF_MAX] = {0, }; + char cmdline_buf[PROC_NAME_MAX] = {0, }; + stc_error_e ret = STC_ERROR_NONE; - snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid); - fp = fopen(buf, "r"); - if (fp == NULL) - return STC_ERROR_FAIL; + snprintf(path_buf, sizeof(path_buf), "/proc/%d/cmdline", pid); - if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) { - fclose(fp); - return STC_ERROR_FAIL; - } - fclose(fp); + ret = __proc_get_data(path_buf, cmdline_buf, PROC_NAME_MAX); + if (ret != STC_ERROR_NONE) + return ret; if (g_strstr_len(cmdline_buf, strlen(USRAPPS), USRAPPS) != NULL) { /* Application */ @@ -80,100 +89,25 @@ int proc_get_cmdline(pid_t pid, char *cmdline) } strncpy(cmdline, filename, PROC_NAME_MAX-1); - return STC_ERROR_NONE; } -pid_t find_pid_from_cmdline(char *cmdline) -{ - pid_t pid = -1, foundpid = -1; - int ret = 0; - DIR *dp; - struct dirent *dentry; - char appname[PROC_NAME_MAX]; - - dp = opendir("/proc"); - if (!dp) { - STC_LOGE("BACKGRD MANAGE : fail to open /proc"); - return STC_ERROR_FAIL; - } - - while ((dentry = readdir(dp)) != NULL) { - if (!isdigit(dentry->d_name[0])) - continue; - - pid = atoi(dentry->d_name); - if (!pid) - continue; - ret = proc_get_cmdline(pid, appname); - if (ret == STC_ERROR_NONE) { - if (!strncmp(cmdline, appname, strlen(appname)+1)) { - foundpid = pid; - break; - } - } - } - closedir(dp); - return foundpid; -} - int proc_get_label(pid_t pid, char *label) { - char buf[PROC_BUF_MAX]; - FILE *fp; + char path_buf[PROC_BUF_MAX] = {0, }; + char label_buf[PROC_NAME_MAX] = {0, }; + stc_error_e ret = STC_ERROR_NONE; - snprintf(buf, sizeof(buf), "/proc/%d/attr/current", pid); - fp = fopen(buf, "r"); - if (fp == NULL) - return STC_ERROR_FAIL; + snprintf(path_buf, sizeof(path_buf), "/proc/%d/attr/current", pid); - if (fgets(label, PROC_NAME_MAX-1, fp) == NULL) { - fclose(fp); - return STC_ERROR_FAIL; - } - fclose(fp); - return STC_ERROR_NONE; -} + ret = __proc_get_data(path_buf, label_buf, PROC_NAME_MAX); + if (ret != STC_ERROR_NONE) + return ret; -int proc_get_exepath(pid_t pid, char *buf, int len) -{ - char path[PROC_BUF_MAX]; - int ret = 0; - - snprintf(path, sizeof(path), "/proc/%d/exe", pid); - ret = readlink(path, buf, len-1); - if (ret > 0) - buf[ret] = '\0'; - else - buf[0] = '\0'; + strncpy(label, label_buf, PROC_NAME_MAX-1); return STC_ERROR_NONE; } -static int proc_get_data(char *path, char *buf, int len) -{ - _cleanup_close_ int fd = -1; - int ret; - - fd = open(path, O_RDONLY); - if (fd < 0) - return STC_ERROR_FAIL; - - ret = read(fd, buf, len-1); - if (ret < 0) { - buf[0] = '\0'; - return STC_ERROR_FAIL; - } - buf[ret] = '\0'; - return STC_ERROR_NONE; -} - -int proc_get_raw_cmdline(pid_t pid, char *buf, int len) -{ - char path[PROC_BUF_MAX]; - snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); - return proc_get_data(path, buf, len); -} - int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX]) { unsigned int i; diff --git a/src/helper/helper-procfs.h b/src/helper/helper-procfs.h index 55c6a03..c6c9b90 100755 --- a/src/helper/helper-procfs.h +++ b/src/helper/helper-procfs.h @@ -29,13 +29,6 @@ int proc_get_cmdline(pid_t pid, char *cmdline); /** - * @desc find pid with /proc/{pid}/cmdline - * it returns first entry when many pids have same cmdline - * @return negative value if error - */ -pid_t find_pid_from_cmdline(char *cmdline); - -/** * @desc get smack subject label from /proc/{pid}/attr/current * this label can indicate package name about child processes * @return negative value if error or pid doesn't exist @@ -43,18 +36,6 @@ pid_t find_pid_from_cmdline(char *cmdline); int proc_get_label(pid_t pid, char *label); /** - * @desc get command line from /proc/{pid}/cmdline without any truncation - * @return negative value if error - */ -int proc_get_raw_cmdline(pid_t pid, char *buf, int len); - -/** - * @desc get symblolic link about /proc/{pid}/exe - * @return negative value if error - */ -int proc_get_exepath(pid_t pid, char *buf, int len); - -/** * @desc get status from /proc/{pid}/status * @return negative value if error */ diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index 0ecce65..ee2213f 100755 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -148,7 +148,7 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info); stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info); -stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline); +stc_error_e stc_monitor_check_excn_by_app_id(char *app_id); int stc_monitor_get_counter_socket(void); diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index eb18816..81b976b 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -36,7 +36,7 @@ typedef struct { } proc_key_s; typedef struct { - char cmdline[PROC_NAME_MAX]; + char *app_id; char status[PROC_STATUS_CNT][PROC_BUF_MAX]; } proc_value_s; @@ -78,6 +78,7 @@ static void __proc_tree_value_free(gpointer data) { proc_value_s *value = (proc_value_s *)data; + FREE(value->app_id); FREE(value); } @@ -108,8 +109,8 @@ static gboolean __proc_tree_foreach_print(gpointer key, gpointer value, proc_value_s *proc_value = (proc_value_s *)value; STC_LOGD("Proc pid [\033[1;33m%d\033[0;m] ppid [\033[1;35m%s\033[0;m] " - "cmdline [\033[0;34m%s\033[0;m]", proc_key->pid, - proc_value->status[PROC_STATUS_PPID], proc_value->cmdline); + "app_id [\033[0;34m%s\033[0;m]", proc_key->pid, + proc_value->status[PROC_STATUS_PPID], proc_value->app_id); return FALSE; } @@ -135,9 +136,9 @@ static proc_value_s * __proc_tree_find_parent(proc_value_s *value) 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] " - "ppid[\033[1;35m%s\033[0;m] cmdline[\033[0;34m%s\033[0;m] name[%s]", + "ppid[\033[1;35m%s\033[0;m] app_id[\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, + parent->status[PROC_STATUS_PPID], parent->app_id, parent->status[PROC_STATUS_NAME]); } @@ -159,13 +160,13 @@ static void __proc_tree_add(proc_key_s *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] " - "cmdline[\033[0;34m%s\033[0;m] name[%s]", lookup->status[PROC_STATUS_TGID], + "app_id[\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]); + lookup->app_id, lookup->status[PROC_STATUS_NAME]); return; } - STC_LOGD("cmdline [%s] pid[%s] ppid[%s]", value->cmdline, + STC_LOGD("app_id [%s] pid[%s] ppid[%s]", value->app_id, value->status[PROC_STATUS_PID], value->status[PROC_STATUS_PPID]); g_tree_insert(proc_tree, key, value); @@ -176,10 +177,10 @@ 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, - parent->cmdline, parent->cmdline, STC_APP_TYPE_SERVICE); + parent->app_id, parent->app_id, STC_APP_TYPE_SERVICE); else stc_manager_app_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid, - value->cmdline, value->cmdline, STC_APP_TYPE_SERVICE); + value->app_id, value->app_id, STC_APP_TYPE_SERVICE); } static void __proc_tree_remove(const proc_key_s *key) @@ -198,14 +199,14 @@ static void __proc_tree_remove(const proc_key_s *key) __proc_tree_printall(); } -static gboolean __check_excn(char *cmdline) +static gboolean __check_excn(char *app_id) { stc_error_e ret = STC_ERROR_NONE; - if (cmdline[0] == '(') + if (app_id && app_id[0] == '(') return TRUE; - ret = stc_monitor_check_excn_by_cmdline(cmdline); + ret = stc_monitor_check_excn_by_app_id(app_id); if (ret == STC_ERROR_NO_DATA) return FALSE; else @@ -390,24 +391,41 @@ stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, return ret; } -static void __process_event_fork(int tgid, int pid) +static char * __process_get_app_id(int pid) { + char label[PROC_NAME_MAX] = {0, }; char cmdline[PROC_NAME_MAX] = {0, }; + char *app_id = NULL; + + if (STC_ERROR_NONE == proc_get_label(pid, label)) { + app_id = strstr(label, "User::Pkg::"); + if (app_id != NULL) + return g_strdup(app_id + 11); + } + + if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline)) + return g_strdup(cmdline); + + return NULL; +} + +static void __process_event_fork(int tgid, int pid) +{ char status[PROC_STATUS_CNT][PROC_BUF_MAX]; + char *app_id = __process_get_app_id(pid); - if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && - STC_ERROR_NONE == proc_get_status(pid, status)) { + if (app_id != NULL && proc_get_status(pid, status) == STC_ERROR_NONE) { - if (__check_excn(cmdline)) { + unsigned int i; + proc_key_s *key = NULL; + proc_value_s *value = NULL; + + if (__check_excn(app_id)) { if (STC_DEBUG_LOG) - STC_LOGD("[%s] monitoring is excepted", cmdline); + STC_LOGD("[%s] monitoring is excepted", app_id); return; } - unsigned int i; - proc_key_s *key; - proc_value_s *value; - key = MALLOC0(proc_key_s, 1); if (key == NULL) { STC_LOGE("memory allocation failed"); @@ -422,13 +440,13 @@ static void __process_event_fork(int tgid, int pid) } key->pid = tgid; + value->app_id = app_id; for (i = 0; i < PROC_STATUS_CNT; ++i) g_strlcpy(value->status[i], status[i], sizeof(value->status[i])); - 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] " - "cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, pid); + "app_id[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], app_id, pid); 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]); @@ -440,22 +458,21 @@ static void __process_event_fork(int tgid, int pid) static void __process_event_exec(int tgid, int pid) { - char cmdline[PROC_NAME_MAX] = {0, }; char status[PROC_STATUS_CNT][PROC_BUF_MAX]; + char *app_id = __process_get_app_id(pid); - if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && - STC_ERROR_NONE == proc_get_status(pid, status)) { + if (app_id != NULL && proc_get_status(pid, status) == STC_ERROR_NONE) { - if (__check_excn(cmdline)) { + unsigned int i; + proc_key_s *key = NULL; + proc_value_s *value = NULL; + + if (__check_excn(app_id)) { if (STC_DEBUG_LOG) - STC_LOGD("[%s] monitoring is excepted", cmdline); + STC_LOGD("[%s] monitoring is excepted", app_id); return; } - unsigned int i; - proc_key_s *key; - proc_value_s *value; - key = MALLOC0(proc_key_s, 1); if (key == NULL) { STC_LOGE("memory allocation failed"); @@ -470,13 +487,13 @@ static void __process_event_exec(int tgid, int pid) } key->pid = tgid; + value->app_id = app_id; for (i = 0; i < PROC_STATUS_CNT; ++i) g_strlcpy(value->status[i], status[i], sizeof(value->status[i])); - 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] " - "cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, pid); + "app_id[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], app_id, pid); 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]); diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 1cc0b5e..2530dd0 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -1948,13 +1948,13 @@ 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) +stc_error_e stc_monitor_check_excn_by_app_id(char *app_id) { 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); + exe_type = g_hash_table_lookup(g_system->excns_hash, app_id); if (!exe_type) return STC_ERROR_NO_DATA; -- 2.7.4 From 11ea9abc4e3df7431685a10e330a4a5743868c1b Mon Sep 17 00:00:00 2001 From: taesub kim Date: Mon, 16 Oct 2017 05:31:20 +0000 Subject: [PATCH 16/16] Revert "[Fix] Fetch proper app_id for creation of cgroup." This reverts commit 951a7b00437655c92d1a998c4e1ed4353010eb0b. Change-Id: I10c8ea979a54ca189b0eab6c565e7320307e85e0 --- include/stc-plugin.h | 2 +- packaging/stc-manager.spec | 2 +- plugin/stc-plugin.c | 7 ++- src/helper/helper-inotify.h | 4 +- src/helper/helper-procfs.c | 124 +++++++++++++++++++++++++++++--------- src/helper/helper-procfs.h | 19 ++++++ src/monitor/include/stc-monitor.h | 2 +- src/monitor/stc-app-lifecycle.c | 87 +++++++++++--------------- src/monitor/stc-monitor.c | 4 +- 9 files changed, 160 insertions(+), 91 deletions(-) diff --git a/include/stc-plugin.h b/include/stc-plugin.h index 53cfaec..4d843b8 100755 --- a/include/stc-plugin.h +++ b/include/stc-plugin.h @@ -21,7 +21,7 @@ #include "stc-error.h" #include "stc-manager.h" -typedef stc_error_e(*stc_plugin_app_state_changed_cb) (stc_cmd_type_e cmd, +typedef stc_error_e (*stc_plugin_app_state_changed_cb)(stc_cmd_type_e cmd, pid_t pid, const gchar *app_id, const gchar *pkg_id, diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 9500d61..cf69262 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.36 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/plugin/stc-plugin.c b/plugin/stc-plugin.c index 3587db1..916a75e 100755 --- a/plugin/stc-plugin.c +++ b/plugin/stc-plugin.c @@ -74,12 +74,13 @@ static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection, g_variant_get(parameters, AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE, &pid, &appid, &pkgid, &statstr, &pkgtype); - if (!strncmp(statstr, "fg", 2)) + if (!strncmp(statstr, "fg", 2)) { status = STC_CMD_SET_FOREGRD; - else if (!strncmp(statstr, "bg", 2)) + } else if (!strncmp(statstr, "bg", 2)) { status = STC_CMD_SET_BACKGRD; - else + } else { goto out; + } if (!strncmp(pkgtype, "svc", 3)) apptype = STC_APP_TYPE_SERVICE; diff --git a/src/helper/helper-inotify.h b/src/helper/helper-inotify.h index 9ee6995..1fe9d66 100755 --- a/src/helper/helper-inotify.h +++ b/src/helper/helper-inotify.h @@ -20,8 +20,8 @@ #include struct inotify_event; -typedef void (*inotify_event_cb) (struct inotify_event *event, - const char *ident); +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); diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c index 9dcd36e..168c34a 100755 --- a/src/helper/helper-procfs.c +++ b/src/helper/helper-procfs.c @@ -40,34 +40,25 @@ #define USRAPPS "/usr/apps/" -static int __proc_get_data(char *path, char *buf, int len) -{ - _cleanup_fclose_ FILE *fp = NULL; - - fp = fopen(path, "r"); - if (fp == NULL) - return STC_ERROR_FAIL; - - if (fgets(buf, len - 1, fp) == NULL) - return STC_ERROR_FAIL; - - return STC_ERROR_NONE; -} - int proc_get_cmdline(pid_t pid, char *cmdline) { + char buf[PROC_BUF_MAX]; + char cmdline_buf[PROC_NAME_MAX]; char *filename; + FILE *fp; char *token = NULL; char *saveptr = NULL; - char path_buf[PROC_BUF_MAX] = {0, }; - char cmdline_buf[PROC_NAME_MAX] = {0, }; - stc_error_e ret = STC_ERROR_NONE; - snprintf(path_buf, sizeof(path_buf), "/proc/%d/cmdline", pid); + snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid); + fp = fopen(buf, "r"); + if (fp == NULL) + return STC_ERROR_FAIL; - ret = __proc_get_data(path_buf, cmdline_buf, PROC_NAME_MAX); - if (ret != STC_ERROR_NONE) - return ret; + if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) { + fclose(fp); + return STC_ERROR_FAIL; + } + fclose(fp); if (g_strstr_len(cmdline_buf, strlen(USRAPPS), USRAPPS) != NULL) { /* Application */ @@ -89,25 +80,100 @@ int proc_get_cmdline(pid_t pid, char *cmdline) } strncpy(cmdline, filename, PROC_NAME_MAX-1); + return STC_ERROR_NONE; } +pid_t find_pid_from_cmdline(char *cmdline) +{ + pid_t pid = -1, foundpid = -1; + int ret = 0; + DIR *dp; + struct dirent *dentry; + char appname[PROC_NAME_MAX]; + + dp = opendir("/proc"); + if (!dp) { + STC_LOGE("BACKGRD MANAGE : fail to open /proc"); + return STC_ERROR_FAIL; + } + + while ((dentry = readdir(dp)) != NULL) { + if (!isdigit(dentry->d_name[0])) + continue; + + pid = atoi(dentry->d_name); + if (!pid) + continue; + ret = proc_get_cmdline(pid, appname); + if (ret == STC_ERROR_NONE) { + if (!strncmp(cmdline, appname, strlen(appname)+1)) { + foundpid = pid; + break; + } + } + } + closedir(dp); + return foundpid; +} + int proc_get_label(pid_t pid, char *label) { - char path_buf[PROC_BUF_MAX] = {0, }; - char label_buf[PROC_NAME_MAX] = {0, }; - stc_error_e ret = STC_ERROR_NONE; + char buf[PROC_BUF_MAX]; + FILE *fp; - snprintf(path_buf, sizeof(path_buf), "/proc/%d/attr/current", pid); + snprintf(buf, sizeof(buf), "/proc/%d/attr/current", pid); + fp = fopen(buf, "r"); + if (fp == NULL) + return STC_ERROR_FAIL; - ret = __proc_get_data(path_buf, label_buf, PROC_NAME_MAX); - if (ret != STC_ERROR_NONE) - return ret; + if (fgets(label, PROC_NAME_MAX-1, fp) == NULL) { + fclose(fp); + return STC_ERROR_FAIL; + } + fclose(fp); + return STC_ERROR_NONE; +} - strncpy(label, label_buf, PROC_NAME_MAX-1); +int proc_get_exepath(pid_t pid, char *buf, int len) +{ + char path[PROC_BUF_MAX]; + int ret = 0; + + snprintf(path, sizeof(path), "/proc/%d/exe", pid); + ret = readlink(path, buf, len-1); + if (ret > 0) + buf[ret] = '\0'; + else + buf[0] = '\0'; return STC_ERROR_NONE; } +static int proc_get_data(char *path, char *buf, int len) +{ + _cleanup_close_ int fd = -1; + int ret; + + fd = open(path, O_RDONLY); + if (fd < 0) + return STC_ERROR_FAIL; + + ret = read(fd, buf, len-1); + if (ret < 0) { + buf[0] = '\0'; + return STC_ERROR_FAIL; + } + buf[ret] = '\0'; + return STC_ERROR_NONE; +} + +int proc_get_raw_cmdline(pid_t pid, char *buf, int len) +{ + char path[PROC_BUF_MAX]; + snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); + return proc_get_data(path, buf, len); +} + int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX]) { unsigned int i; diff --git a/src/helper/helper-procfs.h b/src/helper/helper-procfs.h index c6c9b90..55c6a03 100755 --- a/src/helper/helper-procfs.h +++ b/src/helper/helper-procfs.h @@ -29,6 +29,13 @@ int proc_get_cmdline(pid_t pid, char *cmdline); /** + * @desc find pid with /proc/{pid}/cmdline + * it returns first entry when many pids have same cmdline + * @return negative value if error + */ +pid_t find_pid_from_cmdline(char *cmdline); + +/** * @desc get smack subject label from /proc/{pid}/attr/current * this label can indicate package name about child processes * @return negative value if error or pid doesn't exist @@ -36,6 +43,18 @@ int proc_get_cmdline(pid_t pid, char *cmdline); int proc_get_label(pid_t pid, char *label); /** + * @desc get command line from /proc/{pid}/cmdline without any truncation + * @return negative value if error + */ +int proc_get_raw_cmdline(pid_t pid, char *buf, int len); + +/** + * @desc get symblolic link about /proc/{pid}/exe + * @return negative value if error + */ +int proc_get_exepath(pid_t pid, char *buf, int len); + +/** * @desc get status from /proc/{pid}/status * @return negative value if error */ diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index ee2213f..0ecce65 100755 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -148,7 +148,7 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info); stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info); -stc_error_e stc_monitor_check_excn_by_app_id(char *app_id); +stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline); int stc_monitor_get_counter_socket(void); diff --git a/src/monitor/stc-app-lifecycle.c b/src/monitor/stc-app-lifecycle.c index 81b976b..eb18816 100755 --- a/src/monitor/stc-app-lifecycle.c +++ b/src/monitor/stc-app-lifecycle.c @@ -36,7 +36,7 @@ typedef struct { } proc_key_s; typedef struct { - char *app_id; + char cmdline[PROC_NAME_MAX]; char status[PROC_STATUS_CNT][PROC_BUF_MAX]; } proc_value_s; @@ -78,7 +78,6 @@ static void __proc_tree_value_free(gpointer data) { proc_value_s *value = (proc_value_s *)data; - FREE(value->app_id); FREE(value); } @@ -109,8 +108,8 @@ static gboolean __proc_tree_foreach_print(gpointer key, gpointer value, proc_value_s *proc_value = (proc_value_s *)value; STC_LOGD("Proc pid [\033[1;33m%d\033[0;m] ppid [\033[1;35m%s\033[0;m] " - "app_id [\033[0;34m%s\033[0;m]", proc_key->pid, - proc_value->status[PROC_STATUS_PPID], proc_value->app_id); + "cmdline [\033[0;34m%s\033[0;m]", proc_key->pid, + proc_value->status[PROC_STATUS_PPID], proc_value->cmdline); return FALSE; } @@ -136,9 +135,9 @@ static proc_value_s * __proc_tree_find_parent(proc_value_s *value) 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] " - "ppid[\033[1;35m%s\033[0;m] app_id[\033[0;34m%s\033[0;m] name[%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->app_id, + parent->status[PROC_STATUS_PPID], parent->cmdline, parent->status[PROC_STATUS_NAME]); } @@ -160,13 +159,13 @@ static void __proc_tree_add(proc_key_s *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] " - "app_id[\033[0;34m%s\033[0;m] name[%s]", lookup->status[PROC_STATUS_TGID], + "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->app_id, lookup->status[PROC_STATUS_NAME]); + lookup->cmdline, lookup->status[PROC_STATUS_NAME]); return; } - STC_LOGD("app_id [%s] pid[%s] ppid[%s]", value->app_id, + STC_LOGD("cmdline [%s] pid[%s] ppid[%s]", value->cmdline, value->status[PROC_STATUS_PID], value->status[PROC_STATUS_PPID]); g_tree_insert(proc_tree, key, value); @@ -177,10 +176,10 @@ 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, - parent->app_id, parent->app_id, STC_APP_TYPE_SERVICE); + parent->cmdline, parent->cmdline, STC_APP_TYPE_SERVICE); else stc_manager_app_status_changed(STC_CMD_SET_SERVICE_LAUNCHED, key->pid, - value->app_id, value->app_id, STC_APP_TYPE_SERVICE); + value->cmdline, value->cmdline, STC_APP_TYPE_SERVICE); } static void __proc_tree_remove(const proc_key_s *key) @@ -199,14 +198,14 @@ static void __proc_tree_remove(const proc_key_s *key) __proc_tree_printall(); } -static gboolean __check_excn(char *app_id) +static gboolean __check_excn(char *cmdline) { stc_error_e ret = STC_ERROR_NONE; - if (app_id && app_id[0] == '(') + if (cmdline[0] == '(') return TRUE; - ret = stc_monitor_check_excn_by_app_id(app_id); + ret = stc_monitor_check_excn_by_cmdline(cmdline); if (ret == STC_ERROR_NO_DATA) return FALSE; else @@ -391,41 +390,24 @@ stc_error_e stc_manager_app_status_changed(stc_cmd_type_e cmd, return ret; } -static char * __process_get_app_id(int pid) -{ - char label[PROC_NAME_MAX] = {0, }; - char cmdline[PROC_NAME_MAX] = {0, }; - char *app_id = NULL; - - if (STC_ERROR_NONE == proc_get_label(pid, label)) { - app_id = strstr(label, "User::Pkg::"); - if (app_id != NULL) - return g_strdup(app_id + 11); - } - - if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline)) - return g_strdup(cmdline); - - return NULL; -} - static void __process_event_fork(int tgid, int pid) { + char cmdline[PROC_NAME_MAX] = {0, }; char status[PROC_STATUS_CNT][PROC_BUF_MAX]; - char *app_id = __process_get_app_id(pid); - if (app_id != NULL && proc_get_status(pid, status) == STC_ERROR_NONE) { + if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && + STC_ERROR_NONE == proc_get_status(pid, status)) { - unsigned int i; - proc_key_s *key = NULL; - proc_value_s *value = NULL; - - if (__check_excn(app_id)) { + if (__check_excn(cmdline)) { if (STC_DEBUG_LOG) - STC_LOGD("[%s] monitoring is excepted", app_id); + STC_LOGD("[%s] monitoring is excepted", cmdline); return; } + unsigned int i; + proc_key_s *key; + proc_value_s *value; + key = MALLOC0(proc_key_s, 1); if (key == NULL) { STC_LOGE("memory allocation failed"); @@ -440,13 +422,13 @@ static void __process_event_fork(int tgid, int pid) } key->pid = tgid; - value->app_id = app_id; for (i = 0; i < PROC_STATUS_CNT; ++i) g_strlcpy(value->status[i], status[i], sizeof(value->status[i])); + 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] " - "app_id[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], app_id, pid); + "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]", status[PROC_STATUS_TGID], status[PROC_STATUS_PID], status[PROC_STATUS_PPID], status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]); @@ -458,21 +440,22 @@ static void __process_event_fork(int tgid, int pid) static void __process_event_exec(int tgid, int pid) { + char cmdline[PROC_NAME_MAX] = {0, }; char status[PROC_STATUS_CNT][PROC_BUF_MAX]; - char *app_id = __process_get_app_id(pid); - if (app_id != NULL && proc_get_status(pid, status) == STC_ERROR_NONE) { + if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) && + STC_ERROR_NONE == proc_get_status(pid, status)) { - unsigned int i; - proc_key_s *key = NULL; - proc_value_s *value = NULL; - - if (__check_excn(app_id)) { + if (__check_excn(cmdline)) { if (STC_DEBUG_LOG) - STC_LOGD("[%s] monitoring is excepted", app_id); + STC_LOGD("[%s] monitoring is excepted", cmdline); return; } + unsigned int i; + proc_key_s *key; + proc_value_s *value; + key = MALLOC0(proc_key_s, 1); if (key == NULL) { STC_LOGE("memory allocation failed"); @@ -487,13 +470,13 @@ static void __process_event_exec(int tgid, int pid) } key->pid = tgid; - value->app_id = app_id; for (i = 0; i < PROC_STATUS_CNT; ++i) g_strlcpy(value->status[i], status[i], sizeof(value->status[i])); + 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] " - "app_id[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], app_id, pid); + "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]", status[PROC_STATUS_TGID], status[PROC_STATUS_PID], status[PROC_STATUS_PPID], status[PROC_STATUS_NAME], status[PROC_STATUS_STATE], status[PROC_STATUS_TRACERPID]); diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 2530dd0..1cc0b5e 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -1948,13 +1948,13 @@ stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info) return ret; } -stc_error_e stc_monitor_check_excn_by_app_id(char *app_id) +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, app_id); + exe_type = g_hash_table_lookup(g_system->excns_hash, cmdline); if (!exe_type) return STC_ERROR_NO_DATA; -- 2.7.4