extend.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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_EXTENSIONS_HPP_
  6. #define BOOST_PROCESS_EXTENSIONS_HPP_
  7. #include <boost/process/v1/detail/handler.hpp>
  8. #include <boost/process/v1/detail/used_handles.hpp>
  9. #include <memory>
  10. #if defined(BOOST_WINDOWS_API)
  11. #include <boost/process/v1/detail/windows/executor.hpp>
  12. #include <boost/process/v1/detail/windows/async_handler.hpp>
  13. #include <boost/process/v1/detail/windows/asio_fwd.hpp>
  14. #else
  15. #include <boost/process/v1/detail/posix/executor.hpp>
  16. #include <boost/process/v1/detail/posix/async_handler.hpp>
  17. #include <boost/process/v1/detail/posix/asio_fwd.hpp>
  18. #endif
  19. /** \file boost/process/extend.hpp
  20. *
  21. * This header which provides the types and functions provided for custom extensions.
  22. *
  23. * \xmlonly
  24. Please refer to the <link linkend="boost_process.extend">tutorial</link> for more details.
  25. \endxmlonly
  26. */
  27. namespace boost
  28. {
  29. namespace process
  30. {
  31. BOOST_PROCESS_V1_INLINE namespace v1
  32. {
  33. namespace detail
  34. {
  35. template<typename Tuple>
  36. inline asio::io_context &get_io_context(const Tuple &tup);
  37. }
  38. ///Namespace for extensions \attention This is experimental.
  39. namespace extend
  40. {
  41. #if defined(BOOST_WINDOWS_API)
  42. template<typename Char, typename Sequence>
  43. using windows_executor = ::boost::process::v1::detail::windows::executor<Char, Sequence>;
  44. template<typename Sequence>
  45. struct posix_executor;
  46. #elif defined(BOOST_POSIX_API)
  47. template<typename Sequence>
  48. using posix_executor = ::boost::process::v1::detail::posix::executor<Sequence>;
  49. template<typename Char, typename Sequence>
  50. struct windows_executor;
  51. #endif
  52. using ::boost::process::v1::detail::handler;
  53. using ::boost::process::v1::detail::api::require_io_context;
  54. using ::boost::process::v1::detail::api::async_handler;
  55. using ::boost::process::v1::detail::get_io_context;
  56. using ::boost::process::v1::detail::get_last_error;
  57. using ::boost::process::v1::detail::throw_last_error;
  58. using ::boost::process::v1::detail::uses_handles;
  59. using ::boost::process::v1::detail::foreach_used_handle;
  60. using ::boost::process::v1::detail::get_used_handles;
  61. ///This handler is invoked before the process in launched, to setup parameters. The required signature is `void(Exec &)`, where `Exec` is a template parameter.
  62. constexpr boost::process::v1::detail::make_handler_t<boost::process::v1::detail::on_setup_> on_setup;
  63. ///This handler is invoked if an error occurred. The required signature is `void(auto & exec, const std::error_code&)`, where `Exec` is a template parameter.
  64. constexpr boost::process::v1::detail::make_handler_t<boost::process::v1::detail::on_error_> on_error;
  65. ///This handler is invoked if launching the process has succeeded. The required signature is `void(auto & exec)`, where `Exec` is a template parameter.
  66. constexpr boost::process::v1::detail::make_handler_t<boost::process::v1::detail::on_success_> on_success;
  67. #if defined(BOOST_POSIX_API) || defined(BOOST_PROCESS_DOXYGEN)
  68. ///This handler is invoked if the fork failed. The required signature is `void(auto & exec)`, where `Exec` is a template parameter. \note Only available on posix.
  69. constexpr ::boost::process::v1::detail::make_handler_t<::boost::process::v1::detail::posix::on_fork_error_> on_fork_error;
  70. ///This handler is invoked if the fork succeeded. The required signature is `void(Exec &)`, where `Exec` is a template parameter. \note Only available on posix.
  71. constexpr ::boost::process::v1::detail::make_handler_t<::boost::process::v1::detail::posix::on_exec_setup_> on_exec_setup;
  72. ///This handler is invoked if the exec call errored. The required signature is `void(auto & exec)`, where `Exec` is a template parameter. \note Only available on posix.
  73. constexpr ::boost::process::v1::detail::make_handler_t<::boost::process::v1::detail::posix::on_exec_error_> on_exec_error;
  74. #endif
  75. #if defined(BOOST_PROCESS_DOXYGEN)
  76. ///Helper function to get the last error code system-independent
  77. inline std::error_code get_last_error();
  78. ///Helper function to get and throw the last system error.
  79. /// \throws boost::process::v1::process_error
  80. /// \param msg A message to add to the error code.
  81. inline void throw_last_error(const std::string & msg);
  82. ///\overload void throw_last_error(const std::string & msg)
  83. inline void throw_last_error();
  84. /** This function gets the io_context from the initializer sequence.
  85. *
  86. * \attention Yields a compile-time error if no `io_context` is provided.
  87. * \param seq The Sequence of the initializer.
  88. */
  89. template<typename Sequence>
  90. inline asio::io_context& get_io_context(const Sequence & seq);
  91. /** This class is the base for every initializer, to be used for extensions.
  92. *
  93. * The usage is done through compile-time polymorphism, so that the required
  94. * functions can be overloaded.
  95. *
  96. * \note None of the function need to be `const`.
  97. *
  98. */
  99. struct handler
  100. {
  101. ///This function is invoked before the process launch. \note It is not required to be const.
  102. template <class Executor>
  103. void on_setup(Executor&) const {}
  104. /** This function is invoked if an error occured while trying to launch the process.
  105. * \note It is not required to be const.
  106. */
  107. template <class Executor>
  108. void on_error(Executor&, const std::error_code &) const {}
  109. /** This function is invoked if the process was successfully launched.
  110. * \note It is not required to be const.
  111. */
  112. template <class Executor>
  113. void on_success(Executor&) const {}
  114. /**This function is invoked if an error occured during the call of `fork`.
  115. * \note This function will only be called on posix.
  116. */
  117. template<typename Executor>
  118. void on_fork_error (Executor &, const std::error_code&) const {}
  119. /**This function is invoked if the call of `fork` was successful, before
  120. * calling `execve`.
  121. * \note This function will only be called on posix.
  122. * \attention It will be invoked from the new process.
  123. */
  124. template<typename Executor>
  125. void on_exec_setup (Executor &) const {}
  126. /**This function is invoked if the call of `execve` failed.
  127. * \note This function will only be called on posix.
  128. * \attention It will be invoked from the new process.
  129. */
  130. template<typename Executor>
  131. void on_exec_error (Executor &, const std::error_code&) const {}
  132. };
  133. /** Inheriting the class will tell the launching process that an `io_context` is
  134. * needed. This should always be used when \ref get_io_context is used.
  135. *
  136. */
  137. struct require_io_context {};
  138. /** Inheriting this class will tell the launching function, that an event handler
  139. * shall be invoked when the process exits. This automatically does also inherit
  140. * \ref require_io_context.
  141. *
  142. * You must add the following function to your implementation:
  143. *
  144. \code{.cpp}
  145. template<typename Executor>
  146. std::function<void(int, const std::error_code&)> on_exit_handler(Executor & exec)
  147. {
  148. auto handler_ = this->handler;
  149. return [handler_](int exit_code, const std::error_code & ec)
  150. {
  151. handler_(static_cast<int>(exit_code), ec);
  152. };
  153. }
  154. \endcode
  155. The callback will be obtained by calling this function on setup and it will be
  156. invoked when the process exits.
  157. *
  158. * \warning Cannot be used with \ref boost::process::v1::spawn
  159. */
  160. struct async_handler : handler, require_io_context
  161. {
  162. };
  163. ///The posix executor type.
  164. /** This type represents the posix executor and can be used for overloading in a custom handler.
  165. * \note It is an alias for the implementation on posix, and a forward-declaration on windows.
  166. *
  167. * \tparam Sequence The used initializer-sequence, it is fulfills the boost.fusion [sequence](http://www.boost.org/doc/libs/master/libs/fusion/doc/html/fusion/sequence.html) concept.
  168. \xmlonly
  169. As information for extension development, here is the structure of the process launching (in pseudo-code and uml)
  170. <xi:include href="posix_pseudocode.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
  171. <mediaobject>
  172. <caption>
  173. <para>The sequence if when no error occurs.</para>
  174. </caption>
  175. <imageobject>
  176. <imagedata fileref="boost_process/posix_success.svg"/>
  177. </imageobject>
  178. </mediaobject>
  179. <mediaobject>
  180. <caption>
  181. <para>The sequence if the execution fails.</para>
  182. </caption>
  183. <imageobject>
  184. <imagedata fileref="boost_process/posix_exec_err.svg"/>
  185. </imageobject>
  186. </mediaobject>
  187. <mediaobject>
  188. <caption>
  189. <para>The sequence if the fork fails.</para>
  190. </caption>
  191. <imageobject>
  192. <imagedata fileref="boost_process/posix_fork_err.svg"/>
  193. </imageobject>
  194. </mediaobject>
  195. \endxmlonly
  196. \note Error handling if execve fails is done through a pipe, unless \ref ignore_error is used.
  197. */
  198. template<typename Sequence>
  199. struct posix_executor
  200. {
  201. ///A reference to the actual initializer-sequence
  202. Sequence & seq;
  203. ///A pointer to the name of the executable.
  204. const char * exe = nullptr;
  205. ///A pointer to the argument-vector.
  206. char *const* cmd_line = nullptr;
  207. ///A pointer to the environment variables, as default it is set to [environ](http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html)
  208. char **env = ::environ;
  209. ///The pid of the process - it will be -1 before invoking [fork](http://pubs.opengroup.org/onlinepubs/009695399/functions/fork.html), and after forking either 0 for the new process or a positive value if in the current process. */
  210. pid_t pid = -1;
  211. ///This shared-pointer holds the exit code. It's done this way, so it can be shared between an `asio::io_context` and \ref child.
  212. std::shared_ptr<std::atomic<int>> exit_status = std::make_shared<std::atomic<int>>(still_active);
  213. ///This function returns a const reference to the error state of the executor.
  214. const std::error_code & error() const;
  215. ///This function can be used to report an error to the executor. This will be handled according to the configuration of the executor, i.e. it
  216. /// might throw an exception. \note This is the required way to handle errors in initializers.
  217. void set_error(const std::error_code &ec, const std::string &msg);
  218. ///\overload void set_error(const std::error_code &ec, const std::string &msg);
  219. void set_error(const std::error_code &ec, const char* msg);
  220. };
  221. ///The windows executor type.
  222. /** This type represents the posix executor and can be used for overloading in a custom handler.
  223. *
  224. * \note It is an alias for the implementation on posix, and a forward-declaration on windows.
  225. * \tparam Sequence The used initializer-sequence, it is fulfills the boost.fusion [sequence](http://www.boost.org/doc/libs/master/libs/fusion/doc/html/fusion/sequence.html) concept.
  226. * \tparam Char The used char-type, either `char` or `wchar_t`.
  227. *
  228. \xmlonly
  229. As information for extension development, here is the structure of the process launching (in pseudo-code and uml)<xi:include href="windows_pseudocode.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
  230. <mediaobject>
  231. <caption>
  232. <para>The sequence for windows process creation.</para>
  233. </caption>
  234. <imageobject>
  235. <imagedata fileref="boost_process/windows_exec.svg"/>
  236. </imageobject>
  237. </mediaobject>
  238. \endxmlonly
  239. */
  240. template<typename Char, typename Sequence>
  241. struct windows_executor
  242. {
  243. ///A reference to the actual initializer-sequence
  244. Sequence & seq;
  245. ///A pointer to the name of the executable. It's null by default.
  246. const Char * exe = nullptr;
  247. ///A pointer to the argument-vector. Must be set by some initializer.
  248. char Char* cmd_line = nullptr;
  249. ///A pointer to the environment variables. It's null by default.
  250. char Char* env = nullptr;
  251. ///A pointer to the working directory. It's null by default.
  252. const Char * work_dir = nullptr;
  253. ///A pointer to the process-attributes of type [SECURITY_ATTRIBUTES](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379560.aspx). It's null by default.
  254. ::boost::detail::winapi::LPSECURITY_ATTRIBUTES_ proc_attrs = nullptr;
  255. ///A pointer to the thread-attributes of type [SECURITY_ATTRIBUTES](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379560.aspx). It' null by default.
  256. ::boost::detail::winapi::LPSECURITY_ATTRIBUTES_ thread_attrs = nullptr;
  257. ///A logical bool value setting whether handles shall be inherited or not.
  258. ::boost::detail::winapi::BOOL_ inherit_handles = false;
  259. ///The element holding the process-information after process creation. The type is [PROCESS_INFORMATION](https://msdn.microsoft.com/en-us/library/windows/desktop/ms684873.aspx)
  260. ::boost::detail::winapi::PROCESS_INFORMATION_ proc_info{nullptr, nullptr, 0,0};
  261. ///This shared-pointer holds the exit code. It's done this way, so it can be shared between an `asio::io_context` and \ref child.
  262. std::shared_ptr<std::atomic<int>> exit_status = std::make_shared<std::atomic<int>>(still_active);
  263. ///This function returns a const reference to the error state of the executor.
  264. const std::error_code & error() const;
  265. ///This function can be used to report an error to the executor. This will be handled according to the configuration of the executor, i.e. it
  266. /// might throw an exception. \note This is the required way to handle errors in initializers.
  267. void set_error(const std::error_code &ec, const std::string &msg);
  268. ///\overload void set_error(const std::error_code &ec, const std::string &msg);
  269. void set_error(const std::error_code &ec, const char* msg);
  270. ///The creation flags of the process
  271. ::boost::detail::winapi::DWORD_ creation_flags;
  272. ///The type of the [startup-info](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686331.aspx), depending on the char-type.
  273. typedef typename detail::startup_info<Char>::type startup_info_t;
  274. ///The type of the [extended startup-info](https://msdn.microsoft.com/de-de/library/windows/desktop/ms686329.aspx), depending the char-type; only defined with winapi-version equal or higher than 6.
  275. typedef typename detail::startup_info_ex<Char>::type startup_info_ex_t;
  276. ///This function switches the information, so that the extended structure is used. \note It's only defined with winapi-version equal or higher than 6.
  277. void set_startup_info_ex();
  278. ///This element is an instance or a reference (if \ref startup_info_ex exists) to the [startup-info](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686331.aspx) for the process.
  279. startup_info_t startup_info;
  280. ///This element is the instance of the [extended startup-info](https://msdn.microsoft.com/de-de/library/windows/desktop/ms686329.aspx). It is only available with a winapi-version equal or highter than 6.
  281. startup_info_ex_t startup_info_ex;
  282. };
  283. #endif
  284. }
  285. }
  286. }
  287. }
  288. #endif