From be5b5a5754cd4fb7440ff6872b2ca88a55cc47e0 Mon Sep 17 00:00:00 2001 From: WAKAYAMA shirou Date: Sat, 21 Feb 2015 22:43:58 +0900 Subject: [PATCH] move CallSysCall to common/common_freebsd --- common/common_freebsd.go | 41 +++++++++++++++++++++++++++++++++++++++++ process/process_freebsd.go | 43 ++----------------------------------------- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/common/common_freebsd.go b/common/common_freebsd.go index 7201082..3c11246 100644 --- a/common/common_freebsd.go +++ b/common/common_freebsd.go @@ -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 +} + diff --git a/process/process_freebsd.go b/process/process_freebsd.go index b3b5069..373aab5 100644 --- a/process/process_freebsd.go +++ b/process/process_freebsd.go @@ -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 }