In Development Package, API may change between versions.

import "github.com/egdaemon/eg/runtime/x/wasi/egsystemd"

<a name="EnsureRunning"></a>

func EnsureRunning

func EnsureRunning(ctx context.Context, cfn ConnectionFn, units ...string) (err error)

EnsureRunning blocks until all of the named systemd units are in the "active" state, the context is cancelled, or an error is detected.

It subscribes to systemd sub-state change events so it can react promptly when a unit transitions state rather than polling on a fixed interval. On every relevant event it re-queries the live state of all requested units; if any unit is not "active" it returns a descriptive error.

Special deadline behaviour: when the context expires with context.DeadlineExceeded the function performs one final check (with a fresh 3-second timeout) before returning. This lets a caller impose a hard wall-clock limit on startup while still tolerating a unit that becomes active right at the deadline.

Any other context cancellation is returned as-is (wrapped with a stack trace).

<a name="Reload"></a>

func Reload

func Reload(ctx context.Context, cfn ConnectionFn, units ...string) (err error)

Reload issues a reload job for each unit in sequence, waiting for each job to complete before moving to the next. Use this when the unit supports reloading its configuration without a full restart. cfn is called once to obtain the D-Bus connection; pass SystemBus or UserBus as appropriate.

<a name="ReloadOrRestart"></a>

func ReloadOrRestart

func ReloadOrRestart(ctx context.Context, cfn ConnectionFn, units ...string) (err error)

ReloadOrRestart issues a reload-or-restart job for each unit in sequence, waiting for each job to complete before moving to the next. systemd reloads the unit if it supports reloading, otherwise it restarts it. cfn is called once to obtain the D-Bus connection; pass SystemBus or UserBus as appropriate.

<a name="Restart"></a>

func Restart

func Restart(ctx context.Context, cfn ConnectionFn, units ...string) (err error)

Restart issues a restart job for each unit in sequence, waiting for each job to complete before moving to the next. cfn is called once to obtain the D-Bus connection; pass SystemBus or UserBus as appropriate.

<a name="Conn"></a>

type Conn

Conn is the D-Bus connection interface used by all functions in this package. *dbus.Conn satisfies this interface; in tests a mock implementation can be returned from a ConnectionFn without requiring a live D-Bus connection.

type Conn interface {
    Close()
    ListUnitsByNamesContext(ctx context.Context, units []string) ([]dbus.UnitStatus, error)
    SetSubStateSubscriber(updateCh chan<- *dbus.SubStateUpdate, errCh chan<- error)
    RestartUnitContext(ctx context.Context, name, mode string, ch chan<- string) (int, error)
    ReloadUnitContext(ctx context.Context, name, mode string, ch chan<- string) (int, error)
    ReloadOrRestartUnitContext(ctx context.Context, name, mode string, ch chan<- string) (int, error)
}

<a name="SystemBus"></a>

func SystemBus

func SystemBus(ctx context.Context) (Conn, error)

SystemBus opens a D-Bus connection to the system-wide systemd instance.

<a name="UserBus"></a>

func UserBus

func UserBus(ctx context.Context) (Conn, error)

UserBus opens a D-Bus connection to the per-user systemd instance.

<a name="ConnectionFn"></a>

type ConnectionFn

ConnectionFn opens a D-Bus connection to systemd. Pass SystemBus or UserBus to target the system or per-user instance respectively, or supply a custom implementation for testing.

type ConnectionFn func(ctx context.Context) (Conn, error)

<a name="PerformFn"></a>

type PerformFn

PerformFn is the signature shared by dbus job-dispatch methods such as RestartUnitContext, ReloadUnitContext, and ReloadOrRestartUnitContext. It submits a job for the named unit using the given mode (e.g. "replace") and delivers the job result string (e.g. "done", "failed") to ch when the job completes.

type PerformFn func(context.Context, string, string, chan<- string) (int, error)