diff --git a/host/host_freebsd.go b/host/host_freebsd.go index 47dfe50..5b955b4 100644 --- a/host/host_freebsd.go +++ b/host/host_freebsd.go @@ -16,6 +16,12 @@ import ( common "github.com/shirou/gopsutil/common" ) +const ( + UTNameSize = 16 /* see MAXLOGNAME in */ + UTLineSize = 8 + UTHostSize = 16 +) + func HostInfo() (*HostInfoStat, error) { ret := &HostInfoStat{ OS: runtime.GOOS, @@ -84,25 +90,26 @@ func Users() ([]UserStat, error) { return ret, err } - u := utmp{} + 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 + var u Utmp br := bytes.NewReader(b) err := binary.Read(br, binary.LittleEndian, &u) - if err != nil { + if err != nil || u.Time == 0 { continue } user := UserStat{ - User: common.ByteToString(u.UtName[:]), - Terminal: common.ByteToString(u.UtLine[:]), - Host: common.ByteToString(u.UtHost[:]), - Started: int(u.UtTime), + User: common.IntToString(u.Name[:]), + Terminal: common.IntToString(u.Line[:]), + Host: common.IntToString(u.Host[:]), + Started: int(u.Time), } + ret = append(ret, user) } diff --git a/host/host_freebsd_amd64.go b/host/host_freebsd_amd64.go index fc8e521..7706cbd 100644 --- a/host/host_freebsd_amd64.go +++ b/host/host_freebsd_amd64.go @@ -1,17 +1,28 @@ // +build freebsd // +build amd64 +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs host/types_freebsd.go package host const ( - UTNameSize = 16 /* see MAXLOGNAME in */ - UTLineSize = 8 - UTHostSize = 16 + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 ) -type utmp struct { - UtLine [UTLineSize]byte - UtName [UTNameSize]byte - UtHost [UTHostSize]byte - UtTime int32 +type ( + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 +) + +type Utmp struct { + Line [8]int8 + Name [16]int8 + Host [16]int8 + Time int32 } diff --git a/host/host_test.go b/host/host_test.go index 4c6fbc9..64ad747 100644 --- a/host/host_test.go +++ b/host/host_test.go @@ -33,6 +33,7 @@ func TestUsers(t *testing.T) { } empty := UserStat{} for _, u := range v { + fmt.Println(u) if u == empty { t.Errorf("Could not Users %v", v) } diff --git a/host/types_freebsd.go b/host/types_freebsd.go new file mode 100644 index 0000000..dd768f4 --- /dev/null +++ b/host/types_freebsd.go @@ -0,0 +1,40 @@ +// +build ignore + +/* +Input to cgo -godefs. +*/ + +package host + +/* +#define KERNEL +#include +#include + +enum { + sizeofPtr = sizeof(void*), +}; + +*/ +import "C" + +// Machine characteristics; for internal use. + +const ( + sizeofPtr = C.sizeofPtr + sizeofShort = C.sizeof_short + sizeofInt = C.sizeof_int + sizeofLong = C.sizeof_long + sizeofLongLong = C.sizeof_longlong +) + +// Basic types + +type ( + _C_short C.short + _C_int C.int + _C_long C.long + _C_long_long C.longlong +) + +type Utmp C.struct_utmp