From 48cb271a46c7afcb3c3e592de10638a255a9c157 Mon Sep 17 00:00:00 2001 From: Kanagaraj M Date: Tue, 25 Jun 2019 03:32:40 +0530 Subject: [PATCH] include ip address while doing checkPortAvailability (#7818) While checking for port availability, ip address should be included. When a machine has multiple ip addresses, multiple minio instances or some other applications can be run on same port but different ip address. Fixes #7685 --- cmd/gateway-main.go | 2 +- cmd/net.go | 6 +++--- cmd/net_test.go | 8 +++++--- cmd/server-main.go | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cmd/gateway-main.go b/cmd/gateway-main.go index c137fbdc4..588d43580 100644 --- a/cmd/gateway-main.go +++ b/cmd/gateway-main.go @@ -121,7 +121,7 @@ func StartGateway(ctx *cli.Context, gw Gateway) { // to IPv6 address ie minio will start listening on IPv6 address whereas another // (non-)minio process is listening on IPv4 of given port. // To avoid this error situation we check for port availability. - logger.FatalIf(checkPortAvailability(globalMinioPort), "Unable to start the gateway") + logger.FatalIf(checkPortAvailability(globalMinioHost, globalMinioPort), "Unable to start the gateway") // Check and load TLS certificates. var err error diff --git a/cmd/net.go b/cmd/net.go index df070ea6f..f343ba7e9 100644 --- a/cmd/net.go +++ b/cmd/net.go @@ -190,10 +190,10 @@ func isHostIP(ipAddress string) bool { return net.ParseIP(host) != nil } -// checkPortAvailability - check if given port is already in use. +// checkPortAvailability - check if given host and port is already in use. // Note: The check method tries to listen on given port and closes it. // It is possible to have a disconnected client in this tiny window of time. -func checkPortAvailability(port string) (err error) { +func checkPortAvailability(host, port string) (err error) { // Return true if err is "address already in use" error. isAddrInUseErr := func(err error) (b bool) { if opErr, ok := err.(*net.OpError); ok { @@ -209,7 +209,7 @@ func checkPortAvailability(port string) (err error) { network := []string{"tcp", "tcp4", "tcp6"} for _, n := range network { - l, err := net.Listen(n, net.JoinHostPort("", port)) + l, err := net.Listen(n, net.JoinHostPort(host, port)) if err == nil { // As we are able to listen on this network, the port is not in use. // Close the listener and continue check other networks. diff --git a/cmd/net_test.go b/cmd/net_test.go index f60120838..ecbfc94e1 100644 --- a/cmd/net_test.go +++ b/cmd/net_test.go @@ -206,11 +206,13 @@ func TestCheckPortAvailability(t *testing.T) { defer listener.Close() testCases := []struct { + host string port string expectedErr error }{ - {port, fmt.Errorf("listen tcp :%v: bind: address already in use", port)}, - {getFreePort(), nil}, + {"", port, fmt.Errorf("listen tcp :%v: bind: address already in use", port)}, + {"127.0.0.1", port, fmt.Errorf("listen tcp 127.0.0.1:%v: bind: address already in use", port)}, + {"", getFreePort(), nil}, } for _, testCase := range testCases { @@ -219,7 +221,7 @@ func TestCheckPortAvailability(t *testing.T) { continue } - err := checkPortAvailability(testCase.port) + err := checkPortAvailability(testCase.host, testCase.port) if testCase.expectedErr == nil { if err != nil { t.Fatalf("error: expected = , got = %v", err) diff --git a/cmd/server-main.go b/cmd/server-main.go index 6409a7c22..d963ab012 100644 --- a/cmd/server-main.go +++ b/cmd/server-main.go @@ -176,7 +176,7 @@ func serverHandleCmdArgs(ctx *cli.Context) { // to IPv6 address ie minio will start listening on IPv6 address whereas another // (non-)minio process is listening on IPv4 of given port. // To avoid this error sutiation we check for port availability. - logger.FatalIf(checkPortAvailability(globalMinioPort), "Unable to start the server") + logger.FatalIf(checkPortAvailability(globalMinioHost, globalMinioPort), "Unable to start the server") globalIsXL = (setupType == XLSetupType) globalIsDistXL = (setupType == DistXLSetupType)