Page MenuHomePhabricator

bitcoind: Add -daemonwait option to wait for initialization
ClosedPublic

Authored by PiRK on Nov 18 2022, 14:22.

Details

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

Event Timeline

PiRK requested review of this revision.Nov 18 2022, 14:22

Tail of the build log:

[332/523] Building CXX object src/CMakeFiles/server.dir/txdb.cpp.o
[333/523] Building CXX object src/CMakeFiles/server.dir/rpc/mining.cpp.o
[334/523] Building CXX object src/CMakeFiles/bitcoin-cli.dir/bitcoin-cli.cpp.o
[335/523] Building C object src/secp256k1/CMakeFiles/recover-bench.dir/src/bench_recover.c.o
[336/523] Building CXX object src/CMakeFiles/server.dir/rpc/net.cpp.o
[337/523] Building C object src/secp256k1/CMakeFiles/verify-bench.dir/src/bench_verify.c.o
[338/523] Building CXX object src/CMakeFiles/server.dir/rpc/server.cpp.o
[339/523] Building C object src/secp256k1/CMakeFiles/secp256k1.dir/src/secp256k1.c.o
[340/523] Building C object src/secp256k1/CMakeFiles/sign-bench.dir/src/bench_sign.c.o
[341/523] Linking C static library src/secp256k1/libsecp256k1.a
[342/523] Building C object src/secp256k1/CMakeFiles/ecmult-bench.dir/src/bench_ecmult.c.o
[343/523] Linking C executable src/secp256k1/recover-bench
[344/523] Linking C executable src/secp256k1/ecmult-bench
[345/523] Linking C executable src/secp256k1/verify-bench
[346/523] Building C object src/secp256k1/CMakeFiles/internal-bench.dir/src/bench_internal.c.o
[347/523] Linking C executable src/secp256k1/sign-bench
[348/523] Linking C executable src/secp256k1/internal-bench
[349/523] Building CXX object src/CMakeFiles/server.dir/rpc/avalanche.cpp.o
[350/523] Building CXX object src/CMakeFiles/server.dir/txorphanage.cpp.o
[351/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/str.cpp.o
[352/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/logging.cpp.o
[353/523] Building CXX object src/CMakeFiles/bitcoin-tx.dir/bitcoin-tx.cpp.o
[354/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/transaction_utils.cpp.o
[355/523] Building CXX object src/CMakeFiles/server.dir/net_processing.cpp.o
[356/523] Building CXX object src/CMakeFiles/server.dir/torcontrol.cpp.o
[357/523] Building CXX object src/CMakeFiles/server.dir/txmempool.cpp.o
[358/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/wallet.cpp.o
[359/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/context.cpp.o
[360/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/bdb.cpp.o
[361/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/blockfilter.cpp.o
[362/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/coinselection.cpp.o
[363/523] Building CXX object src/CMakeFiles/server.dir/rpc/rawtransaction.cpp.o
[364/523] Building CXX object src/wallet/CMakeFiles/wallet-tool.dir/wallettool.cpp.o
[365/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/net.cpp.o
[366/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/coincontrol.cpp.o
[367/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/mining.cpp.o
[368/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/db.cpp.o
[369/523] Building CXX object src/CMakeFiles/server.dir/wallet/init.cpp.o
[370/523] Building CXX object src/CMakeFiles/server.dir/rpc/blockchain.cpp.o
[371/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/crypter.cpp.o
[372/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/__/interfaces/wallet.cpp.o
[373/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/sqlite.cpp.o
[374/523] Building CXX object src/seeder/CMakeFiles/seeder-base.dir/dns.cpp.o
[375/523] Building CXX object src/seeder/CMakeFiles/seeder-base.dir/db.cpp.o
[376/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/salvage.cpp.o
[377/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/walletutil.cpp.o
[378/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/load.cpp.o
[379/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/fees.cpp.o
[380/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/setup_common.cpp.o
[381/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/scriptpubkeyman.cpp.o
[382/523] Building CXX object src/seeder/CMakeFiles/seeder-base.dir/options.cpp.o
[383/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/rpcdump.cpp.o
[384/523] Building CXX object src/seeder/CMakeFiles/bitcoin-seeder.dir/main.cpp.o
[385/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/rpcwallet.cpp.o
[386/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/walletdb.cpp.o
[387/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/wallet.cpp.o
[388/523] Building CXX object src/CMakeFiles/server.dir/validation.cpp.o
[389/523] Building CXX object src/seeder/CMakeFiles/seeder-base.dir/bitcoin.cpp.o
ninja: build stopped: cannot make progress due to previous errors.
Build build-clang-tidy failed with exit code 1
PiRK edited the summary of this revision. (Show Details)

squash with core#21447

add missing braces to fix clang-tidy error

Tail of the build log:

[332/523] Building CXX object src/CMakeFiles/server.dir/versionbits.cpp.o
[333/523] Building CXX object src/CMakeFiles/server.dir/txdb.cpp.o
[334/523] Building CXX object src/CMakeFiles/server.dir/rpc/net.cpp.o
[335/523] Building C object src/secp256k1/CMakeFiles/recover-bench.dir/src/bench_recover.c.o
[336/523] Building CXX object src/CMakeFiles/server.dir/rpc/avalanche.cpp.o
[337/523] Building C object src/secp256k1/CMakeFiles/verify-bench.dir/src/bench_verify.c.o
[338/523] Building C object src/secp256k1/CMakeFiles/sign-bench.dir/src/bench_sign.c.o
[339/523] Building CXX object src/CMakeFiles/server.dir/validationinterface.cpp.o
[340/523] Building CXX object src/CMakeFiles/server.dir/txorphanage.cpp.o
[341/523] Building C object src/secp256k1/CMakeFiles/secp256k1.dir/src/secp256k1.c.o
[342/523] Linking C static library src/secp256k1/libsecp256k1.a
[343/523] Linking C executable src/secp256k1/recover-bench
[344/523] Linking C executable src/secp256k1/verify-bench
[345/523] Linking C executable src/secp256k1/sign-bench
[346/523] Building CXX object src/CMakeFiles/server.dir/net_processing.cpp.o
[347/523] Building C object src/secp256k1/CMakeFiles/ecmult-bench.dir/src/bench_ecmult.c.o
[348/523] Linking C executable src/secp256k1/ecmult-bench
[349/523] Building C object src/secp256k1/CMakeFiles/internal-bench.dir/src/bench_internal.c.o
[350/523] Linking C executable src/secp256k1/internal-bench
[351/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/str.cpp.o
[352/523] Building CXX object src/CMakeFiles/server.dir/torcontrol.cpp.o
[353/523] Building CXX object src/CMakeFiles/bitcoin-cli.dir/bitcoin-cli.cpp.o
[354/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/logging.cpp.o
[355/523] Building CXX object src/CMakeFiles/server.dir/rpc/rawtransaction.cpp.o
[356/523] Building CXX object src/CMakeFiles/server.dir/txmempool.cpp.o
[357/523] Building CXX object src/CMakeFiles/server.dir/rpc/blockchain.cpp.o
[358/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/blockfilter.cpp.o
[359/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/context.cpp.o
[360/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/net.cpp.o
[361/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/transaction_utils.cpp.o
[362/523] Building CXX object src/CMakeFiles/server.dir/wallet/init.cpp.o
[363/523] Building CXX object src/CMakeFiles/bitcoin-tx.dir/bitcoin-tx.cpp.o
[364/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/mining.cpp.o
[365/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/coinselection.cpp.o
[366/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/coincontrol.cpp.o
[367/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/db.cpp.o
[368/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/bdb.cpp.o
[369/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/setup_common.cpp.o
[370/523] Building CXX object src/test/CMakeFiles/testutil.dir/util/wallet.cpp.o
[371/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/crypter.cpp.o
[372/523] Building CXX object src/wallet/CMakeFiles/wallet-tool.dir/wallettool.cpp.o
[373/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/sqlite.cpp.o
[374/523] Building CXX object src/CMakeFiles/server.dir/validation.cpp.o
[375/523] Building CXX object src/seeder/CMakeFiles/seeder-base.dir/dns.cpp.o
[376/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/fees.cpp.o
[377/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/load.cpp.o
[378/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/walletutil.cpp.o
[379/523] Building CXX object src/seeder/CMakeFiles/seeder-base.dir/db.cpp.o
[380/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/__/interfaces/wallet.cpp.o
[381/523] Building CXX object src/seeder/CMakeFiles/seeder-base.dir/options.cpp.o
[382/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/salvage.cpp.o
[383/523] Building CXX object src/seeder/CMakeFiles/seeder-base.dir/bitcoin.cpp.o
[384/523] Building CXX object src/seeder/CMakeFiles/bitcoin-seeder.dir/main.cpp.o
[385/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/scriptpubkeyman.cpp.o
[386/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/walletdb.cpp.o
[387/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/rpcdump.cpp.o
[388/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/rpcwallet.cpp.o
[389/523] Building CXX object src/wallet/CMakeFiles/wallet.dir/wallet.cpp.o
ninja: build stopped: cannot make progress due to previous errors.
Build build-clang-tidy failed with exit code 1
Fabien added a subscriber: Fabien.
Fabien added inline comments.
src/bitcoind.cpp
249 ↗(On Diff #36393)

good old memories

250 ↗(On Diff #36393)

should be above

src/util/tokenpipe.h
71 ↗(On Diff #36393)

layout

This revision is now accepted and ready to land.Nov 18 2022, 20:56

fix spelling of "initialization" in error message

Fabien requested changes to this revision.Nov 21 2022, 14:50

This also needs a release note

This revision now requires changes to proceed.Nov 21 2022, 14:50
This revision is now accepted and ready to land.Nov 21 2022, 18:56
This revision was landed with ongoing or failed builds.Nov 21 2022, 19:06
This revision was automatically updated to reflect the committed changes.