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"
)
const (
UTNameSize = 16 /* see MAXLOGNAME in <sys/param.h> */
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)
}

@ -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 <sys/param.h> */
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
}

@ -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)
}

@ -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