Shut down cleanly on SIGINT (Ctrl-C), for easier Valgrind runs.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 15 Apr 2013 23:31:24 +0000 (01:31 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 15 Apr 2013 23:31:24 +0000 (01:31 +0200)
main.cpp

index 0c58f41..b927237 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -30,10 +30,14 @@ using namespace std;
 
 ServerPool *servers = NULL;
 volatile bool hupped = false;
+volatile bool stopped = false;
 
-void hup(int ignored)
+void hup(int signum)
 {
        hupped = true;
+       if (signum == SIGINT) {
+               stopped = true;
+       }
 }
 
 CubemapStateProto collect_state(const timeval &serialize_start,
@@ -231,6 +235,7 @@ bool dry_run_config(const std::string &argv0, const std::string &config_filename
 int main(int argc, char **argv)
 {
        signal(SIGHUP, hup);
+       signal(SIGINT, hup);
        signal(SIGPIPE, SIG_IGN);
        
        // Parse options.
@@ -375,18 +380,28 @@ start:
        }
        servers->stop();
 
-       log(INFO, "Serializing state and re-execing...");
-       CubemapStateProto state = collect_state(
-                serialize_start, acceptors, inputs, servers);
-       string serialized;
-       state.SerializeToString(&serialized);
-       state_fd = make_tempfile(serialized);
-       if (state_fd == -1) {
-               exit(1);
+       CubemapStateProto state;
+       if (stopped) {
+               log(INFO, "Shutting down.");
+       } else {
+               log(INFO, "Serializing state and re-execing...");
+               state = collect_state(
+                       serialize_start, acceptors, inputs, servers);
+               string serialized;
+               state.SerializeToString(&serialized);
+               state_fd = make_tempfile(serialized);
+               if (state_fd == -1) {
+                       exit(1);
+               }
        }
        delete servers;
        shut_down_logging();
 
+       if (stopped) {
+               exit(0);
+       }
+
+       // OK, so the signal was SIGHUP. Check that the new config is okay, then exec the new binary.
        if (!dry_run_config(argv[0], config_filename)) {
                open_logs(config.log_destinations);
                log(ERROR, "%s --test-config failed. Restarting old version instead of new.", argv[0]);
@@ -394,7 +409,6 @@ start:
                shut_down_logging();
                goto start;
        }
-
         
        char buf[16];
        sprintf(buf, "%d", state_fd);