Skip to content

Commit

Permalink
Merge pull request #9029 from zhsj/sysctl
Browse files Browse the repository at this point in the history
sdk: read procfs instead of using sysctl command
  • Loading branch information
dnephin authored Jan 4, 2021
2 parents 85e5da5 + b931e98 commit d32e7f0
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions sdk/freeport/ephemeral_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}

0 comments on commit d32e7f0

Please sign in to comment.