2 * collectd - src/uuid.c
3 * Copyright (C) 2007 Red Hat Inc.
4 * Copyright (C) 2015 Ruben Kerkhof
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Dan Berrange <berrange@redhat.com>
21 * Richard W.M. Jones <rjones@redhat.com>
23 * Derived from UUID detection code by Dan Berrange <berrange@redhat.com>
24 * http://hg.et.redhat.com/virt/daemons/spectre--devel?f=f6e3a1b06433;file=lib/uuid.c
33 #include <sys/sysctl.h>
40 #define UUID_RAW_LENGTH 16
41 #define UUID_PRINTABLE_COMPACT_LENGTH (UUID_RAW_LENGTH * 2)
42 #define UUID_PRINTABLE_NORMAL_LENGTH (UUID_PRINTABLE_COMPACT_LENGTH + 4)
44 static char *uuidfile = NULL;
46 static const char *config_keys[] = {"UUIDFile"};
48 static int looks_like_a_uuid(const char *uuid) {
56 if (len < UUID_PRINTABLE_COMPACT_LENGTH)
60 if (!isxdigit((int)*uuid) && *uuid != '-')
67 static char *uuid_parse_dmidecode(FILE *file) {
70 while (fgets(line, sizeof(line), file) != NULL) {
74 strstripnewline(line);
76 /* Look for a line reading:
77 * UUID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
79 fields_num = strsplit(line, fields, STATIC_ARRAY_SIZE(fields));
83 if (strcmp("UUID:", fields[0]) != 0)
86 if (!looks_like_a_uuid(fields[1]))
89 return (strdup(fields[1]));
94 static char *uuid_get_from_dmidecode(void) {
95 FILE *dmidecode = popen("dmidecode -t system 2>/dev/null", "r");
101 uuid = uuid_parse_dmidecode(dmidecode);
107 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
108 static char *uuid_get_from_sysctlbyname(const char *name) {
109 char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1];
110 size_t len = sizeof(uuid);
111 if (sysctlbyname(name, &uuid, &len, NULL, 0) == -1)
113 return (strdup(uuid));
115 #elif defined(__OpenBSD__)
116 static char *uuid_get_from_sysctl(void) {
117 char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1];
118 size_t len = sizeof(uuid);
124 if (sysctl(mib, 2, uuid, &len, NULL, 0) == -1)
126 return (strdup(uuid));
132 #define UUID_PATH "/org/freedesktop/Hal/devices/computer"
133 #define UUID_PROPERTY "smbios.system.uuid"
135 static char *uuid_get_from_hal(void) {
141 dbus_error_init(&error);
143 if (!(con = dbus_bus_get(DBUS_BUS_SYSTEM, &error)))
146 ctx = libhal_ctx_new();
147 libhal_ctx_set_dbus_connection(ctx, con);
149 if (!libhal_ctx_init(ctx, &error))
152 if (!libhal_device_property_exists(ctx, UUID_PATH, UUID_PROPERTY, &error))
156 libhal_device_get_property_string(ctx, UUID_PATH, UUID_PROPERTY, &error);
157 if (looks_like_a_uuid(uuid))
162 dbus_error_init(&ctxerror);
163 if (!(libhal_ctx_shutdown(ctx, &ctxerror)))
164 dbus_error_free(&ctxerror);
167 libhal_ctx_free(ctx);
170 if (dbus_error_is_set(&error))
171 dbus_error_free(&error);
176 static char *uuid_get_from_file(const char *path) {
178 char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1] = "";
180 file = fopen(path, "r");
184 if (!fgets(uuid, sizeof(uuid), file)) {
189 strstripnewline(uuid);
191 return (strdup(uuid));
194 static char *uuid_get_local(void) {
197 /* Check /etc/uuid / UUIDFile before any other method. */
198 if ((uuid = uuid_get_from_file(uuidfile ? uuidfile : "/etc/uuid")) != NULL)
201 #if defined(__APPLE__)
202 if ((uuid = uuid_get_from_sysctlbyname("kern.uuid")) != NULL)
204 #elif defined(__FreeBSD__)
205 if ((uuid = uuid_get_from_sysctlbyname("kern.hostuuid")) != NULL)
207 #elif defined(__NetBSD__)
208 if ((uuid = uuid_get_from_sysctlbyname("machdep.dmi.system-uuid")) != NULL)
210 #elif defined(__OpenBSD__)
211 if ((uuid = uuid_get_from_sysctl()) != NULL)
213 #elif defined(__linux__)
214 if ((uuid = uuid_get_from_file("/sys/class/dmi/id/product_uuid")) != NULL)
219 if ((uuid = uuid_get_from_hal()) != NULL)
223 if ((uuid = uuid_get_from_dmidecode()) != NULL)
226 #if defined(__linux__)
227 if ((uuid = uuid_get_from_file("/sys/hypervisor/uuid")) != NULL)
234 static int uuid_config(const char *key, const char *value) {
235 if (strcasecmp(key, "UUIDFile") == 0) {
236 char *tmp = strdup(value);
247 static int uuid_init(void) {
248 char *uuid = uuid_get_local();
251 sstrncpy(hostname_g, uuid, DATA_MAX_NAME_LEN);
256 WARNING("uuid: could not read UUID using any known method");
260 void module_register(void) {
261 plugin_register_config("uuid", uuid_config, config_keys,
262 STATIC_ARRAY_SIZE(config_keys));
263 plugin_register_init("uuid", uuid_init);
267 * vim: set tabstop=4:
268 * vim: set shiftwidth=4:
269 * vim: set expandtab:
273 * indent-tabs-mode: nil