virsysinfo.c 34.8 KB
Newer Older
1
/*
2
 * virsysinfo.c: get SMBIOS/sysinfo information from the host
3
 *
4
 * Copyright (C) 2010-2013 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * 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>

33
#include "virerror.h"
34
#include "virsysinfo.h"
35
#include "virlog.h"
36
#include "viralloc.h"
37
#include "vircommand.h"
38
#include "virfile.h"
39
#include "virstring.h"
40 41 42 43

#define VIR_FROM_THIS VIR_FROM_SYSINFO


44 45 46
VIR_ENUM_IMPL(virSysinfo, VIR_SYSINFO_LAST,
              "smbios");

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
static const char *sysinfoDmidecode = "dmidecode";
static const char *sysinfoSysinfo = "/proc/sysinfo";
static const char *sysinfoCpuinfo = "/proc/cpuinfo";

#define SYSINFO_SMBIOS_DECODER sysinfoDmidecode
#define SYSINFO sysinfoSysinfo
#define CPUINFO sysinfoCpuinfo

/* only to be used test programs, therefore not in sysinfo.h */
extern void virSysinfoSetup(const char *dmidecode, const char *sysinfo,
                            const char *cpuinfo);

void virSysinfoSetup(const char *dmidecode, const char *sysinfo,
                     const char *cpuinfo)
{
    sysinfoDmidecode = dmidecode;
    sysinfoSysinfo = sysinfo;
    sysinfoCpuinfo = cpuinfo;
}

67 68 69 70 71 72 73 74 75
/**
 * virSysinfoDefFree:
 * @def: a sysinfo structure
 *
 * Free up the sysinfo structure
 */

void virSysinfoDefFree(virSysinfoDefPtr def)
{
76
    size_t i;
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91
    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 已提交
92
    VIR_FREE(def->system_family);
93

94
    for (i = 0; i < def->nprocessor; i++) {
95 96 97 98 99 100 101 102 103 104 105 106 107
        VIR_FREE(def->processor[i].processor_socket_destination);
        VIR_FREE(def->processor[i].processor_type);
        VIR_FREE(def->processor[i].processor_family);
        VIR_FREE(def->processor[i].processor_manufacturer);
        VIR_FREE(def->processor[i].processor_signature);
        VIR_FREE(def->processor[i].processor_version);
        VIR_FREE(def->processor[i].processor_external_clock);
        VIR_FREE(def->processor[i].processor_max_speed);
        VIR_FREE(def->processor[i].processor_status);
        VIR_FREE(def->processor[i].processor_serial_number);
        VIR_FREE(def->processor[i].processor_part_number);
    }
    VIR_FREE(def->processor);
108
    for (i = 0; i < def->nmemory; i++) {
109 110 111 112 113 114 115 116 117 118 119 120
        VIR_FREE(def->memory[i].memory_size);
        VIR_FREE(def->memory[i].memory_form_factor);
        VIR_FREE(def->memory[i].memory_locator);
        VIR_FREE(def->memory[i].memory_bank_locator);
        VIR_FREE(def->memory[i].memory_type);
        VIR_FREE(def->memory[i].memory_type_detail);
        VIR_FREE(def->memory[i].memory_speed);
        VIR_FREE(def->memory[i].memory_manufacturer);
        VIR_FREE(def->memory[i].memory_serial_number);
        VIR_FREE(def->memory[i].memory_part_number);
    }
    VIR_FREE(def->memory);
121

122 123 124 125 126 127 128 129 130 131
    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
 */
P
Prerna Saxena 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

#if defined(__powerpc__)
static int
virSysinfoParseSystem(const char *base, virSysinfoDefPtr ret)
{
    char *eol = NULL;
    const char *cur;

    if ((cur = strstr(base, "platform")) == NULL)
        return 0;

    base = cur;
    /* Account for format 'platform    : XXXX'*/
    cur = strchr(cur, ':') + 1;
    eol = strchr(cur, '\n');
    virSkipSpaces(&cur);
148 149
    if (eol && VIR_STRNDUP(ret->system_family, cur, eol - cur) < 0)
        return -1;
P
Prerna Saxena 已提交
150 151 152 153 154

    if ((cur = strstr(base, "model")) != NULL) {
        cur = strchr(cur, ':') + 1;
        eol = strchr(cur, '\n');
        virSkipSpaces(&cur);
155 156
        if (eol && VIR_STRNDUP(ret->system_serial, cur, eol - cur) < 0)
            return -1;
P
Prerna Saxena 已提交
157 158 159 160 161 162
    }

    if ((cur = strstr(base, "machine")) != NULL) {
        cur = strchr(cur, ':') + 1;
        eol = strchr(cur, '\n');
        virSkipSpaces(&cur);
163 164
        if (eol && VIR_STRNDUP(ret->system_version, cur, eol - cur) < 0)
            return -1;
P
Prerna Saxena 已提交
165 166 167 168 169 170 171 172 173 174 175 176
    }

    return 0;
}

static int
virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
{
    const char *cur;
    char *eol, *tmp_base;
    virSysinfoProcessorDefPtr processor;

177
    while ((tmp_base = strstr(base, "processor")) != NULL) {
P
Prerna Saxena 已提交
178 179 180 181 182
        base = tmp_base;
        eol = strchr(base, '\n');
        cur = strchr(base, ':') + 1;

        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0) {
183
            return -1;
P
Prerna Saxena 已提交
184 185 186 187
        }
        processor = &ret->processor[ret->nprocessor - 1];

        virSkipSpaces(&cur);
188 189 190
        if (eol && VIR_STRNDUP(processor->processor_socket_destination,
                               cur, eol - cur) < 0)
            return -1;
P
Prerna Saxena 已提交
191 192 193 194 195

        if ((cur = strstr(base, "cpu")) != NULL) {
            cur = strchr(cur, ':') + 1;
            eol = strchr(cur, '\n');
            virSkipSpaces(&cur);
196 197 198
            if (eol && VIR_STRNDUP(processor->processor_type,
                                   cur, eol - cur) < 0)
                return -1;
P
Prerna Saxena 已提交
199 200 201 202 203 204
        }

        if ((cur = strstr(base, "revision")) != NULL) {
            cur = strchr(cur, ':') + 1;
            eol = strchr(cur, '\n');
            virSkipSpaces(&cur);
205 206 207
            if (eol && VIR_STRNDUP(processor->processor_version,
                                   cur, eol - cur) < 0)
                return -1;
P
Prerna Saxena 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        }

        base = cur;
    }

    return 0;
}

/* virSysinfoRead for PowerPC
 * Gathers sysinfo data from /proc/cpuinfo */
virSysinfoDefPtr
virSysinfoRead(void) {
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;

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

226
    if (virFileReadAll(CPUINFO, 2048, &outbuf) < 0) {
227 228
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), CPUINFO);
P
Prerna Saxena 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        return NULL;
    }

    ret->nprocessor = 0;
    ret->processor = NULL;
    if (virSysinfoParseProcessor(outbuf, ret) < 0)
        goto no_memory;

    if (virSysinfoParseSystem(outbuf, ret) < 0)
        goto no_memory;

    return ret;

no_memory:
    VIR_FREE(outbuf);
    return NULL;
}

247
#elif defined(__arm__) || defined(__aarch64__)
248 249 250 251 252 253 254 255 256 257 258 259 260 261
static int
virSysinfoParseSystem(const char *base, virSysinfoDefPtr ret)
{
    char *eol = NULL;
    const char *cur;

    if ((cur = strstr(base, "platform")) == NULL)
        return 0;

    base = cur;
    /* Account for format 'platform    : XXXX'*/
    cur = strchr(cur, ':') + 1;
    eol = strchr(cur, '\n');
    virSkipSpaces(&cur);
262 263
    if (eol && VIR_STRNDUP(ret->system_family, cur, eol - cur) < 0)
        return -1;
264 265 266 267 268

    if ((cur = strstr(base, "model")) != NULL) {
        cur = strchr(cur, ':') + 1;
        eol = strchr(cur, '\n');
        virSkipSpaces(&cur);
269 270
        if (eol && VIR_STRNDUP(ret->system_serial, cur, eol - cur) < 0)
            return -1;
271 272 273 274 275 276
    }

    if ((cur = strstr(base, "machine")) != NULL) {
        cur = strchr(cur, ':') + 1;
        eol = strchr(cur, '\n');
        virSkipSpaces(&cur);
277 278
        if (eol && VIR_STRNDUP(ret->system_version, cur, eol - cur) < 0)
            return -1;
279
    }
280

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    return 0;
}

static int
virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
{
    const char *cur;
    char *eol, *tmp_base;
    virSysinfoProcessorDefPtr processor;
    char *processor_type = NULL;

    if (!(tmp_base = strstr(base, "Processor")))
        return 0;

    base = tmp_base;
    eol = strchr(base, '\n');
    cur = strchr(base, ':') + 1;
    virSkipSpaces(&cur);
299 300
    if (eol && VIR_STRNDUP(processor_type, cur, eol - cur) < 0)
        goto error;
301 302 303 304 305 306 307
    base = cur;

    while ((tmp_base = strstr(base, "processor")) != NULL) {
        base = tmp_base;
        eol = strchr(base, '\n');
        cur = strchr(base, ':') + 1;

308
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
309
            goto error;
310 311 312 313
        processor = &ret->processor[ret->nprocessor - 1];

        virSkipSpaces(&cur);
        if (eol &&
314 315 316
            VIR_STRNDUP(processor->processor_socket_destination,
                        cur, eol - cur) < 0)
            goto error;
317 318

        if (processor_type &&
319 320
            VIR_STRDUP(processor->processor_type, processor_type) < 0)
            goto error;
321 322 323 324 325 326 327

        base = cur;
    }

    VIR_FREE(processor_type);
    return 0;

328
error:
329 330 331 332 333 334 335 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 361 362 363 364 365
    VIR_FREE(processor_type);
    return -1;
}

/* virSysinfoRead for ARMv7
 * Gathers sysinfo data from /proc/cpuinfo */
virSysinfoDefPtr
virSysinfoRead(void) {
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;

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

    if (virFileReadAll(CPUINFO, 2048, &outbuf) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), CPUINFO);
        return NULL;
    }

    ret->nprocessor = 0;
    ret->processor = NULL;
    if (virSysinfoParseProcessor(outbuf, ret) < 0)
        goto no_memory;

    if (virSysinfoParseSystem(outbuf, ret) < 0)
        goto no_memory;

    return ret;

no_memory:
    VIR_FREE(outbuf);
    return NULL;
}


#elif defined(__s390__) || defined(__s390x__)
366 367 368 369 370 371 372 373 374 375 376
/*
  we need to ignore warnings about strchr caused by -Wlogical-op
  for some GCC versions.
  Doing it via a local pragma keeps the damage smaller than
  disabling it on the package level.
  Unfortunately, the affected GCCs don't allow diagnostic push/pop
  which would have further reduced the impact.
 */
# if BROKEN_GCC_WLOGICALOP
#  pragma GCC diagnostic ignored "-Wlogical-op"
# endif
377

378 379 380 381 382 383 384 385 386 387 388 389 390
static char *
virSysinfoParseDelimited(const char *base, const char *name, char **value,
                         char delim1, char delim2)
{
    const char *start;
    char *end;

    if (delim1 != delim2 &&
        (start = strstr(base, name)) &&
        (start = strchr(start, delim1))) {
        start += 1;
        end = strchrnul(start, delim2);
        virSkipSpaces(&start);
391 392
        if (VIR_STRNDUP(*value, start, end - start) < 0)
            return NULL;
393 394
        virTrimSpaces(*value, NULL);
        return end;
395
    }
396 397
    return NULL;
}
398

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
static char *
virSysinfoParseLine(const char *base, const char *name, char **value)
{
    return virSysinfoParseDelimited(base, name, value, ':', '\n');
}

static int
virSysinfoParseSystem(const char *base, virSysinfoDefPtr ret)
{
    if (virSysinfoParseLine(base, "Manufacturer",
                            &ret->system_manufacturer) &&
        virSysinfoParseLine(base, "Type",
                            &ret->system_family) &&
        virSysinfoParseLine(base, "Sequence Code",
                            &ret->system_serial))
        return 0;
    else
        return -1;
417 418 419 420 421
}

static int
virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
{
422 423 424 425
    char *tmp_base;
    char *manufacturer = NULL;
    char *procline = NULL;
    int result = -1;
426 427
    virSysinfoProcessorDefPtr processor;

428 429
    if (!(tmp_base=virSysinfoParseLine(base, "vendor_id", &manufacturer)))
        goto cleanup;
430

431 432 433 434 435
    /* Find processor N: line and gather the processor manufacturer,
       version, serial number, and family */
    while ((tmp_base = strstr(tmp_base, "processor "))
           && (tmp_base = virSysinfoParseLine(tmp_base, "processor ",
                                              &procline))) {
436
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
437
            goto cleanup;
438
        processor = &ret->processor[ret->nprocessor - 1];
439 440
        if (VIR_STRDUP(processor->processor_manufacturer, manufacturer) < 0)
            goto cleanup;
441 442 443 444 445 446 447 448 449 450
        if (!virSysinfoParseDelimited(procline, "version",
                                      &processor->processor_version,
                                      '=', ',') ||
            !virSysinfoParseDelimited(procline, "identification",
                                      &processor->processor_serial_number,
                                      '=', ',') ||
            !virSysinfoParseDelimited(procline, "machine",
                                      &processor->processor_family,
                                      '=', '\n'))
            goto cleanup;
451
    }
452
    result = 0;
453

454 455 456 457
cleanup:
    VIR_FREE(manufacturer);
    VIR_FREE(procline);
    return result;
458 459 460 461 462 463 464 465 466 467 468 469 470
}

/* virSysinfoRead for s390x
 * Gathers sysinfo data from /proc/sysinfo and /proc/cpuinfo */
virSysinfoDefPtr
virSysinfoRead(void) {
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;

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

    /* Gather info from /proc/cpuinfo */
471
    if (virFileReadAll(CPUINFO, 8192, &outbuf) < 0) {
472 473
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), CPUINFO);
474 475 476 477 478 479 480 481 482 483 484 485
        return NULL;
    }

    ret->nprocessor = 0;
    ret->processor = NULL;
    if (virSysinfoParseProcessor(outbuf, ret) < 0)
        goto no_memory;

    /* Free buffer before reading next file */
    VIR_FREE(outbuf);

    /* Gather info from /proc/sysinfo */
486
    if (virFileReadAll(SYSINFO, 8192, &outbuf) < 0) {
487 488
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), SYSINFO);
489 490 491 492 493 494 495 496 497
        return NULL;
    }

    if (virSysinfoParseSystem(outbuf, ret) < 0)
        goto no_memory;

    return ret;

no_memory:
498
    virSysinfoDefFree(ret);
499 500 501 502
    VIR_FREE(outbuf);
    return NULL;
}

P
Prerna Saxena 已提交
503
#elif defined(WIN32) || \
504 505
    !(defined(__x86_64__) || \
      defined(__i386__) ||   \
P
Prerna Saxena 已提交
506
      defined(__amd64__) || \
507
      defined(__arm__) || \
508
      defined(__aarch64__) || \
P
Prerna Saxena 已提交
509
      defined(__powerpc__))
510 511 512 513 514 515 516
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",
517
                         _("Host sysinfo extraction not supported on this platform"));
E
Eric Blake 已提交
518
    return NULL;
519
}
E
Eric Blake 已提交
520

521
#else /* !WIN32 && x86 */
E
Eric Blake 已提交
522

523 524
static int
virSysinfoParseBIOS(const char *base, virSysinfoDefPtr ret)
M
Minoru Usui 已提交
525
{
526
    const char *cur, *eol = NULL;
527

M
Minoru Usui 已提交
528
    if ((cur = strstr(base, "BIOS Information")) == NULL)
529
        return 0;
M
Minoru Usui 已提交
530 531

    base = cur;
532 533 534
    if ((cur = strstr(base, "Vendor: ")) != NULL) {
        cur += 8;
        eol = strchr(cur, '\n');
535 536
        if (eol && VIR_STRNDUP(ret->bios_vendor, cur, eol - cur) < 0)
            return -1;
537 538 539 540
    }
    if ((cur = strstr(base, "Version: ")) != NULL) {
        cur += 9;
        eol = strchr(cur, '\n');
541 542
        if (eol && VIR_STRNDUP(ret->bios_version, cur, eol - cur) < 0)
            return -1;
543 544 545 546
    }
    if ((cur = strstr(base, "Release Date: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
547 548
        if (eol && VIR_STRNDUP(ret->bios_date, cur, eol - cur) < 0)
            return -1;
549 550 551 552
    }
    if ((cur = strstr(base, "BIOS Revision: ")) != NULL) {
        cur += 15;
        eol = strchr(cur, '\n');
553 554
        if (eol && VIR_STRNDUP(ret->bios_release, cur, eol - cur) < 0)
            return -1;
555
    }
M
Minoru Usui 已提交
556

557
    return 0;
M
Minoru Usui 已提交
558 559
}

560 561
static int
virSysinfoParseSystem(const char *base, virSysinfoDefPtr ret)
M
Minoru Usui 已提交
562
{
563
    const char *cur, *eol = NULL;
M
Minoru Usui 已提交
564

M
Minoru Usui 已提交
565
    if ((cur = strstr(base, "System Information")) == NULL)
566
        return 0;
M
Minoru Usui 已提交
567

M
Minoru Usui 已提交
568
    base = cur;
569 570 571
    if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
572 573
        if (eol && VIR_STRNDUP(ret->system_manufacturer, cur, eol - cur) < 0)
            return -1;
574 575 576 577
    }
    if ((cur = strstr(base, "Product Name: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
578 579
        if (eol && VIR_STRNDUP(ret->system_product, cur, eol - cur) < 0)
            return -1;
580 581 582 583
    }
    if ((cur = strstr(base, "Version: ")) != NULL) {
        cur += 9;
        eol = strchr(cur, '\n');
584 585
        if (eol && VIR_STRNDUP(ret->system_version, cur, eol - cur) < 0)
            return -1;
586 587 588 589
    }
    if ((cur = strstr(base, "Serial Number: ")) != NULL) {
        cur += 15;
        eol = strchr(cur, '\n');
590 591
        if (eol && VIR_STRNDUP(ret->system_serial, cur, eol - cur) < 0)
            return -1;
592 593 594 595
    }
    if ((cur = strstr(base, "UUID: ")) != NULL) {
        cur += 6;
        eol = strchr(cur, '\n');
596 597
        if (eol && VIR_STRNDUP(ret->system_uuid, cur, eol - cur) < 0)
            return -1;
598 599 600 601
    }
    if ((cur = strstr(base, "SKU Number: ")) != NULL) {
        cur += 12;
        eol = strchr(cur, '\n');
602 603
        if (eol && VIR_STRNDUP(ret->system_sku, cur, eol - cur) < 0)
            return -1;
604
    }
E
Eric Blake 已提交
605 606 607
    if ((cur = strstr(base, "Family: ")) != NULL) {
        cur += 8;
        eol = strchr(cur, '\n');
608 609
        if (eol && VIR_STRNDUP(ret->system_family, cur, eol - cur) < 0)
            return -1;
E
Eric Blake 已提交
610
    }
611

612
    return 0;
M
Minoru Usui 已提交
613 614
}

615 616
static int
virSysinfoParseProcessor(const char *base, virSysinfoDefPtr ret)
617
{
618 619
    const char *cur, *tmp_base;
    char *eol;
620
    virSysinfoProcessorDefPtr processor;
621

622
    while ((tmp_base = strstr(base, "Processor Information")) != NULL) {
623
        base = tmp_base;
E
Eric Blake 已提交
624
        eol = NULL;
625

626 627
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
            return -1;
628 629 630 631 632
        processor = &ret->processor[ret->nprocessor - 1];

        if ((cur = strstr(base, "Socket Designation: ")) != NULL) {
            cur += 20;
            eol = strchr(cur, '\n');
633
            virSkipSpacesBackwards(cur, &eol);
634 635 636
            if (eol && VIR_STRNDUP(processor->processor_socket_destination,
                                   cur, eol - cur) < 0)
                return -1;
637 638 639 640
        }
        if ((cur = strstr(base, "Type: ")) != NULL) {
            cur += 6;
            eol = strchr(cur, '\n');
641
            virSkipSpacesBackwards(cur, &eol);
642 643
            if (eol && VIR_STRNDUP(processor->processor_type, cur, eol - cur) < 0)
                return -1;
644 645 646 647
        }
        if ((cur = strstr(base, "Family: ")) != NULL) {
            cur += 8;
            eol = strchr(cur, '\n');
648
            virSkipSpacesBackwards(cur, &eol);
649 650
            if (eol && VIR_STRNDUP(processor->processor_family, cur, eol - cur) < 0)
                return -1;
651 652 653 654
        }
        if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
            cur += 14;
            eol = strchr(cur, '\n');
655
            virSkipSpacesBackwards(cur, &eol);
656 657 658
            if (eol && VIR_STRNDUP(processor->processor_manufacturer,
                                   cur, eol - cur) < 0)
                return -1;
659 660 661 662
        }
        if ((cur = strstr(base, "Signature: ")) != NULL) {
            cur += 11;
            eol = strchr(cur, '\n');
663
            virSkipSpacesBackwards(cur, &eol);
664 665 666
            if (eol && VIR_STRNDUP(processor->processor_signature,
                                   cur, eol - cur) < 0)
                return -1;
667 668 669 670
        }
        if ((cur = strstr(base, "Version: ")) != NULL) {
            cur += 9;
            eol = strchr(cur, '\n');
671
            virSkipSpacesBackwards(cur, &eol);
672 673 674
            if (eol && VIR_STRNDUP(processor->processor_version,
                                   cur, eol - cur) < 0)
                return -1;
675 676 677 678
        }
        if ((cur = strstr(base, "External Clock: ")) != NULL) {
            cur += 16;
            eol = strchr(cur, '\n');
679
            virSkipSpacesBackwards(cur, &eol);
680 681 682
            if (eol && VIR_STRNDUP(processor->processor_external_clock,
                                   cur, eol - cur) < 0)
                return -1;
683 684 685 686
        }
        if ((cur = strstr(base, "Max Speed: ")) != NULL) {
            cur += 11;
            eol = strchr(cur, '\n');
687
            virSkipSpacesBackwards(cur, &eol);
688 689 690
            if (eol && VIR_STRNDUP(processor->processor_max_speed,
                                   cur, eol - cur) < 0)
                return -1;
691 692 693 694
        }
        if ((cur = strstr(base, "Status: ")) != NULL) {
            cur += 8;
            eol = strchr(cur, '\n');
695
            virSkipSpacesBackwards(cur, &eol);
696 697
            if (eol && VIR_STRNDUP(processor->processor_status, cur, eol - cur) < 0)
                return -1;
698 699 700 701
        }
        if ((cur = strstr(base, "Serial Number: ")) != NULL) {
            cur += 15;
            eol = strchr(cur, '\n');
702
            virSkipSpacesBackwards(cur, &eol);
703 704 705
            if (eol && VIR_STRNDUP(processor->processor_serial_number,
                                   cur, eol - cur) < 0)
                return -1;
706 707 708 709
        }
        if ((cur = strstr(base, "Part Number: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
710
            virSkipSpacesBackwards(cur, &eol);
711 712 713
            if (eol && VIR_STRNDUP(processor->processor_part_number,
                                   cur, eol - cur) < 0)
                return -1;
714 715
        }

M
Minoru Usui 已提交
716
        base += strlen("Processor Information");
717 718
    }

719
    return 0;
720 721
}

722 723
static int
virSysinfoParseMemory(const char *base, virSysinfoDefPtr ret)
724
{
725 726
    const char *cur, *tmp_base;
    char *eol;
727
    virSysinfoMemoryDefPtr memory;
728 729 730

    while ((tmp_base = strstr(base, "Memory Device")) != NULL) {
        base = tmp_base;
E
Eric Blake 已提交
731
        eol = NULL;
732

733 734
        if (VIR_EXPAND_N(ret->memory, ret->nmemory, 1) < 0)
            return -1;
735 736 737 738 739 740 741 742
        memory = &ret->memory[ret->nmemory - 1];

        if ((cur = strstr(base, "Size: ")) != NULL) {
            cur += 6;
            eol = strchr(cur, '\n');
            if (STREQLEN(cur, "No Module Installed", eol - cur))
                goto next;

743
            virSkipSpacesBackwards(cur, &eol);
744 745
            if (eol && VIR_STRNDUP(memory->memory_size, cur, eol - cur) < 0)
                return -1;
746 747 748 749
        }
        if ((cur = strstr(base, "Form Factor: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
750
            virSkipSpacesBackwards(cur, &eol);
751 752 753
            if (eol && VIR_STRNDUP(memory->memory_form_factor,
                                   cur, eol - cur) < 0)
                return -1;
754 755 756 757
        }
        if ((cur = strstr(base, "Locator: ")) != NULL) {
            cur += 9;
            eol = strchr(cur, '\n');
758
            virSkipSpacesBackwards(cur, &eol);
E
Eric Blake 已提交
759
            if (eol && VIR_STRNDUP(memory->memory_locator, cur, eol - cur) < 0)
760
                return -1;
761 762 763 764
        }
        if ((cur = strstr(base, "Bank Locator: ")) != NULL) {
            cur += 14;
            eol = strchr(cur, '\n');
765
            virSkipSpacesBackwards(cur, &eol);
766 767 768
            if (eol && VIR_STRNDUP(memory->memory_bank_locator,
                                   cur, eol - cur) < 0)
                return -1;
769 770 771 772
        }
        if ((cur = strstr(base, "Type: ")) != NULL) {
            cur += 6;
            eol = strchr(cur, '\n');
773
            virSkipSpacesBackwards(cur, &eol);
774 775
            if (eol && VIR_STRNDUP(memory->memory_type, cur, eol - cur) < 0)
                return -1;
776 777 778 779
        }
        if ((cur = strstr(base, "Type Detail: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
780
            virSkipSpacesBackwards(cur, &eol);
781 782
            if (eol && VIR_STRNDUP(memory->memory_type_detail, cur, eol - cur) < 0)
                return -1;
783 784 785 786
        }
        if ((cur = strstr(base, "Speed: ")) != NULL) {
            cur += 7;
            eol = strchr(cur, '\n');
787
            virSkipSpacesBackwards(cur, &eol);
788 789
            if (eol && VIR_STRNDUP(memory->memory_speed, cur, eol - cur) < 0)
                return -1;
790 791 792 793
        }
        if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
            cur += 14;
            eol = strchr(cur, '\n');
794
            virSkipSpacesBackwards(cur, &eol);
795 796
            if (eol && VIR_STRNDUP(memory->memory_manufacturer, cur, eol - cur) < 0)
                return -1;
797 798 799 800
        }
        if ((cur = strstr(base, "Serial Number: ")) != NULL) {
            cur += 15;
            eol = strchr(cur, '\n');
801
            virSkipSpacesBackwards(cur, &eol);
802 803 804
            if (eol && VIR_STRNDUP(memory->memory_serial_number,
                                   cur, eol - cur) < 0)
                return -1;
805 806 807 808
        }
        if ((cur = strstr(base, "Part Number: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
809
            virSkipSpacesBackwards(cur, &eol);
810 811
            if (eol && VIR_STRNDUP(memory->memory_part_number, cur, eol - cur) < 0)
                return -1;
812 813 814
        }

    next:
M
Minoru Usui 已提交
815
        base += strlen("Memory Device");
816 817
    }

818
    return 0;
819 820
}

M
Minoru Usui 已提交
821 822
virSysinfoDefPtr
virSysinfoRead(void) {
823
    char *path;
M
Minoru Usui 已提交
824 825 826 827 828 829
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;
    virCommandPtr cmd;

    path = virFindFileInPath(SYSINFO_SMBIOS_DECODER);
    if (path == NULL) {
830 831 832
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to find path for %s binary"),
                       SYSINFO_SMBIOS_DECODER);
M
Minoru Usui 已提交
833 834 835
        return NULL;
    }

836
    cmd = virCommandNewArgList(path, "-q", "-t", "0,1,4,17", NULL);
M
Minoru Usui 已提交
837 838
    VIR_FREE(path);
    virCommandSetOutputBuffer(cmd, &outbuf);
839
    if (virCommandRun(cmd, NULL) < 0)
M
Minoru Usui 已提交
840 841 842
        goto cleanup;

    if (VIR_ALLOC(ret) < 0)
843
        goto error;
M
Minoru Usui 已提交
844 845 846

    ret->type = VIR_SYSINFO_SMBIOS;

847
    if (virSysinfoParseBIOS(outbuf, ret) < 0)
848
        goto error;
M
Minoru Usui 已提交
849

850
    if (virSysinfoParseSystem(outbuf, ret) < 0)
851
        goto error;
M
Minoru Usui 已提交
852

853 854
    ret->nprocessor = 0;
    ret->processor = NULL;
855
    if (virSysinfoParseProcessor(outbuf, ret) < 0)
856
        goto error;
857

858 859
    ret->nmemory = 0;
    ret->memory = NULL;
860
    if (virSysinfoParseMemory(outbuf, ret) < 0)
861
        goto error;
862

863 864
cleanup:
    VIR_FREE(outbuf);
E
Eric Blake 已提交
865
    virCommandFree(cmd);
866

E
Eric Blake 已提交
867
    return ret;
868

869
error:
870 871 872 873
    virSysinfoDefFree(ret);
    ret = NULL;
    goto cleanup;
}
874
#endif /* !WIN32 && x86 */
E
Eric Blake 已提交
875

M
Minoru Usui 已提交
876
static void
877
virSysinfoBIOSFormat(virBufferPtr buf, virSysinfoDefPtr def)
E
Eric Blake 已提交
878
{
879 880 881
    if (!def->bios_vendor && !def->bios_version &&
        !def->bios_date && !def->bios_release)
        return;
M
Minoru Usui 已提交
882

883 884 885 886 887 888 889 890 891 892
    virBufferAddLit(buf, "  <bios>\n");
    virBufferEscapeString(buf, "    <entry name='vendor'>%s</entry>\n",
                          def->bios_vendor);
    virBufferEscapeString(buf, "    <entry name='version'>%s</entry>\n",
                          def->bios_version);
    virBufferEscapeString(buf, "    <entry name='date'>%s</entry>\n",
                          def->bios_date);
    virBufferEscapeString(buf, "    <entry name='release'>%s</entry>\n",
                          def->bios_release);
    virBufferAddLit(buf, "  </bios>\n");
M
Minoru Usui 已提交
893 894 895
}

static void
896
virSysinfoSystemFormat(virBufferPtr buf, virSysinfoDefPtr def)
M
Minoru Usui 已提交
897
{
898 899 900 901
    if (!def->system_manufacturer && !def->system_product &&
        !def->system_version && !def->system_serial &&
        !def->system_uuid && !def->system_sku && !def->system_family)
        return;
M
Minoru Usui 已提交
902

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
    virBufferAddLit(buf, "  <system>\n");
    virBufferEscapeString(buf, "    <entry name='manufacturer'>%s</entry>\n",
                          def->system_manufacturer);
    virBufferEscapeString(buf, "    <entry name='product'>%s</entry>\n",
                          def->system_product);
    virBufferEscapeString(buf, "    <entry name='version'>%s</entry>\n",
                          def->system_version);
    virBufferEscapeString(buf, "    <entry name='serial'>%s</entry>\n",
                          def->system_serial);
    virBufferEscapeString(buf, "    <entry name='uuid'>%s</entry>\n",
                          def->system_uuid);
    virBufferEscapeString(buf, "    <entry name='sku'>%s</entry>\n",
                          def->system_sku);
    virBufferEscapeString(buf, "    <entry name='family'>%s</entry>\n",
                          def->system_family);
    virBufferAddLit(buf, "  </system>\n");
M
Minoru Usui 已提交
919 920
}

921
static void
922
virSysinfoProcessorFormat(virBufferPtr buf, virSysinfoDefPtr def)
923
{
924
    size_t i;
925
    virSysinfoProcessorDefPtr processor;
926 927 928 929

    for (i = 0; i < def->nprocessor; i++) {
        processor = &def->processor[i];

930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
        if (!processor->processor_socket_destination &&
            !processor->processor_type &&
            !processor->processor_family &&
            !processor->processor_manufacturer &&
            !processor->processor_signature &&
            !processor->processor_version &&
            !processor->processor_external_clock &&
            !processor->processor_max_speed &&
            !processor->processor_status &&
            !processor->processor_serial_number &&
            !processor->processor_part_number)
            continue;

        virBufferAddLit(buf, "  <processor>\n");
        virBufferAdjustIndent(buf, 4);
        virBufferEscapeString(buf,
                              "<entry name='socket_destination'>%s</entry>\n",
                              processor->processor_socket_destination);
        virBufferEscapeString(buf, "<entry name='type'>%s</entry>\n",
                              processor->processor_type);
        virBufferEscapeString(buf, "<entry name='family'>%s</entry>\n",
                              processor->processor_family);
        virBufferEscapeString(buf, "<entry name='manufacturer'>%s</entry>\n",
                              processor->processor_manufacturer);
        virBufferEscapeString(buf, "<entry name='signature'>%s</entry>\n",
                              processor->processor_signature);
        virBufferEscapeString(buf, "<entry name='version'>%s</entry>\n",
                              processor->processor_version);
        virBufferEscapeString(buf, "<entry name='external_clock'>%s</entry>\n",
                              processor->processor_external_clock);
        virBufferEscapeString(buf, "<entry name='max_speed'>%s</entry>\n",
                              processor->processor_max_speed);
        virBufferEscapeString(buf, "<entry name='status'>%s</entry>\n",
                              processor->processor_status);
        virBufferEscapeString(buf, "<entry name='serial_number'>%s</entry>\n",
                              processor->processor_serial_number);
        virBufferEscapeString(buf, "<entry name='part_number'>%s</entry>\n",
                              processor->processor_part_number);
        virBufferAdjustIndent(buf, -4);
        virBufferAddLit(buf, "  </processor>\n");
970 971 972
    }
}

973
static void
974
virSysinfoMemoryFormat(virBufferPtr buf, virSysinfoDefPtr def)
975
{
976
    size_t i;
977
    virSysinfoMemoryDefPtr memory;
978 979 980 981

    for (i = 0; i < def->nmemory; i++) {
        memory = &def->memory[i];

982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
        if (!memory->memory_size &&
            !memory->memory_form_factor &&
            !memory->memory_locator &&
            !memory->memory_bank_locator &&
            !memory->memory_type &&
            !memory->memory_type_detail &&
            !memory->memory_speed &&
            !memory->memory_manufacturer &&
            !memory->memory_serial_number &&
            !memory->memory_part_number)
            continue;

        virBufferAddLit(buf, "  <memory_device>\n");
        virBufferEscapeString(buf, "    <entry name='size'>%s</entry>\n",
                              memory->memory_size);
        virBufferEscapeString(buf,
                              "    <entry name='form_factor'>%s</entry>\n",
                              memory->memory_form_factor);
        virBufferEscapeString(buf, "    <entry name='locator'>%s</entry>\n",
                              memory->memory_locator);
        virBufferEscapeString(buf,
                              "    <entry name='bank_locator'>%s</entry>\n",
                              memory->memory_bank_locator);
        virBufferEscapeString(buf, "    <entry name='type'>%s</entry>\n",
                              memory->memory_type);
        virBufferEscapeString(buf,
                              "    <entry name='type_detail'>%s</entry>\n",
                              memory->memory_type_detail);
        virBufferEscapeString(buf, "    <entry name='speed'>%s</entry>\n",
                              memory->memory_speed);
        virBufferEscapeString(buf,
                              "    <entry name='manufacturer'>%s</entry>\n",
                              memory->memory_manufacturer);
        virBufferEscapeString(buf,
                              "    <entry name='serial_number'>%s</entry>\n",
                              memory->memory_serial_number);
        virBufferEscapeString(buf,
                              "    <entry name='part_number'>%s</entry>\n",
                              memory->memory_part_number);
        virBufferAddLit(buf, "  </memory_device>\n");
1022 1023 1024
    }
}

M
Minoru Usui 已提交
1025 1026
/**
 * virSysinfoFormat:
1027
 * @buf: buffer to append output to (may use auto-indentation)
M
Minoru Usui 已提交
1028 1029
 * @def: structure to convert to xml string
 *
1030
 * Returns 0 on success, -1 on failure after generating an error message.
M
Minoru Usui 已提交
1031
 */
1032 1033
int
virSysinfoFormat(virBufferPtr buf, virSysinfoDefPtr def)
M
Minoru Usui 已提交
1034 1035 1036 1037
{
    const char *type = virSysinfoTypeToString(def->type);

    if (!type) {
1038 1039 1040
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected sysinfo type model %d"),
                       def->type);
1041 1042
        virBufferFreeAndReset(buf);
        return -1;
E
Eric Blake 已提交
1043 1044
    }

1045
    virBufferAsprintf(buf, "<sysinfo type='%s'>\n", type);
M
Minoru Usui 已提交
1046

1047 1048 1049 1050
    virSysinfoBIOSFormat(buf, def);
    virSysinfoSystemFormat(buf, def);
    virSysinfoProcessorFormat(buf, def);
    virSysinfoMemoryFormat(buf, def);
M
Minoru Usui 已提交
1051

1052
    virBufferAddLit(buf, "</sysinfo>\n");
E
Eric Blake 已提交
1053

1054
    if (virBufferError(buf)) {
1055
        virReportOOMError();
1056
        return -1;
1057 1058
    }

1059
    return 0;
E
Eric Blake 已提交
1060 1061
}

1062 1063 1064 1065 1066 1067 1068 1069 1070
bool virSysinfoIsEqual(virSysinfoDefPtr src,
                       virSysinfoDefPtr dst)
{
    bool identical = false;

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

    if ((src && !dst) || (!src && dst)) {
1071 1072
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Target sysinfo does not match source"));
1073 1074 1075 1076
        goto cleanup;
    }

    if (src->type != dst->type) {
1077 1078 1079 1080
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target sysinfo %s does not match source %s"),
                       virSysinfoTypeToString(dst->type),
                       virSysinfoTypeToString(src->type));
1081 1082 1083
        goto cleanup;
    }

1084
#define CHECK_FIELD(name, desc)                                         \
1085 1086
    do {                                                                \
        if (STRNEQ_NULLABLE(src->name, dst->name)) {                    \
1087 1088 1089
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,                  \
                           _("Target sysinfo %s %s does not match source %s"), \
                           desc, NULLSTR(src->name), NULLSTR(dst->name)); \
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
        }                                                               \
    } 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");

1106
#undef CHECK_FIELD
1107 1108 1109 1110 1111 1112

    identical = true;

cleanup:
    return identical;
}