Skip to content

Commit

Permalink
Add Process.Parent() (#46)
Browse files Browse the repository at this point in the history
Adds a `Parent()` method to `Process` and implements it for all platforms. This should make it easier for users of this library to traverse the process tree.
  • Loading branch information
Christoph Wurm authored Apr 11, 2019
1 parent 77ef9c6 commit fd25fda
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
20 changes: 18 additions & 2 deletions providers/darwin/process_darwin_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (s darwinSystem) Self() (types.Process, error) {
}

type process struct {
info *types.ProcessInfo
pid int
cwd string
exe string
Expand All @@ -96,7 +97,20 @@ func (p *process) PID() int {
return p.pid
}

func (p *process) Parent() (types.Process, error) {
info, err := p.Info()
if err != nil {
return nil, err
}

return &process{pid: info.PPID}, nil
}

func (p *process) Info() (types.ProcessInfo, error) {
if p.info != nil {
return *p.info, nil
}

var task procTaskAllInfo
if err := getProcTaskAllInfo(p.pid, &task); err != nil {
return types.ProcessInfo{}, err
Expand All @@ -111,7 +125,7 @@ func (p *process) Info() (types.ProcessInfo, error) {
return types.ProcessInfo{}, err
}

return types.ProcessInfo{
p.info = &types.ProcessInfo{
Name: int8SliceToString(task.Pbsd.Pbi_name[:]),
PID: p.pid,
PPID: int(task.Pbsd.Pbi_ppid),
Expand All @@ -120,7 +134,9 @@ func (p *process) Info() (types.ProcessInfo, error) {
Args: p.args,
StartTime: time.Unix(int64(task.Pbsd.Pbi_start_tvsec),
int64(task.Pbsd.Pbi_start_tvusec)*int64(time.Microsecond)),
}, nil
}

return *p.info, nil
}

func (p *process) User() (types.UserInfo, error) {
Expand Down
2 changes: 2 additions & 0 deletions providers/linux/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// specific language governing permissions and limitations
// under the License.

// +build !windows

package linux

import (
Expand Down
14 changes: 14 additions & 0 deletions providers/linux/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ func (p *process) PID() int {
return p.Proc.PID
}

func (p *process) Parent() (types.Process, error) {
info, err := p.Info()
if err != nil {
return nil, err
}

proc, err := p.fs.NewProc(info.PPID)
if err != nil {
return nil, err
}

return &process{Proc: proc, fs: p.fs}, nil
}

func (p *process) path(pa ...string) string {
return p.fs.Path(append([]string{strconv.Itoa(p.PID())}, pa...)...)
}
Expand Down
9 changes: 9 additions & 0 deletions providers/windows/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ func (p *process) PID() int {
return p.pid
}

func (p *process) Parent() (types.Process, error) {
info, err := p.Info()
if err != nil {
return nil, err
}

return newProcess(info.PPID)
}

func newProcess(pid int) (*process, error) {
p := &process{pid: pid}
if err := p.init(); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func TestSelf(t *testing.T) {
assert.EqualValues(t, os.Getpid(), info.PID)
assert.EqualValues(t, os.Getppid(), info.PPID)
assert.Equal(t, os.Args, info.Args)
assert.WithinDuration(t, info.StartTime, time.Now(), 10*time.Second)

wd, err := os.Getwd()
if err != nil {
Expand All @@ -128,10 +129,11 @@ func TestSelf(t *testing.T) {
}
assert.Equal(t, exe, info.Exe)

parent, err := process.Parent()
if err != nil {
t.Fatal(err)
}
assert.WithinDuration(t, info.StartTime, time.Now(), 10*time.Second)
assert.EqualValues(t, os.Getppid(), parent.PID())

user, err := process.User()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions types/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Process interface {
Info() (ProcessInfo, error)
Memory() (MemoryInfo, error)
User() (UserInfo, error)
Parent() (Process, error)
PID() int
}

Expand Down

0 comments on commit fd25fda

Please sign in to comment.