2
0
mirror of https://github.com/boostorg/process.git synced 2026-01-20 04:42:24 +00:00

Compare commits

..

4 Commits

Author SHA1 Message Date
Klemens Morgenstern
89c5b589c3 Fixed __FreeBSD__ code. 2022-09-14 15:37:56 +08:00
Klemens Morgenstern
4183c66490 Removed android check. 2022-09-14 15:18:34 +08:00
Klemens Morgenstern
9cdc1be868 Ported enum_pids from xproc
Credit to Samuel Venable, @time-killer-games.
2022-09-14 15:15:49 +08:00
Klemens Morgenstern
b9cb5dc0ee Added suspend & resume functions.
Credit to Samuel Venable, @time-killer-games
2022-09-14 12:39:11 +08:00
23 changed files with 451 additions and 1072 deletions

5
.gitignore vendored
View File

@@ -32,8 +32,3 @@
/notes_p.txt
.settings
*.make
*.cmake
*.rsp
*.marks
cmake-*

View File

@@ -1,417 +0,0 @@
// Copyright (c) 2022 Klemens D. Morgenstern
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_PROCESS_V2_GROUP_IMPL_HPP
#define BOOST_PROCESS_V2_GROUP_IMPL_HPP
#include <boost/process/v2/detail/config.hpp>
#if defined(BOOST_PROCESS_V2_STANDALONE)
#include <asio/any_io_executor.hpp>
#include <asio/append.hpp>
#include <asio/compose.hpp>
#include <asio/dispatch.hpp>
#include <asio/post.hpp>
#include <asio/windows/basic_object_handle.hpp>
#else
#include <boost/asio/any_io_executor.hpp>
#include <boost/asio/append.hpp>
#include <boost/asio/compose.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/windows/basic_object_handle.hpp>
#endif
BOOST_PROCESS_V2_BEGIN_NAMESPACE
struct single_process_exit;
namespace detail
{
BOOST_PROCESS_V2_DECL bool job_object_is_empty(HANDLE job_object, error_code & ec);
template<typename Allocator>
BOOST_PROCESS_V2_DECL std::pair<DWORD, DWORD> job_object_something_exited(HANDLE job_object, std::vector<int> & store, Allocator alloc_, error_code & ec)
{
// let's start with a
typename std::aligned_storage<sizeof(JOBOBJECT_BASIC_PROCESS_ID_LIST) * 16, alignof(JOBOBJECT_BASIC_PROCESS_ID_LIST)>::type storage;
JOBOBJECT_BASIC_PROCESS_ID_LIST * id_list = static_cast<JOBOBJECT_BASIC_PROCESS_ID_LIST*>(storage);
if (!QueryInformationJobObject(job_object,
JobObjectBasicProcessIdList,
id_list, sizeof(storage), nullptr))
ec = detail::get_last_error();
if (ec)
return {};
using allocator_type = std::allocator_traits<Allocator>::rebind_alloc<JOBOBJECT_BASIC_PROCESS_ID_LIST>;
allocator_type alloc{alloc_};
std::size_t sz = (std::numeric_limits<std::size_t>::max)();
// dit not fit in the buffer, alloc a buffer
if (id_list.NumberOfAssignedProcesses != id_list.NumberOfProcessIdsInList)
{
// required size:
const additional_size = id_list.NumberOfAssignedProcesses - 1;
sz = (additional_size / (sizeof(JOBOBJECT_BASIC_PROCESS_ID_LIST) / sizeof(ULONG_PTR))) + 1;
id_list = std::allocator_traits<allocator_type>::allocate(alloc_, sz);
if (!QueryInformationJobObject(job_object,
JobObjectBasicProcessIdList,
id_list, sz, nullptr))
{
ec = detail::get_last_error();
goto complete;
}
}
std::pair<DWORD, DWORD> result;
auto * begin = id_list->ProcessIdList,
* end = id_list->ProcessIdList + id_list->NumberOfProcessIdsInList;
for (auto itr = store.begin(); itr != store.end(); itr++)
{
// cross check if it's in the job object
auto it = std::find(begin, end, *itr);
if (it == end) // missing a job
{
result.first = *itr;
// ::GetExitCodeProcess(); // this can't be done based on PID, i need the f'in handle.
}
}
complete:
if (sz != (std::numeric_limits<std::size_t>::max)())
std::allocator_traits<allocator_type>::deallocate(alloc_, id_list, sz);
return result;
}
#if !defined(BOOST_PROCESS_V2_HEADER_ONLY)
//extern template
#endif
struct basic_group_impl_base
{
using native_handle_type = HANDLE;
struct job_object_deleter
{
bool terminate_on_delete = true;
void operator()(HANDLE h)
{
if (h != nullptr && terminate_on_delete)
::TerminateJobObject(h, 255u);
if (h != nullptr && h != INVALID_HANDLE_VALUE)
::CloseHandle(h);
}
};
BOOST_PROCESS_V2_DECL void add(DWORD pid, HANDLE handle, error_code & ec);
BOOST_PROCESS_V2_DECL bool contains(DWORD pid, error_code & ec);
BOOST_PROCESS_V2_DECL void wait_one(DWORD &pid, int &exit_code, error_code & ec);
BOOST_PROCESS_V2_DECL void wait_all(error_code & ec);
BOOST_PROCESS_V2_DECL void interrupt(error_code & ec);
BOOST_PROCESS_V2_DECL void request_exit(error_code & ec);
BOOST_PROCESS_V2_DECL void terminate(error_code & ec);
bool is_open() const
{
return job_object_.get() != nullptr;
}
struct initializer_t
{
basic_group_impl_base * self;
error_code & success_ec;
error_code on_setup(windows::default_launcher & launcher,
const filesystem::path &,
const std::wstring &) const
{
launcher.creation_flags |= CREATE_SUSPENDED;
return error_code {};
};
void on_success(windows::default_launcher & launcher,
const filesystem::path &,
const std::wstring &) const
{
if (!::AssignProcessToJobObject(self->job_object_.get(), launcher.process_information.hProcess))
success_ec = detail::get_last_error();
if (!success_ec &&
::ResumeThread(launcher.process_information.hThread) == static_cast<DWORD>(-1))
success_ec = detail::get_last_error();
};
};
initializer_t get_initializer(error_code & ec)
{
return initializer_t{this, ec};
}
basic_group_impl_base(BOOST_PROCESS_V2_ASIO_NAMESPACE::execution_context & ctx)
{
}
~basic_group_impl_base()
{
}
void detach() {job_object_.get_deleter().terminate_on_delete = false;}
protected:
std::unique_ptr<void, job_object_deleter> job_object_{CreateJobObject(nullptr, nullptr)};
};
template<typename Executor = BOOST_PROCESS_V2_ASIO_NAMESPACE::any_io_executor>
struct basic_group_impl : basic_group_impl_base
{
// Get the native group handle
native_handle_type native_handle() {return port_.native_handle();}
basic_group_impl(Executor exec)
: basic_group_impl_base(BOOST_PROCESS_V2_ASIO_NAMESPACE::query(exec,
BOOST_PROCESS_V2_ASIO_NAMESPACE::execution::context)),
port_(exec, ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 1))
{
error_code ec;
JOBOBJECT_ASSOCIATE_COMPLETION_PORT port{job_object_.get(), port_.native_handle()};
if (!::SetInformationJobObject(
job_object_.get(),
JobObjectAssociateCompletionPortInformation,
&port, sizeof(port)))
ec = v2::detail::get_last_error();
}
template<BOOST_PROCESS_V2_COMPLETION_TOKEN_FOR(void(error_code, single_process_exit))
WaitHandler BOOST_PROCESS_V2_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
BOOST_PROCESS_V2_INITFN_AUTO_RESULT_TYPE(WaitHandler, void (error_code, single_process_exit))
async_wait_one(
WaitHandler &&handler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
{
printf("FOOBAR %d %p\n", port_.native_handle(), this);
return BOOST_PROCESS_V2_ASIO_NAMESPACE::async_compose<
WaitHandler,
void (error_code, single_process_exit)>(
wait_one_op{this},
handler, port_);
}
template<BOOST_PROCESS_V2_COMPLETION_TOKEN_FOR(void(error_code))
WaitHandler BOOST_PROCESS_V2_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
BOOST_PROCESS_V2_INITFN_AUTO_RESULT_TYPE(WaitHandler, void (error_code))
async_wait_all(
WaitHandler &&handler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
{
return BOOST_PROCESS_V2_ASIO_NAMESPACE::async_compose<
WaitHandler,
void (error_code)>(
wait_all_op{this},
handler, port_);
}
using executor_type = Executor;
executor_type get_executor() {return port_.get_executor();}
void wait_all(error_code &ec)
{
if (job_object_is_empty(job_object_.get(), ec))
return ;
DWORD completion_code;
ULONG_PTR completion_key;
LPOVERLAPPED overlapped;
int res;
while (!!(res = GetQueuedCompletionStatus(
port_.native_handle(),
&completion_code,
&completion_key,
&overlapped,
INFINITE)))
{
if (completion_code == JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO)
return;
}
if (!res)
ec = detail::get_last_error();
}
void wait_one(DWORD & pid, int & exit_code, error_code &ec)
{
if (job_object_is_empty(job_object_.get(), ec))
{
ec = BOOST_PROCESS_V2_ASIO_NAMESPACE::error::broken_pipe;
return ;
}
DWORD completion_code;
ULONG_PTR completion_key;
LPOVERLAPPED overlapped;
int res;
while (!!(res = GetQueuedCompletionStatus(
port_.native_handle(),
&completion_code,
&completion_key,
&overlapped,
INFINITE)))
{
if (completion_code == JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO)
{
ec = BOOST_PROCESS_V2_ASIO_NAMESPACE::error::broken_pipe;
return;
}
else if (completion_code == JOB_OBJECT_MSG_EXIT_PROCESS
|| completion_code == JOB_OBJECT_MSG_ABNORMAL_EXIT_PROCESS)
{
pid = *reinterpret_cast<DWORD*>(&overlapped);
const auto p = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
if (p == INVALID_HANDLE_VALUE || p == nullptr)
{
ec = detail::get_last_error();
break;
}
DWORD code;
res = ::GetExitCodeProcess(p, &code);
exit_code = static_cast<int>(code);
break;
}
else if (job_object_is_empty(job_object_.get(), ec))
break;
}
if (!res)
ec = detail::get_last_error();
}
private:
BOOST_PROCESS_V2_ASIO_NAMESPACE::windows::basic_object_handle<Executor> port_;
struct wait_one_op
{
basic_group_impl * this_;
template<typename Self>
void operator()(Self && self)
{
printf("FOOBAR %d %p\n", __LINE__, this_);
BOOST_PROCESS_V2_ASIO_NAMESPACE::dispatch(this_->port_.get_executor(),
BOOST_PROCESS_V2_ASIO_NAMESPACE::append(std::move(self), error_code{}));
}
template<typename Self>
void operator()(Self && self, error_code ec)
{
using namespace BOOST_PROCESS_V2_ASIO_NAMESPACE;
single_process_exit res;
/*// we need to install our handler first, THEN check, THEN
using allocator_type = associated_allocator_t<typename std::decay<Handler::type>;
allocator_type allocator = get_associated_allocator(handler);
*/
printf("FOOBAR %d %p\n", __LINE__, this_);
if (ec)
return self.complete(ec, res);
auto exec = self.get_executor();
printf("FOOBAR %d %p\n", __LINE__, this_);
if (job_object_is_empty(this_->job_object_.get(), ec))
return self.complete(BOOST_PROCESS_V2_ASIO_NAMESPACE::error::broken_pipe, res);
printf("FOOBAR %d %p\n", __LINE__, this_);
//check if done
if (this->poll_one(ec, res))
return self.complete(ec, res);
else
this_->port_.async_wait(std::move(self));
printf("FOOBAR %d %p\n", __LINE__, this_);
}
bool poll_one(error_code & ec, single_process_exit & se)
{
DWORD completion_code;
ULONG_PTR completion_key;
LPOVERLAPPED overlapped;
auto res = GetQueuedCompletionStatus(
this_->port_.native_handle(),
&completion_code,
&completion_key,
&overlapped,
0u);
if (!res && ::GetLastError() == ERROR_ABANDONED_WAIT_0)
return false;
if (completion_code == JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO)
{
ec = BOOST_PROCESS_V2_ASIO_NAMESPACE::error::broken_pipe;
return true;
}
else if (completion_code == JOB_OBJECT_MSG_EXIT_PROCESS
|| completion_code == JOB_OBJECT_MSG_ABNORMAL_EXIT_PROCESS)
{
se.pid = *reinterpret_cast<DWORD*>(&overlapped);
const auto p = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, se.pid);
if (p == INVALID_HANDLE_VALUE || p == nullptr)
{
ec = detail::get_last_error();
return true;
}
DWORD code;
res = ::GetExitCodeProcess(p, &code);
se.exit_code = static_cast<int>(code);
return true;
}
else if (job_object_is_empty(this_->job_object_.get(), ec))
{
ec = BOOST_PROCESS_V2_ASIO_NAMESPACE::error::broken_pipe;
return true;
}
return false;
}
};
struct wait_all_op
{
basic_group_impl * this_;
template<typename Self>
void operator()(Self && self)
{
/*
using namespace BOOST_PROCESS_V2_ASIO_NAMESPACE;
using allocator_type = associated_allocator_t<typename std::decay<Handler::type>;
allocator_type allocator = get_associated_allocator(handler);
*/
}
};
};
#if !defined(BOOST_PROCESS_V2_HEADER_ONLY)
extern template struct basic_group_impl<BOOST_PROCESS_V2_ASIO_NAMESPACE::any_io_executor>;
#endif
}
BOOST_PROCESS_V2_END_NAMESPACE
#if defined(BOOST_PROCESS_V2_HEADER_ONLY)
#include <boost/process/v2/detail/impl/group_impl_windows.ipp>
#endif
#endif //BOOST_PROCESS_V2_GROUP_IMPL_HPP

View File

@@ -1,79 +0,0 @@
//
// boost/process/v2/windows/impl/job_object_service.ipp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2022 Klemens D. Morgenstern (klemens dot morgenstern at gmx dot net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_PROCESS_V2_DETAIL_IMPL_GROUP_IMPL_WINDOWS_IPP
#define BOOST_PROCESS_V2_DETAIL_IMPL_GROUP_IMPL_WINDOWS_IPP
#include <boost/process/v2/detail/config.hpp>
#include <boost/process/v2/detail/last_error.hpp>
#include <boost/process/v2/detail/group_impl_windows.hpp>
#include <windows.h>
BOOST_PROCESS_V2_BEGIN_NAMESPACE
namespace detail
{
bool job_object_is_empty(HANDLE job_object, error_code & ec)
{
JOBOBJECT_BASIC_ACCOUNTING_INFORMATION info;
if (!QueryInformationJobObject(job_object,
JobObjectBasicAccountingInformation,
&info, sizeof(info), nullptr))
ec = detail::get_last_error();
return info.ActiveProcesses == 0u;
}
#if !defined(BOOST_PROCESS_V2_HEADER_ONLY)
template struct basic_group_impl<BOOST_PROCESS_V2_ASIO_NAMESPACE::any_io_executor>;
#endif
void basic_group_impl_base::add(DWORD pid, HANDLE handle, error_code & ec)
{
if (!AssignProcessToJobObject(job_object_.get(), handle))
ec = detail::get_last_error();
}
void basic_group_impl_base::terminate(error_code & ec)
{
if (!::TerminateJobObject(job_object_.get(), 255u))
ec = detail::get_last_error();
}
bool basic_group_impl_base::contains(DWORD pid, error_code & ec)
{
BOOL res = FALSE;
//
struct del
{
void operator()(HANDLE h)
{
if (h != nullptr && h != INVALID_HANDLE_VALUE)
::CloseHandle(h);
}
};
std::unique_ptr<void, del> proc{::OpenProcess(PROCESS_QUERY_INFORMATION , FALSE, pid)};
if (proc.get() != INVALID_HANDLE_VALUE &&
!IsProcessInJob(proc.get(), job_object_.get(), &res))
ec = detail::get_last_error();
return res == TRUE;
}
}
BOOST_PROCESS_V2_END_NAMESPACE
#endif //BOOST_PROCESS_V2_DETAIL_IMPL_GROUP_IMPL_WINDOWS_IPP

View File

@@ -9,7 +9,7 @@
#include <boost/process/v2/detail/last_error.hpp>
#if defined(BOOST_PROCESS_V2_WINDOWS)
#include <Windows.h>
#include <windows.h>
#else
#include <cerrno>
#endif

View File

@@ -12,6 +12,16 @@
#include <windows.h>
#if !defined(BOOST_PROCESS_V2_DISABLE_UNDOCUMENTED_API)
extern "C"
{
LONG WINAPI NtResumeProcess(HANDLE ProcessHandle);
LONG WINAPI NtSuspendProcess(HANDLE ProcessHandle);
}
#endif
BOOST_PROCESS_V2_BEGIN_NAMESPACE
namespace detail
@@ -113,6 +123,33 @@ void check_running_(HANDLE handle, error_code & ec, DWORD & exit_status)
ec = detail::get_last_error();
}
#if !defined(BOOST_PROCESS_V2_DISABLE_UNDOCUMENTED_API)
void suspend_(HANDLE handle, error_code & ec)
{
auto nt_err = NtSuspendProcess(handle);
if (nt_err > 0xC0000000)
ec = detail::get_last_error();
}
void resume_(HANDLE handle, error_code & ec)
{
auto nt_err = NtResumeProcess(handle);
if (nt_err > 0xC0000000)
ec = detail::get_last_error();
}
#else
void suspend_(HANDLE, error_code & ec)
{
ec.assign(ERROR_CALL_NOT_IMPLEMENTED, system_category());
}
void resume_(HANDLE handle, error_code & ec)
{
ec.assign(ERROR_CALL_NOT_IMPLEMENTED, system_category());
}
#endif
#if !defined(BOOST_PROCESS_V2_HEADER_ONLY)
template struct basic_process_handle_win<>;

View File

@@ -182,7 +182,42 @@ struct basic_process_handle_fd
if (ec)
detail::throw_error(ec, "request_exit");
}
void suspend()
{
if (pid_ <= 0)
return ;
error_code ec;
suspend(ec);
if (ec)
detail::throw_error(ec, "suspend");
}
void suspend(error_code &ec)
{
if (pid_ <= 0)
return ;
if (::kill(pid_, SIGCONT) == -1)
ec = get_last_error();
}
void resume()
{
if (pid_ <= 0)
return ;
error_code ec;
resume(ec);
if (ec)
detail::throw_error(ec, "resume");
}
void resume(error_code &ec)
{
if (pid_ <= 0)
return ;
if (::kill(pid_, SIGTERM) == -1)
ec = get_last_error();
}
void terminate(native_exit_code_type &exit_status, error_code &ec)
{
if (pid_ <= 0)

View File

@@ -211,6 +211,42 @@ struct basic_process_handle_fd_or_signal
if (ec)
detail::throw_error(ec, "request_exit");
}
void suspend()
{
if (pid_ <= 0)
return ;
error_code ec;
suspend(ec);
if (ec)
detail::throw_error(ec, "suspend");
}
void suspend(error_code &ec)
{
if (pid_ <= 0)
return ;
if (::kill(pid_, SIGCONT) == -1)
ec = get_last_error();
}
void resume()
{
if (pid_ <= 0)
return ;
error_code ec;
resume(ec);
if (ec)
detail::throw_error(ec, "resume");
}
void resume(error_code &ec)
{
if (pid_ <= 0)
return ;
if (::kill(pid_, SIGTERM) == -1)
ec = get_last_error();
}
void terminate(native_exit_code_type &exit_status, error_code &ec)
{

View File

@@ -180,6 +180,50 @@ struct basic_process_handle_signal
detail::throw_error(ec, "request_exit");
}
void interrupt(error_code &ec)
{
if (pid_ <= 0)
return ;
if (::kill(pid_, SIGTERM) == -1)
ec = get_last_error();
}
void suspend()
{
if (pid_ <= 0)
return ;
error_code ec;
suspend(ec);
if (ec)
detail::throw_error(ec, "suspend");
}
void suspend(error_code &ec)
{
if (pid_ <= 0)
return ;
if (::kill(pid_, SIGCONT) == -1)
ec = get_last_error();
}
void resume()
{
if (pid_ <= 0)
return ;
error_code ec;
resume(ec);
if (ec)
detail::throw_error(ec, "resume");
}
void resume(error_code &ec)
{
if (pid_ <= 0)
return ;
if (::kill(pid_, SIGTERM) == -1)
ec = get_last_error();
}
void terminate(native_exit_code_type &exit_status, error_code &ec)
{
if (pid_ <= 0)

View File

@@ -32,6 +32,8 @@ BOOST_PROCESS_V2_DECL void terminate_if_running_(void * handle);
BOOST_PROCESS_V2_DECL bool check_handle_(void* handle, error_code & ec);
BOOST_PROCESS_V2_DECL bool check_pid_(pid_type pid_, error_code & ec);
BOOST_PROCESS_V2_DECL void interrupt_(pid_type pid_, error_code & ec);
BOOST_PROCESS_V2_DECL void suspend_(void * handle, error_code & ec);
BOOST_PROCESS_V2_DECL void resume_(void * handle, error_code & ec);
BOOST_PROCESS_V2_DECL void terminate_(void * handle, error_code & ec, native_exit_code_type & exit_code);
BOOST_PROCESS_V2_DECL void request_exit_(pid_type pid_, error_code & ec);
BOOST_PROCESS_V2_DECL void check_running_(void* handle, error_code & ec, native_exit_code_type & exit_status);
@@ -176,6 +178,32 @@ struct basic_process_handle_win
detail::throw_error(ec, "request_exit");
}
void suspend(error_code &ec)
{
detail::suspend_(handle_.native_handle(), ec);
}
void suspend()
{
error_code ec;
suspend(ec);
if (ec)
detail::throw_error(ec, "suspend");
}
void resume(error_code &ec)
{
detail::resume_(handle_.native_handle(), ec);
}
void resume()
{
error_code ec;
suspend(ec);
if (ec)
detail::throw_error(ec, "resume");
}
void terminate(native_exit_code_type &exit_status, error_code &ec)
{
if (!detail::check_handle_(handle_.native_handle(), ec))

View File

@@ -12,7 +12,6 @@
#define BOOST_PROCESS_V2_EXIT_CODE_HPP
#include <boost/process/v2/detail/config.hpp>
#include <boost/process/v2/pid.hpp>
#if defined(BOOST_PROCESS_V2_POSIX)
#include <sys/wait.h>
@@ -20,15 +19,6 @@
BOOST_PROCESS_V2_BEGIN_NAMESPACE
/// Result of a single process exiting.
struct single_process_exit
{
/// The pid of the process that exited
pid_type pid;
/// The exit code of the code
int exit_code;
};
#if defined(GENERATING_DOCUMENTATION)
/// The native exit-code type, usually an integral value

View File

@@ -1,321 +0,0 @@
// Copyright (c) 2022 Klemens D. Morgenstern
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_PROCESS_V2_GROUP_HPP
#define BOOST_PROCESS_V2_GROUP_HPP
#include <boost/process/v2/detail/config.hpp>
#include <boost/process/v2/default_launcher.hpp>
#include <boost/process/v2/exit_code.hpp>
#include <boost/process/v2/process_handle.hpp>
#if defined(BOOST_PROCESS_V2_WINDOWS)
#include <boost/process/v2/detail/group_impl_windows.hpp>
#else
#include <boost/process/v2/detail/group_impl_posix.hpp>
#endif
BOOST_PROCESS_V2_BEGIN_NAMESPACE
/** A process handle is an unmanaged version of a process.
* This means it does not terminate the proces on destruction and
* will not keep track of the exit-code.
*
* Note that the exit code might be discovered early, during a call to `running`.
* Thus it can only be discovered that process has exited already.
*/
template<typename Executor = BOOST_PROCESS_V2_ASIO_NAMESPACE::any_io_executor,
typename Launcher = default_process_launcher>
struct basic_group
{
/// The native handle of the process.
/** This might be undefined on posix systems that only support signals */
#if defined(GENERATING_DOCUMENTATION)
using native_handle_type = implementation_defined;
#else
using native_handle_type = typename detail::basic_group_impl<Executor>::native_handle_type;
#endif
/// The executor_type of the group
using executor_type = Executor;
/// The launcher used by the group for emplacing
using launcher_type = Launcher;
// Get the native group handle
native_handle_type native_handle() {return impl_.native_handle();}
/// Getter for the executor
executor_type get_executor() { return impl_.get_executor();}
/// Rebinds the group to another executor.
template<typename Executor1>
struct rebind_executor
{
/// The socket type when rebound to the specified executor.
typedef basic_group<Executor1, Launcher> other;
};
/// Construct a basic_group from an execution_context.
/**
* @tparam ExecutionContext The context must fulfill the asio::execution_context requirements
*/
template<typename ExecutionContext>
basic_group(ExecutionContext &context,
typename std::enable_if<
std::is_convertible<ExecutionContext&,
BOOST_PROCESS_V2_ASIO_NAMESPACE::execution_context&>::value,
launcher_type>::type launcher = launcher_type())
: basic_group(context.get_executor(), std::move(launcher))
{}
/// Construct an empty group from an executor.
basic_group(executor_type executor, launcher_type launcher = launcher_type())
: impl_(std::move(executor)), launcher_(std::move(launcher)) {}
/// Construct an empty group from an executor and bind it to a pid and the native-handle
/** On some non-linux posix systems this overload is not present.
*/
basic_group(executor_type executor, native_handle_type group,
launcher_type launcher = launcher_type())
: impl_(std::move(executor), group), , launcher_(std::move(launcher)) {}
/// Move construct and rebind the executor.
template<typename Executor1>
basic_group(basic_group<Executor1> &&handle)
: procs_(std::move(handle.procs_)), impl_(std::move(handle.impl_)),
launcher_(std::move(handle.launcher_)) {}
/// wait for one process to exit and store the exit code in exit_status.
single_process_exit wait_one(error_code &ec)
{
single_process_exit res;
impl_.wait_one(res.pid, res.exit_code, ec);
return res;
}
/// Throwing @overload wait_one(error_code & ec)
single_process_exit wait_one()
{
error_code ec;
auto res = wait_one(ec);
detail::throw_error(ec, "wait_one");
return res;
}
/// wait for all processes to exit. Returns the number of processes exited
void wait_all(error_code &ec)
{
impl_.wait_all(ec);
}
/// Throwing @overload wait_all(error_code & ec)
void wait_all()
{
error_code ec;
wait_all(ec);
detail::throw_error(ec, "wait_all");
}
/// Sends the processes a signal to ask for an interrupt, which the process may interpret as a shutdown.
/** Maybe be ignored by the subprocess. */
void interrupt(error_code &ec)
{
impl_.interrupt(ec);
}
/// Throwing @overload void interrupt()
void interrupt()
{
error_code ec;
interrupt(ec);
detail::throw_error(ec, "interrupt");
}
/// Sends the processes a signal to ask for a graceful shutdown. Maybe be ignored by the subprocess.
void request_exit(error_code &ec)
{
impl_.request_exit(ec);
}
/// Throwing @overload void request_exit(error_code & ec)
void request_exit()
{
error_code ec;
auto res = request_exit(ec);
detail::throw_error(ec, "request_exit");
}
/// Unconditionally terminates the processes and stores the exit code in exit_status.
void terminate(error_code &ec)
{
impl_.terminate(ec);
}
/// Throwing @overload void terminate(native_exit_code_type &exit_code, error_code & ec)
void terminate()
{
error_code ec;
terminate(ec);
detail::throw_error(ec, "terminate");
}
/// Check if the process handle is referring to an existing process.
bool is_open() const
{
return impl_.is_open();
}
/// Asynchronously wait for one process to exit and deliver the exit-code in the completion handler.
template<BOOST_PROCESS_V2_COMPLETION_TOKEN_FOR(void(error_code, single_process_exit))
WaitHandler BOOST_PROCESS_V2_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
BOOST_PROCESS_V2_INITFN_AUTO_RESULT_TYPE(WaitHandler, void (error_code, single_process_exit))
async_wait_one(WaitHandler &&handler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
{
return impl_.async_wait_one(std::forward<WaitHandler>(handler));
}
/// Asynchronously wait for all processes to exit and the gorup to be empty.
template<BOOST_PROCESS_V2_COMPLETION_TOKEN_FOR(void(error_code))
WaitHandler BOOST_PROCESS_V2_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
BOOST_PROCESS_V2_INITFN_AUTO_RESULT_TYPE(WaitHandler, void (error_code))
async_wait_all(WaitHandler &&handler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
{
return impl_.async_wait_all(std::forward<WaitHandler>(handler));
}
/// Throwing overload of @overload contains(pid_type pid, error_code & ec)
bool contains(pid_type pid)
{
error_code ec;
auto res = contains(ec);
detail::throw_error(ec, "contains");
return res;
}
/// Check if the pid is in the group
bool contains(pid_type pid, error_code & ec)
{
impl_.contains(std::move(proc), ec);
}
template<typename Executor_>
void add(basic_process_handle<Executor_> proc)
{
error_code ec;
add(proc.id(), proc.native_handle(), ec);
detail::throw_error(ec, "add");
}
template<typename Executor_>
void add(basic_process_handle<Executor_> proc, error_code & ec)
{
impl_.add(std::move(proc), ec);
}
template<typename ... Inits>
pid_type emplace(
const filesystem::path& exe,
std::initializer_list<string_view> args,
Inits&&... inits)
{
error_code ec;
auto ph = launcher_(impl_.get_executor(), exe, std::move(args),
std::forward<Inits>(inits)..., impl_.get_initializer(ec));
if (ec)
detail::throw_error(ec);
return ph.detach().id();
}
template<typename ... Inits>
pid_type emplace(
const filesystem::path& exe,
std::initializer_list<wstring_view> args,
Inits&&... inits)
{
error_code ec;
auto ph = launcher_(impl_.get_executor(), exe, std::move(args),
std::forward<Inits>(inits)..., impl_.get_initializer(ec));
if (ec)
detail::throw_error(ec);
return ph.detach().id();
}
template<typename Args, typename ... Inits>
pid_type emplace(
const filesystem::path& exe,
Args && args,
Inits&&... inits)
{
error_code ec;
auto ph = launcher_(impl_.get_executor(), exe, std::move(args),
std::forward<Inits>(inits)..., impl_.get_initializer(ec));
if (ec)
detail::throw_error(ec);
return ph.detach().id();
}
template<typename ... Inits>
pid_type emplace(
error_code & ec,
const filesystem::path& exe,
std::initializer_list<string_view> args,
Inits&&... inits)
{
auto ph = launcher_(impl_.get_executor(), ec, exe, std::move(args),
std::forward<Inits>(inits)..., impl_.get_initializer(ec));
if (ec)
return pid_type{-1};
return ph.detach().id();
}
template<typename ... Inits>
pid_type emplace(
error_code & ec,
const filesystem::path& exe,
std::initializer_list<wstring_view> args,
Inits&&... inits)
{
auto ph = launcher_(impl_.get_executor(), ec, exe, std::move(args),
std::forward<Inits>(inits)..., impl_.get_initializer(ec));
if (ec)
return pid_type{-1};
return ph.detach().id();
}
template<typename Args, typename ... Inits>
pid_type emplace(
error_code & ec,
const filesystem::path& exe,
Args && args,
Inits&&... inits)
{
auto ph = launcher_(impl_.get_executor(), ec, exe, std::move(args),
std::forward<Inits>(inits)..., impl_.get_initializer(ec));
if (ec)
return pid_type{-1};
return ph.detach().id();
}
void detach() { impl_.detach(); }
private:
detail::basic_group_impl<Executor> impl_;
launcher_type launcher_;
};
/// Process group with the default executor.
using group = basic_group<>;
#if !defined(BOOST_PROCESS_V2_HEADER_ONLY)
extern template struct basic_group<BOOST_PROCESS_V2_ASIO_NAMESPACE::any_io_executor, default_process_launcher>;
#endif
BOOST_PROCESS_V2_END_NAMESPACE
#if defined(BOOST_PROCESS_V2_HEADER_ONLY)
#include <boost/process/v2/impl/group.ipp>
#endif
#endif //BOOST_PROCESS_V2_GROUP_HPP

View File

@@ -1,25 +0,0 @@
//
// boost/process/v2/windows/impl/job_object_service.ipp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2022 Klemens D. Morgenstern (klemens dot morgenstern at gmx dot net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_PROCESS_V2_IMPL_GROUP_IPP
#define BOOST_PROCESS_V2_IMPL_GROUP_IPP
#include <boost/process/v2/detail/config.hpp>
#include <boost/process/v2/group.hpp>
BOOST_PROCESS_V2_BEGIN_NAMESPACE
#if !defined(BOOST_PROCESS_V2_HEADER_ONLY)
template struct basic_group_impl<BOOST_PROCESS_V2_ASIO_NAMESPACE::any_io_executor, default_process_launcher>;
#endif
BOOST_PROCESS_V2_END_NAMESPACE
#endif //BOOST_PROCESS_V2_IMPL_GROUP_IPP

View File

@@ -6,12 +6,21 @@
#define BOOST_PROCESS_V2_IMPL_PID_IPP
#include <boost/process/v2/detail/config.hpp>
#include <boost/process/v2/detail/last_error.hpp>
#include <boost/process/v2/detail/throw_error.hpp>
#include <boost/process/v2/pid.hpp>
#if defined(BOOST_PROCESS_V2_WINDOWS)
#include <windows.h>
#include <tlhelp32.h>
#else
#include <unistd.h>
#if (defined(__APPLE__) && defined(__MACH__))
#include <sys/proc_info.h>
#include <libproc.h>
#endif
#endif
BOOST_PROCESS_V2_BEGIN_NAMESPACE
@@ -22,6 +31,220 @@ pid_type current_pid() {return ::GetCurrentProcessId();}
pid_type current_pid() {return ::getpid();}
#endif
#if defined(BOOST_PROCESS_V2_WINDOWS)
std::vector<pid_type> all_pids(error_code & ec)
{
std::vector<pid_type> vec;
HANDLE hp = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (!hp)
{
ec = detail::get_last_error();
return vec;
}
PROCESSENTRY32 pe;
pe.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hp, &pe)) {
do {
vec.push_back(pe.th32ProcessID);
} while (Process32Next(hp, &pe));
}
CloseHandle(hp);
return vec;
}
#elif (defined(__APPLE__) && defined(__MACH__))
std::vector<pid_type> all_pids(error_code & ec)
{
std::vector<pid_type> vec;
vec.resize(proc_listpids(PROC_ALL_PIDS, 0, nullptr, 0));
if (proc_listpids(PROC_ALL_PIDS, 0, &vec[0], sizeof(pid_type) * vec))
{
ec = detail::get_last_error();
return {};
}
auto itr = std::partition(vec.begin(), vec.end(), [](pid_type pt) {return pt != 0;});
vec.erase(itr, vec.end());
return vec;
}
#elif defined(__linux__)
std::vector<pid_type> all_pids(error_code & ec)
{
std::vector<pid_type> vec;
DIR *proc = opendir("/proc");
if (proc == nullptr)
{
ec = detail::get_last_error();
return vec;
}
struct dirent *ent = nullptr;
while ((ent = readdir(proc) != nullptr))
{
if (!isdigit(*ent->d_name))
continue;
vec.push_back(atoi(ent->d_name));
}
closedir(proc);
return vec;
}
#elif defined(__FreeBSD__)
std::vector<pid_type> all_pids(error_code & ec)
{
std::vector<pid_type> vec;
int cntp = 0;
kinfo_proc *proc_info = kinfo_getallproc(&cntp);
if (proc_info)
{
for (int i = 0; i < cntp; i++)
vec.push_back(proc_info[i].ki_pid);
free(proc_info);
}
else
ec = detail::get_last_error();
return vec;
}
#elif defined(__DragonFly__)
std::vector<pid_type> all_pids(error_code & ec)
{
std::vector<pid_type> vec;
int cntp = 0;
kinfo_proc *proc_info = nullptr;
const char *nlistf, *memf;
nlistf = memf = "/dev/null";
kd = kvm_openfiles(nlistf, memf, nullptr, O_RDONLY, nullptr);
if (!kd)
{
ec = detail::get_last_error();
return vec;
}
if ((proc_info = kvm_getprocs(kd, KERN_PROC_ALL, 0, &cntp)))
{
vec.reserve(cntp);
for (int i = 0; i < cntp; i++)
if (proc_info[i].kp_pid >= 0)
vec.push_back(proc_info[i].kp_pid);
}
else
ec = detail::get_last_error();
kvm_close(kd);
return vec;
}
#elif defined(__NetBSD__)
std::vector<pid_type> all_pids(error_code & ec)
{
std::vector<pid_type> vec;
int cntp = 0;
kinfo_proc2 *proc_info = nullptr;
kd = kvm_openfiles(nullptr, nullptr, nullptr, KVM_NO_FILES, nullptr);
if (!kd)
{
ec = detail::get_last_error();
return vec;
}
if ((proc_info = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &cntp)))
{
vec.reserve(cntp);
for (int i = cntp - 1; i >= 0; i--) {
vec.push_back(proc_info[i].p_pid);
}
}
else
ec = detail::get_last_error();
kvm_close(kd);
return vec;
}
#elif defined(__OpenBSD__)
std::vector<pid_type> all_pids(error_code & ec)
{
std::vector<pid_type> vec;
int cntp = 0;
kinfo_proc *proc_info = nullptr;
kd = kvm_openfiles(nullptr, nullptr, nullptr, KVM_NO_FILES, nullptr);
if (!kd)
{
ec = detail::get_last_error();
return vec;
}
if ((proc_info = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &cntp)))
{
vec.reserve(cntp);
for (int i = 0; i < cntp; i++) {
if (proc_info[i].kp_pid >= 0) {
vec.push_back(proc_info[i].kp_pid);
}
}
}
else
ec = detail::get_last_error();
kvm_close(kd);
return vec;
}
#elif defined(__sun)
std::vector<pid_type> all_pids(error_code & ec)
{
std::vector<pid_type> vec;
struct pid cur_pid;
proc *proc_info = nullptr;
kd = kvm_open(nullptr, nullptr, nullptr, O_RDONLY, nullptr);
if (!kd)
{
ec = detail::get_last_error();
return vec;
}
while ((proc_info = kvm_nextproc(kd)))
{
if (kvm_kread(kd, (std::uintptr_t)proc_info->p_pidp, &cur_pid, sizeof(cur_pid)) != -1) {
vec.insert(vec.begin(), cur_pid.pid_id);
}
else
{
ec = detail::get_last_error();
break;
}
}
kvm_close(kd);
return vec;
}
#else
#error "Platform not supported"
#endif defined(BOOST_PROCESS_V2_WINDOWS)
std::vector<pid_type> all_pids()
{
error_code ec;
auto res = all_pids(ec);
if (ec)
detail::throw_error(ec, "all_pids");
return res;
}
BOOST_PROCESS_V2_END_NAMESPACE
#endif //BOOST_PROCESS_V2_IMPL_PID_IPP

View File

@@ -6,6 +6,7 @@
#define BOOST_PROCESS_V2_PID_HPP
#include <boost/process/v2/detail/config.hpp>
#include <boost/process/v2/detail/throw_error.hpp>
BOOST_PROCESS_V2_BEGIN_NAMESPACE
@@ -31,6 +32,12 @@ typedef int pid_type;
/// Get the process id of the current process.
BOOST_PROCESS_V2_DECL pid_type current_pid();
/// List all available pids.
BOOST_PROCESS_V2_DECL std::vector<pid_type> all_pids(error_code & ec);
/// List all available pids.
BOOST_PROCESS_V2_DECL std::vector<pid_type> all_pids();
BOOST_PROCESS_V2_END_NAMESPACE
#if defined(BOOST_PROCESS_V2_HEADER_ONLY)

View File

@@ -214,6 +214,37 @@ struct basic_process
process_handle_.request_exit(ec);
}
/// Send the process a signal requesting it to stop. This may rely on undocmented functions.
void suspend(error_code &ec)
{
process_handle_.suspend(ec);
}
/// Send the process a signal requesting it to stop. This may rely on undocmented functions.
void suspend()
{
error_code ec;
suspend(ec);
if (ec)
detail::throw_error(ec, "suspend");
}
/// Send the process a signal requesting it to resume. This may rely on undocmented functions.
void resume(error_code &ec)
{
process_handle_.resume(ec);
}
/// Send the process a signal requesting it to resume. This may rely on undocmented functions.
void resume()
{
error_code ec;
suspend(ec);
if (ec)
detail::throw_error(ec, "resume");
}
/// Throwing @overload void terminate(native_exit_code_type &exit_code, error_code & ec)
void terminate()
{

View File

@@ -24,8 +24,4 @@
#include <boost/process/v2/impl/process_handle.ipp>
#include <boost/process/v2/impl/shell.ipp>
#if defined(BOOST_PROCESS_V2_WINDOWS)
#include <boost/process/v2/detail/impl/group_impl_windows.ipp>
#endif
#endif //BOOST_PROCESS_V2_SRC_HPP

View File

@@ -11,9 +11,8 @@
#ifndef BOOST_PROCESS_V2_WINDOWS_DEFAULT_LAUNCHER_HPP
#define BOOST_PROCESS_V2_WINDOWS_DEFAULT_LAUNCHER_HPP
#include <boost/process/v2/detail/config.hpp>
#include <boost/process/v2/cstring_ref.hpp>
#include <boost/process/v2/detail/config.hpp>
#include <boost/process/v2/detail/last_error.hpp>
#include <boost/process/v2/detail/utf8.hpp>
#include <boost/process/v2/error.hpp>

View File

@@ -1,23 +0,0 @@
//
// boost/process/v2/windows/impl/job_object_service.ipp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2022 Klemens D. Morgenstern (klemens dot morgenstern at gmx dot net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_PROCESS_V2_WINDOWS_IMPL_JOB_OBJECT_SERVICE_IPP
#define BOOST_PROCESS_V2_WINDOWS_IMPL_JOB_OBJECT_SERVICE_IPP
#include <boost/process/v2/detail/config.hpp>
#include <boost/process/v2/windows/job_object_service.hpp>
BOOST_PROCESS_V2_BEGIN_NAMESPACE
namespace windows
{
}
BOOST_PROCESS_V2_END_NAMESPACE
#endif //BOOST_PROCESS_V2_WINDOWS_IMPL_JOB_OBJECT_SERVICE_IPP

View File

@@ -4,9 +4,6 @@ add_library(boost_process_v2_test_impl test_impl.cpp)
target_link_libraries(boost_process_v2_test_impl Boost::process Boost::unit_test_framework Boost::process)
target_compile_definitions(boost_process_v2_test_impl PUBLIC -DBOOST_PROCESS_V2_SEPARATE_COMPILATION=1)
if (WIN32)
target_compile_definitions(boost_process_v2_test_impl PUBLIC WIN32_LEAN_AND_MEAN=1)
endif()
function(boost_process_v2_standalone_test name)
add_executable(boost_process_v2_${name} ${name}.cpp)
@@ -33,5 +30,4 @@ function(boost_process_v2_test_with_target name)
-- $<TARGET_FILE:boost_process_v2_test_target>)
endfunction()
boost_process_v2_test_with_target(process)
boost_process_v2_test_with_target(group)
boost_process_v2_test_with_target(process)

View File

@@ -46,6 +46,7 @@ lib test_impl : test_impl.cpp filesystem :
<link>static
<target-os>windows:<source>shell32
<target-os>windows:<source>user32
<target-os>windows:<source>Ntdll
;
test-suite standalone :
@@ -59,6 +60,5 @@ test-suite standalone :
test-suite with_target :
[ run process.cpp test_impl : --log_level=all --catch_system_errors=no -- : target ]
[ run windows.cpp test_impl : --log_level=all --catch_system_errors=no -- : target : <build>no <target-os>windows:<build>yes <target-os>windows:<source>Advapi32 ]
[ run group.cpp test_impl : --log_level=all --catch_system_errors=no -- : target ]
;

View File

@@ -1,179 +0,0 @@
// Copyright (c) 2022 Klemens D. Morgenstern
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)
// Test that header file is self-contained.
#include <boost/test/unit_test.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/process/v2/group.hpp>
#include <boost/process/v2.hpp>
namespace bp2 = boost::process::v2;
namespace bpw = boost::process::v2::windows;
namespace bpd = boost::process::v2::detail;
namespace asio = boost::asio;
BOOST_AUTO_TEST_CASE(wait_one)
{
using boost::unit_test::framework::master_test_suite;
asio::io_context ctx;
const auto pth = master_test_suite().argv[1];
bp2::error_code ec;
bp2::group grp{ctx};
BOOST_CHECK_MESSAGE(!ec, ec.message());
auto pid1 = grp.emplace(pth, {"sleep", "100"});
auto pid2 = grp.emplace(pth, {"sleep", "300"});
auto pid3 = grp.emplace(pth, {"sleep", "500"});
auto res1 = grp.wait_one(ec); BOOST_CHECK_MESSAGE(!ec, ec.message());
auto res2 = grp.wait_one(ec); BOOST_CHECK_MESSAGE(!ec, ec.message());
auto res3 = grp.wait_one(ec); BOOST_CHECK_MESSAGE(!ec, ec.message());
grp.wait_all(); //
BOOST_CHECK_EQUAL(res1.exit_code, 0);
BOOST_CHECK_EQUAL(res2.exit_code, 0);
BOOST_CHECK_EQUAL(res3.exit_code, 0);
BOOST_CHECK_EQUAL(res1.pid, pid1);
BOOST_CHECK_EQUAL(res2.pid, pid2);
BOOST_CHECK_EQUAL(res3.pid, pid3);
BOOST_CHECK(grp.is_open());
BOOST_CHECK_MESSAGE(!ec, ec.message());
}
BOOST_AUTO_TEST_CASE(wait_all)
{
using boost::unit_test::framework::master_test_suite;
asio::io_context ctx;
const auto pth = master_test_suite().argv[1];
bp2::group grp{ctx};
auto pid1 = grp.emplace(pth, {"sleep", "10"});
auto pid2 = grp.emplace(pth, {"sleep", "30"});
auto pid3 = grp.emplace(pth, {"sleep", "50"});
grp.wait_all(); //
BOOST_CHECK(grp.is_open());
}
BOOST_AUTO_TEST_CASE(terminate_)
{
using boost::unit_test::framework::master_test_suite;
asio::io_context ctx;
const auto pth = master_test_suite().argv[1];
bp2::error_code ec;
bp2::group grp{ctx};
BOOST_CHECK_MESSAGE(!ec, ec.message());
const auto pid1 = grp.emplace(pth, {"sleep", "10000000"});
const auto pid2 = grp.emplace(pth, {"sleep", "10000000"});
const auto pid3 = grp.emplace(pth, {"sleep", "10000000"});
const auto start = std::chrono::steady_clock::now();
grp.terminate();
grp.wait_all();
const auto end = std::chrono::steady_clock::now();
BOOST_CHECK((start + std::chrono::milliseconds(5000)) > end);
BOOST_CHECK(grp.is_open());
BOOST_CHECK_MESSAGE(!ec, ec.message());
}
BOOST_AUTO_TEST_CASE(async_wait_one)
{
using boost::unit_test::framework::master_test_suite;
asio::io_context ctx;
const auto pth = master_test_suite().argv[1];
bp2::error_code ec;
bp2::group grp{ctx};
BOOST_CHECK_MESSAGE(!ec, ec.message());
auto pid1 = grp.emplace(pth, {"sleep", "100"});
auto pid2 = grp.emplace(pth, {"sleep", "300"});
auto pid3 = grp.emplace(pth, {"sleep", "500"});
std::vector<bp2::pid_type> res;
grp.async_wait_one(
[&](bp2::error_code ec, bp2::single_process_exit sp)
{
res.push_back(sp.pid);
BOOST_CHECK_EQUAL(sp.exit_code, 0u);
BOOST_CHECK_MESSAGE(!ec, ec.message());
});
grp.async_wait_one(
[&](bp2::error_code ec, bp2::single_process_exit sp)
{
res.push_back(sp.pid);
BOOST_CHECK_EQUAL(sp.exit_code, 0u);
BOOST_CHECK_MESSAGE(!ec, ec.message());
});
grp.async_wait_one(
[&](bp2::error_code ec, bp2::single_process_exit sp)
{
res.push_back(sp.pid);
BOOST_CHECK_EQUAL(sp.exit_code, 0u);
BOOST_CHECK_MESSAGE(!ec, ec.message());
});
BOOST_CHECK_GE(ctx.run(), 0u);
BOOST_CHECK_EQUAL(res.at(0), pid1);
BOOST_CHECK_EQUAL(res.at(1), pid2);
BOOST_CHECK_EQUAL(res.at(2), pid3);
BOOST_CHECK(grp.is_open());
BOOST_CHECK_MESSAGE(!ec, ec.message());
}
BOOST_AUTO_TEST_CASE(async_wait_all)
{
using boost::unit_test::framework::master_test_suite;
asio::io_context ctx;
const auto pth = master_test_suite().argv[1];
bp2::error_code ec;
bp2::group grp{ctx};
BOOST_CHECK_MESSAGE(!ec, ec.message());
auto pid1 = grp.emplace(pth, {"sleep", "10"});
auto pid2 = grp.emplace(pth, {"sleep", "30"});
auto pid3 = grp.emplace(pth, {"sleep", "50"});
grp.async_wait_all(
[](bp2::error_code ec)
{
BOOST_CHECK_MESSAGE(!ec, ec.message());
});
ctx.run();
BOOST_CHECK(grp.is_open());
BOOST_CHECK_MESSAGE(!ec, ec.message());
}

View File

@@ -12,4 +12,8 @@ BOOST_AUTO_TEST_CASE(test_pid)
{
namespace bp2 = boost::process::v2;
BOOST_CHECK_NE(bp2::current_pid(), static_cast<bp2::pid_type>(0));
auto all = bp2::all_pids();
auto itr = std::find(all.begin(), all.end(), bp2::current_pid());
BOOST_CHECK(itr != all.end());
}

View File

@@ -149,6 +149,8 @@ BOOST_AUTO_TEST_CASE(terminate)
BOOST_CHECK_MESSAGE(!sh.empty(), sh);
bpv::process proc(ctx, sh, {});
proc.suspend();
proc.resume();
proc.terminate();
proc.wait();
}