From 9916462c47ad77dddeb7fb7ab82ac8fa2c909529 Mon Sep 17 00:00:00 2001 From: Daniel Stutz Date: Tue, 13 Apr 2021 10:31:23 +0200 Subject: [PATCH] use ioreg to read IOPlatformUUID as HostID --- host/host_darwin.go | 21 +++++++++++++++++++-- v3/host/host_darwin.go | 21 +++++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/host/host_darwin.go b/host/host_darwin.go index 8f51b20..d3e23cf 100644 --- a/host/host_darwin.go +++ b/host/host_darwin.go @@ -6,6 +6,7 @@ import ( "bytes" "context" "encoding/binary" + "errors" "io/ioutil" "os" "os/exec" @@ -21,11 +22,27 @@ import ( const USER_PROCESS = 7 func HostIDWithContext(ctx context.Context) (string, error) { - uuid, err := unix.Sysctl("kern.uuid") + ioreg, err := exec.LookPath("ioreg") if err != nil { return "", err } - return strings.ToLower(uuid), err + + out, err := invoke.CommandWithContext(ctx, ioreg, "-rd1", "-c", "IOPlatformExpertDevice") + if err != nil { + return "", err + } + + for _, line := range strings.Split(string(out), "\n") { + if strings.Contains(line, "IOPlatformUUID") { + parts := strings.SplitAfter(line, `" = "`) + if len(parts) == 2 { + uuid := strings.TrimRight(parts[1], `"`) + return strings.ToLower(uuid), nil + } + } + } + + return "", errors.New("cannot find host id") } func numProcs(ctx context.Context) (uint64, error) { diff --git a/v3/host/host_darwin.go b/v3/host/host_darwin.go index 9aadc54..6ac7a82 100644 --- a/v3/host/host_darwin.go +++ b/v3/host/host_darwin.go @@ -6,6 +6,7 @@ import ( "bytes" "context" "encoding/binary" + "errors" "io/ioutil" "os" "os/exec" @@ -21,11 +22,27 @@ import ( const user_PROCESS = 7 func HostIDWithContext(ctx context.Context) (string, error) { - uuid, err := unix.Sysctl("kern.uuid") + ioreg, err := exec.LookPath("ioreg") if err != nil { return "", err } - return strings.ToLower(uuid), err + + out, err := invoke.CommandWithContext(ctx, ioreg, "-rd1", "-c", "IOPlatformExpertDevice") + if err != nil { + return "", err + } + + for _, line := range strings.Split(string(out), "\n") { + if strings.Contains(line, "IOPlatformUUID") { + parts := strings.SplitAfter(line, `" = "`) + if len(parts) == 2 { + uuid := strings.TrimRight(parts[1], `"`) + return strings.ToLower(uuid), nil + } + } + } + + return "", errors.New("cannot find host id") } func numProcs(ctx context.Context) (uint64, error) {