Skip to content

Commit

Permalink
Merge pull request #1059 from d1ss0nanz/issue1058
Browse files Browse the repository at this point in the history
use ioreg to read IOPlatformUUID as HostID
  • Loading branch information
shirou authored Oct 30, 2021
2 parents 9928258 + 9916462 commit e032a1c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
21 changes: 19 additions & 2 deletions host/host_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"context"
"encoding/binary"
"errors"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -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) {
Expand Down
21 changes: 19 additions & 2 deletions v3/host/host_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"context"
"encoding/binary"
"errors"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -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) {
Expand Down

0 comments on commit e032a1c

Please sign in to comment.