virclosecallbacks.c 9.8 KB
Newer Older
1 2 3
/*
 * virclosecallbacks.c: Connection close callbacks routines
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2013-2014 Red Hat, Inc.
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
 *
 * 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/>.
 *
 * Authors:
 *      Daniel P. Berrange <berrange@redhat.com>
 *      Michal Privoznik <mprivozn@redhat.com>
 */

#include <config.h>

#include "viralloc.h"
#include "virclosecallbacks.h"
#include "virlog.h"
#include "virobject.h"

#define VIR_FROM_THIS VIR_FROM_NONE

34 35
VIR_LOG_INIT("util.closecallbacks");

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
typedef struct _virDriverCloseDef virDriverCloseDef;
typedef virDriverCloseDef *virDriverCloseDefPtr;
struct _virDriverCloseDef {
    virConnectPtr conn;
    virCloseCallback cb;
};

struct _virCloseCallbacks {
    virObjectLockable parent;

    /* UUID string to qemuDriverCloseDef mapping */
    virHashTablePtr list;
};


static virClassPtr virCloseCallbacksClass;
static void virCloseCallbacksDispose(void *obj);

static int virCloseCallbacksOnceInit(void)
{
56
    if (!VIR_CLASS_NEW(virCloseCallbacks, virClassForObjectLockable()))
57
        return -1;
58 59

    return 0;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
}

VIR_ONCE_GLOBAL_INIT(virCloseCallbacks)


virCloseCallbacksPtr
virCloseCallbacksNew(void)
{
    virCloseCallbacksPtr closeCallbacks;

    if (virCloseCallbacksInitialize() < 0)
        return NULL;

    if (!(closeCallbacks = virObjectLockableNew(virCloseCallbacksClass)))
        return NULL;

E
Eric Blake 已提交
76
    closeCallbacks->list = virHashCreate(5, virHashValueFree);
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    if (!closeCallbacks->list) {
        virObjectUnref(closeCallbacks);
        return NULL;
    }

    return closeCallbacks;
}

static void
virCloseCallbacksDispose(void *obj)
{
    virCloseCallbacksPtr closeCallbacks = obj;

    virHashFree(closeCallbacks->list);
}

int
virCloseCallbacksSet(virCloseCallbacksPtr closeCallbacks,
                     virDomainObjPtr vm,
                     virConnectPtr conn,
                     virCloseCallback cb)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    virDriverCloseDefPtr closeDef;
    int ret = -1;

    virUUIDFormat(vm->def->uuid, uuidstr);
    VIR_DEBUG("vm=%s, uuid=%s, conn=%p, cb=%p",
              vm->def->name, uuidstr, conn, cb);

    virObjectLock(closeCallbacks);

    closeDef = virHashLookup(closeCallbacks->list, uuidstr);
    if (closeDef) {
        if (closeDef->conn != conn) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Close callback for domain %s already registered"
                             " with another connection %p"),
                           vm->def->name, closeDef->conn);
            goto cleanup;
        }
        if (closeDef->cb && closeDef->cb != cb) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Another close callback is already defined for"
                             " domain %s"), vm->def->name);
            goto cleanup;
        }

        closeDef->cb = cb;
    } else {
        if (VIR_ALLOC(closeDef) < 0)
            goto cleanup;

        closeDef->conn = conn;
        closeDef->cb = cb;
        if (virHashAddEntry(closeCallbacks->list, uuidstr, closeDef) < 0) {
            VIR_FREE(closeDef);
            goto cleanup;
        }
136
        virObjectRef(vm);
137 138
    }

139
    virObjectRef(closeCallbacks);
140
    ret = 0;
141
 cleanup:
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    virObjectUnlock(closeCallbacks);
    return ret;
}

int
virCloseCallbacksUnset(virCloseCallbacksPtr closeCallbacks,
                       virDomainObjPtr vm,
                       virCloseCallback cb)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    virDriverCloseDefPtr closeDef;
    int ret = -1;

    virUUIDFormat(vm->def->uuid, uuidstr);
    VIR_DEBUG("vm=%s, uuid=%s, cb=%p",
              vm->def->name, uuidstr, cb);

    virObjectLock(closeCallbacks);

    closeDef = virHashLookup(closeCallbacks->list, uuidstr);
    if (!closeDef)
        goto cleanup;

    if (closeDef->cb && closeDef->cb != cb) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Trying to remove mismatching close callback for"
                         " domain %s"), vm->def->name);
        goto cleanup;
    }

172 173 174 175 176
    if (virHashRemoveEntry(closeCallbacks->list, uuidstr) < 0)
        goto cleanup;

    virObjectUnref(vm);
    ret = 0;
177
 cleanup:
178
    virObjectUnlock(closeCallbacks);
179 180
    if (!ret)
        virObjectUnref(closeCallbacks);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    return ret;
}

virCloseCallback
virCloseCallbacksGet(virCloseCallbacksPtr closeCallbacks,
                     virDomainObjPtr vm,
                     virConnectPtr conn)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    virDriverCloseDefPtr closeDef;
    virCloseCallback cb = NULL;

    virUUIDFormat(vm->def->uuid, uuidstr);
    VIR_DEBUG("vm=%s, uuid=%s, conn=%p",
              vm->def->name, uuidstr, conn);

    virObjectLock(closeCallbacks);

    closeDef = virHashLookup(closeCallbacks->list, uuidstr);
    if (closeDef && (!conn || closeDef->conn == conn))
        cb = closeDef->cb;

    virObjectUnlock(closeCallbacks);

    VIR_DEBUG("cb=%p", cb);
    return cb;
}

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
virConnectPtr
virCloseCallbacksGetConn(virCloseCallbacksPtr closeCallbacks,
                         virDomainObjPtr vm)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    virDriverCloseDefPtr closeDef;
    virConnectPtr conn = NULL;

    virUUIDFormat(vm->def->uuid, uuidstr);
    VIR_DEBUG("vm=%s, uuid=%s", vm->def->name, uuidstr);

    virObjectLock(closeCallbacks);

    closeDef = virHashLookup(closeCallbacks->list, uuidstr);
    if (closeDef)
        conn = closeDef->conn;

    virObjectUnlock(closeCallbacks);

    VIR_DEBUG("conn=%p", conn);
    return conn;
}


233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
typedef struct _virCloseCallbacksListEntry virCloseCallbacksListEntry;
typedef virCloseCallbacksListEntry *virCloseCallbacksListEntryPtr;
struct _virCloseCallbacksListEntry {
    unsigned char uuid[VIR_UUID_BUFLEN];
    virCloseCallback callback;
};

typedef struct _virCloseCallbacksList virCloseCallbacksList;
typedef virCloseCallbacksList *virCloseCallbacksListPtr;
struct _virCloseCallbacksList {
    size_t nentries;
    virCloseCallbacksListEntryPtr entries;
};

struct virCloseCallbacksData {
    virConnectPtr conn;
    virCloseCallbacksListPtr list;
    bool oom;
};

253
static int
254 255 256 257 258 259 260 261 262 263
virCloseCallbacksGetOne(void *payload,
                        const void *key,
                        void *opaque)
{
    struct virCloseCallbacksData *data = opaque;
    virDriverCloseDefPtr closeDef = payload;
    const char *uuidstr = key;
    unsigned char uuid[VIR_UUID_BUFLEN];

    if (virUUIDParse(uuidstr, uuid) < 0)
264
        return 0;
265 266 267 268 269

    VIR_DEBUG("conn=%p, thisconn=%p, uuid=%s, cb=%p",
              closeDef->conn, data->conn, uuidstr, closeDef->cb);

    if (data->conn != closeDef->conn || !closeDef->cb)
270
        return 0;
271 272 273 274

    if (VIR_EXPAND_N(data->list->entries,
                     data->list->nentries, 1) < 0) {
        data->oom = true;
275
        return 0;
276 277 278 279 280
    }

    memcpy(data->list->entries[data->list->nentries - 1].uuid,
           uuid, VIR_UUID_BUFLEN);
    data->list->entries[data->list->nentries - 1].callback = closeDef->cb;
281
    return 0;
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
}

static virCloseCallbacksListPtr
virCloseCallbacksGetForConn(virCloseCallbacksPtr closeCallbacks,
                            virConnectPtr conn)
{
    virCloseCallbacksListPtr list = NULL;
    struct virCloseCallbacksData data;

    if (VIR_ALLOC(list) < 0)
        return NULL;

    data.conn = conn;
    data.list = list;
    data.oom = false;

    virHashForEach(closeCallbacks->list, virCloseCallbacksGetOne, &data);

    if (data.oom) {
        VIR_FREE(list->entries);
        VIR_FREE(list);
        virReportOOMError();
        return NULL;
    }

    return list;
}


void
virCloseCallbacksRun(virCloseCallbacksPtr closeCallbacks,
                     virConnectPtr conn,
                     virDomainObjListPtr domains,
                     void *opaque)
{
    virCloseCallbacksListPtr list;
    size_t i;

    VIR_DEBUG("conn=%p", conn);

    /* We must not hold the lock while running the callbacks,
     * so first we obtain the list of callbacks, then remove
     * them all from the hash. At that point we can release
     * the lock and run the callbacks safely. */

    virObjectLock(closeCallbacks);
    list = virCloseCallbacksGetForConn(closeCallbacks, conn);
329
    if (!list) {
J
John Ferlan 已提交
330
        virObjectUnlock(closeCallbacks);
331
        return;
332
    }
333 334

    for (i = 0; i < list->nentries; i++) {
335 336 337
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(list->entries[i].uuid, uuidstr);
        virHashRemoveEntry(closeCallbacks->list, uuidstr);
338 339 340 341 342 343
    }
    virObjectUnlock(closeCallbacks);

    for (i = 0; i < list->nentries; i++) {
        virDomainObjPtr vm;

344
        /* Grab a ref and lock to the vm */
345 346
        if (!(vm = virDomainObjListFindByUUID(domains,
                                              list->entries[i].uuid))) {
347 348 349 350 351 352
            char uuidstr[VIR_UUID_STRING_BUFLEN];
            virUUIDFormat(list->entries[i].uuid, uuidstr);
            VIR_DEBUG("No domain object with UUID %s", uuidstr);
            continue;
        }

353 354 355 356
        /* Remove the ref taken out during virCloseCallbacksSet since
         * we're about to call the callback function and we have another
         * ref anyway (so it cannot be deleted).
         *
357
         * Call the callback function and end the API usage. */
358
        virObjectUnref(vm);
359
        list->entries[i].callback(vm, conn, opaque);
360
        virDomainObjEndAPI(&vm);
361 362 363 364
    }
    VIR_FREE(list->entries);
    VIR_FREE(list);
}