diff --git a/process_darwin.go b/process_darwin.go index 5f6ad16..5e1299f 100644 --- a/process_darwin.go +++ b/process_darwin.go @@ -71,9 +71,6 @@ func (p *Process) Status() (string, error) { return string(k.KiStat[:]), nil } -func (p *Process) Username() (string, error) { - return "", NotImplementedError -} func (p *Process) Uids() ([]int32, error) { k, err := p.getKProc() if err != nil { diff --git a/process_freebsd.go b/process_freebsd.go index ee33a69..101b3fc 100644 --- a/process_freebsd.go +++ b/process_freebsd.go @@ -69,9 +69,6 @@ func (p *Process) Status() (string, error) { return string(k.KiStat[:]), nil } -func (p *Process) Username() (string, error) { - return "", NotImplementedError -} func (p *Process) Uids() ([]int32, error) { k, err := p.getKProc() if err != nil { diff --git a/process_linux.go b/process_linux.go index a24a8e4..982e2ae 100644 --- a/process_linux.go +++ b/process_linux.go @@ -101,9 +101,6 @@ func (p *Process) Status() (string, error) { } return status, nil } -func (p *Process) Username() (string, error) { - return "", nil -} func (p *Process) Uids() ([]int32, error) { _, _, uids, _, _, _, err := p.fillFromStatus() if err != nil { diff --git a/process_posix.go b/process_posix.go index 03bd5d8..1514dc7 100644 --- a/process_posix.go +++ b/process_posix.go @@ -4,6 +4,7 @@ package gopsutil import ( "os" + "os/user" "os/exec" "strconv" "strings" @@ -85,3 +86,17 @@ func (p *Process) Terminate() error { func (p *Process) Kill() error { return p.SendSignal(syscall.SIGKILL) } +func (p *Process) Username() (string, error) { + uids, err := p.Uids() + if err != nil { + return "", err + } + if len(uids) > 0 { + u, err := user.LookupId(strconv.Itoa(int(uids[0]))) + if err != nil { + return "", err + } + return u.Username, nil + } + return "", nil +}