File indexing completed on 2025-01-18 09:50:08
0001
0002
0003
0004
0005
0006 #ifndef BOOST_PROCESS_WINDOWS_IO_CONTEXT_REF_HPP_
0007 #define BOOST_PROCESS_WINDOWS_IO_CONTEXT_REF_HPP_
0008
0009 #include <boost/process/detail/handler_base.hpp>
0010 #include <boost/process/detail/windows/async_handler.hpp>
0011 #include <boost/process/detail/windows/is_running.hpp>
0012 #include <boost/asio/io_context.hpp>
0013 #include <boost/asio/post.hpp>
0014 #include <boost/asio/windows/object_handle.hpp>
0015 #include <boost/winapi/process.hpp>
0016 #include <boost/winapi/handles.hpp>
0017
0018 #include <boost/fusion/algorithm/iteration/for_each.hpp>
0019 #include <boost/fusion/algorithm/transformation/filter_if.hpp>
0020 #include <boost/fusion/algorithm/transformation/transform.hpp>
0021 #include <boost/fusion/view/transform_view.hpp>
0022 #include <boost/fusion/container/vector/convert.hpp>
0023
0024
0025 #include <functional>
0026 #include <type_traits>
0027 #include <memory>
0028 #include <atomic>
0029 #include <vector>
0030
0031 #include <boost/type_index.hpp>
0032
0033 namespace boost { namespace process { namespace detail { namespace windows {
0034
0035 template<typename Executor>
0036 struct on_exit_handler_transformer
0037 {
0038 Executor & exec;
0039 on_exit_handler_transformer(Executor & exec) : exec(exec) {}
0040 template<typename Sig>
0041 struct result;
0042
0043 template<typename T>
0044 struct result<on_exit_handler_transformer<Executor>(T&)>
0045 {
0046 typedef typename T::on_exit_handler_t type;
0047 };
0048
0049 template<typename T>
0050 auto operator()(T& t) const -> typename T::on_exit_handler_t
0051 {
0052 return t.on_exit_handler(exec);
0053 }
0054 };
0055
0056 template<typename Executor>
0057 struct async_handler_collector
0058 {
0059 Executor & exec;
0060 std::vector<std::function<void(int, const std::error_code & ec)>> &handlers;
0061
0062
0063 async_handler_collector(Executor & exec,
0064 std::vector<std::function<void(int, const std::error_code & ec)>> &handlers)
0065 : exec(exec), handlers(handlers) {}
0066
0067 template<typename T>
0068 void operator()(T & t) const
0069 {
0070 handlers.push_back(t.on_exit_handler(exec));
0071 }
0072 };
0073
0074
0075 struct io_context_ref : boost::process::detail::handler_base
0076 {
0077
0078 io_context_ref(boost::asio::io_context & ios)
0079 : ios(ios)
0080 {
0081 }
0082 boost::asio::io_context &get() {return ios;};
0083
0084 template <class Executor>
0085 void on_success(Executor& exec) const
0086 {
0087 auto asyncs = boost::fusion::filter_if<
0088 is_async_handler<
0089 typename std::remove_reference< boost::mpl::_ > ::type
0090 >>(exec.seq);
0091
0092
0093 if (boost::fusion::empty(asyncs))
0094 {
0095 return;
0096 }
0097
0098 ::boost::winapi::PROCESS_INFORMATION_ & proc = exec.proc_info;
0099 auto this_proc = ::boost::winapi::GetCurrentProcess();
0100
0101 auto proc_in = proc.hProcess;;
0102 ::boost::winapi::HANDLE_ process_handle;
0103
0104 if (!::boost::winapi::DuplicateHandle(
0105 this_proc, proc_in, this_proc, &process_handle, 0,
0106 static_cast<::boost::winapi::BOOL_>(true),
0107 ::boost::winapi::DUPLICATE_SAME_ACCESS_))
0108
0109 exec.set_error(::boost::process::detail::get_last_error(),
0110 "Duplicate Pipe Failed");
0111
0112
0113 std::vector<std::function<void(int, const std::error_code & ec)>> funcs;
0114 funcs.reserve(boost::fusion::size(asyncs));
0115 boost::fusion::for_each(asyncs, async_handler_collector<Executor>(exec, funcs));
0116
0117 wait_handler wh(std::move(funcs), ios, process_handle, exec.exit_status);
0118
0119 ::boost::winapi::DWORD_ code;
0120 if(::boost::winapi::GetExitCodeProcess(process_handle, &code)
0121 && code != still_active)
0122 {
0123 ::boost::asio::post(wh.handle->get_executor(), std::move(wh));
0124 return;
0125 }
0126
0127
0128 auto handle_p = wh.handle.get();
0129 handle_p->async_wait(std::move(wh));
0130 }
0131
0132
0133 struct wait_handler
0134 {
0135 std::vector<std::function<void(int, const std::error_code & ec)>> funcs;
0136 std::unique_ptr<boost::asio::windows::object_handle> handle;
0137 std::shared_ptr<std::atomic<int>> exit_status;
0138 wait_handler(const wait_handler & ) = delete;
0139 wait_handler(wait_handler && ) = default;
0140 wait_handler(std::vector<std::function<void(int, const std::error_code & ec)>> && funcs,
0141 boost::asio::io_context & ios, void * handle,
0142 const std::shared_ptr<std::atomic<int>> &exit_status)
0143 : funcs(std::move(funcs)),
0144 handle(new boost::asio::windows::object_handle(
0145 asio::prefer(ios.get_executor(), asio::execution::outstanding_work.tracked), handle)),
0146 exit_status(exit_status)
0147 {
0148
0149 }
0150 void operator()(const boost::system::error_code & ec_in = {})
0151 {
0152 std::error_code ec;
0153 if (ec_in)
0154 ec = std::error_code(ec_in.value(), std::system_category());
0155
0156 ::boost::winapi::DWORD_ code;
0157 ::boost::winapi::GetExitCodeProcess(handle->native_handle(), &code);
0158 exit_status->store(code);
0159
0160 for (auto & func : funcs)
0161 func(code, ec);
0162 }
0163
0164 };
0165
0166 private:
0167 boost::asio::io_context &ios;
0168 };
0169
0170 }}}}
0171
0172 #endif