71 lines
1.3 KiB
Go
71 lines
1.3 KiB
Go
// +build darwin
|
|
|
|
package common
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"syscall"
|
|
"unsafe"
|
|
)
|
|
|
|
func DoSysctrl(mib string) ([]string, error) {
|
|
err := os.Setenv("LC_ALL", "C")
|
|
if err != nil {
|
|
return []string{}, err
|
|
}
|
|
|
|
sysctl, err := exec.LookPath("/usr/bin/getconf")
|
|
if err != nil {
|
|
return
|
|
}
|
|
out, err := exec.Command(sysctl, "-n", mib).Output()
|
|
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) {
|
|
miblen := uint64(len(mib))
|
|
|
|
// get required buffer size
|
|
length := uint64(0)
|
|
_, _, err := syscall.Syscall6(
|
|
syscall.SYS___SYSCTL,
|
|
uintptr(unsafe.Pointer(&mib[0])),
|
|
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,
|
|
uintptr(unsafe.Pointer(&mib[0])),
|
|
uintptr(miblen),
|
|
uintptr(unsafe.Pointer(&buf[0])),
|
|
uintptr(unsafe.Pointer(&length)),
|
|
0,
|
|
0)
|
|
if err != 0 {
|
|
return buf, length, err
|
|
}
|
|
|
|
return buf, length, nil
|
|
}
|