mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 23:18:14 +01:00
bitcoind: Daemonize using daemon(3)
Simplified version of #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: https://github.com/bitcoin/bitcoin/pull/8278#issuecomment-242704745). - 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.
This commit is contained in:
committed by
Wladimir J. van der Laan
parent
37871f216e
commit
a92bf4af66
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "chainparams.h"
|
||||
#include "clientversion.h"
|
||||
#include "compat.h"
|
||||
#include "rpc/server.h"
|
||||
#include "init.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");
|
||||
exit(1);
|
||||
}
|
||||
#ifndef WIN32
|
||||
if (GetBoolArg("-daemon", false))
|
||||
{
|
||||
#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
|
||||
|
||||
Reference in New Issue
Block a user