Fix 'occurred' typo
[bpdyndnsd] / src / main.cpp
index 5eb040d..4fcf1ed 100644 (file)
 using namespace std;
 
 Updater::Ptr updater;
-volatile bool need_config_reload = false;
-volatile bool exit_now = false;
-
+volatile bool caught_sig_hup = false;
 volatile bool caught_sig_usr1 = false;
 volatile bool caught_sig_usr2 = false;
-volatile bool caught_sig_rtmin1 = false;
+volatile bool caught_sig_rtmin = false;
+volatile bool caught_sig_rtmax = false;
+volatile bool caught_sig_term = false;
 
 /**
  * Checks if a bpdyndnsd process is already running.
@@ -136,53 +136,62 @@ int shutdown()
 
 
 /**
- * Signal SIGTERM caught, releasing resources and exit.
+ * Signal SIGTERM caught (exit program)
  * @param param Parameter from the signal interface.
  */
-void terminate(int param)
+void sigterm_func(int param)
 {
-    updater->get_logger()->print_caught_sigterm();
-    exit_now = true;
+    caught_sig_term = true;
 } /*lint !e715 */
 
 
 /**
- * Signal SIGUSR1 caught, switching to offline mode.
+ * Signal SIGUSR1 caught (switch to offline mode)
  * @param param Parameter from the signal interface.
  */
-void switch_to_offline(int param)
+void sigusr1_func(int param)
 {
     caught_sig_usr1 = true;
 } /*lint !e715 */
 
 
 /**
- * Signal SIGHUP caught, reloading config.
+ * Signal SIGHUP caught (reload config and reset log level)
  * @param param Parameter from the signal interface.
  */
-void reload_config(int param)
+void sighup_func(int param)
 {
-    need_config_reload = true;
+    caught_sig_hup = true;
 } /*lint !e715 */
 
 
 /**
- * Signal SIGUSR2 caught, switching to online mode.
+ * Signal SIGUSR2 caught (switch to online mode)
  * @param param Parameter from the signal interface.
  */
-void switch_to_online(int param)
+void sigusr2_func(int param)
 {
     caught_sig_usr2 = true;
 } /*lint !e715 */
 
 
 /**
- * Signal SIGRTMIN caught, switching to online mode with webcheck enabled.
+ * Signal SIGRTMIN caught (switch to online mode with webcheck enabled)
+ * @param param Parameter from the signal interface.
+ */
+void sigrtmin_func(int param)
+{
+    caught_sig_rtmin = true;
+} /*lint !e715 */
+
+
+/**
+ * Signal SIGRTMAX caught (increase log level)
  * @param param Parameter from the signal interface.
  */
-void switch_to_online_webcheck(int param)
+void sigrtmax_func(int param)
 {
-    caught_sig_rtmin1 = true;
+    caught_sig_rtmax = true;
 } /*lint !e715 */
 
 
@@ -193,36 +202,42 @@ void switch_to_online_webcheck(int param)
 int init_signals()
 {
     sighandler_t ret_val;
-    ret_val = signal(SIGTERM,terminate);
+    ret_val = signal(SIGTERM, sigterm_func);
     if ( ret_val == SIG_ERR )
     {
         updater->get_logger()->print_error_setting_signal("SIGTERM");
         return -1;
     }
-    ret_val = signal(SIGUSR1,switch_to_offline);
+    ret_val = signal(SIGUSR1, sigusr1_func);
     if ( ret_val == SIG_ERR )
     {
         updater->get_logger()->print_error_setting_signal("SIGUSR1");
         return -1;
     }
-    ret_val = signal(SIGHUP,reload_config);
+    ret_val = signal(SIGHUP, sighup_func);
     if ( ret_val == SIG_ERR )
     {
         updater->get_logger()->print_error_setting_signal("SIGHUP");
         return -1;
     }
-    ret_val = signal(SIGUSR2,switch_to_online);
+    ret_val = signal(SIGUSR2, sigusr2_func);
     if ( ret_val == SIG_ERR )
     {
         updater->get_logger()->print_error_setting_signal("SIGUSR2");
         return -1;
     }
-    ret_val = signal(SIGRTMIN,switch_to_online_webcheck);
+    ret_val = signal(SIGRTMIN, sigrtmin_func);
     if ( ret_val == SIG_ERR )
     {
         updater->get_logger()->print_error_setting_signal("SIGRTMIN");
         return -1;
     }
+    ret_val = signal(SIGRTMAX, sigrtmax_func);
+    if ( ret_val == SIG_ERR )
+    {
+        updater->get_logger()->print_error_setting_signal("SIGRTMAX");
+        return -1;
+    }
 
     return 0;
 }
@@ -278,6 +293,18 @@ int init_daemon_mode(bool daemon_mode)
 }
 
 /**
+ * Calculate how long the dialup mode burst period will last (dialup mode only)
+ * @return Timestamp until dialup period lasts or zero if dialup mode is disabled
+*/
+time_t calc_dialup_mode_burst_period()
+{
+    if (updater->get_config()->get_dialup_mode() )
+        return time(NULL) + updater->get_config()->get_dialup_burst_period_seconds();
+
+    return 0;
+}
+
+/**
  * @brief The main part.
  * @param argc Number of arguments
  * @param argv Command line arguments
@@ -305,13 +332,24 @@ int main(int argc, char *argv[])
         return -1;
 
     // Should we start in offline mode?
+    bool old_online_state = false;
     bool is_online = !updater->get_config()->get_start_offline();
     bool webcheck_enabled = updater->get_config()->get_webcheck_enabled();
+    bool exit_now = false;
 
     // One shot run if daemon mode is disabled
     if (updater->get_config()->get_daemon_mode() != 1)
         exit_now = true;
 
+    // Tell the world we are running
+    updater->get_logger()->print_started();
+
+    int original_log_level = updater->get_logger()->get_loglevel();
+
+    // Dialup mode: Sleep a specified number of seconds between network traffic
+    time_t dialup_mode_burst_mode_until = calc_dialup_mode_burst_period();
+    time_t dialup_mode_sleep_until = 0;
+
     // service processing starts here
     do
     {
@@ -331,14 +369,46 @@ int main(int argc, char *argv[])
             // Go online
             is_online = true;
             webcheck_enabled = false;
-        } else if (caught_sig_rtmin1)
+            dialup_mode_burst_mode_until = calc_dialup_mode_burst_period();
+        } else if (caught_sig_rtmin)
         {
-            caught_sig_rtmin1 = false;
+            caught_sig_rtmin = false;
             updater->get_logger()->print_caught_sigrtmin();
 
             // Go online - with webcheck
             is_online = true;
             webcheck_enabled = true;
+            dialup_mode_burst_mode_until = calc_dialup_mode_burst_period();
+        } else if (caught_sig_rtmax)
+        {
+            caught_sig_rtmax = false;
+
+            // Increase log level
+            int new_loglevel = updater->get_logger()->get_loglevel() + 1;
+            updater->get_logger()->print_caught_sigrtmax(new_loglevel);
+            updater->get_logger()->set_loglevel(new_loglevel);
+        } else if (caught_sig_term)
+        {
+            caught_sig_term = false;
+            updater->get_logger()->print_caught_sigterm();
+
+            exit_now = true;
+        } else if (caught_sig_hup)
+        {
+            caught_sig_hup = false;
+            updater->get_logger()->print_caught_sighup();
+
+            if ( updater->reload_config() != 0 )
+            {
+                updater->get_logger()->print_conf_reload_failed_exit();
+                exit(-1);
+            }
+
+            // Reset log level to startup log level in case it got elevated by SIGRTMAX
+            updater->get_logger()->set_loglevel(original_log_level);
+
+            // Reset dialup mode burst period on config reload
+            dialup_mode_burst_mode_until = calc_dialup_mode_burst_period();
         }
 
         // State handling
@@ -348,8 +418,16 @@ int main(int argc, char *argv[])
             if ( updater->get_config()->get_webcheck_enabled() != webcheck_enabled )    /*lint !e731 */
                 updater->get_config()->set_webcheck_enabled(webcheck_enabled);
 
-            // update all configured services
-            updater->update_services();
+            if (updater->get_config()->get_dialup_mode() == false ||
+                (time(NULL) > dialup_mode_sleep_until || time(NULL) < dialup_mode_burst_mode_until) )
+            {
+                // update all configured services
+                updater->update_services(is_online != old_online_state);
+
+                // Refresh sleep state (dialup mode only)
+                dialup_mode_sleep_until = time(NULL) + updater->get_config()->get_dialup_sleep_seconds();
+            } else
+                updater->get_logger()->print_sleep_dialup_mode(dialup_mode_sleep_until);
         }
         else
         {
@@ -357,17 +435,9 @@ int main(int argc, char *argv[])
             updater->get_logger()->print_offline_mode();
         }
 
-        if (need_config_reload)
-        {
-            updater->get_logger()->print_caught_sighup();
-            need_config_reload = false;
-
-            if ( updater->reload_config() != 0 )
-            {
-                updater->get_logger()->print_conf_reload_failed_exit();
-                exit(-1);
-            }
-        }
+        // Keep old online state so we can determine
+        // if we switched from offline to online
+        old_online_state = is_online;
 
         // Snore, snore... don't hog the cpu if we are in daemon_mode.
         if ( !exit_now )