libvirt/examples/admin/client_close.c
Eric Blake acf522e85a examples: Avoid gnulib, have standalone examples
Commit 0c6ad476 updated gnulib, which rearranged some of the
conditions in gnulib wrapper headers such that compilation
started failing on BSD systems when the normal system <unistd.h>
tried to include another system header but instead got a
gnulib wrapper header in an incomplete state; this is because
gnulib headers only work if <config.h> is included first.

Commit b6f78259 papered over the symptoms of that by including
<config.h> in all the examples.  But this logic is backwards -
if our examples are truly meant to be stand-alone, they should
NOT depend on how libvirt was configured, and should NOT
depend on the gnulib fixes for system quirks.  In particular,
if an example does not need to link against libgnulib.la,
then it also does not need to use -Ignulib in its compile
flags, and likewise does not need to include <config.h> since
none of the gnulib wrapper headers should be interfering.

So, revert (most of) b6f78259 (except for the bogus pre-patch
use of "config.h" in admin/logging.c: if config.h is included,
it should be via <> rather than "", and must be before any
system headers); then additionally nuke all mention of
<config.h>, -Ignulib, and -llibgnu.la, making all of the
examples truly standalone.

Signed-off-by: Eric Blake <eblake@redhat.com>
Acked-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2019-01-08 09:26:51 -06:00

58 lines
2.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>
#include <libvirt/libvirt-admin.h>
int main(void)
{
int ret = -1;
virAdmConnectPtr conn1 = NULL; /* admin connection */
virConnectPtr conn2 = NULL; /* libvirt standard connection */
virAdmServerPtr srv = NULL; /* which server is the client connected to */
virAdmClientPtr clnt = NULL; /* which client to disconnect */
/* first, open a standard libvirt connection to the daemon */
if (!(conn2 = virConnectOpen(NULL)))
return -1;
/* next, open an admin connection that will be used to disconnect the
* standard libvirt client
*/
if (!(conn1 = virAdmConnectOpen(NULL, 0)))
goto cleanup;
/* a virAdmServerPtr handle is needed, so a server lookup is performed */
if (!(srv = virAdmConnectLookupServer(conn1, "libvirtd", 0)))
goto cleanup;
/* a virAdmClientPtr handle is also necessary, so lookup for client is
* performed as well
*/
if (!(clnt = virAdmServerLookupClient(srv, 1, 0)))
goto cleanup;
/* finally, use the client handle to disconnect the standard libvirt client
* from libvirtd daemon
*/
if (virAdmClientClose(clnt, 0) < 0)
goto cleanup;
ret = 0;
cleanup:
/* Once finished, both server and client handles need to be freed and
* both connections @conn1 and @conn2 should be closed to free the
* memory.
* NOTE: Although @conn2 has been disconnected, unlike disconnecting by
* calling virConnectClose which closes the connection voluntarily and
* frees the object automatically, virAdmClientClose is a forceful
* disconnect of another client (client can use it on itself as well).
* Therefore no automatic deallocation of the object takes place and is
* the callers responsibility to do so.
*/
virAdmClientFree(clnt);
virAdmServerFree(srv);
virAdmConnectClose(conn1);
virConnectClose(conn2);
return ret;
}