diff --git a/pkgs/router-webui/leasesdns.go b/pkgs/router-webui/leasesdns.go index 58afb58..4285bfb 100644 --- a/pkgs/router-webui/leasesdns.go +++ b/pkgs/router-webui/leasesdns.go @@ -204,6 +204,7 @@ func syncDynamic(ctl controller, published, want map[string]DynRecord, fresh boo // dynStatus is what the daemon leaves for the web UI. type dynStatus struct { + Pid string `json:"pid,omitempty"` // of the Unbound that has Published Published []DynRecord `json:"published"` Skipped []DynSkip `json:"skipped"` Updated time.Time `json:"updated"` @@ -264,6 +265,18 @@ func parseUnboundPID(status string) (string, error) { return "", fmt.Errorf("no pid in unbound-control status") } +// stillThere is what an earlier run published that the running Unbound still +// has: all of it if it is the same Unbound, nothing if it is a new one. A new +// Unbound has none of it, and removing it by name would remove what the +// configuration publishes under that name now (a client name that became a +// DHCP reservation: the restart that brought the reservation is this case). +func stillThere(published map[string]DynRecord, publishedPid, currentPid string) map[string]DynRecord { + if publishedPid != "" && publishedPid == currentPid { + return published + } + return map[string]DynRecord{} +} + // leasesDNS is `router-webui leases-dns`. func leasesDNS(args []string) { fs := flag.NewFlagSet("leases-dns", flag.ExitOnError) @@ -286,7 +299,9 @@ func leasesDNS(args []string) { // What a previous run published is removed first: the daemon may have // been restarted while Unbound kept its data. published := map[string]DynRecord{} + publishedPid := "" if st, ok := readDynStatus(*run); ok { + publishedPid = st.Pid for _, r := range st.Published { published[r.Name] = r } @@ -302,6 +317,10 @@ func leasesDNS(args []string) { } else { if cur != pid { fresh = true // a new Unbound has none of it + if pid != "" { + publishedPid = pid + } + published = stillThere(published, publishedPid, cur) } var now map[string]DynRecord if now, err = syncDynamic(ctl, published, want, fresh); err == nil { @@ -309,7 +328,7 @@ func leasesDNS(args []string) { } published = now } - status := dynStatus{Published: recordsOf(published), Skipped: skipped, Updated: time.Now()} + status := dynStatus{Pid: pid, Published: recordsOf(published), Skipped: skipped, Updated: time.Now()} if err != nil { status.Error = err.Error() if status.Error != lastErr { diff --git a/pkgs/router-webui/leasesdns_test.go b/pkgs/router-webui/leasesdns_test.go index ea8cca5..5ea38dc 100644 --- a/pkgs/router-webui/leasesdns_test.go +++ b/pkgs/router-webui/leasesdns_test.go @@ -150,6 +150,28 @@ func TestSyncDynamic(t *testing.T) { } } +// A client name that became a DHCP reservation: the restart that brought the +// reservation must not remove the configuration's record by removing what an +// earlier Unbound was given. +func TestNewUnboundForgetsWhatTheOldOneHad(t *testing.T) { + hv1 := DynRecord{Name: "hv1.home.example.org", Address: "192.168.1.21"} + old := map[string]DynRecord{hv1.Name: hv1} + if got := stillThere(old, "100", "200"); len(got) != 0 { + t.Errorf("a new Unbound still has %v", got) + } + if got := stillThere(old, "", "200"); len(got) != 0 { + t.Errorf("an unknown Unbound still has %v", got) + } + if got := stillThere(old, "200", "200"); len(got) != 1 { + t.Errorf("the same Unbound lost %v", got) + } + // So the fresh sync sends nothing for it: the static record stays. + ctl := &fakeCtl{} + if _, err := syncDynamic(ctl, stillThere(old, "100", "200"), nil, true); err != nil || len(ctl.cmds) != 0 { + t.Errorf("removed after a restart: %v %v", ctl.cmds, err) + } +} + func TestParseUnboundPID(t *testing.T) { out := "version: 1.26.1\nverbosity: 1\nthreads: 1\nuptime: 0 seconds\nunbound (pid 1458081) is running...\n" if pid, err := parseUnboundPID(out); err != nil || pid != "1458081" {