diff --git a/client/internal/dns/file_linux.go b/client/internal/dns/file_linux.go index 7755e2910de..acb31b7f03c 100644 --- a/client/internal/dns/file_linux.go +++ b/client/internal/dns/file_linux.go @@ -30,7 +30,7 @@ type fileConfigurator struct { func newFileConfigurator() (hostManager, error) { fc := &fileConfigurator{} - fc.repair = newRepair(fc.updateConfig) + fc.repair = newRepair(defaultResolvConfPath, fc.updateConfig) return fc, nil } diff --git a/client/internal/dns/file_repair_linux.go b/client/internal/dns/file_repair_linux.go index d4f91be5c41..60fe91a2285 100644 --- a/client/internal/dns/file_repair_linux.go +++ b/client/internal/dns/file_repair_linux.go @@ -2,6 +2,7 @@ package dns import ( "errors" + "fmt" "os" "sync" @@ -22,15 +23,17 @@ const ( type repairConfFn func([]string, string, *resolvConf) error type repair struct { - updateFn repairConfFn + operationFile string + updateFn repairConfFn inotify *gonotify.Inotify inotifyWg sync.WaitGroup } -func newRepair(updateFn repairConfFn) *repair { +func newRepair(operationFile string, updateFn repairConfFn) *repair { return &repair{ - updateFn: updateFn, + operationFile: operationFile, + updateFn: updateFn, } } @@ -39,6 +42,7 @@ func (f *repair) watchFileChanges(nbSearchDomains []string, nbNameserverIP strin return } + log.Infof("start to watch resolv.conf") inotify, err := gonotify.NewInotify() if err != nil { log.Errorf("failed to start inotify watcher for resolv.conf: %s", err) @@ -66,13 +70,13 @@ func (f *repair) watchFileChanges(nbSearchDomains []string, nbNameserverIP strin return } - if !isEventRelevant(events) { + if !f.isEventRelevant(events) { continue } - log.Debugf("resolv.conf changed, check if it is broken") + log.Tracef("resolv.conf changed, check if it is broken") - rConf, err := parseDefaultResolvConf() + rConf, err := parseResolvConfFile(f.operationFile) if err != nil { log.Warnf("failed to parse resolv conf: %s", err) continue @@ -80,7 +84,7 @@ func (f *repair) watchFileChanges(nbSearchDomains []string, nbNameserverIP strin log.Debugf("check resolv.conf parameters: %s", rConf) if !isNbParamsMissing(nbSearchDomains, nbNameserverIP, rConf) { - log.Debugf("resolv.conf still correct, skip the update") + log.Tracef("resolv.conf still correct, skip the update") continue } log.Info("broken params in resolv.conf, repair it...") @@ -102,7 +106,6 @@ func (f *repair) watchFileChanges(nbSearchDomains []string, nbNameserverIP strin } } }() - return } func (f *repair) stopWatchFileChanges() { @@ -117,6 +120,16 @@ func (f *repair) stopWatchFileChanges() { f.inotify = nil } +func (f *repair) isEventRelevant(events []gonotify.InotifyEvent) bool { + operatioFileSymlink := fmt.Sprintf("%s~", f.operationFile) + for _, ev := range events { + if ev.Name == f.operationFile || ev.Name == operatioFileSymlink { + return true + } + } + return false +} + // nbParamsAreMissing checks if the resolv.conf file contains all the parameters that NetBird needs // check the NetBird related nameserver IP at the first place // check the NetBird related search domains in the search domains list @@ -135,12 +148,3 @@ func isNbParamsMissing(nbSearchDomains []string, nbNameserverIP string, rConf *r return false } - -func isEventRelevant(events []gonotify.InotifyEvent) bool { - for _, ev := range events { - if ev.Name == defaultResolvConfPath { - return true - } - } - return false -}