proc

package
v0.16.196 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 13 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 process table of this machine: a process's children and descendants, its command line, environment, working folder and start time, the TCP ports a process tree listens on, and memory pressure. Everything is read from /proc; a process that cannot be read (gone, another user's) reads as nothing rather than as an error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v0.16.123

func All() []int

All is every process of the machine, in pid order.

func Argv

func Argv(pid int) ([]string, bool)

Argv is a process's arguments one by one; false when they cannot be read.

func ChildPIDs

func ChildPIDs(parent int) []int

ChildPIDs are a process's children in pid order. They are listed per thread: a process that spawns from a worker thread has them under that thread. Without the per-thread lists (a kernel built without them) the process table is read instead, as pgrep -P does. A process that is gone has none.

func ChildrenFromTable

func ChildrenFromTable(parent int) []int

ChildrenFromTable finds a process's children by reading every process's parent, as pgrep -P does.

func Cmdline

func Cmdline(pid int) (string, bool)

Cmdline is a process's command line as ps prints it: arguments joined by spaces, trimmed; false when it cannot be read.

func Cwd

func Cwd(pid int) string

Cwd is a process's working folder, or "" when it cannot be read.

func Descendants

func Descendants(root int) []int

Descendants is every process below root (root itself excluded), level by level: the children, then their children.

func Environ

func Environ(pid int) ([]string, bool)

Environ is a process's environment as KEY=value entries; false when it cannot be read (gone, another user's).

func HasEnvEntry

func HasEnvEntry(env []string, entries ...string) bool

HasEnvEntry tells whether one of the exact KEY=value entries is in the environment.

func HoldsSocket added in v0.15.40

func HoldsSocket(pid int, inode string) bool

HoldsSocket tells whether one of the process's open files is the socket of this inode.

func KillTree

func KillTree(root int, grace, poll time.Duration, sleep func(time.Duration)) int

KillTree stops every process below root, keeping root itself (a pane's shell) and this process: the tree is taken before the first signal, so a background shell reparented to init when its parent exits is still in it. SIGTERM first; survivors are polled every poll for up to grace, then SIGKILL goes to them and to anything that appeared below root since. It returns how many processes the first tree held. sleep waits between polls (time.Sleep when nil).

func ListeningPorts

func ListeningPorts(pids []int) []int

ListeningPorts are the TCP ports the given processes listen on, ascending and without repeats: what `ss -tlnp` lists for them. Sockets of processes this user cannot look into are not seen, as with ss.

func MemAvailableRatio

func MemAvailableRatio() (float64, bool)

MemAvailableRatio is the share of memory still available: from /proc/meminfo, else from sysinfo(2)'s free and total memory. False only when neither can be read.

func NewestChild

func NewestChild(parent int) int

NewestChild is what `pgrep -n -P pid` finds: the most recently started child, or 0.

func Parent

func Parent(pid int) (int, bool)

Parent is a process's parent pid; false when it cannot be read.

func ParseMemAvailableRatio

func ParseMemAvailableRatio(meminfo string) (float64, bool)

ParseMemAvailableRatio is MemAvailable / MemTotal of a /proc/meminfo text; false when either is missing.

func Running

func Running(pid int) bool

Running is kill(pid, 0): a process another user owns counts as running.

func SocketInode added in v0.15.40

func SocketInode(client, server netip.AddrPort) (string, bool)

SocketInode is the inode of the TCP socket on this machine whose own end is client and whose other end is server (any state but listening): the client's end of a loopback connection a server took at server. The rows are matched on both whole addresses, an IPv4 one also in its v4-mapped form in tcp6. False when no row or more than one matches.

func StartTime

func StartTime(pid int) (time.Time, bool)

StartTime is when a process started, to the second as ps -o lstart prints it; false when it cannot be read.

func TreeListeningPorts

func TreeListeningPorts(root int) []int

TreeListeningPorts are the ports listened on by the processes below root (a pane's shell): a dev server started from the shell, however deep.

func WithGrandchildren

func WithGrandchildren(children []int, childrenOf func(int) []int) []int

WithGrandchildren is the given children followed by each one's own children: one level more, never deeper.

Types

type Procs

type Procs struct {
	Children func(pid int) []int
	// Args is the command line as ps prints it.
	Args func(pid int) (string, bool)
	// Argv is the arguments one by one; when nil they are the space-separated words of Args.
	Argv      func(pid int) ([]string, bool)
	Cwd       func(pid int) string
	Environ   func(pid int) ([]string, bool)
	StartTime func(pid int) (time.Time, bool)
	// All is every process of the machine.
	All func() []int
}

Procs is the process table as the agent detectors read it: the machine's by default, a table of a test's own otherwise. A field left nil reads as "nothing there", never as the machine.

func System

func System() Procs

System reads this machine's /proc.

func (Procs) AllPids added in v0.16.123

func (p Procs) AllPids() []int

AllPids is p.All, empty when unset.

func (Procs) ArgsOf

func (p Procs) ArgsOf(pid int) (string, bool)

ArgsOf is p.Args, false when unset.

func (Procs) ArgvOf

func (p Procs) ArgvOf(pid int) ([]string, bool)

ArgvOf is p.Argv, or the words of p.Args when Argv is unset.

func (Procs) ChildrenOf

func (p Procs) ChildrenOf(pid int) []int

ChildrenOf is p.Children, empty when unset.

func (Procs) CwdOf

func (p Procs) CwdOf(pid int) string

CwdOf is p.Cwd, "" when unset.

func (Procs) EnvironOf

func (p Procs) EnvironOf(pid int) ([]string, bool)

EnvironOf is p.Environ, false when unset.

func (Procs) StartTimeOf

func (p Procs) StartTimeOf(pid int) (time.Time, bool)

StartTimeOf is p.StartTime, false when unset.

func (Procs) WithGrandchildren

func (p Procs) WithGrandchildren(children []int) []int

WithGrandchildren is the given children followed by each one's children.

type Usage added in v0.15.40

type Usage struct {
	PPID int
	Comm string
	CPU  time.Duration
	RSS  int64
}

Usage is what /proc/<pid>/stat says of a process: its parent, its name, the processor time it has used (user and system) and its resident memory.

func UsageOf added in v0.15.40

func UsageOf(pid int) (Usage, bool)

UsageOf reads a process's Usage; false when it is gone or unreadable.

Jump to

Keyboard shortcuts

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