2016-02-12 18:02:16 +00:00
|
|
|
// +build darwin
|
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2020-03-15 08:36:59 +00:00
|
|
|
"context"
|
2016-05-09 15:19:04 +00:00
|
|
|
"os"
|
2016-02-12 18:02:16 +00:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
2017-07-28 16:46:44 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2016-02-12 18:02:16 +00:00
|
|
|
)
|
|
|
|
|
2020-03-15 08:36:59 +00:00
|
|
|
func DoSysctrlWithContext(ctx context.Context, mib string) ([]string, error) {
|
|
|
|
sysctl, err := exec.LookPath("sysctl")
|
2016-05-09 15:19:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
2020-03-15 08:36:59 +00:00
|
|
|
cmd := exec.CommandContext(ctx, sysctl, "-n", mib)
|
2017-08-17 23:52:53 +00:00
|
|
|
cmd.Env = getSysctrlEnv(os.Environ())
|
|
|
|
out, err := cmd.Output()
|
2016-02-12 18:02:16 +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) {
|
|
|
|
miblen := uint64(len(mib))
|
|
|
|
|
|
|
|
// get required buffer size
|
|
|
|
length := uint64(0)
|
2017-07-28 16:46:44 +00:00
|
|
|
_, _, err := unix.Syscall6(
|
|
|
|
unix.SYS___SYSCTL,
|
2016-02-12 18:02:16 +00:00
|
|
|
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)
|
2017-07-28 16:46:44 +00:00
|
|
|
_, _, err = unix.Syscall6(
|
|
|
|
unix.SYS___SYSCTL,
|
2016-02-12 18:02:16 +00:00
|
|
|
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
|
|
|
|
}
|