Skip to content

Commit 0306525

Browse files
committed
[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.
1 parent 511da82 commit 0306525

9 files changed

+9
-57
lines changed

process/process.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,15 @@ func (p *Process) Parent() (*Process, error) {
402402
return p.ParentWithContext(context.Background())
403403
}
404404

405+
// ParentWithContext returns parent Process of the process.
406+
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
407+
ppid, err := p.PpidWithContext(ctx)
408+
if err != nil {
409+
return nil, err
410+
}
411+
return NewProcessWithContext(ctx, ppid)
412+
}
413+
405414
// Status returns the process status.
406415
// Return value could be one of these.
407416
// R: Running S: Sleep T: Stop I: Idle

process/process_darwin.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,6 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {
141141
return start.Unix() * 1000, nil
142142
}
143143

144-
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
145-
out, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR")
146-
if err != nil {
147-
return nil, err
148-
}
149-
for _, line := range out {
150-
if len(line) >= 1 && line[0] == 'R' {
151-
v, err := strconv.Atoi(line[1:])
152-
if err != nil {
153-
return nil, err
154-
}
155-
return NewProcessWithContext(ctx, int32(v))
156-
}
157-
}
158-
return nil, fmt.Errorf("could not find parent line")
159-
}
160-
161144
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
162145
r, err := callPsWithContext(ctx, "state", p.Pid, false, false)
163146
if err != nil {

process/process_fallback.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
7474
return "", common.ErrNotImplementedError
7575
}
7676

77-
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
78-
return nil, common.ErrNotImplementedError
79-
}
80-
8177
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
8278
return []string{""}, common.ErrNotImplementedError
8379
}

process/process_freebsd.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {
118118
return k.Start.Sec*1000 + k.Start.Usec/1000, nil
119119
}
120120

121-
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
122-
return nil, common.ErrNotImplementedError
123-
}
124-
125121
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
126122
k, err := p.getKProc()
127123
if err != nil {

process/process_linux.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
123123
return p.fillFromCwdWithContext()
124124
}
125125

126-
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
127-
err := p.fillFromStatusWithContext()
128-
if err != nil {
129-
return nil, err
130-
}
131-
if p.parent == 0 {
132-
return nil, fmt.Errorf("wrong number of parents")
133-
}
134-
return NewProcessWithContext(ctx, p.parent)
135-
}
136-
137126
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
138127
err := p.fillFromStatusWithContext()
139128
if err != nil {

process/process_openbsd.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,6 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {
142142
return 0, common.ErrNotImplementedError
143143
}
144144

145-
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
146-
return nil, common.ErrNotImplementedError
147-
}
148-
149145
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
150146
k, err := p.getKProc()
151147
if err != nil {

process/process_plan9.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
7474
return "", common.ErrNotImplementedError
7575
}
7676

77-
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
78-
return nil, common.ErrNotImplementedError
79-
}
80-
8177
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
8278
return []string{""}, common.ErrNotImplementedError
8379
}

process/process_solaris.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
8888
return p.fillFromPathCwdWithContext(ctx)
8989
}
9090

91-
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
92-
return nil, common.ErrNotImplementedError
93-
}
94-
9591
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
9692
return []string{""}, common.ErrNotImplementedError
9793
}

process/process_windows.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -435,15 +435,6 @@ func (p *Process) CwdWithContext(_ context.Context) (string, error) {
435435
return "", nil
436436
}
437437

438-
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
439-
ppid, err := p.PpidWithContext(ctx)
440-
if err != nil {
441-
return nil, fmt.Errorf("could not get ParentProcessID: %s", err)
442-
}
443-
444-
return NewProcessWithContext(ctx, ppid)
445-
}
446-
447438
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
448439
return []string{""}, common.ErrNotImplementedError
449440
}

0 commit comments

Comments
 (0)