move CallSysCall to common/common_freebsd

pull/43/merge
WAKAYAMA shirou 10 years ago
parent bb1747fc85
commit be5b5a5754

@ -3,8 +3,10 @@
package common
import (
"syscall"
"os/exec"
"strings"
"unsafe"
)
func DoSysctrl(mib string) ([]string, error) {
@ -18,3 +20,42 @@ func DoSysctrl(mib string) ([]string, error) {
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
}

@ -5,7 +5,6 @@ package process
import (
"bytes"
"encoding/binary"
"syscall"
"unsafe"
common "github.com/shirou/gopsutil/common"
@ -200,7 +199,7 @@ func processes() ([]Process, error) {
results := make([]Process, 0, 50)
mib := []int32{CTLKern, KernProc, KernProcProc, 0}
buf, length, err := callSyscall(mib)
buf, length, err := common.CallSyscall(mib)
if err != nil {
return results, err
}
@ -240,48 +239,10 @@ func parseKinfoProc(buf []byte) (KinfoProc, error) {
return k, 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
}
func (p *Process) getKProc() (*KinfoProc, error) {
mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid}
buf, length, err := callSyscall(mib)
buf, length, err := common.CallSyscall(mib)
if err != nil {
return nil, err
}

Loading…
Cancel
Save