Process error handling and process killing
This commit is contained in:
@@ -283,7 +283,7 @@ struct Win32Process {
|
||||
};
|
||||
static_assert(sizeof(Win32Process) < sizeof(Process::platform));
|
||||
|
||||
static String Win32GetPlatformError(String cmd) {
|
||||
static String Win32GetPlatformError(String msg, String cmd) {
|
||||
LPVOID lpMsgBuf;
|
||||
DWORD dw = GetLastError();
|
||||
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
@@ -296,7 +296,7 @@ static String Win32GetPlatformError(String cmd) {
|
||||
}
|
||||
|
||||
// @warning: leak! but we don't care
|
||||
String r = Format(GetSystemAllocator(), "Failed to create process using cmd: %.*s! %s", FmtString(cmd), (char *)lpMsgBuf);
|
||||
String r = Format(GetSystemAllocator(), "%.*s: %.*s! %s", FmtString(msg), FmtString(cmd), (char *)lpMsgBuf);
|
||||
LocalFree(lpMsgBuf);
|
||||
return r;
|
||||
}
|
||||
@@ -312,9 +312,9 @@ static void Win32CloseProcess(Process *process) {
|
||||
*p = {};
|
||||
}
|
||||
|
||||
static void Win32ProcessError(Process *process, String cmd) {
|
||||
static void Win32ProcessError(Process *process, String msg, String cmd) {
|
||||
Win32Process *p = (Win32Process *)process->platform;
|
||||
process->error_message = Win32GetPlatformError(cmd);
|
||||
process->error_message = Win32GetPlatformError(msg, cmd);
|
||||
Win32CloseProcess(process);
|
||||
}
|
||||
|
||||
@@ -336,22 +336,22 @@ Process CreateCommandLineProcess(String command_line, String working_dir) {
|
||||
security_atrb.bInheritHandle = TRUE;
|
||||
|
||||
if (!CreatePipe(&p->child_stdout_read, &p->child_stdout_write, &security_atrb, 0)) {
|
||||
Win32ProcessError(&process, command_line);
|
||||
Win32ProcessError(&process, "Failed to create process at create pipe stage", command_line);
|
||||
return process;
|
||||
}
|
||||
|
||||
if (!SetHandleInformation(p->child_stdout_read, HANDLE_FLAG_INHERIT, 0)) {
|
||||
Win32ProcessError(&process, command_line);
|
||||
Win32ProcessError(&process, "Failed to create process at create pipe stage", command_line);
|
||||
return process;
|
||||
}
|
||||
|
||||
if (!CreatePipe(&p->child_stdin_read, &p->child_stdin_write, &security_atrb, 0)) {
|
||||
Win32ProcessError(&process, command_line);
|
||||
Win32ProcessError(&process, "Failed to create process at create pipe stage", command_line);
|
||||
return process;
|
||||
}
|
||||
|
||||
if (!SetHandleInformation(p->child_stdin_write, HANDLE_FLAG_INHERIT, 0)) {
|
||||
Win32ProcessError(&process, command_line);
|
||||
Win32ProcessError(&process, "Failed to create process at create pipe stage", command_line);
|
||||
return process;
|
||||
}
|
||||
|
||||
@@ -369,7 +369,7 @@ Process CreateCommandLineProcess(String command_line, String working_dir) {
|
||||
|
||||
PROCESS_INFORMATION info = {};
|
||||
if (!CreateProcessW(L"c:\\windows\\system32\\cmd.exe", cmd.data, 0, 0, TRUE, 0, env, cwd.data, &startup, &info)) {
|
||||
Win32ProcessError(&process, command_line);
|
||||
Win32ProcessError(&process, "failed to create process", command_line);
|
||||
return process;
|
||||
}
|
||||
|
||||
@@ -426,6 +426,7 @@ StdoutPollInfo PollStdout(Process *process, char *buffer, int64_t buffer_size) {
|
||||
StdoutPollInfo result = {};
|
||||
bool peek_error = PeekNamedPipe(p->child_stdout_read, NULL, 0, NULL, &bytes_avail, NULL) == 0;
|
||||
if (peek_error) {
|
||||
process->error_message = Win32GetPlatformError("Failed to peek stdout of child process", "PeekNamedPipe");
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -436,6 +437,7 @@ StdoutPollInfo PollStdout(Process *process, char *buffer, int64_t buffer_size) {
|
||||
DWORD bytes_read = 0;
|
||||
bool read_error = ReadFile(p->child_stdout_read, buffer, (DWORD)buffer_size, &bytes_read, 0) == 0;
|
||||
if (read_error) {
|
||||
process->error_message = Win32GetPlatformError("Failed to read the stdout of child process", "ReadFile");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user