Skip to content

Commit

Permalink
Fix process.Foreground for BSDs, add openbsd implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Lomanic committed Nov 11, 2018
1 parent 878e0a7 commit 8ef9b01
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion process/process_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
if err != nil {
return false, err
}
out, err := invoke.CommandWithContext(ctx, ps, "-o", "stat=", "-p", string(pid))
out, err := invoke.CommandWithContext(ctx, ps, "-o", "stat=", "-p", strconv.Itoa(int(pid)))
if err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion process/process_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
if err != nil {
return false, err
}
out, err := invoke.CommandWithContext(ctx, ps, "-o", "stat=", "-p", string(pid))
out, err := invoke.CommandWithContext(ctx, ps, "-o", "stat=", "-p", strconv.Itoa(int(pid)))
if err != nil {
return false, err
}
Expand Down
17 changes: 17 additions & 0 deletions process/process_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ func (p *Process) StatusWithContext(ctx context.Context) (string, error) {

return s, nil
}
func (p *Process) Foreground() (bool, error) {
return p.ForegroundWithContext(context.Background())
}

func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
// see https://github.com/shirou/gopsutil/issues/596#issuecomment-432707831 for implementation details
pid := p.Pid
ps, err := exec.LookPath("ps")
if err != nil {
return false, err
}
out, err := invoke.CommandWithContext(ctx, ps, "-o", "stat=", "-p", strconv.Itoa(int(pid)))
if err != nil {
return false, err
}
return strings.IndexByte(string(out), '+') != -1, nil
}
func (p *Process) Uids() ([]int32, error) {
return p.UidsWithContext(context.Background())
}
Expand Down

0 comments on commit 8ef9b01

Please sign in to comment.