Merge #8813: bitcoind: Daemonize using daemon(3)

a92bf4a bitcoind: Daemonize using daemon(3) (Matthew King)
This commit is contained in:
Wladimir J. van der Laan 2016-09-30 18:02:53 +02:00
commit fb24d7eeb4
No known key found for this signature in database
GPG key ID: 74810B012346C9A6
3 changed files with 12 additions and 16 deletions

View file

@ -509,6 +509,9 @@ AC_SEARCH_LIBS([inet_pton], [nsl resolv], [AC_DEFINE(HAVE_INET_PTON, 1, [Define
AC_CHECK_DECLS([strnlen]) 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],,, AC_CHECK_DECLS([le16toh, le32toh, le64toh, htole16, htole32, htole64, be16toh, be32toh, be64toh, htobe16, htobe32, htobe64],,,
[#if HAVE_ENDIAN_H [#if HAVE_ENDIAN_H
#include <endian.h> #include <endian.h>

View file

@ -9,6 +9,7 @@
#include "chainparams.h" #include "chainparams.h"
#include "clientversion.h" #include "clientversion.h"
#include "compat.h"
#include "rpc/server.h" #include "rpc/server.h"
#include "init.h" #include "init.h"
#include "noui.h" #include "noui.h"
@ -127,29 +128,21 @@ bool AppInit(int argc, char* argv[])
fprintf(stderr, "Error: There is no RPC client functionality in bitcoind anymore. Use the bitcoin-cli utility instead.\n"); fprintf(stderr, "Error: There is no RPC client functionality in bitcoind anymore. Use the bitcoin-cli utility instead.\n");
exit(1); exit(1);
} }
#ifndef WIN32
if (GetBoolArg("-daemon", false)) if (GetBoolArg("-daemon", false))
{ {
#if HAVE_DECL_DAEMON
fprintf(stdout, "Bitcoin server starting\n"); fprintf(stdout, "Bitcoin server starting\n");
// Daemonize // Daemonize
pid_t pid = fork(); if (daemon(1, 0)) { // don't chdir (1), do close FDs (0)
if (pid < 0) fprintf(stderr, "Error: daemon() failed: %s\n", strerror(errno));
{
fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
return false; return false;
} }
if (pid > 0) // Parent process, pid is child process id #else
{ fprintf(stderr, "Error: -daemon is not supported on this operating system\n");
return true; return false;
#endif // HAVE_DECL_DAEMON
} }
// 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);
}
#endif
SoftSetBoolArg("-server", true); SoftSetBoolArg("-server", true);
// Set this early so that parameter interactions go to console // Set this early so that parameter interactions go to console

View file

@ -324,7 +324,7 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += HelpMessageOpt("-conf=<file>", strprintf(_("Specify configuration file (default: %s)"), BITCOIN_CONF_FILENAME)); strUsage += HelpMessageOpt("-conf=<file>", strprintf(_("Specify configuration file (default: %s)"), BITCOIN_CONF_FILENAME));
if (mode == HMM_BITCOIND) if (mode == HMM_BITCOIND)
{ {
#ifndef WIN32 #if HAVE_DECL_DAEMON
strUsage += HelpMessageOpt("-daemon", _("Run in the background as a daemon and accept commands")); strUsage += HelpMessageOpt("-daemon", _("Run in the background as a daemon and accept commands"));
#endif #endif
} }