diff --git a/process/process_darwin.go b/process/process_darwin.go index 5d64840..8b8b58a 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -146,20 +146,18 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { } func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - rr, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR") + out, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR") if err != nil { return nil, err } - for _, r := range rr { - if strings.HasPrefix(r, "p") { // skip if process - continue - } - l := string(r) - v, err := strconv.Atoi(strings.Replace(l, "R", "", 1)) - if err != nil { - return nil, err + for _, line := range out { + if len(line) >= 1 && line[0] == 'R' { + v, err := strconv.Atoi(line[1:]) + if err != nil { + return nil, err + } + return NewProcessWithContext(ctx, int32(v)) } - return NewProcessWithContext(ctx, int32(v)) } return nil, fmt.Errorf("could not find parent line") } diff --git a/v3/process/process_darwin.go b/v3/process/process_darwin.go index 3f93fb3..a7c4115 100644 --- a/v3/process/process_darwin.go +++ b/v3/process/process_darwin.go @@ -146,20 +146,18 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { } func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - rr, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR") + out, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR") if err != nil { return nil, err } - for _, r := range rr { - if strings.HasPrefix(r, "p") { // skip if process - continue - } - l := string(r) - v, err := strconv.Atoi(strings.Replace(l, "R", "", 1)) - if err != nil { - return nil, err + for _, line := range out { + if len(line) >= 1 && line[0] == 'R' { + v, err := strconv.Atoi(line[1:]) + if err != nil { + return nil, err + } + return NewProcessWithContext(ctx, int32(v)) } - return NewProcessWithContext(ctx, int32(v)) } return nil, fmt.Errorf("could not find parent line") }