Reputation: 289
The documentation boost doesn't provide any example for creating a child process with a custom environment using process::child(...)
.
An example is given with process::system(...)
but the function system
has less possible operations (such as pipes or waitpid) so I would like to have a full example using process::child
if possible.
Upvotes: 6
Views: 4342
Reputation: 71
The last answer and comment are quite old but I can confirm that boost::process::child
with environment parameter works using Boost version 1.65 under Ubuntu 18.04. The documentation about that is quite thin, so I had to find out by myself:
std::string command = "/usr/bin/something";
ipstream pipe_stream;
// Get current env
auto env = boost::this_process::environment();
// Add something
env["CHINESE_FOOD"] = "GOOD";
// Change something
env["CHINESE_FOOD"] = "GREAT";
// Delete something
env["CHINESE_FOOD"].clear();
boost::process::child childProc(command, env, std_out > pipe_stream);
And of course, if environment is not needed, it will inherit from parent process automatically
std::string command = "/usr/bin/something";
ipstream pipe_stream;
boost::process::child childProc(command, std_out > pipe_stream);
Upvotes: 6
Reputation: 19771
in system.hpp, system_impl, which supports custom env, is implemented in terms of child,
template<typename IoService, typename ...Args>
inline int system_impl(
std::true_type, /*needs ios*/
std::true_type, /*has io_context*/
Args && ...args)
{
IoService & ios = ::boost::process::detail::get_io_context_var(args...);
system_impl_success_check check;
std::atomic_bool exited{false};
child c(std::forward<Args>(args)...,
check,
::boost::process::on_exit(
[&](int, const std::error_code&)
{
ios.post([&]{exited.store(true);});
}));
if (!c.valid() || !check.succeeded)
return -1;
while (!exited.load())
ios.poll();
return c.exit_code();
}
So the call to system from the docs:
bp::system("stuff", bp::env["VALUE_1"]="foo", bp::env["VALUE_2"]+={"bar1", "bar2"});
which calls:
template<typename ...Args>
inline int system(Args && ...args)
{
typedef typename ::boost::process::detail::needs_io_context<Args...>::type
need_ios;
typedef typename ::boost::process::detail::has_io_context<Args...>::type
has_ios;
return ::boost::process::detail::system_impl<boost::asio::io_context>(
need_ios(), has_ios(),
std::forward<Args>(args)...);
}
translates into this function, so you should be able to do the same.
Upvotes: 0