sysinfo.c 12.9 KB
Newer Older
1 2 3
/*
 * sysinfo.c: get SMBIOS/sysinfo information from the host
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2010-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 * Copyright (C) 2010 Daniel Veillard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel Veillard <veillard@redhat.com>
 */

#include <config.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include "virterror_internal.h"
#include "sysinfo.h"
#include "util.h"
#include "logging.h"
#include "memory.h"
E
Eric Blake 已提交
38
#include "command.h"
39 40 41

#define VIR_FROM_THIS VIR_FROM_SYSINFO

E
Eric Blake 已提交
42
#define virSmbiosReportError(code, ...)                               \
43
    virReportErrorHelper(VIR_FROM_SYSINFO, code, __FILE__,            \
44 45 46 47
                         __FUNCTION__, __LINE__, __VA_ARGS__)

#define SYSINFO_SMBIOS_DECODER "dmidecode"

48 49 50
VIR_ENUM_IMPL(virSysinfo, VIR_SYSINFO_LAST,
              "smbios");

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
/**
 * virSysinfoDefFree:
 * @def: a sysinfo structure
 *
 * Free up the sysinfo structure
 */

void virSysinfoDefFree(virSysinfoDefPtr def)
{
    if (def == NULL)
        return;

    VIR_FREE(def->bios_vendor);
    VIR_FREE(def->bios_version);
    VIR_FREE(def->bios_date);
    VIR_FREE(def->bios_release);

    VIR_FREE(def->system_manufacturer);
    VIR_FREE(def->system_product);
    VIR_FREE(def->system_version);
    VIR_FREE(def->system_serial);
    VIR_FREE(def->system_uuid);
    VIR_FREE(def->system_sku);
E
Eric Blake 已提交
74
    VIR_FREE(def->system_family);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    VIR_FREE(def);
}

/**
 * virSysinfoRead:
 *
 * Tries to read the SMBIOS information from the current host
 *
 * Returns: a filled up sysinfo structure or NULL in case of error
 */
#ifdef WIN32
virSysinfoDefPtr
virSysinfoRead(void) {
    /*
     * this can probably be extracted from Windows using API or registry
     * http://www.microsoft.com/whdc/system/platform/firmware/SMBIOS.mspx
     */
    virReportSystemError(ENOSYS, "%s",
                 _("Host sysinfo extraction not supported on this platform"));
E
Eric Blake 已提交
94
    return NULL;
95
}
E
Eric Blake 已提交
96 97 98

#else /* !WIN32 */

M
Minoru Usui 已提交
99 100 101 102
static char *
parseBIOSInfo(char *base, virSysinfoDefPtr ret)
{
    char *cur, *eol;
103 104 105 106 107

    if ((cur = strstr(base, "Vendor: ")) != NULL) {
        cur += 8;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->bios_vendor = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
108
            goto no_memory;
109 110 111 112 113
    }
    if ((cur = strstr(base, "Version: ")) != NULL) {
        cur += 9;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->bios_version = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
114
            goto no_memory;
115 116 117 118 119
    }
    if ((cur = strstr(base, "Release Date: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->bios_date = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
120
            goto no_memory;
121 122 123 124 125
    }
    if ((cur = strstr(base, "BIOS Revision: ")) != NULL) {
        cur += 15;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->bios_release = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
126
            goto no_memory;
127
    }
M
Minoru Usui 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

    return eol + 1;

no_memory:
    return NULL;
}

static char *
parseSystemInfo(char *base, virSysinfoDefPtr ret)
{
    char *cur, *eol;

    if ((base = strstr(base, "System Information")) == NULL)
        return 0;

143 144 145 146 147
    if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
        if ((eol) &&
            ((ret->system_manufacturer = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
148
            goto no_memory;
149 150 151 152 153
    }
    if ((cur = strstr(base, "Product Name: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->system_product = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
154
            goto no_memory;
155 156 157 158 159
    }
    if ((cur = strstr(base, "Version: ")) != NULL) {
        cur += 9;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->system_version = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
160
            goto no_memory;
161 162 163 164 165
    }
    if ((cur = strstr(base, "Serial Number: ")) != NULL) {
        cur += 15;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->system_serial = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
166
            goto no_memory;
167 168 169 170 171
    }
    if ((cur = strstr(base, "UUID: ")) != NULL) {
        cur += 6;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->system_uuid = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
172
            goto no_memory;
173 174 175 176 177
    }
    if ((cur = strstr(base, "SKU Number: ")) != NULL) {
        cur += 12;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->system_sku = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
178
            goto no_memory;
179
    }
E
Eric Blake 已提交
180 181 182 183
    if ((cur = strstr(base, "Family: ")) != NULL) {
        cur += 8;
        eol = strchr(cur, '\n');
        if ((eol) && ((ret->system_family = strndup(cur, eol - cur)) == NULL))
E
Eric Blake 已提交
184
            goto no_memory;
E
Eric Blake 已提交
185
    }
186

M
Minoru Usui 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    return eol + 1;

no_memory:
    return NULL;
}

virSysinfoDefPtr
virSysinfoRead(void) {
    char *path, *base;
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;
    virCommandPtr cmd;

    path = virFindFileInPath(SYSINFO_SMBIOS_DECODER);
    if (path == NULL) {
        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
                             _("Failed to find path for %s binary"),
                             SYSINFO_SMBIOS_DECODER);
        return NULL;
    }

    cmd = virCommandNewArgList(path, "-q", "-t", "0,1,4,17", NULL);
    VIR_FREE(path);
    virCommandSetOutputBuffer(cmd, &outbuf);
    if (virCommandRun(cmd, NULL) < 0) {
        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
                             _("Failed to execute command %s"),
                             path);
        goto cleanup;
    }

    if (VIR_ALLOC(ret) < 0)
        goto no_memory;

    ret->type = VIR_SYSINFO_SMBIOS;

    base = outbuf;

    if ((base = parseBIOSInfo(base, ret)) == NULL)
        goto no_memory;

    if ((base = parseSystemInfo(base, ret)) == NULL)
        goto no_memory;

231 232
cleanup:
    VIR_FREE(outbuf);
E
Eric Blake 已提交
233
    virCommandFree(cmd);
234

E
Eric Blake 已提交
235
    return ret;
236 237 238 239 240 241 242 243

no_memory:
    virReportOOMError();

    virSysinfoDefFree(ret);
    ret = NULL;
    goto cleanup;
}
244
#endif /* !WIN32 */
E
Eric Blake 已提交
245

M
Minoru Usui 已提交
246 247
static void
BIOSInfoFormat(virSysinfoDefPtr def, const char *prefix, virBufferPtr buf)
E
Eric Blake 已提交
248
{
M
Minoru Usui 已提交
249
    int len = strlen(prefix);
E
Eric Blake 已提交
250 251 252

    if ((def->bios_vendor != NULL) || (def->bios_version != NULL) ||
        (def->bios_date != NULL) || (def->bios_release != NULL)) {
M
Minoru Usui 已提交
253
        virBufferAsprintf(buf, "%s  <bios>\n", prefix);
E
Eric Blake 已提交
254
        if (def->bios_vendor != NULL) {
M
Minoru Usui 已提交
255 256
            virBufferAdd(buf, prefix, len);
            virBufferEscapeString(buf,
E
Eric Blake 已提交
257 258 259 260
                                  "    <entry name='vendor'>%s</entry>\n",
                                  def->bios_vendor);
        }
        if (def->bios_version != NULL) {
M
Minoru Usui 已提交
261 262
            virBufferAdd(buf, prefix, len);
            virBufferEscapeString(buf,
E
Eric Blake 已提交
263 264 265 266
                                  "    <entry name='version'>%s</entry>\n",
                                  def->bios_version);
        }
        if (def->bios_date != NULL) {
M
Minoru Usui 已提交
267 268
            virBufferAdd(buf, prefix, len);
            virBufferEscapeString(buf,
E
Eric Blake 已提交
269 270 271 272
                                  "    <entry name='date'>%s</entry>\n",
                                  def->bios_date);
        }
        if (def->bios_release != NULL) {
M
Minoru Usui 已提交
273 274
            virBufferAdd(buf, prefix, len);
            virBufferEscapeString(buf,
E
Eric Blake 已提交
275 276 277
                                  "    <entry name='release'>%s</entry>\n",
                                  def->bios_release);
        }
M
Minoru Usui 已提交
278
        virBufferAsprintf(buf, "%s  </bios>\n", prefix);
E
Eric Blake 已提交
279
    }
M
Minoru Usui 已提交
280 281 282 283 284 285 286 287 288

    return;
}

static void
SystemInfoFormat(virSysinfoDefPtr def, const char *prefix, virBufferPtr buf)
{
    int len = strlen(prefix);

E
Eric Blake 已提交
289 290 291 292
    if ((def->system_manufacturer != NULL) || (def->system_product != NULL) ||
        (def->system_version != NULL) || (def->system_serial != NULL) ||
        (def->system_uuid != NULL) || (def->system_sku != NULL) ||
        (def->system_family != NULL)) {
M
Minoru Usui 已提交
293
        virBufferAsprintf(buf, "%s  <system>\n", prefix);
E
Eric Blake 已提交
294
        if (def->system_manufacturer != NULL) {
M
Minoru Usui 已提交
295 296
            virBufferAdd(buf, prefix, len);
            virBufferEscapeString(buf,
E
Eric Blake 已提交
297 298 299 300
                                  "    <entry name='manufacturer'>%s</entry>\n",
                                  def->system_manufacturer);
        }
        if (def->system_product != NULL) {
M
Minoru Usui 已提交
301 302
            virBufferAdd(buf, prefix, len);
            virBufferEscapeString(buf,
E
Eric Blake 已提交
303 304 305 306
                                  "    <entry name='product'>%s</entry>\n",
                                  def->system_product);
        }
        if (def->system_version != NULL) {
M
Minoru Usui 已提交
307 308
            virBufferAdd(buf, prefix, len);
            virBufferEscapeString(buf,
E
Eric Blake 已提交
309 310 311 312
                                  "    <entry name='version'>%s</entry>\n",
                                  def->system_version);
        }
        if (def->system_serial != NULL) {
M
Minoru Usui 已提交
313 314
            virBufferAdd(buf, prefix, len);
            virBufferEscapeString(buf,
E
Eric Blake 已提交
315 316 317 318
                                  "    <entry name='serial'>%s</entry>\n",
                                  def->system_serial);
        }
        if (def->system_uuid != NULL) {
M
Minoru Usui 已提交
319 320
            virBufferAdd(buf, prefix, len);
            virBufferEscapeString(buf,
E
Eric Blake 已提交
321 322 323 324
                                  "    <entry name='uuid'>%s</entry>\n",
                                  def->system_uuid);
        }
        if (def->system_sku != NULL) {
M
Minoru Usui 已提交
325 326
            virBufferAdd(buf, prefix, len);
            virBufferEscapeString(buf,
E
Eric Blake 已提交
327 328 329 330
                                  "    <entry name='sku'>%s</entry>\n",
                                  def->system_sku);
        }
        if (def->system_family != NULL) {
M
Minoru Usui 已提交
331 332
            virBufferAdd(buf, prefix, len);
            virBufferEscapeString(buf,
E
Eric Blake 已提交
333 334 335
                                  "    <entry name='family'>%s</entry>\n",
                                  def->system_family);
        }
M
Minoru Usui 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
        virBufferAsprintf(buf, "%s  </system>\n", prefix);
    }

    return;
}

/**
 * virSysinfoFormat:
 * @def: structure to convert to xml string
 * @prefix: string to prefix before each line of xml
 *
 * This returns the XML description of the sysinfo, or NULL after
 * generating an error message.
 */
char *
virSysinfoFormat(virSysinfoDefPtr def, const char *prefix)
{
    const char *type = virSysinfoTypeToString(def->type);
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (!type) {
        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
                             _("unexpected sysinfo type model %d"),
                             def->type);
        return NULL;
E
Eric Blake 已提交
361 362
    }

M
Minoru Usui 已提交
363 364 365 366 367
    virBufferAsprintf(&buf, "%s<sysinfo type='%s'>\n", prefix, type);

    BIOSInfoFormat(def, prefix, &buf);
    SystemInfoFormat(def, prefix, &buf);

368
    virBufferAsprintf(&buf, "%s</sysinfo>\n", prefix);
E
Eric Blake 已提交
369 370 371 372

    return virBufferContentAndReset(&buf);
}

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
bool virSysinfoIsEqual(virSysinfoDefPtr src,
                       virSysinfoDefPtr dst)
{
    bool identical = false;

    if (!src && !dst)
        return true;

    if ((src && !dst) || (!src && dst)) {
        virSmbiosReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                             _("Target sysinfo does not match source"));
        goto cleanup;
    }

    if (src->type != dst->type) {
        virSmbiosReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                             _("Target sysinfo %s does not match source %s"),
                             virSysinfoTypeToString(dst->type),
                             virSysinfoTypeToString(src->type));
        goto cleanup;
    }

395
#define CHECK_FIELD(name, desc)                                         \
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    do {                                                                \
        if (STRNEQ_NULLABLE(src->name, dst->name)) {                    \
            virSmbiosReportError(VIR_ERR_CONFIG_UNSUPPORTED,            \
                                 _("Target sysinfo %s %s does not match source %s"), \
                                 desc, NULLSTR(src->name), NULLSTR(dst->name)); \
        }                                                               \
    } while (0)

    CHECK_FIELD(bios_vendor, "BIOS vendor");
    CHECK_FIELD(bios_version, "BIOS version");
    CHECK_FIELD(bios_date, "BIOS date");
    CHECK_FIELD(bios_release, "BIOS release");

    CHECK_FIELD(system_manufacturer, "system vendor");
    CHECK_FIELD(system_product, "system product");
    CHECK_FIELD(system_version, "system version");
    CHECK_FIELD(system_serial, "system serial");
    CHECK_FIELD(system_uuid, "system uuid");
    CHECK_FIELD(system_sku, "system sku");
    CHECK_FIELD(system_family, "system family");

417
#undef CHECK_FIELD
418 419 420 421 422 423

    identical = true;

cleanup:
    return identical;
}