xml.c 43.8 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
                if (vncport != NULL) {
250
                    long port = strtol((const char *)vncport, NULL, 10);
251 252 253 254
                    if (port == -1)
                        virBufferAdd(buf, "(vncunused 1)", 13);
                    else if (port > 5900)
                        virBufferVSprintf(buf, "(vncdisplay %d)", port - 5900);
255
                    xmlFree(vncport);
256
                }
257 258 259 260 261 262 263 264
                if (vnclisten != NULL) {
                    virBufferVSprintf(buf, "(vnclisten %s)", vnclisten);
                    xmlFree(vnclisten);
                }
                if (vncpasswd != NULL) {
                    virBufferVSprintf(buf, "(vncpasswd %s)", vncpasswd);
                    xmlFree(vncpasswd);
                }
265 266
            }
        }
267 268 269 270 271 272
        xmlFree(graphics_type);
    }
    return 0;
}


273 274
/**
 * virtDomainParseXMLGraphicsDescVFB:
275
 * @conn: pointer to the hypervisor connection
276 277 278
 * @node: node containing graphics description
 * @buf: a buffer for the result S-Expr
 *
279 280 281
 * 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
282 283 284 285
 * valid over time.
 *
 * Returns 0 in case of success, -1 in case of error
 */
286
static int virDomainParseXMLGraphicsDescVFB(virConnectPtr conn ATTRIBUTE_UNUSED, xmlNodePtr node, virBufferPtr buf)
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
{
    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;
}


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

    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) &&
365
                    (txt->next == NULL))
366 367 368 369 370
                    type = txt->content;
            } else if ((loader == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "loader"))) {
                txt = cur->children;
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
371
                    (txt->next == NULL))
372 373 374 375 376 377 378 379 380 381
                    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 */
382
        virXMLError(conn, VIR_ERR_OS_TYPE, (const char *) type, 0);
383 384 385 386
        return (-1);
    }
    virBufferAdd(buf, "(image (hvm ", 12);
    if (loader == NULL) {
387
        virXMLError(conn, VIR_ERR_NO_KERNEL, NULL, 0);
388
        goto error;
389
    } else {
390
        virBufferVSprintf(buf, "(kernel '%s')", (const char *) loader);
391 392 393 394 395 396
    }

    /* 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)) {
397
        virXMLError(conn, VIR_ERR_NO_KERNEL, NULL, 0); /* TODO: error */
398 399 400 401 402 403 404
        goto error;
    }
    virBufferVSprintf(buf, "(device_model '%s')",
                      (const char *) obj->stringval);
    xmlXPathFreeObject(obj);
    obj = NULL;

405 406
    virBufferVSprintf(buf, "(vcpus %d)", vcpus);

407
    if (boot_dev) {
408 409 410 411 412 413 414 415
        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 */
416
            virXMLError(conn, VIR_ERR_XML_ERROR, NULL, 0);
417 418 419 420 421 422 423 424 425 426 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
        }

        /* 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;
493 494 495 496
    }

    obj = xmlXPathEval(BAD_CAST "count(domain/devices/console) > 0", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_BOOLEAN)) {
497
        virXMLError(conn, VIR_ERR_XML_ERROR, NULL, 0);
498
        goto error;
499 500
    }
    if (obj->boolval) {
501
        virBufferAdd(buf, "(serial pty)", 12);
502 503 504
    }
    xmlXPathFreeObject(obj);
    obj = NULL;
505

506 507 508
    /* Is a graphics device specified? */
    obj = xmlXPathEval(BAD_CAST "/domain/devices/graphics[1]", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
509
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr > 0)) {
510
        res = virDomainParseXMLGraphicsDescImage(conn, obj->nodesetval->nodeTab[0], buf, xendConfigVersion);
511 512
        if (res != 0) {
            goto error;
513 514 515 516 517 518
        }
    }
    xmlXPathFreeObject(obj);

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

519 520 521
    if (boot_dev)
        xmlFree(boot_dev);

522
    return (0);
523
 error:
524 525
    if (boot_dev)
        xmlFree(boot_dev);
526 527 528 529 530 531 532
    if (obj != NULL)
        xmlXPathFreeObject(obj);
    return(-1);
}

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

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

    /* Is a graphics device specified? */
615 616 617 618 619
    /* 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)) {
620
            res = virDomainParseXMLGraphicsDescImage(conn, obj->nodesetval->nodeTab[0], buf, xendConfigVersion);
621 622 623
            if (res != 0) {
                goto error;
            }
624
        }
625
        xmlXPathFreeObject(obj);
626 627 628
    }

 error:
629
    virBufferAdd(buf, "))", 2);
630
    return (0);
631 632
}

633 634 635 636 637 638 639 640 641 642
/**
 * 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
643
virCatchXMLParseError(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
644 645
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;

646
    if ((ctxt != NULL) &&
647
        (ctxt->lastError.level == XML_ERR_FATAL) &&
648
        (ctxt->lastError.message != NULL)) {
649
        virXMLError(NULL, VIR_ERR_XML_DETAIL, ctxt->lastError.message,
650
                    ctxt->lastError.line);
651 652 653
    }
}

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

    type = xmlGetProp(node, BAD_CAST "type");
    if (type != NULL) {
685 686 687 688 689
        if (xmlStrEqual(type, BAD_CAST "file"))
            typ = 0;
        else if (xmlStrEqual(type, BAD_CAST "block"))
            typ = 1;
        xmlFree(type);
690
    }
691
    device = xmlGetProp(node, BAD_CAST "device");
692

693 694 695
    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
696 697 698 699 700 701 702 703 704 705
            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");
706 707 708 709 710
            } 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");
711 712
            } else if (xmlStrEqual(cur->name, BAD_CAST "readonly")) {
                ro = 1;
713
            } else if (xmlStrEqual(cur->name, BAD_CAST "shareable")) {
714
                shareable = 1;
715 716
            }
        }
717 718 719 720
        cur = cur->next;
    }

    if (source == NULL) {
721
        virXMLError(conn, VIR_ERR_NO_SOURCE, (const char *) target, 0);
722 723 724

        if (target != NULL)
            xmlFree(target);
725 726
        if (device != NULL)
            xmlFree(device);
727
        return (-1);
728 729
    }
    if (target == NULL) {
730
        virXMLError(conn, VIR_ERR_NO_TARGET, (const char *) source, 0);
731 732
        if (source != NULL)
            xmlFree(source);
733 734
        if (device != NULL)
            xmlFree(device);
735
        return (-1);
736
    }
737

738 739
    /* Xend (all versions) put the floppy device config
     * under the hvm (image (os)) block
740
     */
741
    if (hvm &&
742
        device &&
743
        !strcmp((const char *)device, "floppy")) {
744
        goto cleanup;
745 746 747
    }

    /* Xend <= 3.0.2 doesn't include cdrom config here */
748
    if (hvm &&
749 750
        device &&
        !strcmp((const char *)device, "cdrom")) {
751
        if (xendConfigVersion == 1)
752
            goto cleanup;
753 754
        else
            cdrom = 1;
755 756 757 758
    }


    virBufferAdd(buf, "(device ", 8);
759 760 761 762 763 764 765 766
    /* 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);
    }
767

768 769
    if (hvm) {
        char *tmp = (char *)target;
770
        /* Just in case user mistakenly still puts ioemu: in their XML */
771 772
        if (!strncmp((const char *) tmp, "ioemu:", 6))
            tmp += 6;
773 774 775

        /* Xend <= 3.0.2 wants a ioemu: prefix on devices for HVM */
        if (xendConfigVersion == 1)
776
            virBufferVSprintf(buf, "(dev 'ioemu:%s')", (const char *)tmp);
777
        else /* But newer does not */
778
            virBufferVSprintf(buf, "(dev '%s%s')", (const char *)tmp, cdrom ? ":cdrom" : ":disk");
779
    } else
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
        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);
        }
802
    }
803
    if (ro == 1)
804
        virBufferVSprintf(buf, "(mode 'r')");
805 806 807 808
    else if (shareable == 1)
        virBufferVSprintf(buf, "(mode 'w!')");
    else
        virBufferVSprintf(buf, "(mode 'w')");
809

810
    virBufferAdd(buf, ")", 1);
811
    virBufferAdd(buf, ")", 1);
812 813

 cleanup:
814 815
    xmlFree(drvType);
    xmlFree(drvName);
816
    xmlFree(device);
817 818
    xmlFree(target);
    xmlFree(source);
819
    return (0);
820 821 822 823
}

/**
 * virDomainParseXMLIfDesc:
824
 * @conn: pointer to the hypervisor connection
825
 * @node: node containing the interface description
826 827 828 829 830 831 832 833 834 835
 * @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
836
virDomainParseXMLIfDesc(virConnectPtr conn ATTRIBUTE_UNUSED, xmlNodePtr node, virBufferPtr buf, int hvm)
837
{
838 839 840 841 842
    xmlNodePtr cur;
    xmlChar *type = NULL;
    xmlChar *source = NULL;
    xmlChar *mac = NULL;
    xmlChar *script = NULL;
843
    xmlChar *ip = NULL;
844 845 846 847
    int typ = 0;

    type = xmlGetProp(node, BAD_CAST "type");
    if (type != NULL) {
848 849 850 851 852
        if (xmlStrEqual(type, BAD_CAST "bridge"))
            typ = 0;
        else if (xmlStrEqual(type, BAD_CAST "ethernet"))
            typ = 1;
        xmlFree(type);
853 854 855 856
    }
    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
857 858 859 860 861 862 863 864 865 866 867 868
            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");
869 870 871 872 873 874 875
            } 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");
876 877
            }
        }
878 879 880 881 882
        cur = cur->next;
    }

    virBufferAdd(buf, "(vif ", 5);
    if (mac != NULL)
883
        virBufferVSprintf(buf, "(mac '%s')", (const char *) mac);
884
    if (source != NULL) {
885 886 887 888
        if (typ == 0)
            virBufferVSprintf(buf, "(bridge '%s')", (const char *) source);
        else                    /* TODO does that work like that ? */
            virBufferVSprintf(buf, "(dev '%s')", (const char *) source);
889 890 891
    }
    if (script != NULL)
        virBufferVSprintf(buf, "(script '%s')", script);
892 893
    if (ip != NULL)
        virBufferVSprintf(buf, "(ip '%s')", ip);
894 895
    if (hvm)
        virBufferAdd(buf, "(type ioemu)", 12);
896 897 898

    virBufferAdd(buf, ")", 1);
    if (mac != NULL)
899
        xmlFree(mac);
900
    if (source != NULL)
901
        xmlFree(source);
902
    if (script != NULL)
903
        xmlFree(script);
904 905
    if (ip != NULL)
        xmlFree(ip);
906
    return (0);
907 908 909 910
}

/**
 * virDomainParseXMLDesc:
911
 * @conn: pointer to the hypervisor connection
912
 * @xmldesc: string with the XML description
913
 * @xendConfigVersion: xend configuration file format
914 915 916 917 918 919 920 921 922 923
 *
 * 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 *
924
virDomainParseXMLDesc(virConnectPtr conn, const char *xmldesc, char **name, int xendConfigVersion)
925
{
926 927
    xmlDocPtr xml = NULL;
    xmlNodePtr node;
928
    char *ret = NULL, *nam = NULL;
929 930
    virBuffer buf;
    xmlChar *prop;
931
    xmlParserCtxtPtr pctxt;
932
    xmlXPathObjectPtr obj = NULL;
933
    xmlXPathObjectPtr tmpobj = NULL;
934 935
    xmlXPathContextPtr ctxt = NULL;
    int i, res;
936
    int bootloader = 0;
937
    int hvm = 0;
938
    unsigned int vcpus = 1;
939
    unsigned long mem = 0, max_mem = 0;
940 941

    if (name != NULL)
942
        *name = NULL;
943 944
    ret = malloc(1000);
    if (ret == NULL)
945
        return (NULL);
946 947 948 949
    buf.content = ret;
    buf.size = 1000;
    buf.use = 0;

950 951 952 953 954
    pctxt = xmlNewParserCtxt();
    if ((pctxt == NULL) || (pctxt->sax == NULL)) {
        goto error;
    }

955 956 957
    /* TODO pass the connection point to the error handler:
     *   pctxt->userData = virConnectPtr;
     */
958 959 960 961 962
    pctxt->sax->error = virCatchXMLParseError;

    xml = xmlCtxtReadDoc(pctxt, (const xmlChar *) xmldesc, "domain.xml", NULL,
                         XML_PARSE_NOENT | XML_PARSE_NONET |
                         XML_PARSE_NOWARNING);
963 964 965 966 967 968 969 970 971 972
    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")) {
973 974 975 976
            xmlFree(prop);
            goto error;
        }
        xmlFree(prop);
977 978 979 980 981 982 983
    }
    virBufferAdd(&buf, "(vm ", 4);
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
        goto error;
    }
    /*
984
     * extract some of the basics, name, memory, cpus ...
985 986
     */
    obj = xmlXPathEval(BAD_CAST "string(/domain/name[1])", ctxt);
987
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
988
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
989
        virXMLError(conn, VIR_ERR_NO_NAME, xmldesc, 0);
990 991 992
        goto error;
    }
    virBufferVSprintf(&buf, "(name '%s')", obj->stringval);
993 994
    nam = strdup((const char *) obj->stringval);
    if (nam == NULL) {
995
        virXMLError(conn, VIR_ERR_NO_MEMORY, "copying name", 0);
996
        goto error;
997
    }
998 999 1000 1001
    xmlXPathFreeObject(obj);

    obj = xmlXPathEval(BAD_CAST "number(/domain/memory[1])", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NUMBER) ||
1002
        (isnan(obj->floatval)) || (obj->floatval < 64000)) {
1003
        max_mem = 128;
1004
    } else {
1005 1006 1007 1008 1009 1010 1011 1012 1013
        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);
1014 1015 1016
        if (mem > max_mem) {
            max_mem = mem;
        }
1017 1018
    }
    xmlXPathFreeObject(obj);
1019
    virBufferVSprintf(&buf, "(memory %lu)(maxmem %lu)", mem, max_mem);
1020 1021

    obj = xmlXPathEval(BAD_CAST "number(/domain/vcpu[1])", ctxt);
1022 1023 1024
    if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
        (!isnan(obj->floatval)) && (obj->floatval > 0)) {
        vcpus = (unsigned int) obj->floatval;
1025
    }
1026
    virBufferVSprintf(&buf, "(vcpus %u)", vcpus);
1027 1028
    xmlXPathFreeObject(obj);

1029
    obj = xmlXPathEval(BAD_CAST "string(/domain/uuid[1])", ctxt);
1030
    if ((obj == NULL) || ((obj->type == XPATH_STRING) &&
1031
                          (obj->stringval != NULL) && (obj->stringval[0] != 0))) {
1032 1033 1034 1035
        virBufferVSprintf(&buf, "(uuid '%s')", obj->stringval);
    }
    xmlXPathFreeObject(obj);

1036 1037 1038
    obj = xmlXPathEval(BAD_CAST "string(/domain/bootloader[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
1039 1040 1041 1042 1043 1044 1045 1046 1047
        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;
1048 1049 1050
    }
    xmlXPathFreeObject(obj);

1051 1052 1053
    obj = xmlXPathEval(BAD_CAST "string(/domain/on_poweroff[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
1054
        virBufferVSprintf(&buf, "(on_poweroff '%s')", obj->stringval);
1055 1056 1057 1058 1059 1060
    }
    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)) {
1061
        virBufferVSprintf(&buf, "(on_reboot '%s')", obj->stringval);
1062 1063 1064 1065 1066 1067
    }
    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)) {
1068
        virBufferVSprintf(&buf, "(on_crash '%s')", obj->stringval);
1069 1070 1071
    }
    xmlXPathFreeObject(obj);

1072
    if (bootloader != 2) {
1073 1074 1075 1076 1077 1078 1079 1080 1081
        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);
1082
                virXMLError(conn, VIR_ERR_OS_TYPE, nam, 0);
1083 1084 1085 1086 1087
                goto error;
            }

            if ((tmpobj == NULL)
                || !xmlStrEqual(tmpobj->stringval, BAD_CAST "hvm")) {
1088
                res = virDomainParseXMLOSDescPV(conn, obj->nodesetval->nodeTab[0],
1089 1090 1091
                                                &buf, ctxt, xendConfigVersion);
            } else {
                hvm = 1;
1092
                res = virDomainParseXMLOSDescHVM(conn, obj->nodesetval->nodeTab[0],
1093 1094 1095 1096 1097 1098 1099 1100
                                                 &buf, ctxt, vcpus, xendConfigVersion);
            }

            xmlXPathFreeObject(tmpobj);

            if (res != 0)
                goto error;
        } else if (bootloader == 0) {
1101
            virXMLError(conn, VIR_ERR_NO_OS, nam, 0);
1102 1103 1104
            goto error;
        }
        xmlXPathFreeObject(obj);
1105 1106 1107 1108
    }

    /* analyze of the devices */
    obj = xmlXPathEval(BAD_CAST "/domain/devices/disk", ctxt);
1109 1110
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
1111
        for (i = 0; i < obj->nodesetval->nodeNr; i++) {
1112
            res = virDomainParseXMLDiskDesc(conn, obj->nodesetval->nodeTab[i], &buf, hvm, xendConfigVersion);
1113 1114 1115 1116
            if (res != 0) {
                goto error;
            }
        }
1117 1118
    }
    xmlXPathFreeObject(obj);
1119

1120 1121 1122
    obj = xmlXPathEval(BAD_CAST "/domain/devices/interface", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
1123 1124
        for (i = 0; i < obj->nodesetval->nodeNr; i++) {
            virBufferAdd(&buf, "(device ", 8);
1125
            res = virDomainParseXMLIfDesc(conn, obj->nodesetval->nodeTab[i], &buf, hvm);
1126 1127 1128 1129 1130
            if (res != 0) {
                goto error;
            }
            virBufferAdd(&buf, ")", 1);
        }
1131 1132 1133
    }
    xmlXPathFreeObject(obj);

1134 1135 1136 1137 1138 1139
    /* 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++) {
1140
                res = virDomainParseXMLGraphicsDescVFB(conn, obj->nodesetval->nodeTab[i], &buf);
1141 1142 1143 1144 1145 1146 1147 1148
                if (res != 0) {
                    goto error;
                }
            }
        }
        xmlXPathFreeObject(obj);
    }

1149

D
Daniel Veillard 已提交
1150
    virBufferAdd(&buf, ")", 1); /* closes (vm */
1151 1152 1153 1154
    buf.content[buf.use] = 0;

    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
1155
    xmlFreeParserCtxt(pctxt);
1156 1157

    if (name != NULL)
1158
        *name = nam;
1159 1160
    else
        free(nam);
1161

1162 1163
    return (ret);

1164
 error:
1165
    if (nam != NULL)
1166
        free(nam);
1167
    if (name != NULL)
1168
        *name = NULL;
1169 1170 1171 1172 1173 1174
    if (obj != NULL)
        xmlXPathFreeObject(obj);
    if (ctxt != NULL)
        xmlXPathFreeContext(ctxt);
    if (xml != NULL)
        xmlFreeDoc(xml);
1175 1176
    if (pctxt != NULL)
        xmlFreeParserCtxt(pctxt);
1177 1178
    if (ret != NULL)
        free(ret);
1179
    return (NULL);
1180
}
1181 1182

#endif /* !PROXY */
1183 1184 1185 1186



unsigned char *virParseUUID(char **ptr, const char *uuid) {
1187
    int rawuuid[VIR_UUID_BUFLEN];
1188
    const char *cur;
1189 1190 1191 1192 1193 1194
    unsigned char *dst_uuid = NULL;
    int i;

    if (uuid == NULL)
        goto error;

1195 1196 1197 1198 1199
    /*
     * do a liberal scan allowing '-' and ' ' anywhere between character
     * pairs as long as there is 32 of them in the end.
     */
    cur = uuid;
1200
    for (i = 0;i < VIR_UUID_BUFLEN;) {
1201 1202
        rawuuid[i] = 0;
        if (*cur == 0)
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
            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++;
1218
        if (*cur == 0)
1219 1220 1221 1222 1223 1224 1225 1226 1227
            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;
1228
        i++;
1229
        cur++;
1230
    }
1231 1232 1233 1234

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

1235
    for (i = 0; i < VIR_UUID_BUFLEN; i++)
1236 1237
        dst_uuid[i] = rawuuid[i] & 0xFF;

1238
 error:
1239
    return(dst_uuid);
1240
}
1241

1242 1243 1244
#ifndef PROXY
/**
 * virParseXMLDevice:
1245
 * @conn: pointer to the hypervisor connection
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
 * @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 *
1259
virParseXMLDevice(virConnectPtr conn, char *xmldesc, int hvm, int xendConfigVersion)
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
{
    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")) {
1279
        if (virDomainParseXMLDiskDesc(conn, node, &buf, hvm, xendConfigVersion) != 0)
1280 1281 1282
            goto error;
    }
    else if (xmlStrEqual(node->name, BAD_CAST "interface")) {
1283
        if (virDomainParseXMLIfDesc(conn, node, &buf, hvm) != 0)
1284 1285
            goto error;
    }
1286
 cleanup:
1287 1288 1289
    if (xml != NULL)
        xmlFreeDoc(xml);
    return buf.content;
1290
 error:
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
    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;
1316
    char *xref;
1317
    int ret = 0;
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330

    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) ||
1331
                (!xmlStrEqual(cur->name, BAD_CAST "target"))) continue;
1332 1333 1334
            attr = xmlGetProp(cur, BAD_CAST "dev");
            if (attr == NULL)
                goto error;
1335
            strcpy(ref, (char *)attr);
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
            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;

1348
            xref = xenStoreDomainGetNetworkID(domain->conn, domain->id,
1349 1350 1351 1352 1353 1354 1355
                                              (char *) attr);
            if (xref != NULL) {
                strcpy(ref, xref);
                free(xref);
                goto cleanup;
            }

1356 1357 1358
            goto error;
        }
    }
1359
 error:
1360
    ret = -1;
1361
 cleanup:
1362 1363 1364 1365 1366 1367 1368 1369
    if (xml != NULL)
        xmlFreeDoc(xml);
    if (attr != NULL)
        xmlFree(attr);
    return ret;
}
#endif /* !PROXY */

1370 1371 1372 1373 1374 1375 1376 1377
/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */