]> git.sesse.net Git - cubemap/blobdiff - main.cpp
Do not segfault on unknown options.
[cubemap] / main.cpp
index b927237529034ec01984150434ad9d713886c5b7..e953a8cea29d79da4579adb06a3580e989598a28 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -1,6 +1,7 @@
 #include <assert.h>
 #include <errno.h>
 #include <getopt.h>
+#include <limits.h>
 #include <signal.h>
 #include <stddef.h>
 #include <stdio.h>
@@ -15,6 +16,7 @@
 #include <utility>
 #include <vector>
 
+#include "accesslog.h"
 #include "acceptor.h"
 #include "config.h"
 #include "input.h"
@@ -28,7 +30,9 @@
 
 using namespace std;
 
+AccessLogThread *access_log = NULL;
 ServerPool *servers = NULL;
+vector<MarkPool *> mark_pools;
 volatile bool hupped = false;
 volatile bool stopped = false;
 
@@ -139,7 +143,6 @@ void create_streams(const Config &config,
                     const set<string> &deserialized_stream_ids,
                    map<string, Input *> *deserialized_inputs)
 {
-       vector<MarkPool *> mark_pools;  // FIXME: leak
        for (unsigned i = 0; i < config.mark_pools.size(); ++i) {
                const MarkPoolConfig &mp_config = config.mark_pools[i];
                mark_pools.push_back(new MarkPool(mp_config.from, mp_config.to));
@@ -245,9 +248,10 @@ int main(int argc, char **argv)
                static const option long_options[] = {
                        { "state", required_argument, 0, 's' },
                        { "test-config", no_argument, 0, 't' },
+                       { 0, 0, 0, 0 }
                };
                int option_index = 0;
-               int c = getopt_long (argc, argv, "s:t", long_options, &option_index);
+               int c = getopt_long(argc, argv, "s:t", long_options, &option_index);
      
                if (c == -1) {
                        break;
@@ -260,7 +264,8 @@ int main(int argc, char **argv)
                        test_config = true;
                        break;
                default:
-                       assert(false);
+                       fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
+                       exit(1);
                }
        }
 
@@ -269,19 +274,51 @@ 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(argv[0]);
+               exit(1);
+       }
+       if (realpath(config_filename.c_str(), config_filename_canon) == NULL) {
+               log_perror(config_filename.c_str());
+               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(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;
@@ -371,6 +408,7 @@ start:
 
        if (stats_thread != NULL) {
                stats_thread->stop();
+               delete stats_thread;
        }
        for (size_t i = 0; i < acceptors.size(); ++i) {
                acceptors[i]->stop();
@@ -395,6 +433,14 @@ start:
                }
        }
        delete servers;
+
+       for (unsigned i = 0; i < mark_pools.size(); ++i) {
+               delete mark_pools[i];
+       }
+       mark_pools.clear();
+
+       access_log->stop();
+       delete access_log;
        shut_down_logging();
 
        if (stopped) {
@@ -402,7 +448,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;
@@ -414,10 +460,10 @@ start:
        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);
        }