procs

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 13 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 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

View Source
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

View Source
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 Alive

func Alive(pid int) bool

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 CwdsOf

func CwdsOf(list []Process) map[int]string

CwdsOf returns the working directory of each listed process, by PID.

func StartedAt

func StartedAt(pid int) time.Time

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 ValidPID

func ValidPID(pid int) bool

ValidPID reports whether pid can be a process's ID: positive, and no larger than MaxPID.

Types

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
)

func Probe

func Probe(pid int) Liveness

Probe reports whether a process has pid, with the signal 0, which checks for one without signaling it: Running for one the signal reaches or that is another user's, Gone when none has it, and Unknown for any other answer. A pid ValidPID refuses is Unknown, never signaled.

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

func Locate(list []Process) *Located

Locate returns list ready to be asked which of its processes work in a folder. It resolves nothing until it is first asked.

func (*Located) In

func (l *Located) In(dir string) []Process

In returns the processes whose working directory is dir itself, not one inside it, by PID, compared as the package's Within compares them, with each process's directory as it resolved the first time l was asked.

func (*Located) Within

func (l *Located) Within(dir string) []Process

Within returns the processes whose working directory is dir or inside it, by PID, compared as the package's Within compares them, with each process's directory as it resolved the first time l was 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

func Others(list []Process, self int) []Process

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

func ParseLsof(out string) []Process

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

func Within(list []Process, dir string) []Process

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

func WithoutServices(list []Process, commands []string) []Process

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.

type System

type System struct {
	Run execpath.Runner
}

System lists this machine's processes. Run runs lsof where the OS needs it; nil runs programs directly.

func (System) Processes

func (s System) Processes(ctx context.Context) ([]Process, error)

Processes returns the signed-in user's running processes, by PID.

Jump to

Keyboard shortcuts

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