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

11
#include <config.h>
12

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
19
#include <limits.h>
20
#ifdef WITH_XEN
21
#include <xs.h>
22
#endif
23
#include <math.h>               /* for isnan() */
24 25
#include "internal.h"
#include "hash.h"
D
Daniel Veillard 已提交
26
#include "sexpr.h"
27
#include "xml.h"
28
#include "buf.h"
29
#include "xs_internal.h"        /* for xenStoreDomainGetNetworkID */
30
#include "xen_unified.h"
31

32 33 34 35 36 37 38 39 40
/**
 * virXMLError:
 * @conn: a connection if any
 * @error: the error number
 * @info: information/format string
 * @value: extra integer parameter for the error string
 *
 * Report an error coming from the XML module.
 */
41
static void
42 43
virXMLError(virConnectPtr conn, virErrorNumber error, const char *info,
            int value)
44
{
45
    const char *errmsg;
46

47 48 49 50
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
51
    __virRaiseError(conn, NULL, NULL, VIR_FROM_XML, error, VIR_ERR_ERROR,
52
                    errmsg, info, NULL, value, 0, errmsg, info, value);
53 54
}

55 56 57 58 59
/************************************************************************
 *									*
 * Parser and converter for the CPUset strings used in libvirt		*
 *									*
 ************************************************************************/
60
#if WITH_XEN
61 62 63 64 65 66 67 68 69
/**
 * skipSpaces:
 * @str: pointer to the char pointer used
 *
 * Skip potential blanks, this includes space tabs, line feed,
 * carriage returns and also '\\' which can be erronously emitted
 * by xend
 */
static void
70 71
skipSpaces(const char **str)
{
72 73 74
    const char *cur = *str;

    while ((*cur == ' ') || (*cur == '\t') || (*cur == '\n') ||
75 76
           (*cur == '\r') || (*cur == '\\'))
        cur++;
77 78 79 80 81 82 83
    *str = cur;
}

/**
 * parseNumber:
 * @str: pointer to the char pointer used
 *
84
 * Parse an unsigned number
85
 *
86
 * Returns the unsigned number or -1 in case of error. @str will be
87 88 89
 *         updated to skip the number.
 */
static int
90 91
parseNumber(const char **str)
{
92 93 94 95
    int ret = 0;
    const char *cur = *str;

    if ((*cur < '0') || (*cur > '9'))
96
        return (-1);
97 98

    while ((*cur >= '0') && (*cur <= '9')) {
99 100 101 102 103 104 105
        unsigned int c = *cur - '0';

        if ((ret > INT_MAX / 10) ||
            ((ret == INT_MAX / 10) && (c > INT_MAX % 10)))
            return (-1);
        ret = ret * 10 + c;
        cur++;
106 107
    }
    *str = cur;
108
    return (ret);
109 110 111 112 113 114 115 116 117 118 119 120 121
}

/**
 * parseCpuNumber:
 * @str: pointer to the char pointer used
 * @maxcpu: maximum CPU number allowed
 *
 * Parse a CPU number
 *
 * Returns the CPU number or -1 in case of error. @str will be
 *         updated to skip the number.
 */
static int
122 123
parseCpuNumber(const char **str, int maxcpu)
{
124 125 126 127
    int ret = 0;
    const char *cur = *str;

    if ((*cur < '0') || (*cur > '9'))
128
        return (-1);
129 130 131

    while ((*cur >= '0') && (*cur <= '9')) {
        ret = ret * 10 + (*cur - '0');
132
        if (ret >= maxcpu)
133 134
            return (-1);
        cur++;
135 136
    }
    *str = cur;
137
    return (ret);
138 139 140
}

/**
141
 * virSaveCpuSet:
142 143 144 145 146 147 148 149 150
 * @conn: connection
 * @cpuset: pointer to a char array for the CPU set
 * @maxcpu: number of elements available in @cpuset
 *
 * Serialize the cpuset to a string
 *
 * Returns the new string NULL in case of error. The string need to be
 *         freed by the caller.
 */
151 152
char *
virSaveCpuSet(virConnectPtr conn, char *cpuset, int maxcpu)
153 154 155 156 157 158
{
    virBufferPtr buf;
    char *ret;
    int start, cur;
    int first = 1;

159 160
    if ((cpuset == NULL) || (maxcpu <= 0) || (maxcpu > 100000))
        return (NULL);
161 162 163

    buf = virBufferNew(1000);
    if (buf == NULL) {
164 165
        virXMLError(conn, VIR_ERR_NO_MEMORY, _("allocate buffer"), 1000);
        return (NULL);
166 167 168 169 170
    }
    cur = 0;
    start = -1;
    while (cur < maxcpu) {
        if (cpuset[cur]) {
171 172 173 174
            if (start == -1)
                start = cur;
        } else if (start != -1) {
            if (!first)
175
                virBufferAddLit(buf, ",");
176
            else
177 178 179 180 181 182 183 184
                first = 0;
            if (cur == start + 1)
                virBufferVSprintf(buf, "%d", start);
            else
                virBufferVSprintf(buf, "%d-%d", start, cur - 1);
            start = -1;
        }
        cur++;
185 186
    }
    if (start != -1) {
187
        if (!first)
188
            virBufferAddLit(buf, ",");
189 190 191 192
        if (maxcpu == start + 1)
            virBufferVSprintf(buf, "%d", start);
        else
            virBufferVSprintf(buf, "%d-%d", start, maxcpu - 1);
193 194
    }
    ret = virBufferContentAndFree(buf);
195
    return (ret);
196 197 198 199
}

/**
 * virParseCpuSet:
200
 * @conn: connection
201 202 203 204 205 206 207 208 209 210 211 212 213 214
 * @str: pointer to a CPU set string pointer
 * @sep: potential character used to mark the end of string if not 0
 * @cpuset: pointer to a char array for the CPU set
 * @maxcpu: number of elements available in @cpuset
 *
 * Parse the cpu set, it will set the value for enabled CPUs in the @cpuset
 * to 1, and 0 otherwise. The syntax allows coma separated entries each
 * can be either a CPU number, ^N to unset that CPU or N-M for ranges.
 *
 * Returns the number of CPU found in that set, or -1 in case of error.
 *         @cpuset is modified accordingly to the value parsed.
 *         @str is updated to the end of the part parsed
 */
int
215 216
virParseCpuSet(virConnectPtr conn, const char **str, char sep,
               char *cpuset, int maxcpu)
217 218 219 220 221 222
{
    const char *cur;
    int ret = 0;
    int i, start, last;
    int neg = 0;

223 224 225
    if ((str == NULL) || (cpuset == NULL) || (maxcpu <= 0) ||
        (maxcpu > 100000))
        return (-1);
226 227 228 229 230 231 232

    cur = *str;
    skipSpaces(&cur);
    if (*cur == 0)
        goto parse_error;

    /* initialize cpumap to all 0s */
233 234
    for (i = 0; i < maxcpu; i++)
        cpuset[i] = 0;
235 236 237
    ret = 0;

    while ((*cur != 0) && (*cur != sep)) {
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        /*
         * 3 constructs are allowed:
         *     - N   : a single CPU number
         *     - N-M : a range of CPU numbers with N < M
         *     - ^N  : remove a single CPU number from the current set
         */
        if (*cur == '^') {
            cur++;
            neg = 1;
        }

        if ((*cur < '0') || (*cur > '9'))
            goto parse_error;
        start = parseCpuNumber(&cur, maxcpu);
        if (start < 0)
            goto parse_error;
        skipSpaces(&cur);
        if ((*cur == ',') || (*cur == 0) || (*cur == sep)) {
            if (neg) {
                if (cpuset[start] == 1) {
                    cpuset[start] = 0;
                    ret--;
                }
            } else {
                if (cpuset[start] == 0) {
                    cpuset[start] = 1;
                    ret++;
                }
            }
        } else if (*cur == '-') {
            if (neg)
                goto parse_error;
            cur++;
            skipSpaces(&cur);
            last = parseCpuNumber(&cur, maxcpu);
            if (last < start)
                goto parse_error;
            for (i = start; i <= last; i++) {
                if (cpuset[i] == 0) {
                    cpuset[i] = 1;
                    ret++;
                }
            }
            skipSpaces(&cur);
        }
        if (*cur == ',') {
            cur++;
            skipSpaces(&cur);
            neg = 0;
        } else if ((*cur == 0) || (*cur == sep)) {
            break;
        } else
            goto parse_error;
291 292
    }
    *str = cur;
293
    return (ret);
294

295
  parse_error:
296
    virXMLError(conn, VIR_ERR_XEN_CALL,
297 298
                _("topology cpuset syntax error"), 0);
    return (-1);
299 300 301 302
}

/**
 * virParseXenCpuTopology:
303
 * @conn: connection
304
 * @xml: XML output buffer
305
 * @str: the topology string
306 307 308 309 310 311 312 313
 * @maxcpu: number of elements available in @cpuset
 *
 * Parse a Xend CPU topology string and build the associated XML
 * format.
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
314 315
virParseXenCpuTopology(virConnectPtr conn, virBufferPtr xml,
                       const char *str, int maxcpu)
316 317 318 319 320 321
{
    const char *cur;
    char *cpuset = NULL;
    int cell, cpu, nb_cpus;
    int ret;

322 323
    if ((str == NULL) || (xml == NULL) || (maxcpu <= 0) || (maxcpu > 100000))
        return (-1);
324

325
    cpuset = malloc(maxcpu * sizeof(*cpuset));
326 327 328 329 330 331
    if (cpuset == NULL)
        goto memory_error;

    cur = str;
    while (*cur != 0) {
        /*
332 333
         * Find the next NUMA cell described in the xend output
         */
334
        cur = strstr(cur, "node");
335 336 337
        if (cur == NULL)
            break;
        cur += 4;
338
        cell = parseNumber(&cur);
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
        if (cell < 0)
            goto parse_error;
        skipSpaces(&cur);
        if (*cur != ':')
            goto parse_error;
        cur++;
        skipSpaces(&cur);
        if (!strncmp(cur, "no cpus", 7)) {
            nb_cpus = 0;
            for (cpu = 0; cpu < maxcpu; cpu++)
                cpuset[cpu] = 0;
        } else {
            nb_cpus = virParseCpuSet(conn, &cur, 'n', cpuset, maxcpu);
            if (nb_cpus < 0)
                goto error;
        }

        /*
         * add xml for all cpus associated with that cell
         */
        ret = virBufferVSprintf(xml, "\
360 361 362 363
      <cell id='%d'>\n\
        <cpus num='%d'>\n", cell, nb_cpus);
#ifdef STANDALONE
        {
364 365
            char *dump;

366
            dump = virSaveCpuSet(conn, cpuset, maxcpu);
367 368 369 370 371 372 373 374
            if (dump != NULL) {
                virBufferVSprintf(xml, "           <dump>%s</dump>\n",
                                  dump);
                free(dump);
            } else {
                virBufferVSprintf(xml, "           <error>%s</error>\n",
                                  "Failed to dump CPU set");
            }
375 376
        }
#endif
377 378 379 380 381
        if (ret < 0)
            goto memory_error;
        for (cpu = 0; cpu < maxcpu; cpu++) {
            if (cpuset[cpu] == 1) {
                ret = virBufferVSprintf(xml, "\
382
           <cpu id='%d'/>\n", cpu);
383 384 385 386
                if (ret < 0)
                    goto memory_error;
            }
        }
387
        ret = virBufferAddLit(xml, "\
388
        </cpus>\n\
389
      </cell>\n");
390
        if (ret < 0)
391 392
            goto memory_error;

393 394
    }
    free(cpuset);
395
    return (0);
396

397 398 399
  parse_error:
    virXMLError(conn, VIR_ERR_XEN_CALL, _("topology syntax error"), 0);
  error:
400
    free(cpuset);
401

402
    return (-1);
403

404
  memory_error:
405
    free(cpuset);
406
    virXMLError(conn, VIR_ERR_NO_MEMORY, _("allocate buffer"), 0);
407
    return (-1);
408 409
}

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
/**
 * virConvertCpuSet:
 * @conn: connection
 * @str: pointer to a Xen or user provided CPU set string pointer
 * @maxcpu: number of CPUs on the node, if 0 4096 will be used
 *
 * Parse the given CPU set string and convert it to a range based
 * string.
 *
 * Returns a new string which must be freed by the caller or NULL in
 *         case of error.
 */
char *
virConvertCpuSet(virConnectPtr conn, const char *str, int maxcpu) {
    int ret;
    char *res, *cpuset;
    const char *cur = str;

    if (str == NULL)
        return(NULL);

    if (maxcpu <= 0)
        maxcpu = 4096;

434
    cpuset = calloc(maxcpu, sizeof(*cpuset));
435 436 437 438
    if (cpuset == NULL) {
	virXMLError(conn, VIR_ERR_NO_MEMORY, _("allocate buffer"), 0);
	return(NULL);
    }
439

440 441 442 443 444 445 446 447 448
    ret = virParseCpuSet(conn, &cur, 0, cpuset, maxcpu);
    if (ret < 0) {
        free(cpuset);
	return(NULL);
    }
    res = virSaveCpuSet(conn, cpuset, maxcpu);
    free(cpuset);
    return (res);
}
449
#endif /* WITH_XEN */
450
#ifndef PROXY
451 452 453 454 455 456 457

/************************************************************************
 *									*
 * Wrappers around libxml2 XPath specific functions			*
 *									*
 ************************************************************************/

458 459 460 461 462 463 464 465 466 467 468
/**
 * virXPathString:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 *
 * Convenience function to evaluate an XPath string
 *
 * Returns a new string which must be deallocated by the caller or NULL
 *         if the evaluation failed.
 */
char *
469 470
virXPathString(const char *xpath, xmlXPathContextPtr ctxt)
{
471 472 473 474
    xmlXPathObjectPtr obj;
    char *ret;

    if ((ctxt == NULL) || (xpath == NULL)) {
475
        virXMLError(NULL, VIR_ERR_INTERNAL_ERROR,
476
                    _("Invalid parameter to virXPathString()"), 0);
477
        return (NULL);
478 479 480
    }
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
D
Daniel P. Berrange 已提交
481 482 483
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
        if (obj)
            xmlXPathFreeObject(obj);
484
        return (NULL);
D
Daniel P. Berrange 已提交
485
    }
486 487 488
    ret = strdup((char *) obj->stringval);
    xmlXPathFreeObject(obj);
    if (ret == NULL) {
489
        virXMLError(NULL, VIR_ERR_NO_MEMORY, _("strdup failed"), 0);
490
    }
491
    return (ret);
492 493 494 495 496 497 498 499 500 501 502 503 504 505
}

/**
 * virXPathNumber:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @value: the returned double value
 *
 * Convenience function to evaluate an XPath number
 *
 * Returns 0 in case of success in which case @value is set,
 *         or -1 if the evaluation failed.
 */
int
506 507
virXPathNumber(const char *xpath, xmlXPathContextPtr ctxt, double *value)
{
508 509 510
    xmlXPathObjectPtr obj;

    if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
511
        virXMLError(NULL, VIR_ERR_INTERNAL_ERROR,
512
                    _("Invalid parameter to virXPathNumber()"), 0);
513
        return (-1);
514 515 516 517
    }
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NUMBER) ||
        (isnan(obj->floatval))) {
518 519
        xmlXPathFreeObject(obj);
        return (-1);
520
    }
521

522 523
    *value = obj->floatval;
    xmlXPathFreeObject(obj);
524
    return (0);
525 526 527 528 529 530 531 532 533 534 535
}

/**
 * virXPathLong:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @value: the returned long value
 *
 * Convenience function to evaluate an XPath number
 *
 * Returns 0 in case of success in which case @value is set,
536 537
 *         or -1 if the XPath evaluation failed or -2 if the
 *         value doesn't have a long format.
538 539
 */
int
540 541
virXPathLong(const char *xpath, xmlXPathContextPtr ctxt, long *value)
{
542 543 544 545
    xmlXPathObjectPtr obj;
    int ret = 0;

    if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
546
        virXMLError(NULL, VIR_ERR_INTERNAL_ERROR,
547
                    _("Invalid parameter to virXPathNumber()"), 0);
548
        return (-1);
549 550 551 552 553
    }
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        char *conv = NULL;
554
        long val;
555

556 557
        val = strtol((const char *) obj->stringval, &conv, 10);
        if (conv == (const char *) obj->stringval) {
558 559
            ret = -2;
        } else {
560 561
            *value = val;
        }
562 563
    } else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
               (!(isnan(obj->floatval)))) {
564 565 566 567
        *value = (long) obj->floatval;
        if (*value != obj->floatval) {
            ret = -2;
        }
568
    } else {
569
        ret = -1;
570
    }
571

572
    xmlXPathFreeObject(obj);
573
    return (ret);
574 575 576 577 578 579 580 581 582 583 584 585
}

/**
 * virXPathBoolean:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 *
 * Convenience function to evaluate an XPath boolean
 *
 * Returns 0 if false, 1 if true, or -1 if the evaluation failed.
 */
int
586 587
virXPathBoolean(const char *xpath, xmlXPathContextPtr ctxt)
{
588 589 590 591
    xmlXPathObjectPtr obj;
    int ret;

    if ((ctxt == NULL) || (xpath == NULL)) {
592
        virXMLError(NULL, VIR_ERR_INTERNAL_ERROR,
593
                    _("Invalid parameter to virXPathBoolean()"), 0);
594
        return (-1);
595 596 597 598
    }
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
    if ((obj == NULL) || (obj->type != XPATH_BOOLEAN) ||
        (obj->boolval < 0) || (obj->boolval > 1)) {
599 600
        xmlXPathFreeObject(obj);
        return (-1);
601 602
    }
    ret = obj->boolval;
603

604
    xmlXPathFreeObject(obj);
605
    return (ret);
606 607 608 609 610 611 612 613 614 615 616 617 618
}

/**
 * virXPathNode:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 *
 * Convenience function to evaluate an XPath node set and returning
 * only one node, the first one in the set if any
 *
 * Returns a pointer to the node or NULL if the evaluation failed.
 */
xmlNodePtr
619 620
virXPathNode(const char *xpath, xmlXPathContextPtr ctxt)
{
621 622 623 624
    xmlXPathObjectPtr obj;
    xmlNodePtr ret;

    if ((ctxt == NULL) || (xpath == NULL)) {
625
        virXMLError(NULL, VIR_ERR_INTERNAL_ERROR,
626
                    _("Invalid parameter to virXPathNode()"), 0);
627
        return (NULL);
628 629 630 631
    }
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
        (obj->nodesetval == NULL) || (obj->nodesetval->nodeNr <= 0) ||
632 633 634
        (obj->nodesetval->nodeTab == NULL)) {
        xmlXPathFreeObject(obj);
        return (NULL);
635
    }
636

637 638
    ret = obj->nodesetval->nodeTab[0];
    xmlXPathFreeObject(obj);
639
    return (ret);
640
}
641

642 643 644 645 646 647 648 649 650 651 652 653
/**
 * virXPathNodeSet:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @list: the returned list of nodes (or NULL if only count matters)
 *
 * Convenience function to evaluate an XPath node set
 *
 * Returns the number of nodes found in which case @list is set (and
 *         must be freed) or -1 if the evaluation failed.
 */
int
654 655 656
virXPathNodeSet(const char *xpath, xmlXPathContextPtr ctxt,
                xmlNodePtr ** list)
{
657 658 659 660
    xmlXPathObjectPtr obj;
    int ret;

    if ((ctxt == NULL) || (xpath == NULL)) {
661
        virXMLError(NULL, VIR_ERR_INTERNAL_ERROR,
662
                    _("Invalid parameter to virXPathNodeSet()"), 0);
663
        return (-1);
664 665 666 667
    }
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
        (obj->nodesetval == NULL) || (obj->nodesetval->nodeNr <= 0) ||
668 669 670 671 672
        (obj->nodesetval->nodeTab == NULL)) {
        xmlXPathFreeObject(obj);
        if (list != NULL)
            *list = NULL;
        return (-1);
673
    }
674

675 676
    ret = obj->nodesetval->nodeNr;
    if (list != NULL) {
677
        *list = malloc(ret * sizeof(**list));
678 679 680
        if (*list == NULL) {
            virXMLError(NULL, VIR_ERR_NO_MEMORY,
                        _("allocate string array"),
681
                        ret * sizeof(**list));
682 683 684 685
        } else {
            memcpy(*list, obj->nodesetval->nodeTab,
                   ret * sizeof(xmlNodePtr));
        }
686 687
    }
    xmlXPathFreeObject(obj);
688
    return (ret);
689 690
}

691 692 693 694 695
/************************************************************************
 *									*
 * Converter functions to go from the XML tree to an S-Expr for Xen	*
 *									*
 ************************************************************************/
696
#if WITH_XEN
697
/**
698
 * virtDomainParseXMLGraphicsDescImage:
699
 * @conn: pointer to the hypervisor connection
700 701
 * @node: node containing graphics description
 * @buf: a buffer for the result S-Expr
702
 * @xendConfigVersion: xend configuration file format
703
 *
704 705 706
 * 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
707 708 709 710
 * valid over time.
 *
 * Returns 0 in case of success, -1 in case of error
 */
711 712 713 714
static int
virDomainParseXMLGraphicsDescImage(virConnectPtr conn ATTRIBUTE_UNUSED,
                                   xmlNodePtr node, virBufferPtr buf,
                                   int xendConfigVersion)
715 716 717 718 719 720
{
    xmlChar *graphics_type = NULL;

    graphics_type = xmlGetProp(node, BAD_CAST "type");
    if (graphics_type != NULL) {
        if (xmlStrEqual(graphics_type, BAD_CAST "sdl")) {
721
            virBufferAddLit(buf, "(sdl 1)");
722 723 724
            /* TODO:
             * Need to understand sdl options
             *
725 726
             *virBufferAddLit(buf, "(display localhost:10.0)");
             *virBufferAddLit(buf, "(xauthority /root/.Xauthority)");
727
             */
728
        } else if (xmlStrEqual(graphics_type, BAD_CAST "vnc")) {
729
            virBufferAddLit(buf, "(vnc 1)");
730
            if (xendConfigVersion >= 2) {
731
                xmlChar *vncport = xmlGetProp(node, BAD_CAST "port");
732 733
                xmlChar *vnclisten = xmlGetProp(node, BAD_CAST "listen");
                xmlChar *vncpasswd = xmlGetProp(node, BAD_CAST "passwd");
734
                xmlChar *keymap = xmlGetProp(node, BAD_CAST "keymap");
735

736
                if (vncport != NULL) {
737 738
                    long port = strtol((const char *) vncport, NULL, 10);

739
                    if (port == -1)
740
                        virBufferAddLit(buf, "(vncunused 1)");
741
                    else if (port >= 5900)
742 743
                        virBufferVSprintf(buf, "(vncdisplay %ld)",
                                          port - 5900);
744
                    xmlFree(vncport);
745
                }
746 747 748 749 750 751 752 753
                if (vnclisten != NULL) {
                    virBufferVSprintf(buf, "(vnclisten %s)", vnclisten);
                    xmlFree(vnclisten);
                }
                if (vncpasswd != NULL) {
                    virBufferVSprintf(buf, "(vncpasswd %s)", vncpasswd);
                    xmlFree(vncpasswd);
                }
754 755 756 757
                if (keymap != NULL) {
                    virBufferVSprintf(buf, "(keymap %s)", keymap);
                    xmlFree(keymap);
                }
758 759
            }
        }
760 761 762 763 764 765
        xmlFree(graphics_type);
    }
    return 0;
}


766 767
/**
 * virtDomainParseXMLGraphicsDescVFB:
768
 * @conn: pointer to the hypervisor connection
769 770 771
 * @node: node containing graphics description
 * @buf: a buffer for the result S-Expr
 *
772 773 774
 * 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
775 776 777 778
 * valid over time.
 *
 * Returns 0 in case of success, -1 in case of error
 */
779 780 781
static int
virDomainParseXMLGraphicsDescVFB(virConnectPtr conn ATTRIBUTE_UNUSED,
                                 xmlNodePtr node, virBufferPtr buf)
782 783 784 785 786
{
    xmlChar *graphics_type = NULL;

    graphics_type = xmlGetProp(node, BAD_CAST "type");
    if (graphics_type != NULL) {
787 788
        virBufferAddLit(buf, "(device (vkbd))");
        virBufferAddLit(buf, "(device (vfb ");
789
        if (xmlStrEqual(graphics_type, BAD_CAST "sdl")) {
790
            virBufferAddLit(buf, "(type sdl)");
791 792 793
            /* TODO:
             * Need to understand sdl options
             *
794 795
             *virBufferAddLit(buf, "(display localhost:10.0)");
             *virBufferAddLit(buf, "(xauthority /root/.Xauthority)");
796
             */
797
        } else if (xmlStrEqual(graphics_type, BAD_CAST "vnc")) {
798
            virBufferAddLit(buf, "(type vnc)");
799 800 801
            xmlChar *vncport = xmlGetProp(node, BAD_CAST "port");
            xmlChar *vnclisten = xmlGetProp(node, BAD_CAST "listen");
            xmlChar *vncpasswd = xmlGetProp(node, BAD_CAST "passwd");
802
            xmlChar *keymap = xmlGetProp(node, BAD_CAST "keymap");
803

804
            if (vncport != NULL) {
805 806
                long port = strtol((const char *) vncport, NULL, 10);

807
                if (port == -1)
808
                    virBufferAddLit(buf, "(vncunused 1)");
809
                else if (port >= 5900)
810 811
                    virBufferVSprintf(buf, "(vncdisplay %ld)",
                                      port - 5900);
812 813 814 815 816 817 818 819 820 821
                xmlFree(vncport);
            }
            if (vnclisten != NULL) {
                virBufferVSprintf(buf, "(vnclisten %s)", vnclisten);
                xmlFree(vnclisten);
            }
            if (vncpasswd != NULL) {
                virBufferVSprintf(buf, "(vncpasswd %s)", vncpasswd);
                xmlFree(vncpasswd);
            }
822 823 824 825
            if (keymap != NULL) {
                virBufferVSprintf(buf, "(keymap %s)", keymap);
                xmlFree(keymap);
            }
826
        }
827
        virBufferAddLit(buf, "))");
828 829 830 831 832 833
        xmlFree(graphics_type);
    }
    return 0;
}


834
/**
835
 * virDomainParseXMLOSDescHVM:
836
 * @conn: pointer to the hypervisor connection
837
 * @node: node containing HVM OS description
838
 * @buf: a buffer for the result S-Expr
839
 * @ctxt: a path context representing the XML description
840
 * @vcpus: number of virtual CPUs to configure
841
 * @xendConfigVersion: xend configuration file format
842
 * @hasKernel: whether the domain is booting from a kernel
843
 *
844 845
 * Parse the OS part of the XML description for a HVM domain
 * and add it to the S-Expr in buf.
846 847 848 849
 *
 * Returns 0 in case of success, -1 in case of error.
 */
static int
850 851
virDomainParseXMLOSDescHVM(virConnectPtr conn, xmlNodePtr node,
                           virBufferPtr buf, xmlXPathContextPtr ctxt,
852 853
                           int vcpus, int xendConfigVersion,
                           int hasKernel)
854 855
{
    xmlNodePtr cur, txt;
856
    xmlNodePtr *nodes = NULL;
857
    xmlChar *loader = NULL;
858 859
    char bootorder[5];
    int nbootorder = 0;
860
    int res, nb_nodes;
861
    char *str;
862 863 864 865

    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
866 867
            if ((loader == NULL) &&
                (xmlStrEqual(cur->name, BAD_CAST "loader"))) {
868 869
                txt = cur->children;
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
870
                    (txt->next == NULL))
871
                    loader = txt->content;
872 873
            } else if ((xmlStrEqual(cur->name, BAD_CAST "boot"))) {
                xmlChar *boot_dev = xmlGetProp(cur, BAD_CAST "dev");
874 875 876 877

                if (nbootorder ==
                    ((sizeof(bootorder) / sizeof(bootorder[0])) - 1)) {
                    virXMLError(conn, VIR_ERR_XML_ERROR,
878
                                _("too many boot devices"), 0);
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
                    return (-1);
                }
                if (xmlStrEqual(boot_dev, BAD_CAST "fd")) {
                    bootorder[nbootorder++] = 'a';
                } else if (xmlStrEqual(boot_dev, BAD_CAST "cdrom")) {
                    bootorder[nbootorder++] = 'd';
                } else if (xmlStrEqual(boot_dev, BAD_CAST "network")) {
                    bootorder[nbootorder++] = 'n';
                } else if (xmlStrEqual(boot_dev, BAD_CAST "hd")) {
                    bootorder[nbootorder++] = 'c';
                } else {
                    xmlFree(boot_dev);
                    /* Any other type of boot dev is unsupported right now */
                    virXMLError(conn, VIR_ERR_XML_ERROR, NULL, 0);
                    return (-1);
                }
                xmlFree(boot_dev);
896 897 898 899
            }
        }
        cur = cur->next;
    }
900 901 902 903 904 905 906
    /*
     * XenD always needs boot order defined for HVM, even if
     * booting off a kernel + initrd, so force to 'c' if nothing
     * else is specified
     */
    if (nbootorder == 0)
        bootorder[nbootorder++] = 'c';
907
    bootorder[nbootorder] = '\0';
908

909
    if (loader == NULL) {
910
        virXMLError(conn, VIR_ERR_INTERNAL_ERROR, _("no HVM domain loader"), 0);
911
        return -1;
912 913
    }

914
    /*
915 916 917 918 919 920 921 922 923
     * Originally XenD abused the 'kernel' parameter for the HVM
     * firmware. New XenD allows HVM guests to boot from a kernel
     * and if this is enabled, the HVM firmware must use the new
     * 'loader' parameter
     */
    if (hasKernel) {
        virBufferVSprintf(buf, "(loader '%s')", (const char *) loader);
    } else {
        virBufferVSprintf(buf, "(kernel '%s')", (const char *) loader);
924 925
    }

926 927
    virBufferVSprintf(buf, "(vcpus %d)", vcpus);

928 929
    if (nbootorder)
        virBufferVSprintf(buf, "(boot %s)", bootorder);
930

931
    /* get the 1st floppy device file */
932 933 934 935
    cur = virXPathNode(
         "/domain/devices/disk[@device='floppy' and target/@dev='fda']/source",
         ctxt);
    if (cur != NULL) {
936
        xmlChar *fdfile;
937

938
        fdfile = xmlGetProp(cur, BAD_CAST "file");
939
        if (fdfile != NULL) {
940 941
            virBufferVSprintf(buf, "(fda '%s')", fdfile);
            free(fdfile);
942
        }
943
    }
944

945
    /* get the 2nd floppy device file */
946 947 948 949
    cur = virXPathNode(
         "/domain/devices/disk[@device='floppy' and target/@dev='fdb']/source",
         ctxt);
    if (cur != NULL) {
950
        xmlChar *fdfile;
951

952
        fdfile = xmlGetProp(cur, BAD_CAST "file");
953
        if (fdfile != NULL) {
954 955
            virBufferVSprintf(buf, "(fdb '%s')", fdfile);
            free(fdfile);
956
        }
957
    }
958 959


960 961 962
    /* get the cdrom device file */
    /* Only XenD <= 3.0.2 wants cdrom config here */
    if (xendConfigVersion == 1) {
963 964 965 966
        cur = virXPathNode(
	  "/domain/devices/disk[@device='cdrom' and target/@dev='hdc']/source",
             ctxt);
        if (cur != NULL) {
967 968 969 970 971
            xmlChar *cdfile;

            cdfile = xmlGetProp(cur, BAD_CAST "file");
            if (cdfile != NULL) {
                virBufferVSprintf(buf, "(cdrom '%s')",
972
                                  (const char *) cdfile);
973
                xmlFree(cdfile);
974 975
            }
        }
976 977
    }

978
    if (virXPathNode("/domain/features/acpi", ctxt) != NULL)
979
        virBufferAddLit(buf, "(acpi 1)");
980
    if (virXPathNode("/domain/features/apic", ctxt) != NULL)
981
        virBufferAddLit(buf, "(apic 1)");
982
    if (virXPathNode("/domain/features/pae", ctxt) != NULL)
983
        virBufferAddLit(buf, "(pae 1)");
984

985
    virBufferAddLit(buf, "(usb 1)");
986 987 988
    nb_nodes = virXPathNodeSet("/domain/devices/input", ctxt, &nodes);
    if (nb_nodes > 0) {
        int i;
989

990 991 992 993
        for (i = 0; i < nb_nodes; i++) {
            xmlChar *itype = NULL, *bus = NULL;
            int isMouse = 1;

994
            itype = xmlGetProp(nodes[i], (xmlChar *) "type");
995 996 997 998

            if (!itype) {
                goto error;
            }
999
            if (!strcmp((const char *) itype, "tablet"))
1000
                isMouse = 0;
1001
            else if (strcmp((const char *) itype, "mouse")) {
1002
                xmlFree(itype);
1003 1004
                virXMLError(conn, VIR_ERR_XML_ERROR,
                            _("invalid input device"), 0);
1005 1006 1007 1008
                goto error;
            }
            xmlFree(itype);

1009
            bus = xmlGetProp(nodes[i], (xmlChar *) "bus");
1010 1011 1012 1013
            if (!bus) {
                if (!isMouse) {
                    /* Nothing - implicit ps2 */
                } else {
1014
                    virBufferAddLit(buf, "(usbdevice tablet)");
1015 1016
                }
            } else {
1017
                if (!strcmp((const char *) bus, "ps2")) {
1018 1019
                    if (!isMouse) {
                        xmlFree(bus);
1020 1021
                        virXMLError(conn, VIR_ERR_XML_ERROR,
                                    _("invalid input device"), 0);
1022 1023 1024
                        goto error;
                    }
                    /* Nothing - implicit ps2 */
1025
                } else if (!strcmp((const char *) bus, "usb")) {
1026
                    if (isMouse)
1027
                        virBufferAddLit(buf, "(usbdevice mouse)");
1028
                    else
1029
                        virBufferAddLit(buf, "(usbdevice tablet)");
1030 1031 1032 1033 1034 1035 1036 1037 1038
                }
            }
            xmlFree(bus);
        }
        free(nodes);
        nodes = NULL;
    }


1039 1040
    res = virXPathBoolean("count(domain/devices/console) > 0", ctxt);
    if (res < 0) {
1041
        virXMLError(conn, VIR_ERR_XML_ERROR, NULL, 0);
1042
        goto error;
1043
    }
1044
    if (res) {
1045
        virBufferAddLit(buf, "(serial pty)");
1046
    }
1047

1048 1049
    str = virXPathString("string(/domain/clock/@offset)", ctxt);
    if (str != NULL && !strcmp(str, "localtime")) {
1050
        virBufferAddLit(buf, "(localtime 1)");
1051
    }
1052
    free(str);
1053

1054
    return (0);
1055

1056
  error:
1057
    free(nodes);
1058
    return (-1);
1059 1060
}

1061

1062
/**
1063
 * virDomainParseXMLOSDescKernel:
1064
 * @conn: pointer to the hypervisor connection
1065 1066 1067
 * @node: node containing PV OS description
 * @buf: a buffer for the result S-Expr
 *
1068 1069
 * Parse the OS part of the XML description for a domain using a direct
 * kernel and initrd to boot.
1070 1071 1072 1073
 *
 * Returns 0 in case of success, -1 in case of error.
 */
static int
1074 1075 1076
virDomainParseXMLOSDescKernel(virConnectPtr conn ATTRIBUTE_UNUSED,
                              xmlNodePtr node,
                              virBufferPtr buf)
1077
{
1078 1079 1080 1081 1082 1083 1084 1085 1086
    xmlNodePtr cur, txt;
    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) {
1087 1088
            if ((kernel == NULL) &&
                (xmlStrEqual(cur->name, BAD_CAST "kernel"))) {
1089
                txt = cur->children;
1090
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
1091
                    (txt->next == NULL))
1092 1093 1094 1095
                    kernel = txt->content;
            } else if ((root == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "root"))) {
                txt = cur->children;
1096
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
1097
                    (txt->next == NULL))
1098 1099 1100 1101
                    root = txt->content;
            } else if ((initrd == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "initrd"))) {
                txt = cur->children;
1102
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
1103
                    (txt->next == NULL))
1104 1105 1106 1107
                    initrd = txt->content;
            } else if ((cmdline == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "cmdline"))) {
                txt = cur->children;
1108
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
1109
                    (txt->next == NULL))
1110 1111 1112
                    cmdline = txt->content;
            }
        }
1113 1114
        cur = cur->next;
    }
1115

1116 1117
    virBufferVSprintf(buf, "(kernel '%s')", (const char *) kernel);

1118
    if (initrd != NULL)
1119
        virBufferVSprintf(buf, "(ramdisk '%s')", (const char *) initrd);
1120
    if (root != NULL)
1121
        virBufferVSprintf(buf, "(root '%s')", (const char *) root);
1122
    if (cmdline != NULL)
1123
        virBufferVSprintf(buf, "(args '%s')", (const char *) cmdline);
1124

1125
    return (0);
1126 1127
}

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
/**
 * 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
1138 1139
virCatchXMLParseError(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
{
1140 1141
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;

1142
    if ((ctxt != NULL) &&
1143
        (ctxt->lastError.level == XML_ERR_FATAL) &&
1144
        (ctxt->lastError.message != NULL)) {
1145
        virXMLError(NULL, VIR_ERR_XML_DETAIL, ctxt->lastError.message,
1146
                    ctxt->lastError.line);
1147 1148 1149
    }
}

1150 1151
/**
 * virDomainParseXMLDiskDesc:
1152
 * @node: node containing disk description
1153
 * @conn: pointer to the hypervisor connection
1154
 * @buf: a buffer for the result S-Expr
1155
 * @xendConfigVersion: xend configuration file format
1156 1157 1158 1159 1160 1161 1162 1163 1164
 *
 * 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
1165 1166
virDomainParseXMLDiskDesc(virConnectPtr conn, xmlNodePtr node,
                          virBufferPtr buf, int hvm, int xendConfigVersion)
1167
{
1168 1169
    xmlNodePtr cur;
    xmlChar *type = NULL;
1170
    xmlChar *device = NULL;
1171 1172
    xmlChar *source = NULL;
    xmlChar *target = NULL;
1173 1174
    xmlChar *drvName = NULL;
    xmlChar *drvType = NULL;
1175
    int ro = 0;
1176
    int shareable = 0;
1177
    int typ = 0;
1178
    int cdrom = 0;
1179
    int isNoSrcCdrom = 0;
1180
    int ret = 0;
1181 1182 1183

    type = xmlGetProp(node, BAD_CAST "type");
    if (type != NULL) {
1184 1185 1186 1187 1188
        if (xmlStrEqual(type, BAD_CAST "file"))
            typ = 0;
        else if (xmlStrEqual(type, BAD_CAST "block"))
            typ = 1;
        xmlFree(type);
1189
    }
1190
    device = xmlGetProp(node, BAD_CAST "device");
1191

1192 1193 1194
    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
            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");
1205 1206 1207
            } else if ((drvName == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "driver"))) {
                drvName = xmlGetProp(cur, BAD_CAST "name");
1208
                if (drvName && !strcmp((const char *) drvName, "tap"))
1209
                    drvType = xmlGetProp(cur, BAD_CAST "type");
1210 1211
            } else if (xmlStrEqual(cur->name, BAD_CAST "readonly")) {
                ro = 1;
1212
            } else if (xmlStrEqual(cur->name, BAD_CAST "shareable")) {
1213
                shareable = 1;
1214 1215
            }
        }
1216 1217 1218 1219
        cur = cur->next;
    }

    if (source == NULL) {
1220 1221 1222
        /* There is a case without the source
         * to the CD-ROM device
         */
1223
        if (hvm && device && !strcmp((const char *) device, "cdrom")) {
1224 1225 1226 1227
            isNoSrcCdrom = 1;
        }
        if (!isNoSrcCdrom) {
            virXMLError(conn, VIR_ERR_NO_SOURCE, (const char *) target, 0);
1228 1229
            ret = -1;
            goto cleanup;
1230
        }
1231 1232
    }
    if (target == NULL) {
1233
        virXMLError(conn, VIR_ERR_NO_TARGET, (const char *) source, 0);
1234 1235
        ret = -1;
        goto cleanup;
1236
    }
1237

1238 1239
    /* Xend (all versions) put the floppy device config
     * under the hvm (image (os)) block
1240
     */
1241
    if (hvm && device && !strcmp((const char *) device, "floppy")) {
1242
        goto cleanup;
1243 1244 1245
    }

    /* Xend <= 3.0.2 doesn't include cdrom config here */
1246
    if (hvm && device && !strcmp((const char *) device, "cdrom")) {
1247
        if (xendConfigVersion == 1)
1248
            goto cleanup;
1249 1250
        else
            cdrom = 1;
1251 1252 1253
    }


1254
    virBufferAddLit(buf, "(device ");
1255
    /* Normally disks are in a (device (vbd ...)) block
1256 1257 1258
     * but blktap disks ended up in a differently named
     * (device (tap ....)) block.... */
    if (drvName && !strcmp((const char *) drvName, "tap")) {
1259
        virBufferAddLit(buf, "(tap ");
1260
    } else {
1261
        virBufferAddLit(buf, "(vbd ");
1262
    }
1263

1264
    if (hvm) {
1265 1266
        char *tmp = (char *) target;

1267
        /* Just in case user mistakenly still puts ioemu: in their XML */
1268 1269
        if (!strncmp((const char *) tmp, "ioemu:", 6))
            tmp += 6;
1270 1271 1272

        /* Xend <= 3.0.2 wants a ioemu: prefix on devices for HVM */
        if (xendConfigVersion == 1)
1273 1274 1275 1276
            virBufferVSprintf(buf, "(dev 'ioemu:%s')", (const char *) tmp);
        else                    /* But newer does not */
            virBufferVSprintf(buf, "(dev '%s%s')", (const char *) tmp,
                              cdrom ? ":cdrom" : ":disk");
1277
    } else
1278
        virBufferVSprintf(buf, "(dev '%s')", (const char *) target);
1279

1280
    if (drvName && !isNoSrcCdrom) {
1281
        if (!strcmp((const char *) drvName, "tap")) {
1282
            virBufferVSprintf(buf, "(uname '%s:%s:%s')",
1283 1284 1285
                              (const char *) drvName,
                              (drvType ? (const char *) drvType : "aio"),
                              (const char *) source);
1286 1287
        } else {
            virBufferVSprintf(buf, "(uname '%s:%s')",
1288 1289
                              (const char *) drvName,
                              (const char *) source);
1290
        }
1291
    } else if (!isNoSrcCdrom) {
1292 1293 1294 1295 1296 1297 1298 1299
        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);
        }
1300
    }
1301
    if (ro == 1)
1302
        virBufferVSprintf(buf, "(mode 'r')");
1303 1304 1305 1306
    else if (shareable == 1)
        virBufferVSprintf(buf, "(mode 'w!')");
    else
        virBufferVSprintf(buf, "(mode 'w')");
1307

1308 1309
    virBufferAddLit(buf, ")");
    virBufferAddLit(buf, ")");
1310

1311 1312
  cleanup:
    if (drvType)
1313
        xmlFree(drvType);
1314
    if (drvName)
1315
        xmlFree(drvName);
1316
    if (device)
1317
        xmlFree(device);
1318
    if (target)
1319
        xmlFree(target);
1320
    if (source)
1321 1322
        xmlFree(source);
    return (ret);
1323 1324 1325 1326
}

/**
 * virDomainParseXMLIfDesc:
1327
 * @conn: pointer to the hypervisor connection
1328
 * @node: node containing the interface description
1329
 * @buf: a buffer for the result S-Expr
1330
 * @xendConfigVersion: xend configuration file format
1331 1332 1333 1334 1335 1336 1337 1338 1339
 *
 * 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
1340 1341 1342
virDomainParseXMLIfDesc(virConnectPtr conn ATTRIBUTE_UNUSED,
                        xmlNodePtr node, virBufferPtr buf, int hvm,
                        int xendConfigVersion)
1343
{
1344 1345 1346 1347 1348
    xmlNodePtr cur;
    xmlChar *type = NULL;
    xmlChar *source = NULL;
    xmlChar *mac = NULL;
    xmlChar *script = NULL;
1349
    xmlChar *ip = NULL;
1350
    int typ = 0;
1351
    int ret = -1;
1352 1353 1354

    type = xmlGetProp(node, BAD_CAST "type");
    if (type != NULL) {
1355 1356 1357 1358
        if (xmlStrEqual(type, BAD_CAST "bridge"))
            typ = 0;
        else if (xmlStrEqual(type, BAD_CAST "ethernet"))
            typ = 1;
1359 1360
        else if (xmlStrEqual(type, BAD_CAST "network"))
            typ = 2;
1361
        xmlFree(type);
1362 1363 1364 1365
    }
    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
1366 1367 1368 1369
            if ((source == NULL) &&
                (xmlStrEqual(cur->name, BAD_CAST "source"))) {
                if (typ == 0)
                    source = xmlGetProp(cur, BAD_CAST "bridge");
1370
                else if (typ == 1)
1371
                    source = xmlGetProp(cur, BAD_CAST "dev");
1372 1373
                else
                    source = xmlGetProp(cur, BAD_CAST "network");
1374 1375 1376 1377 1378 1379
            } 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");
1380 1381 1382
            } else if ((ip == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "ip"))) {
                /* XXX in future expect to need to have > 1 ip
1383 1384 1385
                 * address element - eg ipv4 & ipv6. For now
                 * xen only supports a single address though
                 * so lets ignore that complication */
1386
                ip = xmlGetProp(cur, BAD_CAST "address");
1387 1388
            }
        }
1389 1390 1391
        cur = cur->next;
    }

1392
    virBufferAddLit(buf, "(vif ");
1393 1394
    if (mac != NULL) {
        unsigned int addr[12];
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
        int tmp = sscanf((const char *) mac,
		     "%01x%01x:%01x%01x:%01x%01x:%01x%01x:%01x%01x:%01x%01x",
                         (unsigned int *) &addr[0],
                         (unsigned int *) &addr[1],
                         (unsigned int *) &addr[2],
                         (unsigned int *) &addr[3],
                         (unsigned int *) &addr[4],
                         (unsigned int *) &addr[5],
                         (unsigned int *) &addr[6],
                         (unsigned int *) &addr[7],
                         (unsigned int *) &addr[8],
                         (unsigned int *) &addr[9],
                         (unsigned int *) &addr[10],
                         (unsigned int *) &addr[11]);
1409 1410 1411 1412
        if (tmp != 12 || strlen((const char *) mac) != 17) {
            virXMLError(conn, VIR_ERR_INVALID_MAC, (const char *) mac, 0);
            goto error;
        }
1413
        virBufferVSprintf(buf, "(mac '%s')", (const char *) mac);
1414
    }
1415
    if (source != NULL) {
1416 1417
        if (typ == 0)
            virBufferVSprintf(buf, "(bridge '%s')", (const char *) source);
1418
        else if (typ == 1)      /* TODO does that work like that ? */
1419
            virBufferVSprintf(buf, "(dev '%s')", (const char *) source);
1420
        else {
1421 1422
            virNetworkPtr network =
                virNetworkLookupByName(conn, (const char *) source);
1423
            char *bridge;
1424

1425
            if (!network || !(bridge = virNetworkGetBridgeName(network))) {
1426 1427
                if (network)
                    virNetworkFree(network);
1428 1429
                virXMLError(conn, VIR_ERR_NO_SOURCE, (const char *) source,
                            0);
1430 1431
                goto error;
            }
1432
            virNetworkFree(network);
1433 1434 1435
            virBufferVSprintf(buf, "(bridge '%s')", bridge);
            free(bridge);
        }
1436 1437 1438
    }
    if (script != NULL)
        virBufferVSprintf(buf, "(script '%s')", script);
1439 1440
    if (ip != NULL)
        virBufferVSprintf(buf, "(ip '%s')", ip);
1441 1442 1443 1444 1445
    /*
     * apparently (type ioemu) breaks paravirt drivers on HVM so skip this
     * from Xen 3.1.0
     */
    if ((hvm) && (xendConfigVersion < 4))
1446
        virBufferAddLit(buf, "(type ioemu)");
1447

1448
    virBufferAddLit(buf, ")");
1449
    ret = 0;
1450
  error:
1451
    if (mac != NULL)
1452
        xmlFree(mac);
1453
    if (source != NULL)
1454
        xmlFree(source);
1455
    if (script != NULL)
1456
        xmlFree(script);
1457 1458
    if (ip != NULL)
        xmlFree(ip);
1459
    return (ret);
1460 1461 1462 1463
}

/**
 * virDomainParseXMLDesc:
1464
 * @conn: pointer to the hypervisor connection
1465
 * @xmldesc: string with the XML description
1466
 * @xendConfigVersion: xend configuration file format
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
 *
 * 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 *
1477 1478
virDomainParseXMLDesc(virConnectPtr conn, const char *xmldesc, char **name,
                      int xendConfigVersion)
1479
{
1480 1481
    xmlDocPtr xml = NULL;
    xmlNodePtr node;
1482
    char *nam = NULL;
1483 1484
    virBuffer buf;
    xmlChar *prop;
1485
    xmlParserCtxtPtr pctxt;
1486 1487
    xmlXPathContextPtr ctxt = NULL;
    int i, res;
1488
    int bootloader = 0;
1489
    int hvm = 0;
1490
    unsigned int vcpus = 1;
1491
    unsigned long mem = 0, max_mem = 0;
1492 1493 1494 1495
    char *str;
    double f;
    xmlNodePtr *nodes;
    int nb_nodes;
1496 1497

    if (name != NULL)
1498
        *name = NULL;
1499 1500
    buf.content = malloc(1000);
    if (buf.content == NULL)
1501
        return (NULL);
1502 1503 1504
    buf.size = 1000;
    buf.use = 0;

1505 1506 1507 1508 1509
    pctxt = xmlNewParserCtxt();
    if ((pctxt == NULL) || (pctxt->sax == NULL)) {
        goto error;
    }

1510 1511 1512
    /* TODO pass the connection point to the error handler:
     *   pctxt->userData = virConnectPtr;
     */
1513 1514
    pctxt->sax->error = virCatchXMLParseError;

1515 1516
    xml = xmlCtxtReadDoc(pctxt, (const xmlChar *) xmldesc, "domain.xml",
                         NULL, XML_PARSE_NOENT | XML_PARSE_NONET |
1517
                         XML_PARSE_NOWARNING);
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
    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")) {
1528 1529 1530 1531
            xmlFree(prop);
            goto error;
        }
        xmlFree(prop);
1532
    }
1533
    virBufferAddLit(&buf, "(vm ");
1534 1535 1536 1537 1538
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
        goto error;
    }
    /*
1539
     * extract some of the basics, name, memory, cpus ...
1540
     */
1541
    nam = virXPathString("string(/domain/name[1])", ctxt);
1542
    if (nam == NULL) {
1543
        virXMLError(conn, VIR_ERR_NO_NAME, xmldesc, 0);
1544
        goto error;
1545
    }
1546
    virBufferVSprintf(&buf, "(name '%s')", nam);
1547

1548 1549
    if ((virXPathNumber("number(/domain/memory[1])", ctxt, &f) < 0) ||
        (f < MIN_XEN_GUEST_SIZE * 1024)) {
1550
        max_mem = 128;
1551
    } else {
1552
        max_mem = (f / 1024);
1553
    }
1554

1555 1556
    if ((virXPathNumber("number(/domain/currentMemory[1])", ctxt, &f) < 0)
        || (f < MIN_XEN_GUEST_SIZE * 1024)) {
1557 1558
        mem = max_mem;
    } else {
1559
        mem = (f / 1024);
1560 1561 1562
        if (mem > max_mem) {
            max_mem = mem;
        }
1563
    }
1564
    virBufferVSprintf(&buf, "(memory %lu)(maxmem %lu)", mem, max_mem);
1565

1566 1567 1568
    if ((virXPathNumber("number(/domain/vcpu[1])", ctxt, &f) == 0) &&
        (f > 0)) {
        vcpus = (unsigned int) f;
1569
    }
1570
    virBufferVSprintf(&buf, "(vcpus %u)", vcpus);
1571

1572 1573 1574
    str = virXPathString("string(/domain/vcpu/@cpuset)", ctxt);
    if (str != NULL) {
        int maxcpu = xenNbCpus(conn);
1575 1576 1577 1578 1579 1580 1581 1582 1583
        char *cpuset = NULL;
        char *ranges = NULL;
        const char *cur = str;

        /*
         * Parse the CPUset attribute given in libvirt format and reserialize
         * it in a range format guaranteed to be understood by Xen.
         */
        if (maxcpu > 0) {
1584
            cpuset = malloc(maxcpu * sizeof(*cpuset));
1585 1586 1587
            if (cpuset != NULL) {
                res = virParseCpuSet(conn, &cur, 0, cpuset, maxcpu);
                if (res > 0) {
1588
                    ranges = virSaveCpuSet(conn, cpuset, maxcpu);
1589 1590 1591 1592 1593 1594
                    if (ranges != NULL) {
                        virBufferVSprintf(&buf, "(cpus '%s')", ranges);
                        free(ranges);
                    }
                }
                free(cpuset);
1595
                if (res < 0)
1596
                    goto error;
1597 1598 1599 1600
            } else {
                virXMLError(conn, VIR_ERR_NO_MEMORY, xmldesc, 0);
            }
        }
1601 1602 1603
        free(str);
    }

1604 1605 1606
    str = virXPathString("string(/domain/uuid[1])", ctxt);
    if (str != NULL) {
        virBufferVSprintf(&buf, "(uuid '%s')", str);
1607
        free(str);
1608 1609
    }

1610 1611 1612
    str = virXPathString("string(/domain/bootloader[1])", ctxt);
    if (str != NULL) {
        virBufferVSprintf(&buf, "(bootloader '%s')", str);
1613
        /*
1614
         * if using a bootloader, the kernel and initrd strings are not
1615 1616
         * significant and should be discarded
         */
1617
        bootloader = 1;
1618 1619 1620
        free(str);
    } else if (virXPathNumber("count(/domain/bootloader)", ctxt, &f) == 0
               && (f > 0)) {
D
Daniel P. Berrange 已提交
1621 1622 1623 1624 1625
        virBufferVSprintf(&buf, "(bootloader)");
        /*
         * if using a bootloader, the kernel and initrd strings are not
         * significant and should be discarded
         */
1626
        bootloader = 1;
1627 1628 1629 1630 1631 1632 1633 1634
    }

    str = virXPathString("string(/domain/bootloader_args[1])", ctxt);
    if (str != NULL && bootloader) {
        /*
         * ignore the bootloader_args value unless a bootloader was specified
         */
        virBufferVSprintf(&buf, "(bootloader_args '%s')", str);
1635
        free(str);
1636 1637
    }

1638 1639 1640
    str = virXPathString("string(/domain/on_poweroff[1])", ctxt);
    if (str != NULL) {
        virBufferVSprintf(&buf, "(on_poweroff '%s')", str);
1641
        free(str);
1642 1643
    }

1644 1645 1646
    str = virXPathString("string(/domain/on_reboot[1])", ctxt);
    if (str != NULL) {
        virBufferVSprintf(&buf, "(on_reboot '%s')", str);
1647
        free(str);
1648 1649
    }

1650 1651 1652
    str = virXPathString("string(/domain/on_crash[1])", ctxt);
    if (str != NULL) {
        virBufferVSprintf(&buf, "(on_crash '%s')", str);
1653
        free(str);
1654 1655
    }

1656
    if (!bootloader) {
1657
        if ((node = virXPathNode("/domain/os[1]", ctxt)) != NULL) {
1658 1659
            int has_kernel = 0;

1660
            /* Analyze of the os description, based on HVM or PV. */
1661
            str = virXPathString("string(/domain/os/type[1])", ctxt);
1662
            if ((str != NULL) && STREQ(str, "hvm"))
1663
                hvm = 1;
1664 1665
            xmlFree(str);
            str = NULL;
1666

1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
            if (hvm)
                virBufferAddLit(&buf, "(image (hvm ");
            else
                virBufferAddLit(&buf, "(image (linux ");

            if (virXPathBoolean("count(/domain/os/kernel) > 0", ctxt)) {
                if (virDomainParseXMLOSDescKernel(conn, node,
                                                  &buf) != 0)
                    goto error;
                has_kernel = 1;
            }
1678

1679 1680 1681 1682 1683
            if (hvm &&
                virDomainParseXMLOSDescHVM(conn, node,
                                           &buf, ctxt, vcpus,
                                           xendConfigVersion,
                                           has_kernel) != 0)
1684
                goto error;
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705

            /* get the device emulation model */
            str = virXPathString("string(/domain/devices/emulator[1])", ctxt);
            if (str != NULL) {
                virBufferVSprintf(&buf, "(device_model '%s')", str);
                xmlFree(str);
                str = NULL;
            }

            /* PV graphics for xen <= 3.0.4, or HVM graphics for xen <= 3.1.0 */
            if ((!hvm && xendConfigVersion < 3) ||
                (hvm && xendConfigVersion < 4)) {
                xmlNodePtr cur;
                cur = virXPathNode("/domain/devices/graphics[1]", ctxt);
                if (cur != NULL &&
                    virDomainParseXMLGraphicsDescImage(conn, cur, &buf,
                                                       xendConfigVersion) != 0)
                    goto error;
            }

            virBufferAddLit(&buf, "))");
1706
        } else {
1707
            virXMLError(conn, VIR_ERR_NO_OS, nam, 0);
1708 1709
            goto error;
        }
1710 1711 1712
    }

    /* analyze of the devices */
1713 1714 1715 1716
    nb_nodes = virXPathNodeSet("/domain/devices/disk", ctxt, &nodes);
    if (nb_nodes > 0) {
        for (i = 0; i < nb_nodes; i++) {
            res = virDomainParseXMLDiskDesc(conn, nodes[i], &buf,
1717
                                            hvm, xendConfigVersion);
1718
            if (res != 0) {
1719
                free(nodes);
1720 1721 1722
                goto error;
            }
        }
1723
        free(nodes);
1724
    }
1725

1726 1727 1728
    nb_nodes = virXPathNodeSet("/domain/devices/interface", ctxt, &nodes);
    if (nb_nodes > 0) {
        for (i = 0; i < nb_nodes; i++) {
1729
            virBufferAddLit(&buf, "(device ");
1730 1731 1732
            res =
                virDomainParseXMLIfDesc(conn, nodes[i], &buf, hvm,
                                        xendConfigVersion);
1733
            if (res != 0) {
1734
                free(nodes);
1735 1736
                goto error;
            }
1737
            virBufferAddLit(&buf, ")");
1738
        }
1739
        free(nodes);
1740 1741
    }

1742 1743 1744 1745
    /* New style PV graphics config xen >= 3.0.4,
     * or HVM graphics config xen >= 3.0.5 */
    if ((xendConfigVersion >= 3 && !hvm) ||
        (xendConfigVersion >= 4 && hvm)) {
1746
        nb_nodes = virXPathNodeSet("/domain/devices/graphics", ctxt, &nodes);
1747
        if (nb_nodes > 0) {
1748 1749
            for (i = 0; i < nb_nodes; i++) {
                res = virDomainParseXMLGraphicsDescVFB(conn, nodes[i], &buf);
1750
                if (res != 0) {
1751
                    free(nodes);
1752 1753 1754
                    goto error;
                }
            }
1755
            free(nodes);
1756 1757 1758
        }
    }

1759

1760
    virBufferAddLit(&buf, ")"); /* closes (vm */
1761 1762 1763 1764
    buf.content[buf.use] = 0;

    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
1765
    xmlFreeParserCtxt(pctxt);
1766 1767

    if (name != NULL)
1768
        *name = nam;
1769 1770
    else
        free(nam);
1771

1772
    return (buf.content);
1773

1774
  error:
1775
    free(nam);
1776
    if (name != NULL)
1777
        *name = NULL;
1778
    xmlXPathFreeContext(ctxt);
1779 1780
    if (xml != NULL)
        xmlFreeDoc(xml);
1781 1782
    if (pctxt != NULL)
        xmlFreeParserCtxt(pctxt);
1783
    free(buf.content);
1784
    return (NULL);
1785
}
1786

1787 1788
/**
 * virParseXMLDevice:
1789
 * @conn: pointer to the hypervisor connection
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
 * @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 *
1803 1804
virParseXMLDevice(virConnectPtr conn, const char *xmldesc, int hvm,
                  int xendConfigVersion)
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
{
    xmlDocPtr xml = NULL;
    xmlNodePtr node;
    virBuffer buf;

    buf.content = malloc(1000);
    if (buf.content == NULL)
        return (NULL);
    buf.size = 1000;
    buf.use = 0;
1815
    buf.content[0] = 0;
1816
    xml = xmlReadDoc((const xmlChar *) xmldesc, "device.xml", NULL,
1817 1818
                     XML_PARSE_NOENT | XML_PARSE_NONET |
                     XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
1819 1820
    if (xml == NULL) {
        virXMLError(conn, VIR_ERR_XML_ERROR, NULL, 0);
1821
        goto error;
1822
    }
1823 1824 1825 1826
    node = xmlDocGetRootElement(xml);
    if (node == NULL)
        goto error;
    if (xmlStrEqual(node->name, BAD_CAST "disk")) {
1827 1828
        if (virDomainParseXMLDiskDesc(conn, node, &buf, hvm,
	                              xendConfigVersion) != 0)
1829
            goto error;
1830
        /* SXP is not created when device is "floppy". */
1831 1832 1833 1834 1835
        else if (buf.use == 0)
            goto error;
    } else if (xmlStrEqual(node->name, BAD_CAST "interface")) {
        if (virDomainParseXMLIfDesc(conn, node, &buf, hvm,
	                            xendConfigVersion) != 0)
1836
            goto error;
1837 1838
    } else {
        virXMLError(conn, VIR_ERR_XML_ERROR, (const char *) node->name, 0);
1839
        goto error;
1840
    }
1841
  cleanup:
1842 1843 1844
    if (xml != NULL)
        xmlFreeDoc(xml);
    return buf.content;
1845
  error:
1846 1847 1848 1849 1850
    free(buf.content);
    buf.content = NULL;
    goto cleanup;
}

1851

1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
/**
 * 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
1867 1868
virDomainXMLDevID(virDomainPtr domain, const char *xmldesc, char *class,
                  char *ref, int ref_len)
1869 1870 1871 1872
{
    xmlDocPtr xml = NULL;
    xmlNodePtr node, cur;
    xmlChar *attr = NULL;
1873

1874
    char *xref;
1875
    int ret = 0;
1876

1877
    xml = xmlReadDoc((const xmlChar *) xmldesc, "device.xml", NULL,
1878 1879
                     XML_PARSE_NOENT | XML_PARSE_NONET |
                     XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
1880 1881
    if (xml == NULL) {
        virXMLError(NULL, VIR_ERR_XML_ERROR, NULL, 0);
1882
        goto error;
1883
    }
1884 1885 1886 1887 1888 1889 1890
    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) ||
1891 1892
                (!xmlStrEqual(cur->name, BAD_CAST "target")))
                continue;
1893 1894 1895
            attr = xmlGetProp(cur, BAD_CAST "dev");
            if (attr == NULL)
                goto error;
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908
            xref = xenStoreDomainGetDiskID(domain->conn, domain->id,
                                              (char *) attr);
            if (xref != NULL) {
                strncpy(ref, xref, ref_len);
                free(xref);
                ref[ref_len - 1] = '\0';
                goto cleanup;
            }
            /* hack to avoid the warning that domain is unused */
            if (domain->id < 0)
                ret = -1;

            goto error;
1909
        }
1910
    } else if (xmlStrEqual(node->name, BAD_CAST "interface")) {
1911 1912 1913
        strcpy(class, "vif");
        for (cur = node->children; cur != NULL; cur = cur->next) {
            if ((cur->type != XML_ELEMENT_NODE) ||
1914 1915
                (!xmlStrEqual(cur->name, BAD_CAST "mac")))
                continue;
1916 1917 1918 1919
            attr = xmlGetProp(cur, BAD_CAST "address");
            if (attr == NULL)
                goto error;

1920
            xref = xenStoreDomainGetNetworkID(domain->conn, domain->id,
1921 1922
                                              (char *) attr);
            if (xref != NULL) {
1923
                strncpy(ref, xref, ref_len);
1924
                free(xref);
1925
                ref[ref_len - 1] = '\0';
1926 1927
                goto cleanup;
            }
1928 1929
            /* hack to avoid the warning that domain is unused */
            if (domain->id < 0)
1930
                ret = -1;
1931

1932 1933
            goto error;
        }
1934 1935
    } else {
        virXMLError(NULL, VIR_ERR_XML_ERROR, (const char *) node->name, 0);
1936
    }
1937
  error:
1938
    ret = -1;
1939
  cleanup:
1940 1941 1942 1943 1944 1945
    if (xml != NULL)
        xmlFreeDoc(xml);
    if (attr != NULL)
        xmlFree(attr);
    return ret;
}
1946
#endif /* WITH_XEN */
1947 1948
#endif /* !PROXY */

1949 1950 1951 1952 1953 1954 1955 1956
/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */