systemd-resolved: an unknown path in its directory is left and journalled once (review r1 C-7)

The checks/owned check refuses a directory without unknown (ADR 0422 §8).
/var/lib/node-resolver stays, its value "module" (lead answer L-1), not
"program": no other program writes there. The guard looks for a path it
does not know every round and, finding one, leaves it exactly as it is
and says so to the journal once, never every round.

Builds: design 08 sign-in-page §6, C-7
This commit is contained in:
jochen
2026-11-24 22:01:21 +01:00
parent bd6dcdcd0c
commit a74afe71f3
5 changed files with 74 additions and 3 deletions
@@ -107,6 +107,10 @@ type Guard struct {
// read at start from UplinkNamesAppliedFile, so a restart takes the names off the link it names,
// never inferring which. Only the uplink names' round reads and writes it.
applied UplinkApplied
// unknownSaid is every path in the uplink-names directory this module does not know, already said to
// the journal (design 08 sign-in-page C-7): said once, never every round.
unknownSaid map[string]bool
}
// NewGuard is the machine's.
@@ -349,6 +353,7 @@ func (g *Guard) Run(ctx context.Context) {
case <-routes.C:
g.KeepRoutes(ctx)
g.KeepUplinkNames(ctx)
g.checkUnknownPaths()
}
}
}
@@ -557,6 +562,32 @@ func (g *Guard) writeApplied() {
}
}
// checkUnknownPaths looks at the uplink-names directory for a path this module does not know (design 08
// sign-in-page C-7; ADR 0422 §8, lead answer L-1): it leaves it exactly as it is, and says so to the
// journal once, never every round.
func (g *Guard) checkUnknownPaths() {
dir := filepath.Dir(g.Resolver.UplinkNamesFile)
entries, err := os.ReadDir(dir)
if err != nil {
return
}
known := map[string]bool{
filepath.Base(g.Resolver.UplinkNamesFile): true,
filepath.Base(g.Resolver.UplinkNamesAppliedFile): true,
}
for _, e := range entries {
name := e.Name()
if known[name] || strings.HasSuffix(name, ".tmp") || g.unknownSaid[name] {
continue
}
if g.unknownSaid == nil {
g.unknownSaid = map[string]bool{}
}
g.unknownSaid[name] = true
g.Log("%s in %s is not this module's; left as it is", name, dir)
}
}
// Request is one call over the socket: a verb and its arguments, one JSON line.
type Request struct {
Verb string `json:"verb"`
@@ -337,6 +337,33 @@ func TestUplinkNamesMoveWithTheDefaultRoute(t *testing.T) {
}
}
// A path in the uplink-names directory this module does not know is left exactly as it is, and said to
// the journal once, never every round (design 08 sign-in-page C-7; ADR 0422 §8, lead answer L-1).
func TestAnUnknownPathIsLeftAndJournalledOnce(t *testing.T) {
g, _, _, r := aGuardedMachineWithLinks(t, "wlan0")
dir := filepath.Dir(r.UplinkNamesFile)
stray := filepath.Join(dir, "a-program-left-this")
if err := os.WriteFile(stray, []byte("not this module's"), 0o644); err != nil {
t.Fatal(err)
}
var said []string
g.Log = func(format string, a ...any) { said = append(said, fmt.Sprintf(format, a...)) }
g.checkUnknownPaths()
g.checkUnknownPaths()
count := 0
for _, s := range said {
if strings.Contains(s, "a-program-left-this") {
count++
}
}
if count != 1 {
t.Errorf("said %d times, want once: %v", count, said)
}
if got := contentOf(t, stray); got != "not this module's" {
t.Errorf("the unknown path was changed: %q", got)
}
}
// A changed list replaces the old one on the link, the route itself staying put (design 08 sign-in-page §9).
func TestAChangedListReplacesTheOldOneOnTheLink(t *testing.T) {
g, _, f, r := aGuardedMachineWithLinks(t, "wlan0")
@@ -58,6 +58,17 @@ func TestTheManifestSaysWhatTheGuardKeeps(t *testing.T) {
if m.Facts["private-interface"].Path != PrivateInterfacePath || m.Facts["private-interface"].Template != "mesh0\n" {
t.Errorf("the private interface fact is %+v", m.Facts["private-interface"])
}
// design 08 sign-in-page C-7; ADR 0422 §8, lead answer L-1: a directory needs `unknown`, and this
// one's is "module" (the guard leaves a path it does not know as it is), never "program".
uplinkDir := false
for _, r := range m.Resources {
if r["type"] == "directory" && r["path"] == "/var/lib/node-resolver" {
uplinkDir = r["unknown"] == "module"
}
}
if !uplinkDir {
t.Error("/var/lib/node-resolver's directory resource does not say unknown: module")
}
if m.Facts["resolvers"].Template != m.Facts["kept"].Template {
t.Error("the resolver file and the copy the guard puts back are not one template")
}