Fix compile time issues
[collectd.git] / src / uuid.c
1 /**
2  * collectd - src/uuid.c
3  * Copyright (C) 2007  Red Hat Inc.
4  * Copyright (C) 2015  Ruben Kerkhof
5  *
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.
9  *
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.
14  *
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
18  *
19  * Authors:
20  *   Dan Berrange <berrange@redhat.com>
21  *   Richard W.M. Jones <rjones@redhat.com>
22  *
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
25  **/
26
27 #include "collectd.h"
28
29 #include "plugin.h"
30 #include "utils/common/common.h"
31
32 #if HAVE_SYS_SYSCTL_H
33 #include <sys/sysctl.h>
34 #endif
35
36 #define UUID_RAW_LENGTH 16
37 #define UUID_PRINTABLE_COMPACT_LENGTH (UUID_RAW_LENGTH * 2)
38 #define UUID_PRINTABLE_NORMAL_LENGTH (UUID_PRINTABLE_COMPACT_LENGTH + 4)
39
40 static char *uuidfile;
41
42 static const char *config_keys[] = {"UUIDFile"};
43
44 static int looks_like_a_uuid(const char *uuid) {
45   if (!uuid)
46     return 0;
47
48   size_t len = strlen(uuid);
49   if (len < UUID_PRINTABLE_COMPACT_LENGTH)
50     return 0;
51
52   while (*uuid) {
53     if (!isxdigit((int)*uuid) && *uuid != '-')
54       return 0;
55     uuid++;
56   }
57   return 1;
58 }
59
60 static char *uuid_parse_dmidecode(FILE *file) {
61   char line[1024];
62
63   while (fgets(line, sizeof(line), file) != NULL) {
64     char *fields[4];
65     int fields_num;
66
67     strstripnewline(line);
68
69     /* Look for a line reading:
70      *   UUID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
71      */
72     fields_num = strsplit(line, fields, STATIC_ARRAY_SIZE(fields));
73     if (fields_num != 2)
74       continue;
75
76     if (strcmp("UUID:", fields[0]) != 0)
77       continue;
78
79     if (!looks_like_a_uuid(fields[1]))
80       continue;
81
82     return strdup(fields[1]);
83   }
84   return NULL;
85 }
86
87 static char *uuid_get_from_dmidecode(void) {
88   FILE *dmidecode = popen("dmidecode -t system 2>/dev/null", "r");
89   char *uuid;
90
91   if (!dmidecode)
92     return NULL;
93
94   uuid = uuid_parse_dmidecode(dmidecode);
95
96   pclose(dmidecode);
97   return uuid;
98 }
99
100 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
101 static char *uuid_get_from_sysctlbyname(const char *name) {
102   char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1];
103   size_t len = sizeof(uuid);
104   if (sysctlbyname(name, &uuid, &len, NULL, 0) == -1)
105     return NULL;
106   return strdup(uuid);
107 }
108 #elif defined(__OpenBSD__)
109 static char *uuid_get_from_sysctl(void) {
110   char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1];
111   size_t len = sizeof(uuid);
112   int mib[2];
113
114   mib[0] = CTL_HW;
115   mib[1] = HW_UUID;
116
117   if (sysctl(mib, 2, uuid, &len, NULL, 0) == -1)
118     return NULL;
119   return strdup(uuid);
120 }
121 #endif
122
123 static char *uuid_get_from_file(const char *path) {
124   FILE *file;
125   char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1] = "";
126
127   file = fopen(path, "r");
128   if (file == NULL)
129     return NULL;
130
131   if (!fgets(uuid, sizeof(uuid), file)) {
132     fclose(file);
133     return NULL;
134   }
135   fclose(file);
136   strstripnewline(uuid);
137
138   return strdup(uuid);
139 }
140
141 static char *uuid_get_local(void) {
142   char *uuid;
143
144   /* Check /etc/uuid / UUIDFile before any other method. */
145   if ((uuid = uuid_get_from_file(uuidfile ? uuidfile : "/etc/uuid")) != NULL)
146     return uuid;
147
148 #if defined(__APPLE__)
149   if ((uuid = uuid_get_from_sysctlbyname("kern.uuid")) != NULL)
150     return uuid;
151 #elif defined(__FreeBSD__)
152   if ((uuid = uuid_get_from_sysctlbyname("kern.hostuuid")) != NULL)
153     return uuid;
154 #elif defined(__NetBSD__)
155   if ((uuid = uuid_get_from_sysctlbyname("machdep.dmi.system-uuid")) != NULL)
156     return uuid;
157 #elif defined(__OpenBSD__)
158   if ((uuid = uuid_get_from_sysctl()) != NULL)
159     return uuid;
160 #elif defined(__linux__)
161   if ((uuid = uuid_get_from_file("/sys/class/dmi/id/product_uuid")) != NULL)
162     return uuid;
163 #endif
164
165   if ((uuid = uuid_get_from_dmidecode()) != NULL)
166     return uuid;
167
168 #if defined(__linux__)
169   if ((uuid = uuid_get_from_file("/sys/hypervisor/uuid")) != NULL)
170     return uuid;
171 #endif
172
173   return NULL;
174 }
175
176 static int uuid_config(const char *key, const char *value) {
177   if (strcasecmp(key, "UUIDFile") == 0) {
178     char *tmp = strdup(value);
179     if (tmp == NULL)
180       return -1;
181     sfree(uuidfile);
182     uuidfile = tmp;
183     return 0;
184   }
185
186   return 1;
187 }
188
189 static int uuid_init(void) {
190   char *uuid = uuid_get_local();
191
192   if (uuid) {
193     hostname_set(uuid);
194     sfree(uuid);
195     return 0;
196   }
197
198   WARNING("uuid: could not read UUID using any known method");
199   return 0;
200 }
201
202 void module_register(void) {
203   plugin_register_config("uuid", uuid_config, config_keys,
204                          STATIC_ARRAY_SIZE(config_keys));
205   plugin_register_init("uuid", uuid_init);
206 }