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

port class 'process' to Windows #2277

Merged
merged 5 commits into from
Oct 10, 2023
Merged
Changes from 3 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
168 changes: 167 additions & 1 deletion src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,23 @@
#include <migraphx/env.hpp>
#include <functional>
#include <iostream>
#include <optional>

#ifdef _WIN32
// cppcheck-suppress definePrefix
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
#include <unistd.h>
#endif

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_CMD_EXECUTE)

#ifndef _WIN32

std::function<void(const char*)> redirect_to(std::ostream& os)
{
return [&](const char* x) { os << x; };
Expand Down Expand Up @@ -74,6 +84,155 @@
});
}

#else

constexpr std::size_t MIGRAPHX_PROCESS_BUFSIZE = 4096;

class pipe
{
public:
explicit pipe(bool inherit_handle = true)
{
SECURITY_ATTRIBUTES attrs;
attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
attrs.bInheritHandle = inherit_handle ? TRUE : FALSE;
attrs.lpSecurityDescriptor = nullptr;

if(CreatePipe(&m_read, &m_write, &attrs, 0) == FALSE)
throw GetLastError();

if(SetHandleInformation(&m_read, HANDLE_FLAG_INHERIT, 0) == FALSE)
throw GetLastError();
}

pipe(const pipe&) = delete;
pipe& operator=(const pipe&) = delete;

pipe(pipe&&) = default;

~pipe()
{
CloseHandle(m_read);
m_read = nullptr;
CloseHandle(m_write);
m_write = nullptr;
}

std::optional<std::pair<bool, DWORD>> read(LPVOID buffer, DWORD length) const
{
DWORD bytes_read;
if(ReadFile(m_read, buffer, length, &bytes_read, nullptr) == FALSE)
{
DWORD error{GetLastError()};
if(error != ERROR_MORE_DATA)
{
return std::nullopt;
}
return {{true, bytes_read}};
}
return {{false, bytes_read}};
}

HANDLE get_read_handle() const { return m_read; }

bool write(LPCVOID buffer, DWORD length) const
{
DWORD bytes_written;
return WriteFile(m_write, buffer, length, &bytes_written, nullptr) == TRUE;
}

HANDLE get_write_handle() const { return m_write; }

private:
HANDLE m_write = nullptr, m_read = nullptr;
};

template <typename F>
int exec(const std::string& cmd, F f)
{
try
{
if(enabled(MIGRAPHX_TRACE_CMD_EXECUTE{}))
std::cout << cmd << std::endl;

STARTUPINFO info;
PROCESS_INFORMATION process_info;

pipe in{}, out{};

ZeroMemory(&info, sizeof(STARTUPINFO));
info.cb = sizeof(STARTUPINFO);
info.hStdError = out.get_write_handle();
info.hStdOutput = out.get_write_handle();
info.hStdInput = in.get_read_handle();
info.dwFlags |= STARTF_USESTDHANDLES;

ZeroMemory(&process_info, sizeof(process_info));

if(CreateProcess(nullptr,
const_cast<LPSTR>(cmd.c_str()),
nullptr,
nullptr,
TRUE,
0,
nullptr,
nullptr,
&info,
&process_info) == FALSE)
{
return GetLastError();
}

f(in, out);

WaitForSingleObject(process_info.hProcess, INFINITE);

DWORD status{};
GetExitCodeProcess(process_info.hProcess, &status);

CloseHandle(process_info.hProcess);
CloseHandle(process_info.hThread);

return static_cast<int>(status);
}
// cppcheck-suppress catchExceptionByValue
catch(DWORD last_error)
{
return last_error;
}
}

int exec(const std::string& cmd)
{
TCHAR buffer[MIGRAPHX_PROCESS_BUFSIZE];
HANDLE std_out{GetStdHandle(STD_OUTPUT_HANDLE)};
return (std_out == nullptr || std_out == INVALID_HANDLE_VALUE)

Check warning on line 209 in src/process.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

style: Use 'or' instead of || [UseNamedLogicOperator]
? GetLastError()
: exec(cmd, [&](const pipe&, const pipe& out) {
for(;;)
{
if(auto result = out.read(buffer, MIGRAPHX_PROCESS_BUFSIZE))
{
auto [more_data, bytes_read] = *result;
if(!more_data || bytes_read == 0)

Check warning on line 217 in src/process.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

style: Use 'or' instead of || [UseNamedLogicOperator]

Check warning on line 217 in src/process.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

style: Use 'not' instead of ! [UseNamedLogicOperator]
break;
DWORD written;
if(WriteFile(std_out, buffer, bytes_read, &written, nullptr) == FALSE)
break;
}
}
});
}

int exec(const std::string& cmd, std::function<void(process::writer)> std_in)
{
return exec(cmd, [&](const pipe& in, const pipe&) {
std_in([&](const char* buffer, std::size_t n) { in.write(buffer, n); });
});
}

#endif

struct process_impl
{
std::string command{};
Expand Down Expand Up @@ -119,7 +278,14 @@
return *this;
}

void process::exec() { impl->check_exec(impl->get_command(), redirect_to(std::cout)); }
void process::exec()
{
#ifndef _WIN32
impl->check_exec(impl->get_command(), redirect_to(std::cout));
#else
impl->check_exec(impl->get_command());
#endif
}

void process::write(std::function<void(process::writer)> pipe_in)
{
Expand Down
Loading