Skip to content

Commit 696bb11

Browse files
authored
Merge pull request #1235 from scop/refactor/parentwithcontext-ppidwithcontext
[process] implement ParentWithContext using PpidWithContext
2 parents 34e74aa + 50cad07 commit 696bb11

9 files changed

+9
-49
lines changed

process/process.go

+9
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

-9
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,6 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {
100100
return k.Proc.P_starttime.Sec*1000 + int64(k.Proc.P_starttime.Usec)/1000, nil
101101
}
102102

103-
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
104-
ppid, err := p.PpidWithContext(ctx)
105-
if err != nil {
106-
return nil, err
107-
}
108-
109-
return NewProcessWithContext(ctx, ppid)
110-
}
111-
112103
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
113104
r, err := callPsWithContext(ctx, "state", p.Pid, false, false)
114105
if err != nil {

process/process_fallback.go

-4
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

-4
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

-11
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

-4
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

-4
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

-4
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

-9
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)