extend.hpp 14 KB

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