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_IN_HPP
11 #define BOOST_PROCESS_DETAIL_POSIX_ASYNC_IN_HPP
13 #include <boost/process/detail/handler_base.hpp>
14 #include <boost/process/detail/posix/async_handler.hpp>
15 #include <boost/asio/write.hpp>
16 #include <boost/process/async_pipe.hpp>
20 namespace boost { namespace process { namespace detail { namespace posix {
23 template<typename Buffer>
24 struct async_in_buffer : ::boost::process::detail::posix::handler_base_ext,
25 ::boost::process::detail::posix::require_io_context
29 std::shared_ptr<std::promise<void>> promise;
30 async_in_buffer operator>(std::future<void> & fut)
32 promise = std::make_shared<std::promise<void>>();
33 fut = promise->get_future(); return std::move(*this);
36 std::shared_ptr<boost::process::async_pipe> pipe;
38 async_in_buffer(Buffer & buf) : buf(buf)
41 template <typename Executor>
42 inline void on_success(Executor &exec)
44 auto pipe = this->pipe;
47 auto promise = this->promise;
49 boost::asio::async_write(*pipe, buf,
50 [pipe, promise](const boost::system::error_code & ec, std::size_t)
52 if (ec && (ec.value() != EBADF) && (ec.value() != EPERM) && (ec.value() != ENOENT))
54 std::error_code e(ec.value(), std::system_category());
55 promise->set_exception(std::make_exception_ptr(process_error(e)));
62 boost::asio::async_write(*pipe, buf,
63 [pipe](const boost::system::error_code&ec, std::size_t size){});
65 std::move(*pipe).source().close();
70 template<typename Executor>
71 void on_error(Executor &, const std::error_code &) const
73 std::move(*pipe).source().close();
76 template<typename Executor>
77 void on_setup(Executor & exec)
79 pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
82 template <typename Executor>
83 void on_exec_setup(Executor &exec)
85 if (::dup2(pipe->native_source(), STDIN_FILENO) == -1)
86 exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
88 ::close(pipe->native_source());