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 6 // 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) 9 10 #ifndef BOOST_PROCESS_DETAIL_POSIX_ASYNC_IN_HPP 11 #define BOOST_PROCESS_DETAIL_POSIX_ASYNC_IN_HPP 12 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> 17 #include <memory> 18 #include <future> 19 #include <boost/process/detail/used_handles.hpp> 20 #include <array> 21 22 namespace boost { namespace process { namespace detail { namespace posix { 23 24 25 template<typename Buffer> 26 struct async_in_buffer : ::boost::process::detail::posix::handler_base_ext, 27 ::boost::process::detail::posix::require_io_context, 28 ::boost::process::detail::uses_handles 29 { 30 Buffer & buf; 31 32 std::shared_ptr<std::promise<void>> promise; operator >boost::process::detail::posix::async_in_buffer33 async_in_buffer operator>(std::future<void> & fut) 34 { 35 promise = std::make_shared<std::promise<void>>(); 36 fut = promise->get_future(); return std::move(*this); 37 } 38 39 40 std::shared_ptr<boost::process::async_pipe> pipe; 41 async_in_bufferboost::process::detail::posix::async_in_buffer42 async_in_buffer(Buffer & buf) : buf(buf) 43 { 44 } 45 template <typename Executor> on_successboost::process::detail::posix::async_in_buffer46 inline void on_success(Executor) 47 { 48 auto pipe_ = this->pipe; 49 if (this->promise) 50 { 51 auto promise_ = this->promise; 52 53 boost::asio::async_write(*pipe_, buf, 54 [pipe_, promise_](const boost::system::error_code & ec, std::size_t) 55 { 56 if (ec && (ec.value() != EBADF) && (ec.value() != EPERM) && (ec.value() != ENOENT)) 57 { 58 std::error_code e(ec.value(), std::system_category()); 59 promise_->set_exception(std::make_exception_ptr(process_error(e))); 60 } 61 else 62 promise_->set_value(); 63 }); 64 } 65 else 66 boost::asio::async_write(*pipe_, buf, 67 [pipe_](const boost::system::error_code&, std::size_t){}); 68 69 std::move(*pipe_).source().close(); 70 71 this->pipe = nullptr; 72 } 73 74 template<typename Executor> on_errorboost::process::detail::posix::async_in_buffer75 void on_error(Executor &, const std::error_code &) const 76 { 77 std::move(*pipe).source().close(); 78 } 79 80 template<typename Executor> on_setupboost::process::detail::posix::async_in_buffer81 void on_setup(Executor & exec) 82 { 83 if (!pipe) 84 pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq)); 85 } 86 get_used_handlesboost::process::detail::posix::async_in_buffer87 std::array<int, 3> get_used_handles() 88 { 89 if (pipe) 90 return {STDIN_FILENO, pipe->native_source(), pipe->native_sink()}; 91 else //if pipe is not constructed, limit_ds is invoked before -> this also means on_exec_setup gets invoked before. 92 return {STDIN_FILENO, STDIN_FILENO, STDIN_FILENO}; 93 } 94 95 96 template <typename Executor> on_exec_setupboost::process::detail::posix::async_in_buffer97 void on_exec_setup(Executor &exec) 98 { 99 if (::dup2(pipe->native_source(), STDIN_FILENO) == -1) 100 exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed"); 101 102 if (pipe->native_source() != STDIN_FILENO) 103 ::close(pipe->native_source()); 104 ::close(pipe->native_sink()); 105 } 106 }; 107 108 109 }}}} 110 111 #endif 112