From: Mateusz Poliwczak Date: Thu, 11 May 2023 11:54:28 +0000 (+0000) Subject: net: clear /etc/hosts cache on fs.ErrNotExist and fs.ErrPermission errors X-Git-Tag: go1.21rc1~569 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7db6d8a29d1f9bc4265ff4eb77547c7aa5f8c87e;p=gostls13.git net: clear /etc/hosts cache on fs.ErrNotExist and fs.ErrPermission errors This was also the cause of my issues in CL 455275 Before: root@arch:~/aa# $(time sleep 5 && mv /etc/hosts /tmp/hosts) & [1] 2214 root@arch:~/aa# go run main.go [232.223.232.123] [232.223.232.123] [232.223.232.123] [232.223.232.123] [232.223.232.123] [232.223.232.123] (....) After: root@arch:~/aa# $(time sleep 5 && mv /etc/hosts /tmp/hosts) & [1] 2284 root@arch:~/aa# go run main.go [232.223.232.123] [232.223.232.123] [232.223.232.123] [232.223.232.123] [232.223.232.123] [] lookup sth on 127.0.0.53:53: server misbehaving [] lookup sth on 127.0.0.53:53: server misbehaving Change-Id: I3090fd8f3105db8c2d7c3bf5afe7b18ebca61cda GitHub-Last-Rev: cb0dac6448bbc337cd015ad4b4b3d1da3f14a561 GitHub-Pull-Request: golang/go#59963 Reviewed-on: https://go-review.googlesource.com/c/go/+/492555 TryBot-Result: Gopher Robot Run-TryBot: Ian Lance Taylor Run-TryBot: Mateusz Poliwczak Reviewed-by: Ian Lance Taylor Auto-Submit: Ian Lance Taylor Reviewed-by: Cherry Mui --- diff --git a/src/net/hosts.go b/src/net/hosts.go index 8b954188bf..56e6674144 100644 --- a/src/net/hosts.go +++ b/src/net/hosts.go @@ -5,7 +5,9 @@ package net import ( + "errors" "internal/bytealg" + "io/fs" "net/netip" "sync" "time" @@ -63,48 +65,54 @@ func readHosts() { hs := make(map[string]byName) is := make(map[string][]string) - var file *file - if file, _ = open(hp); file == nil { - return - } - for line, ok := file.readLine(); ok; line, ok = file.readLine() { - if i := bytealg.IndexByteString(line, '#'); i >= 0 { - // Discard comments. - line = line[0:i] - } - f := getFields(line) - if len(f) < 2 { - continue - } - addr := parseLiteralIP(f[0]) - if addr == "" { - continue + file, err := open(hp) + if err != nil { + if !errors.Is(err, fs.ErrNotExist) && !errors.Is(err, fs.ErrPermission) { + return } + } - var canonical string - for i := 1; i < len(f); i++ { - name := absDomainName(f[i]) - h := []byte(f[i]) - lowerASCIIBytes(h) - key := absDomainName(string(h)) - - if i == 1 { - canonical = key + if file != nil { + defer file.close() + for line, ok := file.readLine(); ok; line, ok = file.readLine() { + if i := bytealg.IndexByteString(line, '#'); i >= 0 { + // Discard comments. + line = line[0:i] + } + f := getFields(line) + if len(f) < 2 { + continue + } + addr := parseLiteralIP(f[0]) + if addr == "" { + continue } - is[addr] = append(is[addr], name) + var canonical string + for i := 1; i < len(f); i++ { + name := absDomainName(f[i]) + h := []byte(f[i]) + lowerASCIIBytes(h) + key := absDomainName(string(h)) - if v, ok := hs[key]; ok { - hs[key] = byName{ - addrs: append(v.addrs, addr), - canonicalName: v.canonicalName, + if i == 1 { + canonical = key } - continue - } - hs[key] = byName{ - addrs: []string{addr}, - canonicalName: canonical, + is[addr] = append(is[addr], name) + + if v, ok := hs[key]; ok { + hs[key] = byName{ + addrs: append(v.addrs, addr), + canonicalName: v.canonicalName, + } + continue + } + + hs[key] = byName{ + addrs: []string{addr}, + canonicalName: canonical, + } } } } @@ -115,7 +123,6 @@ func readHosts() { hosts.byAddr = is hosts.mtime = mtime hosts.size = size - file.close() } // lookupStaticHost looks up the addresses and the canonical name for the given host from /etc/hosts.