8 #ifdef ENABLE_EXTERNAL_SIGNER
9 #if defined(WIN32) && !defined(__kernel_entry)
12 #define __kernel_entry
14 #include <boost/process.hpp>
15 #endif // ENABLE_EXTERNAL_SIGNER
26 #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
28 #include <pthread_np.h>
35 #ifdef _POSIX_C_SOURCE
36 #undef _POSIX_C_SOURCE
39 #define _POSIX_C_SOURCE 200112L
47 #include <sys/resource.h>
53 #pragma warning(disable:4786)
54 #pragma warning(disable:4804)
55 #pragma warning(disable:4805)
56 #pragma warning(disable:4717)
69 #ifdef HAVE_MALLOPT_ARENA_MAX
73 #include <boost/algorithm/string/replace.hpp>
95 bool LockDirectory(
const fs::path& directory,
const std::string lockfile_name,
bool probe_only)
98 fs::path pathLockFile = directory / lockfile_name;
101 if (dir_locks.count(pathLockFile.string())) {
107 if (file) fclose(file);
108 auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
109 if (!lock->TryLock()) {
110 return error(
"Error while attempting to lock directory %s: %s", directory.string(), lock->GetReason());
114 dir_locks.emplace(pathLockFile.string(), std::move(lock));
122 dir_locks.erase((directory / lockfile_name).
string());
136 if (!file)
return false;
146 constexpr uint64_t min_disk_space = 52428800;
148 uint64_t free_bytes_available = fs::space(dir).available;
149 return free_bytes_available >= min_disk_space + additional_bytes;
152 std::streampos
GetFileSize(
const char* path, std::streamsize max) {
155 return file.gcount();
177 if (strValue.empty())
179 return (
atoi(strValue) != 0);
184 return arg.size() > 0 && arg[0] ==
'-' ? arg.substr(1) : arg;
210 size_t option_index = key.find(
'.');
211 if (option_index != std::string::npos) {
212 section = key.substr(0, option_index);
213 key.erase(0, option_index + 1);
215 if (key.substr(0, 2) ==
"no") {
219 LogPrintf(
"Warning: parsed potentially confusing double-negative -%s=%s\n", key, value);
237 error =
strprintf(
"Negating of -%s is meaningless and therefore forbidden", key);
244 fs::path StripRedundantLastElementsOfPath(
const fs::path& path)
247 while (result.filename().string() ==
".") {
248 result = result.parent_path();
251 assert(fs::equivalent(result, path));
264 std::set<std::string> unsuitables;
269 if (m_network.empty())
return std::set<std::string> {};
274 for (
const auto& arg : m_network_only_args) {
276 unsuitables.insert(arg);
285 static const std::set<std::string> available_sections{
293 std::list<SectionInfo> unrecognized = m_config_sections;
294 unrecognized.remove_if([](
const SectionInfo& appeared){
return available_sections.find(appeared.
m_name) != available_sections.end(); });
307 m_settings.command_line_options.clear();
309 for (
int i = 1; i < argc; i++) {
310 std::string key(argv[i]);
317 if (key.substr(0, 5) ==
"-psn_")
continue;
320 if (key ==
"-")
break;
322 size_t is_index = key.find(
'=');
323 if (is_index != std::string::npos) {
324 val = key.substr(is_index + 1);
334 if (!m_accept_any_command &&
m_command.empty()) {
351 if (key.length() > 1 && key[1] ==
'-')
363 if (!
flags || !section.empty()) {
370 m_settings.command_line_options[key].push_back(value);
374 if (
auto* includes =
util::FindKey(m_settings.command_line_options,
"includeconf")) {
377 if (!values.empty()) {
378 error =
"-includeconf cannot be used from commandline; -includeconf=" + values.begin()->write();
388 for (
const auto& arg_map : m_available_args) {
389 const auto search = arg_map.second.find(
name);
390 if (search != arg_map.second.end()) {
391 return search->second.m_flags;
400 fs::path& path = m_cached_blocks_path;
404 if (!path.empty())
return path;
407 path = fs::system_complete(
GetArg(
"-blocksdir",
""));
408 if (!fs::is_directory(path)) {
418 fs::create_directories(path);
419 path = StripRedundantLastElementsOfPath(path);
426 fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path;
430 if (!path.empty())
return path;
432 std::string datadir =
GetArg(
"-datadir",
"");
433 if (!datadir.empty()) {
434 path = fs::system_complete(datadir);
435 if (!fs::is_directory(path)) {
445 if (fs::create_directories(path)) {
447 fs::create_directories(path /
"wallets");
450 path = StripRedundantLastElementsOfPath(path);
458 m_cached_datadir_path = fs::path();
459 m_cached_network_datadir_path = fs::path();
460 m_cached_blocks_path = fs::path();
472 if (!m_accept_any_command) {
478 ret.
args.push_back(*(it++));
485 std::vector<std::string> result;
487 result.push_back(value.isFalse() ?
"0" : value.isTrue() ?
"1" : value.get_str());
503 std::vector<std::string> errors;
527 static void SaveErrors(
const std::vector<std::string> errors, std::vector<std::string>* error_out)
529 for (
const auto&
error : errors) {
531 error_out->emplace_back(
error);
546 m_settings.rw_settings.clear();
547 std::vector<std::string> read_errors;
552 for (
const auto& setting : m_settings.rw_settings) {
554 std::string key = setting.first;
557 LogPrintf(
"Ignoring unknown rw_settings value %s\n", setting.first);
565 fs::path path, path_tmp;
567 throw std::logic_error(
"Attempt to write settings file when dynamic settings are disabled.");
571 std::vector<std::string> write_errors;
577 SaveErrors({
strprintf(
"Failed renaming settings file %s to %s\n", path_tmp.string(), path.string())}, errors);
625 m_settings.forced_settings[
SettingName(strArg)] = strValue;
630 Assert(cmd.find(
'=') == std::string::npos);
634 m_accept_any_command =
false;
645 size_t eq_index =
name.find(
'=');
646 if (eq_index == std::string::npos) {
647 eq_index =
name.size();
649 std::string arg_name =
name.substr(0, eq_index);
652 std::map<std::string, Arg>& arg_map = m_available_args[cat];
653 auto ret = arg_map.emplace(arg_name,
Arg{
name.substr(eq_index,
name.size() - eq_index),
help,
flags});
657 m_network_only_args.emplace(arg_name);
663 for (
const std::string&
name : names) {
670 const bool show_debug =
GetBoolArg(
"-help-debug",
false);
672 std::string usage =
"";
674 for (
const auto& arg_map : m_available_args) {
675 switch(arg_map.first) {
701 if (show_debug) usage +=
HelpMessageGroup(
"Wallet debugging/testing options:");
722 for (
const auto& arg : arg_map.second) {
725 if (arg.second.m_help_param.empty()) {
728 name = arg.first + arg.second.m_help_param;
753 return std::string(message) + std::string(
"\n\n");
756 std::string
HelpMessageOpt(
const std::string &option,
const std::string &message) {
757 return std::string(
optIndent,
' ') + std::string(option) +
758 std::string(
"\n") + std::string(
msgIndent,
' ') +
767 GetModuleFileNameA(
nullptr, pszModule,
sizeof(pszModule));
769 const char* pszModule =
"bitcoin";
773 "EXCEPTION: %s \n%s \n%s in %s \n",
typeid(*pex).name(), pex->what(), pszModule, pszThread);
776 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
782 LogPrintf(
"\n\n************************\n%s\n", message);
783 tfm::format(std::cerr,
"\n\n************************\n%s\n", message);
793 return GetSpecialFolderPath(CSIDL_APPDATA) /
"Bitcoin";
796 char* pszHome = getenv(
"HOME");
797 if (pszHome ==
nullptr || strlen(pszHome) == 0)
798 pathRet = fs::path(
"/");
800 pathRet = fs::path(pszHome);
803 return pathRet /
"Library/Application Support/Bitcoin";
806 return pathRet /
".bitcoin";
813 std::string datadir =
gArgs.
GetArg(
"-datadir",
"");
814 return datadir.empty() || fs::is_directory(fs::system_complete(datadir));
822 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)
825 std::string::size_type pos;
827 while (std::getline(stream, str)) {
828 bool used_hash =
false;
829 if ((pos = str.find(
'#')) != std::string::npos) {
830 str = str.substr(0, pos);
833 const static std::string pattern =
" \t\r\n";
836 if (*str.begin() ==
'[' && *str.rbegin() ==
']') {
837 const std::string section = str.substr(1, str.size() - 2);
838 sections.emplace_back(
SectionInfo{section, filepath, linenr});
840 }
else if (*str.begin() ==
'-') {
841 error =
strprintf(
"parse error on line %i: %s, options in configuration file must be specified without leading -", linenr, str);
843 }
else if ((pos = str.find(
'=')) != std::string::npos) {
845 std::string value =
TrimString(str.substr(pos + 1), pattern);
846 if (used_hash &&
name.find(
"rpcpassword") != std::string::npos) {
847 error =
strprintf(
"parse error on line %i, using # in rpcpassword can be ambiguous and should be avoided", linenr);
850 options.emplace_back(
name, value);
851 if ((pos =
name.rfind(
'.')) != std::string::npos &&
prefix.length() <= pos) {
852 sections.emplace_back(
SectionInfo{
name.substr(0, pos), filepath, linenr});
856 if (str.size() >= 2 && str.substr(0, 2) ==
"no") {
857 error +=
strprintf(
", if you intended to specify a negated option, use %s=1 instead", str);
870 std::vector<std::pair<std::string, std::string>> options;
874 for (
const std::pair<std::string, std::string>& option : options) {
876 std::string key = option.first;
883 m_settings.ro_config[section][key].push_back(value);
885 if (ignore_invalid_keys) {
886 LogPrintf(
"Ignoring unknown configuration value %s\n", option.first);
888 error =
strprintf(
"Invalid configuration value %s", option.first);
900 m_settings.ro_config.clear();
901 m_config_sections.clear();
914 bool use_conf_file{
true};
917 if (
auto* includes =
util::FindKey(m_settings.command_line_options,
"includeconf")) {
920 use_conf_file =
false;
925 std::vector<std::string> conf_file_names;
927 auto add_includes = [&](
const std::string& network,
size_t skip = 0) {
928 size_t num_values = 0;
930 if (
auto* section =
util::FindKey(m_settings.ro_config, network)) {
933 conf_file_names.push_back((*values)[i].get_str());
935 num_values = values->size();
943 const size_t chain_includes = add_includes(chain_id);
944 const size_t default_includes = add_includes({});
946 for (
const std::string& conf_file_name : conf_file_names) {
948 if (conf_file_stream.good()) {
952 LogPrintf(
"Included configuration file %s\n", conf_file_name);
954 error =
"Failed to include configuration file " + conf_file_name;
960 conf_file_names.clear();
961 add_includes(chain_id, chain_includes);
962 add_includes({}, default_includes);
964 if (chain_id_final != chain_id) {
966 add_includes(chain_id_final);
968 for (
const std::string& conf_file_name : conf_file_names) {
969 tfm::format(std::cerr,
"warning: -includeconf cannot be used from included files; ignoring -includeconf=%s\n", conf_file_name);
985 auto get_net = [&](
const std::string& arg) {
993 const bool fRegTest = get_net(
"-regtest");
994 const bool fSigNet = get_net(
"-signet");
995 const bool fTestNet = get_net(
"-testnet");
996 const bool is_chain_arg_set =
IsArgSet(
"-chain");
998 if ((
int)is_chain_arg_set + (
int)fRegTest + (
int)fSigNet + (
int)fTestNet > 1) {
999 throw std::runtime_error(
"Invalid combination of -regtest, -signet, -testnet and -chain. Can use at most one.");
1031 const std::string&
prefix,
1032 const std::string& section,
1033 const std::map<std::string, std::vector<util::SettingsValue>>& args)
const
1035 std::string section_str = section.empty() ?
"" :
"[" + section +
"] ";
1036 for (
const auto& arg : args) {
1037 for (
const auto& value : arg.second) {
1040 std::string value_str = (*
flags &
SENSITIVE) ?
"****" : value.write();
1050 for (
const auto& section : m_settings.ro_config) {
1051 logArgsPrefix(
"Config file arg:", section.first, section.second);
1053 for (
const auto& setting : m_settings.rw_settings) {
1054 LogPrintf(
"Setting file arg: %s = %s\n", setting.first, setting.second.write());
1056 logArgsPrefix(
"Command-line arg:",
"", m_settings.command_line_options);
1062 return MoveFileExW(src.wstring().c_str(), dest.wstring().c_str(),
1063 MOVEFILE_REPLACE_EXISTING) != 0;
1065 int rc = std::rename(src.string().c_str(), dest.string().c_str());
1079 return fs::create_directories(p);
1080 }
catch (
const fs::filesystem_error&) {
1081 if (!fs::exists(p) || !fs::is_directory(p))
1091 if (fflush(file) != 0) {
1092 LogPrintf(
"%s: fflush failed: %d\n", __func__, errno);
1096 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
1097 if (FlushFileBuffers(hFile) == 0) {
1098 LogPrintf(
"%s: FlushFileBuffers failed: %d\n", __func__, GetLastError());
1101 #elif defined(MAC_OSX) && defined(F_FULLFSYNC)
1102 if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) {
1103 LogPrintf(
"%s: fcntl F_FULLFSYNC failed: %d\n", __func__, errno);
1106 #elif HAVE_FDATASYNC
1107 if (fdatasync(fileno(file)) != 0 && errno != EINVAL) {
1108 LogPrintf(
"%s: fdatasync failed: %d\n", __func__, errno);
1112 if (fsync(fileno(file)) != 0 && errno != EINVAL) {
1113 LogPrintf(
"%s: fsync failed: %d\n", __func__, errno);
1125 fsync(fileno(file));
1133 return _chsize(_fileno(file), length) == 0;
1135 return ftruncate(fileno(file), length) == 0;
1147 struct rlimit limitFD;
1148 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
1149 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
1150 limitFD.rlim_cur = nMinFD;
1151 if (limitFD.rlim_cur > limitFD.rlim_max)
1152 limitFD.rlim_cur = limitFD.rlim_max;
1153 setrlimit(RLIMIT_NOFILE, &limitFD);
1154 getrlimit(RLIMIT_NOFILE, &limitFD);
1156 return limitFD.rlim_cur;
1169 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
1170 LARGE_INTEGER nFileSize;
1171 int64_t nEndPos = (int64_t)offset + length;
1172 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
1173 nFileSize.u.HighPart = nEndPos >> 32;
1174 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
1175 SetEndOfFile(hFile);
1176 #elif defined(MAC_OSX)
1181 fst.fst_flags = F_ALLOCATECONTIG;
1182 fst.fst_posmode = F_PEOFPOSMODE;
1184 fst.fst_length = length;
1185 fst.fst_bytesalloc = 0;
1186 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
1187 fst.fst_flags = F_ALLOCATEALL;
1188 fcntl(fileno(file), F_PREALLOCATE, &fst);
1190 ftruncate(fileno(file),
static_cast<off_t
>(offset) + length);
1192 #if defined(HAVE_POSIX_FALLOCATE)
1194 off_t nEndPos = (off_t)offset + length;
1195 if (0 == posix_fallocate(fileno(file), 0, nEndPos))
return;
1199 static const char buf[65536] = {};
1200 if (fseek(file, offset, SEEK_SET)) {
1203 while (length > 0) {
1204 unsigned int now = 65536;
1207 fwrite(buf, 1, now, file);
1214 fs::path GetSpecialFolderPath(
int nFolder,
bool fCreate)
1218 if(SHGetSpecialFolderPathW(
nullptr, pszPath, nFolder, fCreate))
1220 return fs::path(pszPath);
1223 LogPrintf(
"SHGetSpecialFolderPathW() failed, could not obtain requested path.\n");
1224 return fs::path(
"");
1231 std::string escaped = arg;
1232 boost::replace_all(escaped,
"'",
"'\"'\"'");
1233 return "'" + escaped +
"'";
1238 void runCommand(
const std::string& strCommand)
1240 if (strCommand.empty())
return;
1242 int nErr = ::system(strCommand.c_str());
1244 int nErr = ::_wsystem(std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t>().from_bytes(strCommand).c_str());
1247 LogPrintf(
"runCommand error: system(%s) returned %d\n", strCommand, nErr);
1253 #ifdef ENABLE_EXTERNAL_SIGNER
1254 namespace bp = boost::process;
1257 bp::opstream stdin_stream;
1258 bp::ipstream stdout_stream;
1259 bp::ipstream stderr_stream;
1265 bp::std_out > stdout_stream,
1266 bp::std_err > stderr_stream,
1267 bp::std_in < stdin_stream
1269 if (!str_std_in.empty()) {
1270 stdin_stream << str_std_in << std::endl;
1272 stdin_stream.pipe().close();
1276 std::getline(stdout_stream, result);
1277 std::getline(stderr_stream,
error);
1280 const int n_error = c.exit_code();
1281 if (n_error)
throw std::runtime_error(
strprintf(
"RunCommandParseJSON error: process(%s) returned %d: %s\n", str_command, n_error,
error));
1282 if (!result_json.
read(result))
throw std::runtime_error(
"Unable to parse JSON: " + result);
1286 throw std::runtime_error(
"Compiled without external signing support (required for external signing).");
1287 #endif // ENABLE_EXTERNAL_SIGNER
1292 #ifdef HAVE_MALLOPT_ARENA_MAX
1298 if (
sizeof(
void*) == 4) {
1299 mallopt(M_ARENA_MAX, 1);
1304 #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
1307 }
catch (
const std::runtime_error&) {
1308 setenv(
"LC_ALL",
"C.UTF-8", 1);
1310 #elif defined(WIN32)
1312 SetConsoleCP(CP_UTF8);
1313 SetConsoleOutputCP(CP_UTF8);
1319 std::locale loc = fs::path::imbue(std::locale::classic());
1321 fs::path::imbue(loc);
1323 fs::path::imbue(std::locale(loc,
new std::codecvt_utf8_utf16<wchar_t>()));
1332 int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
1333 if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
1341 return std::thread::hardware_concurrency();
1347 std::string strCopyrightHolders = strPrefix + copyright_devs;
1350 if (copyright_devs.find(
"Bitcoin Core") == std::string::npos) {
1351 strCopyrightHolders +=
"\n" + strPrefix +
"The Bitcoin Core developers";
1353 return strCopyrightHolders;
1364 if (path.is_absolute()) {
1373 const static sched_param param{};
1374 const int rc = pthread_setschedparam(pthread_self(), SCHED_BATCH, ¶m);
1376 LogPrintf(
"Failed to pthread_setschedparam: %s\n", strerror(rc));
1383 WinCmdLineArgs::WinCmdLineArgs()
1385 wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
1386 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t> utf8_cvt;
1387 argv =
new char*[argc];
1389 for (
int i = 0; i < argc; i++) {
1390 args[i] = utf8_cvt.to_bytes(wargv[i]);
1391 argv[i] = &*args[i].begin();
1396 WinCmdLineArgs::~WinCmdLineArgs()
1401 std::pair<int, char**> WinCmdLineArgs::get()
1403 return std::make_pair(argc, argv);