From 66eee5c39cda2d54a6d09c6a9d10fe1e7464aa21 Mon Sep 17 00:00:00 2001 From: uubulb Date: Tue, 13 Jan 2026 22:35:44 +0800 Subject: [PATCH] [host][darwin]: fix utmpx database parsing --- host/host_darwin.go | 20 +++++++------------- host/host_darwin_amd64.go | 24 ++++++++++++------------ host/host_darwin_arm64.go | 12 ++++++------ host/types_darwin.go | 18 +++++++++++++++--- 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/host/host_darwin.go b/host/host_darwin.go index 2764ce9..ba96afd 100644 --- a/host/host_darwin.go +++ b/host/host_darwin.go @@ -4,9 +4,7 @@ package host import ( - "bytes" "context" - "encoding/binary" "errors" "io" "os" @@ -65,20 +63,16 @@ func UsersWithContext(_ context.Context) ([]UserStat, error) { } // Skip macOS utmpx header part - buf = buf[604:] + const ut32Size = int(unsafe.Sizeof(utmpx32{})) + buf = buf[ut32Size:] - entrySize := int(unsafe.Sizeof(Utmpx{})) - count := len(buf) / entrySize + count := len(buf) / ut32Size - for i := 0; i < count; i++ { - b := buf[i*entrySize : i*entrySize+entrySize] + for i := range count { + b := buf[i*ut32Size : i*ut32Size+ut32Size] - var u Utmpx - br := bytes.NewReader(b) - err := binary.Read(br, binary.LittleEndian, &u) - if err != nil { - continue - } + var u utmpx32 + copy(unsafe.Slice((*byte)(unsafe.Pointer(&u)), len(b)), b) if u.Type != user_PROCESS { continue } diff --git a/host/host_darwin_amd64.go b/host/host_darwin_amd64.go index 1efc353..b27cd46 100644 --- a/host/host_darwin_amd64.go +++ b/host/host_darwin_amd64.go @@ -4,18 +4,18 @@ 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 utmpx32 struct { + User [256]int8 + Id [4]int8 + Line [32]int8 + Pid int32 + Type int16 + Tv timeval32 + Host [256]int8 + Pad [16]uint32 } -type Timeval struct { - Sec int32 +type timeval32 struct { + Sec int32 + Usec int32 } diff --git a/host/host_darwin_arm64.go b/host/host_darwin_arm64.go index 512e569..4a26948 100644 --- a/host/host_darwin_arm64.go +++ b/host/host_darwin_arm64.go @@ -6,18 +6,18 @@ package host -type Utmpx struct { +type utmpx32 struct { User [256]int8 Id [4]int8 Line [32]int8 Pid int32 Type int16 - Tv Timeval + Tv timeval32 Host [256]int8 Pad [16]uint32 } -type Timeval struct { - Sec int64 - Usec int32 - Pad_cgo_0 [4]byte + +type timeval32 struct { + Sec int32 + Usec int32 } diff --git a/host/types_darwin.go b/host/types_darwin.go index 4e35ab7..7c92e55 100644 --- a/host/types_darwin.go +++ b/host/types_darwin.go @@ -10,12 +10,24 @@ Input to cgo -godefs. package host /* -#include #include +#include + +// https://github.com/apple-oss-distributions/Libc/blob/55b54c0a0c37b3b24393b42b90a4c561d6c606b1/gen/utmpx-darwin.h#L86 +struct utmpx32 { + char ut_user[_UTX_USERSIZE]; + char ut_id[_UTX_IDSIZE]; + char ut_line[_UTX_LINESIZE]; + pid_t ut_pid; + short ut_type; + struct timeval32 ut_tv; + char ut_host[_UTX_HOSTSIZE]; + __uint32_t ut_pad[16]; +}; */ import "C" type ( - Utmpx C.struct_utmpx - Timeval C.struct_timeval + utmpx32 C.struct_utmpx32 + timeval32 C.struct_timeval32 ) -- knotmirror 0.0.1