Skip to content

Commit f6f7a12

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22622: util: Check if specified config file cannot be opened
127b460 test: Check if specified config file cannot be opened (nthumann) 6bb5470 util: Check if specified config file cannot be opened (nthumann) Pull request description: Fixes bitcoin/bitcoin#22612. When running e.g. `./src/bitcoind -datadir=/tmp/bitcoin -regtest -conf=/tmp/bitcoin/regtest/bitcoin.conf` and the specified config cannot be opened (doesn't exist, permission denied, ...), the initialization silently uses the default config. As voidburn already noted: > I can't think of a situation in which a config file is specified explicitly (in the startup options, as per service unit linked above), but inaccessible, where the fail condition should be to keep booting using defaults instead. With this patch applied, the initialization will fail immediately, if the specified config file cannot be opened. If no config file is explicitly specified, the behavior is unchanged. This not only affects `bitcoind`, but also `bitcoin-cli` and `bitcoin-qt`. In the example below the datadir is accessible, but the config file is not due to insufficient permissions: ``` $ ./src/bitcoind -datadir=/tmp/bitcoin -regtest --debug=1 -conf=/tmp/bitcoin/regtest/bitcoin.conf Error: Error reading configuration file: specified config file "/tmp/bitcoin/regtest/bitcoin.conf" could not be opened. ``` ACKs for top commit: 0xB10C: ACK 127b460 Zero-1729: tACK 127b460 theStack: Tested ACK 127b460 Tree-SHA512: 4fe487921485426f1d1da8d256c388af517b984b639d776aec7b159b3e23b669824093d3bdd31139d9415ed5f5de405b3e6a51b110c8ab471f12b9c99ac67cc1
2 parents 489beb3 + 127b460 commit f6f7a12

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/util/system.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,11 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
904904
const std::string confPath = GetArg("-conf", BITCOIN_CONF_FILENAME);
905905
fsbridge::ifstream stream(GetConfigFile(confPath));
906906

907+
// not ok to have a config file specified that cannot be opened
908+
if (IsArgSet("-conf") && !stream.good()) {
909+
error = strprintf("specified config file \"%s\" could not be opened.", confPath);
910+
return false;
911+
}
907912
// ok to not have a config file
908913
if (stream.good()) {
909914
if (!ReadConfigStream(stream, confPath, error, ignore_invalid_keys)) {

test/functional/feature_config_args.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ def run_test(self):
248248

249249
self.nodes[0].assert_start_raises_init_error([f'-conf={conf_file}'], f'Error: Error reading configuration file: specified data directory "{new_data_dir}" does not exist.')
250250

251+
# Check that an explicitly specified config file that cannot be opened fails
252+
none_existent_conf_file = os.path.join(default_data_dir, "none_existent_bitcoin.conf")
253+
self.nodes[0].assert_start_raises_init_error(['-conf=' + none_existent_conf_file], 'Error: Error reading configuration file: specified config file "' + none_existent_conf_file + '" could not be opened.')
254+
251255
# Create the directory and ensure the config file now works
252256
os.mkdir(new_data_dir)
253257
self.start_node(0, [f'-conf={conf_file}'])

0 commit comments

Comments
 (0)