Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // boost/process/v2/windows/default_launcher.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2022 Klemens D. Morgenstern (klemens dot morgenstern at gmx dot net)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_PROCESS_V2_WINDOWS_WITH_TOKEN_LAUNCHER_HPP
0012 #define BOOST_PROCESS_V2_WINDOWS_WITH_TOKEN_LAUNCHER_HPP
0013 
0014 #include <boost/process/v2/windows/default_launcher.hpp>
0015 
0016 BOOST_PROCESS_V2_BEGIN_NAMESPACE
0017 namespace windows
0018 {
0019 
0020 /// A windows launcher using CreateProcessWithToken instead of CreateProcess
0021 struct with_token_launcher : default_launcher
0022 {
0023   HANDLE token;
0024   DWORD logon_flags;
0025   with_token_launcher(HANDLE token = INVALID_HANDLE_VALUE,
0026                       DWORD logon_flags = 0u) : token(token), logon_flags(logon_flags) {}
0027 
0028 
0029   template<typename ExecutionContext, typename Args, typename ... Inits>
0030   auto operator()(ExecutionContext & context,
0031                   const typename std::enable_if<std::is_convertible<
0032                              ExecutionContext&, BOOST_PROCESS_V2_ASIO_NAMESPACE::execution_context&>::value,
0033                              filesystem::path >::type & executable,
0034                   Args && args,
0035                   Inits && ... inits ) -> basic_process<typename ExecutionContext::executor_type>
0036   {
0037       error_code ec;
0038       auto proc =  (*this)(context, ec, executable, std::forward<Args>(args), std::forward<Inits>(inits)...);
0039 
0040       if (ec)
0041           v2::detail::throw_error(ec, "with_token_launcher");
0042 
0043       return proc;
0044   }
0045 
0046 
0047   template<typename ExecutionContext, typename Args, typename ... Inits>
0048   auto operator()(ExecutionContext & context,
0049                      error_code & ec,
0050                      const typename std::enable_if<std::is_convertible<
0051                              ExecutionContext&, BOOST_PROCESS_V2_ASIO_NAMESPACE::execution_context&>::value,
0052                              filesystem::path >::type & executable,
0053                      Args && args,
0054                      Inits && ... inits ) -> basic_process<typename ExecutionContext::executor_type>
0055   {
0056       return (*this)(context.get_executor(), ec, executable, std::forward<Args>(args), std::forward<Inits>(inits)...);
0057   }
0058 
0059   template<typename Executor, typename Args, typename ... Inits>
0060   auto operator()(Executor exec,
0061                      const typename std::enable_if<
0062                              BOOST_PROCESS_V2_ASIO_NAMESPACE::execution::is_executor<Executor>::value ||
0063                              BOOST_PROCESS_V2_ASIO_NAMESPACE::is_executor<Executor>::value,
0064                              filesystem::path >::type & executable,
0065                      Args && args,
0066                      Inits && ... inits ) -> basic_process<Executor>
0067   {
0068       error_code ec;
0069       auto proc =  (*this)(std::move(exec), ec, executable, std::forward<Args>(args), std::forward<Inits>(inits)...);
0070 
0071       if (ec)
0072           detail::throw_error(ec, "with_token_launcher");
0073 
0074       return proc;
0075   }
0076   
0077   template<typename Executor, typename Args, typename ... Inits>
0078   auto operator()(Executor exec,
0079                      error_code & ec,
0080                      const typename std::enable_if<
0081                              BOOST_PROCESS_V2_ASIO_NAMESPACE::execution::is_executor<Executor>::value || 
0082                              BOOST_PROCESS_V2_ASIO_NAMESPACE::is_executor<Executor>::value,
0083                              filesystem::path >::type & executable,
0084                      Args && args,
0085                      Inits && ... inits ) -> basic_process<Executor>
0086   {
0087     auto command_line = this->build_command_line(executable, args);
0088 
0089     ec = detail::on_setup(*this, executable, command_line, inits...);
0090     if (ec)
0091     {
0092       detail::on_error(*this, executable, command_line, ec, inits...);
0093       return basic_process<Executor>(exec);
0094     }
0095     auto ok = ::CreateProcessWithTokenW(
0096         token,
0097         logon_flags,
0098         executable.empty() ? nullptr : executable.c_str(),
0099         command_line.empty() ? nullptr :  &command_line.front(),
0100         creation_flags,
0101         environment,
0102         current_directory.empty() ? nullptr : current_directory.c_str(),
0103         &startup_info.StartupInfo,
0104         &process_information);
0105 
0106 
0107     if (ok == 0)
0108     {
0109       BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
0110       detail::on_error(*this, executable, command_line, ec, inits...);
0111 
0112       if (process_information.hProcess != INVALID_HANDLE_VALUE)
0113         ::CloseHandle(process_information.hProcess);
0114       if (process_information.hThread != INVALID_HANDLE_VALUE)
0115         ::CloseHandle(process_information.hThread);
0116 
0117       return basic_process<Executor>(exec);
0118     } else
0119     {
0120       detail::on_success(*this, executable, command_line, inits...);
0121 
0122       if (process_information.hThread != INVALID_HANDLE_VALUE)
0123         ::CloseHandle(process_information.hThread);
0124 
0125       return basic_process<Executor>(exec,
0126                                      this->process_information.dwProcessId,
0127                                      this->process_information.hProcess);
0128     }
0129   }
0130 };
0131 
0132 }
0133 BOOST_PROCESS_V2_END_NAMESPACE
0134 
0135 #endif //  BOOST_PROCESS_V2_WINDOWS_WITH_TOKEN_LAUNCHER_HPP