service

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 11 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 service manages init/service units through an injected exec.Runner.

Build a Manager for an explicit backend (systemd is the only one today) and a Runner, then call its methods. Query verbs (is-enabled/is-active) run unprivileged; mutations escalate through the Runner.

r, _ := exec.NewRunner(exec.Direct)
svc, _ := service.New(service.Systemd, r)
_ = svc.EnableNow(ctx, "nginx.service")

Detect reports whether systemd is usable on the host so a consumer can choose a backend explicitly.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknownBackend = fmt.Errorf("service: unknown backend")

ErrUnknownBackend is returned by New for the zero value or any Backend the SDK does not implement (fail-closed).

View Source
var ErrUnsafeUnitContent = fmt.Errorf("unsafe systemd unit content")

ErrUnsafeUnitContent is returned by WriteUnit when the unit body carries a directive that would turn the agent into a root persistence / dropper primitive. A unit file written to /etc/systemd/system is executed by PID 1 as root, so its content is as privileged as the executables it names — the content policy below is the gate that keeps an attacker-supplied unit from running `curl | sh`, preloading a hostile library into every service, or executing a payload out of a world-writable directory.

Functions

func ValidateUnitContent

func ValidateUnitContent(content string) error

ValidateUnitContent applies the unit-file content policy to content without writing anything — the same gate WriteUnit enforces. Exported so a caller that RENDERS unit content (the agent's embedded unit, spec 27) can pin at test time that its template never produces a directive the write path would reject.

func ValidateUnitName

func ValidateUnitName(unit string) error

ValidateUnitName reports whether unit is a safe, well-formed systemd unit name.

Types

type Backend

type Backend int

Backend selects the service-manager implementation. Passed explicitly even though systemd is the only value today; the zero value is invalid (New → ErrUnknownBackend). The deleted OpenRC/Runit/S6 scaffolds — which only ever returned "not supported" — are not ported; a real second backend is appended here when actually written.

const Systemd Backend = iota + 1

Systemd wraps systemctl.

func Detect

func Detect(ctx context.Context) []Backend

Detect reports the service backends usable on THIS host: Systemd when both systemctl is on PATH and /run/systemd/system exists (systemd is PID 1). It lists; it never picks. An empty slice means no usable service manager.

type Manager

type Manager interface {
	Status(ctx context.Context, unit string) (UnitStatus, error)
	IsEnabled(ctx context.Context, unit string) (bool, error)
	IsActive(ctx context.Context, unit string) (bool, error)
	IsMasked(ctx context.Context, unit string) (bool, error)
	Enable(ctx context.Context, unit string) error
	Disable(ctx context.Context, unit string) error
	EnableNow(ctx context.Context, unit string) error
	DisableNow(ctx context.Context, unit string) error
	Start(ctx context.Context, unit string) error
	Stop(ctx context.Context, unit string) error
	Restart(ctx context.Context, unit string) error
	// Reload asks a running unit to re-read its configuration via its
	// ExecReload command, without restarting it (so it keeps open connections
	// and child processes). A unit that defines no ExecReload makes systemctl
	// exit non-zero, returned as a *exec.CommandError — use Restart for those.
	Reload(ctx context.Context, unit string) error
	Mask(ctx context.Context, unit string) error
	Unmask(ctx context.Context, unit string) error
	DaemonReload(ctx context.Context) error
	WriteUnit(ctx context.Context, unit, content string) error
	// ReadUnit returns the on-disk content of a unit WriteUnit manages —
	// the sibling read so the unit path is constructed in exactly one
	// place. An absent unit surfaces sys/fs's wrapped fs.ErrNotExist
	// (errors.Is-able), so callers can tell "not installed" from a read
	// failure.
	ReadUnit(ctx context.Context, unit string) (string, error)
	RemoveUnit(ctx context.Context, unit string) error
	// Version reports the service manager's major version (for systemd:
	// the first integer token on the first line of `systemctl --version`).
	// Unparseable or empty output is an ERROR, never a guessed value —
	// version-conditional rendering decides its own fail-safe.
	Version(ctx context.Context) (int, error)
	// NeedsReload reports whether the manager's LOADED configuration for
	// unit is stale relative to the on-disk unit file (systemd's
	// NeedDaemonReload property) — i.e. a daemon-reload is pending. Lets
	// a caller that once failed a reload retry it later without tracking
	// state of its own. Unexpected output is an ERROR, never a guessed
	// false.
	NeedsReload(ctx context.Context, unit string) (bool, error)
}

Manager is the service-manager contract.

func New

func New(b Backend, runner exec.Runner, _ ...Option) (Manager, error)

New returns a Manager for the named backend, driven by runner. Pure: validates the backend is known; it does not probe the host (use Detect). The zero value and any unimplemented backend are rejected with ErrUnknownBackend.

type Option

type Option func(*systemd)

Option is the functional-option type for backend-specific knobs (none today).

type UnitStatus

type UnitStatus struct {
	Enabled bool // explicitly enabled (systemctl enable), not boot-via-dependency
	Active  bool
	Masked  bool
	Static  bool // starts at boot via deps but cannot be enabled/disabled
}

UnitStatus is a unit's current state.

Jump to

Keyboard shortcuts

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