| 1 | // Copyright (c) 2018-2022, The Monero Project |
| 2 | |
| 3 | // |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without modification, are |
| 7 | // permitted provided that the following conditions are met: |
| 8 | // |
| 9 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | // conditions and the following disclaimer. |
| 11 | // |
| 12 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | // of conditions and the following disclaimer in the documentation and/or other |
| 14 | // materials provided with the distribution. |
| 15 | // |
| 16 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 17 | // used to endorse or promote products derived from this software without specific |
| 18 | // prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 21 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include <errno.h> |
| 31 | #include <unistd.h> |
| 32 | #include <sys/types.h> |
| 33 | #ifdef _WIN32 |
| 34 | #include <boost/algorithm/string/join.hpp> |
| 35 | #include <boost/scope_exit.hpp> |
| 36 | #include <windows.h> |
| 37 | #else |
| 38 | #include <sys/wait.h> |
| 39 | #include <signal.h> |
| 40 | #endif |
| 41 | |
| 42 | #include "misc_log_ex.h" |
| 43 | #include "util.h" |
| 44 | #include "spawn.h" |
| 45 | |
| 46 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 47 | #define MONERO_DEFAULT_LOG_CATEGORY "spawn" |
| 48 | |
| 49 | namespace tools |
| 50 | { |
| 51 | |
| 52 | int spawn(const char *filename, const std::vector<std::string>& args, bool wait) |
| 53 | { |
| 54 | #ifdef _WIN32 |
| 55 | std::string joined = boost::algorithm::join(args, " "); |
| 56 | char *commandLine = !joined.empty() ? &joined[0] : nullptr; |
| 57 | STARTUPINFOA si = {}; |
| 58 | si.cb = sizeof(si); |
| 59 | PROCESS_INFORMATION pi; |
| 60 | if (!CreateProcessA(filename, commandLine, nullptr, nullptr, false, 0, nullptr, nullptr, &si, &pi)) |
| 61 | { |
| 62 | MERROR("CreateProcess failed. Error code " << GetLastError()); |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | BOOST_SCOPE_EXIT(&pi) |
| 67 | { |
| 68 | CloseHandle(pi.hThread); |
| 69 | CloseHandle(pi.hProcess); |
| 70 | } |
| 71 | BOOST_SCOPE_EXIT_END |
| 72 | |
| 73 | if (!wait) |
| 74 | { |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | DWORD result = WaitForSingleObject(pi.hProcess, INFINITE); |
| 79 | if (result != WAIT_OBJECT_0) |
| 80 | { |
| 81 | MERROR("WaitForSingleObject failed. Result " << result << ", error code " << GetLastError()); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | DWORD exitCode; |
| 86 | if (!GetExitCodeProcess(pi.hProcess, &exitCode)) |
| 87 | { |
| 88 | MERROR("GetExitCodeProcess failed. Error code " << GetLastError()); |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | MINFO("Child exited with " << exitCode); |
| 93 | return static_cast<int>(exitCode); |
| 94 | #else |
| 95 | std::vector<char*> argv(args.size() + 1); |
| 96 | for (size_t n = 0; n < args.size(); ++n) |
| 97 | argv[n] = (char*)args[n].c_str(); |
| 98 | argv[args.size()] = NULL; |
| 99 | |
| 100 | pid_t pid = fork(); |
| 101 | if (pid < 0) |
| 102 | { |
| 103 | MERROR("Error forking: " << strerror(errno)); |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | // child |
| 108 | if (pid == 0) |
| 109 | { |
| 110 | tools::closefrom(3); |
| 111 | close(0); |
| 112 | char *envp[] = {NULL}; |
| 113 | execve(filename, argv.data(), envp); |
| 114 | MERROR("Failed to execve: " << strerror(errno)); |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | // parent |
| 119 | if (pid > 0) |
| 120 | { |
| 121 | if (!wait) |
| 122 | { |
| 123 | signal(SIGCHLD, SIG_IGN); |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | while (1) |
| 128 | { |
| 129 | int wstatus = 0; |
| 130 | pid_t w = waitpid(pid, &wstatus, WUNTRACED | WCONTINUED); |
| 131 | if (w < 0) { |
| 132 | MERROR("Error waiting for child: " << strerror(errno)); |
| 133 | return -1; |
| 134 | } |
| 135 | if (WIFEXITED(wstatus)) |
| 136 | { |
| 137 | MINFO("Child exited with " << WEXITSTATUS(wstatus)); |
| 138 | return WEXITSTATUS(wstatus); |
| 139 | } |
| 140 | if (WIFSIGNALED(wstatus)) |
| 141 | { |
| 142 | MINFO("Child killed by " << WEXITSTATUS(wstatus)); |
| 143 | return WEXITSTATUS(wstatus); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | MERROR("Secret passage found"); |
| 148 | return -1; |
| 149 | #endif |
| 150 | } |
| 151 | |
| 152 | } |