Branch data Line data Source code
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 <util/fs.h>
9 : : #include <util/exec.h>
10 : : #include <util/strencodings.h>
11 : : #include <util/translation.h>
12 : :
13 : : #include <iostream>
14 : : #include <string>
15 : : #include <tinyformat.h>
16 : : #include <vector>
17 : :
18 : : const TranslateFn G_TRANSLATION_FUN{nullptr};
19 : :
20 : : static constexpr auto HELP_USAGE = R"(Usage: %s [OPTIONS] COMMAND...
21 : :
22 : : Options:
23 : : -m, --multiprocess Run multiprocess binaries bitcoin-node, bitcoin-gui.
24 : : -M, --monolithic Run monolithic binaries bitcoind, bitcoin-qt. (Default behavior)
25 : : -v, --version Show version information
26 : : -h, --help Show this help message
27 : :
28 : : Commands:
29 : : gui [ARGS] Start GUI, equivalent to running 'bitcoin-qt [ARGS]' or 'bitcoin-gui [ARGS]'.
30 : : node [ARGS] Start node, equivalent to running 'bitcoind [ARGS]' or 'bitcoin-node [ARGS]'.
31 : : rpc [ARGS] Call RPC method, equivalent to running 'bitcoin-cli -named [ARGS]'.
32 : : wallet [ARGS] Call wallet command, equivalent to running 'bitcoin-wallet [ARGS]'.
33 : : tx [ARGS] Manipulate hex-encoded transactions, equivalent to running 'bitcoin-tx [ARGS]'.
34 : : help [-a] Show this help message. Include -a or --all to show additional commands.
35 : : )";
36 : :
37 : : static constexpr auto HELP_EXTRA = R"(
38 : : Additional less commonly used commands:
39 : : bench [ARGS] Run bench command, equivalent to running 'bench_bitcoin [ARGS]'.
40 : : chainstate [ARGS] Run bitcoin kernel chainstate util, equivalent to running 'bitcoin-chainstate [ARGS]'.
41 : : test [ARGS] Run unit tests, equivalent to running 'test_bitcoin [ARGS]'.
42 : : test-gui [ARGS] Run GUI unit tests, equivalent to running 'test_bitcoin-qt [ARGS]'.
43 : : )";
44 : :
45 : 0 : struct CommandLine {
46 : : bool use_multiprocess{false};
47 : : bool show_version{false};
48 : : bool show_help{false};
49 : : bool show_help_all{false};
50 : : std::string_view command;
51 : : std::vector<const char*> args;
52 : : };
53 : :
54 : : CommandLine ParseCommandLine(int argc, char* argv[]);
55 : : static void ExecCommand(const std::vector<const char*>& args, std::string_view argv0);
56 : :
57 : 0 : int main(int argc, char* argv[])
58 : : {
59 : 0 : try {
60 [ # # ]: 0 : CommandLine cmd{ParseCommandLine(argc, argv)};
61 [ # # ]: 0 : if (cmd.show_version) {
62 [ # # # # : 0 : tfm::format(std::cout, "%s version %s\n%s", CLIENT_NAME, FormatFullVersion(), FormatParagraph(LicenseInfo()));
# # # # ]
63 : 0 : return EXIT_SUCCESS;
64 : : }
65 : :
66 : 0 : std::vector<const char*> args;
67 [ # # # # ]: 0 : if (cmd.show_help || cmd.command.empty()) {
68 [ # # ]: 0 : tfm::format(std::cout, HELP_USAGE, argv[0]);
69 [ # # # # ]: 0 : if (cmd.show_help_all) tfm::format(std::cout, HELP_EXTRA);
70 [ # # ]: 0 : return cmd.show_help ? EXIT_SUCCESS : EXIT_FAILURE;
71 [ # # ]: 0 : } else if (cmd.command == "gui") {
72 [ # # # # ]: 0 : args.emplace_back(cmd.use_multiprocess ? "bitcoin-gui" : "bitcoin-qt");
73 [ # # ]: 0 : } else if (cmd.command == "node") {
74 [ # # # # ]: 0 : args.emplace_back(cmd.use_multiprocess ? "bitcoin-node" : "bitcoind");
75 [ # # ]: 0 : } else if (cmd.command == "rpc") {
76 [ # # ]: 0 : args.emplace_back("bitcoin-cli");
77 : : // Since "bitcoin rpc" is a new interface that doesn't need to be
78 : : // backward compatible, enable -named by default so it is convenient
79 : : // for callers to use a mix of named and unnamed parameters. Callers
80 : : // can override this by specifying -nonamed, but should not need to
81 : : // unless they are passing string values containing '=' characters
82 : : // as unnamed parameters.
83 [ # # ]: 0 : args.emplace_back("-named");
84 [ # # ]: 0 : } else if (cmd.command == "wallet") {
85 [ # # ]: 0 : args.emplace_back("bitcoin-wallet");
86 [ # # ]: 0 : } else if (cmd.command == "tx") {
87 [ # # ]: 0 : args.emplace_back("bitcoin-tx");
88 [ # # ]: 0 : } else if (cmd.command == "bench") {
89 [ # # ]: 0 : args.emplace_back("bench_bitcoin");
90 [ # # ]: 0 : } else if (cmd.command == "chainstate") {
91 [ # # ]: 0 : args.emplace_back("bitcoin-chainstate");
92 [ # # ]: 0 : } else if (cmd.command == "test") {
93 [ # # ]: 0 : args.emplace_back("test_bitcoin");
94 [ # # ]: 0 : } else if (cmd.command == "test-gui") {
95 [ # # ]: 0 : args.emplace_back("test_bitcoin-qt");
96 [ # # ]: 0 : } else if (cmd.command == "util") {
97 [ # # ]: 0 : args.emplace_back("bitcoin-util");
98 : : } else {
99 [ # # # # ]: 0 : throw std::runtime_error(strprintf("Unrecognized command: '%s'", cmd.command));
100 : : }
101 [ # # ]: 0 : if (!args.empty()) {
102 [ # # ]: 0 : args.insert(args.end(), cmd.args.begin(), cmd.args.end());
103 [ # # ]: 0 : ExecCommand(args, argv[0]);
104 : : }
105 [ # # ]: 0 : } catch (const std::exception& e) {
106 [ - - ]: 0 : tfm::format(std::cerr, "Error: %s\nTry '%s --help' for more information.\n", e.what(), argv[0]);
107 : 0 : return EXIT_FAILURE;
108 : 0 : }
109 : 0 : return EXIT_SUCCESS;
110 : : }
111 : :
112 : 0 : CommandLine ParseCommandLine(int argc, char* argv[])
113 : : {
114 : 0 : CommandLine cmd;
115 [ # # ]: 0 : cmd.args.reserve(argc);
116 [ # # ]: 0 : for (int i = 1; i < argc; ++i) {
117 [ # # ]: 0 : std::string_view arg = argv[i];
118 [ # # ]: 0 : if (!cmd.command.empty()) {
119 [ # # ]: 0 : cmd.args.emplace_back(argv[i]);
120 [ # # # # ]: 0 : } else if (arg == "-m" || arg == "--multiprocess") {
121 : 0 : cmd.use_multiprocess = true;
122 [ # # # # ]: 0 : } else if (arg == "-M" || arg == "--monolithic") {
123 : 0 : cmd.use_multiprocess = false;
124 [ # # # # ]: 0 : } else if (arg == "-v" || arg == "--version") {
125 : 0 : cmd.show_version = true;
126 [ # # # # : 0 : } else if (arg == "-h" || arg == "--help" || arg == "help") {
# # ]
127 : 0 : cmd.show_help = true;
128 [ # # # # : 0 : } else if (cmd.show_help && (arg == "-a" || arg == "--all")) {
# # ]
129 : 0 : cmd.show_help_all = true;
130 [ # # ]: 0 : } else if (arg.starts_with("-")) {
131 [ # # # # ]: 0 : throw std::runtime_error(strprintf("Unknown option: %s", arg));
132 [ # # ]: 0 : } else if (!arg.empty()) {
133 : 0 : cmd.command = arg;
134 : : }
135 : : }
136 : 0 : return cmd;
137 : 0 : }
138 : :
139 : : //! Execute the specified bitcoind, bitcoin-qt or other command line in `args`
140 : : //! using src, bin and libexec directory paths relative to this executable, where
141 : : //! the path to this executable is specified in `wrapper_argv0`.
142 : : //!
143 : : //! @param args Command line arguments to execute, where first argument should
144 : : //! be a relative path to a bitcoind, bitcoin-qt or other executable
145 : : //! that will be located on the PATH or relative to wrapper_argv0.
146 : : //!
147 : : //! @param wrapper_argv0 String containing first command line argument passed to
148 : : //! main() to run the current executable. This is used to
149 : : //! help determine the path to the current executable and
150 : : //! how to look for new executables.
151 : : //
152 : : //! @note This function doesn't currently print anything but can be debugged
153 : : //! from the command line using strace or dtrace like:
154 : : //!
155 : : //! strace -e trace=execve -s 10000 build/bin/bitcoin ...
156 : : //! dtrace -n 'proc:::exec-success /pid == $target/ { trace(curpsinfo->pr_psargs); }' -c ...
157 : 0 : static void ExecCommand(const std::vector<const char*>& args, std::string_view wrapper_argv0)
158 : : {
159 : : // Construct argument string for execvp
160 : 0 : std::vector<const char*> exec_args{args};
161 [ # # ]: 0 : exec_args.emplace_back(nullptr);
162 : :
163 : : // Try to call ExecVp with given exe path.
164 : 0 : auto try_exec = [&](fs::path exe_path, bool allow_notfound = true) {
165 : 0 : std::string exe_path_str{fs::PathToString(exe_path)};
166 [ # # ]: 0 : exec_args[0] = exe_path_str.c_str();
167 [ # # # # ]: 0 : if (util::ExecVp(exec_args[0], (char*const*)exec_args.data()) == -1) {
168 [ # # # # ]: 0 : if (allow_notfound && errno == ENOENT) return false;
169 [ # # # # ]: 0 : throw std::system_error(errno, std::system_category(), strprintf("execvp failed to execute '%s'", exec_args[0]));
170 : : }
171 [ # # ]: 0 : throw std::runtime_error("execvp returned unexpectedly");
172 : 0 : };
173 : :
174 : : // Get the wrapper executable path.
175 [ # # ]: 0 : const fs::path wrapper_path{util::GetExePath(wrapper_argv0)};
176 : :
177 : : // Try to resolve any symlinks and figure out the directory containing the wrapper executable.
178 [ # # ]: 0 : std::error_code ec;
179 [ # # ]: 0 : fs::path wrapper_dir{fs::weakly_canonical(wrapper_path, ec)};
180 [ # # # # ]: 0 : if (wrapper_dir.empty()) wrapper_dir = wrapper_path; // Restore previous path if weakly_canonical failed.
181 [ # # ]: 0 : wrapper_dir = wrapper_dir.parent_path();
182 : :
183 : : // Get path of the executable to be invoked.
184 [ # # # # ]: 0 : const fs::path arg0{fs::PathFromString(args[0])};
185 : :
186 : : // Decide whether to fall back to the operating system to search for the
187 : : // specified executable. Avoid doing this if it looks like the wrapper
188 : : // executable was invoked by path, rather than by search, to avoid
189 : : // unintentionally launching system executables in a local build.
190 : : // (https://github.com/bitcoin/bitcoin/pull/31375#discussion_r1861814807)
191 [ # # # # ]: 0 : const bool fallback_os_search{!fs::PathFromString(std::string{wrapper_argv0}).has_parent_path()};
192 : :
193 : : // If wrapper is installed in a bin/ directory, look for target executable
194 : : // in libexec/
195 [ # # # # : 0 : (wrapper_dir.filename() == "bin" && try_exec(fs::path{wrapper_dir.parent_path()} / "libexec" / arg0.filename())) ||
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
196 : : #ifdef WIN32
197 : : // Otherwise check the "daemon" subdirectory in a windows install.
198 : : (!wrapper_dir.empty() && try_exec(wrapper_dir / "daemon" / arg0.filename())) ||
199 : : #endif
200 : : // Otherwise look for target executable next to current wrapper
201 [ # # # # : 0 : (!wrapper_dir.empty() && try_exec(wrapper_dir / arg0.filename(), fallback_os_search)) ||
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
202 : : // Otherwise just look on the system path.
203 [ # # # # ]: 0 : (fallback_os_search && try_exec(arg0.filename(), false));
204 : 0 : }
|