Merge pull request #271 from botherder/master

Obtain executable path from macOS process (fix #270)
This commit is contained in:
shirou 2017-01-02 14:11:51 +09:00 committed by GitHub
commit 93564b3142
2 changed files with 71 additions and 1 deletions

View File

@ -341,3 +341,46 @@ func WaitTimeout(c *exec.Cmd, timeout time.Duration) error {
return ErrTimeout
}
}
// https://gist.github.com/kylelemons/1525278
func Pipeline(cmds ...*exec.Cmd) ([]byte, []byte, error) {
// Require at least one command
if len(cmds) < 1 {
return nil, nil, nil
}
// Collect the output from the command(s)
var output bytes.Buffer
var stderr bytes.Buffer
last := len(cmds) - 1
for i, cmd := range cmds[:last] {
var err error
// Connect each command's stdin to the previous command's stdout
if cmds[i+1].Stdin, err = cmd.StdoutPipe(); err != nil {
return nil, nil, err
}
// Connect each command's stderr to a buffer
cmd.Stderr = &stderr
}
// Connect the output and error for the last command
cmds[last].Stdout, cmds[last].Stderr = &output, &stderr
// Start each command
for _, cmd := range cmds {
if err := cmd.Start(); err != nil {
return output.Bytes(), stderr.Bytes(), err
}
}
// Wait for each command to complete
for _, cmd := range cmds {
if err := cmd.Wait(); err != nil {
return output.Bytes(), stderr.Bytes(), err
}
}
// Return the pipeline output and the collected standard error
return output.Bytes(), stderr.Bytes(), nil
}

View File

@ -80,7 +80,34 @@ func (p *Process) Name() (string, error) {
return common.IntToString(k.Proc.P_comm[:]), nil
}
func (p *Process) Exe() (string, error) {
return "", common.ErrNotImplementedError
lsof_bin, err := exec.LookPath("lsof")
if err != nil {
return "", err
}
awk_bin, err := exec.LookPath("awk")
if err != nil {
return "", err
}
sed_bin, err := exec.LookPath("sed")
if err != nil {
return "", err
}
lsof := exec.Command(lsof_bin, "-p", strconv.Itoa(int(p.Pid)), "-Fn")
awk := exec.Command(awk_bin, "NR==3{print}")
sed := exec.Command(sed_bin, "s/n\\//\\//")
output, _, err := common.Pipeline(lsof, awk, sed)
if err != nil {
return "", err
}
ret := strings.TrimSpace(string(output))
return ret, nil
}
// Cmdline returns the command line arguments of the process as a string with