Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
config.cpp
Go to the documentation of this file.
1// Copyright (c) 2023 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <common/args.h>
6
7#include <common/settings.h>
8#include <logging.h>
9#include <sync.h>
10#include <tinyformat.h>
11#include <univalue.h>
12#include <util/chaintype.h>
13#include <util/fs.h>
14#include <util/string.h>
15
16#include <algorithm>
17#include <cassert>
18#include <cstdlib>
19#include <fstream>
20#include <iostream>
21#include <list>
22#include <map>
23#include <memory>
24#include <optional>
25#include <string>
26#include <string_view>
27#include <utility>
28#include <vector>
29
32
33static bool GetConfigOptions(std::istream& stream, const std::string& filepath, std::string& error, std::vector<std::pair<std::string, std::string>>& options, std::list<SectionInfo>& sections)
34{
35 std::string str, prefix;
36 std::string::size_type pos;
37 int linenr = 1;
38 while (std::getline(stream, str)) {
39 bool used_hash = false;
40 if ((pos = str.find('#')) != std::string::npos) {
41 str = str.substr(0, pos);
42 used_hash = true;
43 }
44 const static std::string pattern = " \t\r\n";
45 str = TrimString(str, pattern);
46 if (!str.empty()) {
47 if (*str.begin() == '[' && *str.rbegin() == ']') {
48 const std::string section = str.substr(1, str.size() - 2);
49 sections.emplace_back(SectionInfo{section, filepath, linenr});
50 prefix = section + '.';
51 } else if (*str.begin() == '-') {
52 error = strprintf("parse error on line %i: %s, options in configuration file must be specified without leading -", linenr, str);
53 return false;
54 } else if ((pos = str.find('=')) != std::string::npos) {
55 std::string name = prefix + TrimString(std::string_view{str}.substr(0, pos), pattern);
56 std::string_view value = TrimStringView(std::string_view{str}.substr(pos + 1), pattern);
57 if (used_hash && name.find("rpcpassword") != std::string::npos) {
58 error = strprintf("parse error on line %i, using # in rpcpassword can be ambiguous and should be avoided", linenr);
59 return false;
60 }
61 options.emplace_back(name, value);
62 if ((pos = name.rfind('.')) != std::string::npos && prefix.length() <= pos) {
63 sections.emplace_back(SectionInfo{name.substr(0, pos), filepath, linenr});
64 }
65 } else {
66 error = strprintf("parse error on line %i: %s", linenr, str);
67 if (str.size() >= 2 && str.substr(0, 2) == "no") {
68 error += strprintf(", if you intended to specify a negated option, use %s=1 instead", str);
69 }
70 return false;
71 }
72 }
73 ++linenr;
74 }
75 return true;
76}
77
78bool IsConfSupported(KeyInfo& key, std::string& error) {
79 if (key.name == "conf") {
80 error = "conf cannot be set in the configuration file; use includeconf= if you want to include additional config files";
81 return false;
82 }
83 if (key.name == "reindex") {
84 // reindex can be set in a config file but it is strongly discouraged as this will cause the node to reindex on
85 // every restart. Allow the config but throw a warning
86 LogPrintf("Warning: reindex=1 is set in the configuration file, which will significantly slow down startup. Consider removing or commenting out this option for better performance, unless there is currently a condition which makes rebuilding the indexes necessary\n");
87 return true;
88 }
89 return true;
90}
91
92bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& filepath, std::string& error, bool ignore_invalid_keys)
93{
95 std::vector<std::pair<std::string, std::string>> options;
96 if (!GetConfigOptions(stream, filepath, error, options, m_config_sections)) {
97 return false;
98 }
99 for (const std::pair<std::string, std::string>& option : options) {
100 KeyInfo key = InterpretKey(option.first);
101 std::optional<unsigned int> flags = GetArgFlags('-' + key.name);
102 if (!IsConfSupported(key, error)) return false;
103 if (flags) {
104 std::optional<common::SettingsValue> value = InterpretValue(key, &option.second, *flags, error);
105 if (!value) {
106 return false;
107 }
108 m_settings.ro_config[key.section][key.name].push_back(*value);
109 } else {
110 if (ignore_invalid_keys) {
111 LogPrintf("Ignoring unknown configuration value %s\n", option.first);
112 } else {
113 error = strprintf("Invalid configuration value %s", option.first);
114 return false;
115 }
116 }
117 }
118 return true;
119}
120
121bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
122{
123 {
124 LOCK(cs_args);
125 m_settings.ro_config.clear();
126 m_config_sections.clear();
127 m_config_path = AbsPathForConfigVal(*this, GetPathArg("-conf", BITCOIN_CONF_FILENAME), /*net_specific=*/false);
128 }
129
130 const auto conf_path{GetConfigFilePath()};
131 std::ifstream stream{conf_path};
132
133 // not ok to have a config file specified that cannot be opened
134 if (IsArgSet("-conf") && !stream.good()) {
135 error = strprintf("specified config file \"%s\" could not be opened.", fs::PathToString(conf_path));
136 return false;
137 }
138 // ok to not have a config file
139 if (stream.good()) {
140 if (!ReadConfigStream(stream, fs::PathToString(conf_path), error, ignore_invalid_keys)) {
141 return false;
142 }
143 // `-includeconf` cannot be included in the command line arguments except
144 // as `-noincludeconf` (which indicates that no included conf file should be used).
145 bool use_conf_file{true};
146 {
147 LOCK(cs_args);
148 if (auto* includes = common::FindKey(m_settings.command_line_options, "includeconf")) {
149 // ParseParameters() fails if a non-negated -includeconf is passed on the command-line
151 use_conf_file = false;
152 }
153 }
154 if (use_conf_file) {
155 std::string chain_id = GetChainTypeString();
156 std::vector<std::string> conf_file_names;
157
158 auto add_includes = [&](const std::string& network, size_t skip = 0) {
159 size_t num_values = 0;
160 LOCK(cs_args);
161 if (auto* section = common::FindKey(m_settings.ro_config, network)) {
162 if (auto* values = common::FindKey(*section, "includeconf")) {
163 for (size_t i = std::max(skip, common::SettingsSpan(*values).negated()); i < values->size(); ++i) {
164 conf_file_names.push_back((*values)[i].get_str());
165 }
166 num_values = values->size();
167 }
168 }
169 return num_values;
170 };
171
172 // We haven't set m_network yet (that happens in SelectParams()), so manually check
173 // for network.includeconf args.
174 const size_t chain_includes = add_includes(chain_id);
175 const size_t default_includes = add_includes({});
176
177 for (const std::string& conf_file_name : conf_file_names) {
178 std::ifstream conf_file_stream{AbsPathForConfigVal(*this, fs::PathFromString(conf_file_name), /*net_specific=*/false)};
179 if (conf_file_stream.good()) {
180 if (!ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) {
181 return false;
182 }
183 LogPrintf("Included configuration file %s\n", conf_file_name);
184 } else {
185 error = "Failed to include configuration file " + conf_file_name;
186 return false;
187 }
188 }
189
190 // Warn about recursive -includeconf
191 conf_file_names.clear();
192 add_includes(chain_id, /* skip= */ chain_includes);
193 add_includes({}, /* skip= */ default_includes);
194 std::string chain_id_final = GetChainTypeString();
195 if (chain_id_final != chain_id) {
196 // Also warn about recursive includeconf for the chain that was specified in one of the includeconfs
197 add_includes(chain_id_final);
198 }
199 for (const std::string& conf_file_name : conf_file_names) {
200 tfm::format(std::cerr, "warning: -includeconf cannot be used from included files; ignoring -includeconf=%s\n", conf_file_name);
201 }
202 }
203 }
204
205 // If datadir is changed in .conf file:
207 if (!CheckDataDirOption(*this)) {
208 error = strprintf("specified data directory \"%s\" does not exist.", GetArg("-datadir", ""));
209 return false;
210 }
211 return true;
212}
213
214fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific)
215{
216 if (path.is_absolute()) {
217 return path;
218 }
219 return fsbridge::AbsPathJoin(net_specific ? args.GetDataDirNet() : args.GetDataDirBase(), path);
220}
std::optional< common::SettingsValue > InterpretValue(const KeyInfo &key, const std::string *value, unsigned int flags, std::string &error)
Interpret settings value based on registered flags.
Definition args.cpp:106
bool CheckDataDirOption(const ArgsManager &args)
Definition args.cpp:730
fs::path AbsPathForConfigVal(const ArgsManager &args, const fs::path &path, bool net_specific=true)
Most paths passed as configuration arguments are treated as relative to the datadir if they are not a...
Definition config.cpp:214
KeyInfo InterpretKey(std::string key)
Parse "name", "section.name", "noname", "section.noname" settings keys.
Definition args.cpp:78
const char *const BITCOIN_CONF_FILENAME
Definition args.cpp:38
int flags
ArgsManager & args
Definition bitcoind.cpp:270
std::string GetChainTypeString() const
Returns the appropriate chain type string from the program arguments.
Definition args.cpp:756
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition args.h:232
std::optional< unsigned int > GetArgFlags(const std::string &name) const
Return Flags for known arg.
Definition args.cpp:259
void ClearPathCache()
Clear cached directory paths.
Definition args.cpp:332
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition args.cpp:370
fs::path GetDataDirBase() const
Get data directory path.
Definition args.h:225
fs::path GetConfigFilePath() const
Return config file path (read-only)
Definition args.cpp:736
RecursiveMutex cs_args
Definition args.h:132
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition args.cpp:456
bool ReadConfigStream(std::istream &stream, const std::string &filepath, std::string &error, bool ignore_invalid_keys=false)
Definition config.cpp:92
bool ReadConfigFiles(std::string &error, bool ignore_invalid_keys=false)
Definition config.cpp:121
fs::path GetPathArg(std::string arg, const fs::path &default_value={}) const
Return path argument or default value.
Definition args.cpp:271
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition fs.h:33
static bool GetConfigOptions(std::istream &stream, const std::string &filepath, std::string &error, std::vector< std::pair< std::string, std::string > > &options, std::list< SectionInfo > &sections)
Definition config.cpp:33
bool IsConfSupported(KeyInfo &key, std::string &error)
Definition config.cpp:78
fs::path AbsPathForConfigVal(const ArgsManager &args, const fs::path &path, bool net_specific)
Most paths passed as configuration arguments are treated as relative to the datadir if they are not a...
Definition config.cpp:214
#define LogPrintf(...)
Definition logging.h:274
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
Definition settings.h:107
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition fs.h:151
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition fs.h:174
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition fs.cpp:36
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition string.h:69
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition string.h:79
const char * prefix
Definition rest.cpp:1007
const char * name
Definition rest.cpp:49
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
Definition args.h:70
std::string name
Definition args.h:71
std::string section
Definition args.h:72
Accessor for list of settings that skips negated values when iterated over.
Definition settings.h:90
bool last_negated() const
True if the last value is negated.
Definition settings.cpp:267
size_t negated() const
Number of negated values.
Definition settings.cpp:268
#define LOCK(cs)
Definition sync.h:257
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
assert(!tx.IsCoinBase())