1 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
2 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
3 // Copyright (c) 2009 Boris Schaeling
4 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
5 // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
6 // Copyright (c) 2016 Klemens D. Morgenstern
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_PROCESS_WINDOWS_EXECUTOR_HPP
12 #define BOOST_PROCESS_WINDOWS_EXECUTOR_HPP
14 #include <boost/process/detail/child_decl.hpp>
15 #include <boost/process/detail/windows/is_running.hpp>
16 #include <boost/process/detail/traits.hpp>
17 #include <boost/process/error.hpp>
18 #include <boost/fusion/algorithm/iteration/for_each.hpp>
19 #include <boost/winapi/handles.hpp>
20 #include <boost/winapi/process.hpp>
21 #include <boost/none.hpp>
22 #include <system_error>
27 namespace boost { namespace process {
29 namespace detail { namespace windows {
31 template<typename CharType> struct startup_info;
32 #if !defined( BOOST_NO_ANSI_APIS )
34 template<> struct startup_info<char>
36 typedef ::boost::winapi::STARTUPINFOA_ type;
40 template<> struct startup_info<wchar_t>
42 typedef ::boost::winapi::STARTUPINFOW_ type;
45 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
47 template<typename CharType> struct startup_info_ex;
49 #if !defined( BOOST_NO_ANSI_APIS )
50 template<> struct startup_info_ex<char>
52 typedef ::boost::winapi::STARTUPINFOEXA_ type;
56 template<> struct startup_info_ex<wchar_t>
58 typedef ::boost::winapi::STARTUPINFOEXW_ type;
64 #if ( BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6 )
66 template<typename CharT>
67 struct startup_info_impl
69 ::boost::winapi::DWORD_ creation_flags = 0;
71 typedef typename startup_info_ex<CharT>::type startup_info_ex_t;
72 typedef typename startup_info<CharT>::type startup_info_t;
74 startup_info_ex_t startup_info_ex
75 {startup_info_t {sizeof(startup_info_t), nullptr, nullptr, nullptr,
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr,
77 ::boost::winapi::invalid_handle_value,
78 ::boost::winapi::invalid_handle_value,
79 ::boost::winapi::invalid_handle_value},
82 startup_info_t & startup_info = startup_info_ex.StartupInfo;
84 void set_startup_info_ex()
86 startup_info.cb = sizeof(startup_info_ex_t);
87 creation_flags |= ::boost::winapi::EXTENDED_STARTUPINFO_PRESENT_;
94 template<typename CharT>
95 struct startup_info_impl
97 typedef typename startup_info<CharT>::type startup_info_t;
99 ::boost::winapi::DWORD_ creation_flags = 0;
100 startup_info_t startup_info
101 {sizeof(startup_info_t), nullptr, nullptr, nullptr,
102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr,
103 ::boost::winapi::invalid_handle_value,
104 ::boost::winapi::invalid_handle_value,
105 ::boost::winapi::invalid_handle_value};
111 template<typename Char, typename Sequence>
112 class executor : public startup_info_impl<Char>
115 void internal_error_handle(const std::error_code &, const char*, boost::mpl::false_, boost::mpl::true_) {}
116 void internal_error_handle(const std::error_code &, const char*, boost::mpl::true_, boost::mpl::true_) {}
118 void internal_error_handle(const std::error_code &ec, const char*, boost::mpl::true_, boost::mpl::false_ )
122 void internal_error_handle(const std::error_code &ec, const char* msg, boost::mpl::false_, boost::mpl::false_ )
124 throw process_error(ec, msg);
130 on_setup_t(executor & exec) : exec(exec) {};
132 void operator()(T & t) const
142 const std::error_code & error;
143 on_error_t(executor & exec, const std::error_code & error) : exec(exec), error(error) {};
145 void operator()(T & t) const
147 t.on_error(exec, error);
154 on_success_t(executor & exec) : exec(exec) {};
156 void operator()(T & t) const
163 typedef typename ::boost::process::detail::has_error_handler<Sequence>::type has_error_handler;
164 typedef typename ::boost::process::detail::has_ignore_error <Sequence>::type has_ignore_error;
166 std::error_code _ec{0, std::system_category()};
170 std::shared_ptr<std::atomic<int>> exit_status = std::make_shared<std::atomic<int>>(still_active);
172 executor(Sequence & seq) : seq(seq)
178 on_setup_t on_setup_fn(*this);
179 boost::fusion::for_each(seq, on_setup_fn);
183 on_error_t on_error_fn(*this, _ec);
184 boost::fusion::for_each(seq, on_error_fn);
188 //NOTE: The non-cast cmd-line string can only be modified by the wchar_t variant which is currently disabled.
189 int err_code = ::boost::winapi::create_process(
190 exe, // LPCSTR_ lpApplicationName,
191 const_cast<Char*>(cmd_line), // LPSTR_ lpCommandLine,
192 proc_attrs, // LPSECURITY_ATTRIBUTES_ lpProcessAttributes,
193 thread_attrs, // LPSECURITY_ATTRIBUTES_ lpThreadAttributes,
194 inherit_handles, // INT_ bInheritHandles,
195 this->creation_flags, // DWORD_ dwCreationFlags,
196 reinterpret_cast<void*>(const_cast<Char*>(env)), // LPVOID_ lpEnvironment,
197 work_dir, // LPCSTR_ lpCurrentDirectory,
198 &this->startup_info, // LPSTARTUPINFOA_ lpStartupInfo,
199 &proc_info); // LPPROCESS_INFORMATION_ lpProcessInformation)
201 child c{child_handle(proc_info), exit_status};
206 on_success_t on_success_fn(*this);
207 boost::fusion::for_each(seq, on_success_fn);
210 set_error(::boost::process::detail::get_last_error(),
211 " CreateProcess failed");
215 on_error_t on_err(*this, _ec);
216 boost::fusion::for_each(seq, on_err);
224 void set_error(const std::error_code & ec, const char* msg = "Unknown Error.")
226 internal_error_handle(ec, msg, has_error_handler(), has_ignore_error());
228 void set_error(const std::error_code & ec, const std::string msg = "Unknown Error.")
230 internal_error_handle(ec, msg.c_str(), has_error_handler(), has_ignore_error());
233 const std::error_code& error() const {return _ec;}
235 ::boost::winapi::LPSECURITY_ATTRIBUTES_ proc_attrs = nullptr;
236 ::boost::winapi::LPSECURITY_ATTRIBUTES_ thread_attrs = nullptr;
237 ::boost::winapi::BOOL_ inherit_handles = false;
238 const Char * work_dir = nullptr;
239 const Char * cmd_line = nullptr;
240 const Char * exe = nullptr;
241 const Char * env = nullptr;
245 ::boost::winapi::PROCESS_INFORMATION_ proc_info{nullptr, nullptr, 0,0};
250 template<typename Char, typename Tup>
251 executor<Char, Tup> make_executor(Tup & tup)
253 return executor<Char, Tup>(tup);