|
|
@@ -1,38 +1,81 @@ |
|
|
|
import errno |
|
|
|
import uuid |
|
|
|
import sys |
|
|
|
|
|
|
|
from ctypes import CDLL, Structure, POINTER |
|
|
|
from ctypes import c_uint8, c_int, c_long, c_int64 |
|
|
|
from ctypes import CDLL, Structure, POINTER, pointer |
|
|
|
from ctypes import c_char, c_uint8, c_int, c_long, c_int64, c_char_p |
|
|
|
from ctypes import c_void_p, c_size_t |
|
|
|
|
|
|
|
uuid_t = c_uint8 * 16 |
|
|
|
__all__ = [ 'hostuuid'] |
|
|
|
|
|
|
|
_clib = CDLL(None) |
|
|
|
|
|
|
|
__all__ = [ 'hostuuid'] |
|
|
|
if False: |
|
|
|
# XXX doesn't work on FreeBSD |
|
|
|
errnov = c_int.in_dll(_clib, 'errno') |
|
|
|
|
|
|
|
def raiseerrno(): |
|
|
|
v = errnov.value |
|
|
|
raise OSError(v, errno.errorcode[v]) |
|
|
|
|
|
|
|
if sys.platform == 'darwin': |
|
|
|
uuid_t = c_uint8 * 16 |
|
|
|
|
|
|
|
class timespec(Structure): |
|
|
|
_fields_ = [ |
|
|
|
('tv_sec', c_int64), |
|
|
|
('tv_nsec', c_long), |
|
|
|
] |
|
|
|
pass |
|
|
|
|
|
|
|
timespec_p = POINTER(timespec) |
|
|
|
|
|
|
|
def hostuuid(): |
|
|
|
ts = timespec() |
|
|
|
hid = uuid_t() |
|
|
|
|
|
|
|
r = _clib.gethostuuid(hid, ts) |
|
|
|
|
|
|
|
if r: |
|
|
|
raise OSErrorException() |
|
|
|
|
|
|
|
return uuid.UUID(bytes=bytes(hid)) |
|
|
|
|
|
|
|
class timespec(Structure): |
|
|
|
_fields_ = [ |
|
|
|
('tv_sec', c_int64), |
|
|
|
('tv_nsec', c_long), |
|
|
|
] |
|
|
|
pass |
|
|
|
for r, f, a in ( |
|
|
|
(c_int, 'gethostuuid', (uuid_t, timespec_p)), |
|
|
|
): |
|
|
|
fun = getattr(_clib, f) |
|
|
|
setattr(fun, 'restype', r) |
|
|
|
setattr(fun, 'argtypes', a) |
|
|
|
locals()[f] = fun |
|
|
|
|
|
|
|
timespec_p = POINTER(timespec) |
|
|
|
elif sys.platform.startswith('freebsd'): |
|
|
|
def hostuuid(): |
|
|
|
uuidstr = (c_char * 37)() |
|
|
|
uuidstrpointer = pointer(uuidstr) |
|
|
|
oldsize = c_size_t(len(uuidstr)) |
|
|
|
|
|
|
|
def hostuuid(): |
|
|
|
ts = timespec() |
|
|
|
hid = uuid_t() |
|
|
|
r = _clib.sysctlbyname('kern.hostuuid'.encode('us-ascii'), |
|
|
|
uuidstrpointer, pointer(oldsize), None, 0) |
|
|
|
|
|
|
|
r = _clib.gethostuuid(hid, ts) |
|
|
|
if r: |
|
|
|
raise OSError() |
|
|
|
|
|
|
|
if r: |
|
|
|
raise OSErrorException() |
|
|
|
v = uuid.UUID(uuidstr.value.decode('us-ascii')) |
|
|
|
if v == uuid.UUID(int=0): |
|
|
|
raise RuntimeError( |
|
|
|
'uuid from kern.hostuuid is all zeros') |
|
|
|
|
|
|
|
return uuid.UUID(bytes=bytes(hid)) |
|
|
|
return v |
|
|
|
|
|
|
|
for r, f, a in ( |
|
|
|
(c_int, 'gethostuuid', (uuid_t, timespec_p)), |
|
|
|
): |
|
|
|
fun = getattr(_clib, f) |
|
|
|
setattr(fun, 'restype', r) |
|
|
|
setattr(fun, 'argtypes', a) |
|
|
|
locals()[f] = fun |
|
|
|
for r, f, a in ( |
|
|
|
(c_int, 'sysctlbyname', (c_char_p, c_void_p, POINTER(c_size_t), |
|
|
|
c_void_p, c_size_t)), |
|
|
|
): |
|
|
|
fun = getattr(_clib, f) |
|
|
|
setattr(fun, 'restype', r) |
|
|
|
setattr(fun, 'argtypes', a) |
|
|
|
locals()[f] = fun |
|
|
|
else: |
|
|
|
raise RuntimeError('no hostuuid defined for platform: %s' % |
|
|
|
repr(sys.platform)) |