X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fuuid.c;h=8bae376103f0328ec9fb20763ee70c668217e928;hb=41288c6a9ed050b41ad47184aa1b53668c3588cc;hp=50882179a5458af16008e0f61bf7b2764b2df793;hpb=354f9991530248e45207d236eb74c1cc3d5238ef;p=collectd.git diff --git a/src/uuid.c b/src/uuid.c index 50882179..8bae3761 100644 --- a/src/uuid.c +++ b/src/uuid.c @@ -1,6 +1,7 @@ /** * collectd - src/uuid.c * Copyright (C) 2007 Red Hat Inc. + * Copyright (C) 2015 Ruben Kerkhof * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -24,11 +25,15 @@ **/ #include "collectd.h" + #include "common.h" -#include "configfile.h" #include "plugin.h" -#if HAVE_LIBHAL +#if HAVE_SYS_SYSCTL_H +#include +#endif + +#if HAVE_LIBHAL_H #include #endif @@ -47,18 +52,20 @@ looks_like_a_uuid (const char *uuid) { int len; - if (!uuid) return 0; + if (!uuid) + return (0); len = strlen (uuid); if (len < UUID_PRINTABLE_COMPACT_LENGTH) - return 0; + return (0); while (*uuid) { - if (!isxdigit ((int)*uuid) && *uuid != '-') return 0; + if (!isxdigit ((int)*uuid) && *uuid != '-') + return (0); uuid++; } - return 1; + return (1); } static char * @@ -86,28 +93,54 @@ uuid_parse_dmidecode(FILE *file) if (!looks_like_a_uuid (fields[1])) continue; - return strdup (fields[1]); + return (strdup (fields[1])); } - return NULL; + return (NULL); } static char * uuid_get_from_dmidecode(void) { - FILE *dmidecode = popen("dmidecode 2>/dev/null", "r"); + FILE *dmidecode = popen("dmidecode -t system 2>/dev/null", "r"); char *uuid; - if (!dmidecode) { - return NULL; - } + if (!dmidecode) + return (NULL); uuid = uuid_parse_dmidecode(dmidecode); pclose(dmidecode); - return uuid; + return (uuid); +} + +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) +static char * +uuid_get_from_sysctlbyname(const char *name) +{ + char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1]; + size_t len = sizeof (uuid); + if (sysctlbyname(name, &uuid, &len, NULL, 0) == -1) + return NULL; + return (strdup (uuid)); +} +#elif defined(__OpenBSD__) +static char * +uuid_get_from_sysctl(void) +{ + char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1]; + size_t len = sizeof (uuid); + int mib[2]; + + mib[0] = CTL_HW; + mib[1] = HW_UUID; + + if (sysctl(mib, 2, uuid, &len, NULL, 0) == -1) + return NULL; + return (strdup (uuid)); } +#endif -#if HAVE_LIBHAL +#if HAVE_LIBHAL_H #define UUID_PATH "/org/freedesktop/Hal/devices/computer" #define UUID_PROPERTY "smbios.system.uuid" @@ -122,50 +155,42 @@ uuid_get_from_hal(void) dbus_error_init(&error); - if (!(con = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) ) { + if (!(con = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) goto bailout_nobus; - } ctx = libhal_ctx_new(); libhal_ctx_set_dbus_connection(ctx, con); - if (!libhal_ctx_init(ctx, &error)) { + if (!libhal_ctx_init(ctx, &error)) goto bailout; - } if (!libhal_device_property_exists(ctx, UUID_PATH, UUID_PROPERTY, - &error)) { + &error)) goto bailout; - } char *uuid = libhal_device_get_property_string(ctx, UUID_PATH, UUID_PROPERTY, &error); - if (looks_like_a_uuid (uuid)) { - return uuid; - } + if (looks_like_a_uuid (uuid)) + return (uuid); bailout: { DBusError ctxerror; dbus_error_init(&ctxerror); - if (!(libhal_ctx_shutdown(ctx, &ctxerror))) { + if (!(libhal_ctx_shutdown(ctx, &ctxerror))) dbus_error_free(&ctxerror); - } } libhal_ctx_free(ctx); - //dbus_connection_unref(con); bailout_nobus: - if (dbus_error_is_set(&error)) { - /*printf("Error %s\n", error.name);*/ + if (dbus_error_is_set(&error)) dbus_error_free(&error); - } - return NULL; + return (NULL); } #endif @@ -177,16 +202,16 @@ uuid_get_from_file(const char *path) file = fopen (path, "r"); if (file == NULL) - return NULL; + return (NULL); if (!fgets(uuid, sizeof(uuid), file)) { fclose(file); - return NULL; + return (NULL); } fclose(file); strstripnewline (uuid); - return strdup (uuid); + return (strdup (uuid)); } static char * @@ -195,25 +220,40 @@ uuid_get_local(void) char *uuid; /* Check /etc/uuid / UUIDFile before any other method. */ - if ((uuid = uuid_get_from_file(uuidfile ? uuidfile : "/etc/uuid")) != NULL){ - return uuid; - } + if ((uuid = uuid_get_from_file(uuidfile ? uuidfile : "/etc/uuid")) != NULL) + return (uuid); + +#if defined(__APPLE__) + if ((uuid = uuid_get_from_sysctlbyname("kern.uuid")) != NULL) + return (uuid); +#elif defined(__FreeBSD__) + if ((uuid = uuid_get_from_sysctlbyname("kern.hostuuid")) != NULL) + return (uuid); +#elif defined(__NetBSD__) + if ((uuid = uuid_get_from_sysctlbyname("machdep.dmi.system-uuid")) != NULL) + return (uuid); +#elif defined(__OpenBSD__) + if ((uuid = uuid_get_from_sysctl()) != NULL) + return (uuid); +#elif defined(__linux__) + if ((uuid = uuid_get_from_file("/sys/class/dmi/id/product_uuid")) != NULL) + return (uuid); +#endif -#if HAVE_LIBHAL - if ((uuid = uuid_get_from_hal()) != NULL) { - return uuid; - } +#if HAVE_LIBHAL_H + if ((uuid = uuid_get_from_hal()) != NULL) + return (uuid); #endif - if ((uuid = uuid_get_from_dmidecode()) != NULL) { - return uuid; - } + if ((uuid = uuid_get_from_dmidecode()) != NULL) + return (uuid); - if ((uuid = uuid_get_from_file("/sys/hypervisor/uuid")) != NULL) { - return uuid; - } +#if defined(__linux__) + if ((uuid = uuid_get_from_file("/sys/hypervisor/uuid")) != NULL) + return (uuid); +#endif - return NULL; + return (NULL); } static int @@ -222,14 +262,13 @@ uuid_config (const char *key, const char *value) if (strcasecmp (key, "UUIDFile") == 0) { char *tmp = strdup (value); if (tmp == NULL) - return -1; + return (-1); sfree (uuidfile); uuidfile = tmp; - } else { - return 1; + return (0); } - return 0; + return (1); } static int @@ -240,11 +279,11 @@ uuid_init (void) if (uuid) { sstrncpy (hostname_g, uuid, DATA_MAX_NAME_LEN); sfree (uuid); - return 0; + return (0); } WARNING ("uuid: could not read UUID using any known method"); - return 0; + return (0); } void module_register (void)