From 7e4ca8c4d2f3044d1d6561693129606ae65afbef Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Wed, 7 Jun 2017 17:26:03 +0100 Subject: [PATCH 14/14] Add dbus_try_get_local_machine_id() Signed-off-by: Simon McVittie --- dbus/dbus-misc.c | 67 ++++++++++++++++++++++++++++++++++++++---------------- dbus/dbus-misc.h | 3 +++ test/dbus-daemon.c | 24 ++++++------------- 3 files changed, 58 insertions(+), 36 deletions(-) diff --git a/dbus/dbus-misc.c b/dbus/dbus-misc.c index 851fa2b1..2be58a25 100644 --- a/dbus/dbus-misc.c +++ b/dbus/dbus-misc.c @@ -67,42 +67,31 @@ * The UUID is not a UUID in the sense of RFC4122; the details * are explained in the D-Bus specification. * - * This function returns #NULL if there was not enough memory to read - * the UUID, or if the UUID could not be read because the D-Bus - * library was installed incorrectly. In the latter case, a warning - * is logged. - * * @returns a 32-byte-long hex-encoded UUID string, or #NULL on failure */ -char* -dbus_get_local_machine_id (void) +char * +dbus_try_get_local_machine_id (DBusError *error) { - DBusError error = DBUS_ERROR_INIT; DBusString uuid; char *s; s = NULL; if (!_dbus_string_init (&uuid)) - return NULL; - - /* The documentation says dbus_get_local_machine_id() only fails on OOM; - * this can actually also fail if the D-Bus installation is faulty - * (no UUID), but we have no good way to report that. Historically, - * _dbus_get_local_machine_uuid_encoded was responsible for issuing the - * warning; now we do that here. */ - if (!_dbus_get_local_machine_uuid_encoded (&uuid, &error)) { - if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY)) - _dbus_warn_check_failed ("%s", error.message); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + return NULL; + } + if (!_dbus_get_local_machine_uuid_encoded (&uuid, error)) + { _dbus_string_free (&uuid); - dbus_error_free (&error); return NULL; } if (!_dbus_string_steal_data (&uuid, &s)) { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (&uuid); return NULL; } @@ -115,6 +104,46 @@ dbus_get_local_machine_id (void) } /** + * Obtains the machine UUID of the machine this process is running on. + * + * The returned string must be freed with dbus_free(). + * + * This function returns #NULL if there was not enough memory to read + * the UUID, or if the UUID could not be read because the D-Bus + * library was installed incorrectly. In the latter case, a warning + * is logged. + * + * Other than its deficient error reporting, this function is the same as + * dbus_try_get_local_machine_id(). + * + * @returns a 32-byte-long hex-encoded UUID string, or #NULL on failure + */ +char * +dbus_get_local_machine_id (void) +{ + DBusError error = DBUS_ERROR_INIT; + char *s; + + s = dbus_try_get_local_machine_id (&error); + + /* The documentation says dbus_get_local_machine_id() only fails on OOM; + * this can actually also fail if the D-Bus installation is faulty + * (no UUID), but we have no good way to report that. Historically, + * _dbus_get_local_machine_uuid_encoded was responsible for issuing the + * warning; now we do that here. */ + if (s == NULL) + { + if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY)) + _dbus_warn_check_failed ("%s", error.message); + + dbus_error_free (&error); + return NULL; + } + + return s; +} + +/** * @def DBUS_MAJOR_VERSION * * The COMPILE TIME major version of libdbus, that is, the "X" in "X.Y.Z", diff --git a/dbus/dbus-misc.h b/dbus/dbus-misc.h index 6e72d9ed..d78d7993 100644 --- a/dbus/dbus-misc.h +++ b/dbus/dbus-misc.h @@ -48,6 +48,9 @@ DBUS_EXPORT dbus_bool_t dbus_setenv (const char *variable, const char *value); +DBUS_EXPORT +char *dbus_try_get_local_machine_id (DBusError *error); + /** @} */ DBUS_END_DECLS diff --git a/test/dbus-daemon.c b/test/dbus-daemon.c index 5087411c..57d7ceea 100644 --- a/test/dbus-daemon.c +++ b/test/dbus-daemon.c @@ -37,8 +37,6 @@ #include #include "bus/stats.h" -#include "dbus/dbus-internals.h" -#include "dbus/dbus-string.h" #include "test-utils-glib.h" #include @@ -1055,21 +1053,18 @@ static void test_peer_get_machine_id (Fixture *f, gconstpointer context) { - DBusString what_i_think; + char *what_i_think; const char *what_daemon_thinks; DBusMessage *m = NULL; DBusPendingCall *pc = NULL; DBusError error = DBUS_ERROR_INIT; - DBusGUID uuid; if (f->skip) return; - /* Unlike dbus_get_local_machine_id(), this does not consider it to be - * a fatal error if dbus is not correctly installed, which is - * useful during build-time tests on a system where dbus might not be - * installed at all. */ - if (!_dbus_read_local_machine_uuid (&uuid, FALSE, &error)) + what_i_think = dbus_try_get_local_machine_id (&error); + + if (what_i_think == NULL) { if (g_getenv ("DBUS_TEST_UNINSTALLED") != NULL) { @@ -1081,14 +1076,10 @@ test_peer_get_machine_id (Fixture *f, else { /* When running integration tests, don't tolerate it */ - g_error ("dbus not installed correctly: machine UUID not available"); + g_error ("%s", error.message); } } - if (!_dbus_string_init (&what_i_think) || - !_dbus_uuid_encode (&uuid, &what_i_think)) - g_error ("OOM"); - /* Check that the dbus-daemon agrees with us. */ m = dbus_message_new_method_call (DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, @@ -1118,14 +1109,13 @@ test_peer_get_machine_id (Fixture *f, DBUS_TYPE_INVALID)) g_error ("%s: %s", error.name, error.message); - g_assert_cmpstr (_dbus_string_get_const_data (&what_i_think), ==, - what_daemon_thinks); + g_assert_cmpstr (what_i_think, ==, what_daemon_thinks); g_assert_nonnull (what_daemon_thinks); g_assert_cmpuint (strlen (what_daemon_thinks), ==, 32); dbus_message_unref (m); dbus_pending_call_unref (pc); - _dbus_string_free (&what_i_think); + dbus_free (what_i_think); } static void -- 2.11.0