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

        VIR_FREE(procline);
526
    }
527
    result = 0;
528

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

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

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

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

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

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

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

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

571
    VIR_FREE(outbuf);
572 573
    return ret;

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

E
Eric Blake 已提交
580

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

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

591 592 593
    if (VIR_ALLOC(def) < 0)
        return ret;

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

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

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

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

644 645 646
    if (VIR_ALLOC(def) < 0)
        return ret;

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

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

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

705
static int
706 707 708
virSysinfoParseX86BaseBoard(const char *base,
                            virSysinfoBaseBoardDefPtr *baseBoard,
                            size_t *nbaseBoard)
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 781 782
{
    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;
}

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

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

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

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

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

887
    return 0;
888 889
}

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

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

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

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

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

986
    return 0;
987 988
}

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

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

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

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

    ret->type = VIR_SYSINFO_SMBIOS;

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

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

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

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

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

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

E
Eric Blake 已提交
1039
    return ret;
1040

1041
 error:
1042 1043 1044 1045
    virSysinfoDefFree(ret);
    ret = NULL;
    goto cleanup;
}
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 1078 1079


/**
 * 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();
1080
#endif /* !WIN32 && x86 */
1081 1082
}

E
Eric Blake 已提交
1083

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1306
    if (virBufferCheckError(buf) < 0)
1307
        goto cleanup;
1308

1309 1310 1311 1312
    ret = 0;
 cleanup:
    virBufferFreeAndReset(&childrenBuf);
    return ret;
E
Eric Blake 已提交
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 1348 1349
#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;
}

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 1376 1377
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;
}

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 1403 1404
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;
}

1405 1406
#undef CHECK_FIELD

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

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

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

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

1430 1431
    if (!virSysinfoBIOSIsEqual(src->bios, dst->bios))
        goto cleanup;
1432

1433 1434
    if (!virSysinfoSystemIsEqual(src->system, dst->system))
        goto cleanup;
1435

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

1448 1449
    identical = true;

1450
 cleanup:
1451 1452
    return identical;
}