Commit bf42f188 authored by Tobias Klauser's avatar Tobias Klauser Committed by Brad Fitzpatrick

unix: use sort.Search for sysctl lookup on OpenBSD

Use sort.Search to look up sysctl mib values from names instead of using
a hand-crafted binary search.

Also add a simple test to verify sysctl is still working after the
change.

Change-Id: I844840ac434a704b14c3166c3730daffabd2f347
Reviewed-on: https://go-review.googlesource.com/76930
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 665f6529
......@@ -60,3 +60,11 @@ func TestSysctlRaw(t *testing.T) {
t.Fatal(err)
}
}
func TestSysctlUint32(t *testing.T) {
maxproc, err := unix.SysctlUint32("kern.maxproc")
if err != nil {
t.Fatal(err)
}
t.Logf("kern.maxproc: %v", maxproc)
}
......@@ -13,6 +13,7 @@
package unix
import (
"sort"
"syscall"
"unsafe"
)
......@@ -32,22 +33,11 @@ type SockaddrDatalink struct {
func Syscall9(trap, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno)
func nametomib(name string) (mib []_C_int, err error) {
// Perform lookup via a binary search
left := 0
right := len(sysctlMib) - 1
for {
idx := left + (right-left)/2
switch {
case name == sysctlMib[idx].ctlname:
return sysctlMib[idx].ctloid, nil
case name > sysctlMib[idx].ctlname:
left = idx + 1
default:
right = idx - 1
}
if left > right {
break
}
i := sort.Search(len(sysctlMib), func(i int) bool {
return sysctlMib[i].ctlname >= name
})
if i < len(sysctlMib) && sysctlMib[i].ctlname == name {
return sysctlMib[i].ctloid, nil
}
return nil, EINVAL
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment