[libvirt] [PATCH 6/6] admin: Usage example of the new server listing API

Erik Skultety eskultet at redhat.com
Tue Aug 11 07:59:01 UTC 2015


Not to be actually pushed since majority of this example will be merged
into virt-admin once it's ready, i.e. virsh splitting series is merged,
but might be good to just see the API's working.
---
 .gitignore                   |  1 +
 Makefile.am                  |  2 +-
 configure.ac                 |  1 +
 examples/admin/Makefile.am   | 25 +++++++++++++++
 examples/admin/listservers.c | 73 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 101 insertions(+), 1 deletion(-)
 create mode 100644 examples/admin/Makefile.am
 create mode 100644 examples/admin/listservers.c

diff --git a/.gitignore b/.gitignore
index 0b40f4a..325f04f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -76,6 +76,7 @@
 /docs/libvirt-refs.xml
 /docs/search.php
 /docs/todo.html.in
+/examples/admin/listservers
 /examples/object-events/event-test
 /examples/dominfo/info1
 /examples/domsuspend/suspend
diff --git a/Makefile.am b/Makefile.am
index 91b943b..c14229e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -24,7 +24,7 @@ SUBDIRS = . gnulib/lib include src daemon tools docs gnulib/tests \
   examples/dominfo examples/domsuspend examples/apparmor \
   examples/xml/nwfilter examples/openauth examples/systemtap \
   tools/wireshark examples/dommigrate \
-  examples/lxcconvert examples/domtop
+  examples/lxcconvert examples/domtop examples/admin
 
 ACLOCAL_AMFLAGS = -I m4
 
diff --git a/configure.ac b/configure.ac
index 46c80ce..35c8cd9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2805,6 +2805,7 @@ AC_CONFIG_FILES([\
         examples/systemtap/Makefile \
         examples/xml/nwfilter/Makefile \
         examples/lxcconvert/Makefile \
+        examples/admin/Makefile \
         tools/wireshark/Makefile \
         tools/wireshark/src/Makefile])
 AC_OUTPUT
diff --git a/examples/admin/Makefile.am b/examples/admin/Makefile.am
new file mode 100644
index 0000000..8373132
--- /dev/null
+++ b/examples/admin/Makefile.am
@@ -0,0 +1,25 @@
+## Copyright (C) 2005-2015 Red Hat, Inc.
+##
+## This library is free software; you can redistribute it and/or
+## modify it under the terms of the GNU Lesser General Public
+## License as published by the Free Software Foundation; either
+## version 2.1 of the License, or (at your option) any later version.
+##
+## This library is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## Lesser General Public License for more details.
+##
+## You should have received a copy of the GNU Lesser General Public
+## License along with this library.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include
+LDADDS = $(STATIC_BINARIES) $(WARN_CFLAGS) \
+		 $(top_builddir)/src/libvirt-admin.la $(COVERAGE_LDFLAGS)
+
+noinst_PROGRAMS=listservers
+
+listservers_SOURCES=listservers.c
+listservers_LDFLAGS=
+listservers_LDADD= $(LDADDS)
diff --git a/examples/admin/listservers.c b/examples/admin/listservers.c
new file mode 100644
index 0000000..6f11d8d
--- /dev/null
+++ b/examples/admin/listservers.c
@@ -0,0 +1,73 @@
+/*
+ * listservers.c: Demo program to show listing of available servers
+ *
+ * Copyright (C) 2015 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Erik Skultety <eskultet at redhat.com>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <libvirt/libvirt-admin.h>
+
+static int
+listDaemonServers(void)
+{
+    int ret = -1;
+    virAdmConnectPtr conn = NULL;
+    virAdmServerPtr *srvs = NULL;
+    int nsrvs = 0;
+    size_t i;
+
+    /* Connect to an admin server on a specific daemon, NULL in this case means
+     * connect to libvirtd UNIX socket
+     */
+    if (!(conn = virAdmConnectOpen(NULL, 0))) {
+        fprintf(stderr, "Failed to connect to the admin server\n");
+        goto cleanup;
+    }
+
+    /* Obtain a list of available servers on the daemon */
+    if ((nsrvs = virAdmConnectListServers(conn, &srvs, 0)) < 0) {
+        fprintf(stderr, "Failed to obtain list of available servers\n");
+        goto cleanup;
+    }
+
+    printf(" %-5s %-15s\n", "Id", "Name");
+    printf("---------------\n");
+    for (i = 0; i < nsrvs; i++)
+        printf(" %-5d %-15s\n", virAdmGetServerID(srvs[i]),
+               virAdmGetServerName(srvs[i]));
+
+    ret = nsrvs;
+
+ cleanup:
+    if (conn)
+        virAdmConnectClose(conn);
+    if (srvs) {
+        for (i = 0; i < nsrvs; i++)
+            virAdmServerFree(srvs[i]);
+        free(srvs);
+    }
+
+    return ret;
+}
+
+int main(void)
+{
+    return listDaemonServers();
+}
-- 
2.4.3




More information about the libvir-list mailing list