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

Compare commits

..

17 Commits

Author SHA1 Message Date
Klemens Morgenstern
0ca663c826 Set ENOTSUP when PROC_PPID_ONLY is undefined
closes #452
2025-01-26 21:49:25 +08:00
Klemens Morgenstern
a2d2753aa8 zombie process fixes
closes #445, #447
2025-01-13 09:31:48 +08:00
Klemens Morgenstern
1b5272a9e1 changed cwd comparison to equivalent. 2025-01-13 08:36:36 +08:00
Jonas Greitemann
e842a060f1 implement move operations for process_io_binding and delete copy operations
This makes the test added in the previous commit pass.
2025-01-13 08:13:33 +08:00
Jonas Greitemann
4bb842564f add a (failing) test for process_stdio move semantics
This test currently fails on POSIX, as `process_io_binding` does not
explicitly implement move operations but holds on to a file descriptor
which exhibits reference semantics and has a non-trivial destructor.

The same test should pass on Windows as the Windows implementation makes
use of `unique_ptr` which gives it correct move semantics by virtue of
the rule of zero.
2025-01-13 08:13:33 +08:00
Jonas Greitemann
359820097b add test case for initializing process_stdio with complementary pipe ends
`process_stdio` holds on to the handles for `in`/`out`/`err`. In case of
pipes, `in` needs the handles of a `readable_pipe`, whereas `out` and
`err` need the handles of `writable_pipe`s. So far, the tests all create
a new pair using `connect_pipe`, pass the "correct" end into
`process_stdio`, and use the other end to interface with the process.

However, `process_io_binding` also supports construction from the
complementary pipe types, i.e., constructing `in`'s binding from a
`writable_pipe` and `out`/`err`'s bindings from `readable_pipe`s.
In this case, the constructor will create the corresponding pipe itself
and keep ownership of it. This mode was thus far not tested.
2025-01-13 08:13:33 +08:00
Klemens Morgenstern
b529769eb5 removed boost::system:: scope spec for error_code. 2024-12-20 20:53:29 +08:00
Klemens Morgenstern
d75ffb30ee windows link fixes. 2024-12-20 20:53:29 +08:00
Klemens Morgenstern
a6f2a1d005 windows fixes. 2024-12-20 20:53:29 +08:00
Klemens Morgenstern
e1c6a9b09b attempting to fix msvc build. 2024-12-20 20:53:29 +08:00
Klemens Morgenstern
9bd57d66b7 aded missing include to example/env.cpp 2024-12-20 20:53:29 +08:00
Klemens Morgenstern
65251a2316 replace png with svg in install. 2024-12-20 20:53:29 +08:00
Klemens Morgenstern
298b60caca fixed for v2 namespace inlining. 2024-12-20 20:53:29 +08:00
Klemens Morgenstern
3fd8b2608c examples are compiled & included. 2024-12-20 20:53:29 +08:00
Klemens Morgenstern
c33828a166 reference docs 2024-12-20 20:53:29 +08:00
Klemens Morgenstern
2ccd97cd48 made v2 the default 2024-12-20 20:53:29 +08:00
Klemens Morgenstern
7874a04958 switched to asciidoc 2024-12-20 20:53:29 +08:00
9 changed files with 150 additions and 43 deletions

View File

@@ -26,6 +26,8 @@
#endif
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
@@ -96,7 +98,7 @@ template<typename Launcher, typename Init>
inline auto invoke_on_error(Launcher & launcher, const filesystem::path &executable,
const char * const * (&cmd_line),
const error_code & ec, Init && init, derived && )
-> decltype(init.on_error(launcher, ec, executable, cmd_line, ec))
-> decltype(init.on_error(launcher, executable, cmd_line, ec))
{
init.on_error(launcher, executable, cmd_line, ec);
}
@@ -160,7 +162,7 @@ template<typename Launcher, typename Init>
inline auto invoke_on_fork_error(Launcher & launcher, const filesystem::path &executable,
const char * const * (&cmd_line),
const error_code & ec, Init && init, derived && )
-> decltype(init.on_fork_error(launcher, ec, executable, cmd_line, ec))
-> decltype(init.on_fork_error(launcher, executable, cmd_line, ec))
{
init.on_fork_error(launcher, executable, cmd_line, ec);
}
@@ -182,41 +184,6 @@ inline void on_fork_error(Launcher & launcher, const filesystem::path &executabl
on_fork_error(launcher, executable, cmd_line, ec, inits...);
}
template<typename Launcher, typename Init>
inline void invoke_on_fork_success(Launcher & /*launcher*/, const filesystem::path &/*executable*/,
const char * const * (&/*cmd_line*/),
Init && /*init*/, base && )
{
}
template<typename Launcher, typename Init>
inline auto invoke_on_fork_success(Launcher & launcher, const filesystem::path &executable,
const char * const * (&cmd_line),
Init && init, derived && )
-> decltype(init.on_fork_success(launcher, executable, cmd_line))
{
init.on_fork_success(launcher, executable, cmd_line);
}
template<typename Launcher>
inline void on_fork_success(Launcher & /*launcher*/, const filesystem::path &/*executable*/,
const char * const * (&/*cmd_line*/))
{
}
template<typename Launcher, typename Init1, typename ... Inits>
inline void on_fork_success(Launcher & launcher, const filesystem::path &executable,
const char * const * (&cmd_line),
Init1 && init1, Inits && ... inits)
{
invoke_on_fork_success(launcher, executable, cmd_line, init1, derived{});
on_fork_success(launcher, executable, cmd_line, inits...);
}
template<typename Launcher, typename Init>
inline error_code invoke_on_exec_setup(Launcher & /*launcher*/, const filesystem::path &/*executable*/,
const char * const * (&/*cmd_line*/),
@@ -266,7 +233,7 @@ template<typename Launcher, typename Init>
inline auto invoke_on_exec_error(Launcher & launcher, const filesystem::path &executable,
const char * const * (&cmd_line),
const error_code & ec, Init && init, derived && )
-> decltype(init.on_exec_error(launcher, ec, executable, cmd_line, ec))
-> decltype(init.on_exec_error(launcher, executable, cmd_line, ec))
{
init.on_exec_error(launcher, executable, cmd_line, ec);
}
@@ -440,6 +407,7 @@ struct default_launcher
if (ec)
{
detail::on_error(*this, executable, argv, ec, inits...);
do { ::waitpid(pid, nullptr, 0); } while (errno == EINTR);
return basic_process<Executor>{exec};
}
}

View File

@@ -124,6 +124,7 @@ struct fork_and_forget_launcher : default_launcher
if (ec)
{
detail::on_error(*this, executable, argv, ec, inits...);
do { ::waitpid(pid, nullptr, 0); } while (errno == EINTR);
return basic_process<Executor>{exec};
}
}

View File

@@ -161,6 +161,7 @@ struct pdfork_launcher : default_launcher
if (ec)
{
detail::on_error(*this, executable, argv, ec, inits...);
do { ::waitpid(pid, nullptr, 0); } while (errno == EINTR);
return basic_process<Executor>{exec};
}
}

View File

@@ -121,6 +121,7 @@ struct vfork_launcher : default_launcher
if (ec)
{
detail::on_error(*this, executable, argv, ec, inits...);
do { ::waitpid(pid, nullptr, 0); } while (errno == EINTR);
return basic_process<Executor>{exec};
}

View File

@@ -166,6 +166,31 @@ struct process_io_binding
}
process_io_binding() = default;
process_io_binding(const process_io_binding &) = delete;
process_io_binding & operator=(const process_io_binding &) = delete;
process_io_binding(process_io_binding && other) noexcept
: fd(other.fd), fd_needs_closing(other.fd), ec(other.ec)
{
other.fd = target;
other.fd_needs_closing = false;
other.ec = {};
}
process_io_binding & operator=(process_io_binding && other) noexcept
{
if (fd_needs_closing)
::close(fd);
fd = other.fd;
fd_needs_closing = other.fd_needs_closing;
ec = other.ec;
other.fd = target;
other.fd_needs_closing = false;
other.ec = {};
return *this;
}
template<typename Stream>
process_io_binding(Stream && str, decltype(std::declval<Stream>().native_handle()) * = nullptr)

View File

@@ -97,7 +97,7 @@ inline void invoke_on_error(Launcher & /*launcher*/, const filesystem::path &/*e
template<typename Launcher, typename Init>
inline auto invoke_on_error(Launcher & launcher, const filesystem::path &executable, std::wstring &cmd_line,
const error_code & ec, Init && init, derived && )
-> decltype(init.on_error(launcher, ec, executable, cmd_line, ec))
-> decltype(init.on_error(launcher, executable, cmd_line, ec))
{
init.on_error(launcher, executable, cmd_line, ec);
}

View File

@@ -178,6 +178,8 @@ pid_type parent_pid(pid_type pid, error_code & ec)
std::vector<pid_type> child_pids(pid_type pid, error_code & ec)
{
std::vector<pid_type> vec;
#if defined(PROC_PPID_ONLY)
vec.resize(proc_listpids(PROC_PPID_ONLY, (uint32_t)pid, nullptr, 0) / sizeof(pid_type));
const auto sz = proc_listpids(PROC_PPID_ONLY, (uint32_t)pid, &vec[0], sizeof(pid_type) * vec.size());
if (sz < 0)
@@ -186,6 +188,9 @@ std::vector<pid_type> child_pids(pid_type pid, error_code & ec)
return {};
}
vec.resize(sz);
#else
BOOST_PROCESS_V2_ASSIGN_EC(ec, ENOTSUP, system_category());
#endif
return vec;
}

View File

@@ -110,10 +110,10 @@ BOOST_AUTO_TEST_CASE(test_cwd_exe)
boost::asio::io_context ctx;
bp2::process proc(ctx, pth, {"sleep", "10000"},
bp2::process_start_dir{tmp});
auto tt = bp2::ext::cwd(proc.handle()).string();
if (tt.back() == '\\')
tt.pop_back();
BOOST_CHECK_EQUAL(tt, tmp);
auto tt = bp2::ext::cwd(proc.handle());
BOOST_CHECK_MESSAGE(bp2::filesystem::equivalent(tmp, tt), tmp << " == " << tt);
bp2::error_code ec;
bp2::filesystem::remove(tmp, ec);
}

View File

@@ -30,9 +30,11 @@
#include <boost/test/unit_test.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/connect_pipe.hpp>
#include <boost/asio/cancel_after.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio/readable_pipe.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/streambuf.hpp>
@@ -328,6 +330,74 @@ BOOST_AUTO_TEST_CASE(echo_file)
BOOST_CHECK_MESSAGE(proc.exit_code() == 0, proc.exit_code());
}
BOOST_AUTO_TEST_CASE(stdio_creates_complementary_pipes)
{
using boost::unit_test::framework::master_test_suite;
const auto pth = master_test_suite().argv[1];
asio::io_context ctx;
asio::readable_pipe rp{ctx};
asio::writable_pipe wp{ctx};
// Pipes intentionally not connected. `process_stdio` will create pipes
// complementing both of these and retains ownership of those pipes.
bpv::process proc(ctx, pth, {"echo"}, bpv::process_stdio{/*.in=*/wp, /*.out=*/rp, /*.err=*/nullptr});
asio::write(wp, asio::buffer("foo", 3));
asio::write(wp, asio::buffer("bar", 3));
wp.close();
bpv::error_code ec;
std::string out;
auto sz = asio::read(rp, asio::dynamic_buffer(out), ec);
while (ec == asio::error::interrupted)
sz += asio::read(rp, asio::dynamic_buffer(out), ec);
BOOST_CHECK_EQUAL(sz, 6u);
BOOST_CHECK_MESSAGE((ec == asio::error::broken_pipe) || (ec == asio::error::eof), ec.message());
BOOST_CHECK_EQUAL(out, "foobar");
proc.wait();
BOOST_CHECK(proc.exit_code() == 0);
}
BOOST_AUTO_TEST_CASE(stdio_move_semantics)
{
using boost::unit_test::framework::master_test_suite;
const auto pth = master_test_suite().argv[1];
asio::io_context ctx;
asio::readable_pipe rp{ctx};
asio::writable_pipe wp{ctx};
auto make_stdio = [&]() -> bpv::process_stdio {
bpv::process_stdio stdio{};
stdio.in = wp;
stdio.out = rp;
stdio.err = nullptr;
// intentionally pessimizing move, preventing NRVO
return std::move(stdio);
};
bpv::process proc(ctx, pth, {"echo"}, make_stdio());
bpv::error_code ec;
asio::write(wp, asio::buffer("foobar", 6), ec);
BOOST_CHECK_MESSAGE(!ec, ec.message());
wp.close();
std::string out;
auto sz = asio::read(rp, asio::dynamic_buffer(out), ec);
while (ec == asio::error::interrupted)
sz += asio::read(rp, asio::dynamic_buffer(out), ec);
BOOST_CHECK_EQUAL(sz, 6u);
BOOST_CHECK_MESSAGE((ec == asio::error::broken_pipe) || (ec == asio::error::eof), ec.message());
BOOST_CHECK_EQUAL(out, "foobar");
proc.wait();
BOOST_CHECK(proc.exit_code() == 0);
}
BOOST_AUTO_TEST_CASE(print_same_cwd)
{
using boost::unit_test::framework::master_test_suite;
@@ -664,6 +734,42 @@ BOOST_AUTO_TEST_CASE(async_cancel_wait)
ctx.run();
}
#if defined(BOOST_POSIX_API)
struct capture_pid
{
pid_t &pid;
template<typename Launcher>
void on_error(Launcher &launcher, const bpv::filesystem::path& executable,
const char * const * (&/*cmd_line*/), const bpv::error_code & ec)
{
BOOST_REQUIRE(!bpv::filesystem::exists(executable));
this->pid = launcher.pid;
}
};
BOOST_AUTO_TEST_CASE(no_zombie)
{
asio::io_context ctx;
using boost::unit_test::framework::master_test_suite;
const auto pth = bpv::filesystem::absolute(master_test_suite().argv[1]);
pid_t res{-1};
boost::system::error_code ec;
bpv::default_process_launcher()(ctx, ec, "/send/more/cops", std::vector<std::string>{}, capture_pid{res});
BOOST_CHECK(ec == boost::system::errc::no_such_file_or_directory);
BOOST_REQUIRE(res != -1);
BOOST_CHECK(res != 0);
auto r = waitpid(res, nullptr, 0);
BOOST_CHECK(r < 0);
BOOST_CHECK_EQUAL(errno, ECHILD);
}
#endif
BOOST_AUTO_TEST_SUITE_END();