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, owner — 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 ¶
- Variables
- func Argv(pid int) ([]string, error)
- func Argv0(argv []string) string
- func BaseName(name string) string
- func CurrentOwner() (string, error)
- func Lookup(env []string, key string) string
- func ReadsTerminal(pid int) (tty, known bool)
- func TTYHosts() map[uint64]string
- func TTYName(dev uint64) 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) Detached(pid int) int
- func (s *Snapshot) Env(pid int) []string
- func (s *Snapshot) EnvWithheld(pid int) bool
- 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) Owner(pid int) string
- func (s *Snapshot) PIDs() []int
- type Source
Constants ¶
This section is empty.
Variables ¶
var ErrEnvWithheld = errors.New("ptable: the OS withholds the environment")
ErrEnvWithheld is Source.Env's error for a process whose command line can be read but whose environment the OS withholds: macOS omits it for restricted processes (CS_RESTRICT — /bin/zsh, /bin/sh, /usr/bin/*, programs signed with entitlements) while System Integrity Protection is on, whoever asks, root included.
Functions ¶
func Argv0 ¶ added in v0.7.0
Argv0 is the BaseName of a command line's program, "" for an empty one.
func BaseName ¶ added in v0.7.0
BaseName normalizes a process name for comparison: lower case, without directory and Windows executable suffix.
func CurrentOwner ¶ added in v0.2.1
CurrentOwner returns the user this process runs as, in the form of Snapshot.Owner: its effective uid.
func Lookup ¶
Lookup returns the value of key in env ("KEY=value" entries; keys compare case-insensitively on Windows, like the OS does). The first entry wins, as with getenv(3).
func ReadsTerminal ¶ added in v0.4.0
ReadsTerminal reports whether pid's stdin is a terminal, by where /proc/<pid>/fd/0 points; known is false when that cannot be read (another user's process, the process exited).
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
// TTY is the device number of the controlling terminal (see TTYName),
// 0 without one, and always on Windows.
TTY uint64
Start time.Time // zero when unknown
}
Proc is one process of a Snapshot.
func Process ¶ added in v0.4.0
Process reads one process without a snapshot: its pid, parent, name, process group and terminal (Start may be zero). ok is false when it cannot be read, and always on Windows.
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) Detached ¶ added in v0.2.1
Detached returns the parent pid New dropped for pid because that process started after pid (the pid was recycled), 0 when pid kept its parent.
func (*Snapshot) Env ¶
Env returns the environment ("KEY=value" entries) of pid, nil when it cannot be read.
func (*Snapshot) EnvWithheld ¶ added in v0.2.1
EnvWithheld reports whether the OS withheld the environment of pid (see ErrEnvWithheld).
func (*Snapshot) Getenv ¶
Getenv returns the value of key in the environment of pid ("" when unset or unreadable).
func (*Snapshot) OpenFiles ¶
OpenFiles returns the paths of the files pid has open, nil when that is unknown: only Linux can tell, and only for the user's own processes.
func (*Snapshot) Owner ¶ added in v0.2.1
Owner returns the user pid runs as: its effective uid in decimal on unix, the SID of its token's user on Windows. It is "" when that cannot be read, and on unix when the process's real and effective uids differ: a set-user-ID program has its owner's privileges but the arguments and environment of whoever started it, so it belongs to neither.
It is loaded on demand rather than listed with every process: Linux only tells reliably in /proc/<pid>/status, which costs as much again as the whole listing, and Windows needs the process's token.
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)
// Owner returns the user the process runs as, in the form of
// Snapshot.Owner (never "" without an error).
Owner(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, and ErrEnvWithheld an environment the OS keeps back from a readable process.