virsysinfo.c 48.5 KB
Newer Older
1
/*
2
 * virsysinfo.c: get SMBIOS/sysinfo information from the host
3
 *
4
 * Copyright (C) 2010-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2010 Daniel Veillard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * Author: Daniel Veillard <veillard@redhat.com>
 */

#include <config.h>

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

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

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

44 45
#define VIR_FROM_THIS VIR_FROM_SYSINFO

46
VIR_LOG_INIT("util.sysinfo");
47

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

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

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


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

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

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

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

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

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

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

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

void virSysinfoChassisDefFree(virSysinfoChassisDefPtr def)
{
    if (def == NULL)
        return;

    VIR_FREE(def->manufacturer);
    VIR_FREE(def->version);
    VIR_FREE(def->serial);
    VIR_FREE(def->asset);
    VIR_FREE(def->sku);
    VIR_FREE(def);
}


126 127 128 129 130 131 132 133 134 135 136 137 138 139
void virSysinfoOEMStringsDefFree(virSysinfoOEMStringsDefPtr def)
{
    size_t i;

    if (def == NULL)
        return;

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

    VIR_FREE(def);
}

140 141 142 143 144 145 146 147 148
/**
 * virSysinfoDefFree:
 * @def: a sysinfo structure
 *
 * Free up the sysinfo structure
 */

void virSysinfoDefFree(virSysinfoDefPtr def)
{
149
    size_t i;
150

151 152 153
    if (def == NULL)
        return;

154
    virSysinfoBIOSDefFree(def->bios);
155
    virSysinfoSystemDefFree(def->system);
156

157 158 159 160
    for (i = 0; i < def->nbaseBoard; i++)
        virSysinfoBaseBoardDefClear(def->baseBoard + i);
    VIR_FREE(def->baseBoard);

161 162
    virSysinfoChassisDefFree(def->chassis);

163
    for (i = 0; i < def->nprocessor; i++) {
164 165 166 167 168 169 170 171 172 173 174 175 176
        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);
177
    for (i = 0; i < def->nmemory; i++) {
178 179 180 181 182 183 184 185 186 187 188 189
        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);
190

191 192
    virSysinfoOEMStringsDefFree(def->oemStrings);

193 194 195
    VIR_FREE(def);
}

P
Prerna Saxena 已提交
196 197

static int
198
virSysinfoParsePPCSystem(const char *base, virSysinfoSystemDefPtr *sysdef)
P
Prerna Saxena 已提交
199
{
200
    int ret = -1;
P
Prerna Saxena 已提交
201 202
    char *eol = NULL;
    const char *cur;
203
    virSysinfoSystemDefPtr def;
P
Prerna Saxena 已提交
204 205 206 207

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

208 209 210
    if (VIR_ALLOC(def) < 0)
        return ret;

P
Prerna Saxena 已提交
211 212 213 214 215
    base = cur;
    /* Account for format 'platform    : XXXX'*/
    cur = strchr(cur, ':') + 1;
    eol = strchr(cur, '\n');
    virSkipSpaces(&cur);
216 217
    if (eol && VIR_STRNDUP(def->family, cur, eol - cur) < 0)
        goto cleanup;
P
Prerna Saxena 已提交
218 219 220 221 222

    if ((cur = strstr(base, "model")) != NULL) {
        cur = strchr(cur, ':') + 1;
        eol = strchr(cur, '\n');
        virSkipSpaces(&cur);
223 224
        if (eol && VIR_STRNDUP(def->serial, cur, eol - cur) < 0)
            goto cleanup;
P
Prerna Saxena 已提交
225 226 227 228 229 230
    }

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

235 236 237 238 239 240
    if (!def->manufacturer && !def->product && !def->version &&
        !def->serial && !def->uuid && !def->sku && !def->family) {
        virSysinfoSystemDefFree(def);
        def = NULL;
    }

M
Michal Privoznik 已提交
241
    *sysdef = def;
242 243 244 245 246
    def = NULL;
    ret = 0;
 cleanup:
    virSysinfoSystemDefFree(def);
    return ret;
P
Prerna Saxena 已提交
247 248 249
}

static int
250
virSysinfoParsePPCProcessor(const char *base, virSysinfoDefPtr ret)
P
Prerna Saxena 已提交
251 252 253 254 255
{
    const char *cur;
    char *eol, *tmp_base;
    virSysinfoProcessorDefPtr processor;

256
    while ((tmp_base = strstr(base, "processor")) != NULL) {
P
Prerna Saxena 已提交
257 258 259 260
        base = tmp_base;
        eol = strchr(base, '\n');
        cur = strchr(base, ':') + 1;

261
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
262
            return -1;
P
Prerna Saxena 已提交
263 264 265
        processor = &ret->processor[ret->nprocessor - 1];

        virSkipSpaces(&cur);
266 267 268
        if (eol && VIR_STRNDUP(processor->processor_socket_destination,
                               cur, eol - cur) < 0)
            return -1;
269
        base = cur;
P
Prerna Saxena 已提交
270 271 272 273 274

        if ((cur = strstr(base, "cpu")) != NULL) {
            cur = strchr(cur, ':') + 1;
            eol = strchr(cur, '\n');
            virSkipSpaces(&cur);
275 276 277
            if (eol && VIR_STRNDUP(processor->processor_type,
                                   cur, eol - cur) < 0)
                return -1;
278
            base = cur;
P
Prerna Saxena 已提交
279 280 281 282 283 284
        }

        if ((cur = strstr(base, "revision")) != NULL) {
            cur = strchr(cur, ':') + 1;
            eol = strchr(cur, '\n');
            virSkipSpaces(&cur);
285 286 287
            if (eol && VIR_STRNDUP(processor->processor_version,
                                   cur, eol - cur) < 0)
                return -1;
288
            base = cur;
P
Prerna Saxena 已提交
289 290 291 292 293 294 295 296 297 298
        }

    }

    return 0;
}

/* virSysinfoRead for PowerPC
 * Gathers sysinfo data from /proc/cpuinfo */
virSysinfoDefPtr
299
virSysinfoReadPPC(void)
300
{
P
Prerna Saxena 已提交
301 302 303 304 305 306
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;

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

307
    if (virFileReadAll(CPUINFO, CPUINFO_FILE_LEN, &outbuf) < 0) {
308 309
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), CPUINFO);
310
        goto no_memory;
P
Prerna Saxena 已提交
311 312 313 314
    }

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

318
    if (virSysinfoParsePPCSystem(outbuf, &ret->system) < 0)
P
Prerna Saxena 已提交
319 320
        goto no_memory;

321
    VIR_FREE(outbuf);
P
Prerna Saxena 已提交
322 323
    return ret;

324
 no_memory:
P
Prerna Saxena 已提交
325
    VIR_FREE(outbuf);
326
    virSysinfoDefFree(ret);
P
Prerna Saxena 已提交
327 328 329
    return NULL;
}

330

331
static int
332
virSysinfoParseARMSystem(const char *base, virSysinfoSystemDefPtr *sysdef)
333
{
334
    int ret = -1;
335 336
    char *eol = NULL;
    const char *cur;
337
    virSysinfoSystemDefPtr def;
338 339 340 341

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

342 343 344
    if (VIR_ALLOC(def) < 0)
        return ret;

345 346 347 348 349
    base = cur;
    /* Account for format 'platform    : XXXX'*/
    cur = strchr(cur, ':') + 1;
    eol = strchr(cur, '\n');
    virSkipSpaces(&cur);
350 351
    if (eol && VIR_STRNDUP(def->family, cur, eol - cur) < 0)
        goto cleanup;
352 353 354 355 356

    if ((cur = strstr(base, "model")) != NULL) {
        cur = strchr(cur, ':') + 1;
        eol = strchr(cur, '\n');
        virSkipSpaces(&cur);
357 358
        if (eol && VIR_STRNDUP(def->serial, cur, eol - cur) < 0)
            goto cleanup;
359 360 361 362 363 364
    }

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

369 370 371 372 373 374
    if (!def->manufacturer && !def->product && !def->version &&
        !def->serial && !def->uuid && !def->sku && !def->family) {
        virSysinfoSystemDefFree(def);
        def = NULL;
    }

M
Michal Privoznik 已提交
375
    *sysdef = def;
376 377 378 379 380
    def = NULL;
    ret = 0;
 cleanup:
    virSysinfoSystemDefFree(def);
    return ret;
381 382 383
}

static int
384
virSysinfoParseARMProcessor(const char *base, virSysinfoDefPtr ret)
385 386 387 388 389 390
{
    const char *cur;
    char *eol, *tmp_base;
    virSysinfoProcessorDefPtr processor;
    char *processor_type = NULL;

M
Michal Privoznik 已提交
391 392
    if (!(tmp_base = strstr(base, "model name")) &&
        !(tmp_base = strstr(base, "Processor")))
393 394
        return 0;

M
Michal Privoznik 已提交
395 396
    eol = strchr(tmp_base, '\n');
    cur = strchr(tmp_base, ':') + 1;
397
    virSkipSpaces(&cur);
398 399
    if (eol && VIR_STRNDUP(processor_type, cur, eol - cur) < 0)
        goto error;
400 401 402 403 404 405

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

406
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
407
            goto error;
408 409 410 411
        processor = &ret->processor[ret->nprocessor - 1];

        virSkipSpaces(&cur);
        if (eol &&
412 413 414
            VIR_STRNDUP(processor->processor_socket_destination,
                        cur, eol - cur) < 0)
            goto error;
415

416
        if (VIR_STRDUP(processor->processor_type, processor_type) < 0)
417
            goto error;
418 419 420 421 422 423 424

        base = cur;
    }

    VIR_FREE(processor_type);
    return 0;

425
 error:
426 427 428 429 430 431 432
    VIR_FREE(processor_type);
    return -1;
}

/* virSysinfoRead for ARMv7
 * Gathers sysinfo data from /proc/cpuinfo */
virSysinfoDefPtr
433
virSysinfoReadARM(void)
434
{
435 436 437 438 439 440
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;

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

441
    if (virFileReadAll(CPUINFO, CPUINFO_FILE_LEN, &outbuf) < 0) {
442 443
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), CPUINFO);
444
        goto no_memory;
445 446 447 448
    }

    ret->nprocessor = 0;
    ret->processor = NULL;
449
    if (virSysinfoParseARMProcessor(outbuf, ret) < 0)
450 451
        goto no_memory;

452
    if (virSysinfoParseARMSystem(outbuf, &ret->system) < 0)
453 454
        goto no_memory;

455
    VIR_FREE(outbuf);
456 457
    return ret;

458
 no_memory:
459
    VIR_FREE(outbuf);
460
    virSysinfoDefFree(ret);
461 462 463 464
    return NULL;
}


465 466

VIR_WARNINGS_NO_WLOGICALOP_STRCHR
467

468
static char *
469 470
virSysinfoParseS390Delimited(const char *base, const char *name, char **value,
                             char delim1, char delim2)
471 472 473 474 475 476 477 478 479 480
{
    const char *start;
    char *end;

    if (delim1 != delim2 &&
        (start = strstr(base, name)) &&
        (start = strchr(start, delim1))) {
        start += 1;
        end = strchrnul(start, delim2);
        virSkipSpaces(&start);
481 482
        if (VIR_STRNDUP(*value, start, end - start) < 0)
            return NULL;
483 484
        virTrimSpaces(*value, NULL);
        return end;
485
    }
486 487
    return NULL;
}
488

489
static char *
490
virSysinfoParseS390Line(const char *base, const char *name, char **value)
491
{
492
    return virSysinfoParseS390Delimited(base, name, value, ':', '\n');
493 494 495
}

static int
496
virSysinfoParseS390System(const char *base, virSysinfoSystemDefPtr *sysdef)
497
{
498 499 500 501 502 503
    int ret = -1;
    virSysinfoSystemDefPtr def;

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

504
    if (!virSysinfoParseS390Line(base, "Manufacturer", &def->manufacturer))
505 506
        goto cleanup;

507
    if (!virSysinfoParseS390Line(base, "Type", &def->family))
508 509
        goto cleanup;

510
    if (!virSysinfoParseS390Line(base, "Sequence Code", &def->serial))
511 512 513 514 515 516 517 518
        goto cleanup;

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

M
Michal Privoznik 已提交
519
    *sysdef = def;
520 521 522 523 524
    def = NULL;
    ret = 0;
 cleanup:
    virSysinfoSystemDefFree(def);
    return ret;
525 526 527
}

static int
528
virSysinfoParseS390Processor(const char *base, virSysinfoDefPtr ret)
529
{
530 531 532
    char *tmp_base;
    char *manufacturer = NULL;
    char *procline = NULL;
533
    char *ncpu = NULL;
534
    int result = -1;
535 536
    virSysinfoProcessorDefPtr processor;

537
    if (!(tmp_base = virSysinfoParseS390Line(base, "vendor_id", &manufacturer)))
538
        goto error;
539

540 541 542
    /* Find processor N: line and gather the processor manufacturer,
       version, serial number, and family */
    while ((tmp_base = strstr(tmp_base, "processor "))
543 544
           && (tmp_base = virSysinfoParseS390Line(tmp_base, "processor ",
                                                  &procline))) {
545
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
546
            goto error;
547
        processor = &ret->processor[ret->nprocessor - 1];
548
        if (VIR_STRDUP(processor->processor_manufacturer, manufacturer) < 0)
549
            goto error;
550 551 552 553 554 555 556 557 558
        if (!virSysinfoParseS390Delimited(procline, "version",
                                          &processor->processor_version,
                                          '=', ',') ||
            !virSysinfoParseS390Delimited(procline, "identification",
                                          &processor->processor_serial_number,
                                          '=', ',') ||
            !virSysinfoParseS390Delimited(procline, "machine",
                                          &processor->processor_family,
                                          '=', '\n'))
559
            goto error;
560 561

        VIR_FREE(procline);
562
    }
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

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

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

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

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

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

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

        VIR_FREE(ncpu);
    }
588

589
 cleanup:
590 591 592
    result = 0;

 error:
593 594
    VIR_FREE(manufacturer);
    VIR_FREE(procline);
595
    VIR_FREE(ncpu);
596
    return result;
597 598 599 600 601
}

/* virSysinfoRead for s390x
 * Gathers sysinfo data from /proc/sysinfo and /proc/cpuinfo */
virSysinfoDefPtr
602
virSysinfoReadS390(void)
603
{
604 605 606 607 608 609 610
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;

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

    /* Gather info from /proc/cpuinfo */
611
    if (virFileReadAll(CPUINFO, CPUINFO_FILE_LEN, &outbuf) < 0) {
612 613
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), CPUINFO);
614
        goto no_memory;
615 616
    }

617
    if (virSysinfoParseS390Processor(outbuf, ret) < 0)
618 619 620 621 622 623
        goto no_memory;

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

    /* Gather info from /proc/sysinfo */
624
    if (virFileReadAll(SYSINFO, 8192, &outbuf) < 0) {
625 626
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), SYSINFO);
627
        goto no_memory;
628 629
    }

630
    if (virSysinfoParseS390System(outbuf, &ret->system) < 0)
631 632
        goto no_memory;

633
    VIR_FREE(outbuf);
634 635
    return ret;

636
 no_memory:
637
    virSysinfoDefFree(ret);
638 639 640 641
    VIR_FREE(outbuf);
    return NULL;
}

E
Eric Blake 已提交
642

643
static int
644
virSysinfoParseBIOS(const char *base, virSysinfoBIOSDefPtr *bios)
M
Minoru Usui 已提交
645
{
646
    int ret = -1;
647
    const char *cur, *eol = NULL;
648
    virSysinfoBIOSDefPtr def;
649

M
Minoru Usui 已提交
650
    if ((cur = strstr(base, "BIOS Information")) == NULL)
651
        return 0;
M
Minoru Usui 已提交
652

653 654 655
    if (VIR_ALLOC(def) < 0)
        return ret;

M
Minoru Usui 已提交
656
    base = cur;
657 658 659
    if ((cur = strstr(base, "Vendor: ")) != NULL) {
        cur += 8;
        eol = strchr(cur, '\n');
660 661
        if (eol && VIR_STRNDUP(def->vendor, cur, eol - cur) < 0)
            goto cleanup;
662 663 664 665
    }
    if ((cur = strstr(base, "Version: ")) != NULL) {
        cur += 9;
        eol = strchr(cur, '\n');
666 667
        if (eol && VIR_STRNDUP(def->version, cur, eol - cur) < 0)
            goto cleanup;
668 669 670 671
    }
    if ((cur = strstr(base, "Release Date: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
672 673
        if (eol && VIR_STRNDUP(def->date, cur, eol - cur) < 0)
            goto cleanup;
674 675 676 677
    }
    if ((cur = strstr(base, "BIOS Revision: ")) != NULL) {
        cur += 15;
        eol = strchr(cur, '\n');
678 679
        if (eol && VIR_STRNDUP(def->release, cur, eol - cur) < 0)
            goto cleanup;
680
    }
M
Minoru Usui 已提交
681

682 683 684 685 686 687 688 689 690 691 692 693
    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 已提交
694 695
}

696
static int
697
virSysinfoParseX86System(const char *base, virSysinfoSystemDefPtr *sysdef)
M
Minoru Usui 已提交
698
{
699
    int ret = -1;
700
    const char *cur, *eol = NULL;
701
    virSysinfoSystemDefPtr def;
M
Minoru Usui 已提交
702

M
Minoru Usui 已提交
703
    if ((cur = strstr(base, "System Information")) == NULL)
704
        return 0;
M
Minoru Usui 已提交
705

706 707 708
    if (VIR_ALLOC(def) < 0)
        return ret;

M
Minoru Usui 已提交
709
    base = cur;
710 711 712
    if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
713 714
        if (eol && VIR_STRNDUP(def->manufacturer, cur, eol - cur) < 0)
            goto cleanup;
715 716 717 718
    }
    if ((cur = strstr(base, "Product Name: ")) != NULL) {
        cur += 14;
        eol = strchr(cur, '\n');
719 720
        if (eol && VIR_STRNDUP(def->product, cur, eol - cur) < 0)
            goto cleanup;
721 722 723 724
    }
    if ((cur = strstr(base, "Version: ")) != NULL) {
        cur += 9;
        eol = strchr(cur, '\n');
725 726
        if (eol && VIR_STRNDUP(def->version, cur, eol - cur) < 0)
            goto cleanup;
727 728 729 730
    }
    if ((cur = strstr(base, "Serial Number: ")) != NULL) {
        cur += 15;
        eol = strchr(cur, '\n');
731 732
        if (eol && VIR_STRNDUP(def->serial, cur, eol - cur) < 0)
            goto cleanup;
733 734 735 736
    }
    if ((cur = strstr(base, "UUID: ")) != NULL) {
        cur += 6;
        eol = strchr(cur, '\n');
737 738
        if (eol && VIR_STRNDUP(def->uuid, cur, eol - cur) < 0)
            goto cleanup;
739 740 741 742
    }
    if ((cur = strstr(base, "SKU Number: ")) != NULL) {
        cur += 12;
        eol = strchr(cur, '\n');
743 744
        if (eol && VIR_STRNDUP(def->sku, cur, eol - cur) < 0)
            goto cleanup;
745
    }
E
Eric Blake 已提交
746 747 748
    if ((cur = strstr(base, "Family: ")) != NULL) {
        cur += 8;
        eol = strchr(cur, '\n');
749 750
        if (eol && VIR_STRNDUP(def->family, cur, eol - cur) < 0)
            goto cleanup;
E
Eric Blake 已提交
751
    }
752

753 754 755 756 757 758
    if (!def->manufacturer && !def->product && !def->version &&
        !def->serial && !def->uuid && !def->sku && !def->family) {
        virSysinfoSystemDefFree(def);
        def = NULL;
    }

M
Michal Privoznik 已提交
759
    *sysdef = def;
760 761 762 763 764
    def = NULL;
    ret = 0;
 cleanup:
    virSysinfoSystemDefFree(def);
    return ret;
M
Minoru Usui 已提交
765 766
}

767
static int
768 769 770
virSysinfoParseX86BaseBoard(const char *base,
                            virSysinfoBaseBoardDefPtr *baseBoard,
                            size_t *nbaseBoard)
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
{
    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;
}

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906

static int
virSysinfoParseX86Chassis(const char *base,
                          virSysinfoChassisDefPtr *chassisdef)
{
    int ret = -1;
    const char *cur, *eol = NULL;
    virSysinfoChassisDefPtr def;

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

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

    base = cur;
    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, "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->sku, cur, eol - cur) < 0)
            goto cleanup;
    }
    if ((cur = strstr(base, "SKU Number: ")) != NULL) {
        cur += 12;
        eol = strchr(cur, '\n');
        if (eol && VIR_STRNDUP(def->sku, cur, eol - cur) < 0)
            goto cleanup;
    }

    if (!def->manufacturer && !def->version &&
        !def->serial && !def->asset && !def->sku) {
        virSysinfoChassisDefFree(def);
        def = NULL;
    }

    *chassisdef = def;
    def = NULL;
    ret = 0;
 cleanup:
    virSysinfoChassisDefFree(def);
    return ret;
}


907
static int
908
virSysinfoParseX86Processor(const char *base, virSysinfoDefPtr ret)
909
{
910 911
    const char *cur, *tmp_base;
    char *eol;
912
    virSysinfoProcessorDefPtr processor;
913

914
    while ((tmp_base = strstr(base, "Processor Information")) != NULL) {
915
        base = tmp_base;
E
Eric Blake 已提交
916
        eol = NULL;
917

918 919
        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0)
            return -1;
920 921 922 923 924
        processor = &ret->processor[ret->nprocessor - 1];

        if ((cur = strstr(base, "Socket Designation: ")) != NULL) {
            cur += 20;
            eol = strchr(cur, '\n');
925
            virSkipSpacesBackwards(cur, &eol);
926 927 928
            if (eol && VIR_STRNDUP(processor->processor_socket_destination,
                                   cur, eol - cur) < 0)
                return -1;
929 930 931 932
        }
        if ((cur = strstr(base, "Type: ")) != NULL) {
            cur += 6;
            eol = strchr(cur, '\n');
933
            virSkipSpacesBackwards(cur, &eol);
934 935
            if (eol && VIR_STRNDUP(processor->processor_type, cur, eol - cur) < 0)
                return -1;
936 937 938 939
        }
        if ((cur = strstr(base, "Family: ")) != NULL) {
            cur += 8;
            eol = strchr(cur, '\n');
940
            virSkipSpacesBackwards(cur, &eol);
941 942
            if (eol && VIR_STRNDUP(processor->processor_family, cur, eol - cur) < 0)
                return -1;
943 944 945 946
        }
        if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
            cur += 14;
            eol = strchr(cur, '\n');
947
            virSkipSpacesBackwards(cur, &eol);
948 949 950
            if (eol && VIR_STRNDUP(processor->processor_manufacturer,
                                   cur, eol - cur) < 0)
                return -1;
951 952 953 954
        }
        if ((cur = strstr(base, "Signature: ")) != NULL) {
            cur += 11;
            eol = strchr(cur, '\n');
955
            virSkipSpacesBackwards(cur, &eol);
956 957 958
            if (eol && VIR_STRNDUP(processor->processor_signature,
                                   cur, eol - cur) < 0)
                return -1;
959 960 961 962
        }
        if ((cur = strstr(base, "Version: ")) != NULL) {
            cur += 9;
            eol = strchr(cur, '\n');
963
            virSkipSpacesBackwards(cur, &eol);
964 965 966
            if (eol && VIR_STRNDUP(processor->processor_version,
                                   cur, eol - cur) < 0)
                return -1;
967 968 969 970
        }
        if ((cur = strstr(base, "External Clock: ")) != NULL) {
            cur += 16;
            eol = strchr(cur, '\n');
971
            virSkipSpacesBackwards(cur, &eol);
972 973 974
            if (eol && VIR_STRNDUP(processor->processor_external_clock,
                                   cur, eol - cur) < 0)
                return -1;
975 976 977 978
        }
        if ((cur = strstr(base, "Max Speed: ")) != NULL) {
            cur += 11;
            eol = strchr(cur, '\n');
979
            virSkipSpacesBackwards(cur, &eol);
980 981 982
            if (eol && VIR_STRNDUP(processor->processor_max_speed,
                                   cur, eol - cur) < 0)
                return -1;
983 984 985 986
        }
        if ((cur = strstr(base, "Status: ")) != NULL) {
            cur += 8;
            eol = strchr(cur, '\n');
987
            virSkipSpacesBackwards(cur, &eol);
988 989
            if (eol && VIR_STRNDUP(processor->processor_status, cur, eol - cur) < 0)
                return -1;
990 991 992 993
        }
        if ((cur = strstr(base, "Serial Number: ")) != NULL) {
            cur += 15;
            eol = strchr(cur, '\n');
994
            virSkipSpacesBackwards(cur, &eol);
995 996 997
            if (eol && VIR_STRNDUP(processor->processor_serial_number,
                                   cur, eol - cur) < 0)
                return -1;
998 999 1000 1001
        }
        if ((cur = strstr(base, "Part Number: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
1002
            virSkipSpacesBackwards(cur, &eol);
1003 1004 1005
            if (eol && VIR_STRNDUP(processor->processor_part_number,
                                   cur, eol - cur) < 0)
                return -1;
1006 1007
        }

M
Minoru Usui 已提交
1008
        base += strlen("Processor Information");
1009 1010
    }

1011
    return 0;
1012 1013
}

1014
static int
1015
virSysinfoParseX86Memory(const char *base, virSysinfoDefPtr ret)
1016
{
1017 1018
    const char *cur, *tmp_base;
    char *eol;
1019
    virSysinfoMemoryDefPtr memory;
1020 1021 1022

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

1025 1026
        if (VIR_EXPAND_N(ret->memory, ret->nmemory, 1) < 0)
            return -1;
1027 1028 1029 1030 1031 1032 1033 1034
        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;

1035
            virSkipSpacesBackwards(cur, &eol);
1036 1037
            if (eol && VIR_STRNDUP(memory->memory_size, cur, eol - cur) < 0)
                return -1;
1038 1039 1040 1041
        }
        if ((cur = strstr(base, "Form Factor: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
1042
            virSkipSpacesBackwards(cur, &eol);
1043 1044 1045
            if (eol && VIR_STRNDUP(memory->memory_form_factor,
                                   cur, eol - cur) < 0)
                return -1;
1046 1047 1048 1049
        }
        if ((cur = strstr(base, "Locator: ")) != NULL) {
            cur += 9;
            eol = strchr(cur, '\n');
1050
            virSkipSpacesBackwards(cur, &eol);
E
Eric Blake 已提交
1051
            if (eol && VIR_STRNDUP(memory->memory_locator, cur, eol - cur) < 0)
1052
                return -1;
1053 1054 1055 1056
        }
        if ((cur = strstr(base, "Bank Locator: ")) != NULL) {
            cur += 14;
            eol = strchr(cur, '\n');
1057
            virSkipSpacesBackwards(cur, &eol);
1058 1059 1060
            if (eol && VIR_STRNDUP(memory->memory_bank_locator,
                                   cur, eol - cur) < 0)
                return -1;
1061 1062 1063 1064
        }
        if ((cur = strstr(base, "Type: ")) != NULL) {
            cur += 6;
            eol = strchr(cur, '\n');
1065
            virSkipSpacesBackwards(cur, &eol);
1066 1067
            if (eol && VIR_STRNDUP(memory->memory_type, cur, eol - cur) < 0)
                return -1;
1068 1069 1070 1071
        }
        if ((cur = strstr(base, "Type Detail: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
1072
            virSkipSpacesBackwards(cur, &eol);
1073 1074
            if (eol && VIR_STRNDUP(memory->memory_type_detail, cur, eol - cur) < 0)
                return -1;
1075 1076 1077 1078
        }
        if ((cur = strstr(base, "Speed: ")) != NULL) {
            cur += 7;
            eol = strchr(cur, '\n');
1079
            virSkipSpacesBackwards(cur, &eol);
1080 1081
            if (eol && VIR_STRNDUP(memory->memory_speed, cur, eol - cur) < 0)
                return -1;
1082 1083 1084 1085
        }
        if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
            cur += 14;
            eol = strchr(cur, '\n');
1086
            virSkipSpacesBackwards(cur, &eol);
1087 1088
            if (eol && VIR_STRNDUP(memory->memory_manufacturer, cur, eol - cur) < 0)
                return -1;
1089 1090 1091 1092
        }
        if ((cur = strstr(base, "Serial Number: ")) != NULL) {
            cur += 15;
            eol = strchr(cur, '\n');
1093
            virSkipSpacesBackwards(cur, &eol);
1094 1095 1096
            if (eol && VIR_STRNDUP(memory->memory_serial_number,
                                   cur, eol - cur) < 0)
                return -1;
1097 1098 1099 1100
        }
        if ((cur = strstr(base, "Part Number: ")) != NULL) {
            cur += 13;
            eol = strchr(cur, '\n');
1101
            virSkipSpacesBackwards(cur, &eol);
1102 1103
            if (eol && VIR_STRNDUP(memory->memory_part_number, cur, eol - cur) < 0)
                return -1;
1104 1105 1106
        }

    next:
M
Minoru Usui 已提交
1107
        base += strlen("Memory Device");
1108 1109
    }

1110
    return 0;
1111 1112
}

M
Minoru Usui 已提交
1113
virSysinfoDefPtr
1114
virSysinfoReadX86(void)
1115
{
1116
    char *path;
M
Minoru Usui 已提交
1117 1118 1119 1120 1121 1122
    virSysinfoDefPtr ret = NULL;
    char *outbuf = NULL;
    virCommandPtr cmd;

    path = virFindFileInPath(SYSINFO_SMBIOS_DECODER);
    if (path == NULL) {
1123 1124 1125
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to find path for %s binary"),
                       SYSINFO_SMBIOS_DECODER);
M
Minoru Usui 已提交
1126 1127 1128
        return NULL;
    }

1129
    cmd = virCommandNewArgList(path, "-q", "-t", "0,1,2,3,4,17", NULL);
M
Minoru Usui 已提交
1130 1131
    VIR_FREE(path);
    virCommandSetOutputBuffer(cmd, &outbuf);
1132
    if (virCommandRun(cmd, NULL) < 0)
M
Minoru Usui 已提交
1133 1134 1135
        goto cleanup;

    if (VIR_ALLOC(ret) < 0)
1136
        goto error;
M
Minoru Usui 已提交
1137 1138 1139

    ret->type = VIR_SYSINFO_SMBIOS;

1140
    if (virSysinfoParseBIOS(outbuf, &ret->bios) < 0)
1141
        goto error;
M
Minoru Usui 已提交
1142

1143
    if (virSysinfoParseX86System(outbuf, &ret->system) < 0)
1144
        goto error;
M
Minoru Usui 已提交
1145

1146
    if (virSysinfoParseX86BaseBoard(outbuf, &ret->baseBoard, &ret->nbaseBoard) < 0)
1147 1148
        goto error;

1149 1150 1151
    if (virSysinfoParseX86Chassis(outbuf, &ret->chassis) < 0)
        goto error;

1152 1153
    ret->nprocessor = 0;
    ret->processor = NULL;
1154
    if (virSysinfoParseX86Processor(outbuf, ret) < 0)
1155
        goto error;
1156

1157 1158
    ret->nmemory = 0;
    ret->memory = NULL;
1159
    if (virSysinfoParseX86Memory(outbuf, ret) < 0)
1160
        goto error;
1161

1162
 cleanup:
1163
    VIR_FREE(outbuf);
E
Eric Blake 已提交
1164
    virCommandFree(cmd);
1165

E
Eric Blake 已提交
1166
    return ret;
1167

1168
 error:
1169 1170 1171 1172
    virSysinfoDefFree(ret);
    ret = NULL;
    goto cleanup;
}
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192


/**
 * 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__) || \
1193
      defined(__i386__) || \
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
      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();
1207
#endif /* !WIN32 && x86 */
1208 1209
}

E
Eric Blake 已提交
1210

M
Minoru Usui 已提交
1211
static void
1212
virSysinfoBIOSFormat(virBufferPtr buf, virSysinfoBIOSDefPtr def)
E
Eric Blake 已提交
1213
{
1214
    if (!def)
1215
        return;
M
Minoru Usui 已提交
1216

1217 1218 1219
    virBufferAddLit(buf, "<bios>\n");
    virBufferAdjustIndent(buf, 2);
    virBufferEscapeString(buf, "<entry name='vendor'>%s</entry>\n",
1220
                          def->vendor);
1221
    virBufferEscapeString(buf, "<entry name='version'>%s</entry>\n",
1222
                          def->version);
1223
    virBufferEscapeString(buf, "<entry name='date'>%s</entry>\n",
1224
                          def->date);
1225
    virBufferEscapeString(buf, "<entry name='release'>%s</entry>\n",
1226
                          def->release);
1227 1228
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</bios>\n");
M
Minoru Usui 已提交
1229 1230 1231
}

static void
1232
virSysinfoSystemFormat(virBufferPtr buf, virSysinfoSystemDefPtr def)
M
Minoru Usui 已提交
1233
{
1234
    if (!def)
1235
        return;
M
Minoru Usui 已提交
1236

1237 1238 1239
    virBufferAddLit(buf, "<system>\n");
    virBufferAdjustIndent(buf, 2);
    virBufferEscapeString(buf, "<entry name='manufacturer'>%s</entry>\n",
1240
                          def->manufacturer);
1241
    virBufferEscapeString(buf, "<entry name='product'>%s</entry>\n",
1242
                          def->product);
1243
    virBufferEscapeString(buf, "<entry name='version'>%s</entry>\n",
1244
                          def->version);
1245
    virBufferEscapeString(buf, "<entry name='serial'>%s</entry>\n",
1246
                          def->serial);
1247
    virBufferEscapeString(buf, "<entry name='uuid'>%s</entry>\n",
1248
                          def->uuid);
1249
    virBufferEscapeString(buf, "<entry name='sku'>%s</entry>\n",
1250
                          def->sku);
1251
    virBufferEscapeString(buf, "<entry name='family'>%s</entry>\n",
1252
                          def->family);
1253 1254
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</system>\n");
M
Minoru Usui 已提交
1255 1256
}

1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
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");
    }
}

1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311

static void
virSysinfoChassisFormat(virBufferPtr buf,
                        virSysinfoChassisDefPtr def)
{
    if (!def)
        return;

    virBufferAddLit(buf, "<chassis>\n");
    virBufferAdjustIndent(buf, 2);
    virBufferEscapeString(buf, "<entry name='manufacturer'>%s</entry>\n",
                          def->manufacturer);
    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='sku'>%s</entry>\n",
                          def->sku);
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</chassis>\n");
}


1312
static void
1313
virSysinfoProcessorFormat(virBufferPtr buf, virSysinfoDefPtr def)
1314
{
1315
    size_t i;
1316
    virSysinfoProcessorDefPtr processor;
1317 1318 1319 1320

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

1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
        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;

1334 1335
        virBufferAddLit(buf, "<processor>\n");
        virBufferAdjustIndent(buf, 2);
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
        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);
1359 1360
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</processor>\n");
1361 1362 1363
    }
}

1364
static void
1365
virSysinfoMemoryFormat(virBufferPtr buf, virSysinfoDefPtr def)
1366
{
1367
    size_t i;
1368
    virSysinfoMemoryDefPtr memory;
1369 1370 1371 1372

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

1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
        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;

1385 1386 1387
        virBufferAddLit(buf, "<memory_device>\n");
        virBufferAdjustIndent(buf, 2);
        virBufferEscapeString(buf, "<entry name='size'>%s</entry>\n",
1388 1389
                              memory->memory_size);
        virBufferEscapeString(buf,
1390
                              "<entry name='form_factor'>%s</entry>\n",
1391
                              memory->memory_form_factor);
1392
        virBufferEscapeString(buf, "<entry name='locator'>%s</entry>\n",
1393 1394
                              memory->memory_locator);
        virBufferEscapeString(buf,
1395
                              "<entry name='bank_locator'>%s</entry>\n",
1396
                              memory->memory_bank_locator);
1397
        virBufferEscapeString(buf, "<entry name='type'>%s</entry>\n",
1398 1399
                              memory->memory_type);
        virBufferEscapeString(buf,
1400
                              "<entry name='type_detail'>%s</entry>\n",
1401
                              memory->memory_type_detail);
1402
        virBufferEscapeString(buf, "<entry name='speed'>%s</entry>\n",
1403 1404
                              memory->memory_speed);
        virBufferEscapeString(buf,
1405
                              "<entry name='manufacturer'>%s</entry>\n",
1406 1407
                              memory->memory_manufacturer);
        virBufferEscapeString(buf,
1408
                              "<entry name='serial_number'>%s</entry>\n",
1409 1410
                              memory->memory_serial_number);
        virBufferEscapeString(buf,
1411
                              "<entry name='part_number'>%s</entry>\n",
1412
                              memory->memory_part_number);
1413 1414
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</memory_device>\n");
1415 1416 1417
    }
}

1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
static void
virSysinfoOEMStringsFormat(virBufferPtr buf, virSysinfoOEMStringsDefPtr def)
{
    size_t i;

    if (!def)
        return;

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

M
Minoru Usui 已提交
1436 1437
/**
 * virSysinfoFormat:
1438
 * @buf: buffer to append output to (may use auto-indentation)
M
Minoru Usui 已提交
1439 1440
 * @def: structure to convert to xml string
 *
1441
 * Returns 0 on success, -1 on failure after generating an error message.
M
Minoru Usui 已提交
1442
 */
1443 1444
int
virSysinfoFormat(virBufferPtr buf, virSysinfoDefPtr def)
M
Minoru Usui 已提交
1445
{
1446
    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
M
Minoru Usui 已提交
1447
    const char *type = virSysinfoTypeToString(def->type);
1448 1449
    int indent = virBufferGetIndent(buf, false);
    int ret = -1;
M
Minoru Usui 已提交
1450 1451

    if (!type) {
1452 1453 1454
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected sysinfo type model %d"),
                       def->type);
1455
        virBufferFreeAndReset(buf);
1456
        goto cleanup;
E
Eric Blake 已提交
1457 1458
    }

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

1461
    virSysinfoBIOSFormat(&childrenBuf, def->bios);
1462
    virSysinfoSystemFormat(&childrenBuf, def->system);
1463
    virSysinfoBaseBoardFormat(&childrenBuf, def->baseBoard, def->nbaseBoard);
1464
    virSysinfoChassisFormat(&childrenBuf, def->chassis);
1465 1466
    virSysinfoProcessorFormat(&childrenBuf, def);
    virSysinfoMemoryFormat(&childrenBuf, def);
1467
    virSysinfoOEMStringsFormat(&childrenBuf, def->oemStrings);
M
Minoru Usui 已提交
1468

1469 1470 1471 1472 1473 1474 1475 1476
    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 已提交
1477

1478
    if (virBufferCheckError(buf) < 0)
1479
        goto cleanup;
1480

1481 1482 1483 1484
    ret = 0;
 cleanup:
    virBufferFreeAndReset(&childrenBuf);
    return ret;
E
Eric Blake 已提交
1485 1486
}

1487 1488 1489 1490
#define CHECK_FIELD(name, desc) \
    do { \
        if (STRNEQ_NULLABLE(src->name, dst->name)) { \
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, \
1491 1492
                           _("Target sysinfo %s %s does not match source %s"), \
                           desc, NULLSTR(dst->name), NULLSTR(src->name)); \
1493 1494
            goto cleanup; \
        } \
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
    } 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;
}

1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
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;
}

1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
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;
}

1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604

static bool
virSysinfoChassisIsEqual(virSysinfoChassisDefPtr src,
                           virSysinfoChassisDefPtr dst)
{
    bool identical = false;

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

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

    CHECK_FIELD(manufacturer, "chassis vendor");
    CHECK_FIELD(version, "chassis version");
    CHECK_FIELD(serial, "chassis serial");
    CHECK_FIELD(asset, "chassis asset");
    CHECK_FIELD(sku, "chassis sku");

    identical = true;
 cleanup:
    return identical;
}


1605 1606
#undef CHECK_FIELD

1607 1608 1609 1610
bool virSysinfoIsEqual(virSysinfoDefPtr src,
                       virSysinfoDefPtr dst)
{
    bool identical = false;
1611
    size_t i;
1612 1613 1614 1615 1616

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

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

    if (src->type != dst->type) {
1623 1624 1625 1626
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target sysinfo %s does not match source %s"),
                       virSysinfoTypeToString(dst->type),
                       virSysinfoTypeToString(src->type));
1627 1628 1629
        goto cleanup;
    }

1630 1631
    if (!virSysinfoBIOSIsEqual(src->bios, dst->bios))
        goto cleanup;
1632

1633 1634
    if (!virSysinfoSystemIsEqual(src->system, dst->system))
        goto cleanup;
1635

1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
    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;

1648 1649 1650
    if (!virSysinfoChassisIsEqual(src->chassis, dst->chassis))
        goto cleanup;

1651 1652
    identical = true;

1653
 cleanup:
1654 1655
    return identical;
}