2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2021-11-16 18:04:01 +00:00
|
|
|
//go:build linux
|
2019-08-27 21:16:41 +00:00
|
|
|
|
|
|
|
package freeport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-10 16:26:01 +00:00
|
|
|
"os"
|
2019-08-27 21:16:41 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2020-10-24 16:36:13 +00:00
|
|
|
const ephemeralPortRangeProcFile = "/proc/sys/net/ipv4/ip_local_port_range"
|
2019-08-27 21:16:41 +00:00
|
|
|
|
|
|
|
var ephemeralPortRangePatt = regexp.MustCompile(`^\s*(\d+)\s+(\d+)\s*$`)
|
|
|
|
|
|
|
|
func getEphemeralPortRange() (int, int, error) {
|
2022-11-10 16:26:01 +00:00
|
|
|
out, err := os.ReadFile(ephemeralPortRangeProcFile)
|
2019-08-27 21:16:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
val := string(out)
|
|
|
|
|
|
|
|
m := ephemeralPortRangePatt.FindStringSubmatch(val)
|
|
|
|
if m != nil {
|
|
|
|
min, err1 := strconv.Atoi(m[1])
|
|
|
|
max, err2 := strconv.Atoi(m[2])
|
|
|
|
|
|
|
|
if err1 == nil && err2 == nil {
|
|
|
|
return min, max, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 16:36:13 +00:00
|
|
|
return 0, 0, fmt.Errorf("unexpected sysctl value %q for key %q", val, ephemeralPortRangeProcFile)
|
2019-08-27 21:16:41 +00:00
|
|
|
}
|