Skip to content

Commit

Permalink
Use syscall instead of x/sys/* (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov authored and rs committed Aug 30, 2018
1 parent 4612dfc commit 15d2654
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
4 changes: 2 additions & 2 deletions hostid_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

package xid

import "golang.org/x/sys/unix"
import "syscall"

func readPlatformMachineID() (string, error) {
return unix.Sysctl("kern.uuid")
return syscall.Sysctl("kern.uuid")
}
4 changes: 2 additions & 2 deletions hostid_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

package xid

import "golang.org/x/sys/unix"
import "syscall"

func readPlatformMachineID() (string, error) {
return unix.Sysctl("kern.hostuuid")
return syscall.Sysctl("kern.hostuuid")
}
21 changes: 10 additions & 11 deletions hostid_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@ package xid

import (
"fmt"
"syscall"
"unsafe"

"golang.org/x/sys/windows"
)

func readPlatformMachineID() (string, error) {
// source: https://github.com/shirou/gopsutil/blob/master/host/host_windows.go
var h windows.Handle
err := windows.RegOpenKeyEx(windows.HKEY_LOCAL_MACHINE, windows.StringToUTF16Ptr(`SOFTWARE\Microsoft\Cryptography`), 0, windows.KEY_READ|windows.KEY_WOW64_64KEY, &h)
// source: https://github.com/shirou/gopsutil/blob/master/host/host_syscall.go
var h syscall.Handle
err := syscall.RegOpenKeyEx(syscall.HKEY_LOCAL_MACHINE, syscall.StringToUTF16Ptr(`SOFTWARE\Microsoft\Cryptography`), 0, syscall.KEY_READ|syscall.KEY_WOW64_64KEY, &h)
if err != nil {
return "", err
}
defer windows.RegCloseKey(h)
defer syscall.RegCloseKey(h)

const windowsRegBufLen = 74 // len(`{`) + len(`abcdefgh-1234-456789012-123345456671` * 2) + len(`}`) // 2 == bytes/UTF16
const syscallRegBufLen = 74 // len(`{`) + len(`abcdefgh-1234-456789012-123345456671` * 2) + len(`}`) // 2 == bytes/UTF16
const uuidLen = 36

var regBuf [windowsRegBufLen]uint16
bufLen := uint32(windowsRegBufLen)
var regBuf [syscallRegBufLen]uint16
bufLen := uint32(syscallRegBufLen)
var valType uint32
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`MachineGuid`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)
err = syscall.RegQueryValueEx(h, syscall.StringToUTF16Ptr(`MachineGuid`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)
if err != nil {
return "", err
}

hostID := windows.UTF16ToString(regBuf[:])
hostID := syscall.UTF16ToString(regBuf[:])
hostIDLen := len(hostID)
if hostIDLen != uuidLen {
return "", fmt.Errorf("HostID incorrect: %q\n", hostID)
Expand Down

0 comments on commit 15d2654

Please sign in to comment.