Least-worst option :-)
Do you think it makes sense to return a semi-descriptive string for
some of the error cases?
On Sat, Aug 13, 2011 at 6:16 AM, <russell(a)russellbryant.net> wrote:
From: Russell Bryant <rbryant(a)redhat.com>
This works by just parsing the output of dmidecode. This seemed like
the most reliable way to get this information.
Signed-off-by: Russell Bryant <rbryant(a)redhat.com>
---
matahari.spec.in | 1 +
src/CMakeLists.txt | 8 +++++
src/lib/host_linux.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 80 insertions(+), 1 deletions(-)
diff --git a/matahari.spec.in b/matahari.spec.in
index 74fa2a6..f1ad227 100644
--- a/matahari.spec.in
+++ b/matahari.spec.in
@@ -31,6 +31,7 @@ BuildRoot:
%{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
Requires: dbus
Requires: qmf > 0.7
Requires: pcre
+Requires: dmidecode
BuildRequires: cmake
BuildRequires: libudev-devel
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d9c77c6..2291399 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -96,6 +96,14 @@ if(CURL STREQUAL "CURL-NOTFOUND")
message(FATAL_ERROR "libcurl not found.")
endif(CURL STREQUAL "CURL-NOTFOUND")
+# dmidecode
+if(NOT WIN32)
+ find_program(DMIDECODE dmidecode)
+ if(DMIDECODE STREQUAL "DMIDECODE-NOTFOUND")
+ message(FATAL_ERROR "dmidecode not found")
+ endif(DMIDECODE STREQUAL "DMIDECODE-NOTFOUND")
+endif(NOT WIN32)
+
# Include QMF and DBus generation macros
include(MatahariMacros)
diff --git a/src/lib/host_linux.c b/src/lib/host_linux.c
index 8d1aaef..a6999a3 100644
--- a/src/lib/host_linux.c
+++ b/src/lib/host_linux.c
@@ -164,7 +164,77 @@ host_os_identify(void)
char *host_os_machine_uuid(void)
{
- return strdup("not-implemented");
+ gchar *output = NULL;
+ gchar **lines = NULL;
+ unsigned int i;
+ static const gint max_lines = 256;
+ pcre *regex;
+ const char *pcre_error = NULL;
+ int pcre_error_offset = 0;
+ static const char regex_str[] = "\\s+UUID:\\s+(.*)";
+ GError *error = NULL;
+ gboolean res;
+ char *uuid = NULL;
+ gchar *argv[] = { "dmidecode", "-t", "system", NULL
};
+
+ /*
+ * libsmbios doesn't expose the UUID. dmidecode already does a good job
+ * of getting it, but it doesn't have a library. Executing dmidecode
+ * doesn't seem pretty, but it should at least provide reliable info.
+ */
+
+ res = g_spawn_sync(NULL, argv, NULL,
+ G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL,
+ NULL, NULL, &output, NULL, NULL, &error);
+
+ if (res == FALSE) {
+ mh_err("Failed to run dmidecode to get UUID: %s\n",
error->message);
+ g_error_free(error);
+ error = NULL;
+ }
+
+ if (!output) {
+ mh_err("Got no output from dmidecode when trying to get UUID.\n");
+ return strdup("");
+ }
+
+ lines = g_strsplit(output, "\n", max_lines);
+
+ g_free(output);
+ output = NULL;
+
+ regex = pcre_compile(regex_str, 0, &pcre_error, &pcre_error_offset, NULL);
+ if (!regex) {
+ mh_err("Unable to compile regular expression '%s' at offset %d:
%s",
+ regex_str, pcre_error_offset, pcre_error);
+ uuid = strdup("");
+ goto cleanup;
+ }
+
+ for (i = 0; lines && lines[i]; i++) {
+ int match;
+ int found[8] = { 0, };
+
+ match = pcre_exec(regex, NULL, lines[i], strlen(lines[i]),
+ 0, 0, found,
+ sizeof(found) / sizeof(found[0]));
+
+ if (match == 2) {
+ uuid = strdup(lines[i] + found[2]);
+ break;
+ }
+ }
+
+cleanup:
+ if (lines) {
+ g_strfreev(lines);
+ }
+
+ if (regex) {
+ pcre_free(regex);
+ }
+
+ return uuid;
}
char *host_os_custom_uuid(void)
--
1.7.6
_______________________________________________
Matahari mailing list
Matahari(a)lists.fedorahosted.org
https://fedorahosted.org/mailman/listinfo/matahari