[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.
pull/1235/head
Ville Skyttä 3 years ago
parent 511da82e94
commit 0306525d78

@ -402,6 +402,15 @@ func (p *Process) Parent() (*Process, error) {
return p.ParentWithContext(context.Background()) 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. // Status returns the process status.
// Return value could be one of these. // Return value could be one of these.
// R: Running S: Sleep T: Stop I: Idle // R: Running S: Sleep T: Stop I: Idle

@ -141,23 +141,6 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {
return start.Unix() * 1000, nil 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) { func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
r, err := callPsWithContext(ctx, "state", p.Pid, false, false) r, err := callPsWithContext(ctx, "state", p.Pid, false, false)
if err != nil { if err != nil {

@ -74,10 +74,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError 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) { func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
return []string{""}, common.ErrNotImplementedError return []string{""}, common.ErrNotImplementedError
} }

@ -118,10 +118,6 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {
return k.Start.Sec*1000 + k.Start.Usec/1000, nil 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) { func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {

@ -123,17 +123,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
return p.fillFromCwdWithContext() 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) { func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
err := p.fillFromStatusWithContext() err := p.fillFromStatusWithContext()
if err != nil { if err != nil {

@ -142,10 +142,6 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {
return 0, common.ErrNotImplementedError 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) { func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {

@ -74,10 +74,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError 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) { func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
return []string{""}, common.ErrNotImplementedError return []string{""}, common.ErrNotImplementedError
} }

@ -88,10 +88,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
return p.fillFromPathCwdWithContext(ctx) 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) { func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
return []string{""}, common.ErrNotImplementedError return []string{""}, common.ErrNotImplementedError
} }

@ -435,15 +435,6 @@ func (p *Process) CwdWithContext(_ context.Context) (string, error) {
return "", nil 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) { func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
return []string{""}, common.ErrNotImplementedError return []string{""}, common.ErrNotImplementedError
} }

Loading…
Cancel
Save