host_freebsd now use godefs

tags/1.0.0^2
WAKAYAMA shirou 11 years ago
parent 4973aa73f9
commit b0c9f81246

@ -16,6 +16,12 @@ import (
common "github.com/shirou/gopsutil/common" common "github.com/shirou/gopsutil/common"
) )
const (
UTNameSize = 16 /* see MAXLOGNAME in <sys/param.h> */
UTLineSize = 8
UTHostSize = 16
)
func HostInfo() (*HostInfoStat, error) { func HostInfo() (*HostInfoStat, error) {
ret := &HostInfoStat{ ret := &HostInfoStat{
OS: runtime.GOOS, OS: runtime.GOOS,
@ -84,25 +90,26 @@ func Users() ([]UserStat, error) {
return ret, err return ret, err
} }
u := utmp{} u := Utmp{}
entrySize := int(unsafe.Sizeof(u)) entrySize := int(unsafe.Sizeof(u))
count := len(buf) / entrySize count := len(buf) / entrySize
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
b := buf[i*entrySize : i*entrySize+entrySize] b := buf[i*entrySize : i*entrySize+entrySize]
var u utmp var u Utmp
br := bytes.NewReader(b) br := bytes.NewReader(b)
err := binary.Read(br, binary.LittleEndian, &u) err := binary.Read(br, binary.LittleEndian, &u)
if err != nil { if err != nil || u.Time == 0 {
continue continue
} }
user := UserStat{ user := UserStat{
User: common.ByteToString(u.UtName[:]), User: common.IntToString(u.Name[:]),
Terminal: common.ByteToString(u.UtLine[:]), Terminal: common.IntToString(u.Line[:]),
Host: common.ByteToString(u.UtHost[:]), Host: common.IntToString(u.Host[:]),
Started: int(u.UtTime), Started: int(u.Time),
} }
ret = append(ret, user) ret = append(ret, user)
} }

@ -1,17 +1,28 @@
// +build freebsd // +build freebsd
// +build amd64 // +build amd64
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs host/types_freebsd.go
package host package host
const ( const (
UTNameSize = 16 /* see MAXLOGNAME in <sys/param.h> */ sizeofPtr = 0x8
UTLineSize = 8 sizeofShort = 0x2
UTHostSize = 16 sizeofInt = 0x4
sizeofLong = 0x8
sizeofLongLong = 0x8
) )
type utmp struct { type (
UtLine [UTLineSize]byte _C_short int16
UtName [UTNameSize]byte _C_int int32
UtHost [UTHostSize]byte _C_long int64
UtTime int32 _C_long_long int64
)
type Utmp struct {
Line [8]int8
Name [16]int8
Host [16]int8
Time int32
} }

@ -33,6 +33,7 @@ func TestUsers(t *testing.T) {
} }
empty := UserStat{} empty := UserStat{}
for _, u := range v { for _, u := range v {
fmt.Println(u)
if u == empty { if u == empty {
t.Errorf("Could not Users %v", v) t.Errorf("Could not Users %v", v)
} }

@ -0,0 +1,40 @@
// +build ignore
/*
Input to cgo -godefs.
*/
package host
/*
#define KERNEL
#include <sys/types.h>
#include <utmp.h>
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
Loading…
Cancel
Save