Files
Reims-Go/internal/plugin/sidecarproc/job_windows.go
T
千川白浪 9006e6d9a7
CI / verify (push) Canceled after 0s
CI / rolling-release (push) Canceled after 0s
refactor: 抽离 HostAPIService 并将 internal/plugin 拆分为四层无环子包 (#202)
2026-12-05 06:05:41 -07:00

122 lines
3.4 KiB
Go

//go:build windows
package sidecarproc
import (
"errors"
"fmt"
"os/exec"
"sync"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
const (
createNoWindow = 0x08000000
createBreakawayFromJob = 0x01000000
createNewProcessGroup = 0x00000200
)
func ConfigureSidecarCmd(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
CreationFlags: createNoWindow | createNewProcessGroup,
}
}
// ConfigureDetachedVMProcess starts the VM suspended, so it joins its job before
// it can start children; ResumeVMProcess lets it run.
func ConfigureDetachedVMProcess(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
CreationFlags: createNoWindow | createNewProcessGroup | createBreakawayFromJob | windows.CREATE_SUSPENDED,
}
}
// ConfigureNestedVMProcess is the fallback when the host's own job forbids
// breakaway: the VM starts inside that job and moves into its per-VM job.
func ConfigureNestedVMProcess(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
CreationFlags: createNoWindow | createNewProcessGroup | windows.CREATE_SUSPENDED,
}
}
var procNtResumeProcess = windows.NewLazySystemDLL("ntdll.dll").NewProc("NtResumeProcess")
func ResumeVMProcess(pid int) error {
proc, err := windows.OpenProcess(windows.PROCESS_SUSPEND_RESUME, false, uint32(pid))
if err != nil {
return fmt.Errorf("OpenProcess(%d): %w", pid, err)
}
defer windows.CloseHandle(proc)
if status, _, _ := procNtResumeProcess.Call(uintptr(proc)); status != 0 {
return fmt.Errorf("resume process %d: %w", pid, windows.NTStatus(status))
}
return nil
}
// IsBreakawayDenied reports a failed CREATE_BREAKAWAY_FROM_JOB start.
func IsBreakawayDenied(err error) bool {
return errors.Is(err, windows.ERROR_ACCESS_DENIED)
}
type SidecarJob struct {
handle windows.Handle
closeOnce sync.Once
}
// NewSidecarJob creates a Windows Job Object with KILL_ON_JOB_CLOSE so that
// closing the job (or host exit/crash) reclaims the entire Sidecar process tree
// with zero orphan processes (§8). Optional memLimitBytes caps process memory (L2).
func NewSidecarJob(memLimitBytes uintptr) (*SidecarJob, error) {
h, err := windows.CreateJobObject(nil, nil)
if err != nil {
return nil, fmt.Errorf("CreateJobObject: %w", err)
}
info := windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION{}
info.BasicLimitInformation.LimitFlags = windows.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
if memLimitBytes > 0 {
info.BasicLimitInformation.LimitFlags |= windows.JOB_OBJECT_LIMIT_PROCESS_MEMORY
info.ProcessMemoryLimit = memLimitBytes
}
if _, err := windows.SetInformationJobObject(
h,
windows.JobObjectExtendedLimitInformation,
uintptr(unsafe.Pointer(&info)),
uint32(unsafe.Sizeof(info)),
); err != nil {
_ = windows.CloseHandle(h)
return nil, fmt.Errorf("SetInformationJobObject: %w", err)
}
return &SidecarJob{handle: h}, nil
}
func (j *SidecarJob) Assign(pid int) error {
if j == nil || j.handle == 0 || pid <= 0 {
return nil
}
proc, err := windows.OpenProcess(windows.PROCESS_SET_QUOTA|windows.PROCESS_TERMINATE, false, uint32(pid))
if err != nil {
return fmt.Errorf("OpenProcess(%d): %w", pid, err)
}
defer windows.CloseHandle(proc)
return windows.AssignProcessToJobObject(j.handle, proc)
}
func (j *SidecarJob) Close() {
if j == nil {
return
}
j.closeOnce.Do(func() {
if j.handle == 0 {
return
}
_ = windows.TerminateJobObject(j.handle, 1)
_ = windows.CloseHandle(j.handle)
j.handle = 0
})
}