Documentation
¶
Overview ¶
Package proc reads the Linux process table from /proc once and answers the questions beekeeper asks of it: who is whose parent, which session a process belongs to, how long it has run. Reading /proc directly instead of grepping `ps` output means a pattern never matches the command that looks for it.
Index ¶
- Constants
- Variables
- func Alive(pid int) bool
- func AnonKiB(pid int) int
- func Background(ctx context.Context) context.Context
- func Cgroup(pid int) string
- func Cwd(pid int) string
- func Environ(pid int) (map[string]string, error)
- func Held(pid int) []string
- func IsBackground(ctx context.Context) bool
- func Kubeconfig(c *Cmd, files []string)
- func LookPath(file string) (string, error)
- func LookTool(name string) (string, error)
- func MaskedCommand(args []string) (string, bool)
- func Named(name string, uid int) []int
- func References(pid int) ([]string, error)
- func Started(pid int) (time.Time, error)
- func StopIdle(pid int, started time.Time, empty func() bool) error
- func Terminate(pids []int, grace time.Duration)
- func TerminateMatching(ps []*Process, grace time.Duration, match func(*Process) bool) []int
- func UID(pid int) int
- func WriteBytes(pid int) (int64, error)
- type Child
- type Cmd
- type ExitError
- type Process
- type ProcessState
- func (s *ProcessState) ExitCode() int
- func (s *ProcessState) Exited() bool
- func (s *ProcessState) Pid() int
- func (s *ProcessState) String() string
- func (s *ProcessState) Success() bool
- func (s *ProcessState) Sys() any
- func (s *ProcessState) SysUsage() any
- func (s *ProcessState) SystemTime() time.Duration
- func (s *ProcessState) UserTime() time.Duration
- type Scan
- type Table
- func (t *Table) Ancestors(pid int) []*Process
- func (t *Table) AnonKiB(pid int) int
- func (t *Table) Children(pid int) []*Process
- func (t *Table) Cwd(pid int) string
- func (t *Table) Descendants(pid int) []*Process
- func (t *Table) Environ(pid int) (map[string]string, error)
- func (t *Table) MissingReason(pid int) string
- func (t *Table) Orphaned(p *Process) bool
- func (t *Table) Process(pid int) *Process
- type UnknownError
- type UnknownProcess
Constants ¶
const Niceness = "10"
Niceness is the nice level of a command started under a Background context: behind every interactive process on a starved machine.
Variables ¶
var ErrNotFound = exec.ErrNotFound
ErrNotFound is the error of a program PATH does not have.
Functions ¶
func AnonKiB ¶
AnonKiB returns the anonymous resident memory of pid in KiB: what killing it would give back.
func Background ¶ added in v0.38.1
Background marks ctx: the commands Command starts under it run at nice Niceness.
func Held ¶ added in v0.136.0
Held returns the paths pid holds: its working directory, its executable and every file it has open, a deleted one by the path it had (readable for the caller's own processes). An ambiguous deletion suffix retains both possible paths.
func IsBackground ¶ added in v0.38.1
IsBackground reports whether ctx is marked by Background.
func Kubeconfig ¶ added in v0.149.0
Kubeconfig makes c read the kubeconfig files as kubectl merges a KUBECONFIG list; none, c keeps the environment's.
func LookPath ¶ added in v0.108.1
LookPath is exec.LookPath: the path of the program file, searched in the directories of PATH for a bare name. LookTool also searches the Go tool directories.
func LookTool ¶
LookTool is exec.LookPath, then the Go tool directories for a bare name PATH lacks: beside the running binary, $GOBIN, every $GOPATH/bin, ~/go/bin and ~/.go/bin, in that order. A systemd user unit, the sandbox broker for one, runs with the service manager's PATH, the distribution's default without the directory go install writes to, where kind and the other Go CLIs live beside beekeeper. A tool found nowhere fails naming the directories looked in; a name with a path separator is exec's alone.
func MaskedCommand ¶
MaskedCommand formats a process command line without flag values, URL credentials or key=value words. The result says whether values were omitted.
func Named ¶ added in v0.101.0
Named returns the PIDs of the live processes whose command name (comm) is name and which run as uid.
func References ¶ added in v0.171.0
References reads a process's files, mappings and temporary/build directory environment. An unreadable live process is an error: a pruner must keep candidates when it cannot establish whether that process uses them.
func StopIdle ¶
StopIdle stops exactly the process identified by pid and started. It freezes that process before checking every thread for children and rechecking the caller's lifecycle evidence. This closes both PID-reuse and concurrent-fork races; an active or unreadable process is resumed and left alone.
func Terminate ¶ added in v0.149.0
Terminate sends the processes pids SIGTERM, and SIGKILL to those still alive after grace.
func TerminateMatching ¶ added in v0.174.4
TerminateMatching pins a stable process handle before checking the snapshot's start time and match. It rechecks both before escalation and signals only the pinned handle. If a stable handle is unavailable, it fails closed. It returns the PIDs successfully sent TERM.
func WriteBytes ¶ added in v0.100.0
WriteBytes is what the live process pid caused to be written to storage (write_bytes of /proc/<pid>/io); another user's process is unreadable.
Types ¶
type Child ¶
type Child struct {
Pid int
// contains filtered or unexported fields
}
Child is a process a Cmd started.
type Cmd ¶
type Cmd struct {
*exec.Cmd
// Process is the started process; nil before Start, and for a Cancel
// already when it runs.
Process *Child
// ProcessState is the exited process's state; nil before Wait.
ProcessState *ProcessState
// contains filtered or unexported fields
}
Cmd is a process beekeeper starts. It is configured as exec.Cmd is (Path, Args, Env, Dir, the standard streams and their pipes, ExtraFiles, SysProcAttr, Cancel, WaitDelay), but the process it starts and the state that process exits with are this package's types, and so are the errors of an exit. Every process the core starts goes through this type, so that one seam can answer them all: a release build answers with os/exec and nothing else, and the behavioral scenarios will answer in their test process (docs/plans/3788-behavioral-scenarios.md, the process seam).
func Command ¶ added in v0.38.1
Command is CommandContext for a read the caller can do without: under a Background context it runs through nice (when the machine has it), and a command killed by ctx gives up its pipes after waitDelay.
func CommandContext ¶
CommandContext is exec.CommandContext: the command name with args, killed when ctx is done before it exits.
func (*Cmd) CombinedOutput ¶
CombinedOutput runs the command and returns its standard output and standard error together, through Start and Wait as Output does.
func (*Cmd) Output ¶
Output runs the command and returns its standard output. Without a Stderr of its own, an *ExitError carries what it wrote there: the first and the last 32 KiB, as os/exec keeps them. It runs through Start and Wait, so Process is set while the command runs, for a Cancel that signals it.
type ExitError ¶
type ExitError struct {
*ProcessState
// Stderr is what the command wrote on standard error, for Output
// without a Stderr of its own.
Stderr []byte
// contains filtered or unexported fields
}
ExitError is a command that exited with a status other than 0.
type Process ¶
type Process struct {
PID int
PPID int
Comm string
Args []string
Start time.Time
// StartTicks is Start as the kernel counts it: clock ticks since boot.
StartTicks int64
// CPU is the processor time it has burned (user and system).
CPU time.Duration
// RSSKiB is its resident memory, file-backed pages included.
RSSKiB int
}
Process is one entry of the table.
type ProcessState ¶
type ProcessState struct {
// contains filtered or unexported fields
}
ProcessState is how a process exited.
func (*ProcessState) ExitCode ¶
func (s *ProcessState) ExitCode() int
ExitCode is the exit code, or -1 for a process a signal ended or one that has not exited.
func (*ProcessState) Exited ¶
func (s *ProcessState) Exited() bool
Exited says whether the process exited by itself.
func (*ProcessState) String ¶
func (s *ProcessState) String() string
String describes the exit as os.ProcessState does ("exit status 1").
func (*ProcessState) Success ¶
func (s *ProcessState) Success() bool
Success says whether the process exited with status 0.
func (*ProcessState) Sys ¶
func (s *ProcessState) Sys() any
Sys is the system's exit information, a syscall.WaitStatus on Unix.
func (*ProcessState) SysUsage ¶
func (s *ProcessState) SysUsage() any
SysUsage is the system's resource usage, a *syscall.Rusage on Unix.
func (*ProcessState) SystemTime ¶
func (s *ProcessState) SystemTime() time.Duration
SystemTime is the CPU time the process spent in the system.
func (*ProcessState) UserTime ¶
func (s *ProcessState) UserTime() time.Duration
UserTime is the CPU time the process spent on its own.
type Scan ¶
type Scan struct {
Refs []string
Commands []string
// Foreign lists the user's-uid processes the kernel refuses to read
// that run in another pid or user namespace (a container's, a kind
// node's pause container for one). Their paths name the container's
// filesystem, not the person's, so they are skipped, not unknown.
Foreign []UnknownProcess
}
Scan is one read of the user's processes' references.
func UserScan ¶
UserScan scans every numeric process entry, including entries the general process table cannot read. Unknown ownership fails closed. Kernel EACCES on helpers protected by dumpability, credentials or capabilities is expected; other errors on live user processes remain failures.
A process whose references cannot be read does not end the scan: the readable processes' references are returned with an *UnknownError naming the unreadable ones, so a caller can keep what they could reference while still deciding with the rest. An error on the process table itself is returned alone. A process the kernel refuses in another namespace is skipped into Scan.Foreign.
type Table ¶
Table is a snapshot of the process table.
func NewTable ¶ added in v0.149.0
NewTable is the table of the processes ps, linked by their parents: a process tree built in memory rather than read.
func ReadAt ¶ added in v0.13.1
ReadAt scans the process tree under root: /proc, or a copy of it (each process's stat, cmdline and environ, and the btime line of stat) captured into testdata.
func (*Table) AnonKiB ¶ added in v0.13.1
AnonKiB returns the anonymous resident memory of pid in the table's process tree.
func (*Table) Cwd ¶ added in v0.13.1
Cwd returns the working directory of pid in the table's process tree.
func (*Table) Descendants ¶
Descendants returns every process below pid, depth first.
func (*Table) Environ ¶ added in v0.13.1
Environ returns the environment of pid in the table's process tree.
func (*Table) MissingReason ¶
MissingReason explains why pid is absent from this snapshot. A process hidden by the caller's PID namespace or procfs mount cannot be distinguished from one that has exited.
type UnknownError ¶ added in v0.172.0
type UnknownError struct{ Processes []UnknownProcess }
UnknownError lists the processes whose references are unknown.
func (*UnknownError) Error ¶ added in v0.172.0
func (e *UnknownError) Error() string
func (*UnknownError) Unwrap ¶ added in v0.172.0
func (e *UnknownError) Unwrap() []error
type UnknownProcess ¶ added in v0.172.0
UnknownProcess is a process whose references could not be read.