summaryrefslogtreecommitdiff
path: root/examples/admin/client_close.c
blob: 5c28ce794912476ff6b7493f7367bb7cb77bf27e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#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;
}