提交 e3a7a454 编写于 作者: J Johan Walles

process.Username(): Correct user names on Darwin

Before this change, process.Username() returned "root" for all processes on
Darwin.
上级 70b7a99f
......@@ -127,7 +127,14 @@ func (p *Process) Uids() ([]int32, error) {
uids := make([]int32, 0, 3)
uids = append(uids, int32(k.Eproc.Pcred.P_ruid), int32(k.Eproc.Ucred.Uid), int32(k.Eproc.Pcred.P_svuid))
// See: http://unix.superglobalmegacorp.com/Net2/newsrc/sys/ucred.h.html
userEffectiveUID := int32(k.Eproc.Ucred.Uid)
// See: http://unix.superglobalmegacorp.com/Net2/newsrc/sys/proc.h.html
procRealUID := int32(k.Eproc.Pcred.P_ruid)
procSavedEffectiveUID := int32(k.Eproc.Pcred.P_svuid)
uids = append(uids, userEffectiveUID, procRealUID, procSavedEffectiveUID)
return uids, nil
}
......@@ -371,6 +378,8 @@ func parseKinfoProc(buf []byte) (KinfoProc, error) {
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) {
mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid}
procK := KinfoProc{}
......
......@@ -2,6 +2,7 @@ package process
import (
"os"
"os/user"
"runtime"
"strings"
"sync"
......@@ -9,6 +10,7 @@ import (
"time"
"github.com/shirou/gopsutil/internal/common"
"github.com/stretchr/testify/assert"
)
var mu sync.Mutex
......@@ -326,5 +328,14 @@ func Test_Children(t *testing.T) {
if len(c) == 0 {
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)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册