Documentation
¶
Overview ¶
Package process provides Linux child-subreaper controls and wait-any reaping for a dedicated worker supervisor or PID namespace init.
Use os/exec for process startup, os/signal for signal handling, and pidfd for stable identity and signaling of an identified process. Assign one reaper for the lifetime of each child. Wait and TryWait can reap any eligible child of the calling process, including a direct child started through os/exec. Complete any Cmd.Wait calls for startup children before using wait-any and keep other managed commands in a separate process.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DisableChildSubreaper ¶
func DisableChildSubreaper() error
DisableChildSubreaper clears the calling process's child-subreaper flag. Previously adopted children stay with the process. PID namespace init still adopts orphans in its namespace. Repeated calls leave the flag disabled.
func EnableChildSubreaper ¶
func EnableChildSubreaper() error
EnableChildSubreaper makes the calling process eligible to adopt orphaned descendants in its PID namespace. The kernel chooses the nearest living ancestor marked as a subreaper, falling back to namespace init.
The flag is shared by all threads, survives exec, and starts clear in newly created child processes. Repeated calls leave it enabled.
func IsChildSubreaper ¶
IsChildSubreaper reports whether the calling process has its child-subreaper flag set. A PID namespace's PID 1 adopts orphans even when this returns false.
Types ¶
type Exit ¶
type Exit struct {
// PID is the child's numeric ID in the caller's PID namespace. It can be
// reused as soon as the child is reaped.
PID int
// Code is the normal exit code (0 through 255), or -1 for signal termination.
Code int
// Signal is the terminating signal, or zero for normal exit.
Signal syscall.Signal
// CoreDump reports Linux's CLD_DUMPED status. A core file may be unavailable.
CoreDump bool
}
Exit is the termination status of one reaped child.
func TryWait ¶
TryWait reaps one eligible exited child without blocking. If eligible children exist but none has exited, it returns a zero Exit, false, and nil. If no eligible children remain, it returns a zero Exit, false, and ECHILD. The boolean is true only when a child was reaped. Wait's reaping ownership and error rules also apply here.
func Wait ¶
Wait blocks until an eligible child exits and reaps that child. Both normal exit and signal termination return a status and nil error. If there are no eligible children, it returns ECHILD. Errors return a zero Exit. Syscall errors preserve their errno, including EINTR when Linux reports interruption.
Wait can occupy an OS thread while blocked. The caller owns reaping for all eligible children. Do not race Wait with Cmd.Wait, os.Process.Wait, or pidfd.Process.Wait. Keep ptrace supervision in a separate process. Linux can consume a ptrace stop during wait-any. A nonterminal status returns an error matching errors.ErrUnsupported. Children with nonstandard clone exit signals are outside this contract.
Example ¶
package main
import (
"fmt"
"os"
"github.com/alexfalkowski/go-linux/v2/process"
)
func main() { //nolint:testableexamples // Executed in a dedicated subprocess by TestWaitExample.
// Run this in a dedicated supervisor that owns wait status for its children.
if err := process.EnableChildSubreaper(); err != nil {
panic(err)
}
child, err := os.StartProcess("/bin/sh", []string{"sh", "-c", "exit 23"}, &os.ProcAttr{})
if err != nil {
panic(err)
}
defer func() { _ = child.Release() }()
exit, err := process.Wait()
if err != nil {
panic(err)
}
fmt.Println(exit.Code) // 23
}
Output: