]> Cypherpunks repositories - gostls13.git/commitdiff
net: use baked-in port numbers as fallback if cgo port lookup fails
authorBrad Fitzpatrick <bradfitz@golang.org>
Tue, 6 Dec 2016 22:05:41 +0000 (22:05 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 7 Dec 2016 16:02:23 +0000 (16:02 +0000)
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>
src/net/lookup_unix.go
src/net/port_unix.go

index 35f253c1da24a7e72a2cdb6f396484f3c70f2ff8..609adbfd9bcfd8284b7dd06b3890bc2556a000e9 100644 (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) {
-       // 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 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
                }
        }
index 4e0478194e57bb4e650f99dfd2a96694d06c7da9..868d1e4784f2110e4e152a5d5e2a49f0d5bb5e01 100644 (file)
@@ -10,12 +10,11 @@ package net
 
 import "sync"
 
-var servicesError error
 var onceReadServices sync.Once
 
 func readServices() {
-       var file *file
-       if file, servicesError = open("/etc/services"); servicesError != nil {
+       file, err := open("/etc/services")
+       if err != nil {
                return
        }
        for line, ok := file.readLine(); ok; line, ok = file.readLine() {