async_out.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #ifndef BOOST_PROCESS_DETAIL_POSIX_ASYNC_OUT_HPP
  10. #define BOOST_PROCESS_DETAIL_POSIX_ASYNC_OUT_HPP
  11. #include <boost/process/detail/posix/handler.hpp>
  12. #include <boost/asio/posix/stream_descriptor.hpp>
  13. #include <boost/asio/read.hpp>
  14. #include <boost/process/async_pipe.hpp>
  15. #include <istream>
  16. #include <memory>
  17. #include <exception>
  18. #include <future>
  19. #include <array>
  20. #include <boost/process/detail/used_handles.hpp>
  21. namespace boost { namespace process { namespace detail { namespace posix {
  22. inline int apply_out_handles(int handle, std::integral_constant<int, 1>, std::integral_constant<int, -1>)
  23. {
  24. return ::dup2(handle, STDOUT_FILENO);
  25. }
  26. inline int apply_out_handles(int handle, std::integral_constant<int, 2>, std::integral_constant<int, -1>)
  27. {
  28. return ::dup2(handle, STDERR_FILENO);
  29. }
  30. inline int apply_out_handles(int handle, std::integral_constant<int, 1>, std::integral_constant<int, 2>)
  31. {
  32. if (::dup2(handle, STDOUT_FILENO) == -1)
  33. return -1;
  34. if (::dup2(handle, STDERR_FILENO) == -1)
  35. return -1;
  36. return 0;
  37. }
  38. template<int p1, int p2, typename Buffer>
  39. struct async_out_buffer : ::boost::process::detail::posix::handler_base_ext,
  40. ::boost::process::detail::posix::require_io_context,
  41. ::boost::process::detail::uses_handles
  42. {
  43. Buffer & buf;
  44. std::shared_ptr<boost::process::async_pipe> pipe;
  45. std::array<int, 4> get_used_handles()
  46. {
  47. const auto pp1 = p1 != -1 ? p1 : p2;
  48. const auto pp2 = p2 != -1 ? p2 : p1;
  49. if (pipe)
  50. return {pipe->native_source(), pipe->native_sink(), pp1, pp2};
  51. else //if pipe is not constructed, limit_ds is invoked before -> this also means on_exec_setup gets invoked before.
  52. return {pp1, pp2, pp1, pp2};
  53. }
  54. async_out_buffer(Buffer & buf) : buf(buf)
  55. {
  56. }
  57. template <typename Executor>
  58. inline void on_success(Executor &exec)
  59. {
  60. auto pipe = this->pipe;
  61. boost::asio::async_read(*pipe, buf,
  62. [pipe](const boost::system::error_code&, std::size_t){});
  63. this->pipe = nullptr;
  64. std::move(*pipe).sink().close();
  65. }
  66. template<typename Executor>
  67. void on_error(Executor &, const std::error_code &) const
  68. {
  69. std::move(*pipe).sink().close();
  70. }
  71. template<typename Executor>
  72. void on_setup(Executor & exec)
  73. {
  74. pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
  75. }
  76. template <typename Executor>
  77. void on_exec_setup(Executor &exec)
  78. {
  79. int res = apply_out_handles(pipe->native_sink(),
  80. std::integral_constant<int, p1>(), std::integral_constant<int, p2>());
  81. if (res == -1)
  82. exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  83. ::close(pipe->native_sink());
  84. ::close(pipe->native_source());
  85. }
  86. };
  87. template<int p1, int p2, typename Type>
  88. struct async_out_future : ::boost::process::detail::posix::handler_base_ext,
  89. ::boost::process::detail::posix::require_io_context
  90. {
  91. std::shared_ptr<std::promise<Type>> promise = std::make_shared<std::promise<Type>>();
  92. std::shared_ptr<boost::asio::streambuf> buffer = std::make_shared<boost::asio::streambuf>();
  93. std::shared_ptr<boost::process::async_pipe> pipe;
  94. async_out_future(std::future<Type> & fut)
  95. {
  96. fut = promise->get_future();
  97. }
  98. template <typename Executor>
  99. inline void on_success(Executor &)
  100. {
  101. auto pipe_ = this->pipe;
  102. auto buffer_ = this->buffer;
  103. auto promise_ = this->promise;
  104. boost::asio::async_read(*pipe_, *buffer_,
  105. [pipe_, buffer_, promise_](const boost::system::error_code& ec, std::size_t)
  106. {
  107. if (ec && (ec.value() != ENOENT))
  108. {
  109. std::error_code e(ec.value(), std::system_category());
  110. promise_->set_exception(std::make_exception_ptr(process_error(e)));
  111. }
  112. else
  113. {
  114. std::istream is (buffer_.get());
  115. Type arg;
  116. arg.resize(buffer_->size());
  117. is.read(&*arg.begin(), buffer_->size());
  118. promise_->set_value(std::move(arg));
  119. }
  120. });
  121. std::move(*pipe_).sink().close();
  122. this->pipe = nullptr;
  123. }
  124. template<typename Executor>
  125. void on_error(Executor &, const std::error_code &) const
  126. {
  127. std::move(*pipe).sink().close();
  128. }
  129. template<typename Executor>
  130. void on_setup(Executor & exec)
  131. {
  132. pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
  133. }
  134. template <typename Executor>
  135. void on_exec_setup(Executor &exec)
  136. {
  137. int res = apply_out_handles(pipe->native_sink(),
  138. std::integral_constant<int, p1>(), std::integral_constant<int, p2>());
  139. if (res == -1)
  140. exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  141. ::close(pipe->native_sink());
  142. ::close(pipe->native_source());
  143. }
  144. };
  145. }}}}
  146. #endif