1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
//go:build windows
// +build windows
package xpty
import (
"fmt"
"os"
"os/exec"
"syscall"
"golang.org/x/sys/windows"
)
func (c *ConPty) start(cmd *exec.Cmd) error {
pid, proc, err := c.ConPty.Spawn(cmd.Path, cmd.Args, &syscall.ProcAttr{
Dir: cmd.Dir,
Env: cmd.Env,
Sys: cmd.SysProcAttr,
})
if err != nil {
return err
}
cmd.Process, err = os.FindProcess(pid)
if err != nil {
// If we can't find the process via os.FindProcess, terminate the
// process as that's what we rely on for all further operations on the
// object.
if tErr := windows.TerminateProcess(windows.Handle(proc), 1); tErr != nil {
return fmt.Errorf("failed to terminate process after process not found: %w", tErr)
}
return fmt.Errorf("failed to find process after starting: %w", err)
}
return nil
}
|