io_context_ref.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (c) 2016 Klemens D. Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PROCESS_WINDOWS_IO_CONTEXT_REF_HPP_
  6. #define BOOST_PROCESS_WINDOWS_IO_CONTEXT_REF_HPP_
  7. #include <boost/process/v1/detail/handler_base.hpp>
  8. #include <boost/process/v1/detail/windows/async_handler.hpp>
  9. #include <boost/process/v1/detail/windows/is_running.hpp>
  10. #include <boost/asio/io_context.hpp>
  11. #include <boost/asio/post.hpp>
  12. #include <boost/asio/windows/object_handle.hpp>
  13. #include <boost/winapi/process.hpp>
  14. #include <boost/winapi/handles.hpp>
  15. #include <boost/fusion/algorithm/iteration/for_each.hpp>
  16. #include <boost/fusion/algorithm/transformation/filter_if.hpp>
  17. #include <boost/fusion/algorithm/transformation/transform.hpp>
  18. #include <boost/fusion/view/transform_view.hpp>
  19. #include <boost/fusion/container/vector/convert.hpp>
  20. #include <functional>
  21. #include <type_traits>
  22. #include <memory>
  23. #include <atomic>
  24. #include <vector>
  25. #include <boost/type_index.hpp>
  26. namespace boost { namespace process { BOOST_PROCESS_V1_INLINE namespace v1 { namespace detail { namespace windows {
  27. template<typename Executor>
  28. struct on_exit_handler_transformer
  29. {
  30. Executor & exec;
  31. on_exit_handler_transformer(Executor & exec) : exec(exec) {}
  32. template<typename Sig>
  33. struct result;
  34. template<typename T>
  35. struct result<on_exit_handler_transformer<Executor>(T&)>
  36. {
  37. typedef typename T::on_exit_handler_t type;
  38. };
  39. template<typename T>
  40. auto operator()(T& t) const -> typename T::on_exit_handler_t
  41. {
  42. return t.on_exit_handler(exec);
  43. }
  44. };
  45. template<typename Executor>
  46. struct async_handler_collector
  47. {
  48. Executor & exec;
  49. std::vector<std::function<void(int, const std::error_code & ec)>> &handlers;
  50. async_handler_collector(Executor & exec,
  51. std::vector<std::function<void(int, const std::error_code & ec)>> &handlers)
  52. : exec(exec), handlers(handlers) {}
  53. template<typename T>
  54. void operator()(T & t) const
  55. {
  56. handlers.push_back(t.on_exit_handler(exec));
  57. }
  58. };
  59. //Also set's up waiting for the exit, so it can close async stuff.
  60. struct io_context_ref : boost::process::v1::detail::handler_base
  61. {
  62. io_context_ref(boost::asio::io_context & ios)
  63. : ios(ios)
  64. {
  65. }
  66. boost::asio::io_context &get() {return ios;};
  67. template <class Executor>
  68. void on_success(Executor& exec) const
  69. {
  70. auto asyncs = boost::fusion::filter_if<
  71. is_async_handler<
  72. typename std::remove_reference< boost::mpl::_ > ::type
  73. >>(exec.seq);
  74. //ok, check if there are actually any.
  75. if (boost::fusion::empty(asyncs))
  76. {
  77. return;
  78. }
  79. ::boost::winapi::PROCESS_INFORMATION_ & proc = exec.proc_info;
  80. auto this_proc = ::boost::winapi::GetCurrentProcess();
  81. auto proc_in = proc.hProcess;;
  82. ::boost::winapi::HANDLE_ process_handle;
  83. if (!::boost::winapi::DuplicateHandle(
  84. this_proc, proc_in, this_proc, &process_handle, 0,
  85. static_cast<::boost::winapi::BOOL_>(true),
  86. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  87. exec.set_error(::boost::process::v1::detail::get_last_error(),
  88. "Duplicate Pipe Failed");
  89. std::vector<std::function<void(int, const std::error_code & ec)>> funcs;
  90. funcs.reserve(boost::fusion::size(asyncs));
  91. boost::fusion::for_each(asyncs, async_handler_collector<Executor>(exec, funcs));
  92. wait_handler wh(std::move(funcs), ios, process_handle, exec.exit_status);
  93. ::boost::winapi::DWORD_ code;
  94. if(::boost::winapi::GetExitCodeProcess(process_handle, &code)
  95. && code != still_active)
  96. {
  97. ::boost::asio::post(wh.handle->get_executor(), std::move(wh));
  98. return;
  99. }
  100. auto handle_p = wh.handle.get();
  101. handle_p->async_wait(std::move(wh));
  102. }
  103. struct wait_handler
  104. {
  105. std::vector<std::function<void(int, const std::error_code & ec)>> funcs;
  106. std::unique_ptr<boost::asio::windows::object_handle> handle;
  107. std::shared_ptr<std::atomic<int>> exit_status;
  108. wait_handler(const wait_handler & ) = delete;
  109. wait_handler(wait_handler && ) = default;
  110. wait_handler(std::vector<std::function<void(int, const std::error_code & ec)>> && funcs,
  111. boost::asio::io_context & ios, void * handle,
  112. const std::shared_ptr<std::atomic<int>> &exit_status)
  113. : funcs(std::move(funcs)),
  114. handle(new boost::asio::windows::object_handle(
  115. asio::prefer(ios.get_executor(), asio::execution::outstanding_work.tracked), handle)),
  116. exit_status(exit_status)
  117. {
  118. }
  119. void operator()(const boost::system::error_code & ec_in = {})
  120. {
  121. std::error_code ec;
  122. if (ec_in)
  123. ec = std::error_code(ec_in.value(), std::system_category());
  124. ::boost::winapi::DWORD_ code;
  125. ::boost::winapi::GetExitCodeProcess(handle->native_handle(), &code);
  126. exit_status->store(code);
  127. for (auto & func : funcs)
  128. func(code, ec);
  129. }
  130. };
  131. private:
  132. boost::asio::io_context &ios;
  133. };
  134. }}}}}
  135. #endif /* BOOST_PROCESS_WINDOWS_IO_CONTEXT_REF_HPP_ */