mirror of
https://github.com/hanzoai/cloud.git
synced 2026-11-17 16:36:24 +00:00
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
//go:build windows
|
|
|
|
package functions
|
|
|
|
import (
|
|
"os/exec"
|
|
"syscall"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
func runIsolated(cmd *exec.Cmd) error {
|
|
job, err := windows.CreateJobObject(nil, nil)
|
|
if err == nil {
|
|
var info windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION
|
|
info.BasicLimitInformation.LimitFlags = windows.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
|
|
_, _ = windows.SetInformationJobObject(
|
|
job,
|
|
windows.JobObjectExtendedLimitInformation,
|
|
uintptr(unsafe.Pointer(&info)),
|
|
uint32(unsafe.Sizeof(info)),
|
|
)
|
|
}
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
|
|
}
|
|
cmd.Cancel = func() error {
|
|
if job != 0 {
|
|
_ = windows.TerminateJobObject(job, 1)
|
|
}
|
|
if cmd.Process != nil {
|
|
return cmd.Process.Kill()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
if job != 0 {
|
|
_ = windows.CloseHandle(job)
|
|
}
|
|
return err
|
|
}
|
|
|
|
if job != 0 {
|
|
if h, oerr := windows.OpenProcess(windows.PROCESS_SET_QUOTA|windows.PROCESS_TERMINATE, false, uint32(cmd.Process.Pid)); oerr == nil {
|
|
_ = windows.AssignProcessToJobObject(job, h)
|
|
_ = windows.CloseHandle(h)
|
|
}
|
|
}
|
|
|
|
waitErr := cmd.Wait()
|
|
if job != 0 {
|
|
_ = windows.TerminateJobObject(job, 1)
|
|
_ = windows.CloseHandle(job)
|
|
}
|
|
return waitErr
|
|
}
|