examples: Organize C examples into categories
[libvirt/ericb.git] / examples / misc / hellolibvirt.c
blobbfb12846e6649004f7851c152e9867f70e9d8795
1 /* This file contains trivial example code to connect to the running
2 * hypervisor and gather a few bits of information about domains.
3 * Similar API's exist for storage pools, networks, and interfaces. */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <libvirt/libvirt.h>
8 #include <libvirt/virterror.h>
10 static int
11 showHypervisorInfo(virConnectPtr conn)
13 int ret = 0;
14 unsigned long hvVer, major, minor, release;
15 const char *hvType;
17 /* virConnectGetType returns a pointer to a static string, so no
18 * allocation or freeing is necessary; it is possible for the call
19 * to fail if, for example, there is no connection to a
20 * hypervisor, so check what it returns. */
21 hvType = virConnectGetType(conn);
22 if (!hvType) {
23 ret = 1;
24 printf("Failed to get hypervisor type: %s\n",
25 virGetLastErrorMessage());
26 goto out;
29 if (0 != virConnectGetVersion(conn, &hvVer)) {
30 ret = 1;
31 printf("Failed to get hypervisor version: %s\n",
32 virGetLastErrorMessage());
33 goto out;
36 major = hvVer / 1000000;
37 hvVer %= 1000000;
38 minor = hvVer / 1000;
39 release = hvVer % 1000;
41 printf("Hypervisor: \"%s\" version: %lu.%lu.%lu\n",
42 hvType,
43 major,
44 minor,
45 release);
47 out:
48 return ret;
52 static int
53 showDomains(virConnectPtr conn)
55 int ret = 0, numNames, numInactiveDomains, numActiveDomains;
56 ssize_t i;
57 int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
58 VIR_CONNECT_LIST_DOMAINS_INACTIVE;
59 virDomainPtr *nameList = NULL;
61 /* NB: The return from the virConnectNum*() APIs is only useful for
62 * the current call. A domain could be started or stopped and any
63 * assumptions made purely on these return values could result in
64 * unexpected results */
65 numActiveDomains = virConnectNumOfDomains(conn);
66 if (numActiveDomains == -1) {
67 ret = 1;
68 printf("Failed to get number of active domains: %s\n",
69 virGetLastErrorMessage());
70 goto out;
73 numInactiveDomains = virConnectNumOfDefinedDomains(conn);
74 if (numInactiveDomains == -1) {
75 ret = 1;
76 printf("Failed to get number of inactive domains: %s\n",
77 virGetLastErrorMessage());
78 goto out;
81 printf("There are %d active and %d inactive domains\n",
82 numActiveDomains, numInactiveDomains);
84 /* Return a list of all active and inactive domains. Using this API
85 * instead of virConnectListDomains() and virConnectListDefinedDomains()
86 * is preferred since it "solves" an inherit race between separated API
87 * calls if domains are started or stopped between calls */
88 numNames = virConnectListAllDomains(conn,
89 &nameList,
90 flags);
91 if (numNames == -1) {
92 ret = 1;
93 printf("Failed to get a list of all domains: %s\n",
94 virGetLastErrorMessage());
95 goto out;
98 for (i = 0; i < numNames; i++) {
99 int active = virDomainIsActive(nameList[i]);
100 printf(" %8s (%s)\n",
101 virDomainGetName(nameList[i]),
102 (active == 1 ? "active" : "non-active"));
103 /* must free the returned named per the API documentation */
104 virDomainFree(nameList[i]);
106 free(nameList);
108 out:
109 return ret;
114 main(int argc, char *argv[])
116 int ret = 0;
117 virConnectPtr conn;
118 char *uri;
120 printf("Attempting to connect to hypervisor\n");
122 uri = (argc > 0 ? argv[1] : NULL);
124 /* virConnectOpenAuth is called here with all default parameters,
125 * except, possibly, the URI of the hypervisor. */
126 conn = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0);
128 if (!conn) {
129 ret = 1;
130 printf("No connection to hypervisor: %s\n",
131 virGetLastErrorMessage());
132 goto out;
135 uri = virConnectGetURI(conn);
136 if (!uri) {
137 ret = 1;
138 printf("Failed to get URI for hypervisor connection: %s\n",
139 virGetLastErrorMessage());
140 goto disconnect;
143 printf("Connected to hypervisor at \"%s\"\n", uri);
144 free(uri);
146 if (0 != showHypervisorInfo(conn)) {
147 ret = 1;
148 goto disconnect;
151 if (0 != showDomains(conn)) {
152 ret = 1;
153 goto disconnect;
156 disconnect:
157 if (0 != virConnectClose(conn)) {
158 printf("Failed to disconnect from hypervisor: %s\n",
159 virGetLastErrorMessage());
160 ret = 1;
161 } else {
162 printf("Disconnected from hypervisor\n");
165 out:
166 return ret;