]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
Add dbus_try_get_local_machine_id()
authorSimon McVittie <smcv@collabora.com>
Wed, 7 Jun 2017 16:26:03 +0000 (17:26 +0100)
committerSimon McVittie <smcv@collabora.com>
Thu, 8 Jun 2017 17:31:10 +0000 (18:31 +0100)
Signed-off-by: Simon McVittie <smcv@collabora.com>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=13194

dbus/dbus-misc.c
dbus/dbus-misc.h
test/dbus-daemon.c

index 851fa2b102f23d1292262f4a3152a59ba83850d7..2be58a25b79f92b49e208d3c6916c79b37de006e 100644 (file)
  * 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;
     }
@@ -114,6 +103,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
  *
index 6e72d9ed25805aad348313152deef6817c4c2067..d78d79931ef2b41965b775f9e9f3bbc6922b7bcb 100644 (file)
@@ -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
index 5087411cbdd19edcd8eb3b0ce9324948e8493e44..57d7ceeaf58b35a2f3703ca88b0acc026e8046ca 100644 (file)
@@ -37,8 +37,6 @@
 #include <gio/gio.h>
 
 #include "bus/stats.h"
-#include "dbus/dbus-internals.h"
-#include "dbus/dbus-string.h"
 #include "test-utils-glib.h"
 
 #include <string.h>
@@ -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