1 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
2 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
3 // Copyright (c) 2009 Boris Schaeling
4 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
5 // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_PROCESS_DETAIL_POSIX_ASYNC_OUT_HPP
11 #define BOOST_PROCESS_DETAIL_POSIX_ASYNC_OUT_HPP
14 #include <boost/process/detail/posix/handler.hpp>
15 #include <boost/asio/posix/stream_descriptor.hpp>
16 #include <boost/asio/read.hpp>
17 #include <boost/process/async_pipe.hpp>
23 #include <boost/process/detail/used_handles.hpp>
25 namespace boost { namespace process { namespace detail { namespace posix {
28 inline int apply_out_handles(int handle, std::integral_constant<int, 1>, std::integral_constant<int, -1>)
30 return ::dup2(handle, STDOUT_FILENO);
33 inline int apply_out_handles(int handle, std::integral_constant<int, 2>, std::integral_constant<int, -1>)
35 return ::dup2(handle, STDERR_FILENO);
38 inline int apply_out_handles(int handle, std::integral_constant<int, 1>, std::integral_constant<int, 2>)
40 if (::dup2(handle, STDOUT_FILENO) == -1)
42 if (::dup2(handle, STDERR_FILENO) == -1)
48 template<int p1, int p2, typename Buffer>
49 struct async_out_buffer : ::boost::process::detail::posix::handler_base_ext,
50 ::boost::process::detail::posix::require_io_context,
51 ::boost::process::detail::uses_handles
55 std::shared_ptr<boost::process::async_pipe> pipe;
57 std::array<int, 4> get_used_handles()
59 const auto pp1 = p1 != -1 ? p1 : p2;
60 const auto pp2 = p2 != -1 ? p2 : p1;
63 return {pipe->native_source(), pipe->native_sink(), pp1, pp2};
64 else //if pipe is not constructed, limit_ds is invoked before -> this also means on_exec_setup gets invoked before.
65 return {pp1, pp2, pp1, pp2};
69 async_out_buffer(Buffer & buf) : buf(buf)
73 template <typename Executor>
74 inline void on_success(Executor &exec)
76 auto pipe = this->pipe;
77 boost::asio::async_read(*pipe, buf,
78 [pipe](const boost::system::error_code&, std::size_t){});
81 std::move(*pipe).sink().close();
84 template<typename Executor>
85 void on_error(Executor &, const std::error_code &) const
87 std::move(*pipe).sink().close();
90 template<typename Executor>
91 void on_setup(Executor & exec)
93 pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
97 template <typename Executor>
98 void on_exec_setup(Executor &exec)
100 int res = apply_out_handles(pipe->native_sink(),
101 std::integral_constant<int, p1>(), std::integral_constant<int, p2>());
103 exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
105 ::close(pipe->native_sink());
106 ::close(pipe->native_source());
114 template<int p1, int p2, typename Type>
115 struct async_out_future : ::boost::process::detail::posix::handler_base_ext,
116 ::boost::process::detail::posix::require_io_context
118 std::shared_ptr<std::promise<Type>> promise = std::make_shared<std::promise<Type>>();
120 std::shared_ptr<boost::asio::streambuf> buffer = std::make_shared<boost::asio::streambuf>();
122 std::shared_ptr<boost::process::async_pipe> pipe;
124 async_out_future(std::future<Type> & fut)
126 fut = promise->get_future();
128 template <typename Executor>
129 inline void on_success(Executor &)
131 auto pipe_ = this->pipe;
133 auto buffer_ = this->buffer;
134 auto promise_ = this->promise;
136 boost::asio::async_read(*pipe_, *buffer_,
137 [pipe_, buffer_, promise_](const boost::system::error_code& ec, std::size_t)
139 if (ec && (ec.value() != ENOENT))
141 std::error_code e(ec.value(), std::system_category());
142 promise_->set_exception(std::make_exception_ptr(process_error(e)));
146 std::istream is (buffer_.get());
148 if (buffer_->size() > 0)
150 arg.resize(buffer_->size());
151 is.read(&*arg.begin(), buffer_->size());
153 promise_->set_value(std::move(arg));
157 std::move(*pipe_).sink().close();
158 this->pipe = nullptr;
161 template<typename Executor>
162 void on_error(Executor &, const std::error_code &) const
164 std::move(*pipe).sink().close();
167 template<typename Executor>
168 void on_setup(Executor & exec)
170 pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
173 template <typename Executor>
174 void on_exec_setup(Executor &exec)
177 int res = apply_out_handles(pipe->native_sink(),
178 std::integral_constant<int, p1>(), std::integral_constant<int, p2>());
180 exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
182 ::close(pipe->native_sink());
183 ::close(pipe->native_source());