examples: Organize C examples into categories
[libvirt/ericb.git] / examples / domain / info1.c
blobf0ea9b9f08d14d708106b44b093a44dc10d8c255
1 /**
2 * section: Informations
3 * synopsis: Extract information about Xen domain 0
4 * purpose: Demonstrate the basic use of the library to connect to the
5 * hypervisor and extract domain information.
6 * usage: info1
7 * test: info1
8 * copy: see Copyright for the status of this software.
9 */
11 #include <stdio.h>
12 #include <libvirt/libvirt.h>
14 /**
15 * getDomainInfo:
16 * @name: the name of the domain
18 * extract the domain 0 information
20 static void
21 getDomainInfo(const char *uri, const char *name)
23 virConnectPtr conn = NULL; /* the hypervisor connection */
24 virDomainPtr dom = NULL; /* the domain being checked */
25 virDomainInfo info; /* the information being fetched */
26 int ret;
28 conn = virConnectOpenReadOnly(uri);
29 if (conn == NULL) {
30 fprintf(stderr, "Failed to connect to hypervisor\n");
31 goto error;
34 /* Find the domain of the given name */
35 dom = virDomainLookupByName(conn, name);
36 if (dom == NULL) {
37 fprintf(stderr, "Failed to find Domain %s\n", name);
38 goto error;
41 /* Get the information */
42 ret = virDomainGetInfo(dom, &info);
43 if (ret < 0) {
44 fprintf(stderr, "Failed to get information for Domain %s\n", name);
45 goto error;
48 printf("Domain %s: %d CPUs\n", name, info.nrVirtCpu);
50 error:
51 if (dom != NULL)
52 virDomainFree(dom);
53 if (conn != NULL)
54 virConnectClose(conn);
57 int main(int argc, char **argv)
59 if (argc != 3) {
60 fprintf(stderr, "syntax: %s: URI NAME\n", argv[0]);
61 return 1;
63 getDomainInfo(argv[1], argv[2]);
65 return 0;