Skip to content

Commit

Permalink
[process][darwin] Fix shirou#925 properly with unix.SysctlRaw("kern.p…
Browse files Browse the repository at this point in the history
…roc.pid", PID)
  • Loading branch information
Lomanic committed Sep 29, 2020
1 parent 8ece829 commit c9c4021
Showing 1 changed file with 3 additions and 19 deletions.
22 changes: 3 additions & 19 deletions process/process_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strconv"
"strings"
"time"
"unsafe"

"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common"
Expand Down Expand Up @@ -602,24 +601,9 @@ func parseKinfoProc(buf []byte) (KinfoProc, error) {
// Returns a proc as defined here:
// http://unix.superglobalmegacorp.com/Net2/newsrc/sys/kinfo_proc.h.html
func (p *Process) getKProc() (*KinfoProc, error) {
return p.getKProcWithContext(context.Background())
}

func (p *Process) getKProcWithContext(ctx context.Context) (*KinfoProc, error) {
mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid}
procK := KinfoProc{}
length := uint64(unsafe.Sizeof(procK))
buf := make([]byte, length)
_, _, syserr := unix.Syscall6(
202, // unix.SYS___SYSCTL https://github.com/golang/sys/blob/76b94024e4b621e672466e8db3d7f084e7ddcad2/unix/zsysnum_darwin_amd64.go#L146
uintptr(unsafe.Pointer(&mib[0])),
uintptr(len(mib)),
uintptr(unsafe.Pointer(&buf[0])),
uintptr(unsafe.Pointer(&length)),
0,
0)
if syserr != 0 {
return nil, syserr
buf, err := unix.SysctlRaw("kern.proc.pid", int(p.Pid))
if err != nil {
return nil, err
}
k, err := parseKinfoProc(buf)
if err != nil {
Expand Down

0 comments on commit c9c4021

Please sign in to comment.