Skip to content

Commit

Permalink
[process][darwin] Fix Parent() when lsof returns warnings
Browse files Browse the repository at this point in the history
Same as shirou#867, the error being:
error strconv.Atoi: parsing "      Output information may be incomplete.": invalid syntax
  • Loading branch information
Lomanic committed Nov 8, 2020
1 parent 39d04ea commit 048a494
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
18 changes: 8 additions & 10 deletions process/process_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
18 changes: 8 additions & 10 deletions v3/process/process_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 048a494

Please sign in to comment.