From 5c7f57b3e564f1164d1132fff34bc4ad3091ebbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Sun, 30 Oct 2022 13:54:40 +0100 Subject: [PATCH] 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 --- gio/gdbusprivate.c | 50 ++++++++++++++++++++++++++++++++++++++++++- gio/meson.build | 9 +++++++- gio/tests/meson.build | 5 +---- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/gio/gdbusprivate.c b/gio/gdbusprivate.c index 762afcee4..1483dacb3 100644 --- a/gio/gdbusprivate.c +++ b/gio/gdbusprivate.c @@ -61,6 +61,11 @@ #include "gwin32sid.h" #endif +#ifdef G_OS_DARWIN +#include +#include +#endif + #include "glibintl.h" static gboolean _g_dbus_worker_do_initial_read (gpointer data); @@ -2424,9 +2429,10 @@ _g_dbus_get_machine_id (GError **error) return res; #else gchar *ret = NULL; - GError *first_error = NULL; gsize i; 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 * 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 */ 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`: * > The machine ID is a single newline-terminated, hexadecimal, 32-character, * > 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 !defined (HAVE_COCOA) g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Invalid machine ID in %s or %s", 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); return NULL; } diff --git a/gio/meson.build b/gio/meson.build index d16fc5230..7e9c94d55 100644 --- a/gio/meson.build +++ b/gio/meson.build @@ -393,7 +393,14 @@ if host_system != 'windows' settings_sources += files('gnextstepsettingsbackend.m') contenttype_sources += files('gosxcontenttype.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] if glib_have_os_x_9_or_later unix_sources += files('gcocoanotificationbackend.m') diff --git a/gio/tests/meson.build b/gio/tests/meson.build index 5e8b6ad19..790476f60 100644 --- a/gio/tests/meson.build +++ b/gio/tests/meson.build @@ -117,10 +117,7 @@ gio_tests = { 'tls-database' : {'extra_sources' : ['gtesttlsbackend.c']}, 'tls-bindings' : {'extra_sources' : ['gtesttlsbackend.c']}, 'unix-fd' : {}, - 'gdbus-address-get-session' : { - # FIXME: https://gitlab.gnome.org/GNOME/glib/-/issues/1392 - 'should_fail' : host_system == 'darwin', - }, + 'gdbus-address-get-session' : {}, 'win32-appinfo' : {}, }