Commit 22bdf236 authored by Simon McVittie's avatar Simon McVittie

Add dbus_try_get_local_machine_id()

Signed-off-by: Simon McVittie's avatarSimon McVittie <smcv@collabora.com>
Reviewed-by: Philip Withnall's avatarPhilip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=13194
parent 6f751caf
......@@ -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;
}
......@@ -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
*
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment