admin_remote.c 5.5 KB
Newer Older
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
/*
 * admin_remote.c
 *
 * 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@redhat.com>
 */

#include <config.h>
#include <rpc/rpc.h>
#include "admin_protocol.h"

typedef struct _remoteAdminPriv remoteAdminPriv;
typedef remoteAdminPriv *remoteAdminPrivPtr;

struct _remoteAdminPriv {
    virObjectLockable parent;

    int counter;
    virNetClientPtr client;
    virNetClientProgramPtr program;
};

static virClassPtr remoteAdminPrivClass;

static void
remoteAdminPrivDispose(void *opaque)
{
    remoteAdminPrivPtr priv = opaque;

    virObjectUnref(priv->program);
    virObjectUnref(priv->client);
}


static int
51
callFull(virAdmDaemonPtr dmn ATTRIBUTE_UNUSED,
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
         remoteAdminPrivPtr priv,
         int *fdin,
         size_t fdinlen,
         int **fdout,
         size_t *fdoutlen,
         int proc_nr,
         xdrproc_t args_filter, char *args,
         xdrproc_t ret_filter, char *ret)
{
    int rv;
    virNetClientProgramPtr prog = priv->program;
    int counter = priv->counter++;
    virNetClientPtr client = priv->client;

    /* Unlock, so that if we get any async events/stream data
     * while processing the RPC, we don't deadlock when our
     * callbacks for those are invoked
     */
    virObjectRef(priv);
    virObjectUnlock(priv);

    rv = virNetClientProgramCall(prog,
                                 client,
                                 counter,
                                 proc_nr,
                                 fdinlen, fdin,
                                 fdoutlen, fdout,
                                 args_filter, args,
                                 ret_filter, ret);

    virObjectLock(priv);
    virObjectUnref(priv);

    return rv;
}

static int
89
call(virAdmDaemonPtr dmn,
90 91 92 93 94 95 96
     unsigned int flags,
     int proc_nr,
     xdrproc_t args_filter, char *args,
     xdrproc_t ret_filter, char *ret)
{
    virCheckFlags(0, -1);

97
    return callFull(dmn, dmn->privateData,
98 99 100 101 102 103
                    NULL, 0, NULL, NULL, proc_nr,
                    args_filter, args, ret_filter, ret);
}

#include "admin_client.h"

104 105 106 107 108
static void
remoteAdminClientCloseFunc(virNetClientPtr client ATTRIBUTE_UNUSED,
                           int reason,
                           void *opaque)
{
109
    virAdmDaemonCloseCallbackDataPtr cbdata = opaque;
110 111 112 113 114 115

    virObjectLock(cbdata);

    if (cbdata->callback) {
        VIR_DEBUG("Triggering connection close callback %p reason=%d, opaque=%p",
                  cbdata->callback, reason, cbdata->opaque);
116
        cbdata->callback(cbdata->dmn, reason, cbdata->opaque);
117 118 119 120 121 122 123 124 125

        if (cbdata->freeCallback)
            cbdata->freeCallback(cbdata->opaque);
        cbdata->callback = NULL;
        cbdata->freeCallback = NULL;
    }
    virObjectUnlock(cbdata);
}

126
static int
127
remoteAdminDaemonOpen(virAdmDaemonPtr dmn, unsigned int flags)
128 129
{
    int rv = -1;
130 131
    remoteAdminPrivPtr priv = dmn->privateData;
    admin_daemon_open_args args;
132 133 134 135 136

    virObjectLock(priv);

    args.flags = flags;

137 138 139 140 141 142
    if (virNetClientRegisterAsyncIO(priv->client) < 0) {
        VIR_DEBUG("Failed to add event watch, disabling events and support for"
                  " keepalive messages");
        virResetLastError();
    }

143
    virObjectRef(dmn->closeCallback);
144
    virNetClientSetCloseCallback(priv->client, remoteAdminClientCloseFunc,
145
                                 dmn->closeCallback,
146 147
                                 virObjectFreeCallback);

148 149
    if (call(dmn, 0, ADMIN_PROC_DAEMON_OPEN,
             (xdrproc_t)xdr_admin_daemon_open_args, (char *)&args,
150 151 152 153 154 155 156 157 158 159 160 161
             (xdrproc_t)xdr_void, (char *)NULL) == -1) {
        goto done;
    }

    rv = 0;

 done:
    virObjectUnlock(priv);
    return rv;
}

static int
162
remoteAdminDaemonClose(virAdmDaemonPtr dmn)
163 164
{
    int rv = -1;
165
    remoteAdminPrivPtr priv = dmn->privateData;
166 167 168

    virObjectLock(priv);

169
    if (call(dmn, 0, ADMIN_PROC_DAEMON_CLOSE,
170 171 172 173 174
             (xdrproc_t)xdr_void, (char *)NULL,
             (xdrproc_t)xdr_void, (char *)NULL) == -1) {
        goto done;
    }

175 176
    virNetClientSetCloseCallback(priv->client, NULL, NULL, NULL);

177 178 179 180 181 182 183
    rv = 0;

 done:
    virObjectUnlock(priv);
    return rv;
}

184 185 186
static void
remoteAdminPrivFree(void *opaque)
{
187
    virAdmDaemonPtr dmn = opaque;
188

189 190
    remoteAdminDaemonClose(dmn);
    virObjectUnref(dmn->privateData);
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

static remoteAdminPrivPtr
remoteAdminPrivNew(const char *sock_path)
{
    remoteAdminPrivPtr priv = NULL;

    if (!(priv = virObjectLockableNew(remoteAdminPrivClass)))
        goto error;

    if (!(priv->client = virNetClientNewUNIX(sock_path, false, NULL)))
        goto error;

    if (!(priv->program = virNetClientProgramNew(ADMIN_PROGRAM,
                                                 ADMIN_PROTOCOL_VERSION,
                                                 NULL, 0, NULL)))
        goto error;

    if (virNetClientAddProgram(priv->client, priv->program) < 0)
        goto error;

    return priv;
 error:
    virObjectUnref(priv);
    return NULL;
}