From 0787e8ba3e173e8348e38b27acceee587234a95e Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Sun, 10 Apr 2016 10:07:35 +0000 Subject: [PATCH 1/4] [host]linux-arm: update type_liux.go. Test will fail. --- host/host_linux.go | 15 +++++-------- host/host_linux_arm.go | 57 ++++++++++++++++++++++++----------------------- host/types_linux.go | 9 +++----- internal/common/common.go | 17 ++++++++++++++ 4 files changed, 55 insertions(+), 43 deletions(-) diff --git a/host/host_linux.go b/host/host_linux.go index f56906c..5b7907a 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -14,7 +14,6 @@ import ( "strconv" "strings" "time" - "unsafe" "github.com/shirou/gopsutil/internal/common" ) @@ -108,14 +107,12 @@ func Users() ([]UserStat, error) { return nil, err } - u := utmp{} - entrySize := int(unsafe.Sizeof(u)) - count := len(buf) / entrySize + count := len(buf) / sizeOfUtmp ret := make([]UserStat, 0, count) for i := 0; i < count; i++ { - b := buf[i*entrySize : i*entrySize+entrySize] + b := buf[i*sizeOfUtmp : (i+1)*sizeOfUtmp] var u utmp br := bytes.NewReader(b) @@ -127,10 +124,10 @@ func Users() ([]UserStat, error) { continue } user := UserStat{ - User: common.IntToString(u.User[:]), - Terminal: common.IntToString(u.Line[:]), - Host: common.IntToString(u.Host[:]), - Started: int(u.Tv.TvSec), + User: common.UintToString(u.User[:]), + Terminal: common.UintToString(u.Line[:]), + Host: common.UintToString(u.Host[:]), + Started: int(u.Tv.Sec), } ret = append(ret, user) } diff --git a/host/host_linux_arm.go b/host/host_linux_arm.go index 5f7e168..794f09e 100644 --- a/host/host_linux_arm.go +++ b/host/host_linux_arm.go @@ -1,42 +1,43 @@ -// +build linux -// +build arm +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_linux.go package host const ( - sizeofPtr = 0x8 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x8 - sizeofLongLong = 0x8 + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x4 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 ) type ( - _C_short int16 - _C_int int32 - _C_long int64 - _C_long_long int64 + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 ) type utmp struct { - Type int16 - Pad_cgo_0 [2]byte - Pid int32 - Line [32]int8 - ID [4]int8 - User [32]int8 - Host [256]int8 - Exit exit_status - Session int32 - Tv UtTv - Addr_v6 [4]int32 - X__glibc_reserved [20]int8 + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]uint8 + Id [4]uint8 + User [32]uint8 + Host [256]uint8 + Exit exit_status + Session int32 + Tv timeval + Addr_v6 [4]int32 + X__glibc_reserved [20]uint8 } type exit_status struct { - Termination int16 - Exit int16 + Termination int16 + Exit int16 } -type UtTv struct { - TvSec int32 - TvUsec int32 +type timeval struct { + Sec int32 + Usec int32 } diff --git a/host/types_linux.go b/host/types_linux.go index 9285455..8adecb6 100644 --- a/host/types_linux.go +++ b/host/types_linux.go @@ -7,12 +7,11 @@ Input to cgo -godefs. package host /* -#define KERNEL #include #include enum { - sizeofPtr = sizeof(void*), + sizeofPtr = sizeof(void*), }; */ @@ -26,6 +25,7 @@ const ( sizeofInt = C.sizeof_int sizeofLong = C.sizeof_long sizeofLongLong = C.sizeof_longlong + sizeOfUtmp = C.sizeof_struct_utmp ) // Basic types @@ -39,7 +39,4 @@ type ( type utmp C.struct_utmp type exit_status C.struct_exit_status -type UtTv struct { - TvSec int32 - TvUsec int32 -} +type timeval C.struct_timeval diff --git a/internal/common/common.go b/internal/common/common.go index d9dce5c..e190f4d 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -117,6 +117,23 @@ func IntToString(orig []int8) string { return string(ret[0:size]) } +func UintToString(orig []uint8) string { + ret := make([]byte, len(orig)) + size := -1 + for i, o := range orig { + if o == 0 { + size = i + break + } + ret[i] = byte(o) + } + if size == -1 { + size = len(orig) + } + + return string(ret[0:size]) +} + func ByteToString(orig []byte) string { n := -1 l := -1 From b1df5a912c540f41cc88b24d4b4de0b2f1ad37a5 Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Sat, 23 Apr 2016 21:21:24 +0900 Subject: [PATCH 2/4] [host]linux: type file changed. --- host/host_linux.go | 6 ++--- host/host_linux_amd64.go | 58 ++++++++++++++++++++++++++---------------------- host/host_linux_arm.go | 12 +++++----- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/host/host_linux.go b/host/host_linux.go index 5b7907a..14d0935 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -124,9 +124,9 @@ func Users() ([]UserStat, error) { continue } user := UserStat{ - User: common.UintToString(u.User[:]), - Terminal: common.UintToString(u.Line[:]), - Host: common.UintToString(u.Host[:]), + 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_linux_amd64.go b/host/host_linux_amd64.go index 180394b..5847720 100644 --- a/host/host_linux_amd64.go +++ b/host/host_linux_amd64.go @@ -4,39 +4,45 @@ package host const ( - sizeofPtr = 0x8 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x8 - sizeofLongLong = 0x8 + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 ) type ( - _C_short int16 - _C_int int32 - _C_long int64 - _C_long_long int64 + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 ) type utmp struct { - Type int16 - Pad_cgo_0 [2]byte - Pid int32 - Line [32]int8 - ID [4]int8 - User [32]int8 - Host [256]int8 - Exit exit_status - Session int32 - Tv UtTv - Addr_v6 [4]int32 - X__glibc_reserved [20]int8 + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv _Ctype_struct___0 + Addr_v6 [4]int32 + X__glibc_reserved [20]int8 } type exit_status struct { - Termination int16 - Exit int16 + Termination int16 + Exit int16 } -type UtTv struct { - TvSec int32 - TvUsec int32 +type timeval struct { + Sec int64 + Usec int64 +} + +type _Ctype_struct___0 struct { + Sec int32 + Usec int32 } diff --git a/host/host_linux_arm.go b/host/host_linux_arm.go index 794f09e..75fb885 100644 --- a/host/host_linux_arm.go +++ b/host/host_linux_arm.go @@ -1,5 +1,5 @@ // Created by cgo -godefs - DO NOT EDIT -// cgo -godefs types_linux.go +// cgo -godefs types_linux.go | sed "s/uint8/int8/g" package host @@ -23,15 +23,15 @@ type utmp struct { Type int16 Pad_cgo_0 [2]byte Pid int32 - Line [32]uint8 - Id [4]uint8 - User [32]uint8 - Host [256]uint8 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 Exit exit_status Session int32 Tv timeval Addr_v6 [4]int32 - X__glibc_reserved [20]uint8 + X__glibc_reserved [20]int8 } type exit_status struct { Termination int16 From 15ff7e19df7f72d75f41948e1330f31df2c65904 Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Sat, 23 Apr 2016 21:34:27 +0900 Subject: [PATCH 3/4] [host]linux: fix i386 and gofmt. --- host/host_linux_386.go | 4 ++-- host/host_linux_amd64.go | 56 ++++++++++++++++++++++++------------------------ host/host_linux_arm.go | 52 ++++++++++++++++++++++---------------------- 3 files changed, 56 insertions(+), 56 deletions(-) diff --git a/host/host_linux_386.go b/host/host_linux_386.go index cab9fee..0035a5c 100644 --- a/host/host_linux_386.go +++ b/host/host_linux_386.go @@ -39,6 +39,6 @@ type exit_status struct { Exit int16 } type UtTv struct { - TvSec int32 - TvUsec int32 + Sec int32 + Usec int32 } diff --git a/host/host_linux_amd64.go b/host/host_linux_amd64.go index 5847720..9a69652 100644 --- a/host/host_linux_amd64.go +++ b/host/host_linux_amd64.go @@ -4,45 +4,45 @@ package host const ( - sizeofPtr = 0x8 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x8 - sizeofLongLong = 0x8 - sizeOfUtmp = 0x180 + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 ) type ( - _C_short int16 - _C_int int32 - _C_long int64 - _C_long_long int64 + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 ) type utmp struct { - Type int16 - Pad_cgo_0 [2]byte - Pid int32 - Line [32]int8 - Id [4]int8 - User [32]int8 - Host [256]int8 - Exit exit_status - Session int32 - Tv _Ctype_struct___0 - Addr_v6 [4]int32 - X__glibc_reserved [20]int8 + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv _Ctype_struct___0 + Addr_v6 [4]int32 + X__glibc_reserved [20]int8 } type exit_status struct { - Termination int16 - Exit int16 + Termination int16 + Exit int16 } type timeval struct { - Sec int64 - Usec int64 + Sec int64 + Usec int64 } type _Ctype_struct___0 struct { - Sec int32 - Usec int32 + Sec int32 + Usec int32 } diff --git a/host/host_linux_arm.go b/host/host_linux_arm.go index 75fb885..e2cf448 100644 --- a/host/host_linux_arm.go +++ b/host/host_linux_arm.go @@ -4,40 +4,40 @@ package host const ( - sizeofPtr = 0x4 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x4 - sizeofLongLong = 0x8 - sizeOfUtmp = 0x180 + sizeofPtr = 0x4 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x4 + sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 ) type ( - _C_short int16 - _C_int int32 - _C_long int32 - _C_long_long int64 + _C_short int16 + _C_int int32 + _C_long int32 + _C_long_long int64 ) type utmp struct { - Type int16 - Pad_cgo_0 [2]byte - Pid int32 - Line [32]int8 - Id [4]int8 - User [32]int8 - Host [256]int8 - Exit exit_status - Session int32 - Tv timeval - Addr_v6 [4]int32 - X__glibc_reserved [20]int8 + Type int16 + Pad_cgo_0 [2]byte + Pid int32 + Line [32]int8 + Id [4]int8 + User [32]int8 + Host [256]int8 + Exit exit_status + Session int32 + Tv timeval + Addr_v6 [4]int32 + X__glibc_reserved [20]int8 } type exit_status struct { - Termination int16 - Exit int16 + Termination int16 + Exit int16 } type timeval struct { - Sec int32 - Usec int32 + Sec int32 + Usec int32 } From ce98590a350ee29d156709f090ae38aad0107321 Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Sat, 23 Apr 2016 22:18:34 +0900 Subject: [PATCH 4/4] [host]linux: forgot to add sizeOfUtmp. --- host/host_linux_386.go | 1 + 1 file changed, 1 insertion(+) diff --git a/host/host_linux_386.go b/host/host_linux_386.go index 0035a5c..79b5cb5 100644 --- a/host/host_linux_386.go +++ b/host/host_linux_386.go @@ -11,6 +11,7 @@ const ( sizeofInt = 0x4 sizeofLong = 0x4 sizeofLongLong = 0x8 + sizeOfUtmp = 0x180 ) type (