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