...

Source file src/github.com/shirou/gopsutil/process/process_darwin_nocgo.go

Documentation: github.com/shirou/gopsutil/process

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

View as plain text