82 lines
1.5 KiB
Go
82 lines
1.5 KiB
Go
//go:build !windows
|
|
|
|
package plugin
|
|
|
|
import (
|
|
"sync"
|
|
"syscall"
|
|
)
|
|
|
|
// vmJob approximates the Windows per-VM job with a process group: it tracks
|
|
// the root process and the group it leads. Reims-Go runs VMs on Windows; this
|
|
// keeps the package compiling and testable elsewhere.
|
|
type vmJob struct {
|
|
mu sync.Mutex
|
|
pgid int
|
|
done chan struct{}
|
|
doneOnce sync.Once
|
|
}
|
|
|
|
func newVMJob() (*vmJob, error) { return &vmJob{done: make(chan struct{})}, nil }
|
|
|
|
func (j *vmJob) markDone() { j.doneOnce.Do(func() { close(j.done) }) }
|
|
|
|
// Done is closed once the root process has exited.
|
|
func (j *vmJob) Done() <-chan struct{} { return j.done }
|
|
|
|
// RootExited closes Done: without a job object the root is the whole tree.
|
|
func (j *vmJob) RootExited() {
|
|
if j != nil {
|
|
j.markDone()
|
|
}
|
|
}
|
|
|
|
func (j *vmJob) Assign(pid int) error {
|
|
if j == nil || pid <= 0 {
|
|
return nil
|
|
}
|
|
j.mu.Lock()
|
|
j.pgid = pid
|
|
j.mu.Unlock()
|
|
return nil
|
|
}
|
|
|
|
func (j *vmJob) PIDs() []uint32 {
|
|
if j == nil {
|
|
return nil
|
|
}
|
|
j.mu.Lock()
|
|
defer j.mu.Unlock()
|
|
select {
|
|
case <-j.done:
|
|
return nil
|
|
default:
|
|
}
|
|
if j.pgid <= 0 {
|
|
return nil
|
|
}
|
|
return []uint32{uint32(j.pgid)}
|
|
}
|
|
|
|
// SetAffinity is a no-op off Windows.
|
|
func (j *vmJob) SetAffinity(uint64) error { return nil }
|
|
|
|
func (j *vmJob) Terminate() error {
|
|
if j == nil {
|
|
return nil
|
|
}
|
|
j.mu.Lock()
|
|
pgid := j.pgid
|
|
j.mu.Unlock()
|
|
if pgid > 0 {
|
|
return syscall.Kill(-pgid, syscall.SIGKILL)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (j *vmJob) Close() {
|
|
if j != nil {
|
|
j.markDone()
|
|
}
|
|
}
|