diff --git a/include/libvir.h b/include/libvir.h index 20b855cab8..86d0e60875 100644 --- a/include/libvir.h +++ b/include/libvir.h @@ -124,6 +124,12 @@ int virConnectListDomains (virConnectPtr conn, int *ids, int maxids); +/* + * Number of domains + */ +int virConnectNumOfDomains (virConnectPtr conn); + + /* * Domain creation and destruction */ diff --git a/src/libvir.c b/src/libvir.c index fb711c4a56..bd578dc4be 100644 --- a/src/libvir.c +++ b/src/libvir.c @@ -257,6 +257,34 @@ done: return(ret); } +/** + * virConnectNumOfDomains: + * @conn: pointer to the hypervisor connection + * + * Returns the number of domain found or -1 in case of error + */ +int +virConnectNumOfDomains(virConnectPtr conn) { + struct xs_transaction_handle* t; + int ret = -1; + unsigned int num; + char **idlist = NULL; + + if ((conn == NULL) || (conn->magic != VIR_CONNECT_MAGIC)) + return(-1); + + t = xs_transaction_start(conn->xshandle); + if (t) { + idlist = xs_directory(conn->xshandle, t, "/local/domain", &num); + if (idlist) { + free(idlist); + ret = num; + } + xs_transaction_end(conn->xshandle, t, 0); + } + return(ret); +} + /** * virDomainCreateLinux: * @conn: pointer to the hypervisor connection