diff --git a/common.go b/common.go index f953008..887ac54 100644 --- a/common.go +++ b/common.go @@ -10,7 +10,6 @@ import ( "bufio" "os" "strings" - ) // Read contents from file and split by new line. @@ -33,8 +32,7 @@ func ReadLines(filename string) ([]string, error) { return ret, err } - -func byteToString(orig []byte) string{ +func byteToString(orig []byte) string { n := -1 for i, b := range orig { if b == 0 { @@ -42,9 +40,9 @@ func byteToString(orig []byte) string{ } n = i + 1 } - if n == -1{ + if n == -1 { return string(orig) - }else{ + } else { return string(orig[:n]) } diff --git a/cpu_freebsd.go b/cpu_freebsd.go index 8318b43..eb3a1a4 100644 --- a/cpu_freebsd.go +++ b/cpu_freebsd.go @@ -2,13 +2,8 @@ package gopsutil -import ( - "fmt" -) - func Cpu_times() ([]CPU_TimesStat, error) { ret := make([]CPU_TimesStat, 0) - fmt.Println("FreeBSD") return ret, nil } diff --git a/host_freebsd.go b/host_freebsd.go index abe299b..d5cc1e6 100644 --- a/host_freebsd.go +++ b/host_freebsd.go @@ -3,11 +3,28 @@ package gopsutil import ( + "bytes" + "encoding/binary" + "io/ioutil" "os" "strconv" "strings" + "unsafe" ) +const ( + UT_NAMESIZE = 16 /* see MAXLOGNAME in */ + UT_LINESIZE = 8 + UT_HOSTSIZE = 16 +) + +type utmp struct { + Ut_line [UT_LINESIZE]byte + Ut_name [UT_NAMESIZE]byte + Ut_host [UT_HOSTSIZE]byte + Ut_time int32 +} + func HostInfo() (HostInfoStat, error) { ret := HostInfoStat{} @@ -35,3 +52,43 @@ func Boot_time() (int64, error) { return boottime, nil } + +func Users() ([]UserStat, error) { + utmpfile := "/var/run/utmp" + ret := make([]UserStat, 0) + + file, err := os.Open(utmpfile) + if err != nil { + return ret, err + } + + buf, err := ioutil.ReadAll(file) + if err != nil { + return ret, err + } + + u := utmp{} + entrySize := int(unsafe.Sizeof(u)) + count := len(buf) / entrySize + + for i := 0; i < count; i++ { + b := buf[i*entrySize : i*entrySize+entrySize] + + var u utmp + br := bytes.NewReader(b) + err := binary.Read(br, binary.LittleEndian, &u) + if err != nil { + continue + } + user := UserStat{ + User: byteToString(u.Ut_name[:]), + Terminal: byteToString(u.Ut_line[:]), + Host: byteToString(u.Ut_host[:]), + Started: int(u.Ut_time), + } + ret = append(ret, user) + } + + return ret, nil + +} diff --git a/host_linux.go b/host_linux.go index 6f71945..5a9a488 100644 --- a/host_linux.go +++ b/host_linux.go @@ -3,8 +3,8 @@ package gopsutil import ( - "encoding/binary" "bytes" + "encoding/binary" "io/ioutil" "os" "syscall" @@ -75,17 +75,17 @@ func Users() ([]UserStat, error) { for i := 0; i < count; i++ { b := buf[i*entrySize : i*entrySize+entrySize] - var u utmp - br := bytes.NewReader(b) - err := binary.Read(br, binary.LittleEndian, &u) + var u utmp + br := bytes.NewReader(b) + err := binary.Read(br, binary.LittleEndian, &u) if err != nil { continue } user := UserStat{ - User: byteToString(u.Ut_user[:]), + User: byteToString(u.Ut_user[:]), Terminal: byteToString(u.Ut_line[:]), - Host: byteToString(u.Ut_host[:]), - Started: int(u.Ut_tv.Tv_sec), + Host: byteToString(u.Ut_host[:]), + Started: int(u.Ut_tv.Tv_sec), } ret = append(ret, user) } diff --git a/host_test.go b/host_test.go index d4906a6..9d1476d 100644 --- a/host_test.go +++ b/host_test.go @@ -24,14 +24,13 @@ func TestBoot_time(t *testing.T) { } } - func TestUsers(t *testing.T) { v, err := Users() if err != nil { t.Errorf("error %v", err) } - for _, u := range v { - if u.User == ""{ + for _, u := range v { + if u.User == "" { t.Errorf("Could not Users %v", v) } }