//go:build windows package compose import ( "context" "io" "os/exec" "path/filepath" "strconv" "syscall" "time" "golang.org/x/sys/windows" ) const finalTerminationBound = 2 * time.Second const processTreeTerminationBound = 2 * time.Second const createNewProcessGroup = 0x00000200 var windowsSystemDirectory = windows.GetSystemDirectory func configureProcess(command *exec.Cmd) { command.SysProcAttr = &syscall.SysProcAttr{CreationFlags: createNewProcessGroup} } func terminateProcess(command *exec.Cmd, done <-chan error) error { if command.Process == nil { return nil } return terminateWindowsProcess( done, func() error { return terminateWindowsProcessTree(command.Process.Pid) }, command.Process.Kill, finalTerminationBound, ) } func terminateWindowsProcessTree(pid int) error { ctx, cancel := context.WithTimeout(context.Background(), processTreeTerminationBound) defer cancel() taskkillPath, err := systemTaskkillPath() if err != nil { return ErrProcessReap } command := windowsTreeKillCommand(ctx, taskkillPath, pid) command.Stdout = io.Discard command.Stderr = io.Discard if err := command.Run(); err != nil { return ErrProcessReap } if ctx.Err() != nil { return ErrProcessReap } return nil } // systemTaskkillPath resolves taskkill through Windows' protected system-directory API, never PATH. func systemTaskkillPath() (string, error) { systemDirectory, err := windowsSystemDirectory() if err != nil { return "", ErrProcessReap } systemDirectory = filepath.Clean(systemDirectory) if systemDirectory == "." || !filepath.IsAbs(systemDirectory) { return "", ErrProcessReap } taskkillPath := filepath.Join(systemDirectory, "taskkill.exe") if !filepath.IsAbs(taskkillPath) || filepath.Dir(taskkillPath) != systemDirectory { return "", ErrProcessReap } return taskkillPath, nil } func windowsTreeKillCommand(ctx context.Context, taskkillPath string, pid int) *exec.Cmd { return exec.CommandContext(ctx, taskkillPath, "/PID", strconv.Itoa(pid), "/T", "/F") }