libvirt.c 34.8 KB
Newer Older
D
Daniel Veillard 已提交
1
/*
2
 * libvirt.c: Main interfaces for the libvirt library to handle virtualization
D
Daniel Veillard 已提交
3 4
 *           domains from a process running in domain 0
 *
5
 * Copyright (C) 2005,2006 Red Hat, Inc.
D
Daniel Veillard 已提交
6 7 8 9 10 11
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

12
#include "libvirt.h"
D
Daniel Veillard 已提交
13

14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <string.h>
17 18 19
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
20

21
#include <xs.h>
22

D
Daniel Veillard 已提交
23
#include "internal.h"
24
#include "driver.h"
25
#include "xen_internal.h"
26
#include "xend_internal.h"
27
#include "xs_internal.h"
28
#include "hash.h"
29
#include "xml.h"
30

D
Daniel Veillard 已提交
31 32 33 34 35

/*
 * TODO:
 * - use lock to protect against concurrent accesses ?
 * - use reference counting to garantee coherent pointer state ?
36 37
 * - error reporting layer
 * - memory wrappers for malloc/free ?
D
Daniel Veillard 已提交
38 39
 */

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
static virDriverPtr virDriverTab[MAX_DRIVERS];
static int initialized = 0;

/**
 * virInitialize:
 *
 * Initialize the library. It's better to call this routine at startup
 * in multithreaded applications to avoid potential race when initializing
 * the library.
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
virInitialize(void)
{
    int i;

    if (initialized)
        return(0);
59
    initialized = 1;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

    /*
     * should not be needed but...
     */
    for (i = 0;i < MAX_DRIVERS;i++) 
         virDriverTab[i] = NULL;

    /*
     * Note that the order is important the first ones have a higher priority
     */
    xenHypervisorRegister();
    xenDaemonRegister();
    xenStoreRegister();
    return(0);
}



D
Daniel Veillard 已提交
78 79 80 81 82 83 84 85 86
/**
 * virLibConnError:
 * @conn: the connection if available
 * @error: the error noumber
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
87 88
virLibConnError(virConnectPtr conn, virErrorNumber error, const char *info)
{
D
Daniel Veillard 已提交
89
    const char *errmsg;
90

D
Daniel Veillard 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
    __virRaiseError(conn, NULL, VIR_FROM_NONE, error, VIR_ERR_ERROR,
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

/**
 * virLibConnError:
 * @conn: the connection if available
 * @error: the error noumber
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
108 109 110
virLibDomainError(virDomainPtr domain, virErrorNumber error,
                  const char *info)
{
D
Daniel Veillard 已提交
111 112
    virConnectPtr conn = NULL;
    const char *errmsg;
113

D
Daniel Veillard 已提交
114 115 116 117 118 119 120 121 122 123 124
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
    if (error != VIR_ERR_INVALID_DOMAIN) {
        conn = domain->conn;
    }
    __virRaiseError(conn, domain, VIR_FROM_DOM, error, VIR_ERR_ERROR,
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

125 126 127 128 129 130 131 132 133 134 135 136 137
/**
 * virRegisterDriver:
 * @driver: pointer to a driver block
 *
 * Register a virtualization driver
 *
 * Returns the driver priority or -1 in case of error.
 */
int
virRegisterDriver(virDriverPtr driver)
{
    int i;

138 139 140
    if (!initialized)
        virInitialize();

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    if (driver == NULL) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }
    for (i = 0;i < MAX_DRIVERS;i++) {
        if (virDriverTab[i] == driver)
	    return(i);
    }
    for (i = 0;i < MAX_DRIVERS;i++) {
        if (virDriverTab[i] == NULL) {
	    virDriverTab[i] = driver;
	    return(i);
	}
    }
    virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
    return(-1);
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
/**
 * virGetVersion:
 * @libVer: return value for the library version (OUT)
 * @type: hypervisor type
 * @typeVer: return value for the version of the hypervisor (OUT)
 *
 * Provides two information back, @libVer is the version of the library
 * while @typeVer will be the version of the hypervisor type @type against
 * which the library was compiled. If @type is NULL, "Xen" is assumed, if
 * @type is unknown or not availble, an error code will be returned and 
 * @typeVer will be 0.
 *
 * Returns -1 in case of failure, 0 otherwise, and values for @libVer and
 *       @typeVer have the format major * 1,000,000 + minor * 1,000 + release.
 */
int
175 176 177
virGetVersion(unsigned long *libVer, const char *type,
              unsigned long *typeVer)
{
178 179 180
    if (!initialized)
        virInitialize();

181
    if (libVer == NULL)
182
        return (-1);
183 184 185
    *libVer = LIBVIR_VERSION_NUMBER;

    if (typeVer != NULL) {
186 187 188
        if ((type == NULL) || (!strcasecmp(type, "Xen"))) {
            if ((DOM0_INTERFACE_VERSION & 0xFFFF0000) == (0xAAAA0000)) {
                /* one time glitch hopefully ! */
189
                *typeVer = 2 * 1000000 +
190 191 192 193 194 195 196 197 198 199 200 201 202 203
                    ((DOM0_INTERFACE_VERSION >> 8) & 0xFF) * 1000 +
                    (DOM0_INTERFACE_VERSION & 0xFF);
            } else {
                *typeVer = (DOM0_INTERFACE_VERSION >> 24) * 1000000 +
                    ((DOM0_INTERFACE_VERSION >> 16) & 0xFF) * 1000 +
                    (DOM0_INTERFACE_VERSION & 0xFFFF);
            }
        } else {
            *typeVer = 0;
            virLibConnError(NULL, VIR_ERR_NO_SUPPORT, "type");
            return (-1);
        }
    }
    return (0);
204 205
}

D
Daniel Veillard 已提交
206
/**
207
 * virConnectOpen:
D
Daniel Veillard 已提交
208 209 210
 * @name: optional argument currently unused, pass NULL
 *
 * This function should be called first to get a connection to the 
211
 * Hypervisor and xen store
D
Daniel Veillard 已提交
212 213 214
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
 */
215
virConnectPtr
216 217
virConnectOpen(const char *name)
{
218
    int i, res;
219
    virConnectPtr ret = NULL;
220

221 222 223
    if (!initialized)
        virInitialize();

224 225 226 227 228 229 230
    ret = (virConnectPtr) malloc(sizeof(virConnect));
    if (ret == NULL) {
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, "Allocating connection");
        goto failed;
    }
    memset(ret, 0, sizeof(virConnect));
    ret->magic = VIR_CONNECT_MAGIC;
231
    ret->nb_drivers = 0;
232

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    for (i = 0;i < MAX_DRIVERS;i++) {
        if ((virDriverTab[i] != NULL) && (virDriverTab[i]->open != NULL)) {
	    res = virDriverTab[i]->open(ret, name, 0);
	    /*
	     * For a default connect to Xen make sure we manage to contact
	     * all related drivers.
	     */
	    if ((res < 0) && (name == NULL) &&
	        (!strcmp(virDriverTab[i]->name, "Xen")))
		goto failed;
	    if (res == 0)
	        ret->drivers[ret->nb_drivers++] = virDriverTab[i];
	}
    }

    if (ret->nb_drivers == 0) {
	/* we failed to find an adequate driver */
	virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name);
	goto failed;
    }
253

254
    ret->domains = virHashCreate(20);
255 256
    if (ret->domains == NULL)
        goto failed;
257
    ret->flags = 0;
258

259
    return (ret);
260 261 262

failed:
    if (ret != NULL) {
263 264 265 266
	for (i = 0;i < ret->nb_drivers;i++) {
	    if ((ret->drivers[i] != NULL) && (ret->drivers[i]->close != NULL))
	        ret->drivers[i]->close(ret);
	}
267
        free(ret);
268
    }
269
    return (NULL);
270 271 272
}

/**
273
 * virConnectOpenReadOnly:
274 275
 * @name: optional argument currently unused, pass NULL
 *
276 277 278
 * This function should be called first to get a restricted connection to the 
 * libbrary functionalities. The set of APIs usable are then restricted
 * on the available methods to control the domains.
279 280 281
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
 */
282
virConnectPtr
283 284
virConnectOpenReadOnly(const char *name)
{
285
    int i, res;
286
    virConnectPtr ret = NULL;
287

288 289 290
    if (!initialized)
        virInitialize();

291
    ret = (virConnectPtr) malloc(sizeof(virConnect));
D
Daniel Veillard 已提交
292 293
    if (ret == NULL) {
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, "Allocating connection");
294
        goto failed;
D
Daniel Veillard 已提交
295
    }
296
    memset(ret, 0, sizeof(virConnect));
297
    ret->magic = VIR_CONNECT_MAGIC;
298
    ret->nb_drivers = 0;
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    for (i = 0;i < MAX_DRIVERS;i++) {
        if ((virDriverTab[i] != NULL) && (virDriverTab[i]->open != NULL)) {
	    res = virDriverTab[i]->open(ret, name,
	                                VIR_DRV_OPEN_QUIET | VIR_DRV_OPEN_RO);
	    if (res == 0)
	        ret->drivers[ret->nb_drivers++] = virDriverTab[i];
	}
    }
    if (ret->nb_drivers == 0) {
	if (name == NULL)
	    virLibConnError(NULL, VIR_ERR_NO_CONNECT,
			    "could not connect to Xen Daemon nor Xen Store");
	else
	    /* we failed to find an adequate driver */
	    virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name);
	goto failed;
D
Daniel Veillard 已提交
316
    }
317

318 319 320 321 322
    ret->domains = virHashCreate(20);
    if (ret->domains == NULL)
        goto failed;
    ret->flags = VIR_CONNECT_RO;

323
    return (ret);
324 325

failed:
326
    if (ret != NULL) {
327 328 329 330
	for (i = 0;i < ret->nb_drivers;i++) {
	    if ((ret->drivers[i] != NULL) && (ret->drivers[i]->close != NULL))
	        ret->drivers[i]->close(ret);
	}
331
        free(ret);
332
    }
333
    return (NULL);
D
Daniel Veillard 已提交
334 335
}

336
/**
337
 * virDomainFreeName:
338 339 340 341 342 343 344
 * @domain: a domain object
 *
 * Destroy the domain object, this is just used by the domain hash callback.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
static int
345 346 347
virDomainFreeName(virDomainPtr domain, const char *name ATTRIBUTE_UNUSED)
{
    return (virDomainFree(domain));
348 349
}

D
Daniel Veillard 已提交
350
/**
351
 * virConnectClose:
D
Daniel Veillard 已提交
352 353 354 355 356 357 358 359 360 361
 * @conn: pointer to the hypervisor connection
 *
 * This function closes the connection to the Hypervisor. This should
 * not be called if further interaction with the Hypervisor are needed
 * especially if there is running domain which need further monitoring by
 * the application.
 *
 * Returns 0 in case of success or -1 in case of error.
 */
int
362 363
virConnectClose(virConnectPtr conn)
{
364 365
    int i;

K
Karel Zak 已提交
366
    if (!VIR_IS_CONNECT(conn))
367
        return (-1);
368
    virHashFree(conn->domains, (virHashDeallocator) virDomainFreeName);
369
    conn->domains = NULL;
370 371 372 373
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) && (conn->drivers[i]->close != NULL))
	    conn->drivers[i]->close(conn);
    }
374
    conn->magic = -1;
D
Daniel Veillard 已提交
375
    free(conn);
376
    return (0);
D
Daniel Veillard 已提交
377 378
}

379 380 381 382 383 384 385 386 387
/**
 * virConnectGetType:
 * @conn: pointer to the hypervisor connection
 *
 * Get the name of the Hypervisor software used.
 *
 * Returns NULL in case of error, a static zero terminated string otherwise.
 */
const char *
388 389
virConnectGetType(virConnectPtr conn)
{
D
Daniel Veillard 已提交
390
    if (!VIR_IS_CONNECT(conn)) {
391
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
392
        return (NULL);
D
Daniel Veillard 已提交
393
    }
394
    return ("Xen");
395 396
}

D
Daniel Veillard 已提交
397
/**
398
 * virConnectGetVersion:
D
Daniel Veillard 已提交
399
 * @conn: pointer to the hypervisor connection
400
 * @hvVer: return value for the version of the running hypervisor (OUT)
D
Daniel Veillard 已提交
401
 *
402 403 404
 * Get the version level of the Hypervisor running. This may work only with 
 * hypervisor call, i.e. with priviledged access to the hypervisor, not
 * with a Read-Only connection.
D
Daniel Veillard 已提交
405
 *
406 407 408
 * Returns -1 in case of error, 0 otherwise. if the version can't be
 *    extracted by lack of capacities returns 0 and @hvVer is 0, otherwise
 *    @hvVer value is major * 1,000,000 + minor * 1,000 + release
D
Daniel Veillard 已提交
409
 */
410
int
411 412
virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
{
413
    unsigned long ver;
414

D
Daniel Veillard 已提交
415 416
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
417
        return (-1);
D
Daniel Veillard 已提交
418
    }
419

D
Daniel Veillard 已提交
420 421
    if (hvVer == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
422
        return (-1);
D
Daniel Veillard 已提交
423
    }
424

425
    /* this can't be extracted from the Xenstore */
426 427
    if (conn->handle < 0) {
        *hvVer = 0;
428
        return (0);
429
    }
430

431
    ver = xenHypervisorGetVersion(conn, hvVer);
432
    return (0);
433 434 435
}

/**
436
 * virConnectListDomains:
437 438 439 440 441 442 443 444 445
 * @conn: pointer to the hypervisor connection
 * @ids: array to collect the list of IDs of active domains
 * @maxids: size of @ids
 *
 * Collect the list of active domains, and store their ID in @maxids
 *
 * Returns the number of domain found or -1 in case of error
 */
int
446 447
virConnectListDomains(virConnectPtr conn, int *ids, int maxids)
{
448
    int ret = -1;
449
    unsigned int i;
450
    long id;
451
    char **idlist = NULL;
452

D
Daniel Veillard 已提交
453 454
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
455
        return (-1);
D
Daniel Veillard 已提交
456
    }
457

D
Daniel Veillard 已提交
458 459
    if ((ids == NULL) || (maxids <= 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
460
        return (-1);
D
Daniel Veillard 已提交
461
    }
462

463 464 465
    /*
     * try first though the Xen Daemon
     */
466
    idlist = xenDaemonListDomains(conn);
467
    if (idlist != NULL) {
468
        for (ret = 0, i = 0; (idlist[i] != NULL) && (ret < maxids); i++) {
469
            id = xenDaemonDomainLookupByName_ids(conn, idlist[i], NULL);
470 471 472
            if (id >= 0)
                ids[ret++] = (int) id;
        }
473 474
	free(idlist);
        return(ret);
475
    }
476

477 478 479 480
    /*
     * Then fallback to the XenStore
     */
    ret = xenStoreListDomains(conn, ids, maxids);
481
    return (ret);
D
Daniel Veillard 已提交
482 483
}

K
 
Karel Zak 已提交
484 485 486 487
/**
 * virConnectNumOfDomains:
 * @conn: pointer to the hypervisor connection
 *
488 489
 * Provides the number of active domains.
 *
K
 
Karel Zak 已提交
490 491 492
 * Returns the number of domain found or -1 in case of error
 */
int
493 494
virConnectNumOfDomains(virConnectPtr conn)
{
K
 
Karel Zak 已提交
495 496 497
    int ret = -1;
    char **idlist = NULL;

D
Daniel Veillard 已提交
498 499
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
500
        return (-1);
D
Daniel Veillard 已提交
501
    }
K
Karel Zak 已提交
502

503
    /* TODO: there must be a way to do that with an hypervisor call too ! */
504 505 506
    /* 
     * try first with Xend interface
     */
507
    idlist = xenDaemonListDomains(conn);
508 509 510 511 512
    if (idlist != NULL) {
        char **tmp = idlist;

        ret = 0;
        while (*tmp != NULL) {
513 514 515
            tmp++;
            ret++;
        }
516 517
	free(idlist);
	return(ret);
K
 
Karel Zak 已提交
518
    }
519 520
    /* Then Xen Store */
    return(xenStoreNumOfDomains(conn));
K
 
Karel Zak 已提交
521 522
}

D
Daniel Veillard 已提交
523
/**
524
 * virDomainCreateLinux:
D
Daniel Veillard 已提交
525
 * @conn: pointer to the hypervisor connection
526
 * @xmlDesc: an XML description of the domain
527
 * @flags: an optional set of virDomainFlags
D
Daniel Veillard 已提交
528
 *
529 530 531
 * Launch a new Linux guest domain, based on an XML description similar
 * to the one returned by virDomainGetXMLDesc()
 * This function may requires priviledged access to the hypervisor.
D
Daniel Veillard 已提交
532 533 534
 * 
 * Returns a new domain object or NULL in case of failure
 */
535
virDomainPtr
536
virDomainCreateLinux(virConnectPtr conn,
537
                     const char *xmlDesc,
538 539
                     unsigned int flags ATTRIBUTE_UNUSED)
{
540 541 542 543
    int ret;
    char *sexpr;
    char *name = NULL;
    virDomainPtr dom;
K
Karel Zak 已提交
544

D
Daniel Veillard 已提交
545 546
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
547
        return (NULL);
D
Daniel Veillard 已提交
548 549 550
    }
    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
551
        return (NULL);
D
Daniel Veillard 已提交
552 553
    }

554 555 556
    sexpr = virDomainParseXMLDesc(xmlDesc, &name);
    if ((sexpr == NULL) || (name == NULL)) {
        if (sexpr != NULL)
557
            free(sexpr);
558
        if (name != NULL)
559
            free(name);
560

561
        return (NULL);
562 563
    }

564
    ret = xenDaemonDomainCreateLinux(conn, sexpr);
565 566 567
    free(sexpr);
    if (ret != 0) {
        fprintf(stderr, "Failed to create domain %s\n", name);
568
        goto error;
569 570 571 572 573
    }

    ret = xend_wait_for_devices(conn, name);
    if (ret != 0) {
        fprintf(stderr, "Failed to get devices for domain %s\n", name);
574
        goto error;
575 576
    }

577 578 579 580 581 582
    dom = virDomainLookupByName(conn, name);
    if (dom == NULL) {
        goto error;
    }

    ret = xenDaemonDomainResume(dom);
583 584
    if (ret != 0) {
        fprintf(stderr, "Failed to resume new domain %s\n", name);
585
        xenDaemonDomainDestroy(dom);
586
        goto error;
587 588 589 590 591
    }

    dom = virDomainLookupByName(conn, name);
    free(name);

592 593
    return (dom);
  error:
594 595
    if (name != NULL)
        free(name);
596
    return (NULL);
D
Daniel Veillard 已提交
597 598
}

599

600
/**
601
 * virDomainLookupByID:
602 603 604 605 606 607 608
 * @conn: pointer to the hypervisor connection
 * @id: the domain ID number
 *
 * Try to find a domain based on the hypervisor ID number
 *
 * Returns a new domain object or NULL in case of failure
 */
609
virDomainPtr
610 611
virDomainLookupByID(virConnectPtr conn, int id)
{
612
    char *path = NULL;
613
    virDomainPtr ret;
614
    char *name = NULL;
615
    unsigned char uuid[16];
616

D
Daniel Veillard 已提交
617 618
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
619
        return (NULL);
D
Daniel Veillard 已提交
620 621 622
    }
    if (id < 0) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
623
        return (NULL);
D
Daniel Veillard 已提交
624
    }
625

626 627
    /* lookup is easier with the Xen store so try it first */
    if (conn->xshandle != NULL) {
628
        path = xs_get_domain_path(conn->xshandle, (unsigned int) id);
629 630
    }
    /* fallback to xend API then */
631
    if (path == NULL) {
632
        char **names = xenDaemonListDomains(conn);
633 634 635 636 637
        char **tmp = names;
        int ident;

        if (names != NULL) {
            while (*tmp != NULL) {
638
                ident = xenDaemonDomainLookupByName_ids(conn, *tmp, &uuid[0]);
639 640 641 642 643 644 645 646
                if (ident == id) {
                    name = strdup(*tmp);
                    break;
                }
                tmp++;
            }
            free(names);
        }
647
    }
648

649
    ret = (virDomainPtr) malloc(sizeof(virDomain));
650
    if (ret == NULL) {
651
        goto error;
652
    }
653
    memset(ret, 0, sizeof(virDomain));
654
    ret->magic = VIR_DOMAIN_MAGIC;
655 656
    ret->conn = conn;
    ret->handle = id;
657
    ret->path = path;
658 659
    ret->name = name;
    memcpy(&ret->uuid[0], uuid, 16);
660
    if (ret->name == NULL) {
661 662 663
        goto error;
    }

664 665
    return (ret);
  error:
666
    if (ret != NULL)
667
        free(ret);
668
    if (path != NULL)
669 670
        free(path);
    return (NULL);
671 672 673 674 675 676 677 678 679 680 681 682
}

/**
 * virDomainLookupByUUID:
 * @conn: pointer to the hypervisor connection
 * @uuid: the UUID string for the domain
 *
 * Try to lookup a domain on the given hypervisor based on its UUID.
 *
 * Returns a new domain object or NULL in case of failure
 */
virDomainPtr
683 684
virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
685 686 687 688 689
    virDomainPtr ret;
    char *name = NULL;
    char **names;
    char **tmp;
    unsigned char ident[16];
690
    int id = -1;
691

D
Daniel Veillard 已提交
692 693
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
694
        return (NULL);
D
Daniel Veillard 已提交
695 696 697
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
698
        return (NULL);
D
Daniel Veillard 已提交
699
    }
700
    names = xenDaemonListDomains(conn);
701 702 703
    tmp = names;

    if (names == NULL) {
704 705
        TODO                    /* try to fallback to xenstore lookup */
            return (NULL);
706
    }
707
    while (*tmp != NULL) {
708
        id = xenDaemonDomainLookupByName_ids(conn, *tmp, &ident[0]);
709 710 711 712 713 714 715
        if (id >= 0) {
            if (!memcmp(uuid, ident, 16)) {
                name = strdup(*tmp);
                break;
            }
        }
        tmp++;
716 717 718 719
    }
    free(names);

    if (name == NULL)
720
        return (NULL);
721 722 723 724

    ret = (virDomainPtr) malloc(sizeof(virDomain));
    if (ret == NULL) {
        if (name != NULL)
725 726
            free(name);
        return (NULL);
727
    }
728
    memset(ret, 0, sizeof(virDomain));
729 730 731 732
    ret->magic = VIR_DOMAIN_MAGIC;
    ret->conn = conn;
    ret->handle = id;
    ret->name = name;
733
    ret->path = 0;
734
    memcpy(ret->uuid, uuid, 16);
735

736
    return (ret);
737 738
}

739 740 741 742 743
/**
 * virDomainLookupByName:
 * @conn: pointer to the hypervisor connection
 * @name: name for the domain
 *
744
 * Try to lookup a domain on the given hypervisor based on its name.
745 746 747 748
 *
 * Returns a new domain object or NULL in case of failure
 */
virDomainPtr
749 750
virDomainLookupByName(virConnectPtr conn, const char *name)
{
751 752
    virDomainPtr ret = NULL;

D
Daniel Veillard 已提交
753 754
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
755
        return (NULL);
D
Daniel Veillard 已提交
756 757 758
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
759
        return (NULL);
D
Daniel Veillard 已提交
760
    }
761

762
    /* try first though Xend */
763 764 765
    ret = xenDaemonDomainLookupByName(conn, name);
    if (ret != NULL) {
        return(ret);
766 767 768
    }

    /* then though the XenStore */
769 770 771
    ret = xenStoreDomainLookupByName(conn, name);
    if (ret != NULL) {
        return(ret);
772 773 774
    }

    return (ret);
775 776
}

D
Daniel Veillard 已提交
777
/**
778
 * virDomainDestroy:
D
Daniel Veillard 已提交
779 780 781 782
 * @domain: a domain object
 *
 * Destroy the domain object. The running instance is shutdown if not down
 * already and all resources used by it are given back to the hypervisor.
783 784
 * The data structure is freed and should not be used thereafter if the
 * call does not return an error.
785
 * This function may requires priviledged access
D
Daniel Veillard 已提交
786 787
 *
 * Returns 0 in case of success and -1 in case of failure.
788
 */
D
Daniel Veillard 已提交
789
int
790 791
virDomainDestroy(virDomainPtr domain)
{
792 793
    int ret;

D
Daniel Veillard 已提交
794 795
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
796
        return (-1);
D
Daniel Veillard 已提交
797
    }
798 799 800 801

    /*
     * try first with the xend method
     */
802
    ret = xenDaemonDomainDestroy(domain);
803 804
    if (ret == 0) {
        virDomainFree(domain);
805
        return (0);
806 807
    }

808
    ret = xenHypervisorDestroyDomain(domain);
809
    if (ret < 0)
810 811
        return (-1);

812
    virDomainFree(domain);
813
    return (0);
814 815 816 817 818 819 820 821 822 823 824 825
}

/**
 * virDomainFree:
 * @domain: a domain object
 *
 * Free the domain object. The running instance is kept alive.
 * The data structure is freed and should not be used thereafter.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
826 827
virDomainFree(virDomainPtr domain)
{
D
Daniel Veillard 已提交
828 829
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
830
        return (-1);
D
Daniel Veillard 已提交
831
    }
832 833 834 835 836 837 838
    domain->magic = -1;
    domain->handle = -1;
    if (domain->path != NULL)
        free(domain->path);
    if (domain->name)
        free(domain->name);
    free(domain);
839
    return (0);
D
Daniel Veillard 已提交
840 841 842
}

/**
843
 * virDomainSuspend:
D
Daniel Veillard 已提交
844 845 846 847
 * @domain: a domain object
 *
 * Suspends an active domain, the process is frozen without further access
 * to CPU resources and I/O but the memory used by the domain at the 
848
 * hypervisor level will stay allocated. Use virDomainResume() to reactivate
D
Daniel Veillard 已提交
849
 * the domain.
850
 * This function may requires priviledged access.
D
Daniel Veillard 已提交
851 852 853 854
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
855 856
virDomainSuspend(virDomainPtr domain)
{
857 858
    int ret;

D
Daniel Veillard 已提交
859 860
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
861
        return (-1);
D
Daniel Veillard 已提交
862 863
    }

864
    /* first try though the Xen daemon */
865
    ret = xenDaemonDomainSuspend(domain);
866
    if (ret == 0)
867
        return (0);
868 869

    /* then try a direct hypervisor access */
870
    return (xenHypervisorPauseDomain(domain));
D
Daniel Veillard 已提交
871 872 873
}

/**
874
 * virDomainResume:
D
Daniel Veillard 已提交
875 876 877
 * @domain: a domain object
 *
 * Resume an suspended domain, the process is restarted from the state where
878
 * it was frozen by calling virSuspendDomain().
879
 * This function may requires priviledged access
D
Daniel Veillard 已提交
880 881 882 883
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
884 885
virDomainResume(virDomainPtr domain)
{
886 887
    int ret;

D
Daniel Veillard 已提交
888 889
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
890
        return (-1);
D
Daniel Veillard 已提交
891 892
    }

893
    /* first try though the Xen daemon */
894
    ret = xenDaemonDomainResume(domain);
895
    if (ret == 0)
896
        return (0);
897 898

    /* then try a direct hypervisor access */
899
    return (xenHypervisorResumeDomain(domain));
D
Daniel Veillard 已提交
900 901
}

902 903 904 905 906 907
/**
 * virDomainSave:
 * @domain: a domain object
 * @to: path for the output file
 *
 * This method will suspend a domain and save its memory contents to
908 909 910
 * a file on disk. After the call, if successful, the domain is not
 * listed as running anymore (this may be a problem).
 * Use virDomainRestore() to restore a domain after saving.
911 912 913 914
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
915 916
virDomainSave(virDomainPtr domain, const char *to)
{
917
    int ret;
918
    char filepath[4096];
919

D
Daniel Veillard 已提交
920 921
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
922
        return (-1);
D
Daniel Veillard 已提交
923 924 925
    }
    if (to == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
926
        return (-1);
D
Daniel Veillard 已提交
927
    }
928

929 930 931 932 933
    /*
     * We must absolutize the file path as the save is done out of process
     * TODO: check for URI when libxml2 is linked in.
     */
    if (to[0] != '/') {
934
        unsigned int len, t;
935

936 937 938 939 940 941 942 943 944 945
        t = strlen(to);
        if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL)
            return (-1);
        len = strlen(filepath);
        /* that should be covered by getcwd() semantic, but be 100% sure */
        if (len > sizeof(filepath) - (t + 3))
            return (-1);
        filepath[len] = '/';
        strcpy(&filepath[len + 1], to);
        to = &filepath[0];
946 947 948

    }

949
    ret = xenDaemonDomainSave(domain, to);
950
    return (ret);
951 952 953 954
}

/**
 * virDomainRestore:
955
 * @conn: pointer to the hypervisor connection
956 957 958 959 960 961 962
 * @from: path to the 
 *
 * This method will restore a domain saved to disk by virDomainSave().
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
963 964
virDomainRestore(virConnectPtr conn, const char *from)
{
965
    int ret;
966
    char filepath[4096];
967

D
Daniel Veillard 已提交
968 969
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
970
        return (-1);
D
Daniel Veillard 已提交
971 972 973
    }
    if (from == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
974
        return (-1);
D
Daniel Veillard 已提交
975 976
    }

977 978 979 980 981
    /*
     * We must absolutize the file path as the restore is done out of process
     * TODO: check for URI when libxml2 is linked in.
     */
    if (from[0] != '/') {
982 983 984 985 986 987 988 989 990 991 992 993 994 995
        unsigned int len, t;

        t = strlen(from);
        if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL)
            return (-1);
        len = strlen(filepath);
        /* that should be covered by getcwd() semantic, but be 100% sure */
        if (len > sizeof(filepath) - (t + 3))
            return (-1);
        filepath[len] = '/';
        strcpy(&filepath[len + 1], from);
        from = &filepath[0];
    }

996
    ret = xenDaemonDomainRestore(conn, from);
997
    return (ret);
998 999
}

1000 1001 1002 1003 1004
/**
 * virDomainShutdown:
 * @domain: a domain object
 *
 * Shutdown a domain, the domain object is still usable there after but
1005 1006
 * the domain OS is being stopped. Note that the guest OS may ignore the
 * request.
1007 1008 1009 1010 1011 1012 1013
 *
 * TODO: should we add an option for reboot, knowing it may not be doable
 *       in the general case ?
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1014 1015
virDomainShutdown(virDomainPtr domain)
{
1016 1017
    int ret;

D
Daniel Veillard 已提交
1018 1019
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1020
        return (-1);
D
Daniel Veillard 已提交
1021
    }
1022

1023 1024 1025
    /*
     * try first with the xend daemon
     */
1026
    ret = xenDaemonDomainShutdown(domain);
1027 1028
    if (ret == 0) {
        domain->flags |= DOMAIN_IS_SHUTDOWN;
1029
        return (0);
1030
    }
1031

1032
    /*
1033 1034 1035
     * this is very hackish, the domU kernel probes for a special 
     * node in the xenstore and launch the shutdown command if found.
     */
1036
    ret = xenDaemonDomainShutdown(domain);
1037 1038 1039
    if (ret == 0) {
        domain->flags |= DOMAIN_IS_SHUTDOWN;
    }
1040
    return (ret);
1041 1042
}

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
/**
 * virDomainReboot:
 * @domain: a domain object
 * @flags: extra flags for the reboot operation, not used yet
 *
 * Reboot a domain, the domain object is still usable there after but
 * the domain OS is being stopped for a restart.
 * Note that the guest OS may ignore the request.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virDomainReboot(virDomainPtr domain, unsigned int flags)
{
    int ret;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }

    /*
     * try first with the xend daemon
     */
    ret = xenDaemonDomainReboot(domain, flags);
    if (ret == 0) {
        domain->flags |= DOMAIN_IS_SHUTDOWN;
        return (0);
    }

    /*
     * this is very hackish, the domU kernel probes for a special 
     * node in the xenstore and launch the shutdown command if found.
     */
    ret = xenDaemonDomainReboot(domain, flags);
    if (ret == 0) {
        domain->flags |= DOMAIN_IS_SHUTDOWN;
    }
    return (ret);
}

1084
/**
1085
 * virDomainGetName:
1086 1087 1088 1089 1090 1091 1092 1093
 * @domain: a domain object
 *
 * Get the public name for that domain
 *
 * Returns a pointer to the name or NULL, the string need not be deallocated
 * its lifetime will be the same as the domain object.
 */
const char *
1094 1095
virDomainGetName(virDomainPtr domain)
{
D
Daniel Veillard 已提交
1096 1097
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1098
        return (NULL);
D
Daniel Veillard 已提交
1099
    }
1100
    return (domain->name);
1101 1102
}

1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
/**
 * virDomainGetUUID:
 * @domain: a domain object
 * @uuid: pointer to a 16 bytes array
 *
 * Get the UUID for a domain
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
1113 1114
virDomainGetUUID(virDomainPtr domain, unsigned char *uuid)
{
D
Daniel Veillard 已提交
1115 1116
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1117
        return (-1);
D
Daniel Veillard 已提交
1118 1119 1120
    }
    if (uuid == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1121
        return (-1);
D
Daniel Veillard 已提交
1122 1123 1124
    }

    if (domain->handle == 0) {
1125
        memset(uuid, 0, 16);
D
Daniel Veillard 已提交
1126
    } else {
1127 1128 1129 1130 1131 1132 1133 1134
        if ((domain->uuid[0] == 0) && (domain->uuid[1] == 0) &&
            (domain->uuid[2] == 0) && (domain->uuid[3] == 0) &&
            (domain->uuid[4] == 0) && (domain->uuid[5] == 0) &&
            (domain->uuid[6] == 0) && (domain->uuid[7] == 0) &&
            (domain->uuid[8] == 0) && (domain->uuid[9] == 0) &&
            (domain->uuid[10] == 0) && (domain->uuid[11] == 0) &&
            (domain->uuid[12] == 0) && (domain->uuid[13] == 0) &&
            (domain->uuid[14] == 0) && (domain->uuid[15] == 0))
1135
            xenDaemonDomainLookupByName_ids(domain->conn, domain->name,
1136 1137 1138 1139
                                &domain->uuid[0]);
        memcpy(uuid, &domain->uuid[0], 16);
    }
    return (0);
1140 1141
}

1142
/**
1143
 * virDomainGetID:
1144 1145 1146 1147 1148 1149 1150
 * @domain: a domain object
 *
 * Get the hypervisor ID number for the domain
 *
 * Returns the domain ID number or (unsigned int) -1 in case of error
 */
unsigned int
1151 1152
virDomainGetID(virDomainPtr domain)
{
D
Daniel Veillard 已提交
1153 1154
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1155
        return ((unsigned int) -1);
D
Daniel Veillard 已提交
1156
    }
1157
    return (domain->handle);
1158 1159
}

1160 1161 1162 1163 1164 1165
/**
 * virDomainGetOSType:
 * @domain: a domain object
 *
 * Get the type of domain operation system.
 *
1166 1167
 * Returns the new string or NULL in case of error, the string must be
 *         freed by the caller.
1168 1169
 */
char *
1170 1171
virDomainGetOSType(virDomainPtr domain)
{
1172
    char *vm, *str = NULL;
1173

D
Daniel Veillard 已提交
1174 1175
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1176
        return (NULL);
D
Daniel Veillard 已提交
1177
    }
1178

1179 1180
    vm = virDomainGetVM(domain);
    if (vm) {
1181 1182
        str = virDomainGetVMInfo(domain, vm, "image/ostype");
        free(vm);
1183
    }
1184 1185 1186
    if (str == NULL)
        str = strdup("linux");

1187
    return (str);
1188 1189
}

1190
/**
1191
 * virDomainGetMaxMemory:
1192 1193 1194 1195 1196 1197 1198 1199 1200
 * @domain: a domain object or NULL
 * 
 * Retrieve the maximum amount of physical memory allocated to a
 * domain. If domain is NULL, then this get the amount of memory reserved
 * to Domain0 i.e. the domain where the application runs.
 *
 * Returns the memory size in kilobytes or 0 in case of error.
 */
unsigned long
1201 1202
virDomainGetMaxMemory(virDomainPtr domain)
{
1203 1204
    unsigned long ret = 0;

D
Daniel Veillard 已提交
1205 1206
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1207
        return (0);
D
Daniel Veillard 已提交
1208
    }
1209

1210 1211 1212 1213
    /*
     * try first with the hypervisor if available
     */
    if (!(domain->conn->flags & VIR_CONNECT_RO)) {
1214
        virDomainInfo dominfo;
1215
        int tmp;
1216

1217
        tmp = xenHypervisorGetDomainInfo(domain, &dominfo);
1218
        if (tmp >= 0)
1219
	    return(dominfo.maxMem);
1220
    }
1221 1222 1223 1224
    ret = xenStoreDomainGetMaxMemory(domain);
    if (ret > 0)
        return(ret);
    ret = xenDaemonDomainGetMaxMemory(domain);
1225
    return (ret);
1226 1227
}

D
Daniel Veillard 已提交
1228
/**
1229
 * virDomainSetMaxMemory:
D
Daniel Veillard 已提交
1230 1231 1232 1233 1234 1235
 * @domain: a domain object or NULL
 * @memory: the memory size in kilobytes
 * 
 * Dynamically change the maximum amount of physical memory allocated to a
 * domain. If domain is NULL, then this change the amount of memory reserved
 * to Domain0 i.e. the domain where the application runs.
1236
 * This function requires priviledged access to the hypervisor.
D
Daniel Veillard 已提交
1237 1238 1239 1240
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1241 1242
virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
{
1243 1244
    int ret;
    char s[256], v[30];
1245

D
Daniel Veillard 已提交
1246 1247
    if (memory < 4096) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1248
        return (-1);
D
Daniel Veillard 已提交
1249
    }
1250 1251 1252 1253 1254 1255
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1256
        return (-1);
1257 1258
    }
    if (domain->conn->flags & VIR_CONNECT_RO)
1259
        return (-1);
1260 1261 1262 1263 1264

    ret = xenDaemonDomainSetMaxMemory(domain, memory);
    if (ret == 0)
        return(0);

1265
    ret = xenHypervisorSetMaxMemory(domain, memory);
1266
    if (ret < 0)
1267
        return (-1);
1268

1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
    if (domain->conn->xshandle != NULL) {
	/*
	 * try to update at the Xenstore level too
	 * Failing to do so should not be considered fatal though as long
	 * as the hypervisor call succeeded
	 */
	snprintf(s, 255, "/local/domain/%d/memory/target", domain->handle);
	s[255] = 0;
	snprintf(v, 29, "%lu", memory);
	v[30] = 0;
1279

1280 1281 1282
	if (!xs_write(domain->conn->xshandle, 0, &s[0], &v[0], strlen(v)))
	    ret = -1;
    }
1283

1284
    return (ret);
D
Daniel Veillard 已提交
1285 1286
}

1287 1288
/**
 * virDomainGetInfo:
1289
 * @domain: a domain object
1290 1291 1292 1293 1294 1295 1296 1297 1298
 * @info: pointer to a virDomainInfo structure allocated by the user
 * 
 * Extract information about a domain. Note that if the connection
 * used to get the domain is limited only a partial set of the informations
 * can be extracted.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1299 1300
virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
{
1301 1302
    int ret;

D
Daniel Veillard 已提交
1303 1304
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1305
        return (-1);
D
Daniel Veillard 已提交
1306 1307 1308
    }
    if (info == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1309
        return (-1);
D
Daniel Veillard 已提交
1310
    }
1311

1312
    memset(info, 0, sizeof(virDomainInfo));
1313

1314 1315 1316 1317
    /*
     * if we have direct access though the hypervisor do a direct call
     */
    if (domain->conn->handle >= 0) {
1318
        ret = xenHypervisorGetDomainInfo(domain, info);
1319 1320
        if (ret == 0)
	    return (0);
1321 1322 1323 1324 1325
    }

    /*
     * try to extract the informations though access to the Xen Daemon
     */
1326
    if (xenDaemonDomainGetInfo(domain, info) == 0)
1327
        return (0);
1328 1329

    /*
1330
     * last fallback, try to get the informations from the Xen store
1331
     */
1332 1333
    if (xenStoreGetDomainInfo(domain, info) == 0)
        return (0);
1334

1335
    return (-1);
1336
}
1337

1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
/**
 * virDomainGetXMLDesc:
 * @domain: a domain object
 * @flags: and OR'ed set of extraction flags, not used yet
 *
 * Provide an XML description of the domain. The description may be reused
 * later to relaunch the domain with virDomainCreateLinux().
 *
 * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
1350 1351
virDomainGetXMLDesc(virDomainPtr domain, int flags)
{
D
Daniel Veillard 已提交
1352 1353
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1354
        return (NULL);
D
Daniel Veillard 已提交
1355 1356 1357
    }
    if (flags != 0) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1358
        return (NULL);
D
Daniel Veillard 已提交
1359
    }
1360

1361
    return (xenDaemonDomainDumpXML(domain));
1362
}
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400

/**
 * virNodeGetInfo:
 * @conn: pointer to the hypervisor connection
 * @info: pointer to a virNodeInfo structure allocated by the user
 * 
 * Extract hardware information about the node.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info) {
    int i;
    int ret = -1;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }
    if (info == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->nodeGetInfo != NULL)) {
	    ret = conn->drivers[i]->nodeGetInfo(conn, info);
	    if (ret == 0)
	        break;
	}
    }
    if (ret != 0) {
        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
        return (-1);
    }
    return(0);
}