| index 0ca06618b..3e929f59d 100644 |
| --- a/internal/activator/control.go |
| +++ b/internal/activator/control.go |
| @@ -3,6 +3,7 @@ package activator |
| import ( |
| "context" |
| "errors" |
| + "fmt" |
| "os" |
| "os/exec" |
| "path/filepath" |
| @@ -100,7 +101,7 @@ type UnitState func(ctx context.Context, unit string) (active, invocation string |
| func systemctlUnitState(ctx context.Context, unit string) (string, string, error) { |
| out, err := exec.CommandContext(ctx, "systemctl", "show", "-p", "ActiveState,InvocationID", unit).Output() |
| if err != nil { |
| - return "", "", err |
| + return "", "", unitStateError(err) |
| } |
| var active, inv string |
| for _, line := range strings.Split(string(out), "\n") { |
| @@ -134,7 +135,7 @@ type MainExit struct { |
| func systemctlUnitMainExit(ctx context.Context, unit string) (MainExit, error) { |
| out, err := exec.CommandContext(ctx, "systemctl", "show", "-p", "InvocationID,ExecMainCode,ExecMainStatus,SuccessExitStatus", unit).Output() |
| if err != nil { |
| - return MainExit{}, err |
| + return MainExit{}, unitStateError(err) |
| } |
| var m MainExit |
| for _, line := range strings.Split(string(out), "\n") { |
| @@ -167,7 +168,7 @@ type ControlGroupOf func(ctx context.Context, unit string) (string, error) |
| func systemctlControlGroup(ctx context.Context, unit string) (string, error) { |
| out, err := exec.CommandContext(ctx, "systemctl", "show", "-p", "ControlGroup", "--value", unit).Output() |
| if err != nil { |
| - return "", err |
| + return "", unitStateError(err) |
| } |
| return strings.TrimSpace(string(out)), nil |
| } |
| @@ -190,3 +191,22 @@ func readCgroupEvents(path string) (exists, populated bool, err error) { |
| } |
| return true, !strings.Contains(string(b), "populated 0"), nil |
| } |
| + |
| +// unitStateError is a failed systemctl read's error with systemctl's own |
| +// stderr appended, cut to 200 bytes: "exit status 1" alone could not tell |
| +// a lost D-Bus at shutdown from a missing unit (#451). An error that is |
| +// not an exit is returned unchanged. |
| +func unitStateError(err error) error { |
| + var ee *exec.ExitError |
| + if !errors.As(err, &ee) { |
| + return err |
| + } |
| + msg := strings.TrimSpace(string(ee.Stderr)) |
| + if msg == "" { |
| + return err |
| + } |
| + if len(msg) > 200 { |
| + msg = msg[:200] |
| + } |
| + return fmt.Errorf("%w: %s", err, msg) |
| +} |