2017-02-01 18:26:00 +00:00
|
|
|
// +build freebsd openbsd
|
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2020-07-01 12:47:56 +00:00
|
|
|
"fmt"
|
2017-02-01 18:26:00 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
2018-10-19 18:33:23 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2017-02-01 18:26:00 +00:00
|
|
|
)
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
func SysctlUint(mib string) (uint64, error) {
|
|
|
|
buf, err := unix.SysctlRaw(mib)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if len(buf) == 8 { // 64 bit
|
|
|
|
return *(*uint64)(unsafe.Pointer(&buf[0])), nil
|
|
|
|
}
|
|
|
|
if len(buf) == 4 { // 32bit
|
|
|
|
t := *(*uint32)(unsafe.Pointer(&buf[0]))
|
|
|
|
return uint64(t), nil
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("unexpected size: %s, %d", mib, len(buf))
|
|
|
|
}
|
|
|
|
|
2017-02-01 18:26:00 +00:00
|
|
|
func DoSysctrl(mib string) ([]string, error) {
|
2020-07-01 12:47:56 +00:00
|
|
|
sysctl, err := exec.LookPath("sysctl")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
cmd := exec.Command(sysctl, "-n", mib)
|
|
|
|
cmd.Env = getSysctrlEnv(os.Environ())
|
|
|
|
out, err := cmd.Output()
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
v := strings.Replace(string(out), "{ ", "", 1)
|
|
|
|
v = strings.Replace(string(v), " }", "", 1)
|
|
|
|
values := strings.Fields(string(v))
|
|
|
|
|
|
|
|
return values, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CallSyscall(mib []int32) ([]byte, uint64, error) {
|
|
|
|
mibptr := unsafe.Pointer(&mib[0])
|
|
|
|
miblen := uint64(len(mib))
|
|
|
|
|
|
|
|
// get required buffer size
|
|
|
|
length := uint64(0)
|
2018-10-19 18:33:23 +00:00
|
|
|
_, _, err := unix.Syscall6(
|
|
|
|
unix.SYS___SYSCTL,
|
2017-02-01 18:26:00 +00:00
|
|
|
uintptr(mibptr),
|
|
|
|
uintptr(miblen),
|
|
|
|
0,
|
|
|
|
uintptr(unsafe.Pointer(&length)),
|
|
|
|
0,
|
|
|
|
0)
|
|
|
|
if err != 0 {
|
|
|
|
var b []byte
|
|
|
|
return b, length, err
|
|
|
|
}
|
|
|
|
if length == 0 {
|
|
|
|
var b []byte
|
|
|
|
return b, length, err
|
|
|
|
}
|
|
|
|
// get proc info itself
|
|
|
|
buf := make([]byte, length)
|
2018-10-19 18:33:23 +00:00
|
|
|
_, _, err = unix.Syscall6(
|
|
|
|
unix.SYS___SYSCTL,
|
2017-02-01 18:26:00 +00:00
|
|
|
uintptr(mibptr),
|
|
|
|
uintptr(miblen),
|
|
|
|
uintptr(unsafe.Pointer(&buf[0])),
|
|
|
|
uintptr(unsafe.Pointer(&length)),
|
|
|
|
0,
|
|
|
|
0)
|
|
|
|
if err != 0 {
|
|
|
|
return buf, length, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, length, nil
|
|
|
|
}
|