Documentation
¶
Overview ¶
Package procs lists the running processes of the signed-in user with the directory each works in, so the app can tell a worktree some session still runs in, whichever coding agent or tool that is.
On macOS it asks lsof, on Linux it reads /proc; on any other OS it reports ErrUnsupported, and callers treat whether a directory is in use as unknown.
Index ¶
- Constants
- Variables
- func Adopted(p Process, byPID map[int]Process) bool
- func Alive(pid int) bool
- func ByPID(list []Process) map[int]Process
- func CwdsOf(list []Process) map[int]string
- func EnvOf(pid int, name string) (string, bool)
- func ParseProcArgs(buf []byte) []string
- func ParseProcEnv(buf []byte, name string) (string, bool)
- func StartedAt(pid int) time.Time
- func TTY(ctx context.Context, run execpath.Runner, pid int) (string, error)
- func ValidPID(pid int) bool
- type Detail
- type Lister
- type Liveness
- type Located
- type Process
- type System
Constants ¶
const MaxPID = math.MaxInt32
MaxPID is the largest process ID any OS the app runs on hands out. A larger one would be cut short by the kernel, so kill(2) could read it as -1 and reach every process.
Variables ¶
var ErrNoTTY = errors.New("the process runs on no terminal")
ErrNoTTY is returned for a process that runs on no terminal, or that ps can't find.
var ErrUnsupported = errors.New("listing processes is not supported on this OS")
ErrUnsupported is returned where this OS's processes cannot be listed.
Functions ¶
func Adopted ¶
Adopted reports a process the system's process manager adopted: its parent is PID 1, or a process of byPID named launchd, init, or systemd (a user's own systemd too). Either what started it exited and it was handed over, or it was started as a background service.
func Alive ¶
Alive reports whether a process with pid runs: one the signal 0 reaches, or that exists but is another user's. It sends no signal.
func EnvOf ¶
EnvOf returns the value of one environment variable, name, of the process with pid, as the process started with it, from /proc, and reports whether it could. It returns that one variable and keeps nothing else: the environment holds the process's secrets, so what was read is cleared before EnvOf returns, and a caller never logs or stores any more of it. Another user's process hides its environment, and reports false.
func ParseProcArgs ¶
ParseProcArgs reads the arguments from macOS's kern.procargs2 for a process: its argument count as a 32-bit integer in the machine's byte order, the path it was executed from, NUL padding, then that many arguments, each ending in a NUL, and its environment after them, which it never reads. It returns nil for a buffer it can't read.
func ParseProcEnv ¶
ParseProcEnv reads one environment variable, name, from macOS's kern.procargs2 for a process, laid out as ParseProcArgs reads it: it skips the argument count, the path the process was executed from and its NUL padding, and that many arguments, then looks for name among the environment's NAME=value strings, each ending in a NUL, which end at an empty one. It returns name's value alone, copied out, and reports false for a name that isn't set there (one only among the arguments included), for a buffer it can't read, and for an empty name or one holding '=' or a NUL. The environment holds the process's secrets, so it keeps nothing else of it.
func StartedAt ¶
StartedAt returns when the process with pid started, from /proc, or the zero time when it isn't running or can't be read. A PID the kernel hands out again belongs to a process with a different start time, which is how a caller tells a process it recorded from a stranger that reused its PID.
Linux keeps the start as ticks since boot, and the boot time it reports moves whenever the wall clock is stepped, so the answer moves with it: compare two readings with some slack, and don't compare one with a wall-clock time recorded elsewhere.
func TTY ¶
TTY returns the terminal device the process pid runs on, such as /dev/ttys004 or /dev/pts/3, asking ps through run. A terminal's tab runs on such a device, so a terminal that finds tabs by it (iTerm2, Terminal.app) can bring forward the tab a session or a run's script runs in. A process with no terminal, or none at all, is ErrNoTTY.
Types ¶
type Detail ¶
type Detail struct {
// Args is its command line, its arguments joined by spaces on one line, as in
// "/bin/sh -c sleep 5", cut short past maxArgs runes, or "" when it can't be read.
Args string
// Terminal reports a process with a controlling terminal: one a terminal's shell
// started, as a session the user runs is.
Terminal bool
}
Detail is more of one process than a listing reads: its whole command line, and whether it has a terminal. DetailOf reads it for the few processes that need it.
type Lister ¶
type Lister interface {
// Processes returns the signed-in user's running processes, by PID, or
// ErrUnsupported.
Processes(ctx context.Context) ([]Process, error)
}
Lister lists the running processes. System is the real one.
type Liveness ¶
type Liveness int
Liveness is what Probe learned of a process ID.
const ( // Unknown means it can't be told whether a process has the ID: one ValidPID refuses, // an answer that says neither, or an OS where Probe doesn't ask. Unknown Liveness = iota // Running means a process has the ID, the user's own or another user's. Running // Gone means no process has the ID. Gone )
type Located ¶
type Located struct {
// contains filtered or unexported fields
}
Located is a list of processes whose working directories are resolved once, the first time it is asked of a folder, and reused for every folder after: a listing that asks of each worktree resolves every process's directory once, not once per worktree. It is not safe for concurrent use.
func Locate ¶
Locate returns list ready to be asked which of its processes work in a folder. It resolves nothing until it is first asked.
type Process ¶
type Process struct {
// PID is its process ID, and PPID its parent's, or 0 when unknown.
PID int
PPID int
// Command is its program's name, such as "codex", as the OS reports it.
Command string
// Dir is its working directory, an absolute path.
Dir string
}
Process is one running process.
func Others ¶
Others returns the processes that are neither self nor its children, such as the git commands self runs in a directory while it looks at that directory.
func ParseLsof ¶
ParseLsof reads `lsof -F pcRn` output: a "p<pid>" line starts each process, an "R<ppid>" line gives its parent, a "c<command>" line names it, and the first "n<path>" line after it is its working directory when lsof is asked for the cwd descriptor alone. Other lines are skipped, and a process with no directory is left out.
func Within ¶
Within returns the processes whose working directory is dir or inside it, by PID. dir and each process's directory are compared with symbolic links resolved where they can be, so a link to a worktree still counts.
func WithoutServices ¶
WithoutServices returns list without the background services of the commands named, and every process under one. A service is a process of one of commands whose parent is the system's process manager (PID 1, or launchd, init, or systemd), as a daemon's is, such as Codex's app-server daemon. It outlives the session that started it and keeps that session's folder as its working directory, but works for no session there. The same command run by a terminal or an editor, as a session is, has that as its parent, and stays.