gvisor: never name an unloaded gVisor unit to systemctl
The gVisor templates ship an empty ExecStart= filled by a per-start drop-in, so show/stop of a never-started instance made systemd load it and log "Service has no ExecStart=... Refusing" on every run stop, demo cleanup, demo build cleanup and legacy proof on systemd-mode boxes. A gVisor unit with no cgroup, no drop-in directory and no entry in `systemctl list-units --all <family glob>` is now proven gone without its name. Any error keeps the existing show/stop proof. The activator's spawn guard allows the read-only list-units verb.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
11 files changed,
+444
−3
internal/activator/backend_exec.go+50 −2internal/activator/backend_legacy_test.go+73 −0internal/activator/control.go+12 −0internal/activator/sandbox_spawn_guard_test.go+2 −1internal/demobuild/gvisor_cleanup_test.go+4 −0internal/demobuild/gvisor_never_loaded_test.go+96 −0internal/demobuild/gvisor_test.go+12 −0internal/demobuild/unit.go+31 −0internal/instancectl/demo_cleanup.go+3 −0internal/instancectl/gvisor_never_loaded_test.go+111 −0internal/instancectl/sandboxstop.go+50 −0
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go| index 707548179..f6de02f2f 100644 |
| --- a/internal/activator/backend_exec.go |
| +++ b/internal/activator/backend_exec.go |
| @@ -239,6 +239,14 @@ type ExecBackend struct { |
| // processes are in the group systemd reports, not under the host |
| // slice. Nil: systemctlControlGroup. |
| ControlGroup ControlGroupOf |
| + // UnitsListed answers `systemctl list-units --all ... <glob>` for the |
| + // legacy proof's gVisor sibling (gvisorSiblingNeverLoaded). Nil: |
| + // systemctlUnitsListed, unless UnitState is a fake, in which case no |
| + // listing is asked and the full proof runs. |
| + UnitsListed UnitsListed |
| + // GVisorDropInDir is where root writes gVisor drop-ins. Empty: |
| + // /run/systemd/system. |
| + GVisorDropInDir string |
| // NestedOK gates a nested wake on the box facts a unit test can't see: |
| // the isolation mounts, the control service's own /ready. Nil: no |
| // gate (every unit test and any box that predates the check). Wired in |
| @@ -3036,8 +3044,8 @@ func (b *ExecBackend) LegacyRunGone(ctx context.Context, rt registry.Route) erro |
| return fmt.Errorf("stop %s: %w", unit, err) |
| } |
| units := []string{unit} |
| - if !rt.Rootfs() { |
| - units = append(units, instancectl.GVisorUnitName(rt.Host)) |
| + if sibling := instancectl.GVisorUnitName(rt.Host); !rt.Rootfs() && !b.gvisorSiblingNeverLoaded(ctx, rt, sibling) { |
| + units = append(units, sibling) |
| } |
| for _, unit := range units { |
| if err := b.waitLegacyRunGone(ctx, rt, unit); err != nil { |
| @@ -3046,6 +3054,46 @@ func (b *ExecBackend) LegacyRunGone(ctx context.Context, rt registry.Route) erro |
| } |
| return nil |
| } |
| + |
| +// gvisorSiblingNeverLoaded proves the gVisor sibling gone without naming it |
| +// to systemd: its template's empty ExecStart= is filled only by a per-start |
| +// drop-in, so a `systemctl show` of a name never started makes systemd load |
| +// it and log "Service has no ExecStart=... Refusing" on every proof of a |
| +// systemd-mode box. Proof: no cgroup, no drop-in directory, and systemd does |
| +// not list the name among the units it holds (asked by family glob, never |
| +// by the escaped name). Any doubt or error is false: the full proof runs. |
| +func (b *ExecBackend) gvisorSiblingNeverLoaded(ctx context.Context, rt registry.Route, unit string) bool { |
| + listed := b.UnitsListed |
| + if listed == nil { |
| + if b.UnitState != nil { |
| + return false |
| + } |
| + listed = systemctlUnitsListed |
| + } |
| + dir := b.GVisorDropInDir |
| + if dir == "" { |
| + dir = "/run/systemd/system" |
| + } |
| + if _, err := os.Lstat(filepath.Join(dir, unit+".d")); !errors.Is(err, os.ErrNotExist) { |
| + return false |
| + } |
| + if exists, _, err := b.unitEvents(ctx, rt, unit); err != nil || exists { |
| + return false |
| + } |
| + out, err := listed(ctx, instancectl.UnitPrefixGVisor+"*.service") |
| + if err != nil { |
| + return false |
| + } |
| + for _, line := range strings.Split(out, "\n") { |
| + for _, f := range strings.Fields(line) { |
| + if f == unit { |
| + return false |
| + } |
| + } |
| + } |
| + return true |
| +} |
| + |
| func (b *ExecBackend) waitLegacyRunGone(ctx context.Context, rt registry.Route, unit string) error { |
| state := b.UnitState |
| if state == nil { |
diff --git a/internal/activator/backend_legacy_test.go b/internal/activator/backend_legacy_test.go| index 24ea99926..3072efa1d 100644 |
| --- a/internal/activator/backend_legacy_test.go |
| +++ b/internal/activator/backend_legacy_test.go |
| @@ -2,6 +2,10 @@ package activator |
| import ( |
| "context" |
| + "errors" |
| + "os" |
| + "path/filepath" |
| + "slices" |
| "strings" |
| "sync" |
| "testing" |
| @@ -107,3 +111,72 @@ func TestLegacyRunGoneRefusesWhileTheUnitRuns(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// The legacy proof's gVisor sibling is never named to systemctl on a box |
| +// where systemd does not hold it and it has no cgroup or drop-in: a |
| +// `systemctl show` would load its commandless template and log "Refusing". |
| +// Listed, or with a drop-in, it keeps the full proof. |
| +func TestLegacyRunGoneSkipsAnUnloadedGVisorSibling(t *testing.T) { |
| + t.Parallel() |
| + for _, tc := range []struct { |
| + name string |
| + listed bool |
| + dropIn bool |
| + listErr bool |
| + wantNamed bool |
| + }{ |
| + {name: "unlisted"}, |
| + {name: "listed", listed: true, wantNamed: true}, |
| + {name: "drop-in", dropIn: true, wantNamed: true}, |
| + {name: "listing error", listErr: true, wantNamed: true}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + unit := &fakeUnit{} |
| + unit.set("inactive", "", false, false) |
| + b, rt := nestedBackend(t, &fakeControl{}, unit) |
| + sibling := instancectl.GVisorUnitName(rt.Host) |
| + b.GVisorDropInDir = t.TempDir() |
| + if tc.dropIn { |
| + if err := os.Mkdir(filepath.Join(b.GVisorDropInDir, sibling+".d"), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + var globs []string |
| + b.UnitsListed = func(_ context.Context, glob string) (string, error) { |
| + globs = append(globs, glob) |
| + if tc.listErr { |
| + return "", errors.New("bus") |
| + } |
| + if tc.listed { |
| + return sibling + " loaded inactive dead x\n", nil |
| + } |
| + return "", nil |
| + } |
| + var mu sync.Mutex |
| + var asked []string |
| + b.UnitState = func(ctx context.Context, u string) (string, string, error) { |
| + mu.Lock() |
| + asked = append(asked, u) |
| + mu.Unlock() |
| + return unit.state(ctx, u) |
| + } |
| + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| + defer cancel() |
| + if err := b.LegacyRunGone(ctx, rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + named := slices.Contains(asked, sibling) |
| + if named != tc.wantNamed { |
| + t.Fatalf("gVisor sibling asked = %v, want %v (asked %v)", named, tc.wantNamed, asked) |
| + } |
| + if !slices.Contains(asked, instancectl.UnitName(instancectl.RoleRun, rt.Host)) { |
| + t.Fatalf("native run not proven: %v", asked) |
| + } |
| + for _, g := range globs { |
| + if g != instancectl.UnitPrefixGVisor+"*.service" { |
| + t.Fatalf("listing asked %q, not the family glob", g) |
| + } |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/activator/control.go b/internal/activator/control.go| index 1b942185f..a4dc01826 100644 |
| --- a/internal/activator/control.go |
| +++ b/internal/activator/control.go |
| @@ -136,6 +136,18 @@ func systemctlUnitState(ctx context.Context, unit string) (string, string, error |
| return active, inv, nil |
| } |
| +// UnitsListed answers `systemctl list-units --all --plain --no-legend |
| +// --no-pager <glob>`: the units systemd holds in memory, read-only. |
| +type UnitsListed func(ctx context.Context, glob string) (string, error) |
| + |
| +func systemctlUnitsListed(ctx context.Context, glob string) (string, error) { |
| + out, err := exec.CommandContext(ctx, "systemctl", "list-units", "--all", "--plain", "--no-legend", "--no-pager", glob).Output() |
| + if err != nil { |
| + return "", unitStateError(err) |
| + } |
| + return string(out), nil |
| +} |
| + |
| // UnitMainExit answers `systemctl show -p |
| // InvocationID,ExecMainCode,ExecMainStatus,SuccessExitStatus <unit>`, which |
| // needs no privilege: how systemd recorded the end of the unit's main |
diff --git a/internal/activator/sandbox_spawn_guard_test.go b/internal/activator/sandbox_spawn_guard_test.go| index 90d2aaae2..9ef33fbba 100644 |
| --- a/internal/activator/sandbox_spawn_guard_test.go |
| +++ b/internal/activator/sandbox_spawn_guard_test.go |
| @@ -96,7 +96,7 @@ func sandboxSystemctlViolations(file *ast.File) []string { |
| verb, constant := call.Args[arg+1].(*ast.BasicLit) |
| if constant { |
| switch verb.Value { |
| - case `"show"`, `"stop"`, `"reset-failed"`, `"daemon-reload"`: |
| + case `"show"`, `"stop"`, `"reset-failed"`, `"daemon-reload"`, `"list-units"`: |
| return true |
| } |
| } |
| @@ -131,6 +131,7 @@ var sandboxSpawnInventory = map[string]int{ |
| "control.go:systemctlControlGroup:Output": 1, |
| "control.go:systemctlUnitMainExit:Output": 1, |
| "control.go:systemctlUnitState:Output": 1, |
| + "control.go:systemctlUnitsListed:Output": 1, |
| "genbackend.go:StartStaged:spawnChildFrom": 2, |
| "genbackend.go:StartStaged:spawnUnitFrom": 1, |
| "genservice.go:startGen:Start": 1, |
diff --git a/internal/demobuild/gvisor_cleanup_test.go b/internal/demobuild/gvisor_cleanup_test.go| index 6e28f1d30..ba8d12fa6 100644 |
| --- a/internal/demobuild/gvisor_cleanup_test.go |
| +++ b/internal/demobuild/gvisor_cleanup_test.go |
| @@ -67,6 +67,10 @@ func TestGVisorBuildExitDiagnosticsOnly(t *testing.T) { |
| } |
| ctl := systemctl |
| systemctl = func(ctx context.Context, args ...string) (string, error) { |
| + if args[0] == "list-units" && fault == "termination" { |
| + // A unit with a main process is loaded. |
| + return "carlos-demo-build-gvisor@test.service loaded active running x\n", nil |
| + } |
| out, err := ctl(ctx, args...) |
| if fault == "termination" { |
| out = strings.ReplaceAll(out, "MainPID=0", "MainPID=12") |
diff --git a/internal/demobuild/gvisor_never_loaded_test.go b/internal/demobuild/gvisor_never_loaded_test.go| new file mode 100644 |
| index 000000000..b2ff52cc4 |
| --- /dev/null |
| +++ b/internal/demobuild/gvisor_never_loaded_test.go |
| @@ -0,0 +1,96 @@ |
| +package demobuild |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/execsandbox" |
| +) |
| + |
| +// cleanUnit stops both build families. On a systemd-mode box the gVisor |
| +// build unit was never started, and naming it to systemctl makes systemd |
| +// load its commandless template and log "Refusing". Unlisted, with no |
| +// cgroup and no job drop-in, it is proven gone without its name. |
| +func TestGVisorBuildNeverLoadedIsNotNamed(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + listing string |
| + listErr error |
| + cgroup bool |
| + dropIn bool |
| + wantNamed bool |
| + }{ |
| + {name: "unlisted", listing: "carlos-demo-build-gvisor@ff_1.service loaded inactive dead x\n"}, |
| + {name: "listed", listing: "LISTED loaded active running x\n", wantNamed: true}, |
| + {name: "cgroup present", cgroup: true, wantNamed: true}, |
| + {name: "drop-in present", dropIn: true, wantNamed: true}, |
| + {name: "listing error", listErr: errors.New("bus"), wantNamed: true}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + fakeUnitController(t) |
| + w, j, _, _ := workerFixture(t) |
| + dir := filepath.Join(w.root, j.ID, "1") |
| + if err := os.MkdirAll(dir+"/work", 0o700); err != nil { |
| + t.Fatal(err) |
| + } |
| + native, err := layoutFor(w.root, dir, execsandbox.FamilySystemd) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + gvisor, err := layoutFor(w.root, dir, execsandbox.FamilyGVisor) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if tc.cgroup { |
| + if err := os.Mkdir(filepath.Join(buildCgroupRoot, gvisor.unit), 0o700); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(filepath.Join(buildCgroupRoot, gvisor.unit, "cgroup.events"), []byte("populated 0\n"), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if tc.dropIn { |
| + if err := os.Mkdir(filepath.Join(unitRoot, gvisor.unit+".d"), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + var calls []string |
| + w.removeSandbox = func(string) error { return nil } |
| + systemctl = func(_ context.Context, args ...string) (string, error) { |
| + calls = append(calls, strings.Join(args, " ")) |
| + switch args[0] { |
| + case "list-units": |
| + return strings.ReplaceAll(tc.listing, "LISTED", gvisor.unit), tc.listErr |
| + case "show": |
| + return "LoadState=not-found\nActiveState=inactive\nMainPID=0\nControlPID=0\nSubState=dead\nJob=\n", nil |
| + } |
| + return "", nil |
| + } |
| + if err := w.cleanUnit(context.Background(), native); err != nil { |
| + t.Fatal(err) |
| + } |
| + named := false |
| + for _, c := range calls { |
| + if strings.HasPrefix(c, "list-units") { |
| + if !strings.HasSuffix(c, " carlos-demo-build-gvisor@*.service") { |
| + t.Fatalf("listing must ask the family glob: %q", c) |
| + } |
| + continue |
| + } |
| + if strings.Contains(c, gvisor.unit) { |
| + named = true |
| + } |
| + } |
| + if named != tc.wantNamed { |
| + t.Fatalf("gVisor build name passed to systemctl = %v, want %v\n%s", named, tc.wantNamed, strings.Join(calls, "\n")) |
| + } |
| + if !strings.Contains(strings.Join(calls, "\n"), "show "+native.unit) { |
| + t.Fatalf("native build not proven:\n%s", strings.Join(calls, "\n")) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/demobuild/gvisor_test.go b/internal/demobuild/gvisor_test.go| index 5cabec1a3..1376e99bc 100644 |
| --- a/internal/demobuild/gvisor_test.go |
| +++ b/internal/demobuild/gvisor_test.go |
| @@ -159,7 +159,15 @@ func TestGVisorBuildRecoveryBothFamilies(t *testing.T) { |
| removed = true |
| return nil |
| } |
| + gvisor, lerr := layoutFor(w.root, dir, execsandbox.FamilyGVisor) |
| + if lerr != nil { |
| + t.Fatal(lerr) |
| + } |
| systemctl = func(_ context.Context, args ...string) (string, error) { |
| + if args[0] == "list-units" { |
| + // Running, or holding a queued stop job: loaded either way. |
| + return gvisor.unit + " loaded active running x\n", nil |
| + } |
| if args[0] == "stop" && strings.HasSuffix(args[len(args)-1], ".service") { |
| stops[args[len(args)-1]] = true |
| } |
| @@ -349,6 +357,10 @@ func TestGVisorBuildStopProof(t *testing.T) { |
| if args[0] == "kill" { |
| t.Fatal("extra kill could terminate runtime deletion") |
| } |
| + if args[0] == "list-units" { |
| + // A unit with a job or a process is loaded. |
| + return l.unit + " loaded active running x\n", nil |
| + } |
| out, err := ctl(ctx, args...) |
| if fault == "missing-job" { |
| out = strings.ReplaceAll(out, "Job=\n", "") |
diff --git a/internal/demobuild/unit.go b/internal/demobuild/unit.go| index 250c34dab..882a1f605 100644 |
| --- a/internal/demobuild/unit.go |
| +++ b/internal/demobuild/unit.go |
| @@ -343,6 +343,9 @@ func stopBuild(l buildLayout) error { |
| } |
| func stopBuildContext(ctx context.Context, l buildLayout) error { |
| + if l.family == execsandbox.FamilyGVisor && gvisorBuildNeverLoaded(ctx, l.unit) { |
| + return nil |
| + } |
| out, err := systemctl(ctx, "show", l.unit, "-p", "ActiveState,SubState,MainPID,ControlPID,LoadState,Job") |
| if err != nil { |
| return fmt.Errorf("show %s: %w: %s", l.unit, err, out) |
| @@ -381,6 +384,34 @@ func stopBuildContext(ctx context.Context, l buildLayout) error { |
| } |
| } |
| +// gvisorBuildNeverLoaded proves a carlos-demo-build-gvisor@ unit gone without |
| +// naming it to systemd: its template's empty ExecStart= is filled only by the |
| +// job drop-in, so a show or stop of a name never started makes systemd load |
| +// it and log "Service has no ExecStart=... Refusing" (cleanUnit stops both |
| +// families, so every build on a systemd-mode box did). The proof: no cgroup |
| +// directory (the path emptyBuildCgroup reads), no job drop-in directory, and |
| +// no listing among the units systemd holds, asked by family glob rather than |
| +// by name. Any error is false and the full proof runs. |
| +func gvisorBuildNeverLoaded(ctx context.Context, unit string) bool { |
| + for _, path := range []string{filepath.Join(buildCgroupRoot, unit), filepath.Join(unitRoot, unit+".d")} { |
| + if _, err := os.Lstat(path); !errors.Is(err, os.ErrNotExist) { |
| + return false |
| + } |
| + } |
| + out, err := systemctl(ctx, "list-units", "--all", "--plain", "--no-legend", "--no-pager", "carlos-demo-build-gvisor@*.service") |
| + if err != nil { |
| + return false |
| + } |
| + for _, line := range strings.Split(out, "\n") { |
| + for _, f := range strings.Fields(line) { |
| + if f == unit { |
| + return false |
| + } |
| + } |
| + } |
| + return true |
| +} |
| + |
| func buildInactive(m map[string]string) bool { |
| job := m["Job"] |
| return (job == "" || job == "0") && (m["ActiveState"] == "inactive" || m["ActiveState"] == "failed") && (m["SubState"] == "dead" || m["SubState"] == "failed") && m["MainPID"] == "0" && m["ControlPID"] == "0" |
diff --git a/internal/instancectl/demo_cleanup.go b/internal/instancectl/demo_cleanup.go| index f55eb83b9..ec5b89d5d 100644 |
| --- a/internal/instancectl/demo_cleanup.go |
| +++ b/internal/instancectl/demo_cleanup.go |
| @@ -246,6 +246,9 @@ func (s *Server) stopDemoUnits(ctx context.Context, host string) (map[string]boo |
| return nil, err |
| } |
| for unit := range units { |
| + if unit == GVisorUnitName(host) && s.gvisorUnitNeverLoaded(ctx, host, unit) { |
| + continue |
| + } |
| out, err = s.Systemctl(ctx, "show", unit, "-p", "ActiveState") |
| if err != nil { |
| return nil, err |
diff --git a/internal/instancectl/gvisor_never_loaded_test.go b/internal/instancectl/gvisor_never_loaded_test.go| new file mode 100644 |
| index 000000000..0296f48ee |
| --- /dev/null |
| +++ b/internal/instancectl/gvisor_never_loaded_test.go |
| @@ -0,0 +1,111 @@ |
| +package instancectl |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/execsandbox" |
| +) |
| + |
| +// A systemd-mode box stops the gVisor sibling of every run. Its template's |
| +// empty ExecStart= makes systemd log "Refusing" whenever the name is loaded, |
| +// so a sibling systemd does not hold, with no cgroup and no drop-in, is |
| +// proven gone without its name reaching systemctl. Every other answer keeps |
| +// the show/stop proof. |
| +func TestGVisorSiblingNeverLoadedIsNotNamed(t *testing.T) { |
| + const host = "hs-ie.example" |
| + sibling := GVisorUnitName(host) |
| + if !strings.Contains(sibling, `\x2d`) { |
| + t.Fatalf("fixture must exercise an escaped name: %s", sibling) |
| + } |
| + for _, tc := range []struct { |
| + name string |
| + listing string |
| + listErr error |
| + cgroup bool |
| + dropIn bool |
| + wantNamed bool |
| + }{ |
| + {name: "unlisted", listing: "carlos-instance-gvisor@other.example.service loaded inactive dead x\n"}, |
| + {name: "empty listing", listing: ""}, |
| + {name: "listed escaped name", listing: sibling + " loaded inactive dead carlos gVisor demo instance\n", wantNamed: true}, |
| + {name: "listed with marker column", listing: "● " + sibling + " loaded failed failed x\n", wantNamed: true}, |
| + {name: "cgroup present", cgroup: true, wantNamed: true}, |
| + {name: "drop-in present", dropIn: true, wantNamed: true}, |
| + {name: "listing error", listErr: errors.New("bus"), wantNamed: true}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + var calls []string |
| + s := &Server{CgroupRoot: t.TempDir(), DropInDir: t.TempDir(), SandboxEffective: execsandbox.EffectiveSystemd} |
| + s.removeSandbox = func(string) error { return nil } |
| + s.Systemctl = func(_ context.Context, args ...string) (string, error) { |
| + calls = append(calls, strings.Join(args, " ")) |
| + if args[0] == "list-units" { |
| + return tc.listing, tc.listErr |
| + } |
| + return systemd257Stopped(args[len(args)-1]), nil |
| + } |
| + if tc.cgroup { |
| + if err := os.MkdirAll(filepath.Join(s.CgroupRoot, parentSlices, SliceName(host), sibling), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if tc.dropIn { |
| + if err := os.MkdirAll(filepath.Join(s.DropInDir, sibling+".d"), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := s.stopRunFamilies(context.Background(), host); err != nil { |
| + t.Fatal(err) |
| + } |
| + named := false |
| + for _, c := range calls { |
| + if strings.HasPrefix(c, "list-units") { |
| + if !strings.HasSuffix(c, " "+UnitPrefixGVisor+"*.service") || strings.Contains(c, `\`) { |
| + t.Fatalf("listing must ask the family glob, not the escaped name: %q", c) |
| + } |
| + continue |
| + } |
| + if strings.Contains(c, sibling) { |
| + named = true |
| + } |
| + } |
| + if named != tc.wantNamed { |
| + t.Fatalf("gVisor name passed to systemctl = %v, want %v\n%s", named, tc.wantNamed, strings.Join(calls, "\n")) |
| + } |
| + if !strings.Contains(strings.Join(calls, "\n"), "stop "+UnitName(RoleRun, host)) { |
| + t.Fatalf("native run not stopped:\n%s", strings.Join(calls, "\n")) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// The best-effort stop (a fence that could not be taken) and demo cleanup |
| +// follow the same rule. |
| +func TestGVisorSiblingNeverLoadedBestEffortAndDemo(t *testing.T) { |
| + const host = "hs-ie.example" |
| + sibling := GVisorUnitName(host) |
| + var calls []string |
| + s := &Server{CgroupRoot: t.TempDir(), DropInDir: t.TempDir(), SandboxEffective: execsandbox.EffectiveSystemd} |
| + s.removeSandbox = func(string) error { return nil } |
| + s.Systemctl = func(_ context.Context, args ...string) (string, error) { |
| + calls = append(calls, strings.Join(args, " ")) |
| + if args[0] == "list-units" { |
| + return "", nil |
| + } |
| + return systemd257Stopped(args[len(args)-1]), nil |
| + } |
| + s.stopRunFamiliesBestEffort(context.Background(), host) |
| + if _, err := s.stopDemoUnits(context.Background(), host); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, c := range calls { |
| + if !strings.HasPrefix(c, "list-units") && strings.Contains(c, sibling) { |
| + t.Fatalf("gVisor name passed to systemctl: %q\n%s", c, strings.Join(calls, "\n")) |
| + } |
| + } |
| +} |
diff --git a/internal/instancectl/sandboxstop.go b/internal/instancectl/sandboxstop.go| index 643b6493f..9858a9a76 100644 |
| --- a/internal/instancectl/sandboxstop.go |
| +++ b/internal/instancectl/sandboxstop.go |
| @@ -59,6 +59,9 @@ func runInactive(m map[string]string) bool { |
| m["MainPID"] == "0" && m["ControlPID"] == "0" && (m["Job"] == "" || m["Job"] == "0") |
| } |
| func (s *Server) stopAndProveRun(ctx context.Context, host, unit string) error { |
| + if s.gvisorUnitNeverLoaded(ctx, host, unit) { |
| + return nil |
| + } |
| if strings.HasPrefix(unit, UnitPrefixRun) && s.effectiveSandbox() == execsandbox.EffectiveSystemd { |
| if _, err := s.Systemctl(ctx, "stop", unit); err != nil { |
| return fmt.Errorf("%s: %w", unit, err) |
| @@ -81,6 +84,9 @@ func (s *Server) stopAndProveRun(ctx context.Context, host, unit string) error { |
| } |
| func (s *Server) proveRunGone(ctx context.Context, host, unit string) error { |
| + if s.gvisorUnitNeverLoaded(ctx, host, unit) { |
| + return nil |
| + } |
| out, err := s.Systemctl(ctx, "show", "-p", runProofProperties, unit) |
| if err != nil { |
| return fmt.Errorf("%s: %w", unit, err) |
| @@ -148,3 +154,47 @@ func (s *Server) sandboxRunFilesPresent(host string) (bool, error) { |
| } |
| return false, nil |
| } |
| + |
| +// gvisorUnitNeverLoaded proves a carlos-instance-gvisor@ instance gone |
| +// without naming it to systemd. The template ships an empty ExecStart= that |
| +// only a per-start drop-in fills, so `systemctl show` or `stop` of a name |
| +// that was never started makes systemd load it and log "Service has no |
| +// ExecStart=... Refusing" on every stop of a systemd-mode box. The proof |
| +// here: no cgroup directory, no drop-in directory a start could have used, |
| +// and systemd does not list the name among the units it holds (a running |
| +// unit, or one with a queued job, is always loaded). The listing asks for |
| +// the family glob, never the escaped name: fnmatch would read the \x2d |
| +// backslashes as escapes. Anything else, including any error, is false and |
| +// the caller runs the existing show/stop proof. |
| +func (s *Server) gvisorUnitNeverLoaded(ctx context.Context, host, unit string) bool { |
| + if !strings.HasPrefix(unit, UnitPrefixGVisor) || s.Systemctl == nil || s.CgroupRoot == "" || s.DropInDir == "" { |
| + return false |
| + } |
| + for _, path := range []string{ |
| + filepath.Join(s.CgroupRoot, parentSlices, SliceName(host), unit), |
| + filepath.Join(s.DropInDir, unit+".d"), |
| + } { |
| + if _, err := os.Lstat(path); !errors.Is(err, os.ErrNotExist) { |
| + return false |
| + } |
| + } |
| + out, err := s.Systemctl(ctx, "list-units", "--all", "--plain", "--no-legend", "--no-pager", UnitPrefixGVisor+"*.service") |
| + if err != nil { |
| + return false |
| + } |
| + return !unitListed(out, unit) |
| +} |
| + |
| +// unitListed reads list-units output loosely: any field equal to the name |
| +// counts, so a marker column systemd may add can only turn the answer into |
| +// "listed", which keeps the full proof. |
| +func unitListed(out, unit string) bool { |
| + for _, line := range strings.Split(out, "\n") { |
| + for _, f := range strings.Fields(line) { |
| + if f == unit { |
| + return true |
| + } |
| + } |
| + } |
| + return false |
| +} |