Skip to content

Commit 210242e

Browse files
laanwjcodablock
authored andcommitted
Merge bitcoin#8813: bitcoind: Daemonize using daemon(3)
a92bf4a bitcoind: Daemonize using daemon(3) (Matthew King)
1 parent 2f3abc6 commit 210242e

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ AC_SEARCH_LIBS([inet_pton], [nsl resolv], [AC_DEFINE(HAVE_INET_PTON, 1, [Define
518518

519519
AC_CHECK_DECLS([strnlen])
520520

521+
# Check for daemon(3), unrelated to --with-daemon (although used by it)
522+
AC_CHECK_DECLS([daemon])
523+
521524
AC_CHECK_DECLS([le16toh, le32toh, le64toh, htole16, htole32, htole64, be16toh, be32toh, be64toh, htobe16, htobe32, htobe64],,,
522525
[#if HAVE_ENDIAN_H
523526
#include <endian.h>

src/dashd.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "chainparams.h"
1212
#include "clientversion.h"
13+
#include "compat.h"
1314
#include "rpc/server.h"
1415
#include "init.h"
1516
#include "noui.h"
@@ -142,29 +143,21 @@ bool AppInit(int argc, char* argv[])
142143
fprintf(stderr, "Error: There is no RPC client functionality in dashd anymore. Use the dash-cli utility instead.\n");
143144
exit(EXIT_FAILURE);
144145
}
145-
#ifndef WIN32
146146
if (GetBoolArg("-daemon", false))
147147
{
148+
#if HAVE_DECL_DAEMON
148149
fprintf(stdout, "Dash Core server starting\n");
149150

150151
// Daemonize
151-
pid_t pid = fork();
152-
if (pid < 0)
153-
{
154-
fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
152+
if (daemon(1, 0)) { // don't chdir (1), do close FDs (0)
153+
fprintf(stderr, "Error: daemon() failed: %s\n", strerror(errno));
155154
return false;
156155
}
157-
if (pid > 0) // Parent process, pid is child process id
158-
{
159-
return true;
160-
}
161-
// Child process falls through to rest of initialization
162-
163-
pid_t sid = setsid();
164-
if (sid < 0)
165-
fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
156+
#else
157+
fprintf(stderr, "Error: -daemon is not supported on this operating system\n");
158+
return false;
159+
#endif // HAVE_DECL_DAEMON
166160
}
167-
#endif
168161
SoftSetBoolArg("-server", true);
169162

170163
// Set this early so that parameter interactions go to console

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ std::string HelpMessage(HelpMessageMode mode)
395395
strUsage += HelpMessageOpt("-conf=<file>", strprintf(_("Specify configuration file (default: %s)"), BITCOIN_CONF_FILENAME));
396396
if (mode == HMM_BITCOIND)
397397
{
398-
#ifndef WIN32
398+
#if HAVE_DECL_DAEMON
399399
strUsage += HelpMessageOpt("-daemon", _("Run in the background as a daemon and accept commands"));
400400
#endif
401401
}

0 commit comments

Comments
 (0)