process

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package process provides PID-file management, daemonize re-exec, and signal-handler installation primitives shared by the af governor start/stop/status subcommands.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAlreadyRunning is returned when a process is already running.
	ErrAlreadyRunning = errors.New("process: already running")

	// ErrStalePID is returned when the PID file exists but the process is dead.
	ErrStalePID = errors.New("process: stale PID (process not running)")

	// ErrNotRunning is returned when no PID file is found.
	ErrNotRunning = errors.New("process: not running")

	// ErrUnsupported is returned on platforms where a feature is not supported.
	ErrUnsupported = errors.New("process: unsupported on this platform")
)

Sentinel errors returned by this package.

Functions

func Daemonize

func Daemonize() (isChild bool, childPID int, err error)

Daemonize re-execs the current process as a background daemon.

If the environment variable DONMAI_DAEMON=1 (legacy: AF_DAEMON=1) is set, Daemonize returns (true, 0, nil) — the current process is already the daemon child and the caller should continue its work.

Otherwise Daemonize re-execs os.Args[0] with os.Args[1:], appending DONMAI_DAEMON=1 to the environment, with Setsid set and stdin/stdout/stderr redirected to /dev/null. It returns (false, childPID, nil) — the caller should print a "started PID <childPID>" message and then call os.Exit(0).

func InstallSignalHandlers

func InstallSignalHandlers(ctx context.Context, cancel context.CancelFunc)

InstallSignalHandlers starts a goroutine that listens for SIGINT and SIGTERM. On receipt of either signal, cancel is called. The goroutine stops cleanly when ctx is done and signal.Stop is called for cleanup.

InstallSignalHandlers does not block — it returns immediately after starting the background goroutine.

Types

type PIDFile

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

PIDFile manages a PID file for tracking a running process.

func NewPIDFile

func NewPIDFile(name string) (*PIDFile, error)

NewPIDFile returns a PIDFile for the given process name. It prefers $XDG_RUNTIME_DIR/agentfactory/<name>.pid and falls back to os.TempDir()/agentfactory/<name>.pid.

func (*PIDFile) Path

func (p *PIDFile) Path() string

Path returns the absolute path to the PID file.

func (*PIDFile) Read

func (p *PIDFile) Read() (int, error)

Read reads and validates the PID from the file. Returns ErrNotRunning if the file does not exist. Returns ErrStalePID if the recorded process is no longer alive.

func (*PIDFile) Remove

func (p *PIDFile) Remove() error

Remove deletes the PID file. It is idempotent — no error is returned if the file does not exist.

func (*PIDFile) Write

func (p *PIDFile) Write(pid int) error

Write creates the parent directory (mode 0o700) and writes pid to the file with mode 0o600, overwriting any existing content.

Jump to

Keyboard shortcuts

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