ptable

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 11 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 — 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

This section is empty.

Functions

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

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

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

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.

Jump to

Keyboard shortcuts

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