mirror of
https://github.com/nezhahq/nezha.git
synced 2025-01-22 20:58:14 -05:00
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package utils
|
|
|
|
import (
|
|
"os"
|
|
"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 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
|
|
}
|
|
|
|
func (g ProcessExitGroup) Dispose() error {
|
|
return windows.CloseHandle(windows.Handle(g))
|
|
}
|
|
|
|
func (g ProcessExitGroup) AddProcess(p *os.Process) error {
|
|
return windows.AssignProcessToJobObject(
|
|
windows.Handle(g),
|
|
windows.Handle((*process)(unsafe.Pointer(p)).Handle))
|
|
}
|