From b931e989b8a2b2d3905733db8927e1a4493bc9d0 Mon Sep 17 00:00:00 2001 From: Shengjing Zhu Date: Sun, 25 Oct 2020 00:36:13 +0800 Subject: [PATCH] sdk: read procfs instead of using sysctl command This drops the dependency of sysctl command --- sdk/freeport/ephemeral_linux.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/sdk/freeport/ephemeral_linux.go b/sdk/freeport/ephemeral_linux.go index c93765feaa..88cc62a886 100644 --- a/sdk/freeport/ephemeral_linux.go +++ b/sdk/freeport/ephemeral_linux.go @@ -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) }