github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/system/process/process_darwin_cgo.go (about)

     1  //go:build darwin && cgo
     2  
     3  package process
     4  
     5  // #include <stdlib.h>
     6  // #include <libproc.h>
     7  import "C"
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"unsafe"
    12  )
    13  
    14  func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
    15  	var c C.char // need a var for unsafe.Sizeof need a var
    16  	const bufsize = C.PROC_PIDPATHINFO_MAXSIZE * unsafe.Sizeof(c)
    17  	buffer := (*C.char)(C.malloc(C.size_t(bufsize)))
    18  	defer C.free(unsafe.Pointer(buffer))
    19  
    20  	ret, err := C.proc_pidpath(C.int(p.Pid), unsafe.Pointer(buffer), C.uint32_t(bufsize))
    21  	if err != nil {
    22  		return "", err
    23  	}
    24  	if ret <= 0 {
    25  		return "", fmt.Errorf("unknown error: proc_pidpath returned %d", int(ret))
    26  	}
    27  
    28  	return C.GoString(buffer), nil
    29  }