Merge pull request #9029 from zhsj/sysctl

sdk: read procfs instead of using sysctl command
This commit is contained in:
Daniel Nephin 2021-01-04 14:13:33 -05:00 committed by GitHub
commit 66c2393de2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 5 deletions

View File

@ -4,18 +4,17 @@ package freeport
import (
"fmt"
"os/exec"
"io/ioutil"
"regexp"
"strconv"
)
const ephemeralPortRangeSysctlKey = "net.ipv4.ip_local_port_range"
const ephemeralPortRangeProcFile = "/proc/sys/net/ipv4/ip_local_port_range"
var ephemeralPortRangePatt = regexp.MustCompile(`^\s*(\d+)\s+(\d+)\s*$`)
func getEphemeralPortRange() (int, int, error) {
cmd := exec.Command("/sbin/sysctl", "-n", ephemeralPortRangeSysctlKey)
out, err := cmd.Output()
out, err := ioutil.ReadFile(ephemeralPortRangeProcFile)
if err != nil {
return 0, 0, err
}
@ -32,5 +31,5 @@ func getEphemeralPortRange() (int, int, error) {
}
}
return 0, 0, fmt.Errorf("unexpected sysctl value %q for key %q", val, ephemeralPortRangeSysctlKey)
return 0, 0, fmt.Errorf("unexpected sysctl value %q for key %q", val, ephemeralPortRangeProcFile)
}