Skip to content

bitcoind: process -help/-version before touching the datadir - #341

Open
kwsantiago wants to merge 1 commit into
bitcoinknots:29.x-knotsfrom
privkeyio:fix-bitcoind-help-version-datadir
Open

bitcoind: process -help/-version before touching the datadir#341
kwsantiago wants to merge 1 commit into
bitcoinknots:29.x-knotsfrom
privkeyio:fix-bitcoind-help-version-datadir

Conversation

@kwsantiago

@kwsantiago kwsantiago commented Jul 25, 2026

Copy link
Copy Markdown

Follow-up to #329, where @luke-jr asked about the comment in ProcessInitCommands() claiming help and version are processed "before taking care about datadir", when in fact ParseArgs() calls common::InitConfig() first.

InitConfig() creates the datadir and its wallets/ subdirectory, stats bitcoin.conf, and writes settings.json. So bitcoind --help and bitcoind --version both create $HOME/.bitcoin, and fail outright if it exists but is not traversable (issue #304).

Move the InitConfig() call after ProcessInitCommands(), so the comment becomes true. This is the ordering bitcoin-cli already uses: ParseParameters(), then help/version, then config.

Testing

Built with clang and compared against a pre-patch build of the same tree.

Scenario Before After bitcoin-cli
fresh $HOME, --version / --help creates .bitcoin/, wallets/, settings.json nothing created nothing created
chmod 444 ~/.bitcoin, --version / --help (#304) Permission denied, exit 1 exit 0 exit 0
--help / --version output byte-identical
--help foo (loose token) error, exit 1 unchanged
unparseable bitcoin.conf, normal startup error, exit 1 unchanged
-datadir=/nonexistent, normal startup error, exit 1 unchanged

Because the config file is no longer read before help/version, three things change, all of them matching bitcoin-cli's existing behavior:

Scenario Before After bitcoin-cli
help-debug=1 in bitcoin.conf, --help expands help output no effect no effect
-conf=/nonexistent.conf -version error, exit 1 prints version, exit 0 prints version, exit 0
-datadir=/nonexistent -version / -help error, exit 1 prints output, exit 0 prints output, exit 0

-help-debug on the command line still works, and both -conf and -datadir are still validated on a normal startup.

feature_help.py, feature_config_args.py, feature_settings.py and feature_init.py pass.

Comment thread src/bitcoind.cpp Outdated
@kwsantiago
kwsantiago force-pushed the fix-bitcoind-help-version-datadir branch from b8832a9 to 51e7235 Compare July 25, 2026 13:36
@kwsantiago
kwsantiago requested a review from luke-jr July 25, 2026 13:37
@kwsantiago
kwsantiago force-pushed the fix-bitcoind-help-version-datadir branch from 51e7235 to 0bed563 Compare July 25, 2026 15:05
@kwsantiago

Copy link
Copy Markdown
Author

Rebased onto v29.0 as a daggy fix and force-pushed.

@pdath pdath left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 0bed563.

I reviewed the changes to src/bitcoind.cpp, and found them to be simple, low risk, and as described.

Code Rabbit reviewed the change and found no issues.

I tested using the following commands on Ubuntu 24.04 running on ARMv8-A (64-bit) and gcc to verify the the original issue #304 is resolved.

git clone https://github.com/bitcoinknots/bitcoin.git pr341
cd pr341
git fetch origin pull/341/head:pr341
git switch pr341

cmake -B build -DCMAKE_BUILD_TYPE=Release
nice cmake --build build --target bitcoind -j $(nproc)

chmod 444 ~/.bitcoin
./contrib/devtools/gen-bitcoin-conf.sh

And then verified that the following file was created and contained the expected content:

./share/examples/bitcoin.conf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants