extend.hpp 14 KB

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