diff options
| author | Mateusz Poliwczak <mpoliwczak34@gmail.com> | 2023-05-11 11:54:28 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-05-11 23:47:00 +0000 |
| commit | 7db6d8a29d1f9bc4265ff4eb77547c7aa5f8c87e (patch) | |
| tree | c1bb73cf5ad0effc5b47a1e7a9c4ebb5da1edcf8 /src | |
| parent | 42023d6f529239680773eab5343eedf641e78bb3 (diff) | |
| download | go-7db6d8a29d1f9bc4265ff4eb77547c7aa5f8c87e.tar.xz | |
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] <nil>
[232.223.232.123] <nil>
[232.223.232.123] <nil>
[232.223.232.123] <nil>
[232.223.232.123] <nil>
[232.223.232.123] <nil>
(....)
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] <nil>
[232.223.232.123] <nil>
[232.223.232.123] <nil>
[232.223.232.123] <nil>
[232.223.232.123] <nil>
[] 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 <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: Mateusz Poliwczak <mpoliwczak34@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/net/hosts.go | 79 |
1 files changed, 43 insertions, 36 deletions
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. |
