Skip to content

Commit

Permalink
bitcoind: Daemonize using daemon(3)
Browse files Browse the repository at this point in the history
Simplified version of bitcoin#8278. Assumes that every OS that (a) is supported
by Bitcoin Core (b) supports daemonization has the `daemon()` function
in its C library.

- Removes the fallback path for operating systems that support
  daemonization but not `daemon()`. This prevents never-exercised code from
  ending up in the repository (see discussion here:
  bitcoin#8278 (comment)).

- Removes the windows-specific path. Windows doesn't support `daemon()`,
  so it don't support daemonization there, automatically.

Original code by Matthew King, adapted by Wladimir van der Laan.

Github-Pull: bitcoin#8813
Rebased-From: a92bf4a
  • Loading branch information
ChoHag authored and luke-jr committed Dec 10, 2016
1 parent e4382fb commit 377ab84
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
3 changes: 3 additions & 0 deletions configure.ac
Expand Up @@ -521,6 +521,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 <endian.h>
Expand Down
23 changes: 8 additions & 15 deletions src/bitcoind.cpp
Expand Up @@ -9,6 +9,7 @@

#include "chainparams.h"
#include "clientversion.h"
#include "compat.h"
#include "rpc/server.h"
#include "init.h"
#include "noui.h"
Expand Down Expand Up @@ -129,30 +130,22 @@ 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");
exit(1);
}
#ifndef WIN32
fDaemon = GetBoolArg("-daemon", false);
if (fDaemon)
{
#if HAVE_DECL_DAEMON
fprintf(stdout, "Bitcoin 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
Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Expand Up @@ -313,7 +313,7 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += HelpMessageOpt("-conf=<file>", 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
}
Expand Down

0 comments on commit 377ab84

Please sign in to comment.