From a68b693bfded779fd3a86727952594906c9bdf96 Mon Sep 17 00:00:00 2001 From: nikitar020 <42252263+nikitar020@users.noreply.github.com> Date: Tue, 5 Mar 2019 09:00:34 +0200 Subject: [PATCH] Add correct handling of attaching to child console errors (#239) --- platform/winpty.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/platform/winpty.go b/platform/winpty.go index d330745..b792f85 100644 --- a/platform/winpty.go +++ b/platform/winpty.go @@ -7,6 +7,7 @@ import ( "syscall" "time" + "fmt" "github.com/MaxRis/w32" ) @@ -135,15 +136,20 @@ func (pty *winConPty) Close() error { func (pty *winConPty) CreateGuestProcess(imagePath string) (Process, error) { process, err := createPtyChildProcess(imagePath, pty.hcon) + if err != nil { + return nil, err + } - if err == nil { - setupChildConsole(C.DWORD(process.processID), C.STD_OUTPUT_HANDLE, C.ENABLE_PROCESSED_OUTPUT|C.ENABLE_WRAP_AT_EOL_OUTPUT) + err = setupChildConsole(C.DWORD(process.processID), C.STD_OUTPUT_HANDLE, C.ENABLE_PROCESSED_OUTPUT|C.ENABLE_WRAP_AT_EOL_OUTPUT) + if err != nil { + process.Close() + return nil, err } return process, err } -func setupChildConsole(processID C.DWORD, nStdHandle C.DWORD, mode uint) bool { +func setupChildConsole(processID C.DWORD, nStdHandle C.DWORD, mode uint) error { C.FreeConsole() defer C.AttachConsole(^C.DWORD(0)) // attach to parent process console @@ -158,7 +164,7 @@ func setupChildConsole(processID C.DWORD, nStdHandle C.DWORD, mode uint) bool { } lastError := C.GetLastError() if lastError != C.ERROR_GEN_FAILURE || count <= 0 { - return false + return fmt.Errorf("Was not able to attach to the child prosess' console") } time.Sleep(time.Millisecond * time.Duration(waitStepMilliSeconds)) @@ -169,7 +175,7 @@ func setupChildConsole(processID C.DWORD, nStdHandle C.DWORD, mode uint) bool { C.SetConsoleMode(h, C.DWORD(mode)) C.FreeConsole() - return true + return nil } func (pty *winConPty) Resize(x, y int) error {