fix(process): kill 兜底弃 taskkill 命令行, 改 Windows API 进程树强杀
- Toolhelp32 快照枚举父子关系→收集后代(防环)→OpenProcess+TerminateProcess 按 PID 定向结束 - 产品红线: 系统操作禁拼 cmd 命令字符串(2026-10-09 用户令, 与 zomaintain AGENTS §一同源) - wire 不变: 仅错误文案 taskkill 段改'进程树终止'
This commit is contained in:
@@ -7,10 +7,8 @@ package process
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/process"
|
||||
|
||||
@@ -85,7 +83,8 @@ func listProcesses(w http.ResponseWriter, _ *http.Request) {
|
||||
}
|
||||
|
||||
// killProcess 结束进程: {pid: 必填, name: 可选校验(防 pid 复用误杀)}。
|
||||
// Windows 上 gopsutil 权限不足时回退 taskkill /F /T(连子进程树强杀)。
|
||||
// Windows 上 gopsutil 权限不足时回退进程树强杀(Windows API: Toolhelp32 快照+TerminateProcess,
|
||||
// 2026-10-09 起替代原 taskkill /F /T 命令行兜底——产品红线禁命令行查杀)。
|
||||
func killProcess(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Pid int32 `json:"pid"`
|
||||
@@ -111,8 +110,8 @@ func killProcess(w http.ResponseWriter, r *http.Request) {
|
||||
fail(w, "结束进程失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
if out, e := exec.Command("taskkill", "/F", "/T", "/PID", strconv.Itoa(int(req.Pid))).CombinedOutput(); e != nil {
|
||||
fail(w, "结束进程失败: "+err.Error()+"; taskkill: "+string(out))
|
||||
if e := killProcessTree(uint32(req.Pid)); e != nil {
|
||||
fail(w, "结束进程失败: "+err.Error()+"; 进程树终止: "+e.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user