]> git.sesse.net Git - cubemap/commitdiff
Support daemonizing.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 16 Apr 2013 18:01:26 +0000 (20:01 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 16 Apr 2013 18:01:26 +0000 (20:01 +0200)
config.cpp
config.h
cubemap.config.sample
main.cpp

index 660090d4a237b3c1fb22cf9793988838ed47ae72..77acf6dd61cac95c6a4792446a42bc99ff6def29 100644 (file)
@@ -269,6 +269,8 @@ bool parse_config(const string &filename, Config *config)
                return false;
        }
 
+       config->daemonize = false;
+
        if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
                log(ERROR, "Missing 'num_servers' statement in config file.");
                return false;
@@ -304,6 +306,8 @@ bool parse_config(const string &filename, Config *config)
                        if (!parse_error_log(line, config)) {
                                return false;
                        }
+               } else if (line.keyword == "daemonize") {
+                       config->daemonize = true;
                } else {
                        log(ERROR, "Unknown configuration keyword '%s'.",
                                line.keyword.c_str());
index bed0551c3e3de4709963dc0fa305b3143dfd7362..eee2e1b12e600829bde2688747b765c7fb434909 100644 (file)
--- a/config.h
+++ b/config.h
@@ -28,6 +28,7 @@ struct LogConfig {
 };
 
 struct Config {
+       bool daemonize;
        int num_servers;
        std::vector<MarkPoolConfig> mark_pools;
        std::vector<StreamConfig> streams;
index 1a691ee3ccf2482f98f52ff427de3ffcfc5ec62d..c639cd95db9cb3a44dcb8323635cfc9b0b0f8ca7 100644 (file)
@@ -1,3 +1,6 @@
+# uncomment to run in the background:
+# daemonize
+
 num_servers 4   # one for each cpu
 
 #
index 75c36d8d6a409c025d67e65ba092b67a0ba85d91..2c5ef67d90bb78b7c897040ce8d3710d6975c044 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -291,6 +291,16 @@ 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.