2014-04-22 10:58:03 +08:00
|
|
|
// +build freebsd
|
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package common
|
2014-04-22 10:58:03 +08:00
|
|
|
|
|
|
|
import (
|
2016-02-17 15:29:35 +08:00
|
|
|
"os"
|
2014-04-22 10:58:03 +08:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2015-10-19 11:40:01 +08:00
|
|
|
"syscall"
|
2015-02-21 21:43:58 +08:00
|
|
|
"unsafe"
|
2014-04-22 10:58:03 +08:00
|
|
|
)
|
|
|
|
|
2014-11-27 09:18:15 +08:00
|
|
|
func DoSysctrl(mib string) ([]string, error) {
|
2016-04-01 20:02:20 +08:00
|
|
|
err := os.Setenv("LC_ALL", "C")
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
2016-04-01 21:13:05 +08:00
|
|
|
sysctl, err := exec.LookPath("/sbin/sysctl")
|
|
|
|
if err != nil {
|
2016-04-01 21:22:03 +08:00
|
|
|
return []string{}, err
|
2016-04-01 21:13:05 +08:00
|
|
|
}
|
|
|
|
out, err := exec.Command(sysctl, "-n", mib).Output()
|
2014-04-22 10:58:03 +08: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
|
|
|
|
}
|
2015-02-21 21:43:58 +08:00
|
|
|
|
|
|
|
func CallSyscall(mib []int32) ([]byte, uint64, error) {
|
2016-02-05 05:29:15 +08:00
|
|
|
mibptr := unsafe.Pointer(&mib[0])
|
2015-02-21 21:43:58 +08:00
|
|
|
miblen := uint64(len(mib))
|
|
|
|
|
|
|
|
// get required buffer size
|
|
|
|
length := uint64(0)
|
|
|
|
_, _, err := syscall.Syscall6(
|
|
|
|
syscall.SYS___SYSCTL,
|
2016-02-05 05:29:15 +08:00
|
|
|
uintptr(mibptr),
|
2015-02-21 21:43:58 +08:00
|
|
|
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)
|
|
|
|
_, _, err = syscall.Syscall6(
|
|
|
|
syscall.SYS___SYSCTL,
|
2016-02-05 05:29:15 +08:00
|
|
|
uintptr(mibptr),
|
2015-02-21 21:43:58 +08:00
|
|
|
uintptr(miblen),
|
|
|
|
uintptr(unsafe.Pointer(&buf[0])),
|
|
|
|
uintptr(unsafe.Pointer(&length)),
|
|
|
|
0,
|
|
|
|
0)
|
|
|
|
if err != 0 {
|
|
|
|
return buf, length, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, length, nil
|
|
|
|
}
|