TEMP comment out libvirtd.stp
[libvirt/apevec.git] / src / libvirt.c
blobaebd3bc325fe02e4e67047c7ee63c01ba1631f8b
1 /*
2 * libvirt.c: Main interfaces for the libvirt library to handle virtualization
3 * domains from a process running in domain 0
5 * Copyright (C) 2005-2006, 2008-2010 Red Hat, Inc.
7 * See COPYING.LIB for the License of this software
9 * Daniel Veillard <veillard@redhat.com>
12 #include <config.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 #include <assert.h>
21 #include <sys/wait.h>
22 #include <time.h>
23 #include <gcrypt.h>
25 #include <libxml/parser.h>
26 #include <libxml/xpath.h>
27 #include <libxml/uri.h>
28 #include "getpass.h"
30 #ifdef HAVE_WINSOCK2_H
31 # include <winsock2.h>
32 #endif
34 #include "virterror_internal.h"
35 #include "logging.h"
36 #include "datatypes.h"
37 #include "driver.h"
39 #include "uuid.h"
40 #include "util.h"
41 #include "memory.h"
43 #ifndef WITH_DRIVER_MODULES
44 # ifdef WITH_TEST
45 # include "test/test_driver.h"
46 # endif
47 # ifdef WITH_XEN
48 # include "xen/xen_driver.h"
49 # endif
50 # ifdef WITH_REMOTE
51 # include "remote/remote_driver.h"
52 # endif
53 # ifdef WITH_OPENVZ
54 # include "openvz/openvz_driver.h"
55 # endif
56 # ifdef WITH_PHYP
57 # include "phyp/phyp_driver.h"
58 # endif
59 # ifdef WITH_VBOX
60 # include "vbox/vbox_driver.h"
61 # endif
62 # ifdef WITH_ESX
63 # include "esx/esx_driver.h"
64 # endif
65 # ifdef WITH_XENAPI
66 # include "xenapi/xenapi_driver.h"
67 # endif
68 #endif
70 #define VIR_FROM_THIS VIR_FROM_NONE
73 * TODO:
74 * - use lock to protect against concurrent accesses ?
75 * - use reference counting to guarantee coherent pointer state ?
78 #define MAX_DRIVERS 10
80 static virDriverPtr virDriverTab[MAX_DRIVERS];
81 static int virDriverTabCount = 0;
82 static virNetworkDriverPtr virNetworkDriverTab[MAX_DRIVERS];
83 static int virNetworkDriverTabCount = 0;
84 static virInterfaceDriverPtr virInterfaceDriverTab[MAX_DRIVERS];
85 static int virInterfaceDriverTabCount = 0;
86 static virStorageDriverPtr virStorageDriverTab[MAX_DRIVERS];
87 static int virStorageDriverTabCount = 0;
88 static virDeviceMonitorPtr virDeviceMonitorTab[MAX_DRIVERS];
89 static int virDeviceMonitorTabCount = 0;
90 static virSecretDriverPtr virSecretDriverTab[MAX_DRIVERS];
91 static int virSecretDriverTabCount = 0;
92 static virNWFilterDriverPtr virNWFilterDriverTab[MAX_DRIVERS];
93 static int virNWFilterDriverTabCount = 0;
94 #ifdef WITH_LIBVIRTD
95 static virStateDriverPtr virStateDriverTab[MAX_DRIVERS];
96 static int virStateDriverTabCount = 0;
97 #endif
98 static int initialized = 0;
100 #if defined(POLKIT_AUTH)
101 static int virConnectAuthGainPolkit(const char *privilege) {
102 const char *const args[] = {
103 POLKIT_AUTH, "--obtain", privilege, NULL
105 int childpid, status, ret;
107 /* Root has all rights */
108 if (getuid() == 0)
109 return 0;
111 if ((childpid = fork()) < 0)
112 return -1;
114 if (!childpid) {
115 execvp(args[0], (char **)args);
116 _exit(-1);
119 while ((ret = waitpid(childpid, &status, 0) == -1) && errno == EINTR);
120 if (ret == -1) {
121 return -1;
124 if (!WIFEXITED(status) ||
125 (WEXITSTATUS(status) != 0 && WEXITSTATUS(status) != 1)) {
126 return -1;
129 return 0;
131 #endif
133 static int virConnectAuthCallbackDefault(virConnectCredentialPtr cred,
134 unsigned int ncred,
135 void *cbdata ATTRIBUTE_UNUSED) {
136 int i;
138 for (i = 0 ; i < ncred ; i++) {
139 char buf[1024];
140 char *bufptr = buf;
141 size_t len;
143 switch (cred[i].type) {
144 case VIR_CRED_EXTERNAL: {
145 if (STRNEQ(cred[i].challenge, "PolicyKit"))
146 return -1;
148 #if defined(POLKIT_AUTH)
149 if (virConnectAuthGainPolkit(cred[i].prompt) < 0)
150 return -1;
151 #else
153 * Ignore & carry on. Although we can't auth
154 * directly, the user may have authenticated
155 * themselves already outside context of libvirt
157 #endif
158 break;
161 case VIR_CRED_USERNAME:
162 case VIR_CRED_AUTHNAME:
163 case VIR_CRED_ECHOPROMPT:
164 case VIR_CRED_REALM:
165 if (printf("%s: ", cred[i].prompt) < 0)
166 return -1;
167 if (fflush(stdout) != 0)
168 return -1;
170 if (!fgets(buf, sizeof(buf), stdin)) {
171 if (feof(stdin)) { /* Treat EOF as "" */
172 buf[0] = '\0';
173 break;
175 return -1;
177 len = strlen(buf);
178 if (len != 0 && buf[len-1] == '\n')
179 buf[len-1] = '\0';
180 break;
182 case VIR_CRED_PASSPHRASE:
183 case VIR_CRED_NOECHOPROMPT:
184 if (printf("%s: ", cred[i].prompt) < 0)
185 return -1;
186 if (fflush(stdout) != 0)
187 return -1;
189 bufptr = getpass("");
190 if (!bufptr)
191 return -1;
192 break;
194 default:
195 return -1;
198 if (cred[i].type != VIR_CRED_EXTERNAL) {
199 if (STREQ(bufptr, "") && cred[i].defresult)
200 cred[i].result = strdup(cred[i].defresult);
201 else
202 cred[i].result = strdup(bufptr);
203 if (!cred[i].result)
204 return -1;
205 cred[i].resultlen = strlen(cred[i].result);
209 return 0;
212 /* Don't typically want VIR_CRED_USERNAME. It enables you to authenticate
213 * as one user, and act as another. It just results in annoying
214 * prompts for the username twice & is very rarely what you want
216 static int virConnectCredTypeDefault[] = {
217 VIR_CRED_AUTHNAME,
218 VIR_CRED_ECHOPROMPT,
219 VIR_CRED_REALM,
220 VIR_CRED_PASSPHRASE,
221 VIR_CRED_NOECHOPROMPT,
222 VIR_CRED_EXTERNAL,
225 static virConnectAuth virConnectAuthDefault = {
226 virConnectCredTypeDefault,
227 sizeof(virConnectCredTypeDefault)/sizeof(int),
228 virConnectAuthCallbackDefault,
229 NULL,
233 * virConnectAuthPtrDefault
235 * A default implementation of the authentication callbacks. This
236 * implementation is suitable for command line based tools. It will
237 * prompt for username, passwords, realm and one time keys as needed.
238 * It will print on STDOUT, and read from STDIN. If this is not
239 * suitable for the application's needs an alternative implementation
240 * should be provided.
242 virConnectAuthPtr virConnectAuthPtrDefault = &virConnectAuthDefault;
244 #if HAVE_WINSOCK2_H
245 static int
246 winsock_init (void)
248 WORD winsock_version, err;
249 WSADATA winsock_data;
251 /* http://msdn2.microsoft.com/en-us/library/ms742213.aspx */
252 winsock_version = MAKEWORD (2, 2);
253 err = WSAStartup (winsock_version, &winsock_data);
254 return err == 0 ? 0 : -1;
256 #endif
258 static int virTLSMutexInit (void **priv)
260 virMutexPtr lock = NULL;
262 if (VIR_ALLOC(lock) < 0)
263 return ENOMEM;
265 if (virMutexInit(lock) < 0) {
266 VIR_FREE(lock);
267 return errno;
270 *priv = lock;
271 return 0;
274 static int virTLSMutexDestroy(void **priv)
276 virMutexPtr lock = *priv;
277 virMutexDestroy(lock);
278 VIR_FREE(lock);
279 return 0;
282 static int virTLSMutexLock(void **priv)
284 virMutexPtr lock = *priv;
285 virMutexLock(lock);
286 return 0;
289 static int virTLSMutexUnlock(void **priv)
291 virMutexPtr lock = *priv;
292 virMutexUnlock(lock);
293 return 0;
296 static struct gcry_thread_cbs virTLSThreadImpl = {
297 /* GCRY_THREAD_OPTION_VERSION was added in gcrypt 1.4.2 */
298 #ifdef GCRY_THREAD_OPTION_VERSION
299 (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION << 8)),
300 #else
301 GCRY_THREAD_OPTION_PTHREAD,
302 #endif
303 NULL,
304 virTLSMutexInit,
305 virTLSMutexDestroy,
306 virTLSMutexLock,
307 virTLSMutexUnlock,
308 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
313 * virInitialize:
315 * Initialize the library. It's better to call this routine at startup
316 * in multithreaded applications to avoid potential race when initializing
317 * the library.
319 * Returns 0 in case of success, -1 in case of error
322 virInitialize(void)
324 if (initialized)
325 return(0);
327 initialized = 1;
329 if (virThreadInitialize() < 0 ||
330 virErrorInitialize() < 0 ||
331 virRandomInitialize(time(NULL) ^ getpid()))
332 return -1;
334 gcry_control(GCRYCTL_SET_THREAD_CBS, &virTLSThreadImpl);
335 gcry_check_version(NULL);
337 virLogSetFromEnv();
339 DEBUG0("register drivers");
341 #if HAVE_WINSOCK2_H
342 if (winsock_init () == -1) return -1;
343 #endif
345 if (!bindtextdomain(GETTEXT_PACKAGE, LOCALEBASEDIR))
346 return (-1);
349 * Note that the order is important: the first ones have a higher
350 * priority when calling virConnectOpen.
352 #ifdef WITH_DRIVER_MODULES
353 /* We don't care if any of these fail, because the whole point
354 * is to allow users to only install modules they want to use.
355 * If they try to open a connection for a module that
356 * is not loaded they'll get a suitable error at that point
358 # ifdef WITH_TEST
359 virDriverLoadModule("test");
360 # endif
361 # ifdef WITH_XEN
362 virDriverLoadModule("xen");
363 # endif
364 # ifdef WITH_OPENVZ
365 virDriverLoadModule("openvz");
366 # endif
367 # ifdef WITH_VBOX
368 virDriverLoadModule("vbox");
369 # endif
370 # ifdef WITH_ESX
371 virDriverLoadModule("esx");
372 # endif
373 # ifdef WITH_XENAPI
374 virDriverLoadModule("xenapi");
375 # endif
376 # ifdef WITH_REMOTE
377 virDriverLoadModule("remote");
378 # endif
379 #else
380 # ifdef WITH_TEST
381 if (testRegister() == -1) return -1;
382 # endif
383 # ifdef WITH_XEN
384 if (xenRegister () == -1) return -1;
385 # endif
386 # ifdef WITH_OPENVZ
387 if (openvzRegister() == -1) return -1;
388 # endif
389 # ifdef WITH_PHYP
390 if (phypRegister() == -1) return -1;
391 # endif
392 # ifdef WITH_VBOX
393 if (vboxRegister() == -1) return -1;
394 # endif
395 # ifdef WITH_ESX
396 if (esxRegister() == -1) return -1;
397 # endif
398 # ifdef WITH_XENAPI
399 if (xenapiRegister() == -1) return -1;
400 # endif
401 # ifdef WITH_REMOTE
402 if (remoteRegister () == -1) return -1;
403 # endif
404 #endif
406 return(0);
409 #ifdef WIN32
410 BOOL WINAPI
411 DllMain (HINSTANCE instance, DWORD reason, LPVOID ignore);
413 BOOL WINAPI
414 DllMain (HINSTANCE instance ATTRIBUTE_UNUSED,
415 DWORD reason,
416 LPVOID ignore ATTRIBUTE_UNUSED)
418 switch (reason) {
419 case DLL_PROCESS_ATTACH:
420 virInitialize();
421 break;
423 case DLL_THREAD_ATTACH:
424 /* Nothing todo in libvirt yet */
425 break;
427 case DLL_THREAD_DETACH:
428 /* Release per-thread local data */
429 virThreadOnExit();
430 break;
432 case DLL_PROCESS_DETACH:
433 /* Don't bother releasing per-thread data
434 since (hopefully) windows cleans up
435 everything on process exit */
436 break;
439 return TRUE;
441 #endif
445 * virLibConnError:
446 * @conn: the connection if available
447 * @error: the error number
448 * @info: extra information string
450 * Handle an error at the connection level
452 static void
453 virLibConnError(virConnectPtr conn, virErrorNumber error, const char *info)
455 const char *errmsg;
457 if (error == VIR_ERR_OK)
458 return;
460 errmsg = virErrorMsg(error, info);
461 virRaiseError(conn, NULL, NULL, VIR_FROM_NONE, error, VIR_ERR_ERROR,
462 errmsg, info, NULL, 0, 0, errmsg, info);
466 * virLibConnWarning:
467 * @conn: the connection if available
468 * @error: the error number
469 * @info: extra information string
471 * Handle an error at the connection level
473 static void
474 virLibConnWarning(virConnectPtr conn, virErrorNumber error, const char *info)
476 const char *errmsg;
478 if (error == VIR_ERR_OK)
479 return;
481 errmsg = virErrorMsg(error, info);
482 virRaiseError(conn, NULL, NULL, VIR_FROM_NONE, error, VIR_ERR_WARNING,
483 errmsg, info, NULL, 0, 0, errmsg, info);
487 * virLibDomainError:
488 * @domain: the domain if available
489 * @error: the error number
490 * @info: extra information string
492 * Handle an error at the connection level
494 static void
495 virLibDomainError(virDomainPtr domain, virErrorNumber error,
496 const char *info)
498 virConnectPtr conn = NULL;
499 const char *errmsg;
501 if (error == VIR_ERR_OK)
502 return;
504 errmsg = virErrorMsg(error, info);
505 if (error != VIR_ERR_INVALID_DOMAIN) {
506 conn = domain->conn;
508 virRaiseError(conn, domain, NULL, VIR_FROM_DOM, error, VIR_ERR_ERROR,
509 errmsg, info, NULL, 0, 0, errmsg, info);
513 * virLibNetworkError:
514 * @conn: the connection if available
515 * @error: the error number
516 * @info: extra information string
518 * Handle an error at the connection level
520 static void
521 virLibNetworkError(virNetworkPtr network, virErrorNumber error,
522 const char *info)
524 virConnectPtr conn = NULL;
525 const char *errmsg;
527 if (error == VIR_ERR_OK)
528 return;
530 errmsg = virErrorMsg(error, info);
531 if (error != VIR_ERR_INVALID_NETWORK) {
532 conn = network->conn;
534 virRaiseError(conn, NULL, network, VIR_FROM_NET, error, VIR_ERR_ERROR,
535 errmsg, info, NULL, 0, 0, errmsg, info);
539 * virLibInterfaceError:
540 * @conn: the connection if available
541 * @error: the error number
542 * @info: extra information string
544 * Handle an error at the connection level
546 static void
547 virLibInterfaceError(virInterfacePtr iface, virErrorNumber error,
548 const char *info)
550 virConnectPtr conn = NULL;
551 const char *errmsg;
553 if (error == VIR_ERR_OK)
554 return;
556 errmsg = virErrorMsg(error, info);
557 if (error != VIR_ERR_INVALID_INTERFACE) {
558 conn = iface->conn;
560 virRaiseError(conn, NULL, NULL, VIR_FROM_INTERFACE, error, VIR_ERR_ERROR,
561 errmsg, info, NULL, 0, 0, errmsg, info);
565 * virLibStoragePoolError:
566 * @conn: the connection if available
567 * @error: the error number
568 * @info: extra information string
570 * Handle an error at the connection level
572 static void
573 virLibStoragePoolError(virStoragePoolPtr pool, virErrorNumber error,
574 const char *info)
576 virConnectPtr conn = NULL;
577 const char *errmsg;
579 if (error == VIR_ERR_OK)
580 return;
582 errmsg = virErrorMsg(error, info);
583 if (error != VIR_ERR_INVALID_STORAGE_POOL)
584 conn = pool->conn;
586 virRaiseError(conn, NULL, NULL, VIR_FROM_STORAGE, error, VIR_ERR_ERROR,
587 errmsg, info, NULL, 0, 0, errmsg, info);
591 * virLibStorageVolError:
592 * @conn: the connection if available
593 * @error: the error number
594 * @info: extra information string
596 * Handle an error at the connection level
598 static void
599 virLibStorageVolError(virStorageVolPtr vol, virErrorNumber error,
600 const char *info)
602 virConnectPtr conn = NULL;
603 const char *errmsg;
605 if (error == VIR_ERR_OK)
606 return;
608 errmsg = virErrorMsg(error, info);
609 if (error != VIR_ERR_INVALID_STORAGE_VOL)
610 conn = vol->conn;
612 virRaiseError(conn, NULL, NULL, VIR_FROM_STORAGE, error, VIR_ERR_ERROR,
613 errmsg, info, NULL, 0, 0, errmsg, info);
617 * virLibNodeDeviceError:
618 * @dev: the device if available
619 * @error: the error number
620 * @info: extra information string
622 * Handle an error at the node device level
624 static void
625 virLibNodeDeviceError(virNodeDevicePtr dev, virErrorNumber error,
626 const char *info)
628 virConnectPtr conn = NULL;
629 const char *errmsg;
631 if (error == VIR_ERR_OK)
632 return;
634 errmsg = virErrorMsg(error, info);
635 if (error != VIR_ERR_INVALID_NODE_DEVICE)
636 conn = dev->conn;
638 virRaiseError(conn, NULL, NULL, VIR_FROM_NODEDEV, error, VIR_ERR_ERROR,
639 errmsg, info, NULL, 0, 0, errmsg, info);
642 #define virLibStreamError(conn, code, ...) \
643 virReportErrorHelper(conn, VIR_FROM_NONE, code, __FILE__, \
644 __FUNCTION__, __LINE__, __VA_ARGS__)
647 * virLibSecretError:
648 * @secret: the secret if available
649 * @error: the error number
650 * @info: extra information string
652 * Handle an error at the secret level
654 static void
655 virLibSecretError(virSecretPtr secret, virErrorNumber error, const char *info)
657 virConnectPtr conn = NULL;
658 const char *errmsg;
660 if (error == VIR_ERR_OK)
661 return;
663 errmsg = virErrorMsg(error, info);
664 if (error != VIR_ERR_INVALID_SECRET)
665 conn = secret->conn;
667 virRaiseError(conn, NULL, NULL, VIR_FROM_SECRET, error, VIR_ERR_ERROR,
668 errmsg, info, NULL, 0, 0, errmsg, info);
672 * virLibNWFilterError:
673 * @conn: the connection if available
674 * @error: the error number
675 * @info: extra information string
677 * Handle an error at the connection level
679 static void
680 virLibNWFilterError(virNWFilterPtr pool, virErrorNumber error,
681 const char *info)
683 virConnectPtr conn = NULL;
684 const char *errmsg;
686 if (error == VIR_ERR_OK)
687 return;
689 errmsg = virErrorMsg(error, info);
690 if (error != VIR_ERR_INVALID_NWFILTER)
691 conn = pool->conn;
693 virRaiseError(conn, NULL, NULL, VIR_FROM_NWFILTER, error, VIR_ERR_ERROR,
694 errmsg, info, NULL, 0, 0, errmsg, info);
698 * virLibDomainSnapshotError:
699 * @snapshot: the snapshot if available
700 * @error: the error number
701 * @info: extra information string
703 * Handle an error at the domain snapshot level
705 static void
706 virLibDomainSnapshotError(virDomainSnapshotPtr snapshot, virErrorNumber error, const char *info)
708 virConnectPtr conn = NULL;
709 const char *errmsg;
711 if (error == VIR_ERR_OK)
712 return;
714 errmsg = virErrorMsg(error, info);
715 if (error != VIR_ERR_INVALID_DOMAIN_SNAPSHOT)
716 conn = snapshot->domain->conn;
718 virRaiseError(conn, NULL, NULL, VIR_FROM_DOMAIN_SNAPSHOT, error, VIR_ERR_ERROR,
719 errmsg, info, NULL, 0, 0, errmsg, info);
723 * virRegisterNetworkDriver:
724 * @driver: pointer to a network driver block
726 * Register a network virtualization driver
728 * Returns the driver priority or -1 in case of error.
731 virRegisterNetworkDriver(virNetworkDriverPtr driver)
733 if (virInitialize() < 0)
734 return -1;
736 if (driver == NULL) {
737 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
738 return(-1);
741 if (virNetworkDriverTabCount >= MAX_DRIVERS) {
742 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
743 return(-1);
746 DEBUG ("registering %s as network driver %d",
747 driver->name, virNetworkDriverTabCount);
749 virNetworkDriverTab[virNetworkDriverTabCount] = driver;
750 return virNetworkDriverTabCount++;
754 * virRegisterInterfaceDriver:
755 * @driver: pointer to an interface driver block
757 * Register an interface virtualization driver
759 * Returns the driver priority or -1 in case of error.
762 virRegisterInterfaceDriver(virInterfaceDriverPtr driver)
764 if (virInitialize() < 0)
765 return -1;
767 if (driver == NULL) {
768 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
769 return(-1);
772 if (virInterfaceDriverTabCount >= MAX_DRIVERS) {
773 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
774 return(-1);
777 DEBUG ("registering %s as interface driver %d",
778 driver->name, virInterfaceDriverTabCount);
780 virInterfaceDriverTab[virInterfaceDriverTabCount] = driver;
781 return virInterfaceDriverTabCount++;
785 * virRegisterStorageDriver:
786 * @driver: pointer to a storage driver block
788 * Register a storage virtualization driver
790 * Returns the driver priority or -1 in case of error.
793 virRegisterStorageDriver(virStorageDriverPtr driver)
795 if (virInitialize() < 0)
796 return -1;
798 if (driver == NULL) {
799 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
800 return(-1);
803 if (virStorageDriverTabCount >= MAX_DRIVERS) {
804 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
805 return(-1);
808 DEBUG ("registering %s as storage driver %d",
809 driver->name, virStorageDriverTabCount);
811 virStorageDriverTab[virStorageDriverTabCount] = driver;
812 return virStorageDriverTabCount++;
816 * virRegisterDeviceMonitor:
817 * @driver: pointer to a device monitor block
819 * Register a device monitor
821 * Returns the driver priority or -1 in case of error.
824 virRegisterDeviceMonitor(virDeviceMonitorPtr driver)
826 if (virInitialize() < 0)
827 return -1;
829 if (driver == NULL) {
830 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
831 return(-1);
834 if (virDeviceMonitorTabCount >= MAX_DRIVERS) {
835 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
836 return(-1);
839 DEBUG ("registering %s as device driver %d",
840 driver->name, virDeviceMonitorTabCount);
842 virDeviceMonitorTab[virDeviceMonitorTabCount] = driver;
843 return virDeviceMonitorTabCount++;
847 * virRegisterSecretDriver:
848 * @driver: pointer to a secret driver block
850 * Register a secret driver
852 * Returns the driver priority or -1 in case of error.
855 virRegisterSecretDriver(virSecretDriverPtr driver)
857 if (virInitialize() < 0)
858 return -1;
860 if (driver == NULL) {
861 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
862 return(-1);
865 if (virSecretDriverTabCount >= MAX_DRIVERS) {
866 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
867 return(-1);
870 DEBUG ("registering %s as secret driver %d",
871 driver->name, virSecretDriverTabCount);
873 virSecretDriverTab[virSecretDriverTabCount] = driver;
874 return virSecretDriverTabCount++;
878 * virRegisterNWFilterDriver:
879 * @driver: pointer to a network filter driver block
881 * Register a network filter virtualization driver
883 * Returns the driver priority or -1 in case of error.
886 virRegisterNWFilterDriver(virNWFilterDriverPtr driver)
888 if (virInitialize() < 0)
889 return -1;
891 if (driver == NULL) {
892 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
893 return -1;
896 if (virNWFilterDriverTabCount >= MAX_DRIVERS) {
897 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
898 return -1;
901 DEBUG ("registering %s as network filter driver %d",
902 driver->name, virNWFilterDriverTabCount);
904 virNWFilterDriverTab[virNWFilterDriverTabCount] = driver;
905 return virNWFilterDriverTabCount++;
910 * virRegisterDriver:
911 * @driver: pointer to a driver block
913 * Register a virtualization driver
915 * Returns the driver priority or -1 in case of error.
918 virRegisterDriver(virDriverPtr driver)
920 if (virInitialize() < 0)
921 return -1;
923 if (driver == NULL) {
924 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
925 return(-1);
928 if (virDriverTabCount >= MAX_DRIVERS) {
929 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
930 return(-1);
933 if (driver->no < 0) {
934 virLibConnError
935 (NULL, VIR_ERR_INVALID_ARG,
936 "virRegisterDriver: tried to register an internal Xen driver");
937 return -1;
940 DEBUG ("registering %s as driver %d",
941 driver->name, virDriverTabCount);
943 virDriverTab[virDriverTabCount] = driver;
944 return virDriverTabCount++;
947 #ifdef WITH_LIBVIRTD
949 * virRegisterStateDriver:
950 * @driver: pointer to a driver block
952 * Register a virtualization driver
954 * Returns the driver priority or -1 in case of error.
957 virRegisterStateDriver(virStateDriverPtr driver)
959 if (virInitialize() < 0)
960 return -1;
962 if (driver == NULL) {
963 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
964 return(-1);
967 if (virStateDriverTabCount >= MAX_DRIVERS) {
968 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
969 return(-1);
972 virStateDriverTab[virStateDriverTabCount] = driver;
973 return virStateDriverTabCount++;
977 * virStateInitialize:
978 * @privileged: set to 1 if running with root priviledge, 0 otherwise
980 * Initialize all virtualization drivers.
982 * Returns 0 if all succeed, -1 upon any failure.
984 int virStateInitialize(int privileged) {
985 int i, ret = 0;
987 if (virInitialize() < 0)
988 return -1;
990 for (i = 0 ; i < virStateDriverTabCount ; i++) {
991 if (virStateDriverTab[i]->initialize &&
992 virStateDriverTab[i]->initialize(privileged) < 0) {
993 VIR_ERROR(_("Initialization of %s state driver failed"),
994 virStateDriverTab[i]->name);
995 ret = -1;
998 return ret;
1002 * virStateCleanup:
1004 * Run each virtualization driver's cleanup method.
1006 * Returns 0 if all succeed, -1 upon any failure.
1008 int virStateCleanup(void) {
1009 int i, ret = 0;
1011 for (i = 0 ; i < virStateDriverTabCount ; i++) {
1012 if (virStateDriverTab[i]->cleanup &&
1013 virStateDriverTab[i]->cleanup() < 0)
1014 ret = -1;
1016 return ret;
1020 * virStateReload:
1022 * Run each virtualization driver's reload method.
1024 * Returns 0 if all succeed, -1 upon any failure.
1026 int virStateReload(void) {
1027 int i, ret = 0;
1029 for (i = 0 ; i < virStateDriverTabCount ; i++) {
1030 if (virStateDriverTab[i]->reload &&
1031 virStateDriverTab[i]->reload() < 0)
1032 ret = -1;
1034 return ret;
1038 * virStateActive:
1040 * Run each virtualization driver's "active" method.
1042 * Returns 0 if none are active, 1 if at least one is.
1044 int virStateActive(void) {
1045 int i, ret = 0;
1047 for (i = 0 ; i < virStateDriverTabCount ; i++) {
1048 if (virStateDriverTab[i]->active &&
1049 virStateDriverTab[i]->active())
1050 ret = 1;
1052 return ret;
1055 #endif
1060 * virGetVersion:
1061 * @libVer: return value for the library version (OUT)
1062 * @type: the type of connection/driver looked at
1063 * @typeVer: return value for the version of the hypervisor (OUT)
1065 * Provides two information back, @libVer is the version of the library
1066 * while @typeVer will be the version of the hypervisor type @type against
1067 * which the library was compiled. If @type is NULL, "Xen" is assumed, if
1068 * @type is unknown or not available, an error code will be returned and
1069 * @typeVer will be 0.
1071 * Returns -1 in case of failure, 0 otherwise, and values for @libVer and
1072 * @typeVer have the format major * 1,000,000 + minor * 1,000 + release.
1075 virGetVersion(unsigned long *libVer, const char *type,
1076 unsigned long *typeVer)
1078 DEBUG("libVir=%p, type=%s, typeVer=%p", libVer, type, typeVer);
1080 if (!initialized)
1081 if (virInitialize() < 0)
1082 goto error;
1084 if (libVer == NULL)
1085 goto error;
1086 *libVer = LIBVIR_VERSION_NUMBER;
1088 if (typeVer != NULL) {
1089 if (type == NULL)
1090 type = "Xen";
1092 /* FIXME: Add _proper_ type version handling for loadable driver modules... */
1093 #ifdef WITH_DRIVER_MODULES
1094 *typeVer = LIBVIR_VERSION_NUMBER;
1095 #else
1096 *typeVer = 0;
1098 # if WITH_XEN
1099 if (STRCASEEQ(type, "Xen"))
1100 *typeVer = xenUnifiedVersion();
1101 # endif
1102 # if WITH_TEST
1103 if (STRCASEEQ(type, "Test"))
1104 *typeVer = LIBVIR_VERSION_NUMBER;
1105 # endif
1106 # if WITH_QEMU
1107 if (STRCASEEQ(type, "QEMU"))
1108 *typeVer = LIBVIR_VERSION_NUMBER;
1109 # endif
1110 # if WITH_LXC
1111 if (STRCASEEQ(type, "LXC"))
1112 *typeVer = LIBVIR_VERSION_NUMBER;
1113 # endif
1114 # if WITH_PHYP
1115 if (STRCASEEQ(type, "phyp"))
1116 *typeVer = LIBVIR_VERSION_NUMBER;
1117 # endif
1118 # if WITH_OPENVZ
1119 if (STRCASEEQ(type, "OpenVZ"))
1120 *typeVer = LIBVIR_VERSION_NUMBER;
1121 # endif
1122 # if WITH_VBOX
1123 if (STRCASEEQ(type, "VBox"))
1124 *typeVer = LIBVIR_VERSION_NUMBER;
1125 # endif
1126 # if WITH_UML
1127 if (STRCASEEQ(type, "UML"))
1128 *typeVer = LIBVIR_VERSION_NUMBER;
1129 # endif
1130 # if WITH_ONE
1131 if (STRCASEEQ(type, "ONE"))
1132 *typeVer = LIBVIR_VERSION_NUMBER;
1133 # endif
1134 # if WITH_ESX
1135 if (STRCASEEQ(type, "ESX"))
1136 *typeVer = LIBVIR_VERSION_NUMBER;
1137 # endif
1138 # if WITH_XENAPI
1139 if (STRCASEEQ(type, "XenAPI"))
1140 *typeVer = LIBVIR_VERSION_NUMBER;
1141 # endif
1142 # if WITH_REMOTE
1143 if (STRCASEEQ(type, "Remote"))
1144 *typeVer = remoteVersion();
1145 # endif
1146 if (*typeVer == 0) {
1147 virLibConnError(NULL, VIR_ERR_NO_SUPPORT, type);
1148 goto error;
1150 #endif /* WITH_DRIVER_MODULES */
1152 return (0);
1154 error:
1155 virDispatchError(NULL);
1156 return -1;
1159 static virConnectPtr
1160 do_open (const char *name,
1161 virConnectAuthPtr auth,
1162 int flags)
1164 int i, res;
1165 virConnectPtr ret;
1167 virResetLastError();
1169 ret = virGetConnect();
1170 if (ret == NULL)
1171 return NULL;
1174 * If no URI is passed, then check for an environment string if not
1175 * available probe the compiled in drivers to find a default hypervisor
1176 * if detectable.
1178 if (!name || name[0] == '\0') {
1179 char *defname = getenv("LIBVIRT_DEFAULT_URI");
1180 if (defname && *defname) {
1181 DEBUG("Using LIBVIRT_DEFAULT_URI %s", defname);
1182 name = defname;
1183 } else {
1184 name = NULL;
1188 if (name) {
1189 /* Convert xen -> xen:/// for back compat */
1190 if (STRCASEEQ(name, "xen"))
1191 name = "xen:///";
1193 /* Convert xen:// -> xen:/// because xmlParseURI cannot parse the
1194 * former. This allows URIs such as xen://localhost to work.
1196 if (STREQ (name, "xen://"))
1197 name = "xen:///";
1199 ret->uri = xmlParseURI (name);
1200 if (!ret->uri) {
1201 virLibConnError (ret, VIR_ERR_INVALID_ARG,
1202 _("could not parse connection URI"));
1203 goto failed;
1206 DEBUG("name \"%s\" to URI components:\n"
1207 " scheme %s\n"
1208 " opaque %s\n"
1209 " authority %s\n"
1210 " server %s\n"
1211 " user %s\n"
1212 " port %d\n"
1213 " path %s\n",
1214 name,
1215 NULLSTR(ret->uri->scheme), NULLSTR(ret->uri->opaque),
1216 NULLSTR(ret->uri->authority), NULLSTR(ret->uri->server),
1217 NULLSTR(ret->uri->user), ret->uri->port,
1218 NULLSTR(ret->uri->path));
1219 } else {
1220 DEBUG0("no name, allowing driver auto-select");
1223 /* Cleansing flags */
1224 ret->flags = flags & VIR_CONNECT_RO;
1226 for (i = 0; i < virDriverTabCount; i++) {
1227 /* We're going to probe the remote driver next. So we have already
1228 * probed all other client-side-only driver before, but none of them
1229 * accepted the URI.
1230 * If the scheme corresponds to a known but disabled client-side-only
1231 * driver then report a useful error, instead of a cryptic one about
1232 * not being able to connect to libvirtd or not being able to find
1233 * certificates. */
1234 if (virDriverTab[i]->no == VIR_DRV_REMOTE &&
1235 ret->uri != NULL && ret->uri->scheme != NULL &&
1237 #ifndef WITH_PHYP
1238 STRCASEEQ(ret->uri->scheme, "phyp") ||
1239 #endif
1240 #ifndef WITH_ESX
1241 STRCASEEQ(ret->uri->scheme, "vpx") ||
1242 STRCASEEQ(ret->uri->scheme, "esx") ||
1243 STRCASEEQ(ret->uri->scheme, "gsx") ||
1244 #endif
1245 #ifndef WITH_XENAPI
1246 STRCASEEQ(ret->uri->scheme, "xenapi") ||
1247 #endif
1248 false)) {
1249 virReportErrorHelper(NULL, VIR_FROM_NONE, VIR_ERR_INVALID_ARG,
1250 __FILE__, __FUNCTION__, __LINE__,
1251 _("libvirt was built without the '%s' driver"),
1252 ret->uri->scheme);
1253 goto failed;
1256 DEBUG("trying driver %d (%s) ...",
1257 i, virDriverTab[i]->name);
1258 res = virDriverTab[i]->open (ret, auth, flags);
1259 DEBUG("driver %d %s returned %s",
1260 i, virDriverTab[i]->name,
1261 res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
1262 (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
1263 (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
1264 if (res == VIR_DRV_OPEN_ERROR) goto failed;
1265 else if (res == VIR_DRV_OPEN_SUCCESS) {
1266 ret->driver = virDriverTab[i];
1267 break;
1271 if (!ret->driver) {
1272 /* If we reach here, then all drivers declined the connection. */
1273 virLibConnError (NULL, VIR_ERR_NO_CONNECT, name);
1274 goto failed;
1277 for (i = 0; i < virNetworkDriverTabCount; i++) {
1278 res = virNetworkDriverTab[i]->open (ret, auth, flags);
1279 DEBUG("network driver %d %s returned %s",
1280 i, virNetworkDriverTab[i]->name,
1281 res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
1282 (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
1283 (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
1284 if (res == VIR_DRV_OPEN_ERROR) {
1285 if (STREQ(virNetworkDriverTab[i]->name, "remote")) {
1286 virLibConnWarning (NULL, VIR_WAR_NO_NETWORK,
1287 "Is the daemon running ?");
1289 break;
1290 } else if (res == VIR_DRV_OPEN_SUCCESS) {
1291 ret->networkDriver = virNetworkDriverTab[i];
1292 break;
1296 for (i = 0; i < virInterfaceDriverTabCount; i++) {
1297 res = virInterfaceDriverTab[i]->open (ret, auth, flags);
1298 DEBUG("interface driver %d %s returned %s",
1299 i, virInterfaceDriverTab[i]->name,
1300 res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
1301 (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
1302 (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
1303 if (res == VIR_DRV_OPEN_ERROR) {
1304 if (STREQ(virInterfaceDriverTab[i]->name, "remote")) {
1305 virLibConnWarning (NULL, VIR_WAR_NO_INTERFACE,
1306 "Is the daemon running ?");
1308 break;
1309 } else if (res == VIR_DRV_OPEN_SUCCESS) {
1310 ret->interfaceDriver = virInterfaceDriverTab[i];
1311 break;
1315 /* Secondary driver for storage. Optional */
1316 for (i = 0; i < virStorageDriverTabCount; i++) {
1317 res = virStorageDriverTab[i]->open (ret, auth, flags);
1318 DEBUG("storage driver %d %s returned %s",
1319 i, virStorageDriverTab[i]->name,
1320 res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
1321 (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
1322 (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
1323 if (res == VIR_DRV_OPEN_ERROR) {
1324 if (STREQ(virStorageDriverTab[i]->name, "remote")) {
1325 virLibConnWarning (NULL, VIR_WAR_NO_STORAGE,
1326 "Is the daemon running ?");
1328 break;
1329 } else if (res == VIR_DRV_OPEN_SUCCESS) {
1330 ret->storageDriver = virStorageDriverTab[i];
1331 break;
1335 /* Node driver (optional) */
1336 for (i = 0; i < virDeviceMonitorTabCount; i++) {
1337 res = virDeviceMonitorTab[i]->open (ret, auth, flags);
1338 DEBUG("node driver %d %s returned %s",
1339 i, virDeviceMonitorTab[i]->name,
1340 res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
1341 (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
1342 (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
1343 if (res == VIR_DRV_OPEN_ERROR) {
1344 if (STREQ(virDeviceMonitorTab[i]->name, "remote")) {
1345 virLibConnWarning (NULL, VIR_WAR_NO_NODE,
1346 "Is the libvirtd daemon running ?");
1347 } else {
1348 char *msg;
1349 if (virAsprintf(&msg, "Is the %s daemon running?",
1350 virDeviceMonitorTab[i]->name) > 0) {
1351 virLibConnWarning (NULL, VIR_WAR_NO_NODE, msg);
1352 VIR_FREE(msg);
1355 break;
1356 } else if (res == VIR_DRV_OPEN_SUCCESS) {
1357 ret->deviceMonitor = virDeviceMonitorTab[i];
1358 break;
1362 /* Secret manipulation driver. Optional */
1363 for (i = 0; i < virSecretDriverTabCount; i++) {
1364 res = virSecretDriverTab[i]->open (ret, auth, flags);
1365 DEBUG("secret driver %d %s returned %s",
1366 i, virSecretDriverTab[i]->name,
1367 res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
1368 (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
1369 (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
1370 if (res == VIR_DRV_OPEN_ERROR) {
1371 if (STREQ(virSecretDriverTab[i]->name, "remote")) {
1372 virLibConnWarning (NULL, VIR_WAR_NO_SECRET,
1373 "Is the daemon running ?");
1375 break;
1376 } else if (res == VIR_DRV_OPEN_SUCCESS) {
1377 ret->secretDriver = virSecretDriverTab[i];
1378 break;
1382 /* Network filter driver. Optional */
1383 for (i = 0; i < virNWFilterDriverTabCount; i++) {
1384 res = virNWFilterDriverTab[i]->open (ret, auth, flags);
1385 DEBUG("nwfilter driver %d %s returned %s",
1386 i, virNWFilterDriverTab[i]->name,
1387 res == VIR_DRV_OPEN_SUCCESS ? "SUCCESS" :
1388 (res == VIR_DRV_OPEN_DECLINED ? "DECLINED" :
1389 (res == VIR_DRV_OPEN_ERROR ? "ERROR" : "unknown status")));
1390 if (res == VIR_DRV_OPEN_ERROR) {
1391 if (STREQ(virNWFilterDriverTab[i]->name, "remote")) {
1392 virLibConnWarning (NULL, VIR_WAR_NO_NWFILTER,
1393 _("Is the daemon running ?"));
1395 break;
1396 } else if (res == VIR_DRV_OPEN_SUCCESS) {
1397 ret->nwfilterDriver = virNWFilterDriverTab[i];
1398 break;
1402 return ret;
1404 failed:
1405 virUnrefConnect(ret);
1407 return NULL;
1411 * virConnectOpen:
1412 * @name: URI of the hypervisor
1414 * This function should be called first to get a connection to the
1415 * Hypervisor and xen store
1417 * Returns a pointer to the hypervisor connection or NULL in case of error
1419 * If @name is NULL then probing will be done to determine a suitable
1420 * default driver to activate. This involves trying each hypervisor
1421 * in turn until one successfully opens. If the LIBVIRT_DEFAULT_URI
1422 * environment variable is set, then it will be used in preference
1423 * to probing for a driver.
1425 * If connecting to an unprivileged hypervisor driver which requires
1426 * the libvirtd daemon to be active, it will automatically be launched
1427 * if not already running. This can be prevented by setting the
1428 * environment variable LIBVIRT_AUTOSTART=0
1430 * URIs are documented at http://libvirt.org/uri.html
1432 virConnectPtr
1433 virConnectOpen (const char *name)
1435 virConnectPtr ret = NULL;
1436 if (!initialized)
1437 if (virInitialize() < 0)
1438 goto error;
1440 DEBUG("name=%s", name);
1441 ret = do_open (name, NULL, 0);
1442 if (!ret)
1443 goto error;
1444 return ret;
1446 error:
1447 virDispatchError(NULL);
1448 return NULL;
1452 * virConnectOpenReadOnly:
1453 * @name: URI of the hypervisor
1455 * This function should be called first to get a restricted connection to the
1456 * library functionalities. The set of APIs usable are then restricted
1457 * on the available methods to control the domains.
1459 * See virConnectOpen for notes about environment variables which can
1460 * have an effect on opening drivers
1462 * Returns a pointer to the hypervisor connection or NULL in case of error
1464 * URIs are documented at http://libvirt.org/uri.html
1466 virConnectPtr
1467 virConnectOpenReadOnly(const char *name)
1469 virConnectPtr ret = NULL;
1470 if (!initialized)
1471 if (virInitialize() < 0)
1472 goto error;
1474 DEBUG("name=%s", name);
1475 ret = do_open (name, NULL, VIR_CONNECT_RO);
1476 if (!ret)
1477 goto error;
1478 return ret;
1480 error:
1481 virDispatchError(NULL);
1482 return NULL;
1486 * virConnectOpenAuth:
1487 * @name: URI of the hypervisor
1488 * @auth: Authenticate callback parameters
1489 * @flags: Open flags
1491 * This function should be called first to get a connection to the
1492 * Hypervisor. If necessary, authentication will be performed fetching
1493 * credentials via the callback
1495 * See virConnectOpen for notes about environment variables which can
1496 * have an effect on opening drivers
1498 * Returns a pointer to the hypervisor connection or NULL in case of error
1500 * URIs are documented at http://libvirt.org/uri.html
1502 virConnectPtr
1503 virConnectOpenAuth(const char *name,
1504 virConnectAuthPtr auth,
1505 int flags)
1507 virConnectPtr ret = NULL;
1508 if (!initialized)
1509 if (virInitialize() < 0)
1510 goto error;
1512 DEBUG("name=%s, auth=%p, flags=%d", NULLSTR(name), auth, flags);
1513 ret = do_open (name, auth, flags);
1514 if (!ret)
1515 goto error;
1516 return ret;
1518 error:
1519 virDispatchError(NULL);
1520 return NULL;
1524 * virConnectClose:
1525 * @conn: pointer to the hypervisor connection
1527 * This function closes the connection to the Hypervisor. This should
1528 * not be called if further interaction with the Hypervisor are needed
1529 * especially if there is running domain which need further monitoring by
1530 * the application.
1532 * Returns 0 in case of success or -1 in case of error.
1535 virConnectClose(virConnectPtr conn)
1537 int ret = -1;
1538 DEBUG("conn=%p", conn);
1540 virResetLastError();
1542 if (!VIR_IS_CONNECT(conn)) {
1543 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1544 goto error;
1547 ret = virUnrefConnect(conn);
1548 if (ret < 0)
1549 goto error;
1550 return ret;
1552 error:
1553 virDispatchError(NULL);
1554 return ret;
1558 * virConnectRef:
1559 * @conn: the connection to hold a reference on
1561 * Increment the reference count on the connection. For each
1562 * additional call to this method, there shall be a corresponding
1563 * call to virConnectClose to release the reference count, once
1564 * the caller no longer needs the reference to this object.
1566 * This method is typically useful for applications where multiple
1567 * threads are using a connection, and it is required that the
1568 * connection remain open until all threads have finished using
1569 * it. ie, each new thread using a connection would increment
1570 * the reference count.
1572 * Returns 0 in case of success, -1 in case of failure
1575 virConnectRef(virConnectPtr conn)
1577 if ((!VIR_IS_CONNECT(conn))) {
1578 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
1579 virDispatchError(NULL);
1580 return(-1);
1582 virMutexLock(&conn->lock);
1583 DEBUG("conn=%p refs=%d", conn, conn->refs);
1584 conn->refs++;
1585 virMutexUnlock(&conn->lock);
1586 return 0;
1590 * Not for public use. This function is part of the internal
1591 * implementation of driver features in the remote case.
1594 virDrvSupportsFeature (virConnectPtr conn, int feature)
1596 int ret;
1597 DEBUG("conn=%p, feature=%d", conn, feature);
1599 virResetLastError();
1601 if (!VIR_IS_CONNECT(conn)) {
1602 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1603 virDispatchError(NULL);
1604 return (-1);
1607 ret = VIR_DRV_SUPPORTS_FEATURE (conn->driver, conn, feature);
1609 if (ret < 0)
1610 virDispatchError(conn);
1612 return ret;
1616 * virConnectGetType:
1617 * @conn: pointer to the hypervisor connection
1619 * Get the name of the Hypervisor software used.
1621 * Returns NULL in case of error, a static zero terminated string otherwise.
1623 * See also:
1624 * http://www.redhat.com/archives/libvir-list/2007-February/msg00096.html
1626 const char *
1627 virConnectGetType(virConnectPtr conn)
1629 const char *ret;
1630 DEBUG("conn=%p", conn);
1632 virResetLastError();
1634 if (!VIR_IS_CONNECT(conn)) {
1635 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1636 virDispatchError(NULL);
1637 return (NULL);
1640 if (conn->driver->type) {
1641 ret = conn->driver->type (conn);
1642 if (ret) return ret;
1644 return conn->driver->name;
1648 * virConnectGetVersion:
1649 * @conn: pointer to the hypervisor connection
1650 * @hvVer: return value for the version of the running hypervisor (OUT)
1652 * Get the version level of the Hypervisor running. This may work only with
1653 * hypervisor call, i.e. with privileged access to the hypervisor, not
1654 * with a Read-Only connection.
1656 * Returns -1 in case of error, 0 otherwise. if the version can't be
1657 * extracted by lack of capacities returns 0 and @hvVer is 0, otherwise
1658 * @hvVer value is major * 1,000,000 + minor * 1,000 + release
1661 virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
1663 DEBUG("conn=%p, hvVer=%p", conn, hvVer);
1665 virResetLastError();
1667 if (!VIR_IS_CONNECT(conn)) {
1668 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1669 virDispatchError(NULL);
1670 return -1;
1673 if (hvVer == NULL) {
1674 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1675 goto error;
1678 if (conn->driver->version) {
1679 int ret = conn->driver->version (conn, hvVer);
1680 if (ret < 0)
1681 goto error;
1682 return ret;
1685 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1687 error:
1688 virDispatchError(conn);
1689 return -1;
1693 * virConnectGetLibVersion:
1694 * @conn: pointer to the hypervisor connection
1695 * @libVer: returns the libvirt library version used on the connection (OUT)
1697 * Provides @libVer, which is the version of libvirt used by the
1698 * daemon running on the @conn host
1700 * Returns -1 in case of failure, 0 otherwise, and values for @libVer have
1701 * the format major * 1,000,000 + minor * 1,000 + release.
1704 virConnectGetLibVersion(virConnectPtr conn, unsigned long *libVer)
1706 int ret = -1;
1707 DEBUG("conn=%p, libVir=%p", conn, libVer);
1709 virResetLastError();
1711 if (!VIR_IS_CONNECT(conn)) {
1712 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1713 virDispatchError(NULL);
1714 return -1;
1717 if (libVer == NULL) {
1718 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1719 goto error;
1722 if (conn->driver->libvirtVersion) {
1723 ret = conn->driver->libvirtVersion(conn, libVer);
1724 if (ret < 0)
1725 goto error;
1726 return ret;
1729 *libVer = LIBVIR_VERSION_NUMBER;
1730 return 0;
1732 error:
1733 virDispatchError(conn);
1734 return ret;
1738 * virConnectGetHostname:
1739 * @conn: pointer to a hypervisor connection
1741 * This returns the system hostname on which the hypervisor is
1742 * running (the result of the gethostname system call). If
1743 * we are connected to a remote system, then this returns the
1744 * hostname of the remote system.
1746 * Returns the hostname which must be freed by the caller, or
1747 * NULL if there was an error.
1749 char *
1750 virConnectGetHostname (virConnectPtr conn)
1752 DEBUG("conn=%p", conn);
1754 virResetLastError();
1756 if (!VIR_IS_CONNECT(conn)) {
1757 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1758 virDispatchError(NULL);
1759 return NULL;
1762 if (conn->driver->getHostname) {
1763 char *ret = conn->driver->getHostname (conn);
1764 if (!ret)
1765 goto error;
1766 return ret;
1769 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1771 error:
1772 virDispatchError(conn);
1773 return NULL;
1777 * virConnectGetURI:
1778 * @conn: pointer to a hypervisor connection
1780 * This returns the URI (name) of the hypervisor connection.
1781 * Normally this is the same as or similar to the string passed
1782 * to the virConnectOpen/virConnectOpenReadOnly call, but
1783 * the driver may make the URI canonical. If name == NULL
1784 * was passed to virConnectOpen, then the driver will return
1785 * a non-NULL URI which can be used to connect to the same
1786 * hypervisor later.
1788 * Returns the URI string which must be freed by the caller, or
1789 * NULL if there was an error.
1791 char *
1792 virConnectGetURI (virConnectPtr conn)
1794 char *name;
1795 DEBUG("conn=%p", conn);
1797 virResetLastError();
1799 if (!VIR_IS_CONNECT(conn)) {
1800 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1801 virDispatchError(NULL);
1802 return NULL;
1805 name = (char *)xmlSaveUri(conn->uri);
1806 if (!name) {
1807 virReportOOMError();
1808 goto error;
1810 return name;
1812 error:
1813 virDispatchError(conn);
1814 return NULL;
1818 * virConnectGetMaxVcpus:
1819 * @conn: pointer to the hypervisor connection
1820 * @type: value of the 'type' attribute in the <domain> element
1822 * Provides the maximum number of virtual CPUs supported for a guest VM of a
1823 * specific type. The 'type' parameter here corresponds to the 'type'
1824 * attribute in the <domain> element of the XML.
1826 * Returns the maximum of virtual CPU or -1 in case of error.
1829 virConnectGetMaxVcpus(virConnectPtr conn,
1830 const char *type)
1832 DEBUG("conn=%p, type=%s", conn, type);
1834 virResetLastError();
1836 if (!VIR_IS_CONNECT(conn)) {
1837 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1838 virDispatchError(NULL);
1839 return -1;
1842 if (conn->driver->getMaxVcpus) {
1843 int ret = conn->driver->getMaxVcpus (conn, type);
1844 if (ret < 0)
1845 goto error;
1846 return ret;
1849 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1850 error:
1851 virDispatchError(conn);
1852 return -1;
1856 * virConnectListDomains:
1857 * @conn: pointer to the hypervisor connection
1858 * @ids: array to collect the list of IDs of active domains
1859 * @maxids: size of @ids
1861 * Collect the list of active domains, and store their ID in @maxids
1863 * Returns the number of domain found or -1 in case of error
1866 virConnectListDomains(virConnectPtr conn, int *ids, int maxids)
1868 DEBUG("conn=%p, ids=%p, maxids=%d", conn, ids, maxids);
1870 virResetLastError();
1872 if (!VIR_IS_CONNECT(conn)) {
1873 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1874 virDispatchError(NULL);
1875 return -1;
1878 if ((ids == NULL) || (maxids < 0)) {
1879 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1880 goto error;
1883 if (conn->driver->listDomains) {
1884 int ret = conn->driver->listDomains (conn, ids, maxids);
1885 if (ret < 0)
1886 goto error;
1887 return ret;
1890 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1891 error:
1892 virDispatchError(conn);
1893 return -1;
1897 * virConnectNumOfDomains:
1898 * @conn: pointer to the hypervisor connection
1900 * Provides the number of active domains.
1902 * Returns the number of domain found or -1 in case of error
1905 virConnectNumOfDomains(virConnectPtr conn)
1907 DEBUG("conn=%p", conn);
1909 virResetLastError();
1911 if (!VIR_IS_CONNECT(conn)) {
1912 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1913 virDispatchError(NULL);
1914 return -1;
1917 if (conn->driver->numOfDomains) {
1918 int ret = conn->driver->numOfDomains (conn);
1919 if (ret < 0)
1920 goto error;
1921 return ret;
1924 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1925 error:
1926 virDispatchError(conn);
1927 return -1;
1931 * virDomainGetConnect:
1932 * @dom: pointer to a domain
1934 * Provides the connection pointer associated with a domain. The
1935 * reference counter on the connection is not increased by this
1936 * call.
1938 * WARNING: When writing libvirt bindings in other languages, do
1939 * not use this function. Instead, store the connection and
1940 * the domain object together.
1942 * Returns the virConnectPtr or NULL in case of failure.
1944 virConnectPtr
1945 virDomainGetConnect (virDomainPtr dom)
1947 DEBUG("dom=%p", dom);
1949 virResetLastError();
1951 if (!VIR_IS_CONNECTED_DOMAIN (dom)) {
1952 virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1953 virDispatchError(NULL);
1954 return NULL;
1956 return dom->conn;
1960 * virDomainCreateXML:
1961 * @conn: pointer to the hypervisor connection
1962 * @xmlDesc: string containing an XML description of the domain
1963 * @flags: bitwise-or of supported virDomainCreateFlags
1965 * Launch a new guest domain, based on an XML description similar
1966 * to the one returned by virDomainGetXMLDesc()
1967 * This function may requires privileged access to the hypervisor.
1968 * The domain is not persistent, so its definition will disappear when it
1969 * is destroyed, or if the host is restarted (see virDomainDefineXML() to
1970 * define persistent domains).
1972 * Returns a new domain object or NULL in case of failure
1974 virDomainPtr
1975 virDomainCreateXML(virConnectPtr conn, const char *xmlDesc,
1976 unsigned int flags)
1978 DEBUG("conn=%p, xmlDesc=%s, flags=%d", conn, xmlDesc, flags);
1980 virResetLastError();
1982 if (!VIR_IS_CONNECT(conn)) {
1983 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
1984 virDispatchError(NULL);
1985 return (NULL);
1987 if (xmlDesc == NULL) {
1988 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1989 goto error;
1991 if (conn->flags & VIR_CONNECT_RO) {
1992 virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
1993 goto error;
1996 if (conn->driver->domainCreateXML) {
1997 virDomainPtr ret;
1998 ret = conn->driver->domainCreateXML (conn, xmlDesc, flags);
1999 if (!ret)
2000 goto error;
2001 return ret;
2004 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2005 error:
2006 virDispatchError(conn);
2007 return NULL;
2011 * virDomainCreateLinux:
2012 * @conn: pointer to the hypervisor connection
2013 * @xmlDesc: string containing an XML description of the domain
2014 * @flags: callers should always pass 0
2016 * Deprecated after 0.4.6.
2017 * Renamed to virDomainCreateXML() providing identical functionality.
2018 * This existing name will left indefinitely for API compatibility.
2020 * Returns a new domain object or NULL in case of failure
2022 virDomainPtr
2023 virDomainCreateLinux(virConnectPtr conn, const char *xmlDesc,
2024 unsigned int flags)
2026 return(virDomainCreateXML(conn, xmlDesc, flags));
2030 * virDomainLookupByID:
2031 * @conn: pointer to the hypervisor connection
2032 * @id: the domain ID number
2034 * Try to find a domain based on the hypervisor ID number
2035 * Note that this won't work for inactive domains which have an ID of -1,
2036 * in that case a lookup based on the Name or UUId need to be done instead.
2038 * Returns a new domain object or NULL in case of failure. If the
2039 * domain cannot be found, then VIR_ERR_NO_DOMAIN error is raised.
2041 virDomainPtr
2042 virDomainLookupByID(virConnectPtr conn, int id)
2044 DEBUG("conn=%p, id=%d", conn, id);
2046 virResetLastError();
2048 if (!VIR_IS_CONNECT(conn)) {
2049 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2050 virDispatchError(NULL);
2051 return (NULL);
2053 if (id < 0) {
2054 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2055 goto error;
2058 if (conn->driver->domainLookupByID) {
2059 virDomainPtr ret;
2060 ret = conn->driver->domainLookupByID (conn, id);
2061 if (!ret)
2062 goto error;
2063 return ret;
2066 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2068 error:
2069 virDispatchError(conn);
2070 return NULL;
2074 * virDomainLookupByUUID:
2075 * @conn: pointer to the hypervisor connection
2076 * @uuid: the raw UUID for the domain
2078 * Try to lookup a domain on the given hypervisor based on its UUID.
2080 * Returns a new domain object or NULL in case of failure. If the
2081 * domain cannot be found, then VIR_ERR_NO_DOMAIN error is raised.
2083 virDomainPtr
2084 virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
2086 DEBUG("conn=%p, uuid=%s", conn, uuid);
2088 virResetLastError();
2090 if (!VIR_IS_CONNECT(conn)) {
2091 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2092 virDispatchError(NULL);
2093 return (NULL);
2095 if (uuid == NULL) {
2096 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2097 goto error;
2100 if (conn->driver->domainLookupByUUID) {
2101 virDomainPtr ret;
2102 ret = conn->driver->domainLookupByUUID (conn, uuid);
2103 if (!ret)
2104 goto error;
2105 return ret;
2108 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2110 error:
2111 virDispatchError(conn);
2112 return NULL;
2116 * virDomainLookupByUUIDString:
2117 * @conn: pointer to the hypervisor connection
2118 * @uuidstr: the string UUID for the domain
2120 * Try to lookup a domain on the given hypervisor based on its UUID.
2122 * Returns a new domain object or NULL in case of failure. If the
2123 * domain cannot be found, then VIR_ERR_NO_DOMAIN error is raised.
2125 virDomainPtr
2126 virDomainLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
2128 unsigned char uuid[VIR_UUID_BUFLEN];
2129 DEBUG("conn=%p, uuidstr=%s", conn, uuidstr);
2131 virResetLastError();
2133 if (!VIR_IS_CONNECT(conn)) {
2134 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2135 virDispatchError(NULL);
2136 return (NULL);
2138 if (uuidstr == NULL) {
2139 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2140 goto error;
2143 if (virUUIDParse(uuidstr, uuid) < 0) {
2144 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2145 goto error;
2148 return virDomainLookupByUUID(conn, &uuid[0]);
2150 error:
2151 virDispatchError(conn);
2152 return NULL;
2156 * virDomainLookupByName:
2157 * @conn: pointer to the hypervisor connection
2158 * @name: name for the domain
2160 * Try to lookup a domain on the given hypervisor based on its name.
2162 * Returns a new domain object or NULL in case of failure. If the
2163 * domain cannot be found, then VIR_ERR_NO_DOMAIN error is raised.
2165 virDomainPtr
2166 virDomainLookupByName(virConnectPtr conn, const char *name)
2168 DEBUG("conn=%p, name=%s", conn, name);
2170 virResetLastError();
2172 if (!VIR_IS_CONNECT(conn)) {
2173 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2174 virDispatchError(NULL);
2175 return (NULL);
2177 if (name == NULL) {
2178 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2179 goto error;
2182 if (conn->driver->domainLookupByName) {
2183 virDomainPtr dom;
2184 dom = conn->driver->domainLookupByName (conn, name);
2185 if (!dom)
2186 goto error;
2187 return dom;
2190 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2192 error:
2193 virDispatchError(conn);
2194 return NULL;
2198 * virDomainDestroy:
2199 * @domain: a domain object
2201 * Destroy the domain object. The running instance is shutdown if not down
2202 * already and all resources used by it are given back to the hypervisor. This
2203 * does not free the associated virDomainPtr object.
2204 * This function may require privileged access
2206 * Returns 0 in case of success and -1 in case of failure.
2209 virDomainDestroy(virDomainPtr domain)
2211 virConnectPtr conn;
2213 DEBUG("domain=%p", domain);
2215 virResetLastError();
2217 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2218 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2219 virDispatchError(NULL);
2220 return (-1);
2223 conn = domain->conn;
2224 if (conn->flags & VIR_CONNECT_RO) {
2225 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2226 goto error;
2229 if (conn->driver->domainDestroy) {
2230 int ret;
2231 ret = conn->driver->domainDestroy (domain);
2232 if (ret < 0)
2233 goto error;
2234 return ret;
2237 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2239 error:
2240 virDispatchError(conn);
2241 return -1;
2245 * virDomainFree:
2246 * @domain: a domain object
2248 * Free the domain object. The running instance is kept alive.
2249 * The data structure is freed and should not be used thereafter.
2251 * Returns 0 in case of success and -1 in case of failure.
2254 virDomainFree(virDomainPtr domain)
2256 DEBUG("domain=%p", domain);
2258 virResetLastError();
2260 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2261 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2262 virDispatchError(NULL);
2263 return (-1);
2265 if (virUnrefDomain(domain) < 0) {
2266 virDispatchError(NULL);
2267 return -1;
2269 return(0);
2273 * virDomainRef:
2274 * @domain: the domain to hold a reference on
2276 * Increment the reference count on the domain. For each
2277 * additional call to this method, there shall be a corresponding
2278 * call to virDomainFree to release the reference count, once
2279 * the caller no longer needs the reference to this object.
2281 * This method is typically useful for applications where multiple
2282 * threads are using a connection, and it is required that the
2283 * connection remain open until all threads have finished using
2284 * it. ie, each new thread using a domain would increment
2285 * the reference count.
2287 * Returns 0 in case of success and -1 in case of failure.
2290 virDomainRef(virDomainPtr domain)
2292 if ((!VIR_IS_CONNECTED_DOMAIN(domain))) {
2293 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
2294 virDispatchError(NULL);
2295 return(-1);
2297 virMutexLock(&domain->conn->lock);
2298 DEBUG("domain=%p refs=%d", domain, domain->refs);
2299 domain->refs++;
2300 virMutexUnlock(&domain->conn->lock);
2301 return 0;
2306 * virDomainSuspend:
2307 * @domain: a domain object
2309 * Suspends an active domain, the process is frozen without further access
2310 * to CPU resources and I/O but the memory used by the domain at the
2311 * hypervisor level will stay allocated. Use virDomainResume() to reactivate
2312 * the domain.
2313 * This function may requires privileged access.
2315 * Returns 0 in case of success and -1 in case of failure.
2318 virDomainSuspend(virDomainPtr domain)
2320 virConnectPtr conn;
2321 DEBUG("domain=%p", domain);
2323 virResetLastError();
2325 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2326 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2327 virDispatchError(NULL);
2328 return (-1);
2330 if (domain->conn->flags & VIR_CONNECT_RO) {
2331 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2332 goto error;
2335 conn = domain->conn;
2337 if (conn->driver->domainSuspend) {
2338 int ret;
2339 ret = conn->driver->domainSuspend (domain);
2340 if (ret < 0)
2341 goto error;
2342 return ret;
2345 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2347 error:
2348 virDispatchError(domain->conn);
2349 return -1;
2353 * virDomainResume:
2354 * @domain: a domain object
2356 * Resume a suspended domain, the process is restarted from the state where
2357 * it was frozen by calling virSuspendDomain().
2358 * This function may requires privileged access
2360 * Returns 0 in case of success and -1 in case of failure.
2363 virDomainResume(virDomainPtr domain)
2365 virConnectPtr conn;
2366 DEBUG("domain=%p", domain);
2368 virResetLastError();
2370 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2371 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2372 virDispatchError(NULL);
2373 return (-1);
2375 if (domain->conn->flags & VIR_CONNECT_RO) {
2376 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2377 goto error;
2380 conn = domain->conn;
2382 if (conn->driver->domainResume) {
2383 int ret;
2384 ret = conn->driver->domainResume (domain);
2385 if (ret < 0)
2386 goto error;
2387 return ret;
2390 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2392 error:
2393 virDispatchError(domain->conn);
2394 return -1;
2398 * virDomainSave:
2399 * @domain: a domain object
2400 * @to: path for the output file
2402 * This method will suspend a domain and save its memory contents to
2403 * a file on disk. After the call, if successful, the domain is not
2404 * listed as running anymore (this may be a problem).
2405 * Use virDomainRestore() to restore a domain after saving.
2407 * Returns 0 in case of success and -1 in case of failure.
2410 virDomainSave(virDomainPtr domain, const char *to)
2412 char filepath[4096];
2413 virConnectPtr conn;
2414 DEBUG("domain=%p, to=%s", domain, to);
2416 virResetLastError();
2418 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2419 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2420 virDispatchError(NULL);
2421 return (-1);
2423 if (domain->conn->flags & VIR_CONNECT_RO) {
2424 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2425 goto error;
2427 conn = domain->conn;
2428 if (to == NULL) {
2429 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
2430 goto error;
2434 * We must absolutize the file path as the save is done out of process
2435 * TODO: check for URI when libxml2 is linked in.
2437 if (to[0] != '/') {
2438 unsigned int len, t;
2440 t = strlen(to);
2441 if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL)
2442 return (-1);
2443 len = strlen(filepath);
2444 /* that should be covered by getcwd() semantic, but be 100% sure */
2445 if (len > sizeof(filepath) - (t + 3))
2446 return (-1);
2447 filepath[len] = '/';
2448 strcpy(&filepath[len + 1], to);
2449 to = &filepath[0];
2453 if (conn->driver->domainSave) {
2454 int ret;
2455 ret = conn->driver->domainSave (domain, to);
2456 if (ret < 0)
2457 goto error;
2458 return ret;
2461 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2463 error:
2464 virDispatchError(domain->conn);
2465 return -1;
2469 * virDomainRestore:
2470 * @conn: pointer to the hypervisor connection
2471 * @from: path to the
2473 * This method will restore a domain saved to disk by virDomainSave().
2475 * Returns 0 in case of success and -1 in case of failure.
2478 virDomainRestore(virConnectPtr conn, const char *from)
2480 char filepath[4096];
2481 DEBUG("conn=%p, from=%s", conn, from);
2483 virResetLastError();
2485 if (!VIR_IS_CONNECT(conn)) {
2486 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
2487 virDispatchError(NULL);
2488 return (-1);
2490 if (conn->flags & VIR_CONNECT_RO) {
2491 virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2492 goto error;
2494 if (from == NULL) {
2495 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2496 goto error;
2500 * We must absolutize the file path as the restore is done out of process
2501 * TODO: check for URI when libxml2 is linked in.
2503 if (from[0] != '/') {
2504 unsigned int len, t;
2506 t = strlen(from);
2507 if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL) {
2508 virLibConnError(conn, VIR_ERR_SYSTEM_ERROR,
2509 _("cannot get working directory"));
2510 goto error;
2512 len = strlen(filepath);
2513 /* that should be covered by getcwd() semantic, but be 100% sure */
2514 if (len > sizeof(filepath) - (t + 3)) {
2515 virLibConnError(conn, VIR_ERR_INTERNAL_ERROR,
2516 _("path too long"));
2517 goto error;
2519 filepath[len] = '/';
2520 strcpy(&filepath[len + 1], from);
2521 from = &filepath[0];
2524 if (conn->driver->domainRestore) {
2525 int ret;
2526 ret = conn->driver->domainRestore (conn, from);
2527 if (ret < 0)
2528 goto error;
2529 return ret;
2532 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2534 error:
2535 virDispatchError(conn);
2536 return -1;
2540 * virDomainCoreDump:
2541 * @domain: a domain object
2542 * @to: path for the core file
2543 * @flags: extra flags, currently unused
2545 * This method will dump the core of a domain on a given file for analysis.
2546 * Note that for remote Xen Daemon the file path will be interpreted in
2547 * the remote host.
2549 * Returns 0 in case of success and -1 in case of failure.
2552 virDomainCoreDump(virDomainPtr domain, const char *to, int flags)
2554 char filepath[4096];
2555 virConnectPtr conn;
2556 DEBUG("domain=%p, to=%s, flags=%d", domain, to, flags);
2558 virResetLastError();
2560 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2561 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2562 virDispatchError(NULL);
2563 return (-1);
2565 if (domain->conn->flags & VIR_CONNECT_RO) {
2566 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2567 goto error;
2569 conn = domain->conn;
2570 if (to == NULL) {
2571 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
2572 goto error;
2576 * We must absolutize the file path as the save is done out of process
2577 * TODO: check for URI when libxml2 is linked in.
2579 if (to[0] != '/') {
2580 unsigned int len, t;
2582 t = strlen(to);
2583 if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL) {
2584 virLibDomainError(domain, VIR_ERR_SYSTEM_ERROR,
2585 _("cannot get current directory"));
2586 goto error;
2588 len = strlen(filepath);
2589 /* that should be covered by getcwd() semantic, but be 100% sure */
2590 if (len > sizeof(filepath) - (t + 3)) {
2591 virLibDomainError(domain, VIR_ERR_INTERNAL_ERROR,
2592 _("path too long"));
2593 goto error;
2595 filepath[len] = '/';
2596 strcpy(&filepath[len + 1], to);
2597 to = &filepath[0];
2601 if (conn->driver->domainCoreDump) {
2602 int ret;
2603 ret = conn->driver->domainCoreDump (domain, to, flags);
2604 if (ret < 0)
2605 goto error;
2606 return ret;
2609 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2611 error:
2612 virDispatchError(domain->conn);
2613 return -1;
2617 * virDomainShutdown:
2618 * @domain: a domain object
2620 * Shutdown a domain, the domain object is still usable there after but
2621 * the domain OS is being stopped. Note that the guest OS may ignore the
2622 * request.
2624 * TODO: should we add an option for reboot, knowing it may not be doable
2625 * in the general case ?
2627 * Returns 0 in case of success and -1 in case of failure.
2630 virDomainShutdown(virDomainPtr domain)
2632 virConnectPtr conn;
2633 DEBUG("domain=%p", domain);
2635 virResetLastError();
2637 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2638 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2639 virDispatchError(NULL);
2640 return (-1);
2642 if (domain->conn->flags & VIR_CONNECT_RO) {
2643 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2644 goto error;
2647 conn = domain->conn;
2649 if (conn->driver->domainShutdown) {
2650 int ret;
2651 ret = conn->driver->domainShutdown (domain);
2652 if (ret < 0)
2653 goto error;
2654 return ret;
2657 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2659 error:
2660 virDispatchError(domain->conn);
2661 return -1;
2665 * virDomainReboot:
2666 * @domain: a domain object
2667 * @flags: extra flags for the reboot operation, not used yet
2669 * Reboot a domain, the domain object is still usable there after but
2670 * the domain OS is being stopped for a restart.
2671 * Note that the guest OS may ignore the request.
2673 * Returns 0 in case of success and -1 in case of failure.
2676 virDomainReboot(virDomainPtr domain, unsigned int flags)
2678 virConnectPtr conn;
2679 DEBUG("domain=%p, flags=%u", domain, flags);
2681 virResetLastError();
2683 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2684 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2685 virDispatchError(NULL);
2686 return (-1);
2688 if (domain->conn->flags & VIR_CONNECT_RO) {
2689 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2690 goto error;
2693 conn = domain->conn;
2695 if (conn->driver->domainReboot) {
2696 int ret;
2697 ret = conn->driver->domainReboot (domain, flags);
2698 if (ret < 0)
2699 goto error;
2700 return ret;
2703 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2705 error:
2706 virDispatchError(domain->conn);
2707 return -1;
2711 * virDomainGetName:
2712 * @domain: a domain object
2714 * Get the public name for that domain
2716 * Returns a pointer to the name or NULL, the string need not be deallocated
2717 * its lifetime will be the same as the domain object.
2719 const char *
2720 virDomainGetName(virDomainPtr domain)
2722 DEBUG("domain=%p", domain);
2724 virResetLastError();
2726 if (!VIR_IS_DOMAIN(domain)) {
2727 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2728 virDispatchError(NULL);
2729 return (NULL);
2731 return (domain->name);
2735 * virDomainGetUUID:
2736 * @domain: a domain object
2737 * @uuid: pointer to a VIR_UUID_BUFLEN bytes array
2739 * Get the UUID for a domain
2741 * Returns -1 in case of error, 0 in case of success
2744 virDomainGetUUID(virDomainPtr domain, unsigned char *uuid)
2746 DEBUG("domain=%p, uuid=%p", domain, uuid);
2748 virResetLastError();
2750 if (!VIR_IS_DOMAIN(domain)) {
2751 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2752 virDispatchError(NULL);
2753 return (-1);
2755 if (uuid == NULL) {
2756 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
2757 virDispatchError(domain->conn);
2758 return (-1);
2761 memcpy(uuid, &domain->uuid[0], VIR_UUID_BUFLEN);
2763 return (0);
2767 * virDomainGetUUIDString:
2768 * @domain: a domain object
2769 * @buf: pointer to a VIR_UUID_STRING_BUFLEN bytes array
2771 * Get the UUID for a domain as string. For more information about
2772 * UUID see RFC4122.
2774 * Returns -1 in case of error, 0 in case of success
2777 virDomainGetUUIDString(virDomainPtr domain, char *buf)
2779 unsigned char uuid[VIR_UUID_BUFLEN];
2780 DEBUG("domain=%p, buf=%p", domain, buf);
2782 virResetLastError();
2784 if (!VIR_IS_DOMAIN(domain)) {
2785 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2786 virDispatchError(NULL);
2787 return (-1);
2789 if (buf == NULL) {
2790 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
2791 goto error;
2794 if (virDomainGetUUID(domain, &uuid[0]))
2795 goto error;
2797 virUUIDFormat(uuid, buf);
2798 return (0);
2800 error:
2801 virDispatchError(domain->conn);
2802 return -1;
2806 * virDomainGetID:
2807 * @domain: a domain object
2809 * Get the hypervisor ID number for the domain
2811 * Returns the domain ID number or (unsigned int) -1 in case of error
2813 unsigned int
2814 virDomainGetID(virDomainPtr domain)
2816 DEBUG("domain=%p", domain);
2818 virResetLastError();
2820 if (!VIR_IS_DOMAIN(domain)) {
2821 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2822 virDispatchError(NULL);
2823 return ((unsigned int) -1);
2825 return (domain->id);
2829 * virDomainGetOSType:
2830 * @domain: a domain object
2832 * Get the type of domain operation system.
2834 * Returns the new string or NULL in case of error, the string must be
2835 * freed by the caller.
2837 char *
2838 virDomainGetOSType(virDomainPtr domain)
2840 virConnectPtr conn;
2841 DEBUG("domain=%p", domain);
2843 virResetLastError();
2845 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2846 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2847 virDispatchError(NULL);
2848 return (NULL);
2851 conn = domain->conn;
2853 if (conn->driver->domainGetOSType) {
2854 char *ret;
2855 ret = conn->driver->domainGetOSType (domain);
2856 if (!ret)
2857 goto error;
2858 return ret;
2861 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2863 error:
2864 virDispatchError(domain->conn);
2865 return NULL;
2869 * virDomainGetMaxMemory:
2870 * @domain: a domain object or NULL
2872 * Retrieve the maximum amount of physical memory allocated to a
2873 * domain. If domain is NULL, then this get the amount of memory reserved
2874 * to Domain0 i.e. the domain where the application runs.
2876 * Returns the memory size in kilobytes or 0 in case of error.
2878 unsigned long
2879 virDomainGetMaxMemory(virDomainPtr domain)
2881 virConnectPtr conn;
2882 DEBUG("domain=%p", domain);
2884 virResetLastError();
2886 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2887 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2888 virDispatchError(NULL);
2889 return (0);
2892 conn = domain->conn;
2894 if (conn->driver->domainGetMaxMemory) {
2895 unsigned long ret;
2896 ret = conn->driver->domainGetMaxMemory (domain);
2897 if (ret == 0)
2898 goto error;
2899 return ret;
2902 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2904 error:
2905 virDispatchError(domain->conn);
2906 return 0;
2910 * virDomainSetMaxMemory:
2911 * @domain: a domain object or NULL
2912 * @memory: the memory size in kilobytes
2914 * Dynamically change the maximum amount of physical memory allocated to a
2915 * domain. If domain is NULL, then this change the amount of memory reserved
2916 * to Domain0 i.e. the domain where the application runs.
2917 * This function requires privileged access to the hypervisor.
2919 * This command only changes the runtime configuration of the domain,
2920 * so can only be called on an active domain.
2922 * Returns 0 in case of success and -1 in case of failure.
2925 virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
2927 virConnectPtr conn;
2928 DEBUG("domain=%p, memory=%lu", domain, memory);
2930 virResetLastError();
2932 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2933 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2934 virDispatchError(NULL);
2935 return (-1);
2937 if (domain->conn->flags & VIR_CONNECT_RO) {
2938 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2939 goto error;
2941 if (memory < 4096) {
2942 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
2943 goto error;
2945 conn = domain->conn;
2947 if (conn->driver->domainSetMaxMemory) {
2948 int ret;
2949 ret = conn->driver->domainSetMaxMemory (domain, memory);
2950 if (ret < 0)
2951 goto error;
2952 return ret;
2955 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
2957 error:
2958 virDispatchError(domain->conn);
2959 return -1;
2963 * virDomainSetMemory:
2964 * @domain: a domain object or NULL
2965 * @memory: the memory size in kilobytes
2967 * Dynamically change the target amount of physical memory allocated to a
2968 * domain. If domain is NULL, then this change the amount of memory reserved
2969 * to Domain0 i.e. the domain where the application runs.
2970 * This function may requires privileged access to the hypervisor.
2972 * This command only changes the runtime configuration of the domain,
2973 * so can only be called on an active domain.
2975 * Returns 0 in case of success and -1 in case of failure.
2978 virDomainSetMemory(virDomainPtr domain, unsigned long memory)
2980 virConnectPtr conn;
2981 DEBUG("domain=%p, memory=%lu", domain, memory);
2983 virResetLastError();
2985 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
2986 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
2987 virDispatchError(NULL);
2988 return (-1);
2990 if (domain->conn->flags & VIR_CONNECT_RO) {
2991 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
2992 goto error;
2994 if (memory < 4096) {
2995 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
2996 goto error;
2999 conn = domain->conn;
3001 if (conn->driver->domainSetMemory) {
3002 int ret;
3003 ret = conn->driver->domainSetMemory (domain, memory);
3004 if (ret < 0)
3005 goto error;
3006 return ret;
3009 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3011 error:
3012 virDispatchError(domain->conn);
3013 return -1;
3017 * virDomainSetMemoryParameters:
3018 * @domain: pointer to domain object
3019 * @params: pointer to memory parameter objects
3020 * @nparams: number of memory parameter (this value should be same or
3021 * less than the number of parameters supported)
3022 * @flags: currently unused, for future extension
3024 * Change the memory tunables
3025 * This function requires privileged access to the hypervisor.
3027 * Returns -1 in case of error, 0 in case of success.
3030 virDomainSetMemoryParameters(virDomainPtr domain,
3031 virMemoryParameterPtr params,
3032 int nparams, unsigned int flags)
3034 virConnectPtr conn;
3035 DEBUG("domain=%p, params=%p, nparams=%d, flags=%u", domain, params, nparams, flags);
3037 virResetLastError();
3039 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
3040 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
3041 virDispatchError(NULL);
3042 return -1;
3044 if (domain->conn->flags & VIR_CONNECT_RO) {
3045 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3046 goto error;
3048 if ((nparams <= 0) || (params == NULL)) {
3049 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
3050 goto error;
3052 conn = domain->conn;
3054 if (conn->driver->domainSetMemoryParameters) {
3055 int ret;
3056 ret = conn->driver->domainSetMemoryParameters (domain, params, nparams, flags);
3057 if (ret < 0)
3058 goto error;
3059 return ret;
3062 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3064 error:
3065 virDispatchError(domain->conn);
3066 return -1;
3070 * virDomainGetMemoryParameters:
3071 * @domain: pointer to domain object
3072 * @params: pointer to memory parameter object
3073 * (return value, allocated by the caller)
3074 * @nparams: pointer to number of memory parameters
3075 * @flags: currently unused, for future extension
3077 * Get the memory parameters, the @params array will be filled with the values
3078 * equal to the number of parameters suggested by @nparams
3080 * As the value of @nparams is dynamic, call the API setting @nparams to 0 and
3081 * @params as NULL, the API returns the number of parameters supported by the
3082 * HV by updating @nparams on SUCCESS. The caller should then allocate @params
3083 * array, i.e. (sizeof(@virMemoryParameter) * @nparams) bytes and call the API
3084 * again.
3086 * Here is the sample code snippet:
3088 * if ((virDomainGetMemoryParameters(dom, NULL, &nparams, 0) == 0) &&
3089 * (nparams != 0)) {
3090 * params = vshMalloc(ctl, sizeof(virMemoryParameter) * nparams);
3091 * memset(params, 0, sizeof(virMemoryParameter) * nparams);
3092 * if (virDomainGetMemoryParameters(dom, params, &nparams, 0)) {
3093 * vshError(ctl, "%s", _("Unable to get memory parameters"));
3094 * goto error;
3098 * This function requires privileged access to the hypervisor. This function
3099 * expects the caller to allocate the @param
3101 * Returns -1 in case of error, 0 in case of success.
3104 virDomainGetMemoryParameters(virDomainPtr domain,
3105 virMemoryParameterPtr params,
3106 int *nparams, unsigned int flags)
3108 virConnectPtr conn;
3109 DEBUG("domain=%p, params=%p, nparams=%d, flags=%u", domain, params, (nparams)?*nparams:-1, flags);
3111 virResetLastError();
3113 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
3114 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
3115 virDispatchError(NULL);
3116 return -1;
3118 if (domain->conn->flags & VIR_CONNECT_RO) {
3119 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3120 goto error;
3122 if ((nparams == NULL) || (*nparams < 0)) {
3123 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
3124 goto error;
3126 conn = domain->conn;
3128 if (conn->driver->domainGetMemoryParameters) {
3129 int ret;
3130 ret = conn->driver->domainGetMemoryParameters (domain, params, nparams, flags);
3131 if (ret < 0)
3132 goto error;
3133 return ret;
3135 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3137 error:
3138 virDispatchError(domain->conn);
3139 return -1;
3143 * virDomainGetInfo:
3144 * @domain: a domain object
3145 * @info: pointer to a virDomainInfo structure allocated by the user
3147 * Extract information about a domain. Note that if the connection
3148 * used to get the domain is limited only a partial set of the information
3149 * can be extracted.
3151 * Returns 0 in case of success and -1 in case of failure.
3154 virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
3156 virConnectPtr conn;
3157 DEBUG("domain=%p, info=%p", domain, info);
3159 virResetLastError();
3161 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
3162 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
3163 virDispatchError(NULL);
3164 return (-1);
3166 if (info == NULL) {
3167 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
3168 goto error;
3171 memset(info, 0, sizeof(virDomainInfo));
3173 conn = domain->conn;
3175 if (conn->driver->domainGetInfo) {
3176 int ret;
3177 ret = conn->driver->domainGetInfo (domain, info);
3178 if (ret < 0)
3179 goto error;
3180 return ret;
3183 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3185 error:
3186 virDispatchError(domain->conn);
3187 return -1;
3191 * virDomainGetXMLDesc:
3192 * @domain: a domain object
3193 * @flags: an OR'ed set of virDomainXMLFlags
3195 * Provide an XML description of the domain. The description may be reused
3196 * later to relaunch the domain with virDomainCreateXML().
3198 * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
3199 * the caller must free() the returned value.
3201 char *
3202 virDomainGetXMLDesc(virDomainPtr domain, int flags)
3204 virConnectPtr conn;
3205 DEBUG("domain=%p, flags=%d", domain, flags);
3207 virResetLastError();
3209 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
3210 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
3211 virDispatchError(NULL);
3212 return (NULL);
3215 conn = domain->conn;
3217 if ((conn->flags & VIR_CONNECT_RO) && (flags & VIR_DOMAIN_XML_SECURE)) {
3218 virLibConnError(conn, VIR_ERR_OPERATION_DENIED,
3219 _("virDomainGetXMLDesc with secure flag"));
3220 goto error;
3223 flags &= VIR_DOMAIN_XML_FLAGS_MASK;
3225 if (conn->driver->domainDumpXML) {
3226 char *ret;
3227 ret = conn->driver->domainDumpXML (domain, flags);
3228 if (!ret)
3229 goto error;
3230 return ret;
3233 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3235 error:
3236 virDispatchError(domain->conn);
3237 return NULL;
3241 * virConnectDomainXMLFromNative:
3242 * @conn: a connection object
3243 * @nativeFormat: configuration format importing from
3244 * @nativeConfig: the configuration data to import
3245 * @flags: currently unused, pass 0
3247 * Reads native configuration data describing a domain, and
3248 * generates libvirt domain XML. The format of the native
3249 * data is hypervisor dependant.
3251 * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
3252 * the caller must free() the returned value.
3254 char *virConnectDomainXMLFromNative(virConnectPtr conn,
3255 const char *nativeFormat,
3256 const char *nativeConfig,
3257 unsigned int flags)
3259 DEBUG("conn=%p, format=%s config=%s flags=%u", conn, nativeFormat, nativeConfig, flags);
3261 virResetLastError();
3263 if (!VIR_IS_CONNECT(conn)) {
3264 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
3265 virDispatchError(NULL);
3266 return (NULL);
3269 if (nativeFormat == NULL || nativeConfig == NULL) {
3270 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3271 goto error;
3274 if (conn->driver->domainXMLFromNative) {
3275 char *ret;
3276 ret = conn->driver->domainXMLFromNative (conn,
3277 nativeFormat,
3278 nativeConfig,
3279 flags);
3280 if (!ret)
3281 goto error;
3282 return ret;
3285 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3287 error:
3288 virDispatchError(conn);
3289 return NULL;
3293 * virConnectDomainXMLToNative:
3294 * @conn: a connection object
3295 * @nativeFormat: configuration format exporting to
3296 * @domainXml: the domain configuration to export
3297 * @flags: currently unused, pass 0
3299 * Reads a domain XML configuration document, and generates
3300 * generates a native configuration file describing the domain.
3301 * The format of the native data is hypervisor dependant.
3303 * Returns a 0 terminated UTF-8 encoded native config datafile, or NULL in case of error.
3304 * the caller must free() the returned value.
3306 char *virConnectDomainXMLToNative(virConnectPtr conn,
3307 const char *nativeFormat,
3308 const char *domainXml,
3309 unsigned int flags)
3311 DEBUG("conn=%p, format=%s xml=%s flags=%u", conn, nativeFormat, domainXml, flags);
3313 virResetLastError();
3315 if (!VIR_IS_CONNECT(conn)) {
3316 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
3317 virDispatchError(NULL);
3318 return (NULL);
3321 if (nativeFormat == NULL || domainXml == NULL) {
3322 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3323 goto error;
3326 if (conn->driver->domainXMLToNative) {
3327 char *ret;
3328 ret = conn->driver->domainXMLToNative(conn,
3329 nativeFormat,
3330 domainXml,
3331 flags);
3332 if (!ret)
3333 goto error;
3334 return ret;
3337 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3339 error:
3340 virDispatchError(conn);
3341 return NULL;
3345 static virDomainPtr
3346 virDomainMigrateVersion1 (virDomainPtr domain,
3347 virConnectPtr dconn,
3348 unsigned long flags,
3349 const char *dname,
3350 const char *uri,
3351 unsigned long bandwidth)
3353 virDomainPtr ddomain = NULL;
3354 char *uri_out = NULL;
3355 char *cookie = NULL;
3356 int cookielen = 0, ret;
3357 virDomainInfo info;
3359 ret = virDomainGetInfo (domain, &info);
3360 if (ret == 0 && info.state == VIR_DOMAIN_PAUSED) {
3361 flags |= VIR_MIGRATE_PAUSED;
3364 /* Prepare the migration.
3366 * The destination host may return a cookie, or leave cookie as
3367 * NULL.
3369 * The destination host MUST set uri_out if uri_in is NULL.
3371 * If uri_in is non-NULL, then the destination host may modify
3372 * the URI by setting uri_out. If it does not wish to modify
3373 * the URI, it should leave uri_out as NULL.
3375 if (dconn->driver->domainMigratePrepare
3376 (dconn, &cookie, &cookielen, uri, &uri_out, flags, dname,
3377 bandwidth) == -1)
3378 goto done;
3380 if (uri == NULL && uri_out == NULL) {
3381 virLibConnError (domain->conn, VIR_ERR_INTERNAL_ERROR,
3382 _("domainMigratePrepare did not set uri"));
3383 goto done;
3385 if (uri_out)
3386 uri = uri_out; /* Did domainMigratePrepare change URI? */
3387 assert (uri != NULL);
3389 /* Perform the migration. The driver isn't supposed to return
3390 * until the migration is complete.
3392 if (domain->conn->driver->domainMigratePerform
3393 (domain, cookie, cookielen, uri, flags, dname, bandwidth) == -1)
3394 goto done;
3396 /* Get the destination domain and return it or error.
3397 * 'domain' no longer actually exists at this point
3398 * (or so we hope), but we still use the object in memory
3399 * in order to get the name.
3401 dname = dname ? dname : domain->name;
3402 if (dconn->driver->domainMigrateFinish)
3403 ddomain = dconn->driver->domainMigrateFinish
3404 (dconn, dname, cookie, cookielen, uri, flags);
3405 else
3406 ddomain = virDomainLookupByName (dconn, dname);
3408 done:
3409 VIR_FREE (uri_out);
3410 VIR_FREE (cookie);
3411 return ddomain;
3414 static virDomainPtr
3415 virDomainMigrateVersion2 (virDomainPtr domain,
3416 virConnectPtr dconn,
3417 unsigned long flags,
3418 const char *dname,
3419 const char *uri,
3420 unsigned long bandwidth)
3422 virDomainPtr ddomain = NULL;
3423 char *uri_out = NULL;
3424 char *cookie = NULL;
3425 char *dom_xml = NULL;
3426 int cookielen = 0, ret;
3427 virDomainInfo info;
3428 virErrorPtr orig_err = NULL;
3430 /* Prepare the migration.
3432 * The destination host may return a cookie, or leave cookie as
3433 * NULL.
3435 * The destination host MUST set uri_out if uri_in is NULL.
3437 * If uri_in is non-NULL, then the destination host may modify
3438 * the URI by setting uri_out. If it does not wish to modify
3439 * the URI, it should leave uri_out as NULL.
3442 /* In version 2 of the protocol, the prepare step is slightly
3443 * different. We fetch the domain XML of the source domain
3444 * and pass it to Prepare2.
3446 if (!domain->conn->driver->domainDumpXML) {
3447 virLibConnError (domain->conn, VIR_ERR_INTERNAL_ERROR, __FUNCTION__);
3448 virDispatchError(domain->conn);
3449 return NULL;
3451 dom_xml = domain->conn->driver->domainDumpXML (domain,
3452 VIR_DOMAIN_XML_SECURE |
3453 VIR_DOMAIN_XML_UPDATE_CPU);
3454 if (!dom_xml)
3455 return NULL;
3457 ret = virDomainGetInfo (domain, &info);
3458 if (ret == 0 && info.state == VIR_DOMAIN_PAUSED) {
3459 flags |= VIR_MIGRATE_PAUSED;
3462 ret = dconn->driver->domainMigratePrepare2
3463 (dconn, &cookie, &cookielen, uri, &uri_out, flags, dname,
3464 bandwidth, dom_xml);
3465 VIR_FREE (dom_xml);
3466 if (ret == -1)
3467 goto done;
3469 if (uri == NULL && uri_out == NULL) {
3470 virLibConnError (domain->conn, VIR_ERR_INTERNAL_ERROR,
3471 _("domainMigratePrepare2 did not set uri"));
3472 virDispatchError(domain->conn);
3473 goto done;
3475 if (uri_out)
3476 uri = uri_out; /* Did domainMigratePrepare2 change URI? */
3477 assert (uri != NULL);
3479 /* Perform the migration. The driver isn't supposed to return
3480 * until the migration is complete.
3482 ret = domain->conn->driver->domainMigratePerform
3483 (domain, cookie, cookielen, uri, flags, dname, bandwidth);
3485 /* Perform failed. Make sure Finish doesn't overwrite the error */
3486 if (ret < 0)
3487 orig_err = virSaveLastError();
3489 /* In version 2 of the migration protocol, we pass the
3490 * status code from the sender to the destination host,
3491 * so it can do any cleanup if the migration failed.
3493 dname = dname ? dname : domain->name;
3494 ddomain = dconn->driver->domainMigrateFinish2
3495 (dconn, dname, cookie, cookielen, uri, flags, ret);
3497 done:
3498 if (orig_err) {
3499 virSetError(orig_err);
3500 virFreeError(orig_err);
3502 VIR_FREE (uri_out);
3503 VIR_FREE (cookie);
3504 return ddomain;
3509 * This is sort of a migration v3
3511 * In this version, the client does not talk to the destination
3512 * libvirtd. The source libvirtd will still try to talk to the
3513 * destination libvirtd though, and will do the prepare/perform/finish
3514 * steps.
3516 static int
3517 virDomainMigratePeer2Peer (virDomainPtr domain,
3518 unsigned long flags,
3519 const char *dname,
3520 const char *uri,
3521 unsigned long bandwidth)
3523 if (!domain->conn->driver->domainMigratePerform) {
3524 virLibConnError (domain->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3525 virDispatchError(domain->conn);
3526 return -1;
3529 /* Perform the migration. The driver isn't supposed to return
3530 * until the migration is complete.
3532 return domain->conn->driver->domainMigratePerform(domain,
3533 NULL, /* cookie */
3534 0, /* cookielen */
3535 uri,
3536 flags,
3537 dname,
3538 bandwidth);
3543 * This is a variation on v1 & 2 migration
3545 * This is for hypervisors which can directly handshake
3546 * without any libvirtd involvement on destination either
3547 * from client, or source libvirt.
3549 * eg, XenD can talk direct to XenD, so libvirtd on dest
3550 * does not need to be involved at all, or even running
3552 static int
3553 virDomainMigrateDirect (virDomainPtr domain,
3554 unsigned long flags,
3555 const char *dname,
3556 const char *uri,
3557 unsigned long bandwidth)
3559 if (!domain->conn->driver->domainMigratePerform) {
3560 virLibConnError (domain->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3561 virDispatchError(domain->conn);
3562 return -1;
3565 /* Perform the migration. The driver isn't supposed to return
3566 * until the migration is complete.
3568 return domain->conn->driver->domainMigratePerform(domain,
3569 NULL, /* cookie */
3570 0, /* cookielen */
3571 uri,
3572 flags,
3573 dname,
3574 bandwidth);
3579 * virDomainMigrate:
3580 * @domain: a domain object
3581 * @dconn: destination host (a connection object)
3582 * @flags: flags
3583 * @dname: (optional) rename domain to this at destination
3584 * @uri: (optional) dest hostname/URI as seen from the source host
3585 * @bandwidth: (optional) specify migration bandwidth limit in Mbps
3587 * Migrate the domain object from its current host to the destination
3588 * host given by dconn (a connection to the destination host).
3590 * Flags may be one of more of the following:
3591 * VIR_MIGRATE_LIVE Do not pause the VM during migration
3592 * VIR_MIGRATE_PEER2PEER Direct connection between source & destination hosts
3593 * VIR_MIGRATE_TUNNELLED Tunnel migration data over the libvirt RPC channel
3594 * VIR_MIGRATE_PERSIST_DEST If the migration is successful, persist the domain
3595 * on the destination host.
3596 * VIR_MIGRATE_UNDEFINE_SOURCE If the migration is successful, undefine the
3597 * domain on the source host.
3598 * VIR_MIGRATE_PAUSED Leave the domain suspended on the remote side.
3600 * VIR_MIGRATE_TUNNELLED requires that VIR_MIGRATE_PEER2PEER be set.
3601 * Applications using the VIR_MIGRATE_PEER2PEER flag will probably
3602 * prefer to invoke virDomainMigrateToURI, avoiding the need to
3603 * open connection to the destination host themselves.
3605 * If a hypervisor supports renaming domains during migration,
3606 * then you may set the dname parameter to the new name (otherwise
3607 * it keeps the same name). If this is not supported by the
3608 * hypervisor, dname must be NULL or else you will get an error.
3610 * If the VIR_MIGRATE_PEER2PEER flag is set, the uri parameter
3611 * must be a valid libvirt connection URI, by which the source
3612 * libvirt driver can connect to the destination libvirt. If
3613 * omitted, the dconn connection object will be queried for its
3614 * current URI.
3616 * If the VIR_MIGRATE_PEER2PEER flag is NOT set, the URI parameter
3617 * takes a hypervisor specific format. The hypervisor capabilities
3618 * XML includes details of the support URI schemes. If omitted
3619 * the dconn will be asked for a default URI.
3621 * In either case it is typically only necessary to specify a
3622 * URI if the destination host has multiple interfaces and a
3623 * specific interface is required to transmit migration data.
3625 * The maximum bandwidth (in Mbps) that will be used to do migration
3626 * can be specified with the bandwidth parameter. If set to 0,
3627 * libvirt will choose a suitable default. Some hypervisors do
3628 * not support this feature and will return an error if bandwidth
3629 * is not 0.
3631 * To see which features are supported by the current hypervisor,
3632 * see virConnectGetCapabilities, /capabilities/host/migration_features.
3634 * There are many limitations on migration imposed by the underlying
3635 * technology - for example it may not be possible to migrate between
3636 * different processors even with the same architecture, or between
3637 * different types of hypervisor.
3639 * Returns the new domain object if the migration was successful,
3640 * or NULL in case of error. Note that the new domain object
3641 * exists in the scope of the destination connection (dconn).
3643 virDomainPtr
3644 virDomainMigrate (virDomainPtr domain,
3645 virConnectPtr dconn,
3646 unsigned long flags,
3647 const char *dname,
3648 const char *uri,
3649 unsigned long bandwidth)
3651 virDomainPtr ddomain = NULL;
3652 DEBUG("domain=%p, dconn=%p, flags=%lu, dname=%s, uri=%s, bandwidth=%lu",
3653 domain, dconn, flags, NULLSTR(dname), NULLSTR(uri), bandwidth);
3655 virResetLastError();
3657 /* First checkout the source */
3658 if (!VIR_IS_CONNECTED_DOMAIN (domain)) {
3659 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
3660 virDispatchError(NULL);
3661 return NULL;
3663 if (domain->conn->flags & VIR_CONNECT_RO) {
3664 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3665 goto error;
3668 /* Now checkout the destination */
3669 if (!VIR_IS_CONNECT(dconn)) {
3670 virLibConnError(domain->conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
3671 goto error;
3673 if (dconn->flags & VIR_CONNECT_RO) {
3674 /* NB, deliberately report error against source object, not dest */
3675 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3676 goto error;
3679 if (flags & VIR_MIGRATE_PEER2PEER) {
3680 if (VIR_DRV_SUPPORTS_FEATURE (domain->conn->driver, domain->conn,
3681 VIR_DRV_FEATURE_MIGRATION_P2P)) {
3682 char *dstURI = NULL;
3683 if (uri == NULL) {
3684 dstURI = virConnectGetURI(dconn);
3685 if (!dstURI)
3686 return NULL;
3689 if (virDomainMigratePeer2Peer(domain, flags, dname, uri ? uri : dstURI, bandwidth) < 0) {
3690 VIR_FREE(dstURI);
3691 goto error;
3693 VIR_FREE(dstURI);
3695 ddomain = virDomainLookupByName (dconn, dname ? dname : domain->name);
3696 } else {
3697 /* This driver does not support peer to peer migration */
3698 virLibConnError (domain->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3699 goto error;
3701 } else {
3702 if (flags & VIR_MIGRATE_TUNNELLED) {
3703 virLibConnError(domain->conn, VIR_ERR_OPERATION_INVALID,
3704 _("cannot perform tunnelled migration without using peer2peer flag"));
3705 goto error;
3708 /* Check that migration is supported by both drivers. */
3709 if (VIR_DRV_SUPPORTS_FEATURE(domain->conn->driver, domain->conn,
3710 VIR_DRV_FEATURE_MIGRATION_V1) &&
3711 VIR_DRV_SUPPORTS_FEATURE(dconn->driver, dconn,
3712 VIR_DRV_FEATURE_MIGRATION_V1))
3713 ddomain = virDomainMigrateVersion1(domain, dconn, flags, dname, uri, bandwidth);
3714 else if (VIR_DRV_SUPPORTS_FEATURE(domain->conn->driver, domain->conn,
3715 VIR_DRV_FEATURE_MIGRATION_V2) &&
3716 VIR_DRV_SUPPORTS_FEATURE(dconn->driver, dconn,
3717 VIR_DRV_FEATURE_MIGRATION_V2))
3718 ddomain = virDomainMigrateVersion2(domain, dconn, flags, dname, uri, bandwidth);
3719 else {
3720 /* This driver does not support any migration method */
3721 virLibConnError(domain->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3722 goto error;
3726 if (ddomain == NULL)
3727 goto error;
3729 return ddomain;
3731 error:
3732 virDispatchError(domain->conn);
3733 return NULL;
3738 * virDomainMigrateToURI:
3739 * @domain: a domain object
3740 * @duri: mandatory URI for the destination host
3741 * @flags: flags
3742 * @dname: (optional) rename domain to this at destination
3743 * @bandwidth: (optional) specify migration bandwidth limit in Mbps
3745 * Migrate the domain object from its current host to the destination
3746 * host given by duri.
3748 * Flags may be one of more of the following:
3749 * VIR_MIGRATE_LIVE Do not pause the VM during migration
3750 * VIR_MIGRATE_PEER2PEER Direct connection between source & destination hosts
3751 * VIR_MIGRATE_TUNNELLED Tunnel migration data over the libvirt RPC channel
3752 * VIR_MIGRATE_PERSIST_DEST If the migration is successful, persist the domain
3753 * on the destination host.
3754 * VIR_MIGRATE_UNDEFINE_SOURCE If the migration is successful, undefine the
3755 * domain on the source host.
3757 * The operation of this API hinges on the VIR_MIGRATE_PEER2PEER flag.
3758 * If the VIR_MIGRATE_PEER2PEER flag is NOT set, the duri parameter
3759 * takes a hypervisor specific format. The uri_transports element of the
3760 * hypervisor capabilities XML includes details of the supported URI
3761 * schemes. Not all hypervisors will support this mode of migration, so
3762 * if the VIR_MIGRATE_PEER2PEER flag is not set, then it may be necessary
3763 * to use the alternative virDomainMigrate API providing and explicit
3764 * virConnectPtr for the destination host.
3766 * If the VIR_MIGRATE_PEER2PEER flag IS set, the duri parameter
3767 * must be a valid libvirt connection URI, by which the source
3768 * libvirt driver can connect to the destination libvirt.
3770 * VIR_MIGRATE_TUNNELLED requires that VIR_MIGRATE_PEER2PEER be set.
3772 * If a hypervisor supports renaming domains during migration,
3773 * the dname parameter specifies the new name for the domain.
3774 * Setting dname to NULL keeps the domain name the same. If domain
3775 * renaming is not supported by the hypervisor, dname must be NULL or
3776 * else an error will be returned.
3778 * The maximum bandwidth (in Mbps) that will be used to do migration
3779 * can be specified with the bandwidth parameter. If set to 0,
3780 * libvirt will choose a suitable default. Some hypervisors do
3781 * not support this feature and will return an error if bandwidth
3782 * is not 0.
3784 * To see which features are supported by the current hypervisor,
3785 * see virConnectGetCapabilities, /capabilities/host/migration_features.
3787 * There are many limitations on migration imposed by the underlying
3788 * technology - for example it may not be possible to migrate between
3789 * different processors even with the same architecture, or between
3790 * different types of hypervisor.
3792 * Returns 0 if the migration succeeded, -1 upon error.
3795 virDomainMigrateToURI (virDomainPtr domain,
3796 const char *duri,
3797 unsigned long flags,
3798 const char *dname,
3799 unsigned long bandwidth)
3801 DEBUG("domain=%p, duri=%p, flags=%lu, dname=%s, bandwidth=%lu",
3802 domain, NULLSTR(duri), flags, NULLSTR(dname), bandwidth);
3804 virResetLastError();
3806 /* First checkout the source */
3807 if (!VIR_IS_CONNECTED_DOMAIN (domain)) {
3808 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
3809 virDispatchError(NULL);
3810 return -1;
3812 if (domain->conn->flags & VIR_CONNECT_RO) {
3813 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3814 goto error;
3817 if (duri == NULL) {
3818 virLibConnError (domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3819 goto error;
3822 if (flags & VIR_MIGRATE_PEER2PEER) {
3823 if (VIR_DRV_SUPPORTS_FEATURE (domain->conn->driver, domain->conn,
3824 VIR_DRV_FEATURE_MIGRATION_P2P)) {
3825 if (virDomainMigratePeer2Peer (domain, flags, dname, duri, bandwidth) < 0)
3826 goto error;
3827 } else {
3828 /* No peer to peer migration supported */
3829 virLibConnError (domain->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3830 goto error;
3832 } else {
3833 if (VIR_DRV_SUPPORTS_FEATURE (domain->conn->driver, domain->conn,
3834 VIR_DRV_FEATURE_MIGRATION_DIRECT)) {
3835 if (virDomainMigrateDirect (domain, flags, dname, duri, bandwidth) < 0)
3836 goto error;
3837 } else {
3838 /* Cannot do a migration with only the perform step */
3839 virLibConnError (domain->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3840 goto error;
3844 return 0;
3846 error:
3847 virDispatchError(domain->conn);
3848 return -1;
3853 * Not for public use. This function is part of the internal
3854 * implementation of migration in the remote case.
3857 virDomainMigratePrepare (virConnectPtr dconn,
3858 char **cookie,
3859 int *cookielen,
3860 const char *uri_in,
3861 char **uri_out,
3862 unsigned long flags,
3863 const char *dname,
3864 unsigned long bandwidth)
3866 VIR_DEBUG("dconn=%p, cookie=%p, cookielen=%p, uri_in=%s, uri_out=%p, "
3867 "flags=%lu, dname=%s, bandwidth=%lu", dconn, cookie, cookielen,
3868 NULLSTR(uri_in), uri_out, flags, NULLSTR(dname), bandwidth);
3870 virResetLastError();
3872 if (!VIR_IS_CONNECT (dconn)) {
3873 virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
3874 virDispatchError(NULL);
3875 return -1;
3878 if (dconn->flags & VIR_CONNECT_RO) {
3879 virLibConnError(dconn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3880 goto error;
3883 if (dconn->driver->domainMigratePrepare) {
3884 int ret;
3885 ret = dconn->driver->domainMigratePrepare (dconn, cookie, cookielen,
3886 uri_in, uri_out,
3887 flags, dname, bandwidth);
3888 if (ret < 0)
3889 goto error;
3890 return ret;
3893 virLibConnError (dconn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3895 error:
3896 virDispatchError(dconn);
3897 return -1;
3901 * Not for public use. This function is part of the internal
3902 * implementation of migration in the remote case.
3905 virDomainMigratePerform (virDomainPtr domain,
3906 const char *cookie,
3907 int cookielen,
3908 const char *uri,
3909 unsigned long flags,
3910 const char *dname,
3911 unsigned long bandwidth)
3913 virConnectPtr conn;
3914 VIR_DEBUG("domain=%p, cookie=%p, cookielen=%d, uri=%s, flags=%lu, "
3915 "dname=%s, bandwidth=%lu", domain, cookie, cookielen, uri, flags,
3916 NULLSTR(dname), bandwidth);
3918 virResetLastError();
3920 if (!VIR_IS_CONNECTED_DOMAIN (domain)) {
3921 virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
3922 virDispatchError(NULL);
3923 return -1;
3925 conn = domain->conn;
3927 if (domain->conn->flags & VIR_CONNECT_RO) {
3928 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3929 goto error;
3932 if (conn->driver->domainMigratePerform) {
3933 int ret;
3934 ret = conn->driver->domainMigratePerform (domain, cookie, cookielen,
3935 uri,
3936 flags, dname, bandwidth);
3937 if (ret < 0)
3938 goto error;
3939 return ret;
3942 virLibDomainError (domain, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3944 error:
3945 virDispatchError(domain->conn);
3946 return -1;
3950 * Not for public use. This function is part of the internal
3951 * implementation of migration in the remote case.
3953 virDomainPtr
3954 virDomainMigrateFinish (virConnectPtr dconn,
3955 const char *dname,
3956 const char *cookie,
3957 int cookielen,
3958 const char *uri,
3959 unsigned long flags)
3961 VIR_DEBUG("dconn=%p, dname=%s, cookie=%p, cookielen=%d, uri=%s, "
3962 "flags=%lu", dconn, NULLSTR(dname), cookie, cookielen,
3963 uri, flags);
3965 virResetLastError();
3967 if (!VIR_IS_CONNECT (dconn)) {
3968 virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
3969 virDispatchError(NULL);
3970 return NULL;
3973 if (dconn->flags & VIR_CONNECT_RO) {
3974 virLibConnError(dconn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
3975 goto error;
3978 if (dconn->driver->domainMigrateFinish) {
3979 virDomainPtr ret;
3980 ret = dconn->driver->domainMigrateFinish (dconn, dname,
3981 cookie, cookielen,
3982 uri, flags);
3983 if (!ret)
3984 goto error;
3985 return ret;
3988 virLibConnError (dconn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
3990 error:
3991 virDispatchError(dconn);
3992 return NULL;
3997 * Not for public use. This function is part of the internal
3998 * implementation of migration in the remote case.
4001 virDomainMigratePrepare2 (virConnectPtr dconn,
4002 char **cookie,
4003 int *cookielen,
4004 const char *uri_in,
4005 char **uri_out,
4006 unsigned long flags,
4007 const char *dname,
4008 unsigned long bandwidth,
4009 const char *dom_xml)
4011 VIR_DEBUG("dconn=%p, cookie=%p, cookielen=%p, uri_in=%s, uri_out=%p,"
4012 "flags=%lu, dname=%s, bandwidth=%lu, dom_xml=%s", dconn,
4013 cookie, cookielen, uri_in, uri_out, flags, NULLSTR(dname),
4014 bandwidth, dom_xml);
4016 virResetLastError();
4018 if (!VIR_IS_CONNECT (dconn)) {
4019 virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
4020 virDispatchError(NULL);
4021 return -1;
4024 if (dconn->flags & VIR_CONNECT_RO) {
4025 virLibConnError(dconn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4026 goto error;
4029 if (dconn->driver->domainMigratePrepare2) {
4030 int ret;
4031 ret = dconn->driver->domainMigratePrepare2 (dconn, cookie, cookielen,
4032 uri_in, uri_out,
4033 flags, dname, bandwidth,
4034 dom_xml);
4035 if (ret < 0)
4036 goto error;
4037 return ret;
4040 virLibConnError (dconn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4042 error:
4043 virDispatchError(dconn);
4044 return -1;
4048 * Not for public use. This function is part of the internal
4049 * implementation of migration in the remote case.
4051 virDomainPtr
4052 virDomainMigrateFinish2 (virConnectPtr dconn,
4053 const char *dname,
4054 const char *cookie,
4055 int cookielen,
4056 const char *uri,
4057 unsigned long flags,
4058 int retcode)
4060 VIR_DEBUG("dconn=%p, dname=%s, cookie=%p, cookielen=%d, uri=%s, "
4061 "flags=%lu, retcode=%d", dconn, NULLSTR(dname), cookie,
4062 cookielen, uri, flags, retcode);
4064 virResetLastError();
4066 if (!VIR_IS_CONNECT (dconn)) {
4067 virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
4068 virDispatchError(NULL);
4069 return NULL;
4072 if (dconn->flags & VIR_CONNECT_RO) {
4073 virLibConnError(dconn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4074 goto error;
4077 if (dconn->driver->domainMigrateFinish2) {
4078 virDomainPtr ret;
4079 ret = dconn->driver->domainMigrateFinish2 (dconn, dname,
4080 cookie, cookielen,
4081 uri, flags,
4082 retcode);
4083 if (!ret)
4084 goto error;
4085 return ret;
4088 virLibConnError (dconn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4090 error:
4091 virDispatchError(dconn);
4092 return NULL;
4097 * Not for public use. This function is part of the internal
4098 * implementation of migration in the remote case.
4101 virDomainMigratePrepareTunnel(virConnectPtr conn,
4102 virStreamPtr st,
4103 unsigned long flags,
4104 const char *dname,
4105 unsigned long bandwidth,
4106 const char *dom_xml)
4109 VIR_DEBUG("conn=%p, stream=%p, flags=%lu, dname=%s, "
4110 "bandwidth=%lu, dom_xml=%s", conn, st, flags,
4111 NULLSTR(dname), bandwidth, dom_xml);
4113 virResetLastError();
4115 if (!VIR_IS_CONNECT(conn)) {
4116 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
4117 virDispatchError(NULL);
4118 return -1;
4121 if (conn->flags & VIR_CONNECT_RO) {
4122 virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4123 goto error;
4126 if (conn != st->conn) {
4127 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4128 goto error;
4131 if (conn->driver->domainMigratePrepareTunnel) {
4132 int rv = conn->driver->domainMigratePrepareTunnel(conn, st,
4133 flags, dname,
4134 bandwidth, dom_xml);
4135 if (rv < 0)
4136 goto error;
4137 return rv;
4140 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4142 error:
4143 virDispatchError(conn);
4144 return -1;
4149 * virNodeGetInfo:
4150 * @conn: pointer to the hypervisor connection
4151 * @info: pointer to a virNodeInfo structure allocated by the user
4153 * Extract hardware information about the node.
4155 * Returns 0 in case of success and -1 in case of failure.
4158 virNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info)
4160 DEBUG("conn=%p, info=%p", conn, info);
4162 virResetLastError();
4164 if (!VIR_IS_CONNECT(conn)) {
4165 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
4166 virDispatchError(NULL);
4167 return (-1);
4169 if (info == NULL) {
4170 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4171 goto error;
4174 if (conn->driver->nodeGetInfo) {
4175 int ret;
4176 ret = conn->driver->nodeGetInfo (conn, info);
4177 if (ret < 0)
4178 goto error;
4179 return ret;
4182 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4184 error:
4185 virDispatchError(conn);
4186 return -1;
4190 * virConnectGetCapabilities:
4191 * @conn: pointer to the hypervisor connection
4193 * Provides capabilities of the hypervisor / driver.
4195 * Returns NULL in case of error, or an XML string
4196 * defining the capabilities.
4197 * The client must free the returned string after use.
4199 char *
4200 virConnectGetCapabilities (virConnectPtr conn)
4202 DEBUG("conn=%p", conn);
4204 virResetLastError();
4206 if (!VIR_IS_CONNECT (conn)) {
4207 virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
4208 virDispatchError(NULL);
4209 return NULL;
4212 if (conn->driver->getCapabilities) {
4213 char *ret;
4214 ret = conn->driver->getCapabilities (conn);
4215 if (!ret)
4216 goto error;
4217 DEBUG("conn=%p ret=%s", conn, ret);
4218 return ret;
4221 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4223 error:
4224 virDispatchError(conn);
4225 return NULL;
4229 * virNodeGetFreeMemory:
4230 * @conn: pointer to the hypervisor connection
4232 * provides the free memory available on the Node
4233 * Note: most libvirt APIs provide memory sizes in kilobytes, but in this
4234 * function the returned value is in bytes. Divide by 1024 as necessary.
4236 * Returns the available free memory in bytes or 0 in case of error
4238 unsigned long long
4239 virNodeGetFreeMemory(virConnectPtr conn)
4241 DEBUG("conn=%p", conn);
4243 virResetLastError();
4245 if (!VIR_IS_CONNECT (conn)) {
4246 virLibConnError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
4247 virDispatchError(NULL);
4248 return 0;
4251 if (conn->driver->getFreeMemory) {
4252 unsigned long long ret;
4253 ret = conn->driver->getFreeMemory (conn);
4254 if (ret == 0)
4255 goto error;
4256 return ret;
4259 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4261 error:
4262 virDispatchError(conn);
4263 return 0;
4267 * virDomainGetSchedulerType:
4268 * @domain: pointer to domain object
4269 * @nparams: number of scheduler parameters(return value)
4271 * Get the scheduler type.
4273 * Returns NULL in case of error. The caller must free the returned string.
4275 char *
4276 virDomainGetSchedulerType(virDomainPtr domain, int *nparams)
4278 virConnectPtr conn;
4279 char *schedtype;
4280 DEBUG("domain=%p, nparams=%p", domain, nparams);
4282 virResetLastError();
4284 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4285 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4286 virDispatchError(NULL);
4287 return NULL;
4289 conn = domain->conn;
4291 if (conn->driver->domainGetSchedulerType){
4292 schedtype = conn->driver->domainGetSchedulerType (domain, nparams);
4293 if (!schedtype)
4294 goto error;
4295 return schedtype;
4298 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4300 error:
4301 virDispatchError(domain->conn);
4302 return NULL;
4307 * virDomainGetSchedulerParameters:
4308 * @domain: pointer to domain object
4309 * @params: pointer to scheduler parameter object
4310 * (return value)
4311 * @nparams: pointer to number of scheduler parameter
4312 * (this value should be same than the returned value
4313 * nparams of virDomainGetSchedulerType)
4315 * Get the scheduler parameters, the @params array will be filled with the
4316 * values.
4318 * Returns -1 in case of error, 0 in case of success.
4321 virDomainGetSchedulerParameters(virDomainPtr domain,
4322 virSchedParameterPtr params, int *nparams)
4324 virConnectPtr conn;
4325 DEBUG("domain=%p, params=%p, nparams=%p", domain, params, nparams);
4327 virResetLastError();
4329 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4330 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4331 virDispatchError(NULL);
4332 return -1;
4334 conn = domain->conn;
4336 if (conn->driver->domainGetSchedulerParameters) {
4337 int ret;
4338 ret = conn->driver->domainGetSchedulerParameters (domain, params, nparams);
4339 if (ret < 0)
4340 goto error;
4341 return ret;
4344 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4346 error:
4347 virDispatchError(domain->conn);
4348 return -1;
4352 * virDomainSetSchedulerParameters:
4353 * @domain: pointer to domain object
4354 * @params: pointer to scheduler parameter objects
4355 * @nparams: number of scheduler parameter
4356 * (this value should be same or less than the returned value
4357 * nparams of virDomainGetSchedulerType)
4359 * Change the scheduler parameters
4361 * Returns -1 in case of error, 0 in case of success.
4364 virDomainSetSchedulerParameters(virDomainPtr domain,
4365 virSchedParameterPtr params, int nparams)
4367 virConnectPtr conn;
4368 DEBUG("domain=%p, params=%p, nparams=%d", domain, params, nparams);
4370 virResetLastError();
4372 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4373 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4374 virDispatchError(NULL);
4375 return -1;
4377 if (domain->conn->flags & VIR_CONNECT_RO) {
4378 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4379 goto error;
4381 conn = domain->conn;
4383 if (conn->driver->domainSetSchedulerParameters) {
4384 int ret;
4385 ret = conn->driver->domainSetSchedulerParameters (domain, params, nparams);
4386 if (ret < 0)
4387 goto error;
4388 return ret;
4391 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4393 error:
4394 virDispatchError(domain->conn);
4395 return -1;
4400 * virDomainBlockStats:
4401 * @dom: pointer to the domain object
4402 * @path: path to the block device
4403 * @stats: block device stats (returned)
4404 * @size: size of stats structure
4406 * This function returns block device (disk) stats for block
4407 * devices attached to the domain.
4409 * The path parameter is the name of the block device. Get this
4410 * by calling virDomainGetXMLDesc and finding the <target dev='...'>
4411 * attribute within //domain/devices/disk. (For example, "xvda").
4413 * Domains may have more than one block device. To get stats for
4414 * each you should make multiple calls to this function.
4416 * Individual fields within the stats structure may be returned
4417 * as -1, which indicates that the hypervisor does not support
4418 * that particular statistic.
4420 * Returns: 0 in case of success or -1 in case of failure.
4423 virDomainBlockStats (virDomainPtr dom, const char *path,
4424 virDomainBlockStatsPtr stats, size_t size)
4426 virConnectPtr conn;
4427 struct _virDomainBlockStats stats2 = { -1, -1, -1, -1, -1 };
4428 DEBUG("domain=%p, path=%s, stats=%p, size=%zi", dom, path, stats, size);
4430 virResetLastError();
4432 if (!VIR_IS_CONNECTED_DOMAIN (dom)) {
4433 virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4434 virDispatchError(NULL);
4435 return -1;
4437 if (!path || !stats || size > sizeof stats2) {
4438 virLibDomainError (dom, VIR_ERR_INVALID_ARG, __FUNCTION__);
4439 goto error;
4441 conn = dom->conn;
4443 if (conn->driver->domainBlockStats) {
4444 if (conn->driver->domainBlockStats (dom, path, &stats2) == -1)
4445 goto error;
4447 memcpy (stats, &stats2, size);
4448 return 0;
4451 virLibDomainError (dom, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4453 error:
4454 virDispatchError(dom->conn);
4455 return -1;
4459 * virDomainInterfaceStats:
4460 * @dom: pointer to the domain object
4461 * @path: path to the interface
4462 * @stats: network interface stats (returned)
4463 * @size: size of stats structure
4465 * This function returns network interface stats for interfaces
4466 * attached to the domain.
4468 * The path parameter is the name of the network interface.
4470 * Domains may have more than one network interface. To get stats for
4471 * each you should make multiple calls to this function.
4473 * Individual fields within the stats structure may be returned
4474 * as -1, which indicates that the hypervisor does not support
4475 * that particular statistic.
4477 * Returns: 0 in case of success or -1 in case of failure.
4480 virDomainInterfaceStats (virDomainPtr dom, const char *path,
4481 virDomainInterfaceStatsPtr stats, size_t size)
4483 virConnectPtr conn;
4484 struct _virDomainInterfaceStats stats2 = { -1, -1, -1, -1,
4485 -1, -1, -1, -1 };
4486 DEBUG("domain=%p, path=%s, stats=%p, size=%zi", dom, path, stats, size);
4488 virResetLastError();
4490 if (!VIR_IS_CONNECTED_DOMAIN (dom)) {
4491 virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4492 virDispatchError(NULL);
4493 return -1;
4495 if (!path || !stats || size > sizeof stats2) {
4496 virLibDomainError (dom, VIR_ERR_INVALID_ARG, __FUNCTION__);
4497 goto error;
4499 conn = dom->conn;
4501 if (conn->driver->domainInterfaceStats) {
4502 if (conn->driver->domainInterfaceStats (dom, path, &stats2) == -1)
4503 goto error;
4505 memcpy (stats, &stats2, size);
4506 return 0;
4509 virLibDomainError (dom, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4511 error:
4512 virDispatchError(dom->conn);
4513 return -1;
4517 * virDomainMemoryStats:
4518 * @dom: pointer to the domain object
4519 * @stats: nr_stats-sized array of stat structures (returned)
4520 * @nr_stats: number of memory statistics requested
4521 * @flags: unused, always pass 0
4523 * This function provides memory statistics for the domain.
4525 * Up to 'nr_stats' elements of 'stats' will be populated with memory statistics
4526 * from the domain. Only statistics supported by the domain, the driver, and
4527 * this version of libvirt will be returned.
4529 * Memory Statistics:
4531 * VIR_DOMAIN_MEMORY_STAT_SWAP_IN:
4532 * The total amount of data read from swap space (in kb).
4533 * VIR_DOMAIN_MEMORY_STAT_SWAP_OUT:
4534 * The total amount of memory written out to swap space (in kb).
4535 * VIR_DOMAIN_MEMORY_STAT_MAJOR_FAULT:
4536 * The number of page faults that required disk IO to service.
4537 * VIR_DOMAIN_MEMORY_STAT_MINOR_FAULT:
4538 * The number of page faults serviced without disk IO.
4539 * VIR_DOMAIN_MEMORY_STAT_UNUSED:
4540 * The amount of memory which is not being used for any purpose (in kb).
4541 * VIR_DOMAIN_MEMORY_STAT_AVAILABLE:
4542 * The total amount of memory available to the domain's OS (in kb).
4544 * Returns: The number of stats provided or -1 in case of failure.
4546 int virDomainMemoryStats (virDomainPtr dom, virDomainMemoryStatPtr stats,
4547 unsigned int nr_stats, unsigned int flags)
4549 virConnectPtr conn;
4550 unsigned long nr_stats_ret = 0;
4551 DEBUG("domain=%p, stats=%p, nr_stats=%u", dom, stats, nr_stats);
4553 if (flags != 0) {
4554 virLibDomainError (dom, VIR_ERR_INVALID_ARG,
4555 _("flags must be zero"));
4556 goto error;
4559 virResetLastError();
4561 if (!VIR_IS_CONNECTED_DOMAIN (dom)) {
4562 virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4563 virDispatchError(NULL);
4564 return -1;
4566 if (!stats || nr_stats == 0)
4567 return 0;
4569 if (nr_stats > VIR_DOMAIN_MEMORY_STAT_NR)
4570 nr_stats = VIR_DOMAIN_MEMORY_STAT_NR;
4572 conn = dom->conn;
4573 if (conn->driver->domainMemoryStats) {
4574 nr_stats_ret = conn->driver->domainMemoryStats (dom, stats, nr_stats);
4575 if (nr_stats_ret == -1)
4576 goto error;
4577 return nr_stats_ret;
4580 virLibDomainError (dom, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4582 error:
4583 virDispatchError(dom->conn);
4584 return -1;
4588 * virDomainBlockPeek:
4589 * @dom: pointer to the domain object
4590 * @path: path to the block device
4591 * @offset: offset within block device
4592 * @size: size to read
4593 * @buffer: return buffer (must be at least size bytes)
4594 * @flags: unused, always pass 0
4596 * This function allows you to read the contents of a domain's
4597 * disk device.
4599 * Typical uses for this are to determine if the domain has
4600 * written a Master Boot Record (indicating that the domain
4601 * has completed installation), or to try to work out the state
4602 * of the domain's filesystems.
4604 * (Note that in the local case you might try to open the
4605 * block device or file directly, but that won't work in the
4606 * remote case, nor if you don't have sufficient permission.
4607 * Hence the need for this call).
4609 * 'path' must be a device or file corresponding to the domain.
4610 * In other words it must be the precise string returned in
4611 * a <disk><source dev='...'/></disk> from
4612 * virDomainGetXMLDesc.
4614 * 'offset' and 'size' represent an area which must lie entirely
4615 * within the device or file. 'size' may be 0 to test if the
4616 * call would succeed.
4618 * 'buffer' is the return buffer and must be at least 'size' bytes.
4620 * NB. The remote driver imposes a 64K byte limit on 'size'.
4621 * For your program to be able to work reliably over a remote
4622 * connection you should split large requests to <= 65536 bytes.
4624 * Returns: 0 in case of success or -1 in case of failure.
4627 virDomainBlockPeek (virDomainPtr dom,
4628 const char *path,
4629 unsigned long long offset /* really 64 bits */,
4630 size_t size,
4631 void *buffer,
4632 unsigned int flags)
4634 virConnectPtr conn;
4635 DEBUG("domain=%p, path=%s, offset=%lld, size=%zi, buffer=%p",
4636 dom, path, offset, size, buffer);
4638 virResetLastError();
4640 if (!VIR_IS_CONNECTED_DOMAIN (dom)) {
4641 virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4642 virDispatchError(NULL);
4643 return -1;
4645 conn = dom->conn;
4647 if (dom->conn->flags & VIR_CONNECT_RO) {
4648 virLibDomainError(dom, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4649 goto error;
4652 if (!path) {
4653 virLibDomainError (dom, VIR_ERR_INVALID_ARG,
4654 _("path is NULL"));
4655 goto error;
4658 if (flags != 0) {
4659 virLibDomainError (dom, VIR_ERR_INVALID_ARG,
4660 _("flags must be zero"));
4661 goto error;
4664 /* Allow size == 0 as an access test. */
4665 if (size > 0 && !buffer) {
4666 virLibDomainError (dom, VIR_ERR_INVALID_ARG,
4667 _("buffer is NULL"));
4668 goto error;
4671 if (conn->driver->domainBlockPeek) {
4672 int ret;
4673 ret =conn->driver->domainBlockPeek (dom, path, offset, size,
4674 buffer, flags);
4675 if (ret < 0)
4676 goto error;
4677 return ret;
4680 virLibDomainError (dom, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4682 error:
4683 virDispatchError(dom->conn);
4684 return -1;
4688 * virDomainMemoryPeek:
4689 * @dom: pointer to the domain object
4690 * @start: start of memory to peek
4691 * @size: size of memory to peek
4692 * @buffer: return buffer (must be at least size bytes)
4693 * @flags: flags, see below
4695 * This function allows you to read the contents of a domain's
4696 * memory.
4698 * The memory which is read is controlled by the 'start', 'size'
4699 * and 'flags' parameters.
4701 * If 'flags' is VIR_MEMORY_VIRTUAL then the 'start' and 'size'
4702 * parameters are interpreted as virtual memory addresses for
4703 * whichever task happens to be running on the domain at the
4704 * moment. Although this sounds haphazard it is in fact what
4705 * you want in order to read Linux kernel state, because it
4706 * ensures that pointers in the kernel image can be interpreted
4707 * coherently.
4709 * 'buffer' is the return buffer and must be at least 'size' bytes.
4710 * 'size' may be 0 to test if the call would succeed.
4712 * NB. The remote driver imposes a 64K byte limit on 'size'.
4713 * For your program to be able to work reliably over a remote
4714 * connection you should split large requests to <= 65536 bytes.
4716 * Returns: 0 in case of success or -1 in case of failure.
4719 virDomainMemoryPeek (virDomainPtr dom,
4720 unsigned long long start /* really 64 bits */,
4721 size_t size,
4722 void *buffer,
4723 unsigned int flags)
4725 virConnectPtr conn;
4726 DEBUG ("domain=%p, start=%lld, size=%zi, buffer=%p, flags=%d",
4727 dom, start, size, buffer, flags);
4729 virResetLastError();
4731 if (!VIR_IS_CONNECTED_DOMAIN (dom)) {
4732 virLibDomainError (NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4733 virDispatchError(NULL);
4734 return -1;
4736 conn = dom->conn;
4738 if (dom->conn->flags & VIR_CONNECT_RO) {
4739 virLibDomainError(dom, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4740 goto error;
4743 /* Note on access to physical memory: A VIR_MEMORY_PHYSICAL flag is
4744 * a possibility. However it isn't really useful unless the caller
4745 * can also access registers, particularly CR3 on x86 in order to
4746 * get the Page Table Directory. Since registers are different on
4747 * every architecture, that would imply another call to get the
4748 * machine registers.
4750 * The QEMU driver handles VIR_MEMORY_VIRTUAL, mapping it
4751 * to the qemu 'memsave' command which does the virtual to physical
4752 * mapping inside qemu.
4754 * The QEMU driver also handles VIR_MEMORY_PHYSICAL, mapping it
4755 * to the qemu 'pmemsave' command.
4757 * At time of writing there is no Xen driver. However the Xen
4758 * hypervisor only lets you map physical pages from other domains,
4759 * and so the Xen driver would have to do the virtual to physical
4760 * mapping by chasing 2, 3 or 4-level page tables from the PTD.
4761 * There is example code in libxc (xc_translate_foreign_address)
4762 * which does this, although we cannot copy this code directly
4763 * because of incompatible licensing.
4766 if (flags != VIR_MEMORY_VIRTUAL && flags != VIR_MEMORY_PHYSICAL) {
4767 virLibDomainError (dom, VIR_ERR_INVALID_ARG,
4768 _("flags parameter must be VIR_MEMORY_VIRTUAL or VIR_MEMORY_PHYSICAL"));
4769 goto error;
4772 /* Allow size == 0 as an access test. */
4773 if (size > 0 && !buffer) {
4774 virLibDomainError (dom, VIR_ERR_INVALID_ARG,
4775 _("buffer is NULL but size is non-zero"));
4776 goto error;
4779 if (conn->driver->domainMemoryPeek) {
4780 int ret;
4781 ret = conn->driver->domainMemoryPeek (dom, start, size,
4782 buffer, flags);
4783 if (ret < 0)
4784 goto error;
4785 return ret;
4788 virLibDomainError (dom, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4790 error:
4791 virDispatchError(dom->conn);
4792 return -1;
4797 * virDomainGetBlockInfo:
4798 * @domain: a domain object
4799 * @path: path to the block device or file
4800 * @info: pointer to a virDomainBlockInfo structure allocated by the user
4801 * @flags: currently unused, pass zero
4803 * Extract information about a domain's block device.
4805 * Returns 0 in case of success and -1 in case of failure.
4808 virDomainGetBlockInfo(virDomainPtr domain, const char *path, virDomainBlockInfoPtr info, unsigned int flags)
4810 virConnectPtr conn;
4811 DEBUG("domain=%p, info=%p flags=%u", domain, info, flags);
4813 virResetLastError();
4815 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4816 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4817 virDispatchError(NULL);
4818 return (-1);
4820 if (info == NULL) {
4821 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
4822 goto error;
4825 memset(info, 0, sizeof(virDomainBlockInfo));
4827 conn = domain->conn;
4829 if (conn->driver->domainGetBlockInfo) {
4830 int ret;
4831 ret = conn->driver->domainGetBlockInfo (domain, path, info, flags);
4832 if (ret < 0)
4833 goto error;
4834 return ret;
4837 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4839 error:
4840 virDispatchError(domain->conn);
4841 return -1;
4845 /************************************************************************
4847 * Handling of defined but not running domains *
4849 ************************************************************************/
4852 * virDomainDefineXML:
4853 * @conn: pointer to the hypervisor connection
4854 * @xml: the XML description for the domain, preferably in UTF-8
4856 * Define a domain, but does not start it.
4857 * This definition is persistent, until explicitly undefined with
4858 * virDomainUndefine(). A previous definition for this domain would be
4859 * overriden if it already exists.
4861 * Returns NULL in case of error, a pointer to the domain otherwise
4863 virDomainPtr
4864 virDomainDefineXML(virConnectPtr conn, const char *xml) {
4865 DEBUG("conn=%p, xml=%s", conn, xml);
4867 virResetLastError();
4869 if (!VIR_IS_CONNECT(conn)) {
4870 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
4871 virDispatchError(NULL);
4872 return (NULL);
4874 if (conn->flags & VIR_CONNECT_RO) {
4875 virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4876 goto error;
4878 if (xml == NULL) {
4879 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4880 goto error;
4883 if (conn->driver->domainDefineXML) {
4884 virDomainPtr ret;
4885 ret = conn->driver->domainDefineXML (conn, xml);
4886 if (!ret)
4887 goto error;
4888 return ret;
4891 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4893 error:
4894 virDispatchError(conn);
4895 return NULL;
4899 * virDomainUndefine:
4900 * @domain: pointer to a defined domain
4902 * Undefine a domain but does not stop it if it is running
4904 * Returns 0 in case of success, -1 in case of error
4907 virDomainUndefine(virDomainPtr domain) {
4908 virConnectPtr conn;
4909 DEBUG("domain=%p", domain);
4911 virResetLastError();
4913 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
4914 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
4915 virDispatchError(NULL);
4916 return (-1);
4918 conn = domain->conn;
4919 if (conn->flags & VIR_CONNECT_RO) {
4920 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
4921 goto error;
4924 if (conn->driver->domainUndefine) {
4925 int ret;
4926 ret = conn->driver->domainUndefine (domain);
4927 if (ret < 0)
4928 goto error;
4929 return ret;
4932 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4934 error:
4935 virDispatchError(domain->conn);
4936 return -1;
4940 * virConnectNumOfDefinedDomains:
4941 * @conn: pointer to the hypervisor connection
4943 * Provides the number of defined but inactive domains.
4945 * Returns the number of domain found or -1 in case of error
4948 virConnectNumOfDefinedDomains(virConnectPtr conn)
4950 DEBUG("conn=%p", conn);
4952 virResetLastError();
4954 if (!VIR_IS_CONNECT(conn)) {
4955 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
4956 virDispatchError(NULL);
4957 return (-1);
4960 if (conn->driver->numOfDefinedDomains) {
4961 int ret;
4962 ret = conn->driver->numOfDefinedDomains (conn);
4963 if (ret < 0)
4964 goto error;
4965 return ret;
4968 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
4970 error:
4971 virDispatchError(conn);
4972 return -1;
4976 * virConnectListDefinedDomains:
4977 * @conn: pointer to the hypervisor connection
4978 * @names: pointer to an array to store the names
4979 * @maxnames: size of the array
4981 * list the defined but inactive domains, stores the pointers to the names
4982 * in @names
4984 * Returns the number of names provided in the array or -1 in case of error
4987 virConnectListDefinedDomains(virConnectPtr conn, char **const names,
4988 int maxnames) {
4989 DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);
4991 virResetLastError();
4993 if (!VIR_IS_CONNECT(conn)) {
4994 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
4995 virDispatchError(NULL);
4996 return (-1);
4999 if ((names == NULL) || (maxnames < 0)) {
5000 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
5001 goto error;
5004 if (conn->driver->listDefinedDomains) {
5005 int ret;
5006 ret = conn->driver->listDefinedDomains (conn, names, maxnames);
5007 if (ret < 0)
5008 goto error;
5009 return ret;
5012 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5014 error:
5015 virDispatchError(conn);
5016 return -1;
5020 * virDomainCreate:
5021 * @domain: pointer to a defined domain
5023 * Launch a defined domain. If the call succeeds the domain moves from the
5024 * defined to the running domains pools.
5026 * Returns 0 in case of success, -1 in case of error
5029 virDomainCreate(virDomainPtr domain) {
5030 virConnectPtr conn;
5031 DEBUG("domain=%p", domain);
5033 virResetLastError();
5035 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5036 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5037 virDispatchError(NULL);
5038 return (-1);
5040 conn = domain->conn;
5041 if (conn->flags & VIR_CONNECT_RO) {
5042 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5043 goto error;
5046 if (conn->driver->domainCreate) {
5047 int ret;
5048 ret = conn->driver->domainCreate (domain);
5049 if (ret < 0)
5050 goto error;
5051 return ret;
5054 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5056 error:
5057 virDispatchError(domain->conn);
5058 return -1;
5062 * virDomainCreateWithFlags:
5063 * @domain: pointer to a defined domain
5064 * @flags: bitwise-or of supported virDomainCreateFlags
5066 * Launch a defined domain. If the call succeeds the domain moves from the
5067 * defined to the running domains pools.
5069 * Returns 0 in case of success, -1 in case of error
5072 virDomainCreateWithFlags(virDomainPtr domain, unsigned int flags) {
5073 virConnectPtr conn;
5074 DEBUG("domain=%p, flags=%d", domain, flags);
5076 virResetLastError();
5078 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5079 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5080 virDispatchError(NULL);
5081 return (-1);
5083 conn = domain->conn;
5084 if (conn->flags & VIR_CONNECT_RO) {
5085 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5086 goto error;
5089 if (conn->driver->domainCreateWithFlags) {
5090 int ret;
5091 ret = conn->driver->domainCreateWithFlags (domain, flags);
5092 if (ret < 0)
5093 goto error;
5094 return ret;
5097 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5099 error:
5100 virDispatchError(domain->conn);
5101 return -1;
5105 * virDomainGetAutostart:
5106 * @domain: a domain object
5107 * @autostart: the value returned
5109 * Provides a boolean value indicating whether the domain
5110 * configured to be automatically started when the host
5111 * machine boots.
5113 * Returns -1 in case of error, 0 in case of success
5116 virDomainGetAutostart(virDomainPtr domain,
5117 int *autostart)
5119 virConnectPtr conn;
5120 DEBUG("domain=%p, autostart=%p", domain, autostart);
5122 virResetLastError();
5124 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5125 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5126 virDispatchError(NULL);
5127 return (-1);
5129 if (!autostart) {
5130 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
5131 goto error;
5134 conn = domain->conn;
5136 if (conn->driver->domainGetAutostart) {
5137 int ret;
5138 ret = conn->driver->domainGetAutostart (domain, autostart);
5139 if (ret < 0)
5140 goto error;
5141 return ret;
5144 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5146 error:
5147 virDispatchError(domain->conn);
5148 return -1;
5152 * virDomainSetAutostart:
5153 * @domain: a domain object
5154 * @autostart: whether the domain should be automatically started 0 or 1
5156 * Configure the domain to be automatically started
5157 * when the host machine boots.
5159 * Returns -1 in case of error, 0 in case of success
5162 virDomainSetAutostart(virDomainPtr domain,
5163 int autostart)
5165 virConnectPtr conn;
5166 DEBUG("domain=%p, autostart=%d", domain, autostart);
5168 virResetLastError();
5170 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5171 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5172 virDispatchError(NULL);
5173 return (-1);
5176 conn = domain->conn;
5178 if (domain->conn->flags & VIR_CONNECT_RO) {
5179 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5180 goto error;
5183 if (conn->driver->domainSetAutostart) {
5184 int ret;
5185 ret = conn->driver->domainSetAutostart (domain, autostart);
5186 if (ret < 0)
5187 goto error;
5188 return ret;
5191 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5193 error:
5194 virDispatchError(domain->conn);
5195 return -1;
5199 * virDomainSetVcpus:
5200 * @domain: pointer to domain object, or NULL for Domain0
5201 * @nvcpus: the new number of virtual CPUs for this domain
5203 * Dynamically change the number of virtual CPUs used by the domain.
5204 * Note that this call may fail if the underlying virtualization hypervisor
5205 * does not support it or if growing the number is arbitrary limited.
5206 * This function requires privileged access to the hypervisor.
5208 * This command only changes the runtime configuration of the domain,
5209 * so can only be called on an active domain. It is hypervisor-dependent
5210 * whether it also affects persistent configuration; for more control,
5211 * use virDomainSetVcpusFlags().
5213 * Returns 0 in case of success, -1 in case of failure.
5217 virDomainSetVcpus(virDomainPtr domain, unsigned int nvcpus)
5219 virConnectPtr conn;
5220 DEBUG("domain=%p, nvcpus=%u", domain, nvcpus);
5222 virResetLastError();
5224 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5225 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5226 virDispatchError(NULL);
5227 return (-1);
5229 if (domain->conn->flags & VIR_CONNECT_RO) {
5230 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5231 goto error;
5234 if (nvcpus < 1) {
5235 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
5236 goto error;
5238 conn = domain->conn;
5240 if (conn->driver->domainSetVcpus) {
5241 int ret;
5242 ret = conn->driver->domainSetVcpus (domain, nvcpus);
5243 if (ret < 0)
5244 goto error;
5245 return ret;
5248 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5250 error:
5251 virDispatchError(domain->conn);
5252 return -1;
5256 * virDomainSetVcpusFlags:
5257 * @domain: pointer to domain object, or NULL for Domain0
5258 * @nvcpus: the new number of virtual CPUs for this domain, must be at least 1
5259 * @flags: an OR'ed set of virDomainVcpuFlags
5261 * Dynamically change the number of virtual CPUs used by the domain.
5262 * Note that this call may fail if the underlying virtualization hypervisor
5263 * does not support it or if growing the number is arbitrary limited.
5264 * This function requires privileged access to the hypervisor.
5266 * @flags must include VIR_DOMAIN_VCPU_LIVE to affect a running
5267 * domain (which may fail if domain is not active), or
5268 * VIR_DOMAIN_VCPU_CONFIG to affect the next boot via the XML
5269 * description of the domain. Both flags may be set.
5271 * If @flags includes VIR_DOMAIN_VCPU_MAXIMUM, then
5272 * VIR_DOMAIN_VCPU_LIVE must be clear, and only the maximum virtual
5273 * CPU limit is altered; generally, this value must be less than or
5274 * equal to virConnectGetMaxVcpus(). Otherwise, this call affects the
5275 * current virtual CPU limit, which must be less than or equal to the
5276 * maximum limit.
5278 * Returns 0 in case of success, -1 in case of failure.
5282 virDomainSetVcpusFlags(virDomainPtr domain, unsigned int nvcpus,
5283 unsigned int flags)
5285 virConnectPtr conn;
5286 DEBUG("domain=%p, nvcpus=%u, flags=%u", domain, nvcpus, flags);
5288 virResetLastError();
5290 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5291 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5292 virDispatchError(NULL);
5293 return (-1);
5295 if (domain->conn->flags & VIR_CONNECT_RO) {
5296 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5297 goto error;
5300 /* Perform some argument validation common to all implementations. */
5301 if (nvcpus < 1 || (unsigned short) nvcpus != nvcpus ||
5302 (flags & (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG)) == 0) {
5303 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
5304 goto error;
5306 conn = domain->conn;
5308 if (conn->driver->domainSetVcpusFlags) {
5309 int ret;
5310 ret = conn->driver->domainSetVcpusFlags (domain, nvcpus, flags);
5311 if (ret < 0)
5312 goto error;
5313 return ret;
5316 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5318 error:
5319 virDispatchError(domain->conn);
5320 return -1;
5324 * virDomainGetVcpusFlags:
5325 * @domain: pointer to domain object, or NULL for Domain0
5326 * @flags: an OR'ed set of virDomainVcpuFlags
5328 * Query the number of virtual CPUs used by the domain. Note that
5329 * this call may fail if the underlying virtualization hypervisor does
5330 * not support it. This function requires privileged access to the
5331 * hypervisor.
5333 * @flags must include either VIR_DOMAIN_VCPU_ACTIVE to query a
5334 * running domain (which will fail if domain is not active), or
5335 * VIR_DOMAIN_VCPU_PERSISTENT to query the XML description of the
5336 * domain. It is an error to set both flags.
5338 * If @flags includes VIR_DOMAIN_VCPU_MAXIMUM, then the maximum
5339 * virtual CPU limit is queried. Otherwise, this call queries the
5340 * current virtual CPU limit.
5342 * Returns 0 in case of success, -1 in case of failure.
5346 virDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
5348 virConnectPtr conn;
5349 DEBUG("domain=%p, flags=%u", domain, flags);
5351 virResetLastError();
5353 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5354 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5355 virDispatchError(NULL);
5356 return (-1);
5359 /* Exactly one of these two flags should be set. */
5360 if (!(flags & VIR_DOMAIN_VCPU_LIVE) == !(flags & VIR_DOMAIN_VCPU_CONFIG)) {
5361 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
5362 goto error;
5364 conn = domain->conn;
5366 if (conn->driver->domainGetVcpusFlags) {
5367 int ret;
5368 ret = conn->driver->domainGetVcpusFlags (domain, flags);
5369 if (ret < 0)
5370 goto error;
5371 return ret;
5374 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5376 error:
5377 virDispatchError(domain->conn);
5378 return -1;
5382 * virDomainPinVcpu:
5383 * @domain: pointer to domain object, or NULL for Domain0
5384 * @vcpu: virtual CPU number
5385 * @cpumap: pointer to a bit map of real CPUs (in 8-bit bytes) (IN)
5386 * Each bit set to 1 means that corresponding CPU is usable.
5387 * Bytes are stored in little-endian order: CPU0-7, 8-15...
5388 * In each byte, lowest CPU number is least significant bit.
5389 * @maplen: number of bytes in cpumap, from 1 up to size of CPU map in
5390 * underlying virtualization system (Xen...).
5391 * If maplen < size, missing bytes are set to zero.
5392 * If maplen > size, failure code is returned.
5394 * Dynamically change the real CPUs which can be allocated to a virtual CPU.
5395 * This function requires privileged access to the hypervisor.
5397 * This command only changes the runtime configuration of the domain,
5398 * so can only be called on an active domain.
5400 * Returns 0 in case of success, -1 in case of failure.
5403 virDomainPinVcpu(virDomainPtr domain, unsigned int vcpu,
5404 unsigned char *cpumap, int maplen)
5406 virConnectPtr conn;
5407 DEBUG("domain=%p, vcpu=%u, cpumap=%p, maplen=%d", domain, vcpu, cpumap, maplen);
5409 virResetLastError();
5411 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5412 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5413 virDispatchError(NULL);
5414 return (-1);
5416 if (domain->conn->flags & VIR_CONNECT_RO) {
5417 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5418 goto error;
5421 if ((vcpu > 32000) || (cpumap == NULL) || (maplen < 1)) {
5422 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
5423 goto error;
5426 conn = domain->conn;
5428 if (conn->driver->domainPinVcpu) {
5429 int ret;
5430 ret = conn->driver->domainPinVcpu (domain, vcpu, cpumap, maplen);
5431 if (ret < 0)
5432 goto error;
5433 return ret;
5436 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5438 error:
5439 virDispatchError(domain->conn);
5440 return -1;
5444 * virDomainGetVcpus:
5445 * @domain: pointer to domain object, or NULL for Domain0
5446 * @info: pointer to an array of virVcpuInfo structures (OUT)
5447 * @maxinfo: number of structures in info array
5448 * @cpumaps: pointer to a bit map of real CPUs for all vcpus of this
5449 * domain (in 8-bit bytes) (OUT)
5450 * If cpumaps is NULL, then no cpumap information is returned by the API.
5451 * It's assumed there is <maxinfo> cpumap in cpumaps array.
5452 * The memory allocated to cpumaps must be (maxinfo * maplen) bytes
5453 * (ie: calloc(maxinfo, maplen)).
5454 * One cpumap inside cpumaps has the format described in
5455 * virDomainPinVcpu() API.
5456 * @maplen: number of bytes in one cpumap, from 1 up to size of CPU map in
5457 * underlying virtualization system (Xen...).
5458 * Must be zero when cpumaps is NULL and positive when it is non-NULL.
5460 * Extract information about virtual CPUs of domain, store it in info array
5461 * and also in cpumaps if this pointer isn't NULL.
5463 * Returns the number of info filled in case of success, -1 in case of failure.
5466 virDomainGetVcpus(virDomainPtr domain, virVcpuInfoPtr info, int maxinfo,
5467 unsigned char *cpumaps, int maplen)
5469 virConnectPtr conn;
5470 DEBUG("domain=%p, info=%p, maxinfo=%d, cpumaps=%p, maplen=%d", domain, info, maxinfo, cpumaps, maplen);
5472 virResetLastError();
5474 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5475 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5476 virDispatchError(NULL);
5477 return (-1);
5479 if ((info == NULL) || (maxinfo < 1)) {
5480 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
5481 goto error;
5484 /* Ensure that domainGetVcpus (aka remoteDomainGetVcpus) does not
5485 try to memcpy anything into a NULL pointer. */
5486 if ((cpumaps == NULL && maplen != 0)
5487 || (cpumaps && maplen <= 0)) {
5488 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
5489 goto error;
5492 conn = domain->conn;
5494 if (conn->driver->domainGetVcpus) {
5495 int ret;
5496 ret = conn->driver->domainGetVcpus (domain, info, maxinfo,
5497 cpumaps, maplen);
5498 if (ret < 0)
5499 goto error;
5500 return ret;
5503 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5505 error:
5506 virDispatchError(domain->conn);
5507 return -1;
5511 * virDomainGetMaxVcpus:
5512 * @domain: pointer to domain object
5514 * Provides the maximum number of virtual CPUs supported for
5515 * the guest VM. If the guest is inactive, this is basically
5516 * the same as virConnectGetMaxVcpus(). If the guest is running
5517 * this will reflect the maximum number of virtual CPUs the
5518 * guest was booted with. For more details, see virDomainGetVcpusFlags().
5520 * Returns the maximum of virtual CPU or -1 in case of error.
5523 virDomainGetMaxVcpus(virDomainPtr domain)
5525 virConnectPtr conn;
5526 DEBUG("domain=%p", domain);
5528 virResetLastError();
5530 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5531 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5532 virDispatchError(NULL);
5533 return (-1);
5536 conn = domain->conn;
5538 if (conn->driver->domainGetMaxVcpus) {
5539 int ret;
5540 ret = conn->driver->domainGetMaxVcpus (domain);
5541 if (ret < 0)
5542 goto error;
5543 return ret;
5546 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5548 error:
5549 virDispatchError(domain->conn);
5550 return -1;
5554 * virDomainGetSecurityLabel:
5555 * @domain: a domain object
5556 * @seclabel: pointer to a virSecurityLabel structure
5558 * Extract security label of an active domain. The 'label' field
5559 * in the @seclabel argument will be initialized to the empty
5560 * string if the domain is not running under a security model.
5562 * Returns 0 in case of success, -1 in case of failure
5565 virDomainGetSecurityLabel(virDomainPtr domain, virSecurityLabelPtr seclabel)
5567 virConnectPtr conn;
5569 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5570 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5571 virDispatchError(NULL);
5572 return -1;
5575 if (seclabel == NULL) {
5576 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
5577 goto error;
5580 conn = domain->conn;
5582 if (conn->driver->domainGetSecurityLabel) {
5583 int ret;
5584 ret = conn->driver->domainGetSecurityLabel(domain, seclabel);
5585 if (ret < 0)
5586 goto error;
5587 return ret;
5590 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5592 error:
5593 virDispatchError(domain->conn);
5594 return -1;
5598 * virNodeGetSecurityModel:
5599 * @conn: a connection object
5600 * @secmodel: pointer to a virSecurityModel structure
5602 * Extract the security model of a hypervisor. The 'model' field
5603 * in the @secmodel argument may be initialized to the empty
5604 * string if the driver has not activated a security model.
5606 * Returns 0 in case of success, -1 in case of failure
5609 virNodeGetSecurityModel(virConnectPtr conn, virSecurityModelPtr secmodel)
5611 if (!VIR_IS_CONNECT(conn)) {
5612 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
5613 virDispatchError(NULL);
5614 return -1;
5617 if (secmodel == NULL) {
5618 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
5619 goto error;
5622 if (conn->driver->nodeGetSecurityModel) {
5623 int ret;
5624 ret = conn->driver->nodeGetSecurityModel(conn, secmodel);
5625 if (ret < 0)
5626 goto error;
5627 return ret;
5630 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5632 error:
5633 virDispatchError(conn);
5634 return -1;
5638 * virDomainAttachDevice:
5639 * @domain: pointer to domain object
5640 * @xml: pointer to XML description of one device
5642 * Create a virtual device attachment to backend. This function,
5643 * having hotplug semantics, is only allowed on an active domain.
5645 * For compatibility, this method can also be used to change the media
5646 * in an existing CDROM/Floppy device, however, applications are
5647 * recommended to use the virDomainUpdateDeviceFlag method instead.
5649 * Returns 0 in case of success, -1 in case of failure.
5652 virDomainAttachDevice(virDomainPtr domain, const char *xml)
5654 virConnectPtr conn;
5655 DEBUG("domain=%p, xml=%s", domain, xml);
5657 virResetLastError();
5659 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5660 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5661 virDispatchError(NULL);
5662 return (-1);
5664 if (domain->conn->flags & VIR_CONNECT_RO) {
5665 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5666 goto error;
5668 conn = domain->conn;
5670 if (conn->driver->domainAttachDevice) {
5671 int ret;
5672 ret = conn->driver->domainAttachDevice (domain, xml);
5673 if (ret < 0)
5674 goto error;
5675 return ret;
5678 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5680 error:
5681 virDispatchError(domain->conn);
5682 return -1;
5686 * virDomainAttachDeviceFlags:
5687 * @domain: pointer to domain object
5688 * @xml: pointer to XML description of one device
5689 * @flags: an OR'ed set of virDomainDeviceModifyFlags
5691 * Attach a virtual device to a domain, using the flags parameter
5692 * to control how the device is attached. VIR_DOMAIN_DEVICE_MODIFY_CURRENT
5693 * specifies that the device allocation is made based on current domain
5694 * state. VIR_DOMAIN_DEVICE_MODIFY_LIVE specifies that the device shall be
5695 * allocated to the active domain instance only and is not added to the
5696 * persisted domain configuration. VIR_DOMAIN_DEVICE_MODIFY_CONFIG
5697 * specifies that the device shall be allocated to the persisted domain
5698 * configuration only. Note that the target hypervisor must return an
5699 * error if unable to satisfy flags. E.g. the hypervisor driver will
5700 * return failure if LIVE is specified but it only supports modifying the
5701 * persisted device allocation.
5703 * For compatibility, this method can also be used to change the media
5704 * in an existing CDROM/Floppy device, however, applications are
5705 * recommended to use the virDomainUpdateDeviceFlag method instead.
5707 * Returns 0 in case of success, -1 in case of failure.
5710 virDomainAttachDeviceFlags(virDomainPtr domain,
5711 const char *xml, unsigned int flags)
5713 virConnectPtr conn;
5714 DEBUG("domain=%p, xml=%s, flags=%d", domain, xml, flags);
5716 virResetLastError();
5718 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5719 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5720 return (-1);
5722 if (domain->conn->flags & VIR_CONNECT_RO) {
5723 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5724 goto error;
5726 conn = domain->conn;
5728 if (conn->driver->domainAttachDeviceFlags) {
5729 int ret;
5730 ret = conn->driver->domainAttachDeviceFlags(domain, xml, flags);
5731 if (ret < 0)
5732 goto error;
5733 return ret;
5736 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5738 error:
5739 virDispatchError(domain->conn);
5740 return -1;
5744 * virDomainDetachDevice:
5745 * @domain: pointer to domain object
5746 * @xml: pointer to XML description of one device
5748 * Destroy a virtual device attachment to backend. This function,
5749 * having hot-unplug semantics, is only allowed on an active domain.
5751 * Returns 0 in case of success, -1 in case of failure.
5754 virDomainDetachDevice(virDomainPtr domain, const char *xml)
5756 virConnectPtr conn;
5757 DEBUG("domain=%p, xml=%s", domain, xml);
5759 virResetLastError();
5761 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5762 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5763 virDispatchError(NULL);
5764 return (-1);
5766 if (domain->conn->flags & VIR_CONNECT_RO) {
5767 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5768 goto error;
5770 conn = domain->conn;
5772 if (conn->driver->domainDetachDevice) {
5773 int ret;
5774 ret = conn->driver->domainDetachDevice (domain, xml);
5775 if (ret < 0)
5776 goto error;
5777 return ret;
5780 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5782 error:
5783 virDispatchError(domain->conn);
5784 return -1;
5788 * virDomainDetachDeviceFlags:
5789 * @domain: pointer to domain object
5790 * @xml: pointer to XML description of one device
5791 * @flags: an OR'ed set of virDomainDeviceModifyFlags
5793 * Detach a virtual device from a domain, using the flags parameter
5794 * to control how the device is detached. VIR_DOMAIN_DEVICE_MODIFY_CURRENT
5795 * specifies that the device allocation is removed based on current domain
5796 * state. VIR_DOMAIN_DEVICE_MODIFY_LIVE specifies that the device shall be
5797 * deallocated from the active domain instance only and is not from the
5798 * persisted domain configuration. VIR_DOMAIN_DEVICE_MODIFY_CONFIG
5799 * specifies that the device shall be deallocated from the persisted domain
5800 * configuration only. Note that the target hypervisor must return an
5801 * error if unable to satisfy flags. E.g. the hypervisor driver will
5802 * return failure if LIVE is specified but it only supports removing the
5803 * persisted device allocation.
5805 * Returns 0 in case of success, -1 in case of failure.
5808 virDomainDetachDeviceFlags(virDomainPtr domain,
5809 const char *xml, unsigned int flags)
5811 virConnectPtr conn;
5812 DEBUG("domain=%p, xml=%s, flags=%d", domain, xml, flags);
5814 virResetLastError();
5816 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5817 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5818 return (-1);
5820 if (domain->conn->flags & VIR_CONNECT_RO) {
5821 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5822 goto error;
5824 conn = domain->conn;
5826 if (conn->driver->domainDetachDeviceFlags) {
5827 int ret;
5828 ret = conn->driver->domainDetachDeviceFlags(domain, xml, flags);
5829 if (ret < 0)
5830 goto error;
5831 return ret;
5834 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5836 error:
5837 virDispatchError(domain->conn);
5838 return -1;
5842 * virDomainUpdateDeviceFlags:
5843 * @domain: pointer to domain object
5844 * @xml: pointer to XML description of one device
5845 * @flags: an OR'ed set of virDomainDeviceModifyFlags
5847 * Change a virtual device on a domain, using the flags parameter
5848 * to control how the device is changed. VIR_DOMAIN_DEVICE_MODIFY_CURRENT
5849 * specifies that the device change is made based on current domain
5850 * state. VIR_DOMAIN_DEVICE_MODIFY_LIVE specifies that the device shall be
5851 * changed on the active domain instance only and is not added to the
5852 * persisted domain configuration. VIR_DOMAIN_DEVICE_MODIFY_CONFIG
5853 * specifies that the device shall be changed on the persisted domain
5854 * configuration only. Note that the target hypervisor must return an
5855 * error if unable to satisfy flags. E.g. the hypervisor driver will
5856 * return failure if LIVE is specified but it only supports modifying the
5857 * persisted device allocation.
5859 * This method is used for actions such changing CDROM/Floppy device
5860 * media, altering the graphics configuration such as password,
5861 * reconfiguring the NIC device backend connectivity, etc.
5863 * Returns 0 in case of success, -1 in case of failure.
5866 virDomainUpdateDeviceFlags(virDomainPtr domain,
5867 const char *xml, unsigned int flags)
5869 virConnectPtr conn;
5870 DEBUG("domain=%p, xml=%s, flags=%d", domain, xml, flags);
5872 virResetLastError();
5874 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
5875 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
5876 return (-1);
5878 if (domain->conn->flags & VIR_CONNECT_RO) {
5879 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
5880 goto error;
5882 conn = domain->conn;
5884 if (conn->driver->domainUpdateDeviceFlags) {
5885 int ret;
5886 ret = conn->driver->domainUpdateDeviceFlags(domain, xml, flags);
5887 if (ret < 0)
5888 goto error;
5889 return ret;
5892 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5894 error:
5895 virDispatchError(domain->conn);
5896 return -1;
5900 * virNodeGetCellsFreeMemory:
5901 * @conn: pointer to the hypervisor connection
5902 * @freeMems: pointer to the array of unsigned long long
5903 * @startCell: index of first cell to return freeMems info on.
5904 * @maxCells: Maximum number of cells for which freeMems information can
5905 * be returned.
5907 * This call returns the amount of free memory in one or more NUMA cells.
5908 * The @freeMems array must be allocated by the caller and will be filled
5909 * with the amount of free memory in bytes for each cell requested,
5910 * starting with startCell (in freeMems[0]), up to either
5911 * (startCell + maxCells), or the number of additional cells in the node,
5912 * whichever is smaller.
5914 * Returns the number of entries filled in freeMems, or -1 in case of error.
5918 virNodeGetCellsFreeMemory(virConnectPtr conn, unsigned long long *freeMems,
5919 int startCell, int maxCells)
5921 DEBUG("conn=%p, freeMems=%p, startCell=%d, maxCells=%d",
5922 conn, freeMems, startCell, maxCells);
5924 virResetLastError();
5926 if (!VIR_IS_CONNECT(conn)) {
5927 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
5928 virDispatchError(NULL);
5929 return (-1);
5932 if ((freeMems == NULL) || (maxCells <= 0) || (startCell < 0)) {
5933 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
5934 goto error;
5937 if (conn->driver->nodeGetCellsFreeMemory) {
5938 int ret;
5939 ret = conn->driver->nodeGetCellsFreeMemory (conn, freeMems, startCell, maxCells);
5940 if (ret < 0)
5941 goto error;
5942 return ret;
5945 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
5947 error:
5948 virDispatchError(conn);
5949 return -1;
5953 * virNetworkGetConnect:
5954 * @net: pointer to a network
5956 * Provides the connection pointer associated with a network. The
5957 * reference counter on the connection is not increased by this
5958 * call.
5960 * WARNING: When writing libvirt bindings in other languages, do
5961 * not use this function. Instead, store the connection and
5962 * the network object together.
5964 * Returns the virConnectPtr or NULL in case of failure.
5966 virConnectPtr
5967 virNetworkGetConnect (virNetworkPtr net)
5969 DEBUG("net=%p", net);
5971 virResetLastError();
5973 if (!VIR_IS_CONNECTED_NETWORK (net)) {
5974 virLibNetworkError (NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
5975 virDispatchError(NULL);
5976 return NULL;
5978 return net->conn;
5982 * virConnectNumOfNetworks:
5983 * @conn: pointer to the hypervisor connection
5985 * Provides the number of active networks.
5987 * Returns the number of network found or -1 in case of error
5990 virConnectNumOfNetworks(virConnectPtr conn)
5992 DEBUG("conn=%p", conn);
5994 virResetLastError();
5996 if (!VIR_IS_CONNECT(conn)) {
5997 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
5998 virDispatchError(NULL);
5999 return (-1);
6002 if (conn->networkDriver && conn->networkDriver->numOfNetworks) {
6003 int ret;
6004 ret = conn->networkDriver->numOfNetworks (conn);
6005 if (ret < 0)
6006 goto error;
6007 return ret;
6010 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6012 error:
6013 virDispatchError(conn);
6014 return -1;
6018 * virConnectListNetworks:
6019 * @conn: pointer to the hypervisor connection
6020 * @names: array to collect the list of names of active networks
6021 * @maxnames: size of @names
6023 * Collect the list of active networks, and store their names in @names
6025 * Returns the number of networks found or -1 in case of error
6028 virConnectListNetworks(virConnectPtr conn, char **const names, int maxnames)
6030 DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);
6032 virResetLastError();
6034 if (!VIR_IS_CONNECT(conn)) {
6035 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
6036 virDispatchError(NULL);
6037 return (-1);
6040 if ((names == NULL) || (maxnames < 0)) {
6041 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6042 goto error;
6045 if (conn->networkDriver && conn->networkDriver->listNetworks) {
6046 int ret;
6047 ret = conn->networkDriver->listNetworks (conn, names, maxnames);
6048 if (ret < 0)
6049 goto error;
6050 return ret;
6053 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6055 error:
6056 virDispatchError(conn);
6057 return -1;
6061 * virConnectNumOfDefinedNetworks:
6062 * @conn: pointer to the hypervisor connection
6064 * Provides the number of inactive networks.
6066 * Returns the number of networks found or -1 in case of error
6069 virConnectNumOfDefinedNetworks(virConnectPtr conn)
6071 DEBUG("conn=%p", conn);
6073 virResetLastError();
6075 if (!VIR_IS_CONNECT(conn)) {
6076 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
6077 virDispatchError(NULL);
6078 return (-1);
6081 if (conn->networkDriver && conn->networkDriver->numOfDefinedNetworks) {
6082 int ret;
6083 ret = conn->networkDriver->numOfDefinedNetworks (conn);
6084 if (ret < 0)
6085 goto error;
6086 return ret;
6089 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6091 error:
6092 virDispatchError(conn);
6093 return -1;
6097 * virConnectListDefinedNetworks:
6098 * @conn: pointer to the hypervisor connection
6099 * @names: pointer to an array to store the names
6100 * @maxnames: size of the array
6102 * list the inactive networks, stores the pointers to the names in @names
6104 * Returns the number of names provided in the array or -1 in case of error
6107 virConnectListDefinedNetworks(virConnectPtr conn, char **const names,
6108 int maxnames)
6110 DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);
6112 virResetLastError();
6114 if (!VIR_IS_CONNECT(conn)) {
6115 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
6116 virDispatchError(NULL);
6117 return (-1);
6120 if ((names == NULL) || (maxnames < 0)) {
6121 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6122 goto error;
6125 if (conn->networkDriver && conn->networkDriver->listDefinedNetworks) {
6126 int ret;
6127 ret = conn->networkDriver->listDefinedNetworks (conn,
6128 names, maxnames);
6129 if (ret < 0)
6130 goto error;
6131 return ret;
6134 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6136 error:
6137 virDispatchError(conn);
6138 return -1;
6142 * virNetworkLookupByName:
6143 * @conn: pointer to the hypervisor connection
6144 * @name: name for the network
6146 * Try to lookup a network on the given hypervisor based on its name.
6148 * Returns a new network object or NULL in case of failure. If the
6149 * network cannot be found, then VIR_ERR_NO_NETWORK error is raised.
6151 virNetworkPtr
6152 virNetworkLookupByName(virConnectPtr conn, const char *name)
6154 DEBUG("conn=%p, name=%s", conn, name);
6156 virResetLastError();
6158 if (!VIR_IS_CONNECT(conn)) {
6159 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
6160 virDispatchError(NULL);
6161 return (NULL);
6163 if (name == NULL) {
6164 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6165 goto error;
6168 if (conn->networkDriver && conn->networkDriver->networkLookupByName) {
6169 virNetworkPtr ret;
6170 ret = conn->networkDriver->networkLookupByName (conn, name);
6171 if (!ret)
6172 goto error;
6173 return ret;
6176 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6178 error:
6179 virDispatchError(conn);
6180 return NULL;
6184 * virNetworkLookupByUUID:
6185 * @conn: pointer to the hypervisor connection
6186 * @uuid: the raw UUID for the network
6188 * Try to lookup a network on the given hypervisor based on its UUID.
6190 * Returns a new network object or NULL in case of failure. If the
6191 * network cannot be found, then VIR_ERR_NO_NETWORK error is raised.
6193 virNetworkPtr
6194 virNetworkLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
6196 DEBUG("conn=%p, uuid=%s", conn, uuid);
6198 virResetLastError();
6200 if (!VIR_IS_CONNECT(conn)) {
6201 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
6202 virDispatchError(NULL);
6203 return (NULL);
6205 if (uuid == NULL) {
6206 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6207 goto error;
6210 if (conn->networkDriver && conn->networkDriver->networkLookupByUUID){
6211 virNetworkPtr ret;
6212 ret = conn->networkDriver->networkLookupByUUID (conn, uuid);
6213 if (!ret)
6214 goto error;
6215 return ret;
6218 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6220 error:
6221 virDispatchError(conn);
6222 return NULL;
6226 * virNetworkLookupByUUIDString:
6227 * @conn: pointer to the hypervisor connection
6228 * @uuidstr: the string UUID for the network
6230 * Try to lookup a network on the given hypervisor based on its UUID.
6232 * Returns a new network object or NULL in case of failure. If the
6233 * network cannot be found, then VIR_ERR_NO_NETWORK error is raised.
6235 virNetworkPtr
6236 virNetworkLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
6238 unsigned char uuid[VIR_UUID_BUFLEN];
6239 DEBUG("conn=%p, uuidstr=%s", conn, uuidstr);
6241 virResetLastError();
6243 if (!VIR_IS_CONNECT(conn)) {
6244 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
6245 virDispatchError(NULL);
6246 return (NULL);
6248 if (uuidstr == NULL) {
6249 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6250 goto error;
6253 if (virUUIDParse(uuidstr, uuid) < 0) {
6254 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6255 goto error;
6258 return virNetworkLookupByUUID(conn, &uuid[0]);
6260 error:
6261 virDispatchError(conn);
6262 return NULL;
6266 * virNetworkCreateXML:
6267 * @conn: pointer to the hypervisor connection
6268 * @xmlDesc: an XML description of the network
6270 * Create and start a new virtual network, based on an XML description
6271 * similar to the one returned by virNetworkGetXMLDesc()
6273 * Returns a new network object or NULL in case of failure
6275 virNetworkPtr
6276 virNetworkCreateXML(virConnectPtr conn, const char *xmlDesc)
6278 DEBUG("conn=%p, xmlDesc=%s", conn, xmlDesc);
6280 virResetLastError();
6282 if (!VIR_IS_CONNECT(conn)) {
6283 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
6284 virDispatchError(NULL);
6285 return (NULL);
6287 if (xmlDesc == NULL) {
6288 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6289 goto error;
6291 if (conn->flags & VIR_CONNECT_RO) {
6292 virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
6293 goto error;
6296 if (conn->networkDriver && conn->networkDriver->networkCreateXML) {
6297 virNetworkPtr ret;
6298 ret = conn->networkDriver->networkCreateXML (conn, xmlDesc);
6299 if (!ret)
6300 goto error;
6301 return ret;
6304 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6306 error:
6307 virDispatchError(conn);
6308 return NULL;
6312 * virNetworkDefineXML:
6313 * @conn: pointer to the hypervisor connection
6314 * @xml: the XML description for the network, preferably in UTF-8
6316 * Define a network, but does not create it
6318 * Returns NULL in case of error, a pointer to the network otherwise
6320 virNetworkPtr
6321 virNetworkDefineXML(virConnectPtr conn, const char *xml)
6323 DEBUG("conn=%p, xml=%s", conn, xml);
6325 virResetLastError();
6327 if (!VIR_IS_CONNECT(conn)) {
6328 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
6329 virDispatchError(NULL);
6330 return (NULL);
6332 if (conn->flags & VIR_CONNECT_RO) {
6333 virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
6334 goto error;
6336 if (xml == NULL) {
6337 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6338 goto error;
6341 if (conn->networkDriver && conn->networkDriver->networkDefineXML) {
6342 virNetworkPtr ret;
6343 ret = conn->networkDriver->networkDefineXML (conn, xml);
6344 if (!ret)
6345 goto error;
6346 return ret;
6349 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6351 error:
6352 virDispatchError(conn);
6353 return NULL;
6357 * virNetworkUndefine:
6358 * @network: pointer to a defined network
6360 * Undefine a network but does not stop it if it is running
6362 * Returns 0 in case of success, -1 in case of error
6365 virNetworkUndefine(virNetworkPtr network) {
6366 virConnectPtr conn;
6367 DEBUG("network=%p", network);
6369 virResetLastError();
6371 if (!VIR_IS_CONNECTED_NETWORK(network)) {
6372 virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
6373 virDispatchError(NULL);
6374 return (-1);
6376 conn = network->conn;
6377 if (conn->flags & VIR_CONNECT_RO) {
6378 virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
6379 goto error;
6382 if (conn->networkDriver && conn->networkDriver->networkUndefine) {
6383 int ret;
6384 ret = conn->networkDriver->networkUndefine (network);
6385 if (ret < 0)
6386 goto error;
6387 return ret;
6390 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6392 error:
6393 virDispatchError(network->conn);
6394 return -1;
6398 * virNetworkCreate:
6399 * @network: pointer to a defined network
6401 * Create and start a defined network. If the call succeed the network
6402 * moves from the defined to the running networks pools.
6404 * Returns 0 in case of success, -1 in case of error
6407 virNetworkCreate(virNetworkPtr network)
6409 virConnectPtr conn;
6410 DEBUG("network=%p", network);
6412 virResetLastError();
6414 if (!VIR_IS_CONNECTED_NETWORK(network)) {
6415 virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
6416 virDispatchError(NULL);
6417 return (-1);
6419 conn = network->conn;
6420 if (conn->flags & VIR_CONNECT_RO) {
6421 virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
6422 goto error;
6425 if (conn->networkDriver && conn->networkDriver->networkCreate) {
6426 int ret;
6427 ret = conn->networkDriver->networkCreate (network);
6428 if (ret < 0)
6429 goto error;
6430 return ret;
6433 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6435 error:
6436 virDispatchError(network->conn);
6437 return -1;
6441 * virNetworkDestroy:
6442 * @network: a network object
6444 * Destroy the network object. The running instance is shutdown if not down
6445 * already and all resources used by it are given back to the hypervisor. This
6446 * does not free the associated virNetworkPtr object.
6447 * This function may require privileged access
6449 * Returns 0 in case of success and -1 in case of failure.
6452 virNetworkDestroy(virNetworkPtr network)
6454 virConnectPtr conn;
6455 DEBUG("network=%p", network);
6457 virResetLastError();
6459 if (!VIR_IS_CONNECTED_NETWORK(network)) {
6460 virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
6461 virDispatchError(NULL);
6462 return (-1);
6465 conn = network->conn;
6466 if (conn->flags & VIR_CONNECT_RO) {
6467 virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
6468 goto error;
6471 if (conn->networkDriver && conn->networkDriver->networkDestroy) {
6472 int ret;
6473 ret = conn->networkDriver->networkDestroy (network);
6474 if (ret < 0)
6475 goto error;
6476 return ret;
6479 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6481 error:
6482 virDispatchError(network->conn);
6483 return -1;
6487 * virNetworkFree:
6488 * @network: a network object
6490 * Free the network object. The running instance is kept alive.
6491 * The data structure is freed and should not be used thereafter.
6493 * Returns 0 in case of success and -1 in case of failure.
6496 virNetworkFree(virNetworkPtr network)
6498 DEBUG("network=%p", network);
6500 virResetLastError();
6502 if (!VIR_IS_CONNECTED_NETWORK(network)) {
6503 virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
6504 virDispatchError(NULL);
6505 return (-1);
6507 if (virUnrefNetwork(network) < 0) {
6508 virDispatchError(NULL);
6509 return (-1);
6511 return(0);
6515 * virNetworkRef:
6516 * @network: the network to hold a reference on
6518 * Increment the reference count on the network. For each
6519 * additional call to this method, there shall be a corresponding
6520 * call to virNetworkFree to release the reference count, once
6521 * the caller no longer needs the reference to this object.
6523 * This method is typically useful for applications where multiple
6524 * threads are using a connection, and it is required that the
6525 * connection remain open until all threads have finished using
6526 * it. ie, each new thread using a network would increment
6527 * the reference count.
6529 * Returns 0 in case of success, -1 in case of failure.
6532 virNetworkRef(virNetworkPtr network)
6534 if ((!VIR_IS_CONNECTED_NETWORK(network))) {
6535 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
6536 virDispatchError(NULL);
6537 return(-1);
6539 virMutexLock(&network->conn->lock);
6540 DEBUG("network=%p refs=%d", network, network->refs);
6541 network->refs++;
6542 virMutexUnlock(&network->conn->lock);
6543 return 0;
6547 * virNetworkGetName:
6548 * @network: a network object
6550 * Get the public name for that network
6552 * Returns a pointer to the name or NULL, the string need not be deallocated
6553 * its lifetime will be the same as the network object.
6555 const char *
6556 virNetworkGetName(virNetworkPtr network)
6558 DEBUG("network=%p", network);
6560 virResetLastError();
6562 if (!VIR_IS_NETWORK(network)) {
6563 virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
6564 virDispatchError(NULL);
6565 return (NULL);
6567 return (network->name);
6571 * virNetworkGetUUID:
6572 * @network: a network object
6573 * @uuid: pointer to a VIR_UUID_BUFLEN bytes array
6575 * Get the UUID for a network
6577 * Returns -1 in case of error, 0 in case of success
6580 virNetworkGetUUID(virNetworkPtr network, unsigned char *uuid)
6582 DEBUG("network=%p, uuid=%p", network, uuid);
6584 virResetLastError();
6586 if (!VIR_IS_NETWORK(network)) {
6587 virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
6588 virDispatchError(NULL);
6589 return (-1);
6591 if (uuid == NULL) {
6592 virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
6593 goto error;
6596 memcpy(uuid, &network->uuid[0], VIR_UUID_BUFLEN);
6598 return (0);
6600 error:
6601 virDispatchError(network->conn);
6602 return -1;
6606 * virNetworkGetUUIDString:
6607 * @network: a network object
6608 * @buf: pointer to a VIR_UUID_STRING_BUFLEN bytes array
6610 * Get the UUID for a network as string. For more information about
6611 * UUID see RFC4122.
6613 * Returns -1 in case of error, 0 in case of success
6616 virNetworkGetUUIDString(virNetworkPtr network, char *buf)
6618 unsigned char uuid[VIR_UUID_BUFLEN];
6619 DEBUG("network=%p, buf=%p", network, buf);
6621 virResetLastError();
6623 if (!VIR_IS_NETWORK(network)) {
6624 virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
6625 virDispatchError(NULL);
6626 return (-1);
6628 if (buf == NULL) {
6629 virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
6630 goto error;
6633 if (virNetworkGetUUID(network, &uuid[0]))
6634 goto error;
6636 virUUIDFormat(uuid, buf);
6637 return (0);
6639 error:
6640 virDispatchError(network->conn);
6641 return -1;
6645 * virNetworkGetXMLDesc:
6646 * @network: a network object
6647 * @flags: an OR'ed set of extraction flags, not used yet
6649 * Provide an XML description of the network. The description may be reused
6650 * later to relaunch the network with virNetworkCreateXML().
6652 * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
6653 * the caller must free() the returned value.
6655 char *
6656 virNetworkGetXMLDesc(virNetworkPtr network, int flags)
6658 virConnectPtr conn;
6659 DEBUG("network=%p, flags=%d", network, flags);
6661 virResetLastError();
6663 if (!VIR_IS_CONNECTED_NETWORK(network)) {
6664 virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
6665 virDispatchError(NULL);
6666 return (NULL);
6668 if (flags != 0) {
6669 virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
6670 goto error;
6673 conn = network->conn;
6675 if (conn->networkDriver && conn->networkDriver->networkDumpXML) {
6676 char *ret;
6677 ret = conn->networkDriver->networkDumpXML (network, flags);
6678 if (!ret)
6679 goto error;
6680 return ret;
6683 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6685 error:
6686 virDispatchError(network->conn);
6687 return NULL;
6691 * virNetworkGetBridgeName:
6692 * @network: a network object
6694 * Provides a bridge interface name to which a domain may connect
6695 * a network interface in order to join the network.
6697 * Returns a 0 terminated interface name, or NULL in case of error.
6698 * the caller must free() the returned value.
6700 char *
6701 virNetworkGetBridgeName(virNetworkPtr network)
6703 virConnectPtr conn;
6704 DEBUG("network=%p", network);
6706 virResetLastError();
6708 if (!VIR_IS_CONNECTED_NETWORK(network)) {
6709 virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
6710 virDispatchError(NULL);
6711 return (NULL);
6714 conn = network->conn;
6716 if (conn->networkDriver && conn->networkDriver->networkGetBridgeName) {
6717 char *ret;
6718 ret = conn->networkDriver->networkGetBridgeName (network);
6719 if (!ret)
6720 goto error;
6721 return ret;
6724 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6726 error:
6727 virDispatchError(network->conn);
6728 return NULL;
6732 * virNetworkGetAutostart:
6733 * @network: a network object
6734 * @autostart: the value returned
6736 * Provides a boolean value indicating whether the network
6737 * configured to be automatically started when the host
6738 * machine boots.
6740 * Returns -1 in case of error, 0 in case of success
6743 virNetworkGetAutostart(virNetworkPtr network,
6744 int *autostart)
6746 virConnectPtr conn;
6747 DEBUG("network=%p, autostart=%p", network, autostart);
6749 virResetLastError();
6751 if (!VIR_IS_CONNECTED_NETWORK(network)) {
6752 virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
6753 virDispatchError(NULL);
6754 return (-1);
6756 if (!autostart) {
6757 virLibNetworkError(network, VIR_ERR_INVALID_ARG, __FUNCTION__);
6758 goto error;
6761 conn = network->conn;
6763 if (conn->networkDriver && conn->networkDriver->networkGetAutostart) {
6764 int ret;
6765 ret = conn->networkDriver->networkGetAutostart (network, autostart);
6766 if (ret < 0)
6767 goto error;
6768 return ret;
6771 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6773 error:
6774 virDispatchError(network->conn);
6775 return -1;
6779 * virNetworkSetAutostart:
6780 * @network: a network object
6781 * @autostart: whether the network should be automatically started 0 or 1
6783 * Configure the network to be automatically started
6784 * when the host machine boots.
6786 * Returns -1 in case of error, 0 in case of success
6789 virNetworkSetAutostart(virNetworkPtr network,
6790 int autostart)
6792 virConnectPtr conn;
6793 DEBUG("network=%p, autostart=%d", network, autostart);
6795 virResetLastError();
6797 if (!VIR_IS_CONNECTED_NETWORK(network)) {
6798 virLibNetworkError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
6799 virDispatchError(NULL);
6800 return (-1);
6803 if (network->conn->flags & VIR_CONNECT_RO) {
6804 virLibNetworkError(network, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
6805 goto error;
6808 conn = network->conn;
6810 if (conn->networkDriver && conn->networkDriver->networkSetAutostart) {
6811 int ret;
6812 ret = conn->networkDriver->networkSetAutostart (network, autostart);
6813 if (ret < 0)
6814 goto error;
6815 return ret;
6818 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6820 error:
6821 virDispatchError(network->conn);
6822 return -1;
6826 * virInterfaceGetConnect:
6827 * @iface: pointer to an interface
6829 * Provides the connection pointer associated with an interface. The
6830 * reference counter on the connection is not increased by this
6831 * call.
6833 * WARNING: When writing libvirt bindings in other languages, do
6834 * not use this function. Instead, store the connection and
6835 * the interface object together.
6837 * Returns the virConnectPtr or NULL in case of failure.
6839 virConnectPtr
6840 virInterfaceGetConnect (virInterfacePtr iface)
6842 DEBUG("iface=%p", iface);
6844 virResetLastError();
6846 if (!VIR_IS_CONNECTED_INTERFACE (iface)) {
6847 virLibInterfaceError (NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
6848 virDispatchError(NULL);
6849 return NULL;
6851 return iface->conn;
6855 * virConnectNumOfInterfaces:
6856 * @conn: pointer to the hypervisor connection
6858 * Provides the number of active interfaces on the physical host.
6860 * Returns the number of active interfaces found or -1 in case of error
6863 virConnectNumOfInterfaces(virConnectPtr conn)
6865 DEBUG("conn=%p", conn);
6867 virResetLastError();
6869 if (!VIR_IS_CONNECT(conn)) {
6870 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
6871 virDispatchError(NULL);
6872 return (-1);
6875 if (conn->interfaceDriver && conn->interfaceDriver->numOfInterfaces) {
6876 int ret;
6877 ret = conn->interfaceDriver->numOfInterfaces (conn);
6878 if (ret < 0)
6879 goto error;
6880 return ret;
6883 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6885 error:
6886 virDispatchError(conn);
6887 return -1;
6891 * virConnectListInterfaces:
6892 * @conn: pointer to the hypervisor connection
6893 * @names: array to collect the list of names of interfaces
6894 * @maxnames: size of @names
6896 * Collect the list of active physical host interfaces,
6897 * and store their names in @names
6899 * Returns the number of interfaces found or -1 in case of error
6902 virConnectListInterfaces(virConnectPtr conn, char **const names, int maxnames)
6904 DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);
6906 virResetLastError();
6908 if (!VIR_IS_CONNECT(conn)) {
6909 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
6910 virDispatchError(NULL);
6911 return (-1);
6914 if ((names == NULL) || (maxnames < 0)) {
6915 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6916 goto error;
6919 if (conn->interfaceDriver && conn->interfaceDriver->listInterfaces) {
6920 int ret;
6921 ret = conn->interfaceDriver->listInterfaces (conn, names, maxnames);
6922 if (ret < 0)
6923 goto error;
6924 return ret;
6927 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6929 error:
6930 virDispatchError(conn);
6931 return -1;
6935 * virConnectNumOfDefinedInterfaces:
6936 * @conn: pointer to the hypervisor connection
6938 * Provides the number of defined (inactive) interfaces on the physical host.
6940 * Returns the number of defined interface found or -1 in case of error
6943 virConnectNumOfDefinedInterfaces(virConnectPtr conn)
6945 DEBUG("conn=%p", conn);
6947 virResetLastError();
6949 if (!VIR_IS_CONNECT(conn)) {
6950 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
6951 virDispatchError(NULL);
6952 return (-1);
6955 if (conn->interfaceDriver && conn->interfaceDriver->numOfDefinedInterfaces) {
6956 int ret;
6957 ret = conn->interfaceDriver->numOfDefinedInterfaces (conn);
6958 if (ret < 0)
6959 goto error;
6960 return ret;
6963 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
6965 error:
6966 virDispatchError(conn);
6967 return -1;
6971 * virConnectListDefinedInterfaces:
6972 * @conn: pointer to the hypervisor connection
6973 * @names: array to collect the list of names of interfaces
6974 * @maxnames: size of @names
6976 * Collect the list of defined (inactive) physical host interfaces,
6977 * and store their names in @names.
6979 * Returns the number of interfaces found or -1 in case of error
6982 virConnectListDefinedInterfaces(virConnectPtr conn,
6983 char **const names,
6984 int maxnames)
6986 DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);
6988 virResetLastError();
6990 if (!VIR_IS_CONNECT(conn)) {
6991 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
6992 virDispatchError(NULL);
6993 return (-1);
6996 if ((names == NULL) || (maxnames < 0)) {
6997 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
6998 goto error;
7001 if (conn->interfaceDriver && conn->interfaceDriver->listDefinedInterfaces) {
7002 int ret;
7003 ret = conn->interfaceDriver->listDefinedInterfaces (conn, names, maxnames);
7004 if (ret < 0)
7005 goto error;
7006 return ret;
7009 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7011 error:
7012 virDispatchError(conn);
7013 return -1;
7017 * virInterfaceLookupByName:
7018 * @conn: pointer to the hypervisor connection
7019 * @name: name for the interface
7021 * Try to lookup an interface on the given hypervisor based on its name.
7023 * Returns a new interface object or NULL in case of failure. If the
7024 * interface cannot be found, then VIR_ERR_NO_INTERFACE error is raised.
7026 virInterfacePtr
7027 virInterfaceLookupByName(virConnectPtr conn, const char *name)
7029 DEBUG("conn=%p, name=%s", conn, name);
7031 virResetLastError();
7033 if (!VIR_IS_CONNECT(conn)) {
7034 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
7035 virDispatchError(NULL);
7036 return (NULL);
7038 if (name == NULL) {
7039 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7040 goto error;
7043 if (conn->interfaceDriver && conn->interfaceDriver->interfaceLookupByName) {
7044 virInterfacePtr ret;
7045 ret = conn->interfaceDriver->interfaceLookupByName (conn, name);
7046 if (!ret)
7047 goto error;
7048 return ret;
7051 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7053 error:
7054 virDispatchError(conn);
7055 return NULL;
7059 * virInterfaceLookupByMACString:
7060 * @conn: pointer to the hypervisor connection
7061 * @macstr: the MAC for the interface (null-terminated ASCII format)
7063 * Try to lookup an interface on the given hypervisor based on its MAC.
7065 * Returns a new interface object or NULL in case of failure. If the
7066 * interface cannot be found, then VIR_ERR_NO_INTERFACE error is raised.
7068 virInterfacePtr
7069 virInterfaceLookupByMACString(virConnectPtr conn, const char *macstr)
7071 DEBUG("conn=%p, macstr=%s", conn, macstr);
7073 virResetLastError();
7075 if (!VIR_IS_CONNECT(conn)) {
7076 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
7077 virDispatchError(NULL);
7078 return (NULL);
7080 if (macstr == NULL) {
7081 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7082 goto error;
7085 if (conn->interfaceDriver && conn->interfaceDriver->interfaceLookupByMACString) {
7086 virInterfacePtr ret;
7087 ret = conn->interfaceDriver->interfaceLookupByMACString (conn, macstr);
7088 if (!ret)
7089 goto error;
7090 return ret;
7093 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7095 error:
7096 virDispatchError(conn);
7097 return NULL;
7101 * virInterfaceGetName:
7102 * @iface: an interface object
7104 * Get the public name for that interface
7106 * Returns a pointer to the name or NULL, the string need not be deallocated
7107 * its lifetime will be the same as the interface object.
7109 const char *
7110 virInterfaceGetName(virInterfacePtr iface)
7112 DEBUG("iface=%p", iface);
7114 virResetLastError();
7116 if (!VIR_IS_INTERFACE(iface)) {
7117 virLibInterfaceError(NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
7118 virDispatchError(NULL);
7119 return (NULL);
7121 return (iface->name);
7125 * virInterfaceGetMACString:
7126 * @iface: an interface object
7128 * Get the MAC for an interface as string. For more information about
7129 * MAC see RFC4122.
7131 * Returns a pointer to the MAC address (in null-terminated ASCII
7132 * format) or NULL, the string need not be deallocated its lifetime
7133 * will be the same as the interface object.
7135 const char *
7136 virInterfaceGetMACString(virInterfacePtr iface)
7138 DEBUG("iface=%p", iface);
7140 virResetLastError();
7142 if (!VIR_IS_INTERFACE(iface)) {
7143 virLibInterfaceError(NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
7144 virDispatchError(NULL);
7145 return (NULL);
7147 return (iface->mac);
7151 * virInterfaceGetXMLDesc:
7152 * @iface: an interface object
7153 * @flags: an OR'ed set of extraction flags. Current valid bits:
7155 * VIR_INTERFACE_XML_INACTIVE - return the static configuration,
7156 * suitable for use redefining the
7157 * interface via virInterfaceDefineXML()
7159 * Provide an XML description of the interface. If
7160 * VIR_INTERFACE_XML_INACTIVE is set, the description may be reused
7161 * later to redefine the interface with virInterfaceDefineXML(). If it
7162 * is not set, the ip address and netmask will be the current live
7163 * setting of the interface, not the settings from the config files.
7165 * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
7166 * the caller must free() the returned value.
7168 char *
7169 virInterfaceGetXMLDesc(virInterfacePtr iface, unsigned int flags)
7171 virConnectPtr conn;
7172 DEBUG("iface=%p, flags=%d", iface, flags);
7174 virResetLastError();
7176 if (!VIR_IS_CONNECTED_INTERFACE(iface)) {
7177 virLibInterfaceError(NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
7178 virDispatchError(NULL);
7179 return (NULL);
7181 if ((flags & ~VIR_INTERFACE_XML_INACTIVE) != 0) {
7182 virLibInterfaceError(iface, VIR_ERR_INVALID_ARG, __FUNCTION__);
7183 goto error;
7186 conn = iface->conn;
7188 if (conn->interfaceDriver && conn->interfaceDriver->interfaceGetXMLDesc) {
7189 char *ret;
7190 ret = conn->interfaceDriver->interfaceGetXMLDesc (iface, flags);
7191 if (!ret)
7192 goto error;
7193 return ret;
7196 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7198 error:
7199 virDispatchError(iface->conn);
7200 return NULL;
7204 * virInterfaceDefineXML:
7205 * @conn: pointer to the hypervisor connection
7206 * @xml: the XML description for the interface, preferably in UTF-8
7207 * @flags: and OR'ed set of extraction flags, not used yet
7209 * Define an interface (or modify existing interface configuration)
7211 * Returns NULL in case of error, a pointer to the interface otherwise
7213 virInterfacePtr
7214 virInterfaceDefineXML(virConnectPtr conn, const char *xml, unsigned int flags)
7216 DEBUG("conn=%p, xml=%s, flags=%d", conn, xml, flags);
7218 virResetLastError();
7220 if (!VIR_IS_CONNECT(conn)) {
7221 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
7222 virDispatchError(NULL);
7223 return (NULL);
7225 if (conn->flags & VIR_CONNECT_RO) {
7226 virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7227 goto error;
7229 if (xml == NULL) {
7230 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7231 goto error;
7234 if (conn->interfaceDriver && conn->interfaceDriver->interfaceDefineXML) {
7235 virInterfacePtr ret;
7236 ret = conn->interfaceDriver->interfaceDefineXML (conn, xml, flags);
7237 if (!ret)
7238 goto error;
7239 return ret;
7242 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7244 error:
7245 virDispatchError(conn);
7246 return NULL;
7250 * virInterfaceUndefine:
7251 * @iface: pointer to a defined interface
7253 * Undefine an interface, ie remove it from the config.
7254 * This does not free the associated virInterfacePtr object.
7256 * Returns 0 in case of success, -1 in case of error
7259 virInterfaceUndefine(virInterfacePtr iface) {
7260 virConnectPtr conn;
7261 DEBUG("iface=%p", iface);
7263 virResetLastError();
7265 if (!VIR_IS_CONNECTED_INTERFACE(iface)) {
7266 virLibInterfaceError(NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
7267 virDispatchError(NULL);
7268 return (-1);
7270 conn = iface->conn;
7271 if (conn->flags & VIR_CONNECT_RO) {
7272 virLibInterfaceError(iface, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7273 goto error;
7276 if (conn->interfaceDriver && conn->interfaceDriver->interfaceUndefine) {
7277 int ret;
7278 ret = conn->interfaceDriver->interfaceUndefine (iface);
7279 if (ret < 0)
7280 goto error;
7281 return ret;
7284 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7286 error:
7287 virDispatchError(iface->conn);
7288 return -1;
7292 * virInterfaceCreate:
7293 * @iface: pointer to a defined interface
7294 * @flags: and OR'ed set of extraction flags, not used yet
7296 * Activate an interface (ie call "ifup")
7298 * Returns 0 in case of success, -1 in case of error
7301 virInterfaceCreate(virInterfacePtr iface, unsigned int flags)
7303 virConnectPtr conn;
7304 DEBUG("iface=%p, flags=%d", iface, flags);
7306 virResetLastError();
7308 if (!VIR_IS_CONNECTED_INTERFACE(iface)) {
7309 virLibInterfaceError(NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
7310 virDispatchError(NULL);
7311 return (-1);
7313 conn = iface->conn;
7314 if (conn->flags & VIR_CONNECT_RO) {
7315 virLibInterfaceError(iface, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7316 goto error;
7319 if (conn->interfaceDriver && conn->interfaceDriver->interfaceCreate) {
7320 int ret;
7321 ret = conn->interfaceDriver->interfaceCreate (iface, flags);
7322 if (ret < 0)
7323 goto error;
7324 return ret;
7327 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7329 error:
7330 virDispatchError(iface->conn);
7331 return -1;
7335 * virInterfaceDestroy:
7336 * @iface: an interface object
7337 * @flags: and OR'ed set of extraction flags, not used yet
7339 * deactivate an interface (ie call "ifdown")
7340 * This does not remove the interface from the config, and
7341 * does not free the associated virInterfacePtr object.
7343 * Returns 0 in case of success and -1 in case of failure.
7346 virInterfaceDestroy(virInterfacePtr iface, unsigned int flags)
7348 virConnectPtr conn;
7349 DEBUG("iface=%p, flags=%d", iface, flags);
7351 virResetLastError();
7353 if (!VIR_IS_CONNECTED_INTERFACE(iface)) {
7354 virLibInterfaceError(NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
7355 virDispatchError(NULL);
7356 return (-1);
7359 conn = iface->conn;
7360 if (conn->flags & VIR_CONNECT_RO) {
7361 virLibInterfaceError(iface, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7362 goto error;
7365 if (conn->interfaceDriver && conn->interfaceDriver->interfaceDestroy) {
7366 int ret;
7367 ret = conn->interfaceDriver->interfaceDestroy (iface, flags);
7368 if (ret < 0)
7369 goto error;
7370 return ret;
7373 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7375 error:
7376 virDispatchError(iface->conn);
7377 return -1;
7381 * virInterfaceRef:
7382 * @iface: the interface to hold a reference on
7384 * Increment the reference count on the interface. For each
7385 * additional call to this method, there shall be a corresponding
7386 * call to virInterfaceFree to release the reference count, once
7387 * the caller no longer needs the reference to this object.
7389 * This method is typically useful for applications where multiple
7390 * threads are using a connection, and it is required that the
7391 * connection remain open until all threads have finished using
7392 * it. ie, each new thread using an interface would increment
7393 * the reference count.
7395 * Returns 0 in case of success, -1 in case of failure.
7398 virInterfaceRef(virInterfacePtr iface)
7400 if ((!VIR_IS_CONNECTED_INTERFACE(iface))) {
7401 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
7402 virDispatchError(NULL);
7403 return(-1);
7405 virMutexLock(&iface->conn->lock);
7406 DEBUG("iface=%p refs=%d", iface, iface->refs);
7407 iface->refs++;
7408 virMutexUnlock(&iface->conn->lock);
7409 return 0;
7413 * virInterfaceFree:
7414 * @iface: an interface object
7416 * Free the interface object. The interface itself is unaltered.
7417 * The data structure is freed and should not be used thereafter.
7419 * Returns 0 in case of success and -1 in case of failure.
7422 virInterfaceFree(virInterfacePtr iface)
7424 DEBUG("iface=%p", iface);
7426 virResetLastError();
7428 if (!VIR_IS_CONNECTED_INTERFACE(iface)) {
7429 virLibInterfaceError(NULL, VIR_ERR_INVALID_INTERFACE, __FUNCTION__);
7430 virDispatchError(NULL);
7431 return (-1);
7433 if (virUnrefInterface(iface) < 0) {
7434 virDispatchError(NULL);
7435 return (-1);
7437 return(0);
7442 * virStoragePoolGetConnect:
7443 * @pool: pointer to a pool
7445 * Provides the connection pointer associated with a storage pool. The
7446 * reference counter on the connection is not increased by this
7447 * call.
7449 * WARNING: When writing libvirt bindings in other languages, do
7450 * not use this function. Instead, store the connection and
7451 * the pool object together.
7453 * Returns the virConnectPtr or NULL in case of failure.
7455 virConnectPtr
7456 virStoragePoolGetConnect (virStoragePoolPtr pool)
7458 DEBUG("pool=%p", pool);
7460 virResetLastError();
7462 if (!VIR_IS_CONNECTED_STORAGE_POOL (pool)) {
7463 virLibStoragePoolError (NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
7464 virDispatchError(NULL);
7465 return NULL;
7467 return pool->conn;
7471 * virConnectNumOfStoragePools:
7472 * @conn: pointer to hypervisor connection
7474 * Provides the number of active storage pools
7476 * Returns the number of pools found, or -1 on error
7479 virConnectNumOfStoragePools (virConnectPtr conn)
7481 DEBUG("conn=%p", conn);
7483 virResetLastError();
7485 if (!VIR_IS_CONNECT(conn)) {
7486 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
7487 virDispatchError(NULL);
7488 return (-1);
7491 if (conn->storageDriver && conn->storageDriver->numOfPools) {
7492 int ret;
7493 ret = conn->storageDriver->numOfPools (conn);
7494 if (ret < 0)
7495 goto error;
7496 return ret;
7499 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7501 error:
7502 virDispatchError(conn);
7503 return -1;
7507 * virConnectListStoragePools:
7508 * @conn: pointer to hypervisor connection
7509 * @names: array of char * to fill with pool names (allocated by caller)
7510 * @maxnames: size of the names array
7512 * Provides the list of names of active storage pools
7513 * upto maxnames. If there are more than maxnames, the
7514 * remaining names will be silently ignored.
7516 * Returns 0 on success, -1 on error
7519 virConnectListStoragePools (virConnectPtr conn,
7520 char **const names,
7521 int maxnames)
7523 DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);
7525 virResetLastError();
7527 if (!VIR_IS_CONNECT(conn)) {
7528 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
7529 virDispatchError(NULL);
7530 return (-1);
7533 if ((names == NULL) || (maxnames < 0)) {
7534 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7535 goto error;
7538 if (conn->storageDriver && conn->storageDriver->listPools) {
7539 int ret;
7540 ret = conn->storageDriver->listPools (conn, names, maxnames);
7541 if (ret < 0)
7542 goto error;
7543 return ret;
7546 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7548 error:
7549 virDispatchError(conn);
7550 return -1;
7555 * virConnectNumOfDefinedStoragePools:
7556 * @conn: pointer to hypervisor connection
7558 * Provides the number of inactive storage pools
7560 * Returns the number of pools found, or -1 on error
7563 virConnectNumOfDefinedStoragePools(virConnectPtr conn)
7565 DEBUG("conn=%p", conn);
7567 virResetLastError();
7569 if (!VIR_IS_CONNECT(conn)) {
7570 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
7571 virDispatchError(NULL);
7572 return (-1);
7575 if (conn->storageDriver && conn->storageDriver->numOfDefinedPools) {
7576 int ret;
7577 ret = conn->storageDriver->numOfDefinedPools (conn);
7578 if (ret < 0)
7579 goto error;
7580 return ret;
7583 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7585 error:
7586 virDispatchError(conn);
7587 return -1;
7592 * virConnectListDefinedStoragePools:
7593 * @conn: pointer to hypervisor connection
7594 * @names: array of char * to fill with pool names (allocated by caller)
7595 * @maxnames: size of the names array
7597 * Provides the list of names of inactive storage pools
7598 * upto maxnames. If there are more than maxnames, the
7599 * remaining names will be silently ignored.
7601 * Returns 0 on success, -1 on error
7604 virConnectListDefinedStoragePools(virConnectPtr conn,
7605 char **const names,
7606 int maxnames)
7608 DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);
7610 virResetLastError();
7612 if (!VIR_IS_CONNECT(conn)) {
7613 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
7614 virDispatchError(NULL);
7615 return (-1);
7618 if ((names == NULL) || (maxnames < 0)) {
7619 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7620 goto error;
7623 if (conn->storageDriver && conn->storageDriver->listDefinedPools) {
7624 int ret;
7625 ret = conn->storageDriver->listDefinedPools (conn, names, maxnames);
7626 if (ret < 0)
7627 goto error;
7628 return ret;
7631 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7633 error:
7634 virDispatchError(conn);
7635 return -1;
7640 * virConnectFindStoragePoolSources:
7641 * @conn: pointer to hypervisor connection
7642 * @type: type of storage pool sources to discover
7643 * @srcSpec: XML document specifying discovery source
7644 * @flags: flags for discovery (unused, pass 0)
7646 * Talks to a storage backend and attempts to auto-discover the set of
7647 * available storage pool sources. e.g. For iSCSI this would be a set of
7648 * iSCSI targets. For NFS this would be a list of exported paths. The
7649 * srcSpec (optional for some storage pool types, e.g. local ones) is
7650 * an instance of the storage pool's source element specifying where
7651 * to look for the pools.
7653 * srcSpec is not required for some types (e.g., those querying
7654 * local storage resources only)
7656 * Returns an xml document consisting of a SourceList element
7657 * containing a source document appropriate to the given pool
7658 * type for each discovered source.
7660 char *
7661 virConnectFindStoragePoolSources(virConnectPtr conn,
7662 const char *type,
7663 const char *srcSpec,
7664 unsigned int flags)
7666 DEBUG("conn=%p, type=%s, src=%s, flags=%u", conn, type ? type : "", srcSpec ? srcSpec : "", flags);
7668 virResetLastError();
7670 if (!VIR_IS_CONNECT(conn)) {
7671 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
7672 virDispatchError(NULL);
7673 return NULL;
7675 if (type == NULL) {
7676 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7677 goto error;
7680 if (conn->flags & VIR_CONNECT_RO) {
7681 virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7682 goto error;
7685 if (conn->storageDriver && conn->storageDriver->findPoolSources) {
7686 char *ret;
7687 ret = conn->storageDriver->findPoolSources(conn, type, srcSpec, flags);
7688 if (!ret)
7689 goto error;
7690 return ret;
7693 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7695 error:
7696 virDispatchError(conn);
7697 return NULL;
7702 * virStoragePoolLookupByName:
7703 * @conn: pointer to hypervisor connection
7704 * @name: name of pool to fetch
7706 * Fetch a storage pool based on its unique name
7708 * Returns a virStoragePoolPtr object, or NULL if no matching pool is found
7710 virStoragePoolPtr
7711 virStoragePoolLookupByName(virConnectPtr conn,
7712 const char *name)
7714 DEBUG("conn=%p, name=%s", conn, name);
7716 virResetLastError();
7718 if (!VIR_IS_CONNECT(conn)) {
7719 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
7720 virDispatchError(NULL);
7721 return (NULL);
7723 if (name == NULL) {
7724 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7725 goto error;
7728 if (conn->storageDriver && conn->storageDriver->poolLookupByName) {
7729 virStoragePoolPtr ret;
7730 ret = conn->storageDriver->poolLookupByName (conn, name);
7731 if (!ret)
7732 goto error;
7733 return ret;
7736 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7738 error:
7739 virDispatchError(conn);
7740 return NULL;
7745 * virStoragePoolLookupByUUID:
7746 * @conn: pointer to hypervisor connection
7747 * @uuid: globally unique id of pool to fetch
7749 * Fetch a storage pool based on its globally unique id
7751 * Returns a virStoragePoolPtr object, or NULL if no matching pool is found
7753 virStoragePoolPtr
7754 virStoragePoolLookupByUUID(virConnectPtr conn,
7755 const unsigned char *uuid)
7757 DEBUG("conn=%p, uuid=%s", conn, uuid);
7759 virResetLastError();
7761 if (!VIR_IS_CONNECT(conn)) {
7762 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
7763 virDispatchError(NULL);
7764 return (NULL);
7766 if (uuid == NULL) {
7767 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7768 goto error;
7771 if (conn->storageDriver && conn->storageDriver->poolLookupByUUID) {
7772 virStoragePoolPtr ret;
7773 ret = conn->storageDriver->poolLookupByUUID (conn, uuid);
7774 if (!ret)
7775 goto error;
7776 return ret;
7779 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7781 error:
7782 virDispatchError(conn);
7783 return NULL;
7788 * virStoragePoolLookupByUUIDString:
7789 * @conn: pointer to hypervisor connection
7790 * @uuidstr: globally unique id of pool to fetch
7792 * Fetch a storage pool based on its globally unique id
7794 * Returns a virStoragePoolPtr object, or NULL if no matching pool is found
7796 virStoragePoolPtr
7797 virStoragePoolLookupByUUIDString(virConnectPtr conn,
7798 const char *uuidstr)
7800 unsigned char uuid[VIR_UUID_BUFLEN];
7801 DEBUG("conn=%p, uuidstr=%s", conn, uuidstr);
7803 virResetLastError();
7805 if (!VIR_IS_CONNECT(conn)) {
7806 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
7807 virDispatchError(NULL);
7808 return (NULL);
7810 if (uuidstr == NULL) {
7811 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7812 goto error;
7815 if (virUUIDParse(uuidstr, uuid) < 0) {
7816 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7817 goto error;
7820 return virStoragePoolLookupByUUID(conn, uuid);
7822 error:
7823 virDispatchError(conn);
7824 return NULL;
7829 * virStoragePoolLookupByVolume:
7830 * @vol: pointer to storage volume
7832 * Fetch a storage pool which contains a particular volume
7834 * Returns a virStoragePoolPtr object, or NULL if no matching pool is found
7836 virStoragePoolPtr
7837 virStoragePoolLookupByVolume(virStorageVolPtr vol)
7839 DEBUG("vol=%p", vol);
7841 virResetLastError();
7843 if (!VIR_IS_CONNECTED_STORAGE_VOL(vol)) {
7844 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
7845 virDispatchError(NULL);
7846 return (NULL);
7849 if (vol->conn->storageDriver && vol->conn->storageDriver->poolLookupByVolume) {
7850 virStoragePoolPtr ret;
7851 ret = vol->conn->storageDriver->poolLookupByVolume (vol);
7852 if (!ret)
7853 goto error;
7854 return ret;
7857 virLibConnError (vol->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7859 error:
7860 virDispatchError(vol->conn);
7861 return NULL;
7865 * virStoragePoolCreateXML:
7866 * @conn: pointer to hypervisor connection
7867 * @xmlDesc: XML description for new pool
7868 * @flags: future flags, use 0 for now
7870 * Create a new storage based on its XML description. The
7871 * pool is not persistent, so its definition will disappear
7872 * when it is destroyed, or if the host is restarted
7874 * Returns a virStoragePoolPtr object, or NULL if creation failed
7876 virStoragePoolPtr
7877 virStoragePoolCreateXML(virConnectPtr conn,
7878 const char *xmlDesc,
7879 unsigned int flags)
7881 DEBUG("conn=%p, xmlDesc=%s", conn, xmlDesc);
7883 virResetLastError();
7885 if (!VIR_IS_CONNECT(conn)) {
7886 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
7887 virDispatchError(NULL);
7888 return (NULL);
7890 if (xmlDesc == NULL) {
7891 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7892 goto error;
7894 if (conn->flags & VIR_CONNECT_RO) {
7895 virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7896 goto error;
7899 if (conn->storageDriver && conn->storageDriver->poolCreateXML) {
7900 virStoragePoolPtr ret;
7901 ret = conn->storageDriver->poolCreateXML (conn, xmlDesc, flags);
7902 if (!ret)
7903 goto error;
7904 return ret;
7907 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7909 error:
7910 virDispatchError(conn);
7911 return NULL;
7915 * virStoragePoolDefineXML:
7916 * @conn: pointer to hypervisor connection
7917 * @xml: XML description for new pool
7918 * @flags: future flags, use 0 for now
7920 * Define a new inactive storage pool based on its XML description. The
7921 * pool is persistent, until explicitly undefined.
7923 * Returns a virStoragePoolPtr object, or NULL if creation failed
7925 virStoragePoolPtr
7926 virStoragePoolDefineXML(virConnectPtr conn,
7927 const char *xml,
7928 unsigned int flags)
7930 DEBUG("conn=%p, xml=%s", conn, xml);
7932 virResetLastError();
7934 if (!VIR_IS_CONNECT(conn)) {
7935 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
7936 virDispatchError(NULL);
7937 return (NULL);
7939 if (conn->flags & VIR_CONNECT_RO) {
7940 virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7941 goto error;
7943 if (xml == NULL) {
7944 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
7945 goto error;
7948 if (conn->storageDriver && conn->storageDriver->poolDefineXML) {
7949 virStoragePoolPtr ret;
7950 ret = conn->storageDriver->poolDefineXML (conn, xml, flags);
7951 if (!ret)
7952 goto error;
7953 return ret;
7956 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
7958 error:
7959 virDispatchError(conn);
7960 return NULL;
7964 * virStoragePoolBuild:
7965 * @pool: pointer to storage pool
7966 * @flags: future flags, use 0 for now
7968 * Build the underlying storage pool
7970 * Returns 0 on success, or -1 upon failure
7973 virStoragePoolBuild(virStoragePoolPtr pool,
7974 unsigned int flags)
7976 virConnectPtr conn;
7977 DEBUG("pool=%p, flags=%u", pool, flags);
7979 virResetLastError();
7981 if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
7982 virLibStoragePoolError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
7983 virDispatchError(NULL);
7984 return (-1);
7986 conn = pool->conn;
7987 if (conn->flags & VIR_CONNECT_RO) {
7988 virLibStoragePoolError(pool, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
7989 goto error;
7992 if (conn->storageDriver && conn->storageDriver->poolBuild) {
7993 int ret;
7994 ret = conn->storageDriver->poolBuild (pool, flags);
7995 if (ret < 0)
7996 goto error;
7997 return ret;
8000 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8002 error:
8003 virDispatchError(pool->conn);
8004 return -1;
8009 * virStoragePoolUndefine:
8010 * @pool: pointer to storage pool
8012 * Undefine an inactive storage pool
8014 * Returns 0 on success, -1 on failure
8017 virStoragePoolUndefine(virStoragePoolPtr pool)
8019 virConnectPtr conn;
8020 DEBUG("pool=%p", pool);
8022 virResetLastError();
8024 if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
8025 virLibStoragePoolError(NULL, VIR_ERR_INVALID_NETWORK, __FUNCTION__);
8026 virDispatchError(NULL);
8027 return (-1);
8029 conn = pool->conn;
8030 if (conn->flags & VIR_CONNECT_RO) {
8031 virLibStoragePoolError(pool, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
8032 goto error;
8035 if (conn->storageDriver && conn->storageDriver->poolUndefine) {
8036 int ret;
8037 ret = conn->storageDriver->poolUndefine (pool);
8038 if (ret < 0)
8039 goto error;
8040 return ret;
8043 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8045 error:
8046 virDispatchError(pool->conn);
8047 return -1;
8052 * virStoragePoolCreate:
8053 * @pool: pointer to storage pool
8054 * @flags: future flags, use 0 for now
8056 * Starts an inactive storage pool
8058 * Returns 0 on success, or -1 if it could not be started
8061 virStoragePoolCreate(virStoragePoolPtr pool,
8062 unsigned int flags)
8064 virConnectPtr conn;
8065 DEBUG("pool=%p", pool);
8067 virResetLastError();
8069 if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
8070 virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8071 virDispatchError(NULL);
8072 return (-1);
8074 conn = pool->conn;
8075 if (conn->flags & VIR_CONNECT_RO) {
8076 virLibStoragePoolError(pool, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
8077 goto error;
8080 if (conn->storageDriver && conn->storageDriver->poolCreate) {
8081 int ret;
8082 ret = conn->storageDriver->poolCreate (pool, flags);
8083 if (ret < 0)
8084 goto error;
8085 return ret;
8088 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8090 error:
8091 virDispatchError(pool->conn);
8092 return -1;
8097 * virStoragePoolDestroy:
8098 * @pool: pointer to storage pool
8100 * Destroy an active storage pool. This will deactivate the
8101 * pool on the host, but keep any persistent config associated
8102 * with it. If it has a persistent config it can later be
8103 * restarted with virStoragePoolCreate(). This does not free
8104 * the associated virStoragePoolPtr object.
8106 * Returns 0 on success, or -1 if it could not be destroyed
8109 virStoragePoolDestroy(virStoragePoolPtr pool)
8111 virConnectPtr conn;
8112 DEBUG("pool=%p", pool);
8114 virResetLastError();
8116 if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
8117 virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8118 virDispatchError(NULL);
8119 return (-1);
8122 conn = pool->conn;
8123 if (conn->flags & VIR_CONNECT_RO) {
8124 virLibStoragePoolError(pool, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
8125 goto error;
8128 if (conn->storageDriver && conn->storageDriver->poolDestroy) {
8129 int ret;
8130 ret = conn->storageDriver->poolDestroy (pool);
8131 if (ret < 0)
8132 goto error;
8133 return ret;
8136 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8138 error:
8139 virDispatchError(pool->conn);
8140 return -1;
8144 * virStoragePoolDelete:
8145 * @pool: pointer to storage pool
8146 * @flags: flags for obliteration process
8148 * Delete the underlying pool resources. This is
8149 * a non-recoverable operation. The virStoragePoolPtr object
8150 * itself is not free'd.
8152 * Returns 0 on success, or -1 if it could not be obliterate
8155 virStoragePoolDelete(virStoragePoolPtr pool,
8156 unsigned int flags)
8158 virConnectPtr conn;
8159 DEBUG("pool=%p, flags=%u", pool, flags);
8161 virResetLastError();
8163 if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
8164 virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8165 virDispatchError(NULL);
8166 return (-1);
8169 conn = pool->conn;
8170 if (conn->flags & VIR_CONNECT_RO) {
8171 virLibStoragePoolError(pool, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
8172 goto error;
8175 if (conn->storageDriver && conn->storageDriver->poolDelete) {
8176 int ret;
8177 ret = conn->storageDriver->poolDelete (pool, flags);
8178 if (ret < 0)
8179 goto error;
8180 return ret;
8183 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8185 error:
8186 virDispatchError(pool->conn);
8187 return -1;
8192 * virStoragePoolFree:
8193 * @pool: pointer to storage pool
8195 * Free a storage pool object, releasing all memory associated with
8196 * it. Does not change the state of the pool on the host.
8198 * Returns 0 on success, or -1 if it could not be free'd.
8201 virStoragePoolFree(virStoragePoolPtr pool)
8203 DEBUG("pool=%p", pool);
8205 virResetLastError();
8207 if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
8208 virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8209 virDispatchError(NULL);
8210 return (-1);
8212 if (virUnrefStoragePool(pool) < 0) {
8213 virDispatchError(NULL);
8214 return (-1);
8216 return(0);
8222 * virStoragePoolRef:
8223 * @pool: the pool to hold a reference on
8225 * Increment the reference count on the pool. For each
8226 * additional call to this method, there shall be a corresponding
8227 * call to virStoragePoolFree to release the reference count, once
8228 * the caller no longer needs the reference to this object.
8230 * This method is typically useful for applications where multiple
8231 * threads are using a connection, and it is required that the
8232 * connection remain open until all threads have finished using
8233 * it. ie, each new thread using a pool would increment
8234 * the reference count.
8236 * Returns 0 in case of success, -1 in case of failure.
8239 virStoragePoolRef(virStoragePoolPtr pool)
8241 if ((!VIR_IS_CONNECTED_STORAGE_POOL(pool))) {
8242 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
8243 virDispatchError(NULL);
8244 return(-1);
8246 virMutexLock(&pool->conn->lock);
8247 DEBUG("pool=%p refs=%d", pool, pool->refs);
8248 pool->refs++;
8249 virMutexUnlock(&pool->conn->lock);
8250 return 0;
8254 * virStoragePoolRefresh:
8255 * @pool: pointer to storage pool
8256 * @flags: flags to control refresh behaviour (currently unused, use 0)
8258 * Request that the pool refresh its list of volumes. This may
8259 * involve communicating with a remote server, and/or initializing
8260 * new devices at the OS layer
8262 * Returns 0 if the volume list was refreshed, -1 on failure
8265 virStoragePoolRefresh(virStoragePoolPtr pool,
8266 unsigned int flags)
8268 virConnectPtr conn;
8269 DEBUG("pool=%p flags=%u", pool, flags);
8271 virResetLastError();
8273 if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
8274 virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8275 virDispatchError(NULL);
8276 return (-1);
8279 conn = pool->conn;
8280 if (conn->flags & VIR_CONNECT_RO) {
8281 virLibStoragePoolError(pool, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
8282 goto error;
8285 if (conn->storageDriver && conn->storageDriver->poolRefresh) {
8286 int ret;
8287 ret = conn->storageDriver->poolRefresh (pool, flags);
8288 if (ret < 0)
8289 goto error;
8290 return ret;
8293 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8295 error:
8296 virDispatchError(pool->conn);
8297 return -1;
8302 * virStoragePoolGetName:
8303 * @pool: pointer to storage pool
8305 * Fetch the locally unique name of the storage pool
8307 * Returns the name of the pool, or NULL on error
8309 const char*
8310 virStoragePoolGetName(virStoragePoolPtr pool)
8312 DEBUG("pool=%p", pool);
8314 virResetLastError();
8316 if (!VIR_IS_STORAGE_POOL(pool)) {
8317 virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8318 virDispatchError(NULL);
8319 return (NULL);
8321 return (pool->name);
8326 * virStoragePoolGetUUID:
8327 * @pool: pointer to storage pool
8328 * @uuid: buffer of VIR_UUID_BUFLEN bytes in size
8330 * Fetch the globally unique ID of the storage pool
8332 * Returns 0 on success, or -1 on error;
8335 virStoragePoolGetUUID(virStoragePoolPtr pool,
8336 unsigned char *uuid)
8338 DEBUG("pool=%p, uuid=%p", pool, uuid);
8340 virResetLastError();
8342 if (!VIR_IS_STORAGE_POOL(pool)) {
8343 virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8344 virDispatchError(NULL);
8345 return (-1);
8347 if (uuid == NULL) {
8348 virLibStoragePoolError(pool, VIR_ERR_INVALID_ARG, __FUNCTION__);
8349 goto error;
8352 memcpy(uuid, &pool->uuid[0], VIR_UUID_BUFLEN);
8354 return (0);
8356 error:
8357 virDispatchError(pool->conn);
8358 return -1;
8362 * virStoragePoolGetUUIDString:
8363 * @pool: pointer to storage pool
8364 * @buf: buffer of VIR_UUID_STRING_BUFLEN bytes in size
8366 * Fetch the globally unique ID of the storage pool as a string
8368 * Returns 0 on success, or -1 on error;
8371 virStoragePoolGetUUIDString(virStoragePoolPtr pool,
8372 char *buf)
8374 unsigned char uuid[VIR_UUID_BUFLEN];
8375 DEBUG("pool=%p, buf=%p", pool, buf);
8377 virResetLastError();
8379 if (!VIR_IS_STORAGE_POOL(pool)) {
8380 virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8381 virDispatchError(NULL);
8382 return (-1);
8384 if (buf == NULL) {
8385 virLibStoragePoolError(pool, VIR_ERR_INVALID_ARG, __FUNCTION__);
8386 goto error;
8389 if (virStoragePoolGetUUID(pool, &uuid[0]))
8390 goto error;
8392 virUUIDFormat(uuid, buf);
8393 return (0);
8395 error:
8396 virDispatchError(pool->conn);
8397 return -1;
8402 * virStoragePoolGetInfo:
8403 * @pool: pointer to storage pool
8404 * @info: pointer at which to store info
8406 * Get volatile information about the storage pool
8407 * such as free space / usage summary
8409 * Returns 0 on success, or -1 on failure.
8412 virStoragePoolGetInfo(virStoragePoolPtr pool,
8413 virStoragePoolInfoPtr info)
8415 virConnectPtr conn;
8416 DEBUG("pool=%p, info=%p", pool, info);
8418 virResetLastError();
8420 if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
8421 virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8422 virDispatchError(NULL);
8423 return (-1);
8425 if (info == NULL) {
8426 virLibStoragePoolError(pool, VIR_ERR_INVALID_ARG, __FUNCTION__);
8427 goto error;
8430 memset(info, 0, sizeof(virStoragePoolInfo));
8432 conn = pool->conn;
8434 if (conn->storageDriver->poolGetInfo) {
8435 int ret;
8436 ret = conn->storageDriver->poolGetInfo (pool, info);
8437 if (ret < 0)
8438 goto error;
8439 return ret;
8442 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8444 error:
8445 virDispatchError(pool->conn);
8446 return -1;
8451 * virStoragePoolGetXMLDesc:
8452 * @pool: pointer to storage pool
8453 * @flags: flags for XML format options (set of virDomainXMLFlags)
8455 * Fetch an XML document describing all aspects of the
8456 * storage pool. This is suitable for later feeding back
8457 * into the virStoragePoolCreateXML method.
8459 * Returns a XML document, or NULL on error
8461 char *
8462 virStoragePoolGetXMLDesc(virStoragePoolPtr pool,
8463 unsigned int flags)
8465 virConnectPtr conn;
8466 DEBUG("pool=%p, flags=%u", pool, flags);
8468 virResetLastError();
8470 if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
8471 virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8472 virDispatchError(NULL);
8473 return (NULL);
8475 if (flags != 0) {
8476 virLibStoragePoolError(pool, VIR_ERR_INVALID_ARG, __FUNCTION__);
8477 goto error;
8480 conn = pool->conn;
8482 if (conn->storageDriver && conn->storageDriver->poolGetXMLDesc) {
8483 char *ret;
8484 ret = conn->storageDriver->poolGetXMLDesc (pool, flags);
8485 if (!ret)
8486 goto error;
8487 return ret;
8490 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8492 error:
8493 virDispatchError(pool->conn);
8494 return NULL;
8499 * virStoragePoolGetAutostart:
8500 * @pool: pointer to storage pool
8501 * @autostart: location in which to store autostart flag
8503 * Fetches the value of the autostart flag, which determines
8504 * whether the pool is automatically started at boot time
8506 * Returns 0 on success, -1 on failure
8509 virStoragePoolGetAutostart(virStoragePoolPtr pool,
8510 int *autostart)
8512 virConnectPtr conn;
8513 DEBUG("pool=%p, autostart=%p", pool, autostart);
8515 virResetLastError();
8517 if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
8518 virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8519 virDispatchError(NULL);
8520 return (-1);
8522 if (!autostart) {
8523 virLibStoragePoolError(pool, VIR_ERR_INVALID_ARG, __FUNCTION__);
8524 goto error;
8527 conn = pool->conn;
8529 if (conn->storageDriver && conn->storageDriver->poolGetAutostart) {
8530 int ret;
8531 ret = conn->storageDriver->poolGetAutostart (pool, autostart);
8532 if (ret < 0)
8533 goto error;
8534 return ret;
8537 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8539 error:
8540 virDispatchError(pool->conn);
8541 return -1;
8546 * virStoragePoolSetAutostart:
8547 * @pool: pointer to storage pool
8548 * @autostart: new flag setting
8550 * Sets the autostart flag
8552 * Returns 0 on success, -1 on failure
8555 virStoragePoolSetAutostart(virStoragePoolPtr pool,
8556 int autostart)
8558 virConnectPtr conn;
8559 DEBUG("pool=%p, autostart=%d", pool, autostart);
8561 virResetLastError();
8563 if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
8564 virLibStoragePoolError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8565 virDispatchError(NULL);
8566 return -1;
8569 if (pool->conn->flags & VIR_CONNECT_RO) {
8570 virLibStoragePoolError(pool, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
8571 goto error;
8574 conn = pool->conn;
8576 if (conn->storageDriver && conn->storageDriver->poolSetAutostart) {
8577 int ret;
8578 ret = conn->storageDriver->poolSetAutostart (pool, autostart);
8579 if (ret < 0)
8580 goto error;
8581 return ret;
8584 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8586 error:
8587 virDispatchError(pool->conn);
8588 return -1;
8593 * virStoragePoolNumOfVolumes:
8594 * @pool: pointer to storage pool
8596 * Fetch the number of storage volumes within a pool
8598 * Returns the number of storage pools, or -1 on failure
8601 virStoragePoolNumOfVolumes(virStoragePoolPtr pool)
8603 DEBUG("pool=%p", pool);
8605 virResetLastError();
8607 if (!VIR_IS_STORAGE_POOL(pool)) {
8608 virLibConnError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8609 virDispatchError(NULL);
8610 return (-1);
8613 if (pool->conn->storageDriver && pool->conn->storageDriver->poolNumOfVolumes) {
8614 int ret;
8615 ret = pool->conn->storageDriver->poolNumOfVolumes (pool);
8616 if (ret < 0)
8617 goto error;
8618 return ret;
8621 virLibConnError (pool->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8623 error:
8624 virDispatchError(pool->conn);
8625 return -1;
8630 * virStoragePoolListVolumes:
8631 * @pool: pointer to storage pool
8632 * @names: array in which to storage volume names
8633 * @maxnames: size of names array
8635 * Fetch list of storage volume names, limiting to
8636 * at most maxnames.
8638 * Returns the number of names fetched, or -1 on error
8641 virStoragePoolListVolumes(virStoragePoolPtr pool,
8642 char **const names,
8643 int maxnames)
8645 DEBUG("pool=%p, names=%p, maxnames=%d", pool, names, maxnames);
8647 virResetLastError();
8649 if (!VIR_IS_STORAGE_POOL(pool)) {
8650 virLibConnError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8651 virDispatchError(NULL);
8652 return (-1);
8655 if ((names == NULL) || (maxnames < 0)) {
8656 virLibConnError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
8657 goto error;
8660 if (pool->conn->storageDriver && pool->conn->storageDriver->poolListVolumes) {
8661 int ret;
8662 ret = pool->conn->storageDriver->poolListVolumes (pool, names, maxnames);
8663 if (ret < 0)
8664 goto error;
8665 return ret;
8668 virLibConnError (pool->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8670 error:
8671 virDispatchError(pool->conn);
8672 return -1;
8677 * virStorageVolGetConnect:
8678 * @vol: pointer to a pool
8680 * Provides the connection pointer associated with a storage volume. The
8681 * reference counter on the connection is not increased by this
8682 * call.
8684 * WARNING: When writing libvirt bindings in other languages, do
8685 * not use this function. Instead, store the connection and
8686 * the volume object together.
8688 * Returns the virConnectPtr or NULL in case of failure.
8690 virConnectPtr
8691 virStorageVolGetConnect (virStorageVolPtr vol)
8693 DEBUG("vol=%p", vol);
8695 virResetLastError();
8697 if (!VIR_IS_STORAGE_VOL (vol)) {
8698 virLibStoragePoolError (NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
8699 virDispatchError(NULL);
8700 return NULL;
8702 return vol->conn;
8707 * virStorageVolLookupByName:
8708 * @pool: pointer to storage pool
8709 * @name: name of storage volume
8711 * Fetch a pointer to a storage volume based on its name
8712 * within a pool
8714 * Returns a storage volume, or NULL if not found / error
8716 virStorageVolPtr
8717 virStorageVolLookupByName(virStoragePoolPtr pool,
8718 const char *name)
8720 DEBUG("pool=%p, name=%s", pool, name);
8722 virResetLastError();
8724 if (!VIR_IS_STORAGE_POOL(pool)) {
8725 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
8726 virDispatchError(NULL);
8727 return (NULL);
8729 if (name == NULL) {
8730 virLibConnError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
8731 goto error;
8734 if (pool->conn->storageDriver && pool->conn->storageDriver->volLookupByName) {
8735 virStorageVolPtr ret;
8736 ret = pool->conn->storageDriver->volLookupByName (pool, name);
8737 if (!ret)
8738 goto error;
8739 return ret;
8742 virLibConnError (pool->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8744 error:
8745 virDispatchError(pool->conn);
8746 return NULL;
8752 * virStorageVolLookupByKey:
8753 * @conn: pointer to hypervisor connection
8754 * @key: globally unique key
8756 * Fetch a pointer to a storage volume based on its
8757 * globally unique key
8759 * Returns a storage volume, or NULL if not found / error
8761 virStorageVolPtr
8762 virStorageVolLookupByKey(virConnectPtr conn,
8763 const char *key)
8765 DEBUG("conn=%p, key=%s", conn, key);
8767 virResetLastError();
8769 if (!VIR_IS_CONNECT(conn)) {
8770 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
8771 virDispatchError(NULL);
8772 return (NULL);
8774 if (key == NULL) {
8775 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
8776 goto error;
8779 if (conn->storageDriver && conn->storageDriver->volLookupByKey) {
8780 virStorageVolPtr ret;
8781 ret = conn->storageDriver->volLookupByKey (conn, key);
8782 if (!ret)
8783 goto error;
8784 return ret;
8787 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8789 error:
8790 virDispatchError(conn);
8791 return NULL;
8795 * virStorageVolLookupByPath:
8796 * @conn: pointer to hypervisor connection
8797 * @path: locally unique path
8799 * Fetch a pointer to a storage volume based on its
8800 * locally (host) unique path
8802 * Returns a storage volume, or NULL if not found / error
8804 virStorageVolPtr
8805 virStorageVolLookupByPath(virConnectPtr conn,
8806 const char *path)
8808 DEBUG("conn=%p, path=%s", conn, path);
8810 virResetLastError();
8812 if (!VIR_IS_CONNECT(conn)) {
8813 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
8814 virDispatchError(NULL);
8815 return (NULL);
8817 if (path == NULL) {
8818 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
8819 goto error;
8822 if (conn->storageDriver && conn->storageDriver->volLookupByPath) {
8823 virStorageVolPtr ret;
8824 ret = conn->storageDriver->volLookupByPath (conn, path);
8825 if (!ret)
8826 goto error;
8827 return ret;
8830 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8832 error:
8833 virDispatchError(conn);
8834 return NULL;
8839 * virStorageVolGetName:
8840 * @vol: pointer to storage volume
8842 * Fetch the storage volume name. This is unique
8843 * within the scope of a pool
8845 * Returns the volume name, or NULL on error
8847 const char*
8848 virStorageVolGetName(virStorageVolPtr vol)
8850 DEBUG("vol=%p", vol);
8852 virResetLastError();
8854 if (!VIR_IS_STORAGE_VOL(vol)) {
8855 virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
8856 virDispatchError(NULL);
8857 return (NULL);
8859 return (vol->name);
8864 * virStorageVolGetKey:
8865 * @vol: pointer to storage volume
8867 * Fetch the storage volume key. This is globally
8868 * unique, so the same volume will have the same
8869 * key no matter what host it is accessed from
8871 * Returns the volume key, or NULL on error
8873 const char*
8874 virStorageVolGetKey(virStorageVolPtr vol)
8876 DEBUG("vol=%p", vol);
8878 virResetLastError();
8880 if (!VIR_IS_STORAGE_VOL(vol)) {
8881 virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
8882 virDispatchError(NULL);
8883 return (NULL);
8885 return (vol->key);
8890 * virStorageVolCreateXML:
8891 * @pool: pointer to storage pool
8892 * @xmldesc: description of volume to create
8893 * @flags: flags for creation (unused, pass 0)
8895 * Create a storage volume within a pool based
8896 * on an XML description. Not all pools support
8897 * creation of volumes
8899 * Returns the storage volume, or NULL on error
8901 virStorageVolPtr
8902 virStorageVolCreateXML(virStoragePoolPtr pool,
8903 const char *xmldesc,
8904 unsigned int flags)
8906 DEBUG("pool=%p, flags=%u", pool, flags);
8908 virResetLastError();
8910 if (!VIR_IS_STORAGE_POOL(pool)) {
8911 virLibConnError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8912 virDispatchError(NULL);
8913 return (NULL);
8916 if (pool->conn->flags & VIR_CONNECT_RO) {
8917 virLibConnError(pool->conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
8918 goto error;
8921 if (pool->conn->storageDriver && pool->conn->storageDriver->volCreateXML) {
8922 virStorageVolPtr ret;
8923 ret = pool->conn->storageDriver->volCreateXML (pool, xmldesc, flags);
8924 if (!ret)
8925 goto error;
8926 return ret;
8929 virLibConnError (pool->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8931 error:
8932 virDispatchError(pool->conn);
8933 return NULL;
8938 * virStorageVolCreateXMLFrom:
8939 * @pool: pointer to parent pool for the new volume
8940 * @xmldesc: description of volume to create
8941 * @clonevol: storage volume to use as input
8942 * @flags: flags for creation (unused, pass 0)
8944 * Create a storage volume in the parent pool, using the
8945 * 'clonevol' volume as input. Information for the new
8946 * volume (name, perms) are passed via a typical volume
8947 * XML description.
8949 * Returns the storage volume, or NULL on error
8951 virStorageVolPtr
8952 virStorageVolCreateXMLFrom(virStoragePoolPtr pool,
8953 const char *xmldesc,
8954 virStorageVolPtr clonevol,
8955 unsigned int flags)
8957 DEBUG("pool=%p, flags=%u, clonevol=%p", pool, flags, clonevol);
8959 virResetLastError();
8961 if (!VIR_IS_STORAGE_POOL(pool)) {
8962 virLibConnError(NULL, VIR_ERR_INVALID_STORAGE_POOL, __FUNCTION__);
8963 virDispatchError(NULL);
8964 return (NULL);
8967 if (!VIR_IS_STORAGE_VOL(clonevol)) {
8968 virLibConnError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
8969 goto error;
8972 if (pool->conn->flags & VIR_CONNECT_RO ||
8973 clonevol->conn->flags & VIR_CONNECT_RO) {
8974 virLibConnError(pool->conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
8975 goto error;
8978 if (pool->conn->storageDriver &&
8979 pool->conn->storageDriver->volCreateXMLFrom) {
8980 virStorageVolPtr ret;
8981 ret = pool->conn->storageDriver->volCreateXMLFrom (pool, xmldesc,
8982 clonevol, flags);
8983 if (!ret)
8984 goto error;
8985 return ret;
8988 virLibConnError (pool->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
8990 error:
8991 virDispatchError(pool->conn);
8992 return NULL;
8997 * virStorageVolDelete:
8998 * @vol: pointer to storage volume
8999 * @flags: future flags, use 0 for now
9001 * Delete the storage volume from the pool
9003 * Returns 0 on success, or -1 on error
9006 virStorageVolDelete(virStorageVolPtr vol,
9007 unsigned int flags)
9009 virConnectPtr conn;
9010 DEBUG("vol=%p, flags=%u", vol, flags);
9012 virResetLastError();
9014 if (!VIR_IS_CONNECTED_STORAGE_VOL(vol)) {
9015 virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
9016 virDispatchError(NULL);
9017 return (-1);
9020 conn = vol->conn;
9021 if (conn->flags & VIR_CONNECT_RO) {
9022 virLibStorageVolError(vol, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
9023 goto error;
9026 if (conn->storageDriver && conn->storageDriver->volDelete) {
9027 int ret;
9028 ret = conn->storageDriver->volDelete (vol, flags);
9029 if (ret < 0)
9030 goto error;
9031 return ret;
9034 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9036 error:
9037 virDispatchError(vol->conn);
9038 return -1;
9043 * virStorageVolWipe:
9044 * @vol: pointer to storage volume
9045 * @flags: future flags, use 0 for now
9047 * Ensure data previously on a volume is not accessible to future reads
9049 * Returns 0 on success, or -1 on error
9052 virStorageVolWipe(virStorageVolPtr vol,
9053 unsigned int flags)
9055 virConnectPtr conn;
9056 VIR_DEBUG("vol=%p, flags=%u", vol, flags);
9058 virResetLastError();
9060 if (!VIR_IS_CONNECTED_STORAGE_VOL(vol)) {
9061 virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
9062 virDispatchError(NULL);
9063 return (-1);
9066 conn = vol->conn;
9067 if (conn->flags & VIR_CONNECT_RO) {
9068 virLibStorageVolError(vol, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
9069 goto error;
9072 if (conn->storageDriver && conn->storageDriver->volWipe) {
9073 int ret;
9074 ret = conn->storageDriver->volWipe(vol, flags);
9075 if (ret < 0) {
9076 goto error;
9078 return ret;
9081 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9083 error:
9084 virDispatchError(vol->conn);
9085 return -1;
9090 * virStorageVolFree:
9091 * @vol: pointer to storage volume
9093 * Release the storage volume handle. The underlying
9094 * storage volume continues to exist.
9096 * Returns 0 on success, or -1 on error
9099 virStorageVolFree(virStorageVolPtr vol)
9101 DEBUG("vol=%p", vol);
9103 virResetLastError();
9105 if (!VIR_IS_STORAGE_VOL(vol)) {
9106 virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
9107 virDispatchError(NULL);
9108 return (-1);
9110 if (virUnrefStorageVol(vol) < 0) {
9111 virDispatchError(NULL);
9112 return (-1);
9114 return(0);
9119 * virStorageVolRef:
9120 * @vol: the vol to hold a reference on
9122 * Increment the reference count on the vol. For each
9123 * additional call to this method, there shall be a corresponding
9124 * call to virStorageVolFree to release the reference count, once
9125 * the caller no longer needs the reference to this object.
9127 * This method is typically useful for applications where multiple
9128 * threads are using a connection, and it is required that the
9129 * connection remain open until all threads have finished using
9130 * it. ie, each new thread using a vol would increment
9131 * the reference count.
9133 * Returns 0 in case of success, -1 in case of failure.
9136 virStorageVolRef(virStorageVolPtr vol)
9138 if ((!VIR_IS_CONNECTED_STORAGE_VOL(vol))) {
9139 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
9140 virDispatchError(NULL);
9141 return(-1);
9143 virMutexLock(&vol->conn->lock);
9144 DEBUG("vol=%p refs=%d", vol, vol->refs);
9145 vol->refs++;
9146 virMutexUnlock(&vol->conn->lock);
9147 return 0;
9151 * virStorageVolGetInfo:
9152 * @vol: pointer to storage volume
9153 * @info: pointer at which to store info
9155 * Fetches volatile information about the storage
9156 * volume such as its current allocation
9158 * Returns 0 on success, or -1 on failure
9161 virStorageVolGetInfo(virStorageVolPtr vol,
9162 virStorageVolInfoPtr info)
9164 virConnectPtr conn;
9165 DEBUG("vol=%p, info=%p", vol, info);
9167 virResetLastError();
9169 if (!VIR_IS_CONNECTED_STORAGE_VOL(vol)) {
9170 virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
9171 virDispatchError(NULL);
9172 return (-1);
9174 if (info == NULL) {
9175 virLibStorageVolError(vol, VIR_ERR_INVALID_ARG, __FUNCTION__);
9176 goto error;
9179 memset(info, 0, sizeof(virStorageVolInfo));
9181 conn = vol->conn;
9183 if (conn->storageDriver->volGetInfo){
9184 int ret;
9185 ret = conn->storageDriver->volGetInfo (vol, info);
9186 if (ret < 0)
9187 goto error;
9188 return ret;
9191 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9193 error:
9194 virDispatchError(vol->conn);
9195 return -1;
9200 * virStorageVolGetXMLDesc:
9201 * @vol: pointer to storage volume
9202 * @flags: flags for XML generation (unused, pass 0)
9204 * Fetch an XML document describing all aspects of
9205 * the storage volume
9207 * Returns the XML document, or NULL on error
9209 char *
9210 virStorageVolGetXMLDesc(virStorageVolPtr vol,
9211 unsigned int flags)
9213 virConnectPtr conn;
9214 DEBUG("vol=%p, flags=%u", vol, flags);
9216 virResetLastError();
9218 if (!VIR_IS_STORAGE_VOL(vol)) {
9219 virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
9220 virDispatchError(NULL);
9221 return (NULL);
9223 if (flags != 0) {
9224 virLibStorageVolError(vol, VIR_ERR_INVALID_ARG, __FUNCTION__);
9225 goto error;
9228 conn = vol->conn;
9230 if (conn->storageDriver && conn->storageDriver->volGetXMLDesc) {
9231 char *ret;
9232 ret = conn->storageDriver->volGetXMLDesc (vol, flags);
9233 if (!ret)
9234 goto error;
9235 return ret;
9238 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9240 error:
9241 virDispatchError(vol->conn);
9242 return NULL;
9247 * virStorageVolGetPath:
9248 * @vol: pointer to storage volume
9250 * Fetch the storage volume path. Depending on the pool
9251 * configuration this is either persistent across hosts,
9252 * or dynamically assigned at pool startup. Consult
9253 * pool documentation for information on getting the
9254 * persistent naming
9256 * Returns the storage volume path, or NULL on error
9258 char *
9259 virStorageVolGetPath(virStorageVolPtr vol)
9261 virConnectPtr conn;
9262 DEBUG("vol=%p", vol);
9264 virResetLastError();
9266 if (!VIR_IS_STORAGE_VOL(vol)) {
9267 virLibStorageVolError(NULL, VIR_ERR_INVALID_STORAGE_VOL, __FUNCTION__);
9268 virDispatchError(NULL);
9269 return (NULL);
9272 conn = vol->conn;
9274 if (conn->storageDriver && conn->storageDriver->volGetPath) {
9275 char *ret;
9276 ret = conn->storageDriver->volGetPath (vol);
9277 if (!ret)
9278 goto error;
9279 return ret;
9282 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9284 error:
9285 virDispatchError(vol->conn);
9286 return NULL;
9291 * virNodeNumOfDevices:
9292 * @conn: pointer to the hypervisor connection
9293 * @cap: capability name
9294 * @flags: flags (unused, pass 0)
9296 * Provides the number of node devices.
9298 * If the optional 'cap' argument is non-NULL, then the count
9299 * will be restricted to devices with the specified capability
9301 * Returns the number of node devices or -1 in case of error
9304 virNodeNumOfDevices(virConnectPtr conn, const char *cap, unsigned int flags)
9306 DEBUG("conn=%p, cap=%s, flags=%d", conn, NULLSTR(cap), flags);
9308 virResetLastError();
9310 if (!VIR_IS_CONNECT(conn)) {
9311 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
9312 virDispatchError(NULL);
9313 return (-1);
9315 if (flags != 0) {
9316 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
9317 goto error;
9320 if (conn->deviceMonitor && conn->deviceMonitor->numOfDevices) {
9321 int ret;
9322 ret = conn->deviceMonitor->numOfDevices (conn, cap, flags);
9323 if (ret < 0)
9324 goto error;
9325 return ret;
9328 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9330 error:
9331 virDispatchError(conn);
9332 return -1;
9337 * virNodeListDevices:
9338 * @conn: pointer to the hypervisor connection
9339 * @cap: capability name
9340 * @names: array to collect the list of node device names
9341 * @maxnames: size of @names
9342 * @flags: flags (unused, pass 0)
9344 * Collect the list of node devices, and store their names in @names
9346 * If the optional 'cap' argument is non-NULL, then the count
9347 * will be restricted to devices with the specified capability
9349 * Returns the number of node devices found or -1 in case of error
9352 virNodeListDevices(virConnectPtr conn,
9353 const char *cap,
9354 char **const names, int maxnames,
9355 unsigned int flags)
9357 DEBUG("conn=%p, cap=%s, names=%p, maxnames=%d, flags=%d",
9358 conn, cap, names, maxnames, flags);
9360 virResetLastError();
9362 if (!VIR_IS_CONNECT(conn)) {
9363 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
9364 virDispatchError(NULL);
9365 return (-1);
9367 if ((flags != 0) || (names == NULL) || (maxnames < 0)) {
9368 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
9369 goto error;
9372 if (conn->deviceMonitor && conn->deviceMonitor->listDevices) {
9373 int ret;
9374 ret = conn->deviceMonitor->listDevices (conn, cap, names, maxnames, flags);
9375 if (ret < 0)
9376 goto error;
9377 return ret;
9380 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9382 error:
9383 virDispatchError(conn);
9384 return -1;
9389 * virNodeDeviceLookupByName:
9390 * @conn: pointer to the hypervisor connection
9391 * @name: unique device name
9393 * Lookup a node device by its name.
9395 * Returns a virNodeDevicePtr if found, NULL otherwise.
9397 virNodeDevicePtr virNodeDeviceLookupByName(virConnectPtr conn, const char *name)
9399 DEBUG("conn=%p, name=%p", conn, name);
9401 virResetLastError();
9403 if (!VIR_IS_CONNECT(conn)) {
9404 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
9405 virDispatchError(NULL);
9406 return NULL;
9409 if (name == NULL) {
9410 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
9411 goto error;
9414 if (conn->deviceMonitor && conn->deviceMonitor->deviceLookupByName) {
9415 virNodeDevicePtr ret;
9416 ret = conn->deviceMonitor->deviceLookupByName (conn, name);
9417 if (!ret)
9418 goto error;
9419 return ret;
9422 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9424 error:
9425 virDispatchError(conn);
9426 return NULL;
9431 * virNodeDeviceGetXMLDesc:
9432 * @dev: pointer to the node device
9433 * @flags: flags for XML generation (unused, pass 0)
9435 * Fetch an XML document describing all aspects of
9436 * the device.
9438 * Returns the XML document, or NULL on error
9440 char *virNodeDeviceGetXMLDesc(virNodeDevicePtr dev, unsigned int flags)
9442 DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);
9444 virResetLastError();
9446 if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
9447 virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
9448 virDispatchError(NULL);
9449 return NULL;
9452 if (dev->conn->deviceMonitor && dev->conn->deviceMonitor->deviceDumpXML) {
9453 char *ret;
9454 ret = dev->conn->deviceMonitor->deviceDumpXML (dev, flags);
9455 if (!ret)
9456 goto error;
9457 return ret;
9460 virLibConnError (dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9462 error:
9463 virDispatchError(dev->conn);
9464 return NULL;
9469 * virNodeDeviceGetName:
9470 * @dev: the device
9472 * Just return the device name
9474 * Returns the device name or NULL in case of error
9476 const char *virNodeDeviceGetName(virNodeDevicePtr dev)
9478 DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);
9480 if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
9481 virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
9482 virDispatchError(NULL);
9483 return NULL;
9486 return dev->name;
9490 * virNodeDeviceGetParent:
9491 * @dev: the device
9493 * Accessor for the parent of the device
9495 * Returns the name of the device's parent, or NULL if the
9496 * device has no parent.
9498 const char *virNodeDeviceGetParent(virNodeDevicePtr dev)
9500 DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);
9502 virResetLastError();
9504 if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
9505 virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
9506 virDispatchError(NULL);
9507 return NULL;
9510 if (!dev->parent) {
9511 if (dev->conn->deviceMonitor && dev->conn->deviceMonitor->deviceGetParent) {
9512 dev->parent = dev->conn->deviceMonitor->deviceGetParent (dev);
9513 } else {
9514 virLibConnError (dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9515 virDispatchError(dev->conn);
9516 return NULL;
9519 return dev->parent;
9523 * virNodeDeviceNumOfCaps:
9524 * @dev: the device
9526 * Accessor for the number of capabilities supported by the device.
9528 * Returns the number of capabilities supported by the device.
9530 int virNodeDeviceNumOfCaps(virNodeDevicePtr dev)
9532 DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);
9534 virResetLastError();
9536 if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
9537 virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
9538 virDispatchError(NULL);
9539 return -1;
9542 if (dev->conn->deviceMonitor && dev->conn->deviceMonitor->deviceNumOfCaps) {
9543 int ret;
9544 ret = dev->conn->deviceMonitor->deviceNumOfCaps (dev);
9545 if (ret < 0)
9546 goto error;
9547 return ret;
9550 virLibConnError (dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9552 error:
9553 virDispatchError(dev->conn);
9554 return -1;
9558 * virNodeDeviceListCaps:
9559 * @dev: the device
9560 * @names: array to collect the list of capability names
9561 * @maxnames: size of @names
9563 * Lists the names of the capabilities supported by the device.
9565 * Returns the number of capability names listed in @names.
9567 int virNodeDeviceListCaps(virNodeDevicePtr dev,
9568 char **const names,
9569 int maxnames)
9571 DEBUG("dev=%p, conn=%p, names=%p, maxnames=%d",
9572 dev, dev ? dev->conn : NULL, names, maxnames);
9574 virResetLastError();
9576 if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
9577 virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
9578 virDispatchError(NULL);
9579 return -1;
9582 if (dev->conn->deviceMonitor && dev->conn->deviceMonitor->deviceListCaps) {
9583 int ret;
9584 ret = dev->conn->deviceMonitor->deviceListCaps (dev, names, maxnames);
9585 if (ret < 0)
9586 goto error;
9587 return ret;
9590 virLibConnError (dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9592 error:
9593 virDispatchError(dev->conn);
9594 return -1;
9599 * virNodeDeviceFree:
9600 * @dev: pointer to the node device
9602 * Drops a reference to the node device, freeing it if
9603 * this was the last reference.
9605 * Returns the 0 for success, -1 for error.
9607 int virNodeDeviceFree(virNodeDevicePtr dev)
9609 DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);
9611 virResetLastError();
9613 if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
9614 virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
9615 virDispatchError(NULL);
9616 return (-1);
9618 if (virUnrefNodeDevice(dev) < 0) {
9619 virDispatchError(NULL);
9620 return (-1);
9622 return(0);
9627 * virNodeDeviceRef:
9628 * @dev: the dev to hold a reference on
9630 * Increment the reference count on the dev. For each
9631 * additional call to this method, there shall be a corresponding
9632 * call to virNodeDeviceFree to release the reference count, once
9633 * the caller no longer needs the reference to this object.
9635 * This method is typically useful for applications where multiple
9636 * threads are using a connection, and it is required that the
9637 * connection remain open until all threads have finished using
9638 * it. ie, each new thread using a dev would increment
9639 * the reference count.
9641 * Returns 0 in case of success, -1 in case of failure.
9644 virNodeDeviceRef(virNodeDevicePtr dev)
9646 if ((!VIR_IS_CONNECTED_NODE_DEVICE(dev))) {
9647 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
9648 virDispatchError(NULL);
9649 return(-1);
9651 virMutexLock(&dev->conn->lock);
9652 DEBUG("dev=%p refs=%d", dev, dev->refs);
9653 dev->refs++;
9654 virMutexUnlock(&dev->conn->lock);
9655 return 0;
9659 * virNodeDeviceDettach:
9660 * @dev: pointer to the node device
9662 * Dettach the node device from the node itself so that it may be
9663 * assigned to a guest domain.
9665 * Depending on the hypervisor, this may involve operations such
9666 * as unbinding any device drivers from the device, binding the
9667 * device to a dummy device driver and resetting the device.
9669 * If the device is currently in use by the node, this method may
9670 * fail.
9672 * Once the device is not assigned to any guest, it may be re-attached
9673 * to the node using the virNodeDeviceReattach() method.
9675 * Returns 0 in case of success, -1 in case of failure.
9678 virNodeDeviceDettach(virNodeDevicePtr dev)
9680 DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);
9682 virResetLastError();
9684 if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
9685 virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
9686 virDispatchError(NULL);
9687 return (-1);
9690 if (dev->conn->driver->nodeDeviceDettach) {
9691 int ret;
9692 ret = dev->conn->driver->nodeDeviceDettach (dev);
9693 if (ret < 0)
9694 goto error;
9695 return ret;
9698 virLibConnError(dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9700 error:
9701 virDispatchError(dev->conn);
9702 return (-1);
9706 * virNodeDeviceReAttach:
9707 * @dev: pointer to the node device
9709 * Re-attach a previously dettached node device to the node so that it
9710 * may be used by the node again.
9712 * Depending on the hypervisor, this may involve operations such
9713 * as resetting the device, unbinding it from a dummy device driver
9714 * and binding it to its appropriate driver.
9716 * If the device is currently in use by a guest, this method may fail.
9718 * Returns 0 in case of success, -1 in case of failure.
9721 virNodeDeviceReAttach(virNodeDevicePtr dev)
9723 DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);
9725 virResetLastError();
9727 if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
9728 virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
9729 virDispatchError(NULL);
9730 return (-1);
9733 if (dev->conn->driver->nodeDeviceReAttach) {
9734 int ret;
9735 ret = dev->conn->driver->nodeDeviceReAttach (dev);
9736 if (ret < 0)
9737 goto error;
9738 return ret;
9741 virLibConnError(dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9743 error:
9744 virDispatchError(dev->conn);
9745 return (-1);
9749 * virNodeDeviceReset:
9750 * @dev: pointer to the node device
9752 * Reset a previously dettached node device to the node before or
9753 * after assigning it to a guest.
9755 * The exact reset semantics depends on the hypervisor and device
9756 * type but, for example, KVM will attempt to reset PCI devices with
9757 * a Function Level Reset, Secondary Bus Reset or a Power Management
9758 * D-State reset.
9760 * If the reset will affect other devices which are currently in use,
9761 * this function may fail.
9763 * Returns 0 in case of success, -1 in case of failure.
9766 virNodeDeviceReset(virNodeDevicePtr dev)
9768 DEBUG("dev=%p, conn=%p", dev, dev ? dev->conn : NULL);
9770 virResetLastError();
9772 if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
9773 virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
9774 virDispatchError(NULL);
9775 return (-1);
9778 if (dev->conn->driver->nodeDeviceReset) {
9779 int ret;
9780 ret = dev->conn->driver->nodeDeviceReset (dev);
9781 if (ret < 0)
9782 goto error;
9783 return ret;
9786 virLibConnError(dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9788 error:
9789 virDispatchError(dev->conn);
9790 return (-1);
9795 * virNodeDeviceCreateXML:
9796 * @conn: pointer to the hypervisor connection
9797 * @xmlDesc: string containing an XML description of the device to be created
9798 * @flags: callers should always pass 0
9800 * Create a new device on the VM host machine, for example, virtual
9801 * HBAs created using vport_create.
9803 * Returns a node device object if successful, NULL in case of failure
9805 virNodeDevicePtr
9806 virNodeDeviceCreateXML(virConnectPtr conn,
9807 const char *xmlDesc,
9808 unsigned int flags)
9810 VIR_DEBUG("conn=%p, xmlDesc=%s, flags=%d", conn, xmlDesc, flags);
9812 virResetLastError();
9814 if (!VIR_IS_CONNECT(conn)) {
9815 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
9816 virDispatchError(NULL);
9817 return NULL;
9820 if (conn->flags & VIR_CONNECT_RO) {
9821 virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
9822 goto error;
9825 if (xmlDesc == NULL) {
9826 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
9827 goto error;
9830 if (conn->deviceMonitor &&
9831 conn->deviceMonitor->deviceCreateXML) {
9832 virNodeDevicePtr dev = conn->deviceMonitor->deviceCreateXML(conn, xmlDesc, flags);
9833 if (dev == NULL)
9834 goto error;
9835 return dev;
9838 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9840 error:
9841 virDispatchError(conn);
9842 return NULL;
9847 * virNodeDeviceDestroy:
9848 * @dev: a device object
9850 * Destroy the device object. The virtual device is removed from the host operating system.
9851 * This function may require privileged access
9853 * Returns 0 in case of success and -1 in case of failure.
9856 virNodeDeviceDestroy(virNodeDevicePtr dev)
9858 DEBUG("dev=%p", dev);
9860 virResetLastError();
9862 if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) {
9863 virLibNodeDeviceError(NULL, VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__);
9864 virDispatchError(NULL);
9865 return (-1);
9868 if (dev->conn->flags & VIR_CONNECT_RO) {
9869 virLibConnError(dev->conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
9870 goto error;
9873 if (dev->conn->deviceMonitor &&
9874 dev->conn->deviceMonitor->deviceDestroy) {
9875 int retval = dev->conn->deviceMonitor->deviceDestroy(dev);
9876 if (retval < 0) {
9877 goto error;
9880 return 0;
9883 virLibConnError (dev->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9885 error:
9886 virDispatchError(dev->conn);
9887 return -1;
9892 * Domain Event Notification
9896 * virConnectDomainEventRegister:
9897 * @conn: pointer to the connection
9898 * @cb: callback to the function handling domain events
9899 * @opaque: opaque data to pass on to the callback
9900 * @freecb: optional function to deallocate opaque when not used anymore
9902 * Adds a callback to receive notifications of domain lifecycle events
9903 * occurring on a connection
9905 * Use of this method is no longer recommended. Instead applications
9906 * should try virConnectDomainEventRegisterAny which has a more flexible
9907 * API contract
9909 * The virDomainPtr object handle passed into the callback upon delivery
9910 * of an event is only valid for the duration of execution of the callback.
9911 * If the callback wishes to keep the domain object after the callback
9912 * returns, it shall take a reference to it, by calling virDomainRef.
9913 * The reference can be released once the object is no longer required
9914 * by calling virDomainFree.
9916 * Returns 0 on success, -1 on failure
9919 virConnectDomainEventRegister(virConnectPtr conn,
9920 virConnectDomainEventCallback cb,
9921 void *opaque,
9922 virFreeCallback freecb)
9924 DEBUG("conn=%p, cb=%p, opaque=%p, freecb=%p", conn, cb, opaque, freecb);
9925 virResetLastError();
9927 if (!VIR_IS_CONNECT(conn)) {
9928 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
9929 virDispatchError(NULL);
9930 return (-1);
9932 if (cb == NULL) {
9933 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
9934 goto error;
9937 if ((conn->driver) && (conn->driver->domainEventRegister)) {
9938 int ret;
9939 ret = conn->driver->domainEventRegister (conn, cb, opaque, freecb);
9940 if (ret < 0)
9941 goto error;
9942 return ret;
9945 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9946 error:
9947 virDispatchError(conn);
9948 return -1;
9952 * virConnectDomainEventDeregister:
9953 * @conn: pointer to the connection
9954 * @cb: callback to the function handling domain events
9956 * Removes a callback previously registered with the virConnectDomainEventRegister
9957 * funtion.
9959 * Use of this method is no longer recommended. Instead applications
9960 * should try virConnectDomainEventUnregisterAny which has a more flexible
9961 * API contract
9963 * Returns 0 on success, -1 on failure
9966 virConnectDomainEventDeregister(virConnectPtr conn,
9967 virConnectDomainEventCallback cb)
9969 DEBUG("conn=%p, cb=%p", conn, cb);
9971 virResetLastError();
9973 if (!VIR_IS_CONNECT(conn)) {
9974 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
9975 virDispatchError(NULL);
9976 return (-1);
9978 if (cb == NULL) {
9979 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
9980 goto error;
9982 if ((conn->driver) && (conn->driver->domainEventDeregister)) {
9983 int ret;
9984 ret = conn->driver->domainEventDeregister (conn, cb);
9985 if (ret < 0)
9986 goto error;
9987 return ret;
9990 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
9991 error:
9992 virDispatchError(conn);
9993 return -1;
9997 * virSecretGetConnect:
9998 * @secret: A virSecret secret
10000 * Provides the connection pointer associated with a secret. The reference
10001 * counter on the connection is not increased by this call.
10003 * WARNING: When writing libvirt bindings in other languages, do not use this
10004 * function. Instead, store the connection and the secret object together.
10006 * Returns the virConnectPtr or NULL in case of failure.
10008 virConnectPtr
10009 virSecretGetConnect (virSecretPtr secret)
10011 DEBUG("secret=%p", secret);
10013 virResetLastError();
10015 if (!VIR_IS_CONNECTED_SECRET (secret)) {
10016 virLibSecretError (NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
10017 virDispatchError(NULL);
10018 return NULL;
10020 return secret->conn;
10024 * virConnectNumOfSecrets:
10025 * @conn: virConnect connection
10027 * Fetch number of currently defined secrets.
10029 * Returns the number currently defined secrets.
10032 virConnectNumOfSecrets(virConnectPtr conn)
10034 VIR_DEBUG("conn=%p", conn);
10036 virResetLastError();
10038 if (!VIR_IS_CONNECT(conn)) {
10039 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
10040 virDispatchError(NULL);
10041 return -1;
10044 if (conn->secretDriver != NULL &&
10045 conn->secretDriver->numOfSecrets != NULL) {
10046 int ret;
10048 ret = conn->secretDriver->numOfSecrets(conn);
10049 if (ret < 0)
10050 goto error;
10051 return ret;
10054 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
10056 error:
10057 virDispatchError(conn);
10058 return -1;
10062 * virConnectListSecrets:
10063 * @conn: virConnect connection
10064 * @uuids: Pointer to an array to store the UUIDs
10065 * @maxuuids: size of the array.
10067 * List UUIDs of defined secrets, store pointers to names in uuids.
10069 * Returns the number of UUIDs provided in the array, or -1 on failure.
10072 virConnectListSecrets(virConnectPtr conn, char **uuids, int maxuuids)
10074 VIR_DEBUG("conn=%p, uuids=%p, maxuuids=%d", conn, uuids, maxuuids);
10076 virResetLastError();
10078 if (!VIR_IS_CONNECT(conn)) {
10079 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
10080 virDispatchError(NULL);
10081 return -1;
10083 if (uuids == NULL || maxuuids < 0) {
10084 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
10085 goto error;
10088 if (conn->secretDriver != NULL && conn->secretDriver->listSecrets != NULL) {
10089 int ret;
10091 ret = conn->secretDriver->listSecrets(conn, uuids, maxuuids);
10092 if (ret < 0)
10093 goto error;
10094 return ret;
10097 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
10099 error:
10100 virDispatchError(conn);
10101 return -1;
10105 * virSecretLookupByUUID:
10106 * @conn: pointer to the hypervisor connection
10107 * @uuid: the raw UUID for the secret
10109 * Try to lookup a secret on the given hypervisor based on its UUID.
10110 * Uses the 16 bytes of raw data to describe the UUID
10112 * Returns a new secret object or NULL in case of failure. If the
10113 * secret cannot be found, then VIR_ERR_NO_SECRET error is raised.
10115 virSecretPtr
10116 virSecretLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
10118 DEBUG("conn=%p, uuid=%s", conn, uuid);
10120 virResetLastError();
10122 if (!VIR_IS_CONNECT(conn)) {
10123 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
10124 virDispatchError(NULL);
10125 return (NULL);
10127 if (uuid == NULL) {
10128 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
10129 goto error;
10132 if (conn->secretDriver &&
10133 conn->secretDriver->lookupByUUID) {
10134 virSecretPtr ret;
10135 ret = conn->secretDriver->lookupByUUID (conn, uuid);
10136 if (!ret)
10137 goto error;
10138 return ret;
10141 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
10143 error:
10144 virDispatchError(conn);
10145 return NULL;
10149 * virSecretLookupByUUIDString:
10150 * @conn: pointer to the hypervisor connection
10151 * @uuidstr: the string UUID for the secret
10153 * Try to lookup a secret on the given hypervisor based on its UUID.
10154 * Uses the printable string value to describe the UUID
10156 * Returns a new secret object or NULL in case of failure. If the
10157 * secret cannot be found, then VIR_ERR_NO_SECRET error is raised.
10159 virSecretPtr
10160 virSecretLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
10162 unsigned char uuid[VIR_UUID_BUFLEN];
10163 DEBUG("conn=%p, uuidstr=%s", conn, uuidstr);
10165 virResetLastError();
10167 if (!VIR_IS_CONNECT(conn)) {
10168 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
10169 virDispatchError(NULL);
10170 return (NULL);
10172 if (uuidstr == NULL) {
10173 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
10174 goto error;
10177 if (virUUIDParse(uuidstr, uuid) < 0) {
10178 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
10179 goto error;
10182 return virSecretLookupByUUID(conn, &uuid[0]);
10184 error:
10185 virDispatchError(conn);
10186 return NULL;
10191 * virSecretLookupByUsage:
10192 * @conn: pointer to the hypervisor connection
10193 * @usageType: the type of secret usage
10194 * @usageID: identifier of the object using the secret
10196 * Try to lookup a secret on the given hypervisor based on its usage
10197 * The usageID is unique within the set of secrets sharing the
10198 * same usageType value.
10200 * Returns a new secret object or NULL in case of failure. If the
10201 * secret cannot be found, then VIR_ERR_NO_SECRET error is raised.
10203 virSecretPtr
10204 virSecretLookupByUsage(virConnectPtr conn,
10205 int usageType,
10206 const char *usageID)
10208 DEBUG("conn=%p, usageType=%d usageID=%s", conn, usageType, NULLSTR(usageID));
10210 virResetLastError();
10212 if (!VIR_IS_CONNECT(conn)) {
10213 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
10214 virDispatchError(NULL);
10215 return (NULL);
10217 if (usageID == NULL) {
10218 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
10219 goto error;
10222 if (conn->secretDriver &&
10223 conn->secretDriver->lookupByUsage) {
10224 virSecretPtr ret;
10225 ret = conn->secretDriver->lookupByUsage (conn, usageType, usageID);
10226 if (!ret)
10227 goto error;
10228 return ret;
10231 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
10233 error:
10234 virDispatchError(conn);
10235 return NULL;
10240 * virSecretDefineXML:
10241 * @conn: virConnect connection
10242 * @xml: XML describing the secret.
10243 * @flags: flags, use 0 for now
10245 * If XML specifies a UUID, locates the specified secret and replaces all
10246 * attributes of the secret specified by UUID by attributes specified in xml
10247 * (any attributes not specified in xml are discarded).
10249 * Otherwise, creates a new secret with an automatically chosen UUID, and
10250 * initializes its attributes from xml.
10252 * Returns a the secret on success, NULL on failure.
10254 virSecretPtr
10255 virSecretDefineXML(virConnectPtr conn, const char *xml, unsigned int flags)
10257 VIR_DEBUG("conn=%p, xml=%s, flags=%u", conn, xml, flags);
10259 virResetLastError();
10261 if (!VIR_IS_CONNECT(conn)) {
10262 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
10263 virDispatchError(NULL);
10264 return NULL;
10266 if (conn->flags & VIR_CONNECT_RO) {
10267 virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
10268 goto error;
10270 if (xml == NULL) {
10271 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
10272 goto error;
10275 if (conn->secretDriver != NULL && conn->secretDriver->defineXML != NULL) {
10276 virSecretPtr ret;
10278 ret = conn->secretDriver->defineXML(conn, xml, flags);
10279 if (ret == NULL)
10280 goto error;
10281 return ret;
10284 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
10286 error:
10287 virDispatchError(conn);
10288 return NULL;
10292 * virSecretGetUUID:
10293 * @secret: A virSecret secret
10294 * @uuid: buffer of VIR_UUID_BUFLEN bytes in size
10296 * Fetches the UUID of the secret.
10298 * Returns 0 on success with the uuid buffer being filled, or
10299 * -1 upon failure.
10302 virSecretGetUUID(virSecretPtr secret, unsigned char *uuid)
10304 VIR_DEBUG("secret=%p", secret);
10306 virResetLastError();
10308 if (!VIR_IS_CONNECTED_SECRET(secret)) {
10309 virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
10310 virDispatchError(NULL);
10311 return -1;
10313 if (uuid == NULL) {
10314 virLibSecretError(secret, VIR_ERR_INVALID_ARG, __FUNCTION__);
10315 virDispatchError(secret->conn);
10316 return -1;
10319 memcpy(uuid, &secret->uuid[0], VIR_UUID_BUFLEN);
10321 return 0;
10325 * virSecretGetUUIDString:
10326 * @secret: a secret object
10327 * @buf: pointer to a VIR_UUID_STRING_BUFLEN bytes array
10329 * Get the UUID for a secret as string. For more information about
10330 * UUID see RFC4122.
10332 * Returns -1 in case of error, 0 in case of success
10335 virSecretGetUUIDString(virSecretPtr secret, char *buf)
10337 unsigned char uuid[VIR_UUID_BUFLEN];
10338 DEBUG("secret=%p, buf=%p", secret, buf);
10340 virResetLastError();
10342 if (!VIR_IS_SECRET(secret)) {
10343 virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
10344 virDispatchError(NULL);
10345 return (-1);
10347 if (buf == NULL) {
10348 virLibSecretError(secret, VIR_ERR_INVALID_ARG, __FUNCTION__);
10349 goto error;
10352 if (virSecretGetUUID(secret, &uuid[0]))
10353 goto error;
10355 virUUIDFormat(uuid, buf);
10356 return (0);
10358 error:
10359 virDispatchError(secret->conn);
10360 return -1;
10364 * virSecretGetUsageType:
10365 * @secret: a secret object
10367 * Get the type of object which uses this secret. The returned
10368 * value is one of the constants defined in the virSecretUsageType
10369 * enumeration. More values may be added to this enumeration in
10370 * the future, so callers should expect to see usage types they
10371 * do not explicitly know about.
10373 * Returns a positive integer identifying the type of object,
10374 * or -1 upon error.
10377 virSecretGetUsageType(virSecretPtr secret)
10379 DEBUG("secret=%p", secret);
10381 virResetLastError();
10383 if (!VIR_IS_SECRET(secret)) {
10384 virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
10385 virDispatchError(NULL);
10386 return (-1);
10388 return (secret->usageType);
10392 * virSecretGetUsageID:
10393 * @secret: a secret object
10395 * Get the unique identifier of the object with which this
10396 * secret is to be used. The format of the identifier is
10397 * dependant on the usage type of the secret. For a secret
10398 * with a usage type of VIR_SECRET_USAGE_TYPE_VOLUME the
10399 * identifier will be a fully qualfied path name. The
10400 * identifiers are intended to be unique within the set of
10401 * all secrets sharing the same usage type. ie, there shall
10402 * only ever be one secret for each volume path.
10404 * Returns a string identifying the object using the secret,
10405 * or NULL upon error
10407 const char *
10408 virSecretGetUsageID(virSecretPtr secret)
10410 DEBUG("secret=%p", secret);
10412 virResetLastError();
10414 if (!VIR_IS_SECRET(secret)) {
10415 virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
10416 virDispatchError(NULL);
10417 return (NULL);
10419 return (secret->usageID);
10424 * virSecretGetXMLDesc:
10425 * @secret: A virSecret secret
10426 * @flags: flags, use 0 for now
10428 * Fetches an XML document describing attributes of the secret.
10430 * Returns the XML document on success, NULL on failure. The caller must
10431 * free() the XML.
10433 char *
10434 virSecretGetXMLDesc(virSecretPtr secret, unsigned int flags)
10436 virConnectPtr conn;
10438 VIR_DEBUG("secret=%p, flags=%u", secret, flags);
10440 virResetLastError();
10442 if (!VIR_IS_CONNECTED_SECRET(secret)) {
10443 virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
10444 virDispatchError(NULL);
10445 return NULL;
10448 conn = secret->conn;
10449 if (conn->secretDriver != NULL && conn->secretDriver->getXMLDesc != NULL) {
10450 char *ret;
10452 ret = conn->secretDriver->getXMLDesc(secret, flags);
10453 if (ret == NULL)
10454 goto error;
10455 return ret;
10458 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
10460 error:
10461 virDispatchError(conn);
10462 return NULL;
10466 * virSecretSetValue:
10467 * @secret: A virSecret secret
10468 * @value: Value of the secret
10469 * @value_size: Size of the value
10470 * @flags: flags, use 0 for now
10472 * Sets the value of a secret.
10474 * Returns 0 on success, -1 on failure.
10477 virSecretSetValue(virSecretPtr secret, const unsigned char *value,
10478 size_t value_size, unsigned int flags)
10480 virConnectPtr conn;
10482 VIR_DEBUG("secret=%p, value=%p, value_size=%zu, flags=%u", secret, value,
10483 value_size, flags);
10485 virResetLastError();
10487 if (!VIR_IS_CONNECTED_SECRET(secret)) {
10488 virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
10489 virDispatchError(NULL);
10490 return -1;
10492 conn = secret->conn;
10493 if (conn->flags & VIR_CONNECT_RO) {
10494 virLibSecretError(secret, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
10495 goto error;
10497 if (value == NULL) {
10498 virLibSecretError(secret, VIR_ERR_INVALID_ARG, __FUNCTION__);
10499 goto error;
10502 if (conn->secretDriver != NULL && conn->secretDriver->setValue != NULL) {
10503 int ret;
10505 ret = conn->secretDriver->setValue(secret, value, value_size, flags);
10506 if (ret < 0)
10507 goto error;
10508 return ret;
10511 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
10513 error:
10514 virDispatchError(conn);
10515 return -1;
10519 * virSecretGetValue:
10520 * @secret: A virSecret connection
10521 * @value_size: Place for storing size of the secret value
10522 * @flags: flags, use 0 for now
10524 * Fetches the value of a secret.
10526 * Returns the secret value on success, NULL on failure. The caller must
10527 * free() the secret value.
10529 unsigned char *
10530 virSecretGetValue(virSecretPtr secret, size_t *value_size, unsigned int flags)
10532 virConnectPtr conn;
10534 VIR_DEBUG("secret=%p, value_size=%p, flags=%u", secret, value_size, flags);
10536 virResetLastError();
10538 if (!VIR_IS_CONNECTED_SECRET(secret)) {
10539 virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
10540 virDispatchError(NULL);
10541 return NULL;
10543 conn = secret->conn;
10544 if (conn->flags & VIR_CONNECT_RO) {
10545 virLibSecretError(secret, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
10546 goto error;
10548 if (value_size == NULL) {
10549 virLibSecretError(secret, VIR_ERR_INVALID_ARG, __FUNCTION__);
10550 goto error;
10553 flags &= VIR_SECRET_GET_VALUE_FLAGS_MASK;
10555 if (conn->secretDriver != NULL && conn->secretDriver->getValue != NULL) {
10556 unsigned char *ret;
10558 ret = conn->secretDriver->getValue(secret, value_size, flags);
10559 if (ret == NULL)
10560 goto error;
10561 return ret;
10564 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
10566 error:
10567 virDispatchError(conn);
10568 return NULL;
10572 * virSecretUndefine:
10573 * @secret: A virSecret secret
10575 * Deletes the specified secret. This does not free the associated
10576 * virSecretPtr object.
10578 * Returns 0 on success, -1 on failure.
10581 virSecretUndefine(virSecretPtr secret)
10583 virConnectPtr conn;
10585 VIR_DEBUG("secret=%p", secret);
10587 virResetLastError();
10589 if (!VIR_IS_CONNECTED_SECRET(secret)) {
10590 virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
10591 virDispatchError(NULL);
10592 return -1;
10594 conn = secret->conn;
10595 if (conn->flags & VIR_CONNECT_RO) {
10596 virLibSecretError(secret, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
10597 goto error;
10600 if (conn->secretDriver != NULL && conn->secretDriver->undefine != NULL) {
10601 int ret;
10603 ret = conn->secretDriver->undefine(secret);
10604 if (ret < 0)
10605 goto error;
10606 return ret;
10609 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
10611 error:
10612 virDispatchError(conn);
10613 return -1;
10617 * virSecretRef:
10618 * @secret: the secret to hold a reference on
10620 * Increment the reference count on the secret. For each additional call to
10621 * this method, there shall be a corresponding call to virSecretFree to release
10622 * the reference count, once the caller no longer needs the reference to this
10623 * object.
10625 * This method is typically useful for applications where multiple threads are
10626 * using a connection, and it is required that the connection remain open until
10627 * all threads have finished using it. ie, each new thread using a secret would
10628 * increment the reference count.
10630 * Returns 0 in case of success, -1 in case of failure.
10633 virSecretRef(virSecretPtr secret)
10635 if (!VIR_IS_CONNECTED_SECRET(secret)) {
10636 virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
10637 virDispatchError(NULL);
10638 return -1;
10640 virMutexLock(&secret->conn->lock);
10641 DEBUG("secret=%p refs=%d", secret, secret->refs);
10642 secret->refs++;
10643 virMutexUnlock(&secret->conn->lock);
10644 return 0;
10648 * virSecretFree:
10649 * @secret: pointer to a secret
10651 * Release the secret handle. The underlying secret continues to exist.
10653 * Returns 0 on success, or -1 on error
10656 virSecretFree(virSecretPtr secret)
10658 DEBUG("secret=%p", secret);
10660 virResetLastError();
10662 if (!VIR_IS_CONNECTED_SECRET(secret)) {
10663 virLibSecretError(NULL, VIR_ERR_INVALID_SECRET, __FUNCTION__);
10664 virDispatchError(NULL);
10665 return -1;
10667 if (virUnrefSecret(secret) < 0) {
10668 virDispatchError(NULL);
10669 return -1;
10671 return 0;
10676 * virStreamNew:
10677 * @conn: pointer to the connection
10678 * @flags: control features of the stream
10680 * Creates a new stream object which can be used to perform
10681 * streamed I/O with other public API function.
10683 * When no longer needed, a stream object must be released
10684 * with virStreamFree. If a data stream has been used,
10685 * then the application must call virStreamFinish or
10686 * virStreamAbort before free'ing to, in order to notify
10687 * the driver of termination.
10689 * If a non-blocking data stream is required passed
10690 * VIR_STREAM_NONBLOCK for flags, otherwise pass 0.
10692 * Returns the new stream, or NULL upon error
10694 virStreamPtr
10695 virStreamNew(virConnectPtr conn,
10696 unsigned int flags)
10698 virStreamPtr st;
10700 DEBUG("conn=%p, flags=%u", conn, flags);
10702 virResetLastError();
10704 if (!VIR_IS_CONNECT(conn)) {
10705 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
10706 virDispatchError(NULL);
10707 return (NULL);
10710 st = virGetStream(conn);
10711 if (st)
10712 st->flags = flags;
10714 return st;
10719 * virStreamRef:
10720 * @stream: pointer to the stream
10722 * Increment the reference count on the stream. For each
10723 * additional call to this method, there shall be a corresponding
10724 * call to virStreamFree to release the reference count, once
10725 * the caller no longer needs the reference to this object.
10727 * Returns 0 in case of success, -1 in case of failure
10730 virStreamRef(virStreamPtr stream)
10732 if ((!VIR_IS_CONNECTED_STREAM(stream))) {
10733 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
10734 virDispatchError(NULL);
10735 return(-1);
10737 virMutexLock(&stream->conn->lock);
10738 DEBUG("stream=%p refs=%d", stream, stream->refs);
10739 stream->refs++;
10740 virMutexUnlock(&stream->conn->lock);
10741 return 0;
10746 * virStreamSend:
10747 * @stream: pointer to the stream object
10748 * @data: buffer to write to stream
10749 * @nbytes: size of @data buffer
10751 * Write a series of bytes to the stream. This method may
10752 * block the calling application for an arbitrary amount
10753 * of time. Once an application has finished sending data
10754 * it should call virStreamFinish to wait for successful
10755 * confirmation from the driver, or detect any error
10757 * This method may not be used if a stream source has been
10758 * registered
10760 * Errors are not guaranteed to be reported synchronously
10761 * with the call, but may instead be delayed until a
10762 * subsequent call.
10764 * An example using this with a hypothetical file upload
10765 * API looks like
10767 * virStreamPtr st = virStreamNew(conn, 0);
10768 * int fd = open("demo.iso", O_RDONLY)
10770 * virConnectUploadFile(conn, "demo.iso", st);
10772 * while (1) {
10773 * char buf[1024];
10774 * int got = read(fd, buf, 1024);
10775 * if (got < 0) {
10776 * virStreamAbort(st);
10777 * break;
10779 * if (got == 0) {
10780 * virStreamFinish(st);
10781 * break;
10783 * int offset = 0;
10784 * while (offset < got) {
10785 * int sent = virStreamSend(st, buf+offset, got-offset)
10786 * if (sent < 0) {
10787 * virStreamAbort(st);
10788 * goto done;
10790 * offset += sent;
10793 * if (virStreamFinish(st) < 0)
10794 * ... report an error ....
10795 * done:
10796 * virStreamFree(st);
10797 * close(fd);
10799 * Returns the number of bytes written, which may be less
10800 * than requested.
10802 * Returns -1 upon error, at which time the stream will
10803 * be marked as aborted, and the caller should now release
10804 * the stream with virStreamFree.
10806 * Returns -2 if the outgoing transmit buffers are full &
10807 * the stream is marked as non-blocking.
10809 int virStreamSend(virStreamPtr stream,
10810 const char *data,
10811 size_t nbytes)
10813 DEBUG("stream=%p, data=%p, nbytes=%zi", stream, data, nbytes);
10815 virResetLastError();
10817 if (!VIR_IS_CONNECTED_STREAM(stream)) {
10818 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
10819 virDispatchError(NULL);
10820 return (-1);
10823 if (stream->driver &&
10824 stream->driver->streamSend) {
10825 int ret;
10826 ret = (stream->driver->streamSend)(stream, data, nbytes);
10827 if (ret == -2)
10828 return -2;
10829 if (ret < 0)
10830 goto error;
10831 return ret;
10834 virLibConnError(stream->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
10836 error:
10837 virDispatchError(stream->conn);
10838 return -1;
10843 * virStreamRecv:
10844 * @stream: pointer to the stream object
10845 * @data: buffer to write to stream
10846 * @nbytes: size of @data buffer
10848 * Write a series of bytes to the stream. This method may
10849 * block the calling application for an arbitrary amount
10850 * of time.
10852 * Errors are not guaranteed to be reported synchronously
10853 * with the call, but may instead be delayed until a
10854 * subsequent call.
10856 * An example using this with a hypothetical file download
10857 * API looks like
10859 * virStreamPtr st = virStreamNew(conn, 0);
10860 * int fd = open("demo.iso", O_WRONLY, 0600)
10862 * virConnectDownloadFile(conn, "demo.iso", st);
10864 * while (1) {
10865 * char buf[1024];
10866 * int got = virStreamRecv(st, buf, 1024);
10867 * if (got < 0)
10868 * break;
10869 * if (got == 0) {
10870 * virStreamFinish(st);
10871 * break;
10873 * int offset = 0;
10874 * while (offset < got) {
10875 * int sent = write(fd, buf+offset, got-offset)
10876 * if (sent < 0) {
10877 * virStreamAbort(st);
10878 * goto done;
10880 * offset += sent;
10883 * if (virStreamFinish(st) < 0)
10884 * ... report an error ....
10885 * done:
10886 * virStreamFree(st);
10887 * close(fd);
10890 * Returns the number of bytes read, which may be less
10891 * than requested.
10893 * Returns 0 when the end of the stream is reached, at
10894 * which time the caller should invoke virStreamFinish()
10895 * to get confirmation of stream completion.
10897 * Returns -1 upon error, at which time the stream will
10898 * be marked as aborted, and the caller should now release
10899 * the stream with virStreamFree.
10901 * Returns -2 if there is no data pending to be read & the
10902 * stream is marked as non-blocking.
10904 int virStreamRecv(virStreamPtr stream,
10905 char *data,
10906 size_t nbytes)
10908 DEBUG("stream=%p, data=%p, nbytes=%zi", stream, data, nbytes);
10910 virResetLastError();
10912 if (!VIR_IS_CONNECTED_STREAM(stream)) {
10913 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
10914 virDispatchError(NULL);
10915 return (-1);
10918 if (stream->driver &&
10919 stream->driver->streamRecv) {
10920 int ret;
10921 ret = (stream->driver->streamRecv)(stream, data, nbytes);
10922 if (ret == -2)
10923 return -2;
10924 if (ret < 0)
10925 goto error;
10926 return ret;
10929 virLibConnError(stream->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
10931 error:
10932 virDispatchError(stream->conn);
10933 return -1;
10938 * virStreamSendAll:
10939 * @stream: pointer to the stream object
10940 * @handler: source callback for reading data from application
10941 * @opaque: application defined data
10943 * Send the entire data stream, reading the data from the
10944 * requested data source. This is simply a convenient alternative
10945 * to virStreamSend, for apps that do blocking-I/o.
10947 * An example using this with a hypothetical file upload
10948 * API looks like
10950 * int mysource(virStreamPtr st, char *buf, int nbytes, void *opaque) {
10951 * int *fd = opaque;
10953 * return read(*fd, buf, nbytes);
10956 * virStreamPtr st = virStreamNew(conn, 0);
10957 * int fd = open("demo.iso", O_RDONLY)
10959 * virConnectUploadFile(conn, st);
10960 * if (virStreamSendAll(st, mysource, &fd) < 0) {
10961 * ...report an error ...
10962 * goto done;
10964 * if (virStreamFinish(st) < 0)
10965 * ...report an error...
10966 * virStreamFree(st);
10967 * close(fd);
10969 * Returns 0 if all the data was successfully sent. The caller
10970 * should invoke virStreamFinish(st) to flush the stream upon
10971 * success and then virStreamFree
10973 * Returns -1 upon any error, with virStreamAbort() already
10974 * having been called, so the caller need only call
10975 * virStreamFree()
10977 int virStreamSendAll(virStreamPtr stream,
10978 virStreamSourceFunc handler,
10979 void *opaque)
10981 char *bytes = NULL;
10982 int want = 1024*64;
10983 int ret = -1;
10984 DEBUG("stream=%p, handler=%p, opaque=%p", stream, handler, opaque);
10986 virResetLastError();
10988 if (!VIR_IS_CONNECTED_STREAM(stream)) {
10989 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
10990 virDispatchError(NULL);
10991 return (-1);
10994 if (stream->flags & VIR_STREAM_NONBLOCK) {
10995 virLibConnError(NULL, VIR_ERR_OPERATION_INVALID,
10996 _("data sources cannot be used for non-blocking streams"));
10997 goto cleanup;
11000 if (VIR_ALLOC_N(bytes, want) < 0) {
11001 virReportOOMError();
11002 goto cleanup;
11005 for (;;) {
11006 int got, offset = 0;
11007 got = (handler)(stream, bytes, want, opaque);
11008 if (got < 0) {
11009 virStreamAbort(stream);
11010 goto cleanup;
11012 if (got == 0)
11013 break;
11014 while (offset < got) {
11015 int done;
11016 done = virStreamSend(stream, bytes + offset, got - offset);
11017 if (done < 0)
11018 goto cleanup;
11019 offset += done;
11022 ret = 0;
11024 cleanup:
11025 VIR_FREE(bytes);
11027 if (ret != 0)
11028 virDispatchError(stream->conn);
11030 return ret;
11035 * virStreamRecvAll:
11036 * @stream: pointer to the stream object
11037 * @handler: sink callback for writing data to application
11038 * @opaque: application defined data
11040 * Receive the entire data stream, sending the data to the
11041 * requested data sink. This is simply a convenient alternative
11042 * to virStreamRecv, for apps that do blocking-I/o.
11044 * An example using this with a hypothetical file download
11045 * API looks like
11047 * int mysink(virStreamPtr st, const char *buf, int nbytes, void *opaque) {
11048 * int *fd = opaque;
11050 * return write(*fd, buf, nbytes);
11053 * virStreamPtr st = virStreamNew(conn, 0);
11054 * int fd = open("demo.iso", O_WRONLY)
11056 * virConnectUploadFile(conn, st);
11057 * if (virStreamRecvAll(st, mysink, &fd) < 0) {
11058 * ...report an error ...
11059 * goto done;
11061 * if (virStreamFinish(st) < 0)
11062 * ...report an error...
11063 * virStreamFree(st);
11064 * close(fd);
11066 * Returns 0 if all the data was successfully received. The caller
11067 * should invoke virStreamFinish(st) to flush the stream upon
11068 * success and then virStreamFree
11070 * Returns -1 upon any error, with virStreamAbort() already
11071 * having been called, so the caller need only call
11072 * virStreamFree()
11074 int virStreamRecvAll(virStreamPtr stream,
11075 virStreamSinkFunc handler,
11076 void *opaque)
11078 char *bytes = NULL;
11079 int want = 1024*64;
11080 int ret = -1;
11081 DEBUG("stream=%p, handler=%p, opaque=%p", stream, handler, opaque);
11083 virResetLastError();
11085 if (!VIR_IS_CONNECTED_STREAM(stream)) {
11086 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11087 virDispatchError(NULL);
11088 return (-1);
11091 if (stream->flags & VIR_STREAM_NONBLOCK) {
11092 virLibConnError(NULL, VIR_ERR_OPERATION_INVALID,
11093 _("data sinks cannot be used for non-blocking streams"));
11094 goto cleanup;
11098 if (VIR_ALLOC_N(bytes, want) < 0) {
11099 virReportOOMError();
11100 goto cleanup;
11103 for (;;) {
11104 int got, offset = 0;
11105 got = virStreamRecv(stream, bytes, want);
11106 if (got < 0)
11107 goto cleanup;
11108 if (got == 0)
11109 break;
11110 while (offset < got) {
11111 int done;
11112 done = (handler)(stream, bytes + offset, got - offset, opaque);
11113 if (done < 0) {
11114 virStreamAbort(stream);
11115 goto cleanup;
11117 offset += done;
11120 ret = 0;
11122 cleanup:
11123 VIR_FREE(bytes);
11125 if (ret != 0)
11126 virDispatchError(stream->conn);
11128 return ret;
11133 * virStreamEventAddCallback:
11134 * @stream: pointer to the stream object
11135 * @events: set of events to monitor
11136 * @cb: callback to invoke when an event occurs
11137 * @opaque: application defined data
11138 * @ff: callback to free @opaque data
11140 * Register a callback to be notified when a stream
11141 * becomes writable, or readable. This is most commonly
11142 * used in conjunction with non-blocking data streams
11143 * to integrate into an event loop
11145 * Returns 0 on success, -1 upon error
11147 int virStreamEventAddCallback(virStreamPtr stream,
11148 int events,
11149 virStreamEventCallback cb,
11150 void *opaque,
11151 virFreeCallback ff)
11153 DEBUG("stream=%p, events=%d, cb=%p, opaque=%p, ff=%p", stream, events, cb, opaque, ff);
11155 virResetLastError();
11157 if (!VIR_IS_CONNECTED_STREAM(stream)) {
11158 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11159 virDispatchError(NULL);
11160 return (-1);
11163 if (stream->driver &&
11164 stream->driver->streamAddCallback) {
11165 int ret;
11166 ret = (stream->driver->streamAddCallback)(stream, events, cb, opaque, ff);
11167 if (ret < 0)
11168 goto error;
11169 return ret;
11172 virLibConnError(stream->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11174 error:
11175 virDispatchError(stream->conn);
11176 return -1;
11181 * virStreamEventUpdateCallback:
11182 * @stream: pointer to the stream object
11183 * @events: set of events to monitor
11185 * Changes the set of events to monitor for a stream. This allows
11186 * for event notification to be changed without having to
11187 * unregister & register the callback completely. This method
11188 * is guarenteed to succeed if a callback is already registered
11190 * Returns 0 on success, -1 if no callback is registered
11192 int virStreamEventUpdateCallback(virStreamPtr stream,
11193 int events)
11195 DEBUG("stream=%p, events=%d", stream, events);
11197 virResetLastError();
11199 if (!VIR_IS_CONNECTED_STREAM(stream)) {
11200 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11201 virDispatchError(NULL);
11202 return (-1);
11205 if (stream->driver &&
11206 stream->driver->streamUpdateCallback) {
11207 int ret;
11208 ret = (stream->driver->streamUpdateCallback)(stream, events);
11209 if (ret < 0)
11210 goto error;
11211 return ret;
11214 virLibConnError (stream->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11216 error:
11217 virDispatchError(stream->conn);
11218 return -1;
11222 * virStreamEventRemoveCallback:
11223 * @stream: pointer to the stream object
11225 * Remove an event callback from the stream
11227 * Returns 0 on success, -1 on error
11229 int virStreamEventRemoveCallback(virStreamPtr stream)
11231 DEBUG("stream=%p", stream);
11233 virResetLastError();
11235 if (!VIR_IS_CONNECTED_STREAM(stream)) {
11236 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11237 virDispatchError(NULL);
11238 return (-1);
11241 if (stream->driver &&
11242 stream->driver->streamRemoveCallback) {
11243 int ret;
11244 ret = (stream->driver->streamRemoveCallback)(stream);
11245 if (ret < 0)
11246 goto error;
11247 return ret;
11250 virLibConnError (stream->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11252 error:
11253 virDispatchError(stream->conn);
11254 return -1;
11258 * virStreamFinish:
11259 * @stream: pointer to the stream object
11261 * Indicate that there is no further data is to be transmitted
11262 * on the stream. For output streams this should be called once
11263 * all data has been written. For input streams this should be
11264 * called once virStreamRecv returns end-of-file.
11266 * This method is a synchronization point for all asynchronous
11267 * errors, so if this returns a success code the application can
11268 * be sure that all data has been successfully processed.
11270 * Returns 0 on success, -1 upon error
11272 int virStreamFinish(virStreamPtr stream)
11274 DEBUG("stream=%p", stream);
11276 virResetLastError();
11278 if (!VIR_IS_CONNECTED_STREAM(stream)) {
11279 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11280 virDispatchError(NULL);
11281 return (-1);
11284 if (stream->driver &&
11285 stream->driver->streamFinish) {
11286 int ret;
11287 ret = (stream->driver->streamFinish)(stream);
11288 if (ret < 0)
11289 goto error;
11290 return ret;
11293 virLibConnError (stream->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11295 error:
11296 virDispatchError(stream->conn);
11297 return -1;
11301 * virStreamAbort:
11302 * @stream: pointer to the stream object
11304 * Request that the in progress data transfer be cancelled
11305 * abnormally before the end of the stream has been reached.
11306 * For output streams this can be used to inform the driver
11307 * that the stream is being terminated early. For input
11308 * streams this can be used to inform the driver that it
11309 * should stop sending data.
11311 * Returns 0 on success, -1 upon error
11313 int virStreamAbort(virStreamPtr stream)
11315 DEBUG("stream=%p", stream);
11317 virResetLastError();
11319 if (!VIR_IS_CONNECTED_STREAM(stream)) {
11320 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11321 virDispatchError(NULL);
11322 return (-1);
11325 if (stream->driver &&
11326 stream->driver->streamAbort) {
11327 int ret;
11328 ret = (stream->driver->streamAbort)(stream);
11329 if (ret < 0)
11330 goto error;
11331 return ret;
11334 virLibConnError (stream->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11336 error:
11337 virDispatchError(stream->conn);
11338 return -1;
11342 * virStreamFree:
11343 * @stream: pointer to the stream object
11345 * Decrement the reference count on a stream, releasing
11346 * the stream object if the reference count has hit zero.
11348 * There must not be an active data transfer in progress
11349 * when releasing the stream. If a stream needs to be
11350 * disposed of prior to end of stream being reached, then
11351 * the virStreamAbort function should be called first.
11353 * Returns 0 upon success, or -1 on error
11355 int virStreamFree(virStreamPtr stream)
11357 DEBUG("stream=%p", stream);
11359 virResetLastError();
11361 if (!VIR_IS_CONNECTED_STREAM(stream)) {
11362 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11363 virDispatchError(NULL);
11364 return (-1);
11367 /* XXX Enforce shutdown before free'ing resources ? */
11369 if (virUnrefStream(stream) < 0) {
11370 virDispatchError(NULL);
11371 return (-1);
11373 return (0);
11378 * virDomainIsActive:
11379 * @dom: pointer to the domain object
11381 * Determine if the domain is currently running
11383 * Returns 1 if running, 0 if inactive, -1 on error
11385 int virDomainIsActive(virDomainPtr dom)
11387 DEBUG("dom=%p", dom);
11389 virResetLastError();
11391 if (!VIR_IS_CONNECTED_DOMAIN(dom)) {
11392 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11393 virDispatchError(NULL);
11394 return (-1);
11396 if (dom->conn->driver->domainIsActive) {
11397 int ret;
11398 ret = dom->conn->driver->domainIsActive(dom);
11399 if (ret < 0)
11400 goto error;
11401 return ret;
11404 virLibConnError(dom->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11405 error:
11406 virDispatchError(dom->conn);
11407 return -1;
11411 * virDomainIsPersistent:
11412 * @dom: pointer to the domain object
11414 * Determine if the domain has a persistent configuration
11415 * which means it will still exist after shutting down
11417 * Returns 1 if persistent, 0 if transient, -1 on error
11419 int virDomainIsPersistent(virDomainPtr dom)
11421 DEBUG("dom=%p", dom);
11423 virResetLastError();
11425 if (!VIR_IS_CONNECTED_DOMAIN(dom)) {
11426 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11427 virDispatchError(NULL);
11428 return (-1);
11430 if (dom->conn->driver->domainIsPersistent) {
11431 int ret;
11432 ret = dom->conn->driver->domainIsPersistent(dom);
11433 if (ret < 0)
11434 goto error;
11435 return ret;
11438 virLibConnError(dom->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11439 error:
11440 virDispatchError(dom->conn);
11441 return -1;
11445 * virNetworkIsActive:
11446 * @net: pointer to the network object
11448 * Determine if the network is currently running
11450 * Returns 1 if running, 0 if inactive, -1 on error
11452 int virNetworkIsActive(virNetworkPtr net)
11454 DEBUG("net=%p", net);
11456 virResetLastError();
11458 if (!VIR_IS_CONNECTED_NETWORK(net)) {
11459 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11460 virDispatchError(NULL);
11461 return (-1);
11463 if (net->conn->networkDriver->networkIsActive) {
11464 int ret;
11465 ret = net->conn->networkDriver->networkIsActive(net);
11466 if (ret < 0)
11467 goto error;
11468 return ret;
11471 virLibConnError(net->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11472 error:
11473 virDispatchError(net->conn);
11474 return -1;
11479 * virNetworkIsPersistent:
11480 * @net: pointer to the network object
11482 * Determine if the network has a persistent configuration
11483 * which means it will still exist after shutting down
11485 * Returns 1 if persistent, 0 if transient, -1 on error
11487 int virNetworkIsPersistent(virNetworkPtr net)
11489 DEBUG("net=%p", net);
11491 virResetLastError();
11493 if (!VIR_IS_CONNECTED_NETWORK(net)) {
11494 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11495 virDispatchError(NULL);
11496 return (-1);
11498 if (net->conn->networkDriver->networkIsPersistent) {
11499 int ret;
11500 ret = net->conn->networkDriver->networkIsPersistent(net);
11501 if (ret < 0)
11502 goto error;
11503 return ret;
11506 virLibConnError(net->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11507 error:
11508 virDispatchError(net->conn);
11509 return -1;
11514 * virStoragePoolIsActive:
11515 * @pool: pointer to the storage pool object
11517 * Determine if the storage pool is currently running
11519 * Returns 1 if running, 0 if inactive, -1 on error
11521 int virStoragePoolIsActive(virStoragePoolPtr pool)
11523 DEBUG("pool=%p", pool);
11525 virResetLastError();
11527 if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
11528 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11529 virDispatchError(NULL);
11530 return (-1);
11532 if (pool->conn->storageDriver->poolIsActive) {
11533 int ret;
11534 ret = pool->conn->storageDriver->poolIsActive(pool);
11535 if (ret < 0)
11536 goto error;
11537 return ret;
11540 virLibConnError(pool->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11541 error:
11542 virDispatchError(pool->conn);
11543 return -1;
11548 * virStoragePoolIsPersistent:
11549 * @pool: pointer to the storage pool object
11551 * Determine if the storage pool has a persistent configuration
11552 * which means it will still exist after shutting down
11554 * Returns 1 if persistent, 0 if transient, -1 on error
11556 int virStoragePoolIsPersistent(virStoragePoolPtr pool)
11558 DEBUG("pool=%p", pool);
11560 virResetLastError();
11562 if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) {
11563 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11564 virDispatchError(NULL);
11565 return (-1);
11567 if (pool->conn->storageDriver->poolIsPersistent) {
11568 int ret;
11569 ret = pool->conn->storageDriver->poolIsPersistent(pool);
11570 if (ret < 0)
11571 goto error;
11572 return ret;
11575 virLibConnError(pool->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11576 error:
11577 virDispatchError(pool->conn);
11578 return -1;
11584 * virConnectNumOfNWFilters:
11585 * @conn: pointer to the hypervisor connection
11587 * Provides the number of nwfilters.
11589 * Returns the number of nwfilters found or -1 in case of error
11592 virConnectNumOfNWFilters(virConnectPtr conn)
11594 DEBUG("conn=%p", conn);
11596 virResetLastError();
11598 if (!VIR_IS_CONNECT(conn)) {
11599 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11600 virDispatchError(NULL);
11601 return -1;
11604 if (conn->nwfilterDriver && conn->nwfilterDriver->numOfNWFilters) {
11605 int ret;
11606 ret = conn->nwfilterDriver->numOfNWFilters (conn);
11607 if (ret < 0)
11608 goto error;
11609 return ret;
11612 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11614 error:
11615 virDispatchError(conn);
11616 return -1;
11621 * virConnectListNWFilters:
11622 * @conn: pointer to the hypervisor connection
11623 * @names: array to collect the list of names of network filters
11624 * @maxnames: size of @names
11626 * Collect the list of network filters, and store their names in @names
11628 * Returns the number of network filters found or -1 in case of error
11631 virConnectListNWFilters(virConnectPtr conn, char **const names, int maxnames)
11633 DEBUG("conn=%p, names=%p, maxnames=%d", conn, names, maxnames);
11635 virResetLastError();
11637 if (!VIR_IS_CONNECT(conn)) {
11638 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11639 virDispatchError(NULL);
11640 return -1;
11643 if ((names == NULL) || (maxnames < 0)) {
11644 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
11645 goto error;
11648 if (conn->nwfilterDriver && conn->nwfilterDriver->listNWFilters) {
11649 int ret;
11650 ret = conn->nwfilterDriver->listNWFilters (conn, names, maxnames);
11651 if (ret < 0)
11652 goto error;
11653 return ret;
11656 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11658 error:
11659 virDispatchError(conn);
11660 return -1;
11665 * virNWFilterLookupByName:
11666 * @conn: pointer to the hypervisor connection
11667 * @name: name for the network filter
11669 * Try to lookup a network filter on the given hypervisor based on its name.
11671 * Returns a new nwfilter object or NULL in case of failure. If the
11672 * network filter cannot be found, then VIR_ERR_NO_NWFILTER error is raised.
11674 virNWFilterPtr
11675 virNWFilterLookupByName(virConnectPtr conn, const char *name)
11677 DEBUG("conn=%p, name=%s", conn, name);
11679 virResetLastError();
11681 if (!VIR_IS_CONNECT(conn)) {
11682 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11683 virDispatchError(NULL);
11684 return (NULL);
11686 if (name == NULL) {
11687 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
11688 goto error;
11691 if (conn->nwfilterDriver && conn->nwfilterDriver->nwfilterLookupByName) {
11692 virNWFilterPtr ret;
11693 ret = conn->nwfilterDriver->nwfilterLookupByName (conn, name);
11694 if (!ret)
11695 goto error;
11696 return ret;
11699 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11701 error:
11702 virDispatchError(conn);
11703 return NULL;
11707 * virNWFilterLookupByUUID:
11708 * @conn: pointer to the hypervisor connection
11709 * @uuid: the raw UUID for the network filter
11711 * Try to lookup a network filter on the given hypervisor based on its UUID.
11713 * Returns a new nwfilter object or NULL in case of failure. If the
11714 * nwfdilter cannot be found, then VIR_ERR_NO_NWFILTER error is raised.
11716 virNWFilterPtr
11717 virNWFilterLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
11719 DEBUG("conn=%p, uuid=%s", conn, uuid);
11721 virResetLastError();
11723 if (!VIR_IS_CONNECT(conn)) {
11724 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11725 virDispatchError(NULL);
11726 return (NULL);
11728 if (uuid == NULL) {
11729 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
11730 goto error;
11733 if (conn->nwfilterDriver && conn->nwfilterDriver->nwfilterLookupByUUID){
11734 virNWFilterPtr ret;
11735 ret = conn->nwfilterDriver->nwfilterLookupByUUID (conn, uuid);
11736 if (!ret)
11737 goto error;
11738 return ret;
11741 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11743 error:
11744 virDispatchError(conn);
11745 return NULL;
11749 * virNWFilterLookupByUUIDString:
11750 * @conn: pointer to the hypervisor connection
11751 * @uuidstr: the string UUID for the nwfilter
11753 * Try to lookup an nwfilter on the given hypervisor based on its UUID.
11755 * Returns a new nwfilter object or NULL in case of failure. If the
11756 * nwfilter cannot be found, then VIR_ERR_NO_NWFILTER error is raised.
11758 virNWFilterPtr
11759 virNWFilterLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
11761 unsigned char uuid[VIR_UUID_BUFLEN];
11762 DEBUG("conn=%p, uuidstr=%s", conn, uuidstr);
11764 virResetLastError();
11766 if (!VIR_IS_CONNECT(conn)) {
11767 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11768 virDispatchError(NULL);
11769 return (NULL);
11771 if (uuidstr == NULL) {
11772 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
11773 goto error;
11776 if (virUUIDParse(uuidstr, uuid) < 0) {
11777 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
11778 goto error;
11781 return virNWFilterLookupByUUID(conn, &uuid[0]);
11783 error:
11784 virDispatchError(conn);
11785 return NULL;
11789 * virNWFilterFree:
11790 * @nwfilter: a nwfilter object
11792 * Free the nwfilter object. The running instance is kept alive.
11793 * The data structure is freed and should not be used thereafter.
11795 * Returns 0 in case of success and -1 in case of failure.
11798 virNWFilterFree(virNWFilterPtr nwfilter)
11800 DEBUG("nwfilter=%p", nwfilter);
11802 virResetLastError();
11804 if (!VIR_IS_CONNECTED_NWFILTER(nwfilter)) {
11805 virLibNWFilterError(NULL, VIR_ERR_INVALID_NWFILTER, __FUNCTION__);
11806 virDispatchError(NULL);
11807 return -1;
11809 if (virUnrefNWFilter(nwfilter) < 0) {
11810 virDispatchError(NULL);
11811 return -1;
11813 return 0;
11817 * virNWFilterGetName:
11818 * @nwfilter: a nwfilter object
11820 * Get the public name for the network filter
11822 * Returns a pointer to the name or NULL, the string need not be deallocated
11823 * its lifetime will be the same as the nwfilter object.
11825 const char *
11826 virNWFilterGetName(virNWFilterPtr nwfilter)
11828 DEBUG("nwfilter=%p", nwfilter);
11830 virResetLastError();
11832 if (!VIR_IS_NWFILTER(nwfilter)) {
11833 virLibNWFilterError(NULL, VIR_ERR_INVALID_NWFILTER, __FUNCTION__);
11834 virDispatchError(NULL);
11835 return (NULL);
11837 return (nwfilter->name);
11841 * virNWFilterGetUUID:
11842 * @nwfilter: a nwfilter object
11843 * @uuid: pointer to a VIR_UUID_BUFLEN bytes array
11845 * Get the UUID for a network filter
11847 * Returns -1 in case of error, 0 in case of success
11850 virNWFilterGetUUID(virNWFilterPtr nwfilter, unsigned char *uuid)
11852 DEBUG("nwfilter=%p, uuid=%p", nwfilter, uuid);
11854 virResetLastError();
11856 if (!VIR_IS_NWFILTER(nwfilter)) {
11857 virLibNWFilterError(NULL, VIR_ERR_INVALID_NWFILTER, __FUNCTION__);
11858 virDispatchError(NULL);
11859 return -1;
11861 if (uuid == NULL) {
11862 virLibNWFilterError(nwfilter, VIR_ERR_INVALID_ARG, __FUNCTION__);
11863 goto error;
11866 memcpy(uuid, &nwfilter->uuid[0], VIR_UUID_BUFLEN);
11868 return 0;
11870 error:
11871 virDispatchError(nwfilter->conn);
11872 return -1;
11876 * virNWFilterGetUUIDString:
11877 * @nwfilter: a nwfilter object
11878 * @buf: pointer to a VIR_UUID_STRING_BUFLEN bytes array
11880 * Get the UUID for a network filter as string. For more information about
11881 * UUID see RFC4122.
11883 * Returns -1 in case of error, 0 in case of success
11886 virNWFilterGetUUIDString(virNWFilterPtr nwfilter, char *buf)
11888 unsigned char uuid[VIR_UUID_BUFLEN];
11889 DEBUG("nwfilter=%p, buf=%p", nwfilter, buf);
11891 virResetLastError();
11893 if (!VIR_IS_NWFILTER(nwfilter)) {
11894 virLibNWFilterError(NULL, VIR_ERR_INVALID_NWFILTER, __FUNCTION__);
11895 virDispatchError(NULL);
11896 return -1;
11898 if (buf == NULL) {
11899 virLibNWFilterError(nwfilter, VIR_ERR_INVALID_ARG, __FUNCTION__);
11900 goto error;
11903 if (virNWFilterGetUUID(nwfilter, &uuid[0]))
11904 goto error;
11906 virUUIDFormat(uuid, buf);
11907 return 0;
11909 error:
11910 virDispatchError(nwfilter->conn);
11911 return -1;
11916 * virNWFilterDefineXML:
11917 * @conn: pointer to the hypervisor connection
11918 * @xmlDesc: an XML description of the nwfilter
11920 * Define a new network filter, based on an XML description
11921 * similar to the one returned by virNWFilterGetXMLDesc()
11923 * Returns a new nwfilter object or NULL in case of failure
11925 virNWFilterPtr
11926 virNWFilterDefineXML(virConnectPtr conn, const char *xmlDesc)
11928 DEBUG("conn=%p, xmlDesc=%s", conn, xmlDesc);
11930 virResetLastError();
11932 if (!VIR_IS_CONNECT(conn)) {
11933 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
11934 virDispatchError(NULL);
11935 return (NULL);
11937 if (xmlDesc == NULL) {
11938 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
11939 goto error;
11941 if (conn->flags & VIR_CONNECT_RO) {
11942 virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
11943 goto error;
11946 if (conn->nwfilterDriver && conn->nwfilterDriver->defineXML) {
11947 virNWFilterPtr ret;
11948 ret = conn->nwfilterDriver->defineXML (conn, xmlDesc, 0);
11949 if (!ret)
11950 goto error;
11951 return ret;
11954 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
11956 error:
11957 virDispatchError(conn);
11958 return NULL;
11963 * virNWFilterUndefine:
11964 * @nwfilter: a nwfilter object
11966 * Undefine the nwfilter object. This call will not succeed if
11967 * a running VM is referencing the filter. This does not free the
11968 * associated virNWFilterPtr object.
11970 * Returns 0 in case of success and -1 in case of failure.
11973 virNWFilterUndefine(virNWFilterPtr nwfilter)
11975 virConnectPtr conn;
11976 DEBUG("nwfilter=%p", nwfilter);
11978 virResetLastError();
11980 if (!VIR_IS_CONNECTED_NWFILTER(nwfilter)) {
11981 virLibNWFilterError(NULL, VIR_ERR_INVALID_NWFILTER, __FUNCTION__);
11982 virDispatchError(NULL);
11983 return -1;
11986 conn = nwfilter->conn;
11987 if (conn->flags & VIR_CONNECT_RO) {
11988 virLibNWFilterError(nwfilter, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
11989 goto error;
11992 if (conn->nwfilterDriver && conn->nwfilterDriver->undefine) {
11993 int ret;
11994 ret = conn->nwfilterDriver->undefine (nwfilter);
11995 if (ret < 0)
11996 goto error;
11997 return ret;
12000 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12002 error:
12003 virDispatchError(nwfilter->conn);
12004 return -1;
12009 * virNWFilterGetXMLDesc:
12010 * @nwfilter: a nwfilter object
12011 * @flags: an OR'ed set of extraction flags, not used yet
12013 * Provide an XML description of the network filter. The description may be
12014 * reused later to redefine the network filter with virNWFilterCreateXML().
12016 * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
12017 * the caller must free() the returned value.
12019 char *
12020 virNWFilterGetXMLDesc(virNWFilterPtr nwfilter, int flags)
12022 virConnectPtr conn;
12023 DEBUG("nwfilter=%p, flags=%d", nwfilter, flags);
12025 virResetLastError();
12027 if (!VIR_IS_CONNECTED_NWFILTER(nwfilter)) {
12028 virLibNWFilterError(NULL, VIR_ERR_INVALID_NWFILTER, __FUNCTION__);
12029 virDispatchError(NULL);
12030 return (NULL);
12032 if (flags != 0) {
12033 virLibNWFilterError(nwfilter, VIR_ERR_INVALID_ARG, __FUNCTION__);
12034 goto error;
12037 conn = nwfilter->conn;
12039 if (conn->nwfilterDriver && conn->nwfilterDriver->getXMLDesc) {
12040 char *ret;
12041 ret = conn->nwfilterDriver->getXMLDesc (nwfilter, flags);
12042 if (!ret)
12043 goto error;
12044 return ret;
12047 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12049 error:
12050 virDispatchError(nwfilter->conn);
12051 return NULL;
12056 * virNWFilterRef:
12057 * @nwfilter: the nwfilter to hold a reference on
12059 * Increment the reference count on the nwfilter. For each
12060 * additional call to this method, there shall be a corresponding
12061 * call to virNWFilterFree to release the reference count, once
12062 * the caller no longer needs the reference to this object.
12064 * This method is typically useful for applications where multiple
12065 * threads are using a connection, and it is required that the
12066 * connection remain open until all threads have finished using
12067 * it. ie, each new thread using an nwfilter would increment
12068 * the reference count.
12070 * Returns 0 in case of success, -1 in case of failure.
12073 virNWFilterRef(virNWFilterPtr nwfilter)
12075 if ((!VIR_IS_CONNECTED_NWFILTER(nwfilter))) {
12076 virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
12077 virDispatchError(NULL);
12078 return -1;
12080 virMutexLock(&nwfilter->conn->lock);
12081 DEBUG("nwfilter=%p refs=%d", nwfilter, nwfilter->refs);
12082 nwfilter->refs++;
12083 virMutexUnlock(&nwfilter->conn->lock);
12084 return 0;
12089 * virInterfaceIsActive:
12090 * @iface: pointer to the interface object
12092 * Determine if the interface is currently running
12094 * Returns 1 if running, 0 if inactive, -1 on error
12096 int virInterfaceIsActive(virInterfacePtr iface)
12098 DEBUG("iface=%p", iface);
12100 virResetLastError();
12102 if (!VIR_IS_CONNECTED_INTERFACE(iface)) {
12103 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
12104 virDispatchError(NULL);
12105 return (-1);
12107 if (iface->conn->interfaceDriver->interfaceIsActive) {
12108 int ret;
12109 ret = iface->conn->interfaceDriver->interfaceIsActive(iface);
12110 if (ret < 0)
12111 goto error;
12112 return ret;
12115 virLibConnError(iface->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12116 error:
12117 virDispatchError(iface->conn);
12118 return -1;
12123 * virConnectIsEncrypted:
12124 * @conn: pointer to the connection object
12126 * Determine if the connection to the hypervisor is encrypted
12128 * Returns 1 if encrypted, 0 if not encrypted, -1 on error
12130 int virConnectIsEncrypted(virConnectPtr conn)
12132 DEBUG("conn=%p", conn);
12134 virResetLastError();
12136 if (!VIR_IS_CONNECT(conn)) {
12137 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
12138 virDispatchError(NULL);
12139 return (-1);
12141 if (conn->driver->isEncrypted) {
12142 int ret;
12143 ret = conn->driver->isEncrypted(conn);
12144 if (ret < 0)
12145 goto error;
12146 return ret;
12149 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12150 error:
12151 virDispatchError(conn);
12152 return -1;
12156 * virConnectIsSecure:
12157 * @conn: pointer to the connection object
12159 * Determine if the connection to the hypervisor is secure
12161 * A connection will be classed as secure if it is either
12162 * encrypted, or running over a channel which is not exposed
12163 * to eavesdropping (eg a UNIX domain socket, or pipe)
12165 * Returns 1 if secure, 0 if secure, -1 on error
12167 int virConnectIsSecure(virConnectPtr conn)
12169 DEBUG("conn=%p", conn);
12171 virResetLastError();
12173 if (!VIR_IS_CONNECT(conn)) {
12174 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
12175 virDispatchError(NULL);
12176 return (-1);
12178 if (conn->driver->isSecure) {
12179 int ret;
12180 ret = conn->driver->isSecure(conn);
12181 if (ret < 0)
12182 goto error;
12183 return ret;
12186 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12187 error:
12188 virDispatchError(conn);
12189 return -1;
12194 * virConnectCompareCPU:
12195 * @conn: virConnect connection
12196 * @xmlDesc: XML describing the CPU to compare with host CPU
12197 * @flags: currently unused, pass 0
12199 * Compares the given CPU description with the host CPU
12201 * Returns comparison result according to enum virCPUCompareResult
12204 virConnectCompareCPU(virConnectPtr conn,
12205 const char *xmlDesc,
12206 unsigned int flags)
12208 VIR_DEBUG("conn=%p, xmlDesc=%s, flags=%u", conn, xmlDesc, flags);
12210 virResetLastError();
12212 if (!VIR_IS_CONNECT(conn)) {
12213 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
12214 virDispatchError(NULL);
12215 return VIR_CPU_COMPARE_ERROR;
12217 if (xmlDesc == NULL) {
12218 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
12219 goto error;
12222 if (conn->driver->cpuCompare) {
12223 int ret;
12225 ret = conn->driver->cpuCompare(conn, xmlDesc, flags);
12226 if (ret == VIR_CPU_COMPARE_ERROR)
12227 goto error;
12228 return ret;
12231 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12233 error:
12234 virDispatchError(conn);
12235 return VIR_CPU_COMPARE_ERROR;
12240 * virConnectBaselineCPU:
12242 * @conn: virConnect connection
12243 * @xmlCPUs: array of XML descriptions of host CPUs
12244 * @ncpus: number of CPUs in xmlCPUs
12245 * @flags: fine-tuning flags, currently unused, pass 0.
12247 * Computes the most feature-rich CPU which is compatible with all given
12248 * host CPUs.
12250 * Returns XML description of the computed CPU or NULL on error.
12252 char *
12253 virConnectBaselineCPU(virConnectPtr conn,
12254 const char **xmlCPUs,
12255 unsigned int ncpus,
12256 unsigned int flags)
12258 unsigned int i;
12260 VIR_DEBUG("conn=%p, xmlCPUs=%p, ncpus=%u, flags=%u",
12261 conn, xmlCPUs, ncpus, flags);
12262 if (xmlCPUs) {
12263 for (i = 0; i < ncpus; i++)
12264 VIR_DEBUG("xmlCPUs[%u]=%s", i, NULLSTR(xmlCPUs[i]));
12267 virResetLastError();
12269 if (!VIR_IS_CONNECT(conn)) {
12270 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
12271 virDispatchError(NULL);
12272 return NULL;
12274 if (xmlCPUs == NULL) {
12275 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
12276 goto error;
12279 if (conn->driver->cpuBaseline) {
12280 char *cpu;
12282 cpu = conn->driver->cpuBaseline(conn, xmlCPUs, ncpus, flags);
12283 if (!cpu)
12284 goto error;
12285 return cpu;
12288 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12290 error:
12291 virDispatchError(conn);
12292 return NULL;
12297 * virDomainGetJobInfo:
12298 * @domain: a domain object
12299 * @info: pointer to a virDomainJobInfo structure allocated by the user
12301 * Extract information about progress of a background job on a domain.
12302 * Will return an error if the domain is not active.
12304 * Returns 0 in case of success and -1 in case of failure.
12307 virDomainGetJobInfo(virDomainPtr domain, virDomainJobInfoPtr info)
12309 virConnectPtr conn;
12310 DEBUG("domain=%p, info=%p", domain, info);
12312 virResetLastError();
12314 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
12315 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
12316 virDispatchError(NULL);
12317 return (-1);
12319 if (info == NULL) {
12320 virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
12321 goto error;
12324 memset(info, 0, sizeof(virDomainJobInfo));
12326 conn = domain->conn;
12328 if (conn->driver->domainGetJobInfo) {
12329 int ret;
12330 ret = conn->driver->domainGetJobInfo (domain, info);
12331 if (ret < 0)
12332 goto error;
12333 return ret;
12336 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12338 error:
12339 virDispatchError(domain->conn);
12340 return -1;
12345 * virDomainAbortJob:
12346 * @domain: a domain object
12348 * Requests that the current background job be aborted at the
12349 * soonest opportunity. This will block until the job has
12350 * either completed, or aborted.
12352 * Returns 0 in case of success and -1 in case of failure.
12355 virDomainAbortJob(virDomainPtr domain)
12357 virConnectPtr conn;
12359 DEBUG("domain=%p", domain);
12361 virResetLastError();
12363 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
12364 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
12365 virDispatchError(NULL);
12366 return (-1);
12369 conn = domain->conn;
12370 if (conn->flags & VIR_CONNECT_RO) {
12371 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
12372 goto error;
12375 if (conn->driver->domainAbortJob) {
12376 int ret;
12377 ret = conn->driver->domainAbortJob(domain);
12378 if (ret < 0)
12379 goto error;
12380 return ret;
12383 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12385 error:
12386 virDispatchError(conn);
12387 return -1;
12392 * virDomainMigrateSetMaxDowntime:
12393 * @domain: a domain object
12394 * @downtime: maximum tolerable downtime for live migration, in milliseconds
12395 * @flags: fine-tuning flags, currently unused, use 0
12397 * Sets maximum tolerable time for which the domain is allowed to be paused
12398 * at the end of live migration. It's supposed to be called while the domain is
12399 * being live-migrated as a reaction to migration progress.
12401 * Returns 0 in case of success, -1 otherwise.
12404 virDomainMigrateSetMaxDowntime(virDomainPtr domain,
12405 unsigned long long downtime,
12406 unsigned int flags)
12408 virConnectPtr conn;
12410 DEBUG("domain=%p, downtime=%llu, flags=%u", domain, downtime, flags);
12412 virResetLastError();
12414 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
12415 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
12416 virDispatchError(NULL);
12417 return -1;
12420 conn = domain->conn;
12421 if (conn->flags & VIR_CONNECT_RO) {
12422 virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
12423 goto error;
12426 if (conn->driver->domainMigrateSetMaxDowntime) {
12427 if (conn->driver->domainMigrateSetMaxDowntime(domain, downtime, flags) < 0)
12428 goto error;
12429 return 0;
12432 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12433 error:
12434 virDispatchError(conn);
12435 return -1;
12439 * virConnectDomainEventRegisterAny:
12440 * @conn: pointer to the connection
12441 * @dom: pointer to the domain
12442 * @eventID: the event type to receive
12443 * @cb: callback to the function handling domain events
12444 * @opaque: opaque data to pass on to the callback
12445 * @freecb: optional function to deallocate opaque when not used anymore
12447 * Adds a callback to receive notifications of arbitrary domain events
12448 * occurring on a domain.
12450 * If dom is NULL, then events will be monitored for any domain. If dom
12451 * is non-NULL, then only the specific domain will be monitored
12453 * Most types of event have a callback providing a custom set of parameters
12454 * for the event. When registering an event, it is thus neccessary to use
12455 * the VIR_DOMAIN_EVENT_CALLBACK() macro to cast the supplied function pointer
12456 * to match the signature of this method.
12458 * The virDomainPtr object handle passed into the callback upon delivery
12459 * of an event is only valid for the duration of execution of the callback.
12460 * If the callback wishes to keep the domain object after the callback
12461 * returns, it shall take a reference to it, by calling virDomainRef.
12462 * The reference can be released once the object is no longer required
12463 * by calling virDomainFree.
12465 * The return value from this method is a positive integer identifier
12466 * for the callback. To unregister a callback, this callback ID should
12467 * be passed to the virDomainEventUnregisterAny method
12469 * Returns a callback identifier on success, -1 on failure
12472 virConnectDomainEventRegisterAny(virConnectPtr conn,
12473 virDomainPtr dom,
12474 int eventID,
12475 virConnectDomainEventGenericCallback cb,
12476 void *opaque,
12477 virFreeCallback freecb)
12479 DEBUG("conn=%p dom=%p, eventID=%d, cb=%p, opaque=%p, freecb=%p", conn, dom, eventID, cb, opaque, freecb);
12480 virResetLastError();
12482 if (!VIR_IS_CONNECT(conn)) {
12483 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
12484 virDispatchError(NULL);
12485 return (-1);
12487 if (dom != NULL &&
12488 !(VIR_IS_CONNECTED_DOMAIN(dom) && dom->conn == conn)) {
12489 virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
12490 virDispatchError(conn);
12491 return (-1);
12493 if (eventID < 0 || eventID >= VIR_DOMAIN_EVENT_ID_LAST || cb == NULL) {
12494 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
12495 goto error;
12498 if ((conn->driver) && (conn->driver->domainEventRegisterAny)) {
12499 int ret;
12500 ret = conn->driver->domainEventRegisterAny(conn, dom, eventID, cb, opaque, freecb);
12501 if (ret < 0)
12502 goto error;
12503 return ret;
12506 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12507 error:
12508 virDispatchError(conn);
12509 return -1;
12513 * virConnectDomainEventDeregisterAny:
12514 * @conn: pointer to the connection
12515 * @callbackID: the callback identifier
12517 * Removes an event callback. The callbackID parameter should be the
12518 * vaule obtained from a previous virDomainEventRegisterAny method.
12520 * Returns 0 on success, -1 on failure
12523 virConnectDomainEventDeregisterAny(virConnectPtr conn,
12524 int callbackID)
12526 DEBUG("conn=%p, callbackID=%d", conn, callbackID);
12528 virResetLastError();
12530 if (!VIR_IS_CONNECT(conn)) {
12531 virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
12532 virDispatchError(NULL);
12533 return (-1);
12535 if (callbackID < 0) {
12536 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
12537 goto error;
12539 if ((conn->driver) && (conn->driver->domainEventDeregisterAny)) {
12540 int ret;
12541 ret = conn->driver->domainEventDeregisterAny(conn, callbackID);
12542 if (ret < 0)
12543 goto error;
12544 return ret;
12547 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12548 error:
12549 virDispatchError(conn);
12550 return -1;
12554 * virDomainManagedSave:
12555 * @dom: pointer to the domain
12556 * @flags: optional flags currently unused
12558 * This method will suspend a domain and save its memory contents to
12559 * a file on disk. After the call, if successful, the domain is not
12560 * listed as running anymore.
12561 * The difference from virDomainSave() is that libvirt is keeping track of
12562 * the saved state itself, and will reuse it once the domain is being
12563 * restarted (automatically or via an explicit libvirt call).
12564 * As a result any running domain is sure to not have a managed saved image.
12566 * Returns 0 in case of success or -1 in case of failure
12568 int virDomainManagedSave(virDomainPtr dom, unsigned int flags)
12570 virConnectPtr conn;
12572 VIR_DEBUG("dom=%p, flags=%u", dom, flags);
12574 virResetLastError();
12576 if (!VIR_IS_CONNECTED_DOMAIN(dom)) {
12577 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
12578 virDispatchError(NULL);
12579 return -1;
12582 conn = dom->conn;
12583 if (conn->flags & VIR_CONNECT_RO) {
12584 virLibDomainError(dom, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
12585 goto error;
12588 if (conn->driver->domainManagedSave) {
12589 int ret;
12591 ret = conn->driver->domainManagedSave(dom, flags);
12592 if (ret < 0)
12593 goto error;
12594 return ret;
12597 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12599 error:
12600 virDispatchError(conn);
12601 return -1;
12605 * virDomainHasManagedSaveImage:
12606 * @dom: pointer to the domain
12607 * @flags: optional flags currently unused
12609 * Check if a domain has a managed save image as created by
12610 * virDomainManagedSave(). Note that any running domain should not have
12611 * such an image, as it should have been removed on restart.
12613 * Returns 0 if no image is present, 1 if an image is present, and
12614 * -1 in case of error
12616 int virDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
12618 virConnectPtr conn;
12620 VIR_DEBUG("dom=%p, flags=%u", dom, flags);
12622 virResetLastError();
12624 if (!VIR_IS_CONNECTED_DOMAIN(dom)) {
12625 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
12626 virDispatchError(NULL);
12627 return -1;
12630 conn = dom->conn;
12632 if (conn->driver->domainHasManagedSaveImage) {
12633 int ret;
12635 ret = conn->driver->domainHasManagedSaveImage(dom, flags);
12636 if (ret < 0)
12637 goto error;
12638 return ret;
12641 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12643 error:
12644 virDispatchError(conn);
12645 return -1;
12649 * virDomainManagedSaveRemove:
12650 * @dom: pointer to the domain
12651 * @flags: optional flags currently unused
12653 * Remove any managed save image for this domain.
12655 * Returns 0 in case of success, and -1 in case of error
12657 int virDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags)
12659 virConnectPtr conn;
12661 VIR_DEBUG("dom=%p, flags=%u", dom, flags);
12663 virResetLastError();
12665 if (!VIR_IS_CONNECTED_DOMAIN(dom)) {
12666 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
12667 virDispatchError(NULL);
12668 return -1;
12671 conn = dom->conn;
12672 if (conn->flags & VIR_CONNECT_RO) {
12673 virLibDomainError(dom, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
12674 goto error;
12677 if (conn->driver->domainManagedSaveRemove) {
12678 int ret;
12680 ret = conn->driver->domainManagedSaveRemove(dom, flags);
12681 if (ret < 0)
12682 goto error;
12683 return ret;
12686 virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12688 error:
12689 virDispatchError(conn);
12690 return -1;
12694 * virDomainSnapshotCreateXML:
12695 * @domain: a domain object
12696 * @xmlDesc: string containing an XML description of the domain
12697 * @flags: unused flag parameters; callers should pass 0
12699 * Creates a new snapshot of a domain based on the snapshot xml
12700 * contained in xmlDesc.
12702 * Returns an (opaque) virDomainSnapshotPtr on success, NULL on failure.
12704 virDomainSnapshotPtr
12705 virDomainSnapshotCreateXML(virDomainPtr domain,
12706 const char *xmlDesc,
12707 unsigned int flags)
12709 virConnectPtr conn;
12711 DEBUG("domain=%p, xmlDesc=%s, flags=%u", domain, xmlDesc, flags);
12713 virResetLastError();
12715 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
12716 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
12717 virDispatchError(NULL);
12718 return NULL;
12721 conn = domain->conn;
12722 if (conn->flags & VIR_CONNECT_RO) {
12723 virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
12724 goto error;
12727 if (conn->driver->domainSnapshotCreateXML) {
12728 virDomainSnapshotPtr ret;
12729 ret = conn->driver->domainSnapshotCreateXML(domain, xmlDesc, flags);
12730 if (!ret)
12731 goto error;
12732 return ret;
12735 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12736 error:
12737 virDispatchError(conn);
12738 return NULL;
12742 * virDomainSnapshotGetXMLDesc:
12743 * @snapshot: a domain snapshot object
12744 * @flags: unused flag parameters; callers should pass 0
12746 * Provide an XML description of the domain snapshot.
12748 * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
12749 * the caller must free() the returned value.
12751 char *
12752 virDomainSnapshotGetXMLDesc(virDomainSnapshotPtr snapshot,
12753 unsigned int flags)
12755 virConnectPtr conn;
12756 DEBUG("snapshot=%p, flags=%d", snapshot, flags);
12758 virResetLastError();
12760 if (!VIR_IS_DOMAIN_SNAPSHOT(snapshot)) {
12761 virLibDomainSnapshotError(NULL, VIR_ERR_INVALID_DOMAIN_SNAPSHOT,
12762 __FUNCTION__);
12763 virDispatchError(NULL);
12764 return (NULL);
12767 conn = snapshot->domain->conn;
12769 if ((conn->flags & VIR_CONNECT_RO) && (flags & VIR_DOMAIN_XML_SECURE)) {
12770 virLibConnError(conn, VIR_ERR_OPERATION_DENIED,
12771 _("virDomainSnapshotGetXMLDesc with secure flag"));
12772 goto error;
12775 if (conn->driver->domainSnapshotDumpXML) {
12776 char *ret;
12777 ret = conn->driver->domainSnapshotDumpXML(snapshot, flags);
12778 if (!ret)
12779 goto error;
12780 return ret;
12783 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12784 error:
12785 virDispatchError(conn);
12786 return NULL;
12790 * virDomainSnapshotNum:
12791 * @domain: a domain object
12792 * @flags: unused flag parameters; callers should pass 0
12794 * Provides the number of domain snapshots for this domain..
12796 * Returns the number of domain snapshost found or -1 in case of error.
12799 virDomainSnapshotNum(virDomainPtr domain, unsigned int flags)
12801 virConnectPtr conn;
12802 DEBUG("domain=%p", domain);
12804 virResetLastError();
12806 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
12807 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
12808 virDispatchError(NULL);
12809 return -1;
12812 conn = domain->conn;
12813 if (conn->driver->domainSnapshotNum) {
12814 int ret = conn->driver->domainSnapshotNum(domain, flags);
12815 if (ret < 0)
12816 goto error;
12817 return ret;
12820 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12821 error:
12822 virDispatchError(conn);
12823 return -1;
12827 * virDomainSnapshotListNames:
12828 * @domain: a domain object
12829 * @names: array to collect the list of names of snapshots
12830 * @nameslen: size of @names
12831 * @flags: unused flag parameters; callers should pass 0
12833 * Collect the list of domain snapshots for the given domain, and store
12834 * their names in @names. Caller is responsible for freeing each member
12835 * of the array.
12837 * Returns the number of domain snapshots found or -1 in case of error.
12840 virDomainSnapshotListNames(virDomainPtr domain, char **names, int nameslen,
12841 unsigned int flags)
12843 virConnectPtr conn;
12845 DEBUG("domain=%p, names=%p, nameslen=%d, flags=%u",
12846 domain, names, nameslen, flags);
12848 virResetLastError();
12850 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
12851 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
12852 virDispatchError(NULL);
12853 return -1;
12856 conn = domain->conn;
12858 if ((names == NULL) || (nameslen < 0)) {
12859 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
12860 goto error;
12863 if (conn->driver->domainSnapshotListNames) {
12864 int ret = conn->driver->domainSnapshotListNames(domain, names,
12865 nameslen, flags);
12866 if (ret < 0)
12867 goto error;
12868 return ret;
12871 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12872 error:
12873 virDispatchError(conn);
12874 return -1;
12878 * virDomainSnapshotLookupByName:
12879 * @domain: a domain object
12880 * @name: name for the domain snapshot
12881 * @flags: unused flag parameters; callers should pass 0
12883 * Try to lookup a domain snapshot based on its name.
12885 * Returns a domain snapshot object or NULL in case of failure. If the
12886 * domain snapshot cannot be found, then the VIR_ERR_NO_DOMAIN_SNAPSHOT
12887 * error is raised.
12889 virDomainSnapshotPtr
12890 virDomainSnapshotLookupByName(virDomainPtr domain,
12891 const char *name,
12892 unsigned int flags)
12894 virConnectPtr conn;
12895 DEBUG("domain=%p, name=%s, flags=%u", domain, name, flags);
12897 virResetLastError();
12899 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
12900 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
12901 virDispatchError(NULL);
12902 return (NULL);
12905 conn = domain->conn;
12907 if (name == NULL) {
12908 virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
12909 goto error;
12912 if (conn->driver->domainSnapshotLookupByName) {
12913 virDomainSnapshotPtr dom;
12914 dom = conn->driver->domainSnapshotLookupByName(domain, name, flags);
12915 if (!dom)
12916 goto error;
12917 return dom;
12920 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12921 error:
12922 virDispatchError(conn);
12923 return NULL;
12927 * virDomainHasCurrentSnapshot:
12928 * @domain: pointer to the domain object
12929 * @flags: unused flag parameters; callers should pass 0
12931 * Determine if the domain has a current snapshot.
12933 * Returns 1 if such snapshot exists, 0 if it doesn't, -1 on error.
12936 virDomainHasCurrentSnapshot(virDomainPtr domain, unsigned int flags)
12938 virConnectPtr conn;
12939 DEBUG("domain=%p, flags=%u", domain, flags);
12941 virResetLastError();
12943 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
12944 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
12945 virDispatchError(NULL);
12946 return -1;
12949 conn = domain->conn;
12951 if (conn->driver->domainHasCurrentSnapshot) {
12952 int ret = conn->driver->domainHasCurrentSnapshot(domain, flags);
12953 if (ret < 0)
12954 goto error;
12955 return ret;
12958 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
12959 error:
12960 virDispatchError(conn);
12961 return -1;
12965 * virDomainSnapshotCurrent:
12966 * @domain: a domain object
12967 * @flags: unused flag parameters; callers should pass 0
12969 * Get the current snapshot for a domain, if any.
12971 * Returns a domain snapshot object or NULL in case of failure. If the
12972 * current domain snapshot cannot be found, then the VIR_ERR_NO_DOMAIN_SNAPSHOT
12973 * error is raised.
12975 virDomainSnapshotPtr
12976 virDomainSnapshotCurrent(virDomainPtr domain,
12977 unsigned int flags)
12979 virConnectPtr conn;
12980 DEBUG("domain=%p, flags=%u", domain, flags);
12982 virResetLastError();
12984 if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
12985 virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
12986 virDispatchError(NULL);
12987 return (NULL);
12990 conn = domain->conn;
12992 if (conn->driver->domainSnapshotCurrent) {
12993 virDomainSnapshotPtr snap;
12994 snap = conn->driver->domainSnapshotCurrent(domain, flags);
12995 if (!snap)
12996 goto error;
12997 return snap;
13000 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
13001 error:
13002 virDispatchError(conn);
13003 return NULL;
13007 * virDomainRevertToSnapshot:
13008 * @snapshot: a domain snapshot object
13009 * @flags: unused flag parameters; callers should pass 0
13011 * Revert the domain to a given snapshot.
13013 * Returns 0 if the creation is successful, -1 on error.
13016 virDomainRevertToSnapshot(virDomainSnapshotPtr snapshot,
13017 unsigned int flags)
13019 virConnectPtr conn;
13021 DEBUG("snapshot=%p, flags=%u", snapshot, flags);
13023 virResetLastError();
13025 if (!VIR_IS_DOMAIN_SNAPSHOT(snapshot)) {
13026 virLibDomainSnapshotError(NULL, VIR_ERR_INVALID_DOMAIN_SNAPSHOT,
13027 __FUNCTION__);
13028 virDispatchError(NULL);
13029 return -1;
13032 conn = snapshot->domain->conn;
13034 if (conn->driver->domainRevertToSnapshot) {
13035 int ret = conn->driver->domainRevertToSnapshot(snapshot, flags);
13036 if (ret < 0)
13037 goto error;
13038 return ret;
13041 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
13042 error:
13043 virDispatchError(conn);
13044 return -1;
13048 * virDomainSnapshotDelete:
13049 * @snapshot: a domain snapshot object
13050 * @flags: flag parameters
13052 * Delete the snapshot.
13054 * If @flags is 0, then just this snapshot is deleted, and changes from
13055 * this snapshot are automatically merged into children snapshots. If
13056 * flags is VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN, then this snapshot
13057 * and any children snapshots are deleted.
13059 * Returns 0 if the snapshot was successfully deleted, -1 on error.
13062 virDomainSnapshotDelete(virDomainSnapshotPtr snapshot,
13063 unsigned int flags)
13065 virConnectPtr conn;
13067 DEBUG("snapshot=%p, flags=%u", snapshot, flags);
13069 virResetLastError();
13071 if (!VIR_IS_DOMAIN_SNAPSHOT(snapshot)) {
13072 virLibDomainSnapshotError(NULL, VIR_ERR_INVALID_DOMAIN_SNAPSHOT,
13073 __FUNCTION__);
13074 virDispatchError(NULL);
13075 return -1;
13078 conn = snapshot->domain->conn;
13080 if (conn->driver->domainSnapshotDelete) {
13081 int ret = conn->driver->domainSnapshotDelete(snapshot, flags);
13082 if (ret < 0)
13083 goto error;
13084 return ret;
13087 virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
13088 error:
13089 virDispatchError(conn);
13090 return -1;
13094 * virDomainSnapshotFree:
13095 * @snapshot: a domain snapshot object
13097 * Free the domain snapshot object. The snapshot itself is not modified.
13098 * The data structure is freed and should not be used thereafter.
13100 * Returns 0 in case of success and -1 in case of failure.
13103 virDomainSnapshotFree(virDomainSnapshotPtr snapshot)
13105 DEBUG("snapshot=%p", snapshot);
13107 virResetLastError();
13109 if (!VIR_IS_DOMAIN_SNAPSHOT(snapshot)) {
13110 virLibDomainSnapshotError(NULL, VIR_ERR_INVALID_DOMAIN_SNAPSHOT,
13111 __FUNCTION__);
13112 virDispatchError(NULL);
13113 return -1;
13115 if (virUnrefDomainSnapshot(snapshot) < 0) {
13116 virDispatchError(NULL);
13117 return -1;
13119 return 0;