1
0
mirror of https://github.com/golang/go synced 2024-11-18 05:44:47 -07:00

net: use baked-in port numbers as fallback if cgo port lookup fails

Fixes TestLookupPort_Minimal on android.

Fixes #18213

Change-Id: I1b65e790525d339a4cb7f17afe7e3a02c4587302
Reviewed-on: https://go-review.googlesource.com/34014
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Brad Fitzpatrick 2016-12-06 22:05:41 +00:00
parent 2641cffd41
commit 26aa7422e5
2 changed files with 9 additions and 8 deletions

View File

@ -76,13 +76,15 @@ func (r *Resolver) lookupIP(ctx context.Context, host string) (addrs []IPAddr, e
} }
func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int, error) { func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int, error) {
// TODO: use the context if there ever becomes a need. Related
// is issue 15321. But port lookup generally just involves
// local files, and the os package has no context support. The
// files might be on a remote filesystem, though. This should
// probably race goroutines if ctx != context.Background().
if !r.PreferGo && systemConf().canUseCgo() { if !r.PreferGo && systemConf().canUseCgo() {
if port, err, ok := cgoLookupPort(ctx, network, service); ok { if port, err, ok := cgoLookupPort(ctx, network, service); ok {
if err != nil {
// Issue 18213: if cgo fails, first check to see whether we
// have the answer baked-in to the net package.
if port, err := goLookupPort(network, service); err == nil {
return port, nil
}
}
return port, err return port, err
} }
} }

View File

@ -10,12 +10,11 @@ package net
import "sync" import "sync"
var servicesError error
var onceReadServices sync.Once var onceReadServices sync.Once
func readServices() { func readServices() {
var file *file file, err := open("/etc/services")
if file, servicesError = open("/etc/services"); servicesError != nil { if err != nil {
return return
} }
for line, ok := file.readLine(); ok; line, ok = file.readLine() { for line, ok := file.readLine(); ok; line, ok = file.readLine() {