2021-12-23 05:54:41 +08:00
|
|
|
//go:build openbsd
|
2016-11-22 06:18:52 +08:00
|
|
|
// +build openbsd
|
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
2017-06-03 04:51:00 +08:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2016-11-22 06:18:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func DoSysctrl(mib string) ([]string, error) {
|
2022-03-05 00:18:03 +08:00
|
|
|
cmd := exec.Command("sysctl", "-n", mib)
|
2017-08-17 06:32:21 +08:00
|
|
|
cmd.Env = getSysctrlEnv(os.Environ())
|
|
|
|
out, err := cmd.Output()
|
2016-11-22 06:18:52 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
func CallSyscall(mib []int32) ([]byte, uint64, error) {
|
|
|
|
mibptr := unsafe.Pointer(&mib[0])
|
|
|
|
miblen := uint64(len(mib))
|
|
|
|
|
|
|
|
// get required buffer size
|
|
|
|
length := uint64(0)
|
2017-06-03 04:51:00 +08:00
|
|
|
_, _, err := unix.Syscall6(
|
|
|
|
unix.SYS___SYSCTL,
|
2016-11-22 06:18:52 +08: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)
|
2017-06-03 04:51:00 +08:00
|
|
|
_, _, err = unix.Syscall6(
|
|
|
|
unix.SYS___SYSCTL,
|
2016-11-22 06:18:52 +08: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
|
|
|
|
}
|