libvirt.c 74.9 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/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 22 23
#include <libxml/parser.h>
#include <libxml/xpath.h>

D
Daniel Veillard 已提交
24
#include "internal.h"
25
#include "driver.h"
26

27
#include "xml.h"
28
#include "test.h"
29
#include "xen_unified.h"
D
Daniel P. Berrange 已提交
30
#include "qemu_internal.h"
31
#include "remote_internal.h"
32

D
Daniel Veillard 已提交
33 34 35 36 37 38
/*
 * TODO:
 * - use lock to protect against concurrent accesses ?
 * - use reference counting to garantee coherent pointer state ?
 */

39
static virDriverPtr virDriverTab[MAX_DRIVERS];
40
static int virDriverTabCount = 0;
41
static virNetworkDriverPtr virNetworkDriverTab[MAX_DRIVERS];
42
static int virNetworkDriverTabCount = 0;
43 44
static virStateDriverPtr virStateDriverTab[MAX_DRIVERS];
static int virStateDriverTabCount = 0;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
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)
{
    if (initialized)
        return(0);
61
    initialized = 1;
62

63 64 65
    if (!bindtextdomain(GETTEXT_PACKAGE, LOCALEBASEDIR))
        return (-1);

66
    /*
67 68
     * Note that the order is important: the first ones have a higher
     * priority when calling virConnectOpen.
69
     */
70
#ifdef WITH_TEST
71
    if (testRegister() == -1) return -1;
72 73
#endif
#ifdef WITH_QEMU
74
    if (qemuRegister() == -1) return -1;
75
#endif
76 77
#ifdef WITH_XEN
    if (xenUnifiedRegister () == -1) return -1;
78 79 80
#endif
#ifdef WITH_REMOTE
    if (remoteRegister () == -1) return -1;
81
#endif
D
Daniel P. Berrange 已提交
82

83 84 85 86 87
    return(0);
}



D
Daniel Veillard 已提交
88 89 90
/**
 * virLibConnError:
 * @conn: the connection if available
91
 * @error: the error number
D
Daniel Veillard 已提交
92 93 94 95 96
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
97 98
virLibConnError(virConnectPtr conn, virErrorNumber error, const char *info)
{
D
Daniel Veillard 已提交
99
    const char *errmsg;
100

D
Daniel Veillard 已提交
101 102 103 104
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
105
    __virRaiseError(conn, NULL, NULL, VIR_FROM_NONE, error, VIR_ERR_ERROR,
D
Daniel Veillard 已提交
106 107 108 109
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

/**
110
 * virLibConnWarning:
D
Daniel Veillard 已提交
111
 * @conn: the connection if available
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
 * @error: the error number
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
virLibConnWarning(virConnectPtr conn, virErrorNumber error, const char *info)
{
    const char *errmsg;

    if (error == VIR_ERR_OK)
        return;

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

/**
 * virLibDomainError:
 * @domain: the domain if available
 * @error: the error number
D
Daniel Veillard 已提交
134 135 136 137 138
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
139 140 141
virLibDomainError(virDomainPtr domain, virErrorNumber error,
                  const char *info)
{
D
Daniel Veillard 已提交
142 143
    virConnectPtr conn = NULL;
    const char *errmsg;
144

D
Daniel Veillard 已提交
145 146 147 148 149 150 151
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
    if (error != VIR_ERR_INVALID_DOMAIN) {
        conn = domain->conn;
    }
152
    __virRaiseError(conn, domain, NULL, VIR_FROM_DOM, error, VIR_ERR_ERROR,
D
Daniel Veillard 已提交
153 154 155
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

156
/**
157 158 159 160
 * virLibNetworkError:
 * @conn: the connection if available
 * @error: the error noumber
 * @info: extra information string
161
 *
162
 * Handle an error at the connection level
163
 */
164 165 166 167 168 169 170 171 172 173 174 175 176 177
static void
virLibNetworkError(virNetworkPtr network, virErrorNumber error,
                   const char *info)
{
    virConnectPtr conn = NULL;
    const char *errmsg;

    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
    if (error != VIR_ERR_INVALID_NETWORK) {
        conn = network->conn;
    }
178
    __virRaiseError(conn, NULL, network, VIR_FROM_NET, error, VIR_ERR_ERROR,
179 180 181 182 183 184 185 186 187 188 189 190 191 192
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

/**
 * virRegisterNetworkDriver:
 * @driver: pointer to a network driver block
 *
 * Register a network virtualization driver
 *
 * Returns the driver priority or -1 in case of error.
 */
int
virRegisterNetworkDriver(virNetworkDriverPtr driver)
{
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    if (virInitialize() < 0)
      return -1;

    if (driver == NULL) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }

    if (virNetworkDriverTabCount >= MAX_DRIVERS) {
    	virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }

    virNetworkDriverTab[virNetworkDriverTabCount] = driver;
    return virNetworkDriverTabCount++;
208 209 210 211 212 213 214 215 216 217 218 219 220
}

/**
 * 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)
{
221 222 223 224 225 226 227 228 229 230 231 232 233
    if (virInitialize() < 0)
      return -1;

    if (driver == NULL) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }

    if (virDriverTabCount >= MAX_DRIVERS) {
    	virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }

234 235 236 237 238 239 240
    if (driver->no < 0) {
    	virLibConnError
            (NULL, VIR_ERR_INVALID_ARG,
             "virRegisterDriver: tried to register an internal Xen driver");
        return -1;
    }

241 242
    virDriverTab[virDriverTabCount] = driver;
    return virDriverTabCount++;
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
/**
 * virRegisterStateDriver:
 * @driver: pointer to a driver block
 *
 * Register a virtualization driver
 *
 * Returns the driver priority or -1 in case of error.
 */
int
virRegisterStateDriver(virStateDriverPtr driver)
{
    if (virInitialize() < 0)
      return -1;

    if (driver == NULL) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }

    if (virStateDriverTabCount >= MAX_DRIVERS) {
    	virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }

    virStateDriverTab[virStateDriverTabCount] = driver;
    return virStateDriverTabCount++;
}

int __virStateInitialize(void) {
    int i, ret = 0;

    if (virInitialize() < 0)
        return -1;

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
        if (virStateDriverTab[i]->initialize() < 0)
            ret = -1;
    }
    return ret;
}

int __virStateCleanup(void) {
    int i, ret = 0;

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
        if (virStateDriverTab[i]->cleanup() < 0)
            ret = -1;
    }
    return ret;
}

int __virStateReload(void) {
    int i, ret = 0;

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
        if (virStateDriverTab[i]->reload() < 0)
            ret = -1;
    }
    return ret;
}

int __virStateActive(void) {
    int i, ret = 0;

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
        if (virStateDriverTab[i]->active())
            ret = 1;
    }
    return ret;
}



318 319 320
/**
 * virGetVersion:
 * @libVer: return value for the library version (OUT)
321
 * @type: the type of connection/driver looked at
322 323 324 325 326 327 328 329 330 331 332 333
 * @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
334 335 336
virGetVersion(unsigned long *libVer, const char *type,
              unsigned long *typeVer)
{
337 338
    int i;

339
    if (!initialized)
340 341
        if (virInitialize() < 0)
	    return -1;
342

343
    if (libVer == NULL)
344
        return (-1);
345 346 347
    *libVer = LIBVIR_VERSION_NUMBER;

    if (typeVer != NULL) {
348 349
        if (type == NULL)
	    type = "Xen";
350
	for (i = 0;i < virDriverTabCount;i++) {
351
	    if ((virDriverTab[i] != NULL) &&
352
	        (!strcasecmp(virDriverTab[i]->name, type))) {
353 354 355 356
		*typeVer = virDriverTab[i]->ver;
		break;
	    }
	}
357
        if (i >= virDriverTabCount) {
358
            *typeVer = 0;
359
            virLibConnError(NULL, VIR_ERR_NO_SUPPORT, type);
360 361 362 363
            return (-1);
        }
    }
    return (0);
364 365
}

366 367
static virConnectPtr
do_open (const char *name, int flags)
368
{
369
    int i, res;
370
    virConnectPtr ret = NULL;
371

372
    if (!initialized)
373 374
        if (virInitialize() < 0)
	    return NULL;
375

376
    ret = virGetConnect();
377
    if (ret == NULL) {
378
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, _("allocating connection"));
379 380 381
        goto failed;
    }

382 383 384 385 386 387 388
    for (i = 0; i < virDriverTabCount; i++) {
        res = virDriverTab[i]->open (ret, name, flags);
        if (res == VIR_DRV_OPEN_ERROR) goto failed;
        else if (res == VIR_DRV_OPEN_SUCCESS) {
            ret->driver = virDriverTab[i];
            break;
        }
389 390
    }

391
    if (!ret->driver) {
392 393
        /* If we reach here, then all drivers declined the connection. */
        virLibConnError (NULL, VIR_ERR_NO_CONNECT, name);
394
        goto failed;
395 396
    }

397 398
    for (i = 0; i < virNetworkDriverTabCount; i++) {
        res = virNetworkDriverTab[i]->open (ret, name, flags);
399 400 401 402 403
        if (res == VIR_DRV_OPEN_ERROR) {
	    virLibConnWarning (NULL, VIR_WAR_NO_NETWORK, 
	                       "Is the daemon running ?");
            break;
        } else if (res == VIR_DRV_OPEN_SUCCESS) {
404 405 406
            ret->networkDriver = virNetworkDriverTab[i];
            break;
        }
407
    }
408

409 410 411 412
    if (flags & VIR_DRV_OPEN_RO) {
        ret->flags = VIR_CONNECT_RO;
    }

413
    return ret;
414 415

failed:
416
    if (ret->driver) ret->driver->close (ret);
417
	virFreeConnect(ret);
418
    return (NULL);
419 420
}

421 422
/**
 * virConnectOpen:
423
 * @name: URI of the hypervisor
424 425 426 427 428
 *
 * This function should be called first to get a connection to the 
 * Hypervisor and xen store
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
429 430
 *
 * URIs are documented at http://libvirt.org/uri.html
431 432 433 434
 */
virConnectPtr
virConnectOpen (const char *name)
{
435
    return do_open (name, 0);
436 437
}

438
/**
439
 * virConnectOpenReadOnly:
440
 * @name: URI of the hypervisor
441
 *
442 443 444
 * 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.
445 446
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
447 448
 *
 * URIs are documented at http://libvirt.org/uri.html
449
 */
450
virConnectPtr
451 452
virConnectOpenReadOnly(const char *name)
{
453
    return do_open (name, VIR_DRV_OPEN_RO);
D
Daniel Veillard 已提交
454 455 456
}

/**
457
 * virConnectClose:
D
Daniel Veillard 已提交
458 459 460 461 462 463 464 465 466 467
 * @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
468 469
virConnectClose(virConnectPtr conn)
{
K
Karel Zak 已提交
470
    if (!VIR_IS_CONNECT(conn))
471
        return (-1);
472 473 474 475 476

    conn->driver->close (conn);
    if (conn->networkDriver)
        conn->networkDriver->close (conn);

477 478
    if (virFreeConnect(conn) < 0)
        return (-1);
479
    return (0);
D
Daniel Veillard 已提交
480 481
}

482 483 484 485 486 487 488
/**
 * 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.
489 490 491
 *
 * See also:
 * http://www.redhat.com/archives/libvir-list/2007-February/msg00096.html
492 493
 */
const char *
494 495
virConnectGetType(virConnectPtr conn)
{
496
    const char *ret;
497

D
Daniel Veillard 已提交
498
    if (!VIR_IS_CONNECT(conn)) {
499
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
500
        return (NULL);
D
Daniel Veillard 已提交
501
    }
502 503 504 505

    if (conn->driver->type) {
        ret = conn->driver->type (conn);
        if (ret) return ret;
506
    }
507
    return conn->driver->name;
508 509
}

D
Daniel Veillard 已提交
510
/**
511
 * virConnectGetVersion:
D
Daniel Veillard 已提交
512
 * @conn: pointer to the hypervisor connection
513
 * @hvVer: return value for the version of the running hypervisor (OUT)
D
Daniel Veillard 已提交
514
 *
515 516 517
 * 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 已提交
518
 *
519 520 521
 * 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 已提交
522
 */
523
int
524 525
virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
{
D
Daniel Veillard 已提交
526 527
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
528
        return (-1);
D
Daniel Veillard 已提交
529
    }
530

D
Daniel Veillard 已提交
531 532
    if (hvVer == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
533
        return (-1);
D
Daniel Veillard 已提交
534
    }
535

536 537
    if (conn->driver->version)
        return conn->driver->version (conn, hvVer);
D
Daniel P. Berrange 已提交
538

539 540
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
541 542
}

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
/**
 * virConnectGetHostname:
 * @conn: pointer to a hypervisor connection
 *
 * This returns the system hostname on which the hypervisor is
 * running (the result of the gethostname(2) system call).  If
 * we are connected to a remote system, then this returns the
 * hostname of the remote system.
 *
 * Returns the hostname which must be freed by the caller, or
 * NULL if there was an error.
 */
char *
virConnectGetHostname (virConnectPtr conn)
{
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return NULL;
    }

    if (conn->driver->getHostname)
        return conn->driver->getHostname (conn);

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return NULL;
}

/**
 * virConnectGetURI:
 * @conn: pointer to a hypervisor connection
 *
 * This returns the URI (name) of the hypervisor connection.
 * Normally this is the same as or similar to the string passed
 * to the virConnectOpen/virConnectOpenReadOnly call, but
 * the driver may make the URI canonical.  If name == NULL
 * was passed to virConnectOpen, then the driver will return
 * a non-NULL URI which can be used to connect to the same
 * hypervisor later.
 *
 * Returns the URI string which must be freed by the caller, or
 * NULL if there was an error.
 */
char *
virConnectGetURI (virConnectPtr conn)
{
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return NULL;
    }

    if (conn->driver->getURI)
        return conn->driver->getURI (conn);

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return NULL;
}

600 601 602 603 604
/**
 * virConnectGetMaxVcpus:
 * @conn: pointer to the hypervisor connection
 * @type: value of the 'type' attribute in the <domain> element
 *
605
 * Provides the maximum number of virtual CPUs supported for a guest VM of a
606 607 608 609 610 611 612 613 614 615 616 617 618 619
 * specific type. The 'type' parameter here corresponds to the 'type'
 * attribute in the <domain> element of the XML.
 *
 * Returns the maximum of virtual CPU or -1 in case of error.
 */
int
virConnectGetMaxVcpus(virConnectPtr conn,
                      const char *type)
{
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

620 621
    if (conn->driver->getMaxVcpus)
        return conn->driver->getMaxVcpus (conn, type);
622

623 624
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
625 626
}

627
/**
628
 * virConnectListDomains:
629 630 631 632 633 634 635 636 637
 * @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
638 639
virConnectListDomains(virConnectPtr conn, int *ids, int maxids)
{
D
Daniel Veillard 已提交
640 641
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
642
        return (-1);
D
Daniel Veillard 已提交
643
    }
644

645
    if ((ids == NULL) || (maxids < 0)) {
D
Daniel Veillard 已提交
646
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
647
        return (-1);
D
Daniel Veillard 已提交
648
    }
649

650 651
    if (conn->driver->listDomains)
        return conn->driver->listDomains (conn, ids, maxids);
652

653 654
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
D
Daniel Veillard 已提交
655 656
}

K
 
Karel Zak 已提交
657 658 659 660
/**
 * virConnectNumOfDomains:
 * @conn: pointer to the hypervisor connection
 *
661 662
 * Provides the number of active domains.
 *
K
 
Karel Zak 已提交
663 664 665
 * Returns the number of domain found or -1 in case of error
 */
int
666 667
virConnectNumOfDomains(virConnectPtr conn)
{
D
Daniel Veillard 已提交
668 669
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
670
        return (-1);
D
Daniel Veillard 已提交
671
    }
K
Karel Zak 已提交
672

673 674
    if (conn->driver->numOfDomains)
        return conn->driver->numOfDomains (conn);
675

676 677
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
K
 
Karel Zak 已提交
678 679
}

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
/**
 * virDomainGetConnect:
 * @dom: pointer to a domain
 *
 * Returns the connection pointer associated with a domain.  The
 * reference counter on the connection is not increased by this
 * call.
 *
 * Returns the virConnectPtr or NULL in case of failure.
 */
virConnectPtr
virDomainGetConnect (virDomainPtr dom)
{
    if (!VIR_IS_DOMAIN (dom)) {
        virLibDomainError (dom, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return NULL;
    }
    return dom->conn;
}

D
Daniel Veillard 已提交
700
/**
701
 * virDomainCreateLinux:
D
Daniel Veillard 已提交
702
 * @conn: pointer to the hypervisor connection
703
 * @xmlDesc: an XML description of the domain
704
 * @flags: an optional set of virDomainFlags
D
Daniel Veillard 已提交
705
 *
706 707 708
 * 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 已提交
709 710 711
 * 
 * Returns a new domain object or NULL in case of failure
 */
712
virDomainPtr
713 714
virDomainCreateLinux(virConnectPtr conn, const char *xmlDesc,
                     unsigned int flags)
715
{
D
Daniel Veillard 已提交
716 717
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
718
        return (NULL);
D
Daniel Veillard 已提交
719 720 721
    }
    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
722
        return (NULL);
D
Daniel Veillard 已提交
723
    }
724 725 726 727
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (NULL);
    }
D
Daniel Veillard 已提交
728

729 730 731 732 733
    if (conn->driver->domainCreateLinux)
        return conn->driver->domainCreateLinux (conn, xmlDesc, flags);

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return NULL;
D
Daniel Veillard 已提交
734 735
}

736

737
/**
738
 * virDomainLookupByID:
739 740 741 742 743 744 745
 * @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
 */
746
virDomainPtr
747 748
virDomainLookupByID(virConnectPtr conn, int id)
{
D
Daniel Veillard 已提交
749 750
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
751
        return (NULL);
D
Daniel Veillard 已提交
752 753 754
    }
    if (id < 0) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
755
        return (NULL);
D
Daniel Veillard 已提交
756
    }
757

758 759
    if (conn->driver->domainLookupByID)
        return conn->driver->domainLookupByID (conn, id);
760

761 762
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return NULL;
763 764 765 766 767
}

/**
 * virDomainLookupByUUID:
 * @conn: pointer to the hypervisor connection
K
Karel Zak 已提交
768
 * @uuid: the raw UUID for the domain
769 770 771 772 773 774
 *
 * 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
775 776
virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
D
Daniel Veillard 已提交
777 778
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
779
        return (NULL);
D
Daniel Veillard 已提交
780 781 782
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
783
        return (NULL);
D
Daniel Veillard 已提交
784
    }
785

786 787
    if (conn->driver->domainLookupByUUID)
        return conn->driver->domainLookupByUUID (conn, uuid);
788

789 790
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return NULL;
791 792
}

K
Karel Zak 已提交
793 794 795 796 797 798 799 800 801 802 803 804
/**
 * virDomainLookupByUUIDString:
 * @conn: pointer to the hypervisor connection
 * @uuidstr: the string UUID 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
virDomainLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
{
805 806
    int raw[VIR_UUID_BUFLEN], i;
    unsigned char uuid[VIR_UUID_BUFLEN];
K
Karel Zak 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
    int ret;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (uuidstr == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
	
    }
    /* XXX: sexpr_uuid() also supports 'xxxx-xxxx-xxxx-xxxx' format. 
     *      We needn't it here. Right? 
     */
    ret = sscanf(uuidstr,
                 "%02x%02x%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x%02x%02x%02x%02x",
                 raw + 0, raw + 1, raw + 2, raw + 3,
                 raw + 4, raw + 5, raw + 6, raw + 7,
                 raw + 8, raw + 9, raw + 10, raw + 11,
                 raw + 12, raw + 13, raw + 14, raw + 15);
    
832
    if (ret!=VIR_UUID_BUFLEN) {
K
Karel Zak 已提交
833 834 835
	virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return (NULL);
    }
836
    for (i = 0; i < VIR_UUID_BUFLEN; i++)
K
Karel Zak 已提交
837 838 839 840 841
        uuid[i] = raw[i] & 0xFF;
    
    return virDomainLookupByUUID(conn, &uuid[0]);
}

842 843 844 845 846
/**
 * virDomainLookupByName:
 * @conn: pointer to the hypervisor connection
 * @name: name for the domain
 *
847
 * Try to lookup a domain on the given hypervisor based on its name.
848 849 850 851
 *
 * Returns a new domain object or NULL in case of failure
 */
virDomainPtr
852 853
virDomainLookupByName(virConnectPtr conn, const char *name)
{
D
Daniel Veillard 已提交
854 855
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
856
        return (NULL);
D
Daniel Veillard 已提交
857 858 859
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
860
        return (NULL);
D
Daniel Veillard 已提交
861
    }
862

863 864 865 866 867
    if (conn->driver->domainLookupByName)
        return conn->driver->domainLookupByName (conn, name);

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return NULL;
868 869
}

D
Daniel Veillard 已提交
870
/**
871
 * virDomainDestroy:
D
Daniel Veillard 已提交
872 873 874 875
 * @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.
876 877
 * The data structure is freed and should not be used thereafter if the
 * call does not return an error.
878
 * This function may requires priviledged access
D
Daniel Veillard 已提交
879 880
 *
 * Returns 0 in case of success and -1 in case of failure.
881
 */
D
Daniel Veillard 已提交
882
int
883 884
virDomainDestroy(virDomainPtr domain)
{
885
    virConnectPtr conn;
886

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

892
    conn = domain->conn;
893 894 895 896
    if (conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
897

898 899
    if (conn->driver->domainDestroy)
        return conn->driver->domainDestroy (domain);
900

901 902
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
903 904 905 906 907 908 909 910 911 912 913 914
}

/**
 * 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
915 916
virDomainFree(virDomainPtr domain)
{
D
Daniel Veillard 已提交
917 918
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
919
        return (-1);
D
Daniel Veillard 已提交
920
    }
921 922 923
    if (virFreeDomain(domain->conn, domain) < 0)
        return (-1);
    return(0);
D
Daniel Veillard 已提交
924 925 926
}

/**
927
 * virDomainSuspend:
D
Daniel Veillard 已提交
928 929 930 931
 * @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 
932
 * hypervisor level will stay allocated. Use virDomainResume() to reactivate
D
Daniel Veillard 已提交
933
 * the domain.
934
 * This function may requires priviledged access.
D
Daniel Veillard 已提交
935 936 937 938
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
939 940
virDomainSuspend(virDomainPtr domain)
{
941
    virConnectPtr conn;
942

D
Daniel Veillard 已提交
943 944
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
945
        return (-1);
D
Daniel Veillard 已提交
946
    }
947 948 949 950
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
D
Daniel Veillard 已提交
951

952 953
    conn = domain->conn;

954 955
    if (conn->driver->domainSuspend)
        return conn->driver->domainSuspend (domain);
956

957 958
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
D
Daniel Veillard 已提交
959 960 961
}

/**
962
 * virDomainResume:
D
Daniel Veillard 已提交
963 964 965
 * @domain: a domain object
 *
 * Resume an suspended domain, the process is restarted from the state where
966
 * it was frozen by calling virSuspendDomain().
967
 * This function may requires priviledged access
D
Daniel Veillard 已提交
968 969 970 971
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
972 973
virDomainResume(virDomainPtr domain)
{
974
    virConnectPtr conn;
975

D
Daniel Veillard 已提交
976 977
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
978
        return (-1);
D
Daniel Veillard 已提交
979
    }
980 981 982 983
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
D
Daniel Veillard 已提交
984

985 986
    conn = domain->conn;

987 988
    if (conn->driver->domainResume)
        return conn->driver->domainResume (domain);
989

990 991
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
    return -1;
D
Daniel Veillard 已提交
992 993
}

994 995 996 997 998 999
/**
 * virDomainSave:
 * @domain: a domain object
 * @to: path for the output file
 *
 * This method will suspend a domain and save its memory contents to
1000 1001 1002
 * 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.
1003 1004 1005 1006
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1007 1008
virDomainSave(virDomainPtr domain, const char *to)
{
1009
    char filepath[4096];
1010
    virConnectPtr conn;
1011

D
Daniel Veillard 已提交
1012 1013
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1014
        return (-1);
D
Daniel Veillard 已提交
1015
    }
1016 1017 1018 1019
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1020
    conn = domain->conn;
D
Daniel Veillard 已提交
1021 1022
    if (to == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1023
        return (-1);
D
Daniel Veillard 已提交
1024
    }
1025

1026 1027 1028 1029 1030
    /*
     * 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] != '/') {
1031
        unsigned int len, t;
1032

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
        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];
1043 1044 1045

    }

1046 1047 1048
    if (conn->driver->domainSave)
        return conn->driver->domainSave (domain, to);

1049
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1050
    return -1;
1051 1052 1053 1054
}

/**
 * virDomainRestore:
1055
 * @conn: pointer to the hypervisor connection
1056 1057 1058 1059 1060 1061 1062
 * @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
1063 1064
virDomainRestore(virConnectPtr conn, const char *from)
{
1065
    char filepath[4096];
1066

D
Daniel Veillard 已提交
1067 1068
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
1069
        return (-1);
D
Daniel Veillard 已提交
1070
    }
1071 1072 1073 1074
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
D
Daniel Veillard 已提交
1075 1076
    if (from == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1077
        return (-1);
D
Daniel Veillard 已提交
1078 1079
    }

1080 1081 1082 1083 1084
    /*
     * 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] != '/') {
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
        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];
    }

1099 1100 1101
    if (conn->driver->domainRestore)
        return conn->driver->domainRestore (conn, from);

1102
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1103
    return -1;
D
Daniel Veillard 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
}

/**
 * virDomainCoreDump:
 * @domain: a domain object
 * @to: path for the core file
 * @flags: extra flags, currently unused
 *
 * This method will dump the core of a domain on a given file for analysis.
 * Note that for remote Xen Daemon the file path will be interpreted in
 * the remote host.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virDomainCoreDump(virDomainPtr domain, const char *to, int flags)
{
    char filepath[4096];
    virConnectPtr conn;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
    conn = domain->conn;
    if (to == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    /*
     * 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] != '/') {
        unsigned int len, t;

        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];

    }

1158 1159 1160
    if (conn->driver->domainCoreDump)
        return conn->driver->domainCoreDump (domain, to, flags);

1161
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1162
    return -1;
1163 1164
}

1165 1166 1167 1168 1169
/**
 * virDomainShutdown:
 * @domain: a domain object
 *
 * Shutdown a domain, the domain object is still usable there after but
1170 1171
 * the domain OS is being stopped. Note that the guest OS may ignore the
 * request.
1172 1173 1174 1175 1176 1177 1178
 *
 * 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
1179 1180
virDomainShutdown(virDomainPtr domain)
{
1181
    virConnectPtr conn;
1182

D
Daniel Veillard 已提交
1183 1184
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1185
        return (-1);
D
Daniel Veillard 已提交
1186
    }
1187 1188 1189 1190
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1191

1192 1193
    conn = domain->conn;

1194 1195
    if (conn->driver->domainShutdown)
        return conn->driver->domainShutdown (domain);
1196

1197
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1198
    return -1;
1199 1200
}

1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
/**
 * 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)
{
1215
    virConnectPtr conn;
1216 1217 1218 1219 1220

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1221 1222 1223 1224
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1225

1226 1227
    conn = domain->conn;

1228 1229
    if (conn->driver->domainReboot)
        return conn->driver->domainReboot (domain, flags);
1230

1231
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1232
    return -1;
1233 1234
}

1235
/**
1236
 * virDomainGetName:
1237 1238 1239 1240 1241 1242 1243 1244
 * @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 *
1245 1246
virDomainGetName(virDomainPtr domain)
{
D
Daniel Veillard 已提交
1247 1248
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1249
        return (NULL);
D
Daniel Veillard 已提交
1250
    }
1251
    return (domain->name);
1252 1253
}

1254 1255 1256
/**
 * virDomainGetUUID:
 * @domain: a domain object
1257
 * @uuid: pointer to a VIR_UUID_BUFLEN bytes array
1258 1259 1260 1261 1262 1263
 *
 * Get the UUID for a domain
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
1264 1265
virDomainGetUUID(virDomainPtr domain, unsigned char *uuid)
{
D
Daniel Veillard 已提交
1266 1267
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1268
        return (-1);
D
Daniel Veillard 已提交
1269 1270 1271
    }
    if (uuid == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1272
        return (-1);
D
Daniel Veillard 已提交
1273 1274
    }

1275
    if (domain->id == 0) {
1276
        memset(uuid, 0, VIR_UUID_BUFLEN);
D
Daniel Veillard 已提交
1277
    } else {
1278 1279 1280 1281 1282
#if 0
        /* Probably legacy code: It appears that we always fill in
         * the UUID when creating the virDomain structure, so this
         * shouldn't be necessary.
         */
1283 1284 1285 1286 1287 1288 1289 1290
        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))
1291
            xenDaemonDomainLookupByName_ids(domain->conn, domain->name,
1292
                                &domain->uuid[0]);
1293
#endif
1294
        memcpy(uuid, &domain->uuid[0], VIR_UUID_BUFLEN);
1295 1296
    }
    return (0);
1297 1298
}

K
Karel Zak 已提交
1299 1300 1301
/**
 * virDomainGetUUIDString:
 * @domain: a domain object
1302
 * @buf: pointer to a VIR_UUID_STRING_BUFLEN bytes array
K
Karel Zak 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311
 *
 * Get the UUID for a domain as string. For more information about 
 * UUID see RFC4122.
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virDomainGetUUIDString(virDomainPtr domain, char *buf)
{
1312
    unsigned char uuid[VIR_UUID_BUFLEN];
1313

K
Karel Zak 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    if (buf == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
    
    if (virDomainGetUUID(domain, &uuid[0]))
1324
        return (-1);
K
Karel Zak 已提交
1325

1326
    snprintf(buf, VIR_UUID_STRING_BUFLEN,
K
Karel Zak 已提交
1327 1328 1329 1330 1331 1332 1333 1334
	"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
                      uuid[0], uuid[1], uuid[2], uuid[3],
                      uuid[4], uuid[5], uuid[6], uuid[7],
                      uuid[8], uuid[9], uuid[10], uuid[11],
                      uuid[12], uuid[13], uuid[14], uuid[15]);
    return (0);
}

1335
/**
1336
 * virDomainGetID:
1337 1338 1339 1340 1341 1342 1343
 * @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
1344 1345
virDomainGetID(virDomainPtr domain)
{
D
Daniel Veillard 已提交
1346 1347
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1348
        return ((unsigned int) -1);
D
Daniel Veillard 已提交
1349
    }
1350
    return (domain->id);
1351 1352
}

1353 1354 1355 1356 1357 1358
/**
 * virDomainGetOSType:
 * @domain: a domain object
 *
 * Get the type of domain operation system.
 *
1359 1360
 * Returns the new string or NULL in case of error, the string must be
 *         freed by the caller.
1361 1362
 */
char *
1363 1364
virDomainGetOSType(virDomainPtr domain)
{
1365
    virConnectPtr conn;
1366

D
Daniel Veillard 已提交
1367 1368
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1369
        return (NULL);
D
Daniel Veillard 已提交
1370
    }
1371

1372 1373 1374 1375
    conn = domain->conn;

    if (conn->driver->domainGetOSType)
        return conn->driver->domainGetOSType (domain);
1376

1377
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1378
    return NULL;
1379 1380
}

1381
/**
1382
 * virDomainGetMaxMemory:
1383 1384 1385 1386 1387 1388 1389 1390 1391
 * @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
1392 1393
virDomainGetMaxMemory(virDomainPtr domain)
{
1394
    virConnectPtr conn;
1395

D
Daniel Veillard 已提交
1396 1397
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1398
        return (0);
D
Daniel Veillard 已提交
1399
    }
1400

1401 1402
    conn = domain->conn;

1403 1404 1405
    if (conn->driver->domainGetMaxMemory)
        return conn->driver->domainGetMaxMemory (domain);

1406
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1407
    return 0;
1408 1409
}

D
Daniel Veillard 已提交
1410
/**
1411
 * virDomainSetMaxMemory:
D
Daniel Veillard 已提交
1412 1413 1414 1415 1416 1417
 * @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.
1418
 * This function requires priviledged access to the hypervisor.
D
Daniel Veillard 已提交
1419 1420 1421 1422
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1423 1424
virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
{
1425
    virConnectPtr conn;
1426

1427 1428 1429 1430 1431 1432
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1433
        return (-1);
1434
    }
1435 1436 1437 1438 1439 1440 1441 1442
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
    if (memory < 4096) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
1443
    conn = domain->conn;
1444

1445 1446 1447
    if (conn->driver->domainSetMaxMemory)
        return conn->driver->domainSetMaxMemory (domain, memory);

1448
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1449
    return -1;
1450
}
1451

1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
/**
 * virDomainSetMemory:
 * @domain: a domain object or NULL
 * @memory: the memory size in kilobytes
 * 
 * Dynamically change the target 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.
 * This function may requires priviledged access to the hypervisor.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virDomainSetMemory(virDomainPtr domain, unsigned long memory)
{
    virConnectPtr conn;
1468

1469 1470 1471 1472 1473 1474 1475
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
1476
    }
1477 1478 1479 1480 1481 1482
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
    if (memory < 4096) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1483
        return (-1);
1484 1485 1486
    }

    conn = domain->conn;
1487

1488 1489 1490
    if (conn->driver->domainSetMemory)
        return conn->driver->domainSetMemory (domain, memory);

1491
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1492
    return -1;
D
Daniel Veillard 已提交
1493 1494
}

1495 1496
/**
 * virDomainGetInfo:
1497
 * @domain: a domain object
1498 1499 1500
 * @info: pointer to a virDomainInfo structure allocated by the user
 * 
 * Extract information about a domain. Note that if the connection
1501
 * used to get the domain is limited only a partial set of the information
1502 1503 1504 1505 1506
 * can be extracted.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1507 1508
virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
{
1509
    virConnectPtr conn;
1510

D
Daniel Veillard 已提交
1511 1512
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1513
        return (-1);
D
Daniel Veillard 已提交
1514 1515 1516
    }
    if (info == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1517
        return (-1);
D
Daniel Veillard 已提交
1518
    }
1519

1520
    memset(info, 0, sizeof(virDomainInfo));
1521

1522 1523 1524 1525
    conn = domain->conn;

    if (conn->driver->domainGetInfo)
        return conn->driver->domainGetInfo (domain, info);
1526

1527
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1528
    return -1;
1529
}
1530

1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
/**
 * 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 *
1543 1544
virDomainGetXMLDesc(virDomainPtr domain, int flags)
{
1545 1546
    virConnectPtr conn;

D
Daniel Veillard 已提交
1547 1548
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1549
        return (NULL);
D
Daniel Veillard 已提交
1550 1551 1552
    }
    if (flags != 0) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1553
        return (NULL);
D
Daniel Veillard 已提交
1554
    }
1555

1556 1557 1558 1559 1560
    conn = domain->conn;

    if (conn->driver->domainDumpXML)
        return conn->driver->domainDumpXML (domain, flags);

1561
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1562
    return NULL;
1563
}
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574

/**
 * 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
1575 1576
virNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
{
1577 1578 1579 1580 1581 1582 1583 1584 1585
    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);
    }

1586 1587 1588
    if (conn->driver->nodeGetInfo)
        return conn->driver->nodeGetInfo (conn, info);

1589
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1590
    return -1;
1591
}
1592

1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
/**
 * virConnectGetCapabilities:
 * @conn: pointer to the hypervisor connection
 *
 * Provides capabilities of the hypervisor / driver.
 *
 * Returns NULL in case of error, or a pointer to an opaque
 * virCapabilities structure (virCapabilitiesPtr).
 * The client must free the returned string after use.
 */
char *
virConnectGetCapabilities (virConnectPtr conn)
{
    if (!VIR_IS_CONNECT (conn)) {
        virLibConnError (conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return NULL;
    }

1611 1612
    if (conn->driver->getCapabilities)
        return conn->driver->getCapabilities (conn);
1613

1614
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1615 1616 1617
    return NULL;
}

1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
/**
 * virDomainGetSchedulerType:
 * @domain: pointer to domain object
 * @nparams: number of scheduler parameters(return value)
 *
 * Get the scheduler type.
 *
 * Returns NULL in case of error. The caller must free the returned string.
 */
char *
virDomainGetSchedulerType(virDomainPtr domain, int *nparams)
{
    virConnectPtr conn;
    char *schedtype;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return NULL;
    }
    conn = domain->conn;

    if (!VIR_IS_CONNECT (conn)) {
        virLibConnError (conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return NULL;
    }

    if (conn->driver->domainGetSchedulerType){
        schedtype = conn->driver->domainGetSchedulerType (domain, nparams);
        return schedtype;
    }

1649
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
    return NULL;
}


/**
 * virDomainGetSchedulerParameters:
 * @domain: pointer to domain object
 * @params: pointer to scheduler parameter object
 *          (return value)
 * @nparams: pointer to number of scheduler parameter
 *          (this value should be same than the returned value
 *           nparams of virDomainGetSchedulerType)
 *
 * Get the scheduler parameters, the @params array will be filled with the
 * values.
 *
 * Returns -1 in case of error, 0 in case of success.
 */
int
virDomainGetSchedulerParameters(virDomainPtr domain,
				virSchedParameterPtr params, int *nparams)
{
    virConnectPtr conn;

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

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

    if (conn->driver->domainGetSchedulerParameters)
        return conn->driver->domainGetSchedulerParameters (domain, params, nparams);

1688
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
    return -1;
}

/**
 * virDomainSetSchedulerParameters:
 * @domain: pointer to domain object
 * @params: pointer to scheduler parameter objects
 * @nparams: number of scheduler parameter
 *          (this value should be same or less than the returned value
 *           nparams of virDomainGetSchedulerType)
 *
 * Change the scheduler parameters
 *
 * Returns -1 in case of error, 0 in case of success.
 */
int
virDomainSetSchedulerParameters(virDomainPtr domain, 
				virSchedParameterPtr params, int nparams)
{
    virConnectPtr conn;

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

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

    if (conn->driver->domainSetSchedulerParameters)
        return conn->driver->domainSetSchedulerParameters (domain, params, nparams);

1724
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1725 1726 1727 1728 1729
    return -1;
}



1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
/************************************************************************
 *									*
 *		Handling of defined but not running domains		*
 *									*
 ************************************************************************/

/**
 * virDomainDefineXML:
 * @conn: pointer to the hypervisor connection
 * @xml: the XML description for the domain, preferably in UTF-8
 *
 * define a domain, but does not start it
 *
 * Returns NULL in case of error, a pointer to the domain otherwise
 */
virDomainPtr
virDomainDefineXML(virConnectPtr conn, const char *xml) {
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
1751 1752 1753 1754
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (NULL);
    }
1755 1756 1757 1758 1759
    if (xml == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

1760 1761
    if (conn->driver->domainDefineXML)
        return conn->driver->domainDefineXML (conn, xml);
1762

1763
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1764
    return NULL;
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
}

/**
 * virDomainUndefine:
 * @domain: pointer to a defined domain
 *
 * undefine a domain but does not stop it if it is running
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
virDomainUndefine(virDomainPtr domain) {
1777
    virConnectPtr conn;
1778 1779 1780 1781 1782

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1783 1784
    conn = domain->conn;
    if (conn->flags & VIR_CONNECT_RO) {
1785 1786 1787 1788
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }

1789 1790
    if (conn->driver->domainUndefine)
        return conn->driver->domainUndefine (domain);
1791

1792
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1793
    return -1;
1794 1795 1796 1797 1798 1799
}

/**
 * virConnectNumOfDefinedDomains:
 * @conn: pointer to the hypervisor connection
 *
1800
 * Provides the number of inactive domains.
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
 *
 * Returns the number of domain found or -1 in case of error
 */
int
virConnectNumOfDefinedDomains(virConnectPtr conn)
{
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

1812 1813
    if (conn->driver->numOfDefinedDomains)
        return conn->driver->numOfDefinedDomains (conn);
1814

1815
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1816
    return -1;
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
}

/**
 * virConnectListDefinedDomains:
 * @conn: pointer to the hypervisor connection
 * @names: pointer to an array to store the names
 * @maxnames: size of the array
 *
 * list the defined domains, stores the pointers to the names in @names
 * 
 * Returns the number of names provided in the array or -1 in case of error
 */
int
1830
virConnectListDefinedDomains(virConnectPtr conn, char **const names,
1831
                             int maxnames) {
1832 1833 1834 1835 1836
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

1837
    if ((names == NULL) || (maxnames < 0)) {
1838 1839 1840 1841
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

1842 1843
    if (conn->driver->listDefinedDomains)
        return conn->driver->listDefinedDomains (conn, names, maxnames);
1844

1845
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1846
    return -1;
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
}

/**
 * virDomainCreate:
 * @domain: pointer to a defined domain
 *
 * launch a defined domain. If the call succeed the domain moves from the
 * defined to the running domains pools.
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
virDomainCreate(virDomainPtr domain) {
1860
    virConnectPtr conn;
1861

1862 1863 1864 1865
    if (domain == NULL) {
        TODO
	return (-1);
    }
1866 1867 1868 1869
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1870 1871
    conn = domain->conn;
    if (conn->flags & VIR_CONNECT_RO) {
1872 1873 1874
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1875

1876 1877 1878
    if (conn->driver->domainCreate)
        return conn->driver->domainCreate (domain);

1879
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1880
    return -1;
1881 1882
}

1883 1884 1885
/**
 * virDomainGetAutostart:
 * @domain: a domain object
1886
 * @autostart: the value returned
1887
 *
1888
 * Provides a boolean value indicating whether the domain
1889 1890 1891 1892 1893 1894 1895
 * configured to be automatically started when the host
 * machine boots.
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virDomainGetAutostart(virDomainPtr domain,
1896 1897 1898
                      int *autostart)
{
    virConnectPtr conn;
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908

    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    if (!autostart) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

1909 1910 1911 1912 1913
    conn = domain->conn;

    if (conn->driver->domainGetAutostart)
        return conn->driver->domainGetAutostart (domain, autostart);

1914
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1915
    return -1;
1916 1917 1918 1919 1920
}

/**
 * virDomainSetAutostart:
 * @domain: a domain object
1921
 * @autostart: whether the domain should be automatically started 0 or 1
1922 1923 1924 1925 1926 1927 1928 1929
 *
 * Configure the domain to be automatically started
 * when the host machine boots.
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virDomainSetAutostart(virDomainPtr domain,
1930 1931 1932
                      int autostart)
{
    virConnectPtr conn;
1933 1934 1935 1936 1937 1938

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

1939 1940 1941 1942 1943
    conn = domain->conn;

    if (conn->driver->domainSetAutostart)
        return conn->driver->domainSetAutostart (domain, autostart);

1944
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1945
    return -1;
1946 1947
}

1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
/**
 * virDomainSetVcpus:
 * @domain: pointer to domain object, or NULL for Domain0
 * @nvcpus: the new number of virtual CPUs for this domain
 *
 * Dynamically change the number of virtual CPUs used by the domain.
 * Note that this call may fail if the underlying virtualization hypervisor
 * does not support it or if growing the number is arbitrary limited.
 * This function requires priviledged access to the hypervisor.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */

int
virDomainSetVcpus(virDomainPtr domain, unsigned int nvcpus)
{
1964 1965
    virConnectPtr conn;

1966 1967 1968 1969 1970 1971 1972 1973
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1974 1975 1976 1977
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1978

1979 1980 1981 1982
    if (nvcpus < 1) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
1983
    conn = domain->conn;
1984

1985 1986
    if (conn->driver->domainSetVcpus)
        return conn->driver->domainSetVcpus (domain, nvcpus);
1987

1988
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1989
    return -1;
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
}

/**
 * virDomainPinVcpu:
 * @domain: pointer to domain object, or NULL for Domain0
 * @vcpu: virtual CPU number
 * @cpumap: pointer to a bit map of real CPUs (in 8-bit bytes) (IN)
 * 	Each bit set to 1 means that corresponding CPU is usable.
 * 	Bytes are stored in little-endian order: CPU0-7, 8-15...
 * 	In each byte, lowest CPU number is least significant bit.
 * @maplen: number of bytes in cpumap, from 1 up to size of CPU map in
 *	underlying virtualization system (Xen...).
 *	If maplen < size, missing bytes are set to zero.
 *	If maplen > size, failure code is returned.
 * 
 * Dynamically change the real CPUs which can be allocated to a virtual CPU.
 * This function requires priviledged access to the hypervisor.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
int
virDomainPinVcpu(virDomainPtr domain, unsigned int vcpu,
                 unsigned char *cpumap, int maplen)
{
2014 2015
    virConnectPtr conn;

2016 2017 2018 2019 2020 2021 2022 2023
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
2024 2025 2026 2027
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
2028

2029
    if ((vcpu > 32000) || (cpumap == NULL) || (maplen < 1)) {
2030 2031 2032
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
2033

2034 2035
    conn = domain->conn;

2036 2037 2038
    if (conn->driver->domainPinVcpu)
        return conn->driver->domainPinVcpu (domain, vcpu, cpumap, maplen);

2039
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2040
    return -1;
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
}

/**
 * virDomainGetVcpus:
 * @domain: pointer to domain object, or NULL for Domain0
 * @info: pointer to an array of virVcpuInfo structures (OUT)
 * @maxinfo: number of structures in info array
 * @cpumaps: pointer to an bit map of real CPUs for all vcpus of this
 *      domain (in 8-bit bytes) (OUT)
 *	If cpumaps is NULL, then no cupmap information is returned by the API.
 *	It's assumed there is <maxinfo> cpumap in cpumaps array.
 *	The memory allocated to cpumaps must be (maxinfo * maplen) bytes
 *	(ie: calloc(maxinfo, maplen)).
 *	One cpumap inside cpumaps has the format described in
 *      virDomainPinVcpu() API.
 * @maplen: number of bytes in one cpumap, from 1 up to size of CPU map in
 *	underlying virtualization system (Xen...).
 * 
 * Extract information about virtual CPUs of domain, store it in info array
 * and also in cpumaps if this pointer is'nt NULL.
 *
 * Returns the number of info filled in case of success, -1 in case of failure.
 */
int
virDomainGetVcpus(virDomainPtr domain, virVcpuInfoPtr info, int maxinfo,
		  unsigned char *cpumaps, int maplen)
{
2068
    virConnectPtr conn;
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085

    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    if ((info == NULL) || (maxinfo < 1)) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
    if (cpumaps != NULL && maplen < 1) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
2086

2087 2088
    conn = domain->conn;

2089 2090 2091 2092
    if (conn->driver->domainGetVcpus)
        return conn->driver->domainGetVcpus (domain, info, maxinfo,
                                             cpumaps, maplen);

2093
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2094
    return -1;
2095
}
2096

2097 2098 2099 2100
/**
 * virDomainGetMaxVcpus:
 * @domain: pointer to domain object
 * 
2101 2102 2103 2104 2105
 * Provides the maximum number of virtual CPUs supported for
 * the guest VM. If the guest is inactive, this is basically
 * the same as virConnectGetMaxVcpus. If the guest is running
 * this will reflect the maximum number of virtual CPUs the
 * guest was booted with.
2106 2107 2108 2109
 *
 * Returns the maximum of virtual CPU or -1 in case of error.
 */
int
2110 2111
virDomainGetMaxVcpus(virDomainPtr domain)
{
2112 2113 2114 2115 2116 2117 2118 2119 2120
    virConnectPtr conn;

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

    conn = domain->conn;

2121 2122 2123
    if (conn->driver->domainGetMaxVcpus)
        return conn->driver->domainGetMaxVcpus (domain);

2124
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2125
    return -1;
2126 2127 2128
}


2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
/**
 * virDomainAttachDevice:
 * @domain: pointer to domain object
 * @xml: pointer to XML description of one device
 * 
 * Create a virtual device attachment to backend.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
int
virDomainAttachDevice(virDomainPtr domain, char *xml)
{
    virConnectPtr conn;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
    conn = domain->conn;

2153 2154 2155
    if (conn->driver->domainAttachDevice)
        return conn->driver->domainAttachDevice (domain, xml);

2156
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2157
    return -1;
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
}

/**
 * virDomainDetachDevice:
 * @domain: pointer to domain object
 * @xml: pointer to XML description of one device
 * 
 * Destroy a virtual device attachment to backend.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
int
virDomainDetachDevice(virDomainPtr domain, char *xml)
{
    virConnectPtr conn;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
    conn = domain->conn;

2184 2185 2186
    if (conn->driver->domainDetachDevice)
        return conn->driver->domainDetachDevice (domain, xml);

2187
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2188
    return -1;
2189
}
2190

2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
/**
 * virNetworkGetConnect:
 * @net: pointer to a network
 *
 * Returns the connection pointer associated with a network.  The
 * reference counter on the connection is not increased by this
 * call.
 *
 * Returns the virConnectPtr or NULL in case of failure.
 */
virConnectPtr
virNetworkGetConnect (virNetworkPtr net)
{
    if (!VIR_IS_NETWORK (net)) {
        virLibNetworkError (net, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return NULL;
    }
    return net->conn;
}

2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
/**
 * virConnectNumOfNetworks:
 * @conn: pointer to the hypervisor connection
 *
 * Provides the number of active networks.
 *
 * Returns the number of network found or -1 in case of error
 */
int
virConnectNumOfNetworks(virConnectPtr conn)
{
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

2227 2228
    if (conn->networkDriver && conn->networkDriver->numOfNetworks)
        return conn->networkDriver->numOfNetworks (conn);
2229

2230
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2231
    return -1;
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244
}

/**
 * virConnectListNetworks:
 * @conn: pointer to the hypervisor connection
 * @names: array to collect the list of names of active networks
 * @maxnames: size of @names
 *
 * Collect the list of active networks, and store their names in @names
 *
 * Returns the number of networks found or -1 in case of error
 */
int
2245
virConnectListNetworks(virConnectPtr conn, char **const names, int maxnames)
2246 2247 2248 2249 2250 2251
{
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

2252
    if ((names == NULL) || (maxnames < 0)) {
2253 2254 2255 2256
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

2257 2258
    if (conn->networkDriver && conn->networkDriver->listNetworks)
        return conn->networkDriver->listNetworks (conn, names, maxnames);
2259

2260
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2261
    return -1;
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279
}

/**
 * virConnectNumOfDefinedNetworks:
 * @conn: pointer to the hypervisor connection
 *
 * Provides the number of inactive networks.
 *
 * Returns the number of networks found or -1 in case of error
 */
int
virConnectNumOfDefinedNetworks(virConnectPtr conn)
{
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

2280 2281
    if (conn->networkDriver && conn->networkDriver->numOfDefinedNetworks)
        return conn->networkDriver->numOfDefinedNetworks (conn);
2282

2283
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2284
    return -1;
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
}

/**
 * virConnectListDefinedNetworks:
 * @conn: pointer to the hypervisor connection
 * @names: pointer to an array to store the names
 * @maxnames: size of the array
 *
 * list the inactive networks, stores the pointers to the names in @names
 *
 * Returns the number of names provided in the array or -1 in case of error
 */
int
2298
virConnectListDefinedNetworks(virConnectPtr conn, char **const names,
2299 2300
                              int maxnames)
{
2301 2302 2303 2304 2305
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

2306
    if ((names == NULL) || (maxnames < 0)) {
2307 2308 2309 2310
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

2311 2312 2313
    if (conn->networkDriver && conn->networkDriver->listDefinedNetworks)
        return conn->networkDriver->listDefinedNetworks (conn,
                                                         names, maxnames);
2314

2315
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2316
    return -1;
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339
}

/**
 * virNetworkLookupByName:
 * @conn: pointer to the hypervisor connection
 * @name: name for the network
 *
 * Try to lookup a network on the given hypervisor based on its name.
 *
 * Returns a new network object or NULL in case of failure
 */
virNetworkPtr
virNetworkLookupByName(virConnectPtr conn, const char *name)
{
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

2340 2341 2342
    if (conn->networkDriver && conn->networkDriver->networkLookupByName)
        return conn->networkDriver->networkLookupByName (conn, name);

2343
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2344
    return NULL;
2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367
}

/**
 * virNetworkLookupByUUID:
 * @conn: pointer to the hypervisor connection
 * @uuid: the raw UUID for the network
 *
 * Try to lookup a network on the given hypervisor based on its UUID.
 *
 * Returns a new network object or NULL in case of failure
 */
virNetworkPtr
virNetworkLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

2368 2369
    if (conn->networkDriver && conn->networkDriver->networkLookupByUUID)
        return conn->networkDriver->networkLookupByUUID (conn, uuid);
2370

2371
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2372
    return NULL;
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
}

/**
 * virNetworkLookupByUUIDString:
 * @conn: pointer to the hypervisor connection
 * @uuidstr: the string UUID for the network
 *
 * Try to lookup a network on the given hypervisor based on its UUID.
 *
 * Returns a new network object or NULL in case of failure
 */
virNetworkPtr
virNetworkLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
{
    int raw[VIR_UUID_BUFLEN], i;
    unsigned char uuid[VIR_UUID_BUFLEN];
    int ret;

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

    /* XXX: sexpr_uuid() also supports 'xxxx-xxxx-xxxx-xxxx' format.
     *      We needn't it here. Right?
     */
    ret = sscanf(uuidstr,
                 "%02x%02x%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x%02x%02x%02x%02x",
                 raw + 0, raw + 1, raw + 2, raw + 3,
                 raw + 4, raw + 5, raw + 6, raw + 7,
                 raw + 8, raw + 9, raw + 10, raw + 11,
                 raw + 12, raw + 13, raw + 14, raw + 15);

    if (ret!=VIR_UUID_BUFLEN) {
	virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return (NULL);
    }
    for (i = 0; i < VIR_UUID_BUFLEN; i++)
        uuid[i] = raw[i] & 0xFF;

    return virNetworkLookupByUUID(conn, &uuid[0]);
}

/**
 * virNetworkCreateXML:
 * @conn: pointer to the hypervisor connection
 * @xmlDesc: an XML description of the network
 *
 * Create and start a new virtual network, based on an XML description
 * similar to the one returned by virNetworkGetXMLDesc()
 *
 * Returns a new network object or NULL in case of failure
 */
virNetworkPtr
virNetworkCreateXML(virConnectPtr conn, const char *xmlDesc)
{
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (NULL);
    }

2450 2451 2452
    if (conn->networkDriver && conn->networkDriver->networkCreateXML)
        return conn->networkDriver->networkCreateXML (conn, xmlDesc);

2453
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2454
    return NULL;
2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466
}

/**
 * virNetworkDefineXML:
 * @conn: pointer to the hypervisor connection
 * @xml: the XML description for the network, preferably in UTF-8
 *
 * Define a network, but does not create it
 *
 * Returns NULL in case of error, a pointer to the network otherwise
 */
virNetworkPtr
2467 2468
virNetworkDefineXML(virConnectPtr conn, const char *xml)
{
2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (NULL);
    }
    if (xml == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

2482 2483
    if (conn->networkDriver && conn->networkDriver->networkDefineXML)
        return conn->networkDriver->networkDefineXML (conn, xml);
2484

2485
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2486
    return NULL;
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510
}

/**
 * virNetworkUndefine:
 * @network: pointer to a defined network
 *
 * Undefine a network but does not stop it if it is running
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
virNetworkUndefine(virNetworkPtr network) {
    virConnectPtr conn;

    if (!VIR_IS_CONNECTED_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }
    conn = network->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }

2511 2512
    if (conn->networkDriver && conn->networkDriver->networkUndefine)
        return conn->networkDriver->networkUndefine (network);
2513

2514
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2515
    return -1;
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527
}

/**
 * virNetworkCreate:
 * @network: pointer to a defined network
 *
 * Create and start a defined network. If the call succeed the network
 * moves from the defined to the running networks pools.
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
2528 2529
virNetworkCreate(virNetworkPtr network)
{
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544
    virConnectPtr conn;
    if (network == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }
    conn = network->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }

2545 2546 2547
    if (conn->networkDriver && conn->networkDriver->networkCreate)
        return conn->networkDriver->networkCreate (network);

2548
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2549
    return -1;
2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
}

/**
 * virNetworkDestroy:
 * @network: a network object
 *
 * Destroy the network object. The running instance is shutdown if not down
 * already and all resources used by it are given back to the hypervisor.
 * The data structure is freed and should not be used thereafter if the
 * call does not return an error.
 * This function may requires priviledged access
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virNetworkDestroy(virNetworkPtr network)
{
    virConnectPtr conn;

    if (!VIR_IS_CONNECTED_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }

    conn = network->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }

2580 2581
    if (conn->networkDriver && conn->networkDriver->networkDestroy)
        return conn->networkDriver->networkDestroy (network);
2582

2583
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2584
    return -1;
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702
}

/**
 * virNetworkFree:
 * @network: a network object
 *
 * Free the network 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
virNetworkFree(virNetworkPtr network)
{
    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }
    if (virFreeNetwork(network->conn, network) < 0)
        return (-1);
    return(0);
}

/**
 * virNetworkGetName:
 * @network: a network object
 *
 * Get the public name for that network
 *
 * Returns a pointer to the name or NULL, the string need not be deallocated
 * its lifetime will be the same as the network object.
 */
const char *
virNetworkGetName(virNetworkPtr network)
{
    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (NULL);
    }
    return (network->name);
}

/**
 * virNetworkGetUUID:
 * @network: a network object
 * @uuid: pointer to a VIR_UUID_BUFLEN bytes array
 *
 * Get the UUID for a network
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virNetworkGetUUID(virNetworkPtr network, unsigned char *uuid)
{
    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }
    if (uuid == NULL) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    memcpy(uuid, &network->uuid[0], VIR_UUID_BUFLEN);

    return (0);
}

/**
 * virNetworkGetUUIDString:
 * @network: a network object
 * @buf: pointer to a VIR_UUID_STRING_BUFLEN bytes array
 *
 * Get the UUID for a network as string. For more information about
 * UUID see RFC4122.
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virNetworkGetUUIDString(virNetworkPtr network, char *buf)
{
    unsigned char uuid[VIR_UUID_BUFLEN];

    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }
    if (buf == NULL) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    if (virNetworkGetUUID(network, &uuid[0]))
	return (-1);

    snprintf(buf, VIR_UUID_STRING_BUFLEN,
	"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
                      uuid[0], uuid[1], uuid[2], uuid[3],
                      uuid[4], uuid[5], uuid[6], uuid[7],
                      uuid[8], uuid[9], uuid[10], uuid[11],
                      uuid[12], uuid[13], uuid[14], uuid[15]);
    return (0);
}

/**
 * virNetworkGetXMLDesc:
 * @network: a network object
 * @flags: and OR'ed set of extraction flags, not used yet
 *
 * Provide an XML description of the network. The description may be reused
 * later to relaunch the network with virNetworkCreateXML().
 *
 * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
virNetworkGetXMLDesc(virNetworkPtr network, int flags)
{
2703 2704
    virConnectPtr conn;

2705 2706 2707 2708 2709 2710 2711 2712 2713
    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (NULL);
    }
    if (flags != 0) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

2714 2715 2716 2717 2718
    conn = network->conn;

    if (conn->networkDriver && conn->networkDriver->networkDumpXML)
        return conn->networkDriver->networkDumpXML (network, flags);

2719
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2720
    return NULL;
2721
}
2722 2723 2724 2725 2726

/**
 * virNetworkGetBridgeName:
 * @network: a network object
 *
2727
 * Provides a bridge interface name to which a domain may connect
2728 2729 2730 2731 2732 2733 2734 2735
 * a network interface in order to join the network.
 *
 * Returns a 0 terminated interface name, or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
virNetworkGetBridgeName(virNetworkPtr network)
{
2736 2737
    virConnectPtr conn;

2738 2739 2740 2741 2742
    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (NULL);
    }

2743 2744 2745 2746 2747
    conn = network->conn;

    if (conn->networkDriver && conn->networkDriver->networkGetBridgeName)
        return conn->networkDriver->networkGetBridgeName (network);

2748
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2749
    return NULL;
2750
}
2751 2752 2753 2754

/**
 * virNetworkGetAutostart:
 * @network: a network object
2755
 * @autostart: the value returned
2756
 *
2757
 * Provides a boolean value indicating whether the network
2758 2759 2760 2761 2762 2763 2764
 * configured to be automatically started when the host
 * machine boots.
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virNetworkGetAutostart(virNetworkPtr network,
2765 2766 2767
                       int *autostart)
{
    virConnectPtr conn;
2768 2769 2770 2771 2772 2773 2774 2775 2776 2777

    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }
    if (!autostart) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

2778 2779 2780 2781 2782
    conn = network->conn;

    if (conn->networkDriver && conn->networkDriver->networkGetAutostart)
        return conn->networkDriver->networkGetAutostart (network, autostart);

2783
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2784
    return -1;
2785 2786 2787 2788 2789
}

/**
 * virNetworkSetAutostart:
 * @network: a network object
2790
 * @autostart: whether the network should be automatically started 0 or 1
2791 2792 2793 2794 2795 2796 2797 2798
 *
 * Configure the network to be automatically started
 * when the host machine boots.
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virNetworkSetAutostart(virNetworkPtr network,
2799 2800 2801
                       int autostart)
{
    virConnectPtr conn;
2802 2803 2804 2805 2806 2807

    if (!VIR_IS_NETWORK(network)) {
        virLibNetworkError(network, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }

2808 2809 2810 2811 2812
    conn = network->conn;

    if (conn->networkDriver && conn->networkDriver->networkSetAutostart)
        return conn->networkDriver->networkSetAutostart (network, autostart);

2813
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2814
    return -1;
2815
}
2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829

/*
 * vim: set tabstop=4:
 * vim: set shiftwidth=4:
 * vim: set expandtab:
 */
/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */