From: hyunuktak Date: Fri, 8 Jun 2018 05:51:25 +0000 (+0900) Subject: Add inotify for information config X-Git-Tag: accepted/tizen/unified/20180612.044129^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F98%2F181098%2F2;p=platform%2Fcore%2Fconnectivity%2Fstc-manager.git Add inotify for information config Change-Id: I2d7a54bb54390442702dfe78ab30cd3434a898df Signed-off-by: hyunuktak --- diff --git a/plugin/procfs/stc-plugin-procfs.c b/plugin/procfs/stc-plugin-procfs.c index e889b7d..82b301c 100755 --- a/plugin/procfs/stc-plugin-procfs.c +++ b/plugin/procfs/stc-plugin-procfs.c @@ -164,14 +164,8 @@ static void __proc_tree_add(proc_key_s *key, proc_value_s *value) } lookup = g_tree_lookup(proc_tree, key); - if (lookup) { - if (STC_DEBUG_LOG) - STC_LOGD("LOOKUP: tgid[\033[1;33m%s\033[0;m] pid[%s] ppid[\033[1;35m%s\033[0;m] " - "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]); + if (lookup) return; - } proc_key_s *proc_key = MALLOC0(proc_key_s, 1); if (proc_key == NULL) { @@ -189,10 +183,6 @@ static void __proc_tree_add(proc_key_s *key, proc_value_s *value) memcpy(proc_key, key, sizeof(proc_key_s)); memcpy(proc_value, value, sizeof(proc_value_s)); - if (STC_DEBUG_LOG) - 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, proc_key, proc_value); /* @@ -330,13 +320,9 @@ 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]); - } __proc_tree_add(&key, &value); } @@ -375,13 +361,9 @@ static void __process_event_exec(int tgid, int pid) 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]); - } __proc_tree_add(&key, &value); } diff --git a/src/helper/helper-inotify.c b/src/helper/helper-inotify.c new file mode 100755 index 0000000..05d10db --- /dev/null +++ b/src/helper/helper-inotify.c @@ -0,0 +1,175 @@ +/* + * 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 void __inotify_destroy(gpointer user_data) +{ + int fd; + stc_inotify_s *inotify = user_data; + + if (!inotify->channel) + return; + + if (inotify->watch > 0) + g_source_remove(inotify->watch); + + if (inotify->wd >= 0) { + fd = g_io_channel_unix_get_fd(inotify->channel); + inotify_rm_watch(fd, inotify->wd); + } + + g_io_channel_unref(inotify->channel); +} + +static gboolean __inotify_data(GIOChannel *channel, + GIOCondition cond, gpointer user_data) +{ + stc_inotify_s *inotify = user_data; + char buf[256]; + char *next_event = buf; + 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, buf, + sizeof(buf), &bytes_read, NULL); + if (status != G_IO_STATUS_NORMAL) { + if (status == G_IO_STATUS_AGAIN) + return true; + else { + STC_LOGE("Failed to read from inotify channel"); + inotify->watch = 0; + return false; + } + } + + while (bytes_read > 0) { + struct inotify_event *event = next_event; + gchar *ident = NULL; + gsize len = 0; + + len = sizeof(*event) + event->len; + if (bytes_read < len) + break; + + if (event->len) + ident = next_event + sizeof(*event); + + next_event += len; + bytes_read -= len; + + (inotify->cb)(event, ident); + } + + return true; +} + +int inotify_register(const char *path, inotify_event_cb cb) +{ + int fd; + stc_inotify_s *inotify; + + if (!cb) + return -EINVAL; + + inotify = g_hash_table_lookup(g_inotify_hash, path); + if (inotify) { + inotify->cb = cb; + return 0; + } + + inotify = g_try_new0(stc_inotify_s, 1); + if (!inotify) + return -ENOMEM; + + fd = inotify_init(); + if (fd < 0) { + FREE(inotify); + return -EIO; + } + + inotify->wd = inotify_add_watch(fd, path, IN_MODIFY); + if (inotify->wd < 0) { + STC_LOGE("Failed to create watch [%s]", path); + FREE(inotify); + close(fd); + return -EIO; + } + + inotify->channel = g_io_channel_unix_new(fd); + if (!inotify->channel) { + STC_LOGE("Failed to create channel"); + inotify_rm_watch(fd, inotify->wd); + FREE(inotify); + 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); + + inotify->cb = cb; + + g_hash_table_insert(g_inotify_hash, g_strdup(path), inotify); + 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..aaadafc --- /dev/null +++ b/src/helper/helper-inotify.h @@ -0,0 +1,31 @@ +/* + * 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 cb); +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 old mode 100644 new mode 100755 index e13c729..f3a2ef5 --- a/src/helper/helper-net-cls.c +++ b/src/helper/helper-net-cls.c @@ -189,11 +189,8 @@ stc_error_e place_pids_to_net_cgroup(const int pid, const char *app_id) else path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK; //LCOV_EXCL_LINE - if (access(child_buf, F_OK)) { - if (STC_DEBUG_LOG) - STC_LOGD("%s of %s is not existed", child_buf, app_id); //LCOV_EXCL_LINE + if (access(child_buf, F_OK)) return cgroup_write_pid(path_to_net_cgroup_dir, app_id, pid); - } return cgroup_write_pidtree(path_to_net_cgroup_dir, app_id, pid); //LCOV_EXCL_LINE } diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c old mode 100644 new mode 100755 index 3923daf..caf1174 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -142,9 +142,6 @@ 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); - if (STC_DEBUG_LOG) - STC_LOGD("counter name %s", counter->name); //LCOV_EXCL_LINE - /* padding */ add_uint64_attr(req, 0, NFACCT_PKTS); add_uint64_attr(req, 0, NFACCT_BYTES); @@ -172,9 +169,6 @@ stc_error_e nfacct_send_del(nfacct_rule_s *counter) return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE } - if (STC_DEBUG_LOG) - STC_LOGD("send remove request for %s", counter->name); //LCOV_EXCL_LINE - prepare_netlink_msg(req, NFNL_MSG_ACCT_DEL, NLM_F_ACK); add_string_attr(req, counter->name, NFACCT_NAME); diff --git a/src/helper/helper-procfs.c b/src/helper/helper-procfs.c old mode 100755 new mode 100644 diff --git a/src/helper/helper-procfs.h b/src/helper/helper-procfs.h old mode 100755 new mode 100644 diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index cbd6f11..8ee3533 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -1151,9 +1151,6 @@ static void __fill_nfacct_result(char *cnt_name, int64_t bytes, .data_limit_reached = FALSE, }; - if (STC_DEBUG_LOG) - STC_LOGD("cnt_name %s", cnt_name); //LCOV_EXCL_LINE - if (!recreate_counter_by_name(cnt_name, &counter)) { STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE return; //LCOV_EXCL_LINE @@ -1760,11 +1757,8 @@ API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key, ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!"); lookup = __application_lookup(g_system->apps, &app_key); - if (lookup) { - if (STC_DEBUG_LOG) - STC_LOGD("app_key already present"); //LCOV_EXCL_LINE + if (lookup) return STC_ERROR_NONE; //LCOV_EXCL_LINE - } key = MALLOC0(stc_app_key_s, 1); if (!key) { @@ -1822,11 +1816,8 @@ API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key, } proc_lookup = __process_lookup(app_lookup->processes, &proc_key); - if (proc_lookup) { - if (STC_DEBUG_LOG) - STC_LOGD("proc_key already present"); //LCOV_EXCL_LINE + if (proc_lookup) return STC_ERROR_NONE; //LCOV_EXCL_LINE - } key = MALLOC0(stc_process_key_s, 1); if (!key) { diff --git a/src/stc-manager.c b/src/stc-manager.c index 00854ea..d83f079 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -26,6 +26,7 @@ #include "helper-cgroup.h" #include "helper-nfacct-rule.h" #include "helper-iptables.h" +#include "helper-inotify.h" #include "stc-monitor.h" #include "stc-firewall.h" #include "stc-manager-plugin-appstatus.h" @@ -36,7 +37,6 @@ static stc_s *g_stc = NULL; -/* static gboolean __validate_ident(const char *ident) { unsigned int i; @@ -50,7 +50,22 @@ static gboolean __validate_ident(const char *ident) return TRUE; } -*/ + +static void __stc_inotify_handler(struct inotify_event *event, const char *ident) +{ + if (!ident) + return; + + if (!__validate_ident(ident)) { + STC_LOGE("Invalid ident [%s]", ident); + return; + } + + if (!g_strcmp0(ident, INFO_CONFIG)) { + int debug = stc_util_get_config_int(INFO_DEBUGLOG); + stc_util_set_debuglog(debug); + } +} static void __stc_manager_deinit(void) { @@ -76,6 +91,9 @@ static void __stc_manager_deinit(void) stc_plugin_exception_deinit(); stc_plugin_procfs_deinit(); + inotify_deregister(INFO_STORAGE_DIR); + inotify_deinitialize(); + STC_LOGI("stc manager deinitialized"); FREE(g_stc); __STC_LOG_FUNC_EXIT__; @@ -96,6 +114,9 @@ static stc_s *__stc_manager_init(void) stc_util_initialize_config(); + inotify_initialize(); + inotify_register(INFO_STORAGE_DIR, __stc_inotify_handler); + cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT); EXEC(STC_ERROR_NONE, stc_db_initialize());