X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fstc-manager.c;h=436ee29dbc56fa290394a2b9aecdc72d4047cc6a;hb=5b158c5dc9ad4538d48b9571d7954b990079d7da;hp=dc923ff43e2a84b3aeb0ada2e75636801d4513bb;hpb=0e4036f2ae7a28b65e7341eb67b3275ac15dab9a;p=platform%2Fcore%2Fconnectivity%2Fstc-manager.git diff --git a/src/stc-manager.c b/src/stc-manager.c old mode 100755 new mode 100644 index dc923ff..436ee29 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -15,21 +15,57 @@ */ #include +#include +#include #include "stc-manager.h" -#include "stc-emulator.h" -#include "stc-statistics.h" -#include "stc-restriction.h" #include "stc-manager-gdbus.h" #include "stc-db.h" #include "counter.h" #include "table-restrictions.h" #include "helper-cgroup.h" #include "helper-nfacct-rule.h" +#include "helper-iptables.h" +#include "helper-inotify.h" #include "stc-monitor.h" -#include "stc-manager-plugin.h" +#include "stc-firewall.h" +#include "stc-manager-plugin-appstatus.h" +#include "stc-manager-plugin-exception.h" +#include "stc-manager-plugin-procfs.h" + +#define BUF_SIZE_FOR_ERR 100 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 (!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) { __STC_LOG_FUNC_ENTER__; @@ -42,8 +78,20 @@ static void __stc_manager_deinit(void) stc_monitor_deinit(); stc_deinit_db_guard(); stc_db_deinitialize(); + + iptables_flush_chains(); + iptables_deinit(); + stc_manager_gdbus_deinit((gpointer)g_stc); - stc_manager_plugin_deinit(); + + stc_firewall_deinit(); + + stc_plugin_appstatus_deinit(); + stc_plugin_exception_deinit(); + stc_plugin_procfs_deinit(); + + inotify_deregister(INFO_STORAGE_DIR); + inotify_deinitialize(); STC_LOGI("stc manager deinitialized"); FREE(g_stc); @@ -58,30 +106,41 @@ static stc_s *__stc_manager_init(void) stc = MALLOC0(stc_s, 1); if (!stc) { - STC_LOGE("Failed to allocate memory for manager structure"); - return NULL; + STC_LOGE("Failed to allocate memory for manager structure"); //LCOV_EXCL_LINE + return NULL; //LCOV_EXCL_LINE } g_stc = stc; + 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()); + stc_plugin_appstatus_init(); + stc_plugin_exception_init(); + stc_plugin_procfs_init(); + + stc_firewall_init(); + err = stc_monitor_init(); if (err != STC_ERROR_NONE) goto handle_error; + stc_plugin_procfs_load_pid(); stc_manager_gdbus_init((gpointer)stc); - stc_manager_plugin_init(); STC_LOGI("stc manager initialized"); __STC_LOG_FUNC_EXIT__; return stc; handle_error: - STC_LOGD("Failed to initialize stc manager"); - __stc_manager_deinit(); - return NULL; + STC_LOGD("Failed to initialize stc manager"); //LCOV_EXCL_LINE + __stc_manager_deinit(); //LCOV_EXCL_LINE + return NULL; //LCOV_EXCL_LINE } stc_s *stc_get_manager(void) @@ -89,6 +148,71 @@ stc_s *stc_get_manager(void) return g_stc; } +void stc_stop_manager(void) +{ + if (g_stc && g_stc->main_loop) + g_main_loop_quit(g_stc->main_loop); +} + +int stc_commit_iptables(char *cmd, int *err_num, char **err_str) +{ + pid_t pid = 0; + int status = 0; + int ret = 0; + char err_buf[BUF_SIZE_FOR_ERR] = { 0, }; + gchar **args = NULL; + + if (cmd == NULL) { + STC_LOGE("Invalid arguments"); + return STC_ERROR_INVALID_PARAMETER; + } + + args = g_strsplit_set(cmd, " ", -1); + + errno = 0; + pid = fork(); + + if (pid == 0) { + errno = 0; + if (execv(args[0], args) == -1) { + STC_LOGE("Failed to execute [%s]", *err_str); + g_strfreev(args); + exit(-1); + } + } else if (pid > 0) { + if (waitpid(pid, &status, 0) == -1) + STC_LOGD("wait pid [%u] status [%d] ", pid, status); + + if (WIFEXITED(status)) { + ret = WEXITSTATUS(status); + STC_LOGD("exited, status [%d]", status); + } else if (WIFSIGNALED(status)) { + STC_LOGD("killed by signal [%d]", WTERMSIG(status)); + } else if (WIFSTOPPED(status)) { + STC_LOGD("stopped by signal [%d]", WSTOPSIG(status)); + } else if (WIFCONTINUED(status)) { + STC_LOGD("continued"); + } + + *err_num = ret; + *err_str = strerror_r(ret, err_buf, BUF_SIZE_FOR_ERR); + STC_LOGD("return err_num [%d] err_str [%s]", *err_num, *err_str); + + g_strfreev(args); + if (ret == 0) + return STC_ERROR_NONE; + else + return STC_ERROR_FAIL; + } + + *err_num = errno; + *err_str = strerror_r(errno, err_buf, BUF_SIZE_FOR_ERR); + STC_LOGD("Failed to fork [%d:%s]", *err_num, *err_str); + + g_strfreev(args); + return STC_ERROR_FAIL; +} + gint32 main(gint32 argc, gchar *argv[]) { GMainLoop *main_loop = NULL; @@ -96,8 +220,12 @@ gint32 main(gint32 argc, gchar *argv[]) STC_LOGI("Smart Traffic Control Manager"); +#ifdef TIZEN_GTESTS + setenv("GCOV_PREFIX", "/tmp/daemon", 1); +#endif + if (daemon(0, 0) != 0) - STC_LOGE("Can't start daemon"); + STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE /* Initialize required subsystems */ #if !GLIB_CHECK_VERSION(2, 35, 0) @@ -107,13 +235,11 @@ gint32 main(gint32 argc, gchar *argv[]) /* Crate the GLIB main loop */ main_loop = g_main_loop_new(NULL, FALSE); - stc_emulator_check_environment(); - if (stc_emulator_is_emulated() == FALSE) { - g_stc = __stc_manager_init(); - if (!g_stc) - goto fail; - g_stc->main_loop = main_loop; - } + g_stc = __stc_manager_init(); + if (!g_stc) + goto fail; + + g_stc->main_loop = main_loop; /* Run the main loop */ g_main_loop_run(main_loop); @@ -121,8 +247,7 @@ gint32 main(gint32 argc, gchar *argv[]) ret = 0; fail: - if (stc_emulator_is_emulated() == FALSE) - __stc_manager_deinit(); + __stc_manager_deinit(); if (main_loop) g_main_loop_unref(main_loop);