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