proc

package
v0.6.76 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: AGPL-3.0, Apache-2.0, MIT Imports: 14 Imported by: 0

Documentation

Overview

Package proc runs and supervises the daemon's child processes and, when the daemon is PID 1, reaps orphaned zombies without racing exec.Cmd.Wait for the children the daemon started itself.

Every child the daemon starts must go through Start (or Run/Output), which registers its PID while holding the reaper's lock. The reaper only collects zombies whose PIDs aren't registered, so os/exec keeps receiving the exit status of its own children.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommandContext

func CommandContext(ctx context.Context, name string, args ...string) *exec.Cmd

CommandContext is exec.CommandContext with the child in its own process group, so signals reach the whole job and the daemon's terminal signals don't.

func IsPID1

func IsPID1() bool

IsPID1 reports whether the daemon runs as init.

func Output

func Output(cmd *exec.Cmd) ([]byte, error)

Output is cmd.Output through Start and Wait. Stderr is included in the error.

func Reap

func Reap(ctx context.Context, log *slog.Logger)

Reap collects orphaned zombies until ctx ends. It reacts to SIGCHLD and also sweeps periodically, because SIGCHLD coalesces.

func Run

func Run(cmd *exec.Cmd) error

Run is cmd.Run through Start and Wait.

func Start

func Start(cmd *exec.Cmd, start func() error) error

Start starts cmd (through start when non-nil, e.g. pty.StartWithSize) and registers its PID with the reaper. Callers must call Wait, not cmd.Wait.

func Wait

func Wait(cmd *exec.Cmd) error

Wait waits for a child started with Start and unregisters it.

Types

type Process

type Process struct {
	Name string
	Cmd  *exec.Cmd
	// ExcludeFromIdle keeps the process's CPU out of idle detection (e.g. the browser stack, whose activity is
	// measured on its streams instead).
	ExcludeFromIdle bool
	StartedAt       time.Time
	// contains filtered or unexported fields
}

Process is a long-running child the supervisor knows about (harness, Xvfb, Chromium, VNC, ...).

func (*Process) Done

func (p *Process) Done() <-chan struct{}

Done is closed when the process exits.

func (*Process) Err

func (p *Process) Err() error

Err is the exit error once Done is closed.

func (*Process) Pid

func (p *Process) Pid() int

Pid is the process ID.

type ProcessInfo

type ProcessInfo struct {
	Name            string
	Pid             int
	StartedAt       time.Time
	ExcludeFromIdle bool
}

ProcessInfo is a snapshot of a supervised process.

type Supervisor

type Supervisor struct {
	// contains filtered or unexported fields
}

Supervisor tracks long-running children so signals can be forwarded to them and they can be stopped together.

func NewSupervisor

func NewSupervisor(log *slog.Logger) *Supervisor

NewSupervisor returns an empty supervisor.

func (*Supervisor) ExcludedPids

func (s *Supervisor) ExcludedPids() map[int]bool

ExcludedPids returns PIDs (process-group leaders) whose CPU idle detection should ignore.

func (*Supervisor) Forward

func (s *Supervisor) Forward(sig syscall.Signal)

Forward sends sig to every supervised process group.

func (*Supervisor) List

func (s *Supervisor) List() []ProcessInfo

List returns the supervised processes, sorted by name.

func (*Supervisor) Spawn

func (s *Supervisor) Spawn(name string, cmd *exec.Cmd, start func() error, excludeFromIdle bool) (*Process, error)

Spawn starts cmd under supervision. start may be nil (cmd.Start) or a custom starter (pty).

func (*Supervisor) Stop

func (s *Supervisor) Stop(p *Process, grace time.Duration) error

Stop sends SIGTERM to the process group, then SIGKILL after grace.

func (*Supervisor) StopAll

func (s *Supervisor) StopAll(ctx context.Context, grace time.Duration)

StopAll stops every supervised process.

Jump to

Keyboard shortcuts

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