executor.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. // Copyright (c) 2016 Klemens D. Morgenstern
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_PROCESS_WINDOWS_EXECUTOR_HPP
  11. #define BOOST_PROCESS_WINDOWS_EXECUTOR_HPP
  12. #include <boost/process/detail/child_decl.hpp>
  13. #include <boost/process/detail/windows/is_running.hpp>
  14. #include <boost/process/detail/traits.hpp>
  15. #include <boost/process/error.hpp>
  16. #include <boost/fusion/algorithm/iteration/for_each.hpp>
  17. #include <boost/winapi/handles.hpp>
  18. #include <boost/winapi/process.hpp>
  19. #include <boost/none.hpp>
  20. #include <system_error>
  21. #include <memory>
  22. #include <atomic>
  23. #include <cstring>
  24. namespace boost { namespace process {
  25. namespace detail { namespace windows {
  26. template<typename CharType> struct startup_info;
  27. #if !defined( BOOST_NO_ANSI_APIS )
  28. template<> struct startup_info<char>
  29. {
  30. typedef ::boost::winapi::STARTUPINFOA_ type;
  31. };
  32. #endif
  33. template<> struct startup_info<wchar_t>
  34. {
  35. typedef ::boost::winapi::STARTUPINFOW_ type;
  36. };
  37. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  38. template<typename CharType> struct startup_info_ex;
  39. #if !defined( BOOST_NO_ANSI_APIS )
  40. template<> struct startup_info_ex<char>
  41. {
  42. typedef ::boost::winapi::STARTUPINFOEXA_ type;
  43. };
  44. #endif
  45. template<> struct startup_info_ex<wchar_t>
  46. {
  47. typedef ::boost::winapi::STARTUPINFOEXW_ type;
  48. };
  49. #endif
  50. #if ( BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6 )
  51. template<typename CharT>
  52. struct startup_info_impl
  53. {
  54. ::boost::winapi::DWORD_ creation_flags = 0;
  55. typedef typename startup_info_ex<CharT>::type startup_info_ex_t;
  56. typedef typename startup_info<CharT>::type startup_info_t;
  57. startup_info_ex_t startup_info_ex
  58. {startup_info_t {sizeof(startup_info_t), nullptr, nullptr, nullptr,
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr,
  60. ::boost::winapi::invalid_handle_value,
  61. ::boost::winapi::invalid_handle_value,
  62. ::boost::winapi::invalid_handle_value},
  63. nullptr
  64. };
  65. startup_info_t & startup_info = startup_info_ex.StartupInfo;
  66. void set_startup_info_ex()
  67. {
  68. startup_info.cb = sizeof(startup_info_ex_t);
  69. creation_flags |= ::boost::winapi::EXTENDED_STARTUPINFO_PRESENT_;
  70. }
  71. };
  72. #else
  73. template<typename CharT>
  74. struct startup_info_impl
  75. {
  76. typedef typename startup_info<CharT>::type startup_info_t;
  77. ::boost::winapi::DWORD_ creation_flags = 0;
  78. startup_info_t startup_info
  79. {sizeof(startup_info_t), nullptr, nullptr, nullptr,
  80. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr,
  81. ::boost::winapi::invalid_handle_value,
  82. ::boost::winapi::invalid_handle_value,
  83. ::boost::winapi::invalid_handle_value};
  84. };
  85. #endif
  86. template<typename Char, typename Sequence>
  87. class executor : public startup_info_impl<Char>
  88. {
  89. void internal_error_handle(const std::error_code &, const char*, boost::mpl::false_, boost::mpl::true_) {}
  90. void internal_error_handle(const std::error_code &, const char*, boost::mpl::true_, boost::mpl::true_) {}
  91. void internal_error_handle(const std::error_code &ec, const char*, boost::mpl::true_, boost::mpl::false_ )
  92. {
  93. this->_ec = ec;
  94. }
  95. void internal_error_handle(const std::error_code &ec, const char* msg, boost::mpl::false_, boost::mpl::false_ )
  96. {
  97. throw process_error(ec, msg);
  98. }
  99. struct on_setup_t
  100. {
  101. executor & exec;
  102. on_setup_t(executor & exec) : exec(exec) {};
  103. template<typename T>
  104. void operator()(T & t) const
  105. {
  106. if (!exec.error())
  107. t.on_setup(exec);
  108. }
  109. };
  110. struct on_error_t
  111. {
  112. executor & exec;
  113. const std::error_code & error;
  114. on_error_t(executor & exec, const std::error_code & error) : exec(exec), error(error) {};
  115. template<typename T>
  116. void operator()(T & t) const
  117. {
  118. t.on_error(exec, error);
  119. }
  120. };
  121. struct on_success_t
  122. {
  123. executor & exec;
  124. on_success_t(executor & exec) : exec(exec) {};
  125. template<typename T>
  126. void operator()(T & t) const
  127. {
  128. if (!exec.error())
  129. t.on_success(exec);
  130. }
  131. };
  132. typedef typename ::boost::process::detail::has_error_handler<Sequence>::type has_error_handler;
  133. typedef typename ::boost::process::detail::has_ignore_error <Sequence>::type has_ignore_error;
  134. std::error_code _ec{0, std::system_category()};
  135. public:
  136. std::shared_ptr<std::atomic<int>> exit_status = std::make_shared<std::atomic<int>>(still_active);
  137. executor(Sequence & seq) : seq(seq)
  138. {
  139. }
  140. child operator()()
  141. {
  142. on_setup_t on_setup_fn(*this);
  143. boost::fusion::for_each(seq, on_setup_fn);
  144. if (_ec)
  145. {
  146. on_error_t on_error_fn(*this, _ec);
  147. boost::fusion::for_each(seq, on_error_fn);
  148. return child();
  149. }
  150. //NOTE: The non-cast cmd-line string can only be modified by the wchar_t variant which is currently disabled.
  151. int err_code = ::boost::winapi::create_process(
  152. exe, // LPCSTR_ lpApplicationName,
  153. const_cast<Char*>(cmd_line), // LPSTR_ lpCommandLine,
  154. proc_attrs, // LPSECURITY_ATTRIBUTES_ lpProcessAttributes,
  155. thread_attrs, // LPSECURITY_ATTRIBUTES_ lpThreadAttributes,
  156. inherit_handles, // INT_ bInheritHandles,
  157. this->creation_flags, // DWORD_ dwCreationFlags,
  158. reinterpret_cast<void*>(const_cast<Char*>(env)), // LPVOID_ lpEnvironment,
  159. work_dir, // LPCSTR_ lpCurrentDirectory,
  160. &this->startup_info, // LPSTARTUPINFOA_ lpStartupInfo,
  161. &proc_info); // LPPROCESS_INFORMATION_ lpProcessInformation)
  162. child c{child_handle(proc_info), exit_status};
  163. if (err_code != 0)
  164. {
  165. _ec.clear();
  166. on_success_t on_success_fn(*this);
  167. boost::fusion::for_each(seq, on_success_fn);
  168. }
  169. else
  170. set_error(::boost::process::detail::get_last_error(),
  171. " CreateProcess failed");
  172. if ( _ec)
  173. {
  174. on_error_t on_err(*this, _ec);
  175. boost::fusion::for_each(seq, on_err);
  176. return child();
  177. }
  178. else
  179. return c;
  180. }
  181. void set_error(const std::error_code & ec, const char* msg = "Unknown Error.")
  182. {
  183. internal_error_handle(ec, msg, has_error_handler(), has_ignore_error());
  184. }
  185. void set_error(const std::error_code & ec, const std::string msg = "Unknown Error.")
  186. {
  187. internal_error_handle(ec, msg.c_str(), has_error_handler(), has_ignore_error());
  188. }
  189. const std::error_code& error() const {return _ec;}
  190. ::boost::winapi::LPSECURITY_ATTRIBUTES_ proc_attrs = nullptr;
  191. ::boost::winapi::LPSECURITY_ATTRIBUTES_ thread_attrs = nullptr;
  192. ::boost::winapi::BOOL_ inherit_handles = false;
  193. const Char * work_dir = nullptr;
  194. const Char * cmd_line = nullptr;
  195. const Char * exe = nullptr;
  196. const Char * env = nullptr;
  197. Sequence & seq;
  198. ::boost::winapi::PROCESS_INFORMATION_ proc_info{nullptr, nullptr, 0,0};
  199. };
  200. template<typename Char, typename Tup>
  201. executor<Char, Tup> make_executor(Tup & tup)
  202. {
  203. return executor<Char, Tup>(tup);
  204. }
  205. }}}}
  206. #endif