From e20d3c75489e2be9475eb352597eb3f96d087d46 Mon Sep 17 00:00:00 2001 From: jochen Date: Tue, 24 Nov 2026 21:31:51 +0100 Subject: [PATCH] M8 networkmanager: the folder's unknown is the module's MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The folder the two parts share says unknown module, so the node-engine does nothing inside it and the watcher reacts: a path it does not know is left as it is and written to its journal once per path, at its start and at the file event that makes it; the README says so. Builds: design 08 sign-in-page §12, §24, §25, §27; lead answer L-1 (hq 3a3a18a9) --- modules/networkmanager/README.md | 4 +- .../networkmanager/cmd/uplink-tools/dbus.go | 54 +++++++++++++++++ .../cmd/uplink-tools/state_test.go | 49 +++++++++++++++ .../cmd/uplink-tools/templates_test.go | 2 +- .../cmd/uplink-tools/unknown.go | 60 +++++++++++++++++++ .../networkmanager/cmd/uplink-tools/watch.go | 16 ++++- modules/networkmanager/module.json | 3 +- 7 files changed, 184 insertions(+), 4 deletions(-) create mode 100644 modules/networkmanager/cmd/uplink-tools/unknown.go diff --git a/modules/networkmanager/README.md b/modules/networkmanager/README.md index ab0b47c0f..355ba3d15 100644 --- a/modules/networkmanager/README.md +++ b/modules/networkmanager/README.md @@ -27,7 +27,9 @@ written whole (beside, then renamed over): | `names.json` | the bundle's last `uplink-names` call: the names, its result (`ok`, `no holder` or the failure) and when | | `sequence` | the next sequence of the watcher's message ids, never reset while the folder lives | -When the module leaves, the node-engine keeps the folder while it holds files, and nothing reads it then. +The folder's `unknown` is `module`: the node-engine does nothing inside it, and the watcher leaves any other path it +finds there as it is and writes it to its journal once per path. When the module leaves, the node-engine keeps the +folder while it holds files, and nothing reads it then. ## The watcher opens the page on its own machine, without the bus diff --git a/modules/networkmanager/cmd/uplink-tools/dbus.go b/modules/networkmanager/cmd/uplink-tools/dbus.go index 88fbe5562..2332db355 100644 --- a/modules/networkmanager/cmd/uplink-tools/dbus.go +++ b/modules/networkmanager/cmd/uplink-tools/dbus.go @@ -4,6 +4,7 @@ package main // SessionNew, and the system bus's socket coming back, heard as a file event of the kernel on its folder. import ( + "bytes" "errors" "fmt" "os" @@ -338,3 +339,56 @@ func watchFiles(dirs []string, match func(string) bool, stop <-chan struct{}) <- }() return out } + +// pathsMade hands each path a file event of the kernel makes in one of dirs, until stop closes; a folder that is not +// there is not watched, and said. +func pathsMade(dirs []string, stop <-chan struct{}) <-chan string { + out := make(chan string, 16) + fd, err := unix.InotifyInit1(unix.IN_CLOEXEC | unix.IN_NONBLOCK) + if err != nil { + journal("watching %v failed: %v", dirs, err) + return out + } + wds := map[int32]string{} + for _, dir := range dirs { + wd, err := unix.InotifyAddWatch(fd, dir, unix.IN_CREATE|unix.IN_MOVED_TO) + if err != nil { + journal("watching %s failed: %v", dir, err) + continue + } + wds[int32(wd)] = dir + } + f := os.NewFile(uintptr(fd), "inotify") + go func() { + <-stop + f.Close() + }() + go func() { + defer f.Close() + buf := make([]byte, 64*(unix.SizeofInotifyEvent+unix.NAME_MAX+1)) + for { + n, err := f.Read(buf) + if err != nil { + return + } + for off := 0; off+unix.SizeofInotifyEvent <= n; { + ev := (*unix.InotifyEvent)(unsafe.Pointer(&buf[off])) + nameAt := off + unix.SizeofInotifyEvent + end := nameAt + int(ev.Len) + if end > n { + break + } + name := string(bytes.TrimRight(buf[nameAt:end], "\x00")) + off = end + if dir, ok := wds[ev.Wd]; ok && name != "" { + select { + case out <- filepath.Join(dir, name): + case <-stop: + return + } + } + } + } + }() + return out +} diff --git a/modules/networkmanager/cmd/uplink-tools/state_test.go b/modules/networkmanager/cmd/uplink-tools/state_test.go index 75d3615eb..bc6ffeb99 100644 --- a/modules/networkmanager/cmd/uplink-tools/state_test.go +++ b/modules/networkmanager/cmd/uplink-tools/state_test.go @@ -114,3 +114,52 @@ func TestTheWatchersSocketAnswersOneLine(t *testing.T) { t.Fatal("a closed socket answered") } } + +// A path in the folder the watcher does not know is left as it is and written to its journal once per path: what is +// there at its start, and what a file event makes after (§12; lead answer L-1). +func TestAPathTheWatcherDoesNotKnowIsLeftAndSaidOnce(t *testing.T) { + lines := journalLines(t) + sys := &fakeSystem{} + sys.set(guest(ratingFull)) + w := aWatcher(t, sys) + f := w.Folder + if _, err := f.keep(eventEnded, "s", map[string]string{}); err != nil { + t.Fatal(err) + } + f.writeCheckAddress("http://check.example.net/") + for _, p := range []string{filepath.Join(f.Dir, "notes.txt"), filepath.Join(f.Dir, pendingName, "stray")} { + if err := os.WriteFile(p, []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + } + made := make(chan string) + w.Made = func(dirs []string, stop <-chan struct{}) <-chan string { + if len(dirs) != 2 || dirs[0] != f.Dir || dirs[1] != filepath.Join(f.Dir, pendingName) { + t.Errorf("watched %v", dirs) + } + return made + } + stop, done := make(chan struct{}), make(chan struct{}) + go func() { w.run(stop); close(done) }() + for _, p := range []string{filepath.Join(f.Dir, "other"), filepath.Join(f.Dir, "other"), filepath.Join(f.Dir, "sequence.new"), + filepath.Join(f.Dir, pendingName, "0000000002.json")} { + made <- p + } + w.askLoop(askState) + close(stop) + <-done + var said []string + for _, l := range lines() { + if strings.Contains(l, "is not the module's") { + said = append(said, strings.TrimPrefix(l, f.Dir+"/")) + } + } + want := []string{"notes.txt is not the module's: left as it is", "pending/stray is not the module's: left as it is", + "other is not the module's: left as it is"} + if strings.Join(said, "|") != strings.Join(want, "|") { + t.Fatalf("said %q", said) + } + if _, err := os.Stat(filepath.Join(f.Dir, "notes.txt")); err != nil { + t.Fatal("a path the watcher does not know was not left") + } +} diff --git a/modules/networkmanager/cmd/uplink-tools/templates_test.go b/modules/networkmanager/cmd/uplink-tools/templates_test.go index c54d1b809..7c438ea2d 100644 --- a/modules/networkmanager/cmd/uplink-tools/templates_test.go +++ b/modules/networkmanager/cmd/uplink-tools/templates_test.go @@ -107,7 +107,7 @@ func TestTheManifestDeclaresTheSignInParts(t *testing.T) { } dir := byID["sign-in-state"] if dir["type"] != "directory" || dir["path"] != "${machine:account-home}/"+stateHome || - dir["owner"] != "${machine:account}" || dir["mode"] != "0700" { + dir["owner"] != "${machine:account}" || dir["mode"] != "0700" || dir["unknown"] != "module" { t.Errorf("the folder is %v", dir) } p := byID["sign-in"] diff --git a/modules/networkmanager/cmd/uplink-tools/unknown.go b/modules/networkmanager/cmd/uplink-tools/unknown.go new file mode 100644 index 000000000..6f07e01e7 --- /dev/null +++ b/modules/networkmanager/cmd/uplink-tools/unknown.go @@ -0,0 +1,60 @@ +package main + +// A path in the module's folder the watcher does not know (design 08 sign-in-page §12; lead answer L-1): the folder's +// `unknown` is `module`, so the node-engine does nothing inside it and the watcher reacts. It leaves such a path as it +// is and writes it to its journal once per path: at its start for what is there, and at each file event of the kernel +// that makes one. + +import ( + "os" + "path/filepath" + "regexp" + "strings" +) + +// keptFile is a kept event's name in pending/, or one being written beside it. +var keptFile = regexp.MustCompile(`^[0-9]{10}\.json(\.new)?$`) + +// knownPath says whether path is one of the folder's own: watch.sock, check-address, pending/ and its files, +// names.json, sequence, or a file being written beside one of them. +func (f Folder) knownPath(path string) bool { + rel, err := filepath.Rel(f.Dir, path) + if err != nil || strings.HasPrefix(rel, "..") { + return false + } + dir, name := filepath.Split(rel) + switch dir { + case "": + name = strings.TrimSuffix(name, ".new") + return name == socketName || name == checkName || name == pendingName || name == namesName || name == sequenceName + case pendingName + "/": + return keptFile.MatchString(name) + } + return false +} + +// unknownAtStart is every path in the folder and in pending/ the watcher does not know. +func (f Folder) unknownAtStart() []string { + var out []string + for _, dir := range []string{f.Dir, filepath.Join(f.Dir, pendingName)} { + entries, _ := os.ReadDir(dir) + for _, e := range entries { + if p := filepath.Join(dir, e.Name()); !f.knownPath(p) { + out = append(out, p) + } + } + } + return out +} + +// strange writes a path the watcher does not know to its journal, once per path, and leaves it as it is. +func (w *Watcher) strange(path string) { + if w.Folder.knownPath(path) || w.told[path] { + return + } + if w.told == nil { + w.told = map[string]bool{} + } + w.told[path] = true + journal("%s is not the module's: left as it is", path) +} diff --git a/modules/networkmanager/cmd/uplink-tools/watch.go b/modules/networkmanager/cmd/uplink-tools/watch.go index 54e89d269..0d5eedd6b 100644 --- a/modules/networkmanager/cmd/uplink-tools/watch.go +++ b/modules/networkmanager/cmd/uplink-tools/watch.go @@ -11,6 +11,7 @@ import ( "fmt" "net/url" "os" + "path/filepath" "time" "git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/nodeuplink" @@ -148,6 +149,10 @@ type Watcher struct { // Beside runs an open beside the loop; the func it returns is run by the loop at its end. Beside func(work func() func()) + // Made fires with each path a file event of the kernel makes in one of dirs, until stop closes. + Made func(dirs []string, stop <-chan struct{}) <-chan string + + told map[string]bool starting *sessionStart opening bool later []func() @@ -166,7 +171,7 @@ type Watcher struct { func newWatcher(f Folder) *Watcher { w := &Watcher{Folder: f, Dial: dialSystemBus, SocketThere: systemSocketThere, Now: time.Now, asks: make(chan asked), UID: uint32(os.Getuid()), Environment: userEnvironment, FileEvents: fileEvents, After: time.After, - Desk: newSessionDesk(), ends: make(chan func())} + Desk: newSessionDesk(), ends: make(chan func()), Made: pathsMade} w.Beside = w.besideLoop return w } @@ -206,6 +211,13 @@ func (w *Watcher) run(stop <-chan struct{}) { if w.asks == nil { w.asks = make(chan asked) } + var made <-chan string + if w.Made != nil { + made = w.Made([]string{w.Folder.Dir, filepath.Join(w.Folder.Dir, pendingName)}, stop) + } + for _, p := range w.Folder.unknownAtStart() { + w.strange(p) + } w.subscribe() for { select { @@ -227,6 +239,8 @@ func (w *Watcher) run(stop <-chan struct{}) { case <-w.socketAt: w.socketAt = nil w.subscribe() + case p := <-made: + w.strange(p) case end := <-w.ends: end() case a := <-w.asks: diff --git a/modules/networkmanager/module.json b/modules/networkmanager/module.json index 0ebea3fef..deba6b7a5 100644 --- a/modules/networkmanager/module.json +++ b/modules/networkmanager/module.json @@ -88,7 +88,8 @@ "type": "directory", "path": "${machine:account-home}/.local/state/mesh/networkmanager", "owner": "${machine:account}", - "mode": "0700" + "mode": "0700", + "unknown": "module" }, { "id": "sign-in",