virsysinfo.c 45.0 KB
Newer Older
1
/*
2
 * virsysinfo.c: get SMBIOS/sysinfo information from the host
3
 *
4
 * Copyright (C) 2010-2014 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 "viralloc.h"
36
#include "vircommand.h"
37
#include "virlog.h"
38
#include "virfile.h"
39
#include "virstring.h"
40

41 42 43
#define __VIR_SYSINFO_PRIV_H_ALLOW__
#include "virsysinfopriv.h"

44 45
#define VIR_FROM_THIS VIR_FROM_SYSINFO

46
VIR_LOG_INIT("util.sysinfo");
47

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

51
static const char *sysinfoDmidecode = DMIDECODE;
52 53 54 55 56 57
static const char *sysinfoSysinfo = "/proc/sysinfo";
static const char *sysinfoCpuinfo = "/proc/cpuinfo";

#define SYSINFO_SMBIOS_DECODER sysinfoDmidecode
#define SYSINFO sysinfoSysinfo
#define CPUINFO sysinfoCpuinfo
58
#define CPUINFO_FILE_LEN (1024*1024)    /* 1MB limit for /proc/cpuinfo file */
59 60


61 62 63 64
void
virSysinfoSetup(const char *dmidecode,
                const char *sysinfo,
                const char *cpuinfo)
65 66 67 68 69 70
{
    sysinfoDmidecode = dmidecode;
    sysinfoSysinfo = sysinfo;
    sysinfoCpuinfo = cpuinfo;
}

71 72 73 74 75 76 77 78 79 80 81 82
void virSysinfoBIOSDefFree(virSysinfoBIOSDefPtr def)
{
    if (def == NULL)
        return;

    VIR_FREE(def->vendor);
    VIR_FREE(def->version);
    VIR_FREE(def->date);
    VIR_FREE(def->release);
    VIR_FREE(def);
}

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
void virSysinfoSystemDefFree(virSysinfoSystemDefPtr def)
{
    if (def == NULL)
        return;

    VIR_FREE(def->manufacturer);
    VIR_FREE(def->product);
    VIR_FREE(def->version);
    VIR_FREE(def->serial);
    VIR_FREE(def->uuid);
    VIR_FREE(def->sku);
    VIR_FREE(def->family);
    VIR_FREE(def);
}

98 99 100 101 102 103 104 105 106 107 108 109
void virSysinfoBaseBoardDefClear(virSysinfoBaseBoardDefPtr def)
{
    if (def == NULL)
        return;

    VIR_FREE(def->manufacturer);
    VIR_FREE(def->product);
    VIR_FREE(def->version);
    VIR_FREE(def->serial);
    VIR_FREE(def->asset);
    VIR_FREE(def->location);
}
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124
void virSysinfoOEMStringsDefFree(virSysinfoOEMStringsDefPtr def)
{
    size_t i;

    if (def == NULL)
        return;

    for (i = 0; i < def->nvalues; i++)
        VIR_FREE(def->values[i]);
    VIR_FREE(def->values);

    VIR_FREE(def);
}

125 126 127 128 129 130 131 132 133
/**
 * virSysinfoDefFree:
 * @def: a sysinfo structure
 *
 * Free up the sysinfo structure
 */

void virSysinfoDefFree(virSysinfoDefPtr def)
{
134
    size_t i;
135

136 137 138
    if (def == NULL)
        return;

139
    virSysinfoBIOSDefFree(def->bios);
140
    virSysinfoSystemDefFree(def->system);
141

142 143 144 145
    for (i = 0; i < def->nbaseBoard; i++)
        virSysinfoBaseBoardDefClear(def->baseBoard + i);
    VIR_FREE(def->baseBoard);

146
    for (i = 0; i < def->nprocessor; i++) {
147 148 149 150 151 152 153 154 155 156 157 158 159
        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);
160
    for (i = 0; i < def->nmemory; i++) {
161 162 163 164 165 166 167 168 169 170 171 172
        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);
173

174 175
    virSysinfoOEMStringsDefFree(def->oemStrings);

176 177 178
    VIR_FREE(def);
}

P
Prerna Saxena 已提交
179 180

static int
181
virSysinfoParsePPCSystem(const char *base, virSysinfoSystemDefPtr *sysdef)
P
Prerna Saxena 已提交
182
{
183
    int ret = -1;
P
Prerna Saxena 已提交
184 185
    char *eol = NULL;
    const char *cur;
186
    virSysinfoSystemDefPtr def;
P
Prerna Saxena 已提交
187 188 189 190

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

191 192 193
    if (VIR_ALLOC(def) < 0)
        return ret;

P
Prerna Saxena 已提交
194 195 196 197 198
    base = cur;
    /* Account for format 'platform    : XXXX'*/
    cur = strchr(cur, ':') + 1;
    eol = strchr(cur, '\n');
    virSkipSpaces(&cur);
199 200
    if (eol && VIR_STRNDUP(def->family, cur, eol - cur) < 0)
        goto cleanup;
P
Prerna Saxena 已提交
201 202 203 204 205

    if ((cur = strstr(base, "model")) != NULL) {
        cur = strchr(cur, ':') + 1;
        eol = strchr(cur, '\n');
        virSkipSpaces(&cur);
206 207
        if (eol && VIR_STRNDUP(def->serial, cur, eol - cur) < 0)
            goto cleanup;
P
Prerna Saxena 已提交
208 209 210 211 212 213
    }

    if ((cur = strstr(base, "machine")) != NULL) {
        cur = strchr(cur, ':') + 1;
        eol = strchr(cur, '\n');
        virSkipSpaces(&cur);
214 215
        if (eol && VIR_STRNDUP(def->version, cur, eol - cur) < 0)
            goto cleanup;
P
Prerna Saxena 已提交
216 217
    }

218 219 220 221 222 223
    if (!def->manufacturer && !def->product && !def->version &&
        !def->serial && !def->uuid && !def->sku && !def->family) {
        virSysinfoSystemDefFree(def);
        def = NULL;
    }

M
Michal Privoznik 已提交
224
    *sysdef = def;
225 226 227 228 229
    def = NULL;
    ret = 0;
 cleanup:
    virSysinfoSystemDefFree(def);
    return ret;
P
Prerna Saxena 已提交
230 231 232
}

static int
233
virSysinfoParsePPCProcessor(const char *base, virSysinfoDefPtr ret)
P
Prerna Saxena 已提交
234 235 236 237 238
{
    const char *cur;
    char *eol, *tmp_base;
    virSysinfoProcessorDefPtr processor;

239
    while ((tmp_base = strstr(base, "processor")) != NULL) {
P
Prerna Saxena 已提交
240 241 242 243
        base = tmp_base;
        eol = strchr(base, '\n');
        cur = strchr(base, ':') + 1;

244
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
245
            return -1;
P
Prerna Saxena 已提交
246 247 248
        processor = &ret->processor[ret->nprocessor - 1];

        virSkipSpaces(&cur);
249 250 251
        if (eol && VIR_STRNDUP(processor->processor_socket_destination,
                               cur, eol - cur) < 0)
            return -1;
252
        base = cur;
P
Prerna Saxena 已提交
253 254 255 256 257

        if ((cur = strstr(base, "cpu")) != NULL) {
            cur = strchr(cur, ':') + 1;
            eol = strchr(cur, '\n');
            virSkipSpaces(&cur);
258 259 260
            if (eol && VIR_STRNDUP(processor->processor_type,
                                   cur, eol - cur) < 0)
                return -1;
261
            base = cur;
P
Prerna Saxena 已提交
262 263 264 265 266 267
        }

        if ((cur = strstr(base, "revision")) != NULL) {
            cur = strchr(cur, ':') + 1;
            eol = strchr(cur, '\n');
            virSkipSpaces(&cur);
268 269 270
            if (eol && VIR_STRNDUP(processor->processor_version,
                                   cur, eol - cur) < 0)
                return -1;
271
            base = cur;
P
Prerna Saxena 已提交
272 273 274 275 276 277 278 279 280 281
        }

    }

    return 0;
}

/* virSysinfoRead for PowerPC
 * Gathers sysinfo data from /proc/cpuinfo */
virSysinfoDefPtr
282
virSysinfoReadPPC(void)
283
{
P
Prerna Saxena 已提交
284 285 286 287 288 289
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;

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

290
    if (virFileReadAll(CPUINFO, CPUINFO_FILE_LEN, &outbuf) < 0) {
291 292
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), CPUINFO);
293
        goto no_memory;
P
Prerna Saxena 已提交
294 295 296 297
    }

    ret->nprocessor = 0;
    ret->processor = NULL;
298
    if (virSysinfoParsePPCProcessor(outbuf, ret) < 0)
P
Prerna Saxena 已提交
299 300
        goto no_memory;

301
    if (virSysinfoParsePPCSystem(outbuf, &ret->system) < 0)
P
Prerna Saxena 已提交
302 303
        goto no_memory;

304
    VIR_FREE(outbuf);
P
Prerna Saxena 已提交
305 306
    return ret;

307
 no_memory:
P
Prerna Saxena 已提交
308
    VIR_FREE(outbuf);
309
    virSysinfoDefFree(ret);
P
Prerna Saxena 已提交
310 311 312
    return NULL;
}

313

314
static int
315
virSysinfoParseARMSystem(const char *base, virSysinfoSystemDefPtr *sysdef)
316
{
317
    int ret = -1;
318 319
    char *eol = NULL;
    const char *cur;
320
    virSysinfoSystemDefPtr def;
321 322 323 324

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

325 326 327
    if (VIR_ALLOC(def) < 0)
        return ret;

328 329 330 331 332
    base = cur;
    /* Account for format 'platform    : XXXX'*/
    cur = strchr(cur, ':') + 1;
    eol = strchr(cur, '\n');
    virSkipSpaces(&cur);
333 334
    if (eol && VIR_STRNDUP(def->family, cur, eol - cur) < 0)
        goto cleanup;
335 336 337 338 339

    if ((cur = strstr(base, "model")) != NULL) {
        cur = strchr(cur, ':') + 1;
        eol = strchr(cur, '\n');
        virSkipSpaces(&cur);
340 341
        if (eol && VIR_STRNDUP(def->serial, cur, eol - cur) < 0)
            goto cleanup;
342 343 344 345 346 347
    }

    if ((cur = strstr(base, "machine")) != NULL) {
        cur = strchr(cur, ':') + 1;
        eol = strchr(cur, '\n');
        virSkipSpaces(&cur);
348 349
        if (eol && VIR_STRNDUP(def->version, cur, eol - cur) < 0)
            goto cleanup;
350
    }
351

352 353 354 355 356 357
    if (!def->manufacturer && !def->product && !def->version &&
        !def->serial && !def->uuid && !def->sku && !def->family) {
        virSysinfoSystemDefFree(def);
        def = NULL;
    }

M
Michal Privoznik 已提交
358
    *sysdef = def;
359 360 361 362 363
    def = NULL;
    ret = 0;
 cleanup:
    virSysinfoSystemDefFree(def);
    return ret;
364 365 366
}

static int
367
virSysinfoParseARMProcessor(const char *base, virSysinfoDefPtr ret)
368 369 370 371 372 373
{
    const char *cur;
    char *eol, *tmp_base;
    virSysinfoProcessorDefPtr processor;
    char *processor_type = NULL;

M
Michal Privoznik 已提交
374 375
    if (!(tmp_base = strstr(base, "model name")) &&
        !(tmp_base = strstr(base, "Processor")))
376 377
        return 0;

M
Michal Privoznik 已提交
378 379
    eol = strchr(tmp_base, '\n');
    cur = strchr(tmp_base, ':') + 1;
380
    virSkipSpaces(&cur);
381 382
    if (eol && VIR_STRNDUP(processor_type, cur, eol - cur) < 0)
        goto error;
383 384 385 386 387 388

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

389
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
390
            goto error;
391 392 393 394
        processor = &ret->processor[ret->nprocessor - 1];

        virSkipSpaces(&cur);
        if (eol &&
395 396 397
            VIR_STRNDUP(processor->processor_socket_destination,
                        cur, eol - cur) < 0)
            goto error;
398

399
        if (VIR_STRDUP(processor->processor_type, processor_type) < 0)
400
            goto error;
401 402 403 404 405 406 407

        base = cur;
    }

    VIR_FREE(processor_type);
    return 0;

408
 error:
409 410 411 412 413 414 415
    VIR_FREE(processor_type);
    return -1;
}

/* virSysinfoRead for ARMv7
 * Gathers sysinfo data from /proc/cpuinfo */
virSysinfoDefPtr
416
virSysinfoReadARM(void)
417
{
418 419 420 421 422 423
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;

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

424
    if (virFileReadAll(CPUINFO, CPUINFO_FILE_LEN, &outbuf) < 0) {
425 426
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), CPUINFO);
427
        goto no_memory;
428 429 430 431
    }

    ret->nprocessor = 0;
    ret->processor = NULL;
432
    if (virSysinfoParseARMProcessor(outbuf, ret) < 0)
433 434
        goto no_memory;

435
    if (virSysinfoParseARMSystem(outbuf, &ret->system) < 0)
436 437
        goto no_memory;

438
    VIR_FREE(outbuf);
439 440
    return ret;

441
 no_memory:
442
    VIR_FREE(outbuf);
443
    virSysinfoDefFree(ret);
444 445 446 447
    return NULL;
}


448 449

VIR_WARNINGS_NO_WLOGICALOP_STRCHR
450

451
static char *
452 453
virSysinfoParseS390Delimited(const char *base, const char *name, char **value,
                             char delim1, char delim2)
454 455 456 457 458 459 460 461 462 463
{
    const char *start;
    char *end;

    if (delim1 != delim2 &&
        (start = strstr(base, name)) &&
        (start = strchr(start, delim1))) {
        start += 1;
        end = strchrnul(start, delim2);
        virSkipSpaces(&start);
464 465
        if (VIR_STRNDUP(*value, start, end - start) < 0)
            return NULL;
466 467
        virTrimSpaces(*value, NULL);
        return end;
468
    }
469 470
    return NULL;
}
471

472
static char *
473
virSysinfoParseS390Line(const char *base, const char *name, char **value)
474
{
475
    return virSysinfoParseS390Delimited(base, name, value, ':', '\n');
476 477 478
}

static int
479
virSysinfoParseS390System(const char *base, virSysinfoSystemDefPtr *sysdef)
480
{
481 482 483 484 485 486
    int ret = -1;
    virSysinfoSystemDefPtr def;

    if (VIR_ALLOC(def) < 0)
        return ret;

487
    if (!virSysinfoParseS390Line(base, "Manufacturer", &def->manufacturer))
488 489
        goto cleanup;

490
    if (!virSysinfoParseS390Line(base, "Type", &def->family))
491 492
        goto cleanup;

493
    if (!virSysinfoParseS390Line(base, "Sequence Code", &def->serial))
494 495 496 497 498 499 500 501
        goto cleanup;

    if (!def->manufacturer && !def->product && !def->version &&
        !def->serial && !def->uuid && !def->sku && !def->family) {
        virSysinfoSystemDefFree(def);
        def = NULL;
    }

M
Michal Privoznik 已提交
502
    *sysdef = def;
503 504 505 506 507
    def = NULL;
    ret = 0;
 cleanup:
    virSysinfoSystemDefFree(def);
    return ret;
508 509 510
}

static int
511
virSysinfoParseS390Processor(const char *base, virSysinfoDefPtr ret)
512
{
513 514 515
    char *tmp_base;
    char *manufacturer = NULL;
    char *procline = NULL;
516
    char *ncpu = NULL;
517
    int result = -1;
518 519
    virSysinfoProcessorDefPtr processor;

520
    if (!(tmp_base = virSysinfoParseS390Line(base, "vendor_id", &manufacturer)))
521
        goto error;
522

523 524 525
    /* Find processor N: line and gather the processor manufacturer,
       version, serial number, and family */
    while ((tmp_base = strstr(tmp_base, "processor "))
526 527
           && (tmp_base = virSysinfoParseS390Line(tmp_base, "processor ",
                                                  &procline))) {
528
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
529
            goto error;
530
        processor = &ret->processor[ret->nprocessor - 1];
531
        if (VIR_STRDUP(processor->processor_manufacturer, manufacturer) < 0)
532
            goto error;
533 534 535 536 537 538 539 540 541
        if (!virSysinfoParseS390Delimited(procline, "version",
                                          &processor->processor_version,
                                          '=', ',') ||
            !virSysinfoParseS390Delimited(procline, "identification",
                                          &processor->processor_serial_number,
                                          '=', ',') ||
            !virSysinfoParseS390Delimited(procline, "machine",
                                          &processor->processor_family,
                                          '=', '\n'))
542
            goto error;
543 544

        VIR_FREE(procline);
545
    }
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

    /* now, for each processor found, extract the frequency information */
    tmp_base = (char *) base;

    while ((tmp_base = strstr(tmp_base, "cpu number")) &&
           (tmp_base = virSysinfoParseS390Line(tmp_base, "cpu number", &ncpu))) {
        unsigned int n;
        char *mhz = NULL;

        if (virStrToLong_uip(ncpu, NULL, 10, &n) < 0)
            goto error;

        if (n >= ret->nprocessor) {
            VIR_DEBUG("CPU number '%u' out of range", n);
            goto cleanup;
        }

        if (!(tmp_base = strstr(tmp_base, "cpu MHz static")) ||
            !virSysinfoParseS390Line(tmp_base, "cpu MHz static", &mhz))
            goto cleanup;

        ret->processor[n].processor_max_speed = mhz;

        VIR_FREE(ncpu);
    }
571

572
 cleanup:
573 574 575
    result = 0;

 error:
576 577
    VIR_FREE(manufacturer);
    VIR_FREE(procline);
578
    VIR_FREE(ncpu);
579
    return result;
580 581 582 583 584
}

/* virSysinfoRead for s390x
 * Gathers sysinfo data from /proc/sysinfo and /proc/cpuinfo */
virSysinfoDefPtr
585
virSysinfoReadS390(void)
586
{
587 588 589 590 591 592 593
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;

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

    /* Gather info from /proc/cpuinfo */
594
    if (virFileReadAll(CPUINFO, CPUINFO_FILE_LEN, &outbuf) < 0) {
595 596
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), CPUINFO);
597
        goto no_memory;
598 599 600 601
    }

    ret->nprocessor = 0;
    ret->processor = NULL;
602
    if (virSysinfoParseS390Processor(outbuf, ret) < 0)
603 604 605 606 607 608
        goto no_memory;

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

    /* Gather info from /proc/sysinfo */
609
    if (virFileReadAll(SYSINFO, 8192, &outbuf) < 0) {
610 611
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), SYSINFO);
612
        goto no_memory;
613 614
    }

615
    if (virSysinfoParseS390System(outbuf, &ret->system) < 0)
616 617
        goto no_memory;

618
    VIR_FREE(outbuf);
619 620
    return ret;

621
 no_memory:
622
    virSysinfoDefFree(ret);
623 624 625 626
    VIR_FREE(outbuf);
    return NULL;
}

E
Eric Blake 已提交
627

628
static int
629
virSysinfoParseBIOS(const char *base, virSysinfoBIOSDefPtr *bios)
M
Minoru Usui 已提交
630
{
631
    int ret = -1;
632
    const char *cur, *eol = NULL;
633
    virSysinfoBIOSDefPtr def;
634

M
Minoru Usui 已提交
635
    if ((cur = strstr(base, "BIOS Information")) == NULL)
636
        return 0;
M
Minoru Usui 已提交
637

638 639 640
    if (VIR_ALLOC(def) < 0)
        return ret;

M
Minoru Usui 已提交
641
    base = cur;
642 643 644
    if ((cur = strstr(base, "Vendor: ")) != NULL) {
        cur += 8;
        eol = strchr(cur, '\n');
645 646
        if (eol && VIR_STRNDUP(def->vendor, cur, eol - cur) < 0)
            goto cleanup;
647 648 649 650
    }
    if ((cur = strstr(base, "Version: ")) != NULL) {
        cur += 9;
        eol = strchr(cur, '\n');
651 652
        if (eol && VIR_STRNDUP(def->version, cur, eol - cur) < 0)
            goto cleanup;
653 654 655 656
    }
    if ((cur = strstr(base, "Release Date: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
657 658
        if (eol && VIR_STRNDUP(def->date, cur, eol - cur) < 0)
            goto cleanup;
659 660 661 662
    }
    if ((cur = strstr(base, "BIOS Revision: ")) != NULL) {
        cur += 15;
        eol = strchr(cur, '\n');
663 664
        if (eol && VIR_STRNDUP(def->release, cur, eol - cur) < 0)
            goto cleanup;
665
    }
M
Minoru Usui 已提交
666

667 668 669 670 671 672 673 674 675 676 677 678
    if (!def->vendor && !def->version &&
        !def->date && !def->release) {
        virSysinfoBIOSDefFree(def);
        def = NULL;
    }

    *bios = def;
    def = NULL;
    ret = 0;
 cleanup:
    virSysinfoBIOSDefFree(def);
    return ret;
M
Minoru Usui 已提交
679 680
}

681
static int
682
virSysinfoParseX86System(const char *base, virSysinfoSystemDefPtr *sysdef)
M
Minoru Usui 已提交
683
{
684
    int ret = -1;
685
    const char *cur, *eol = NULL;
686
    virSysinfoSystemDefPtr def;
M
Minoru Usui 已提交
687

M
Minoru Usui 已提交
688
    if ((cur = strstr(base, "System Information")) == NULL)
689
        return 0;
M
Minoru Usui 已提交
690

691 692 693
    if (VIR_ALLOC(def) < 0)
        return ret;

M
Minoru Usui 已提交
694
    base = cur;
695 696 697
    if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
698 699
        if (eol && VIR_STRNDUP(def->manufacturer, cur, eol - cur) < 0)
            goto cleanup;
700 701 702 703
    }
    if ((cur = strstr(base, "Product Name: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
704 705
        if (eol && VIR_STRNDUP(def->product, cur, eol - cur) < 0)
            goto cleanup;
706 707 708 709
    }
    if ((cur = strstr(base, "Version: ")) != NULL) {
        cur += 9;
        eol = strchr(cur, '\n');
710 711
        if (eol && VIR_STRNDUP(def->version, cur, eol - cur) < 0)
            goto cleanup;
712 713 714 715
    }
    if ((cur = strstr(base, "Serial Number: ")) != NULL) {
        cur += 15;
        eol = strchr(cur, '\n');
716 717
        if (eol && VIR_STRNDUP(def->serial, cur, eol - cur) < 0)
            goto cleanup;
718 719 720 721
    }
    if ((cur = strstr(base, "UUID: ")) != NULL) {
        cur += 6;
        eol = strchr(cur, '\n');
722 723
        if (eol && VIR_STRNDUP(def->uuid, cur, eol - cur) < 0)
            goto cleanup;
724 725 726 727
    }
    if ((cur = strstr(base, "SKU Number: ")) != NULL) {
        cur += 12;
        eol = strchr(cur, '\n');
728 729
        if (eol && VIR_STRNDUP(def->sku, cur, eol - cur) < 0)
            goto cleanup;
730
    }
E
Eric Blake 已提交
731 732 733
    if ((cur = strstr(base, "Family: ")) != NULL) {
        cur += 8;
        eol = strchr(cur, '\n');
734 735
        if (eol && VIR_STRNDUP(def->family, cur, eol - cur) < 0)
            goto cleanup;
E
Eric Blake 已提交
736
    }
737

738 739 740 741 742 743
    if (!def->manufacturer && !def->product && !def->version &&
        !def->serial && !def->uuid && !def->sku && !def->family) {
        virSysinfoSystemDefFree(def);
        def = NULL;
    }

M
Michal Privoznik 已提交
744
    *sysdef = def;
745 746 747 748 749
    def = NULL;
    ret = 0;
 cleanup:
    virSysinfoSystemDefFree(def);
    return ret;
M
Minoru Usui 已提交
750 751
}

752
static int
753 754 755
virSysinfoParseX86BaseBoard(const char *base,
                            virSysinfoBaseBoardDefPtr *baseBoard,
                            size_t *nbaseBoard)
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
{
    int ret = -1;
    const char *cur, *eol = NULL;
    virSysinfoBaseBoardDefPtr boards = NULL;
    size_t nboards = 0;
    char *board_type = NULL;

    while (base && (cur = strstr(base, "Base Board Information"))) {
        virSysinfoBaseBoardDefPtr def;

        if (VIR_EXPAND_N(boards, nboards, 1) < 0)
            goto cleanup;

        def = &boards[nboards - 1];

        base = cur + 22;
        if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
            cur += 14;
            eol = strchr(cur, '\n');
            if (eol && VIR_STRNDUP(def->manufacturer, cur, eol - cur) < 0)
                goto cleanup;
        }
        if ((cur = strstr(base, "Product Name: ")) != NULL) {
            cur += 14;
            eol = strchr(cur, '\n');
            if (eol && VIR_STRNDUP(def->product, cur, eol - cur) < 0)
                goto cleanup;
        }
        if ((cur = strstr(base, "Version: ")) != NULL) {
            cur += 9;
            eol = strchr(cur, '\n');
            if (eol && VIR_STRNDUP(def->version, cur, eol - cur) < 0)
                goto cleanup;
        }
        if ((cur = strstr(base, "Serial Number: ")) != NULL) {
            cur += 15;
            eol = strchr(cur, '\n');
            if (eol && VIR_STRNDUP(def->serial, cur, eol - cur) < 0)
                goto cleanup;
        }
        if ((cur = strstr(base, "Asset Tag: ")) != NULL) {
            cur += 11;
            eol = strchr(cur, '\n');
            if (eol && VIR_STRNDUP(def->asset, cur, eol - cur) < 0)
                goto cleanup;
        }
        if ((cur = strstr(base, "Location In Chassis: ")) != NULL) {
            cur += 21;
            eol = strchr(cur, '\n');
            if (eol && VIR_STRNDUP(def->location, cur, eol - cur) < 0)
                goto cleanup;
        }

        if (!def->manufacturer && !def->product && !def->version &&
            !def->serial && !def->asset && !def->location)
            nboards--;
    }

    /* This is safe, as we can be only shrinking the memory */
    ignore_value(VIR_REALLOC_N(boards, nboards));

    *baseBoard = boards;
    *nbaseBoard = nboards;
    boards = NULL;
    nboards = 0;
    ret = 0;
 cleanup:
    while (nboards--)
        virSysinfoBaseBoardDefClear(&boards[nboards]);
    VIR_FREE(boards);
    VIR_FREE(board_type);
    return ret;
}

830
static int
831
virSysinfoParseX86Processor(const char *base, virSysinfoDefPtr ret)
832
{
833 834
    const char *cur, *tmp_base;
    char *eol;
835
    virSysinfoProcessorDefPtr processor;
836

837
    while ((tmp_base = strstr(base, "Processor Information")) != NULL) {
838
        base = tmp_base;
E
Eric Blake 已提交
839
        eol = NULL;
840

841 842
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
            return -1;
843 844 845 846 847
        processor = &ret->processor[ret->nprocessor - 1];

        if ((cur = strstr(base, "Socket Designation: ")) != NULL) {
            cur += 20;
            eol = strchr(cur, '\n');
848
            virSkipSpacesBackwards(cur, &eol);
849 850 851
            if (eol && VIR_STRNDUP(processor->processor_socket_destination,
                                   cur, eol - cur) < 0)
                return -1;
852 853 854 855
        }
        if ((cur = strstr(base, "Type: ")) != NULL) {
            cur += 6;
            eol = strchr(cur, '\n');
856
            virSkipSpacesBackwards(cur, &eol);
857 858
            if (eol && VIR_STRNDUP(processor->processor_type, cur, eol - cur) < 0)
                return -1;
859 860 861 862
        }
        if ((cur = strstr(base, "Family: ")) != NULL) {
            cur += 8;
            eol = strchr(cur, '\n');
863
            virSkipSpacesBackwards(cur, &eol);
864 865
            if (eol && VIR_STRNDUP(processor->processor_family, cur, eol - cur) < 0)
                return -1;
866 867 868 869
        }
        if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
            cur += 14;
            eol = strchr(cur, '\n');
870
            virSkipSpacesBackwards(cur, &eol);
871 872 873
            if (eol && VIR_STRNDUP(processor->processor_manufacturer,
                                   cur, eol - cur) < 0)
                return -1;
874 875 876 877
        }
        if ((cur = strstr(base, "Signature: ")) != NULL) {
            cur += 11;
            eol = strchr(cur, '\n');
878
            virSkipSpacesBackwards(cur, &eol);
879 880 881
            if (eol && VIR_STRNDUP(processor->processor_signature,
                                   cur, eol - cur) < 0)
                return -1;
882 883 884 885
        }
        if ((cur = strstr(base, "Version: ")) != NULL) {
            cur += 9;
            eol = strchr(cur, '\n');
886
            virSkipSpacesBackwards(cur, &eol);
887 888 889
            if (eol && VIR_STRNDUP(processor->processor_version,
                                   cur, eol - cur) < 0)
                return -1;
890 891 892 893
        }
        if ((cur = strstr(base, "External Clock: ")) != NULL) {
            cur += 16;
            eol = strchr(cur, '\n');
894
            virSkipSpacesBackwards(cur, &eol);
895 896 897
            if (eol && VIR_STRNDUP(processor->processor_external_clock,
                                   cur, eol - cur) < 0)
                return -1;
898 899 900 901
        }
        if ((cur = strstr(base, "Max Speed: ")) != NULL) {
            cur += 11;
            eol = strchr(cur, '\n');
902
            virSkipSpacesBackwards(cur, &eol);
903 904 905
            if (eol && VIR_STRNDUP(processor->processor_max_speed,
                                   cur, eol - cur) < 0)
                return -1;
906 907 908 909
        }
        if ((cur = strstr(base, "Status: ")) != NULL) {
            cur += 8;
            eol = strchr(cur, '\n');
910
            virSkipSpacesBackwards(cur, &eol);
911 912
            if (eol && VIR_STRNDUP(processor->processor_status, cur, eol - cur) < 0)
                return -1;
913 914 915 916
        }
        if ((cur = strstr(base, "Serial Number: ")) != NULL) {
            cur += 15;
            eol = strchr(cur, '\n');
917
            virSkipSpacesBackwards(cur, &eol);
918 919 920
            if (eol && VIR_STRNDUP(processor->processor_serial_number,
                                   cur, eol - cur) < 0)
                return -1;
921 922 923 924
        }
        if ((cur = strstr(base, "Part Number: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
925
            virSkipSpacesBackwards(cur, &eol);
926 927 928
            if (eol && VIR_STRNDUP(processor->processor_part_number,
                                   cur, eol - cur) < 0)
                return -1;
929 930
        }

M
Minoru Usui 已提交
931
        base += strlen("Processor Information");
932 933
    }

934
    return 0;
935 936
}

937
static int
938
virSysinfoParseX86Memory(const char *base, virSysinfoDefPtr ret)
939
{
940 941
    const char *cur, *tmp_base;
    char *eol;
942
    virSysinfoMemoryDefPtr memory;
943 944 945

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

948 949
        if (VIR_EXPAND_N(ret->memory, ret->nmemory, 1) < 0)
            return -1;
950 951 952 953 954 955 956 957
        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;

958
            virSkipSpacesBackwards(cur, &eol);
959 960
            if (eol && VIR_STRNDUP(memory->memory_size, cur, eol - cur) < 0)
                return -1;
961 962 963 964
        }
        if ((cur = strstr(base, "Form Factor: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
965
            virSkipSpacesBackwards(cur, &eol);
966 967 968
            if (eol && VIR_STRNDUP(memory->memory_form_factor,
                                   cur, eol - cur) < 0)
                return -1;
969 970 971 972
        }
        if ((cur = strstr(base, "Locator: ")) != NULL) {
            cur += 9;
            eol = strchr(cur, '\n');
973
            virSkipSpacesBackwards(cur, &eol);
E
Eric Blake 已提交
974
            if (eol && VIR_STRNDUP(memory->memory_locator, cur, eol - cur) < 0)
975
                return -1;
976 977 978 979
        }
        if ((cur = strstr(base, "Bank Locator: ")) != NULL) {
            cur += 14;
            eol = strchr(cur, '\n');
980
            virSkipSpacesBackwards(cur, &eol);
981 982 983
            if (eol && VIR_STRNDUP(memory->memory_bank_locator,
                                   cur, eol - cur) < 0)
                return -1;
984 985 986 987
        }
        if ((cur = strstr(base, "Type: ")) != NULL) {
            cur += 6;
            eol = strchr(cur, '\n');
988
            virSkipSpacesBackwards(cur, &eol);
989 990
            if (eol && VIR_STRNDUP(memory->memory_type, cur, eol - cur) < 0)
                return -1;
991 992 993 994
        }
        if ((cur = strstr(base, "Type Detail: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
995
            virSkipSpacesBackwards(cur, &eol);
996 997
            if (eol && VIR_STRNDUP(memory->memory_type_detail, cur, eol - cur) < 0)
                return -1;
998 999 1000 1001
        }
        if ((cur = strstr(base, "Speed: ")) != NULL) {
            cur += 7;
            eol = strchr(cur, '\n');
1002
            virSkipSpacesBackwards(cur, &eol);
1003 1004
            if (eol && VIR_STRNDUP(memory->memory_speed, cur, eol - cur) < 0)
                return -1;
1005 1006 1007 1008
        }
        if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
            cur += 14;
            eol = strchr(cur, '\n');
1009
            virSkipSpacesBackwards(cur, &eol);
1010 1011
            if (eol && VIR_STRNDUP(memory->memory_manufacturer, cur, eol - cur) < 0)
                return -1;
1012 1013 1014 1015
        }
        if ((cur = strstr(base, "Serial Number: ")) != NULL) {
            cur += 15;
            eol = strchr(cur, '\n');
1016
            virSkipSpacesBackwards(cur, &eol);
1017 1018 1019
            if (eol && VIR_STRNDUP(memory->memory_serial_number,
                                   cur, eol - cur) < 0)
                return -1;
1020 1021 1022 1023
        }
        if ((cur = strstr(base, "Part Number: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
1024
            virSkipSpacesBackwards(cur, &eol);
1025 1026
            if (eol && VIR_STRNDUP(memory->memory_part_number, cur, eol - cur) < 0)
                return -1;
1027 1028 1029
        }

    next:
M
Minoru Usui 已提交
1030
        base += strlen("Memory Device");
1031 1032
    }

1033
    return 0;
1034 1035
}

M
Minoru Usui 已提交
1036
virSysinfoDefPtr
1037
virSysinfoReadX86(void)
1038
{
1039
    char *path;
M
Minoru Usui 已提交
1040 1041 1042 1043 1044 1045
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;
    virCommandPtr cmd;

    path = virFindFileInPath(SYSINFO_SMBIOS_DECODER);
    if (path == NULL) {
1046 1047 1048
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to find path for %s binary"),
                       SYSINFO_SMBIOS_DECODER);
M
Minoru Usui 已提交
1049 1050 1051
        return NULL;
    }

1052
    cmd = virCommandNewArgList(path, "-q", "-t", "0,1,2,4,17", NULL);
M
Minoru Usui 已提交
1053 1054
    VIR_FREE(path);
    virCommandSetOutputBuffer(cmd, &outbuf);
1055
    if (virCommandRun(cmd, NULL) < 0)
M
Minoru Usui 已提交
1056 1057 1058
        goto cleanup;

    if (VIR_ALLOC(ret) < 0)
1059
        goto error;
M
Minoru Usui 已提交
1060 1061 1062

    ret->type = VIR_SYSINFO_SMBIOS;

1063
    if (virSysinfoParseBIOS(outbuf, &ret->bios) < 0)
1064
        goto error;
M
Minoru Usui 已提交
1065

1066
    if (virSysinfoParseX86System(outbuf, &ret->system) < 0)
1067
        goto error;
M
Minoru Usui 已提交
1068

1069
    if (virSysinfoParseX86BaseBoard(outbuf, &ret->baseBoard, &ret->nbaseBoard) < 0)
1070 1071
        goto error;

1072 1073
    ret->nprocessor = 0;
    ret->processor = NULL;
1074
    if (virSysinfoParseX86Processor(outbuf, ret) < 0)
1075
        goto error;
1076

1077 1078
    ret->nmemory = 0;
    ret->memory = NULL;
1079
    if (virSysinfoParseX86Memory(outbuf, ret) < 0)
1080
        goto error;
1081

1082
 cleanup:
1083
    VIR_FREE(outbuf);
E
Eric Blake 已提交
1084
    virCommandFree(cmd);
1085

E
Eric Blake 已提交
1086
    return ret;
1087

1088
 error:
1089 1090 1091 1092
    virSysinfoDefFree(ret);
    ret = NULL;
    goto cleanup;
}
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112


/**
 * virSysinfoRead:
 *
 * Tries to read the SMBIOS information from the current host
 *
 * Returns: a filled up sysinfo structure or NULL in case of error
 */
virSysinfoDefPtr
virSysinfoRead(void)
{
#if defined(__powerpc__)
    return virSysinfoReadPPC();
#elif defined(__arm__) || defined(__aarch64__)
    return virSysinfoReadARM();
#elif defined(__s390__) || defined(__s390x__)
    return virSysinfoReadS390();
#elif defined(WIN32) || \
    !(defined(__x86_64__) || \
1113
      defined(__i386__) || \
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
      defined(__amd64__) || \
      defined(__arm__) || \
      defined(__aarch64__) || \
      defined(__powerpc__))
    /*
     * 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"));
    return NULL;
#else /* !WIN32 && x86 */
    return virSysinfoReadX86();
1127
#endif /* !WIN32 && x86 */
1128 1129
}

E
Eric Blake 已提交
1130

M
Minoru Usui 已提交
1131
static void
1132
virSysinfoBIOSFormat(virBufferPtr buf, virSysinfoBIOSDefPtr def)
E
Eric Blake 已提交
1133
{
1134
    if (!def)
1135
        return;
M
Minoru Usui 已提交
1136

1137 1138 1139
    virBufferAddLit(buf, "<bios>\n");
    virBufferAdjustIndent(buf, 2);
    virBufferEscapeString(buf, "<entry name='vendor'>%s</entry>\n",
1140
                          def->vendor);
1141
    virBufferEscapeString(buf, "<entry name='version'>%s</entry>\n",
1142
                          def->version);
1143
    virBufferEscapeString(buf, "<entry name='date'>%s</entry>\n",
1144
                          def->date);
1145
    virBufferEscapeString(buf, "<entry name='release'>%s</entry>\n",
1146
                          def->release);
1147 1148
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</bios>\n");
M
Minoru Usui 已提交
1149 1150 1151
}

static void
1152
virSysinfoSystemFormat(virBufferPtr buf, virSysinfoSystemDefPtr def)
M
Minoru Usui 已提交
1153
{
1154
    if (!def)
1155
        return;
M
Minoru Usui 已提交
1156

1157 1158 1159
    virBufferAddLit(buf, "<system>\n");
    virBufferAdjustIndent(buf, 2);
    virBufferEscapeString(buf, "<entry name='manufacturer'>%s</entry>\n",
1160
                          def->manufacturer);
1161
    virBufferEscapeString(buf, "<entry name='product'>%s</entry>\n",
1162
                          def->product);
1163
    virBufferEscapeString(buf, "<entry name='version'>%s</entry>\n",
1164
                          def->version);
1165
    virBufferEscapeString(buf, "<entry name='serial'>%s</entry>\n",
1166
                          def->serial);
1167
    virBufferEscapeString(buf, "<entry name='uuid'>%s</entry>\n",
1168
                          def->uuid);
1169
    virBufferEscapeString(buf, "<entry name='sku'>%s</entry>\n",
1170
                          def->sku);
1171
    virBufferEscapeString(buf, "<entry name='family'>%s</entry>\n",
1172
                          def->family);
1173 1174
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</system>\n");
M
Minoru Usui 已提交
1175 1176
}

1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
static void
virSysinfoBaseBoardFormat(virBufferPtr buf,
                          virSysinfoBaseBoardDefPtr baseBoard,
                          size_t nbaseBoard)
{
    virSysinfoBaseBoardDefPtr def;
    size_t i;

    for (i = 0; i < nbaseBoard; i++) {
        def = baseBoard + i;

        virBufferAddLit(buf, "<baseBoard>\n");
        virBufferAdjustIndent(buf, 2);
        virBufferEscapeString(buf, "<entry name='manufacturer'>%s</entry>\n",
                              def->manufacturer);
        virBufferEscapeString(buf, "<entry name='product'>%s</entry>\n",
                              def->product);
        virBufferEscapeString(buf, "<entry name='version'>%s</entry>\n",
                              def->version);
        virBufferEscapeString(buf, "<entry name='serial'>%s</entry>\n",
                              def->serial);
        virBufferEscapeString(buf, "<entry name='asset'>%s</entry>\n",
                              def->asset);
        virBufferEscapeString(buf, "<entry name='location'>%s</entry>\n",
                              def->location);
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</baseBoard>\n");
    }
}

1207
static void
1208
virSysinfoProcessorFormat(virBufferPtr buf, virSysinfoDefPtr def)
1209
{
1210
    size_t i;
1211
    virSysinfoProcessorDefPtr processor;
1212 1213 1214 1215

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

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
        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;

1229 1230
        virBufferAddLit(buf, "<processor>\n");
        virBufferAdjustIndent(buf, 2);
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
        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);
1254 1255
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</processor>\n");
1256 1257 1258
    }
}

1259
static void
1260
virSysinfoMemoryFormat(virBufferPtr buf, virSysinfoDefPtr def)
1261
{
1262
    size_t i;
1263
    virSysinfoMemoryDefPtr memory;
1264 1265 1266 1267

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

1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
        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;

1280 1281 1282
        virBufferAddLit(buf, "<memory_device>\n");
        virBufferAdjustIndent(buf, 2);
        virBufferEscapeString(buf, "<entry name='size'>%s</entry>\n",
1283 1284
                              memory->memory_size);
        virBufferEscapeString(buf,
1285
                              "<entry name='form_factor'>%s</entry>\n",
1286
                              memory->memory_form_factor);
1287
        virBufferEscapeString(buf, "<entry name='locator'>%s</entry>\n",
1288 1289
                              memory->memory_locator);
        virBufferEscapeString(buf,
1290
                              "<entry name='bank_locator'>%s</entry>\n",
1291
                              memory->memory_bank_locator);
1292
        virBufferEscapeString(buf, "<entry name='type'>%s</entry>\n",
1293 1294
                              memory->memory_type);
        virBufferEscapeString(buf,
1295
                              "<entry name='type_detail'>%s</entry>\n",
1296
                              memory->memory_type_detail);
1297
        virBufferEscapeString(buf, "<entry name='speed'>%s</entry>\n",
1298 1299
                              memory->memory_speed);
        virBufferEscapeString(buf,
1300
                              "<entry name='manufacturer'>%s</entry>\n",
1301 1302
                              memory->memory_manufacturer);
        virBufferEscapeString(buf,
1303
                              "<entry name='serial_number'>%s</entry>\n",
1304 1305
                              memory->memory_serial_number);
        virBufferEscapeString(buf,
1306
                              "<entry name='part_number'>%s</entry>\n",
1307
                              memory->memory_part_number);
1308 1309
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</memory_device>\n");
1310 1311 1312
    }
}

1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
static void
virSysinfoOEMStringsFormat(virBufferPtr buf, virSysinfoOEMStringsDefPtr def)
{
    size_t i;

    if (!def)
        return;

    virBufferAddLit(buf, "<oemStrings>\n");
    virBufferAdjustIndent(buf, 2);
    for (i = 0; i < def->nvalues; i++) {
        virBufferEscapeString(buf, "<entry>%s</entry>\n",
                              def->values[i]);
    }
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</oemStrings>\n");
}

M
Minoru Usui 已提交
1331 1332
/**
 * virSysinfoFormat:
1333
 * @buf: buffer to append output to (may use auto-indentation)
M
Minoru Usui 已提交
1334 1335
 * @def: structure to convert to xml string
 *
1336
 * Returns 0 on success, -1 on failure after generating an error message.
M
Minoru Usui 已提交
1337
 */
1338 1339
int
virSysinfoFormat(virBufferPtr buf, virSysinfoDefPtr def)
M
Minoru Usui 已提交
1340
{
1341
    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
M
Minoru Usui 已提交
1342
    const char *type = virSysinfoTypeToString(def->type);
1343 1344
    int indent = virBufferGetIndent(buf, false);
    int ret = -1;
M
Minoru Usui 已提交
1345 1346

    if (!type) {
1347 1348 1349
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected sysinfo type model %d"),
                       def->type);
1350
        virBufferFreeAndReset(buf);
1351
        goto cleanup;
E
Eric Blake 已提交
1352 1353
    }

1354
    virBufferAdjustIndent(&childrenBuf, indent + 2);
M
Minoru Usui 已提交
1355

1356
    virSysinfoBIOSFormat(&childrenBuf, def->bios);
1357
    virSysinfoSystemFormat(&childrenBuf, def->system);
1358
    virSysinfoBaseBoardFormat(&childrenBuf, def->baseBoard, def->nbaseBoard);
1359 1360
    virSysinfoProcessorFormat(&childrenBuf, def);
    virSysinfoMemoryFormat(&childrenBuf, def);
1361
    virSysinfoOEMStringsFormat(&childrenBuf, def->oemStrings);
M
Minoru Usui 已提交
1362

1363 1364 1365 1366 1367 1368 1369 1370
    virBufferAsprintf(buf, "<sysinfo type='%s'", type);
    if (virBufferUse(&childrenBuf)) {
        virBufferAddLit(buf, ">\n");
        virBufferAddBuffer(buf, &childrenBuf);
        virBufferAddLit(buf, "</sysinfo>\n");
    } else {
        virBufferAddLit(buf, "/>\n");
    }
E
Eric Blake 已提交
1371

1372
    if (virBufferCheckError(buf) < 0)
1373
        goto cleanup;
1374

1375 1376 1377 1378
    ret = 0;
 cleanup:
    virBufferFreeAndReset(&childrenBuf);
    return ret;
E
Eric Blake 已提交
1379 1380
}

1381 1382 1383 1384
#define CHECK_FIELD(name, desc) \
    do { \
        if (STRNEQ_NULLABLE(src->name, dst->name)) { \
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, \
1385 1386
                           _("Target sysinfo %s %s does not match source %s"), \
                           desc, NULLSTR(dst->name), NULLSTR(src->name)); \
1387 1388
            goto cleanup; \
        } \
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
    } while (0)

static bool
virSysinfoBIOSIsEqual(virSysinfoBIOSDefPtr src,
                      virSysinfoBIOSDefPtr dst)
{
    bool identical = false;

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

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

    CHECK_FIELD(vendor, "BIOS vendor");
    CHECK_FIELD(version, "BIOS version");
    CHECK_FIELD(date, "BIOS date");
    CHECK_FIELD(release, "BIOS release");

    identical = true;
 cleanup:
    return identical;
}

1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
static bool
virSysinfoSystemIsEqual(virSysinfoSystemDefPtr src,
                        virSysinfoSystemDefPtr dst)
{
    bool identical = false;

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

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

    CHECK_FIELD(manufacturer, "system vendor");
    CHECK_FIELD(product, "system product");
    CHECK_FIELD(version, "system version");
    CHECK_FIELD(serial, "system serial");
    CHECK_FIELD(uuid, "system uuid");
    CHECK_FIELD(sku, "system sku");
    CHECK_FIELD(family, "system family");

    identical = true;
 cleanup:
    return identical;
}

1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
static bool
virSysinfoBaseBoardIsEqual(virSysinfoBaseBoardDefPtr src,
                           virSysinfoBaseBoardDefPtr dst)
{
    bool identical = false;

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

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

    CHECK_FIELD(manufacturer, "base board vendor");
    CHECK_FIELD(product, "base board product");
    CHECK_FIELD(version, "base board version");
    CHECK_FIELD(serial, "base board serial");
    CHECK_FIELD(asset, "base board asset");
    CHECK_FIELD(location, "base board location");

    identical = true;
 cleanup:
    return identical;
}

1471 1472
#undef CHECK_FIELD

1473 1474 1475 1476
bool virSysinfoIsEqual(virSysinfoDefPtr src,
                       virSysinfoDefPtr dst)
{
    bool identical = false;
1477
    size_t i;
1478 1479 1480 1481 1482

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

    if ((src && !dst) || (!src && dst)) {
1483 1484
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Target sysinfo does not match source"));
1485 1486 1487 1488
        goto cleanup;
    }

    if (src->type != dst->type) {
1489 1490 1491 1492
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target sysinfo %s does not match source %s"),
                       virSysinfoTypeToString(dst->type),
                       virSysinfoTypeToString(src->type));
1493 1494 1495
        goto cleanup;
    }

1496 1497
    if (!virSysinfoBIOSIsEqual(src->bios, dst->bios))
        goto cleanup;
1498

1499 1500
    if (!virSysinfoSystemIsEqual(src->system, dst->system))
        goto cleanup;
1501

1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
    if (src->nbaseBoard != dst->nbaseBoard) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target sysinfo base board count '%zu' does not match source '%zu'"),
                       dst->nbaseBoard, src->nbaseBoard);
        goto cleanup;
    }

    for (i = 0; i < src->nbaseBoard; i++)
        if (!virSysinfoBaseBoardIsEqual(src->baseBoard + i,
                                        dst->baseBoard + i))
            goto cleanup;

1514 1515
    identical = true;

1516
 cleanup:
1517 1518
    return identical;
}