Skip to content

Commit c800222

Browse files
ncopaodeke-em
authored andcommitted
net: prefer /etc/hosts over DNS when no /etc/nsswitch.conf is present
Do not mimic glibc behavior if /etc/nsswitch.conf is missing. This will will likely be missing on musl libc systems and glibc systems will likely always have it, resulting in localhost lookup being done over DNS rather than from /etc/hosts. Do what makes most sense rather than making any assumption about the libc. Fixes #35305 Change-Id: I20bd7e24131bba8eaa39a20c8950fe552364784d GitHub-Last-Rev: 1194098 GitHub-Pull-Request: #39685 Reviewed-on: https://go-review.googlesource.com/c/go/+/238629 Run-TryBot: Dan Peterson <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Dan Peterson <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Trust: Emmanuel Odeke <[email protected]>
1 parent 9488741 commit c800222

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/net/conf.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,6 @@ func (c *conf) hostLookupOrder(r *Resolver, hostname string) (ret hostLookupOrde
202202
// illumos defaults to "nis [NOTFOUND=return] files"
203203
return fallbackOrder
204204
}
205-
if c.goos == "linux" {
206-
// glibc says the default is "dns [!UNAVAIL=return] files"
207-
// https://www.gnu.org/software/libc/manual/html_node/Notes-on-NSS-Configuration-File.html.
208-
return hostLookupDNSFiles
209-
}
210205
return hostLookupFilesDNS
211206
}
212207
if nss.err != nil {

src/net/conf_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,23 @@ func TestConfHostLookupOrder(t *testing.T) {
170170
},
171171
hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupDNSFiles}},
172172
},
173-
// glibc lacking an nsswitch.conf, per
174-
// https://www.gnu.org/software/libc/manual/html_node/Notes-on-NSS-Configuration-File.html
175173
{
176174
name: "linux_no_nsswitch.conf",
177175
c: &conf{
178176
goos: "linux",
179177
nss: &nssConf{err: fs.ErrNotExist},
180178
resolv: defaultResolvConf,
181179
},
182-
hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupDNSFiles}},
180+
hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFilesDNS}},
181+
},
182+
{
183+
name: "linux_empty_nsswitch.conf",
184+
c: &conf{
185+
goos: "linux",
186+
nss: nssStr(""),
187+
resolv: defaultResolvConf,
188+
},
189+
hostTests: []nssHostTest{{"google.com", "myhostname", hostLookupFilesDNS}},
183190
},
184191
{
185192
name: "files_mdns_dns",

0 commit comments

Comments
 (0)