xml.c 53.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * xml.c: XML based interfaces for the libvir library
 *
 * Copyright (C) 2005 Red Hat, Inc.
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

11
#include "libvirt/libvirt.h"
12 13 14 15 16 17

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <xs.h>
18 19 20
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
21
#include <math.h> /* for isnan() */
22 23
#include "internal.h"
#include "hash.h"
D
Daniel Veillard 已提交
24
#include "sexpr.h"
25
#include "xml.h"
26
#include "xs_internal.h" /* for xenStoreDomainGetNetworkID */
27

28
static void
29 30
virXMLError(virErrorNumber error, const char *info, int value)
{
31
    const char *errmsg;
32

33 34 35 36 37
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
    __virRaiseError(NULL, NULL, VIR_FROM_XML, error, VIR_ERR_ERROR,
38
                    errmsg, info, NULL, value, 0, errmsg, info, value);
39 40
}

41 42 43 44 45 46 47 48 49 50
/**
 * virBufferGrow:
 * @buf:  the buffer
 * @len:  the minimum free size to allocate
 *
 * Grow the available space of an XML buffer.
 *
 * Returns the new available space or -1 in case of error
 */
static int
51 52
virBufferGrow(virBufferPtr buf, unsigned int len)
{
53 54 55
    int size;
    char *newbuf;

56 57 58 59
    if (buf == NULL)
        return (-1);
    if (len + buf->use < buf->size)
        return (0);
60 61 62 63 64

    size = buf->use + len + 1000;

    newbuf = (char *) realloc(buf->content, size);
    if (newbuf == NULL) {
65
        virXMLError(VIR_ERR_NO_MEMORY, _("growing buffer"), size);
66
        return (-1);
67 68 69
    }
    buf->content = newbuf;
    buf->size = size;
70
    return (buf->size - buf->use);
71 72 73 74 75 76 77 78 79 80 81 82 83
}

/**
 * virBufferAdd:
 * @buf:  the buffer to dump
 * @str:  the string
 * @len:  the number of bytes to add
 *
 * Add a string range to an XML buffer. if len == -1, the length of
 * str is recomputed to the full string.
 *
 * Returns 0 successful, -1 in case of internal or API error.
 */
D
Daniel Veillard 已提交
84
int
85 86
virBufferAdd(virBufferPtr buf, const char *str, int len)
{
87 88 89
    unsigned int needSize;

    if ((str == NULL) || (buf == NULL)) {
90
        return -1;
91
    }
92 93
    if (len == 0)
        return 0;
94 95 96 97 98

    if (len < 0)
        len = strlen(str);

    needSize = buf->use + len + 2;
99 100 101
    if (needSize > buf->size) {
        if (!virBufferGrow(buf, needSize)) {
            return (-1);
102 103
        }
    }
K
Karel Zak 已提交
104
    /* XXX: memmove() is 2x slower than memcpy(), do we really need it? */
105 106 107
    memmove(&buf->content[buf->use], str, len);
    buf->use += len;
    buf->content[buf->use] = 0;
108
    return (0);
109 110
}

K
Karel Zak 已提交
111 112 113 114 115 116
virBufferPtr
virBufferNew(unsigned int size)
{
    virBufferPtr buf;

    if (!(buf = malloc(sizeof(*buf)))) {
117
        virXMLError(VIR_ERR_NO_MEMORY, _("allocate new buffer"), sizeof(*buf));
K
Karel Zak 已提交
118 119 120
        return NULL;
    }
    if (size && (buf->content = malloc(size))==NULL) {
121
        virXMLError(VIR_ERR_NO_MEMORY, _("allocate buffer content"), size);
K
Karel Zak 已提交
122 123 124 125 126 127 128 129
        free(buf);
        return NULL;
    }
    buf->size = size;
    buf->use = 0;

    return buf;
}
130

K
Karel Zak 已提交
131 132 133 134
void
virBufferFree(virBufferPtr buf)
{
    if (buf) {
K
Karel Zak 已提交
135
        if (buf->content)
136 137
            free(buf->content);
        free(buf);
K
Karel Zak 已提交
138 139 140
    }
}

141 142 143 144 145 146 147 148 149 150
/**
 * virBufferVSprintf:
 * @buf:  the buffer to dump
 * @format:  the format
 * @argptr:  the variable list of arguments
 *
 * Do a formatted print to an XML buffer.
 *
 * Returns 0 successful, -1 in case of internal or API error.
 */
D
Daniel Veillard 已提交
151
int
152 153
virBufferVSprintf(virBufferPtr buf, const char *format, ...)
{
154 155 156 157
    int size, count;
    va_list locarg, argptr;

    if ((format == NULL) || (buf == NULL)) {
158
        return (-1);
159 160 161 162 163 164
    }
    size = buf->size - buf->use - 1;
    va_start(argptr, format);
    va_copy(locarg, argptr);
    while (((count = vsnprintf(&buf->content[buf->use], size, format,
                               locarg)) < 0) || (count >= size - 1)) {
165 166 167 168 169 170 171
        buf->content[buf->use] = 0;
        va_end(locarg);
        if (virBufferGrow(buf, 1000) < 0) {
            return (-1);
        }
        size = buf->size - buf->use - 1;
        va_copy(locarg, argptr);
172 173 174 175
    }
    va_end(locarg);
    buf->use += count;
    buf->content[buf->use] = 0;
176
    return (0);
177 178
}

K
Karel Zak 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192
/**
 * virBufferStrcat:
 * @buf:  the buffer to dump
 * @argptr:  the variable list of strings, the last argument must be NULL
 *
 * Concatenate strings to an XML buffer.
 *
 * Returns 0 successful, -1 in case of internal or API error.
 */
int
virBufferStrcat(virBufferPtr buf, ...)
{
    va_list ap;
    char *str;
193

K
Karel Zak 已提交
194
    va_start(ap, buf);
195

K
Karel Zak 已提交
196 197 198 199 200
    while ((str = va_arg(ap, char *)) != NULL) {
        unsigned int len = strlen(str);
        unsigned int needSize = buf->use + len + 2;

        if (needSize > buf->size) {
201 202 203
            if (!virBufferGrow(buf, needSize))
                return -1;
        }
K
Karel Zak 已提交
204 205 206 207 208 209 210 211
        memcpy(&buf->content[buf->use], str, len);
        buf->use += len;
        buf->content[buf->use] = 0;
    }
    va_end(ap);
    return 0;
}

D
Daniel Veillard 已提交
212
#if 0
213

D
Daniel Veillard 已提交
214 215 216 217 218
/*
 * This block of function are now implemented by a xend poll in
 * xend_internal.c instead of querying the Xen store, code is kept
 * for reference of in case Xend may not be available in the future ...
 */
219

220 221 222 223 224 225 226 227 228 229 230 231
/**
 * virDomainGetXMLDevice:
 * @domain: a domain object
 * @sub: the xenstore subsection 'vbd', 'vif', ...
 * @dev: the xenstrore internal device number
 * @name: the value's name
 *
 * Extract one information the device used by the domain from xensttore
 *
 * Returns the new string or NULL in case of error
 */
static char *
232 233 234
virDomainGetXMLDeviceInfo(virDomainPtr domain, const char *sub,
                          long dev, const char *name)
{
235 236 237 238
    char s[256];
    unsigned int len = 0;

    snprintf(s, 255, "/local/domain/0/backend/%s/%d/%ld/%s",
239
             sub, domain->id, dev, name);
240 241
    s[255] = 0;

242
    return xs_read(domain->conn->xshandle, 0, &s[0], &len);
243 244 245 246 247 248 249 250
}

/**
 * virDomainGetXMLDevice:
 * @domain: a domain object
 * @buf: the output buffer object
 * @dev: the xenstrore internal device number
 *
251
 * Extract and dump in the buffer information on the device used by the domain
252 253 254 255
 *
 * Returns 0 in case of success, -1 in case of failure
 */
static int
256 257
virDomainGetXMLDevice(virDomainPtr domain, virBufferPtr buf, long dev)
{
258 259 260 261
    char *type, *val;

    type = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "type");
    if (type == NULL)
262
        return (-1);
263
    if (!strcmp(type, "file")) {
264 265 266 267 268 269 270 271
        virBufferVSprintf(buf, "    <disk type='file'>\n");
        val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "params");
        if (val != NULL) {
            virBufferVSprintf(buf, "      <source file='%s'/>\n", val);
            free(val);
        }
        val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "dev");
        if (val != NULL) {
272 273 274 275
            char *tmp = val;
            if (!strncmp(tmp, "ioemu:", 6))
                tmp += 6;
            virBufferVSprintf(buf, "      <target dev='%s'/>\n", tmp);
276 277 278 279 280 281 282 283
            free(val);
        }
        val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "read-only");
        if (val != NULL) {
            virBufferVSprintf(buf, "      <readonly/>\n", val);
            free(val);
        }
        virBufferAdd(buf, "    </disk>\n", 12);
284
    } else if (!strcmp(type, "phy")) {
285 286 287 288 289 290 291 292
        virBufferVSprintf(buf, "    <disk type='device'>\n");
        val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "params");
        if (val != NULL) {
            virBufferVSprintf(buf, "      <source device='%s'/>\n", val);
            free(val);
        }
        val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "dev");
        if (val != NULL) {
293 294 295 296
            char *tmp = val;
            if (!strncmp(tmp, "ioemu:", 6))
                tmp += 6;
            virBufferVSprintf(buf, "      <target dev='%s'/>\n", tmp);
297 298 299 300 301 302 303 304
            free(val);
        }
        val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "read-only");
        if (val != NULL) {
            virBufferVSprintf(buf, "      <readonly/>\n", val);
            free(val);
        }
        virBufferAdd(buf, "    </disk>\n", 12);
305
    } else {
306 307
        TODO fprintf(stderr, "Don't know how to handle device type %s\n",
                     type);
308 309 310
    }
    free(type);

311
    return (0);
312 313 314 315 316 317 318 319 320 321 322 323
}

/**
 * virDomainGetXMLDevices:
 * @domain: a domain object
 * @buf: the output buffer object
 *
 * Extract the devices used by the domain and dumps then in the buffer
 *
 * Returns 0 in case of success, -1 in case of failure
 */
static int
324 325
virDomainGetXMLDevices(virDomainPtr domain, virBufferPtr buf)
{
326 327 328 329 330 331 332
    int ret = -1;
    unsigned int num, i;
    long id;
    char **list = NULL, *endptr;
    char backend[200];
    virConnectPtr conn;

K
Karel Zak 已提交
333
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
334 335
        return (-1);

336 337
    conn = domain->conn;

338
    snprintf(backend, 199, "/local/domain/0/backend/vbd/%d",
339 340
             virDomainGetID(domain));
    backend[199] = 0;
341
    list = xs_directory(conn->xshandle, 0, backend, &num);
342 343 344 345
    ret = 0;
    if (list == NULL)
        goto done;

346
    for (i = 0; i < num; i++) {
347
        id = strtol(list[i], &endptr, 10);
348 349 350 351 352
        if ((endptr == list[i]) || (*endptr != 0)) {
            ret = -1;
            goto done;
        }
        virDomainGetXMLDevice(domain, buf, id);
353 354
    }

355
 done:
356 357 358
    if (list != NULL)
        free(list);

359
    return (ret);
360 361 362 363 364 365 366 367
}

/**
 * virDomainGetXMLInterface:
 * @domain: a domain object
 * @buf: the output buffer object
 * @dev: the xenstrore internal device number
 *
368
 * Extract and dump in the buffer information on the interface used by
369 370 371 372 373
 * the domain
 *
 * Returns 0 in case of success, -1 in case of failure
 */
static int
374 375
virDomainGetXMLInterface(virDomainPtr domain, virBufferPtr buf, long dev)
{
376 377 378 379
    char *type, *val;

    type = virDomainGetXMLDeviceInfo(domain, "vif", dev, "bridge");
    if (type == NULL) {
380 381 382 383 384 385 386 387 388 389 390 391
        virBufferVSprintf(buf, "    <interface type='default'>\n");
        val = virDomainGetXMLDeviceInfo(domain, "vif", dev, "mac");
        if (val != NULL) {
            virBufferVSprintf(buf, "      <mac address='%s'/>\n", val);
            free(val);
        }
        val = virDomainGetXMLDeviceInfo(domain, "vif", dev, "script");
        if (val != NULL) {
            virBufferVSprintf(buf, "      <script path='%s'/>\n", val);
            free(val);
        }
        virBufferAdd(buf, "    </interface>\n", 17);
392
    } else {
393 394 395 396 397 398 399 400 401 402 403 404 405
        virBufferVSprintf(buf, "    <interface type='bridge'>\n");
        virBufferVSprintf(buf, "      <source bridge='%s'/>\n", type);
        val = virDomainGetXMLDeviceInfo(domain, "vif", dev, "mac");
        if (val != NULL) {
            virBufferVSprintf(buf, "      <mac address='%s'/>\n", val);
            free(val);
        }
        val = virDomainGetXMLDeviceInfo(domain, "vif", dev, "script");
        if (val != NULL) {
            virBufferVSprintf(buf, "      <script path='%s'/>\n", val);
            free(val);
        }
        virBufferAdd(buf, "    </interface>\n", 17);
406 407 408
    }
    free(type);

409
    return (0);
410 411 412 413 414 415 416 417 418 419 420 421
}

/**
 * virDomainGetXMLInterfaces:
 * @domain: a domain object
 * @buf: the output buffer object
 *
 * Extract the interfaces used by the domain and dumps then in the buffer
 *
 * Returns 0 in case of success, -1 in case of failure
 */
static int
422 423
virDomainGetXMLInterfaces(virDomainPtr domain, virBufferPtr buf)
{
424 425 426 427 428 429 430
    int ret = -1;
    unsigned int num, i;
    long id;
    char **list = NULL, *endptr;
    char backend[200];
    virConnectPtr conn;

K
Karel Zak 已提交
431
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
432 433
        return (-1);

434 435
    conn = domain->conn;

436
    snprintf(backend, 199, "/local/domain/0/backend/vif/%d",
437 438
             virDomainGetID(domain));
    backend[199] = 0;
439
    list = xs_directory(conn->xshandle, 0, backend, &num);
440 441 442 443
    ret = 0;
    if (list == NULL)
        goto done;

444
    for (i = 0; i < num; i++) {
445
        id = strtol(list[i], &endptr, 10);
446 447 448 449 450
        if ((endptr == list[i]) || (*endptr != 0)) {
            ret = -1;
            goto done;
        }
        virDomainGetXMLInterface(domain, buf, id);
451 452
    }

453
 done:
454 455 456
    if (list != NULL)
        free(list);

457
    return (ret);
458 459
}

460 461 462



463 464 465 466 467
/**
 * virDomainGetXMLBoot:
 * @domain: a domain object
 * @buf: the output buffer object
 *
468
 * Extract the boot information used to start that domain
469 470 471 472
 *
 * Returns 0 in case of success, -1 in case of failure
 */
static int
473 474
virDomainGetXMLBoot(virDomainPtr domain, virBufferPtr buf)
{
475 476
    char *vm, *str;

K
Karel Zak 已提交
477
    if (!VIR_IS_DOMAIN(domain))
478 479
        return (-1);

480
    vm = virDomainGetVM(domain);
481
    if (vm == NULL)
482
        return (-1);
483 484 485 486 487 488 489 490 491 492 493 494 495 496

    virBufferAdd(buf, "  <os>\n", 7);
    str = virDomainGetVMInfo(domain, vm, "image/ostype");
    if (str != NULL) {
        virBufferVSprintf(buf, "    <type>%s</type>\n", str);
        free(str);
    }
    str = virDomainGetVMInfo(domain, vm, "image/kernel");
    if (str != NULL) {
        virBufferVSprintf(buf, "    <kernel>%s</kernel>\n", str);
        free(str);
    }
    str = virDomainGetVMInfo(domain, vm, "image/ramdisk");
    if (str != NULL) {
497 498
        if (str[0] != 0)
            virBufferVSprintf(buf, "    <initrd>%s</initrd>\n", str);
499 500 501 502
        free(str);
    }
    str = virDomainGetVMInfo(domain, vm, "image/cmdline");
    if (str != NULL) {
503 504
        if (str[0] != 0)
            virBufferVSprintf(buf, "    <cmdline>%s</cmdline>\n", str);
505 506 507 508 509
        free(str);
    }
    virBufferAdd(buf, "  </os>\n", 8);

    free(vm);
510
    return (0);
511 512
}

513 514 515 516 517 518 519 520 521 522 523 524
/**
 * virDomainGetXMLDesc:
 * @domain: a domain object
 * @flags: and OR'ed set of extraction flags, not used yet
 *
 * Provide an XML description of the domain. NOTE: this API is subject
 * to changes.
 *
 * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
525 526
virDomainGetXMLDesc(virDomainPtr domain, int flags)
{
527
    char *ret = NULL;
528
    unsigned char uuid[VIR_UUID_BUFLEN];
529 530 531
    virBuffer buf;
    virDomainInfo info;

K
Karel Zak 已提交
532
    if (!VIR_IS_DOMAIN(domain))
533
        return (NULL);
K
Karel Zak 已提交
534
    if (flags != 0)
535
        return (NULL);
536
    if (virDomainGetInfo(domain, &info) < 0)
537
        return (NULL);
538 539 540

    ret = malloc(1000);
    if (ret == NULL)
541
        return (NULL);
542 543 544 545 546 547
    buf.content = ret;
    buf.size = 1000;
    buf.use = 0;

    virBufferVSprintf(&buf, "<domain type='xen' id='%d'>\n",
                      virDomainGetID(domain));
548 549
    virBufferVSprintf(&buf, "  <name>%s</name>\n",
                      virDomainGetName(domain));
550
    if (virDomainGetUUID(domain, &uuid[0]) == 0) {
551 552 553 554 555 556
        virBufferVSprintf(&buf,
                          "  <uuid>%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x</uuid>\n",
                          uuid[0], uuid[1], uuid[2], uuid[3],
                          uuid[4], uuid[5], uuid[6], uuid[7],
                          uuid[8], uuid[9], uuid[10], uuid[11],
                          uuid[12], uuid[13], uuid[14], uuid[15]);
557
    }
558
    virDomainGetXMLBoot(domain, &buf);
559 560 561 562 563 564
    virBufferVSprintf(&buf, "  <memory>%lu</memory>\n", info.maxMem);
    virBufferVSprintf(&buf, "  <vcpu>%d</vcpu>\n", (int) info.nrVirtCpu);
    virBufferAdd(&buf, "  <devices>\n", 12);
    virDomainGetXMLDevices(domain, &buf);
    virDomainGetXMLInterfaces(domain, &buf);
    virBufferAdd(&buf, "  </devices>\n", 13);
565
    virBufferAdd(&buf, "</domain>\n", 10);
566

567
    buf.content[buf.use] = 0;
568
    return (ret);
569
}
570

571
#endif /* 0 - UNUSED */
D
Daniel Veillard 已提交
572

573
#ifndef PROXY
574
/**
575
 * virtDomainParseXMLGraphicsDescImage:
576 577
 * @node: node containing graphics description
 * @buf: a buffer for the result S-Expr
578
 * @xendConfigVersion: xend configuration file format
579
 *
580 581 582
 * Parse the graphics part of the XML description and add it to the S-Expr
 * in buf.  This is a temporary interface as the S-Expr interface will be
 * replaced by XML-RPC in the future. However the XML format should stay
583 584 585 586
 * valid over time.
 *
 * Returns 0 in case of success, -1 in case of error
 */
587
static int virDomainParseXMLGraphicsDescImage(xmlNodePtr node, virBufferPtr buf, int xendConfigVersion)
588 589 590 591 592 593 594
{
    xmlChar *graphics_type = NULL;

    graphics_type = xmlGetProp(node, BAD_CAST "type");
    if (graphics_type != NULL) {
        if (xmlStrEqual(graphics_type, BAD_CAST "sdl")) {
            virBufferAdd(buf, "(sdl 1)", 7);
595 596 597 598 599 600
            /* TODO:
             * Need to understand sdl options
             *
             *virBufferAdd(buf, "(display localhost:10.0)", 24);
             *virBufferAdd(buf, "(xauthority /root/.Xauthority)", 30);
             */
601
        }
602
        else if (xmlStrEqual(graphics_type, BAD_CAST "vnc")) {
603
            virBufferAdd(buf, "(vnc 1)", 7);
604
            if (xendConfigVersion >= 2) {
605
                xmlChar *vncport = xmlGetProp(node, BAD_CAST "port");
606 607
                xmlChar *vnclisten = xmlGetProp(node, BAD_CAST "listen");
                xmlChar *vncpasswd = xmlGetProp(node, BAD_CAST "passwd");
608
                if (vncport != NULL) {
609
                    long port = strtol((const char *)vncport, NULL, 10);
610 611 612 613
                    if (port == -1)
                        virBufferAdd(buf, "(vncunused 1)", 13);
                    else if (port > 5900)
                        virBufferVSprintf(buf, "(vncdisplay %d)", port - 5900);
614
                    xmlFree(vncport);
615
                }
616 617 618 619 620 621 622 623
                if (vnclisten != NULL) {
                    virBufferVSprintf(buf, "(vnclisten %s)", vnclisten);
                    xmlFree(vnclisten);
                }
                if (vncpasswd != NULL) {
                    virBufferVSprintf(buf, "(vncpasswd %s)", vncpasswd);
                    xmlFree(vncpasswd);
                }
624 625
            }
        }
626 627 628 629 630 631
        xmlFree(graphics_type);
    }
    return 0;
}


632 633 634 635 636
/**
 * virtDomainParseXMLGraphicsDescVFB:
 * @node: node containing graphics description
 * @buf: a buffer for the result S-Expr
 *
637 638 639
 * Parse the graphics part of the XML description and add it to the S-Expr
 * in buf.  This is a temporary interface as the S-Expr interface will be
 * replaced by XML-RPC in the future. However the XML format should stay
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
 * valid over time.
 *
 * Returns 0 in case of success, -1 in case of error
 */
static int virDomainParseXMLGraphicsDescVFB(xmlNodePtr node, virBufferPtr buf)
{
    xmlChar *graphics_type = NULL;

    graphics_type = xmlGetProp(node, BAD_CAST "type");
    if (graphics_type != NULL) {
        virBufferAdd(buf, "(device (vkbd))", 15);
        virBufferAdd(buf, "(device (vfb ", 13);
        if (xmlStrEqual(graphics_type, BAD_CAST "sdl")) {
            virBufferAdd(buf, "(type sdl)", 10);
            /* TODO:
             * Need to understand sdl options
             *
             *virBufferAdd(buf, "(display localhost:10.0)", 24);
             *virBufferAdd(buf, "(xauthority /root/.Xauthority)", 30);
             */
        }
        else if (xmlStrEqual(graphics_type, BAD_CAST "vnc")) {
            virBufferAdd(buf, "(type vnc)", 10);
            xmlChar *vncport = xmlGetProp(node, BAD_CAST "port");
            xmlChar *vnclisten = xmlGetProp(node, BAD_CAST "listen");
            xmlChar *vncpasswd = xmlGetProp(node, BAD_CAST "passwd");
            if (vncport != NULL) {
                long port = strtol((const char *)vncport, NULL, 10);
                if (port == -1)
                    virBufferAdd(buf, "(vncunused 1)", 13);
                else if (port > 5900)
                    virBufferVSprintf(buf, "(vncdisplay %d)", port - 5900);
                xmlFree(vncport);
            }
            if (vnclisten != NULL) {
                virBufferVSprintf(buf, "(vnclisten %s)", vnclisten);
                xmlFree(vnclisten);
            }
            if (vncpasswd != NULL) {
                virBufferVSprintf(buf, "(vncpasswd %s)", vncpasswd);
                xmlFree(vncpasswd);
            }
        }
        virBufferAdd(buf, "))", 2);
        xmlFree(graphics_type);
    }
    return 0;
}


690
/**
691 692
 * virDomainParseXMLOSDescHVM:
 * @node: node containing HVM OS description
693
 * @buf: a buffer for the result S-Expr
694
 * @ctxt: a path context representing the XML description
695
 * @vcpus: number of virtual CPUs to configure
696
 * @xendConfigVersion: xend configuration file format
697
 *
698 699
 * Parse the OS part of the XML description for an HVM domain and add it to
 * the S-Expr in buf. This is a temporary interface as the S-Expr interface
700 701 702 703 704 705
 * will be replaced by XML-RPC in the future. However the XML format should
 * stay valid over time.
 *
 * Returns 0 in case of success, -1 in case of error.
 */
static int
706
virDomainParseXMLOSDescHVM(xmlNodePtr node, virBufferPtr buf, xmlXPathContextPtr ctxt, int vcpus, int xendConfigVersion)
707 708 709
{
    xmlXPathObjectPtr obj = NULL;
    xmlNodePtr cur, txt;
710 711 712
    xmlChar *type = NULL;
    xmlChar *loader = NULL;
    xmlChar *boot_dev = NULL;
713
    int res;
714 715 716 717 718 719 720 721

    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
            if ((type == NULL)
                && (xmlStrEqual(cur->name, BAD_CAST "type"))) {
                txt = cur->children;
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
722
                    (txt->next == NULL))
723 724 725 726 727
                    type = txt->content;
            } else if ((loader == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "loader"))) {
                txt = cur->children;
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
728
                    (txt->next == NULL))
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
                    loader = txt->content;
            } else if ((boot_dev == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "boot"))) {
                boot_dev = xmlGetProp(cur, BAD_CAST "dev");
            }
        }
        cur = cur->next;
    }
    if ((type == NULL) || (!xmlStrEqual(type, BAD_CAST "hvm"))) {
        /* VIR_ERR_OS_TYPE */
        virXMLError(VIR_ERR_OS_TYPE, (const char *) type, 0);
        return (-1);
    }
    virBufferAdd(buf, "(image (hvm ", 12);
    if (loader == NULL) {
744 745
        virXMLError(VIR_ERR_NO_KERNEL, NULL, 0);
        goto error;
746
    } else {
747
        virBufferVSprintf(buf, "(kernel '%s')", (const char *) loader);
748 749 750 751 752 753 754 755 756 757 758 759 760 761
    }

    /* get the device emulation model */
    obj = xmlXPathEval(BAD_CAST "string(/domain/devices/emulator[1])", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
        virXMLError(VIR_ERR_NO_KERNEL, NULL, 0); /* TODO: error */
        goto error;
    }
    virBufferVSprintf(buf, "(device_model '%s')",
                      (const char *) obj->stringval);
    xmlXPathFreeObject(obj);
    obj = NULL;

762 763
    virBufferVSprintf(buf, "(vcpus %d)", vcpus);

764
    if (boot_dev) {
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
        if (xmlStrEqual(boot_dev, BAD_CAST "fd")) {
            virBufferVSprintf(buf, "(boot a)", (const char *) boot_dev);
        } else if (xmlStrEqual(boot_dev, BAD_CAST "cdrom")) {
            virBufferVSprintf(buf, "(boot d)", (const char *) boot_dev);
        } else if (xmlStrEqual(boot_dev, BAD_CAST "hd")) {
            virBufferVSprintf(buf, "(boot c)", (const char *) boot_dev);
        } else {
            /* Any other type of boot dev is unsupported right now */
            virXMLError(VIR_ERR_XML_ERROR, NULL, 0);
        }

        /* get the 1st floppy device file */
        obj = xmlXPathEval(BAD_CAST "/domain/devices/disk[@device='floppy' and target/@dev='fda']/source", ctxt);
        if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
            (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr == 1)) {
            cur = obj->nodesetval->nodeTab[0];
            virBufferVSprintf(buf, "(fda '%s')",
                              (const char *) xmlGetProp(cur, BAD_CAST "file"));
            cur = NULL;
        }
        if (obj) {
            xmlXPathFreeObject(obj);
            obj = NULL;
        }

        /* get the 2nd floppy device file */
        obj = xmlXPathEval(BAD_CAST "/domain/devices/disk[@device='floppy' and target/@dev='fdb']/source", ctxt);
        if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
            (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr == 1)) {
            xmlChar *fdfile = NULL;
            cur = obj->nodesetval->nodeTab[0];
            fdfile = xmlGetProp(cur, BAD_CAST "file");
            virBufferVSprintf(buf, "(fdb '%s')",
                              (const char *) fdfile);
            xmlFree(fdfile);
            cur = NULL;
        }
        if (obj) {
            xmlXPathFreeObject(obj);
            obj = NULL;
        }


        /* get the cdrom device file */
        /* Only XenD <= 3.0.2 wants cdrom config here */
        if (xendConfigVersion == 1) {
            obj = xmlXPathEval(BAD_CAST "/domain/devices/disk[@device='cdrom' and target/@dev='hdc']/source", ctxt);
            if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
                (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr == 1)) {
                xmlChar *cdfile = NULL;
                cur = obj->nodesetval->nodeTab[0];
                cdfile = xmlGetProp(cur, BAD_CAST "file");
                virBufferVSprintf(buf, "(cdrom '%s')",
                                  (const char *)cdfile);
                xmlFree(cdfile);
                cur = NULL;
            }
            if (obj) {
                xmlXPathFreeObject(obj);
                obj = NULL;
            }
        }

        obj = xmlXPathEval(BAD_CAST "/domain/features/acpi", ctxt);
        if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
            (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr == 1)) {
            virBufferAdd(buf, "(acpi 1)", 8);
        }
        if (obj)
            xmlXPathFreeObject(obj);
        obj = xmlXPathEval(BAD_CAST "/domain/features/apic", ctxt);
        if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
            (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr == 1)) {
            virBufferAdd(buf, "(apic 1)", 8);
        }
        if (obj)
            xmlXPathFreeObject(obj);
        obj = xmlXPathEval(BAD_CAST "/domain/features/pae", ctxt);
        if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
            (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr == 1)) {
            virBufferAdd(buf, "(pae 1)", 7);
        }
        if (obj)
            xmlXPathFreeObject(obj);
        obj = NULL;
850 851 852 853
    }

    obj = xmlXPathEval(BAD_CAST "count(domain/devices/console) > 0", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_BOOLEAN)) {
854 855
        virXMLError(VIR_ERR_XML_ERROR, NULL, 0);
        goto error;
856 857
    }
    if (obj->boolval) {
858
        virBufferAdd(buf, "(serial pty)", 12);
859 860 861
    }
    xmlXPathFreeObject(obj);
    obj = NULL;
862

863 864 865
    /* Is a graphics device specified? */
    obj = xmlXPathEval(BAD_CAST "/domain/devices/graphics[1]", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
866
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr > 0)) {
867
        res = virDomainParseXMLGraphicsDescImage(obj->nodesetval->nodeTab[0], buf, xendConfigVersion);
868 869
        if (res != 0) {
            goto error;
870 871 872 873 874 875
        }
    }
    xmlXPathFreeObject(obj);

    virBufferAdd(buf, "))", 2);

876 877 878
    if (boot_dev)
        xmlFree(boot_dev);

879
    return (0);
880
 error:
881 882
    if (boot_dev)
        xmlFree(boot_dev);
883 884 885 886 887 888 889 890 891
    if (obj != NULL)
        xmlXPathFreeObject(obj);
    return(-1);
}

/**
 * virDomainParseXMLOSDescPV:
 * @node: node containing PV OS description
 * @buf: a buffer for the result S-Expr
892
 * @ctxt: a path context representing the XML description
893
 * @xendConfigVersion: xend configuration file format
894 895 896 897 898 899 900 901 902
 *
 * Parse the OS part of the XML description for a paravirtualized domain
 * and add it to the S-Expr in buf.  This is a temporary interface as the
 * S-Expr interface will be replaced by XML-RPC in the future. However
 * the XML format should stay valid over time.
 *
 * Returns 0 in case of success, -1 in case of error.
 */
static int
903
virDomainParseXMLOSDescPV(xmlNodePtr node, virBufferPtr buf, xmlXPathContextPtr ctxt, int xendConfigVersion)
904
{
905
    xmlNodePtr cur, txt;
906
    xmlXPathObjectPtr obj = NULL;
907 908 909 910 911
    const xmlChar *type = NULL;
    const xmlChar *root = NULL;
    const xmlChar *kernel = NULL;
    const xmlChar *initrd = NULL;
    const xmlChar *cmdline = NULL;
912
    int res;
913 914 915 916

    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
917 918 919
            if ((type == NULL)
                && (xmlStrEqual(cur->name, BAD_CAST "type"))) {
                txt = cur->children;
920
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
921
                    (txt->next == NULL))
922 923 924 925
                    type = txt->content;
            } else if ((kernel == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "kernel"))) {
                txt = cur->children;
926
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
927
                    (txt->next == NULL))
928 929 930 931
                    kernel = txt->content;
            } else if ((root == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "root"))) {
                txt = cur->children;
932
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
933
                    (txt->next == NULL))
934 935 936 937
                    root = txt->content;
            } else if ((initrd == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "initrd"))) {
                txt = cur->children;
938
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
939
                    (txt->next == NULL))
940 941 942 943
                    initrd = txt->content;
            } else if ((cmdline == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "cmdline"))) {
                txt = cur->children;
944
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
945
                    (txt->next == NULL))
946 947 948
                    cmdline = txt->content;
            }
        }
949 950 951 952
        cur = cur->next;
    }
    if ((type != NULL) && (!xmlStrEqual(type, BAD_CAST "linux"))) {
        /* VIR_ERR_OS_TYPE */
953 954
        virXMLError(VIR_ERR_OS_TYPE, (const char *) type, 0);
        return (-1);
955
    }
956
    virBufferAdd(buf, "(image (linux ", 14);
957
    if (kernel == NULL) {
958 959
        virXMLError(VIR_ERR_NO_KERNEL, NULL, 0);
        return (-1);
960
    } else {
961
        virBufferVSprintf(buf, "(kernel '%s')", (const char *) kernel);
962 963
    }
    if (initrd != NULL)
964
        virBufferVSprintf(buf, "(ramdisk '%s')", (const char *) initrd);
965
    if (root != NULL)
966
        virBufferVSprintf(buf, "(root '%s')", (const char *) root);
967
    if (cmdline != NULL)
968
        virBufferVSprintf(buf, "(args '%s')", (const char *) cmdline);
969 970

    /* Is a graphics device specified? */
971 972 973 974 975 976 977 978 979
    /* Old style config before merge of PVFB */
    if (xendConfigVersion < 3) {
        obj = xmlXPathEval(BAD_CAST "/domain/devices/graphics[1]", ctxt);
        if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
            (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr > 0)) {
            res = virDomainParseXMLGraphicsDescImage(obj->nodesetval->nodeTab[0], buf, xendConfigVersion);
            if (res != 0) {
                goto error;
            }
980
        }
981
        xmlXPathFreeObject(obj);
982 983 984
    }

 error:
985
    virBufferAdd(buf, "))", 2);
986
    return (0);
987 988
}

989 990 991 992 993 994 995 996 997 998
/**
 * virCatchXMLParseError:
 * @ctx: the context
 * @msg: the error message
 * @...: extra arguments
 *
 * SAX callback on parsing errors, act as a gate for libvirt own
 * error reporting.
 */
static void
999
virCatchXMLParseError(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
1000 1001
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;

1002
    if ((ctxt != NULL) &&
1003
        (ctxt->lastError.level == XML_ERR_FATAL) &&
1004
        (ctxt->lastError.message != NULL)) {
1005
        virXMLError(VIR_ERR_XML_DETAIL, ctxt->lastError.message,
1006
                    ctxt->lastError.line);
1007 1008 1009
    }
}

1010 1011
/**
 * virDomainParseXMLDiskDesc:
1012
 * @node: node containing disk description
1013
 * @buf: a buffer for the result S-Expr
1014
 * @xendConfigVersion: xend configuration file format
1015 1016 1017 1018 1019 1020 1021 1022 1023
 *
 * Parse the one disk in the XML description and add it to the S-Expr in buf
 * This is a temporary interface as the S-Expr interface
 * will be replaced by XML-RPC in the future. However the XML format should
 * stay valid over time.
 *
 * Returns 0 in case of success, -1 in case of error.
 */
static int
1024
virDomainParseXMLDiskDesc(xmlNodePtr node, virBufferPtr buf, int hvm, int xendConfigVersion)
1025
{
1026 1027
    xmlNodePtr cur;
    xmlChar *type = NULL;
1028
    xmlChar *device = NULL;
1029 1030
    xmlChar *source = NULL;
    xmlChar *target = NULL;
1031 1032
    xmlChar *drvName = NULL;
    xmlChar *drvType = NULL;
1033
    int ro = 0;
1034
    int shareable = 0;
1035
    int typ = 0;
1036
    int cdrom = 0;
1037 1038 1039

    type = xmlGetProp(node, BAD_CAST "type");
    if (type != NULL) {
1040 1041 1042 1043 1044
        if (xmlStrEqual(type, BAD_CAST "file"))
            typ = 0;
        else if (xmlStrEqual(type, BAD_CAST "block"))
            typ = 1;
        xmlFree(type);
1045
    }
1046
    device = xmlGetProp(node, BAD_CAST "device");
1047

1048 1049 1050
    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
            if ((source == NULL) &&
                (xmlStrEqual(cur->name, BAD_CAST "source"))) {

                if (typ == 0)
                    source = xmlGetProp(cur, BAD_CAST "file");
                else
                    source = xmlGetProp(cur, BAD_CAST "dev");
            } else if ((target == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "target"))) {
                target = xmlGetProp(cur, BAD_CAST "dev");
1061 1062 1063 1064 1065
            } else if ((drvName == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "driver"))) {
                drvName = xmlGetProp(cur, BAD_CAST "name");
                if (drvName && !strcmp((const char *)drvName, "tap"))
                    drvType = xmlGetProp(cur, BAD_CAST "type");
1066 1067
            } else if (xmlStrEqual(cur->name, BAD_CAST "readonly")) {
                ro = 1;
1068
            } else if (xmlStrEqual(cur->name, BAD_CAST "shareable")) {
1069
                shareable = 1;
1070 1071
            }
        }
1072 1073 1074 1075
        cur = cur->next;
    }

    if (source == NULL) {
1076 1077 1078 1079
        virXMLError(VIR_ERR_NO_SOURCE, (const char *) target, 0);

        if (target != NULL)
            xmlFree(target);
1080 1081
        if (device != NULL)
            xmlFree(device);
1082
        return (-1);
1083 1084
    }
    if (target == NULL) {
1085 1086 1087
        virXMLError(VIR_ERR_NO_TARGET, (const char *) source, 0);
        if (source != NULL)
            xmlFree(source);
1088 1089
        if (device != NULL)
            xmlFree(device);
1090
        return (-1);
1091
    }
1092

1093 1094
    /* Xend (all versions) put the floppy device config
     * under the hvm (image (os)) block
1095
     */
1096
    if (hvm &&
1097
        device &&
1098
        !strcmp((const char *)device, "floppy")) {
1099
        goto cleanup;
1100 1101 1102
    }

    /* Xend <= 3.0.2 doesn't include cdrom config here */
1103
    if (hvm &&
1104 1105
        device &&
        !strcmp((const char *)device, "cdrom")) {
1106
        if (xendConfigVersion == 1)
1107
            goto cleanup;
1108 1109
        else
            cdrom = 1;
1110 1111 1112 1113
    }


    virBufferAdd(buf, "(device ", 8);
1114 1115 1116 1117 1118 1119 1120 1121
    /* Normally disks are in a (device (vbd ...)) block
       but blktap disks ended up in a differently named
       (device (tap ....)) block.... */
    if (drvName && !strcmp((const char *)drvName, "tap")) {
        virBufferAdd(buf, "(tap ", 5);
    } else {
        virBufferAdd(buf, "(vbd ", 5);
    }
1122

1123 1124
    if (hvm) {
        char *tmp = (char *)target;
1125
        /* Just in case user mistakenly still puts ioemu: in their XML */
1126 1127
        if (!strncmp((const char *) tmp, "ioemu:", 6))
            tmp += 6;
1128 1129 1130

        /* Xend <= 3.0.2 wants a ioemu: prefix on devices for HVM */
        if (xendConfigVersion == 1)
1131
            virBufferVSprintf(buf, "(dev 'ioemu:%s')", (const char *)tmp);
1132
        else /* But newer does not */
1133
            virBufferVSprintf(buf, "(dev '%s%s')", (const char *)tmp, cdrom ? ":cdrom" : ":disk");
1134
    } else
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
        virBufferVSprintf(buf, "(dev '%s')", (const char *)target);

    if (drvName) {
        if (!strcmp((const char *)drvName, "tap")) {
            virBufferVSprintf(buf, "(uname '%s:%s:%s')",
                              (const char *)drvName,
                              (drvType ? (const char *)drvType : "aio"),
                              (const char *)source);
        } else {
            virBufferVSprintf(buf, "(uname '%s:%s')",
                              (const char *)drvName,
                              (const char *)source);
        }
    } else {
        if (typ == 0)
            virBufferVSprintf(buf, "(uname 'file:%s')", source);
        else if (typ == 1) {
            if (source[0] == '/')
                virBufferVSprintf(buf, "(uname 'phy:%s')", source);
            else
                virBufferVSprintf(buf, "(uname 'phy:/dev/%s')", source);
        }
1157
    }
1158
    if (ro == 1)
1159
        virBufferVSprintf(buf, "(mode 'r')");
1160 1161 1162 1163
    else if (shareable == 1)
        virBufferVSprintf(buf, "(mode 'w!')");
    else
        virBufferVSprintf(buf, "(mode 'w')");
1164

1165
    virBufferAdd(buf, ")", 1);
1166
    virBufferAdd(buf, ")", 1);
1167 1168

 cleanup:
1169 1170
    xmlFree(drvType);
    xmlFree(drvName);
1171
    xmlFree(device);
1172 1173
    xmlFree(target);
    xmlFree(source);
1174
    return (0);
1175 1176 1177 1178
}

/**
 * virDomainParseXMLIfDesc:
1179
 * @node: node containing the interface description
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
 * @buf: a buffer for the result S-Expr
 *
 * Parse the one interface the XML description and add it to the S-Expr in buf
 * This is a temporary interface as the S-Expr interface
 * will be replaced by XML-RPC in the future. However the XML format should
 * stay valid over time.
 *
 * Returns 0 in case of success, -1 in case of error.
 */
static int
1190
virDomainParseXMLIfDesc(xmlNodePtr node, virBufferPtr buf, int hvm)
1191
{
1192 1193 1194 1195 1196
    xmlNodePtr cur;
    xmlChar *type = NULL;
    xmlChar *source = NULL;
    xmlChar *mac = NULL;
    xmlChar *script = NULL;
1197
    xmlChar *ip = NULL;
1198 1199 1200 1201
    int typ = 0;

    type = xmlGetProp(node, BAD_CAST "type");
    if (type != NULL) {
1202 1203 1204 1205 1206
        if (xmlStrEqual(type, BAD_CAST "bridge"))
            typ = 0;
        else if (xmlStrEqual(type, BAD_CAST "ethernet"))
            typ = 1;
        xmlFree(type);
1207 1208 1209 1210
    }
    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
            if ((source == NULL) &&
                (xmlStrEqual(cur->name, BAD_CAST "source"))) {
                if (typ == 0)
                    source = xmlGetProp(cur, BAD_CAST "bridge");
                else
                    source = xmlGetProp(cur, BAD_CAST "dev");
            } else if ((mac == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "mac"))) {
                mac = xmlGetProp(cur, BAD_CAST "address");
            } else if ((script == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "script"))) {
                script = xmlGetProp(cur, BAD_CAST "path");
1223 1224 1225 1226 1227 1228 1229
            } else if ((ip == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "ip"))) {
                /* XXX in future expect to need to have > 1 ip
                   address element - eg ipv4 & ipv6. For now
                   xen only supports a single address though
                   so lets ignore that complication */
                ip = xmlGetProp(cur, BAD_CAST "address");
1230 1231
            }
        }
1232 1233 1234 1235 1236
        cur = cur->next;
    }

    virBufferAdd(buf, "(vif ", 5);
    if (mac != NULL)
1237
        virBufferVSprintf(buf, "(mac '%s')", (const char *) mac);
1238
    if (source != NULL) {
1239 1240 1241 1242
        if (typ == 0)
            virBufferVSprintf(buf, "(bridge '%s')", (const char *) source);
        else                    /* TODO does that work like that ? */
            virBufferVSprintf(buf, "(dev '%s')", (const char *) source);
1243 1244 1245
    }
    if (script != NULL)
        virBufferVSprintf(buf, "(script '%s')", script);
1246 1247
    if (ip != NULL)
        virBufferVSprintf(buf, "(ip '%s')", ip);
1248 1249
    if (hvm)
        virBufferAdd(buf, "(type ioemu)", 12);
1250 1251 1252

    virBufferAdd(buf, ")", 1);
    if (mac != NULL)
1253
        xmlFree(mac);
1254
    if (source != NULL)
1255
        xmlFree(source);
1256
    if (script != NULL)
1257
        xmlFree(script);
1258 1259
    if (ip != NULL)
        xmlFree(ip);
1260
    return (0);
1261 1262 1263 1264 1265
}

/**
 * virDomainParseXMLDesc:
 * @xmldesc: string with the XML description
1266
 * @xendConfigVersion: xend configuration file format
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
 *
 * Parse the XML description and turn it into the xend sexp needed to
 * create the comain. This is a temporary interface as the S-Expr interface
 * will be replaced by XML-RPC in the future. However the XML format should
 * stay valid over time.
 *
 * Returns the 0 terminatedi S-Expr string or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
1277
virDomainParseXMLDesc(const char *xmldesc, char **name, int xendConfigVersion)
1278
{
1279 1280
    xmlDocPtr xml = NULL;
    xmlNodePtr node;
1281
    char *ret = NULL, *nam = NULL;
1282 1283
    virBuffer buf;
    xmlChar *prop;
1284
    xmlParserCtxtPtr pctxt;
1285
    xmlXPathObjectPtr obj = NULL;
1286
    xmlXPathObjectPtr tmpobj = NULL;
1287 1288
    xmlXPathContextPtr ctxt = NULL;
    int i, res;
1289
    int bootloader = 0;
1290
    int hvm = 0;
1291
    unsigned int vcpus = 1;
1292
    unsigned long mem = 0, max_mem = 0;
1293 1294

    if (name != NULL)
1295
        *name = NULL;
1296 1297
    ret = malloc(1000);
    if (ret == NULL)
1298
        return (NULL);
1299 1300 1301 1302
    buf.content = ret;
    buf.size = 1000;
    buf.use = 0;

1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
    pctxt = xmlNewParserCtxt();
    if ((pctxt == NULL) || (pctxt->sax == NULL)) {
        goto error;
    }

    pctxt->sax->error = virCatchXMLParseError;

    xml = xmlCtxtReadDoc(pctxt, (const xmlChar *) xmldesc, "domain.xml", NULL,
                         XML_PARSE_NOENT | XML_PARSE_NONET |
                         XML_PARSE_NOWARNING);
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
    if (xml == NULL) {
        goto error;
    }
    node = xmlDocGetRootElement(xml);
    if ((node == NULL) || (!xmlStrEqual(node->name, BAD_CAST "domain")))
        goto error;

    prop = xmlGetProp(node, BAD_CAST "type");
    if (prop != NULL) {
        if (!xmlStrEqual(prop, BAD_CAST "xen")) {
1323 1324 1325 1326
            xmlFree(prop);
            goto error;
        }
        xmlFree(prop);
1327 1328 1329 1330 1331 1332 1333
    }
    virBufferAdd(&buf, "(vm ", 4);
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
        goto error;
    }
    /*
1334
     * extract some of the basics, name, memory, cpus ...
1335 1336
     */
    obj = xmlXPathEval(BAD_CAST "string(/domain/name[1])", ctxt);
1337
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
1338
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
1339
        virXMLError(VIR_ERR_NO_NAME, xmldesc, 0);
1340 1341 1342
        goto error;
    }
    virBufferVSprintf(&buf, "(name '%s')", obj->stringval);
1343 1344
    nam = strdup((const char *) obj->stringval);
    if (nam == NULL) {
1345 1346
        virXMLError(VIR_ERR_NO_MEMORY, "copying name", 0);
        goto error;
1347
    }
1348 1349 1350 1351
    xmlXPathFreeObject(obj);

    obj = xmlXPathEval(BAD_CAST "number(/domain/memory[1])", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NUMBER) ||
1352
        (isnan(obj->floatval)) || (obj->floatval < 64000)) {
1353
        max_mem = 128;
1354
    } else {
1355 1356 1357 1358 1359 1360 1361 1362 1363
        max_mem = (obj->floatval / 1024);
    }
    xmlXPathFreeObject(obj);
    obj = xmlXPathEval(BAD_CAST "number(/domain/currentMemory[1])", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NUMBER) ||
        (isnan(obj->floatval)) || (obj->floatval < 64000)) {
        mem = max_mem;
    } else {
        mem = (obj->floatval / 1024);
1364 1365 1366
        if (mem > max_mem) {
            max_mem = mem;
        }
1367 1368
    }
    xmlXPathFreeObject(obj);
1369
    virBufferVSprintf(&buf, "(memory %lu)(maxmem %lu)", mem, max_mem);
1370 1371

    obj = xmlXPathEval(BAD_CAST "number(/domain/vcpu[1])", ctxt);
1372 1373 1374
    if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
        (!isnan(obj->floatval)) && (obj->floatval > 0)) {
        vcpus = (unsigned int) obj->floatval;
1375
    }
1376
    virBufferVSprintf(&buf, "(vcpus %u)", vcpus);
1377 1378
    xmlXPathFreeObject(obj);

1379
    obj = xmlXPathEval(BAD_CAST "string(/domain/uuid[1])", ctxt);
1380
    if ((obj == NULL) || ((obj->type == XPATH_STRING) &&
1381
                          (obj->stringval != NULL) && (obj->stringval[0] != 0))) {
1382 1383 1384 1385
        virBufferVSprintf(&buf, "(uuid '%s')", obj->stringval);
    }
    xmlXPathFreeObject(obj);

1386 1387 1388
    obj = xmlXPathEval(BAD_CAST "string(/domain/bootloader[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
1389 1390 1391 1392 1393 1394 1395 1396 1397
        virBufferVSprintf(&buf, "(bootloader '%s')", obj->stringval);
        /*
         * if using pygrub, the kernel and initrd strings are not
         * significant and should be discarded
         */
        if (xmlStrstr(obj->stringval, BAD_CAST "pygrub"))
            bootloader = 2;
        else
            bootloader = 1;
1398 1399 1400
    }
    xmlXPathFreeObject(obj);

1401 1402 1403
    obj = xmlXPathEval(BAD_CAST "string(/domain/on_poweroff[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
1404
        virBufferVSprintf(&buf, "(on_poweroff '%s')", obj->stringval);
1405 1406 1407 1408 1409 1410
    }
    xmlXPathFreeObject(obj);

    obj = xmlXPathEval(BAD_CAST "string(/domain/on_reboot[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
1411
        virBufferVSprintf(&buf, "(on_reboot '%s')", obj->stringval);
1412 1413 1414 1415 1416 1417
    }
    xmlXPathFreeObject(obj);

    obj = xmlXPathEval(BAD_CAST "string(/domain/on_crash[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
1418
        virBufferVSprintf(&buf, "(on_crash '%s')", obj->stringval);
1419 1420 1421
    }
    xmlXPathFreeObject(obj);

1422
    if (bootloader != 2) {
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
        obj = xmlXPathEval(BAD_CAST "/domain/os[1]", ctxt);
        if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
            (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr == 1)) {
            /* Analyze of the os description, based on HVM or PV. */
            tmpobj = xmlXPathEval(BAD_CAST "string(/domain/os/type[1])", ctxt);
            if ((tmpobj != NULL) &&
                ((tmpobj->type != XPATH_STRING) || (tmpobj->stringval == NULL)
                 || (tmpobj->stringval[0] == 0))) {
                xmlXPathFreeObject(tmpobj);
                virXMLError(VIR_ERR_OS_TYPE, nam, 0);
                goto error;
            }

            if ((tmpobj == NULL)
                || !xmlStrEqual(tmpobj->stringval, BAD_CAST "hvm")) {
                res = virDomainParseXMLOSDescPV(obj->nodesetval->nodeTab[0],
                                                &buf, ctxt, xendConfigVersion);
            } else {
                hvm = 1;
                res = virDomainParseXMLOSDescHVM(obj->nodesetval->nodeTab[0],
                                                 &buf, ctxt, vcpus, xendConfigVersion);
            }

            xmlXPathFreeObject(tmpobj);

            if (res != 0)
                goto error;
        } else if (bootloader == 0) {
            virXMLError(VIR_ERR_NO_OS, nam, 0);
            goto error;
        }
        xmlXPathFreeObject(obj);
1455 1456 1457 1458
    }

    /* analyze of the devices */
    obj = xmlXPathEval(BAD_CAST "/domain/devices/disk", ctxt);
1459 1460
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
1461 1462 1463 1464 1465 1466
        for (i = 0; i < obj->nodesetval->nodeNr; i++) {
            res = virDomainParseXMLDiskDesc(obj->nodesetval->nodeTab[i], &buf, hvm, xendConfigVersion);
            if (res != 0) {
                goto error;
            }
        }
1467 1468
    }
    xmlXPathFreeObject(obj);
1469

1470 1471 1472
    obj = xmlXPathEval(BAD_CAST "/domain/devices/interface", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
1473 1474
        for (i = 0; i < obj->nodesetval->nodeNr; i++) {
            virBufferAdd(&buf, "(device ", 8);
1475
            res = virDomainParseXMLIfDesc(obj->nodesetval->nodeTab[i], &buf, hvm);
1476 1477 1478 1479 1480
            if (res != 0) {
                goto error;
            }
            virBufferAdd(&buf, ")", 1);
        }
1481 1482 1483
    }
    xmlXPathFreeObject(obj);

1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
    /* New style PVFB config  - 3.0.4 merge */
    if (xendConfigVersion >= 3 && !hvm) {
        obj = xmlXPathEval(BAD_CAST "/domain/devices/graphics", ctxt);
        if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
            (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
            for (i = 0; i < obj->nodesetval->nodeNr; i++) {
                res = virDomainParseXMLGraphicsDescVFB(obj->nodesetval->nodeTab[i], &buf);
                if (res != 0) {
                    goto error;
                }
            }
        }
        xmlXPathFreeObject(obj);
    }

1499

D
Daniel Veillard 已提交
1500
    virBufferAdd(&buf, ")", 1); /* closes (vm */
1501 1502 1503 1504
    buf.content[buf.use] = 0;

    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
1505
    xmlFreeParserCtxt(pctxt);
1506 1507

    if (name != NULL)
1508
        *name = nam;
1509 1510
    else
        free(nam);
1511

1512 1513
    return (ret);

1514
 error:
1515
    if (nam != NULL)
1516
        free(nam);
1517
    if (name != NULL)
1518
        *name = NULL;
1519 1520 1521 1522 1523 1524
    if (obj != NULL)
        xmlXPathFreeObject(obj);
    if (ctxt != NULL)
        xmlXPathFreeContext(ctxt);
    if (xml != NULL)
        xmlFreeDoc(xml);
1525 1526
    if (pctxt != NULL)
        xmlFreeParserCtxt(pctxt);
1527 1528
    if (ret != NULL)
        free(ret);
1529
    return (NULL);
1530
}
1531 1532

#endif /* !PROXY */
1533 1534 1535 1536



unsigned char *virParseUUID(char **ptr, const char *uuid) {
1537
    int rawuuid[VIR_UUID_BUFLEN];
1538
    const char *cur;
1539 1540 1541 1542 1543 1544
    unsigned char *dst_uuid = NULL;
    int i;

    if (uuid == NULL)
        goto error;

1545 1546 1547 1548 1549
    /*
     * do a liberal scan allowing '-' and ' ' anywhere between character
     * pairs as long as there is 32 of them in the end.
     */
    cur = uuid;
1550
    for (i = 0;i < VIR_UUID_BUFLEN;) {
1551 1552
        rawuuid[i] = 0;
        if (*cur == 0)
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
            goto error;
        if ((*cur == '-') || (*cur == ' ')) {
            cur++;
            continue;
        }
        if ((*cur >= '0') && (*cur <= '9'))
            rawuuid[i] = *cur - '0';
        else if ((*cur >= 'a') && (*cur <= 'f'))
            rawuuid[i] = *cur - 'a' + 10;
        else if ((*cur >= 'A') && (*cur <= 'F'))
            rawuuid[i] = *cur - 'A' + 10;
        else
            goto error;
        rawuuid[i] *= 16;
        cur++;
1568
        if (*cur == 0)
1569 1570 1571 1572 1573 1574 1575 1576 1577
            goto error;
        if ((*cur >= '0') && (*cur <= '9'))
            rawuuid[i] += *cur - '0';
        else if ((*cur >= 'a') && (*cur <= 'f'))
            rawuuid[i] += *cur - 'a' + 10;
        else if ((*cur >= 'A') && (*cur <= 'F'))
            rawuuid[i] += *cur - 'A' + 10;
        else
            goto error;
1578
        i++;
1579
        cur++;
1580
    }
1581 1582 1583 1584

    dst_uuid = (unsigned char *) *ptr;
    *ptr += 16;

1585
    for (i = 0; i < VIR_UUID_BUFLEN; i++)
1586 1587
        dst_uuid[i] = rawuuid[i] & 0xFF;

1588
 error:
1589
    return(dst_uuid);
1590
}
1591

1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
#ifndef PROXY
/**
 * virParseXMLDevice:
 * @xmldesc: string with the XML description
 * @hvm: 1 for fully virtualized guest, 0 for paravirtualized
 * @xendConfigVersion: xend configuration file format
 *
 * Parse the XML description and turn it into the xend sexp needed to
 * create the device. This is a temporary interface as the S-Expr interface
 * will be replaced by XML-RPC in the future. However the XML format should
 * stay valid over time.
 *
 * Returns the 0-terminated S-Expr string, or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
virParseXMLDevice(char *xmldesc, int hvm, int xendConfigVersion)
{
    xmlDocPtr xml = NULL;
    xmlNodePtr node;
    virBuffer buf;

    buf.content = malloc(1000);
    if (buf.content == NULL)
        return (NULL);
    buf.size = 1000;
    buf.use = 0;
    xml = xmlReadDoc((const xmlChar *) xmldesc, "domain.xml", NULL,
                     XML_PARSE_NOENT | XML_PARSE_NONET |
                     XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
    if (xml == NULL)
        goto error;
    node = xmlDocGetRootElement(xml);
    if (node == NULL)
        goto error;
    if (xmlStrEqual(node->name, BAD_CAST "disk")) {
        if (virDomainParseXMLDiskDesc(node, &buf, hvm, xendConfigVersion) != 0)
            goto error;
    }
    else if (xmlStrEqual(node->name, BAD_CAST "interface")) {
        if (virDomainParseXMLIfDesc(node, &buf, hvm) != 0)
            goto error;
    }
1635
 cleanup:
1636 1637 1638
    if (xml != NULL)
        xmlFreeDoc(xml);
    return buf.content;
1639
 error:
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
    free(buf.content);
    buf.content = NULL;
    goto cleanup;
}

/**
 * virDomainXMLDevID:
 * @domain: pointer to domain object
 * @xmldesc: string with the XML description
 * @class: Xen device class "vbd" or "vif" (OUT)
 * @ref: Xen device reference (OUT)
 *
 * Set class according to XML root, and:
 *  - if disk, copy in ref the target name from description
 *  - if network, get MAC address from description, scan XenStore and
 *    copy in ref the corresponding vif number.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
int
virDomainXMLDevID(virDomainPtr domain, char *xmldesc, char *class, char *ref)
{
    xmlDocPtr xml = NULL;
    xmlNodePtr node, cur;
    xmlChar *attr = NULL;
1665
    char *xref;
1666
    int ret = 0;
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679

    xml = xmlReadDoc((const xmlChar *) xmldesc, "domain.xml", NULL,
                     XML_PARSE_NOENT | XML_PARSE_NONET |
                     XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
    if (xml == NULL)
        goto error;
    node = xmlDocGetRootElement(xml);
    if (node == NULL)
        goto error;
    if (xmlStrEqual(node->name, BAD_CAST "disk")) {
        strcpy(class, "vbd");
        for (cur = node->children; cur != NULL; cur = cur->next) {
            if ((cur->type != XML_ELEMENT_NODE) ||
1680
                (!xmlStrEqual(cur->name, BAD_CAST "target"))) continue;
1681 1682 1683
            attr = xmlGetProp(cur, BAD_CAST "dev");
            if (attr == NULL)
                goto error;
1684
            strcpy(ref, (char *)attr);
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
            goto cleanup;
        }
    }
    else if (xmlStrEqual(node->name, BAD_CAST "interface")) {
        strcpy(class, "vif");
        for (cur = node->children; cur != NULL; cur = cur->next) {
            if ((cur->type != XML_ELEMENT_NODE) ||
                (!xmlStrEqual(cur->name, BAD_CAST "mac"))) continue;
            attr = xmlGetProp(cur, BAD_CAST "address");
            if (attr == NULL)
                goto error;

1697
            xref = xenStoreDomainGetNetworkID(domain->conn, domain->id,
1698 1699 1700 1701 1702 1703 1704
                                              (char *) attr);
            if (xref != NULL) {
                strcpy(ref, xref);
                free(xref);
                goto cleanup;
            }

1705 1706 1707
            goto error;
        }
    }
1708
 error:
1709
    ret = -1;
1710
 cleanup:
1711 1712 1713 1714 1715 1716 1717 1718
    if (xml != NULL)
        xmlFreeDoc(xml);
    if (attr != NULL)
        xmlFree(attr);
    return ret;
}
#endif /* !PROXY */

1719 1720 1721 1722 1723 1724 1725 1726
/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */