Documentation
¶
Overview ¶
Package proctree runs shell and exec commands in an isolated process group or tree, streams stdout/stderr, and kills the full child tree on context cancellation or timeout.
Introspection helpers (Inspect, Children, Descendants) read OS process tables directly. See README "Non-goals" for features intentionally out of scope (PTY, pipelines).
Platform notes:
- Linux: Setpgid isolation; KillTreeByPID sends SIGKILL to -pid; inspect via /proc.
- macOS: Setpgid isolation; KillTreeByPID signals the group and leader pid; inspect via sysctl (KinfoProc, procargs2); Alive skips zombies.
- Windows: new process group plus kill-on-close Job Object on Run; tree kill via TerminateJobObject with descendant-walk fallback; inspect via Toolhelp and NT APIs.
- VerifyOwnership and ReconcilePID fail closed when cmdline or create-time cannot be confirmed.
Index ¶
- Variables
- func Alive(pid int) bool
- func Children(pid int) ([]int, error)
- func Cmdline(pid int) ([]string, error)
- func CreateTime(pid int) (time.Time, error)
- func Descendants(root int) ([]int, error)
- func KillTree(cmd *exec.Cmd)
- func KillTreeByPID(pid int) error
- func NewCommand(spec *Spec) *exec.Cmd
- func NewCommandContext(ctx context.Context, spec *Spec) *exec.Cmd
- func Start(cmd *exec.Cmd) error
- func TeeLine(w io.Writer, fn func(line string)) func(string)
- func VerifyOwned(pid int, spec *Spec) bool
- func VerifyOwnership(pid int, own *Ownership) bool
- func VerifyOwnershipOpts(pid int, own *Ownership, opts *VerifyOptions) bool
- func WaitNotAlive(pid int, timeout time.Duration) bool
- type CmdlineMatcher
- type Options
- type Ownership
- type ProcessInfo
- type ReconcileOutcome
- type ReconcileResult
- type Result
- type Spec
- type Stream
- type VerifyOptions
Constants ¶
This section is empty.
Variables ¶
var ErrProcessNotFound = errors.New("proctree: process not found")
ErrProcessNotFound indicates pid does not refer to a live process we can read.
Functions ¶
func CreateTime ¶
CreateTime returns the process start time when available.
func Descendants ¶
Descendants returns pid and all descendant pids breadth-first.
func KillTreeByPID ¶
KillTreeByPID sends SIGKILL to the process group rooted at pid.
func NewCommand ¶
NewCommand builds a not-yet-started exec.Cmd with an isolated process group.
func NewCommandContext ¶
NewCommandContext is like NewCommand but binds the command to ctx.
func Start ¶
Start starts cmd. On Windows, assigns a kill-on-close job object when possible so later tree kills can use TerminateJobObject.
func VerifyOwned ¶
VerifyOwned reports whether pid still refers to the process started for spec. Returns false when ownership cannot be confirmed (fail closed).
func VerifyOwnership ¶
VerifyOwnership reports whether pid still matches own using cmdline, optional create-time window, and platform group checks. Fails closed when uncertain.
func VerifyOwnershipOpts ¶
func VerifyOwnershipOpts(pid int, own *Ownership, opts *VerifyOptions) bool
VerifyOwnershipOpts is VerifyOwnership with explicit options.
Types ¶
type CmdlineMatcher ¶
CmdlineMatcher returns whether argv parts belong to the supervised process.
type Options ¶
type Options struct {
OnStdout func(line string)
OnStderr func(line string)
// OnLine receives every stdout/stderr line when set.
OnLine func(stream Stream, line string)
// OnStart is invoked with the child pid immediately after Start succeeds.
OnStart func(pid int)
// Stdin is attached to the child process stdin when non-nil.
Stdin io.Reader
// Stdout receives each stdout line (with newline) when non-nil.
Stdout io.Writer
// Stderr receives each stderr line (with newline) when non-nil.
Stderr io.Writer
}
Options configure streaming and lifecycle hooks for Run.
type Ownership ¶
type Ownership struct {
Spec Spec
StartedAt time.Time
// Match, when set, overrides Spec-based cmdline matching.
Match CmdlineMatcher
}
Ownership describes a supervised process for conservative verification.
type ProcessInfo ¶
type ProcessInfo struct {
PID int
PPID int
PGID int
Name string
Status string
Cmdline []string
CreateTime time.Time
MemoryRSS uint64
OpenFiles int
}
ProcessInfo is a point-in-time snapshot of one process.
func Inspect ¶
func Inspect(pid int) (ProcessInfo, error)
Inspect returns a snapshot of pid. Fails when the process is gone or unreadable.
func InspectTree ¶
func InspectTree(root int) ([]ProcessInfo, error)
InspectTree returns snapshots for pid and all descendants.
type ReconcileOutcome ¶
type ReconcileOutcome int
ReconcileOutcome describes the result of ReconcilePID.
const ( // ReconcileNotRunning indicates pid is absent or not a live supervised process. ReconcileNotRunning ReconcileOutcome = iota // ReconcileKilled indicates pid matched own and was terminated. ReconcileKilled // ReconcileUnverified indicates pid is alive but ownership could not be confirmed. // The process is left running (fail closed). ReconcileUnverified )
type ReconcileResult ¶
type ReconcileResult struct {
Outcome ReconcileOutcome
}
ReconcileResult is the outcome of a recovery reconcile attempt.
func ReconcilePID ¶
func ReconcilePID(pid int, own *Ownership) ReconcileResult
ReconcilePID verifies ownership of pid and kills the process tree when confirmed. When pid is not alive, returns ReconcileNotRunning. When pid is alive but ownership cannot be verified, returns ReconcileUnverified without sending signals (fail closed).
func ReconcilePIDOpts ¶
func ReconcilePIDOpts(pid int, own *Ownership, opts *VerifyOptions) ReconcileResult
ReconcilePIDOpts is ReconcilePID with explicit verification options.
type Result ¶
type Result struct {
PID int
StartedAt time.Time
ExitCode int
// Canceled is true when ctx ended before a natural exit (cancel or deadline).
Canceled bool
// TimedOut is true when ctx ended due to deadline exceeded.
TimedOut bool
}
Result is the outcome of Run after the process exits or is killed.
type Spec ¶
type Spec struct {
// Shell runs command through the platform shell (sh -c / cmd /C).
Shell string
// Path is the executable for argv mode. When set, Shell is ignored.
Path string
Args []string
Dir string
// Env entries are appended to os.Environ() when non-empty.
Env []string
}
Spec describes a command to run. Prefer Path/Args over Shell when possible.
type VerifyOptions ¶
type VerifyOptions struct {
// MaxClockSkew allows the OS create time to be slightly after StartedAt.
MaxClockSkew time.Duration
// MaxStartLead allows the OS create time to be before StartedAt (spawn latency).
MaxStartLead time.Duration
// Match, when set, overrides Ownership.Match and Spec-based cmdline matching.
Match CmdlineMatcher
// SkipProcessGroup skips the Unix pgid-leader check when true.
SkipProcessGroup bool
}
VerifyOptions tune create-time and cmdline checks for VerifyOwnership.
func DefaultVerifyOptions ¶
func DefaultVerifyOptions() VerifyOptions
DefaultVerifyOptions is used by VerifyOwnership.