libvirt/examples/admin/list_servers.c
Erik Skultety fbb8205de3 examples: admin: Add some examples for the new admin APIs
Some of the examples make use of asprintf and strtol functions (to keep
things simple) which are prohibited to use within our code (enforced by
syntax-check). Therefore besides adding some examples, this patch also updates
cfg.mk to exclude examples directory from asprintf and strtol rules, as well as
updates .gitignore to exclude all the new admin binaries created in the
'examples' dir.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
2016-06-26 00:21:06 +02:00

43 lines
1.3 KiB
C

#include<stdio.h>
#include<stdlib.h>
#include<libvirt/libvirt-admin.h>
int main(void)
{
int ret = -1;
virAdmConnectPtr conn = NULL;
virAdmServerPtr *servers = NULL; /* where to store the servers */
virAdmServerPtr *tmp = NULL;
size_t i = 0;
int count = 0;
/* first, open a connection to the daemon */
if (!(conn = virAdmConnectOpen(NULL, 0)))
goto cleanup;
/* get the available servers on the default daemon - libvirtd */
if ((count = virAdmConnectListServers(conn, &servers, 0)) < 0)
goto cleanup;
/* let's print the available servers, we have 2 options how to interate
* over the returned list, use @count as the boundary or use the fact
* that @servers are guaranteed to contain 1 extra element NULL;
* this example uses the second option
*/
printf(" %-15s\n", "Server name");
printf("---------------\n");
for (tmp = servers; *tmp; tmp++)
printf(" %-15s\n", virAdmServerGetName(*tmp));
ret = 0;
cleanup:
/* Once finished, free the list of servers and close the connection
* properly, @conn will be deallocated automatically
*/
for (i = 0; i < count; i++)
virAdmServerFree(servers[i]);
free(servers);
virAdmConnectClose(conn);
return ret;
}