From 210242e5c012116aebeddae4f45c43a9c1eb3674 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Fri, 30 Sep 2016 18:02:53 +0200 Subject: [PATCH] Merge #8813: bitcoind: Daemonize using daemon(3) a92bf4a bitcoind: Daemonize using daemon(3) (Matthew King) --- configure.ac | 3 +++ src/dashd.cpp | 23 ++++++++--------------- src/init.cpp | 2 +- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/configure.ac b/configure.ac index 12bee5b187..fe2ec838b2 100644 --- a/configure.ac +++ b/configure.ac @@ -518,6 +518,9 @@ AC_SEARCH_LIBS([inet_pton], [nsl resolv], [AC_DEFINE(HAVE_INET_PTON, 1, [Define AC_CHECK_DECLS([strnlen]) +# Check for daemon(3), unrelated to --with-daemon (although used by it) +AC_CHECK_DECLS([daemon]) + AC_CHECK_DECLS([le16toh, le32toh, le64toh, htole16, htole32, htole64, be16toh, be32toh, be64toh, htobe16, htobe32, htobe64],,, [#if HAVE_ENDIAN_H #include diff --git a/src/dashd.cpp b/src/dashd.cpp index d5ec2c98ac..cfb21cbbb8 100644 --- a/src/dashd.cpp +++ b/src/dashd.cpp @@ -10,6 +10,7 @@ #include "chainparams.h" #include "clientversion.h" +#include "compat.h" #include "rpc/server.h" #include "init.h" #include "noui.h" @@ -142,29 +143,21 @@ bool AppInit(int argc, char* argv[]) fprintf(stderr, "Error: There is no RPC client functionality in dashd anymore. Use the dash-cli utility instead.\n"); exit(EXIT_FAILURE); } -#ifndef WIN32 if (GetBoolArg("-daemon", false)) { +#if HAVE_DECL_DAEMON fprintf(stdout, "Dash Core server starting\n"); // Daemonize - pid_t pid = fork(); - if (pid < 0) - { - fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno); + if (daemon(1, 0)) { // don't chdir (1), do close FDs (0) + fprintf(stderr, "Error: daemon() failed: %s\n", strerror(errno)); return false; } - if (pid > 0) // Parent process, pid is child process id - { - return true; - } - // Child process falls through to rest of initialization - - pid_t sid = setsid(); - if (sid < 0) - fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno); +#else + fprintf(stderr, "Error: -daemon is not supported on this operating system\n"); + return false; +#endif // HAVE_DECL_DAEMON } -#endif SoftSetBoolArg("-server", true); // Set this early so that parameter interactions go to console diff --git a/src/init.cpp b/src/init.cpp index 917473ad4d..2926e6e009 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -395,7 +395,7 @@ std::string HelpMessage(HelpMessageMode mode) strUsage += HelpMessageOpt("-conf=", strprintf(_("Specify configuration file (default: %s)"), BITCOIN_CONF_FILENAME)); if (mode == HMM_BITCOIND) { -#ifndef WIN32 +#if HAVE_DECL_DAEMON strUsage += HelpMessageOpt("-daemon", _("Run in the background as a daemon and accept commands")); #endif }