libvirt.c 276.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,2008,2009 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 <config.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
#include <assert.h>
21 22 23
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
24
#include <time.h>
25

26 27
#include <libxml/parser.h>
#include <libxml/xpath.h>
28
#include <libxml/uri.h>
29 30
#include "getpass.h"

31
#ifdef HAVE_WINSOCK2_H
32 33
#include <winsock2.h>
#endif
34

35
#include "virterror_internal.h"
36
#include "logging.h"
37
#include "datatypes.h"
38
#include "driver.h"
39

40
#include "uuid.h"
41
#include "util.h"
42
#include "memory.h"
43

44
#ifndef WITH_DRIVER_MODULES
45
#ifdef WITH_TEST
46
#include "test/test_driver.h"
47 48
#endif
#ifdef WITH_XEN
49
#include "xen/xen_driver.h"
50 51
#endif
#ifdef WITH_REMOTE
52
#include "remote/remote_driver.h"
53
#endif
54
#ifdef WITH_OPENVZ
55
#include "openvz/openvz_driver.h"
56
#endif
57 58 59
#ifdef WITH_PHYP
#include "phyp/phyp_driver.h"
#endif
60 61 62
#ifdef WITH_VBOX
#include "vbox/vbox_driver.h"
#endif
63 64 65
#ifdef WITH_ESX
#include "esx/esx_driver.h"
#endif
66
#endif
67

68 69
#define VIR_FROM_THIS VIR_FROM_NONE

D
Daniel Veillard 已提交
70 71 72
/*
 * TODO:
 * - use lock to protect against concurrent accesses ?
D
Daniel Veillard 已提交
73
 * - use reference counting to guarantee coherent pointer state ?
D
Daniel Veillard 已提交
74 75
 */

76 77
#define MAX_DRIVERS 10

78
static virDriverPtr virDriverTab[MAX_DRIVERS];
79
static int virDriverTabCount = 0;
80
static virNetworkDriverPtr virNetworkDriverTab[MAX_DRIVERS];
81
static int virNetworkDriverTabCount = 0;
D
Daniel Veillard 已提交
82 83
static virInterfaceDriverPtr virInterfaceDriverTab[MAX_DRIVERS];
static int virInterfaceDriverTabCount = 0;
84 85
static virStorageDriverPtr virStorageDriverTab[MAX_DRIVERS];
static int virStorageDriverTabCount = 0;
86 87
static virDeviceMonitorPtr virDeviceMonitorTab[MAX_DRIVERS];
static int virDeviceMonitorTabCount = 0;
88 89
static virSecretDriverPtr virSecretDriverTab[MAX_DRIVERS];
static int virSecretDriverTabCount = 0;
A
Atsushi SAKAI 已提交
90
#ifdef WITH_LIBVIRTD
91 92
static virStateDriverPtr virStateDriverTab[MAX_DRIVERS];
static int virStateDriverTabCount = 0;
A
Atsushi SAKAI 已提交
93
#endif
94 95
static int initialized = 0;

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#if defined(POLKIT_AUTH)
static int virConnectAuthGainPolkit(const char *privilege) {
    const char *const args[] = {
        POLKIT_AUTH, "--obtain", privilege, NULL
    };
    int childpid, status, ret;

    /* Root has all rights */
    if (getuid() == 0)
        return 0;

    if ((childpid = fork()) < 0)
        return -1;

    if (!childpid) {
        execvp(args[0], (char **)args);
        _exit(-1);
    }

    while ((ret = waitpid(childpid, &status, 0) == -1) && errno == EINTR);
    if (ret == -1) {
        return -1;
    }

    if (!WIFEXITED(status) ||
        (WEXITSTATUS(status) != 0 && WEXITSTATUS(status) != 1)) {
        return -1;
    }

    return 0;
}
#endif

129 130 131 132 133 134 135 136
static int virConnectAuthCallbackDefault(virConnectCredentialPtr cred,
                                         unsigned int ncred,
                                         void *cbdata ATTRIBUTE_UNUSED) {
    int i;

    for (i = 0 ; i < ncred ; i++) {
        char buf[1024];
        char *bufptr = buf;
137
        size_t len;
138 139

        switch (cred[i].type) {
140 141 142 143
        case VIR_CRED_EXTERNAL: {
            if (STRNEQ(cred[i].challenge, "PolicyKit"))
                return -1;

144
#if defined(POLKIT_AUTH)
145
            if (virConnectAuthGainPolkit(cred[i].prompt) < 0)
146
                return -1;
147 148 149 150 151 152 153
#else
            /*
             * Ignore & carry on. Although we can't auth
             * directly, the user may have authenticated
             * themselves already outside context of libvirt
             */
#endif
154 155
            break;
        }
156

157 158 159 160
        case VIR_CRED_USERNAME:
        case VIR_CRED_AUTHNAME:
        case VIR_CRED_ECHOPROMPT:
        case VIR_CRED_REALM:
161 162 163 164 165
            if (printf("%s:", cred[i].prompt) < 0)
                return -1;
            if (fflush(stdout) != 0)
                return -1;

166 167 168 169 170 171 172
            if (!fgets(buf, sizeof(buf), stdin)) {
                if (feof(stdin)) { /* Treat EOF as "" */
                    buf[0] = '\0';
                    break;
                }
                return -1;
            }
173 174 175
            len = strlen(buf);
            if (len != 0 && buf[len-1] == '\n')
                buf[len-1] = '\0';
176 177 178 179
            break;

        case VIR_CRED_PASSPHRASE:
        case VIR_CRED_NOECHOPROMPT:
180 181 182 183 184
            if (printf("%s:", cred[i].prompt) < 0)
                return -1;
            if (fflush(stdout) != 0)
                return -1;

185 186 187 188
            bufptr = getpass("");
            if (!bufptr)
                return -1;
            break;
189 190 191

        default:
            return -1;
192 193
        }

D
Daniel P. Berrange 已提交
194 195 196 197 198 199 200 201 202
        if (cred[i].type != VIR_CRED_EXTERNAL) {
            if (STREQ(bufptr, "") && cred[i].defresult)
                cred[i].result = strdup(cred[i].defresult);
            else
                cred[i].result = strdup(bufptr);
            if (!cred[i].result)
                return -1;
            cred[i].resultlen = strlen(cred[i].result);
        }
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    }

    return 0;
}

/* Don't typically want VIR_CRED_USERNAME. It enables you to authenticate
 * as one user, and act as another. It just results in annoying
 * prompts for the username twice & is very rarely what you want
 */
static int virConnectCredTypeDefault[] = {
    VIR_CRED_AUTHNAME,
    VIR_CRED_ECHOPROMPT,
    VIR_CRED_REALM,
    VIR_CRED_PASSPHRASE,
    VIR_CRED_NOECHOPROMPT,
218
    VIR_CRED_EXTERNAL,
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
};

static virConnectAuth virConnectAuthDefault = {
    virConnectCredTypeDefault,
    sizeof(virConnectCredTypeDefault)/sizeof(int),
    virConnectAuthCallbackDefault,
    NULL,
};

/*
 * virConnectAuthPtrDefault
 *
 * A default implementation of the authentication callbacks. This
 * implementation is suitable for command line based tools. It will
 * prompt for username, passwords, realm and one time keys as needed.
 * It will print on STDOUT, and read from STDIN. If this is not
 * suitable for the application's needs an alternative implementation
 * should be provided.
 */
virConnectAuthPtr virConnectAuthPtrDefault = &virConnectAuthDefault;

240 241 242 243 244 245 246 247 248 249
#if HAVE_WINSOCK2_H
static int
winsock_init (void)
{
    WORD winsock_version, err;
    WSADATA winsock_data;

    /* http://msdn2.microsoft.com/en-us/library/ms742213.aspx */
    winsock_version = MAKEWORD (2, 2);
    err = WSAStartup (winsock_version, &winsock_data);
J
Jim Meyering 已提交
250
    return err == 0 ? 0 : -1;
251 252 253
}
#endif

254 255 256 257 258 259 260 261 262 263 264 265 266 267
/**
 * 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);
268

269
    initialized = 1;
270

271
    if (virThreadInitialize() < 0 ||
272 273
        virErrorInitialize() < 0 ||
        virRandomInitialize(time(NULL) ^ getpid()))
274 275
        return -1;

276
    virLogSetFromEnv();
277 278 279

    DEBUG0("register drivers");

280 281 282 283
#if HAVE_WINSOCK2_H
    if (winsock_init () == -1) return -1;
#endif

284 285 286
    if (!bindtextdomain(GETTEXT_PACKAGE, LOCALEBASEDIR))
        return (-1);

287
    /*
288 289
     * Note that the order is important: the first ones have a higher
     * priority when calling virConnectOpen.
290
     */
291 292 293 294 295 296 297 298 299
#ifdef WITH_DRIVER_MODULES
    /* We don't care if any of these fail, because the whole point
     * is to allow users to only install modules they want to use.
     * If they try to use a open a connection for a module that
     * is not loaded they'll get a suitable error at that point
     */
    virDriverLoadModule("test");
    virDriverLoadModule("xen");
    virDriverLoadModule("openvz");
300
    virDriverLoadModule("vbox");
301
    virDriverLoadModule("esx");
302 303
    virDriverLoadModule("remote");
#else
304
#ifdef WITH_TEST
305
    if (testRegister() == -1) return -1;
306
#endif
307
#ifdef WITH_XEN
308
    if (xenRegister () == -1) return -1;
309
#endif
310 311
#ifdef WITH_OPENVZ
    if (openvzRegister() == -1) return -1;
D
Daniel Veillard 已提交
312
#endif
313 314 315
#ifdef WITH_PHYP
    if (phypRegister() == -1) return -1;
#endif
316 317 318
#ifdef WITH_VBOX
    if (vboxRegister() == -1) return -1;
#endif
319 320 321
#ifdef WITH_ESX
    if (esxRegister() == -1) return -1;
#endif
322 323
#ifdef WITH_REMOTE
    if (remoteRegister () == -1) return -1;
324
#endif
325
#endif
D
Daniel P. Berrange 已提交
326

327 328 329
    return(0);
}

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
#ifdef WIN32
BOOL WINAPI
DllMain (HINSTANCE instance, DWORD reason, LPVOID ignore);

BOOL WINAPI
DllMain (HINSTANCE instance ATTRIBUTE_UNUSED,
         DWORD reason,
         LPVOID ignore ATTRIBUTE_UNUSED)
{
    switch (reason) {
    case DLL_PROCESS_ATTACH:
        virInitialize();
        break;

    case DLL_THREAD_ATTACH:
        /* Nothing todo in libvirt yet */
        break;

    case DLL_THREAD_DETACH:
        /* Release per-thread local data */
        virThreadOnExit();
        break;

    case DLL_PROCESS_DETACH:
        /* Don't bother releasing per-thread data
           since (hopefully) windows cleans up
           everything on process exit */
        break;
    }

    return TRUE;
}
#endif
363 364


D
Daniel Veillard 已提交
365 366 367
/**
 * virLibConnError:
 * @conn: the connection if available
368
 * @error: the error number
D
Daniel Veillard 已提交
369 370 371 372 373
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
374 375
virLibConnError(virConnectPtr conn, virErrorNumber error, const char *info)
{
D
Daniel Veillard 已提交
376
    const char *errmsg;
377

D
Daniel Veillard 已提交
378 379 380
    if (error == VIR_ERR_OK)
        return;

381 382 383
    errmsg = virErrorMsg(error, info);
    virRaiseError(conn, NULL, NULL, VIR_FROM_NONE, error, VIR_ERR_ERROR,
                  errmsg, info, NULL, 0, 0, errmsg, info);
D
Daniel Veillard 已提交
384 385 386
}

/**
387
 * virLibConnWarning:
D
Daniel Veillard 已提交
388
 * @conn: the connection if available
389 390 391 392 393 394 395 396 397 398 399 400 401
 * @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;

402 403 404
    errmsg = virErrorMsg(error, info);
    virRaiseError(conn, NULL, NULL, VIR_FROM_NONE, error, VIR_ERR_WARNING,
                  errmsg, info, NULL, 0, 0, errmsg, info);
405 406 407 408 409 410
}

/**
 * virLibDomainError:
 * @domain: the domain if available
 * @error: the error number
D
Daniel Veillard 已提交
411 412 413 414 415
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
416 417 418
virLibDomainError(virDomainPtr domain, virErrorNumber error,
                  const char *info)
{
D
Daniel Veillard 已提交
419 420
    virConnectPtr conn = NULL;
    const char *errmsg;
421

D
Daniel Veillard 已提交
422 423 424
    if (error == VIR_ERR_OK)
        return;

425
    errmsg = virErrorMsg(error, info);
D
Daniel Veillard 已提交
426 427 428
    if (error != VIR_ERR_INVALID_DOMAIN) {
        conn = domain->conn;
    }
429 430
    virRaiseError(conn, domain, NULL, VIR_FROM_DOM, error, VIR_ERR_ERROR,
                  errmsg, info, NULL, 0, 0, errmsg, info);
D
Daniel Veillard 已提交
431 432
}

433
/**
434 435
 * virLibNetworkError:
 * @conn: the connection if available
D
Daniel Veillard 已提交
436
 * @error: the error number
437
 * @info: extra information string
438
 *
439
 * Handle an error at the connection level
440
 */
441 442 443 444 445 446 447 448 449 450
static void
virLibNetworkError(virNetworkPtr network, virErrorNumber error,
                   const char *info)
{
    virConnectPtr conn = NULL;
    const char *errmsg;

    if (error == VIR_ERR_OK)
        return;

451
    errmsg = virErrorMsg(error, info);
452 453 454
    if (error != VIR_ERR_INVALID_NETWORK) {
        conn = network->conn;
    }
455 456
    virRaiseError(conn, NULL, network, VIR_FROM_NET, error, VIR_ERR_ERROR,
                  errmsg, info, NULL, 0, 0, errmsg, info);
457 458
}

D
Daniel Veillard 已提交
459 460 461 462 463 464 465 466 467
/**
 * virLibInterfaceError:
 * @conn: the connection if available
 * @error: the error number
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
468
virLibInterfaceError(virInterfacePtr iface, virErrorNumber error,
D
Daniel Veillard 已提交
469 470 471 472 473 474 475 476 477 478
                   const char *info)
{
    virConnectPtr conn = NULL;
    const char *errmsg;

    if (error == VIR_ERR_OK)
        return;

    errmsg = virErrorMsg(error, info);
    if (error != VIR_ERR_INVALID_INTERFACE) {
479
        conn = iface->conn;
D
Daniel Veillard 已提交
480 481 482 483 484
    }
    virRaiseError(conn, NULL, NULL, VIR_FROM_INTERFACE, error, VIR_ERR_ERROR,
                  errmsg, info, NULL, 0, 0, errmsg, info);
}

485 486 487
/**
 * virLibStoragePoolError:
 * @conn: the connection if available
D
Daniel Veillard 已提交
488
 * @error: the error number
489 490 491 492 493 494 495 496 497 498 499 500 501 502
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
virLibStoragePoolError(virStoragePoolPtr pool, virErrorNumber error,
                       const char *info)
{
    virConnectPtr conn = NULL;
    const char *errmsg;

    if (error == VIR_ERR_OK)
        return;

503
    errmsg = virErrorMsg(error, info);
504 505 506
    if (error != VIR_ERR_INVALID_STORAGE_POOL)
        conn = pool->conn;

507 508
    virRaiseError(conn, NULL, NULL, VIR_FROM_STORAGE, error, VIR_ERR_ERROR,
                  errmsg, info, NULL, 0, 0, errmsg, info);
509 510 511 512 513
}

/**
 * virLibStorageVolError:
 * @conn: the connection if available
D
Daniel Veillard 已提交
514
 * @error: the error number
515 516 517 518 519 520 521 522 523 524 525 526 527 528
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
virLibStorageVolError(virStorageVolPtr vol, virErrorNumber error,
                      const char *info)
{
    virConnectPtr conn = NULL;
    const char *errmsg;

    if (error == VIR_ERR_OK)
        return;

529
    errmsg = virErrorMsg(error, info);
530 531 532
    if (error != VIR_ERR_INVALID_STORAGE_VOL)
        conn = vol->conn;

533 534
    virRaiseError(conn, NULL, NULL, VIR_FROM_STORAGE, error, VIR_ERR_ERROR,
                  errmsg, info, NULL, 0, 0, errmsg, info);
535 536
}

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
/**
 * virLibNodeDeviceError:
 * @dev: the device if available
 * @error: the error number
 * @info: extra information string
 *
 * Handle an error at the node device level
 */
static void
virLibNodeDeviceError(virNodeDevicePtr dev, 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_NODE_DEVICE)
        conn = dev->conn;

    virRaiseError(conn, NULL, NULL, VIR_FROM_NODEDEV, error, VIR_ERR_ERROR,
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

563 564 565 566
#define virLibStreamError(conn, code, fmt...)                   \
    virReportErrorHelper(conn, VIR_FROM_NONE, code, __FILE__,   \
                         __FUNCTION__, __LINE__, fmt)

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
/**
 * virLibSecretError:
 * @secret: the secret if available
 * @error: the error number
 * @info: extra information string
 *
 * Handle an error at the secret level
 */
static void
virLibSecretError(virSecretPtr secret, 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_SECRET)
        conn = secret->conn;

    virRaiseError(conn, NULL, NULL, VIR_FROM_SECRET, error, VIR_ERR_ERROR,
                  errmsg, info, NULL, 0, 0, errmsg, info);
}

592 593 594 595 596 597 598 599 600 601 602
/**
 * 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)
{
603 604 605 606 607
    if (virInitialize() < 0)
      return -1;

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

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

616 617 618
    DEBUG ("registering %s as network driver %d",
           driver->name, virNetworkDriverTabCount);

619 620
    virNetworkDriverTab[virNetworkDriverTabCount] = driver;
    return virNetworkDriverTabCount++;
621 622
}

D
Daniel Veillard 已提交
623 624
/**
 * virRegisterInterfaceDriver:
L
Laine Stump 已提交
625
 * @driver: pointer to an interface driver block
D
Daniel Veillard 已提交
626
 *
L
Laine Stump 已提交
627
 * Register an interface virtualization driver
D
Daniel Veillard 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
 *
 * Returns the driver priority or -1 in case of error.
 */
int
virRegisterInterfaceDriver(virInterfaceDriverPtr driver)
{
    if (virInitialize() < 0)
      return -1;

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

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

    DEBUG ("registering %s as interface driver %d",
           driver->name, virInterfaceDriverTabCount);

    virInterfaceDriverTab[virInterfaceDriverTabCount] = driver;
    return virInterfaceDriverTabCount++;
}

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
/**
 * virRegisterStorageDriver:
 * @driver: pointer to a storage driver block
 *
 * Register a storage virtualization driver
 *
 * Returns the driver priority or -1 in case of error.
 */
int
virRegisterStorageDriver(virStorageDriverPtr driver)
{
    if (virInitialize() < 0)
      return -1;

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

    if (virStorageDriverTabCount >= MAX_DRIVERS) {
674
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
675 676 677
        return(-1);
    }

678 679 680
    DEBUG ("registering %s as storage driver %d",
           driver->name, virStorageDriverTabCount);

681 682 683 684
    virStorageDriverTab[virStorageDriverTabCount] = driver;
    return virStorageDriverTabCount++;
}

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
/**
 * virRegisterDeviceMonitor:
 * @driver: pointer to a device monitor block
 *
 * Register a device monitor
 *
 * Returns the driver priority or -1 in case of error.
 */
int
virRegisterDeviceMonitor(virDeviceMonitorPtr driver)
{
    if (virInitialize() < 0)
      return -1;

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

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

709 710 711
    DEBUG ("registering %s as device driver %d",
           driver->name, virDeviceMonitorTabCount);

712 713 714 715
    virDeviceMonitorTab[virDeviceMonitorTabCount] = driver;
    return virDeviceMonitorTabCount++;
}

716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
/**
 * virRegisterSecretDriver:
 * @driver: pointer to a secret driver block
 *
 * Register a secret driver
 *
 * Returns the driver priority or -1 in case of error.
 */
int
virRegisterSecretDriver(virSecretDriverPtr driver)
{
    if (virInitialize() < 0)
      return -1;

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

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

    DEBUG ("registering %s as secret driver %d",
           driver->name, virSecretDriverTabCount);

    virSecretDriverTab[virSecretDriverTabCount] = driver;
    return virSecretDriverTabCount++;
}

747 748 749 750 751 752 753 754 755 756 757
/**
 * 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)
{
758 759 760 761 762
    if (virInitialize() < 0)
      return -1;

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

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

771
    if (driver->no < 0) {
772
        virLibConnError
773 774 775 776 777
            (NULL, VIR_ERR_INVALID_ARG,
             "virRegisterDriver: tried to register an internal Xen driver");
        return -1;
    }

778 779 780
    DEBUG ("registering %s as driver %d",
           driver->name, virDriverTabCount);

781 782
    virDriverTab[virDriverTabCount] = driver;
    return virDriverTabCount++;
783 784
}

A
Atsushi SAKAI 已提交
785
#ifdef WITH_LIBVIRTD
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
/**
 * 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) {
806
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
807 808 809 810 811 812 813
        return(-1);
    }

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

814 815
/**
 * virStateInitialize:
816
 * @privileged: set to 1 if running with root priviledge, 0 otherwise
817 818 819
 *
 * Initialize all virtualization drivers.
 *
820
 * Returns 0 if all succeed, -1 upon any failure.
821
 */
822
int virStateInitialize(int privileged) {
823 824 825 826 827 828
    int i, ret = 0;

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

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
829
        if (virStateDriverTab[i]->initialize &&
830
            virStateDriverTab[i]->initialize(privileged) < 0)
831 832 833 834 835
            ret = -1;
    }
    return ret;
}

836 837 838 839 840
/**
 * virStateCleanup:
 *
 * Run each virtualization driver's cleanup method.
 *
841
 * Returns 0 if all succeed, -1 upon any failure.
842
 */
D
Daniel P. Berrange 已提交
843
int virStateCleanup(void) {
844 845 846
    int i, ret = 0;

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
847 848
        if (virStateDriverTab[i]->cleanup &&
            virStateDriverTab[i]->cleanup() < 0)
849 850 851 852 853
            ret = -1;
    }
    return ret;
}

854 855 856 857 858
/**
 * virStateReload:
 *
 * Run each virtualization driver's reload method.
 *
859
 * Returns 0 if all succeed, -1 upon any failure.
860
 */
D
Daniel P. Berrange 已提交
861
int virStateReload(void) {
862 863 864
    int i, ret = 0;

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
865 866
        if (virStateDriverTab[i]->reload &&
            virStateDriverTab[i]->reload() < 0)
867 868 869 870 871
            ret = -1;
    }
    return ret;
}

872 873 874 875 876
/**
 * virStateActive:
 *
 * Run each virtualization driver's "active" method.
 *
877
 * Returns 0 if none are active, 1 if at least one is.
878
 */
D
Daniel P. Berrange 已提交
879
int virStateActive(void) {
880 881 882
    int i, ret = 0;

    for (i = 0 ; i < virStateDriverTabCount ; i++) {
883 884
        if (virStateDriverTab[i]->active &&
            virStateDriverTab[i]->active())
885 886 887 888 889
            ret = 1;
    }
    return ret;
}

A
Atsushi SAKAI 已提交
890
#endif
891

892 893


894 895 896
/**
 * virGetVersion:
 * @libVer: return value for the library version (OUT)
897
 * @type: the type of connection/driver looked at
898 899 900 901 902
 * @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
D
Daniel Veillard 已提交
903
 * @type is unknown or not available, an error code will be returned and
904 905 906 907 908 909
 * @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
910 911 912
virGetVersion(unsigned long *libVer, const char *type,
              unsigned long *typeVer)
{
913
    DEBUG("libVir=%p, type=%s, typeVer=%p", libVer, type, typeVer);
914

915
    if (!initialized)
916
        if (virInitialize() < 0)
917
            return -1;
918

919
    if (libVer == NULL)
920
        return (-1);
921 922 923
    *libVer = LIBVIR_VERSION_NUMBER;

    if (typeVer != NULL) {
924
        if (type == NULL)
925
            type = "Xen";
926 927 928 929 930

/* FIXME: Add _proper_ type version handling for loadable driver modules... */
#ifdef WITH_DRIVER_MODULES
        *typeVer = LIBVIR_VERSION_NUMBER;
#else
931
        *typeVer = 0;
932

933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
#if WITH_XEN
        if (STRCASEEQ(type, "Xen"))
            *typeVer = xenUnifiedVersion();
#endif
#if WITH_TEST
        if (STRCASEEQ(type, "Test"))
            *typeVer = LIBVIR_VERSION_NUMBER;
#endif
#if WITH_QEMU
        if (STRCASEEQ(type, "QEMU"))
            *typeVer = LIBVIR_VERSION_NUMBER;
#endif
#if WITH_LXC
        if (STRCASEEQ(type, "LXC"))
            *typeVer = LIBVIR_VERSION_NUMBER;
#endif
949 950 951 952
#if WITH_PHYP
        if (STRCASEEQ(type, "phyp"))
            *typeVer = LIBVIR_VERSION_NUMBER;
#endif
953 954 955 956
#if WITH_OPENVZ
        if (STRCASEEQ(type, "OpenVZ"))
            *typeVer = LIBVIR_VERSION_NUMBER;
#endif
957 958 959 960
#if WITH_VBOX
        if (STRCASEEQ(type, "VBox"))
            *typeVer = LIBVIR_VERSION_NUMBER;
#endif
961 962 963 964
#if WITH_UML
        if (STRCASEEQ(type, "UML"))
            *typeVer = LIBVIR_VERSION_NUMBER;
#endif
D
Daniel Veillard 已提交
965 966 967 968
#if WITH_ONE
        if (STRCASEEQ(type, "ONE"))
            *typeVer = LIBVIR_VERSION_NUMBER;
#endif
969 970 971 972
#if WITH_ESX
        if (STRCASEEQ(type, "ESX"))
            *typeVer = LIBVIR_VERSION_NUMBER;
#endif
973 974 975 976 977
#if WITH_REMOTE
        if (STRCASEEQ(type, "Remote"))
            *typeVer = remoteVersion();
#endif
        if (*typeVer == 0) {
978
            virLibConnError(NULL, VIR_ERR_NO_SUPPORT, type);
979 980
            return (-1);
        }
981
#endif /* WITH_DRIVER_MODULES */
982 983
    }
    return (0);
984 985
}

986
static virConnectPtr
987 988 989
do_open (const char *name,
         virConnectAuthPtr auth,
         int flags)
990
{
991
    int i, res;
992 993
    virConnectPtr ret;

994 995
    virResetLastError();

996 997 998
    ret = virGetConnect();
    if (ret == NULL)
        return NULL;
999

1000 1001 1002 1003 1004 1005 1006 1007
    /*
     *  If no URI is passed, then check for an environment string if not
     *  available probe the compiled in drivers to find a default hypervisor
     *  if detectable.
     */
    if (!name || name[0] == '\0') {
        char *defname = getenv("LIBVIRT_DEFAULT_URI");
        if (defname && *defname) {
1008
            DEBUG("Using LIBVIRT_DEFAULT_URI %s", defname);
1009 1010
            name = defname;
        } else {
1011
            name = NULL;
1012
        }
1013
    }
1014

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
    if (name) {
        /* Convert xen -> xen:/// for back compat */
        if (STRCASEEQ(name, "xen"))
            name = "xen:///";

        /* Convert xen:// -> xen:/// because xmlParseURI cannot parse the
         * former.  This allows URIs such as xen://localhost to work.
         */
        if (STREQ (name, "xen://"))
            name = "xen:///";

        ret->uri = xmlParseURI (name);
        if (!ret->uri) {
            virLibConnError (ret, VIR_ERR_INVALID_ARG,
                             _("could not parse connection URI"));
            goto failed;
        }
1032

1033 1034 1035 1036 1037 1038 1039 1040 1041
        DEBUG("name \"%s\" to URI components:\n"
              "  scheme %s\n"
              "  opaque %s\n"
              "  authority %s\n"
              "  server %s\n"
              "  user %s\n"
              "  port %d\n"
              "  path %s\n",
              name,
1042 1043 1044 1045
              NULLSTR(ret->uri->scheme), NULLSTR(ret->uri->opaque),
              NULLSTR(ret->uri->authority), NULLSTR(ret->uri->server),
              NULLSTR(ret->uri->user), ret->uri->port,
              NULLSTR(ret->uri->path));
1046 1047
    } else {
        DEBUG0("no name, allowing driver auto-select");
1048 1049
    }

1050 1051 1052
    /* Cleansing flags */
    ret->flags = flags & VIR_CONNECT_RO;

1053
    for (i = 0; i < virDriverTabCount; i++) {
1054 1055
        DEBUG("trying driver %d (%s) ...",
              i, virDriverTab[i]->name);
1056
        res = virDriverTab[i]->open (ret, auth, flags);
1057 1058 1059 1060 1061
        DEBUG("driver %d %s returned %s",
              i, virDriverTab[i]->name,
              res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
              (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
               (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
1062 1063 1064 1065 1066
        if (res == VIR_DRV_OPEN_ERROR) goto failed;
        else if (res == VIR_DRV_OPEN_SUCCESS) {
            ret->driver = virDriverTab[i];
            break;
        }
1067 1068
    }

1069
    if (!ret->driver) {
1070 1071
        /* If we reach here, then all drivers declined the connection. */
        virLibConnError (NULL, VIR_ERR_NO_CONNECT, name);
1072
        goto failed;
1073 1074
    }

1075
    for (i = 0; i < virNetworkDriverTabCount; i++) {
1076
        res = virNetworkDriverTab[i]->open (ret, auth, flags);
1077 1078 1079 1080 1081
        DEBUG("network driver %d %s returned %s",
              i, virNetworkDriverTab[i]->name,
              res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
              (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
               (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
1082
        if (res == VIR_DRV_OPEN_ERROR) {
1083
            if (STREQ(virNetworkDriverTab[i]->name, "remote")) {
1084
                virLibConnWarning (NULL, VIR_WAR_NO_NETWORK,
1085 1086
                                   "Is the daemon running ?");
            }
1087 1088
            break;
        } else if (res == VIR_DRV_OPEN_SUCCESS) {
1089 1090 1091
            ret->networkDriver = virNetworkDriverTab[i];
            break;
        }
1092
    }
1093

D
Daniel Veillard 已提交
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    for (i = 0; i < virInterfaceDriverTabCount; i++) {
        res = virInterfaceDriverTab[i]->open (ret, auth, flags);
        DEBUG("interface driver %d %s returned %s",
              i, virInterfaceDriverTab[i]->name,
              res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
              (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
               (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
        if (res == VIR_DRV_OPEN_ERROR) {
            if (STREQ(virInterfaceDriverTab[i]->name, "remote")) {
                virLibConnWarning (NULL, VIR_WAR_NO_INTERFACE,
                                   "Is the daemon running ?");
            }
            break;
        } else if (res == VIR_DRV_OPEN_SUCCESS) {
            ret->interfaceDriver = virInterfaceDriverTab[i];
            break;
        }
    }
1112 1113 1114

    /* Secondary driver for storage. Optional */
    for (i = 0; i < virStorageDriverTabCount; i++) {
1115
        res = virStorageDriverTab[i]->open (ret, auth, flags);
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
        DEBUG("storage driver %d %s returned %s",
              i, virStorageDriverTab[i]->name,
              res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
              (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
               (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
        if (res == VIR_DRV_OPEN_ERROR) {
            if (0 && STREQ(virStorageDriverTab[i]->name, "remote")) {
                virLibConnWarning (NULL, VIR_WAR_NO_STORAGE,
                                   "Is the daemon running ?");
            }
            break;
         } else if (res == VIR_DRV_OPEN_SUCCESS) {
            ret->storageDriver = virStorageDriverTab[i];
            break;
        }
    }

1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
    /* Node driver (optional) */
    for (i = 0; i < virDeviceMonitorTabCount; i++) {
        res = virDeviceMonitorTab[i]->open (ret, auth, flags);
        DEBUG("node driver %d %s returned %s",
              i, virDeviceMonitorTab[i]->name,
              res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
              (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
               (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
        if (res == VIR_DRV_OPEN_ERROR) {
            if (STREQ(virDeviceMonitorTab[i]->name, "remote")) {
                virLibConnWarning (NULL, VIR_WAR_NO_NODE,
                                   "Is the libvirtd daemon running ?");
            } else {
                char *msg;
1147 1148
                if (virAsprintf(&msg, "Is the %s daemon running?",
                                virDeviceMonitorTab[i]->name) > 0) {
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
                    virLibConnWarning (NULL, VIR_WAR_NO_NODE, msg);
                    VIR_FREE(msg);
                }
            }
            break;
        } else if (res == VIR_DRV_OPEN_SUCCESS) {
            ret->deviceMonitor = virDeviceMonitorTab[i];
            break;
        }
    }

1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
    /* Secret manipulation driver. Optional */
    for (i = 0; i < virSecretDriverTabCount; i++) {
        res = virSecretDriverTab[i]->open (ret, auth, flags);
        DEBUG("secret driver %d %s returned %s",
              i, virSecretDriverTab[i]->name,
              res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
              (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
               (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
        if (res == VIR_DRV_OPEN_ERROR) {
            if (STREQ(virSecretDriverTab[i]->name, "remote")) {
                virLibConnWarning (NULL, VIR_WAR_NO_SECRET,
                                   "Is the daemon running ?");
            }
            break;
         } else if (res == VIR_DRV_OPEN_SUCCESS) {
            ret->secretDriver = virSecretDriverTab[i];
            break;
        }
    }

1180
    return ret;
1181 1182

failed:
1183 1184
    /* Ensure a global error is set in case driver forgot */
    virSetGlobalError();
1185 1186 1187

    virUnrefConnect(ret);

1188
    return NULL;
1189 1190
}

1191 1192
/**
 * virConnectOpen:
1193
 * @name: URI of the hypervisor
1194
 *
1195
 * This function should be called first to get a connection to the
1196 1197 1198
 * Hypervisor and xen store
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
1199
 *
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
 * If @name is NULL then probing will be done to determine a suitable
 * default driver to activate. This involves trying each hypervisor
 * in turn until one successfully opens. If the LIBVIRT_DEFAULT_URI
 * environment variable is set, then it will be used in preference
 * to probing for a driver.
 *
 * If connecting to an unprivileged hypervisor driver which requires
 * the libvirtd daemon to be active, it will automatically be launched
 * if not already running. This can be prevented by setting the
 * environment variable LIBVIRT_AUTOSTART=0
 *
1211
 * URIs are documented at http://libvirt.org/uri.html
1212 1213 1214 1215
 */
virConnectPtr
virConnectOpen (const char *name)
{
1216 1217 1218 1219
    if (!initialized)
        if (virInitialize() < 0)
            return NULL;

1220
    DEBUG("name=%s", name);
1221
    return do_open (name, NULL, 0);
1222 1223
}

1224
/**
1225
 * virConnectOpenReadOnly:
1226
 * @name: URI of the hypervisor
1227
 *
1228
 * This function should be called first to get a restricted connection to the
D
Daniel Veillard 已提交
1229
 * library functionalities. The set of APIs usable are then restricted
1230
 * on the available methods to control the domains.
1231
 *
1232 1233 1234
 * See virConnectOpen for notes about environment variables which can
 * have an effect on opening drivers
 *
1235
 * Returns a pointer to the hypervisor connection or NULL in case of error
1236 1237
 *
 * URIs are documented at http://libvirt.org/uri.html
1238
 */
1239
virConnectPtr
1240 1241
virConnectOpenReadOnly(const char *name)
{
1242 1243 1244 1245
    if (!initialized)
        if (virInitialize() < 0)
            return NULL;

1246
    DEBUG("name=%s", name);
1247 1248 1249 1250 1251 1252 1253 1254 1255
    return do_open (name, NULL, VIR_CONNECT_RO);
}

/**
 * virConnectOpenAuth:
 * @name: URI of the hypervisor
 * @auth: Authenticate callback parameters
 * @flags: Open flags
 *
1256
 * This function should be called first to get a connection to the
1257
 * Hypervisor. If necessary, authentication will be performed fetching
1258 1259
 * credentials via the callback
 *
1260 1261 1262
 * See virConnectOpen for notes about environment variables which can
 * have an effect on opening drivers
 *
1263 1264 1265 1266 1267 1268 1269 1270 1271
 * Returns a pointer to the hypervisor connection or NULL in case of error
 *
 * URIs are documented at http://libvirt.org/uri.html
 */
virConnectPtr
virConnectOpenAuth(const char *name,
                   virConnectAuthPtr auth,
                   int flags)
{
1272 1273 1274 1275
    if (!initialized)
        if (virInitialize() < 0)
            return NULL;

1276
    DEBUG("name=%s, auth=%p, flags=%d", NULLSTR(name), auth, flags);
1277
    return do_open (name, auth, flags);
D
Daniel Veillard 已提交
1278 1279 1280
}

/**
1281
 * virConnectClose:
D
Daniel Veillard 已提交
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
 * @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
1292 1293
virConnectClose(virConnectPtr conn)
{
1294 1295
    DEBUG("conn=%p", conn);

1296 1297 1298 1299
    virResetLastError();

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

1303
    return virUnrefConnect(conn);
D
Daniel Veillard 已提交
1304 1305
}

1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
/**
 * virConnectRef:
 * @conn: the connection to hold a reference on
 *
 * Increment the reference count on the connection. For each
 * additional call to this method, there shall be a corresponding
 * call to virConnectClose to release the reference count, once
 * the caller no longer needs the reference to this object.
 *
 * This method is typically useful for applications where multiple
 * threads are using a connection, and it is required that the
 * connection remain open until all threads have finished using
 * it. ie, each new thread using a connection would increment
 * the reference count.
D
Daniel Veillard 已提交
1320 1321
 *
 * Returns 0 in case of success, -1 in case of failure
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
 */
int
virConnectRef(virConnectPtr conn)
{
    if ((!VIR_IS_CONNECT(conn))) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }
    virMutexLock(&conn->lock);
    DEBUG("conn=%p refs=%d", conn, conn->refs);
    conn->refs++;
    virMutexUnlock(&conn->lock);
    return 0;
}

D
Daniel Veillard 已提交
1337 1338
/*
 * Not for public use.  This function is part of the internal
1339 1340 1341
 * implementation of driver features in the remote case.
 */
int
D
Daniel P. Berrange 已提交
1342
virDrvSupportsFeature (virConnectPtr conn, int feature)
1343
{
1344
    int ret;
1345 1346
    DEBUG("conn=%p, feature=%d", conn, feature);

1347 1348 1349 1350
    virResetLastError();

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

1354
    ret = VIR_DRV_SUPPORTS_FEATURE (conn->driver, conn, feature);
1355 1356 1357 1358 1359

    if (ret < 0)
        /* Copy to connection error object for back compatability */
        virSetConnError(conn);

1360
    return ret;
1361 1362
}

1363 1364 1365 1366 1367 1368 1369
/**
 * 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.
1370 1371 1372
 *
 * See also:
 * http://www.redhat.com/archives/libvir-list/2007-February/msg00096.html
1373 1374
 */
const char *
1375 1376
virConnectGetType(virConnectPtr conn)
{
1377
    const char *ret;
1378
    DEBUG("conn=%p", conn);
1379

1380 1381
    virResetLastError();

D
Daniel Veillard 已提交
1382
    if (!VIR_IS_CONNECT(conn)) {
1383
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1384
        return (NULL);
D
Daniel Veillard 已提交
1385
    }
1386 1387 1388 1389

    if (conn->driver->type) {
        ret = conn->driver->type (conn);
        if (ret) return ret;
1390
    }
1391
    return conn->driver->name;
1392 1393
}

D
Daniel Veillard 已提交
1394
/**
1395
 * virConnectGetVersion:
D
Daniel Veillard 已提交
1396
 * @conn: pointer to the hypervisor connection
1397
 * @hvVer: return value for the version of the running hypervisor (OUT)
D
Daniel Veillard 已提交
1398
 *
1399
 * Get the version level of the Hypervisor running. This may work only with
1400
 * hypervisor call, i.e. with privileged access to the hypervisor, not
1401
 * with a Read-Only connection.
D
Daniel Veillard 已提交
1402
 *
1403 1404 1405
 * 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 已提交
1406
 */
1407
int
1408 1409
virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
{
1410 1411
    DEBUG("conn=%p, hvVer=%p", conn, hvVer);

1412 1413
    virResetLastError();

D
Daniel Veillard 已提交
1414
    if (!VIR_IS_CONNECT(conn)) {
1415
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1416
        return -1;
D
Daniel Veillard 已提交
1417
    }
1418

D
Daniel Veillard 已提交
1419 1420
    if (hvVer == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1421
        goto error;
D
Daniel Veillard 已提交
1422
    }
1423

1424 1425 1426 1427 1428 1429
    if (conn->driver->version) {
        int ret = conn->driver->version (conn, hvVer);
        if (ret < 0)
            goto error;
        return ret;
    }
D
Daniel P. Berrange 已提交
1430

1431
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1432 1433 1434 1435

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
1436
    return -1;
1437 1438
}

1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
/**
 * 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)
{
1454 1455
    DEBUG("conn=%p", conn);

1456 1457
    virResetLastError();

1458
    if (!VIR_IS_CONNECT(conn)) {
1459
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1460 1461 1462
        return NULL;
    }

1463 1464 1465 1466 1467 1468
    if (conn->driver->getHostname) {
        char *ret = conn->driver->getHostname (conn);
        if (!ret)
            goto error;
        return ret;
    }
1469 1470

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1471 1472 1473 1474

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
    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)
{
1496
    char *name;
1497 1498
    DEBUG("conn=%p", conn);

1499 1500
    virResetLastError();

1501
    if (!VIR_IS_CONNECT(conn)) {
1502
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1503 1504 1505
        return NULL;
    }

1506
    name = (char *)xmlSaveUri(conn->uri);
1507
    if (!name) {
1508
        virReportOOMError (conn);
1509
        goto error;
1510 1511
    }
    return name;
1512 1513 1514 1515 1516

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
1517 1518
}

1519 1520 1521 1522 1523
/**
 * virConnectGetMaxVcpus:
 * @conn: pointer to the hypervisor connection
 * @type: value of the 'type' attribute in the <domain> element
 *
1524
 * Provides the maximum number of virtual CPUs supported for a guest VM of a
1525 1526 1527 1528 1529 1530 1531 1532 1533
 * 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)
{
1534 1535
    DEBUG("conn=%p, type=%s", conn, type);

1536 1537
    virResetLastError();

1538
    if (!VIR_IS_CONNECT(conn)) {
1539
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1540
        return -1;
1541 1542
    }

1543 1544 1545 1546 1547 1548
    if (conn->driver->getMaxVcpus) {
        int ret = conn->driver->getMaxVcpus (conn, type);
        if (ret < 0)
            goto error;
        return ret;
    }
1549

1550
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1551 1552 1553
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
1554
    return -1;
1555 1556
}

1557
/**
1558
 * virConnectListDomains:
1559 1560 1561 1562 1563 1564 1565 1566 1567
 * @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
1568 1569
virConnectListDomains(virConnectPtr conn, int *ids, int maxids)
{
1570 1571
    DEBUG("conn=%p, ids=%p, maxids=%d", conn, ids, maxids);

1572 1573
    virResetLastError();

D
Daniel Veillard 已提交
1574
    if (!VIR_IS_CONNECT(conn)) {
1575
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1576
        return -1;
D
Daniel Veillard 已提交
1577
    }
1578

1579
    if ((ids == NULL) || (maxids < 0)) {
D
Daniel Veillard 已提交
1580
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1581
        goto error;
D
Daniel Veillard 已提交
1582
    }
1583

1584 1585 1586 1587 1588 1589
    if (conn->driver->listDomains) {
        int ret = conn->driver->listDomains (conn, ids, maxids);
        if (ret < 0)
            goto error;
        return ret;
    }
1590

1591
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1592 1593 1594
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
1595
    return -1;
D
Daniel Veillard 已提交
1596 1597
}

K
 
Karel Zak 已提交
1598 1599 1600 1601
/**
 * virConnectNumOfDomains:
 * @conn: pointer to the hypervisor connection
 *
1602 1603
 * Provides the number of active domains.
 *
K
 
Karel Zak 已提交
1604 1605 1606
 * Returns the number of domain found or -1 in case of error
 */
int
1607 1608
virConnectNumOfDomains(virConnectPtr conn)
{
1609 1610
    DEBUG("conn=%p", conn);

1611 1612
    virResetLastError();

D
Daniel Veillard 已提交
1613
    if (!VIR_IS_CONNECT(conn)) {
1614
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1615
        return -1;
D
Daniel Veillard 已提交
1616
    }
K
Karel Zak 已提交
1617

1618 1619 1620 1621 1622 1623
    if (conn->driver->numOfDomains) {
        int ret = conn->driver->numOfDomains (conn);
        if (ret < 0)
            goto error;
        return ret;
    }
1624

1625
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1626 1627 1628
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
1629
    return -1;
K
 
Karel Zak 已提交
1630 1631
}

1632 1633 1634 1635
/**
 * virDomainGetConnect:
 * @dom: pointer to a domain
 *
1636
 * Provides the connection pointer associated with a domain.  The
1637 1638 1639
 * reference counter on the connection is not increased by this
 * call.
 *
1640 1641 1642 1643
 * WARNING: When writing libvirt bindings in other languages, do
 * not use this function.  Instead, store the connection and
 * the domain object together.
 *
1644 1645 1646 1647 1648
 * Returns the virConnectPtr or NULL in case of failure.
 */
virConnectPtr
virDomainGetConnect (virDomainPtr dom)
{
1649 1650
    DEBUG("dom=%p", dom);

1651 1652 1653
    virResetLastError();

    if (!VIR_IS_CONNECTED_DOMAIN (dom)) {
1654
        virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1655 1656 1657 1658 1659
        return NULL;
    }
    return dom->conn;
}

D
Daniel Veillard 已提交
1660
/**
1661
 * virDomainCreateXML:
D
Daniel Veillard 已提交
1662
 * @conn: pointer to the hypervisor connection
D
Daniel Veillard 已提交
1663
 * @xmlDesc: string containing an XML description of the domain
1664
 * @flags: callers should always pass 0
D
Daniel Veillard 已提交
1665
 *
1666
 * Launch a new guest domain, based on an XML description similar
1667
 * to the one returned by virDomainGetXMLDesc()
1668
 * This function may requires privileged access to the hypervisor.
1669 1670 1671
 * The domain is not persistent, so its definition will disappear when it
 * is destroyed, or if the host is restarted (see virDomainDefineXML() to
 * define persistent domains).
1672
 *
D
Daniel Veillard 已提交
1673 1674
 * Returns a new domain object or NULL in case of failure
 */
1675
virDomainPtr
1676 1677
virDomainCreateXML(virConnectPtr conn, const char *xmlDesc,
                   unsigned int flags)
1678
{
1679 1680
    DEBUG("conn=%p, xmlDesc=%s, flags=%d", conn, xmlDesc, flags);

1681 1682
    virResetLastError();

D
Daniel Veillard 已提交
1683
    if (!VIR_IS_CONNECT(conn)) {
1684
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1685
        return (NULL);
D
Daniel Veillard 已提交
1686 1687 1688
    }
    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1689
        goto error;
D
Daniel Veillard 已提交
1690
    }
1691 1692
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
1693
        goto error;
1694
    }
D
Daniel Veillard 已提交
1695

1696 1697 1698 1699 1700 1701 1702
    if (conn->driver->domainCreateXML) {
        virDomainPtr ret;
        ret = conn->driver->domainCreateXML (conn, xmlDesc, flags);
        if (!ret)
            goto error;
        return ret;
    }
1703 1704

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1705 1706 1707
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
1708
    return NULL;
D
Daniel Veillard 已提交
1709 1710
}

1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
/**
 * virDomainCreateLinux:
 * @conn: pointer to the hypervisor connection
 * @xmlDesc: string containing an XML description of the domain
 * @flags: callers should always pass 0
 *
 * Deprecated after 0.4.6.
 * Renamed to virDomainCreateXML() providing identical functionality.
 * This existing name will left indefinitely for API compatability.
 *
 * Returns a new domain object or NULL in case of failure
 */
virDomainPtr
virDomainCreateLinux(virConnectPtr conn, const char *xmlDesc,
                     unsigned int flags)
{
    return(virDomainCreateXML(conn, xmlDesc, flags));
}
1729

1730
/**
1731
 * virDomainLookupByID:
1732 1733 1734 1735
 * @conn: pointer to the hypervisor connection
 * @id: the domain ID number
 *
 * Try to find a domain based on the hypervisor ID number
D
Daniel Veillard 已提交
1736 1737
 * Note that this won't work for inactive domains which have an ID of -1,
 * in that case a lookup based on the Name or UUId need to be done instead.
1738
 *
1739 1740
 * Returns a new domain object or NULL in case of failure.  If the
 * domain cannot be found, then VIR_ERR_NO_DOMAIN error is raised.
1741
 */
1742
virDomainPtr
1743 1744
virDomainLookupByID(virConnectPtr conn, int id)
{
1745 1746
    DEBUG("conn=%p, id=%d", conn, id);

1747 1748
    virResetLastError();

D
Daniel Veillard 已提交
1749
    if (!VIR_IS_CONNECT(conn)) {
1750
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1751
        return (NULL);
D
Daniel Veillard 已提交
1752 1753 1754
    }
    if (id < 0) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1755
        goto error;
D
Daniel Veillard 已提交
1756
    }
1757

1758 1759 1760 1761 1762 1763 1764
    if (conn->driver->domainLookupByID) {
        virDomainPtr ret;
        ret = conn->driver->domainLookupByID (conn, id);
        if (!ret)
            goto error;
        return ret;
    }
1765

1766
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1767 1768 1769 1770

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
1771
    return NULL;
1772 1773 1774 1775 1776
}

/**
 * virDomainLookupByUUID:
 * @conn: pointer to the hypervisor connection
K
Karel Zak 已提交
1777
 * @uuid: the raw UUID for the domain
1778 1779 1780
 *
 * Try to lookup a domain on the given hypervisor based on its UUID.
 *
1781 1782
 * Returns a new domain object or NULL in case of failure.  If the
 * domain cannot be found, then VIR_ERR_NO_DOMAIN error is raised.
1783 1784
 */
virDomainPtr
1785 1786
virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
1787 1788
    DEBUG("conn=%p, uuid=%s", conn, uuid);

1789 1790
    virResetLastError();

D
Daniel Veillard 已提交
1791
    if (!VIR_IS_CONNECT(conn)) {
1792
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1793
        return (NULL);
D
Daniel Veillard 已提交
1794 1795 1796
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1797
        goto error;
D
Daniel Veillard 已提交
1798
    }
1799

1800 1801 1802 1803 1804 1805 1806
    if (conn->driver->domainLookupByUUID) {
        virDomainPtr ret;
        ret = conn->driver->domainLookupByUUID (conn, uuid);
        if (!ret)
            goto error;
        return ret;
    }
1807

1808
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1809 1810 1811 1812

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
1813
    return NULL;
1814 1815
}

K
Karel Zak 已提交
1816 1817 1818 1819 1820 1821 1822
/**
 * 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.
 *
1823 1824
 * Returns a new domain object or NULL in case of failure.  If the
 * domain cannot be found, then VIR_ERR_NO_DOMAIN error is raised.
K
Karel Zak 已提交
1825 1826 1827 1828
 */
virDomainPtr
virDomainLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
{
1829
    unsigned char uuid[VIR_UUID_BUFLEN];
1830 1831
    DEBUG("conn=%p, uuidstr=%s", conn, uuidstr);

1832 1833
    virResetLastError();

K
Karel Zak 已提交
1834
    if (!VIR_IS_CONNECT(conn)) {
1835
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
K
Karel Zak 已提交
1836 1837 1838 1839
        return (NULL);
    }
    if (uuidstr == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1840
        goto error;
K
Karel Zak 已提交
1841
    }
1842 1843

    if (virUUIDParse(uuidstr, uuid) < 0) {
1844
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1845
        goto error;
K
Karel Zak 已提交
1846
    }
1847

K
Karel Zak 已提交
1848
    return virDomainLookupByUUID(conn, &uuid[0]);
1849 1850 1851 1852 1853

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
K
Karel Zak 已提交
1854 1855
}

1856 1857 1858 1859 1860
/**
 * virDomainLookupByName:
 * @conn: pointer to the hypervisor connection
 * @name: name for the domain
 *
1861
 * Try to lookup a domain on the given hypervisor based on its name.
1862
 *
1863 1864
 * Returns a new domain object or NULL in case of failure.  If the
 * domain cannot be found, then VIR_ERR_NO_DOMAIN error is raised.
1865 1866
 */
virDomainPtr
1867 1868
virDomainLookupByName(virConnectPtr conn, const char *name)
{
1869 1870
    DEBUG("conn=%p, name=%s", conn, name);

1871 1872
    virResetLastError();

D
Daniel Veillard 已提交
1873
    if (!VIR_IS_CONNECT(conn)) {
1874
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1875
        return (NULL);
D
Daniel Veillard 已提交
1876 1877 1878
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1879
        goto error;
D
Daniel Veillard 已提交
1880
    }
1881

1882 1883 1884 1885 1886 1887 1888
    if (conn->driver->domainLookupByName) {
        virDomainPtr dom;
        dom = conn->driver->domainLookupByName (conn, name);
        if (!dom)
            goto error;
        return dom;
    }
1889 1890

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1891 1892 1893 1894

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
1895
    return NULL;
1896 1897
}

D
Daniel Veillard 已提交
1898
/**
1899
 * virDomainDestroy:
D
Daniel Veillard 已提交
1900 1901 1902
 * @domain: a domain object
 *
 * Destroy the domain object. The running instance is shutdown if not down
1903 1904 1905
 * already and all resources used by it are given back to the hypervisor. This
 * does not free the associated virDomainPtr object.
 * This function may require privileged access
D
Daniel Veillard 已提交
1906 1907
 *
 * Returns 0 in case of success and -1 in case of failure.
1908
 */
D
Daniel Veillard 已提交
1909
int
1910 1911
virDomainDestroy(virDomainPtr domain)
{
1912
    virConnectPtr conn;
1913

1914 1915
    DEBUG("domain=%p", domain);

1916 1917
    virResetLastError();

D
Daniel Veillard 已提交
1918
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
1919
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1920
        return (-1);
D
Daniel Veillard 已提交
1921
    }
1922

1923
    conn = domain->conn;
1924 1925
    if (conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
1926
        goto error;
1927
    }
1928

1929 1930 1931 1932 1933 1934 1935
    if (conn->driver->domainDestroy) {
        int ret;
        ret = conn->driver->domainDestroy (domain);
        if (ret < 0)
            goto error;
        return ret;
    }
1936

1937
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1938 1939 1940 1941

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
1942
    return -1;
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
}

/**
 * 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
1955 1956
virDomainFree(virDomainPtr domain)
{
1957 1958
    DEBUG("domain=%p", domain);

1959 1960 1961
    virResetLastError();

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
1962
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1963
        return (-1);
D
Daniel Veillard 已提交
1964
    }
1965
    if (virUnrefDomain(domain) < 0)
1966
        return -1;
1967
    return(0);
D
Daniel Veillard 已提交
1968 1969
}

1970 1971
/**
 * virDomainRef:
D
Daniel Veillard 已提交
1972
 * @domain: the domain to hold a reference on
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
 *
 * Increment the reference count on the domain. For each
 * additional call to this method, there shall be a corresponding
 * call to virDomainFree to release the reference count, once
 * the caller no longer needs the reference to this object.
 *
 * This method is typically useful for applications where multiple
 * threads are using a connection, and it is required that the
 * connection remain open until all threads have finished using
 * it. ie, each new thread using a domain would increment
 * the reference count.
D
Daniel Veillard 已提交
1984 1985
 *
 * Returns 0 in case of success and -1 in case of failure.
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
 */
int
virDomainRef(virDomainPtr domain)
{
    if ((!VIR_IS_CONNECTED_DOMAIN(domain))) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }
    virMutexLock(&domain->conn->lock);
    DEBUG("domain=%p refs=%d", domain, domain->refs);
    domain->refs++;
    virMutexUnlock(&domain->conn->lock);
    return 0;
}


D
Daniel Veillard 已提交
2002
/**
2003
 * virDomainSuspend:
D
Daniel Veillard 已提交
2004 2005 2006
 * @domain: a domain object
 *
 * Suspends an active domain, the process is frozen without further access
2007
 * to CPU resources and I/O but the memory used by the domain at the
2008
 * hypervisor level will stay allocated. Use virDomainResume() to reactivate
D
Daniel Veillard 已提交
2009
 * the domain.
2010
 * This function may requires privileged access.
D
Daniel Veillard 已提交
2011 2012 2013 2014
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
2015 2016
virDomainSuspend(virDomainPtr domain)
{
2017
    virConnectPtr conn;
2018
    DEBUG("domain=%p", domain);
2019

2020 2021
    virResetLastError();

D
Daniel Veillard 已提交
2022
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2023
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2024
        return (-1);
D
Daniel Veillard 已提交
2025
    }
2026 2027
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2028
        goto error;
2029
    }
D
Daniel Veillard 已提交
2030

2031 2032
    conn = domain->conn;

2033 2034 2035 2036 2037 2038 2039
    if (conn->driver->domainSuspend) {
        int ret;
        ret = conn->driver->domainSuspend (domain);
        if (ret < 0)
            goto error;
        return ret;
    }
2040

2041
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2042 2043 2044 2045

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
2046
    return -1;
D
Daniel Veillard 已提交
2047 2048 2049
}

/**
2050
 * virDomainResume:
D
Daniel Veillard 已提交
2051 2052 2053
 * @domain: a domain object
 *
 * Resume an suspended domain, the process is restarted from the state where
2054
 * it was frozen by calling virSuspendDomain().
2055
 * This function may requires privileged access
D
Daniel Veillard 已提交
2056 2057 2058 2059
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
2060 2061
virDomainResume(virDomainPtr domain)
{
2062
    virConnectPtr conn;
2063
    DEBUG("domain=%p", domain);
2064

2065 2066
    virResetLastError();

D
Daniel Veillard 已提交
2067
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2068
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2069
        return (-1);
D
Daniel Veillard 已提交
2070
    }
2071 2072
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2073
        goto error;
2074
    }
D
Daniel Veillard 已提交
2075

2076 2077
    conn = domain->conn;

2078 2079 2080 2081 2082 2083 2084
    if (conn->driver->domainResume) {
        int ret;
        ret = conn->driver->domainResume (domain);
        if (ret < 0)
            goto error;
        return ret;
    }
2085

2086
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2087 2088 2089 2090

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
2091
    return -1;
D
Daniel Veillard 已提交
2092 2093
}

2094 2095 2096 2097 2098 2099
/**
 * virDomainSave:
 * @domain: a domain object
 * @to: path for the output file
 *
 * This method will suspend a domain and save its memory contents to
2100 2101 2102
 * 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.
2103 2104 2105 2106
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
2107 2108
virDomainSave(virDomainPtr domain, const char *to)
{
2109
    char filepath[4096];
2110
    virConnectPtr conn;
2111
    DEBUG("domain=%p, to=%s", domain, to);
2112

2113 2114
    virResetLastError();

D
Daniel Veillard 已提交
2115
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2116
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2117
        return (-1);
D
Daniel Veillard 已提交
2118
    }
2119 2120
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2121
        goto error;
2122
    }
2123
    conn = domain->conn;
D
Daniel Veillard 已提交
2124 2125
    if (to == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
2126
        goto error;
D
Daniel Veillard 已提交
2127
    }
2128

2129 2130 2131 2132 2133
    /*
     * 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] != '/') {
2134
        unsigned int len, t;
2135

2136 2137 2138 2139 2140 2141 2142 2143 2144 2145
        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];
2146 2147 2148

    }

2149 2150 2151 2152 2153 2154 2155
    if (conn->driver->domainSave) {
        int ret;
        ret = conn->driver->domainSave (domain, to);
        if (ret < 0)
            goto error;
        return ret;
    }
2156

2157
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2158 2159 2160 2161

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
2162
    return -1;
2163 2164 2165 2166
}

/**
 * virDomainRestore:
2167
 * @conn: pointer to the hypervisor connection
2168
 * @from: path to the
2169 2170 2171 2172 2173 2174
 *
 * This method will restore a domain saved to disk by virDomainSave().
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
2175 2176
virDomainRestore(virConnectPtr conn, const char *from)
{
2177
    char filepath[4096];
2178
    DEBUG("conn=%p, from=%s", conn, from);
2179

2180 2181
    virResetLastError();

D
Daniel Veillard 已提交
2182
    if (!VIR_IS_CONNECT(conn)) {
2183
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2184
        return (-1);
D
Daniel Veillard 已提交
2185
    }
2186 2187
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2188
        goto error;
2189
    }
D
Daniel Veillard 已提交
2190 2191
    if (from == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2192
        goto error;
D
Daniel Veillard 已提交
2193 2194
    }

2195 2196 2197 2198 2199
    /*
     * 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] != '/') {
2200 2201 2202
        unsigned int len, t;

        t = strlen(from);
2203 2204 2205 2206 2207
        if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL) {
            virLibConnError(conn, VIR_ERR_SYSTEM_ERROR,
                            _("cannot get working directory"));
            goto error;
        }
2208 2209
        len = strlen(filepath);
        /* that should be covered by getcwd() semantic, but be 100% sure */
2210 2211 2212 2213 2214
        if (len > sizeof(filepath) - (t + 3)) {
            virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
                            _("path too long"));
            goto error;
        }
2215 2216 2217 2218 2219
        filepath[len] = '/';
        strcpy(&filepath[len + 1], from);
        from = &filepath[0];
    }

2220 2221 2222 2223 2224 2225 2226
    if (conn->driver->domainRestore) {
        int ret;
        ret = conn->driver->domainRestore (conn, from);
        if (ret < 0)
            goto error;
        return ret;
    }
2227

2228
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2229 2230 2231 2232

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
2233
    return -1;
D
Daniel Veillard 已提交
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
}

/**
 * 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;
2253
    DEBUG("domain=%p, to=%s, flags=%d", domain, to, flags);
D
Daniel Veillard 已提交
2254

2255 2256
    virResetLastError();

D
Daniel Veillard 已提交
2257
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2258
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
D
Daniel Veillard 已提交
2259 2260 2261 2262
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2263
        goto error;
D
Daniel Veillard 已提交
2264 2265 2266 2267
    }
    conn = domain->conn;
    if (to == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
2268
        goto error;
D
Daniel Veillard 已提交
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
    }

    /*
     * 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);
2279 2280 2281 2282 2283
        if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL) {
            virLibDomainError(domain, VIR_ERR_SYSTEM_ERROR,
                              _("cannot get current directory"));
            goto error;
        }
D
Daniel Veillard 已提交
2284 2285
        len = strlen(filepath);
        /* that should be covered by getcwd() semantic, but be 100% sure */
2286 2287 2288 2289 2290
        if (len > sizeof(filepath) - (t + 3)) {
            virLibDomainError(domain, VIR_ERR_INTERNAL_ERROR,
                              _("path too long"));
            goto error;
        }
D
Daniel Veillard 已提交
2291 2292 2293 2294 2295 2296
        filepath[len] = '/';
        strcpy(&filepath[len + 1], to);
        to = &filepath[0];

    }

2297 2298 2299 2300 2301 2302 2303
    if (conn->driver->domainCoreDump) {
        int ret;
        ret = conn->driver->domainCoreDump (domain, to, flags);
        if (ret < 0)
            goto error;
        return ret;
    }
2304

2305
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2306 2307 2308 2309

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
2310
    return -1;
2311 2312
}

2313 2314 2315 2316 2317
/**
 * virDomainShutdown:
 * @domain: a domain object
 *
 * Shutdown a domain, the domain object is still usable there after but
2318 2319
 * the domain OS is being stopped. Note that the guest OS may ignore the
 * request.
2320 2321 2322 2323 2324 2325 2326
 *
 * 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
2327 2328
virDomainShutdown(virDomainPtr domain)
{
2329
    virConnectPtr conn;
2330
    DEBUG("domain=%p", domain);
2331

2332 2333
    virResetLastError();

D
Daniel Veillard 已提交
2334
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2335
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2336
        return (-1);
D
Daniel Veillard 已提交
2337
    }
2338 2339
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2340
        goto error;
2341
    }
2342

2343 2344
    conn = domain->conn;

2345 2346 2347 2348 2349 2350 2351
    if (conn->driver->domainShutdown) {
        int ret;
        ret = conn->driver->domainShutdown (domain);
        if (ret < 0)
            goto error;
        return ret;
    }
2352

2353
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2354 2355 2356 2357

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
2358
    return -1;
2359 2360
}

2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
/**
 * 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)
{
2375
    virConnectPtr conn;
2376
    DEBUG("domain=%p, flags=%u", domain, flags);
2377

2378 2379
    virResetLastError();

2380
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2381
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2382 2383
        return (-1);
    }
2384 2385
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2386
        goto error;
2387
    }
2388

2389 2390
    conn = domain->conn;

2391 2392 2393 2394 2395 2396 2397
    if (conn->driver->domainReboot) {
        int ret;
        ret = conn->driver->domainReboot (domain, flags);
        if (ret < 0)
            goto error;
        return ret;
    }
2398

2399
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2400 2401 2402 2403

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
2404
    return -1;
2405 2406
}

2407
/**
2408
 * virDomainGetName:
2409 2410 2411 2412 2413 2414 2415 2416
 * @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 *
2417 2418
virDomainGetName(virDomainPtr domain)
{
2419 2420
    DEBUG("domain=%p", domain);

2421 2422
    virResetLastError();

D
Daniel Veillard 已提交
2423
    if (!VIR_IS_DOMAIN(domain)) {
2424
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2425
        return (NULL);
D
Daniel Veillard 已提交
2426
    }
2427
    return (domain->name);
2428 2429
}

2430 2431 2432
/**
 * virDomainGetUUID:
 * @domain: a domain object
2433
 * @uuid: pointer to a VIR_UUID_BUFLEN bytes array
2434 2435 2436 2437 2438 2439
 *
 * Get the UUID for a domain
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
2440 2441
virDomainGetUUID(virDomainPtr domain, unsigned char *uuid)
{
2442 2443
    DEBUG("domain=%p, uuid=%p", domain, uuid);

2444 2445
    virResetLastError();

D
Daniel Veillard 已提交
2446
    if (!VIR_IS_DOMAIN(domain)) {
2447
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2448
        return (-1);
D
Daniel Veillard 已提交
2449 2450 2451
    }
    if (uuid == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
2452 2453
        /* Copy to connection error object for back compatability */
        virSetConnError(domain->conn);
2454
        return (-1);
D
Daniel Veillard 已提交
2455 2456
    }

2457 2458
    memcpy(uuid, &domain->uuid[0], VIR_UUID_BUFLEN);

2459
    return (0);
2460 2461
}

K
Karel Zak 已提交
2462 2463 2464
/**
 * virDomainGetUUIDString:
 * @domain: a domain object
2465
 * @buf: pointer to a VIR_UUID_STRING_BUFLEN bytes array
K
Karel Zak 已提交
2466
 *
2467
 * Get the UUID for a domain as string. For more information about
K
Karel Zak 已提交
2468 2469 2470 2471 2472 2473 2474
 * UUID see RFC4122.
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virDomainGetUUIDString(virDomainPtr domain, char *buf)
{
2475
    unsigned char uuid[VIR_UUID_BUFLEN];
2476
    DEBUG("domain=%p, buf=%p", domain, buf);
2477

2478 2479
    virResetLastError();

K
Karel Zak 已提交
2480
    if (!VIR_IS_DOMAIN(domain)) {
2481
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
K
Karel Zak 已提交
2482 2483 2484 2485
        return (-1);
    }
    if (buf == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
2486
        goto error;
K
Karel Zak 已提交
2487
    }
2488

K
Karel Zak 已提交
2489
    if (virDomainGetUUID(domain, &uuid[0]))
2490
        goto error;
K
Karel Zak 已提交
2491

2492
    virUUIDFormat(uuid, buf);
K
Karel Zak 已提交
2493
    return (0);
2494 2495 2496 2497 2498

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
    return -1;
K
Karel Zak 已提交
2499 2500
}

2501
/**
2502
 * virDomainGetID:
2503 2504 2505 2506 2507 2508 2509
 * @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
2510 2511
virDomainGetID(virDomainPtr domain)
{
2512 2513
    DEBUG("domain=%p", domain);

2514 2515
    virResetLastError();

D
Daniel Veillard 已提交
2516
    if (!VIR_IS_DOMAIN(domain)) {
2517
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2518
        return ((unsigned int) -1);
D
Daniel Veillard 已提交
2519
    }
2520
    return (domain->id);
2521 2522
}

2523 2524 2525 2526 2527 2528
/**
 * virDomainGetOSType:
 * @domain: a domain object
 *
 * Get the type of domain operation system.
 *
2529 2530
 * Returns the new string or NULL in case of error, the string must be
 *         freed by the caller.
2531 2532
 */
char *
2533 2534
virDomainGetOSType(virDomainPtr domain)
{
2535
    virConnectPtr conn;
2536
    DEBUG("domain=%p", domain);
2537

2538 2539 2540
    virResetLastError();

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2541
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2542
        return (NULL);
D
Daniel Veillard 已提交
2543
    }
2544

2545 2546
    conn = domain->conn;

2547 2548 2549 2550 2551 2552 2553
    if (conn->driver->domainGetOSType) {
        char *ret;
        ret = conn->driver->domainGetOSType (domain);
        if (!ret)
            goto error;
        return ret;
    }
2554

2555
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2556 2557 2558 2559

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
2560
    return NULL;
2561 2562
}

2563
/**
2564
 * virDomainGetMaxMemory:
2565
 * @domain: a domain object or NULL
2566
 *
2567 2568 2569 2570 2571 2572 2573
 * 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
2574 2575
virDomainGetMaxMemory(virDomainPtr domain)
{
2576
    virConnectPtr conn;
2577
    DEBUG("domain=%p", domain);
2578

2579 2580
    virResetLastError();

D
Daniel Veillard 已提交
2581
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2582
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2583
        return (0);
D
Daniel Veillard 已提交
2584
    }
2585

2586 2587
    conn = domain->conn;

2588 2589 2590 2591 2592 2593 2594
    if (conn->driver->domainGetMaxMemory) {
        unsigned long ret;
        ret = conn->driver->domainGetMaxMemory (domain);
        if (ret == 0)
            goto error;
        return ret;
    }
2595

2596
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2597 2598 2599 2600

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
2601
    return 0;
2602 2603
}

D
Daniel Veillard 已提交
2604
/**
2605
 * virDomainSetMaxMemory:
D
Daniel Veillard 已提交
2606 2607
 * @domain: a domain object or NULL
 * @memory: the memory size in kilobytes
2608
 *
D
Daniel Veillard 已提交
2609 2610 2611
 * 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.
2612
 * This function requires privileged access to the hypervisor.
D
Daniel Veillard 已提交
2613 2614 2615 2616
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
2617 2618
virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
{
2619
    virConnectPtr conn;
2620
    DEBUG("domain=%p, memory=%lu", domain, memory);
2621

2622 2623
    virResetLastError();

2624
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2625
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2626
        return (-1);
2627
    }
2628 2629
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2630
        goto error;
2631 2632 2633
    }
    if (memory < 4096) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
2634
        goto error;
2635
    }
2636
    conn = domain->conn;
2637

2638 2639 2640 2641 2642 2643 2644
    if (conn->driver->domainSetMaxMemory) {
        int ret;
        ret = conn->driver->domainSetMaxMemory (domain, memory);
        if (ret < 0)
            goto error;
        return ret;
    }
2645

2646
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2647 2648 2649 2650

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
2651
    return -1;
2652
}
2653

2654 2655 2656 2657
/**
 * virDomainSetMemory:
 * @domain: a domain object or NULL
 * @memory: the memory size in kilobytes
2658
 *
2659 2660 2661
 * 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.
2662
 * This function may requires privileged access to the hypervisor.
2663 2664 2665 2666 2667 2668 2669
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virDomainSetMemory(virDomainPtr domain, unsigned long memory)
{
    virConnectPtr conn;
2670
    DEBUG("domain=%p, memory=%lu", domain, memory);
2671

2672 2673
    virResetLastError();

2674
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2675
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2676
        return (-1);
2677
    }
2678 2679
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2680
        goto error;
2681 2682 2683
    }
    if (memory < 4096) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
2684
        goto error;
2685 2686 2687
    }

    conn = domain->conn;
2688

2689 2690 2691 2692 2693 2694 2695
    if (conn->driver->domainSetMemory) {
        int ret;
        ret = conn->driver->domainSetMemory (domain, memory);
        if (ret < 0)
            goto error;
        return ret;
    }
2696

2697
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2698 2699 2700 2701

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
2702
    return -1;
D
Daniel Veillard 已提交
2703 2704
}

2705 2706
/**
 * virDomainGetInfo:
2707
 * @domain: a domain object
2708
 * @info: pointer to a virDomainInfo structure allocated by the user
2709
 *
2710
 * Extract information about a domain. Note that if the connection
2711
 * used to get the domain is limited only a partial set of the information
2712 2713 2714 2715 2716
 * can be extracted.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
2717 2718
virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
{
2719
    virConnectPtr conn;
2720
    DEBUG("domain=%p, info=%p", domain, info);
2721

2722 2723
    virResetLastError();

D
Daniel Veillard 已提交
2724
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2725
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2726
        return (-1);
D
Daniel Veillard 已提交
2727 2728 2729
    }
    if (info == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
2730
        goto error;
D
Daniel Veillard 已提交
2731
    }
2732

2733
    memset(info, 0, sizeof(virDomainInfo));
2734

2735 2736
    conn = domain->conn;

2737 2738 2739 2740 2741 2742 2743
    if (conn->driver->domainGetInfo) {
        int ret;
        ret = conn->driver->domainGetInfo (domain, info);
        if (ret < 0)
            goto error;
        return ret;
    }
2744

2745
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2746 2747 2748 2749

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
2750
    return -1;
2751
}
2752

2753 2754 2755
/**
 * virDomainGetXMLDesc:
 * @domain: a domain object
2756
 * @flags: an OR'ed set of virDomainXMLFlags
2757 2758
 *
 * Provide an XML description of the domain. The description may be reused
2759
 * later to relaunch the domain with virDomainCreateXML().
2760 2761 2762 2763 2764
 *
 * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
2765 2766
virDomainGetXMLDesc(virDomainPtr domain, int flags)
{
2767
    virConnectPtr conn;
2768
    DEBUG("domain=%p, flags=%d", domain, flags);
2769

2770 2771 2772
    virResetLastError();

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2773
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2774
        return (NULL);
D
Daniel Veillard 已提交
2775
    }
2776

2777 2778
    conn = domain->conn;

2779 2780 2781 2782 2783 2784
    if ((conn->flags & VIR_CONNECT_RO) && (flags & VIR_DOMAIN_XML_SECURE)) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED,
                        _("virDomainGetXMLDesc with secure flag"));
        goto error;
    }

2785 2786
    flags &= VIR_DOMAIN_XML_FLAGS_MASK;

2787 2788 2789 2790 2791 2792 2793
    if (conn->driver->domainDumpXML) {
        char *ret;
        ret = conn->driver->domainDumpXML (domain, flags);
        if (!ret)
            goto error;
        return ret;
    }
2794

2795
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2796 2797 2798 2799

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
2800
    return NULL;
2801
}
2802

2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907
/**
 * virConnectDomainXMLFromNative:
 * @conn: a connection object
 * @nativeFormat: configuration format importing from
 * @nativeConfig: the configuration data to import
 * @flags: currently unused, pass 0
 *
 * Reads native configuration data  describing a domain, and
 * generates libvirt domain XML. The format of the native
 * data is hypervisor dependant.
 *
 * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *virConnectDomainXMLFromNative(virConnectPtr conn,
                                    const char *nativeFormat,
                                    const char *nativeConfig,
                                    unsigned int flags)
{
    DEBUG("conn=%p, format=%s config=%s flags=%u", conn, nativeFormat, nativeConfig, flags);

    virResetLastError();

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

    if (nativeFormat == NULL || nativeConfig == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

    if (conn->driver->domainXMLFromNative) {
        char *ret;
        ret = conn->driver->domainXMLFromNative (conn,
                                                 nativeFormat,
                                                 nativeConfig,
                                                 flags);
        if (!ret)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
}

/**
 * virConnectDomainXMLToNative:
 * @conn: a connection object
 * @nativeFormat: configuration format exporting to
 * @domainXml: the domain configuration to export
 * @flags: currently unused, pass 0
 *
 * Reads a domain XML configuration document, and generates
 * generates a native configuration file describing the domain.
 * The format of the native data is hypervisor dependant.
 *
 * Returns a 0 terminated UTF-8 encoded native config datafile, or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *virConnectDomainXMLToNative(virConnectPtr conn,
                                  const char *nativeFormat,
                                  const char *domainXml,
                                  unsigned int flags)
{
    DEBUG("conn=%p, format=%s xml=%s flags=%u", conn, nativeFormat, domainXml, flags);

    virResetLastError();

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

    if (nativeFormat == NULL || domainXml == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

    if (conn->driver->domainXMLToNative) {
        char *ret;
        ret = conn->driver->domainXMLToNative(conn,
                                              nativeFormat,
                                              domainXml,
                                              flags);
        if (!ret)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
}


2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045
static virDomainPtr
virDomainMigrateVersion1 (virDomainPtr domain,
                          virConnectPtr dconn,
                          unsigned long flags,
                          const char *dname,
                          const char *uri,
                          unsigned long bandwidth)
{
    virDomainPtr ddomain = NULL;
    char *uri_out = NULL;
    char *cookie = NULL;
    int cookielen = 0;

    /* Prepare the migration.
     *
     * The destination host may return a cookie, or leave cookie as
     * NULL.
     *
     * The destination host MUST set uri_out if uri_in is NULL.
     *
     * If uri_in is non-NULL, then the destination host may modify
     * the URI by setting uri_out.  If it does not wish to modify
     * the URI, it should leave uri_out as NULL.
     */
    if (dconn->driver->domainMigratePrepare
        (dconn, &cookie, &cookielen, uri, &uri_out, flags, dname,
         bandwidth) == -1)
        goto done;

    if (uri == NULL && uri_out == NULL) {
        virLibConnError (domain->conn, VIR_ERR_INTERNAL_ERROR,
                         _("domainMigratePrepare did not set uri"));
        goto done;
    }
    if (uri_out)
        uri = uri_out; /* Did domainMigratePrepare change URI? */
    assert (uri != NULL);

    /* Perform the migration.  The driver isn't supposed to return
     * until the migration is complete.
     */
    if (domain->conn->driver->domainMigratePerform
        (domain, cookie, cookielen, uri, flags, dname, bandwidth) == -1)
        goto done;

    /* Get the destination domain and return it or error.
     * 'domain' no longer actually exists at this point
     * (or so we hope), but we still use the object in memory
     * in order to get the name.
     */
    dname = dname ? dname : domain->name;
    if (dconn->driver->domainMigrateFinish)
        ddomain = dconn->driver->domainMigrateFinish
            (dconn, dname, cookie, cookielen, uri, flags);
    else
        ddomain = virDomainLookupByName (dconn, dname);

 done:
    VIR_FREE (uri_out);
    VIR_FREE (cookie);
    return ddomain;
}

static virDomainPtr
virDomainMigrateVersion2 (virDomainPtr domain,
                          virConnectPtr dconn,
                          unsigned long flags,
                          const char *dname,
                          const char *uri,
                          unsigned long bandwidth)
{
    virDomainPtr ddomain = NULL;
    char *uri_out = NULL;
    char *cookie = NULL;
    char *dom_xml = NULL;
    int cookielen = 0, ret;

    /* Prepare the migration.
     *
     * The destination host may return a cookie, or leave cookie as
     * NULL.
     *
     * The destination host MUST set uri_out if uri_in is NULL.
     *
     * If uri_in is non-NULL, then the destination host may modify
     * the URI by setting uri_out.  If it does not wish to modify
     * the URI, it should leave uri_out as NULL.
     */

    /* In version 2 of the protocol, the prepare step is slightly
     * different.  We fetch the domain XML of the source domain
     * and pass it to Prepare2.
     */
    if (!domain->conn->driver->domainDumpXML) {
        virLibConnError (domain->conn, VIR_ERR_INTERNAL_ERROR, __FUNCTION__);
        return NULL;
    }
    dom_xml = domain->conn->driver->domainDumpXML (domain,
                                                   VIR_DOMAIN_XML_SECURE);
    if (!dom_xml)
        return NULL;

    ret = dconn->driver->domainMigratePrepare2
        (dconn, &cookie, &cookielen, uri, &uri_out, flags, dname,
         bandwidth, dom_xml);
    VIR_FREE (dom_xml);
    if (ret == -1)
        goto done;

    if (uri == NULL && uri_out == NULL) {
        virLibConnError (domain->conn, VIR_ERR_INTERNAL_ERROR,
                         _("domainMigratePrepare2 did not set uri"));
        goto done;
    }
    if (uri_out)
        uri = uri_out; /* Did domainMigratePrepare2 change URI? */
    assert (uri != NULL);

    /* Perform the migration.  The driver isn't supposed to return
     * until the migration is complete.
     */
    ret = domain->conn->driver->domainMigratePerform
        (domain, cookie, cookielen, uri, flags, dname, bandwidth);

    /* In version 2 of the migration protocol, we pass the
     * status code from the sender to the destination host,
     * so it can do any cleanup if the migration failed.
     */
    dname = dname ? dname : domain->name;
    ddomain = dconn->driver->domainMigrateFinish2
        (dconn, dname, cookie, cookielen, uri, flags, ret);

 done:
    VIR_FREE (uri_out);
    VIR_FREE (cookie);
    return ddomain;
}

3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079

 /*
  * This is sort of a migration v3
  *
  * In this version, the client does not talk to the destination
  * libvirtd. The source libvirtd will still try to talk to the
  * destination libvirtd though, and will do the prepare/perform/finish
  * steps.
  */
static int
virDomainMigratePeer2Peer (virDomainPtr domain,
                           unsigned long flags,
                           const char *dname,
                           const char *uri,
                           unsigned long bandwidth)
{
    if (!domain->conn->driver->domainMigratePerform) {
        virLibConnError (domain->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
        return -1;
    }

    /* Perform the migration.  The driver isn't supposed to return
     * until the migration is complete.
     */
    return domain->conn->driver->domainMigratePerform(domain,
                                                      NULL, /* cookie */
                                                      0,    /* cookielen */
                                                      uri,
                                                      flags,
                                                      dname,
                                                      bandwidth);
}


C
Chris Lalancette 已提交
3080
/*
3081
 * This is a variation on v1 & 2  migration
C
Chris Lalancette 已提交
3082
 *
3083 3084 3085 3086 3087 3088
 * This is for hypervisors which can directly handshake
 * without any libvirtd involvement on destination either
 * from client, or source libvirt.
 *
 * eg, XenD can talk direct to XenD, so libvirtd on dest
 * does not need to be involved at all, or even running
C
Chris Lalancette 已提交
3089
 */
3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101
static int
virDomainMigrateDirect (virDomainPtr domain,
                        unsigned long flags,
                        const char *dname,
                        const char *uri,
                        unsigned long bandwidth)
{
    if (!domain->conn->driver->domainMigratePerform) {
        virLibConnError (domain->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
        return -1;
    }

C
Chris Lalancette 已提交
3102 3103 3104
    /* Perform the migration.  The driver isn't supposed to return
     * until the migration is complete.
     */
3105 3106 3107 3108 3109 3110 3111
    return domain->conn->driver->domainMigratePerform(domain,
                                                      NULL, /* cookie */
                                                      0,    /* cookielen */
                                                      uri,
                                                      flags,
                                                      dname,
                                                      bandwidth);
C
Chris Lalancette 已提交
3112 3113
}

3114

3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127
/**
 * virDomainMigrate:
 * @domain: a domain object
 * @dconn: destination host (a connection object)
 * @flags: flags
 * @dname: (optional) rename domain to this at destination
 * @uri: (optional) dest hostname/URI as seen from the source host
 * @bandwidth: (optional) specify migration bandwidth limit in Mbps
 *
 * Migrate the domain object from its current host to the destination
 * host given by dconn (a connection to the destination host).
 *
 * Flags may be one of more of the following:
3128 3129 3130
 *   VIR_MIGRATE_LIVE      Do not pause the VM during migration
 *   VIR_MIGRATE_PEER2PEER Direct connection between source & destination hosts
 *   VIR_MIGRATE_TUNNELLED Tunnel migration data over the libvirt RPC channel
C
Chris Lalancette 已提交
3131 3132 3133 3134
 *   VIR_MIGRATE_PERSIST_DEST If the migration is successful, persist the domain
 *                            on the destination host.
 *   VIR_MIGRATE_UNDEFINE_SOURCE If the migration is successful, undefine the
 *                               domain on the source host.
3135 3136 3137 3138 3139
 *
 * VIR_MIGRATE_TUNNELLED requires that VIR_MIGRATE_PEER2PEER be set.
 * Applications using the VIR_MIGRATE_PEER2PEER flag will probably
 * prefer to invoke virDomainMigrateToURI, avoiding the need to
 * open connection to the destination host themselves.
3140 3141 3142 3143 3144 3145
 *
 * If a hypervisor supports renaming domains during migration,
 * then you may set the dname parameter to the new name (otherwise
 * it keeps the same name).  If this is not supported by the
 * hypervisor, dname must be NULL or else you will get an error.
 *
3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159
 * If the VIR_MIGRATE_PEER2PEER flag is set, the uri parameter
 * must be a valid libvirt connection URI, by which the source
 * libvirt driver can connect to the destination libvirt. If
 * omitted, the dconn connection object will be queried for its
 * current URI.
 *
 * If the VIR_MIGRATE_PEER2PEER flag is NOT set, the URI parameter
 * takes a hypervisor specific format. The hypervisor capabilities
 * XML includes details of the support URI schemes. If omitted
 * the dconn will be asked for a default URI.
 *
 * In either case it is typically only neccessary to specify a
 * URI if the destination host has multiple interfaces and a
 * specific interface is required to transmit migration data.
3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188
 *
 * The maximum bandwidth (in Mbps) that will be used to do migration
 * can be specified with the bandwidth parameter.  If set to 0,
 * libvirt will choose a suitable default.  Some hypervisors do
 * not support this feature and will return an error if bandwidth
 * is not 0.
 *
 * To see which features are supported by the current hypervisor,
 * see virConnectGetCapabilities, /capabilities/host/migration_features.
 *
 * There are many limitations on migration imposed by the underlying
 * technology - for example it may not be possible to migrate between
 * different processors even with the same architecture, or between
 * different types of hypervisor.
 *
 * Returns the new domain object if the migration was successful,
 *   or NULL in case of error.  Note that the new domain object
 *   exists in the scope of the destination connection (dconn).
 */
virDomainPtr
virDomainMigrate (virDomainPtr domain,
                  virConnectPtr dconn,
                  unsigned long flags,
                  const char *dname,
                  const char *uri,
                  unsigned long bandwidth)
{
    virDomainPtr ddomain = NULL;
    DEBUG("domain=%p, dconn=%p, flags=%lu, dname=%s, uri=%s, bandwidth=%lu",
3189
          domain, dconn, flags, NULLSTR(dname), NULLSTR(uri), bandwidth);
3190

3191 3192
    virResetLastError();

3193
    /* First checkout the source */
3194
    if (!VIR_IS_CONNECTED_DOMAIN (domain)) {
3195
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
3196 3197
        return NULL;
    }
3198 3199
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3200
        goto error;
3201 3202
    }

3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213
    /* Now checkout the destination */
    if (!VIR_IS_CONNECT(dconn)) {
        virLibConnError(domain->conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        goto error;
    }
    if (dconn->flags & VIR_CONNECT_RO) {
        /* NB, deliberately report error against source object, not dest */
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        goto error;
    }

3214 3215 3216 3217 3218 3219 3220 3221 3222
    if (flags & VIR_MIGRATE_PEER2PEER) {
        if (VIR_DRV_SUPPORTS_FEATURE (domain->conn->driver, domain->conn,
                                      VIR_DRV_FEATURE_MIGRATION_P2P)) {
            char *dstURI = NULL;
            if (uri == NULL) {
                dstURI = virConnectGetURI(dconn);
                if (!uri)
                    return NULL;
            }
3223

3224 3225 3226 3227 3228
            if (virDomainMigratePeer2Peer(domain, flags, dname, uri ? uri : dstURI, bandwidth) < 0) {
                VIR_FREE(dstURI);
                goto error;
            }
            VIR_FREE(dstURI);
C
Chris Lalancette 已提交
3229

3230 3231 3232 3233 3234 3235
            ddomain = virDomainLookupByName (dconn, dname ? dname : domain->name);
        } else {
            /* This driver does not support peer to peer migration */
            virLibConnError (domain->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
            goto error;
        }
3236
    } else {
3237 3238 3239 3240 3241 3242
        if (flags & VIR_MIGRATE_TUNNELLED) {
            virLibConnError(domain->conn, VIR_ERR_OPERATION_INVALID,
                            _("cannot perform tunnelled migration without using peer2peer flag"));
            goto error;
        }

C
Chris Lalancette 已提交
3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254
        /* Check that migration is supported by both drivers. */
        if (VIR_DRV_SUPPORTS_FEATURE(domain->conn->driver, domain->conn,
                                     VIR_DRV_FEATURE_MIGRATION_V1) &&
            VIR_DRV_SUPPORTS_FEATURE(dconn->driver, dconn,
                                     VIR_DRV_FEATURE_MIGRATION_V1))
            ddomain = virDomainMigrateVersion1(domain, dconn, flags, dname, uri, bandwidth);
        else if (VIR_DRV_SUPPORTS_FEATURE(domain->conn->driver, domain->conn,
                                          VIR_DRV_FEATURE_MIGRATION_V2) &&
                 VIR_DRV_SUPPORTS_FEATURE(dconn->driver, dconn,
                                          VIR_DRV_FEATURE_MIGRATION_V2))
            ddomain = virDomainMigrateVersion2(domain, dconn, flags, dname, uri, bandwidth);
        else {
3255
            /* This driver does not support any migration method */
C
Chris Lalancette 已提交
3256 3257 3258
            virLibConnError(domain->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
            goto error;
        }
3259 3260
    }

3261 3262
    if (ddomain == NULL)
        goto error;
D
Daniel Veillard 已提交
3263

3264
    return ddomain;
3265 3266 3267 3268 3269

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
    return NULL;
3270 3271
}

3272 3273 3274 3275 3276 3277 3278 3279 3280 3281

/**
 * virDomainMigrateToURI:
 * @domain: a domain object
 * @duri: mandatory URI for the destination host
 * @flags: flags
 * @dname: (optional) rename domain to this at destination
 * @bandwidth: (optional) specify migration bandwidth limit in Mbps
 *
 * Migrate the domain object from its current host to the destination
D
Dan Kenigsberg 已提交
3282
 * host given by duri.
3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300
 *
 * Flags may be one of more of the following:
 *   VIR_MIGRATE_LIVE      Do not pause the VM during migration
 *   VIR_MIGRATE_PEER2PEER Direct connection between source & destination hosts
 *   VIR_MIGRATE_TUNNELLED Tunnel migration data over the libvirt RPC channel
 *
 * VIR_MIGRATE_TUNNELLED requires that VIR_MIGRATE_PEER2PEER be set.
 * Applications using the VIR_MIGRATE_PEER2PEER flag will probably
 * prefer to invoke virDomainMigrateToURI, avoiding the need to
 * open connection to the destination host themselves.
 *
 * If a hypervisor supports renaming domains during migration,
 * then you may set the dname parameter to the new name (otherwise
 * it keeps the same name).  If this is not supported by the
 * hypervisor, dname must be NULL or else you will get an error.
 *
 * If the VIR_MIGRATE_PEER2PEER flag is set, the duri parameter
 * must be a valid libvirt connection URI, by which the source
D
Dan Kenigsberg 已提交
3301
 * libvirt driver can connect to the destination libvirt.
3302 3303 3304
 *
 * If the VIR_MIGRATE_PEER2PEER flag is NOT set, the duri parameter
 * takes a hypervisor specific format. The hypervisor capabilities
D
Dan Kenigsberg 已提交
3305
 * XML includes details of the support URI schemes. Not all hypervisors
3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384
 * will support this mode of migration, so if the VIR_MIGRATE_PEER2PEER
 * flag is not set, then it may be neccessary to use the alternative
 * virDomainMigrate API providing an explicit virConnectPtr for the
 * destination host
 *
 * The maximum bandwidth (in Mbps) that will be used to do migration
 * can be specified with the bandwidth parameter.  If set to 0,
 * libvirt will choose a suitable default.  Some hypervisors do
 * not support this feature and will return an error if bandwidth
 * is not 0.
 *
 * To see which features are supported by the current hypervisor,
 * see virConnectGetCapabilities, /capabilities/host/migration_features.
 *
 * There are many limitations on migration imposed by the underlying
 * technology - for example it may not be possible to migrate between
 * different processors even with the same architecture, or between
 * different types of hypervisor.
 *
 * Returns 0 if the migration succeeded, -1 upon error.
 */
int
virDomainMigrateToURI (virDomainPtr domain,
                       const char *duri,
                       unsigned long flags,
                       const char *dname,
                       unsigned long bandwidth)
{
    DEBUG("domain=%p, duri=%p, flags=%lu, dname=%s, bandwidth=%lu",
          domain, NULLSTR(duri), flags, NULLSTR(dname), bandwidth);

    virResetLastError();

    /* First checkout the source */
    if (!VIR_IS_CONNECTED_DOMAIN (domain)) {
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return -1;
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        goto error;
    }

    if (duri == NULL) {
        virLibConnError (domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

    if (flags & VIR_MIGRATE_PEER2PEER) {
        if (VIR_DRV_SUPPORTS_FEATURE (domain->conn->driver, domain->conn,
                                      VIR_DRV_FEATURE_MIGRATION_P2P)) {
            if (virDomainMigratePeer2Peer (domain, flags, dname, duri, bandwidth) < 0)
                goto error;
        } else {
            /* No peer to peer migration supported */
            virLibConnError (domain->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
            goto error;
        }
    } else {
        if (VIR_DRV_SUPPORTS_FEATURE (domain->conn->driver, domain->conn,
                                      VIR_DRV_FEATURE_MIGRATION_DIRECT)) {
            if (virDomainMigrateDirect (domain, flags, dname, duri, bandwidth) < 0)
                goto error;
        } else {
            /* Cannot do a migration with only the perform step */
            virLibConnError (domain->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
            goto error;
        }
    }

    return 0;

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
    return -1;
}


D
Daniel Veillard 已提交
3385 3386
/*
 * Not for public use.  This function is part of the internal
3387 3388 3389
 * implementation of migration in the remote case.
 */
int
D
Daniel P. Berrange 已提交
3390
virDomainMigratePrepare (virConnectPtr dconn,
3391 3392 3393 3394 3395 3396 3397
                         char **cookie,
                         int *cookielen,
                         const char *uri_in,
                         char **uri_out,
                         unsigned long flags,
                         const char *dname,
                         unsigned long bandwidth)
3398
{
J
John Levon 已提交
3399 3400 3401
    VIR_DEBUG("dconn=%p, cookie=%p, cookielen=%p, uri_in=%s, uri_out=%p, "
              "flags=%lu, dname=%s, bandwidth=%lu", dconn, cookie, cookielen,
              NULLSTR(uri_in), uri_out, flags, NULLSTR(dname), bandwidth);
3402

3403 3404
    virResetLastError();

3405
    if (!VIR_IS_CONNECT (dconn)) {
3406
        virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
3407 3408 3409
        return -1;
    }

3410 3411
    if (dconn->flags & VIR_CONNECT_RO) {
        virLibConnError(dconn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3412
        goto error;
3413 3414
    }

3415 3416 3417 3418 3419 3420 3421 3422 3423
    if (dconn->driver->domainMigratePrepare) {
        int ret;
        ret = dconn->driver->domainMigratePrepare (dconn, cookie, cookielen,
                                                   uri_in, uri_out,
                                                   flags, dname, bandwidth);
        if (ret < 0)
            goto error;
        return ret;
    }
3424 3425

    virLibConnError (dconn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3426 3427 3428 3429

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dconn);
3430 3431 3432
    return -1;
}

D
Daniel Veillard 已提交
3433 3434
/*
 * Not for public use.  This function is part of the internal
3435 3436 3437
 * implementation of migration in the remote case.
 */
int
D
Daniel P. Berrange 已提交
3438
virDomainMigratePerform (virDomainPtr domain,
3439 3440 3441 3442 3443 3444 3445 3446
                           const char *cookie,
                           int cookielen,
                           const char *uri,
                           unsigned long flags,
                           const char *dname,
                           unsigned long bandwidth)
{
    virConnectPtr conn;
J
John Levon 已提交
3447 3448 3449
    VIR_DEBUG("domain=%p, cookie=%p, cookielen=%d, uri=%s, flags=%lu, "
              "dname=%s, bandwidth=%lu", domain, cookie, cookielen, uri, flags,
              NULLSTR(dname), bandwidth);
3450

3451 3452 3453
    virResetLastError();

    if (!VIR_IS_CONNECTED_DOMAIN (domain)) {
3454
        virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
3455 3456 3457 3458
        return -1;
    }
    conn = domain->conn;

3459 3460
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3461
        goto error;
3462 3463
    }

3464 3465 3466 3467 3468 3469 3470 3471 3472
    if (conn->driver->domainMigratePerform) {
        int ret;
        ret = conn->driver->domainMigratePerform (domain, cookie, cookielen,
                                                  uri,
                                                  flags, dname, bandwidth);
        if (ret < 0)
            goto error;
        return ret;
    }
3473 3474

    virLibDomainError (domain, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3475 3476 3477 3478

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
3479 3480 3481
    return -1;
}

D
Daniel Veillard 已提交
3482 3483
/*
 * Not for public use.  This function is part of the internal
3484 3485 3486
 * implementation of migration in the remote case.
 */
virDomainPtr
D
Daniel P. Berrange 已提交
3487
virDomainMigrateFinish (virConnectPtr dconn,
3488 3489 3490 3491 3492 3493
                          const char *dname,
                          const char *cookie,
                          int cookielen,
                          const char *uri,
                          unsigned long flags)
{
J
John Levon 已提交
3494 3495 3496
    VIR_DEBUG("dconn=%p, dname=%s, cookie=%p, cookielen=%d, uri=%s, "
              "flags=%lu", dconn, NULLSTR(dname), cookie, cookielen,
              uri, flags);
3497

3498 3499
    virResetLastError();

3500
    if (!VIR_IS_CONNECT (dconn)) {
3501
        virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
3502 3503 3504
        return NULL;
    }

3505 3506
    if (dconn->flags & VIR_CONNECT_RO) {
        virLibConnError(dconn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3507
        goto error;
3508 3509
    }

3510 3511 3512 3513 3514 3515 3516 3517 3518
    if (dconn->driver->domainMigrateFinish) {
        virDomainPtr ret;
        ret = dconn->driver->domainMigrateFinish (dconn, dname,
                                                  cookie, cookielen,
                                                  uri, flags);
        if (!ret)
            goto error;
        return ret;
    }
3519 3520

    virLibConnError (dconn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3521 3522 3523 3524

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dconn);
3525
    return NULL;
D
Daniel Veillard 已提交
3526 3527 3528
}


D
Daniel Veillard 已提交
3529 3530
/*
 * Not for public use.  This function is part of the internal
D
Daniel Veillard 已提交
3531 3532 3533
 * implementation of migration in the remote case.
 */
int
D
Daniel P. Berrange 已提交
3534 3535 3536 3537 3538 3539 3540 3541 3542
virDomainMigratePrepare2 (virConnectPtr dconn,
                          char **cookie,
                          int *cookielen,
                          const char *uri_in,
                          char **uri_out,
                          unsigned long flags,
                          const char *dname,
                          unsigned long bandwidth,
                          const char *dom_xml)
D
Daniel Veillard 已提交
3543
{
J
John Levon 已提交
3544 3545 3546 3547
    VIR_DEBUG("dconn=%p, cookie=%p, cookielen=%p, uri_in=%s, uri_out=%p,"
              "flags=%lu, dname=%s, bandwidth=%lu, dom_xml=%s", dconn,
              cookie, cookielen, uri_in, uri_out, flags, NULLSTR(dname),
              bandwidth, dom_xml);
D
Daniel Veillard 已提交
3548

3549 3550
    virResetLastError();

D
Daniel Veillard 已提交
3551 3552 3553 3554 3555
    if (!VIR_IS_CONNECT (dconn)) {
        virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return -1;
    }

3556 3557
    if (dconn->flags & VIR_CONNECT_RO) {
        virLibConnError(dconn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3558
        goto error;
3559 3560
    }

3561 3562 3563 3564 3565 3566 3567 3568 3569 3570
    if (dconn->driver->domainMigratePrepare2) {
        int ret;
        ret = dconn->driver->domainMigratePrepare2 (dconn, cookie, cookielen,
                                                    uri_in, uri_out,
                                                    flags, dname, bandwidth,
                                                    dom_xml);
        if (ret < 0)
            goto error;
        return ret;
    }
D
Daniel Veillard 已提交
3571 3572

    virLibConnError (dconn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3573 3574 3575 3576

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dconn);
D
Daniel Veillard 已提交
3577 3578 3579
    return -1;
}

D
Daniel Veillard 已提交
3580 3581
/*
 * Not for public use.  This function is part of the internal
D
Daniel Veillard 已提交
3582 3583 3584
 * implementation of migration in the remote case.
 */
virDomainPtr
D
Daniel P. Berrange 已提交
3585 3586 3587 3588 3589 3590 3591
virDomainMigrateFinish2 (virConnectPtr dconn,
                         const char *dname,
                         const char *cookie,
                         int cookielen,
                         const char *uri,
                         unsigned long flags,
                         int retcode)
D
Daniel Veillard 已提交
3592
{
J
John Levon 已提交
3593 3594 3595
    VIR_DEBUG("dconn=%p, dname=%s, cookie=%p, cookielen=%d, uri=%s, "
              "flags=%lu, retcode=%d", dconn, NULLSTR(dname), cookie,
              cookielen, uri, flags, retcode);
D
Daniel Veillard 已提交
3596

3597 3598
    virResetLastError();

D
Daniel Veillard 已提交
3599 3600 3601 3602 3603
    if (!VIR_IS_CONNECT (dconn)) {
        virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return NULL;
    }

3604 3605
    if (dconn->flags & VIR_CONNECT_RO) {
        virLibConnError(dconn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3606
        goto error;
3607 3608
    }

3609 3610 3611 3612 3613 3614 3615 3616 3617 3618
    if (dconn->driver->domainMigrateFinish2) {
        virDomainPtr ret;
        ret = dconn->driver->domainMigrateFinish2 (dconn, dname,
                                                   cookie, cookielen,
                                                   uri, flags,
                                                   retcode);
        if (!ret)
            goto error;
        return ret;
    }
D
Daniel Veillard 已提交
3619 3620

    virLibConnError (dconn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3621 3622 3623 3624

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dconn);
D
Daniel Veillard 已提交
3625
    return NULL;
3626 3627 3628
}


C
Chris Lalancette 已提交
3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641
/*
 * Not for public use.  This function is part of the internal
 * implementation of migration in the remote case.
 */
int
virDomainMigratePrepareTunnel(virConnectPtr conn,
                              virStreamPtr st,
                              unsigned long flags,
                              const char *dname,
                              unsigned long bandwidth,
                              const char *dom_xml)

{
3642 3643
    VIR_DEBUG("conn=%p, stream=%p, flags=%lu, dname=%s, "
              "bandwidth=%lu, dom_xml=%s", conn, st, flags,
C
Chris Lalancette 已提交
3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663
              NULLSTR(dname), bandwidth, dom_xml);

    virResetLastError();

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

    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        goto error;
    }

    if (conn != st->conn) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

    if (conn->driver->domainMigratePrepareTunnel) {
3664
        int rv = conn->driver->domainMigratePrepareTunnel(conn, st,
C
Chris Lalancette 已提交
3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680
                                                          flags, dname,
                                                          bandwidth, dom_xml);
        if (rv < 0)
            goto error;
        return rv;
    }

    virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return -1;
}


3681 3682 3683 3684
/**
 * virNodeGetInfo:
 * @conn: pointer to the hypervisor connection
 * @info: pointer to a virNodeInfo structure allocated by the user
3685
 *
3686 3687 3688 3689 3690
 * Extract hardware information about the node.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
3691 3692
virNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
{
3693 3694
    DEBUG("conn=%p, info=%p", conn, info);

3695 3696
    virResetLastError();

3697
    if (!VIR_IS_CONNECT(conn)) {
3698
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
3699 3700 3701 3702
        return (-1);
    }
    if (info == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3703
        goto error;
3704 3705
    }

3706 3707 3708 3709 3710 3711 3712
    if (conn->driver->nodeGetInfo) {
        int ret;
        ret = conn->driver->nodeGetInfo (conn, info);
        if (ret < 0)
            goto error;
        return ret;
    }
3713

3714
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3715 3716 3717 3718

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
3719
    return -1;
3720
}
3721

3722 3723 3724 3725 3726 3727
/**
 * virConnectGetCapabilities:
 * @conn: pointer to the hypervisor connection
 *
 * Provides capabilities of the hypervisor / driver.
 *
3728 3729
 * Returns NULL in case of error, or an XML string
 * defining the capabilities.
3730 3731 3732 3733 3734
 * The client must free the returned string after use.
 */
char *
virConnectGetCapabilities (virConnectPtr conn)
{
3735 3736
    DEBUG("conn=%p", conn);

3737 3738
    virResetLastError();

3739
    if (!VIR_IS_CONNECT (conn)) {
3740
        virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
3741 3742 3743
        return NULL;
    }

3744 3745 3746 3747 3748 3749 3750
    if (conn->driver->getCapabilities) {
        char *ret;
        ret = conn->driver->getCapabilities (conn);
        if (!ret)
            goto error;
        return ret;
    }
3751

3752
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3753 3754 3755 3756

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
3757 3758 3759
    return NULL;
}

3760 3761 3762
/**
 * virNodeGetFreeMemory:
 * @conn: pointer to the hypervisor connection
3763
 *
D
Daniel Veillard 已提交
3764
 * provides the free memory available on the Node
D
Daniel Veillard 已提交
3765 3766
 * Note: most libvirt APIs provide memory sizes in kilobytes, but in this
 * function the returned value is in bytes. Divide by 1024 as necessary.
3767
 *
D
Daniel Veillard 已提交
3768
 * Returns the available free memory in bytes or 0 in case of error
3769 3770 3771 3772
 */
unsigned long long
virNodeGetFreeMemory(virConnectPtr conn)
{
3773 3774
    DEBUG("conn=%p", conn);

3775 3776
    virResetLastError();

3777 3778 3779 3780 3781
    if (!VIR_IS_CONNECT (conn)) {
        virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return 0;
    }

3782 3783 3784 3785 3786 3787 3788
    if (conn->driver->getFreeMemory) {
        unsigned long long ret;
        ret = conn->driver->getFreeMemory (conn);
        if (ret == 0)
            goto error;
        return ret;
    }
3789 3790

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3791 3792 3793 3794

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
3795 3796 3797
    return 0;
}

3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811
/**
 * 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;
3812
    DEBUG("domain=%p, nparams=%p", domain, nparams);
3813

3814 3815
    virResetLastError();

3816
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
3817
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
3818 3819 3820 3821 3822 3823
        return NULL;
    }
    conn = domain->conn;

    if (conn->driver->domainGetSchedulerType){
        schedtype = conn->driver->domainGetSchedulerType (domain, nparams);
3824 3825
        if (!schedtype)
            goto error;
3826 3827 3828
        return schedtype;
    }

3829
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3830 3831 3832 3833

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853
    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,
3854
                                virSchedParameterPtr params, int *nparams)
3855 3856
{
    virConnectPtr conn;
3857
    DEBUG("domain=%p, params=%p, nparams=%p", domain, params, nparams);
3858

3859 3860
    virResetLastError();

3861
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
3862
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
3863 3864 3865 3866
        return -1;
    }
    conn = domain->conn;

3867 3868 3869 3870 3871 3872 3873
    if (conn->driver->domainGetSchedulerParameters) {
        int ret;
        ret = conn->driver->domainGetSchedulerParameters (domain, params, nparams);
        if (ret < 0)
            goto error;
        return ret;
    }
3874

3875
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3876 3877 3878 3879

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895
    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
3896
virDomainSetSchedulerParameters(virDomainPtr domain,
3897
                                virSchedParameterPtr params, int nparams)
3898 3899
{
    virConnectPtr conn;
3900
    DEBUG("domain=%p, params=%p, nparams=%d", domain, params, nparams);
3901

3902 3903
    virResetLastError();

3904
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
3905
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
3906 3907
        return -1;
    }
3908 3909
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3910
        goto error;
3911
    }
3912 3913
    conn = domain->conn;

3914 3915 3916 3917 3918 3919 3920
    if (conn->driver->domainSetSchedulerParameters) {
        int ret;
        ret = conn->driver->domainSetSchedulerParameters (domain, params, nparams);
        if (ret < 0)
            goto error;
        return ret;
    }
3921

3922
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3923 3924 3925 3926

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
3927 3928 3929 3930
    return -1;
}


3931
/**
3932
 * virDomainBlockStats:
3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961
 * @dom: pointer to the domain object
 * @path: path to the block device
 * @stats: block device stats (returned)
 * @size: size of stats structure
 *
 * This function returns block device (disk) stats for block
 * devices attached to the domain.
 *
 * The path parameter is the name of the block device.  Get this
 * by calling virDomainGetXMLDesc and finding the <target dev='...'>
 * attribute within //domain/devices/disk.  (For example, "xvda").
 *
 * Domains may have more than one block device.  To get stats for
 * each you should make multiple calls to this function.
 *
 * Individual fields within the stats structure may be returned
 * as -1, which indicates that the hypervisor does not support
 * that particular statistic.
 *
 * Returns: 0 in case of success or -1 in case of failure.
 */
int
virDomainBlockStats (virDomainPtr dom, const char *path,
                     virDomainBlockStatsPtr stats, size_t size)
{
    virConnectPtr conn;
    struct _virDomainBlockStats stats2 = { -1, -1, -1, -1, -1 };
    DEBUG("domain=%p, path=%s, stats=%p, size=%zi", dom, path, stats, size);

3962 3963
    virResetLastError();

3964
    if (!VIR_IS_CONNECTED_DOMAIN (dom)) {
3965
        virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
3966 3967
        return -1;
    }
3968
    if (!path || !stats || size > sizeof stats2) {
3969 3970 3971
        virLibDomainError (dom, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }
3972 3973 3974 3975
    conn = dom->conn;

    if (conn->driver->domainBlockStats) {
        if (conn->driver->domainBlockStats (dom, path, &stats2) == -1)
3976
            goto error;
3977 3978 3979 3980 3981 3982

        memcpy (stats, &stats2, size);
        return 0;
    }

    virLibDomainError (dom, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3983 3984 3985 3986

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dom->conn);
3987 3988 3989 3990
    return -1;
}

/**
3991
 * virDomainInterfaceStats:
3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019
 * @dom: pointer to the domain object
 * @path: path to the interface
 * @stats: network interface stats (returned)
 * @size: size of stats structure
 *
 * This function returns network interface stats for interfaces
 * attached to the domain.
 *
 * The path parameter is the name of the network interface.
 *
 * Domains may have more than network interface.  To get stats for
 * each you should make multiple calls to this function.
 *
 * Individual fields within the stats structure may be returned
 * as -1, which indicates that the hypervisor does not support
 * that particular statistic.
 *
 * Returns: 0 in case of success or -1 in case of failure.
 */
int
virDomainInterfaceStats (virDomainPtr dom, const char *path,
                         virDomainInterfaceStatsPtr stats, size_t size)
{
    virConnectPtr conn;
    struct _virDomainInterfaceStats stats2 = { -1, -1, -1, -1,
                                               -1, -1, -1, -1 };
    DEBUG("domain=%p, path=%s, stats=%p, size=%zi", dom, path, stats, size);

4020 4021
    virResetLastError();

4022
    if (!VIR_IS_CONNECTED_DOMAIN (dom)) {
4023
        virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4024 4025
        return -1;
    }
4026
    if (!path || !stats || size > sizeof stats2) {
4027 4028 4029
        virLibDomainError (dom, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }
4030 4031 4032 4033
    conn = dom->conn;

    if (conn->driver->domainInterfaceStats) {
        if (conn->driver->domainInterfaceStats (dom, path, &stats2) == -1)
4034
            goto error;
4035 4036 4037 4038 4039 4040

        memcpy (stats, &stats2, size);
        return 0;
    }

    virLibDomainError (dom, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4041 4042 4043 4044

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dom->conn);
4045 4046 4047
    return -1;
}

R
Richard W.M. Jones 已提交
4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080
/**
 * virDomainBlockPeek:
 * @dom: pointer to the domain object
 * @path: path to the block device
 * @offset: offset within block device
 * @size: size to read
 * @buffer: return buffer (must be at least size bytes)
 * @flags: unused, always pass 0
 *
 * This function allows you to read the contents of a domain's
 * disk device.
 *
 * Typical uses for this are to determine if the domain has
 * written a Master Boot Record (indicating that the domain
 * has completed installation), or to try to work out the state
 * of the domain's filesystems.
 *
 * (Note that in the local case you might try to open the
 * block device or file directly, but that won't work in the
 * remote case, nor if you don't have sufficient permission.
 * Hence the need for this call).
 *
 * 'path' must be a device or file corresponding to the domain.
 * In other words it must be the precise string returned in
 * a <disk><source dev='...'/></disk> from
 * virDomainGetXMLDesc.
 *
 * 'offset' and 'size' represent an area which must lie entirely
 * within the device or file.  'size' may be 0 to test if the
 * call would succeed.
 *
 * 'buffer' is the return buffer and must be at least 'size' bytes.
 *
R
Richard W.M. Jones 已提交
4081 4082 4083 4084
 * NB. The remote driver imposes a 64K byte limit on 'size'.
 * For your program to be able to work reliably over a remote
 * connection you should split large requests to <= 65536 bytes.
 *
R
Richard W.M. Jones 已提交
4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098
 * Returns: 0 in case of success or -1 in case of failure.
 */
int
virDomainBlockPeek (virDomainPtr dom,
                    const char *path,
                    unsigned long long offset /* really 64 bits */,
                    size_t size,
                    void *buffer,
                    unsigned int flags)
{
    virConnectPtr conn;
    DEBUG("domain=%p, path=%s, offset=%lld, size=%zi, buffer=%p",
          dom, path, offset, size, buffer);

4099 4100
    virResetLastError();

R
Richard W.M. Jones 已提交
4101 4102 4103 4104 4105
    if (!VIR_IS_CONNECTED_DOMAIN (dom)) {
        virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return -1;
    }
    conn = dom->conn;
4106

4107 4108
    if (dom->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(dom, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4109
        goto error;
4110 4111
    }

R
Richard W.M. Jones 已提交
4112 4113 4114
    if (!path) {
        virLibDomainError (dom, VIR_ERR_INVALID_ARG,
                           _("path is NULL"));
4115
        goto error;
R
Richard W.M. Jones 已提交
4116 4117 4118 4119 4120
    }

    if (flags != 0) {
        virLibDomainError (dom, VIR_ERR_INVALID_ARG,
                           _("flags must be zero"));
4121
        goto error;
R
Richard W.M. Jones 已提交
4122 4123 4124 4125 4126 4127
    }

    /* Allow size == 0 as an access test. */
    if (size > 0 && !buffer) {
        virLibDomainError (dom, VIR_ERR_INVALID_ARG,
                           _("buffer is NULL"));
4128
        goto error;
R
Richard W.M. Jones 已提交
4129 4130
    }

4131 4132 4133 4134 4135 4136 4137 4138
    if (conn->driver->domainBlockPeek) {
        int ret;
        ret =conn->driver->domainBlockPeek (dom, path, offset, size,
                                            buffer, flags);
        if (ret < 0)
            goto error;
        return ret;
    }
R
Richard W.M. Jones 已提交
4139 4140

    virLibDomainError (dom, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4141 4142 4143 4144

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dom->conn);
R
Richard W.M. Jones 已提交
4145 4146
    return -1;
}
4147

R
Richard W.M. Jones 已提交
4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189
/**
 * virDomainMemoryPeek:
 * @dom: pointer to the domain object
 * @start: start of memory to peek
 * @size: size of memory to peek
 * @buffer: return buffer (must be at least size bytes)
 * @flags: flags, see below
 *
 * This function allows you to read the contents of a domain's
 * memory.
 *
 * The memory which is read is controlled by the 'start', 'size'
 * and 'flags' parameters.
 *
 * If 'flags' is VIR_MEMORY_VIRTUAL then the 'start' and 'size'
 * parameters are interpreted as virtual memory addresses for
 * whichever task happens to be running on the domain at the
 * moment.  Although this sounds haphazard it is in fact what
 * you want in order to read Linux kernel state, because it
 * ensures that pointers in the kernel image can be interpreted
 * coherently.
 *
 * 'buffer' is the return buffer and must be at least 'size' bytes.
 * 'size' may be 0 to test if the call would succeed.
 *
 * NB. The remote driver imposes a 64K byte limit on 'size'.
 * For your program to be able to work reliably over a remote
 * connection you should split large requests to <= 65536 bytes.
 *
 * Returns: 0 in case of success or -1 in case of failure.
 */
int
virDomainMemoryPeek (virDomainPtr dom,
                     unsigned long long start /* really 64 bits */,
                     size_t size,
                     void *buffer,
                     unsigned int flags)
{
    virConnectPtr conn;
    DEBUG ("domain=%p, start=%lld, size=%zi, buffer=%p, flags=%d",
           dom, start, size, buffer, flags);

4190 4191
    virResetLastError();

R
Richard W.M. Jones 已提交
4192 4193 4194 4195 4196 4197
    if (!VIR_IS_CONNECTED_DOMAIN (dom)) {
        virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return -1;
    }
    conn = dom->conn;

4198 4199
    if (dom->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(dom, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4200
        goto error;
4201 4202
    }

4203
    /* Note on access to physical memory: A VIR_MEMORY_PHYSICAL flag is
R
Richard W.M. Jones 已提交
4204 4205 4206 4207 4208 4209
     * a possibility.  However it isn't really useful unless the caller
     * can also access registers, particularly CR3 on x86 in order to
     * get the Page Table Directory.  Since registers are different on
     * every architecture, that would imply another call to get the
     * machine registers.
     *
4210
     * The QEMU driver handles VIR_MEMORY_VIRTUAL, mapping it
R
Richard W.M. Jones 已提交
4211 4212 4213
     * to the qemu 'memsave' command which does the virtual to physical
     * mapping inside qemu.
     *
4214 4215 4216
     * The QEMU driver also handles VIR_MEMORY_PHYSICAL, mapping it
     * to the qemu 'pmemsave' command.
     *
R
Richard W.M. Jones 已提交
4217 4218 4219 4220 4221 4222 4223 4224
     * At time of writing there is no Xen driver.  However the Xen
     * hypervisor only lets you map physical pages from other domains,
     * and so the Xen driver would have to do the virtual to physical
     * mapping by chasing 2, 3 or 4-level page tables from the PTD.
     * There is example code in libxc (xc_translate_foreign_address)
     * which does this, although we cannot copy this code directly
     * because of incompatible licensing.
     */
4225 4226

    if (flags != VIR_MEMORY_VIRTUAL && flags != VIR_MEMORY_PHYSICAL) {
R
Richard W.M. Jones 已提交
4227
        virLibDomainError (dom, VIR_ERR_INVALID_ARG,
4228
                     _("flags parameter must be VIR_MEMORY_VIRTUAL or VIR_MEMORY_PHYSICAL"));
4229
        goto error;
R
Richard W.M. Jones 已提交
4230 4231 4232 4233 4234 4235
    }

    /* Allow size == 0 as an access test. */
    if (size > 0 && !buffer) {
        virLibDomainError (dom, VIR_ERR_INVALID_ARG,
                           _("buffer is NULL but size is non-zero"));
4236
        goto error;
R
Richard W.M. Jones 已提交
4237 4238
    }

4239 4240 4241 4242 4243 4244 4245 4246
    if (conn->driver->domainMemoryPeek) {
        int ret;
        ret = conn->driver->domainMemoryPeek (dom, start, size,
                                              buffer, flags);
        if (ret < 0)
            goto error;
        return ret;
    }
R
Richard W.M. Jones 已提交
4247 4248

    virLibDomainError (dom, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4249 4250 4251 4252

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dom->conn);
R
Richard W.M. Jones 已提交
4253 4254 4255
    return -1;
}

4256

4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267
/************************************************************************
 *									*
 *		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
 *
4268 4269
 * Define a domain, but does not start it.
 * This definition is persistent, until explicitly undefined with
4270 4271
 * virDomainUndefine(). A previous definition for this domain would be
 * overriden if it already exists.
4272 4273 4274 4275 4276
 *
 * Returns NULL in case of error, a pointer to the domain otherwise
 */
virDomainPtr
virDomainDefineXML(virConnectPtr conn, const char *xml) {
4277 4278
    DEBUG("conn=%p, xml=%s", conn, xml);

4279 4280
    virResetLastError();

4281
    if (!VIR_IS_CONNECT(conn)) {
4282
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
4283 4284
        return (NULL);
    }
4285 4286
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4287
        goto error;
4288
    }
4289 4290
    if (xml == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4291
        goto error;
4292 4293
    }

4294 4295 4296 4297 4298 4299 4300
    if (conn->driver->domainDefineXML) {
        virDomainPtr ret;
        ret = conn->driver->domainDefineXML (conn, xml);
        if (!ret)
            goto error;
        return ret;
    }
4301

4302
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4303 4304 4305 4306

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
4307
    return NULL;
4308 4309 4310 4311 4312 4313
}

/**
 * virDomainUndefine:
 * @domain: pointer to a defined domain
 *
4314
 * Undefine a domain but does not stop it if it is running
4315 4316 4317 4318 4319
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
virDomainUndefine(virDomainPtr domain) {
4320
    virConnectPtr conn;
4321
    DEBUG("domain=%p", domain);
4322

4323 4324
    virResetLastError();

4325
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4326
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4327 4328
        return (-1);
    }
4329 4330
    conn = domain->conn;
    if (conn->flags & VIR_CONNECT_RO) {
4331
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4332
        goto error;
4333 4334
    }

4335 4336 4337 4338 4339 4340 4341
    if (conn->driver->domainUndefine) {
        int ret;
        ret = conn->driver->domainUndefine (domain);
        if (ret < 0)
            goto error;
        return ret;
    }
4342

4343
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4344 4345 4346 4347

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
4348
    return -1;
4349 4350 4351 4352 4353 4354
}

/**
 * virConnectNumOfDefinedDomains:
 * @conn: pointer to the hypervisor connection
 *
4355
 * Provides the number of defined but inactive domains.
4356 4357 4358 4359 4360 4361
 *
 * Returns the number of domain found or -1 in case of error
 */
int
virConnectNumOfDefinedDomains(virConnectPtr conn)
{
4362 4363
    DEBUG("conn=%p", conn);

4364 4365
    virResetLastError();

4366
    if (!VIR_IS_CONNECT(conn)) {
4367
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
4368 4369 4370
        return (-1);
    }

4371 4372 4373 4374 4375 4376 4377
    if (conn->driver->numOfDefinedDomains) {
        int ret;
        ret = conn->driver->numOfDefinedDomains (conn);
        if (ret < 0)
            goto error;
        return ret;
    }
4378

4379
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4380 4381 4382 4383

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
4384
    return -1;
4385 4386 4387 4388 4389 4390 4391 4392
}

/**
 * virConnectListDefinedDomains:
 * @conn: pointer to the hypervisor connection
 * @names: pointer to an array to store the names
 * @maxnames: size of the array
 *
4393 4394
 * list the defined but inactive domains, stores the pointers to the names
 * in @names
4395
 *
4396 4397 4398
 * Returns the number of names provided in the array or -1 in case of error
 */
int
4399
virConnectListDefinedDomains(virConnectPtr conn, char **const names,
4400
                             int maxnames) {
4401 4402
    DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);

4403 4404
    virResetLastError();

4405
    if (!VIR_IS_CONNECT(conn)) {
4406
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
4407 4408 4409
        return (-1);
    }

4410
    if ((names == NULL) || (maxnames < 0)) {
4411
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4412
        goto error;
4413 4414
    }

4415 4416 4417 4418 4419 4420 4421
    if (conn->driver->listDefinedDomains) {
        int ret;
        ret = conn->driver->listDefinedDomains (conn, names, maxnames);
        if (ret < 0)
            goto error;
        return ret;
    }
4422

4423
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4424 4425 4426 4427

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
4428
    return -1;
4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441
}

/**
 * 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) {
4442
    virConnectPtr conn;
4443
    DEBUG("domain=%p", domain);
4444

4445 4446
    virResetLastError();

4447
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4448
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4449 4450
        return (-1);
    }
4451 4452
    conn = domain->conn;
    if (conn->flags & VIR_CONNECT_RO) {
4453
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4454
        goto error;
4455
    }
4456

4457 4458 4459 4460 4461 4462 4463
    if (conn->driver->domainCreate) {
        int ret;
        ret = conn->driver->domainCreate (domain);
        if (ret < 0)
            goto error;
        return ret;
    }
4464

4465
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4466 4467 4468 4469

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
4470
    return -1;
4471 4472
}

4473 4474 4475
/**
 * virDomainGetAutostart:
 * @domain: a domain object
4476
 * @autostart: the value returned
4477
 *
4478
 * Provides a boolean value indicating whether the domain
4479 4480 4481 4482 4483 4484 4485
 * 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,
4486 4487 4488
                      int *autostart)
{
    virConnectPtr conn;
4489
    DEBUG("domain=%p, autostart=%p", domain, autostart);
4490

4491 4492 4493
    virResetLastError();

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4494
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4495 4496 4497 4498
        return (-1);
    }
    if (!autostart) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
4499
        goto error;
4500 4501
    }

4502 4503
    conn = domain->conn;

4504 4505 4506 4507 4508 4509 4510
    if (conn->driver->domainGetAutostart) {
        int ret;
        ret = conn->driver->domainGetAutostart (domain, autostart);
        if (ret < 0)
            goto error;
        return ret;
    }
4511

4512
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4513 4514 4515 4516

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
4517
    return -1;
4518 4519 4520 4521 4522
}

/**
 * virDomainSetAutostart:
 * @domain: a domain object
4523
 * @autostart: whether the domain should be automatically started 0 or 1
4524 4525 4526 4527 4528 4529 4530 4531
 *
 * 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,
4532 4533 4534
                      int autostart)
{
    virConnectPtr conn;
4535
    DEBUG("domain=%p, autostart=%d", domain, autostart);
4536

4537 4538 4539
    virResetLastError();

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4540
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4541 4542 4543
        return (-1);
    }

4544 4545
    conn = domain->conn;

4546 4547
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4548
        goto error;
4549 4550
    }

4551 4552 4553 4554 4555 4556 4557
    if (conn->driver->domainSetAutostart) {
        int ret;
        ret = conn->driver->domainSetAutostart (domain, autostart);
        if (ret < 0)
            goto error;
        return ret;
    }
4558

4559
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4560 4561 4562 4563

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
4564
    return -1;
4565 4566
}

4567 4568 4569 4570 4571 4572 4573 4574
/**
 * 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.
4575
 * This function requires privileged access to the hypervisor.
4576 4577 4578 4579 4580 4581 4582
 *
 * Returns 0 in case of success, -1 in case of failure.
 */

int
virDomainSetVcpus(virDomainPtr domain, unsigned int nvcpus)
{
4583
    virConnectPtr conn;
4584
    DEBUG("domain=%p, nvcpus=%u", domain, nvcpus);
4585

4586 4587
    virResetLastError();

4588
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4589
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4590 4591
        return (-1);
    }
4592 4593
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4594
        goto error;
4595
    }
4596

4597 4598
    if (nvcpus < 1) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
4599
        goto error;
4600
    }
4601
    conn = domain->conn;
4602

4603 4604 4605 4606 4607 4608 4609
    if (conn->driver->domainSetVcpus) {
        int ret;
        ret = conn->driver->domainSetVcpus (domain, nvcpus);
        if (ret < 0)
            goto error;
        return ret;
    }
4610

4611
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4612 4613 4614 4615

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
4616
    return -1;
4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630
}

/**
 * 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.
4631
 *
4632
 * Dynamically change the real CPUs which can be allocated to a virtual CPU.
4633
 * This function requires privileged access to the hypervisor.
4634 4635 4636 4637 4638 4639 4640
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
int
virDomainPinVcpu(virDomainPtr domain, unsigned int vcpu,
                 unsigned char *cpumap, int maplen)
{
4641
    virConnectPtr conn;
4642
    DEBUG("domain=%p, vcpu=%u, cpumap=%p, maplen=%d", domain, vcpu, cpumap, maplen);
4643

4644 4645
    virResetLastError();

4646
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4647
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4648 4649
        return (-1);
    }
4650 4651
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4652
        goto error;
4653
    }
4654

4655
    if ((vcpu > 32000) || (cpumap == NULL) || (maplen < 1)) {
4656
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
4657
       goto error;
4658
    }
4659

4660 4661
    conn = domain->conn;

4662 4663 4664 4665 4666 4667 4668
    if (conn->driver->domainPinVcpu) {
        int ret;
        ret = conn->driver->domainPinVcpu (domain, vcpu, cpumap, maplen);
        if (ret < 0)
            goto error;
        return ret;
    }
4669

4670
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4671 4672 4673 4674

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
4675
    return -1;
4676 4677 4678 4679 4680 4681 4682 4683 4684
}

/**
 * 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)
D
Daniel Veillard 已提交
4685
 *	If cpumaps is NULL, then no cpumap information is returned by the API.
4686 4687 4688 4689 4690 4691 4692
 *	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...).
4693
 *
4694
 * Extract information about virtual CPUs of domain, store it in info array
D
Daniel Veillard 已提交
4695
 * and also in cpumaps if this pointer isn't NULL.
4696 4697 4698 4699 4700
 *
 * Returns the number of info filled in case of success, -1 in case of failure.
 */
int
virDomainGetVcpus(virDomainPtr domain, virVcpuInfoPtr info, int maxinfo,
4701
                  unsigned char *cpumaps, int maplen)
4702
{
4703
    virConnectPtr conn;
4704
    DEBUG("domain=%p, info=%p, maxinfo=%d, cpumaps=%p, maplen=%d", domain, info, maxinfo, cpumaps, maplen);
4705

4706 4707
    virResetLastError();

4708
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4709
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4710 4711 4712 4713
        return (-1);
    }
    if ((info == NULL) || (maxinfo < 1)) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
4714
        goto error;
4715 4716 4717
    }
    if (cpumaps != NULL && maplen < 1) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
4718
        goto error;
4719
    }
4720

4721 4722
    conn = domain->conn;

4723 4724 4725 4726 4727 4728 4729 4730
    if (conn->driver->domainGetVcpus) {
        int ret;
        ret = conn->driver->domainGetVcpus (domain, info, maxinfo,
                                            cpumaps, maplen);
        if (ret < 0)
            goto error;
        return ret;
    }
4731

4732
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4733 4734 4735 4736

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
4737
    return -1;
4738
}
4739

4740 4741 4742
/**
 * virDomainGetMaxVcpus:
 * @domain: pointer to domain object
4743
 *
4744 4745 4746 4747 4748
 * 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.
4749 4750 4751 4752
 *
 * Returns the maximum of virtual CPU or -1 in case of error.
 */
int
4753 4754
virDomainGetMaxVcpus(virDomainPtr domain)
{
4755
    virConnectPtr conn;
4756
    DEBUG("domain=%p", domain);
4757

4758 4759
    virResetLastError();

4760
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4761
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4762 4763 4764 4765 4766
        return (-1);
    }

    conn = domain->conn;

4767 4768 4769 4770 4771 4772 4773
    if (conn->driver->domainGetMaxVcpus) {
        int ret;
        ret = conn->driver->domainGetMaxVcpus (domain);
        if (ret < 0)
            goto error;
        return ret;
    }
4774

4775
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4776 4777 4778 4779

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
4780
    return -1;
4781 4782
}

4783 4784 4785 4786 4787
/**
 * virDomainGetSecurityLabel:
 * @domain: a domain object
 * @seclabel: pointer to a virSecurityLabel structure
 *
4788 4789 4790
 * Extract security label of an active domain. The 'label' field
 * in the @seclabel argument will be initialized to the empty
 * string if the domain is not running under a security model.
4791
 *
4792
 * Returns 0 in case of success, -1 in case of failure
4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805
 */
int
virDomainGetSecurityLabel(virDomainPtr domain, virSecurityLabelPtr seclabel)
{
    virConnectPtr conn;

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

    if (seclabel == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
4806
        goto error;
4807 4808 4809 4810
    }

    conn = domain->conn;

4811 4812 4813 4814 4815 4816 4817 4818 4819
    if (conn->driver->domainGetSecurityLabel) {
        int ret;
        ret = conn->driver->domainGetSecurityLabel(domain, seclabel);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4820

4821 4822 4823
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
4824
    return -1;
4825 4826 4827 4828 4829 4830 4831
}

/**
 * virNodeGetSecurityModel:
 * @conn: a connection object
 * @secmodel: pointer to a virSecurityModel structure
 *
4832 4833 4834
 * Extract the security model of a hypervisor. The 'model' field
 * in the @secmodel argument may be initialized to the empty
 * string if the driver has not activated a security model.
4835
 *
4836
 * Returns 0 in case of success, -1 in case of failure
4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847
 */
int
virNodeGetSecurityModel(virConnectPtr conn, virSecurityModelPtr secmodel)
{
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return -1;
    }

    if (secmodel == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4848 4849 4850 4851 4852 4853 4854 4855 4856
        goto error;
    }

    if (conn->driver->nodeGetSecurityModel) {
        int ret;
        ret = conn->driver->nodeGetSecurityModel(conn, secmodel);
        if (ret < 0)
            goto error;
        return ret;
4857 4858
    }

4859
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4860

4861 4862 4863
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
4864
    return -1;
4865
}
4866

4867 4868 4869 4870
/**
 * virDomainAttachDevice:
 * @domain: pointer to domain object
 * @xml: pointer to XML description of one device
4871
 *
4872 4873 4874 4875 4876
 * Create a virtual device attachment to backend.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
int
4877
virDomainAttachDevice(virDomainPtr domain, const char *xml)
4878 4879
{
    virConnectPtr conn;
4880
    DEBUG("domain=%p, xml=%s", domain, xml);
4881

4882 4883
    virResetLastError();

4884
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4885
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4886 4887 4888 4889
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4890
        goto error;
4891 4892 4893
    }
    conn = domain->conn;

4894 4895 4896 4897 4898 4899 4900
    if (conn->driver->domainAttachDevice) {
        int ret;
        ret = conn->driver->domainAttachDevice (domain, xml);
        if (ret < 0)
            goto error;
        return ret;
    }
4901

4902
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4903 4904 4905 4906

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
4907
    return -1;
4908 4909 4910 4911 4912 4913
}

/**
 * virDomainDetachDevice:
 * @domain: pointer to domain object
 * @xml: pointer to XML description of one device
4914
 *
4915 4916 4917 4918 4919
 * Destroy a virtual device attachment to backend.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
int
4920
virDomainDetachDevice(virDomainPtr domain, const char *xml)
4921 4922
{
    virConnectPtr conn;
4923
    DEBUG("domain=%p, xml=%s", domain, xml);
4924

4925 4926
    virResetLastError();

4927
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4928
        virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4929 4930 4931 4932
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4933
        goto error;
4934 4935 4936
    }
    conn = domain->conn;

4937 4938 4939 4940 4941 4942 4943
    if (conn->driver->domainDetachDevice) {
        int ret;
        ret = conn->driver->domainDetachDevice (domain, xml);
        if (ret < 0)
            goto error;
        return ret;
    }
4944

4945
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4946 4947 4948 4949

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(domain->conn);
4950
    return -1;
4951
}
4952

4953 4954 4955 4956 4957 4958 4959 4960 4961 4962
/**
 * virNodeGetCellsFreeMemory:
 * @conn: pointer to the hypervisor connection
 * @freeMems: pointer to the array of unsigned long long
 * @startCell: index of first cell to return freeMems info on.
 * @maxCells: Maximum number of cells for which freeMems information can
 *            be returned.
 *
 * This call returns the amount of free memory in one or more NUMA cells.
 * The @freeMems array must be allocated by the caller and will be filled
4963
 * with the amount of free memory in bytes for each cell requested,
4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974
 * starting with startCell (in freeMems[0]), up to either
 * (startCell + maxCells), or the number of additional cells in the node,
 * whichever is smaller.
 *
 * Returns the number of entries filled in freeMems, or -1 in case of error.
 */

int
virNodeGetCellsFreeMemory(virConnectPtr conn, unsigned long long *freeMems,
                          int startCell, int maxCells)
{
4975 4976 4977
    DEBUG("conn=%p, freeMems=%p, startCell=%d, maxCells=%d",
          conn, freeMems, startCell, maxCells);

4978 4979
    virResetLastError();

4980 4981 4982 4983 4984
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

D
Daniel Veillard 已提交
4985
    if ((freeMems == NULL) || (maxCells <= 0) || (startCell < 0)) {
4986
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4987
        goto error;
4988 4989
    }

4990 4991 4992 4993 4994 4995 4996
    if (conn->driver->nodeGetCellsFreeMemory) {
        int ret;
        ret = conn->driver->nodeGetCellsFreeMemory (conn, freeMems, startCell, maxCells);
        if (ret < 0)
            goto error;
        return ret;
    }
4997 4998

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4999 5000 5001 5002

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
5003 5004 5005
    return -1;
}

5006 5007 5008 5009
/**
 * virNetworkGetConnect:
 * @net: pointer to a network
 *
5010
 * Provides the connection pointer associated with a network.  The
5011 5012 5013
 * reference counter on the connection is not increased by this
 * call.
 *
5014 5015 5016 5017
 * WARNING: When writing libvirt bindings in other languages, do
 * not use this function.  Instead, store the connection and
 * the network object together.
 *
5018 5019 5020 5021 5022
 * Returns the virConnectPtr or NULL in case of failure.
 */
virConnectPtr
virNetworkGetConnect (virNetworkPtr net)
{
5023 5024
    DEBUG("net=%p", net);

5025 5026 5027
    virResetLastError();

    if (!VIR_IS_CONNECTED_NETWORK (net)) {
5028
        virLibNetworkError (NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
5029 5030 5031 5032 5033
        return NULL;
    }
    return net->conn;
}

5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044
/**
 * 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)
{
5045 5046
    DEBUG("conn=%p", conn);

5047 5048
    virResetLastError();

5049
    if (!VIR_IS_CONNECT(conn)) {
5050
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
5051 5052 5053
        return (-1);
    }

5054 5055 5056 5057 5058 5059 5060
    if (conn->networkDriver && conn->networkDriver->numOfNetworks) {
        int ret;
        ret = conn->networkDriver->numOfNetworks (conn);
        if (ret < 0)
            goto error;
        return ret;
    }
5061

5062
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5063 5064 5065 5066

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
5067
    return -1;
5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080
}

/**
 * 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
5081
virConnectListNetworks(virConnectPtr conn, char **const names, int maxnames)
5082
{
5083 5084
    DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);

5085 5086
    virResetLastError();

5087
    if (!VIR_IS_CONNECT(conn)) {
5088
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
5089 5090 5091
        return (-1);
    }

5092
    if ((names == NULL) || (maxnames < 0)) {
5093
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
5094
        goto error;
5095 5096
    }

5097 5098 5099 5100 5101 5102 5103
    if (conn->networkDriver && conn->networkDriver->listNetworks) {
        int ret;
        ret = conn->networkDriver->listNetworks (conn, names, maxnames);
        if (ret < 0)
            goto error;
        return ret;
    }
5104

5105
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5106 5107 5108 5109

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
5110
    return -1;
5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123
}

/**
 * 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)
{
5124 5125
    DEBUG("conn=%p", conn);

5126 5127
    virResetLastError();

5128
    if (!VIR_IS_CONNECT(conn)) {
5129
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
5130 5131 5132
        return (-1);
    }

5133 5134 5135 5136 5137 5138 5139
    if (conn->networkDriver && conn->networkDriver->numOfDefinedNetworks) {
        int ret;
        ret = conn->networkDriver->numOfDefinedNetworks (conn);
        if (ret < 0)
            goto error;
        return ret;
    }
5140

5141
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5142 5143 5144 5145

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
5146
    return -1;
5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159
}

/**
 * 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
5160
virConnectListDefinedNetworks(virConnectPtr conn, char **const names,
5161 5162
                              int maxnames)
{
5163 5164
    DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);

5165 5166
    virResetLastError();

5167
    if (!VIR_IS_CONNECT(conn)) {
5168
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
5169 5170 5171
        return (-1);
    }

5172
    if ((names == NULL) || (maxnames < 0)) {
5173
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
5174
        goto error;
5175 5176
    }

5177 5178 5179 5180 5181 5182 5183 5184
    if (conn->networkDriver && conn->networkDriver->listDefinedNetworks) {
        int ret;
        ret = conn->networkDriver->listDefinedNetworks (conn,
                                                        names, maxnames);
        if (ret < 0)
            goto error;
        return ret;
    }
5185

5186
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5187 5188 5189 5190

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
5191
    return -1;
5192 5193 5194 5195 5196 5197 5198 5199 5200
}

/**
 * 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.
 *
5201 5202
 * Returns a new network object or NULL in case of failure.  If the
 * network cannot be found, then VIR_ERR_NO_NETWORK error is raised.
5203 5204 5205 5206
 */
virNetworkPtr
virNetworkLookupByName(virConnectPtr conn, const char *name)
{
5207 5208
    DEBUG("conn=%p, name=%s", conn, name);

5209 5210
    virResetLastError();

5211
    if (!VIR_IS_CONNECT(conn)) {
5212
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
5213 5214 5215 5216
        return (NULL);
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
5217
        goto  error;
5218 5219
    }

5220 5221 5222 5223 5224 5225 5226
    if (conn->networkDriver && conn->networkDriver->networkLookupByName) {
        virNetworkPtr ret;
        ret = conn->networkDriver->networkLookupByName (conn, name);
        if (!ret)
            goto error;
        return ret;
    }
5227

5228
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5229 5230 5231 5232

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
5233
    return NULL;
5234 5235 5236 5237 5238 5239 5240 5241 5242
}

/**
 * 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.
 *
5243 5244
 * Returns a new network object or NULL in case of failure.  If the
 * network cannot be found, then VIR_ERR_NO_NETWORK error is raised.
5245 5246 5247 5248
 */
virNetworkPtr
virNetworkLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
5249 5250
    DEBUG("conn=%p, uuid=%s", conn, uuid);

5251 5252
    virResetLastError();

5253
    if (!VIR_IS_CONNECT(conn)) {
5254
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
5255 5256 5257 5258
        return (NULL);
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
5259
        goto error;
5260 5261
    }

5262 5263 5264 5265 5266 5267 5268
    if (conn->networkDriver && conn->networkDriver->networkLookupByUUID){
        virNetworkPtr ret;
        ret = conn->networkDriver->networkLookupByUUID (conn, uuid);
        if (!ret)
            goto error;
        return ret;
    }
5269

5270
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5271 5272 5273 5274

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
5275
    return NULL;
5276 5277 5278 5279 5280 5281 5282 5283 5284
}

/**
 * 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.
 *
5285 5286
 * Returns a new network object or NULL in case of failure.  If the
 * network cannot be found, then VIR_ERR_NO_NETWORK error is raised.
5287 5288 5289 5290 5291
 */
virNetworkPtr
virNetworkLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
{
    unsigned char uuid[VIR_UUID_BUFLEN];
5292
    DEBUG("conn=%p, uuidstr=%s", conn, uuidstr);
5293

5294 5295
    virResetLastError();

5296
    if (!VIR_IS_CONNECT(conn)) {
5297
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
5298 5299 5300 5301
        return (NULL);
    }
    if (uuidstr == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
5302
        goto error;
5303 5304
    }

5305
    if (virUUIDParse(uuidstr, uuid) < 0) {
5306
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
5307
        goto error;
5308 5309 5310
    }

    return virNetworkLookupByUUID(conn, &uuid[0]);
5311 5312 5313 5314 5315

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330
}

/**
 * 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)
{
5331 5332
    DEBUG("conn=%p, xmlDesc=%s", conn, xmlDesc);

5333 5334
    virResetLastError();

5335
    if (!VIR_IS_CONNECT(conn)) {
5336
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
5337 5338 5339 5340
        return (NULL);
    }
    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
5341
        goto error;
5342 5343 5344
    }
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5345
        goto error;
5346 5347
    }

5348 5349 5350 5351 5352 5353 5354
    if (conn->networkDriver && conn->networkDriver->networkCreateXML) {
        virNetworkPtr ret;
        ret = conn->networkDriver->networkCreateXML (conn, xmlDesc);
        if (!ret)
            goto error;
        return ret;
    }
5355

5356
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5357 5358 5359 5360

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
5361
    return NULL;
5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373
}

/**
 * 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
5374 5375
virNetworkDefineXML(virConnectPtr conn, const char *xml)
{
5376 5377
    DEBUG("conn=%p, xml=%s", conn, xml);

5378 5379
    virResetLastError();

5380
    if (!VIR_IS_CONNECT(conn)) {
5381
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
5382 5383 5384 5385
        return (NULL);
    }
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5386
        goto error;
5387 5388 5389
    }
    if (xml == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
5390
        goto error;
5391 5392
    }

5393 5394 5395 5396 5397 5398 5399
    if (conn->networkDriver && conn->networkDriver->networkDefineXML) {
        virNetworkPtr ret;
        ret = conn->networkDriver->networkDefineXML (conn, xml);
        if (!ret)
            goto error;
        return ret;
    }
5400

5401
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5402 5403 5404 5405

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
5406
    return NULL;
5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419
}

/**
 * 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;
5420
    DEBUG("network=%p", network);
5421

5422 5423
    virResetLastError();

5424
    if (!VIR_IS_CONNECTED_NETWORK(network)) {
5425
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
5426 5427 5428 5429 5430
        return (-1);
    }
    conn = network->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5431
        goto error;
5432 5433
    }

5434 5435 5436 5437 5438 5439 5440
    if (conn->networkDriver && conn->networkDriver->networkUndefine) {
        int ret;
        ret = conn->networkDriver->networkUndefine (network);
        if (ret < 0)
            goto error;
        return ret;
    }
5441

5442
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5443 5444 5445 5446

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(network->conn);
5447
    return -1;
5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459
}

/**
 * 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
5460 5461
virNetworkCreate(virNetworkPtr network)
{
5462
    virConnectPtr conn;
5463 5464
    DEBUG("network=%p", network);

5465 5466
    virResetLastError();

5467
    if (!VIR_IS_CONNECTED_NETWORK(network)) {
5468
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
5469 5470 5471 5472 5473
        return (-1);
    }
    conn = network->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5474
        goto error;
5475 5476
    }

5477 5478 5479 5480 5481 5482 5483
    if (conn->networkDriver && conn->networkDriver->networkCreate) {
        int ret;
        ret = conn->networkDriver->networkCreate (network);
        if (ret < 0)
            goto error;
        return ret;
    }
5484

5485
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5486 5487 5488 5489

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(network->conn);
5490
    return -1;
5491 5492 5493 5494 5495 5496 5497
}

/**
 * virNetworkDestroy:
 * @network: a network object
 *
 * Destroy the network object. The running instance is shutdown if not down
5498 5499 5500
 * already and all resources used by it are given back to the hypervisor. This
 * does not free the associated virNetworkPtr object.
 * This function may require privileged access
5501 5502 5503 5504 5505 5506 5507
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virNetworkDestroy(virNetworkPtr network)
{
    virConnectPtr conn;
5508
    DEBUG("network=%p", network);
5509

5510 5511
    virResetLastError();

5512
    if (!VIR_IS_CONNECTED_NETWORK(network)) {
5513
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
5514 5515 5516 5517 5518 5519
        return (-1);
    }

    conn = network->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5520
        goto error;
5521 5522
    }

5523 5524 5525 5526 5527 5528 5529
    if (conn->networkDriver && conn->networkDriver->networkDestroy) {
        int ret;
        ret = conn->networkDriver->networkDestroy (network);
        if (ret < 0)
            goto error;
        return ret;
    }
5530

5531
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5532 5533 5534 5535

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(network->conn);
5536
    return -1;
5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550
}

/**
 * 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)
{
5551 5552
    DEBUG("network=%p", network);

5553 5554 5555
    virResetLastError();

    if (!VIR_IS_CONNECTED_NETWORK(network)) {
5556
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
5557 5558
        return (-1);
    }
5559
    if (virUnrefNetwork(network) < 0)
5560 5561 5562 5563
        return (-1);
    return(0);
}

5564 5565
/**
 * virNetworkRef:
D
Daniel Veillard 已提交
5566
 * @network: the network to hold a reference on
5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577
 *
 * Increment the reference count on the network. For each
 * additional call to this method, there shall be a corresponding
 * call to virNetworkFree to release the reference count, once
 * the caller no longer needs the reference to this object.
 *
 * This method is typically useful for applications where multiple
 * threads are using a connection, and it is required that the
 * connection remain open until all threads have finished using
 * it. ie, each new thread using a network would increment
 * the reference count.
D
Daniel Veillard 已提交
5578 5579
 *
 * Returns 0 in case of success, -1 in case of failure.
5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594
 */
int
virNetworkRef(virNetworkPtr network)
{
    if ((!VIR_IS_CONNECTED_NETWORK(network))) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }
    virMutexLock(&network->conn->lock);
    DEBUG("network=%p refs=%d", network, network->refs);
    network->refs++;
    virMutexUnlock(&network->conn->lock);
    return 0;
}

5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606
/**
 * 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)
{
5607 5608
    DEBUG("network=%p", network);

5609 5610
    virResetLastError();

5611
    if (!VIR_IS_NETWORK(network)) {
5612
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629
        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)
{
5630 5631
    DEBUG("network=%p, uuid=%p", network, uuid);

5632 5633
    virResetLastError();

5634
    if (!VIR_IS_NETWORK(network)) {
5635
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
5636 5637 5638 5639
        return (-1);
    }
    if (uuid == NULL) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
5640
        goto error;
5641 5642 5643 5644 5645
    }

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

    return (0);
5646 5647 5648 5649 5650

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(network->conn);
    return -1;
5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666
}

/**
 * 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];
5667
    DEBUG("network=%p, buf=%p", network, buf);
5668

5669 5670
    virResetLastError();

5671
    if (!VIR_IS_NETWORK(network)) {
5672
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
5673 5674 5675 5676
        return (-1);
    }
    if (buf == NULL) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
5677
        goto error;
5678 5679 5680
    }

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

5683
    virUUIDFormat(uuid, buf);
5684
    return (0);
5685 5686 5687 5688 5689

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(network->conn);
    return -1;
5690 5691 5692 5693 5694
}

/**
 * virNetworkGetXMLDesc:
 * @network: a network object
L
Laine Stump 已提交
5695
 * @flags: an OR'ed set of extraction flags, not used yet
5696 5697 5698 5699 5700 5701 5702 5703 5704 5705
 *
 * 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)
{
5706
    virConnectPtr conn;
5707
    DEBUG("network=%p, flags=%d", network, flags);
5708

5709 5710 5711
    virResetLastError();

    if (!VIR_IS_CONNECTED_NETWORK(network)) {
5712
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
5713 5714 5715 5716
        return (NULL);
    }
    if (flags != 0) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
5717
        goto error;
5718 5719
    }

5720 5721
    conn = network->conn;

5722 5723 5724 5725 5726 5727 5728
    if (conn->networkDriver && conn->networkDriver->networkDumpXML) {
        char *ret;
        ret = conn->networkDriver->networkDumpXML (network, flags);
        if (!ret)
            goto error;
        return ret;
    }
5729

5730
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5731 5732 5733 5734

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(network->conn);
5735
    return NULL;
5736
}
5737 5738 5739 5740 5741

/**
 * virNetworkGetBridgeName:
 * @network: a network object
 *
5742
 * Provides a bridge interface name to which a domain may connect
5743 5744 5745 5746 5747 5748 5749 5750
 * 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)
{
5751
    virConnectPtr conn;
5752
    DEBUG("network=%p", network);
5753

5754 5755 5756
    virResetLastError();

    if (!VIR_IS_CONNECTED_NETWORK(network)) {
5757
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
5758 5759 5760
        return (NULL);
    }

5761 5762
    conn = network->conn;

5763 5764 5765 5766 5767 5768 5769
    if (conn->networkDriver && conn->networkDriver->networkGetBridgeName) {
        char *ret;
        ret = conn->networkDriver->networkGetBridgeName (network);
        if (!ret)
            goto error;
        return ret;
    }
5770

5771
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5772 5773 5774 5775

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(network->conn);
5776
    return NULL;
5777
}
5778 5779 5780 5781

/**
 * virNetworkGetAutostart:
 * @network: a network object
5782
 * @autostart: the value returned
5783
 *
5784
 * Provides a boolean value indicating whether the network
5785 5786 5787 5788 5789 5790 5791
 * 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,
5792 5793 5794
                       int *autostart)
{
    virConnectPtr conn;
5795
    DEBUG("network=%p, autostart=%p", network, autostart);
5796

5797 5798 5799
    virResetLastError();

    if (!VIR_IS_CONNECTED_NETWORK(network)) {
5800
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
5801 5802 5803 5804
        return (-1);
    }
    if (!autostart) {
        virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
5805
        goto error;
5806 5807
    }

5808 5809
    conn = network->conn;

5810 5811 5812 5813 5814 5815 5816
    if (conn->networkDriver && conn->networkDriver->networkGetAutostart) {
        int ret;
        ret = conn->networkDriver->networkGetAutostart (network, autostart);
        if (ret < 0)
            goto error;
        return ret;
    }
5817

5818
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5819 5820 5821 5822

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(network->conn);
5823
    return -1;
5824 5825 5826 5827 5828
}

/**
 * virNetworkSetAutostart:
 * @network: a network object
5829
 * @autostart: whether the network should be automatically started 0 or 1
5830 5831 5832 5833 5834 5835 5836 5837
 *
 * 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,
5838 5839 5840
                       int autostart)
{
    virConnectPtr conn;
5841
    DEBUG("network=%p, autostart=%d", network, autostart);
5842

5843 5844 5845
    virResetLastError();

    if (!VIR_IS_CONNECTED_NETWORK(network)) {
5846
        virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
5847 5848 5849
        return (-1);
    }

5850 5851
    if (network->conn->flags & VIR_CONNECT_RO) {
        virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5852
        goto error;
5853 5854
    }

5855 5856
    conn = network->conn;

5857 5858 5859 5860 5861 5862 5863
    if (conn->networkDriver && conn->networkDriver->networkSetAutostart) {
        int ret;
        ret = conn->networkDriver->networkSetAutostart (network, autostart);
        if (ret < 0)
            goto error;
        return ret;
    }
5864

5865
    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5866 5867 5868 5869

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(network->conn);
5870
    return -1;
5871
}
5872

D
Daniel Veillard 已提交
5873 5874
/**
 * virInterfaceGetConnect:
L
Laine Stump 已提交
5875
 * @iface: pointer to an interface
D
Daniel Veillard 已提交
5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887
 *
 * Provides the connection pointer associated with an interface.  The
 * reference counter on the connection is not increased by this
 * call.
 *
 * WARNING: When writing libvirt bindings in other languages, do
 * not use this function.  Instead, store the connection and
 * the interface object together.
 *
 * Returns the virConnectPtr or NULL in case of failure.
 */
virConnectPtr
5888
virInterfaceGetConnect (virInterfacePtr iface)
D
Daniel Veillard 已提交
5889
{
5890
    DEBUG("iface=%p", iface);
D
Daniel Veillard 已提交
5891 5892 5893

    virResetLastError();

5894
    if (!VIR_IS_CONNECTED_INTERFACE (iface)) {
D
Daniel Veillard 已提交
5895 5896 5897
        virLibInterfaceError (NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
        return NULL;
    }
5898
    return iface->conn;
D
Daniel Veillard 已提交
5899 5900 5901 5902 5903 5904
}

/**
 * virConnectNumOfInterfaces:
 * @conn: pointer to the hypervisor connection
 *
5905
 * Provides the number of active interfaces on the physical host.
D
Daniel Veillard 已提交
5906
 *
5907
 * Returns the number of active interfaces found or -1 in case of error
D
Daniel Veillard 已提交
5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942
 */
int
virConnectNumOfInterfaces(virConnectPtr conn)
{
    DEBUG("conn=%p", conn);

    virResetLastError();

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

    if (conn->interfaceDriver && conn->interfaceDriver->numOfInterfaces) {
        int ret;
        ret = conn->interfaceDriver->numOfInterfaces (conn);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return -1;
}

/**
 * virConnectListInterfaces:
 * @conn: pointer to the hypervisor connection
 * @names: array to collect the list of names of interfaces
 * @maxnames: size of @names
 *
5943 5944
 * Collect the list of active physical host interfaces,
 * and store their names in @names
D
Daniel Veillard 已提交
5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980
 *
 * Returns the number of interfaces found or -1 in case of error
 */
int
virConnectListInterfaces(virConnectPtr conn, char **const names, int maxnames)
{
    DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);

    virResetLastError();

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

    if ((names == NULL) || (maxnames < 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

    if (conn->interfaceDriver && conn->interfaceDriver->listInterfaces) {
        int ret;
        ret = conn->interfaceDriver->listInterfaces (conn, names, maxnames);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return -1;
}

5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062
/**
 * virConnectNumOfDefinedInterfaces:
 * @conn: pointer to the hypervisor connection
 *
 * Provides the number of defined (inactive) interfaces on the physical host.
 *
 * Returns the number of defined interface found or -1 in case of error
 */
int
virConnectNumOfDefinedInterfaces(virConnectPtr conn)
{
    DEBUG("conn=%p", conn);

    virResetLastError();

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

    if (conn->interfaceDriver && conn->interfaceDriver->numOfDefinedInterfaces) {
        int ret;
        ret = conn->interfaceDriver->numOfDefinedInterfaces (conn);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return -1;
}

/**
 * virConnectListDefinedInterfaces:
 * @conn: pointer to the hypervisor connection
 * @names: array to collect the list of names of interfaces
 * @maxnames: size of @names
 *
 * Collect the list of defined (inactive) physical host interfaces,
 * and store their names in @names.
 *
 * Returns the number of interfaces found or -1 in case of error
 */
int
virConnectListDefinedInterfaces(virConnectPtr conn,
                                char **const names,
                                int maxnames)
{
    DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);

    virResetLastError();

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

    if ((names == NULL) || (maxnames < 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

    if (conn->interfaceDriver && conn->interfaceDriver->listDefinedInterfaces) {
        int ret;
        ret = conn->interfaceDriver->listDefinedInterfaces (conn, names, maxnames);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return -1;
}

D
Daniel Veillard 已提交
6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148
/**
 * virInterfaceLookupByName:
 * @conn: pointer to the hypervisor connection
 * @name: name for the interface
 *
 * Try to lookup an interface on the given hypervisor based on its name.
 *
 * Returns a new interface object or NULL in case of failure.  If the
 * interface cannot be found, then VIR_ERR_NO_INTERFACE error is raised.
 */
virInterfacePtr
virInterfaceLookupByName(virConnectPtr conn, const char *name)
{
    DEBUG("conn=%p, name=%s", conn, name);

    virResetLastError();

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

    if (conn->interfaceDriver && conn->interfaceDriver->interfaceLookupByName) {
        virInterfacePtr ret;
        ret = conn->interfaceDriver->interfaceLookupByName (conn, name);
        if (!ret)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
}

/**
 * virInterfaceLookupByMACString:
 * @conn: pointer to the hypervisor connection
 * @macstr: the MAC for the interface (null-terminated ASCII format)
 *
 * Try to lookup an interface on the given hypervisor based on its MAC.
 *
 * Returns a new interface object or NULL in case of failure.  If the
 * interface cannot be found, then VIR_ERR_NO_INTERFACE error is raised.
 */
virInterfacePtr
virInterfaceLookupByMACString(virConnectPtr conn, const char *macstr)
{
    DEBUG("conn=%p, macstr=%s", conn, macstr);

    virResetLastError();

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

    if (conn->interfaceDriver && conn->interfaceDriver->interfaceLookupByMACString) {
        virInterfacePtr ret;
        ret = conn->interfaceDriver->interfaceLookupByMACString (conn, macstr);
        if (!ret)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
}

/**
 * virInterfaceGetName:
L
Laine Stump 已提交
6149
 * @iface: an interface object
D
Daniel Veillard 已提交
6150 6151 6152 6153 6154 6155 6156
 *
 * Get the public name for that interface
 *
 * Returns a pointer to the name or NULL, the string need not be deallocated
 * its lifetime will be the same as the interface object.
 */
const char *
6157
virInterfaceGetName(virInterfacePtr iface)
D
Daniel Veillard 已提交
6158
{
6159
    DEBUG("iface=%p", iface);
D
Daniel Veillard 已提交
6160 6161 6162

    virResetLastError();

6163
    if (!VIR_IS_INTERFACE(iface)) {
D
Daniel Veillard 已提交
6164 6165 6166
        virLibInterfaceError(NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
        return (NULL);
    }
6167
    return (iface->name);
D
Daniel Veillard 已提交
6168 6169 6170 6171
}

/**
 * virInterfaceGetMACString:
L
Laine Stump 已提交
6172
 * @iface: an interface object
D
Daniel Veillard 已提交
6173
 *
L
Laine Stump 已提交
6174
 * Get the MAC for an interface as string. For more information about
D
Daniel Veillard 已提交
6175 6176 6177 6178 6179 6180 6181
 * MAC see RFC4122.
 *
 * Returns a pointer to the MAC address (in null-terminated ASCII
 * format) or NULL, the string need not be deallocated its lifetime
 * will be the same as the interface object.
 */
const char *
6182
virInterfaceGetMACString(virInterfacePtr iface)
D
Daniel Veillard 已提交
6183
{
6184
    DEBUG("iface=%p", iface);
D
Daniel Veillard 已提交
6185 6186 6187

    virResetLastError();

6188
    if (!VIR_IS_INTERFACE(iface)) {
D
Daniel Veillard 已提交
6189 6190 6191
        virLibInterfaceError(NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
        return (NULL);
    }
6192
    return (iface->mac);
D
Daniel Veillard 已提交
6193 6194 6195 6196
}

/**
 * virInterfaceGetXMLDesc:
L
Laine Stump 已提交
6197 6198
 * @iface: an interface object
 * @flags: an OR'ed set of extraction flags, not used yet
D
Daniel Veillard 已提交
6199 6200
 *
 * Provide an XML description of the interface. The description may be reused
L
Laine Stump 已提交
6201
 * later to redefine the interface with virInterfaceDefineXML().
D
Daniel Veillard 已提交
6202 6203 6204 6205 6206
 *
 * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
6207
virInterfaceGetXMLDesc(virInterfacePtr iface, unsigned int flags)
D
Daniel Veillard 已提交
6208 6209
{
    virConnectPtr conn;
6210
    DEBUG("iface=%p, flags=%d", iface, flags);
D
Daniel Veillard 已提交
6211 6212 6213

    virResetLastError();

6214
    if (!VIR_IS_CONNECTED_INTERFACE(iface)) {
D
Daniel Veillard 已提交
6215 6216 6217 6218
        virLibInterfaceError(NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
        return (NULL);
    }
    if (flags != 0) {
6219
        virLibInterfaceError(iface, VIR_ERR_INVALID_ARG, __FUNCTION__);
D
Daniel Veillard 已提交
6220 6221 6222
        goto error;
    }

6223
    conn = iface->conn;
D
Daniel Veillard 已提交
6224 6225 6226

    if (conn->interfaceDriver && conn->interfaceDriver->interfaceGetXMLDesc) {
        char *ret;
6227
        ret = conn->interfaceDriver->interfaceGetXMLDesc (iface, flags);
D
Daniel Veillard 已提交
6228 6229 6230 6231 6232 6233 6234 6235 6236
        if (!ret)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
6237
    virSetConnError(iface->conn);
D
Daniel Veillard 已提交
6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288
    return NULL;
}

/**
 * virInterfaceDefineXML:
 * @conn: pointer to the hypervisor connection
 * @xml: the XML description for the interface, preferably in UTF-8
 * @flags: and OR'ed set of extraction flags, not used yet
 *
 * Define an interface (or modify existing interface configuration)
 *
 * Returns NULL in case of error, a pointer to the interface otherwise
 */
virInterfacePtr
virInterfaceDefineXML(virConnectPtr conn, const char *xml, unsigned int flags)
{
    DEBUG("conn=%p, xml=%s, flags=%d", conn, xml, flags);

    virResetLastError();

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        goto error;
    }
    if (xml == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

    if (conn->interfaceDriver && conn->interfaceDriver->interfaceDefineXML) {
        virInterfacePtr ret;
        ret = conn->interfaceDriver->interfaceDefineXML (conn, xml, flags);
        if (!ret)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
}

/**
 * virInterfaceUndefine:
6289
 * @iface: pointer to a defined interface
D
Daniel Veillard 已提交
6290 6291 6292 6293 6294 6295 6296
 *
 * Undefine an interface, ie remove it from the config.
 * This does not free the associated virInterfacePtr object.
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
6297
virInterfaceUndefine(virInterfacePtr iface) {
D
Daniel Veillard 已提交
6298
    virConnectPtr conn;
6299
    DEBUG("iface=%p", iface);
D
Daniel Veillard 已提交
6300 6301 6302

    virResetLastError();

6303
    if (!VIR_IS_CONNECTED_INTERFACE(iface)) {
D
Daniel Veillard 已提交
6304 6305 6306
        virLibInterfaceError(NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
        return (-1);
    }
6307
    conn = iface->conn;
D
Daniel Veillard 已提交
6308
    if (conn->flags & VIR_CONNECT_RO) {
6309
        virLibInterfaceError(iface, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
D
Daniel Veillard 已提交
6310 6311 6312 6313 6314
        goto error;
    }

    if (conn->interfaceDriver && conn->interfaceDriver->interfaceUndefine) {
        int ret;
6315
        ret = conn->interfaceDriver->interfaceUndefine (iface);
D
Daniel Veillard 已提交
6316 6317 6318 6319 6320 6321 6322 6323 6324
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
6325
    virSetConnError(iface->conn);
D
Daniel Veillard 已提交
6326 6327 6328 6329 6330
    return -1;
}

/**
 * virInterfaceCreate:
6331
 * @iface: pointer to a defined interface
D
Daniel Veillard 已提交
6332 6333 6334 6335 6336 6337 6338
 * @flags: and OR'ed set of extraction flags, not used yet
 *
 * Activate an interface (ie call "ifup")
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
6339
virInterfaceCreate(virInterfacePtr iface, unsigned int flags)
D
Daniel Veillard 已提交
6340 6341
{
    virConnectPtr conn;
6342
    DEBUG("iface=%p, flags=%d", iface, flags);
D
Daniel Veillard 已提交
6343 6344 6345

    virResetLastError();

6346
    if (!VIR_IS_CONNECTED_INTERFACE(iface)) {
D
Daniel Veillard 已提交
6347 6348 6349
        virLibInterfaceError(NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
        return (-1);
    }
6350
    conn = iface->conn;
D
Daniel Veillard 已提交
6351
    if (conn->flags & VIR_CONNECT_RO) {
6352
        virLibInterfaceError(iface, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
D
Daniel Veillard 已提交
6353 6354 6355 6356 6357
        goto error;
    }

    if (conn->interfaceDriver && conn->interfaceDriver->interfaceCreate) {
        int ret;
6358
        ret = conn->interfaceDriver->interfaceCreate (iface, flags);
D
Daniel Veillard 已提交
6359 6360 6361 6362 6363 6364 6365 6366 6367
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
6368
    virSetConnError(iface->conn);
D
Daniel Veillard 已提交
6369 6370 6371 6372 6373
    return -1;
}

/**
 * virInterfaceDestroy:
6374
 * @iface: an interface object
D
Daniel Veillard 已提交
6375 6376 6377 6378 6379 6380 6381 6382 6383
 * @flags: and OR'ed set of extraction flags, not used yet
 *
 * deactivate an interface (ie call "ifdown")
 * This does not remove the interface from the config, and
 * does not free the associated virInterfacePtr object.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
6384
virInterfaceDestroy(virInterfacePtr iface, unsigned int flags)
D
Daniel Veillard 已提交
6385 6386
{
    virConnectPtr conn;
6387
    DEBUG("iface=%p, flags=%d", iface, flags);
D
Daniel Veillard 已提交
6388 6389 6390

    virResetLastError();

6391
    if (!VIR_IS_CONNECTED_INTERFACE(iface)) {
D
Daniel Veillard 已提交
6392 6393 6394 6395
        virLibInterfaceError(NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
        return (-1);
    }

6396
    conn = iface->conn;
D
Daniel Veillard 已提交
6397
    if (conn->flags & VIR_CONNECT_RO) {
6398
        virLibInterfaceError(iface, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
D
Daniel Veillard 已提交
6399 6400 6401 6402 6403
        goto error;
    }

    if (conn->interfaceDriver && conn->interfaceDriver->interfaceDestroy) {
        int ret;
6404
        ret = conn->interfaceDriver->interfaceDestroy (iface, flags);
D
Daniel Veillard 已提交
6405 6406 6407 6408 6409 6410 6411 6412 6413
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
6414
    virSetConnError(iface->conn);
D
Daniel Veillard 已提交
6415 6416 6417 6418 6419
    return -1;
}

/**
 * virInterfaceRef:
6420
 * @iface: the interface to hold a reference on
D
Daniel Veillard 已提交
6421 6422 6423 6424 6425 6426 6427 6428 6429
 *
 * Increment the reference count on the interface. For each
 * additional call to this method, there shall be a corresponding
 * call to virInterfaceFree to release the reference count, once
 * the caller no longer needs the reference to this object.
 *
 * This method is typically useful for applications where multiple
 * threads are using a connection, and it is required that the
 * connection remain open until all threads have finished using
L
Laine Stump 已提交
6430
 * it. ie, each new thread using an interface would increment
D
Daniel Veillard 已提交
6431 6432 6433 6434 6435
 * the reference count.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
int
6436
virInterfaceRef(virInterfacePtr iface)
D
Daniel Veillard 已提交
6437
{
6438
    if ((!VIR_IS_CONNECTED_INTERFACE(iface))) {
D
Daniel Veillard 已提交
6439 6440 6441
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }
6442 6443 6444 6445
    virMutexLock(&iface->conn->lock);
    DEBUG("iface=%p refs=%d", iface, iface->refs);
    iface->refs++;
    virMutexUnlock(&iface->conn->lock);
D
Daniel Veillard 已提交
6446 6447 6448 6449 6450
    return 0;
}

/**
 * virInterfaceFree:
L
Laine Stump 已提交
6451
 * @iface: an interface object
D
Daniel Veillard 已提交
6452 6453 6454 6455 6456 6457 6458
 *
 * Free the interface object. The interface itself is unaltered.
 * The data structure is freed and should not be used thereafter.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
6459
virInterfaceFree(virInterfacePtr iface)
D
Daniel Veillard 已提交
6460
{
6461
    DEBUG("iface=%p", iface);
D
Daniel Veillard 已提交
6462 6463 6464

    virResetLastError();

6465
    if (!VIR_IS_CONNECTED_INTERFACE(iface)) {
D
Daniel Veillard 已提交
6466 6467 6468
        virLibInterfaceError(NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
        return (-1);
    }
6469
    if (virUnrefInterface(iface) < 0)
D
Daniel Veillard 已提交
6470 6471 6472 6473
        return (-1);
    return(0);
}

6474 6475 6476

/**
 * virStoragePoolGetConnect:
6477
 * @pool: pointer to a pool
6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493
 *
 * Provides the connection pointer associated with a storage pool.  The
 * reference counter on the connection is not increased by this
 * call.
 *
 * WARNING: When writing libvirt bindings in other languages, do
 * not use this function.  Instead, store the connection and
 * the pool object together.
 *
 * Returns the virConnectPtr or NULL in case of failure.
 */
virConnectPtr
virStoragePoolGetConnect (virStoragePoolPtr pool)
{
    DEBUG("pool=%p", pool);

6494 6495 6496
    virResetLastError();

    if (!VIR_IS_CONNECTED_STORAGE_POOL (pool)) {
6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515
        virLibStoragePoolError (NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return NULL;
    }
    return pool->conn;
}

/**
 * virConnectNumOfStoragePools:
 * @conn: pointer to hypervisor connection
 *
 * Provides the number of active storage pools
 *
 * Returns the number of pools found, or -1 on error
 */
int
virConnectNumOfStoragePools	(virConnectPtr conn)
{
    DEBUG("conn=%p", conn);

6516 6517
    virResetLastError();

6518 6519 6520 6521 6522
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

6523 6524 6525 6526 6527 6528 6529
    if (conn->storageDriver && conn->storageDriver->numOfPools) {
        int ret;
        ret = conn->storageDriver->numOfPools (conn);
        if (ret < 0)
            goto error;
        return ret;
    }
6530 6531

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6532 6533 6534 6535

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557
    return -1;
}

/**
 * virConnectListStoragePools:
 * @conn: pointer to hypervisor connection
 * @names: array of char * to fill with pool names (allocated by caller)
 * @maxnames: size of the names array
 *
 * Provides the list of names of active storage pools
 * upto maxnames. If there are more than maxnames, the
 * remaining names will be silently ignored.
 *
 * Returns 0 on success, -1 on error
 */
int
virConnectListStoragePools	(virConnectPtr conn,
                             char **const names,
                             int maxnames)
{
    DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);

6558 6559
    virResetLastError();

6560 6561 6562 6563 6564 6565 6566
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if ((names == NULL) || (maxnames < 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6567
        goto error;
6568 6569
    }

6570 6571 6572 6573 6574 6575 6576
    if (conn->storageDriver && conn->storageDriver->listPools) {
        int ret;
        ret = conn->storageDriver->listPools (conn, names, maxnames);
        if (ret < 0)
            goto error;
        return ret;
    }
6577 6578 6579

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

6580 6581 6582 6583
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return -1;
6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599
}


/**
 * virConnectNumOfDefinedStoragePools:
 * @conn: pointer to hypervisor connection
 *
 * Provides the number of inactive storage pools
 *
 * Returns the number of pools found, or -1 on error
 */
int
virConnectNumOfDefinedStoragePools(virConnectPtr conn)
{
    DEBUG("conn=%p", conn);

6600 6601
    virResetLastError();

6602 6603 6604 6605 6606
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

6607 6608 6609 6610 6611 6612 6613
    if (conn->storageDriver && conn->storageDriver->numOfDefinedPools) {
        int ret;
        ret = conn->storageDriver->numOfDefinedPools (conn);
        if (ret < 0)
            goto error;
        return ret;
    }
6614 6615

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6616 6617 6618 6619

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642
    return -1;
}


/**
 * virConnectListDefinedStoragePools:
 * @conn: pointer to hypervisor connection
 * @names: array of char * to fill with pool names (allocated by caller)
 * @maxnames: size of the names array
 *
 * Provides the list of names of inactive storage pools
 * upto maxnames. If there are more than maxnames, the
 * remaining names will be silently ignored.
 *
 * Returns 0 on success, -1 on error
 */
int
virConnectListDefinedStoragePools(virConnectPtr conn,
                                  char **const names,
                                  int maxnames)
{
    DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);

6643 6644
    virResetLastError();

6645 6646 6647 6648 6649 6650 6651
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if ((names == NULL) || (maxnames < 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6652
        goto error;
6653 6654
    }

6655 6656 6657 6658 6659 6660 6661
    if (conn->storageDriver && conn->storageDriver->listDefinedPools) {
        int ret;
        ret = conn->storageDriver->listDefinedPools (conn, names, maxnames);
        if (ret < 0)
            goto error;
        return ret;
    }
6662 6663

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6664 6665 6666 6667

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
6668 6669 6670 6671
    return -1;
}


6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698
/**
 * virConnectFindStoragePoolSources:
 * @conn: pointer to hypervisor connection
 * @type: type of storage pool sources to discover
 * @srcSpec: XML document specifying discovery source
 * @flags: flags for discovery (unused, pass 0)
 *
 * Talks to a storage backend and attempts to auto-discover the set of
 * available storage pool sources. e.g. For iSCSI this would be a set of
 * iSCSI targets. For NFS this would be a list of exported paths.  The
 * srcSpec (optional for some storage pool types, e.g. local ones) is
 * an instance of the storage pool's source element specifying where
 * to look for the pools.
 *
 * srcSpec is not required for some types (e.g., those querying
 * local storage resources only)
 *
 * Returns an xml document consisting of a SourceList element
 * containing a source document appropriate to the given pool
 * type for each discovered source.
 */
char *
virConnectFindStoragePoolSources(virConnectPtr conn,
                                 const char *type,
                                 const char *srcSpec,
                                 unsigned int flags)
{
6699 6700 6701 6702
    DEBUG("conn=%p, type=%s, src=%s, flags=%u", conn, type ? type : "", srcSpec ? srcSpec : "", flags);

    virResetLastError();

6703 6704
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
6705
        return NULL;
6706 6707 6708
    }
    if (type == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6709
        goto error;
6710 6711
    }

6712 6713
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
6714
        goto error;
6715 6716
    }

6717 6718 6719 6720 6721 6722 6723
    if (conn->storageDriver && conn->storageDriver->findPoolSources) {
        char *ret;
        ret = conn->storageDriver->findPoolSources(conn, type, srcSpec, flags);
        if (!ret)
            goto error;
        return ret;
    }
6724 6725

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6726 6727 6728 6729

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
6730 6731 6732 6733
    return NULL;
}


6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748
/**
 * virStoragePoolLookupByName:
 * @conn: pointer to hypervisor connection
 * @name: name of pool to fetch
 *
 * Fetch a storage pool based on its unique name
 *
 * Returns a virStoragePoolPtr object, or NULL if no matching pool is found
 */
virStoragePoolPtr
virStoragePoolLookupByName(virConnectPtr conn,
                           const char *name)
{
    DEBUG("conn=%p, name=%s", conn, name);

6749 6750
    virResetLastError();

6751 6752 6753 6754 6755 6756
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6757
        goto error;
6758 6759
    }

6760 6761 6762 6763 6764 6765 6766
    if (conn->storageDriver && conn->storageDriver->poolLookupByName) {
        virStoragePoolPtr ret;
        ret = conn->storageDriver->poolLookupByName (conn, name);
        if (!ret)
            goto error;
        return ret;
    }
6767 6768

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6769 6770 6771 6772

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791
    return NULL;
}


/**
 * virStoragePoolLookupByUUID:
 * @conn: pointer to hypervisor connection
 * @uuid: globally unique id of pool to fetch
 *
 * Fetch a storage pool based on its globally unique id
 *
 * Returns a virStoragePoolPtr object, or NULL if no matching pool is found
 */
virStoragePoolPtr
virStoragePoolLookupByUUID(virConnectPtr conn,
                           const unsigned char *uuid)
{
    DEBUG("conn=%p, uuid=%s", conn, uuid);

6792 6793
    virResetLastError();

6794 6795 6796 6797 6798 6799
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6800
        goto error;
6801 6802
    }

6803 6804 6805 6806 6807 6808 6809
    if (conn->storageDriver && conn->storageDriver->poolLookupByUUID) {
        virStoragePoolPtr ret;
        ret = conn->storageDriver->poolLookupByUUID (conn, uuid);
        if (!ret)
            goto error;
        return ret;
    }
6810 6811 6812

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

6813 6814 6815 6816
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830
}


/**
 * virStoragePoolLookupByUUIDString:
 * @conn: pointer to hypervisor connection
 * @uuidstr: globally unique id of pool to fetch
 *
 * Fetch a storage pool based on its globally unique id
 *
 * Returns a virStoragePoolPtr object, or NULL if no matching pool is found
 */
virStoragePoolPtr
virStoragePoolLookupByUUIDString(virConnectPtr conn,
6831
                                 const char *uuidstr)
6832 6833 6834 6835
{
    unsigned char uuid[VIR_UUID_BUFLEN];
    DEBUG("conn=%p, uuidstr=%s", conn, uuidstr);

6836 6837
    virResetLastError();

6838 6839 6840 6841 6842 6843
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (uuidstr == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6844
        goto error;
6845 6846 6847 6848
    }

    if (virUUIDParse(uuidstr, uuid) < 0) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6849
        goto error;
6850 6851 6852
    }

    return virStoragePoolLookupByUUID(conn, uuid);
6853 6854 6855 6856 6857

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873
}


/**
 * virStoragePoolLookupByVolume:
 * @vol: pointer to storage volume
 *
 * Fetch a storage pool which contains a particular volume
 *
 * Returns a virStoragePoolPtr object, or NULL if no matching pool is found
 */
virStoragePoolPtr
virStoragePoolLookupByVolume(virStorageVolPtr vol)
{
    DEBUG("vol=%p", vol);

6874 6875 6876
    virResetLastError();

    if (!VIR_IS_CONNECTED_STORAGE_VOL(vol)) {
6877 6878 6879 6880
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }

6881 6882 6883 6884 6885 6886 6887
    if (vol->conn->storageDriver && vol->conn->storageDriver->poolLookupByVolume) {
        virStoragePoolPtr ret;
        ret = vol->conn->storageDriver->poolLookupByVolume (vol);
        if (!ret)
            goto error;
        return ret;
    }
6888 6889 6890

    virLibConnError (vol->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

6891 6892 6893 6894
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(vol->conn);
    return NULL;
6895 6896 6897 6898 6899 6900
}

/**
 * virStoragePoolCreateXML:
 * @conn: pointer to hypervisor connection
 * @xmlDesc: XML description for new pool
6901
 * @flags: future flags, use 0 for now
6902 6903
 *
 * Create a new storage based on its XML description. The
D
Daniel Veillard 已提交
6904
 * pool is not persistent, so its definition will disappear
6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915
 * when it is destroyed, or if the host is restarted
 *
 * Returns a virStoragePoolPtr object, or NULL if creation failed
 */
virStoragePoolPtr
virStoragePoolCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
                        unsigned int flags)
{
    DEBUG("conn=%p, xmlDesc=%s", conn, xmlDesc);

6916 6917
    virResetLastError();

6918 6919 6920 6921 6922 6923
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6924
        goto error;
6925 6926 6927
    }
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
6928
        goto error;
6929 6930
    }

6931 6932 6933 6934 6935 6936 6937
    if (conn->storageDriver && conn->storageDriver->poolCreateXML) {
        virStoragePoolPtr ret;
        ret = conn->storageDriver->poolCreateXML (conn, xmlDesc, flags);
        if (!ret)
            goto error;
        return ret;
    }
6938 6939

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6940 6941 6942 6943

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
6944 6945 6946 6947 6948 6949 6950
    return NULL;
}

/**
 * virStoragePoolDefineXML:
 * @conn: pointer to hypervisor connection
 * @xml: XML description for new pool
6951
 * @flags: future flags, use 0 for now
6952 6953
 *
 * Define a new inactive storage pool based on its XML description. The
D
Daniel Veillard 已提交
6954
 * pool is persistent, until explicitly undefined.
6955 6956 6957 6958 6959 6960 6961 6962 6963 6964
 *
 * Returns a virStoragePoolPtr object, or NULL if creation failed
 */
virStoragePoolPtr
virStoragePoolDefineXML(virConnectPtr conn,
                        const char *xml,
                        unsigned int flags)
{
    DEBUG("conn=%p, xml=%s", conn, xml);

6965 6966
    virResetLastError();

6967 6968 6969 6970 6971 6972
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
6973
        goto error;
6974 6975 6976
    }
    if (xml == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6977
        goto error;
6978 6979
    }

6980 6981 6982 6983 6984 6985 6986
    if (conn->storageDriver && conn->storageDriver->poolDefineXML) {
        virStoragePoolPtr ret;
        ret = conn->storageDriver->poolDefineXML (conn, xml, flags);
        if (!ret)
            goto error;
        return ret;
    }
6987 6988 6989

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

6990 6991 6992 6993
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
6994 6995 6996 6997 6998
}

/**
 * virStoragePoolBuild:
 * @pool: pointer to storage pool
6999
 * @flags: future flags, use 0 for now
7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011
 *
 * Build the underlying storage pool
 *
 * Returns 0 on success, or -1 upon failure
 */
int
virStoragePoolBuild(virStoragePoolPtr pool,
                    unsigned int flags)
{
    virConnectPtr conn;
    DEBUG("pool=%p, flags=%u", pool, flags);

7012 7013
    virResetLastError();

7014 7015 7016 7017 7018 7019 7020
    if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
        virLibStoragePoolError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }
    conn = pool->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibStoragePoolError(pool, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7021
        goto error;
7022 7023
    }

7024 7025 7026 7027 7028 7029 7030
    if (conn->storageDriver && conn->storageDriver->poolBuild) {
        int ret;
        ret = conn->storageDriver->poolBuild (pool, flags);
        if (ret < 0)
            goto error;
        return ret;
    }
7031 7032 7033

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

7034 7035 7036 7037
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
    return -1;
7038 7039 7040 7041 7042 7043 7044 7045 7046
}


/**
 * virStoragePoolUndefine:
 * @pool: pointer to storage pool
 *
 * Undefine an inactive storage pool
 *
7047
 * Returns 0 on success, -1 on failure
7048 7049 7050 7051 7052 7053 7054
 */
int
virStoragePoolUndefine(virStoragePoolPtr pool)
{
    virConnectPtr conn;
    DEBUG("pool=%p", pool);

7055 7056
    virResetLastError();

7057 7058 7059 7060 7061 7062 7063
    if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
        virLibStoragePoolError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
        return (-1);
    }
    conn = pool->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibStoragePoolError(pool, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7064
        goto error;
7065 7066
    }

7067 7068 7069 7070 7071 7072 7073
    if (conn->storageDriver && conn->storageDriver->poolUndefine) {
        int ret;
        ret = conn->storageDriver->poolUndefine (pool);
        if (ret < 0)
            goto error;
        return ret;
    }
7074 7075 7076

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

7077 7078 7079 7080
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
    return -1;
7081 7082 7083 7084 7085 7086
}


/**
 * virStoragePoolCreate:
 * @pool: pointer to storage pool
7087
 * @flags: future flags, use 0 for now
7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099
 *
 * Starts an inactive storage pool
 *
 * Returns 0 on success, or -1 if it could not be started
 */
int
virStoragePoolCreate(virStoragePoolPtr pool,
                     unsigned int flags)
{
    virConnectPtr conn;
    DEBUG("pool=%p", pool);

7100 7101
    virResetLastError();

7102 7103 7104 7105 7106 7107 7108
    if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
        virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return (-1);
    }
    conn = pool->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibStoragePoolError(pool, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7109
        goto error;
7110 7111
    }

7112 7113 7114 7115 7116 7117 7118
    if (conn->storageDriver && conn->storageDriver->poolCreate) {
        int ret;
        ret = conn->storageDriver->poolCreate (pool, flags);
        if (ret < 0)
            goto error;
        return ret;
    }
7119 7120 7121

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

7122 7123 7124 7125
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
    return -1;
7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146
}


/**
 * virStoragePoolDestroy:
 * @pool: pointer to storage pool
 *
 * Destroy an active storage pool. This will deactivate the
 * pool on the host, but keep any persistent config associated
 * with it. If it has a persistent config it can later be
 * restarted with virStoragePoolCreate(). This does not free
 * the associated virStoragePoolPtr object.
 *
 * Returns 0 on success, or -1 if it could not be destroyed
 */
int
virStoragePoolDestroy(virStoragePoolPtr pool)
{
    virConnectPtr conn;
    DEBUG("pool=%p", pool);

7147 7148
    virResetLastError();

7149 7150 7151 7152 7153 7154 7155 7156
    if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
        virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return (-1);
    }

    conn = pool->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibStoragePoolError(pool, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7157
        goto error;
7158 7159
    }

7160 7161 7162 7163 7164 7165 7166
    if (conn->storageDriver && conn->storageDriver->poolDestroy) {
        int ret;
        ret = conn->storageDriver->poolDestroy (pool);
        if (ret < 0)
            goto error;
        return ret;
    }
7167 7168

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7169 7170 7171 7172

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193
    return -1;
}

/**
 * virStoragePoolDelete:
 * @pool: pointer to storage pool
 * @flags: flags for obliteration process
 *
 * Delete the underlying pool resources. This is
 * a non-recoverable operation. The virStoragePoolPtr object
 * itself is not free'd.
 *
 * Returns 0 on success, or -1 if it could not be obliterate
 */
int
virStoragePoolDelete(virStoragePoolPtr pool,
                     unsigned int flags)
{
    virConnectPtr conn;
    DEBUG("pool=%p, flags=%u", pool, flags);

7194 7195
    virResetLastError();

7196 7197 7198 7199 7200 7201 7202 7203
    if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
        virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return (-1);
    }

    conn = pool->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibStoragePoolError(pool, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7204
        goto error;
7205 7206
    }

7207 7208 7209 7210 7211 7212 7213
    if (conn->storageDriver && conn->storageDriver->poolDelete) {
        int ret;
        ret = conn->storageDriver->poolDelete (pool, flags);
        if (ret < 0)
            goto error;
        return ret;
    }
7214 7215

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7216 7217 7218 7219

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237
    return -1;
}


/**
 * virStoragePoolFree:
 * @pool: pointer to storage pool
 *
 * Free a storage pool object, releasing all memory associated with
 * it. Does not change the state of the pool on the host.
 *
 * Returns 0 on success, or -1 if it could not be free'd.
 */
int
virStoragePoolFree(virStoragePoolPtr pool)
{
    DEBUG("pool=%p", pool);

7238 7239 7240
    virResetLastError();

    if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
7241 7242 7243 7244 7245 7246 7247 7248 7249 7250
        virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return (-1);
    }
    if (virUnrefStoragePool(pool) < 0)
        return (-1);
    return(0);

}


7251 7252
/**
 * virStoragePoolRef:
D
Daniel Veillard 已提交
7253
 * @pool: the pool to hold a reference on
7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264
 *
 * Increment the reference count on the pool. For each
 * additional call to this method, there shall be a corresponding
 * call to virStoragePoolFree to release the reference count, once
 * the caller no longer needs the reference to this object.
 *
 * This method is typically useful for applications where multiple
 * threads are using a connection, and it is required that the
 * connection remain open until all threads have finished using
 * it. ie, each new thread using a pool would increment
 * the reference count.
D
Daniel Veillard 已提交
7265 7266
 *
 * Returns 0 in case of success, -1 in case of failure.
7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281
 */
int
virStoragePoolRef(virStoragePoolPtr pool)
{
    if ((!VIR_IS_CONNECTED_STORAGE_POOL(pool))) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }
    virMutexLock(&pool->conn->lock);
    DEBUG("pool=%p refs=%d", pool, pool->refs);
    pool->refs++;
    virMutexUnlock(&pool->conn->lock);
    return 0;
}

7282 7283 7284 7285 7286 7287 7288 7289 7290
/**
 * virStoragePoolRefresh:
 * @pool: pointer to storage pool
 * @flags: flags to control refresh behaviour (currently unused, use 0)
 *
 * Request that the pool refresh its list of volumes. This may
 * involve communicating with a remote server, and/or initializing
 * new devices at the OS layer
 *
7291
 * Returns 0 if the volume list was refreshed, -1 on failure
7292 7293 7294 7295 7296 7297 7298 7299
 */
int
virStoragePoolRefresh(virStoragePoolPtr pool,
                      unsigned int flags)
{
    virConnectPtr conn;
    DEBUG("pool=%p flags=%u", pool, flags);

7300 7301
    virResetLastError();

7302 7303 7304 7305 7306 7307 7308 7309
    if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
        virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return (-1);
    }

    conn = pool->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibStoragePoolError(pool, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7310
        goto error;
7311 7312
    }

7313 7314 7315 7316 7317 7318 7319
    if (conn->storageDriver && conn->storageDriver->poolRefresh) {
        int ret;
        ret = conn->storageDriver->poolRefresh (pool, flags);
        if (ret < 0)
            goto error;
        return ret;
    }
7320 7321

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7322 7323 7324 7325

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
7326 7327 7328 7329 7330 7331 7332 7333 7334 7335
    return -1;
}


/**
 * virStoragePoolGetName:
 * @pool: pointer to storage pool
 *
 * Fetch the locally unique name of the storage pool
 *
7336
 * Returns the name of the pool, or NULL on error
7337 7338 7339 7340 7341 7342
 */
const char*
virStoragePoolGetName(virStoragePoolPtr pool)
{
    DEBUG("pool=%p", pool);

7343 7344
    virResetLastError();

7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359
    if (!VIR_IS_STORAGE_POOL(pool)) {
        virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return (NULL);
    }
    return (pool->name);
}


/**
 * virStoragePoolGetUUID:
 * @pool: pointer to storage pool
 * @uuid: buffer of VIR_UUID_BUFLEN bytes in size
 *
 * Fetch the globally unique ID of the storage pool
 *
7360
 * Returns 0 on success, or -1 on error;
7361 7362 7363 7364 7365 7366 7367
 */
int
virStoragePoolGetUUID(virStoragePoolPtr pool,
                      unsigned char *uuid)
{
    DEBUG("pool=%p, uuid=%p", pool, uuid);

7368 7369
    virResetLastError();

7370 7371 7372 7373 7374 7375
    if (!VIR_IS_STORAGE_POOL(pool)) {
        virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return (-1);
    }
    if (uuid == NULL) {
        virLibStoragePoolError(pool, VIR_ERR_INVALID_ARG, __FUNCTION__);
7376
        goto error;
7377 7378 7379 7380 7381 7382
    }

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

    return (0);

7383 7384 7385 7386
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
    return -1;
7387 7388 7389 7390 7391 7392 7393 7394 7395
}

/**
 * virStoragePoolGetUUIDString:
 * @pool: pointer to storage pool
 * @buf: buffer of VIR_UUID_STRING_BUFLEN bytes in size
 *
 * Fetch the globally unique ID of the storage pool as a string
 *
7396
 * Returns 0 on success, or -1 on error;
7397 7398 7399 7400 7401 7402 7403 7404
 */
int
virStoragePoolGetUUIDString(virStoragePoolPtr pool,
                            char *buf)
{
    unsigned char uuid[VIR_UUID_BUFLEN];
    DEBUG("pool=%p, buf=%p", pool, buf);

7405 7406
    virResetLastError();

7407 7408 7409 7410 7411 7412
    if (!VIR_IS_STORAGE_POOL(pool)) {
        virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return (-1);
    }
    if (buf == NULL) {
        virLibStoragePoolError(pool, VIR_ERR_INVALID_ARG, __FUNCTION__);
7413
        goto error;
7414 7415 7416
    }

    if (virStoragePoolGetUUID(pool, &uuid[0]))
7417
        goto error;
7418 7419 7420 7421

    virUUIDFormat(uuid, buf);
    return (0);

7422 7423 7424 7425
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
    return -1;
7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436
}


/**
 * virStoragePoolGetInfo:
 * @pool: pointer to storage pool
 * @info: pointer at which to store info
 *
 * Get volatile information about the storage pool
 * such as free space / usage summary
 *
7437
 * Returns 0 on success, or -1 on failure.
7438 7439 7440 7441 7442 7443 7444 7445
 */
int
virStoragePoolGetInfo(virStoragePoolPtr pool,
                      virStoragePoolInfoPtr info)
{
    virConnectPtr conn;
    DEBUG("pool=%p, info=%p", pool, info);

7446 7447
    virResetLastError();

7448 7449 7450 7451 7452 7453
    if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
        virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return (-1);
    }
    if (info == NULL) {
        virLibStoragePoolError(pool, VIR_ERR_INVALID_ARG, __FUNCTION__);
7454
        goto error;
7455 7456 7457 7458 7459 7460
    }

    memset(info, 0, sizeof(virStoragePoolInfo));

    conn = pool->conn;

7461 7462 7463 7464 7465 7466 7467
    if (conn->storageDriver->poolGetInfo) {
        int ret;
        ret = conn->storageDriver->poolGetInfo (pool, info);
        if (ret < 0)
            goto error;
        return ret;
    }
7468 7469 7470

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

7471 7472 7473 7474
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
    return -1;
7475 7476 7477 7478 7479 7480
}


/**
 * virStoragePoolGetXMLDesc:
 * @pool: pointer to storage pool
7481
 * @flags: flags for XML format options (set of virDomainXMLFlags)
7482 7483 7484 7485 7486
 *
 * Fetch an XML document describing all aspects of the
 * storage pool. This is suitable for later feeding back
 * into the virStoragePoolCreateXML method.
 *
7487
 * Returns a XML document, or NULL on error
7488 7489 7490 7491 7492 7493 7494 7495
 */
char *
virStoragePoolGetXMLDesc(virStoragePoolPtr pool,
                         unsigned int flags)
{
    virConnectPtr conn;
    DEBUG("pool=%p, flags=%u", pool, flags);

7496 7497 7498
    virResetLastError();

    if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
7499 7500 7501 7502 7503
        virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return (NULL);
    }
    if (flags != 0) {
        virLibStoragePoolError(pool, VIR_ERR_INVALID_ARG, __FUNCTION__);
7504
        goto error;
7505 7506 7507 7508
    }

    conn = pool->conn;

7509 7510 7511 7512 7513 7514 7515
    if (conn->storageDriver && conn->storageDriver->poolGetXMLDesc) {
        char *ret;
        ret = conn->storageDriver->poolGetXMLDesc (pool, flags);
        if (!ret)
            goto error;
        return ret;
    }
7516 7517 7518

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

7519 7520 7521 7522
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
    return NULL;
7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533
}


/**
 * virStoragePoolGetAutostart:
 * @pool: pointer to storage pool
 * @autostart: location in which to store autostart flag
 *
 * Fetches the value of the autostart flag, which determines
 * whether the pool is automatically started at boot time
 *
7534
 * Returns 0 on success, -1 on failure
7535 7536 7537 7538 7539 7540 7541 7542
 */
int
virStoragePoolGetAutostart(virStoragePoolPtr pool,
                           int *autostart)
{
    virConnectPtr conn;
    DEBUG("pool=%p, autostart=%p", pool, autostart);

7543 7544 7545
    virResetLastError();

    if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
7546 7547 7548 7549 7550
        virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return (-1);
    }
    if (!autostart) {
        virLibStoragePoolError(pool, VIR_ERR_INVALID_ARG, __FUNCTION__);
7551
        goto error;
7552 7553 7554 7555
    }

    conn = pool->conn;

7556 7557 7558 7559 7560 7561 7562
    if (conn->storageDriver && conn->storageDriver->poolGetAutostart) {
        int ret;
        ret = conn->storageDriver->poolGetAutostart (pool, autostart);
        if (ret < 0)
            goto error;
        return ret;
    }
7563 7564

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7565 7566 7567 7568

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579
    return -1;
}


/**
 * virStoragePoolSetAutostart:
 * @pool: pointer to storage pool
 * @autostart: new flag setting
 *
 * Sets the autostart flag
 *
7580
 * Returns 0 on success, -1 on failure
7581 7582 7583 7584 7585 7586 7587 7588
 */
int
virStoragePoolSetAutostart(virStoragePoolPtr pool,
                           int autostart)
{
    virConnectPtr conn;
    DEBUG("pool=%p, autostart=%d", pool, autostart);

7589 7590 7591
    virResetLastError();

    if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
7592
        virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
7593
        return -1;
7594 7595
    }

7596 7597
    if (pool->conn->flags & VIR_CONNECT_RO) {
        virLibStoragePoolError(pool, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7598
        goto error;
7599 7600
    }

7601 7602
    conn = pool->conn;

7603 7604 7605 7606 7607 7608 7609
    if (conn->storageDriver && conn->storageDriver->poolSetAutostart) {
        int ret;
        ret = conn->storageDriver->poolSetAutostart (pool, autostart);
        if (ret < 0)
            goto error;
        return ret;
    }
7610 7611

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7612 7613 7614 7615

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632
    return -1;
}


/**
 * virStoragePoolNumOfVolumes:
 * @pool: pointer to storage pool
 *
 * Fetch the number of storage volumes within a pool
 *
 * Returns the number of storage pools, or -1 on failure
 */
int
virStoragePoolNumOfVolumes(virStoragePoolPtr pool)
{
    DEBUG("pool=%p", pool);

7633 7634
    virResetLastError();

7635 7636 7637 7638 7639
    if (!VIR_IS_STORAGE_POOL(pool)) {
        virLibConnError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return (-1);
    }

7640 7641 7642 7643 7644 7645 7646
    if (pool->conn->storageDriver && pool->conn->storageDriver->poolNumOfVolumes) {
        int ret;
        ret = pool->conn->storageDriver->poolNumOfVolumes (pool);
        if (ret < 0)
            goto error;
        return ret;
    }
7647 7648

    virLibConnError (pool->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7649 7650 7651 7652

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674
    return -1;
}


/**
 * virStoragePoolListVolumes:
 * @pool: pointer to storage pool
 * @names: array in which to storage volume names
 * @maxnames: size of names array
 *
 * Fetch list of storage volume names, limiting to
 * at most maxnames.
 *
 * Returns the number of names fetched, or -1 on error
 */
int
virStoragePoolListVolumes(virStoragePoolPtr pool,
                          char **const names,
                          int maxnames)
{
    DEBUG("pool=%p, names=%p, maxnames=%d", pool, names, maxnames);

7675 7676
    virResetLastError();

7677 7678 7679 7680 7681 7682 7683
    if (!VIR_IS_STORAGE_POOL(pool)) {
        virLibConnError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return (-1);
    }

    if ((names == NULL) || (maxnames < 0)) {
        virLibConnError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7684
        goto error;
7685 7686
    }

7687 7688 7689 7690 7691 7692 7693
    if (pool->conn->storageDriver && pool->conn->storageDriver->poolListVolumes) {
        int ret;
        ret = pool->conn->storageDriver->poolListVolumes (pool, names, maxnames);
        if (ret < 0)
            goto error;
        return ret;
    }
7694 7695

    virLibConnError (pool->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7696 7697 7698 7699

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
7700 7701 7702 7703 7704 7705
    return -1;
}


/**
 * virStorageVolGetConnect:
7706
 * @vol: pointer to a pool
7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722
 *
 * Provides the connection pointer associated with a storage volume.  The
 * reference counter on the connection is not increased by this
 * call.
 *
 * WARNING: When writing libvirt bindings in other languages, do
 * not use this function.  Instead, store the connection and
 * the volume object together.
 *
 * Returns the virConnectPtr or NULL in case of failure.
 */
virConnectPtr
virStorageVolGetConnect (virStorageVolPtr vol)
{
    DEBUG("vol=%p", vol);

7723 7724
    virResetLastError();

7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740
    if (!VIR_IS_STORAGE_VOL (vol)) {
        virLibStoragePoolError (NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
        return NULL;
    }
    return vol->conn;
}


/**
 * virStorageVolLookupByName:
 * @pool: pointer to storage pool
 * @name: name of storage volume
 *
 * Fetch a pointer to a storage volume based on its name
 * within a pool
 *
7741
 * Returns a storage volume, or NULL if not found / error
7742 7743 7744 7745 7746 7747 7748
 */
virStorageVolPtr
virStorageVolLookupByName(virStoragePoolPtr pool,
                          const char *name)
{
    DEBUG("pool=%p, name=%s", pool, name);

7749 7750
    virResetLastError();

7751 7752 7753 7754 7755 7756
    if (!VIR_IS_STORAGE_POOL(pool)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (name == NULL) {
        virLibConnError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7757
        goto error;
7758 7759
    }

7760 7761 7762 7763 7764 7765 7766
    if (pool->conn->storageDriver && pool->conn->storageDriver->volLookupByName) {
        virStorageVolPtr ret;
        ret = pool->conn->storageDriver->volLookupByName (pool, name);
        if (!ret)
            goto error;
        return ret;
    }
7767 7768

    virLibConnError (pool->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7769 7770 7771 7772

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785
    return NULL;
}



/**
 * virStorageVolLookupByKey:
 * @conn: pointer to hypervisor connection
 * @key: globally unique key
 *
 * Fetch a pointer to a storage volume based on its
 * globally unique key
 *
7786
 * Returns a storage volume, or NULL if not found / error
7787 7788 7789 7790 7791 7792 7793
 */
virStorageVolPtr
virStorageVolLookupByKey(virConnectPtr conn,
                         const char *key)
{
    DEBUG("conn=%p, key=%s", conn, key);

7794 7795
    virResetLastError();

7796 7797 7798 7799 7800 7801
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (key == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7802
        goto error;
7803 7804
    }

7805 7806 7807 7808 7809 7810 7811
    if (conn->storageDriver && conn->storageDriver->volLookupByKey) {
        virStorageVolPtr ret;
        ret = conn->storageDriver->volLookupByKey (conn, key);
        if (!ret)
            goto error;
        return ret;
    }
7812 7813

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7814 7815 7816 7817

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828
    return NULL;
}

/**
 * virStorageVolLookupByPath:
 * @conn: pointer to hypervisor connection
 * @path: locally unique path
 *
 * Fetch a pointer to a storage volume based on its
 * locally (host) unique path
 *
7829
 * Returns a storage volume, or NULL if not found / error
7830 7831 7832 7833 7834 7835 7836
 */
virStorageVolPtr
virStorageVolLookupByPath(virConnectPtr conn,
                          const char *path)
{
    DEBUG("conn=%p, path=%s", conn, path);

7837 7838
    virResetLastError();

7839 7840 7841 7842 7843 7844
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (path == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7845
        goto error;
7846 7847
    }

7848 7849 7850 7851 7852 7853 7854
    if (conn->storageDriver && conn->storageDriver->volLookupByPath) {
        virStorageVolPtr ret;
        ret = conn->storageDriver->volLookupByPath (conn, path);
        if (!ret)
            goto error;
        return ret;
    }
7855 7856

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7857 7858 7859 7860

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871
    return NULL;
}


/**
 * virStorageVolGetName:
 * @vol: pointer to storage volume
 *
 * Fetch the storage volume name. This is unique
 * within the scope of a pool
 *
7872
 * Returns the volume name, or NULL on error
7873 7874 7875 7876 7877 7878
 */
const char*
virStorageVolGetName(virStorageVolPtr vol)
{
    DEBUG("vol=%p", vol);

7879 7880
    virResetLastError();

7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893
    if (!VIR_IS_STORAGE_VOL(vol)) {
        virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
        return (NULL);
    }
    return (vol->name);
}


/**
 * virStorageVolGetKey:
 * @vol: pointer to storage volume
 *
 * Fetch the storage volume key. This is globally
7894
 * unique, so the same volume will have the same
7895 7896
 * key no matter what host it is accessed from
 *
7897
 * Returns the volume key, or NULL on error
7898 7899 7900 7901 7902 7903
 */
const char*
virStorageVolGetKey(virStorageVolPtr vol)
{
    DEBUG("vol=%p", vol);

7904 7905
    virResetLastError();

7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923
    if (!VIR_IS_STORAGE_VOL(vol)) {
        virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
        return (NULL);
    }
    return (vol->key);
}


/**
 * virStorageVolCreateXML:
 * @pool: pointer to storage pool
 * @xmldesc: description of volume to create
 * @flags: flags for creation (unused, pass 0)
 *
 * Create a storage volume within a pool based
 * on an XML description. Not all pools support
 * creation of volumes
 *
7924
 * Returns the storage volume, or NULL on error
7925 7926 7927 7928 7929 7930 7931 7932
 */
virStorageVolPtr
virStorageVolCreateXML(virStoragePoolPtr pool,
                       const char *xmldesc,
                       unsigned int flags)
{
    DEBUG("pool=%p, flags=%u", pool, flags);

7933 7934
    virResetLastError();

7935
    if (!VIR_IS_STORAGE_POOL(pool)) {
7936
        virLibConnError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
7937 7938 7939 7940 7941
        return (NULL);
    }

    if (pool->conn->flags & VIR_CONNECT_RO) {
        virLibConnError(pool->conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7942
        goto error;
7943 7944
    }

7945 7946 7947 7948 7949 7950 7951
    if (pool->conn->storageDriver && pool->conn->storageDriver->volCreateXML) {
        virStorageVolPtr ret;
        ret = pool->conn->storageDriver->volCreateXML (pool, xmldesc, flags);
        if (!ret)
            goto error;
        return ret;
    }
7952 7953

    virLibConnError (pool->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7954 7955 7956 7957

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
7958 7959 7960 7961
    return NULL;
}


7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973
/**
 * virStorageVolCreateXMLFrom:
 * @pool: pointer to parent pool for the new volume
 * @xmldesc: description of volume to create
 * @clonevol: storage volume to use as input
 * @flags: flags for creation (unused, pass 0)
 *
 * Create a storage volume in the parent pool, using the
 * 'clonevol' volume as input. Information for the new
 * volume (name, perms)  are passed via a typical volume
 * XML description.
 *
7974
 * Returns the storage volume, or NULL on error
7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020
 */
virStorageVolPtr
virStorageVolCreateXMLFrom(virStoragePoolPtr pool,
                           const char *xmldesc,
                           virStorageVolPtr clonevol,
                           unsigned int flags)
{
    DEBUG("pool=%p, flags=%u, clonevol=%p", pool, flags, clonevol);

    virResetLastError();

    if (!VIR_IS_STORAGE_POOL(pool)) {
        virLibConnError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
        return (NULL);
    }

    if (!VIR_IS_STORAGE_VOL(clonevol)) {
        virLibConnError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
        return (NULL);
    }

    if (pool->conn->flags & VIR_CONNECT_RO ||
        clonevol->conn->flags & VIR_CONNECT_RO) {
        virLibConnError(pool->conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        goto error;
    }

    if (pool->conn->storageDriver &&
        pool->conn->storageDriver->volCreateXMLFrom) {
        virStorageVolPtr ret;
        ret = pool->conn->storageDriver->volCreateXMLFrom (pool, xmldesc,
                                                           clonevol, flags);
        if (!ret)
            goto error;
        return ret;
    }

    virLibConnError (pool->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(pool->conn);
    return NULL;
}


8021 8022 8023
/**
 * virStorageVolDelete:
 * @vol: pointer to storage volume
8024
 * @flags: future flags, use 0 for now
8025 8026 8027
 *
 * Delete the storage volume from the pool
 *
8028
 * Returns 0 on success, or -1 on error
8029 8030 8031 8032 8033 8034 8035 8036
 */
int
virStorageVolDelete(virStorageVolPtr vol,
                    unsigned int flags)
{
    virConnectPtr conn;
    DEBUG("vol=%p, flags=%u", vol, flags);

8037 8038
    virResetLastError();

8039 8040 8041 8042 8043 8044 8045 8046
    if (!VIR_IS_CONNECTED_STORAGE_VOL(vol)) {
        virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
        return (-1);
    }

    conn = vol->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibStorageVolError(vol, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
8047
        goto error;
8048 8049
    }

8050 8051 8052 8053 8054 8055 8056
    if (conn->storageDriver && conn->storageDriver->volDelete) {
        int ret;
        ret = conn->storageDriver->volDelete (vol, flags);
        if (ret < 0)
            goto error;
        return ret;
    }
8057 8058

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8059 8060 8061 8062

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(vol->conn);
8063 8064 8065 8066 8067 8068 8069 8070 8071
    return -1;
}


/**
 * virStorageVolFree:
 * @vol: pointer to storage volume
 *
 * Release the storage volume handle. The underlying
8072
 * storage volume continues to exist.
8073
 *
8074
 * Returns 0 on success, or -1 on error
8075 8076 8077 8078 8079 8080
 */
int
virStorageVolFree(virStorageVolPtr vol)
{
    DEBUG("vol=%p", vol);

8081 8082
    virResetLastError();

8083 8084 8085 8086 8087 8088 8089 8090 8091 8092
    if (!VIR_IS_STORAGE_VOL(vol)) {
        virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
        return (-1);
    }
    if (virUnrefStorageVol(vol) < 0)
        return (-1);
    return(0);
}


8093 8094
/**
 * virStorageVolRef:
D
Daniel Veillard 已提交
8095
 * @vol: the vol to hold a reference on
8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106
 *
 * Increment the reference count on the vol. For each
 * additional call to this method, there shall be a corresponding
 * call to virStorageVolFree to release the reference count, once
 * the caller no longer needs the reference to this object.
 *
 * This method is typically useful for applications where multiple
 * threads are using a connection, and it is required that the
 * connection remain open until all threads have finished using
 * it. ie, each new thread using a vol would increment
 * the reference count.
D
Daniel Veillard 已提交
8107 8108
 *
 * Returns 0 in case of success, -1 in case of failure.
8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123
 */
int
virStorageVolRef(virStorageVolPtr vol)
{
    if ((!VIR_IS_CONNECTED_STORAGE_VOL(vol))) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }
    virMutexLock(&vol->conn->lock);
    DEBUG("vol=%p refs=%d", vol, vol->refs);
    vol->refs++;
    virMutexUnlock(&vol->conn->lock);
    return 0;
}

8124 8125 8126 8127 8128 8129 8130 8131
/**
 * virStorageVolGetInfo:
 * @vol: pointer to storage volume
 * @info: pointer at which to store info
 *
 * Fetches volatile information about the storage
 * volume such as its current allocation
 *
8132
 * Returns 0 on success, or -1 on failure
8133 8134 8135 8136 8137 8138 8139 8140
 */
int
virStorageVolGetInfo(virStorageVolPtr vol,
                     virStorageVolInfoPtr info)
{
    virConnectPtr conn;
    DEBUG("vol=%p, info=%p", vol, info);

8141 8142
    virResetLastError();

8143 8144 8145 8146 8147 8148
    if (!VIR_IS_CONNECTED_STORAGE_VOL(vol)) {
        virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
        return (-1);
    }
    if (info == NULL) {
        virLibStorageVolError(vol, VIR_ERR_INVALID_ARG, __FUNCTION__);
8149
        goto error;
8150 8151 8152 8153 8154 8155
    }

    memset(info, 0, sizeof(virStorageVolInfo));

    conn = vol->conn;

8156 8157 8158 8159 8160 8161 8162
    if (conn->storageDriver->volGetInfo){
        int ret;
        ret = conn->storageDriver->volGetInfo (vol, info);
        if (ret < 0)
            goto error;
        return ret;
    }
8163 8164

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8165 8166 8167 8168

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(vol->conn);
8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180
    return -1;
}


/**
 * virStorageVolGetXMLDesc:
 * @vol: pointer to storage volume
 * @flags: flags for XML generation (unused, pass 0)
 *
 * Fetch an XML document describing all aspects of
 * the storage volume
 *
8181
 * Returns the XML document, or NULL on error
8182 8183 8184 8185 8186 8187 8188 8189
 */
char *
virStorageVolGetXMLDesc(virStorageVolPtr vol,
                        unsigned int flags)
{
    virConnectPtr conn;
    DEBUG("vol=%p, flags=%u", vol, flags);

8190 8191
    virResetLastError();

8192 8193 8194 8195 8196 8197
    if (!VIR_IS_STORAGE_VOL(vol)) {
        virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
        return (NULL);
    }
    if (flags != 0) {
        virLibStorageVolError(vol, VIR_ERR_INVALID_ARG, __FUNCTION__);
8198
        goto error;
8199 8200 8201 8202
    }

    conn = vol->conn;

8203 8204 8205 8206 8207 8208 8209
    if (conn->storageDriver && conn->storageDriver->volGetXMLDesc) {
        char *ret;
        ret = conn->storageDriver->volGetXMLDesc (vol, flags);
        if (!ret)
            goto error;
        return ret;
    }
8210 8211 8212

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

8213 8214 8215 8216
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(vol->conn);
    return NULL;
8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237
}


/**
 * virStorageVolGetPath:
 * @vol: pointer to storage volume
 *
 * Fetch the storage volume path. Depending on the pool
 * configuration this is either persistent across hosts,
 * or dynamically assigned at pool startup. Consult
 * pool documentation for information on getting the
 * persistent naming
 *
 * Returns the storage volume path, or NULL on error
 */
char *
virStorageVolGetPath(virStorageVolPtr vol)
{
    virConnectPtr conn;
    DEBUG("vol=%p", vol);

8238 8239
    virResetLastError();

8240 8241 8242 8243 8244 8245 8246
    if (!VIR_IS_STORAGE_VOL(vol)) {
        virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
        return (NULL);
    }

    conn = vol->conn;

8247 8248 8249 8250 8251 8252 8253
    if (conn->storageDriver && conn->storageDriver->volGetPath) {
        char *ret;
        ret = conn->storageDriver->volGetPath (vol);
        if (!ret)
            goto error;
        return ret;
    }
8254 8255

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8256 8257 8258 8259

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(vol->conn);
8260 8261
    return NULL;
}
8262 8263


8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279
/**
 * virNodeNumOfDevices:
 * @conn: pointer to the hypervisor connection
 * @cap: capability name
 * @flags: flags (unused, pass 0)
 *
 * Provides the number of node devices.
 *
 * If the optional 'cap'  argument is non-NULL, then the count
 * will be restricted to devices with the specified capability
 *
 * Returns the number of node devices or -1 in case of error
 */
int
virNodeNumOfDevices(virConnectPtr conn, const char *cap, unsigned int flags)
{
J
John Levon 已提交
8280
    DEBUG("conn=%p, cap=%s, flags=%d", conn, NULLSTR(cap), flags);
8281

8282 8283
    virResetLastError();

8284 8285 8286 8287 8288 8289
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }
    if (flags != 0) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
8290
        goto error;
8291 8292
    }

8293 8294 8295 8296 8297 8298 8299
    if (conn->deviceMonitor && conn->deviceMonitor->numOfDevices) {
        int ret;
        ret = conn->deviceMonitor->numOfDevices (conn, cap, flags);
        if (ret < 0)
            goto error;
        return ret;
    }
8300 8301

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8302 8303 8304 8305

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333
    return -1;
}


/**
 * virNodeListDevices:
 * @conn: pointer to the hypervisor connection
 * @cap: capability name
 * @names: array to collect the list of node device names
 * @maxnames: size of @names
 * @flags: flags (unused, pass 0)
 *
 * Collect the list of node devices, and store their names in @names
 *
 * If the optional 'cap'  argument is non-NULL, then the count
 * will be restricted to devices with the specified capability
 *
 * Returns the number of node devices found or -1 in case of error
 */
int
virNodeListDevices(virConnectPtr conn,
                   const char *cap,
                   char **const names, int maxnames,
                   unsigned int flags)
{
    DEBUG("conn=%p, cap=%s, names=%p, maxnames=%d, flags=%d",
          conn, cap, names, maxnames, flags);

8334 8335
    virResetLastError();

8336 8337 8338 8339 8340 8341
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }
    if ((flags != 0) || (names == NULL) || (maxnames < 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
8342
        goto error;
8343 8344
    }

8345 8346 8347 8348 8349 8350 8351
    if (conn->deviceMonitor && conn->deviceMonitor->listDevices) {
        int ret;
        ret = conn->deviceMonitor->listDevices (conn, cap, names, maxnames, flags);
        if (ret < 0)
            goto error;
        return ret;
    }
8352 8353

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8354 8355 8356 8357

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374
    return -1;
}


/**
 * virNodeDeviceLookupByName:
 * @conn: pointer to the hypervisor connection
 * @name: unique device name
 *
 * Lookup a node device by its name.
 *
 * Returns a virNodeDevicePtr if found, NULL otherwise.
 */
virNodeDevicePtr virNodeDeviceLookupByName(virConnectPtr conn, const char *name)
{
    DEBUG("conn=%p, name=%p", conn, name);

8375 8376
    virResetLastError();

8377 8378 8379 8380 8381 8382 8383
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return NULL;
    }

    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
8384
        goto error;
8385 8386
    }

8387 8388 8389 8390 8391 8392 8393
    if (conn->deviceMonitor && conn->deviceMonitor->deviceLookupByName) {
        virNodeDevicePtr ret;
        ret = conn->deviceMonitor->deviceLookupByName (conn, name);
        if (!ret)
            goto error;
        return ret;
    }
8394 8395

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8396 8397 8398 8399

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411
    return NULL;
}


/**
 * virNodeDeviceGetXMLDesc:
 * @dev: pointer to the node device
 * @flags: flags for XML generation (unused, pass 0)
 *
 * Fetch an XML document describing all aspects of
 * the device.
 *
8412
 * Returns the XML document, or NULL on error
8413 8414 8415 8416 8417
 */
char *virNodeDeviceGetXMLDesc(virNodeDevicePtr dev, unsigned int flags)
{
    DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);

8418 8419
    virResetLastError();

8420 8421 8422 8423 8424
    if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
        virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
        return NULL;
    }

8425 8426 8427 8428 8429 8430 8431
    if (dev->conn->deviceMonitor && dev->conn->deviceMonitor->deviceDumpXML) {
        char *ret;
        ret = dev->conn->deviceMonitor->deviceDumpXML (dev, flags);
        if (!ret)
            goto error;
        return ret;
    }
8432 8433

    virLibConnError (dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8434 8435 8436 8437

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dev->conn);
8438 8439 8440 8441 8442 8443 8444 8445
    return NULL;
}


/**
 * virNodeDeviceGetName:
 * @dev: the device
 *
D
Daniel Veillard 已提交
8446 8447 8448
 * Just return the device name
 *
 * Returns the device name or NULL in case of error
8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465
 */
const char *virNodeDeviceGetName(virNodeDevicePtr dev)
{
    DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);

    if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
        virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
        return NULL;
    }

    return dev->name;
}

/**
 * virNodeDeviceGetParent:
 * @dev: the device
 *
D
Daniel Veillard 已提交
8466 8467
 * Accessor for the parent of the device
 *
8468 8469 8470 8471 8472 8473 8474
 * Returns the name of the device's parent, or NULL if the
 * device has no parent.
 */
const char *virNodeDeviceGetParent(virNodeDevicePtr dev)
{
    DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);

8475 8476
    virResetLastError();

8477 8478 8479 8480 8481
    if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
        virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
        return NULL;
    }

8482 8483 8484 8485 8486
    if (!dev->parent) {
        if (dev->conn->deviceMonitor && dev->conn->deviceMonitor->deviceGetParent) {
            dev->parent = dev->conn->deviceMonitor->deviceGetParent (dev);
        } else {
            virLibConnError (dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8487
            virSetConnError(dev->conn);
8488 8489 8490 8491
            return NULL;
        }
    }
    return dev->parent;
8492 8493 8494 8495 8496 8497
}

/**
 * virNodeDeviceNumOfCaps:
 * @dev: the device
 *
D
Daniel Veillard 已提交
8498 8499
 * Accessor for the number of capabilities supported by the device.
 *
8500 8501 8502 8503 8504 8505
 * Returns the number of capabilities supported by the device.
 */
int virNodeDeviceNumOfCaps(virNodeDevicePtr dev)
{
    DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);

8506 8507
    virResetLastError();

8508 8509 8510 8511 8512
    if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
        virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
        return -1;
    }

8513 8514 8515 8516 8517 8518 8519
    if (dev->conn->deviceMonitor && dev->conn->deviceMonitor->deviceNumOfCaps) {
        int ret;
        ret = dev->conn->deviceMonitor->deviceNumOfCaps (dev);
        if (ret < 0)
            goto error;
        return ret;
    }
8520 8521

    virLibConnError (dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8522 8523 8524 8525

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dev->conn);
8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545
    return -1;
}

/**
 * virNodeDeviceListCaps:
 * @dev: the device
 * @names: array to collect the list of capability names
 * @maxnames: size of @names
 *
 * Lists the names of the capabilities supported by the device.
 *
 * Returns the number of capability names listed in @names.
 */
int virNodeDeviceListCaps(virNodeDevicePtr dev,
                          char **const names,
                          int maxnames)
{
    DEBUG("dev=%p, conn=%p, names=%p, maxnames=%d",
          dev, dev ? dev->conn : NULL, names, maxnames);

8546 8547
    virResetLastError();

8548 8549 8550 8551 8552
    if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
        virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
        return -1;
    }

8553 8554 8555 8556 8557 8558 8559
    if (dev->conn->deviceMonitor && dev->conn->deviceMonitor->deviceListCaps) {
        int ret;
        ret = dev->conn->deviceMonitor->deviceListCaps (dev, names, maxnames);
        if (ret < 0)
            goto error;
        return ret;
    }
8560 8561

    virLibConnError (dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8562 8563 8564 8565

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dev->conn);
8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582
    return -1;
}


/**
 * virNodeDeviceFree:
 * @dev: pointer to the node device
 *
 * Drops a reference to the node device, freeing it if
 * this was the last reference.
 *
 * Returns the 0 for success, -1 for error.
 */
int virNodeDeviceFree(virNodeDevicePtr dev)
{
    DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);

8583 8584
    virResetLastError();

8585 8586 8587 8588 8589 8590 8591 8592 8593 8594
    if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
        virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
        return (-1);
    }
    if (virUnrefNodeDevice(dev) < 0)
        return (-1);
    return(0);
}


8595 8596
/**
 * virNodeDeviceRef:
D
Daniel Veillard 已提交
8597
 * @dev: the dev to hold a reference on
8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608
 *
 * Increment the reference count on the dev. For each
 * additional call to this method, there shall be a corresponding
 * call to virNodeDeviceFree to release the reference count, once
 * the caller no longer needs the reference to this object.
 *
 * This method is typically useful for applications where multiple
 * threads are using a connection, and it is required that the
 * connection remain open until all threads have finished using
 * it. ie, each new thread using a dev would increment
 * the reference count.
D
Daniel Veillard 已提交
8609 8610
 *
 * Returns 0 in case of success, -1 in case of failure.
8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625
 */
int
virNodeDeviceRef(virNodeDevicePtr dev)
{
    if ((!VIR_IS_CONNECTED_NODE_DEVICE(dev))) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }
    virMutexLock(&dev->conn->lock);
    DEBUG("dev=%p refs=%d", dev, dev->refs);
    dev->refs++;
    virMutexUnlock(&dev->conn->lock);
    return 0;
}

8626
/**
D
Daniel Veillard 已提交
8627
 * virNodeDeviceDettach:
8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641
 * @dev: pointer to the node device
 *
 * Dettach the node device from the node itself so that it may be
 * assigned to a guest domain.
 *
 * Depending on the hypervisor, this may involve operations such
 * as unbinding any device drivers from the device, binding the
 * device to a dummy device driver and resetting the device.
 *
 * If the device is currently in use by the node, this method may
 * fail.
 *
 * Once the device is not assigned to any guest, it may be re-attached
 * to the node using the virNodeDeviceReattach() method.
D
Daniel Veillard 已提交
8642 8643
 *
 * Returns 0 in case of success, -1 in case of failure.
8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684
 */
int
virNodeDeviceDettach(virNodeDevicePtr dev)
{
    DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);

    virResetLastError();

    if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
        virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
        return (-1);
    }

    if (dev->conn->driver->nodeDeviceDettach) {
        int ret;
        ret = dev->conn->driver->nodeDeviceDettach (dev);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError(dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dev->conn);
    return (-1);
}

/**
 * virNodeDeviceReAttach:
 * @dev: pointer to the node device
 *
 * Re-attach a previously dettached node device to the node so that it
 * may be used by the node again.
 *
 * Depending on the hypervisor, this may involve operations such
 * as resetting the device, unbinding it from a dummy device driver
 * and binding it to its appropriate driver.
 *
 * If the device is currently in use by a guest, this method may fail.
D
Daniel Veillard 已提交
8685 8686
 *
 * Returns 0 in case of success, -1 in case of failure.
8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729
 */
int
virNodeDeviceReAttach(virNodeDevicePtr dev)
{
    DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);

    virResetLastError();

    if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
        virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
        return (-1);
    }

    if (dev->conn->driver->nodeDeviceReAttach) {
        int ret;
        ret = dev->conn->driver->nodeDeviceReAttach (dev);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError(dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dev->conn);
    return (-1);
}

/**
 * virNodeDeviceReset:
 * @dev: pointer to the node device
 *
 * Reset a previously dettached node device to the node before or
 * after assigning it to a guest.
 *
 * The exact reset semantics depends on the hypervisor and device
 * type but, for example, KVM will attempt to reset PCI devices with
 * a Function Level Reset, Secondary Bus Reset or a Power Management
 * D-State reset.
 *
 * If the reset will affect other devices which are currently in use,
 * this function may fail.
D
Daniel Veillard 已提交
8730 8731
 *
 * Returns 0 in case of success, -1 in case of failure.
8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760
 */
int
virNodeDeviceReset(virNodeDevicePtr dev)
{
    DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);

    virResetLastError();

    if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
        virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
        return (-1);
    }

    if (dev->conn->driver->nodeDeviceReset) {
        int ret;
        ret = dev->conn->driver->nodeDeviceReset (dev);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError(dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dev->conn);
    return (-1);
}

8761

8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858
/**
 * virNodeDeviceCreateXML:
 * @conn: pointer to the hypervisor connection
 * @xmlDesc: string containing an XML description of the device to be created
 * @flags: callers should always pass 0
 *
 * Create a new device on the VM host machine, for example, virtual
 * HBAs created using vport_create.
 *
 * Returns a node device object if successful, NULL in case of failure
 */
virNodeDevicePtr
virNodeDeviceCreateXML(virConnectPtr conn,
                       const char *xmlDesc,
                       unsigned int flags)
{
    VIR_DEBUG("conn=%p, xmlDesc=%s, flags=%d", conn, xmlDesc, flags);

    virResetLastError();

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

    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        goto error;
    }

    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

    if (conn->deviceMonitor &&
        conn->deviceMonitor->deviceCreateXML) {
        virNodeDevicePtr dev = conn->deviceMonitor->deviceCreateXML(conn, xmlDesc, flags);
        if (dev == NULL)
            goto error;
        return dev;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
}


/**
 * virNodeDeviceDestroy:
 * @dev: a device object
 *
 * Destroy the device object. The virtual device is removed from the host operating system.
 * This function may require privileged access
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virNodeDeviceDestroy(virNodeDevicePtr dev)
{
    DEBUG("dev=%p", dev);

    virResetLastError();

    if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
        virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
        return (-1);
    }

    if (dev->conn->flags & VIR_CONNECT_RO) {
        virLibConnError(dev->conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        goto error;
    }

    if (dev->conn->deviceMonitor &&
        dev->conn->deviceMonitor->deviceDestroy) {
        int retval = dev->conn->deviceMonitor->deviceDestroy(dev);
        if (retval < 0) {
            goto error;
        }

        return 0;
    }

    virLibConnError (dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(dev->conn);
    return -1;
}


8859 8860 8861 8862 8863 8864 8865 8866 8867
/*
 * Domain Event Notification
 */

/**
 * virConnectDomainEventRegister:
 * @conn: pointer to the connection
 * @cb: callback to the function handling domain events
 * @opaque: opaque data to pass on to the callback
D
Daniel Veillard 已提交
8868
 * @freecb: optional function to deallocate opaque when not used anymore
8869 8870 8871 8872
 *
 * Adds a Domain Event Callback.
 * Registering for a domain callback will enable delivery of the events
 *
8873 8874 8875 8876 8877 8878 8879
 * The virDomainPtr object handle passed into the callback upon delivery
 * of an event is only valid for the duration of execution of the callback.
 * If the callback wishes to keep the domain object after the callback
 * returns, it shall take a reference to it, by calling virDomainRef.
 * The reference can be released once the object is no longer required
 * by calling virDomainFree.
 *
8880 8881 8882 8883 8884
 * Returns 0 on success, -1 on failure
 */
int
virConnectDomainEventRegister(virConnectPtr conn,
                              virConnectDomainEventCallback cb,
8885 8886
                              void *opaque,
                              virFreeCallback freecb)
8887
{
8888 8889
    DEBUG("conn=%p, cb=%p, opaque=%p, freecb=%p", conn, cb, opaque, freecb);
    virResetLastError();
8890 8891 8892 8893 8894 8895 8896

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }
    if (cb == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
8897
        goto error;
8898 8899
    }

8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911
    if ((conn->driver) && (conn->driver->domainEventRegister)) {
        int ret;
        ret = conn->driver->domainEventRegister (conn, cb, opaque, freecb);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929
    return -1;
}

/**
 * virConnectDomainEventDeregister:
 * @conn: pointer to the connection
 * @cb: callback to the function handling domain events
 *
 * Removes a Domain Event Callback.
 * De-registering for a domain callback will disable
 * delivery of this event type
 *
 * Returns 0 on success, -1 on failure
 */
int
virConnectDomainEventDeregister(virConnectPtr conn,
                                virConnectDomainEventCallback cb)
{
8930 8931 8932
    DEBUG("conn=%p, cb=%p", conn, cb);

    virResetLastError();
8933 8934 8935 8936 8937 8938 8939

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }
    if (cb == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
8940 8941 8942 8943 8944 8945 8946 8947
        goto error;
    }
    if ((conn->driver) && (conn->driver->domainEventDeregister)) {
        int ret;
        ret = conn->driver->domainEventDeregister (conn, cb);
        if (ret < 0)
            goto error;
        return ret;
8948 8949
    }

8950 8951 8952 8953
    virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
8954 8955
    return -1;
}
8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064

/**
 * virSecretGetConnect:
 * @secret: A virSecret secret
 *
 * Provides the connection pointer associated with a secret.  The reference
 * counter on the connection is not increased by this call.
 *
 * WARNING: When writing libvirt bindings in other languages, do not use this
 * function.  Instead, store the connection and the secret object together.
 *
 * Returns the virConnectPtr or NULL in case of failure.
 */
virConnectPtr
virSecretGetConnect (virSecretPtr secret)
{
    DEBUG("secret=%p", secret);

    virResetLastError();

    if (!VIR_IS_CONNECTED_SECRET (secret)) {
        virLibSecretError (NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
        return NULL;
    }
    return secret->conn;
}

/**
 * virConnectNumOfSecrets:
 * @conn: virConnect connection
 *
 * Fetch number of currently defined secrets.
 *
 * Returns the number currently defined secrets.
 */
int
virConnectNumOfSecrets(virConnectPtr conn)
{
    VIR_DEBUG("conn=%p", conn);

    virResetLastError();

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

    if (conn->secretDriver != NULL &&
        conn->secretDriver->numOfSecrets != NULL) {
        int ret;

        ret = conn->secretDriver->numOfSecrets(conn);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return -1;
}

/**
 * virConnectListSecrets:
 * @conn: virConnect connection
 * @uuids: Pointer to an array to store the UUIDs
 * @maxuuids: size of the array.
 *
 * List UUIDs of defined secrets, store pointers to names in uuids.
 *
 * Returns the number of UUIDs provided in the array, or -1 on failure.
 */
int
virConnectListSecrets(virConnectPtr conn, char **uuids, int maxuuids)
{
    VIR_DEBUG("conn=%p, uuids=%p, maxuuids=%d", conn, uuids, maxuuids);

    virResetLastError();

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return -1;
    }
    if (uuids == NULL || maxuuids < 0) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

    if (conn->secretDriver != NULL && conn->secretDriver->listSecrets != NULL) {
        int ret;

        ret = conn->secretDriver->listSecrets(conn, uuids, maxuuids);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return -1;
}

/**
9065 9066 9067
 * virSecretLookupByUUID:
 * @conn: pointer to the hypervisor connection
 * @uuid: the raw UUID for the secret
9068
 *
9069 9070
 * Try to lookup a secret on the given hypervisor based on its UUID.
 * Uses the 16 bytes of raw data to describe the UUID
9071
 *
9072 9073
 * Returns a new secret object or NULL in case of failure.  If the
 * secret cannot be found, then VIR_ERR_NO_SECRET error is raised.
9074 9075
 */
virSecretPtr
9076
virSecretLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
9077
{
9078
    DEBUG("conn=%p, uuid=%s", conn, uuid);
9079 9080 9081 9082 9083

    virResetLastError();

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
9084
        return (NULL);
9085 9086 9087 9088 9089 9090
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

9091 9092
    if (conn->secretDriver &&
        conn->secretDriver->lookupByUUID) {
9093
        virSecretPtr ret;
9094 9095
        ret = conn->secretDriver->lookupByUUID (conn, uuid);
        if (!ret)
9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
}

9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134
/**
 * virSecretLookupByUUIDString:
 * @conn: pointer to the hypervisor connection
 * @uuidstr: the string UUID for the secret
 *
 * Try to lookup a secret on the given hypervisor based on its UUID.
 * Uses the printable string value to describe the UUID
 *
 * Returns a new secret object or NULL in case of failure.  If the
 * secret cannot be found, then VIR_ERR_NO_SECRET error is raised.
 */
virSecretPtr
virSecretLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
{
    unsigned char uuid[VIR_UUID_BUFLEN];
    DEBUG("conn=%p, uuidstr=%s", conn, uuidstr);

    virResetLastError();

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

    if (virUUIDParse(uuidstr, uuid) < 0) {
9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

    return virSecretLookupByUUID(conn, &uuid[0]);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
}


9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198
/**
 * virSecretLookupByUsage:
 * @conn: pointer to the hypervisor connection
 * @usageType: the type of secret usage
 * @usageID: identifier of the object using the secret
 *
 * Try to lookup a secret on the given hypervisor based on its usage
 * The usageID is unique within the set of secrets sharing the
 * same usageType value.
 *
 * Returns a new secret object or NULL in case of failure.  If the
 * secret cannot be found, then VIR_ERR_NO_SECRET error is raised.
 */
virSecretPtr
virSecretLookupByUsage(virConnectPtr conn,
                       int usageType,
                       const char *usageID)
{
    DEBUG("conn=%p, usageType=%d usageID=%s", conn, usageType, NULLSTR(usageID));

    virResetLastError();

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

    if (conn->secretDriver &&
        conn->secretDriver->lookupByUsage) {
        virSecretPtr ret;
        ret = conn->secretDriver->lookupByUsage (conn, usageType, usageID);
        if (!ret)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
}


9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251
/**
 * virSecretDefineXML:
 * @conn: virConnect connection
 * @xml: XML describing the secret.
 * @flags: flags, use 0 for now
 *
 * If XML specifies an UUID, locates the specified secret and replaces all
 * attributes of the secret specified by UUID by attributes specified in xml
 * (any attributes not specified in xml are discarded).
 *
 * Otherwise, creates a new secret with an automatically chosen UUID, and
 * initializes its attributes from xml.
 *
 * Returns a the secret on success, NULL on failure.
 */
virSecretPtr
virSecretDefineXML(virConnectPtr conn, const char *xml, unsigned int flags)
{
    VIR_DEBUG("conn=%p, xml=%s, flags=%u", conn, xml, flags);

    virResetLastError();

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return NULL;
    }
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        goto error;
    }
    if (xml == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

    if (conn->secretDriver != NULL && conn->secretDriver->defineXML != NULL) {
        virSecretPtr ret;

        ret = conn->secretDriver->defineXML(conn, xml, flags);
        if (ret == NULL)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
}

/**
9252
 * virSecretGetUUID:
9253
 * @secret: A virSecret secret
9254
 * @uuid: buffer of VIR_UUID_BUFLEN bytes in size
9255 9256 9257
 *
 * Fetches the UUID of the secret.
 *
9258 9259
 * Returns 0 on success with the uuid buffer being filled, or
 * -1 upon failure.
9260
 */
9261 9262
int
virSecretGetUUID(virSecretPtr secret, unsigned char *uuid)
9263 9264 9265 9266 9267 9268 9269
{
    VIR_DEBUG("secret=%p", secret);

    virResetLastError();

    if (!VIR_IS_CONNECTED_SECRET(secret)) {
        virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
9270 9271 9272 9273 9274 9275 9276
        return -1;
    }
    if (uuid == NULL) {
        virLibSecretError(secret, VIR_ERR_INVALID_ARG, __FUNCTION__);
        /* Copy to connection error object for back compatability */
        virSetConnError(secret->conn);
        return -1;
9277 9278
    }

9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300
    memcpy(uuid, &secret->uuid[0], VIR_UUID_BUFLEN);

    return 0;
}

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

    virResetLastError();
9301

9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318
    if (!VIR_IS_SECRET(secret)) {
        virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
        return (-1);
    }
    if (buf == NULL) {
        virLibSecretError(secret, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

    if (virSecretGetUUID(secret, &uuid[0]))
        goto error;

    virUUIDFormat(uuid, buf);
    return (0);

error:
    /* Copy to connection error object for back compatability */
9319
    virSetConnError(secret->conn);
9320
    return -1;
9321 9322
}

9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379
/**
 * virSecretGetUsageType:
 * @secret: a secret object
 *
 * Get the type of object which uses this secret. The returned
 * value is one of the constants defined in the virSecretUsageType
 * enumeration. More values may be added to this enumeration in
 * the future, so callers should expect to see usage types they
 * do not explicitly know about.
 *
 * Returns a positive integer identifying the type of object,
 * or -1 upon error.
 */
int
virSecretGetUsageType(virSecretPtr secret)
{
    DEBUG("secret=%p", secret);

    virResetLastError();

    if (!VIR_IS_SECRET(secret)) {
        virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
        return (-1);
    }
    return (secret->usageType);
}

/**
 * virSecretGetUsageID:
 * @secret: a secret object
 *
 * Get the unique identifier of the object with which this
 * secret is to be used. The format of the identifier is
 * dependant on the usage type of the secret. For a secret
 * with a usage type of VIR_SECRET_USAGE_TYPE_VOLUME the
 * identifier will be a fully qualfied path name. The
 * identifiers are intended to be unique within the set of
 * all secrets sharing the same usage type. ie, there shall
 * only ever be one secret for each volume path.
 *
 * Returns a string identifying the object using the secret,
 * or NULL upon error
 */
const char *
virSecretGetUsageID(virSecretPtr secret)
{
    DEBUG("secret=%p", secret);

    virResetLastError();

    if (!VIR_IS_SECRET(secret)) {
        virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
        return (NULL);
    }
    return (secret->usageID);
}

9380

9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509
/**
 * virSecretGetXMLDesc:
 * @secret: A virSecret secret
 * @flags: flags, use 0 for now
 *
 * Fetches an XML document describing attributes of the secret.
 *
 * Returns the XML document on success, NULL on failure.  The caller must
 * free() the XML.
 */
char *
virSecretGetXMLDesc(virSecretPtr secret, unsigned int flags)
{
    virConnectPtr conn;

    VIR_DEBUG("secret=%p, flags=%u", secret, flags);

    virResetLastError();

    if (!VIR_IS_CONNECTED_SECRET(secret)) {
        virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
        return NULL;
    }

    conn = secret->conn;
    if (conn->secretDriver != NULL && conn->secretDriver->getXMLDesc != NULL) {
        char *ret;

        ret = conn->secretDriver->getXMLDesc(secret, flags);
        if (ret == NULL)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
}

/**
 * virSecretSetValue:
 * @secret: A virSecret secret
 * @value: Value of the secret
 * @value_size: Size of the value
 * @flags: flags, use 0 for now
 *
 * Sets the value of a secret.
 *
 * Returns 0 on success, -1 on failure.
 */
int
virSecretSetValue(virSecretPtr secret, const unsigned char *value,
                  size_t value_size, unsigned int flags)
{
    virConnectPtr conn;

    VIR_DEBUG("secret=%p, value=%p, value_size=%zu, flags=%u", secret, value,
              value_size, flags);

    virResetLastError();

    if (!VIR_IS_CONNECTED_SECRET(secret)) {
        virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
        return -1;
    }
    conn = secret->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibSecretError(secret, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        goto error;
    }
    if (value == NULL) {
        virLibSecretError(secret, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

    if (conn->secretDriver != NULL && conn->secretDriver->setValue != NULL) {
        int ret;

        ret = conn->secretDriver->setValue(secret, value, value_size, flags);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return -1;
}

/**
 * virSecretGetValue:
 * @secret: A virSecret connection
 * @value_size: Place for storing size of the secret value
 * @flags: flags, use 0 for now
 *
 * Fetches the value of a secret.
 *
 * Returns the secret value on success, NULL on failure.  The caller must
 * free() the secret value.
 */
unsigned char *
virSecretGetValue(virSecretPtr secret, size_t *value_size, unsigned int flags)
{
    virConnectPtr conn;

    VIR_DEBUG("secret=%p, value_size=%p, flags=%u", secret, value_size, flags);

    virResetLastError();

    if (!VIR_IS_CONNECTED_SECRET(secret)) {
        virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
        return NULL;
    }
    conn = secret->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibSecretError(secret, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        goto error;
    }
    if (value_size == NULL) {
        virLibSecretError(secret, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

9510 9511
    flags &= VIR_SECRET_GET_VALUE_FLAGS_MASK;

9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609
    if (conn->secretDriver != NULL && conn->secretDriver->getValue != NULL) {
        unsigned char *ret;

        ret = conn->secretDriver->getValue(secret, value_size, flags);
        if (ret == NULL)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return NULL;
}

/**
 * virSecretUndefine:
 * @secret: A virSecret secret
 *
 * Deletes the specified secret.  This does not free the associated
 * virSecretPtr object.
 *
 * Returns 0 on success, -1 on failure.
 */
int
virSecretUndefine(virSecretPtr secret)
{
    virConnectPtr conn;

    VIR_DEBUG("secret=%p", secret);

    virResetLastError();

    if (!VIR_IS_CONNECTED_SECRET(secret)) {
        virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
        return -1;
    }
    conn = secret->conn;
    if (conn->flags & VIR_CONNECT_RO) {
        virLibSecretError(secret, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        goto error;
    }

    if (conn->secretDriver != NULL && conn->secretDriver->undefine != NULL) {
        int ret;

        ret = conn->secretDriver->undefine(secret);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(conn);
    return -1;
}

/**
 * virSecretRef:
 * @secret: the secret to hold a reference on
 *
 * Increment the reference count on the secret. For each additional call to
 * this method, there shall be a corresponding call to virSecretFree to release
 * the reference count, once the caller no longer needs the reference to this
 * object.
 *
 * This method is typically useful for applications where multiple threads are
 * using a connection, and it is required that the connection remain open until
 * all threads have finished using it. ie, each new thread using a secret would
 * increment the reference count.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
int
virSecretRef(virSecretPtr secret)
{
    if (!VIR_IS_CONNECTED_SECRET(secret)) {
        virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
        return -1;
    }
    virMutexLock(&secret->conn->lock);
    DEBUG("secret=%p refs=%d", secret, secret->refs);
    secret->refs++;
    virMutexUnlock(&secret->conn->lock);
    return 0;
}

/**
 * virSecretFree:
 * @secret: pointer to a secret
 *
 * Release the secret handle. The underlying secret continues to exist.
 *
9610
 * Returns 0 on success, or -1 on error
9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626
 */
int
virSecretFree(virSecretPtr secret)
{
    DEBUG("secret=%p", secret);

    virResetLastError();

    if (!VIR_IS_CONNECTED_SECRET(secret)) {
        virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
        return -1;
    }
    if (virUnrefSecret(secret) < 0)
        return -1;
    return 0;
}
9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705


/**
 * virStreamNew:
 * @conn: pointer to the connection
 * @flags: control features of the stream
 *
 * Creates a new stream object which can be used to perform
 * streamed I/O with other public API function.
 *
 * When no longer needed, a stream object must be released
 * with virStreamFree. If a data stream has been used,
 * then the application must call virStreamFinish or
 * virStreamAbort before free'ing to, in order to notify
 * the driver of termination.
 *
 * If a non-blocking data stream is required passed
 * VIR_STREAM_NONBLOCK for flags, otherwise pass 0.
 *
 * Returns the new stream, or NULL upon error
 */
virStreamPtr
virStreamNew(virConnectPtr conn,
             unsigned int flags)
{
    virStreamPtr st;

    DEBUG("conn=%p, flags=%u", conn, flags);

    virResetLastError();

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

    st = virGetStream(conn);
    if (st)
        st->flags = flags;

    return st;
}


/**
 * virStreamRef:
 * @stream: pointer to the stream
 *
 * Increment the reference count on the stream. For each
 * additional call to this method, there shall be a corresponding
 * call to virStreamFree to release the reference count, once
 * the caller no longer needs the reference to this object.
 *
 * Returns 0 in case of success, -1 in case of failure
 */
int
virStreamRef(virStreamPtr stream)
{
    if ((!VIR_IS_CONNECTED_STREAM(stream))) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }
    virMutexLock(&stream->conn->lock);
    DEBUG("stream=%p refs=%d", stream, stream->refs);
    stream->refs++;
    virMutexUnlock(&stream->conn->lock);
    return 0;
}


/**
 * virStreamSend:
 * @stream: pointer to the stream object
 * @data: buffer to write to stream
 * @nbytes: size of @data buffer
 *
 * Write a series of bytes to the stream. This method may
 * block the calling application for an arbitrary amount
 * of time. Once an application has finished sending data
9706
 * it should call virStreamFinish to wait for successful
9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920
 * confirmation from the driver, or detect any error
 *
 * This method may not be used if a stream source has been
 * registered
 *
 * Errors are not guaranteed to be reported synchronously
 * with the call, but may instead be delayed until a
 * subsequent call.
 *
 * A example using this with a hypothetical file upload
 * API looks like
 *
 *   virStreamPtr st = virStreamNew(conn, 0);
 *   int fd = open("demo.iso", O_RDONLY)
 *
 *   virConnectUploadFile(conn, "demo.iso", st);
 *
 *   while (1) {
 *       char buf[1024];
 *       int got = read(fd, buf, 1024);
 *       if (got < 0) {
 *          virStreamAbort(st);
 *          break;
 *       }
 *       if (got == 0) {
 *          virStreamFinish(st);
 *          break;
 *       }
 *       int offset = 0;
 *       while (offset < got) {
 *          int sent = virStreamSend(st, buf+offset, got-offset)
 *          if (sent < 0) {
 *             virStreamAbort(st);
 *             goto done;
 *          }
 *          offset += sent;
 *       }
 *   }
 *   if (virStreamFinish(st) < 0)
 *      ... report an error ....
 * done:
 *   virStreamFree(st);
 *   close(fd);
 *
 * Returns the number of bytes written, which may be less
 * than requested.
 *
 * Returns -1 upon error, at which time the stream will
 * be marked as aborted, and the caller should now release
 * the stream with virStreamFree.
 *
 * Returns -2 if the outgoing transmit buffers are full &
 * the stream is marked as non-blocking.
 */
int virStreamSend(virStreamPtr stream,
                  const char *data,
                  size_t nbytes)
{
    DEBUG("stream=%p, data=%p, nbytes=%zi", stream, data, nbytes);

    virResetLastError();

    if (!VIR_IS_CONNECTED_STREAM(stream)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if (stream->driver &&
        stream->driver->streamSend) {
        int ret;
        ret = (stream->driver->streamSend)(stream, data, nbytes);
        if (ret == -2)
            return -2;
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError(stream->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(stream->conn);
    return -1;
}


/**
 * virStreamRecv:
 * @stream: pointer to the stream object
 * @data: buffer to write to stream
 * @nbytes: size of @data buffer
 *
 * Write a series of bytes to the stream. This method may
 * block the calling application for an arbitrary amount
 * of time.
 *
 * Errors are not guaranteed to be reported synchronously
 * with the call, but may instead be delayed until a
 * subsequent call.
 *
 * A example using this with a hypothetical file download
 * API looks like
 *
 *   virStreamPtr st = virStreamNew(conn, 0);
 *   int fd = open("demo.iso", O_WRONLY, 0600)
 *
 *   virConnectDownloadFile(conn, "demo.iso", st);
 *
 *   while (1) {
 *       char buf[1024];
 *       int got = virStreamRecv(st, buf, 1024);
 *       if (got < 0)
 *          break;
 *       if (got == 0) {
 *          virStreamFinish(st);
 *          break;
 *       }
 *       int offset = 0;
 *       while (offset < got) {
 *          int sent = write(fd, buf+offset, got-offset)
 *          if (sent < 0) {
 *             virStreamAbort(st);
 *             goto done;
 *          }
 *          offset += sent;
 *       }
 *   }
 *   if (virStreamFinish(st) < 0)
 *      ... report an error ....
 * done:
 *   virStreamFree(st);
 *   close(fd);
 *
 *
 * Returns the number of bytes read, which may be less
 * than requested.
 *
 * Returns 0 when the end of the stream is reached, at
 * which time the caller should invoke virStreamFinish()
 * to get confirmation of stream completion.
 *
 * Returns -1 upon error, at which time the stream will
 * be marked as aborted, and the caller should now release
 * the stream with virStreamFree.
 *
 * Returns -2 if there is no data pending to be read & the
 * stream is marked as non-blocking.
 */
int virStreamRecv(virStreamPtr stream,
                  char *data,
                  size_t nbytes)
{
    DEBUG("stream=%p, data=%p, nbytes=%zi", stream, data, nbytes);

    virResetLastError();

    if (!VIR_IS_CONNECTED_STREAM(stream)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if (stream->driver &&
        stream->driver->streamRecv) {
        int ret;
        ret = (stream->driver->streamRecv)(stream, data, nbytes);
        if (ret == -2)
            return -2;
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError(stream->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(stream->conn);
    return -1;
}


/**
 * virStreamSendAll:
 * @stream: pointer to the stream object
 * @handler: source callback for reading data from application
 * @opaque: application defined data
 *
 * Send the entire data stream, reading the data from the
 * requested data source. This is simply a convenient alternative
 * to virStreamSend, for apps that do blocking-I/o.
 *
 * A example using this with a hypothetical file upload
 * API looks like
 *
 *   int mysource(virStreamPtr st, char *buf, int nbytes, void *opaque) {
 *       int *fd = opaque;
 *
 *       return read(*fd, buf, nbytes);
 *   }
 *
 *   virStreamPtr st = virStreamNew(conn, 0);
 *   int fd = open("demo.iso", O_RDONLY)
 *
 *   virConnectUploadFile(conn, st);
 *   if (virStreamSendAll(st, mysource, &fd) < 0) {
 *      ...report an error ...
 *      goto done;
 *   }
 *   if (virStreamFinish(st) < 0)
 *      ...report an error...
 *   virStreamFree(st);
 *   close(fd);
 *
9921
 * Returns 0 if all the data was successfully sent. The caller
9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017
 * should invoke virStreamFinish(st) to flush the stream upon
 * success and then virStreamFree
 *
 * Returns -1 upon any error, with virStreamAbort() already
 * having been called,  so the caller need only call
 * virStreamFree()
 */
int virStreamSendAll(virStreamPtr stream,
                     virStreamSourceFunc handler,
                     void *opaque)
{
    char *bytes = NULL;
    int want = 1024*64;
    int ret = -1;
    DEBUG("stream=%p, handler=%p, opaque=%p", stream, handler, opaque);

    virResetLastError();

    if (!VIR_IS_CONNECTED_STREAM(stream)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if (stream->flags & VIR_STREAM_NONBLOCK) {
        virLibConnError(NULL, VIR_ERR_OPERATION_INVALID,
                        _("data sources cannot be used for non-blocking streams"));
        goto cleanup;
    }

    if (VIR_ALLOC_N(bytes, want) < 0) {
        virReportOOMError(stream->conn);
        goto cleanup;
    }

    for (;;) {
        int got, offset = 0;
        got = (handler)(stream, bytes, want, opaque);
        if (got < 0) {
            virStreamAbort(stream);
            goto cleanup;
        }
        if (got == 0)
            break;
        while (offset < got) {
            int done;
            done = virStreamSend(stream, bytes + offset, got - offset);
            if (done < 0)
                goto cleanup;
            offset += done;
        }
    }
    ret = 0;

cleanup:
    VIR_FREE(bytes);

    /* Copy to connection error object for back compatability */
    if (ret != 0)
        virSetConnError(stream->conn);

    return ret;
}


/**
 * virStreamRecvAll:
 * @stream: pointer to the stream object
 * @handler: sink callback for writing data to application
 * @opaque: application defined data
 *
 * Receive the entire data stream, sending the data to the
 * requested data sink. This is simply a convenient alternative
 * to virStreamRecv, for apps that do blocking-I/o.
 *
 * A example using this with a hypothetical file download
 * API looks like
 *
 *   int mysink(virStreamPtr st, const char *buf, int nbytes, void *opaque) {
 *       int *fd = opaque;
 *
 *       return write(*fd, buf, nbytes);
 *   }
 *
 *   virStreamPtr st = virStreamNew(conn, 0);
 *   int fd = open("demo.iso", O_WRONLY)
 *
 *   virConnectUploadFile(conn, st);
 *   if (virStreamRecvAll(st, mysink, &fd) < 0) {
 *      ...report an error ...
 *      goto done;
 *   }
 *   if (virStreamFinish(st) < 0)
 *      ...report an error...
 *   virStreamFree(st);
 *   close(fd);
 *
10018
 * Returns 0 if all the data was successfully received. The caller
10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084
 * should invoke virStreamFinish(st) to flush the stream upon
 * success and then virStreamFree
 *
 * Returns -1 upon any error, with virStreamAbort() already
 * having been called,  so the caller need only call
 * virStreamFree()
 */
int virStreamRecvAll(virStreamPtr stream,
                     virStreamSinkFunc handler,
                     void *opaque)
{
    char *bytes = NULL;
    int want = 1024*64;
    int ret = -1;
    DEBUG("stream=%p, handler=%p, opaque=%p", stream, handler, opaque);

    virResetLastError();

    if (!VIR_IS_CONNECTED_STREAM(stream)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if (stream->flags & VIR_STREAM_NONBLOCK) {
        virLibConnError(NULL, VIR_ERR_OPERATION_INVALID,
                        _("data sinks cannot be used for non-blocking streams"));
        goto cleanup;
    }


    if (VIR_ALLOC_N(bytes, want) < 0) {
        virReportOOMError(stream->conn);
        goto cleanup;
    }

    for (;;) {
        int got, offset = 0;
        got = virStreamRecv(stream, bytes, want);
        if (got < 0)
            goto cleanup;
        if (got == 0)
            break;
        while (offset < got) {
            int done;
            done = (handler)(stream, bytes + offset, got - offset, opaque);
            if (done < 0) {
                virStreamAbort(stream);
                goto cleanup;
            }
            offset += done;
        }
    }
    ret = 0;

cleanup:
    VIR_FREE(bytes);

    /* Copy to connection error object for back compatability */
    if (ret != 0)
        virSetConnError(stream->conn);

    return ret;
}


/**
M
Matthias Bolte 已提交
10085
 * virStreamEventAddCallback:
10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132
 * @stream: pointer to the stream object
 * @events: set of events to monitor
 * @cb: callback to invoke when an event occurs
 * @opaque: application defined data
 * @ff: callback to free @opaque data
 *
 * Register a callback to be notified when a stream
 * becomes writable, or readable. This is most commonly
 * used in conjunction with non-blocking data streams
 * to integrate into an event loop
 *
 * Returns 0 on success, -1 upon error
 */
int virStreamEventAddCallback(virStreamPtr stream,
                              int events,
                              virStreamEventCallback cb,
                              void *opaque,
                              virFreeCallback ff)
{
    DEBUG("stream=%p, events=%d, cb=%p, opaque=%p, ff=%p", stream, events, cb, opaque, ff);

    virResetLastError();

    if (!VIR_IS_CONNECTED_STREAM(stream)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if (stream->driver &&
        stream->driver->streamAddCallback) {
        int ret;
        ret = (stream->driver->streamAddCallback)(stream, events, cb, opaque, ff);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError(stream->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(stream->conn);
    return -1;
}


/**
M
Matthias Bolte 已提交
10133
 * virStreamEventUpdateCallback:
10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173
 * @stream: pointer to the stream object
 * @events: set of events to monitor
 *
 * Changes the set of events to monitor for a stream. This allows
 * for event notification to be changed without having to
 * unregister & register the callback completely. This method
 * is guarenteed to succeed if a callback is already registered
 *
 * Returns 0 on success, -1 if no callback is registered
 */
int virStreamEventUpdateCallback(virStreamPtr stream,
                                 int events)
{
    DEBUG("stream=%p, events=%d", stream, events);

    virResetLastError();

    if (!VIR_IS_CONNECTED_STREAM(stream)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if (stream->driver &&
        stream->driver->streamUpdateCallback) {
        int ret;
        ret = (stream->driver->streamUpdateCallback)(stream, events);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (stream->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(stream->conn);
    return -1;
}

/**
M
Matthias Bolte 已提交
10174
 * virStreamEventRemoveCallback:
10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323
 * @stream: pointer to the stream object
 *
 * Remove a event callback from the stream
 *
 * Returns 0 on success, -1 on error
 */
int virStreamEventRemoveCallback(virStreamPtr stream)
{
    DEBUG("stream=%p", stream);

    virResetLastError();

    if (!VIR_IS_CONNECTED_STREAM(stream)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if (stream->driver &&
        stream->driver->streamRemoveCallback) {
        int ret;
        ret = (stream->driver->streamRemoveCallback)(stream);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (stream->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(stream->conn);
    return -1;
}

/**
 * virStreamFinish:
 * @stream: pointer to the stream object
 *
 * Indicate that there is no further data is to be transmitted
 * on the stream. For output streams this should be called once
 * all data has been written. For input streams this should be
 * called once virStreamRecv returns end-of-file.
 *
 * This method is a synchronization point for all asynchronous
 * errors, so if this returns a success code the application can
 * be sure that all data has been successfully processed.
 *
 * Returns 0 on success, -1 upon error
 */
int virStreamFinish(virStreamPtr stream)
{
    DEBUG("stream=%p", stream);

    virResetLastError();

    if (!VIR_IS_CONNECTED_STREAM(stream)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if (stream->driver &&
        stream->driver->streamFinish) {
        int ret;
        ret = (stream->driver->streamFinish)(stream);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (stream->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(stream->conn);
    return -1;
}

/**
 * virStreamAbort:
 * @stream: pointer to the stream object
 *
 * Request that the in progress data transfer be cancelled
 * abnormally before the end of the stream has been reached.
 * For output streams this can be used to inform the driver
 * that the stream is being terminated early. For input
 * streams this can be used to inform the driver that it
 * should stop sending data.
 *
 * Returns 0 on success, -1 upon error
 */
int virStreamAbort(virStreamPtr stream)
{
    DEBUG("stream=%p", stream);

    virResetLastError();

    if (!VIR_IS_CONNECTED_STREAM(stream)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if (stream->driver &&
        stream->driver->streamAbort) {
        int ret;
        ret = (stream->driver->streamAbort)(stream);
        if (ret < 0)
            goto error;
        return ret;
    }

    virLibConnError (stream->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);

error:
    /* Copy to connection error object for back compatability */
    virSetConnError(stream->conn);
    return -1;
}

/**
 * virStreamFree:
 * @stream: pointer to the stream object
 *
 * Decrement the reference count on a stream, releasing
 * the stream object if the reference count has hit zero.
 *
 * There must not be a active data transfer in progress
 * when releasing the stream. If a stream needs to be
 * disposed of prior to end of stream being reached, then
 * the virStreamAbort function should be called first.
 *
 * Returns 0 upon success, or -1 on error
 */
int virStreamFree(virStreamPtr stream)
{
    DEBUG("stream=%p", stream);

    virResetLastError();

    if (!VIR_IS_CONNECTED_STREAM(stream)) {
        virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    /* XXX Enforce shutdown before free'ing resources ? */

    if (virUnrefStream(stream) < 0)
        return (-1);
    return (0);
}