gdbusprivate: Add support for machine-id for darwin systems

Get the machine ID from ioreg in MacOS, using ioreg to fetch IOPlatformUUID
from IOPlatformExpertDevice.
This seems to be the best way to get the hardware UUID value in such systems.

Helps: https://gitlab.gnome.org/GNOME/glib/-/issues/1392
This commit is contained in:
Marco Trevisan (Treviño) 2022-10-30 13:54:40 +01:00
parent 9635fd4e40
commit 5c7f57b3e5
3 changed files with 58 additions and 6 deletions

View File

@ -61,6 +61,11 @@
#include "gwin32sid.h" #include "gwin32sid.h"
#endif #endif
#ifdef G_OS_DARWIN
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#endif
#include "glibintl.h" #include "glibintl.h"
static gboolean _g_dbus_worker_do_initial_read (gpointer data); static gboolean _g_dbus_worker_do_initial_read (gpointer data);
@ -2424,9 +2429,10 @@ _g_dbus_get_machine_id (GError **error)
return res; return res;
#else #else
gchar *ret = NULL; gchar *ret = NULL;
GError *first_error = NULL;
gsize i; gsize i;
gboolean non_zero = FALSE; gboolean non_zero = FALSE;
#if !defined (G_OS_DARWIN)
GError *first_error = NULL;
/* Copy what dbus.git does: allow the /var/lib path to be configurable at /* Copy what dbus.git does: allow the /var/lib path to be configurable at
* build time, but hard-code the system-wide machine ID path in /etc. */ * build time, but hard-code the system-wide machine ID path in /etc. */
@ -2452,6 +2458,43 @@ _g_dbus_get_machine_id (GError **error)
/* ignore the error from the first try, if any */ /* ignore the error from the first try, if any */
g_clear_error (&first_error); g_clear_error (&first_error);
#else /* defined (HAVE_COCOA) */
CFMutableDictionaryRef matching;
CFStringRef io_platform_uuid;
io_service_t service;
const char *platform_uuid;
size_t platform_uuid_length;
matching = IOServiceMatching ("IOPlatformExpertDevice");
service = IOServiceGetMatchingService (kIOMasterPortDefault, matching);
io_platform_uuid =
IORegistryEntryCreateCFProperty (service, CFSTR ("IOPlatformUUID"),
kCFAllocatorDefault, 0);
platform_uuid = CFStringGetCStringPtr (io_platform_uuid, kCFStringEncodingASCII);
if (platform_uuid == NULL)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Failed to get IOPlatformUUID from IOPlatformExpertDevice"));
return FALSE;
}
platform_uuid_length = strlen (platform_uuid);
ret = g_new0 (char, platform_uuid_length + 2);
size_t j = 0;
for (i = 0; i < platform_uuid_length; i++)
{
if (g_ascii_isxdigit (platform_uuid[i]))
ret[j++] = g_ascii_tolower (platform_uuid[i]);
}
ret[j] = '\n';
IOObjectRelease (service);
CFRelease (io_platform_uuid);
#endif /* !defined (HAVE_COCOA) */
/* Validate the machine ID. From `man 5 machine-id`: /* Validate the machine ID. From `man 5 machine-id`:
* > The machine ID is a single newline-terminated, hexadecimal, 32-character, * > The machine ID is a single newline-terminated, hexadecimal, 32-character,
* > lowercase ID. When decoded from hexadecimal, this corresponds to a * > lowercase ID. When decoded from hexadecimal, this corresponds to a
@ -2469,9 +2512,14 @@ _g_dbus_get_machine_id (GError **error)
if (i != 32 || ret[i] != '\n' || ret[i + 1] != '\0' || !non_zero) if (i != 32 || ret[i] != '\n' || ret[i + 1] != '\0' || !non_zero)
{ {
#if !defined (HAVE_COCOA)
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Invalid machine ID in %s or %s", "Invalid machine ID in %s or %s",
var_lib_path, etc_path); var_lib_path, etc_path);
#else /* defined (HAVE_COCOA) */
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Invalid machine ID from IOPlatformUUID"));
#endif /* !defined (HAVE_COCOA) */
g_free (ret); g_free (ret);
return NULL; return NULL;
} }

View File

@ -393,7 +393,14 @@ if host_system != 'windows'
settings_sources += files('gnextstepsettingsbackend.m') settings_sources += files('gnextstepsettingsbackend.m')
contenttype_sources += files('gosxcontenttype.m') contenttype_sources += files('gosxcontenttype.m')
appinfo_sources += files('gosxappinfo.m') appinfo_sources += files('gosxappinfo.m')
framework_dep = dependency('appleframeworks', modules : ['Foundation', 'CoreFoundation', 'AppKit']) framework_dep = dependency('appleframeworks',
modules : [
'AppKit',
'CoreFoundation',
'Foundation',
'IOKit',
],
)
platform_deps += [framework_dep] platform_deps += [framework_dep]
if glib_have_os_x_9_or_later if glib_have_os_x_9_or_later
unix_sources += files('gcocoanotificationbackend.m') unix_sources += files('gcocoanotificationbackend.m')

View File

@ -117,10 +117,7 @@ gio_tests = {
'tls-database' : {'extra_sources' : ['gtesttlsbackend.c']}, 'tls-database' : {'extra_sources' : ['gtesttlsbackend.c']},
'tls-bindings' : {'extra_sources' : ['gtesttlsbackend.c']}, 'tls-bindings' : {'extra_sources' : ['gtesttlsbackend.c']},
'unix-fd' : {}, 'unix-fd' : {},
'gdbus-address-get-session' : { 'gdbus-address-get-session' : {},
# FIXME: https://gitlab.gnome.org/GNOME/glib/-/issues/1392
'should_fail' : host_system == 'darwin',
},
'win32-appinfo' : {}, 'win32-appinfo' : {},
} }