Skip to content

Commit

Permalink
Fix symlink watch
Browse files Browse the repository at this point in the history
  • Loading branch information
pappz committed Jan 23, 2024
1 parent 838632a commit f41a752
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion client/internal/dns/file_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
38 changes: 21 additions & 17 deletions client/internal/dns/file_repair_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dns

import (
"errors"
"fmt"
"os"
"sync"

Expand All @@ -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,
}
}

Expand All @@ -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)
Expand Down Expand Up @@ -66,21 +70,21 @@ 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
}

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...")
Expand All @@ -102,7 +106,6 @@ func (f *repair) watchFileChanges(nbSearchDomains []string, nbNameserverIP strin
}
}
}()
return
}

func (f *repair) stopWatchFileChanges() {
Expand All @@ -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
Expand All @@ -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
}

0 comments on commit f41a752

Please sign in to comment.