From 1df924e73df54fa4006935c25ed9b95fe4463cf6 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 29 Nov 2014 18:26:42 +1100 Subject: [PATCH] Provide an option to daemonise ckpool --- src/ckpool.c | 21 ++++++++++++++++++++- src/ckpool.h | 3 +++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/ckpool.c b/src/ckpool.c index 66054670..078cd1c3 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -1201,6 +1201,7 @@ static void *watchdog(void *arg) static struct option long_options[] = { {"standalone", no_argument, 0, 'A'}, {"config", required_argument, 0, 'c'}, + {"daemonise", no_argument, 0, 'D'}, {"ckdb-name", required_argument, 0, 'd'}, {"group", required_argument, 0, 'g'}, {"handover", no_argument, 0, 'H'}, @@ -1218,6 +1219,7 @@ static struct option long_options[] = { #else static struct option long_options[] = { {"config", required_argument, 0, 'c'}, + {"daemonise", no_argument, 0, 'D'}, {"group", required_argument, 0, 'g'}, {"handover", no_argument, 0, 'H'}, {"help", no_argument, 0, 'h'}, @@ -1267,7 +1269,7 @@ int main(int argc, char **argv) ckp.initial_args[ckp.args] = strdup(argv[ckp.args]); ckp.initial_args[ckp.args] = NULL; - while ((c = getopt_long(argc, argv, "Ac:d:g:HhkLl:n:PpS:s:", long_options, &i)) != -1) { + while ((c = getopt_long(argc, argv, "Ac:Dd:g:HhkLl:n:PpS:s:", long_options, &i)) != -1) { switch (c) { case 'A': ckp.standalone = true; @@ -1275,6 +1277,9 @@ int main(int argc, char **argv) case 'c': ckp.config = optarg; break; + case 'D': + ckp.daemon = true; + break; case 'd': ckp.ckdb_name = optarg; break; @@ -1500,6 +1505,20 @@ int main(int argc, char **argv) } } + if (ckp.daemon) { + int fd; + + if (fork()) + exit(0); + setsid(); + fd = open("/dev/null",O_RDWR, 0); + if (fd != -1) { + dup2(fd, STDIN_FILENO); + dup2(fd, STDOUT_FILENO); + dup2(fd, STDERR_FILENO); + } + } + write_namepid(&ckp.main); open_process_sock(&ckp, &ckp.main, &ckp.main.us); launch_logger(&ckp.main); diff --git a/src/ckpool.h b/src/ckpool.h index fc534068..b8293474 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -141,6 +141,9 @@ struct ckpool_instance { /* Are we running without ckdb */ bool standalone; + /* Should we daemonise the ckpool process */ + bool daemon; + /* Bitcoind data */ int btcds; char **btcdurl;