mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 04:30:05 +00:00
🐛 fix: windows 杀子进程
This commit is contained in:
@@ -15,7 +15,7 @@ func NewProcessExitGroup() (ProcessExitGroup, error) {
|
||||
return ProcessExitGroup{}, nil
|
||||
}
|
||||
|
||||
func (g ProcessExitGroup) Dispose() error {
|
||||
func (g *ProcessExitGroup) Dispose() error {
|
||||
for _, c := range g.cmds {
|
||||
if err := syscall.Kill(-c.Process.Pid, syscall.SIGKILL); err != nil {
|
||||
return err
|
||||
|
||||
@@ -3,49 +3,28 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
// We use this struct to retreive process handle(which is unexported)
|
||||
// from os.Process using unsafe operation.
|
||||
type process struct {
|
||||
Pid int
|
||||
Handle uintptr
|
||||
type ProcessExitGroup struct {
|
||||
cmds []*exec.Cmd
|
||||
}
|
||||
|
||||
type ProcessExitGroup windows.Handle
|
||||
|
||||
func NewProcessExitGroup() (ProcessExitGroup, error) {
|
||||
handle, err := windows.CreateJobObject(nil, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
info := windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION{
|
||||
BasicLimitInformation: windows.JOBOBJECT_BASIC_LIMIT_INFORMATION{
|
||||
LimitFlags: windows.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
|
||||
},
|
||||
}
|
||||
if _, err := windows.SetInformationJobObject(
|
||||
handle,
|
||||
windows.JobObjectExtendedLimitInformation,
|
||||
uintptr(unsafe.Pointer(&info)),
|
||||
uint32(unsafe.Sizeof(info))); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return ProcessExitGroup(handle), nil
|
||||
return ProcessExitGroup{}, nil
|
||||
}
|
||||
|
||||
func (g ProcessExitGroup) Dispose() error {
|
||||
return windows.CloseHandle(windows.Handle(g))
|
||||
func (g *ProcessExitGroup) Dispose() error {
|
||||
for _, c := range g.cmds {
|
||||
if err := exec.Command("taskkill", "/F", "/T", "/PID", fmt.Sprint(c.Process.Pid)).Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g ProcessExitGroup) AddProcess(cmd *exec.Cmd) error {
|
||||
return windows.AssignProcessToJobObject(
|
||||
windows.Handle(g),
|
||||
windows.Handle((*process)(unsafe.Pointer(cmd.Process)).Handle))
|
||||
func (g *ProcessExitGroup) AddProcess(cmd *exec.Cmd) error {
|
||||
g.cmds = append(g.cmds, cmd)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user