Skip to content

Commit

Permalink
Process.Username method for posix-compliant OS
Browse files Browse the repository at this point in the history
  • Loading branch information
def committed Aug 29, 2014
1 parent 547d722 commit 2297579
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
3 changes: 0 additions & 3 deletions process_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions process_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions process_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package gopsutil

import (
"os"
"os/user"
"os/exec"
"strconv"
"strings"
Expand Down Expand Up @@ -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
}

0 comments on commit 2297579

Please sign in to comment.