From 5af75768951345fca21366af48f165f707779600 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 17 Feb 2015 13:46:53 +0000 Subject: [PATCH] dbus-launch: use libdbus to read the UUID As a side benefit, this means that dbus-launch now understands /etc/machine-id and not just /var/lib/dbus/machine-id. I'm using the "internal" (static) version of libdbus rather than the shared version, because my next commit is going to need that anyway; with that in mind, it makes sense to switch machine_uuid to be allocated with dbus_malloc, rather than having to copy it from malloc-allocated to dbus_malloc-allocated storage. However, I'm deliberately not changing the allocation model of any other strings in dbus-launch right now. --- tools/Makefile.am | 17 ++++++++++++----- tools/dbus-launch.c | 35 +++++------------------------------ tools/dbus-launch.h | 2 ++ 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/tools/Makefile.am b/tools/Makefile.am index 68a5970..6ab64ab 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -51,7 +51,18 @@ else dbus_launch_SOURCES= \ dbus-launch.c \ dbus-launch-x11.c \ - dbus-launch.h + dbus-launch.h \ + tool-common.c \ + tool-common.h \ + $(NULL) +dbus_launch_CPPFLAGS = \ + $(AM_CPPFLAGS) \ + -DDBUS_STATIC_BUILD \ + $(NULL) +dbus_launch_LDADD = \ + $(top_builddir)/dbus/libdbus-internal.la \ + $(DBUS_X_LIBS) \ + $(NULL) dbus_run_session_SOURCES = \ dbus-run-session.c @@ -80,10 +91,6 @@ dbus_uuidgen_LDADD = \ $(top_builddir)/dbus/libdbus-1.la \ $(NULL) -dbus_launch_LDADD = \ - $(DBUS_X_LIBS) \ - $(NULL) - examplesdir = ${docdir}/examples dist_examples_SCRIPTS = \ GetAllMatchRules.py \ diff --git a/tools/dbus-launch.c b/tools/dbus-launch.c index 41a20e8..787ed48 100644 --- a/tools/dbus-launch.c +++ b/tools/dbus-launch.c @@ -102,50 +102,25 @@ save_machine_uuid (const char *uuid_arg) exit (1); } - machine_uuid = xstrdup (uuid_arg); + machine_uuid = _dbus_strdup (uuid_arg); } #ifdef DBUS_BUILD_X11 -#define UUID_MAXLEN 40 /* Read the machine uuid from file if needed. Returns TRUE if machine_uuid is * set after this function */ static int read_machine_uuid_if_needed (void) { - FILE *f; - char uuid[UUID_MAXLEN]; - size_t len; - int ret = FALSE; - if (machine_uuid != NULL) return TRUE; - f = fopen (DBUS_MACHINE_UUID_FILE, "r"); - if (f == NULL) - return FALSE; - - if (fgets (uuid, UUID_MAXLEN, f) == NULL) - goto out; - - len = strlen (uuid); - if (len < 32) - goto out; + machine_uuid = dbus_get_local_machine_id (); - /* rstrip the read uuid */ - while (len > 31 && isspace((int) uuid[len - 1])) - len--; - - if (len != 32) - goto out; + if (machine_uuid == NULL) + return FALSE; - uuid[len] = '\0'; - machine_uuid = xstrdup (uuid); verbose ("UID: %s\n", machine_uuid); - ret = TRUE; - -out: - fclose(f); - return ret; + return TRUE; } #endif /* DBUS_BUILD_X11 */ diff --git a/tools/dbus-launch.h b/tools/dbus-launch.h index 8220bb8..d0ede6b 100644 --- a/tools/dbus-launch.h +++ b/tools/dbus-launch.h @@ -26,6 +26,8 @@ #include +#include + #ifndef TRUE #define TRUE (1) #endif -- 2.1.4