From a2b9f2c732232200fd4822e34d60d4c7f5ad37d2 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 17 May 2012 22:24:31 +0200 Subject: Add daemon mode --- src/config.c | 5 +++++ src/fastd.c | 5 +++++ src/fastd.h | 1 + 3 files changed, 11 insertions(+) diff --git a/src/config.c b/src/config.c index f407239..d9397cc 100644 --- a/src/config.c +++ b/src/config.c @@ -88,6 +88,7 @@ static void default_config(fastd_config *conf) { conf->on_disestablish = NULL; conf->on_disestablish_dir = NULL; + conf->daemon = false; conf->machine_readable = false; conf->generate_key = false; conf->show_key = false; @@ -332,6 +333,7 @@ static void count_peers(fastd_context *ctx, fastd_config *conf) { #define OPTIONS \ OPTION(usage, "--help" OR "-h", "Shows this help text") \ OPTION(version, "--version" OR "-v", "Shows the fastd version") \ + OPTION(option_daemon, "--daemon" OR "-d", "Runs fastd in the background") \ OPTION_ARG(option_log_level, "--log-level", "error|warn|info|verbose|debug", "Sets the log level; default is info") \ OPTION_ARG(option_config, "--config" OR "-c", "", "Loads a config file") \ OPTION_ARG(option_config_peer, "--config-peer", "", "Loads a config file for a single peer") \ @@ -553,6 +555,9 @@ static void option_on_disestablish(fastd_context *ctx, fastd_config *conf, const conf->on_disestablish_dir = get_current_dir_name(); } +static void option_daemon(fastd_context *ctx, fastd_config *conf) { + conf->daemon = true; +} static void option_generate_key(fastd_context *ctx, fastd_config *conf) { conf->generate_key = true; conf->show_key = false; diff --git a/src/fastd.c b/src/fastd.c index 0ea9e26..e270a26 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -671,6 +671,11 @@ int main(int argc, char *argv[]) { init_peers(&ctx); + if (conf.daemon) { + if (daemon(0, 1) < 0) + exit_errno(&ctx, "daemon"); + } + on_up(&ctx); while (!terminate) { diff --git a/src/fastd.h b/src/fastd.h index 6a503d8..564b9c5 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -154,6 +154,7 @@ struct _fastd_config { char *on_disestablish; char *on_disestablish_dir; + bool daemon; bool machine_readable; bool generate_key; bool show_key; -- cgit v1.2.3