X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fuuid.c;h=a5fba034faf22f518f496deb5125a10bb8a24f8b;hp=546d481d81d8daa5ec116fc857ffc9caa146e919;hb=f22b9ecc5e5d204e5dcb6861d6fd8039e1fb0181;hpb=2b4445ea9b4b978468d75864f98e8137ba628dec diff --git a/src/uuid.c b/src/uuid.c index 546d481d..a5fba034 100644 --- a/src/uuid.c +++ b/src/uuid.c @@ -26,10 +26,12 @@ #include "collectd.h" -#include "common.h" #include "plugin.h" +#include "utils/common/common.h" -#if HAVE_SYS_SYSCTL_H +#if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME) || \ + defined(__OpenBSD__) +/* Implies have BSD variant */ #include #endif @@ -37,27 +39,24 @@ #define UUID_PRINTABLE_COMPACT_LENGTH (UUID_RAW_LENGTH * 2) #define UUID_PRINTABLE_NORMAL_LENGTH (UUID_PRINTABLE_COMPACT_LENGTH + 4) -static char *uuidfile = NULL; +static char *uuidfile; static const char *config_keys[] = {"UUIDFile"}; static int looks_like_a_uuid(const char *uuid) { - int len; - if (!uuid) - return (0); - - len = strlen(uuid); + return 0; + size_t len = strlen(uuid); if (len < UUID_PRINTABLE_COMPACT_LENGTH) - return (0); + return 0; while (*uuid) { if (!isxdigit((int)*uuid) && *uuid != '-') - return (0); + return 0; uuid++; } - return (1); + return 1; } static char *uuid_parse_dmidecode(FILE *file) { @@ -82,9 +81,9 @@ static char *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) { @@ -92,12 +91,12 @@ static char *uuid_get_from_dmidecode(void) { char *uuid; if (!dmidecode) - return (NULL); + return NULL; uuid = uuid_parse_dmidecode(dmidecode); pclose(dmidecode); - return (uuid); + return uuid; } #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) @@ -106,7 +105,7 @@ static char *uuid_get_from_sysctlbyname(const char *name) { size_t len = sizeof(uuid); if (sysctlbyname(name, &uuid, &len, NULL, 0) == -1) return NULL; - return (strdup(uuid)); + return strdup(uuid); } #elif defined(__OpenBSD__) static char *uuid_get_from_sysctl(void) { @@ -119,7 +118,7 @@ static char *uuid_get_from_sysctl(void) { if (sysctl(mib, 2, uuid, &len, NULL, 0) == -1) return NULL; - return (strdup(uuid)); + return strdup(uuid); } #endif @@ -129,16 +128,16 @@ static char *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 *uuid_get_local(void) { @@ -146,60 +145,60 @@ static char *uuid_get_local(void) { /* Check /etc/uuid / UUIDFile before any other method. */ if ((uuid = uuid_get_from_file(uuidfile ? uuidfile : "/etc/uuid")) != NULL) - return (uuid); + return uuid; #if defined(__APPLE__) if ((uuid = uuid_get_from_sysctlbyname("kern.uuid")) != NULL) - return (uuid); + return uuid; #elif defined(__FreeBSD__) if ((uuid = uuid_get_from_sysctlbyname("kern.hostuuid")) != NULL) - return (uuid); + return uuid; #elif defined(__NetBSD__) if ((uuid = uuid_get_from_sysctlbyname("machdep.dmi.system-uuid")) != NULL) - return (uuid); + return uuid; #elif defined(__OpenBSD__) if ((uuid = uuid_get_from_sysctl()) != NULL) - return (uuid); + return uuid; #elif defined(__linux__) if ((uuid = uuid_get_from_file("/sys/class/dmi/id/product_uuid")) != NULL) - return (uuid); + return uuid; #endif if ((uuid = uuid_get_from_dmidecode()) != NULL) - return (uuid); + return uuid; #if defined(__linux__) if ((uuid = uuid_get_from_file("/sys/hypervisor/uuid")) != NULL) - return (uuid); + return uuid; #endif - return (NULL); + return NULL; } static int 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; - return (0); + return 0; } - return (1); + return 1; } static int uuid_init(void) { char *uuid = uuid_get_local(); if (uuid) { - sstrncpy(hostname_g, uuid, DATA_MAX_NAME_LEN); + hostname_set(uuid); sfree(uuid); - return (0); + return 0; } WARNING("uuid: could not read UUID using any known method"); - return (0); + return 0; } void module_register(void) {