Documentation
¶
Overview ¶
Package ptable takes snapshots of the host's process table: pid, parent, name, process group and terminal foreground group (unix) and start time of every process. Details that cost a system call or more per process — argv, executable path, environment, working directory, open files — are loaded on first use and cached for the snapshot's lifetime, so walking a few process trees stays cheap on hosts with thousands of processes.
Index ¶
- func Lookup(env []string, key string) string
- type Proc
- type Snapshot
- func (s *Snapshot) Argv(pid int) []string
- func (s *Snapshot) Children(pid int) []int
- func (s *Snapshot) Cwd(pid int) string
- func (s *Snapshot) Env(pid int) []string
- func (s *Snapshot) Exe(pid int) string
- func (s *Snapshot) Get(pid int) *Proc
- func (s *Snapshot) Getenv(pid int, key string) string
- func (s *Snapshot) OpenFiles(pid int) []string
- func (s *Snapshot) PIDs() []int
- type Source
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Proc ¶
type Proc struct {
PID, PPID int
// Name is the executable name the OS keeps per process: Linux comm (at
// most 15 bytes, or what the program set as its title), macOS p_comm,
// Windows image name including ".exe".
Name string
// Pgrp is the process group and Tpgid the foreground process group of
// the controlling terminal (0 when unknown, and always on Windows).
Pgrp, Tpgid int
Start time.Time // zero when unknown
}
Proc is one process of a Snapshot.
func (*Proc) Foreground ¶
Foreground reports whether p belongs to the foreground process group of its terminal, i.e. is what the terminal's user is interacting with.
type Snapshot ¶
type Snapshot struct {
// contains filtered or unexported fields
}
Snapshot is the process table at one moment. It is not safe for concurrent use.
func New ¶
New builds a snapshot of procs whose lazy details come from src.
A parent that started after its child is a recycled pid — Windows keeps the parent pid of a process whose parent exited — so such a child counts as orphaned instead of being attached to an unrelated process.
func (*Snapshot) Env ¶
Env returns the environment ("KEY=value" entries) of pid, nil when it cannot be read.
func (*Snapshot) Getenv ¶
Getenv returns the value of key in the environment of pid ("" when unset or unreadable).
type Source ¶
type Source interface {
Argv(pid int) ([]string, error)
Exe(pid int) (string, error)
Env(pid int) ([]string, error)
Cwd(pid int) (string, error)
OpenFiles(pid int) ([]string, error)
}
Source loads the per-process details a Snapshot fetches lazily. Errors mean "unknown" (the process exited, belongs to another user, or the platform cannot tell); errors.ErrUnsupported marks the latter.