ptable

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 12 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 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

Constants

This section is empty.

Variables

View Source
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 Argv added in v0.4.0

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

Argv reads the command line of one process without a snapshot.

func CurrentOwner added in v0.2.1

func CurrentOwner() (string, error)

CurrentOwner returns the user this process runs as, in the form of Snapshot.Owner: its effective uid.

func Lookup

func Lookup(env []string, key string) string

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

func ReadsTerminal(pid int) (tty, known bool)

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

func TTYHosts added in v0.2.1

func TTYHosts() map[uint64]string

TTYHosts returns nil: Linux does not need it, the command lines of the login processes that name the remote host are readable to everyone.

func TTYName added in v0.2.1

func TTYName(dev uint64) string

TTYName returns the name below /dev of the terminal device dev ("pts/3", "ttys003"), "" when there is no such device.

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

func Process(pid int) (p Proc, ok bool)

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

func (p *Proc) Foreground() bool

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

func New(procs []Proc, src Source) *Snapshot

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 Take

func Take() (*Snapshot, error)

Take snapshots the processes running on this host.

func (*Snapshot) Argv

func (s *Snapshot) Argv(pid int) []string

Argv returns the command line of pid (nil when unreadable).

func (*Snapshot) Children

func (s *Snapshot) Children(pid int) []int

Children returns the child pids of pid in ascending order.

func (*Snapshot) Cwd

func (s *Snapshot) Cwd(pid int) string

Cwd returns the working directory of pid ("" when unreadable).

func (*Snapshot) Detached added in v0.2.1

func (s *Snapshot) Detached(pid int) int

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

func (s *Snapshot) Env(pid int) []string

Env returns the environment ("KEY=value" entries) of pid, nil when it cannot be read.

func (*Snapshot) EnvWithheld added in v0.2.1

func (s *Snapshot) EnvWithheld(pid int) bool

EnvWithheld reports whether the OS withheld the environment of pid (see ErrEnvWithheld).

func (*Snapshot) Exe

func (s *Snapshot) Exe(pid int) string

Exe returns the executable path of pid ("" when unreadable).

func (*Snapshot) Get

func (s *Snapshot) Get(pid int) *Proc

Get returns the process pid, or nil.

func (*Snapshot) Getenv

func (s *Snapshot) Getenv(pid int, key string) string

Getenv returns the value of key in the environment of pid ("" when unset or unreadable).

func (*Snapshot) OpenFiles

func (s *Snapshot) OpenFiles(pid int) []string

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

func (s *Snapshot) Owner(pid int) string

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.

func (*Snapshot) PIDs

func (s *Snapshot) PIDs() []int

PIDs returns every pid in ascending order.

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.

Jump to

Keyboard shortcuts

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