io_context_ref.hpp 4.9 KB

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