Skip to content

Commit

Permalink
Revert procArgs to use C.sysctl instead of unix.SysctlRaw
Browse files Browse the repository at this point in the history
Done this in the hope of fixing a bug happening on CI but not on my system.
  • Loading branch information
PierreF committed Jan 13, 2022
1 parent c29859e commit 7271eee
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions process/process_darwin_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"unsafe"

"github.com/shirou/gopsutil/v3/cpu"
"golang.org/x/sys/unix"
)

var (
Expand Down Expand Up @@ -100,14 +99,21 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
}

func procArgs(pid int32) ([]byte, int, error) {
var nargs C.int

buffer, err := unix.SysctlRaw("kern.procargs2", int(pid))
if err == nil && len(buffer) > 0 {
C.memcpy(unsafe.Pointer(&nargs), unsafe.Pointer(&buffer[0]), C.sizeof_int)
return buffer, int(nargs), nil
var (
mib = [...]C.int{C.CTL_KERN, C.KERN_PROCARGS2, C.int(pid)}
size C.size_t = C.ulong(argMax)
nargs C.int
result []byte
)
procargs := (*C.char)(C.malloc(C.ulong(argMax)))
defer C.free(unsafe.Pointer(procargs))
retval, err := C.sysctl(&mib[0], 3, unsafe.Pointer(procargs), &size, C.NULL, 0)
if retval == 0 {
C.memcpy(unsafe.Pointer(&nargs), unsafe.Pointer(procargs), C.sizeof_int)
result = C.GoBytes(unsafe.Pointer(procargs), C.int(size))
// fmt.Printf("size: %d %d\n%s\n", size, nargs, hex.Dump(result))
return result, int(nargs), nil
}

return nil, 0, err
}

Expand Down

0 comments on commit 7271eee

Please sign in to comment.