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

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