proc

package
v0.175.0-rc.521 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

View Source
var ErrNotFound = exec.ErrNotFound

ErrNotFound is the error of a program PATH does not have.

Functions

func Alive added in v0.6.0

func Alive(pid int) bool

Alive reports whether a process with that PID exists.

func AnonKiB

func AnonKiB(pid int) int

AnonKiB returns the anonymous resident memory of pid in KiB: what killing it would give back.

func Background added in v0.38.1

func Background(ctx context.Context) context.Context

Background marks ctx: the commands Command starts under it run at nice Niceness.

func Cgroup added in v0.5.0

func Cgroup(pid int) string

Cgroup returns the cgroup v2 path of pid ("/user.slice/…"), or "".

func Cwd

func Cwd(pid int) string

Cwd returns the working directory of pid.

func Environ

func Environ(pid int) (map[string]string, error)

Environ returns the environment of pid (readable for the caller's own processes).

func Held added in v0.136.0

func Held(pid int) []string

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

func IsBackground(ctx context.Context) bool

IsBackground reports whether ctx is marked by Background.

func Kubeconfig added in v0.149.0

func Kubeconfig(c *Cmd, files []string)

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

func LookPath(file string) (string, error)

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

func LookTool(name string) (string, error)

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

func MaskedCommand(args []string) (string, bool)

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

func Named(name string, uid int) []int

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

func References(pid int) ([]string, error)

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 Started added in v0.15.0

func Started(pid int) (time.Time, error)

Started is when the live process pid started.

func StopIdle

func StopIdle(pid int, started time.Time, empty func() bool) error

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

func Terminate(pids []int, grace time.Duration)

Terminate sends the processes pids SIGTERM, and SIGKILL to those still alive after grace.

func TerminateMatching added in v0.174.4

func TerminateMatching(ps []*Process, grace time.Duration, match func(*Process) bool) []int

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 UID added in v0.5.0

func UID(pid int) int

UID returns the real user id pid runs as, or -1 when it cannot be read.

func WriteBytes added in v0.100.0

func WriteBytes(pid int) (int64, error)

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.

func (*Child) Kill

func (p *Child) Kill() error

Kill ends the process at once.

func (*Child) Release

func (p *Child) Release() error

Release frees what the process holds without waiting for it.

func (*Child) Signal

func (p *Child) Signal(sig os.Signal) error

Signal sends sig to the process.

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

func Command(ctx context.Context, name string, arg ...string) *Cmd

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

func CommandContext(ctx context.Context, name string, arg ...string) *Cmd

CommandContext is exec.CommandContext: the command name with args, killed when ctx is done before it exits.

func (*Cmd) CombinedOutput

func (c *Cmd) CombinedOutput() ([]byte, error)

CombinedOutput runs the command and returns its standard output and standard error together, through Start and Wait as Output does.

func (*Cmd) Output

func (c *Cmd) Output() ([]byte, error)

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.

func (*Cmd) Run

func (c *Cmd) Run() error

Run starts the command and waits for it.

func (*Cmd) Start

func (c *Cmd) Start() error

Start starts the command without waiting for it. A Cancel of the command's own runs with Process set: os/exec may call it from its own goroutine before Start has returned.

func (*Cmd) Wait

func (c *Cmd) Wait() error

Wait waits for the started command to exit. An exit with a status other than 0 is an *ExitError.

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.

func (*ExitError) Error

func (e *ExitError) Error() string

func (*ExitError) Unwrap

func (e *ExitError) Unwrap() error

Unwrap is the operating system's error, an *exec.ExitError.

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.

func (*Process) Cmdline

func (p *Process) Cmdline() string

Cmdline is the argument vector joined by spaces.

func (*Process) Elapsed

func (p *Process) Elapsed(now time.Time) time.Duration

Elapsed is how long the process has run at now.

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) Pid

func (s *ProcessState) Pid() int

Pid is the exited process's id.

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

func UserScan(uid int) (Scan, error)

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

type Table struct {
	ByPID map[int]*Process
	// contains filtered or unexported fields
}

Table is a snapshot of the process table.

func NewTable added in v0.149.0

func NewTable(ps ...*Process) *Table

NewTable is the table of the processes ps, linked by their parents: a process tree built in memory rather than read.

func Read

func Read() (*Table, error)

Read scans /proc.

func ReadAt added in v0.13.1

func ReadAt(root string) (*Table, error)

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) Ancestors

func (t *Table) Ancestors(pid int) []*Process

Ancestors returns the parent chain of pid, nearest first.

func (*Table) AnonKiB added in v0.13.1

func (t *Table) AnonKiB(pid int) int

AnonKiB returns the anonymous resident memory of pid in the table's process tree.

func (*Table) Children

func (t *Table) Children(pid int) []*Process

Children returns the direct children of pid.

func (*Table) Cwd added in v0.13.1

func (t *Table) Cwd(pid int) string

Cwd returns the working directory of pid in the table's process tree.

func (*Table) Descendants

func (t *Table) Descendants(pid int) []*Process

Descendants returns every process below pid, depth first.

func (*Table) Environ added in v0.13.1

func (t *Table) Environ(pid int) (map[string]string, error)

Environ returns the environment of pid in the table's process tree.

func (*Table) MissingReason

func (t *Table) MissingReason(pid int) string

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.

func (*Table) Orphaned added in v0.156.0

func (t *Table) Orphaned(p *Process) bool

Orphaned reports whether p's parent is gone: the kernel reparented it to PID 1 or to the user's service manager, the subreaper of its units.

func (*Table) Process added in v0.156.0

func (t *Table) Process(pid int) *Process

Process is the process pid of the table, nil when the table has none or is nil.

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

type UnknownProcess struct {
	PID     int
	Command string
	Err     error
}

UnknownProcess is a process whose references could not be read.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL