Change to operate in on-demand mode 53/201753/2 submit/tizen/20190509.073354
authorhyunuktak <hyunuk.tak@samsung.com>
Tue, 19 Mar 2019 07:21:14 +0000 (16:21 +0900)
committerhyunuktak <hyunuk.tak@samsung.com>
Wed, 20 Mar 2019 08:35:41 +0000 (17:35 +0900)
Change-Id: Iea3e5df460e906e6790d66f65e9db338667a1c13
Signed-off-by: hyunuktak <hyunuk.tak@samsung.com>
data/exceptions
include/stc-manager.h
packaging/stc-manager.spec
resources/dbus/net.stc-manager.service [new file with mode: 0755]
resources/systemd/stc-manager.service
src/stc-firewall.c
src/stc-manager-gdbus.c
src/stc-manager.c
src/stc-pcap.c
src/stc-restriction.c
src/stc-statistics.c

index ef89dd3..c3d4cbe 100644 (file)
@@ -28,6 +28,7 @@ ps:inst
 wlan.sh:script
 wpa_supp.sh:script
 stc-manager:sys
+stc-iptables:sys
 dlogutil:sys
 wifi-loader:sys
 wrt-loader:sys
index 82d8747..bc2047f 100755 (executable)
@@ -236,6 +236,9 @@ typedef struct {
 
 typedef struct {
        GMainLoop *main_loop;
+       guint timer;
+       gboolean keep_alive;
+       gboolean ondemand_mode;
 
        gpointer statistics_obj;
        gpointer restriction_obj;
@@ -254,5 +257,6 @@ typedef struct {
 stc_s *stc_get_manager(void);
 void stc_stop_manager(void);
 int stc_commit_iptables(char *cmd, int *err_num, char **err_str);
+void stc_set_keep_alive(gboolean keep_alive);
 
 #endif /* __STC_MANAGER__ */
index 11549fe..b86122b 100644 (file)
@@ -136,6 +136,9 @@ ln -s ../stc-manager.service %{buildroot}%{_libdir}/systemd/system/multi-user.ta
 mkdir -p %{buildroot}%{_sysconfdir}/dbus-1/system.d
 cp resources/dbus/stc-manager.conf %{buildroot}%{_sysconfdir}/dbus-1/system.d/stc-manager.conf
 
+mkdir -p %{buildroot}%{_datadir}/dbus-1/system-services/
+cp resources/dbus/net.stc-manager.service %{buildroot}%{_datadir}/dbus-1/system-services/net.stc-manager.service
+
 %files
 %manifest %{name}.manifest
 %license LICENSE
@@ -150,6 +153,7 @@ cp resources/dbus/stc-manager.conf %{buildroot}%{_sysconfdir}/dbus-1/system.d/st
 
 #DBus DAC
 %attr(644,root,root) %{_sysconfdir}/dbus-1/system.d/*
+%attr(644,root,root) %{_datadir}/dbus-1/system-services/*
 
 %if %{?enable_database} == YES
 %config(noreplace) %attr(660, root, root) %{database_full_path}
diff --git a/resources/dbus/net.stc-manager.service b/resources/dbus/net.stc-manager.service
new file mode 100755 (executable)
index 0000000..13f4097
--- /dev/null
@@ -0,0 +1,5 @@
+[D-BUS Service]
+Name=net.stc
+Exec=/bin/false
+User=root
+SystemdService=stc-manager.service
index 70a9eef..e3bce9f 100644 (file)
@@ -4,10 +4,10 @@ Requires=dbus.socket
 After=dbus.socket
 
 [Service]
-Type=forking
+Type=dbus
+BusName=net.stc
 SmackProcessLabel=System
 ExecStart=/usr/bin/stc-manager
-Restart=always
 
 [Install]
 WantedBy=multi-user.target
index bc71cc9..c13d406 100755 (executable)
@@ -47,6 +47,8 @@ gboolean handle_firewall_lock(StcFirewall *object,
 {
        __STC_LOG_FUNC_ENTER__;
 
+       stc_set_keep_alive(TRUE);
+
        stc_plugin_firewall_lock();
 
        STC_DBUS_REPLY_ERROR_NONE(invocation);
@@ -60,6 +62,8 @@ gboolean handle_firewall_unlock(StcFirewall *object,
 {
        __STC_LOG_FUNC_ENTER__;
 
+       stc_set_keep_alive(TRUE);
+
        stc_plugin_firewall_unlock();
 
        STC_DBUS_REPLY_ERROR_NONE(invocation);
@@ -75,6 +79,8 @@ gboolean handle_firewall_get_lock(StcFirewall *object,
        GVariant *return_parameters = NULL;
        int state = 0;
 
+       stc_set_keep_alive(TRUE);
+
        stc_plugin_firewall_get_lock(&state);
 
        return_parameters = g_variant_new("(i)", state);
@@ -91,6 +97,8 @@ gboolean handle_firewall_add_chain(StcFirewall *object,
        __STC_LOG_FUNC_ENTER__;
        int ret = STC_ERROR_NONE;
 
+       stc_set_keep_alive(TRUE);
+
        if (chain == NULL) {
                STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
                                                STC_ERROR_INVALID_PARAMETER);
@@ -118,6 +126,8 @@ gboolean handle_firewall_remove_chain(StcFirewall *object,
        __STC_LOG_FUNC_ENTER__;
        int ret = STC_ERROR_NONE;
 
+       stc_set_keep_alive(TRUE);
+
        if (chain == NULL) {
                STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
                                                STC_ERROR_INVALID_PARAMETER);
@@ -145,6 +155,8 @@ gboolean handle_firewall_flush_chain(StcFirewall *object,
        __STC_LOG_FUNC_ENTER__;
        int ret = STC_ERROR_NONE;
 
+       stc_set_keep_alive(TRUE);
+
        if (chain == NULL) {
                STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
                                                STC_ERROR_INVALID_PARAMETER);
@@ -172,6 +184,8 @@ gboolean handle_firewall_get_all_chain(StcFirewall *object,
        GVariantBuilder *builder = NULL;
        GVariant *return_parameters = NULL;
 
+       stc_set_keep_alive(TRUE);
+
        builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
 
        stc_plugin_firewall_get_all_chain(builder);
@@ -194,6 +208,8 @@ gboolean handle_firewall_set_chain(StcFirewall *object,
        __STC_LOG_FUNC_ENTER__;
        int ret = STC_ERROR_NONE;
 
+       stc_set_keep_alive(TRUE);
+
        if (chain == NULL ||
                target >= STC_FW_CHAIN_TARGET_MAX) {
                STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
@@ -222,6 +238,8 @@ gboolean handle_firewall_unset_chain(StcFirewall *object,
        __STC_LOG_FUNC_ENTER__;
        int ret = STC_ERROR_NONE;
 
+       stc_set_keep_alive(TRUE);
+
        if (chain == NULL) {
                STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
                                                STC_ERROR_INVALID_PARAMETER);
@@ -249,6 +267,8 @@ gboolean handle_firewall_add_rule(StcFirewall *object,
        __STC_LOG_FUNC_ENTER__;
        int ret = STC_ERROR_NONE;
 
+       stc_set_keep_alive(TRUE);
+
        ret = stc_plugin_firewall_add_rule(parameters);
        if (ret != STC_ERROR_NONE) {
                STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
@@ -269,6 +289,8 @@ gboolean handle_firewall_remove_rule(StcFirewall *object,
        __STC_LOG_FUNC_ENTER__;
        int ret = STC_ERROR_NONE;
 
+       stc_set_keep_alive(TRUE);
+
        ret = stc_plugin_firewall_remove_rule(parameters);
        if (ret != STC_ERROR_NONE) {
                STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
@@ -289,6 +311,8 @@ gboolean handle_firewall_update_rule(StcFirewall *object,
        __STC_LOG_FUNC_ENTER__;
        int ret = STC_ERROR_NONE;
 
+       stc_set_keep_alive(TRUE);
+
        ret = stc_plugin_firewall_update_rule(parameters);
        if (ret != STC_ERROR_NONE) {
                STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
@@ -309,6 +333,8 @@ gboolean handle_firewall_get_all_rule(StcFirewall *object,
        GVariantBuilder *builder = NULL;
        GVariant *return_parameters = NULL;
 
+       stc_set_keep_alive(TRUE);
+
        builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
 
        stc_plugin_firewall_get_all_rule(builder);
index 7990287..943f466 100755 (executable)
@@ -568,6 +568,8 @@ gboolean handle_manager_stop(StcManager *object,
        __STC_LOG_FUNC_ENTER__;
        GVariant *return_parameters = NULL;
 
+       stc_set_keep_alive(TRUE);
+
        STC_LOGI("stc manager stop");
 
        return_parameters = g_variant_new("(i)", STC_ERROR_NONE);
@@ -593,6 +595,8 @@ gboolean handle_manager_commit_iptables(StcManager *object,
        char *err_str = NULL;
        char cmd[STC_CMD_SIZE] = { 0, };
 
+       stc_set_keep_alive(TRUE);
+
        if (option == NULL) {
                STC_MANAGER_DBUS_REPLY_ERROR(invocation,
                                                 STC_ERROR_INVALID_PARAMETER);
@@ -626,6 +630,8 @@ gboolean handle_manager_commit_ip6tables(StcManager *object,
        char *err_str = NULL;
        char cmd[STC_CMD_SIZE] = { 0, };
 
+       stc_set_keep_alive(TRUE);
+
        if (option == NULL) {
                STC_MANAGER_DBUS_REPLY_ERROR(invocation,
                                                 STC_ERROR_INVALID_PARAMETER);
index 5e5bcef..a376e7f 100755 (executable)
@@ -126,12 +126,16 @@ static stc_s *__stc_manager_init(void)
                return NULL; //LCOV_EXCL_LINE
        }
 
+       g_stc->ondemand_mode = TRUE;
+
        stc_plugin_appstatus_init();
        stc_plugin_exception_init();
        stc_plugin_procfs_init();
        stc_plugin_tether_init();
-       stc_plugin_pcap_init();
-       stc_plugin_monitor_init();
+       if (stc_plugin_pcap_init() == STC_ERROR_NONE)
+               g_stc->ondemand_mode = FALSE;
+       if (stc_plugin_monitor_init() == STC_ERROR_NONE)
+               g_stc->ondemand_mode = FALSE;
        stc_plugin_firewall_init();
 
        stc_plugin_procfs_load_pid();
@@ -143,6 +147,18 @@ static stc_s *__stc_manager_init(void)
        return stc;
 }
 
+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;
+}
+
 API stc_s *stc_get_manager(void)
 {
        return g_stc;
@@ -220,10 +236,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");
 
@@ -231,33 +251,37 @@ 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
 
-       /* Crate the GLIB main loop */
-       main_loop = g_main_loop_new(NULL, FALSE);
-
        g_stc = __stc_manager_init();
        if (!g_stc)
                goto fail;
 
+       /* Crate the GLIB main loop */
+       main_loop = g_main_loop_new(NULL, FALSE);
        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:
        __stc_manager_deinit();
 
        if (main_loop)
                g_main_loop_unref(main_loop);
 
-       return ret;
+       return 0;
 }
index 91dd1e2..3faa7ec 100755 (executable)
@@ -91,6 +91,8 @@ gboolean handle_pcap_start(StcPcap *object,
        stc_pcap_s pcap;
        int ret = STC_ERROR_NONE;
 
+       stc_set_keep_alive(TRUE);
+
        memset(&pcap, 0, sizeof(stc_pcap_s));
 
        g_variant_get(parameters, "a{sv}", &iter);
@@ -130,6 +132,8 @@ gboolean handle_pcap_stop(StcPcap *object,
        stc_pcap_s pcap;
        int ret = STC_ERROR_NONE;
 
+       stc_set_keep_alive(TRUE);
+
        memset(&pcap, 0, sizeof(stc_pcap_s));
 
        g_variant_get(parameters, "a{sv}", &iter);
@@ -167,6 +171,8 @@ gboolean handle_pcap_get_all(StcPcap *object,
        GVariantBuilder *builder = NULL;
        GVariant *return_parameters = NULL;
 
+       stc_set_keep_alive(TRUE);
+
        builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
 
        stc_plugin_pcap_get_all_loop(builder);
@@ -188,6 +194,8 @@ gboolean handle_pcap_find_all_devs(StcPcap *object,
        GVariantBuilder *builder = NULL;
        GVariant *return_parameters = NULL;
 
+       stc_set_keep_alive(TRUE);
+
        builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
 
        stc_plugin_pcap_find_all_devs(builder);
index d49eb42..d6bae55 100755 (executable)
@@ -269,6 +269,8 @@ gboolean handle_restriction_set(StcRestriction *object,
        table_restrictions_info rule;
        stc_error_e ret = STC_ERROR_NONE;
 
+       stc_set_keep_alive(TRUE);
+
        memset(&rule, 0, sizeof(table_restrictions_info));
        __initialize_rstn_rule(&rule);
 
@@ -311,6 +313,8 @@ gboolean handle_restriction_unset(StcRestriction *object,
        table_restrictions_info rule;
        stc_error_e ret = STC_ERROR_NONE;
 
+       stc_set_keep_alive(TRUE);
+
        memset(&rule, 0, sizeof(table_restrictions_info));
        __initialize_rstn_rule(&rule);
 
@@ -354,6 +358,8 @@ gboolean handle_restriction_set_list(StcRestriction *object,
        GVariantIter *iter_row = NULL;
        stc_error_e ret = STC_ERROR_NONE;
 
+       stc_set_keep_alive(TRUE);
+
        g_variant_get(parameters, "aa{sv}", &iter);
        while (g_variant_iter_next(iter, "a{sv}", &iter_row)) {
                table_restrictions_info rule;
@@ -403,6 +409,8 @@ gboolean handle_restriction_unset_list(StcRestriction *object,
        GVariantIter *iter_row = NULL;
        stc_error_e ret = STC_ERROR_NONE;
 
+       stc_set_keep_alive(TRUE);
+
        g_variant_get(parameters, "aa{sv}", &iter);
        while (g_variant_iter_next(iter, "a{sv}", &iter_row)) {
                table_restrictions_info rule;
@@ -453,6 +461,8 @@ gboolean handle_restriction_get(StcRestriction *object,
        GVariant *return_parameters = NULL;
        stc_error_e ret;
 
+       stc_set_keep_alive(TRUE);
+
        builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
 
        ret = table_restrictions_per_app(app_id,
@@ -483,6 +493,8 @@ gboolean handle_restriction_get_all(StcRestriction *object,
        GVariant *return_parameters = NULL;
        stc_error_e ret;
 
+       stc_set_keep_alive(TRUE);
+
        builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
 
        ret = table_restrictions_foreach(__table_restrictions_foreach_app_cb,
@@ -514,6 +526,8 @@ gboolean handle_restriction_get_type(StcRestriction *object,
        stc_rstn_type_e type = STC_RSTN_TYPE_UNKNOWN;
        stc_error_e ret;
 
+       stc_set_keep_alive(TRUE);
+
        ret = table_restrictions_get_restriction_type(app_id, iftype, &type);
        if (ret < STC_ERROR_NONE) {
                STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
index f2ff8e9..0d5a761 100755 (executable)
@@ -247,6 +247,8 @@ gboolean handle_statistics_get_all(StcStatistics *object,
        GVariant *return_parameters = NULL;
        stc_error_e ret;
 
+       stc_set_keep_alive(TRUE);
+
        /* Total statistics since epoch */
        rule.from = epoch;
        rule.to = cur_time;
@@ -294,6 +296,8 @@ gboolean handle_statistics_init(StcStatistics *object,
 {
        __STC_LOG_FUNC_ENTER__;
 
+       stc_set_keep_alive(TRUE);
+
        STC_LOGI("stc statistics initialized");
        stc_statistics_complete_init(object, invocation);
 
@@ -315,6 +319,8 @@ gboolean handle_statistics_get(StcStatistics *object,
        GVariant *return_parameters = NULL;
        stc_error_e ret;
 
+       stc_set_keep_alive(TRUE);
+
        /* Total statistics since epoch */
        rule.from = epoch;
        rule.to = cur_time;
@@ -367,6 +373,8 @@ gboolean handle_statistics_reset(StcStatistics *object,
        GVariant *return_parameters = NULL;
        stc_error_e ret;
 
+       stc_set_keep_alive(TRUE);
+
        if (reset_rule != NULL) {
                DEBUG_GDBUS_VARIANT("Selection rule: ", reset_rule);
                GVariantIter *iter = NULL;