extend.hpp 14 KB

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