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 ¶
- func All() []int
- func Argv(pid int) ([]string, bool)
- func ChildPIDs(parent int) []int
- func ChildrenFromTable(parent int) []int
- func Cmdline(pid int) (string, bool)
- func Cwd(pid int) string
- func Descendants(root int) []int
- func Environ(pid int) ([]string, bool)
- func HasEnvEntry(env []string, entries ...string) bool
- func HoldsSocket(pid int, inode string) bool
- func KillTree(root int, grace, poll time.Duration, sleep func(time.Duration)) int
- func ListeningPorts(pids []int) []int
- func MemAvailableRatio() (float64, bool)
- func NewestChild(parent int) int
- func Parent(pid int) (int, bool)
- func ParseMemAvailableRatio(meminfo string) (float64, bool)
- func Running(pid int) bool
- func SocketInode(client, server netip.AddrPort) (string, bool)
- func StartTime(pid int) (time.Time, bool)
- func State(pid int) (byte, bool)
- func TreeListeningPorts(root int) []int
- func Unreadable(pid int) bool
- func WithGrandchildren(children []int, childrenOf func(int) []int) []int
- type Procs
- func (p Procs) AllPids() []int
- func (p Procs) ArgsOf(pid int) (string, bool)
- func (p Procs) ArgvOf(pid int) ([]string, bool)
- func (p Procs) ChildrenOf(pid int) []int
- func (p Procs) CwdOf(pid int) string
- func (p Procs) EnvironOf(pid int) ([]string, bool)
- func (p Procs) StartTimeOf(pid int) (time.Time, bool)
- func (p Procs) WithGrandchildren(children []int) []int
- type Usage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ChildPIDs ¶
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 ¶
ChildrenFromTable finds a process's children by reading every process's parent, as pgrep -P does.
func Cmdline ¶
Cmdline is a process's command line as ps prints it: arguments joined by spaces, trimmed; false when it cannot be read.
func Descendants ¶
Descendants is every process below root (root itself excluded), level by level: the children, then their children.
func Environ ¶
Environ is a process's environment as KEY=value entries; false when it cannot be read (gone, another user's).
func HasEnvEntry ¶
HasEnvEntry tells whether one of the exact KEY=value entries is in the environment.
func HoldsSocket ¶ added in v0.15.40
HoldsSocket tells whether one of the process's open files is the socket of this inode.
func KillTree ¶
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 ¶
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 ¶
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 ¶
NewestChild is what `pgrep -n -P pid` finds: the most recently started child, or 0.
func ParseMemAvailableRatio ¶
ParseMemAvailableRatio is MemAvailable / MemTotal of a /proc/meminfo text; false when either is missing.
func SocketInode ¶ added in v0.15.40
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 ¶
StartTime is when a process started, to the second as ps -o lstart prints it; false when it cannot be read.
func State ¶
State is a process's state letter from /proc/<pid>/stat (R, S, D, Z…); false when it cannot be read. The stat file is read without the process's memory lock, so it answers even for a process blocked on a hung file system.
func TreeListeningPorts ¶
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 Unreadable ¶
Unreadable: the process exists but a look cannot tell what it runs now — it waits uninterruptibly (D: a read of a hung NFS folder, an exit stuck flushing its files) or, still alive, shows an empty command line (its memory already released on the way out, or not readable). Such a look decides nothing about the agent: "cannot read" is not "not the agent".
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 (Procs) ChildrenOf ¶
ChildrenOf is p.Children, empty when unset.
func (Procs) StartTimeOf ¶
StartTimeOf is p.StartTime, false when unset.
func (Procs) WithGrandchildren ¶
WithGrandchildren is the given children followed by each one's children.