xml.c 44.9 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
virXMLError(virConnectPtr conn, virErrorNumber error, const char *info, int value)
30
{
31
    const char *errmsg;
32

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

    errmsg = __virErrorMsg(error, info);
37
    __virRaiseError(conn, 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(NULL, 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(NULL, 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(NULL, 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

213
#ifndef PROXY
214
/**
215
 * virtDomainParseXMLGraphicsDescImage:
216
 * @conn: pointer to the hypervisor connection
217 218
 * @node: node containing graphics description
 * @buf: a buffer for the result S-Expr
219
 * @xendConfigVersion: xend configuration file format
220
 *
221 222 223
 * 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
224 225 226 227
 * valid over time.
 *
 * Returns 0 in case of success, -1 in case of error
 */
228
static int virDomainParseXMLGraphicsDescImage(virConnectPtr conn ATTRIBUTE_UNUSED, xmlNodePtr node, virBufferPtr buf, int xendConfigVersion)
229 230 231 232 233 234 235
{
    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);
236 237 238 239 240 241
            /* TODO:
             * Need to understand sdl options
             *
             *virBufferAdd(buf, "(display localhost:10.0)", 24);
             *virBufferAdd(buf, "(xauthority /root/.Xauthority)", 30);
             */
242
        }
243
        else if (xmlStrEqual(graphics_type, BAD_CAST "vnc")) {
244
            virBufferAdd(buf, "(vnc 1)", 7);
245
            if (xendConfigVersion >= 2) {
246
                xmlChar *vncport = xmlGetProp(node, BAD_CAST "port");
247 248
                xmlChar *vnclisten = xmlGetProp(node, BAD_CAST "listen");
                xmlChar *vncpasswd = xmlGetProp(node, BAD_CAST "passwd");
249
                xmlChar *keymap = xmlGetProp(node, BAD_CAST "keymap");
250
                if (vncport != NULL) {
251
                    long port = strtol((const char *)vncport, NULL, 10);
252 253 254 255
                    if (port == -1)
                        virBufferAdd(buf, "(vncunused 1)", 13);
                    else if (port > 5900)
                        virBufferVSprintf(buf, "(vncdisplay %d)", port - 5900);
256
                    xmlFree(vncport);
257
                }
258 259 260 261 262 263 264 265
                if (vnclisten != NULL) {
                    virBufferVSprintf(buf, "(vnclisten %s)", vnclisten);
                    xmlFree(vnclisten);
                }
                if (vncpasswd != NULL) {
                    virBufferVSprintf(buf, "(vncpasswd %s)", vncpasswd);
                    xmlFree(vncpasswd);
                }
266 267 268 269
                if (keymap != NULL) {
                    virBufferVSprintf(buf, "(keymap %s)", keymap);
                    xmlFree(keymap);
                }
270 271
            }
        }
272 273 274 275 276 277
        xmlFree(graphics_type);
    }
    return 0;
}


278 279
/**
 * virtDomainParseXMLGraphicsDescVFB:
280
 * @conn: pointer to the hypervisor connection
281 282 283
 * @node: node containing graphics description
 * @buf: a buffer for the result S-Expr
 *
284 285 286
 * 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
287 288 289 290
 * valid over time.
 *
 * Returns 0 in case of success, -1 in case of error
 */
291
static int virDomainParseXMLGraphicsDescVFB(virConnectPtr conn ATTRIBUTE_UNUSED, xmlNodePtr node, virBufferPtr buf)
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
{
    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");
313
            xmlChar *keymap = xmlGetProp(node, BAD_CAST "keymap");
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
            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);
            }
330 331 332 333
            if (keymap != NULL) {
                virBufferVSprintf(buf, "(keymap %s)", keymap);
                xmlFree(keymap);
            }
334 335 336 337 338 339 340 341
        }
        virBufferAdd(buf, "))", 2);
        xmlFree(graphics_type);
    }
    return 0;
}


342
/**
343
 * virDomainParseXMLOSDescHVM:
344
 * @conn: pointer to the hypervisor connection
345
 * @node: node containing HVM OS description
346
 * @buf: a buffer for the result S-Expr
347
 * @ctxt: a path context representing the XML description
348
 * @vcpus: number of virtual CPUs to configure
349
 * @xendConfigVersion: xend configuration file format
350
 *
351 352
 * 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
353 354 355 356 357 358
 * 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
359
virDomainParseXMLOSDescHVM(virConnectPtr conn, xmlNodePtr node, virBufferPtr buf, xmlXPathContextPtr ctxt, int vcpus, int xendConfigVersion)
360 361 362
{
    xmlXPathObjectPtr obj = NULL;
    xmlNodePtr cur, txt;
363 364 365
    xmlChar *type = NULL;
    xmlChar *loader = NULL;
    xmlChar *boot_dev = NULL;
366
    int res;
367 368 369 370 371 372 373 374

    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) &&
375
                    (txt->next == NULL))
376 377 378 379 380
                    type = txt->content;
            } else if ((loader == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "loader"))) {
                txt = cur->children;
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
381
                    (txt->next == NULL))
382 383 384 385 386 387 388 389 390 391
                    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 */
392
        virXMLError(conn, VIR_ERR_OS_TYPE, (const char *) type, 0);
393 394 395 396
        return (-1);
    }
    virBufferAdd(buf, "(image (hvm ", 12);
    if (loader == NULL) {
397
        virXMLError(conn, VIR_ERR_NO_KERNEL, NULL, 0);
398
        goto error;
399
    } else {
400
        virBufferVSprintf(buf, "(kernel '%s')", (const char *) loader);
401 402 403 404 405 406
    }

    /* 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)) {
407
        virXMLError(conn, VIR_ERR_NO_KERNEL, NULL, 0); /* TODO: error */
408 409 410 411 412 413 414
        goto error;
    }
    virBufferVSprintf(buf, "(device_model '%s')",
                      (const char *) obj->stringval);
    xmlXPathFreeObject(obj);
    obj = NULL;

415 416
    virBufferVSprintf(buf, "(vcpus %d)", vcpus);

417
    if (boot_dev) {
418 419 420 421 422 423 424 425
        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 */
426
            virXMLError(conn, VIR_ERR_XML_ERROR, NULL, 0);
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
        }

        /* 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;
503 504 505 506
    }

    obj = xmlXPathEval(BAD_CAST "count(domain/devices/console) > 0", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_BOOLEAN)) {
507
        virXMLError(conn, VIR_ERR_XML_ERROR, NULL, 0);
508
        goto error;
509 510
    }
    if (obj->boolval) {
511
        virBufferAdd(buf, "(serial pty)", 12);
512 513 514
    }
    xmlXPathFreeObject(obj);
    obj = NULL;
515

516 517 518
    /* Is a graphics device specified? */
    obj = xmlXPathEval(BAD_CAST "/domain/devices/graphics[1]", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
519
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr > 0)) {
520
        res = virDomainParseXMLGraphicsDescImage(conn, obj->nodesetval->nodeTab[0], buf, xendConfigVersion);
521 522
        if (res != 0) {
            goto error;
523 524 525 526 527 528
        }
    }
    xmlXPathFreeObject(obj);

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

529 530 531
    if (boot_dev)
        xmlFree(boot_dev);

532
    return (0);
533
 error:
534 535
    if (boot_dev)
        xmlFree(boot_dev);
536 537 538 539 540 541 542
    if (obj != NULL)
        xmlXPathFreeObject(obj);
    return(-1);
}

/**
 * virDomainParseXMLOSDescPV:
543
 * @conn: pointer to the hypervisor connection
544 545
 * @node: node containing PV OS description
 * @buf: a buffer for the result S-Expr
546
 * @ctxt: a path context representing the XML description
547
 * @xendConfigVersion: xend configuration file format
548 549 550 551 552 553 554 555 556
 *
 * 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
557
virDomainParseXMLOSDescPV(virConnectPtr conn, xmlNodePtr node, virBufferPtr buf, xmlXPathContextPtr ctxt, int xendConfigVersion)
558
{
559
    xmlNodePtr cur, txt;
560
    xmlXPathObjectPtr obj = NULL;
561 562 563 564 565
    const xmlChar *type = NULL;
    const xmlChar *root = NULL;
    const xmlChar *kernel = NULL;
    const xmlChar *initrd = NULL;
    const xmlChar *cmdline = NULL;
566
    int res;
567 568 569 570

    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
571 572 573
            if ((type == NULL)
                && (xmlStrEqual(cur->name, BAD_CAST "type"))) {
                txt = cur->children;
574
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
575
                    (txt->next == NULL))
576 577 578 579
                    type = txt->content;
            } else if ((kernel == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "kernel"))) {
                txt = cur->children;
580
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
581
                    (txt->next == NULL))
582 583 584 585
                    kernel = txt->content;
            } else if ((root == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "root"))) {
                txt = cur->children;
586
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
587
                    (txt->next == NULL))
588 589 590 591
                    root = txt->content;
            } else if ((initrd == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "initrd"))) {
                txt = cur->children;
592
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
593
                    (txt->next == NULL))
594 595 596 597
                    initrd = txt->content;
            } else if ((cmdline == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "cmdline"))) {
                txt = cur->children;
598
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
599
                    (txt->next == NULL))
600 601 602
                    cmdline = txt->content;
            }
        }
603 604 605 606
        cur = cur->next;
    }
    if ((type != NULL) && (!xmlStrEqual(type, BAD_CAST "linux"))) {
        /* VIR_ERR_OS_TYPE */
607
        virXMLError(conn, VIR_ERR_OS_TYPE, (const char *) type, 0);
608
        return (-1);
609
    }
610
    virBufferAdd(buf, "(image (linux ", 14);
611
    if (kernel == NULL) {
612
        virXMLError(conn, VIR_ERR_NO_KERNEL, NULL, 0);
613
        return (-1);
614
    } else {
615
        virBufferVSprintf(buf, "(kernel '%s')", (const char *) kernel);
616 617
    }
    if (initrd != NULL)
618
        virBufferVSprintf(buf, "(ramdisk '%s')", (const char *) initrd);
619
    if (root != NULL)
620
        virBufferVSprintf(buf, "(root '%s')", (const char *) root);
621
    if (cmdline != NULL)
622
        virBufferVSprintf(buf, "(args '%s')", (const char *) cmdline);
623 624

    /* Is a graphics device specified? */
625 626 627 628 629
    /* 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)) {
630
            res = virDomainParseXMLGraphicsDescImage(conn, obj->nodesetval->nodeTab[0], buf, xendConfigVersion);
631 632 633
            if (res != 0) {
                goto error;
            }
634
        }
635
        xmlXPathFreeObject(obj);
636 637 638
    }

 error:
639
    virBufferAdd(buf, "))", 2);
640
    return (0);
641 642
}

643 644 645 646 647 648 649 650 651 652
/**
 * 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
653
virCatchXMLParseError(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
654 655
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;

656
    if ((ctxt != NULL) &&
657
        (ctxt->lastError.level == XML_ERR_FATAL) &&
658
        (ctxt->lastError.message != NULL)) {
659
        virXMLError(NULL, VIR_ERR_XML_DETAIL, ctxt->lastError.message,
660
                    ctxt->lastError.line);
661 662 663
    }
}

664 665
/**
 * virDomainParseXMLDiskDesc:
666
 * @node: node containing disk description
667
 * @conn: pointer to the hypervisor connection
668
 * @buf: a buffer for the result S-Expr
669
 * @xendConfigVersion: xend configuration file format
670 671 672 673 674 675 676 677 678
 *
 * 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
679
virDomainParseXMLDiskDesc(virConnectPtr conn, xmlNodePtr node, virBufferPtr buf, int hvm, int xendConfigVersion)
680
{
681 682
    xmlNodePtr cur;
    xmlChar *type = NULL;
683
    xmlChar *device = NULL;
684 685
    xmlChar *source = NULL;
    xmlChar *target = NULL;
686 687
    xmlChar *drvName = NULL;
    xmlChar *drvType = NULL;
688
    int ro = 0;
689
    int shareable = 0;
690
    int typ = 0;
691
    int cdrom = 0;
692 693 694

    type = xmlGetProp(node, BAD_CAST "type");
    if (type != NULL) {
695 696 697 698 699
        if (xmlStrEqual(type, BAD_CAST "file"))
            typ = 0;
        else if (xmlStrEqual(type, BAD_CAST "block"))
            typ = 1;
        xmlFree(type);
700
    }
701
    device = xmlGetProp(node, BAD_CAST "device");
702

703 704 705
    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
706 707 708 709 710 711 712 713 714 715
            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");
716 717 718 719 720
            } 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");
721 722
            } else if (xmlStrEqual(cur->name, BAD_CAST "readonly")) {
                ro = 1;
723
            } else if (xmlStrEqual(cur->name, BAD_CAST "shareable")) {
724
                shareable = 1;
725 726
            }
        }
727 728 729 730
        cur = cur->next;
    }

    if (source == NULL) {
731
        virXMLError(conn, VIR_ERR_NO_SOURCE, (const char *) target, 0);
732 733 734

        if (target != NULL)
            xmlFree(target);
735 736
        if (device != NULL)
            xmlFree(device);
737
        return (-1);
738 739
    }
    if (target == NULL) {
740
        virXMLError(conn, VIR_ERR_NO_TARGET, (const char *) source, 0);
741 742
        if (source != NULL)
            xmlFree(source);
743 744
        if (device != NULL)
            xmlFree(device);
745
        return (-1);
746
    }
747

748 749
    /* Xend (all versions) put the floppy device config
     * under the hvm (image (os)) block
750
     */
751
    if (hvm &&
752
        device &&
753
        !strcmp((const char *)device, "floppy")) {
754
        goto cleanup;
755 756 757
    }

    /* Xend <= 3.0.2 doesn't include cdrom config here */
758
    if (hvm &&
759 760
        device &&
        !strcmp((const char *)device, "cdrom")) {
761
        if (xendConfigVersion == 1)
762
            goto cleanup;
763 764
        else
            cdrom = 1;
765 766 767 768
    }


    virBufferAdd(buf, "(device ", 8);
769 770 771 772 773 774 775 776
    /* 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);
    }
777

778 779
    if (hvm) {
        char *tmp = (char *)target;
780
        /* Just in case user mistakenly still puts ioemu: in their XML */
781 782
        if (!strncmp((const char *) tmp, "ioemu:", 6))
            tmp += 6;
783 784 785

        /* Xend <= 3.0.2 wants a ioemu: prefix on devices for HVM */
        if (xendConfigVersion == 1)
786
            virBufferVSprintf(buf, "(dev 'ioemu:%s')", (const char *)tmp);
787
        else /* But newer does not */
788
            virBufferVSprintf(buf, "(dev '%s%s')", (const char *)tmp, cdrom ? ":cdrom" : ":disk");
789
    } else
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
        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);
        }
812
    }
813
    if (ro == 1)
814
        virBufferVSprintf(buf, "(mode 'r')");
815 816 817 818
    else if (shareable == 1)
        virBufferVSprintf(buf, "(mode 'w!')");
    else
        virBufferVSprintf(buf, "(mode 'w')");
819

820
    virBufferAdd(buf, ")", 1);
821
    virBufferAdd(buf, ")", 1);
822 823

 cleanup:
824 825
    xmlFree(drvType);
    xmlFree(drvName);
826
    xmlFree(device);
827 828
    xmlFree(target);
    xmlFree(source);
829
    return (0);
830 831 832 833
}

/**
 * virDomainParseXMLIfDesc:
834
 * @conn: pointer to the hypervisor connection
835
 * @node: node containing the interface description
836 837 838 839 840 841 842 843 844 845
 * @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
846
virDomainParseXMLIfDesc(virConnectPtr conn ATTRIBUTE_UNUSED, xmlNodePtr node, virBufferPtr buf, int hvm)
847
{
848 849 850 851 852
    xmlNodePtr cur;
    xmlChar *type = NULL;
    xmlChar *source = NULL;
    xmlChar *mac = NULL;
    xmlChar *script = NULL;
853
    xmlChar *ip = NULL;
854
    int typ = 0;
855
    int ret = -1;
856 857 858

    type = xmlGetProp(node, BAD_CAST "type");
    if (type != NULL) {
859 860 861 862
        if (xmlStrEqual(type, BAD_CAST "bridge"))
            typ = 0;
        else if (xmlStrEqual(type, BAD_CAST "ethernet"))
            typ = 1;
863 864
        else if (xmlStrEqual(type, BAD_CAST "network"))
            typ = 2;
865
        xmlFree(type);
866 867 868 869
    }
    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
870 871 872 873
            if ((source == NULL) &&
                (xmlStrEqual(cur->name, BAD_CAST "source"))) {
                if (typ == 0)
                    source = xmlGetProp(cur, BAD_CAST "bridge");
874
                else if (typ == 1)
875
                    source = xmlGetProp(cur, BAD_CAST "dev");
876 877
                else
                    source = xmlGetProp(cur, BAD_CAST "network");
878 879 880 881 882 883
            } 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");
884 885 886 887 888 889 890
            } 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");
891 892
            }
        }
893 894 895 896 897
        cur = cur->next;
    }

    virBufferAdd(buf, "(vif ", 5);
    if (mac != NULL)
898
        virBufferVSprintf(buf, "(mac '%s')", (const char *) mac);
899
    if (source != NULL) {
900 901
        if (typ == 0)
            virBufferVSprintf(buf, "(bridge '%s')", (const char *) source);
902
        else if (typ == 1)      /* TODO does that work like that ? */
903
            virBufferVSprintf(buf, "(dev '%s')", (const char *) source);
904 905 906 907 908 909 910 911 912 913
        else {
            virNetworkPtr network = virNetworkLookupByName(conn, (const char *) source);
            char *bridge;
            if (!network || !(bridge = virNetworkGetBridgeName(network))) {
                virXMLError(conn, VIR_ERR_NO_SOURCE, (const char *) source, 0);
                goto error;
            }
            virBufferVSprintf(buf, "(bridge '%s')", bridge);
            free(bridge);
        }
914 915 916
    }
    if (script != NULL)
        virBufferVSprintf(buf, "(script '%s')", script);
917 918
    if (ip != NULL)
        virBufferVSprintf(buf, "(ip '%s')", ip);
919 920
    if (hvm)
        virBufferAdd(buf, "(type ioemu)", 12);
921 922

    virBufferAdd(buf, ")", 1);
923 924
    ret = 0;
 error:
925
    if (mac != NULL)
926
        xmlFree(mac);
927
    if (source != NULL)
928
        xmlFree(source);
929
    if (script != NULL)
930
        xmlFree(script);
931 932
    if (ip != NULL)
        xmlFree(ip);
933
    return (ret);
934 935 936 937
}

/**
 * virDomainParseXMLDesc:
938
 * @conn: pointer to the hypervisor connection
939
 * @xmldesc: string with the XML description
940
 * @xendConfigVersion: xend configuration file format
941 942 943 944 945 946 947 948 949 950
 *
 * 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 *
951
virDomainParseXMLDesc(virConnectPtr conn, const char *xmldesc, char **name, int xendConfigVersion)
952
{
953 954
    xmlDocPtr xml = NULL;
    xmlNodePtr node;
955
    char *ret = NULL, *nam = NULL;
956 957
    virBuffer buf;
    xmlChar *prop;
958
    xmlParserCtxtPtr pctxt;
959
    xmlXPathObjectPtr obj = NULL;
960
    xmlXPathObjectPtr tmpobj = NULL;
961 962
    xmlXPathContextPtr ctxt = NULL;
    int i, res;
963
    int bootloader = 0;
964
    int hvm = 0;
965
    unsigned int vcpus = 1;
966
    unsigned long mem = 0, max_mem = 0;
967 968

    if (name != NULL)
969
        *name = NULL;
970 971
    ret = malloc(1000);
    if (ret == NULL)
972
        return (NULL);
973 974 975 976
    buf.content = ret;
    buf.size = 1000;
    buf.use = 0;

977 978 979 980 981
    pctxt = xmlNewParserCtxt();
    if ((pctxt == NULL) || (pctxt->sax == NULL)) {
        goto error;
    }

982 983 984
    /* TODO pass the connection point to the error handler:
     *   pctxt->userData = virConnectPtr;
     */
985 986 987 988 989
    pctxt->sax->error = virCatchXMLParseError;

    xml = xmlCtxtReadDoc(pctxt, (const xmlChar *) xmldesc, "domain.xml", NULL,
                         XML_PARSE_NOENT | XML_PARSE_NONET |
                         XML_PARSE_NOWARNING);
990 991 992 993 994 995 996 997 998 999
    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")) {
1000 1001 1002 1003
            xmlFree(prop);
            goto error;
        }
        xmlFree(prop);
1004 1005 1006 1007 1008 1009 1010
    }
    virBufferAdd(&buf, "(vm ", 4);
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
        goto error;
    }
    /*
1011
     * extract some of the basics, name, memory, cpus ...
1012 1013
     */
    obj = xmlXPathEval(BAD_CAST "string(/domain/name[1])", ctxt);
1014
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
1015
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
1016
        virXMLError(conn, VIR_ERR_NO_NAME, xmldesc, 0);
1017 1018 1019
        goto error;
    }
    virBufferVSprintf(&buf, "(name '%s')", obj->stringval);
1020 1021
    nam = strdup((const char *) obj->stringval);
    if (nam == NULL) {
1022
        virXMLError(conn, VIR_ERR_NO_MEMORY, "copying name", 0);
1023
        goto error;
1024
    }
1025 1026 1027 1028
    xmlXPathFreeObject(obj);

    obj = xmlXPathEval(BAD_CAST "number(/domain/memory[1])", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NUMBER) ||
1029
        (isnan(obj->floatval)) || (obj->floatval < 64000)) {
1030
        max_mem = 128;
1031
    } else {
1032 1033 1034 1035 1036 1037 1038 1039 1040
        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);
1041 1042 1043
        if (mem > max_mem) {
            max_mem = mem;
        }
1044 1045
    }
    xmlXPathFreeObject(obj);
1046
    virBufferVSprintf(&buf, "(memory %lu)(maxmem %lu)", mem, max_mem);
1047 1048

    obj = xmlXPathEval(BAD_CAST "number(/domain/vcpu[1])", ctxt);
1049 1050 1051
    if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
        (!isnan(obj->floatval)) && (obj->floatval > 0)) {
        vcpus = (unsigned int) obj->floatval;
1052
    }
1053
    virBufferVSprintf(&buf, "(vcpus %u)", vcpus);
1054 1055
    xmlXPathFreeObject(obj);

1056
    obj = xmlXPathEval(BAD_CAST "string(/domain/uuid[1])", ctxt);
1057
    if ((obj == NULL) || ((obj->type == XPATH_STRING) &&
1058
                          (obj->stringval != NULL) && (obj->stringval[0] != 0))) {
1059 1060 1061 1062
        virBufferVSprintf(&buf, "(uuid '%s')", obj->stringval);
    }
    xmlXPathFreeObject(obj);

1063 1064 1065
    obj = xmlXPathEval(BAD_CAST "string(/domain/bootloader[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
1066 1067 1068 1069 1070 1071 1072 1073 1074
        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;
1075 1076 1077
    }
    xmlXPathFreeObject(obj);

1078 1079 1080
    obj = xmlXPathEval(BAD_CAST "string(/domain/on_poweroff[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
1081
        virBufferVSprintf(&buf, "(on_poweroff '%s')", obj->stringval);
1082 1083 1084 1085 1086 1087
    }
    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)) {
1088
        virBufferVSprintf(&buf, "(on_reboot '%s')", obj->stringval);
1089 1090 1091 1092 1093 1094
    }
    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)) {
1095
        virBufferVSprintf(&buf, "(on_crash '%s')", obj->stringval);
1096 1097 1098
    }
    xmlXPathFreeObject(obj);

1099
    if (bootloader != 2) {
1100 1101 1102 1103 1104 1105 1106 1107 1108
        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);
1109
                virXMLError(conn, VIR_ERR_OS_TYPE, nam, 0);
1110 1111 1112 1113 1114
                goto error;
            }

            if ((tmpobj == NULL)
                || !xmlStrEqual(tmpobj->stringval, BAD_CAST "hvm")) {
1115
                res = virDomainParseXMLOSDescPV(conn, obj->nodesetval->nodeTab[0],
1116 1117 1118
                                                &buf, ctxt, xendConfigVersion);
            } else {
                hvm = 1;
1119
                res = virDomainParseXMLOSDescHVM(conn, obj->nodesetval->nodeTab[0],
1120 1121 1122 1123 1124 1125 1126 1127
                                                 &buf, ctxt, vcpus, xendConfigVersion);
            }

            xmlXPathFreeObject(tmpobj);

            if (res != 0)
                goto error;
        } else if (bootloader == 0) {
1128
            virXMLError(conn, VIR_ERR_NO_OS, nam, 0);
1129 1130 1131
            goto error;
        }
        xmlXPathFreeObject(obj);
1132 1133 1134 1135
    }

    /* analyze of the devices */
    obj = xmlXPathEval(BAD_CAST "/domain/devices/disk", ctxt);
1136 1137
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
1138
        for (i = 0; i < obj->nodesetval->nodeNr; i++) {
1139
            res = virDomainParseXMLDiskDesc(conn, obj->nodesetval->nodeTab[i], &buf, hvm, xendConfigVersion);
1140 1141 1142 1143
            if (res != 0) {
                goto error;
            }
        }
1144 1145
    }
    xmlXPathFreeObject(obj);
1146

1147 1148 1149
    obj = xmlXPathEval(BAD_CAST "/domain/devices/interface", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
1150 1151
        for (i = 0; i < obj->nodesetval->nodeNr; i++) {
            virBufferAdd(&buf, "(device ", 8);
1152
            res = virDomainParseXMLIfDesc(conn, obj->nodesetval->nodeTab[i], &buf, hvm);
1153 1154 1155 1156 1157
            if (res != 0) {
                goto error;
            }
            virBufferAdd(&buf, ")", 1);
        }
1158 1159 1160
    }
    xmlXPathFreeObject(obj);

1161 1162 1163 1164 1165 1166
    /* 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++) {
1167
                res = virDomainParseXMLGraphicsDescVFB(conn, obj->nodesetval->nodeTab[i], &buf);
1168 1169 1170 1171 1172 1173 1174 1175
                if (res != 0) {
                    goto error;
                }
            }
        }
        xmlXPathFreeObject(obj);
    }

1176

D
Daniel Veillard 已提交
1177
    virBufferAdd(&buf, ")", 1); /* closes (vm */
1178 1179 1180 1181
    buf.content[buf.use] = 0;

    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
1182
    xmlFreeParserCtxt(pctxt);
1183 1184

    if (name != NULL)
1185
        *name = nam;
1186 1187
    else
        free(nam);
1188

1189 1190
    return (ret);

1191
 error:
1192
    if (nam != NULL)
1193
        free(nam);
1194
    if (name != NULL)
1195
        *name = NULL;
1196 1197 1198 1199 1200 1201
    if (obj != NULL)
        xmlXPathFreeObject(obj);
    if (ctxt != NULL)
        xmlXPathFreeContext(ctxt);
    if (xml != NULL)
        xmlFreeDoc(xml);
1202 1203
    if (pctxt != NULL)
        xmlFreeParserCtxt(pctxt);
1204 1205
    if (ret != NULL)
        free(ret);
1206
    return (NULL);
1207
}
1208 1209

#endif /* !PROXY */
1210 1211 1212 1213



unsigned char *virParseUUID(char **ptr, const char *uuid) {
1214
    int rawuuid[VIR_UUID_BUFLEN];
1215
    const char *cur;
1216 1217 1218 1219 1220 1221
    unsigned char *dst_uuid = NULL;
    int i;

    if (uuid == NULL)
        goto error;

1222 1223 1224 1225 1226
    /*
     * do a liberal scan allowing '-' and ' ' anywhere between character
     * pairs as long as there is 32 of them in the end.
     */
    cur = uuid;
1227
    for (i = 0;i < VIR_UUID_BUFLEN;) {
1228 1229
        rawuuid[i] = 0;
        if (*cur == 0)
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
            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++;
1245
        if (*cur == 0)
1246 1247 1248 1249 1250 1251 1252 1253 1254
            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;
1255
        i++;
1256
        cur++;
1257
    }
1258 1259 1260 1261

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

1262
    for (i = 0; i < VIR_UUID_BUFLEN; i++)
1263 1264
        dst_uuid[i] = rawuuid[i] & 0xFF;

1265
 error:
1266
    return(dst_uuid);
1267
}
1268

1269 1270 1271
#ifndef PROXY
/**
 * virParseXMLDevice:
1272
 * @conn: pointer to the hypervisor connection
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
 * @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 *
1286
virParseXMLDevice(virConnectPtr conn, char *xmldesc, int hvm, int xendConfigVersion)
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
{
    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")) {
1306
        if (virDomainParseXMLDiskDesc(conn, node, &buf, hvm, xendConfigVersion) != 0)
1307 1308 1309
            goto error;
    }
    else if (xmlStrEqual(node->name, BAD_CAST "interface")) {
1310
        if (virDomainParseXMLIfDesc(conn, node, &buf, hvm) != 0)
1311 1312
            goto error;
    }
1313
 cleanup:
1314 1315 1316
    if (xml != NULL)
        xmlFreeDoc(xml);
    return buf.content;
1317
 error:
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
    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;
1343
    char *xref;
1344
    int ret = 0;
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357

    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) ||
1358
                (!xmlStrEqual(cur->name, BAD_CAST "target"))) continue;
1359 1360 1361
            attr = xmlGetProp(cur, BAD_CAST "dev");
            if (attr == NULL)
                goto error;
1362
            strcpy(ref, (char *)attr);
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
            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;

1375
            xref = xenStoreDomainGetNetworkID(domain->conn, domain->id,
1376 1377 1378 1379 1380 1381 1382
                                              (char *) attr);
            if (xref != NULL) {
                strcpy(ref, xref);
                free(xref);
                goto cleanup;
            }

1383 1384 1385
            goto error;
        }
    }
1386
 error:
1387
    ret = -1;
1388
 cleanup:
1389 1390 1391 1392 1393 1394 1395 1396
    if (xml != NULL)
        xmlFreeDoc(xml);
    if (attr != NULL)
        xmlFree(attr);
    return ret;
}
#endif /* !PROXY */

1397 1398 1399 1400 1401 1402 1403 1404
/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */