X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=sidebyside;f=main.cpp;h=be3e8ebcce8e81ca462c34937b3068cdc12e24b1;hb=9abaf929b9da3158e9b7f799775ac56e88d28740;hp=75c36d8d6a409c025d67e65ba092b67a0ba85d91;hpb=867275636c6b2526f6626e503e8e72a21776370f;p=cubemap diff --git a/main.cpp b/main.cpp index 75c36d8..be3e8eb 100644 --- a/main.cpp +++ b/main.cpp @@ -16,6 +16,7 @@ #include #include +#include "accesslog.h" #include "acceptor.h" #include "config.h" #include "input.h" @@ -29,6 +30,7 @@ using namespace std; +AccessLogThread *access_log = NULL; ServerPool *servers = NULL; volatile bool hupped = false; volatile bool stopped = false; @@ -275,11 +277,11 @@ int main(int argc, char **argv) char config_filename_canon[PATH_MAX]; if (realpath(argv[0], argv0_canon) == NULL) { - log_perror("realpath"); + log_perror(argv[0]); exit(1); } if (realpath(config_filename.c_str(), config_filename_canon) == NULL) { - log_perror("realpath"); + log_perror(config_filename.c_str()); exit(1); } @@ -291,12 +293,30 @@ int main(int argc, char **argv) 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(INFO, "Cubemap " SERVER_VERSION " starting."); + if (config.access_log_file.empty()) { + // Create a dummy logger. + access_log = new AccessLogThread(); + } else { + access_log = new AccessLogThread(config.access_log_file); + } + access_log->run(); + servers = new ServerPool(config.num_servers); CubemapStateProto loaded_state; @@ -410,6 +430,8 @@ start: } } delete servers; + access_log->stop(); + delete access_log; shut_down_logging(); if (stopped) { @@ -417,7 +439,7 @@ start: } // 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)) { + if (!dry_run_config(argv0_canon, config_filename_canon)) { open_logs(config.log_destinations); log(ERROR, "%s --test-config failed. Restarting old version instead of new.", argv[0]); hupped = false;