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

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

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

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

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

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

40 41 42 43 44 45 46 47 48 49
/**
 * 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
50 51
virBufferGrow(virBufferPtr buf, unsigned int len)
{
52 53 54
    int size;
    char *newbuf;

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

    size = buf->use + len + 1000;

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

/**
 * 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 已提交
83
int
84 85
virBufferAdd(virBufferPtr buf, const char *str, int len)
{
86 87 88
    unsigned int needSize;

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

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

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

K
Karel Zak 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
virBufferPtr
virBufferNew(unsigned int size)
{
    virBufferPtr buf;

    if (!(buf = malloc(sizeof(*buf)))) {
        virXMLError(VIR_ERR_NO_MEMORY, "allocate new buffer", sizeof(*buf));
        return NULL;
    }
    if (size && (buf->content = malloc(size))==NULL) {
        virXMLError(VIR_ERR_NO_MEMORY, "allocate buffer content", size);
        free(buf);
        return NULL;
    }
    buf->size = size;
    buf->use = 0;

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

140 141 142 143 144 145 146 147 148 149
/**
 * 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 已提交
150
int
151 152
virBufferVSprintf(virBufferPtr buf, const char *format, ...)
{
153 154 155 156
    int size, count;
    va_list locarg, argptr;

    if ((format == NULL) || (buf == NULL)) {
157
        return (-1);
158 159 160 161 162 163
    }
    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)) {
164 165 166 167 168 169 170
        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);
171 172 173 174
    }
    va_end(locarg);
    buf->use += count;
    buf->content[buf->use] = 0;
175
    return (0);
176 177
}

K
Karel Zak 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
/**
 * 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;
    
    va_start(ap, buf);
    
    while ((str = va_arg(ap, char *)) != NULL) {
        unsigned int len = strlen(str);
        unsigned int needSize = buf->use + len + 2;

        if (needSize > buf->size) {
           if (!virBufferGrow(buf, needSize))
              return -1;
	}
        memcpy(&buf->content[buf->use], str, len);
        buf->use += len;
        buf->content[buf->use] = 0;
    }
    va_end(ap);
    return 0;
}

D
Daniel Veillard 已提交
211
#if 0
212

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

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

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

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

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

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

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

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

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

335 336
    conn = domain->conn;

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

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

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

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

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

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

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

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

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

433 434
    conn = domain->conn;

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

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

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

456
    return (ret);
457 458
}

459 460 461



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

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

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

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

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

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

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

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

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

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

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

572
#ifndef PROXY
573 574 575 576
/**
 * virtDomainParseXMLGraphicsDesc:
 * @node: node containing graphics description
 * @buf: a buffer for the result S-Expr
577
 * @xendConfigVersion: xend configuration file format
578 579 580 581 582 583 584 585
 *
 * 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 
 * valid over time.
 *
 * Returns 0 in case of success, -1 in case of error
 */
586
static int virDomainParseXMLGraphicsDesc(xmlNodePtr node, virBufferPtr buf, int xendConfigVersion)
587 588 589 590 591 592 593 594 595 596 597 598 599
{
    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);
            // TODO:
            // Need to understand sdl options
            //
            //virBufferAdd(buf, "(display localhost:10.0)", 24);
            //virBufferAdd(buf, "(xauthority /root/.Xauthority)", 30);
        }
600 601 602 603
        else if (xmlStrEqual(graphics_type, BAD_CAST "vnc")) {
            xmlChar *vncport = NULL;
            long port;

604
            virBufferAdd(buf, "(vnc 1)", 7);
605 606 607 608 609 610 611 612 613 614 615
            if (xendConfigVersion >= 2) {
                vncport = xmlGetProp(node, BAD_CAST "port");
                if (vncport != NULL) {
                    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);
                }
            }
        }
616 617 618 619 620 621
        xmlFree(graphics_type);
    }
    return 0;
}


622
/**
623 624
 * virDomainParseXMLOSDescHVM:
 * @node: node containing HVM OS description
625
 * @buf: a buffer for the result S-Expr
626
 * @ctxt: a path context representing the XML description
627
 * @xendConfigVersion: xend configuration file format
628
 *
629 630
 * 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
631 632 633 634 635 636
 * 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
637
virDomainParseXMLOSDescHVM(xmlNodePtr node, virBufferPtr buf, xmlXPathContextPtr ctxt, int xendConfigVersion)
638 639 640 641 642 643
{
    xmlXPathObjectPtr obj = NULL;
    xmlNodePtr cur, txt;
    const xmlChar *type = NULL;
    const xmlChar *loader = NULL;
    const xmlChar *boot_dev = NULL;
644
    int res;
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693

    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) &&
		    (txt->next == NULL))
                    type = txt->content;
            } else if ((loader == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "loader"))) {
                txt = cur->children;
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
		    (txt->next == NULL))
                    loader = txt->content;
            } else if ((boot_dev == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "boot"))) {
                boot_dev = xmlGetProp(cur, BAD_CAST "dev");
            }
        }
        cur = cur->next;
    }
    if ((type == NULL) || (!xmlStrEqual(type, BAD_CAST "hvm"))) {
        /* VIR_ERR_OS_TYPE */
        virXMLError(VIR_ERR_OS_TYPE, (const char *) type, 0);
        return (-1);
    }
    virBufferAdd(buf, "(image (hvm ", 12);
    if (loader == NULL) {
       virXMLError(VIR_ERR_NO_KERNEL, NULL, 0);
       goto error;
    } else {
       virBufferVSprintf(buf, "(kernel '%s')", (const char *) loader);
    }

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

    if (boot_dev) {
694
       if (xmlStrEqual(boot_dev, BAD_CAST "fd")) {
695
          virBufferVSprintf(buf, "(boot a)", (const char *) boot_dev);
696
       } else if (xmlStrEqual(boot_dev, BAD_CAST "cdrom")) {
697
          virBufferVSprintf(buf, "(boot d)", (const char *) boot_dev);
698
       } else if (xmlStrEqual(boot_dev, BAD_CAST "hd")) {
699
          virBufferVSprintf(buf, "(boot c)", (const char *) boot_dev);
700 701 702 703 704 705 706 707 708
       } else {
         /* Any other type of boot dev is unsupported right now */
         virXMLError(VIR_ERR_XML_ERROR, NULL, 0);
       }

       /* get the 1st floppy device file */
       obj = xmlXPathEval(BAD_CAST "/domain/devices/disk[@device='floppy' and target/@dev='fda']/source", ctxt);
       if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
           (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr == 1)) {
709 710 711 712
           cur = obj->nodesetval->nodeTab[0];
           virBufferVSprintf(buf, "(fda '%s')",
                             (const char *) xmlGetProp(cur, BAD_CAST "file"));
           cur = NULL;
713
       }
714
       if (obj) {
715 716 717
           xmlXPathFreeObject(obj);
           obj = NULL;
       }
718 719 720 721 722

       /* 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)) {
723 724 725 726
           cur = obj->nodesetval->nodeTab[0];
           virBufferVSprintf(buf, "(fdb '%s')",
                             (const char *) xmlGetProp(cur, BAD_CAST "file"));
           cur = NULL;
727 728
       }
       if (obj) {
729 730
           xmlXPathFreeObject(obj);
           obj = NULL;
731 732 733 734
       }


       /* get the cdrom device file */
735 736 737 738 739 740 741 742 743 744 745 746 747 748
       /* 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)) {
               cur = obj->nodesetval->nodeTab[0];
               virBufferVSprintf(buf, "(cdrom '%s')",
                                 (const char *) xmlGetProp(cur, BAD_CAST "file"));
               cur = NULL;
           }
           if (obj) {
               xmlXPathFreeObject(obj);
               obj = NULL;
           }
749
       }
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771

       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);
	   xmlXPathFreeObject(obj);
	   obj = NULL;
       }
       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);
	   xmlXPathFreeObject(obj);
	   obj = NULL;
       }
       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);
	   xmlXPathFreeObject(obj);
	   obj = NULL;
       }
772 773 774 775 776 777 778 779 780 781 782 783
    }

    obj = xmlXPathEval(BAD_CAST "count(domain/devices/console) > 0", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_BOOLEAN)) {
      virXMLError(VIR_ERR_XML_ERROR, NULL, 0);
      goto error;
    }
    if (obj->boolval) {
      virBufferAdd(buf, "(serial pty)", 12);
    }
    xmlXPathFreeObject(obj);
    obj = NULL;
784 785 786 787
    
    /* Is a graphics device specified? */
    obj = xmlXPathEval(BAD_CAST "/domain/devices/graphics[1]", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
788
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr > 0)) {
789
        res = virDomainParseXMLGraphicsDesc(obj->nodesetval->nodeTab[0], buf, xendConfigVersion);
790 791
        if (res != 0) {
            goto error;
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
        }
    }
    xmlXPathFreeObject(obj);

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

    return (0);
error:
    if (obj != NULL)
        xmlXPathFreeObject(obj);
    return(-1);
}

/**
 * virDomainParseXMLOSDescPV:
 * @node: node containing PV OS description
 * @buf: a buffer for the result S-Expr
809
 * @ctxt: a path context representing the XML description
810
 * @xendConfigVersion: xend configuration file format
811 812 813 814 815 816 817 818 819
 *
 * 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
820
virDomainParseXMLOSDescPV(xmlNodePtr node, virBufferPtr buf, xmlXPathContextPtr ctxt, int xendConfigVersion)
821
{
822
    xmlNodePtr cur, txt;
823
    xmlXPathObjectPtr obj = NULL;
824 825 826 827 828
    const xmlChar *type = NULL;
    const xmlChar *root = NULL;
    const xmlChar *kernel = NULL;
    const xmlChar *initrd = NULL;
    const xmlChar *cmdline = NULL;
829
    int res;
830 831 832 833

    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
834 835 836
            if ((type == NULL)
                && (xmlStrEqual(cur->name, BAD_CAST "type"))) {
                txt = cur->children;
837 838
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
		    (txt->next == NULL))
839 840 841 842
                    type = txt->content;
            } else if ((kernel == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "kernel"))) {
                txt = cur->children;
843 844
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
		    (txt->next == NULL))
845 846 847 848
                    kernel = txt->content;
            } else if ((root == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "root"))) {
                txt = cur->children;
849 850
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
		    (txt->next == NULL))
851 852 853 854
                    root = txt->content;
            } else if ((initrd == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "initrd"))) {
                txt = cur->children;
855 856
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
		    (txt->next == NULL))
857 858 859 860
                    initrd = txt->content;
            } else if ((cmdline == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "cmdline"))) {
                txt = cur->children;
861 862
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
		    (txt->next == NULL))
863 864 865
                    cmdline = txt->content;
            }
        }
866 867 868 869
        cur = cur->next;
    }
    if ((type != NULL) && (!xmlStrEqual(type, BAD_CAST "linux"))) {
        /* VIR_ERR_OS_TYPE */
870 871
        virXMLError(VIR_ERR_OS_TYPE, (const char *) type, 0);
        return (-1);
872
    }
873
    virBufferAdd(buf, "(image (linux ", 14);
874
    if (kernel == NULL) {
875 876
      	virXMLError(VIR_ERR_NO_KERNEL, NULL, 0);
	return (-1);
877 878
    } else {
	virBufferVSprintf(buf, "(kernel '%s')", (const char *) kernel);
879 880
    }
    if (initrd != NULL)
881
        virBufferVSprintf(buf, "(ramdisk '%s')", (const char *) initrd);
882
    if (root != NULL)
883
        virBufferVSprintf(buf, "(root '%s')", (const char *) root);
884
    if (cmdline != NULL)
885
        virBufferVSprintf(buf, "(args '%s')", (const char *) cmdline);
886 887 888 889 890

    /* Is a graphics device specified? */
    obj = xmlXPathEval(BAD_CAST "/domain/devices/graphics[1]", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr > 0)) {
891
        res = virDomainParseXMLGraphicsDesc(obj->nodesetval->nodeTab[0], buf, xendConfigVersion);
892 893 894 895 896 897 898
        if (res != 0) {
            goto error;
        }
    }
    xmlXPathFreeObject(obj);

 error:
899
    virBufferAdd(buf, "))", 2);
900
    return (0);
901 902 903 904
}

/**
 * virDomainParseXMLDiskDesc:
905
 * @node: node containing disk description
906
 * @buf: a buffer for the result S-Expr
907
 * @xendConfigVersion: xend configuration file format
908 909 910 911 912 913 914 915 916
 *
 * 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
917
virDomainParseXMLDiskDesc(xmlNodePtr node, virBufferPtr buf, int hvm, int xendConfigVersion)
918
{
919 920
    xmlNodePtr cur;
    xmlChar *type = NULL;
921
    xmlChar *device = NULL;
922 923 924 925
    xmlChar *source = NULL;
    xmlChar *target = NULL;
    int ro = 0;
    int typ = 0;
926
    int cdrom = 0;
927 928 929

    type = xmlGetProp(node, BAD_CAST "type");
    if (type != NULL) {
930 931 932 933 934
        if (xmlStrEqual(type, BAD_CAST "file"))
            typ = 0;
        else if (xmlStrEqual(type, BAD_CAST "block"))
            typ = 1;
        xmlFree(type);
935
    }
936 937
    device = xmlGetProp(node, BAD_CAST "device");
    
938 939 940
    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
941 942 943 944 945 946 947 948 949 950 951 952 953 954
            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");
            } else if (xmlStrEqual(cur->name, BAD_CAST "readonly")) {
                ro = 1;
            }
        }
955 956 957 958
        cur = cur->next;
    }

    if (source == NULL) {
959 960 961 962 963
        virXMLError(VIR_ERR_NO_SOURCE, (const char *) target, 0);

        if (target != NULL)
            xmlFree(target);
        return (-1);
964 965
    }
    if (target == NULL) {
966 967 968 969
        virXMLError(VIR_ERR_NO_TARGET, (const char *) source, 0);
        if (source != NULL)
            xmlFree(source);
        return (-1);
970
    }
971

972 973
    /* Xend (all versions) put the floppy device config
     * under the hvm (image (os)) block
974
     */
975
    if (hvm && 
976
        device &&
977 978 979 980 981 982
        !strcmp((const char *)device, "floppy")) {
        return 0;
    }

    /* Xend <= 3.0.2 doesn't include cdrom config here */
    if (hvm && 
983 984
        device &&
        !strcmp((const char *)device, "cdrom")) {
985 986 987 988
        if (xendConfigVersion == 1)
            return 0;
        else
            cdrom = 1;
989 990 991 992
    }


    virBufferAdd(buf, "(device ", 8);
993
    virBufferAdd(buf, "(vbd ", 5);
994

995 996
    if (hvm) {
        char *tmp = (char *)target;
997
        /* Just in case user mistakenly still puts ioemu: in their XML */
998 999
        if (!strncmp((const char *) tmp, "ioemu:", 6))
            tmp += 6;
1000 1001 1002 1003 1004

        /* Xend <= 3.0.2 wants a ioemu: prefix on devices for HVM */
        if (xendConfigVersion == 1)
            virBufferVSprintf(buf, "(dev 'ioemu:%s')", (const char *) tmp);
        else /* But newer does not */
1005
            virBufferVSprintf(buf, "(dev '%s%s')", (const char *) tmp, cdrom ? ":cdrom" : ":disk");
1006
    } else
1007 1008
        virBufferVSprintf(buf, "(dev '%s')", (const char *) target);

1009 1010 1011 1012
    if (typ == 0)
        virBufferVSprintf(buf, "(uname 'file:%s')", source);
    else if (typ == 1) {
        if (source[0] == '/')
1013 1014 1015
            virBufferVSprintf(buf, "(uname 'phy:%s')", source);
        else
            virBufferVSprintf(buf, "(uname 'phy:/dev/%s')", source);
1016 1017 1018 1019 1020 1021
    }
    if (ro == 0)
        virBufferVSprintf(buf, "(mode 'w')");
    else if (ro == 1)
        virBufferVSprintf(buf, "(mode 'r')");

1022
    virBufferAdd(buf, ")", 1);
1023 1024 1025
    virBufferAdd(buf, ")", 1);
    xmlFree(target);
    xmlFree(source);
1026
    return (0);
1027 1028 1029 1030
}

/**
 * virDomainParseXMLIfDesc:
1031
 * @node: node containing the interface description
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
 * @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
1042
virDomainParseXMLIfDesc(xmlNodePtr node, virBufferPtr buf, int hvm)
1043
{
1044 1045 1046 1047 1048 1049 1050 1051 1052
    xmlNodePtr cur;
    xmlChar *type = NULL;
    xmlChar *source = NULL;
    xmlChar *mac = NULL;
    xmlChar *script = NULL;
    int typ = 0;

    type = xmlGetProp(node, BAD_CAST "type");
    if (type != NULL) {
1053 1054 1055 1056 1057
        if (xmlStrEqual(type, BAD_CAST "bridge"))
            typ = 0;
        else if (xmlStrEqual(type, BAD_CAST "ethernet"))
            typ = 1;
        xmlFree(type);
1058 1059 1060 1061
    }
    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
            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");
            }
        }
1077 1078 1079 1080 1081
        cur = cur->next;
    }

    virBufferAdd(buf, "(vif ", 5);
    if (mac != NULL)
1082
        virBufferVSprintf(buf, "(mac '%s')", (const char *) mac);
1083
    if (source != NULL) {
1084 1085 1086 1087
        if (typ == 0)
            virBufferVSprintf(buf, "(bridge '%s')", (const char *) source);
        else                    /* TODO does that work like that ? */
            virBufferVSprintf(buf, "(dev '%s')", (const char *) source);
1088 1089 1090
    }
    if (script != NULL)
        virBufferVSprintf(buf, "(script '%s')", script);
1091 1092
    if (hvm)
        virBufferAdd(buf, "(type ioemu)", 12);
1093 1094 1095

    virBufferAdd(buf, ")", 1);
    if (mac != NULL)
1096
        xmlFree(mac);
1097
    if (source != NULL)
1098
        xmlFree(source);
1099
    if (script != NULL)
1100 1101
        xmlFree(script);
    return (0);
1102 1103 1104 1105 1106
}

/**
 * virDomainParseXMLDesc:
 * @xmldesc: string with the XML description
1107
 * @xendConfigVersion: xend configuration file format
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
 *
 * 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 *
1118
virDomainParseXMLDesc(const char *xmldesc, char **name, int xendConfigVersion)
1119
{
1120 1121
    xmlDocPtr xml = NULL;
    xmlNodePtr node;
1122
    char *ret = NULL, *nam = NULL;
1123 1124 1125
    virBuffer buf;
    xmlChar *prop;
    xmlXPathObjectPtr obj = NULL;
1126
    xmlXPathObjectPtr tmpobj = NULL;
1127 1128
    xmlXPathContextPtr ctxt = NULL;
    int i, res;
1129
    int bootloader = 0;
1130
    int hvm = 0;
1131 1132

    if (name != NULL)
1133
        *name = NULL;
1134 1135
    ret = malloc(1000);
    if (ret == NULL)
1136
        return (NULL);
1137 1138 1139 1140 1141
    buf.content = ret;
    buf.size = 1000;
    buf.use = 0;

    xml = xmlReadDoc((const xmlChar *) xmldesc, "domain.xml", NULL,
1142 1143
                     XML_PARSE_NOENT | XML_PARSE_NONET |
                     XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    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")) {
1154 1155 1156 1157
            xmlFree(prop);
            goto error;
        }
        xmlFree(prop);
1158 1159 1160 1161 1162 1163 1164
    }
    virBufferAdd(&buf, "(vm ", 4);
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
        goto error;
    }
    /*
1165
     * extract some of the basics, name, memory, cpus ...
1166 1167
     */
    obj = xmlXPathEval(BAD_CAST "string(/domain/name[1])", ctxt);
1168
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
1169
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
1170
        virXMLError(VIR_ERR_NO_NAME, xmldesc, 0);
1171 1172 1173
        goto error;
    }
    virBufferVSprintf(&buf, "(name '%s')", obj->stringval);
1174 1175
    nam = strdup((const char *) obj->stringval);
    if (nam == NULL) {
1176 1177
        virXMLError(VIR_ERR_NO_MEMORY, "copying name", 0);
        goto error;
1178
    }
1179 1180 1181 1182
    xmlXPathFreeObject(obj);

    obj = xmlXPathEval(BAD_CAST "number(/domain/memory[1])", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NUMBER) ||
1183
        (isnan(obj->floatval)) || (obj->floatval < 64000)) {
1184
        virBufferVSprintf(&buf, "(memory 128)(maxmem 128)");
1185 1186
    } else {
        unsigned long mem = (obj->floatval / 1024);
1187

1188 1189 1190 1191 1192 1193
        virBufferVSprintf(&buf, "(memory %lu)(maxmem %lu)", mem, mem);
    }
    xmlXPathFreeObject(obj);

    obj = xmlXPathEval(BAD_CAST "number(/domain/vcpu[1])", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NUMBER) ||
1194
        (isnan(obj->floatval)) || (obj->floatval <= 0)) {
1195
        virBufferVSprintf(&buf, "(vcpus 1)");
1196 1197
    } else {
        unsigned int cpu = (unsigned int) obj->floatval;
1198

1199 1200 1201 1202
        virBufferVSprintf(&buf, "(vcpus %u)", cpu);
    }
    xmlXPathFreeObject(obj);

1203
    obj = xmlXPathEval(BAD_CAST "string(/domain/uuid[1])", ctxt);
1204 1205
    if ((obj == NULL) || ((obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0))) {
1206 1207 1208 1209
        virBufferVSprintf(&buf, "(uuid '%s')", obj->stringval);
    }
    xmlXPathFreeObject(obj);

1210 1211 1212 1213 1214 1215 1216 1217
    obj = xmlXPathEval(BAD_CAST "string(/domain/bootloader[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
	virBufferVSprintf(&buf, "(bootloader '%s')", obj->stringval);
	bootloader = 1;
    }
    xmlXPathFreeObject(obj);

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
    obj = xmlXPathEval(BAD_CAST "string(/domain/on_poweroff[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
	virBufferVSprintf(&buf, "(on_poweroff '%s')", obj->stringval);
    }
    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)) {
	virBufferVSprintf(&buf, "(on_reboot '%s')", obj->stringval);
    }
    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)) {
	virBufferVSprintf(&buf, "(on_crash '%s')", obj->stringval);
    }
    xmlXPathFreeObject(obj);

1239
    obj = xmlXPathEval(BAD_CAST "/domain/os[1]", ctxt);
1240 1241
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr == 1)) {
1242 1243 1244 1245 1246 1247 1248
	/* Analyze of the os description, based on HVM or PV. */
	tmpobj = xmlXPathEval(BAD_CAST "string(/domain/os/type[1])", ctxt);
	if ((tmpobj != NULL) &&
	    ((tmpobj->type != XPATH_STRING) || (tmpobj->stringval == NULL) ||
	     (tmpobj->stringval[0] == 0))) {
	    xmlXPathFreeObject(tmpobj);
	    virXMLError(VIR_ERR_OS_TYPE, nam, 0);
1249 1250
	    goto error;
	}
1251 1252

	if ((tmpobj == NULL) || !xmlStrEqual(tmpobj->stringval, BAD_CAST "hvm")) {
1253
	    res = virDomainParseXMLOSDescPV(obj->nodesetval->nodeTab[0], &buf, ctxt, xendConfigVersion);
1254
	} else {
1255
	    hvm = 1;
1256
	    res = virDomainParseXMLOSDescHVM(obj->nodesetval->nodeTab[0], &buf, ctxt, xendConfigVersion);
1257 1258 1259 1260 1261 1262
	}

	xmlXPathFreeObject(tmpobj);

	if (res != 0)
	    goto error;
1263 1264 1265
    } else if (bootloader == 0) {
	virXMLError(VIR_ERR_NO_OS, nam, 0);
	goto error;
1266 1267 1268 1269 1270
    }
    xmlXPathFreeObject(obj);

    /* analyze of the devices */
    obj = xmlXPathEval(BAD_CAST "/domain/devices/disk", ctxt);
1271 1272 1273
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
	for (i = 0; i < obj->nodesetval->nodeNr; i++) {
1274
	  res = virDomainParseXMLDiskDesc(obj->nodesetval->nodeTab[i], &buf, hvm, xendConfigVersion);
1275 1276 1277 1278
	    if (res != 0) {
		goto error;
	    }
	}
1279 1280
    }
    xmlXPathFreeObject(obj);
1281

1282 1283 1284
    obj = xmlXPathEval(BAD_CAST "/domain/devices/interface", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
1285 1286
        for (i = 0; i < obj->nodesetval->nodeNr; i++) {
            virBufferAdd(&buf, "(device ", 8);
1287
            res = virDomainParseXMLIfDesc(obj->nodesetval->nodeTab[i], &buf, hvm);
1288 1289 1290 1291 1292
            if (res != 0) {
                goto error;
            }
            virBufferAdd(&buf, ")", 1);
        }
1293 1294 1295 1296
    }
    xmlXPathFreeObject(obj);


D
Daniel Veillard 已提交
1297
    virBufferAdd(&buf, ")", 1); /* closes (vm */
1298 1299 1300 1301
    buf.content[buf.use] = 0;

    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
1302 1303

    if (name != NULL)
1304
        *name = nam;
1305

1306 1307 1308
    return (ret);

  error:
1309
    if (nam != NULL)
1310
        free(nam);
1311
    if (name != NULL)
1312
        *name = NULL;
1313 1314 1315 1316 1317 1318 1319 1320
    if (obj != NULL)
        xmlXPathFreeObject(obj);
    if (ctxt != NULL)
        xmlXPathFreeContext(ctxt);
    if (xml != NULL)
        xmlFreeDoc(xml);
    if (ret != NULL)
        free(ret);
1321
    return (NULL);
1322
}
1323 1324

#endif /* !PROXY */
1325 1326 1327 1328 1329



unsigned char *virParseUUID(char **ptr, const char *uuid) {
    int rawuuid[16];
1330
    const char *cur;
1331 1332 1333 1334 1335 1336
    unsigned char *dst_uuid = NULL;
    int i;

    if (uuid == NULL)
        goto error;

1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
    /*
     * do a liberal scan allowing '-' and ' ' anywhere between character
     * pairs as long as there is 32 of them in the end.
     */
    cur = uuid;
    for (i = 0;i < 16;) {
        rawuuid[i] = 0;
        if (*cur == 0)
	    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++;
        if (*cur == 0)
	    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;
        i++;
	cur++;
    }
1373 1374 1375 1376 1377 1378 1379

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

    for (i = 0; i < 16; i++)
        dst_uuid[i] = rawuuid[i] & 0xFF;

1380 1381
error:
    return(dst_uuid);
1382
}
1383 1384 1385 1386 1387 1388 1389 1390 1391

/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */