From a5d366a70c1ad8d8ba95a47e3c5b624839cad0f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=A5=E5=B1=B1=E5=8F=B2=E9=83=8E?= Date: Sat, 14 Feb 2015 23:04:17 +0900 Subject: [PATCH] host: Users() on darwin works. but Started is not correct. --- host/host_darwin.go | 33 +++++++++------------------------ host/host_darwin_amd64.go | 19 +++++++++++++++++++ host/types_darwin.go | 17 +++++++++++++++++ 3 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 host/host_darwin_amd64.go create mode 100644 host/types_darwin.go diff --git a/host/host_darwin.go b/host/host_darwin.go index f1e189e..bc2cf37 100644 --- a/host/host_darwin.go +++ b/host/host_darwin.go @@ -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) } diff --git a/host/host_darwin_amd64.go b/host/host_darwin_amd64.go new file mode 100644 index 0000000..3ea52d5 --- /dev/null +++ b/host/host_darwin_amd64.go @@ -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 +} diff --git a/host/types_darwin.go b/host/types_darwin.go new file mode 100644 index 0000000..b858227 --- /dev/null +++ b/host/types_darwin.go @@ -0,0 +1,17 @@ +// +build ignore +// plus hand editing about timeval + +/* +Input to cgo -godefs. +*/ + +package host + +/* +#include +#include +*/ +import "C" + +type Utmpx C.struct_utmpx +type Timeval C.struct_timeval