virsysinfo.c 43.6 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 "virfile.h"
38
#include "virstring.h"
39

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

43 44 45
#define VIR_FROM_THIS VIR_FROM_SYSINFO


46 47 48
VIR_ENUM_IMPL(virSysinfo, VIR_SYSINFO_LAST,
              "smbios");

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

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


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

69 70 71 72 73 74 75 76 77 78 79 80
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);
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
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);
}

96 97 98 99 100 101 102 103 104 105 106 107
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);
}
108

109 110 111 112 113 114 115 116 117
/**
 * virSysinfoDefFree:
 * @def: a sysinfo structure
 *
 * Free up the sysinfo structure
 */

void virSysinfoDefFree(virSysinfoDefPtr def)
{
118
    size_t i;
119

120 121 122
    if (def == NULL)
        return;

123
    virSysinfoBIOSDefFree(def->bios);
124
    virSysinfoSystemDefFree(def->system);
125

126 127 128 129
    for (i = 0; i < def->nbaseBoard; i++)
        virSysinfoBaseBoardDefClear(def->baseBoard + i);
    VIR_FREE(def->baseBoard);

130
    for (i = 0; i < def->nprocessor; i++) {
131 132 133 134 135 136 137 138 139 140 141 142 143
        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);
144
    for (i = 0; i < def->nmemory; i++) {
145 146 147 148 149 150 151 152 153 154 155 156
        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);
157

158 159 160
    VIR_FREE(def);
}

P
Prerna Saxena 已提交
161 162

static int
163
virSysinfoParsePPCSystem(const char *base, virSysinfoSystemDefPtr *sysdef)
P
Prerna Saxena 已提交
164
{
165
    int ret = -1;
P
Prerna Saxena 已提交
166 167
    char *eol = NULL;
    const char *cur;
168
    virSysinfoSystemDefPtr def;
P
Prerna Saxena 已提交
169 170 171 172

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

173 174 175
    if (VIR_ALLOC(def) < 0)
        return ret;

P
Prerna Saxena 已提交
176 177 178 179 180
    base = cur;
    /* Account for format 'platform    : XXXX'*/
    cur = strchr(cur, ':') + 1;
    eol = strchr(cur, '\n');
    virSkipSpaces(&cur);
181 182
    if (eol && VIR_STRNDUP(def->family, cur, eol - cur) < 0)
        goto cleanup;
P
Prerna Saxena 已提交
183 184 185 186 187

    if ((cur = strstr(base, "model")) != NULL) {
        cur = strchr(cur, ':') + 1;
        eol = strchr(cur, '\n');
        virSkipSpaces(&cur);
188 189
        if (eol && VIR_STRNDUP(def->serial, cur, eol - cur) < 0)
            goto cleanup;
P
Prerna Saxena 已提交
190 191 192 193 194 195
    }

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

200 201 202 203 204 205
    if (!def->manufacturer && !def->product && !def->version &&
        !def->serial && !def->uuid && !def->sku && !def->family) {
        virSysinfoSystemDefFree(def);
        def = NULL;
    }

M
Michal Privoznik 已提交
206
    *sysdef = def;
207 208 209 210 211
    def = NULL;
    ret = 0;
 cleanup:
    virSysinfoSystemDefFree(def);
    return ret;
P
Prerna Saxena 已提交
212 213 214
}

static int
215
virSysinfoParsePPCProcessor(const char *base, virSysinfoDefPtr ret)
P
Prerna Saxena 已提交
216 217 218 219 220
{
    const char *cur;
    char *eol, *tmp_base;
    virSysinfoProcessorDefPtr processor;

221
    while ((tmp_base = strstr(base, "processor")) != NULL) {
P
Prerna Saxena 已提交
222 223 224 225
        base = tmp_base;
        eol = strchr(base, '\n');
        cur = strchr(base, ':') + 1;

226
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
227
            return -1;
P
Prerna Saxena 已提交
228 229 230
        processor = &ret->processor[ret->nprocessor - 1];

        virSkipSpaces(&cur);
231 232 233
        if (eol && VIR_STRNDUP(processor->processor_socket_destination,
                               cur, eol - cur) < 0)
            return -1;
234
        base = cur;
P
Prerna Saxena 已提交
235 236 237 238 239

        if ((cur = strstr(base, "cpu")) != NULL) {
            cur = strchr(cur, ':') + 1;
            eol = strchr(cur, '\n');
            virSkipSpaces(&cur);
240 241 242
            if (eol && VIR_STRNDUP(processor->processor_type,
                                   cur, eol - cur) < 0)
                return -1;
243
            base = cur;
P
Prerna Saxena 已提交
244 245 246 247 248 249
        }

        if ((cur = strstr(base, "revision")) != NULL) {
            cur = strchr(cur, ':') + 1;
            eol = strchr(cur, '\n');
            virSkipSpaces(&cur);
250 251 252
            if (eol && VIR_STRNDUP(processor->processor_version,
                                   cur, eol - cur) < 0)
                return -1;
253
            base = cur;
P
Prerna Saxena 已提交
254 255 256 257 258 259 260 261 262 263
        }

    }

    return 0;
}

/* virSysinfoRead for PowerPC
 * Gathers sysinfo data from /proc/cpuinfo */
virSysinfoDefPtr
264
virSysinfoReadPPC(void)
265
{
P
Prerna Saxena 已提交
266 267 268 269 270 271
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;

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

272
    if (virFileReadAll(CPUINFO, CPUINFO_FILE_LEN, &outbuf) < 0) {
273 274
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), CPUINFO);
275
        goto no_memory;
P
Prerna Saxena 已提交
276 277 278 279
    }

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

283
    if (virSysinfoParsePPCSystem(outbuf, &ret->system) < 0)
P
Prerna Saxena 已提交
284 285
        goto no_memory;

286
    VIR_FREE(outbuf);
P
Prerna Saxena 已提交
287 288
    return ret;

289
 no_memory:
P
Prerna Saxena 已提交
290
    VIR_FREE(outbuf);
291
    virSysinfoDefFree(ret);
P
Prerna Saxena 已提交
292 293 294
    return NULL;
}

295

296
static int
297
virSysinfoParseARMSystem(const char *base, virSysinfoSystemDefPtr *sysdef)
298
{
299
    int ret = -1;
300 301
    char *eol = NULL;
    const char *cur;
302
    virSysinfoSystemDefPtr def;
303 304 305 306

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

307 308 309
    if (VIR_ALLOC(def) < 0)
        return ret;

310 311 312 313 314
    base = cur;
    /* Account for format 'platform    : XXXX'*/
    cur = strchr(cur, ':') + 1;
    eol = strchr(cur, '\n');
    virSkipSpaces(&cur);
315 316
    if (eol && VIR_STRNDUP(def->family, cur, eol - cur) < 0)
        goto cleanup;
317 318 319 320 321

    if ((cur = strstr(base, "model")) != NULL) {
        cur = strchr(cur, ':') + 1;
        eol = strchr(cur, '\n');
        virSkipSpaces(&cur);
322 323
        if (eol && VIR_STRNDUP(def->serial, cur, eol - cur) < 0)
            goto cleanup;
324 325 326 327 328 329
    }

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

334 335 336 337 338 339
    if (!def->manufacturer && !def->product && !def->version &&
        !def->serial && !def->uuid && !def->sku && !def->family) {
        virSysinfoSystemDefFree(def);
        def = NULL;
    }

M
Michal Privoznik 已提交
340
    *sysdef = def;
341 342 343 344 345
    def = NULL;
    ret = 0;
 cleanup:
    virSysinfoSystemDefFree(def);
    return ret;
346 347 348
}

static int
349
virSysinfoParseARMProcessor(const char *base, virSysinfoDefPtr ret)
350 351 352 353 354 355
{
    const char *cur;
    char *eol, *tmp_base;
    virSysinfoProcessorDefPtr processor;
    char *processor_type = NULL;

M
Michal Privoznik 已提交
356 357
    if (!(tmp_base = strstr(base, "model name")) &&
        !(tmp_base = strstr(base, "Processor")))
358 359
        return 0;

M
Michal Privoznik 已提交
360 361
    eol = strchr(tmp_base, '\n');
    cur = strchr(tmp_base, ':') + 1;
362
    virSkipSpaces(&cur);
363 364
    if (eol && VIR_STRNDUP(processor_type, cur, eol - cur) < 0)
        goto error;
365 366 367 368 369 370

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

371
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
372
            goto error;
373 374 375 376
        processor = &ret->processor[ret->nprocessor - 1];

        virSkipSpaces(&cur);
        if (eol &&
377 378 379
            VIR_STRNDUP(processor->processor_socket_destination,
                        cur, eol - cur) < 0)
            goto error;
380

381
        if (VIR_STRDUP(processor->processor_type, processor_type) < 0)
382
            goto error;
383 384 385 386 387 388 389

        base = cur;
    }

    VIR_FREE(processor_type);
    return 0;

390
 error:
391 392 393 394 395 396 397
    VIR_FREE(processor_type);
    return -1;
}

/* virSysinfoRead for ARMv7
 * Gathers sysinfo data from /proc/cpuinfo */
virSysinfoDefPtr
398
virSysinfoReadARM(void)
399
{
400 401 402 403 404 405
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;

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

406
    if (virFileReadAll(CPUINFO, CPUINFO_FILE_LEN, &outbuf) < 0) {
407 408
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), CPUINFO);
409
        goto no_memory;
410 411 412 413
    }

    ret->nprocessor = 0;
    ret->processor = NULL;
414
    if (virSysinfoParseARMProcessor(outbuf, ret) < 0)
415 416
        goto no_memory;

417
    if (virSysinfoParseARMSystem(outbuf, &ret->system) < 0)
418 419
        goto no_memory;

420
    VIR_FREE(outbuf);
421 422
    return ret;

423
 no_memory:
424
    VIR_FREE(outbuf);
425
    virSysinfoDefFree(ret);
426 427 428 429
    return NULL;
}


430 431

VIR_WARNINGS_NO_WLOGICALOP_STRCHR
432

433
static char *
434 435
virSysinfoParseS390Delimited(const char *base, const char *name, char **value,
                             char delim1, char delim2)
436 437 438 439 440 441 442 443 444 445
{
    const char *start;
    char *end;

    if (delim1 != delim2 &&
        (start = strstr(base, name)) &&
        (start = strchr(start, delim1))) {
        start += 1;
        end = strchrnul(start, delim2);
        virSkipSpaces(&start);
446 447
        if (VIR_STRNDUP(*value, start, end - start) < 0)
            return NULL;
448 449
        virTrimSpaces(*value, NULL);
        return end;
450
    }
451 452
    return NULL;
}
453

454
static char *
455
virSysinfoParseS390Line(const char *base, const char *name, char **value)
456
{
457
    return virSysinfoParseS390Delimited(base, name, value, ':', '\n');
458 459 460
}

static int
461
virSysinfoParseS390System(const char *base, virSysinfoSystemDefPtr *sysdef)
462
{
463 464 465 466 467 468
    int ret = -1;
    virSysinfoSystemDefPtr def;

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

469
    if (!virSysinfoParseS390Line(base, "Manufacturer", &def->manufacturer))
470 471
        goto cleanup;

472
    if (!virSysinfoParseS390Line(base, "Type", &def->family))
473 474
        goto cleanup;

475
    if (!virSysinfoParseS390Line(base, "Sequence Code", &def->serial))
476 477 478 479 480 481 482 483
        goto cleanup;

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

M
Michal Privoznik 已提交
484
    *sysdef = def;
485 486 487 488 489
    def = NULL;
    ret = 0;
 cleanup:
    virSysinfoSystemDefFree(def);
    return ret;
490 491 492
}

static int
493
virSysinfoParseS390Processor(const char *base, virSysinfoDefPtr ret)
494
{
495 496 497 498
    char *tmp_base;
    char *manufacturer = NULL;
    char *procline = NULL;
    int result = -1;
499 500
    virSysinfoProcessorDefPtr processor;

501
    if (!(tmp_base = virSysinfoParseS390Line(base, "vendor_id", &manufacturer)))
502
        goto cleanup;
503

504 505 506
    /* Find processor N: line and gather the processor manufacturer,
       version, serial number, and family */
    while ((tmp_base = strstr(tmp_base, "processor "))
507 508
           && (tmp_base = virSysinfoParseS390Line(tmp_base, "processor ",
                                                  &procline))) {
509
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
510
            goto cleanup;
511
        processor = &ret->processor[ret->nprocessor - 1];
512 513
        if (VIR_STRDUP(processor->processor_manufacturer, manufacturer) < 0)
            goto cleanup;
514 515 516 517 518 519 520 521 522
        if (!virSysinfoParseS390Delimited(procline, "version",
                                          &processor->processor_version,
                                          '=', ',') ||
            !virSysinfoParseS390Delimited(procline, "identification",
                                          &processor->processor_serial_number,
                                          '=', ',') ||
            !virSysinfoParseS390Delimited(procline, "machine",
                                          &processor->processor_family,
                                          '=', '\n'))
523
            goto cleanup;
524
    }
525
    result = 0;
526

527
 cleanup:
528 529 530
    VIR_FREE(manufacturer);
    VIR_FREE(procline);
    return result;
531 532 533 534 535
}

/* virSysinfoRead for s390x
 * Gathers sysinfo data from /proc/sysinfo and /proc/cpuinfo */
virSysinfoDefPtr
536
virSysinfoReadS390(void)
537
{
538 539 540 541 542 543 544
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;

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

    /* Gather info from /proc/cpuinfo */
545
    if (virFileReadAll(CPUINFO, CPUINFO_FILE_LEN, &outbuf) < 0) {
546 547
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), CPUINFO);
548
        goto no_memory;
549 550 551 552
    }

    ret->nprocessor = 0;
    ret->processor = NULL;
553
    if (virSysinfoParseS390Processor(outbuf, ret) < 0)
554 555 556 557 558 559
        goto no_memory;

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

    /* Gather info from /proc/sysinfo */
560
    if (virFileReadAll(SYSINFO, 8192, &outbuf) < 0) {
561 562
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), SYSINFO);
563
        goto no_memory;
564 565
    }

566
    if (virSysinfoParseS390System(outbuf, &ret->system) < 0)
567 568
        goto no_memory;

569
    VIR_FREE(outbuf);
570 571
    return ret;

572
 no_memory:
573
    virSysinfoDefFree(ret);
574 575 576 577
    VIR_FREE(outbuf);
    return NULL;
}

E
Eric Blake 已提交
578

579
static int
580
virSysinfoParseBIOS(const char *base, virSysinfoBIOSDefPtr *bios)
M
Minoru Usui 已提交
581
{
582
    int ret = -1;
583
    const char *cur, *eol = NULL;
584
    virSysinfoBIOSDefPtr def;
585

M
Minoru Usui 已提交
586
    if ((cur = strstr(base, "BIOS Information")) == NULL)
587
        return 0;
M
Minoru Usui 已提交
588

589 590 591
    if (VIR_ALLOC(def) < 0)
        return ret;

M
Minoru Usui 已提交
592
    base = cur;
593 594 595
    if ((cur = strstr(base, "Vendor: ")) != NULL) {
        cur += 8;
        eol = strchr(cur, '\n');
596 597
        if (eol && VIR_STRNDUP(def->vendor, cur, eol - cur) < 0)
            goto cleanup;
598 599 600 601
    }
    if ((cur = strstr(base, "Version: ")) != NULL) {
        cur += 9;
        eol = strchr(cur, '\n');
602 603
        if (eol && VIR_STRNDUP(def->version, cur, eol - cur) < 0)
            goto cleanup;
604 605 606 607
    }
    if ((cur = strstr(base, "Release Date: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
608 609
        if (eol && VIR_STRNDUP(def->date, cur, eol - cur) < 0)
            goto cleanup;
610 611 612 613
    }
    if ((cur = strstr(base, "BIOS Revision: ")) != NULL) {
        cur += 15;
        eol = strchr(cur, '\n');
614 615
        if (eol && VIR_STRNDUP(def->release, cur, eol - cur) < 0)
            goto cleanup;
616
    }
M
Minoru Usui 已提交
617

618 619 620 621 622 623 624 625 626 627 628 629
    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 已提交
630 631
}

632
static int
633
virSysinfoParseX86System(const char *base, virSysinfoSystemDefPtr *sysdef)
M
Minoru Usui 已提交
634
{
635
    int ret = -1;
636
    const char *cur, *eol = NULL;
637
    virSysinfoSystemDefPtr def;
M
Minoru Usui 已提交
638

M
Minoru Usui 已提交
639
    if ((cur = strstr(base, "System Information")) == NULL)
640
        return 0;
M
Minoru Usui 已提交
641

642 643 644
    if (VIR_ALLOC(def) < 0)
        return ret;

M
Minoru Usui 已提交
645
    base = cur;
646 647 648
    if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
649 650
        if (eol && VIR_STRNDUP(def->manufacturer, cur, eol - cur) < 0)
            goto cleanup;
651 652 653 654
    }
    if ((cur = strstr(base, "Product Name: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
655 656
        if (eol && VIR_STRNDUP(def->product, cur, eol - cur) < 0)
            goto cleanup;
657 658 659 660
    }
    if ((cur = strstr(base, "Version: ")) != NULL) {
        cur += 9;
        eol = strchr(cur, '\n');
661 662
        if (eol && VIR_STRNDUP(def->version, cur, eol - cur) < 0)
            goto cleanup;
663 664 665 666
    }
    if ((cur = strstr(base, "Serial Number: ")) != NULL) {
        cur += 15;
        eol = strchr(cur, '\n');
667 668
        if (eol && VIR_STRNDUP(def->serial, cur, eol - cur) < 0)
            goto cleanup;
669 670 671 672
    }
    if ((cur = strstr(base, "UUID: ")) != NULL) {
        cur += 6;
        eol = strchr(cur, '\n');
673 674
        if (eol && VIR_STRNDUP(def->uuid, cur, eol - cur) < 0)
            goto cleanup;
675 676 677 678
    }
    if ((cur = strstr(base, "SKU Number: ")) != NULL) {
        cur += 12;
        eol = strchr(cur, '\n');
679 680
        if (eol && VIR_STRNDUP(def->sku, cur, eol - cur) < 0)
            goto cleanup;
681
    }
E
Eric Blake 已提交
682 683 684
    if ((cur = strstr(base, "Family: ")) != NULL) {
        cur += 8;
        eol = strchr(cur, '\n');
685 686
        if (eol && VIR_STRNDUP(def->family, cur, eol - cur) < 0)
            goto cleanup;
E
Eric Blake 已提交
687
    }
688

689 690 691 692 693 694
    if (!def->manufacturer && !def->product && !def->version &&
        !def->serial && !def->uuid && !def->sku && !def->family) {
        virSysinfoSystemDefFree(def);
        def = NULL;
    }

M
Michal Privoznik 已提交
695
    *sysdef = def;
696 697 698 699 700
    def = NULL;
    ret = 0;
 cleanup:
    virSysinfoSystemDefFree(def);
    return ret;
M
Minoru Usui 已提交
701 702
}

703
static int
704 705 706
virSysinfoParseX86BaseBoard(const char *base,
                            virSysinfoBaseBoardDefPtr *baseBoard,
                            size_t *nbaseBoard)
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 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
{
    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;
}

781
static int
782
virSysinfoParseX86Processor(const char *base, virSysinfoDefPtr ret)
783
{
784 785
    const char *cur, *tmp_base;
    char *eol;
786
    virSysinfoProcessorDefPtr processor;
787

788
    while ((tmp_base = strstr(base, "Processor Information")) != NULL) {
789
        base = tmp_base;
E
Eric Blake 已提交
790
        eol = NULL;
791

792 793
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
            return -1;
794 795 796 797 798
        processor = &ret->processor[ret->nprocessor - 1];

        if ((cur = strstr(base, "Socket Designation: ")) != NULL) {
            cur += 20;
            eol = strchr(cur, '\n');
799
            virSkipSpacesBackwards(cur, &eol);
800 801 802
            if (eol && VIR_STRNDUP(processor->processor_socket_destination,
                                   cur, eol - cur) < 0)
                return -1;
803 804 805 806
        }
        if ((cur = strstr(base, "Type: ")) != NULL) {
            cur += 6;
            eol = strchr(cur, '\n');
807
            virSkipSpacesBackwards(cur, &eol);
808 809
            if (eol && VIR_STRNDUP(processor->processor_type, cur, eol - cur) < 0)
                return -1;
810 811 812 813
        }
        if ((cur = strstr(base, "Family: ")) != NULL) {
            cur += 8;
            eol = strchr(cur, '\n');
814
            virSkipSpacesBackwards(cur, &eol);
815 816
            if (eol && VIR_STRNDUP(processor->processor_family, cur, eol - cur) < 0)
                return -1;
817 818 819 820
        }
        if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
            cur += 14;
            eol = strchr(cur, '\n');
821
            virSkipSpacesBackwards(cur, &eol);
822 823 824
            if (eol && VIR_STRNDUP(processor->processor_manufacturer,
                                   cur, eol - cur) < 0)
                return -1;
825 826 827 828
        }
        if ((cur = strstr(base, "Signature: ")) != NULL) {
            cur += 11;
            eol = strchr(cur, '\n');
829
            virSkipSpacesBackwards(cur, &eol);
830 831 832
            if (eol && VIR_STRNDUP(processor->processor_signature,
                                   cur, eol - cur) < 0)
                return -1;
833 834 835 836
        }
        if ((cur = strstr(base, "Version: ")) != NULL) {
            cur += 9;
            eol = strchr(cur, '\n');
837
            virSkipSpacesBackwards(cur, &eol);
838 839 840
            if (eol && VIR_STRNDUP(processor->processor_version,
                                   cur, eol - cur) < 0)
                return -1;
841 842 843 844
        }
        if ((cur = strstr(base, "External Clock: ")) != NULL) {
            cur += 16;
            eol = strchr(cur, '\n');
845
            virSkipSpacesBackwards(cur, &eol);
846 847 848
            if (eol && VIR_STRNDUP(processor->processor_external_clock,
                                   cur, eol - cur) < 0)
                return -1;
849 850 851 852
        }
        if ((cur = strstr(base, "Max Speed: ")) != NULL) {
            cur += 11;
            eol = strchr(cur, '\n');
853
            virSkipSpacesBackwards(cur, &eol);
854 855 856
            if (eol && VIR_STRNDUP(processor->processor_max_speed,
                                   cur, eol - cur) < 0)
                return -1;
857 858 859 860
        }
        if ((cur = strstr(base, "Status: ")) != NULL) {
            cur += 8;
            eol = strchr(cur, '\n');
861
            virSkipSpacesBackwards(cur, &eol);
862 863
            if (eol && VIR_STRNDUP(processor->processor_status, cur, eol - cur) < 0)
                return -1;
864 865 866 867
        }
        if ((cur = strstr(base, "Serial Number: ")) != NULL) {
            cur += 15;
            eol = strchr(cur, '\n');
868
            virSkipSpacesBackwards(cur, &eol);
869 870 871
            if (eol && VIR_STRNDUP(processor->processor_serial_number,
                                   cur, eol - cur) < 0)
                return -1;
872 873 874 875
        }
        if ((cur = strstr(base, "Part Number: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
876
            virSkipSpacesBackwards(cur, &eol);
877 878 879
            if (eol && VIR_STRNDUP(processor->processor_part_number,
                                   cur, eol - cur) < 0)
                return -1;
880 881
        }

M
Minoru Usui 已提交
882
        base += strlen("Processor Information");
883 884
    }

885
    return 0;
886 887
}

888
static int
889
virSysinfoParseX86Memory(const char *base, virSysinfoDefPtr ret)
890
{
891 892
    const char *cur, *tmp_base;
    char *eol;
893
    virSysinfoMemoryDefPtr memory;
894 895 896

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

899 900
        if (VIR_EXPAND_N(ret->memory, ret->nmemory, 1) < 0)
            return -1;
901 902 903 904 905 906 907 908
        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;

909
            virSkipSpacesBackwards(cur, &eol);
910 911
            if (eol && VIR_STRNDUP(memory->memory_size, cur, eol - cur) < 0)
                return -1;
912 913 914 915
        }
        if ((cur = strstr(base, "Form Factor: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
916
            virSkipSpacesBackwards(cur, &eol);
917 918 919
            if (eol && VIR_STRNDUP(memory->memory_form_factor,
                                   cur, eol - cur) < 0)
                return -1;
920 921 922 923
        }
        if ((cur = strstr(base, "Locator: ")) != NULL) {
            cur += 9;
            eol = strchr(cur, '\n');
924
            virSkipSpacesBackwards(cur, &eol);
E
Eric Blake 已提交
925
            if (eol && VIR_STRNDUP(memory->memory_locator, cur, eol - cur) < 0)
926
                return -1;
927 928 929 930
        }
        if ((cur = strstr(base, "Bank Locator: ")) != NULL) {
            cur += 14;
            eol = strchr(cur, '\n');
931
            virSkipSpacesBackwards(cur, &eol);
932 933 934
            if (eol && VIR_STRNDUP(memory->memory_bank_locator,
                                   cur, eol - cur) < 0)
                return -1;
935 936 937 938
        }
        if ((cur = strstr(base, "Type: ")) != NULL) {
            cur += 6;
            eol = strchr(cur, '\n');
939
            virSkipSpacesBackwards(cur, &eol);
940 941
            if (eol && VIR_STRNDUP(memory->memory_type, cur, eol - cur) < 0)
                return -1;
942 943 944 945
        }
        if ((cur = strstr(base, "Type Detail: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
946
            virSkipSpacesBackwards(cur, &eol);
947 948
            if (eol && VIR_STRNDUP(memory->memory_type_detail, cur, eol - cur) < 0)
                return -1;
949 950 951 952
        }
        if ((cur = strstr(base, "Speed: ")) != NULL) {
            cur += 7;
            eol = strchr(cur, '\n');
953
            virSkipSpacesBackwards(cur, &eol);
954 955
            if (eol && VIR_STRNDUP(memory->memory_speed, cur, eol - cur) < 0)
                return -1;
956 957 958 959
        }
        if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
            cur += 14;
            eol = strchr(cur, '\n');
960
            virSkipSpacesBackwards(cur, &eol);
961 962
            if (eol && VIR_STRNDUP(memory->memory_manufacturer, cur, eol - cur) < 0)
                return -1;
963 964 965 966
        }
        if ((cur = strstr(base, "Serial Number: ")) != NULL) {
            cur += 15;
            eol = strchr(cur, '\n');
967
            virSkipSpacesBackwards(cur, &eol);
968 969 970
            if (eol && VIR_STRNDUP(memory->memory_serial_number,
                                   cur, eol - cur) < 0)
                return -1;
971 972 973 974
        }
        if ((cur = strstr(base, "Part Number: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
975
            virSkipSpacesBackwards(cur, &eol);
976 977
            if (eol && VIR_STRNDUP(memory->memory_part_number, cur, eol - cur) < 0)
                return -1;
978 979 980
        }

    next:
M
Minoru Usui 已提交
981
        base += strlen("Memory Device");
982 983
    }

984
    return 0;
985 986
}

M
Minoru Usui 已提交
987
virSysinfoDefPtr
988
virSysinfoReadX86(void)
989
{
990
    char *path;
M
Minoru Usui 已提交
991 992 993 994 995 996
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;
    virCommandPtr cmd;

    path = virFindFileInPath(SYSINFO_SMBIOS_DECODER);
    if (path == NULL) {
997 998 999
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to find path for %s binary"),
                       SYSINFO_SMBIOS_DECODER);
M
Minoru Usui 已提交
1000 1001 1002
        return NULL;
    }

1003
    cmd = virCommandNewArgList(path, "-q", "-t", "0,1,2,4,17", NULL);
M
Minoru Usui 已提交
1004 1005
    VIR_FREE(path);
    virCommandSetOutputBuffer(cmd, &outbuf);
1006
    if (virCommandRun(cmd, NULL) < 0)
M
Minoru Usui 已提交
1007 1008 1009
        goto cleanup;

    if (VIR_ALLOC(ret) < 0)
1010
        goto error;
M
Minoru Usui 已提交
1011 1012 1013

    ret->type = VIR_SYSINFO_SMBIOS;

1014
    if (virSysinfoParseBIOS(outbuf, &ret->bios) < 0)
1015
        goto error;
M
Minoru Usui 已提交
1016

1017
    if (virSysinfoParseX86System(outbuf, &ret->system) < 0)
1018
        goto error;
M
Minoru Usui 已提交
1019

1020
    if (virSysinfoParseX86BaseBoard(outbuf, &ret->baseBoard, &ret->nbaseBoard) < 0)
1021 1022
        goto error;

1023 1024
    ret->nprocessor = 0;
    ret->processor = NULL;
1025
    if (virSysinfoParseX86Processor(outbuf, ret) < 0)
1026
        goto error;
1027

1028 1029
    ret->nmemory = 0;
    ret->memory = NULL;
1030
    if (virSysinfoParseX86Memory(outbuf, ret) < 0)
1031
        goto error;
1032

1033
 cleanup:
1034
    VIR_FREE(outbuf);
E
Eric Blake 已提交
1035
    virCommandFree(cmd);
1036

E
Eric Blake 已提交
1037
    return ret;
1038

1039
 error:
1040 1041 1042 1043
    virSysinfoDefFree(ret);
    ret = NULL;
    goto cleanup;
}
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077


/**
 * 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__) || \
      defined(__i386__) ||   \
      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();
1078
#endif /* !WIN32 && x86 */
1079 1080
}

E
Eric Blake 已提交
1081

M
Minoru Usui 已提交
1082
static void
1083
virSysinfoBIOSFormat(virBufferPtr buf, virSysinfoBIOSDefPtr def)
E
Eric Blake 已提交
1084
{
1085
    if (!def)
1086
        return;
M
Minoru Usui 已提交
1087

1088 1089 1090
    virBufferAddLit(buf, "<bios>\n");
    virBufferAdjustIndent(buf, 2);
    virBufferEscapeString(buf, "<entry name='vendor'>%s</entry>\n",
1091
                          def->vendor);
1092
    virBufferEscapeString(buf, "<entry name='version'>%s</entry>\n",
1093
                          def->version);
1094
    virBufferEscapeString(buf, "<entry name='date'>%s</entry>\n",
1095
                          def->date);
1096
    virBufferEscapeString(buf, "<entry name='release'>%s</entry>\n",
1097
                          def->release);
1098 1099
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</bios>\n");
M
Minoru Usui 已提交
1100 1101 1102
}

static void
1103
virSysinfoSystemFormat(virBufferPtr buf, virSysinfoSystemDefPtr def)
M
Minoru Usui 已提交
1104
{
1105
    if (!def)
1106
        return;
M
Minoru Usui 已提交
1107

1108 1109 1110
    virBufferAddLit(buf, "<system>\n");
    virBufferAdjustIndent(buf, 2);
    virBufferEscapeString(buf, "<entry name='manufacturer'>%s</entry>\n",
1111
                          def->manufacturer);
1112
    virBufferEscapeString(buf, "<entry name='product'>%s</entry>\n",
1113
                          def->product);
1114
    virBufferEscapeString(buf, "<entry name='version'>%s</entry>\n",
1115
                          def->version);
1116
    virBufferEscapeString(buf, "<entry name='serial'>%s</entry>\n",
1117
                          def->serial);
1118
    virBufferEscapeString(buf, "<entry name='uuid'>%s</entry>\n",
1119
                          def->uuid);
1120
    virBufferEscapeString(buf, "<entry name='sku'>%s</entry>\n",
1121
                          def->sku);
1122
    virBufferEscapeString(buf, "<entry name='family'>%s</entry>\n",
1123
                          def->family);
1124 1125
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</system>\n");
M
Minoru Usui 已提交
1126 1127
}

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
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");
    }
}

1158
static void
1159
virSysinfoProcessorFormat(virBufferPtr buf, virSysinfoDefPtr def)
1160
{
1161
    size_t i;
1162
    virSysinfoProcessorDefPtr processor;
1163 1164 1165 1166

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

1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
        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;

1180 1181
        virBufferAddLit(buf, "<processor>\n");
        virBufferAdjustIndent(buf, 2);
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
        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);
1205 1206
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</processor>\n");
1207 1208 1209
    }
}

1210
static void
1211
virSysinfoMemoryFormat(virBufferPtr buf, virSysinfoDefPtr def)
1212
{
1213
    size_t i;
1214
    virSysinfoMemoryDefPtr memory;
1215 1216 1217 1218

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

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
        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;

1231 1232 1233
        virBufferAddLit(buf, "<memory_device>\n");
        virBufferAdjustIndent(buf, 2);
        virBufferEscapeString(buf, "<entry name='size'>%s</entry>\n",
1234 1235
                              memory->memory_size);
        virBufferEscapeString(buf,
1236
                              "<entry name='form_factor'>%s</entry>\n",
1237
                              memory->memory_form_factor);
1238
        virBufferEscapeString(buf, "<entry name='locator'>%s</entry>\n",
1239 1240
                              memory->memory_locator);
        virBufferEscapeString(buf,
1241
                              "<entry name='bank_locator'>%s</entry>\n",
1242
                              memory->memory_bank_locator);
1243
        virBufferEscapeString(buf, "<entry name='type'>%s</entry>\n",
1244 1245
                              memory->memory_type);
        virBufferEscapeString(buf,
1246
                              "<entry name='type_detail'>%s</entry>\n",
1247
                              memory->memory_type_detail);
1248
        virBufferEscapeString(buf, "<entry name='speed'>%s</entry>\n",
1249 1250
                              memory->memory_speed);
        virBufferEscapeString(buf,
1251
                              "<entry name='manufacturer'>%s</entry>\n",
1252 1253
                              memory->memory_manufacturer);
        virBufferEscapeString(buf,
1254
                              "<entry name='serial_number'>%s</entry>\n",
1255 1256
                              memory->memory_serial_number);
        virBufferEscapeString(buf,
1257
                              "<entry name='part_number'>%s</entry>\n",
1258
                              memory->memory_part_number);
1259 1260
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</memory_device>\n");
1261 1262 1263
    }
}

M
Minoru Usui 已提交
1264 1265
/**
 * virSysinfoFormat:
1266
 * @buf: buffer to append output to (may use auto-indentation)
M
Minoru Usui 已提交
1267 1268
 * @def: structure to convert to xml string
 *
1269
 * Returns 0 on success, -1 on failure after generating an error message.
M
Minoru Usui 已提交
1270
 */
1271 1272
int
virSysinfoFormat(virBufferPtr buf, virSysinfoDefPtr def)
M
Minoru Usui 已提交
1273
{
1274
    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
M
Minoru Usui 已提交
1275
    const char *type = virSysinfoTypeToString(def->type);
1276 1277
    int indent = virBufferGetIndent(buf, false);
    int ret = -1;
M
Minoru Usui 已提交
1278 1279

    if (!type) {
1280 1281 1282
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected sysinfo type model %d"),
                       def->type);
1283
        virBufferFreeAndReset(buf);
1284
        goto cleanup;
E
Eric Blake 已提交
1285 1286
    }

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

1289
    virSysinfoBIOSFormat(&childrenBuf, def->bios);
1290
    virSysinfoSystemFormat(&childrenBuf, def->system);
1291
    virSysinfoBaseBoardFormat(&childrenBuf, def->baseBoard, def->nbaseBoard);
1292 1293
    virSysinfoProcessorFormat(&childrenBuf, def);
    virSysinfoMemoryFormat(&childrenBuf, def);
M
Minoru Usui 已提交
1294

1295 1296 1297 1298 1299 1300 1301 1302
    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 已提交
1303

1304
    if (virBufferCheckError(buf) < 0)
1305
        goto cleanup;
1306

1307 1308 1309 1310
    ret = 0;
 cleanup:
    virBufferFreeAndReset(&childrenBuf);
    return ret;
E
Eric Blake 已提交
1311 1312
}

1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
#define CHECK_FIELD(name, desc)                                         \
    do {                                                                \
        if (STRNEQ_NULLABLE(src->name, dst->name)) {                    \
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,                  \
                           _("Target sysinfo %s %s does not match source %s"), \
                           desc, NULLSTR(dst->name), NULLSTR(src->name)); \
            goto cleanup;                                               \
        }                                                               \
    } 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;
}

1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
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;
}

1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
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;
}

1403 1404
#undef CHECK_FIELD

1405 1406 1407 1408
bool virSysinfoIsEqual(virSysinfoDefPtr src,
                       virSysinfoDefPtr dst)
{
    bool identical = false;
1409
    size_t i;
1410 1411 1412 1413 1414

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

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

    if (src->type != dst->type) {
1421 1422 1423 1424
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target sysinfo %s does not match source %s"),
                       virSysinfoTypeToString(dst->type),
                       virSysinfoTypeToString(src->type));
1425 1426 1427
        goto cleanup;
    }

1428 1429
    if (!virSysinfoBIOSIsEqual(src->bios, dst->bios))
        goto cleanup;
1430

1431 1432
    if (!virSysinfoSystemIsEqual(src->system, dst->system))
        goto cleanup;
1433

1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
    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;

1446 1447
    identical = true;

1448
 cleanup:
1449 1450
    return identical;
}