Bitcoin Core 30.99.0
P2P Digital Currency
bitcoin.cpp
Go to the documentation of this file.
1// Copyright (c) 2025 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 <bitcoin-build-config.h> // IWYU pragma: keep
6
7#include <clientversion.h>
8#include <common/args.h>
9#include <common/system.h>
10#include <util/fs.h>
11#include <util/exec.h>
12#include <util/strencodings.h>
13#include <util/translation.h>
14
15#include <iostream>
16#include <string>
17#include <tinyformat.h>
18#include <vector>
19
21
22static constexpr auto HELP_USAGE = R"(Usage: %s [OPTIONS] COMMAND...
23
24Options:
25 -m, --multiprocess Run multiprocess binaries bitcoin-node, bitcoin-gui.
26 -M, --monolithic Run monolithic binaries bitcoind, bitcoin-qt. (Default behavior)
27 -v, --version Show version information
28 -h, --help Show full help message
29
30Commands:
31 gui [ARGS] Start GUI, equivalent to running 'bitcoin-qt [ARGS]' or 'bitcoin-gui [ARGS]'.
32 node [ARGS] Start node, equivalent to running 'bitcoind [ARGS]' or 'bitcoin-node [ARGS]'.
33 rpc [ARGS] Call RPC method, equivalent to running 'bitcoin-cli -named [ARGS]'.
34 wallet [ARGS] Call wallet command, equivalent to running 'bitcoin-wallet [ARGS]'.
35 tx [ARGS] Manipulate hex-encoded transactions, equivalent to running 'bitcoin-tx [ARGS]'.
36 help Show full help message.
37)";
38
39static constexpr auto HELP_FULL = R"(
40Additional less commonly used commands:
41 bench [ARGS] Run bench command, equivalent to running 'bench_bitcoin [ARGS]'.
42 chainstate [ARGS] Run bitcoin kernel chainstate util, equivalent to running 'bitcoin-chainstate [ARGS]'.
43 test [ARGS] Run unit tests, equivalent to running 'test_bitcoin [ARGS]'.
44 test-gui [ARGS] Run GUI unit tests, equivalent to running 'test_bitcoin-qt [ARGS]'.
45)";
46
47static constexpr auto HELP_SHORT = R"(
48Run '%s help' to see additional commands (e.g. for testing and debugging).
49)";
50
52 std::optional<bool> use_multiprocess;
53 bool show_version{false};
54 bool show_help{false};
55 std::string_view command;
56 std::vector<const char*> args;
57};
58
59CommandLine ParseCommandLine(int argc, char* argv[]);
61static void ExecCommand(const std::vector<const char*>& args, std::string_view argv0);
62
63int main(int argc, char* argv[])
64{
66
67 try {
68 CommandLine cmd{ParseCommandLine(argc, argv)};
69 if (cmd.show_version) {
70 tfm::format(std::cout, "%s version %s\n%s", CLIENT_NAME, FormatFullVersion(), FormatParagraph(LicenseInfo()));
71 return EXIT_SUCCESS;
72 }
73
74 std::string exe_name{fs::PathToString(fs::PathFromString(argv[0]).filename())};
75 std::vector<const char*> args;
76 if (cmd.show_help || cmd.command.empty()) {
77 tfm::format(std::cout, HELP_USAGE, exe_name);
78 if (cmd.show_help) {
79 tfm::format(std::cout, HELP_FULL);
80 return EXIT_SUCCESS;
81 } else {
82 tfm::format(std::cout, HELP_SHORT, exe_name);
83 return EXIT_FAILURE;
84 }
85 } else if (cmd.command == "gui") {
86 args.emplace_back(UseMultiprocess(cmd) ? "bitcoin-gui" : "bitcoin-qt");
87 } else if (cmd.command == "node") {
88 args.emplace_back(UseMultiprocess(cmd) ? "bitcoin-node" : "bitcoind");
89 } else if (cmd.command == "rpc") {
90 args.emplace_back("bitcoin-cli");
91 // Since "bitcoin rpc" is a new interface that doesn't need to be
92 // backward compatible, enable -named by default so it is convenient
93 // for callers to use a mix of named and unnamed parameters. Callers
94 // can override this by specifying -nonamed, but should not need to
95 // unless they are passing string values containing '=' characters
96 // as unnamed parameters.
97 args.emplace_back("-named");
98 } else if (cmd.command == "wallet") {
99 args.emplace_back("bitcoin-wallet");
100 } else if (cmd.command == "tx") {
101 args.emplace_back("bitcoin-tx");
102 } else if (cmd.command == "bench") {
103 args.emplace_back("bench_bitcoin");
104 } else if (cmd.command == "chainstate") {
105 args.emplace_back("bitcoin-chainstate");
106 } else if (cmd.command == "test") {
107 args.emplace_back("test_bitcoin");
108 } else if (cmd.command == "test-gui") {
109 args.emplace_back("test_bitcoin-qt");
110 } else if (cmd.command == "util") {
111 args.emplace_back("bitcoin-util");
112 } else {
113 throw std::runtime_error(strprintf("Unrecognized command: '%s'", cmd.command));
114 }
115 if (!args.empty()) {
116 args.insert(args.end(), cmd.args.begin(), cmd.args.end());
117 ExecCommand(args, argv[0]);
118 }
119 } catch (const std::exception& e) {
120 tfm::format(std::cerr, "Error: %s\nTry '%s --help' for more information.\n", e.what(), argv[0]);
121 return EXIT_FAILURE;
122 }
123 return EXIT_SUCCESS;
124}
125
126CommandLine ParseCommandLine(int argc, char* argv[])
127{
129 cmd.args.reserve(argc);
130 for (int i = 1; i < argc; ++i) {
131 std::string_view arg = argv[i];
132 if (!cmd.command.empty()) {
133 cmd.args.emplace_back(argv[i]);
134 } else if (arg == "-m" || arg == "--multiprocess") {
135 cmd.use_multiprocess = true;
136 } else if (arg == "-M" || arg == "--monolithic") {
137 cmd.use_multiprocess = false;
138 } else if (arg == "-v" || arg == "--version") {
139 cmd.show_version = true;
140 } else if (arg == "-h" || arg == "--help" || arg == "help") {
141 cmd.show_help = true;
142 } else if (arg.starts_with("-")) {
143 throw std::runtime_error(strprintf("Unknown option: %s", arg));
144 } else if (!arg.empty()) {
145 cmd.command = arg;
146 }
147 }
148 return cmd;
149}
150
152{
153 // If -m or -M options were explicitly specified, there is no need to
154 // further parse arguments to determine which to use.
155 if (cmd.use_multiprocess) return *cmd.use_multiprocess;
156
159 std::string error_message;
160 auto argv{cmd.args};
161 argv.insert(argv.begin(), nullptr);
162 if (!args.ParseParameters(argv.size(), argv.data(), error_message)) {
163 tfm::format(std::cerr, "Warning: failed to parse subcommand command line options: %s\n", error_message);
164 }
165 if (!args.ReadConfigFiles(error_message, true)) {
166 tfm::format(std::cerr, "Warning: failed to parse subcommand config: %s\n", error_message);
167 }
169
170 // If any -ipc* options are set these need to be processed by a
171 // multiprocess-capable binary.
172 return args.IsArgSet("-ipcbind") || args.IsArgSet("-ipcconnect") || args.IsArgSet("-ipcfd");
173}
174
187//
193static void ExecCommand(const std::vector<const char*>& args, std::string_view wrapper_argv0)
194{
195 // Construct argument string for execvp
196 std::vector<const char*> exec_args{args};
197 exec_args.emplace_back(nullptr);
198
199 // Try to call ExecVp with given exe path.
200 auto try_exec = [&](fs::path exe_path, bool allow_notfound = true) {
201 std::string exe_path_str{fs::PathToString(exe_path)};
202 exec_args[0] = exe_path_str.c_str();
203 if (util::ExecVp(exec_args[0], (char*const*)exec_args.data()) == -1) {
204 if (allow_notfound && errno == ENOENT) return false;
205 throw std::system_error(errno, std::system_category(), strprintf("execvp failed to execute '%s'", exec_args[0]));
206 }
207 throw std::runtime_error("execvp returned unexpectedly");
208 };
209
210 // Get the wrapper executable path.
211 const fs::path wrapper_path{util::GetExePath(wrapper_argv0)};
212
213 // Try to resolve any symlinks and figure out the directory containing the wrapper executable.
214 std::error_code ec;
215 auto wrapper_dir{fs::weakly_canonical(wrapper_path, ec)};
216 if (wrapper_dir.empty()) wrapper_dir = wrapper_path; // Restore previous path if weakly_canonical failed.
217 wrapper_dir = wrapper_dir.parent_path();
218
219 // Get path of the executable to be invoked.
220 const fs::path arg0{fs::PathFromString(args[0])};
221
222 // Decide whether to fall back to the operating system to search for the
223 // specified executable. Avoid doing this if it looks like the wrapper
224 // executable was invoked by path, rather than by search, to avoid
225 // unintentionally launching system executables in a local build.
226 // (https://github.com/bitcoin/bitcoin/pull/31375#discussion_r1861814807)
227 const bool fallback_os_search{!fs::PathFromString(std::string{wrapper_argv0}).has_parent_path()};
228
229 // If wrapper is installed in a bin/ directory, look for target executable
230 // in libexec/
231 (wrapper_dir.filename() == "bin" && try_exec(wrapper_dir.parent_path() / "libexec" / arg0.filename())) ||
232#ifdef WIN32
233 // Otherwise check the "daemon" subdirectory in a windows install.
234 (!wrapper_dir.empty() && try_exec(wrapper_dir / "daemon" / arg0.filename())) ||
235#endif
236 // Otherwise look for target executable next to current wrapper
237 (!wrapper_dir.empty() && try_exec(wrapper_dir / arg0.filename(), fallback_os_search)) ||
238 // Otherwise just look on the system path.
239 (fallback_os_search && try_exec(arg0.filename(), false));
240}
const auto cmd
SetupEnvironment()
Definition: system.cpp:66
return EXIT_SUCCESS
int main(int argc, char *argv[])
Definition: bitcoin.cpp:63
static constexpr auto HELP_FULL
Definition: bitcoin.cpp:39
static constexpr auto HELP_SHORT
Definition: bitcoin.cpp:47
static void ExecCommand(const std::vector< const char * > &args, std::string_view argv0)
Execute the specified bitcoind, bitcoin-qt or other command line in args using src,...
Definition: bitcoin.cpp:193
const TranslateFn G_TRANSLATION_FUN
Translate string to current locale using Qt.
Definition: bitcoin.cpp:20
bool UseMultiprocess(const CommandLine &cmd)
Definition: bitcoin.cpp:151
static constexpr auto HELP_USAGE
Definition: bitcoin.cpp:22
CommandLine ParseCommandLine(int argc, char *argv[])
Definition: bitcoin.cpp:126
ArgsManager & args
Definition: bitcoind.cpp:277
@ ALLOW_ANY
disable validation
Definition: args.h:106
std::string GetChainTypeString() const
Returns the appropriate chain type string from the program arguments.
Definition: args.cpp:794
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: args.cpp:177
void SetDefaultFlags(std::optional< unsigned int >)
Set default flags to return for an unknown arg.
Definition: args.cpp:270
void SelectConfigNetwork(const std::string &network)
Select the network in use.
Definition: args.cpp:171
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: args.cpp:375
bool ReadConfigFiles(std::string &error, bool ignore_invalid_keys=false)
Definition: config.cpp:122
std::string FormatFullVersion()
std::string LicenseInfo()
Returns licensing information (for -version)
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:157
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:180
void format(std::ostream &out, FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1079
int ExecVp(const char *file, char *const argv[])
Cross-platform wrapper for POSIX execvp function.
Definition: exec.cpp:21
fs::path GetExePath(std::string_view argv0)
Return path to current executable assuming it was invoked with argv0.
Definition: exec.cpp:40
std::string_view command
Definition: bitcoin.cpp:55
bool show_version
Definition: bitcoin.cpp:53
std::vector< const char * > args
Definition: bitcoin.cpp:56
bool show_help
Definition: bitcoin.cpp:54
std::optional< bool > use_multiprocess
Definition: bitcoin.cpp:52
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
std::function< std::string(const char *)> TranslateFn
Translate a message to the native language of the user.
Definition: translation.h:16
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.