aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/process.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/zencore/process.cpp')
-rw-r--r--src/zencore/process.cpp61
1 files changed, 59 insertions, 2 deletions
diff --git a/src/zencore/process.cpp b/src/zencore/process.cpp
index 1c208701c..2d0ec2de6 100644
--- a/src/zencore/process.cpp
+++ b/src/zencore/process.cpp
@@ -185,6 +185,11 @@ ProcessHandle::Wait(int TimeoutMs)
int WaitState = 0;
waitpid(m_Pid, &WaitState, WNOHANG | WCONTINUED | WUNTRACED);
+ if (WIFEXITED(WaitState))
+ {
+ m_ExitCode = WEXITSTATUS(WaitState);
+ }
+
if (kill(m_Pid, 0) < 0)
{
int32_t LastError = zen::GetLastError();
@@ -220,6 +225,8 @@ ProcessHandle::WaitExitCode()
ZEN_ASSERT(ExitCode != STILL_ACTIVE);
return ExitCode;
+#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
+ return m_ExitCode;
#else
ZEN_NOT_IMPLEMENTED();
@@ -278,7 +285,7 @@ CreateProcNormal(const std::filesystem::path& Executable, std::string_view Comma
PROCESS_INFORMATION ProcessInfo{};
STARTUPINFO StartupInfo{.cb = sizeof(STARTUPINFO)};
- const bool InheritHandles = false;
+ bool InheritHandles = false;
void* Environment = nullptr;
LPSECURITY_ATTRIBUTES ProcessAttributes = nullptr;
LPSECURITY_ATTRIBUTES ThreadAttributes = nullptr;
@@ -298,6 +305,42 @@ CreateProcNormal(const std::filesystem::path& Executable, std::string_view Comma
ExtendableWideStringBuilder<256> CommandLineZ;
CommandLineZ << CommandLine;
+ if (!Options.StdoutFile.empty())
+ {
+ SECURITY_ATTRIBUTES sa;
+ sa.nLength = sizeof sa;
+ sa.lpSecurityDescriptor = nullptr;
+ sa.bInheritHandle = TRUE;
+
+ StartupInfo.hStdInput = nullptr;
+ StartupInfo.hStdOutput = CreateFileW(Options.StdoutFile.c_str(),
+ GENERIC_READ | GENERIC_WRITE,
+ FILE_SHARE_READ,
+ &sa,
+ CREATE_ALWAYS,
+ FILE_ATTRIBUTE_NORMAL,
+ nullptr);
+
+ const BOOL Success = DuplicateHandle(GetCurrentProcess(),
+ StartupInfo.hStdOutput,
+ GetCurrentProcess(),
+ &StartupInfo.hStdError,
+ 0,
+ TRUE,
+ DUPLICATE_SAME_ACCESS);
+
+ if (Success)
+ {
+ StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
+ InheritHandles = true;
+ }
+ else
+ {
+ CloseHandle(StartupInfo.hStdOutput);
+ StartupInfo.hStdOutput = 0;
+ }
+ }
+
BOOL Success = CreateProcessW(Executable.c_str(),
CommandLineZ.Data(),
ProcessAttributes,
@@ -309,6 +352,12 @@ CreateProcNormal(const std::filesystem::path& Executable, std::string_view Comma
&StartupInfo,
&ProcessInfo);
+ if (StartupInfo.dwFlags & STARTF_USESTDHANDLES)
+ {
+ CloseHandle(StartupInfo.hStdError);
+ CloseHandle(StartupInfo.hStdOutput);
+ }
+
if (!Success)
{
return nullptr;
@@ -395,6 +444,14 @@ CreateProcUnelevated(const std::filesystem::path& Executable, std::string_view C
ExtendableWideStringBuilder<256> CommandLineZ;
CommandLineZ << CommandLine;
+ ExtendableWideStringBuilder<256> CurrentDirZ;
+ LPCWSTR WorkingDirectoryPtr = nullptr;
+ if (Options.WorkingDirectory)
+ {
+ CurrentDirZ << Options.WorkingDirectory->native();
+ WorkingDirectoryPtr = CurrentDirZ.c_str();
+ }
+
bOk = CreateProcessW(Executable.c_str(),
CommandLineZ.Data(),
nullptr,
@@ -402,7 +459,7 @@ CreateProcUnelevated(const std::filesystem::path& Executable, std::string_view C
FALSE,
CreateProcFlags,
nullptr,
- nullptr,
+ WorkingDirectoryPtr,
&StartupInfo.StartupInfo,
&ProcessInfo);
if (bOk == FALSE)