|
||||
File indexing completed on 2025-01-30 09:58:04
0001 // Copyright (c) 2016 Klemens D. Morgenstern 0002 // 0003 // Distributed under the Boost Software License, Version 1.0. (See accompanying 0004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 0005 0006 #ifndef BOOST_PROCESS_EXTENSIONS_HPP_ 0007 #define BOOST_PROCESS_EXTENSIONS_HPP_ 0008 0009 #include <boost/process/detail/handler.hpp> 0010 #include <boost/process/detail/used_handles.hpp> 0011 #include <memory> 0012 0013 #if defined(BOOST_WINDOWS_API) 0014 #include <boost/process/detail/windows/executor.hpp> 0015 #include <boost/process/detail/windows/async_handler.hpp> 0016 #include <boost/process/detail/windows/asio_fwd.hpp> 0017 #else 0018 #include <boost/process/detail/posix/executor.hpp> 0019 #include <boost/process/detail/posix/async_handler.hpp> 0020 #include <boost/process/detail/posix/asio_fwd.hpp> 0021 #endif 0022 0023 0024 /** \file boost/process/extend.hpp 0025 * 0026 * This header which provides the types and functions provided for custom extensions. 0027 * 0028 * \xmlonly 0029 Please refer to the <link linkend="boost_process.extend">tutorial</link> for more details. 0030 \endxmlonly 0031 */ 0032 0033 0034 namespace boost { 0035 namespace process { 0036 namespace detail { 0037 template<typename Tuple> 0038 inline asio::io_context& get_io_context(const Tuple & tup); 0039 } 0040 0041 0042 ///Namespace for extensions \attention This is experimental. 0043 namespace extend { 0044 0045 #if defined(BOOST_WINDOWS_API) 0046 0047 template<typename Char, typename Sequence> 0048 using windows_executor = ::boost::process::detail::windows::executor<Char, Sequence>; 0049 template<typename Sequence> 0050 struct posix_executor; 0051 0052 #elif defined(BOOST_POSIX_API) 0053 0054 template<typename Sequence> 0055 using posix_executor = ::boost::process::detail::posix::executor<Sequence>; 0056 template<typename Char, typename Sequence> 0057 struct windows_executor; 0058 0059 #endif 0060 0061 using ::boost::process::detail::handler; 0062 using ::boost::process::detail::api::require_io_context; 0063 using ::boost::process::detail::api::async_handler; 0064 using ::boost::process::detail::get_io_context; 0065 using ::boost::process::detail::get_last_error; 0066 using ::boost::process::detail::throw_last_error; 0067 using ::boost::process::detail::uses_handles; 0068 using ::boost::process::detail::foreach_used_handle; 0069 using ::boost::process::detail::get_used_handles; 0070 0071 ///This handler is invoked before the process in launched, to setup parameters. The required signature is `void(Exec &)`, where `Exec` is a template parameter. 0072 constexpr boost::process::detail::make_handler_t<boost::process::detail::on_setup_> on_setup; 0073 ///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. 0074 constexpr boost::process::detail::make_handler_t<boost::process::detail::on_error_> on_error; 0075 ///This handler is invoked if launching the process has succeeded. The required signature is `void(auto & exec)`, where `Exec` is a template parameter. 0076 constexpr boost::process::detail::make_handler_t<boost::process::detail::on_success_> on_success; 0077 0078 #if defined(BOOST_POSIX_API) || defined(BOOST_PROCESS_DOXYGEN) 0079 ///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. 0080 constexpr ::boost::process::detail::make_handler_t<::boost::process::detail::posix::on_fork_error_ > on_fork_error; 0081 ///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. 0082 constexpr ::boost::process::detail::make_handler_t<::boost::process::detail::posix::on_exec_setup_ > on_exec_setup; 0083 ///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. 0084 constexpr ::boost::process::detail::make_handler_t<::boost::process::detail::posix::on_exec_error_ > on_exec_error; 0085 #endif 0086 0087 #if defined(BOOST_PROCESS_DOXYGEN) 0088 ///Helper function to get the last error code system-independent 0089 inline std::error_code get_last_error(); 0090 0091 ///Helper function to get and throw the last system error. 0092 /// \throws boost::process::process_error 0093 /// \param msg A message to add to the error code. 0094 inline void throw_last_error(const std::string & msg); 0095 ///\overload void throw_last_error(const std::string & msg) 0096 inline void throw_last_error(); 0097 0098 0099 /** This function gets the io_context from the initializer sequence. 0100 * 0101 * \attention Yields a compile-time error if no `io_context` is provided. 0102 * \param seq The Sequence of the initializer. 0103 */ 0104 template<typename Sequence> 0105 inline asio::io_context& get_io_context(const Sequence & seq); 0106 0107 /** This class is the base for every initializer, to be used for extensions. 0108 * 0109 * The usage is done through compile-time polymorphism, so that the required 0110 * functions can be overloaded. 0111 * 0112 * \note None of the function need to be `const`. 0113 * 0114 */ 0115 struct handler 0116 { 0117 ///This function is invoked before the process launch. \note It is not required to be const. 0118 template <class Executor> 0119 void on_setup(Executor&) const {} 0120 0121 /** This function is invoked if an error occured while trying to launch the process. 0122 * \note It is not required to be const. 0123 */ 0124 template <class Executor> 0125 void on_error(Executor&, const std::error_code &) const {} 0126 0127 /** This function is invoked if the process was successfully launched. 0128 * \note It is not required to be const. 0129 */ 0130 template <class Executor> 0131 void on_success(Executor&) const {} 0132 0133 /**This function is invoked if an error occured during the call of `fork`. 0134 * \note This function will only be called on posix. 0135 */ 0136 template<typename Executor> 0137 void on_fork_error (Executor &, const std::error_code&) const {} 0138 0139 /**This function is invoked if the call of `fork` was successful, before 0140 * calling `execve`. 0141 * \note This function will only be called on posix. 0142 * \attention It will be invoked from the new process. 0143 */ 0144 template<typename Executor> 0145 void on_exec_setup (Executor &) const {} 0146 0147 /**This function is invoked if the call of `execve` failed. 0148 * \note This function will only be called on posix. 0149 * \attention It will be invoked from the new process. 0150 */ 0151 template<typename Executor> 0152 void on_exec_error (Executor &, const std::error_code&) const {} 0153 0154 }; 0155 0156 0157 /** Inheriting the class will tell the launching process that an `io_context` is 0158 * needed. This should always be used when \ref get_io_context is used. 0159 * 0160 */ 0161 struct require_io_context {}; 0162 /** Inheriting this class will tell the launching function, that an event handler 0163 * shall be invoked when the process exits. This automatically does also inherit 0164 * \ref require_io_context. 0165 * 0166 * You must add the following function to your implementation: 0167 * 0168 \code{.cpp} 0169 template<typename Executor> 0170 std::function<void(int, const std::error_code&)> on_exit_handler(Executor & exec) 0171 { 0172 auto handler_ = this->handler; 0173 return [handler_](int exit_code, const std::error_code & ec) 0174 { 0175 handler_(static_cast<int>(exit_code), ec); 0176 }; 0177 0178 } 0179 \endcode 0180 0181 The callback will be obtained by calling this function on setup and it will be 0182 invoked when the process exits. 0183 0184 * 0185 * \warning Cannot be used with \ref boost::process::spawn 0186 */ 0187 struct async_handler : handler, require_io_context 0188 { 0189 0190 }; 0191 0192 ///The posix executor type. 0193 /** This type represents the posix executor and can be used for overloading in a custom handler. 0194 * \note It is an alias for the implementation on posix, and a forward-declaration on windows. 0195 * 0196 * \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. 0197 0198 0199 \xmlonly 0200 As information for extension development, here is the structure of the process launching (in pseudo-code and uml) 0201 <xi:include href="posix_pseudocode.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/> 0202 0203 <mediaobject> 0204 <caption> 0205 <para>The sequence if when no error occurs.</para> 0206 </caption> 0207 <imageobject> 0208 <imagedata fileref="boost_process/posix_success.svg"/> 0209 </imageobject> 0210 </mediaobject> 0211 0212 <mediaobject> 0213 <caption> 0214 <para>The sequence if the execution fails.</para> 0215 </caption> 0216 <imageobject> 0217 <imagedata fileref="boost_process/posix_exec_err.svg"/> 0218 </imageobject> 0219 </mediaobject> 0220 0221 <mediaobject> 0222 <caption> 0223 <para>The sequence if the fork fails.</para> 0224 </caption> 0225 <imageobject> 0226 <imagedata fileref="boost_process/posix_fork_err.svg"/> 0227 </imageobject> 0228 </mediaobject> 0229 0230 \endxmlonly 0231 0232 0233 \note Error handling if execve fails is done through a pipe, unless \ref ignore_error is used. 0234 0235 */ 0236 template<typename Sequence> 0237 struct posix_executor 0238 { 0239 ///A reference to the actual initializer-sequence 0240 Sequence & seq; 0241 ///A pointer to the name of the executable. 0242 const char * exe = nullptr; 0243 ///A pointer to the argument-vector. 0244 char *const* cmd_line = nullptr; 0245 ///A pointer to the environment variables, as default it is set to [environ](http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html) 0246 char **env = ::environ; 0247 ///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. */ 0248 pid_t pid = -1; 0249 ///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. 0250 std::shared_ptr<std::atomic<int>> exit_status = std::make_shared<std::atomic<int>>(still_active); 0251 0252 ///This function returns a const reference to the error state of the executor. 0253 const std::error_code & error() const; 0254 0255 ///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 0256 /// might throw an exception. \note This is the required way to handle errors in initializers. 0257 void set_error(const std::error_code &ec, const std::string &msg); 0258 ///\overload void set_error(const std::error_code &ec, const std::string &msg); 0259 void set_error(const std::error_code &ec, const char* msg); 0260 }; 0261 0262 ///The windows executor type. 0263 /** This type represents the posix executor and can be used for overloading in a custom handler. 0264 * 0265 * \note It is an alias for the implementation on posix, and a forward-declaration on windows. 0266 * \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. 0267 * \tparam Char The used char-type, either `char` or `wchar_t`. 0268 * 0269 0270 \xmlonly 0271 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"/> 0272 <mediaobject> 0273 <caption> 0274 <para>The sequence for windows process creation.</para> 0275 </caption> 0276 <imageobject> 0277 <imagedata fileref="boost_process/windows_exec.svg"/> 0278 </imageobject> 0279 </mediaobject> 0280 \endxmlonly 0281 0282 */ 0283 0284 template<typename Char, typename Sequence> 0285 struct windows_executor 0286 { 0287 ///A reference to the actual initializer-sequence 0288 Sequence & seq; 0289 0290 ///A pointer to the name of the executable. It's null by default. 0291 const Char * exe = nullptr; 0292 ///A pointer to the argument-vector. Must be set by some initializer. 0293 char Char* cmd_line = nullptr; 0294 ///A pointer to the environment variables. It's null by default. 0295 char Char* env = nullptr; 0296 ///A pointer to the working directory. It's null by default. 0297 const Char * work_dir = nullptr; 0298 0299 ///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. 0300 ::boost::detail::winapi::LPSECURITY_ATTRIBUTES_ proc_attrs = nullptr; 0301 ///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. 0302 ::boost::detail::winapi::LPSECURITY_ATTRIBUTES_ thread_attrs = nullptr; 0303 ///A logical bool value setting whether handles shall be inherited or not. 0304 ::boost::detail::winapi::BOOL_ inherit_handles = false; 0305 0306 ///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) 0307 ::boost::detail::winapi::PROCESS_INFORMATION_ proc_info{nullptr, nullptr, 0,0}; 0308 0309 0310 ///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. 0311 std::shared_ptr<std::atomic<int>> exit_status = std::make_shared<std::atomic<int>>(still_active); 0312 0313 ///This function returns a const reference to the error state of the executor. 0314 const std::error_code & error() const; 0315 0316 ///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 0317 /// might throw an exception. \note This is the required way to handle errors in initializers. 0318 void set_error(const std::error_code &ec, const std::string &msg); 0319 ///\overload void set_error(const std::error_code &ec, const std::string &msg); 0320 void set_error(const std::error_code &ec, const char* msg); 0321 0322 ///The creation flags of the process 0323 ::boost::detail::winapi::DWORD_ creation_flags; 0324 ///The type of the [startup-info](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686331.aspx), depending on the char-type. 0325 typedef typename detail::startup_info<Char>::type startup_info_t; 0326 ///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. 0327 typedef typename detail::startup_info_ex<Char>::type startup_info_ex_t; 0328 ///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. 0329 void set_startup_info_ex(); 0330 ///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. 0331 startup_info_t startup_info; 0332 ///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. 0333 startup_info_ex_t startup_info_ex; 0334 }; 0335 0336 0337 0338 #endif 0339 0340 } 0341 } 0342 } 0343 0344 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |