X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=main.cpp;h=2c5ef67d90bb78b7c897040ce8d3710d6975c044;hp=4a5904b0715f1905817210138e3c9b7091329fae;hb=a313e4c7a3c0c453d4dffd317fc143f1f4a7f8ba;hpb=8cc780cf37063ce29f13380976a54dd8302fe3a9 diff --git a/main.cpp b/main.cpp index 4a5904b..2c5ef67 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -30,10 +31,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 +236,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. @@ -264,19 +270,43 @@ int main(int argc, char **argv) config_filename = argv[optind++]; } + // Canonicalize argv[0] and config_filename. + char argv0_canon[PATH_MAX]; + char config_filename_canon[PATH_MAX]; + + if (realpath(argv[0], argv0_canon) == NULL) { + log_perror("realpath"); + exit(1); + } + if (realpath(config_filename.c_str(), config_filename_canon) == NULL) { + log_perror("realpath"); + exit(1); + } + + // Now parse the configuration file. Config config; - if (!parse_config(config_filename, &config)) { + if (!parse_config(config_filename_canon, &config)) { exit(1); } if (test_config) { exit(0); } + + // Ideally we'd like to daemonize only when we've started up all threads etc., + // but daemon() forks, which is not good in multithreaded software, so we'll + // have to do it here. + if (config.daemonize) { + if (daemon(0, 0) == -1) { + log_perror("daemon"); + exit(1); + } + } start: // Open logs as soon as possible. open_logs(config.log_destinations); - log(NO_LEVEL, "Cubemap " SERVER_VERSION " starting."); + log(INFO, "Cubemap " SERVER_VERSION " starting."); servers = new ServerPool(config.num_servers); CubemapStateProto loaded_state; @@ -375,18 +405,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,16 +434,15 @@ start: shut_down_logging(); goto start; } - char buf[16]; sprintf(buf, "%d", state_fd); for ( ;; ) { - execlp(argv[0], argv[0], config_filename.c_str(), "--state", buf, NULL); + execlp(argv0_canon, argv0_canon, config_filename_canon, "--state", buf, NULL); open_logs(config.log_destinations); log_perror("execlp"); - log(ERROR, "re-exec of %s failed. Waiting 0.2 seconds and trying again...", argv[0]); + log(ERROR, "re-exec of %s failed. Waiting 0.2 seconds and trying again...", argv0_canon); shut_down_logging(); usleep(200000); }