diff --git a/cli/cmd/albedo/daemon_service.go b/cli/cmd/albedo/daemon_service.go index a0817225e..b034d18b2 100644 --- a/cli/cmd/albedo/daemon_service.go +++ b/cli/cmd/albedo/daemon_service.go @@ -67,6 +67,7 @@ func (connector *connector) startService(ctx context.Context) (*daemon.Connectio if view.Class != localdaemon.ServiceIdle && view.Class != localdaemon.ServiceTransition { return nil, view.MutationError() } + mark := localdaemon.MarkLogStart(connector.options.HomeDir) if view.Class == localdaemon.ServiceIdle { // The manager may start the service on its own between observations; // re-observe before each mutation so a redundant start is not submitted. @@ -98,7 +99,7 @@ func (connector *connector) startService(ctx context.Context) (*daemon.Connectio } } } - return localdaemon.AwaitManaged(ctx, connector.options.HomeDir, view.Discovery.Snapshot) + return localdaemon.AwaitManaged(ctx, connector.options.HomeDir, view.Discovery.Snapshot, mark) } func (connector *connector) Stop(parent context.Context, kernels bool) error { @@ -322,6 +323,7 @@ func (connector *connector) Install(parent context.Context) (string, error) { running := reloaded.Discovery.Kind == localdaemon.Running if !running && newInstallation { attemptedStartOrEnable = true + mark := localdaemon.MarkLogStart(connector.options.HomeDir) if after.Properties["ActiveState"] == "failed" { if err = view.Unit.Run(ctx, "reset-failed"); err != nil { return fail(err) @@ -330,7 +332,7 @@ func (connector *connector) Install(parent context.Context) (string, error) { if err = view.Unit.Run(ctx, "start"); err != nil { return fail(err) } - connection, readyErr := localdaemon.AwaitManaged(ctx, connector.options.HomeDir, view.Discovery.Snapshot) + connection, readyErr := localdaemon.AwaitManaged(ctx, connector.options.HomeDir, view.Discovery.Snapshot, mark) if readyErr != nil { return fail(readyErr) } diff --git a/cli/cmd/albedo/daemon_service_launchd_policy.go b/cli/cmd/albedo/daemon_service_launchd_policy.go index a44ccabf3..503f2ab81 100644 --- a/cli/cmd/albedo/daemon_service_launchd_policy.go +++ b/cli/cmd/albedo/daemon_service_launchd_policy.go @@ -84,6 +84,7 @@ func (connector *connector) startLaunchd(ctx context.Context) (*daemon.Connectio if err = view.LaunchdStartError(); err != nil { return nil, err } + mark := localdaemon.MarkLogStart(connector.options.HomeDir) switch view.Class { case localdaemon.UnloadedService, localdaemon.ServiceIdle: if localdaemon.HomeHeld(connector.options.HomeDir) { @@ -108,7 +109,7 @@ func (connector *connector) startLaunchd(ctx context.Context) (*daemon.Connectio }) } if current.Class != view.Class { - return localdaemon.AwaitLaunchd(ctx, connector.options.HomeDir, view.Observation, false) + return localdaemon.AwaitLaunchd(ctx, connector.options.HomeDir, view.Observation, false, mark) } if localdaemon.HomeHeld(connector.options.HomeDir) { return nil, errors.New("home is held; service start refused") @@ -133,7 +134,7 @@ func (connector *connector) startLaunchd(ctx context.Context) (*daemon.Connectio return nil, view.MutationError() } submitted := view.Class == localdaemon.ServiceIdle || view.Class == localdaemon.UnloadedService - connection, readyErr := localdaemon.AwaitLaunchd(ctx, connector.options.HomeDir, view.Observation, submitted) + connection, readyErr := localdaemon.AwaitLaunchd(ctx, connector.options.HomeDir, view.Observation, submitted, mark) if readyErr != nil && submitted { return nil, launchdPartial(view.Unit, "service start", readyErr) } @@ -346,10 +347,11 @@ func (connector *connector) installLaunchd(parent context.Context) (string, erro if err = ctx.Err(); err != nil { return "", err } + mark := localdaemon.MarkLogStart(connector.options.HomeDir) if err = after.Unit.Bootstrap(ctx); err != nil { return "", launchdPartial(after.Unit, "bootstrap", err) } - connection, err := localdaemon.AwaitLaunchd(ctx, connector.options.HomeDir, after.Observation, true) + connection, err := localdaemon.AwaitLaunchd(ctx, connector.options.HomeDir, after.Observation, true, mark) if err != nil { return "", launchdPartial(after.Unit, "bootstrap", err) } diff --git a/cli/internal/localdaemon/lifecycle.go b/cli/internal/localdaemon/lifecycle.go index c98c33d07..fa918141d 100644 --- a/cli/internal/localdaemon/lifecycle.go +++ b/cli/internal/localdaemon/lifecycle.go @@ -302,6 +302,22 @@ func watchStartup(home string) startupProgress { return progress } +// LogStart marks where a start's log begins: the size of home's daemon.log +// now. Take it before submitting the start, so a daemon that logs a refusal +// and exits before the start command returns still has its lines reported. +type LogStart int64 + +func MarkLogStart(home string) LogStart { + return LogStart(watchStartup(home).logSize) +} + +// watchStartupFrom watches startup progress, reporting the log from mark on. +func watchStartupFrom(home string, mark LogStart) startupProgress { + progress := watchStartup(home) + progress.logStart = min(int64(mark), progress.logSize) + return progress +} + // logSince is up to 4 KiB of the log written from offset on, trimmed. func logSince(logPath string, offset int64) string { file, err := os.Open(logPath) diff --git a/cli/internal/localdaemon/refusal_test.go b/cli/internal/localdaemon/refusal_test.go index 4c9d0bb2d..6b366bf04 100644 --- a/cli/internal/localdaemon/refusal_test.go +++ b/cli/internal/localdaemon/refusal_test.go @@ -11,15 +11,17 @@ import ( ) // A service manager reports only an exit status. A refusal is final, so the -// CLI shows what the daemon logged for this start, not an earlier start's -// lines or a generic failure. +// CLI shows what the daemon logged for this start: not an earlier start's +// lines, and not without the lines a fast refusal wrote before the start +// command returned and the wait began. func TestServiceRefusalCarriesThisStartsLog(t *testing.T) { home := t.TempDir() log := filepath.Join(home, "daemon.log") if err := os.WriteFile(log, []byte("an earlier start\n"), 0o600); err != nil { t.Fatal(err) } - progress := watchStartup(home) + mark := MarkLogStart(home) + // The daemon refuses and exits while the start command is still running. refusal := "This home was upgraded by a newer albedo (future); update albedo or restore a backup." file, err := os.OpenFile(log, os.O_APPEND|os.O_WRONLY, 0o600) if err != nil { @@ -29,6 +31,8 @@ func TestServiceRefusalCarriesThisStartsLog(t *testing.T) { t.Fatal(err) } file.Close() + // Only now does the wait begin. + progress := watchStartupFrom(home, mark) // stalled() advances its own size; the refusal keeps the start's offset. progress.stalled() diff --git a/cli/internal/localdaemon/service.go b/cli/internal/localdaemon/service.go index 88eaf1da0..75cf9c7b4 100644 --- a/cli/internal/localdaemon/service.go +++ b/cli/internal/localdaemon/service.go @@ -163,8 +163,10 @@ func LockLauncher(ctx context.Context, home string) (*os.File, error) { return acquireLauncher(ctx, home) } -func AwaitManaged(ctx context.Context, home string, dead daemon.ConnectionSnapshot) (*daemon.Connection, error) { - progress := watchStartup(home) +// AwaitManaged waits for the service's daemon. mark is where this start's +// log begins, taken before the start was submitted. +func AwaitManaged(ctx context.Context, home string, dead daemon.ConnectionSnapshot, mark LogStart) (*daemon.Connection, error) { + progress := watchStartupFrom(home, mark) for { view, err := ObserveService(ctx, home) if err != nil { diff --git a/cli/internal/localdaemon/service_launchd_policy.go b/cli/internal/localdaemon/service_launchd_policy.go index 6be25dd0b..439446612 100644 --- a/cli/internal/localdaemon/service_launchd_policy.go +++ b/cli/internal/localdaemon/service_launchd_policy.go @@ -116,9 +116,10 @@ func (view ServiceView) LaunchdStartError() error { // AwaitLaunchd waits for a bracket-validated managed daemon. Failure is // reported only once launchd has made an attempt after initial, so a reply // that precedes the spawn is not mistaken for a stopped service; runs may -// also reset, which leaves the wait to the log-progress bound. -func AwaitLaunchd(ctx context.Context, home string, initial loginservice.Observation, submitted bool) (*daemon.Connection, error) { - progress := watchStartup(home) +// also reset, which leaves the wait to the log-progress bound. mark is where +// this start's log begins, taken before any bootstrap or kickstart. +func AwaitLaunchd(ctx context.Context, home string, initial loginservice.Observation, submitted bool, mark LogStart) (*daemon.Connection, error) { + progress := watchStartupFrom(home, mark) baseline := initial.Launchd if baseline != nil && !baseline.Loaded { baseline = nil