process_darwin_nocgo.go 690 B

12345678910111213141516171819202122232425262728293031323334
  1. // +build darwin
  2. // +build !cgo
  3. package process
  4. import (
  5. "context"
  6. "fmt"
  7. "os/exec"
  8. "strconv"
  9. "strings"
  10. )
  11. func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
  12. lsof_bin, err := exec.LookPath("lsof")
  13. if err != nil {
  14. return "", err
  15. }
  16. out, err := invoke.CommandWithContext(ctx, lsof_bin, "-p", strconv.Itoa(int(p.Pid)), "-Fpfn")
  17. if err != nil {
  18. return "", fmt.Errorf("bad call to lsof: %s", err)
  19. }
  20. txtFound := 0
  21. lines := strings.Split(string(out), "\n")
  22. for i := 1; i < len(lines); i++ {
  23. if lines[i] == "ftxt" {
  24. txtFound++
  25. if txtFound == 2 {
  26. return lines[i-1][1:], nil
  27. }
  28. }
  29. }
  30. return "", fmt.Errorf("missing txt data returned by lsof")
  31. }