提交 b421c35c 编写于 作者: J John Ferlan

hellolibvirt: Adjust code to use new APIs

Change the order of some conditions and use the AllDomains API to get
a list of all the active and defined domains, then use the Active and
Name API's in order to print.

This changes here adjust the output from:

Attempting to connect to hypervisor
Connected to hypervisor at "qemu:///system"
Hypervisor: "QEMU" version: 0.32.656
There are 0 active and 2 inactive domains
Inactive domains:
  foo
  bar
Disconnected from hypervisor

to

Attempting to connect to hypervisor
Connected to hypervisor at "qemu:///system"
Hypervisor: "QEMU" version: 0.32.656
There are 0 active and 2 inactive domains
       foo (non-active)
       bar (non-active)
Disconnected from hypervisor
上级 f7e74294
/* This file contains trivial example code to connect to the running /* This file contains trivial example code to connect to the running
* hypervisor and gather a few bits of information. */ * hypervisor and gather a few bits of information about domains.
* Similar API's exist for storage pools, networks, and interfaces. */
#include <config.h> #include <config.h>
...@@ -15,7 +16,7 @@ showError(virConnectPtr conn) ...@@ -15,7 +16,7 @@ showError(virConnectPtr conn)
virErrorPtr err; virErrorPtr err;
err = malloc(sizeof(*err)); err = malloc(sizeof(*err));
if (NULL == err) { if (!err) {
printf("Could not allocate memory for error data\n"); printf("Could not allocate memory for error data\n");
goto out; goto out;
} }
...@@ -56,7 +57,7 @@ showHypervisorInfo(virConnectPtr conn) ...@@ -56,7 +57,7 @@ showHypervisorInfo(virConnectPtr conn)
* to fail if, for example, there is no connection to a * to fail if, for example, there is no connection to a
* hypervisor, so check what it returns. */ * hypervisor, so check what it returns. */
hvType = virConnectGetType(conn); hvType = virConnectGetType(conn);
if (NULL == hvType) { if (!hvType) {
ret = 1; ret = 1;
printf("Failed to get hypervisor type\n"); printf("Failed to get hypervisor type\n");
showError(conn); showError(conn);
...@@ -90,10 +91,16 @@ static int ...@@ -90,10 +91,16 @@ static int
showDomains(virConnectPtr conn) showDomains(virConnectPtr conn)
{ {
int ret = 0, i, numNames, numInactiveDomains, numActiveDomains; int ret = 0, i, numNames, numInactiveDomains, numActiveDomains;
char **nameList = NULL; int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
VIR_CONNECT_LIST_DOMAINS_INACTIVE;
virDomainPtr *nameList = NULL;
/* NB: The return from the virConnectNum*() APIs is only useful for
* the current call. A domain could be started or stopped and any
* assumptions made purely on these return values could result in
* unexpected results */
numActiveDomains = virConnectNumOfDomains(conn); numActiveDomains = virConnectNumOfDomains(conn);
if (-1 == numActiveDomains) { if (numActiveDomains == -1) {
ret = 1; ret = 1;
printf("Failed to get number of active domains\n"); printf("Failed to get number of active domains\n");
showError(conn); showError(conn);
...@@ -101,7 +108,7 @@ showDomains(virConnectPtr conn) ...@@ -101,7 +108,7 @@ showDomains(virConnectPtr conn)
} }
numInactiveDomains = virConnectNumOfDefinedDomains(conn); numInactiveDomains = virConnectNumOfDefinedDomains(conn);
if (-1 == numInactiveDomains) { if (numInactiveDomains == -1) {
ret = 1; ret = 1;
printf("Failed to get number of inactive domains\n"); printf("Failed to get number of inactive domains\n");
showError(conn); showError(conn);
...@@ -111,39 +118,24 @@ showDomains(virConnectPtr conn) ...@@ -111,39 +118,24 @@ showDomains(virConnectPtr conn)
printf("There are %d active and %d inactive domains\n", printf("There are %d active and %d inactive domains\n",
numActiveDomains, numInactiveDomains); numActiveDomains, numInactiveDomains);
nameList = malloc(sizeof(*nameList) * numInactiveDomains); /* Return a list of all active and inactive domains. Using this API
* instead of virConnectListDomains() and virConnectListDefinedDomains()
if (NULL == nameList) { * is preferred since it "solves" an inherit race between separated API
ret = 1; * calls if domains are started or stopped between calls */
printf("Could not allocate memory for list of inactive domains\n"); numNames = virConnectListAllDomains(conn,
goto out; &nameList,
} flags);
for (i = 0; i < numNames; i++) {
numNames = virConnectListDefinedDomains(conn, int active = virDomainIsActive(nameList[i]);
nameList, printf(" %8s (%s)\n",
numInactiveDomains); virDomainGetName(nameList[i]),
(active == 1 ? "active" : "non-active"));
if (-1 == numNames) { /* must free the returned named per the API documentation */
ret = 1; virDomainFree(nameList[i]);
printf("Could not get list of defined domains from hypervisor\n");
showError(conn);
goto out;
}
if (numNames > 0) {
printf("Inactive domains:\n");
}
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));
} }
free(nameList);
out: out:
free(nameList);
return ret; return ret;
} }
...@@ -163,7 +155,7 @@ main(int argc, char *argv[]) ...@@ -163,7 +155,7 @@ main(int argc, char *argv[])
* except, possibly, the URI of the hypervisor. */ * except, possibly, the URI of the hypervisor. */
conn = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0); conn = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0);
if (NULL == conn) { if (!conn) {
ret = 1; ret = 1;
printf("No connection to hypervisor\n"); printf("No connection to hypervisor\n");
showError(conn); showError(conn);
...@@ -171,7 +163,7 @@ main(int argc, char *argv[]) ...@@ -171,7 +163,7 @@ main(int argc, char *argv[])
} }
uri = virConnectGetURI(conn); uri = virConnectGetURI(conn);
if (NULL == uri) { if (!uri) {
ret = 1; ret = 1;
printf("Failed to get URI for hypervisor connection\n"); printf("Failed to get URI for hypervisor connection\n");
showError(conn); showError(conn);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册