Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2 uses PROC_THREAD_ATTRIBUTE_HANDLE_LIST for limiting fd #423

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/boost/process/v2/stdio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,14 @@ struct process_stdio
#if defined(BOOST_PROCESS_V2_WINDOWS)
error_code on_setup(windows::default_launcher & launcher, const filesystem::path &, const std::wstring &)
{

launcher.startup_info.StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
launcher.startup_info.StartupInfo.hStdInput = in.prepare();
launcher.startup_info.StartupInfo.hStdOutput = out.prepare();
launcher.startup_info.StartupInfo.hStdError = err.prepare();
launcher.inherit_handles = true;
launcher.inherited_handles.reserve(launcher.inherited_handles.size() + 3);
launcher.inherited_handles.push_back(launcher.startup_info.StartupInfo.hStdInput);
launcher.inherited_handles.push_back(launcher.startup_info.StartupInfo.hStdOutput);
launcher.inherited_handles.push_back(launcher.startup_info.StartupInfo.hStdError);
return error_code {};
};
#else
Expand Down
10 changes: 9 additions & 1 deletion include/boost/process/v2/windows/as_user_launcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,21 @@ struct as_user_launcher : default_launcher
detail::on_error(*this, executable, command_line, ec, inits...);
return basic_process<Executor>(exec);
}

if (!inherited_handles.empty())
{
set_handle_list(ec);
if (ec)
return basic_process<Executor>(exec);
}

auto ok = ::CreateProcessAsUserW(
token,
executable.empty() ? nullptr : executable.c_str(),
command_line.empty() ? nullptr : &command_line.front(),
process_attributes,
thread_attributes,
inherit_handles ? TRUE : FALSE,
inherited_handles.empty() ? FALSE : TRUE,
creation_flags,
environment,
current_directory.empty() ? nullptr : current_directory.c_str(),
Expand Down
28 changes: 25 additions & 3 deletions include/boost/process/v2/windows/default_launcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <boost/process/v2/error.hpp>

#include <numeric>
#include <memory>
#include <type_traits>
#include <windows.h>

#if defined(BOOST_PROCESS_V2_STANDALONE)
Expand Down Expand Up @@ -207,8 +209,8 @@ struct default_launcher
SECURITY_ATTRIBUTES * process_attributes = nullptr;
//// The thread_attributes passed to CreateProcess
SECURITY_ATTRIBUTES * thread_attributes = nullptr;
/// The bInheritHandles option. Needs to be set to true by any initializers using handles.
bool inherit_handles = false;
/// The inhreited_handles option. bInheritHandles will be true if not empty..
std::vector<HANDLE> inherited_handles;
/// The creation flags of the process. Initializers may add to them; extended startupinfo is assumed.
DWORD creation_flags{EXTENDED_STARTUPINFO_PRESENT};
/// A pointer to the subprocess environment.
Expand Down Expand Up @@ -294,18 +296,26 @@ struct default_launcher
auto command_line = this->build_command_line(executable, std::forward<Args>(args));

ec = detail::on_setup(*this, executable, command_line, inits...);

if (ec)
{
detail::on_error(*this, executable, command_line, ec, inits...);
return basic_process<Executor>(exec);
}

if (!inherited_handles.empty())
{
set_handle_list(ec);
if (ec)
return basic_process<Executor>(exec);
}

auto ok = ::CreateProcessW(
executable.empty() ? nullptr : executable.c_str(),
command_line.empty() ? nullptr : &command_line.front(),
process_attributes,
thread_attributes,
inherit_handles ? TRUE : FALSE,
inherited_handles.empty() ? FALSE : TRUE,
creation_flags,
environment,
current_directory.empty() ? nullptr : current_directory.c_str(),
Expand Down Expand Up @@ -403,6 +413,18 @@ struct default_launcher
return args;
}

struct lpproc_thread_closer
{
void operator()(::LPPROC_THREAD_ATTRIBUTE_LIST l)
{
::DeleteProcThreadAttributeList(l);
::HeapFree(GetProcessHeap(), 0, l);
}
};
std::unique_ptr<std::remove_pointer<LPPROC_THREAD_ATTRIBUTE_LIST>::type, lpproc_thread_closer> proc_attribute_list_storage;

BOOST_PROCESS_V2_DECL LPPROC_THREAD_ATTRIBUTE_LIST get_thread_attribute_list(error_code & ec);
BOOST_PROCESS_V2_DECL void set_handle_list(error_code & ec);
};


Expand Down
39 changes: 39 additions & 0 deletions src/windows/default_launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,45 @@ namespace windows
return itr - begin;
}

LPPROC_THREAD_ATTRIBUTE_LIST default_launcher::get_thread_attribute_list(error_code & ec)
{
if (startup_info.lpAttributeList != nullptr)
return startup_info.lpAttributeList;
SIZE_T size;
if (!(::InitializeProcThreadAttributeList(NULL, 1, 0, &size) ||
GetLastError() == ERROR_INSUFFICIENT_BUFFER))
{
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec);
return nullptr;
}

LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList = reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(::HeapAlloc(::GetProcessHeap(), 0, size));
if (lpAttributeList == nullptr)
return nullptr;

if (!::InitializeProcThreadAttributeList(lpAttributeList, 1, 0, &size))
{
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec);
::HeapFree(GetProcessHeap(), 0, lpAttributeList);
return nullptr;
}

proc_attribute_list_storage.reset(lpAttributeList);
startup_info.lpAttributeList = proc_attribute_list_storage.get();
return startup_info.lpAttributeList;
}

void default_launcher::set_handle_list(error_code & ec)
{
auto tl = get_thread_attribute_list(ec);
if (ec)
return;
if (!::UpdateProcThreadAttribute(
tl, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
inherited_handles.data(), inherited_handles.size() * sizeof(HANDLE), nullptr, nullptr))
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec);
}

}
BOOST_PROCESS_V2_END_NAMESPACE

Expand Down
Loading