process_darwin_cgo.go 677 B

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