Prevent shell program from keeping alive after Aminal is closed (#165)

This commit is contained in:
nikitar020 2019-01-24 13:06:49 +00:00 committed by Liam Galvin
parent 2d2f4c95cc
commit fd4fee17d9
2 changed files with 22 additions and 6 deletions

View File

@ -51,6 +51,7 @@ func initialize(fn callback) {
pty.Close() pty.Close()
logger.Fatalf("Failed to start your shell: %s", err) logger.Fatalf("Failed to start your shell: %s", err)
} }
defer guestProcess.Close()
logger.Infof("Creating terminal...") logger.Infof("Creating terminal...")
terminal := terminal.New(pty, logger, conf) terminal := terminal.New(pty, logger, conf)

View File

@ -145,7 +145,7 @@ import (
"errors" "errors"
"syscall" "syscall"
"unicode/utf16" "unicode/utf16"
"fmt" "os"
) )
var procsInitSucceeded = false var procsInitSucceeded = false
@ -158,6 +158,8 @@ func init() {
type winProcess struct { type winProcess struct {
hproc uintptr hproc uintptr
processID uint32 processID uint32
goProcess *os.Process
} }
func createPtyChildProcess(imagePath string, hcon uintptr) (*winProcess, error) { func createPtyChildProcess(imagePath string, hcon uintptr) (*winProcess, error) {
@ -178,21 +180,34 @@ func createPtyChildProcess(imagePath string, hcon uintptr) (*winProcess, error)
return nil, errors.New("Failed to create process: " + imagePath) return nil, errors.New("Failed to create process: " + imagePath)
} }
goProcess, err := os.FindProcess(int(dwProcessID))
if err != nil {
return nil, err
}
return &winProcess{ return &winProcess{
hproc: uintptr(hproc), hproc: uintptr(hproc),
processID: uint32(dwProcessID), processID: uint32(dwProcessID),
goProcess: goProcess,
}, nil }, nil
} }
func (process *winProcess) Wait() error { func (process *winProcess) Wait() error {
rc := uint(C.WaitForSingleObject(C.HANDLE(process.hproc), C.INFINITE)) _, err := process.goProcess.Wait()
if rc == C.WAIT_OBJECT_0 { if err != nil {
return nil return err
} }
return fmt.Errorf("error 0x%.8X while waiting for external process ID = %d ", uint(C.GetLastError()), process.processID) return nil
} }
func (process *winProcess) Close() error { func (process *winProcess) Close() error {
return syscall.CloseHandle(syscall.Handle(process.hproc)) err := process.goProcess.Kill()
if err != nil {
return err
}
syscall.CloseHandle(syscall.Handle(process.hproc))
return nil
} }