mirror of
https://github.com/Twigpine/zero.git
synced 2026-11-11 18:00:54 -10:00
Using unqualified taskkill / taskkill.exe commands on Windows can allow binary hijacking under certain conditions. Update all process termination callers on Windows to resolve the absolute path to taskkill.exe under System32 (referencing SystemRoot/windir environment variables, falling back to C:\Windows).
39 lines
889 B
Go
39 lines
889 B
Go
//go:build windows
|
|
|
|
package background
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
)
|
|
|
|
// ConfigureChildProcessGroup is a no-op on Windows: terminateProcess uses
|
|
// `taskkill /T` to kill the whole process tree, so no launch-time process-group
|
|
// setup is required (the POSIX build sets Setpgid here instead).
|
|
func ConfigureChildProcessGroup(cmd *exec.Cmd) {}
|
|
|
|
func terminateProcess(pid int) error {
|
|
taskkill := taskkillPath()
|
|
if err := exec.Command(taskkill, "/T", "/F", "/PID", strconv.Itoa(pid)).Run(); err == nil {
|
|
return nil
|
|
}
|
|
process, err := os.FindProcess(pid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return process.Kill()
|
|
}
|
|
|
|
func taskkillPath() string {
|
|
systemRoot := os.Getenv("SystemRoot")
|
|
if systemRoot == "" {
|
|
systemRoot = os.Getenv("windir")
|
|
}
|
|
if systemRoot == "" {
|
|
systemRoot = `C:\Windows`
|
|
}
|
|
return filepath.Join(systemRoot, "System32", "taskkill.exe")
|
|
}
|