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

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

@ -16,24 +16,6 @@ import (
common "github.com/shirou/gopsutil/common" 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) { func HostInfo() (*HostInfoStat, error) {
ret := &HostInfoStat{ ret := &HostInfoStat{
OS: runtime.GOOS, OS: runtime.GOOS,
@ -102,24 +84,27 @@ func Users() ([]UserStat, error) {
return ret, err return ret, err
} }
u := utmpx32{} u := Utmpx{}
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 utmpx32 var u Utmpx
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 {
continue continue
} }
if u.Line[0] == 0 { // skip if terminal is empty
continue
}
user := UserStat{ user := UserStat{
User: common.ByteToString(u.UtUser[:]), User: common.IntToString(u.User[:]),
// Terminal: 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.Tv.Sec),
} }
ret = append(ret, user) 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