xml.c 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/*
 * 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>
 */

#include "libvir.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <xs.h>
#include "internal.h"
#include "hash.h"

/**
 * virBuffer:
 *
 * A buffer structure.
 */
typedef struct _virBuffer virBuffer;
typedef virBuffer *virBufferPtr;
struct _virBuffer {
    char *content;		/* The buffer content UTF8 */
    unsigned int use;		/* The buffer size used */
    unsigned int size;		/* The buffer size */
};

/**
 * 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
virBufferGrow(virBufferPtr buf, unsigned int len) {
    int size;
    char *newbuf;

    if (buf == NULL) return(-1);
    if (len + buf->use < buf->size) return(0);

    size = buf->use + len + 1000;

    newbuf = (char *) realloc(buf->content, size);
    if (newbuf == NULL) {
        return(-1);
    }
    buf->content = newbuf;
    buf->size = size;
    return(buf->size - buf->use);
}

/**
 * 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.
 */
static int
virBufferAdd(virBufferPtr buf, const char *str, int len) {
    unsigned int needSize;

    if ((str == NULL) || (buf == NULL)) {
	return -1;
    }
    if (len == 0) return 0;

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

    needSize = buf->use + len + 2;
    if (needSize > buf->size){
        if (!virBufferGrow(buf, needSize)){
            return(-1);
        }
    }

    memmove(&buf->content[buf->use], str, len);
    buf->use += len;
    buf->content[buf->use] = 0;
    return(0);
}

/**
 * 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.
 */
static int
virBufferVSprintf(virBufferPtr buf, const char *format, ...) {
    int size, count;
    va_list locarg, argptr;

    if ((format == NULL) || (buf == NULL)) {
	return(-1);
    }
    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)) {
	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);
    }
    va_end(locarg);
    buf->use += count;
    buf->content[buf->use] = 0;
    return(0);
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
/**
 * 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 *
virDomainGetXMLDeviceInfo(virDomainPtr domain, const char *sub, 
                          long dev, const char *name) {
    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;

156
    return xs_read(domain->conn->xshandle, 0, &s[0], &len);
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
}

/**
 * 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
virDomainGetXMLDevice(virDomainPtr domain, virBufferPtr buf, long dev) {
    char *type, *val;

    type = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "type");
    if (type == NULL)
        return(-1);
    if (!strcmp(type, "file")) {
	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);
	}
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "read-only");
	if (val != NULL) {
	    virBufferVSprintf(buf, "      <readonly/>\n", val);
	    free(val);
	}
	virBufferAdd(buf, "    </disk>\n", 12);
    } else if (!strcmp(type, "phy")) {
	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);
	}
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	virBufferAdd(buf, "    </disk>\n", 12);
    } else {
        TODO
	fprintf(stderr, "Don't know how to handle device type %s\n", type);
    }
    free(type);

    return(0);
}

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

K
Karel Zak 已提交
239 240 241
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
	return(-1);
    
242 243 244 245 246
    conn = domain->conn;

    snprintf(backend, 199, "/local/domain/0/backend/vbd/%d", 
             virDomainGetID(domain));
    backend[199] = 0;
247
    list = xs_directory(conn->xshandle, 0, backend, &num);
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
    ret = 0;
    if (list == NULL)
        goto done;

    for (i = 0;i < num;i++) {
        id = strtol(list[i], &endptr, 10);
	if ((endptr == list[i]) || (*endptr != 0)) {
	    ret = -1;
	    goto done;
	}
	virDomainGetXMLDevice(domain, buf, id);
    }

done:
    if (list != NULL)
        free(list);

    return(ret);
}

/**
 * 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
virDomainGetXMLInterface(virDomainPtr domain, virBufferPtr buf, long dev) {
    char *type, *val;

    type = virDomainGetXMLDeviceInfo(domain, "vif", dev, "bridge");
    if (type == NULL) {
285 286 287 288 289 290 291 292 293 294 295 296
	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);
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    } else {
	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);
    }
    free(type);

    return(0);
}

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

K
Karel Zak 已提交
335 336 337
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
	return(-1);
    
338 339 340 341 342
    conn = domain->conn;

    snprintf(backend, 199, "/local/domain/0/backend/vif/%d", 
             virDomainGetID(domain));
    backend[199] = 0;
343
    list = xs_directory(conn->xshandle, 0, backend, &num);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    ret = 0;
    if (list == NULL)
        goto done;

    for (i = 0;i < num;i++) {
        id = strtol(list[i], &endptr, 10);
	if ((endptr == list[i]) || (*endptr != 0)) {
	    ret = -1;
	    goto done;
	}
	virDomainGetXMLInterface(domain, buf, id);
    }

done:
    if (list != NULL)
        free(list);

    return(ret);
}

364 365 366



367 368 369 370 371 372 373 374 375 376 377 378 379
/**
 * 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
virDomainGetXMLBoot(virDomainPtr domain, virBufferPtr buf) {
    char *vm, *str;

K
Karel Zak 已提交
380 381
    if (!VIR_IS_DOMAIN(domain))
	return(-1);
382
    
383
    vm = virDomainGetVM(domain);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
    if (vm == NULL)
        return(-1);

    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) {
	if (str[0] != 0)
	    virBufferVSprintf(buf, "    <initrd>%s</initrd>\n", str);
        free(str);
    }
    str = virDomainGetVMInfo(domain, vm, "image/cmdline");
    if (str != NULL) {
	if (str[0] != 0)
	    virBufferVSprintf(buf, "    <cmdline>%s</cmdline>\n", str);
        free(str);
    }
    virBufferAdd(buf, "  </os>\n", 8);

    free(vm);
    return(0);
}

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
/**
 * 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 *
virDomainGetXMLDesc(virDomainPtr domain, int flags) {
    char *ret = NULL;
    virBuffer buf;
    virDomainInfo info;

K
Karel Zak 已提交
433 434 435
    if (!VIR_IS_DOMAIN(domain))
	return(NULL);
    if (flags != 0)
436 437 438 439 440 441 442 443 444 445 446 447 448 449
	return(NULL);
    if (virDomainGetInfo(domain, &info) < 0)
        return(NULL);

    ret = malloc(1000);
    if (ret == NULL)
        return(NULL);
    buf.content = ret;
    buf.size = 1000;
    buf.use = 0;

    virBufferVSprintf(&buf, "<domain type='xen' id='%d'>\n",
                      virDomainGetID(domain));
    virBufferVSprintf(&buf, "  <name>%s</name>\n", virDomainGetName(domain));
450
    virDomainGetXMLBoot(domain, &buf);
451 452 453 454 455 456
    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);
457 458 459 460 461
    virBufferAdd(&buf, "</domain>\n", 10);
    
    buf.content[buf.use] = 0;
    return(ret);
}