From 0306525d788076dbc4f3c594930acb3391620f05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 22 Jan 2022 18:27:52 +0200 Subject: [PATCH] [process] implement ParentWithContext using PpidWithContext Removes need for redundant ParentWithContext implementations. It had led to it being unsupported on FreeBSD and OpenBSD even though PpidWithContext was available for them, and different implementations for getting the parent info used in ParentWithContext and PpidWithContext on Darwin and Linux. --- process/process.go | 9 +++++++++ process/process_darwin.go | 17 ----------------- process/process_fallback.go | 4 ---- process/process_freebsd.go | 4 ---- process/process_linux.go | 11 ----------- process/process_openbsd.go | 4 ---- process/process_plan9.go | 4 ---- process/process_solaris.go | 4 ---- process/process_windows.go | 9 --------- 9 files changed, 9 insertions(+), 57 deletions(-) diff --git a/process/process.go b/process/process.go index bcfd61691..9e6542e0c 100644 --- a/process/process.go +++ b/process/process.go @@ -402,6 +402,15 @@ func (p *Process) Parent() (*Process, error) { return p.ParentWithContext(context.Background()) } +// ParentWithContext returns parent Process of the process. +func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { + ppid, err := p.PpidWithContext(ctx) + if err != nil { + return nil, err + } + return NewProcessWithContext(ctx, ppid) +} + // Status returns the process status. // Return value could be one of these. // R: Running S: Sleep T: Stop I: Idle diff --git a/process/process_darwin.go b/process/process_darwin.go index c1dc918e9..a70003702 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -141,23 +141,6 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { return start.Unix() * 1000, nil } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - out, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR") - 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 nil, fmt.Errorf("could not find parent line") -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { r, err := callPsWithContext(ctx, "state", p.Pid, false, false) if err != nil { diff --git a/process/process_fallback.go b/process/process_fallback.go index e1c4809d7..1a5d0c4b4 100644 --- a/process/process_fallback.go +++ b/process/process_fallback.go @@ -74,10 +74,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) { return "", common.ErrNotImplementedError } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - return nil, common.ErrNotImplementedError -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { return []string{""}, common.ErrNotImplementedError } diff --git a/process/process_freebsd.go b/process/process_freebsd.go index 383ec0f40..444af35e9 100644 --- a/process/process_freebsd.go +++ b/process/process_freebsd.go @@ -118,10 +118,6 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { return k.Start.Sec*1000 + k.Start.Usec/1000, nil } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - return nil, common.ErrNotImplementedError -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { k, err := p.getKProc() if err != nil { diff --git a/process/process_linux.go b/process/process_linux.go index af8f23296..322ea0c11 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -123,17 +123,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) { return p.fillFromCwdWithContext() } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - err := p.fillFromStatusWithContext() - if err != nil { - return nil, err - } - if p.parent == 0 { - return nil, fmt.Errorf("wrong number of parents") - } - return NewProcessWithContext(ctx, p.parent) -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { err := p.fillFromStatusWithContext() if err != nil { diff --git a/process/process_openbsd.go b/process/process_openbsd.go index cdbdaedc3..77aa8041c 100644 --- a/process/process_openbsd.go +++ b/process/process_openbsd.go @@ -142,10 +142,6 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { return 0, common.ErrNotImplementedError } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - return nil, common.ErrNotImplementedError -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { k, err := p.getKProc() if err != nil { diff --git a/process/process_plan9.go b/process/process_plan9.go index 70e11e3aa..bc4bc062a 100644 --- a/process/process_plan9.go +++ b/process/process_plan9.go @@ -74,10 +74,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) { return "", common.ErrNotImplementedError } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - return nil, common.ErrNotImplementedError -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { return []string{""}, common.ErrNotImplementedError } diff --git a/process/process_solaris.go b/process/process_solaris.go index 93a6e01e2..4f10a67bc 100644 --- a/process/process_solaris.go +++ b/process/process_solaris.go @@ -88,10 +88,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) { return p.fillFromPathCwdWithContext(ctx) } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - return nil, common.ErrNotImplementedError -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { return []string{""}, common.ErrNotImplementedError } diff --git a/process/process_windows.go b/process/process_windows.go index d34894641..2a3a0c493 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -435,15 +435,6 @@ func (p *Process) CwdWithContext(_ context.Context) (string, error) { return "", nil } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - ppid, err := p.PpidWithContext(ctx) - if err != nil { - return nil, fmt.Errorf("could not get ParentProcessID: %s", err) - } - - return NewProcessWithContext(ctx, ppid) -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { return []string{""}, common.ErrNotImplementedError }