Skip to content

contrib: don't use the default datadir in gen-bitcoin-conf.sh - #329

Merged
luke-jr merged 1 commit into
bitcoinknots:29.x-knotsfrom
privkeyio:fix-gen-bitcoin-conf-datadir
Aug 7, 2026
Merged

contrib: don't use the default datadir in gen-bitcoin-conf.sh#329
luke-jr merged 1 commit into
bitcoinknots:29.x-knotsfrom
privkeyio:fix-gen-bitcoin-conf-datadir

Conversation

@kwsantiago

Copy link
Copy Markdown

Fixes #304.

gen-bitcoin-conf.sh invokes bitcoind --version and bitcoind --help. Both of these run common::InitConfig() (src/bitcoind.cpp:122) before the -help/-version early return (src/bitcoind.cpp:138), despite the comment there claiming help and version are processed "before taking care about datadir". So each invocation touches the default datadir:

  • fs::create_directories(base_path / "wallets") (src/common/init.cpp:58) creates $HOME/.bitcoin
  • fs::exists(base_path / BITCOIN_CONF_FILENAME) (src/common/init.cpp:68) stats $HOME/.bitcoin/bitcoin.conf
  • WriteSettingsFile() (src/common/init.cpp:111) writes $HOME/.bitcoin/settings.json

If $HOME/.bitcoin exists but is not traversable, the fs::exists call throws filesystem_error on EACCES and both invocations fail, as @pdath reproduced in the issue.

Point bitcoind at a throwaway datadir instead. Note that -noconf alone is not sufficient: the fs::exists() call above runs regardless of whether a config file is in use, and -nosettings does not help either. Only -datadir avoids all three accesses. Verified by running each variant against a build of this tree.

Also add set -eo pipefail, per the second half of the issue. Previously a failing bitcoind was not detected at all: the VERSION_OUTPUT=$(...) assignment went unchecked, and the exit status of the bitcoind --help | sed | ... | sed pipeline was that of the trailing sed. The script printed bitcoind's errors and then exited 0 with a truncated bitcoin.conf, which is the failure mode seen in the issue report.

Bare mktemp -d matches the existing usage in contrib/devtools/check-deps.sh and is portable to the BSDs, whose mktemp behaves "as if -t tmp was supplied" when only -d is passed.

Testing

Built with -DWITH_ZMQ=ON -DWITH_MINIUPNPC=ON and confirmed the patched script regenerates share/examples/bitcoin.conf byte-for-byte identically to the committed file, so this change is output-neutral. bitcoind --help output is unchanged by -datadir.

Comparing against the script before this change:

Scenario Before After
Fresh $HOME creates .bitcoin/, wallets/, settings.json nothing created
Unwritable $HOME fails succeeds
chmod 444 ~/.bitcoin (issue #304) Permission denied, but exits 0 succeeds, silent
bitcoind --version fails exits 0 propagates exit status
bitcoind --help fails exits 0 propagates exit status
bitcoind missing / not executable exits 1 exits 1

The temporary datadir is removed on exit. test/lint/lint-shell.py passes.

One pre-existing wart is left alone: if bitcoind fails midway, the header has already been written and share/examples/bitcoin.conf is left truncated. That is now at least signalled by a non-zero exit status. Generating into a temporary file and renaming it into place on success would close it, if wanted.

@pdath

pdath commented Jul 11, 2026

Copy link
Copy Markdown

ACK 49db8d1.

I reviewed the changes to the bash script and found them well written and robust.

I tested using the following commands on Ubuntu 24.04 on an ARM CPU:
git fetch origin pull/329/head:pr329
git switch pr329
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

@luke-jr

luke-jr commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator

despite the comment there claiming help and version are processed "before taking care about datadir".

Shouldn't we fix this?

@kwsantiago

Copy link
Copy Markdown
Author

Yes. Opened #341, which moves the common::InitConfig() call after ProcessInitCommands() so the comment becomes true. That matches the ordering bitcoin-cli already uses.

With that in, the -datadir here is redundant, since --help and --version no longer touch the datadir at all (verified against a build of #341: both exit 0 with chmod 444 ~/.bitcoin). The set -eo pipefail half of this PR is still worth having either way, so I am happy to trim this to just that if you would rather fix it only in bitcoind.

@kwsantiago
kwsantiago force-pushed the fix-gen-bitcoin-conf-datadir branch from 49db8d1 to 4f5c538 Compare July 25, 2026 13:42
@kwsantiago

kwsantiago commented Jul 25, 2026

Copy link
Copy Markdown
Author

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

@luke-jr
luke-jr merged commit 2318470 into bitcoinknots:29.x-knots Aug 7, 2026
19 checks passed
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.

contrib/devtools/gen-bitcoin-conf.sh fails if HOME is unwritable

4 participants