Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:08

0001 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
0002 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
0003 // Copyright (c) 2009 Boris Schaeling
0004 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
0005 // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
0006 // Copyright (c) 2016 Klemens D. Morgenstern
0007 //
0008 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0009 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0010 
0011 #ifndef BOOST_PROCESS_WINDOWS_EXECUTOR_HPP
0012 #define BOOST_PROCESS_WINDOWS_EXECUTOR_HPP
0013 
0014 #include <boost/process/detail/child_decl.hpp>
0015 #include <boost/process/detail/windows/is_running.hpp>
0016 #include <boost/process/detail/traits.hpp>
0017 #include <boost/process/error.hpp>
0018 #include <boost/fusion/algorithm/iteration/for_each.hpp>
0019 #include <boost/winapi/handles.hpp>
0020 #include <boost/winapi/process.hpp>
0021 #include <boost/none.hpp>
0022 #include <system_error>
0023 #include <memory>
0024 #include <atomic>
0025 #include <cstring>
0026 
0027 namespace boost { namespace process {
0028 
0029 namespace detail { namespace windows {
0030 
0031 template<typename CharType> struct startup_info;
0032 #if !defined( BOOST_NO_ANSI_APIS )
0033 
0034 template<> struct startup_info<char>
0035 {
0036     typedef ::boost::winapi::STARTUPINFOA_ type;
0037 };
0038 #endif
0039 
0040 template<> struct startup_info<wchar_t>
0041 {
0042     typedef ::boost::winapi::STARTUPINFOW_ type;
0043 };
0044 
0045 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
0046 
0047 template<typename CharType> struct startup_info_ex;
0048 
0049 #if !defined( BOOST_NO_ANSI_APIS )
0050 template<> struct startup_info_ex<char>
0051 {
0052     typedef ::boost::winapi::STARTUPINFOEXA_ type;
0053 };
0054 #endif
0055 
0056 template<> struct startup_info_ex<wchar_t>
0057 {
0058     typedef ::boost::winapi::STARTUPINFOEXW_ type;
0059 };
0060 
0061 
0062 #endif
0063 
0064 #if ( BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6 )
0065 
0066 template<typename CharT>
0067 struct startup_info_impl
0068 {
0069     ::boost::winapi::DWORD_ creation_flags = 0;
0070 
0071     typedef typename startup_info_ex<CharT>::type startup_info_ex_t;
0072     typedef typename startup_info<CharT>::type    startup_info_t;
0073 
0074     startup_info_ex_t  startup_info_ex
0075             {startup_info_t {sizeof(startup_info_t), nullptr, nullptr, nullptr,
0076                                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr,
0077                                ::boost::winapi::invalid_handle_value,
0078                                ::boost::winapi::invalid_handle_value,
0079                                ::boost::winapi::invalid_handle_value},
0080                 nullptr
0081     };
0082     startup_info_t & startup_info = startup_info_ex.StartupInfo;
0083 
0084     void set_startup_info_ex()
0085     {
0086        startup_info.cb = sizeof(startup_info_ex_t);
0087        creation_flags |= ::boost::winapi::EXTENDED_STARTUPINFO_PRESENT_;
0088     }
0089 };
0090 
0091 
0092 #else
0093 
0094 template<typename CharT>
0095 struct startup_info_impl
0096 {
0097     typedef typename startup_info<CharT>::type    startup_info_t;
0098 
0099     ::boost::winapi::DWORD_ creation_flags = 0;
0100     startup_info_t          startup_info
0101             {sizeof(startup_info_t), nullptr, nullptr, nullptr,
0102              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr,
0103              ::boost::winapi::invalid_handle_value,
0104              ::boost::winapi::invalid_handle_value,
0105              ::boost::winapi::invalid_handle_value};
0106 };
0107 #endif
0108 
0109 
0110 
0111 template<typename Char, typename Sequence>
0112 class executor : public startup_info_impl<Char>
0113 {
0114 
0115     void internal_error_handle(const std::error_code &, const char*, boost::mpl::false_, boost::mpl::true_) {}
0116     void internal_error_handle(const std::error_code &, const char*, boost::mpl::true_,  boost::mpl::true_) {}
0117 
0118     void internal_error_handle(const std::error_code &ec, const char*, boost::mpl::true_,  boost::mpl::false_ )
0119     {
0120         this->_ec = ec;
0121     }
0122     void internal_error_handle(const std::error_code &ec, const char* msg, boost::mpl::false_, boost::mpl::false_ )
0123     {
0124         throw process_error(ec, msg);
0125     }
0126 
0127     struct on_setup_t
0128     {
0129         executor & exec;
0130         on_setup_t(executor & exec) : exec(exec) {};
0131         template<typename T>
0132         void operator()(T & t) const
0133         {
0134             if (!exec.error())
0135                 t.on_setup(exec);
0136         }
0137     };
0138 
0139     struct on_error_t
0140     {
0141         executor & exec;
0142         const std::error_code & error;
0143         on_error_t(executor & exec, const std::error_code & error) : exec(exec), error(error) {};
0144         template<typename T>
0145         void operator()(T & t) const
0146         {
0147             t.on_error(exec, error);
0148         }
0149     };
0150 
0151     struct on_success_t
0152     {
0153         executor & exec;
0154         on_success_t(executor & exec) : exec(exec) {};
0155         template<typename T>
0156         void operator()(T & t) const
0157         {
0158             if (!exec.error())
0159                 t.on_success(exec);
0160         }
0161     };
0162 
0163     typedef typename ::boost::process::detail::has_error_handler<Sequence>::type has_error_handler;
0164     typedef typename ::boost::process::detail::has_ignore_error <Sequence>::type has_ignore_error;
0165 
0166     std::error_code _ec{0, std::system_category()};
0167 
0168 public:
0169 
0170     std::shared_ptr<std::atomic<int>> exit_status = std::make_shared<std::atomic<int>>(still_active);
0171 
0172     executor(Sequence & seq) : seq(seq)
0173     {
0174     }
0175 
0176     child operator()()
0177     {
0178         on_setup_t on_setup_fn(*this);
0179         boost::fusion::for_each(seq, on_setup_fn);
0180 
0181         if (_ec)
0182         {
0183             on_error_t on_error_fn(*this, _ec);
0184             boost::fusion::for_each(seq, on_error_fn);
0185             return child();
0186         }
0187 
0188         //NOTE: The non-cast cmd-line string can only be modified by the wchar_t variant which is currently disabled.
0189         int err_code = ::boost::winapi::create_process(
0190             exe,                                        //       LPCSTR_ lpApplicationName,
0191             const_cast<Char*>(cmd_line),                //       LPSTR_ lpCommandLine,
0192             proc_attrs,                                 //       LPSECURITY_ATTRIBUTES_ lpProcessAttributes,
0193             thread_attrs,                               //       LPSECURITY_ATTRIBUTES_ lpThreadAttributes,
0194             inherit_handles,                            //       INT_ bInheritHandles,
0195             this->creation_flags,                       //       DWORD_ dwCreationFlags,
0196             reinterpret_cast<void*>(const_cast<Char*>(env)),  //     LPVOID_ lpEnvironment,
0197             work_dir,                                   //       LPCSTR_ lpCurrentDirectory,
0198             &this->startup_info,                        //       LPSTARTUPINFOA_ lpStartupInfo,
0199             &proc_info);                                //       LPPROCESS_INFORMATION_ lpProcessInformation)
0200 
0201         child c{child_handle(proc_info), exit_status};
0202 
0203         if (err_code != 0)
0204         {
0205             _ec.clear();
0206             on_success_t on_success_fn(*this);
0207             boost::fusion::for_each(seq, on_success_fn);
0208         }
0209         else
0210             set_error(::boost::process::detail::get_last_error(),
0211                     " CreateProcess failed");
0212 
0213         if ( _ec)
0214         {
0215             on_error_t on_err(*this, _ec);
0216             boost::fusion::for_each(seq, on_err);
0217             return child();
0218         }
0219         else
0220             return c;
0221 
0222     }
0223 
0224     void set_error(const std::error_code & ec, const char* msg = "Unknown Error.")
0225     {
0226         internal_error_handle(ec, msg, has_error_handler(),         has_ignore_error());
0227     }
0228     void set_error(const std::error_code & ec, const std::string msg = "Unknown Error.")
0229     {
0230         internal_error_handle(ec, msg.c_str(), has_error_handler(), has_ignore_error());
0231     }
0232 
0233     const std::error_code& error() const {return _ec;}
0234 
0235     ::boost::winapi::LPSECURITY_ATTRIBUTES_ proc_attrs   = nullptr;
0236     ::boost::winapi::LPSECURITY_ATTRIBUTES_ thread_attrs = nullptr;
0237     ::boost::winapi::BOOL_ inherit_handles = false;
0238     const Char * work_dir = nullptr;
0239     const Char * cmd_line = nullptr;
0240     const Char * exe      = nullptr;
0241     const Char * env      = nullptr;
0242 
0243 
0244     Sequence & seq;
0245     ::boost::winapi::PROCESS_INFORMATION_ proc_info{nullptr, nullptr, 0,0};
0246 };
0247 
0248 
0249 
0250 template<typename Char, typename Tup>
0251 executor<Char, Tup> make_executor(Tup & tup)
0252 {
0253     return executor<Char, Tup>(tup);
0254 }
0255 
0256 
0257 }}}}
0258 
0259 #endif