]> Cypherpunks repositories - gostls13.git/commitdiff
net: clear /etc/hosts cache on fs.ErrNotExist and fs.ErrPermission errors
authorMateusz Poliwczak <mpoliwczak34@gmail.com>
Thu, 11 May 2023 11:54:28 +0000 (11:54 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 11 May 2023 23:47:00 +0000 (23:47 +0000)
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>
src/net/hosts.go

index 8b954188bf12d2e1d383114552307998bf5908dc..56e667414446507818a591c6a02e11b3bec28755 100644 (file)
@@ -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.