From 8ef9b0105260847588b204fca6246c454a67d893 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sun, 11 Nov 2018 19:05:34 +0100 Subject: [PATCH] Fix process.Foreground for BSDs, add openbsd implementation --- process/process_darwin.go | 2 +- process/process_freebsd.go | 2 +- process/process_openbsd.go | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index b55c3d2de..ca0f18763 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -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 } diff --git a/process/process_freebsd.go b/process/process_freebsd.go index 01f36596f..d5fc62d4c 100644 --- a/process/process_freebsd.go +++ b/process/process_freebsd.go @@ -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 } diff --git a/process/process_openbsd.go b/process/process_openbsd.go index b7b2cba00..f9e0a0867 100644 --- a/process/process_openbsd.go +++ b/process/process_openbsd.go @@ -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()) }