aboutsummaryrefslogtreecommitdiffstats
path: root/vm/vmimpl/vmimpl.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2018-06-11 16:41:02 +0200
committerDmitry Vyukov <dvyukov@google.com>2018-06-11 16:47:12 +0200
commit0f0e5db62d485e07105f5401349382692271cf31 (patch)
treec62ecbb723eb9014b5116b5f659e90c19bc1d0c0 /vm/vmimpl/vmimpl.go
parent112eec798fb57dea73b6a503e6c83c2438a6d45f (diff)
vm/adb: don't fail if port 35099 is busy
Diffstat (limited to 'vm/vmimpl/vmimpl.go')
-rw-r--r--vm/vmimpl/vmimpl.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/vm/vmimpl/vmimpl.go b/vm/vmimpl/vmimpl.go
index a4dba2f65..532611abd 100644
--- a/vm/vmimpl/vmimpl.go
+++ b/vm/vmimpl/vmimpl.go
@@ -11,6 +11,8 @@ import (
"errors"
"fmt"
"io"
+ "math/rand"
+ "net"
"os/exec"
"time"
@@ -137,3 +139,18 @@ func Multiplex(cmd *exec.Cmd, merger *OutputMerger, console io.Closer, timeout t
}()
return merger.Output, errc, nil
}
+
+func RandomPort() int {
+ return rand.Intn(64<<10-1<<10) + 1<<10
+}
+
+func UnusedTCPPort() int {
+ for {
+ port := RandomPort()
+ ln, err := net.Listen("tcp", fmt.Sprintf("localhost:%v", port))
+ if err == nil {
+ ln.Close()
+ return port
+ }
+ }
+}