Add callback in monitor plugin to stop stc-manager
[platform/core/connectivity/stc-manager.git] / src / stc-manager.c
old mode 100644 (file)
new mode 100755 (executable)
index d83f079..0011c97
@@ -18,7 +18,6 @@
 #include <errno.h>
 #include <sys/wait.h>
 #include "stc-manager.h"
-#include "stc-emulator.h"
 #include "stc-manager-gdbus.h"
 #include "stc-db.h"
 #include "counter.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"
 #include "stc-manager-plugin-exception.h"
 #include "stc-manager-plugin-procfs.h"
+#include "stc-manager-plugin-tether.h"
+#include "stc-manager-plugin-pcap.h"
+#include "stc-manager-plugin-monitor.h"
+#include "stc-manager-plugin-firewall.h"
 
 #define BUF_SIZE_FOR_ERR 100
 
 static stc_s *g_stc = NULL;
+static gboolean g_is_fail_exit = FALSE;
 
 static gboolean __validate_ident(const char *ident)
 {
@@ -61,10 +64,8 @@ static void __stc_inotify_handler(struct inotify_event *event, const char *ident
                return;
        }
 
-       if (!g_strcmp0(ident, INFO_CONFIG)) {
-               int debug = stc_util_get_config_int(INFO_DEBUGLOG);
-               stc_util_set_debuglog(debug);
-       }
+       if (!g_strcmp0(ident, INFO_CONFIG))
+               stc_util_update_log_state();
 }
 
 static void __stc_manager_deinit(void)
@@ -76,7 +77,6 @@ static void __stc_manager_deinit(void)
                return;
        }
 
-       stc_monitor_deinit();
        stc_deinit_db_guard();
        stc_db_deinitialize();
 
@@ -85,11 +85,13 @@ static void __stc_manager_deinit(void)
 
        stc_manager_gdbus_deinit((gpointer)g_stc);
 
-       stc_firewall_deinit();
-
        stc_plugin_appstatus_deinit();
        stc_plugin_exception_deinit();
        stc_plugin_procfs_deinit();
+       stc_plugin_tether_deinit();
+       stc_plugin_pcap_deinit();
+       stc_plugin_monitor_deinit();
+       stc_plugin_firewall_deinit();
 
        inotify_deregister(INFO_STORAGE_DIR);
        inotify_deinitialize();
@@ -99,6 +101,15 @@ static void __stc_manager_deinit(void)
        __STC_LOG_FUNC_EXIT__;
 }
 
+void __stc_manager_stop_with_fail(void)
+{
+       STC_LOGI("plugin needs stc-manager to exit");
+
+       g_is_fail_exit = TRUE;
+
+       stc_stop_manager();
+}
+
 static stc_s *__stc_manager_init(void)
 {
        __STC_LOG_FUNC_ENTER__;
@@ -119,32 +130,46 @@ static stc_s *__stc_manager_init(void)
 
        cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
 
-       EXEC(STC_ERROR_NONE, stc_db_initialize());
+       err = stc_db_initialize();
+       if (err != STC_ERROR_NONE) {
+               STC_LOGD("Failed to initialize stc db"); //LCOV_EXCL_LINE
+               return NULL; //LCOV_EXCL_LINE
+       }
+
+       g_stc->ondemand_mode = TRUE;
 
        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_tether_init();
+       if (stc_plugin_pcap_init() == STC_ERROR_NONE)
+               g_stc->ondemand_mode = FALSE;
+       if (stc_plugin_monitor_init(__stc_manager_stop_with_fail) == STC_ERROR_NONE)
+               g_stc->ondemand_mode = FALSE;
+       stc_plugin_firewall_init();
 
        stc_plugin_procfs_load_pid();
+
        stc_manager_gdbus_init((gpointer)stc);
 
        STC_LOGI("stc manager initialized");
        __STC_LOG_FUNC_EXIT__;
        return stc;
+}
 
-handle_error:
-       STC_LOGD("Failed to initialize stc manager"); //LCOV_EXCL_LINE
-       __stc_manager_deinit(); //LCOV_EXCL_LINE
-       return NULL; //LCOV_EXCL_LINE
+static gboolean __stc_timer_expired(gpointer data)
+{
+       if (g_stc->keep_alive) {
+               g_stc->keep_alive = FALSE;
+               return TRUE;
+       }
+
+       g_main_loop_quit(g_stc->main_loop);
+
+       return FALSE;
 }
 
-stc_s *stc_get_manager(void)
+API stc_s *stc_get_manager(void)
 {
        return g_stc;
 }
@@ -175,8 +200,15 @@ int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
 
        if (pid == 0) {
                errno = 0;
+
+               if (!g_strcmp0(args[1], STC_CMD_INSERT)) {
+                       STC_LOGE("Invalid arguments");
+                       g_strfreev(args);
+                       exit(-1);
+               }
+
                if (execv(args[0], args) == -1) {
-                       STC_LOGE("Failed to execute [%s]", err_str);
+                       STC_LOGE("Failed to execute [%s]", *err_str);
                        g_strfreev(args);
                        exit(-1);
                }
@@ -214,10 +246,14 @@ int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
        return STC_ERROR_FAIL;
 }
 
+void stc_set_keep_alive(gboolean keep_alive)
+{
+       g_stc->keep_alive = keep_alive;
+}
+
 gint32 main(gint32 argc, gchar *argv[])
 {
        GMainLoop *main_loop = NULL;
-       gint32 ret = -1;
 
        STC_LOGI("Smart Traffic Control Manager");
 
@@ -225,36 +261,43 @@ gint32 main(gint32 argc, gchar *argv[])
        setenv("GCOV_PREFIX", "/tmp/daemon", 1);
 #endif
 
+/*
        if (daemon(0, 0) != 0)
                STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE
+*/
 
        /* Initialize required subsystems */
 #if !GLIB_CHECK_VERSION(2, 35, 0)
        g_type_init();
 #endif
 
+       g_stc = __stc_manager_init();
+       if (!g_stc)
+               goto fail;
+
+       if (g_is_fail_exit == TRUE)
+               goto fail;
+
        /* Crate the GLIB main loop */
        main_loop = g_main_loop_new(NULL, FALSE);
+       g_stc->main_loop = main_loop;
 
-       stc_emulator_check_environment();
-       if (stc_emulator_is_emulated() == FALSE) {
-               g_stc = __stc_manager_init();
-               if (!g_stc)
-                       goto fail;
-               g_stc->main_loop = main_loop;
+       if (g_stc->ondemand_mode) {
+               g_stc->timer = g_timeout_add_seconds(10, __stc_timer_expired, NULL);
+               g_stc->keep_alive = FALSE;
        }
 
        /* Run the main loop */
        g_main_loop_run(main_loop);
 
-       ret = 0;
-
 fail:
-       if (stc_emulator_is_emulated() == FALSE)
-               __stc_manager_deinit();
+       __stc_manager_deinit();
 
        if (main_loop)
                g_main_loop_unref(main_loop);
 
-       return ret;
+       if (g_is_fail_exit == TRUE)
+               exit(-1);
+
+       return 0;
 }