package os
import (
)
const (
pidUnset = 0
pidReleased = -1
)
func ( *Process) () ( *ProcessState, error) {
switch .mode {
case modeHandle:
return .pidfdWait()
case modePID:
return .pidWait()
default:
panic("unreachable")
}
}
func ( *Process) () (*ProcessState, error) {
switch .pidStatus() {
case statusReleased:
return nil, syscall.EINVAL
}
, := .blockUntilWaitable()
if != nil {
return nil,
}
if {
.pidDeactivate(statusDone)
.sigMu.Lock()
.sigMu.Unlock()
}
var (
syscall.WaitStatus
syscall.Rusage
)
, := ignoringEINTR2(func() (int, error) {
return syscall.Wait4(.Pid, &, 0, &)
})
if != nil {
return nil, NewSyscallError("wait", )
}
.pidDeactivate(statusDone)
return &ProcessState{
pid: ,
status: ,
rusage: &,
}, nil
}
func ( *Process) ( Signal) error {
, := .(syscall.Signal)
if ! {
return errors.New("os: unsupported signal type")
}
switch .mode {
case modeHandle:
return .pidfdSendSignal()
case modePID:
return .pidSignal()
default:
panic("unreachable")
}
}
func ( *Process) ( syscall.Signal) error {
if .Pid == pidReleased {
return errors.New("os: process already released")
}
if .Pid == pidUnset {
return errors.New("os: process not initialized")
}
.sigMu.RLock()
defer .sigMu.RUnlock()
switch .pidStatus() {
case statusDone:
return ErrProcessDone
case statusReleased:
return errors.New("os: process already released")
}
return convertESRCH(syscall.Kill(.Pid, ))
}
func ( error) error {
if == syscall.ESRCH {
return ErrProcessDone
}
return
}
func ( *Process) () error {
.Pid = pidReleased
switch .mode {
case modeHandle:
.handlePersistentRelease(statusReleased)
case modePID:
.pidDeactivate(statusReleased)
}
runtime.SetFinalizer(, nil)
return nil
}
func ( int) ( *Process, error) {
, := pidfdFind()
if == ErrProcessDone {
return newDoneProcess(), nil
} else if != nil {
return newPIDProcess(), nil
}
return newHandleProcess(, ), nil
}
func ( *ProcessState) () time.Duration {
return time.Duration(.rusage.Utime.Nano()) * time.Nanosecond
}
func ( *ProcessState) () time.Duration {
return time.Duration(.rusage.Stime.Nano()) * time.Nanosecond
}