1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*
Copyright (c) 2019, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "process_wrapper.h"
#include <algorithm>
#include <array>
#include <system_error>
#include <thread>
#include <vector>
#include "mysql/harness/string_utils.h" // split_string
using namespace std::chrono_literals;
ProcessWrapper::ProcessWrapper(
const std::string &app_cmd, const std::vector<std::string> &args,
const std::vector<std::pair<std::string, std::string>> &env_vars,
bool include_stderr, OutputResponder &output_responder)
: launcher_(app_cmd, args, env_vars, include_stderr),
output_responder_(output_responder) {
launcher_.start();
output_reader_ = std::thread([&]() {
while (!output_reader_stop_) {
try {
read_and_autorespond_to_output(5ms);
if (output_reader_stop_) break;
std::this_thread::sleep_for(5ms);
} catch (const std::system_error &e) {
if (std::errc::resource_unavailable_try_again == e.code() ||
std::errc::permission_denied == e.code()) {
continue;
}
// if the underlying process went away we may get "Bad file descriptor"
// exception here
if (std::errc::bad_file_descriptor == e.code() ||
std::errc::invalid_argument == e.code()) {
break;
}
throw;
}
}
});
}
int ProcessWrapper::kill() {
try {
exit_status_ = launcher_.kill();
stop_output_reader_thread();
} catch (const std::exception &e) {
fprintf(stderr, "failed killing process %s: %s\n",
launcher_.get_cmd_line().c_str(), e.what());
return 1;
}
if (auto code = exit_status_->exited()) {
return *code;
} else {
throw std::runtime_error("signalled?");
}
}
mysql_harness::ProcessLauncher::exit_status_type ProcessWrapper::native_kill() {
try {
exit_status_ = launcher_.kill();
stop_output_reader_thread();
} catch (const std::exception &e) {
fprintf(stderr, "failed killing process %s: %s\n",
launcher_.get_cmd_line().c_str(), e.what());
return {1};
}
return exit_status_.value();
}
int ProcessWrapper::wait_for_exit(std::chrono::milliseconds timeout) {
auto exit_code = native_wait_for_exit(timeout);
if (auto code = exit_code.exited()) {
return *code;
} else {
throw std::runtime_error("signalled?");
}
}
mysql_harness::ProcessLauncher::exit_status_type
ProcessWrapper::native_wait_for_exit(std::chrono::milliseconds timeout) {
if (exit_status_) return native_exit_code();
using clock_type = std::chrono::steady_clock;
auto step = 1ms;
if (getenv("WITH_VALGRIND")) {
timeout *= 10;
step *= 200;
}
const auto end_time = clock_type::now() + timeout;
// The child might be blocked on input/output (for example password prompt),
// so we wait giving a output thread a change to deal with it
do {
auto exit_status_res = launcher_.exit_code();
if (exit_status_res) {
exit_status_ = *exit_status_res;
// the child exited, but there might still be some data left in the pipe
// to read, so let's consume it all
stop_output_reader_thread();
while (read_and_autorespond_to_output(step,
/*autoresponder_enabled=*/false))
;
return exit_status_.value();
}
const auto ec = exit_status_res.error();
if (ec != std::errc::timed_out) throw std::system_error(ec);
std::this_thread::sleep_for(step);
} while (clock_type::now() < end_time);
throw std::system_error(make_error_code(std::errc::timed_out));
}
bool ProcessWrapper::expect_output(const std::string &str, bool regex,
std::chrono::milliseconds timeout) {
auto step = 5ms;
if (getenv("WITH_VALGRIND")) {
timeout *= 10;
step *= 10;
}
const auto until = std::chrono::steady_clock::now() + timeout;
for (;;) {
bool has_exited = has_exit_code();
if (output_contains(str, regex)) return true;
// no need to wait any longer, as there is no further output
// as the process has already exited.
if (has_exited || (std::chrono::steady_clock::now() > until)) {
return false;
}
std::this_thread::sleep_for(step);
}
}
bool ProcessWrapper::output_contains(const std::string &str, bool regex) const {
std::lock_guard<std::mutex> output_lock(output_mtx_);
if (!regex) {
return execute_output_raw_.find(str) != std::string::npos;
}
// regex
return pattern_found(execute_output_raw_, str);
}
bool ProcessWrapper::read_and_autorespond_to_output(
std::chrono::milliseconds timeout, bool autoresponder_enabled /*= true*/) {
std::array<char, kReadBufSize> read_buf = {0};
// blocks until timeout expires (very likely) or until at least one byte is
// read (unlikely) throws std::runtime_error on read error
int bytes_read =
launcher_.read(read_buf.data(), read_buf.size() - 1,
timeout); // cmd_output may contain multiple lines
if (bytes_read <= 0) return false;
#ifdef _WIN32
// On Windows we get \r\n instead of \n, so we need to get rid of the \r
// everywhere. As surprising as it is, WIN32API doesn't provide the
// automatic conversion:
// https://stackoverflow.com/questions/18294650/win32-changing-to-binary-mode-childs-stdout-pipe
{
char *new_end =
std::remove(read_buf.data(), read_buf.data() + bytes_read, '\r');
*new_end = '\0';
bytes_read = new_end - read_buf.data();
}
#endif
std::string_view cmd_output(read_buf.data(), bytes_read);
{
std::lock_guard<std::mutex> output_lock(output_mtx_);
execute_output_raw_ += cmd_output;
}
if (autoresponder_enabled) {
autorespond_to_matching_lines(cmd_output);
}
return true;
}
void ProcessWrapper::autorespond_to_matching_lines(
const std::string_view &cmd_output) {
// returned lines do not contain the \n
std::vector<std::string> lines =
mysql_harness::split_string(cmd_output, '\n');
if (lines.empty()) return;
// it is possible that the last line from the previous call did not match
// because it arrived incomplete. Here we try an assumption that the first
// line is a continuation of last line from previous call.
if (last_line_read_.size() &&
autorespond_on_matching_pattern(last_line_read_ + lines.front())) {
// indeed, it was a continuation of previous line. So now we must prevent
// both fragments from being used again
lines.erase(lines.begin());
last_line_read_.clear();
if (lines.empty()) return;
}
// try matching all but last line
for (auto it = lines.cbegin(); it != lines.cend() - 1; ++it)
autorespond_on_matching_pattern(*it);
// try matching the last line
if (autorespond_on_matching_pattern(lines.back()))
last_line_read_.clear();
else
// last line failed to match, it may be because it arrived incomplete. Save
// it for the next time
last_line_read_ = lines.back();
}
bool ProcessWrapper::autorespond_on_matching_pattern(const std::string &line) {
const std::string resp = output_responder_(line);
if (!resp.empty()) {
launcher_.write(resp.c_str(), resp.length());
return true;
}
return false;
}
std::string ProcessWrapper::get_logfile_content(
const std::string &file_name /*= ""*/,
const std::string &file_path /*= ""*/, size_t lines_limit /*= 0*/) const {
const std::string path = file_path.empty() ? logging_dir_ : file_path;
const std::string name = file_name.empty() ? logging_file_ : file_name;
if (name.empty()) return "";
const auto content = get_file_output(name, path);
if (lines_limit > 0)
return mysql_harness::limit_lines(content, lines_limit, "<snap>\n");
else
return content;
}
|