Add a list of active domains, list of active/inactive networks, and list of active/inactive storage pools --- examples/hellolibvirt/hellolibvirt.c | 201 ++++++++++++++++++++++++++++++----- 1 file changed, 173 insertions(+), 28 deletions(-) diff --git a/examples/hellolibvirt/hellolibvirt.c b/examples/hellolibvirt/hellolibvirt.c index 234637e..f191782 100644 --- a/examples/hellolibvirt/hellolibvirt.c +++ b/examples/hellolibvirt/hellolibvirt.c @@ -85,65 +85,200 @@ out: return ret; } - +typedef int (*virFunction)(virConnectPtr conn, + char **nameList, + int maxnames); static int -showDomains(virConnectPtr conn) +listObject(virConnectPtr conn, int maxnames, const char *objNameStr, + virFunction fcn) { - int ret = 0, i, numNames, numInactiveDomains, numActiveDomains; + int ret = 0, i, numNames; char **nameList = NULL; - numActiveDomains = virConnectNumOfDomains(conn); - if (-1 == numActiveDomains) { + nameList = malloc(sizeof(*nameList) * maxnames); + + if (NULL == nameList) { ret = 1; - printf("Failed to get number of active domains\n"); + printf("Could not allocate memory for list of %s\n", objNameStr); + goto out; + } + + numNames = (*fcn)(conn, nameList, maxnames); + + if (-1 == numNames) { + ret = 1; + printf("Could not get list of %s from hypervisor\n", objNameStr); showError(conn); goto out; } - numInactiveDomains = virConnectNumOfDefinedDomains(conn); - if (-1 == numInactiveDomains) { + printf(" %s: \n", objNameStr); + for (i = 0; i < numNames; i++) { + printf("\t%s\n", *(nameList + i)); + /* The API documentation doesn't say so, but the names + * returned by are strdup'd and must be freed here. + */ + free(*(nameList + i)); + } + +out: + free(nameList); + return ret; +} + +static int +showNetworks(virConnectPtr conn) +{ + int ret = 0, numInactiveNetworks, numActiveNetworks; + + numActiveNetworks = virConnectNumOfNetworks(conn); + if (-1 == numActiveNetworks) { ret = 1; - printf("Failed to get number of inactive domains\n"); + printf("Failed to get number of active networks\n"); showError(conn); goto out; } - printf("There are %d active and %d inactive domains\n", - numActiveDomains, numInactiveDomains); + numInactiveNetworks = virConnectNumOfDefinedNetworks(conn); + if (-1 == numInactiveNetworks) { + ret = 1; + printf("Failed to get number of inactive networks\n"); + showError(conn); + goto out; + } - nameList = malloc(sizeof(*nameList) * numInactiveDomains); + printf("There are %d active and %d inactive networks\n", + numActiveNetworks, numInactiveNetworks); - if (NULL == nameList) { + if (numActiveNetworks) { + ret = listObject(conn, numActiveNetworks, "active networks", + virConnectListNetworks); + if (ret != 0) + goto out; + } + + if (numInactiveNetworks) { + ret = listObject(conn, numInactiveNetworks, "defined networks", + virConnectListDefinedNetworks); + if (ret != 0) + goto out; + } + +out: + return ret; +} + +static int +showStoragePools(virConnectPtr conn) +{ + int ret = 0, numInactiveStoragePools, numActiveStoragePools; + + numActiveStoragePools = virConnectNumOfStoragePools(conn); + if (-1 == numActiveStoragePools) { + ret = 1; + printf("Failed to get number of active storage pools\n"); + showError(conn); + goto out; + } + + numInactiveStoragePools = virConnectNumOfDefinedStoragePools(conn); + if (-1 == numInactiveStoragePools) { ret = 1; - printf("Could not allocate memory for list of inactive domains\n"); + printf("Failed to get number of inactive storage pools\n"); + showError(conn); goto out; } - numNames = virConnectListDefinedDomains(conn, - nameList, - numInactiveDomains); + printf("There are %d active and %d inactive storage pools\n", + numActiveStoragePools, numInactiveStoragePools); - if (-1 == numNames) { + if (numActiveStoragePools) { + ret = listObject(conn, numActiveStoragePools, "active storage pools", + virConnectListStoragePools); + if (ret != 0) + goto out; + } + + if (numInactiveStoragePools) { + ret = listObject(conn, numInactiveStoragePools, + "inactive storage pools", + virConnectListDefinedStoragePools); + if (ret != 0) + goto out; + } + +out: + return ret; +} + +static int +showDomains(virConnectPtr conn) +{ + int ret = 0, numInactiveDomains, numActiveDomains; + + numActiveDomains = virConnectNumOfDomains(conn); + if (-1 == numActiveDomains) { ret = 1; - printf("Could not get list of defined domains from hypervisor\n"); + printf("Failed to get number of active domains\n"); showError(conn); goto out; } - if (numNames > 0) { - printf("Inactive domains:\n"); + numInactiveDomains = virConnectNumOfDefinedDomains(conn); + if (-1 == numInactiveDomains) { + ret = 1; + printf("Failed to get number of inactive domains\n"); + showError(conn); + goto out; } - for (i = 0 ; i < numNames ; i++) { - printf(" %s\n", *(nameList + i)); - /* The API documentation doesn't say so, but the names - * returned by virConnectListDefinedDomains are strdup'd and - * must be freed here. */ - free(*(nameList + i)); + printf("There are %d active and %d inactive domains\n", + numActiveDomains, numInactiveDomains); + + if (numActiveDomains) { + int nIDs, i; + int *IDs; + + /* Slightly different than the others... */ + + IDs = malloc(sizeof(*IDs) * numActiveDomains); + + if (NULL == IDs) { + ret = 1; + printf("Could not allocate memory for list of active domains\n"); + goto out; + } + + nIDs = virConnectListDomains(conn, IDs, numActiveDomains); + if (-1 == nIDs) { + ret = 1; + printf("Could not get list of active domains from hypervisor\n"); + showError(conn); + goto out; + } + + printf(" active domains: \n"); + for (i = 0; i < nIDs; i++) { + virDomainPtr dom = virDomainLookupByID(conn, *(IDs + i)); + if (dom) { + printf("\tID=%d Name=%s\n", *(IDs + i), virDomainGetName(dom)); + virDomainFree(dom); + } else { + printf("\tID=%d\n", *(IDs + i)); + } + } + + free(IDs); + } + + if (numInactiveDomains) { + ret = listObject(conn, numInactiveDomains, "defined domains", + virConnectListDefinedDomains); + if (ret != 0) + goto out; } out: - free(nameList); return ret; } @@ -191,6 +326,16 @@ main(int argc, char *argv[]) goto disconnect; } + if (0 != showNetworks(conn)) { + ret = 1; + goto disconnect; + } + + if (0 != showStoragePools(conn)) { + ret = 1; + goto disconnect; + } + disconnect: if (0 != virConnectClose(conn)) { printf("Failed to disconnect from hypervisor\n"); -- 1.7.11.7 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list