From: Steinar H. Gunderson Date: Tue, 16 Apr 2013 18:01:26 +0000 (+0200) Subject: Support daemonizing. X-Git-Tag: 1.0.0~94 X-Git-Url: https://git.sesse.net/?p=cubemap;a=commitdiff_plain;h=a313e4c7a3c0c453d4dffd317fc143f1f4a7f8ba Support daemonizing. --- diff --git a/config.cpp b/config.cpp index 660090d..77acf6d 100644 --- a/config.cpp +++ b/config.cpp @@ -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()); diff --git a/config.h b/config.h index bed0551..eee2e1b 100644 --- a/config.h +++ b/config.h @@ -28,6 +28,7 @@ struct LogConfig { }; struct Config { + bool daemonize; int num_servers; std::vector mark_pools; std::vector streams; diff --git a/cubemap.config.sample b/cubemap.config.sample index 1a691ee..c639cd9 100644 --- a/cubemap.config.sample +++ b/cubemap.config.sample @@ -1,3 +1,6 @@ +# uncomment to run in the background: +# daemonize + num_servers 4 # one for each cpu # diff --git a/main.cpp b/main.cpp index 75c36d8..2c5ef67 100644 --- 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.