24#ifndef _PROCESS_LAUNCHER_H_
25#define _PROCESS_LAUNCHER_H_
33#include <system_error>
38#define _CRT_SECURE_NO_WARNINGS 1
47#include "harness_export.h"
56HARNESS_EXPORT std::string cmdline_quote_arg(
const std::string &arg);
57HARNESS_EXPORT std::string cmdline_from_args(
58 const std::string &executable_path,
const std::vector<std::string> &args);
60class HARNESS_EXPORT Handle {
64 Handle(
HANDLE hndl) : handle_(hndl) {}
65 Handle(
const Handle &) =
delete;
66 Handle(Handle &&other) : handle_(other.release()) {}
67 Handle &operator=(
const Handle &) =
delete;
68 Handle &operator=(Handle &&other) {
69 handle_ = other.release();
73 ~Handle() {
close(); }
75 bool is_open()
const {
return handle_ != INVALID_HANDLE_VALUE; }
81 HANDLE release() {
return std::exchange(handle_, INVALID_HANDLE_VALUE); }
94 HANDLE handle_{INVALID_HANDLE_VALUE};
97class HARNESS_EXPORT FileHandle :
public Handle {
102 const void *
buf, DWORD
buf_size, OVERLAPPED *overlapped =
nullptr);
105 OVERLAPPED *overlapped =
nullptr);
108class HARNESS_EXPORT PipeHandle :
public FileHandle {
110 using FileHandle::FileHandle;
114 DWORD totalBytesAvail;
115 DWORD bytesLeftThisMessage;
121class HARNESS_EXPORT ProcessHandle :
public Handle {
123 using Handle::Handle;
130class HARNESS_EXPORT ThreadHandle :
public Handle {
135class HARNESS_EXPORT JobObject {
137 JobObject() =
default;
139 JobObject(Handle hndl) : handle_(
std::move(hndl)) {}
144 JOBOBJECTINFOCLASS info_class,
void *info, DWORD info_size);
148 bool is_open()
const {
return handle_.is_open(); }
158 Process(ProcessHandle process_hndl, ThreadHandle thread_hndl,
160 : process_handle_(
std::move(process_hndl)),
161 thread_handle_(
std::move(thread_hndl)),
162 process_id_(process_id),
166 const char *app_name,
char *cmd_line, SECURITY_ATTRIBUTES *process_attrs,
167 SECURITY_ATTRIBUTES *thread_attrs, BOOL inherit_handles,
168 DWORD creation_flags,
void *env,
const char *current_dir,
169 STARTUPINFO *startup_info);
171 ProcessHandle &process_handle() {
return process_handle_; }
172 const ProcessHandle &process_handle()
const {
return process_handle_; }
174 ThreadHandle &thread_handle() {
return thread_handle_; }
175 const ThreadHandle &thread_handle()
const {
return thread_handle_; }
177 DWORD process_id()
const {
return process_id_; }
178 DWORD
thread_id()
const {
return thread_id_; }
181 ProcessHandle process_handle_;
182 ThreadHandle thread_handle_;
187class HARNESS_EXPORT Pipe {
189 Pipe(PipeHandle rd, PipeHandle wr) : rd_(
std::move(rd)), wr_(
std::move(wr)) {}
192 SECURITY_ATTRIBUTES *sec_attrs, DWORD sz);
194 PipeHandle &read_handle() {
return rd_; }
195 PipeHandle &write_handle() {
return wr_; }
202class ThreadAttributeList {
204 ThreadAttributeList(LPPROC_THREAD_ATTRIBUTE_LIST attr_list)
205 : attr_list_(attr_list) {}
207 ThreadAttributeList(
const ThreadAttributeList &) =
delete;
208 ThreadAttributeList &operator=(
const ThreadAttributeList &) =
delete;
210 ThreadAttributeList(ThreadAttributeList &&other)
211 : attr_list_(
std::exchange(other.attr_list_,
nullptr)) {}
212 ThreadAttributeList &operator=(ThreadAttributeList &other) {
213 attr_list_ = std::exchange(other.attr_list_,
nullptr);
218 ~ThreadAttributeList() {
219 if (attr_list_ !=
nullptr) DeleteProcThreadAttributeList(attr_list_);
226 void *
value,
size_t value_size,
228 size_t *return_size)
const;
230 LPPROC_THREAD_ATTRIBUTE_LIST
get()
const {
return attr_list_; }
233 LPPROC_THREAD_ATTRIBUTE_LIST attr_list_;
253 std::vector<std::pair<std::string, std::string>> penv_vars,
254 bool predirect_stderr =
true)
255 : executable_path(
std::move(pexecutable_path)),
256 args(
std::move(pargs)),
257 env_vars(
std::move(penv_vars)),
258 redirect_stderr(predirect_stderr) {}
276 [[nodiscard]] std::string get_cmd_line()
const;
278 [[nodiscard]] std::string
executable()
const {
return executable_path; }
283 std::vector<std::pair<std::string, std::string>>
env_vars;
285 win32::PipeHandle child_in_wr;
286 win32::PipeHandle child_out_rd;
287 win32::Process process_;
290 int fd_in[2]{-1, -1};
291 int fd_out[2]{-1, -1};
311 std::vector<std::pair<std::string, std::string>> penv_vars,
312 bool predirect_stderr =
true)
314 std::move(penv_vars), predirect_stderr) {}
323 is_alive(std::exchange(rhs.is_alive,
false)) {}
329 void start(
bool use_std_io_handlers =
false);
331 void start(
bool use_stdout_handler,
bool use_stdin_handler);
349 int write(
const char *
buf,
size_t count);
354 exit_status_type kill();
405 std::error_code send_shutdown_event(
406 ShutdownEvent
event = ShutdownEvent::TERM)
const noexcept;
417 exit_status_type
close();
422 bool is_alive{
false};
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:247
exit status of processes.
Definition: exit_status.h:47
Runs a specified command line and calls a callback for all data that is written by the child program ...
Definition: create_def.cc:125
Definition: process_launcher.h:301
ProcessLauncher(ProcessLauncher &&rhs) noexcept
Definition: process_launcher.h:321
ProcessLauncher(std::string pexecutable_path, std::vector< std::string > pargs, std::vector< std::pair< std::string, std::string > > penv_vars, bool predirect_stderr=true)
Creates a new process and launch it.
Definition: process_launcher.h:310
SpawnedProcess::id_type process_id_type
Definition: process_launcher.h:357
std::mutex fd_out_mtx_
Definition: process_launcher.h:420
ProcessLauncher operator=(const ProcessLauncher &)=delete
std::mutex fd_in_mtx_
Definition: process_launcher.h:419
ShutdownEvent
Definition: process_launcher.h:393
SpawnedProcess::handle_type process_handle_type
Definition: process_launcher.h:356
ProcessLauncher(const ProcessLauncher &)=delete
an alive, spawned process
Definition: process_launcher.h:250
virtual ~SpawnedProcess()=default
bool redirect_stderr
Definition: process_launcher.h:293
pid_t id_type
Definition: process_launcher.h:273
std::string executable_path
Definition: process_launcher.h:281
std::vector< std::pair< std::string, std::string > > env_vars
Definition: process_launcher.h:283
pid_t handle_type
Definition: process_launcher.h:272
SpawnedProcess(const SpawnedProcess &)=delete
SpawnedProcess(std::string pexecutable_path, std::vector< std::string > pargs, std::vector< std::pair< std::string, std::string > > penv_vars, bool predirect_stderr=true)
Definition: process_launcher.h:252
std::string executable() const
Definition: process_launcher.h:278
std::vector< std::string > args
Definition: process_launcher.h:282
SpawnedProcess(SpawnedProcess &&)=default
SpawnedProcess & operator=(const SpawnedProcess &)=delete
SpawnedProcess & operator=(SpawnedProcess &&)=default
Definition: expected.h:286
constexpr DWORD buf_size
Definition: create_def.cc:229
static int flags[50]
Definition: hp_test1.cc:40
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
static mi_bit_type mask[]
Definition: mi_packrec.cc:141
static my_thread_id thread_id
Definition: my_thr_init.cc:60
static int count
Definition: myisam_ftdump.cc:45
static uint update
Definition: myisamlog.cc:94
Definition: buf0block_hint.cc:30
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:498
std::chrono::milliseconds milliseconds
Definition: authorize_manager.cc:67
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
static int wait(mysql_cond_t *that, mysql_mutex_t *mutex_arg, const char *, unsigned int)
Definition: mysql_cond_v1_native.cc:62
static mysql_service_status_t get(THD **thd) noexcept
Definition: mysql_current_thread_reader_all_empty.cc:31
static mysql_service_status_t create(my_h_string *) noexcept
Definition: mysql_string_all_empty.cc:43
stdx::expected< void, std::error_code > close(file_handle_type native_handle)
close file handle.
Definition: file.h:239
stdx::expected< size_t, std::error_code > write(SyncWriteStream &stream, const ConstBufferSequence &buffers)
Definition: buffer.h:977
stdx::expected< size_t, std::error_code > read(SyncReadStream &stream, const MutableBufferSequence &buffers)
Definition: buffer.h:835
Definition: gcs_xcom_synode.h:64
native_handle_type native_handle()
Definition: process.h:56
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2880
required string event
Definition: replication_group_member_actions.proto:32
@ KILL
Definition: task.h:232
#define HANDLE
Definition: violite.h:159