1
0
mirror of https://github.com/golang/go synced 2024-11-23 09:20:05 -07:00

net: support no-reload option for unix go resolver

It adds support for no-reload option, as specified in resolv.conf(5):
 no-reload (since glibc 2.26)
                     Sets RES_NORELOAD in _res.options.  This option
                     disables automatic reloading of a changed
                     configuration file.

Change-Id: I11182c5829434503f719ed162014f2301e3ba8d4
GitHub-Last-Rev: 7ae44be2d5
GitHub-Pull-Request: golang/go#56489
Reviewed-on: https://go-review.googlesource.com/c/go/+/446555
Reviewed-by: Damien Neil <dneil@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Mateusz Poliwczak 2022-11-01 08:53:10 +00:00 committed by Gopher Robot
parent e23876a383
commit 81efd7b347
4 changed files with 51 additions and 3 deletions

View File

@ -369,6 +369,10 @@ func (conf *resolverConfig) init() {
func (conf *resolverConfig) tryUpdate(name string) {
conf.initOnce.Do(conf.init)
if conf.dnsConfig.noReload {
return
}
// Ensure only one update at a time checks resolv.conf.
if !conf.tryAcquireSema() {
return

View File

@ -247,7 +247,7 @@ func newResolvConfTest() (*resolvConfTest, error) {
return conf, nil
}
func (conf *resolvConfTest) writeAndUpdate(lines []string) error {
func (conf *resolvConfTest) write(lines []string) error {
f, err := os.OpenFile(conf.path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return err
@ -257,10 +257,18 @@ func (conf *resolvConfTest) writeAndUpdate(lines []string) error {
return err
}
f.Close()
if err := conf.forceUpdate(conf.path, time.Now().Add(time.Hour)); err != nil {
return nil
}
func (conf *resolvConfTest) writeAndUpdate(lines []string) error {
return conf.writeAndUpdateWithLastCheckedTime(lines, time.Now().Add(time.Hour))
}
func (conf *resolvConfTest) writeAndUpdateWithLastCheckedTime(lines []string, lastChecked time.Time) error {
if err := conf.write(lines); err != nil {
return err
}
return nil
return conf.forceUpdate(conf.path, lastChecked)
}
func (conf *resolvConfTest) forceUpdate(name string, lastChecked time.Time) error {
@ -2409,3 +2417,36 @@ func TestDNSTrustAD(t *testing.T) {
t.Errorf("lookup failed: %v", err)
}
}
func TestDNSConfigNoReload(t *testing.T) {
r := &Resolver{PreferGo: true, Dial: func(ctx context.Context, network, address string) (Conn, error) {
if address != "192.0.2.1:53" {
return nil, errors.New("configuration unexpectedly changed")
}
return fakeDNSServerSuccessful.DialContext(ctx, network, address)
}}
conf, err := newResolvConfTest()
if err != nil {
t.Fatal(err)
}
defer conf.teardown()
err = conf.writeAndUpdateWithLastCheckedTime([]string{"nameserver 192.0.2.1", "options no-reload"}, time.Now().Add(-time.Hour))
if err != nil {
t.Fatal(err)
}
if _, err = r.LookupHost(context.Background(), "go.dev"); err != nil {
t.Fatal(err)
}
err = conf.write([]string{"nameserver 192.0.2.200"})
if err != nil {
t.Fatal(err)
}
if _, err = r.LookupHost(context.Background(), "go.dev"); err != nil {
t.Fatal(err)
}
}

View File

@ -30,6 +30,7 @@ type dnsConfig struct {
singleRequest bool // use sequential A and AAAA queries instead of parallel queries
useTCP bool // force usage of TCP for DNS resolutions
trustAD bool // add AD flag to queries
noReload bool // do not check for config file updates
}
// serverOffset returns an offset that can be used to determine

View File

@ -118,6 +118,8 @@ func dnsReadConfig(filename string) *dnsConfig {
case s == "edns0":
// We use EDNS by default.
// Ignore this option.
case s == "no-reload":
conf.noReload = true
default:
conf.unknownOpt = true
}