xml.c 28.5 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 22
#include "internal.h"
#include "hash.h"
D
Daniel Veillard 已提交
23
#include "sexpr.h"
24
#include "xml.h"
25

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

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

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

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

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

    size = buf->use + len + 1000;

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

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

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

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

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

K
Karel Zak 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
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 已提交
129 130 131 132
void
virBufferFree(virBufferPtr buf)
{
    if (buf) {
K
Karel Zak 已提交
133 134 135
        if (buf->content)
	   free(buf->content);
       	free(buf);
K
Karel Zak 已提交
136 137 138
    }
}

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

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

K
Karel Zak 已提交
177 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
/**
 * 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 已提交
210
#if 0
211

D
Daniel Veillard 已提交
212 213 214 215 216
/*
 * 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 ...
 */
217

218 219 220 221 222 223 224 225 226 227 228 229
/**
 * 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 *
230 231 232
virDomainGetXMLDeviceInfo(virDomainPtr domain, const char *sub,
                          long dev, const char *name)
{
233 234 235 236 237 238 239
    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;

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

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

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

303
    return (0);
304 305 306 307 308 309 310 311 312 313 314 315
}

/**
 * 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
316 317
virDomainGetXMLDevices(virDomainPtr domain, virBufferPtr buf)
{
318 319 320 321 322 323 324
    int ret = -1;
    unsigned int num, i;
    long id;
    char **list = NULL, *endptr;
    char backend[200];
    virConnectPtr conn;

K
Karel Zak 已提交
325
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
326 327
        return (-1);

328 329
    conn = domain->conn;

330
    snprintf(backend, 199, "/local/domain/0/backend/vbd/%d",
331 332
             virDomainGetID(domain));
    backend[199] = 0;
333
    list = xs_directory(conn->xshandle, 0, backend, &num);
334 335 336 337
    ret = 0;
    if (list == NULL)
        goto done;

338
    for (i = 0; i < num; i++) {
339
        id = strtol(list[i], &endptr, 10);
340 341 342 343 344
        if ((endptr == list[i]) || (*endptr != 0)) {
            ret = -1;
            goto done;
        }
        virDomainGetXMLDevice(domain, buf, id);
345 346
    }

347
  done:
348 349 350
    if (list != NULL)
        free(list);

351
    return (ret);
352 353 354 355 356 357 358 359 360 361 362 363 364 365
}

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

    type = virDomainGetXMLDeviceInfo(domain, "vif", dev, "bridge");
    if (type == NULL) {
372 373 374 375 376 377 378 379 380 381 382 383
        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);
384
    } else {
385 386 387 388 389 390 391 392 393 394 395 396 397
        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);
398 399 400
    }
    free(type);

401
    return (0);
402 403 404 405 406 407 408 409 410 411 412 413
}

/**
 * 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
414 415
virDomainGetXMLInterfaces(virDomainPtr domain, virBufferPtr buf)
{
416 417 418 419 420 421 422
    int ret = -1;
    unsigned int num, i;
    long id;
    char **list = NULL, *endptr;
    char backend[200];
    virConnectPtr conn;

K
Karel Zak 已提交
423
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
424 425
        return (-1);

426 427
    conn = domain->conn;

428
    snprintf(backend, 199, "/local/domain/0/backend/vif/%d",
429 430
             virDomainGetID(domain));
    backend[199] = 0;
431
    list = xs_directory(conn->xshandle, 0, backend, &num);
432 433 434 435
    ret = 0;
    if (list == NULL)
        goto done;

436
    for (i = 0; i < num; i++) {
437
        id = strtol(list[i], &endptr, 10);
438 439 440 441 442
        if ((endptr == list[i]) || (*endptr != 0)) {
            ret = -1;
            goto done;
        }
        virDomainGetXMLInterface(domain, buf, id);
443 444
    }

445
  done:
446 447 448
    if (list != NULL)
        free(list);

449
    return (ret);
450 451
}

452 453 454



455 456 457 458 459 460 461 462 463 464
/**
 * virDomainGetXMLBoot:
 * @domain: a domain object
 * @buf: the output buffer object
 *
 * Extract the boot informations used to start that domain
 *
 * Returns 0 in case of success, -1 in case of failure
 */
static int
465 466
virDomainGetXMLBoot(virDomainPtr domain, virBufferPtr buf)
{
467 468
    char *vm, *str;

K
Karel Zak 已提交
469
    if (!VIR_IS_DOMAIN(domain))
470 471
        return (-1);

472
    vm = virDomainGetVM(domain);
473
    if (vm == NULL)
474
        return (-1);
475 476 477 478 479 480 481 482 483 484 485 486 487 488

    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) {
489 490
        if (str[0] != 0)
            virBufferVSprintf(buf, "    <initrd>%s</initrd>\n", str);
491 492 493 494
        free(str);
    }
    str = virDomainGetVMInfo(domain, vm, "image/cmdline");
    if (str != NULL) {
495 496
        if (str[0] != 0)
            virBufferVSprintf(buf, "    <cmdline>%s</cmdline>\n", str);
497 498 499 500 501
        free(str);
    }
    virBufferAdd(buf, "  </os>\n", 8);

    free(vm);
502
    return (0);
503 504
}

505 506 507 508 509 510 511 512 513 514 515 516
/**
 * 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 *
517 518
virDomainGetXMLDesc(virDomainPtr domain, int flags)
{
519
    char *ret = NULL;
520
    unsigned char uuid[16];
521 522 523
    virBuffer buf;
    virDomainInfo info;

K
Karel Zak 已提交
524
    if (!VIR_IS_DOMAIN(domain))
525
        return (NULL);
K
Karel Zak 已提交
526
    if (flags != 0)
527
        return (NULL);
528
    if (virDomainGetInfo(domain, &info) < 0)
529
        return (NULL);
530 531 532

    ret = malloc(1000);
    if (ret == NULL)
533
        return (NULL);
534 535 536 537 538 539
    buf.content = ret;
    buf.size = 1000;
    buf.use = 0;

    virBufferVSprintf(&buf, "<domain type='xen' id='%d'>\n",
                      virDomainGetID(domain));
540 541
    virBufferVSprintf(&buf, "  <name>%s</name>\n",
                      virDomainGetName(domain));
542 543 544 545 546 547 548 549
    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]);
    }
550
    virDomainGetXMLBoot(domain, &buf);
551 552 553 554 555 556
    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);
557
    virBufferAdd(&buf, "</domain>\n", 10);
558

559
    buf.content[buf.use] = 0;
560
    return (ret);
561
}
562

D
Daniel Veillard 已提交
563 564
#endif

565 566 567 568
/**
 * virDomainParseXMLOSDesc:
 * @xmldesc: string with the XML description
 * @buf: a buffer for the result S-Expr
569
 * @bootloader: indocate if a bootloader script was provided
570 571 572 573 574 575 576 577 578
 *
 * Parse the OS 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.
 */
static int
579
virDomainParseXMLOSDesc(xmlNodePtr node, virBufferPtr buf, int bootloader)
580
{
581 582 583 584 585 586 587 588 589 590
    xmlNodePtr cur, txt;
    const xmlChar *type = NULL;
    const xmlChar *root = NULL;
    const xmlChar *kernel = NULL;
    const xmlChar *initrd = NULL;
    const xmlChar *cmdline = NULL;

    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
591 592 593
            if ((type == NULL)
                && (xmlStrEqual(cur->name, BAD_CAST "type"))) {
                txt = cur->children;
594 595
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
		    (txt->next == NULL))
596 597 598 599
                    type = txt->content;
            } else if ((kernel == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "kernel"))) {
                txt = cur->children;
600 601
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
		    (txt->next == NULL))
602 603 604 605
                    kernel = txt->content;
            } else if ((root == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "root"))) {
                txt = cur->children;
606 607
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
		    (txt->next == NULL))
608 609 610 611
                    root = txt->content;
            } else if ((initrd == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "initrd"))) {
                txt = cur->children;
612 613
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
		    (txt->next == NULL))
614 615 616 617
                    initrd = txt->content;
            } else if ((cmdline == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "cmdline"))) {
                txt = cur->children;
618 619
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
		    (txt->next == NULL))
620 621 622
                    cmdline = txt->content;
            }
        }
623 624 625 626
        cur = cur->next;
    }
    if ((type != NULL) && (!xmlStrEqual(type, BAD_CAST "linux"))) {
        /* VIR_ERR_OS_TYPE */
627 628
        virXMLError(VIR_ERR_OS_TYPE, (const char *) type, 0);
        return (-1);
629 630 631
    }
    virBufferAdd(buf, "(linux ", 7);
    if (kernel == NULL) {
632 633 634 635 636 637
	if (bootloader == 0) {
	    virXMLError(VIR_ERR_NO_KERNEL, NULL, 0);
	    return (-1);
	}
    } else {
	virBufferVSprintf(buf, "(kernel '%s')", (const char *) kernel);
638 639
    }
    if (initrd != NULL)
640
        virBufferVSprintf(buf, "(ramdisk '%s')", (const char *) initrd);
641
    if (root != NULL)
642
        virBufferVSprintf(buf, "(root '%s')", (const char *) root);
643
    if (cmdline != NULL)
644
        virBufferVSprintf(buf, "(args '%s')", (const char *) cmdline);
645
    virBufferAdd(buf, ")", 1);
646
    return (0);
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
}

/**
 * virDomainParseXMLDiskDesc:
 * @xmldesc: string with the XML description
 * @buf: a buffer for the result S-Expr
 *
 * 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
662 663
virDomainParseXMLDiskDesc(xmlNodePtr node, virBufferPtr buf)
{
664 665 666 667 668 669 670 671 672
    xmlNodePtr cur;
    xmlChar *type = NULL;
    xmlChar *source = NULL;
    xmlChar *target = NULL;
    int ro = 0;
    int typ = 0;

    type = xmlGetProp(node, BAD_CAST "type");
    if (type != NULL) {
673 674 675 676 677
        if (xmlStrEqual(type, BAD_CAST "file"))
            typ = 0;
        else if (xmlStrEqual(type, BAD_CAST "block"))
            typ = 1;
        xmlFree(type);
678 679 680 681
    }
    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
682 683 684 685 686 687 688 689 690 691 692 693 694 695
            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;
            }
        }
696 697 698 699
        cur = cur->next;
    }

    if (source == NULL) {
700 701 702 703 704
        virXMLError(VIR_ERR_NO_SOURCE, (const char *) target, 0);

        if (target != NULL)
            xmlFree(target);
        return (-1);
705 706
    }
    if (target == NULL) {
707 708 709 710
        virXMLError(VIR_ERR_NO_TARGET, (const char *) source, 0);
        if (source != NULL)
            xmlFree(source);
        return (-1);
711 712 713
    }
    virBufferAdd(buf, "(vbd ", 5);
    if (target[0] == '/')
714
        virBufferVSprintf(buf, "(dev '%s')", (const char *) target);
715
    else
716
        virBufferVSprintf(buf, "(dev '/dev/%s')", (const char *) target);
717 718 719 720
    if (typ == 0)
        virBufferVSprintf(buf, "(uname 'file:%s')", source);
    else if (typ == 1) {
        if (source[0] == '/')
721 722 723
            virBufferVSprintf(buf, "(uname 'phy:%s')", source);
        else
            virBufferVSprintf(buf, "(uname 'phy:/dev/%s')", source);
724 725 726 727 728 729 730 731 732
    }
    if (ro == 0)
        virBufferVSprintf(buf, "(mode 'w')");
    else if (ro == 1)
        virBufferVSprintf(buf, "(mode 'r')");

    virBufferAdd(buf, ")", 1);
    xmlFree(target);
    xmlFree(source);
733
    return (0);
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
}

/**
 * virDomainParseXMLIfDesc:
 * @xmldesc: string with the XML description
 * @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
749 750
virDomainParseXMLIfDesc(xmlNodePtr node, virBufferPtr buf)
{
751 752 753 754 755 756 757 758 759
    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) {
760 761 762 763 764
        if (xmlStrEqual(type, BAD_CAST "bridge"))
            typ = 0;
        else if (xmlStrEqual(type, BAD_CAST "ethernet"))
            typ = 1;
        xmlFree(type);
765 766 767 768
    }
    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
            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");
            }
        }
784 785 786 787 788
        cur = cur->next;
    }

    virBufferAdd(buf, "(vif ", 5);
    if (mac != NULL)
789
        virBufferVSprintf(buf, "(mac '%s')", (const char *) mac);
790
    if (source != NULL) {
791 792 793 794
        if (typ == 0)
            virBufferVSprintf(buf, "(bridge '%s')", (const char *) source);
        else                    /* TODO does that work like that ? */
            virBufferVSprintf(buf, "(dev '%s')", (const char *) source);
795 796 797 798 799 800
    }
    if (script != NULL)
        virBufferVSprintf(buf, "(script '%s')", script);

    virBufferAdd(buf, ")", 1);
    if (mac != NULL)
801
        xmlFree(mac);
802
    if (source != NULL)
803
        xmlFree(source);
804
    if (script != NULL)
805 806
        xmlFree(script);
    return (0);
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
}

/**
 * virDomainParseXMLDesc:
 * @xmldesc: string with the XML description
 *
 * 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 *
822 823
virDomainParseXMLDesc(const char *xmldesc, char **name)
{
824 825
    xmlDocPtr xml = NULL;
    xmlNodePtr node;
826
    char *ret = NULL, *nam = NULL;
827 828 829 830 831
    virBuffer buf;
    xmlChar *prop;
    xmlXPathObjectPtr obj = NULL;
    xmlXPathContextPtr ctxt = NULL;
    int i, res;
832
    int bootloader = 0;
833 834

    if (name != NULL)
835
        *name = NULL;
836 837
    ret = malloc(1000);
    if (ret == NULL)
838
        return (NULL);
839 840 841 842 843
    buf.content = ret;
    buf.size = 1000;
    buf.use = 0;

    xml = xmlReadDoc((const xmlChar *) xmldesc, "domain.xml", NULL,
844 845
                     XML_PARSE_NOENT | XML_PARSE_NONET |
                     XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
846 847 848 849 850 851 852 853 854 855
    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")) {
856 857 858 859
            xmlFree(prop);
            goto error;
        }
        xmlFree(prop);
860 861 862 863 864 865 866 867 868 869
    }
    virBufferAdd(&buf, "(vm ", 4);
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
        goto error;
    }
    /*
     * extract soem of the basics, name, memory, cpus ...
     */
    obj = xmlXPathEval(BAD_CAST "string(/domain/name[1])", ctxt);
870
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
871
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
872
        virXMLError(VIR_ERR_NO_NAME, xmldesc, 0);
873 874 875
        goto error;
    }
    virBufferVSprintf(&buf, "(name '%s')", obj->stringval);
876 877
    nam = strdup((const char *) obj->stringval);
    if (nam == NULL) {
878 879
        virXMLError(VIR_ERR_NO_MEMORY, "copying name", 0);
        goto error;
880
    }
881 882 883 884 885
    xmlXPathFreeObject(obj);

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

890 891 892 893 894 895 896
        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) ||
        (obj->floatval <= 0)) {
897
        virBufferVSprintf(&buf, "(vcpus 1)");
898 899
    } else {
        unsigned int cpu = (unsigned int) obj->floatval;
900

901 902 903 904
        virBufferVSprintf(&buf, "(vcpus %u)", cpu);
    }
    xmlXPathFreeObject(obj);

905 906 907 908 909 910 911 912
    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);

913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
    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);
	bootloader = 1;
    }
    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);
	bootloader = 1;
    }
    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);
	bootloader = 1;
    }
    xmlXPathFreeObject(obj);

937 938 939 940
    /* analyze of the os description */
    virBufferAdd(&buf, "(image ", 7);
    obj = xmlXPathEval(BAD_CAST "/domain/os[1]", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
941 942
        (obj->nodesetval == NULL) || (obj->nodesetval->nodeNr != 1)) {
        virXMLError(VIR_ERR_NO_OS, nam, 0);
943 944
        goto error;
    }
945 946
    res = virDomainParseXMLOSDesc(obj->nodesetval->nodeTab[0], &buf,
                                  bootloader);
947 948 949 950 951 952 953 954 955
    if (res != 0) {
        goto error;
    }
    xmlXPathFreeObject(obj);
    virBufferAdd(&buf, ")", 1);

    /* analyze of the devices */
    obj = xmlXPathEval(BAD_CAST "/domain/devices/disk", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
956 957
        (obj->nodesetval == NULL) || (obj->nodesetval->nodeNr < 1)) {
        virXMLError(VIR_ERR_NO_DEVICE, nam, 0);
958 959
        goto error;
    }
960 961 962 963 964 965 966
    for (i = 0; i < obj->nodesetval->nodeNr; i++) {
        virBufferAdd(&buf, "(device ", 8);
        res = virDomainParseXMLDiskDesc(obj->nodesetval->nodeTab[i], &buf);
        if (res != 0) {
            goto error;
        }
        virBufferAdd(&buf, ")", 1);
967 968 969 970 971
    }
    xmlXPathFreeObject(obj);
    obj = xmlXPathEval(BAD_CAST "/domain/devices/interface", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
        (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
972 973 974 975 976 977 978 979 980
        for (i = 0; i < obj->nodesetval->nodeNr; i++) {
            virBufferAdd(&buf, "(device ", 8);
            res =
                virDomainParseXMLIfDesc(obj->nodesetval->nodeTab[i], &buf);
            if (res != 0) {
                goto error;
            }
            virBufferAdd(&buf, ")", 1);
        }
981 982 983 984
    }
    xmlXPathFreeObject(obj);


D
Daniel Veillard 已提交
985
    virBufferAdd(&buf, ")", 1); /* closes (vm */
986 987 988 989
    buf.content[buf.use] = 0;

    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
990 991

    if (name != NULL)
992
        *name = nam;
993

994 995 996
    return (ret);

  error:
997
    if (nam != NULL)
998
        free(nam);
999
    if (name != NULL)
1000
        *name = NULL;
1001 1002 1003 1004 1005 1006 1007 1008
    if (obj != NULL)
        xmlXPathFreeObject(obj);
    if (ctxt != NULL)
        xmlXPathFreeContext(ctxt);
    if (xml != NULL)
        xmlFreeDoc(xml);
    if (ret != NULL)
        free(ret);
1009
    return (NULL);
1010
}