host: Users() on darwin works. but Started is not correct.

pull/43/merge
若山史郎 10 years ago
parent 4ead971d70
commit a5d366a70c

@ -16,24 +16,6 @@ import (
common "github.com/shirou/gopsutil/common"
)
const (
UTXUserSize = 256 /* include/NetBSD/utmpx.h */
UTXIDSize = 4
UTXLineSize = 32
UTXHostSize = 256
)
type utmpx32 struct {
UtUser [UTXUserSize]byte /* login name */
UtID [UTXIDSize]byte /* id */
UtLine [UTXLineSize]byte /* tty name */
//TODO UtPid pid_t /* process id creating the entry */
UtType [4]byte /* type of this entry */
//TODO UtTv timeval32 /* time entry was created */
UtHost [UTXHostSize]byte /* host name */
UtPad [16]byte /* reserved for future use */
}
func HostInfo() (*HostInfoStat, error) {
ret := &HostInfoStat{
OS: runtime.GOOS,
@ -102,24 +84,27 @@ func Users() ([]UserStat, error) {
return ret, err
}
u := utmpx32{}
u := Utmpx{}
entrySize := int(unsafe.Sizeof(u))
count := len(buf) / entrySize
for i := 0; i < count; i++ {
b := buf[i*entrySize : i*entrySize+entrySize]
var u utmpx32
var u Utmpx
br := bytes.NewReader(b)
err := binary.Read(br, binary.LittleEndian, &u)
if err != nil {
continue
}
if u.Line[0] == 0 { // skip if terminal is empty
continue
}
user := UserStat{
User: common.ByteToString(u.UtUser[:]),
// Terminal: ByteToString(u.UtLine[:]),
Host: common.ByteToString(u.UtHost[:]),
// Started: int(u.UtTime),
User: common.IntToString(u.User[:]),
Terminal: common.IntToString(u.Line[:]),
Host: common.IntToString(u.Host[:]),
Started: int(u.Tv.Sec),
}
ret = append(ret, user)
}

@ -0,0 +1,19 @@
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs types_darwin.go
package host
type Utmpx struct {
User [256]int8
Id [4]int8
Line [32]int8
Pid int32
Type int16
Pad_cgo_0 [6]byte
Tv Timeval
Host [256]int8
Pad [16]uint32
}
type Timeval struct {
Sec int32
}

@ -0,0 +1,17 @@
// +build ignore
// plus hand editing about timeval
/*
Input to cgo -godefs.
*/
package host
/*
#include <sys/time.h>
#include <utmpx.h>
*/
import "C"
type Utmpx C.struct_utmpx
type Timeval C.struct_timeval
Loading…
Cancel
Save