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