HomePhabricator

bitcoind: Add -daemonwait option to wait for initialization

Description

bitcoind: Add -daemonwait option to wait for initialization

Summary:

util: Add RAII TokenPipe

shutdown: Use RAII TokenPipe in shutdown

bitcoind: Add -daemonwait option to wait for initialization

This adds a -daemonwait flag that does the same as -daemon except
it, from a user perspective, backgrounds the process only after
initialization is complete.

This can be useful when the process launching bitcoind wants to
guarantee that either the RPC server is running, or that initialization
failed, before continuing. The exit code indicates the initialization
result.

This replaces the use of the libc function daemon() by a custom
implementation which is inspired by the glibc implementation, but also
creates a pipe from the child to the parent process for communication.

An additional advantage of having our own daemon() implementation is
that no MACOS-specific pragmas are needed anymore to silence a
deprecation warning.

Always add -daemonwait to known command line arguments

This is a backport of core#21007 and core#21447

Test Plan:
ninja all check-all

$ src/bitcoind -daemon
Bitcoin ABC starting
$ src/bitcoin-cli stop
Bitcoin ABC stopping
$ src/bitcoind -daemonwait
Bitcoin ABC starting
$ src/bitcoin-cli stop
Bitcoin ABC stopping

Try again with an init failure:

diff --git a/src/init.cpp b/src/init.cpp
index 676e7de472..05fa480ab5 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -2858,6 +2858,8 @@ bool AppInitMain(Config &config, RPCServer &rpcServer,
             }
         } while (false);

+        return InitError(_("Fake failure"));
+
         if (!fLoaded && !ShutdownRequested()) {
             // first suggest a reindex
             if (!fReset) {
$ src/bitcoind -daemon
Bitcoin ABC starting
$ src/bitcoin-cli stop
error: Could not connect to the server 127.0.0.1:8332

Make sure the bitcoind server is running and that you are connecting to the correct RPC port.
$ src/bitcoind -daemonwait
Bitcoin ABC starting
Error during initializaton - check debug.log for details
0

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D12532

Details

Provenance
Wladimir J. van der Laan <laanwj@protonmail.com>Authored on Jan 26 2021, 18:36
PiRKCommitted on Nov 21 2022, 19:06
PiRKPushed on Nov 21 2022, 19:06
Reviewer
Restricted Project
Differential Revision
D12532: bitcoind: Add -daemonwait option to wait for initialization
Parents
rABC2e37ce4c73a1: [Cashtab][Move Msg Switch pt3] Add transition effect to address inputs
Branches
Unknown
Tags
Unknown