Merge pull request #153 from walles/walles/processusername

Return correct user name from process.Username()
pull/158/head
shirou 9 years ago
commit 7fd975a4c0

@ -125,11 +125,10 @@ func (p *Process) Uids() ([]int32, error) {
return nil, err return nil, err
} }
uids := make([]int32, 0, 3) // See: http://unix.superglobalmegacorp.com/Net2/newsrc/sys/ucred.h.html
userEffectiveUID := int32(k.Eproc.Ucred.Uid)
uids = append(uids, int32(k.Eproc.Pcred.P_ruid), int32(k.Eproc.Ucred.Uid), int32(k.Eproc.Pcred.P_svuid)) return []int32{userEffectiveUID}, nil
return uids, nil
} }
func (p *Process) Gids() ([]int32, error) { func (p *Process) Gids() ([]int32, error) {
k, err := p.getKProc() k, err := p.getKProc()
@ -371,6 +370,8 @@ func parseKinfoProc(buf []byte) (KinfoProc, error) {
return k, nil return k, nil
} }
// Returns a proc as defined here:
// http://unix.superglobalmegacorp.com/Net2/newsrc/sys/kinfo_proc.h.html
func (p *Process) getKProc() (*KinfoProc, error) { func (p *Process) getKProc() (*KinfoProc, error) {
mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid} mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid}
procK := KinfoProc{} procK := KinfoProc{}

@ -2,6 +2,7 @@ package process
import ( import (
"os" "os"
"os/user"
"runtime" "runtime"
"strings" "strings"
"sync" "sync"
@ -9,6 +10,7 @@ import (
"time" "time"
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"github.com/stretchr/testify/assert"
) )
var mu sync.Mutex var mu sync.Mutex
@ -326,5 +328,14 @@ func Test_Children(t *testing.T) {
if len(c) == 0 { if len(c) == 0 {
t.Fatalf("children is empty") t.Fatalf("children is empty")
} }
}
func Test_Username(t *testing.T) {
myPid := os.Getpid()
currentUser, _ := user.Current()
myUsername := currentUser.Username
process, _ := NewProcess(int32(myPid))
pidUsername, _ := process.Username()
assert.Equal(t, myUsername, pidUsername)
} }

Loading…
Cancel
Save