service

package
v0.9.3-dev.2 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: GPL-3.0 Imports: 20 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 installs and controls Mihari as a native OS service.

Index

Constants

View Source
const (
	DefinitionActionMask       = "mask"
	DefinitionActionUnmask     = "unmask"
	DefinitionActionDisable    = "disable"
	DefinitionActionEnable     = "enable"
	DefinitionActionStop       = "stop"
	DefinitionActionStart      = "start"
	DefinitionActionDefinition = "definition"
	DefinitionActionDropin     = "dropin"
	DefinitionActionDisabled   = "disabled"
	DefinitionActionReload     = "reload"
)

Variables

This section is empty.

Functions

func DefinitionFileState added in v0.9.3

func DefinitionFileState(file DefinitionFile, link string) string

DefinitionFileState is the content identity recorded before a file action.

func DirectActionHook added in v0.9.3

func DirectActionHook(ctx context.Context, action DefinitionAction, apply func(context.Context) error) error

DirectActionHook runs apply without journaling. Tests and T14 replace it.

func IsInteractive

func IsInteractive() bool

IsInteractive reports whether this process is running outside the OS service manager. When false (Windows service session), the daemon entrypoint must call Manager.Run so the process registers with SCM (StartServiceCtrlDispatcher).

Types

type ActionHook added in v0.9.3

type ActionHook func(ctx context.Context, action DefinitionAction, apply func(context.Context) error) error

ActionHook surrounds one concrete manager or filesystem effect. T14 supplies a journaled hook; adapters must call it once per effect.

type Clock added in v0.9.3

type Clock interface {
	Now() time.Time
	Sleep(ctx context.Context, d time.Duration) error
}

Clock is an injectable sleeper used by WaitOwnedTreeExit.

type CommandResult added in v0.9.3

type CommandResult struct {
	ExitCode int
	Stdout   []byte
	Stderr   []byte
}

CommandResult is a completed process observation. Non-zero ExitCode is not itself an error.

type CommandRunner added in v0.9.3

type CommandRunner interface {
	Run(ctx context.Context, argv []string) (CommandResult, error)
}

CommandRunner executes argv[0] as an absolute path with argv[1:] as arguments.

type Controller

type Controller interface {
	Install() error
	Uninstall() error
	Start() error
	Stop() error
	Restart() error
	Status() (StatusKind, error)
	Run() error
}

Controller performs service manager operations (tests inject fakes).

type Definition added in v0.9.3

type Definition struct {
	Status  StatusKind
	Enabled bool
	Running bool
	Masked  bool
	Binary  string
	Args    []string
	Env     []string
	Files   []DefinitionFile
	Links   []DefinitionLink
	Process ProcessIdentity
}

Definition is a read-only snapshot of the managed unit or launchd job.

func BuildUnixDefinition added in v0.9.3

func BuildUnixDefinition(layout platform.ResolvedLayout, goos string) (Definition, error)

BuildUnixDefinition returns the absolute, fixed-environment managed definition.

type DefinitionAction added in v0.9.3

type DefinitionAction struct {
	// Path and File identify a concrete private-backed definition effect.
	Path string
	File *DefinitionFile
	Link string

	Kind       string
	TargetRole string
	OldState   string
	NewState   string
}

DefinitionAction is one external mutation T14 wraps with intent/done.

type DefinitionAdapter added in v0.9.3

type DefinitionAdapter interface {
	InspectDefinition(context.Context) (Definition, error)
	DisableAutostartAndStop(context.Context) error
	WaitOwnedTreeExit(context.Context) error
	WriteDefinition(context.Context, Definition) error
	RestoreDefinition(context.Context, Definition) error
	Start(context.Context) error
	Probe(context.Context) (Definition, error)
}

DefinitionAdapter is the Unix service-manager seam consumed by install transactions. Windows keeps Controller and does not implement these methods.

type DefinitionFile added in v0.9.3

type DefinitionFile struct {
	// Identity records the saved inode. Restoration may replace a later unit
	// or mask; transaction recovery validates its recorded publication versions.
	Identity string
	Path     string
	Bytes    []byte
	Owner    uint32
	Mode     uint32
	Kind     string
}

DefinitionFile is one unit, drop-in, mask, or plist object.

type DefinitionLink struct {
	Identity string
	Path     string
	Target   string
	Owner    uint32
	Mode     uint32
}

DefinitionLink is an enable/wants symlink recorded for exact restore.

type DefinitionStore added in v0.9.3

type DefinitionStore interface {
	Read(ctx context.Context, path string) (DefinitionFile, error)
	Write(ctx context.Context, file DefinitionFile) error
	Mask(ctx context.Context, path, target string) error
	Remove(ctx context.Context, path string) error
	ReadLink(ctx context.Context, path string) (string, error)
	List(ctx context.Context, dir string) ([]string, error)
}

DefinitionStore reads and publishes service definition objects.

func NewUnixDefinitionStore added in v0.9.3

func NewUnixDefinitionStore() DefinitionStore

NewUnixDefinitionStore constructs the retained-capability native service store. It performs no IO; each operation verifies and owns its parent capability.

type LaunchdAdapter added in v0.9.3

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

LaunchdAdapter inspects and mutates the system LaunchDaemon.

func (*LaunchdAdapter) BindStopAuthority added in v0.9.3

func (a *LaunchdAdapter) BindStopAuthority(def Definition, boot string)

BindStopAuthority retains the private backup's process identity independently of later service-manager observations. It performs no external mutation.

func (*LaunchdAdapter) DisableAutostartAndStop added in v0.9.3

func (a *LaunchdAdapter) DisableAutostartAndStop(ctx context.Context) error

func (*LaunchdAdapter) InspectDefinition added in v0.9.3

func (a *LaunchdAdapter) InspectDefinition(ctx context.Context) (Definition, error)

func (*LaunchdAdapter) ObserveAction added in v0.9.3

func (a *LaunchdAdapter) ObserveAction(ctx context.Context, action DefinitionAction) (string, error)

func (*LaunchdAdapter) Probe added in v0.9.3

func (a *LaunchdAdapter) Probe(ctx context.Context) (Definition, error)

func (*LaunchdAdapter) ReplayAction added in v0.9.3

func (a *LaunchdAdapter) ReplayAction(ctx context.Context, action DefinitionAction) error

func (*LaunchdAdapter) RestoreDefinition added in v0.9.3

func (a *LaunchdAdapter) RestoreDefinition(ctx context.Context, def Definition) error

func (*LaunchdAdapter) Start added in v0.9.3

func (a *LaunchdAdapter) Start(ctx context.Context) error

func (*LaunchdAdapter) WaitOwnedTreeExit added in v0.9.3

func (a *LaunchdAdapter) WaitOwnedTreeExit(ctx context.Context) error

func (*LaunchdAdapter) WaitRecordedTreeExit added in v0.9.3

func (a *LaunchdAdapter) WaitRecordedTreeExit(ctx context.Context) error

WaitRecordedTreeExit checks the private backup's old group before replaying target effects. A newly running target does not change the old group proof.

func (*LaunchdAdapter) WriteDefinition added in v0.9.3

func (a *LaunchdAdapter) WriteDefinition(ctx context.Context, def Definition) error

type LaunchdPaths added in v0.9.3

type LaunchdPaths struct {
	Launchctl string
	Plist     string
	Label     string
	Domain    string
}

LaunchdPaths is the absolute launchd system-domain layout.

func DefaultLaunchdPaths added in v0.9.3

func DefaultLaunchdPaths() LaunchdPaths

DefaultLaunchdPaths returns the production LaunchDaemons layout.

type Manager

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

Manager is the production entry for service control.

func New

func New(opts Options) *Manager

New returns a Manager with defaults.

func (*Manager) Install

func (m *Manager) Install() error

Install copies mihari into the machine install directory, then registers the OS service with that installed path (never the caller's download location).

func (*Manager) Reinstall

func (m *Manager) Reinstall() error

Reinstall stops the service, removes registration, copies this binary into the install directory, re-registers ImagePath, and starts the service.

func (*Manager) Restart

func (m *Manager) Restart() error

Restart restarts the service.

func (*Manager) Run

func (m *Manager) Run() error

Run executes under the service manager (or foreground when interactive).

func (*Manager) Start

func (m *Manager) Start() error

Start starts the service.

func (*Manager) Status

func (m *Manager) Status() (StatusKind, error)

Status reports the service state. When the service is not registered, it returns StatusNotInstalled with a nil error so callers (CLI status, TUI badge) can distinguish "not installed" without elevation. On Windows, Access is denied from the default service library falls back to a low-privilege SCM query so non-admin users still see running/stopped/not_installed.

func (*Manager) Stop

func (m *Manager) Stop() error

Stop stops the service.

func (*Manager) Uninstall

func (m *Manager) Uninstall() error

Uninstall stops the service (if running), then removes the OS service registration. Stopping first avoids Windows SCM "marked for deletion" while the process is still alive.

func (*Manager) UpdateInstalledBinary added in v0.7.0

func (m *Manager) UpdateInstalledBinary() (bool, error)

UpdateInstalledBinary synchronizes the current executable into the machine install directory and restarts an existing OS service from that copy. It reports whether a service installation was found. A missing service is a successful no-op.

type OSCommandRunner added in v0.9.3

type OSCommandRunner struct{}

OSCommandRunner invokes an absolute executable without a shell.

func (OSCommandRunner) Run added in v0.9.3

type Options

type Options struct {
	// Executable is the absolute path to mihari. Empty uses the current process executable.
	Executable string
	// Arguments defaults to ["daemon"].
	Arguments []string
	// Run is required only for Run().
	Run RunFunc
	// Ready is closed by the daemon body when the control plane is listening.
	// When non-nil, program.Start waits for it (or run exit / timeout) before returning.
	Ready <-chan struct{}
	// StartTimeout bounds how long Start waits for Ready. Zero means 15s.
	StartTimeout time.Duration
	// NewController builds a Controller; tests replace it.
	NewController func(run RunFunc, executable string, arguments []string) (Controller, error)
}

Options configures which binary the OS service should launch.

type ProcessIdentity added in v0.9.3

type ProcessIdentity struct {
	PID       int
	BootID    string
	StartUnix int64
	StartUsec uint32
	Group     string
	Comm      string
}

ProcessIdentity identifies a managed process across PID reuse.

func RecordedLaunchdIdentity added in v0.9.3

func RecordedLaunchdIdentity(boot string, pid int, start string) (ProcessIdentity, error)

RecordedLaunchdIdentity reconstructs a query-only group identity from a protected runtime generation. It never authorizes signaling a historic PID.

type ProcessTree added in v0.9.3

type ProcessTree interface {
	Empty(ctx context.Context, group string) (bool, error)
	SignalGroup(ctx context.Context, group, signal string) error
	Lookup(ctx context.Context, id ProcessIdentity) (bool, error)
	SignalIdentity(ctx context.Context, id ProcessIdentity, signal string) error
	Identify(ctx context.Context, pid int) (ProcessIdentity, error)
}

ProcessTree observes and signals a verified cgroup or launchd identity.

type RecoveryAdapter added in v0.9.3

type RecoveryAdapter interface {
	DefinitionAdapter
	ObserveAction(context.Context, DefinitionAction) (string, error)
	ReplayAction(context.Context, DefinitionAction) error
}

RecoveryAdapter performs one recorded effect beneath an outer journal lease. ReplayAction must only be called after its caller persisted an intent.

type RunFunc

type RunFunc func(ctx context.Context) error

RunFunc is the daemon body; it blocks until ctx is cancelled.

type StatusKind

type StatusKind string

StatusKind is a redacted service state for CLI/JSON.

const (
	StatusRunning      StatusKind = "running"
	StatusStopped      StatusKind = "stopped"
	StatusUnknown      StatusKind = "unknown"
	StatusNotInstalled StatusKind = "not_installed"
)

type SystemdAdapter added in v0.9.3

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

SystemdAdapter inspects and mutates the system mihari.service unit.

func NewSystemdAdapter added in v0.9.3

func NewSystemdAdapter(runner CommandRunner, hook ActionHook) *SystemdAdapter

NewSystemdAdapter constructs the production Linux definition adapter.

func NewSystemdAdapterWithConfig added in v0.9.3

func NewSystemdAdapterWithConfig(cfg SystemdConfig) *SystemdAdapter

NewSystemdAdapterWithConfig constructs an adapter from explicit OS boundaries.

func (*SystemdAdapter) DisableAutostartAndStop added in v0.9.3

func (a *SystemdAdapter) DisableAutostartAndStop(ctx context.Context) error

func (*SystemdAdapter) InspectDefinition added in v0.9.3

func (a *SystemdAdapter) InspectDefinition(ctx context.Context) (Definition, error)

func (*SystemdAdapter) ObserveAction added in v0.9.3

func (a *SystemdAdapter) ObserveAction(ctx context.Context, action DefinitionAction) (string, error)

func (*SystemdAdapter) Probe added in v0.9.3

func (a *SystemdAdapter) Probe(ctx context.Context) (Definition, error)

func (*SystemdAdapter) ReplayAction added in v0.9.3

func (a *SystemdAdapter) ReplayAction(ctx context.Context, action DefinitionAction) error

func (*SystemdAdapter) RestoreDefinition added in v0.9.3

func (a *SystemdAdapter) RestoreDefinition(ctx context.Context, def Definition) error

func (*SystemdAdapter) Start added in v0.9.3

func (a *SystemdAdapter) Start(ctx context.Context) error

func (*SystemdAdapter) WaitOwnedTreeExit added in v0.9.3

func (a *SystemdAdapter) WaitOwnedTreeExit(ctx context.Context) error

func (*SystemdAdapter) WriteDefinition added in v0.9.3

func (a *SystemdAdapter) WriteDefinition(ctx context.Context, def Definition) error

type SystemdConfig added in v0.9.3

type SystemdConfig struct {
	Runner CommandRunner
	Files  DefinitionStore
	Tree   ProcessTree
	Clock  Clock
	Hook   ActionHook
	Paths  SystemdPaths
}

SystemdConfig injects the OS boundaries for an explicitly assembled adapter.

type SystemdPaths added in v0.9.3

type SystemdPaths struct {
	Systemctl string
	UnitDir   string
	UnitFile  string
	DropinDir string
	WantsLink string
	DevNull   string
}

SystemdPaths is the absolute systemd layout used by the Linux adapter.

func DefaultSystemdPaths added in v0.9.3

func DefaultSystemdPaths() SystemdPaths

DefaultSystemdPaths returns the production /etc unit layout.

Jump to

Keyboard shortcuts

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