From 45c928e2f58e23a9daac1b92a11bbb33e5058871 Mon Sep 17 00:00:00 2001 From: Krishna Srinivas Date: Mon, 22 Aug 2016 22:50:01 +0530 Subject: [PATCH] boot: checkPortAvailability() should fail only for EADDRINUSE error and ignore other errors. (#2527) fixes #2510 --- cmd/checkport.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/cmd/checkport.go b/cmd/checkport.go index 0e354c592..c1fb9531c 100644 --- a/cmd/checkport.go +++ b/cmd/checkport.go @@ -19,6 +19,8 @@ package cmd import ( "fmt" "net" + "os" + "syscall" ) // Make sure that none of the other processes are listening on the @@ -35,7 +37,13 @@ func checkPortAvailability(port int) error { for _, n := range network { l, err := net.Listen(n, fmt.Sprintf(":%d", port)) if err != nil { - return err + if isAddrInUse(err) { + // Return error if another process is listening on the + // same port. + return err + } + // Ignore any other error (ex. EAFNOSUPPORT) + continue } // look for error so we don't have dangling connection @@ -46,3 +54,18 @@ func checkPortAvailability(port int) error { return nil } + +// Return true if err is "address already in use" error. +// syscall.EADDRINUSE is available on all OSes. +func isAddrInUse(err error) bool { + if opErr, ok := err.(*net.OpError); ok { + if sysErr, ok := opErr.Err.(*os.SyscallError); ok { + if errno, ok := sysErr.Err.(syscall.Errno); ok { + if errno == syscall.EADDRINUSE { + return true + } + } + } + } + return false +}