From 7271eee3746d78cbb724c78267200ed0d93d0721 Mon Sep 17 00:00:00 2001 From: Pierre Fersing Date: Thu, 13 Jan 2022 19:28:06 +0100 Subject: [PATCH] Revert procArgs to use C.sysctl instead of unix.SysctlRaw Done this in the hope of fixing a bug happening on CI but not on my system. --- process/process_darwin_cgo.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/process/process_darwin_cgo.go b/process/process_darwin_cgo.go index 830652d77..d79a4d4fe 100644 --- a/process/process_darwin_cgo.go +++ b/process/process_darwin_cgo.go @@ -22,7 +22,6 @@ import ( "unsafe" "github.com/shirou/gopsutil/v3/cpu" - "golang.org/x/sys/unix" ) var ( @@ -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 }