Branch data Line data Source code
1 : : // Copyright (c) 2025-present 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 <util/exec.h>
6 : :
7 : : #include <util/fs.h>
8 : : #ifdef WIN32
9 : : #include <util/subprocess.h>
10 : : #endif
11 : :
12 : : #include <cstdlib>
13 : : #include <string>
14 : : #include <system_error>
15 : :
16 : : #ifdef WIN32
17 : : #include <process.h>
18 : : #include <windows.h>
19 : : #else
20 : : #include <unistd.h>
21 : : #endif
22 : :
23 : : namespace util {
24 : 0 : int ExecVp(const char* file, char* const argv[])
25 : : {
26 : : #ifndef WIN32
27 : 0 : return execvp(file, argv);
28 : : #else
29 : : std::vector<std::string> escaped_args;
30 : : for (char* const* arg_ptr{argv}; *arg_ptr; ++arg_ptr) {
31 : : subprocess::util::quote_argument(std::string{*arg_ptr}, escaped_args.emplace_back(), /*force=*/false);
32 : : }
33 : :
34 : : std::vector<const char*> new_argv;
35 : : new_argv.reserve(escaped_args.size() + 1);
36 : : for (const auto& s : escaped_args) new_argv.push_back(s.c_str());
37 : : new_argv.push_back(nullptr);
38 : : // Spawn the child process and wait for it to exit. We can't use _execvp
39 : : // here (the Windows counterpart of POSIX execvp above): on Windows _execvp
40 : : // creates a new child and immediately exits the parent, so anything waiting
41 : : // on the parent (e.g. a test framework) sees it exit with code 0 before the
42 : : // child finishes. Use _P_NOWAIT + _cwait instead of _P_WAIT so a child exit
43 : : // code of -1 is not confused with a spawn failure (both return -1 with _P_WAIT).
44 : : intptr_t child{_spawnvp(_P_NOWAIT, file, new_argv.data())};
45 : : if (child == -1) return -1;
46 : : int status{0};
47 : : if (_cwait(&status, child, _WAIT_CHILD) == -1) return -1;
48 : : _exit(status); // forward child exit code; never returns
49 : : #endif
50 : : }
51 : :
52 : 0 : fs::path GetExePath(std::string_view argv0)
53 : : {
54 : : // Try to figure out where executable is located. This does a simplified
55 : : // search that won't work perfectly on every platform and doesn't need to,
56 : : // as it is only currently being used in a convenience wrapper binary to try
57 : : // to prioritize locally built or installed executables over system
58 : : // executables.
59 [ # # ]: 0 : const fs::path argv0_path{fs::PathFromString(std::string{argv0})};
60 [ # # ]: 0 : fs::path path{argv0_path};
61 : 0 : std::error_code ec;
62 : : #ifndef WIN32
63 : : // If argv0 doesn't contain a path separator, it was invoked from the system
64 : : // PATH and can be searched for there.
65 [ # # ]: 0 : if (!argv0_path.has_parent_path()) {
66 [ # # ]: 0 : if (const char* path_env = std::getenv("PATH")) {
67 : 0 : size_t start{0}, end{0};
68 [ # # ]: 0 : for (std::string_view paths{path_env}; end != std::string_view::npos; start = end + 1) {
69 : 0 : end = paths.find(':', start);
70 [ # # # # ]: 0 : fs::path candidate = fs::path(paths.substr(start, end - start)) / argv0_path;
71 [ # # ]: 0 : if (fs::is_regular_file(candidate, ec)) {
72 [ # # ]: 0 : path = candidate;
73 : 0 : break;
74 : : }
75 : 0 : }
76 : : }
77 : : }
78 : : #else
79 : : wchar_t module_path[MAX_PATH];
80 : : if (GetModuleFileNameW(nullptr, module_path, MAX_PATH) > 0) {
81 : : path = fs::path{module_path};
82 : : }
83 : : #endif
84 : 0 : return path;
85 : 0 : }
86 : :
87 : : } // namespace util
|