X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=main.cpp;h=b927237529034ec01984150434ad9d713886c5b7;hp=0c58f41d013d72a65a93a6224eaa93dc8fe4eb97;hb=e283929bce32fec5c08fc4344a9700f4406d2cc5;hpb=e666e2735bf831084d744027b844de9f4d3fe34d diff --git a/main.cpp b/main.cpp index 0c58f41..b927237 100644 --- 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);