1 // Copyright (c) 2016 Klemens D. Morgenstern
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)
6 #ifndef BOOST_PROCESS_EXTENSIONS_HPP_
7 #define BOOST_PROCESS_EXTENSIONS_HPP_
9 #include <boost/process/detail/handler.hpp>
11 #if defined(BOOST_WINDOWS_API)
12 #include <boost/process/detail/windows/executor.hpp>
13 #include <boost/process/detail/windows/async_handler.hpp>
14 #include <boost/process/detail/windows/asio_fwd.hpp>
16 #include <boost/process/detail/posix/executor.hpp>
17 #include <boost/process/detail/posix/async_handler.hpp>
18 #include <boost/process/detail/posix/asio_fwd.hpp>
22 /** \file boost/process/extend.hpp
24 * This header which provides the types and functions provided for custom extensions.
27 Please refer to the <link linkend="boost_process.extend">tutorial</link> for more details.
35 template<typename Tuple>
36 inline asio::io_service& get_io_service(const Tuple & tup);
40 ///Namespace for extensions \attention This is experimental.
43 #if defined(BOOST_WINDOWS_API)
45 template<typename Char, typename Sequence>
46 using windows_executor = ::boost::process::detail::windows::executor<Char, Sequence>;
47 template<typename Sequence>
48 struct posix_executor;
50 #elif defined(BOOST_POSIX_API)
52 template<typename Sequence>
53 using posix_executor = ::boost::process::detail::posix::executor<Sequence>;
54 template<typename Char, typename Sequence>
55 struct windows_executor;
59 using ::boost::process::detail::handler;
60 using ::boost::process::detail::api::require_io_service;
61 using ::boost::process::detail::api::async_handler;
62 using ::boost::process::detail::get_io_service;
63 using ::boost::process::detail::get_last_error;
64 using ::boost::process::detail::throw_last_error;
66 ///This handler is invoked before the process in launched, to setup parameters. The required signature is `void(Exec &)`, where `Exec` is a template parameter.
67 constexpr boost::process::detail::make_handler_t<boost::process::detail::on_setup_> on_setup;
68 ///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.
69 constexpr boost::process::detail::make_handler_t<boost::process::detail::on_error_> on_error;
70 ///This handler is invoked if launching the process has succeeded. The required signature is `void(auto & exec)`, where `Exec` is a template parameter.
71 constexpr boost::process::detail::make_handler_t<boost::process::detail::on_success_> on_success;
73 #if defined(BOOST_POSIX_API) || defined(BOOST_PROCESS_DOXYGEN)
74 ///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.
75 constexpr ::boost::process::detail::make_handler_t<::boost::process::detail::posix::on_fork_error_ > on_fork_error;
76 ///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.
77 constexpr ::boost::process::detail::make_handler_t<::boost::process::detail::posix::on_exec_setup_ > on_exec_setup;
78 ///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.
79 constexpr ::boost::process::detail::make_handler_t<::boost::process::detail::posix::on_exec_error_ > on_exec_error;
82 #if defined(BOOST_PROCESS_DOXYGEN)
83 ///Helper function to get the last error code system-independent
84 inline std::error_code get_last_error();
86 ///Helper function to get and throw the last system error.
87 /// \throws boost::process::process_error
88 /// \param msg A message to add to the error code.
89 inline void throw_last_error(const std::string & msg);
90 ///\overload void throw_last_error(const std::string & msg)
91 inline void throw_last_error();
94 /** This function gets the io_service from the initializer sequence.
96 * \attention Yields a compile-time error if no `io_service` is provided.
97 * \param seq The Sequence of the initializer.
99 template<typename Sequence>
100 inline asio::io_service& get_io_service(const Sequence & seq);
102 /** This class is the base for every initializer, to be used for extensions.
104 * The usage is done through compile-time polymorphism, so that the required
105 * functions can be overloaded.
107 * \note None of the function need to be `const`.
112 ///This function is invoked before the process launch. \note It is not required to be const.
113 template <class Executor>
114 void on_setup(Executor&) const {}
116 /** This function is invoked if an error occured while trying to launch the process.
117 * \note It is not required to be const.
119 template <class Executor>
120 void on_error(Executor&, const std::error_code &) const {}
122 /** This function is invoked if the process was successfully launched.
123 * \note It is not required to be const.
125 template <class Executor>
126 void on_success(Executor&) const {}
128 /**This function is invoked if an error occured during the call of `fork`.
129 * \note This function will only be called on posix.
131 template<typename Executor>
132 void on_fork_error (Executor &, const std::error_code&) const {}
134 /**This function is invoked if the call of `fork` was successful, before
136 * \note This function will only be called on posix.
137 * \attention It will be invoked from the new process.
139 template<typename Executor>
140 void on_exec_setup (Executor &) const {}
142 /**This function is invoked if the call of `execve` failed.
143 * \note This function will only be called on posix.
144 * \attention It will be invoked from the new process.
146 template<typename Executor>
147 void on_exec_error (Executor &, const std::error_code&) const {}
152 /** Inheriting the class will tell the launching process that an `io_service` is
153 * needed. This should always be used when \ref get_io_service is used.
156 struct require_io_service {};
157 /** Inheriting this class will tell the launching function, that an event handler
158 * shall be invoked when the process exits. This automatically does also inherit
159 * \ref require_io_service.
161 * You must add the following function to your implementation:
164 template<typename Executor>
165 std::function<void(int, const std::error_code&)> on_exit_handler(Executor & exec)
167 auto handler = this->handler;
168 return [handler](int exit_code, const std::error_code & ec)
170 handler(static_cast<int>(exit_code), ec);
176 The callback will be obtained by calling this function on setup and it will be
177 invoked when the process exits.
180 * \warning Cannot be used with \ref boost::process::spawn
182 struct async_handler : handler, require_io_service
187 ///The posix executor type.
188 /** This type represents the posix executor and can be used for overloading in a custom handler.
189 * \note It is an alias for the implementation on posix, and a forward-declaration on windows.
191 * \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.
195 As information for extension development, here is the structure of the process launching (in pseudo-code and uml)
196 <xi:include href="posix_pseudocode.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
200 <para>The sequence if when no error occurs.</para>
203 <imagedata fileref="boost_process/posix_success.svg"/>
209 <para>The sequence if the execution fails.</para>
212 <imagedata fileref="boost_process/posix_exec_err.svg"/>
218 <para>The sequence if the fork fails.</para>
221 <imagedata fileref="boost_process/posix_fork_err.svg"/>
228 \note Error handling if execve fails is done through a pipe, unless \ref ignore_error is used.
231 template<typename Sequence>
232 struct posix_executor
234 ///A reference to the actual initializer-sequence
236 ///A pointer to the name of the executable.
237 const char * exe = nullptr;
238 ///A pointer to the argument-vector.
239 char *const* cmd_line = nullptr;
240 ///A pointer to the environment variables, as default it is set to [environ](http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html)
241 char **env = ::environ;
242 ///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. */
244 ///This shared-pointer holds the exit code. It's done this way, so it can be shared between an `asio::io_service` and \ref child.
245 std::shared_ptr<std::atomic<int>> exit_status = std::make_shared<std::atomic<int>>(still_active);
247 ///This function returns a const reference to the error state of the executor.
248 const std::error_code & error() const;
250 ///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
251 /// might throw an exception. \note This is the required way to handle errors in initializers.
252 void set_error(const std::error_code &ec, const std::string &msg);
253 ///\overload void set_error(const std::error_code &ec, const std::string &msg);
254 void set_error(const std::error_code &ec, const char* msg);
257 ///The windows executor type.
258 /** This type represents the posix executor and can be used for overloading in a custom handler.
260 * \note It is an alias for the implementation on posix, and a forward-declaration on windows.
261 * \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.
262 * \tparam Char The used char-type, either `char` or `wchar_t`.
266 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"/>
269 <para>The sequence for windows process creation.</para>
272 <imagedata fileref="boost_process/windows_exec.svg"/>
279 template<typename Char, typename Sequence>
280 struct windows_executor
282 ///A reference to the actual initializer-sequence
285 ///A pointer to the name of the executable. It's null by default.
286 const Char * exe = nullptr;
287 ///A pointer to the argument-vector. Must be set by some initializer.
288 char Char* cmd_line = nullptr;
289 ///A pointer to the environment variables. It's null by default.
290 char Char* env = nullptr;
291 ///A pointer to the working directory. It's null by default.
292 const Char * work_dir = nullptr;
294 ///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.
295 ::boost::detail::winapi::LPSECURITY_ATTRIBUTES_ proc_attrs = nullptr;
296 ///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.
297 ::boost::detail::winapi::LPSECURITY_ATTRIBUTES_ thread_attrs = nullptr;
298 ///A logical bool value setting whether handles shall be inherited or not.
299 ::boost::detail::winapi::BOOL_ inherit_handles = false;
301 ///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)
302 ::boost::detail::winapi::PROCESS_INFORMATION_ proc_info{nullptr, nullptr, 0,0};
305 ///This shared-pointer holds the exit code. It's done this way, so it can be shared between an `asio::io_service` and \ref child.
306 std::shared_ptr<std::atomic<int>> exit_status = std::make_shared<std::atomic<int>>(still_active);
308 ///This function returns a const reference to the error state of the executor.
309 const std::error_code & error() const;
311 ///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
312 /// might throw an exception. \note This is the required way to handle errors in initializers.
313 void set_error(const std::error_code &ec, const std::string &msg);
314 ///\overload void set_error(const std::error_code &ec, const std::string &msg);
315 void set_error(const std::error_code &ec, const char* msg);
317 ///The creation flags of the process
318 ::boost::detail::winapi::DWORD_ creation_flags;
319 ///The type of the [startup-info](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686331.aspx), depending on the char-type.
320 typedef typename detail::startup_info<Char>::type startup_info_t;
321 ///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.
322 typedef typename detail::startup_info_ex<Char>::type startup_info_ex_t;
323 ///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.
324 void set_startup_info_ex();
325 ///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.
326 startup_info_t startup_info;
327 ///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.
328 startup_info_ex_t startup_info_ex;