domain_event.c 12.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
/*
 * domain_event.c: domain event queue processing helpers
 *
 * Copyright (C) 2008 VirtualIron
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Ben Guthro
 */

#include <config.h>

#include "domain_event.h"
26
#include "logging.h"
27 28
#include "datatypes.h"
#include "memory.h"
C
Cole Robinson 已提交
29 30 31 32 33 34 35
#include "virterror_internal.h"

#define VIR_FROM_THIS VIR_FROM_NONE

#define eventReportError(conn, code, fmt...)                        \
    virReportErrorHelper(conn, VIR_FROM_THIS, code, __FILE__,       \
                         __FUNCTION__, __LINE__, fmt)
36 37 38 39 40 41 42 43 44 45 46 47


/**
 * virDomainEventCallbackListFree:
 * @list: event callback list head
 *
 * Free the memory in the domain event callback list
 */
void
virDomainEventCallbackListFree(virDomainEventCallbackListPtr list)
{
    int i;
48 49 50
    if (!list)
        return;

51
    for (i=0; i<list->count; i++) {
52 53 54
        virFreeCallback freecb = list->callbacks[i]->freecb;
        if (freecb)
            (*freecb)(list->callbacks[i]->opaque);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        VIR_FREE(list->callbacks[i]);
    }
    VIR_FREE(list);
}
/**
 * virDomainEventCallbackListRemove:
 * @conn: pointer to the connection
 * @cbList: the list
 * @callback: the callback to remove
 *
 * Internal function to remove a callback from a virDomainEventCallbackListPtr
 */
int
virDomainEventCallbackListRemove(virConnectPtr conn,
                                 virDomainEventCallbackListPtr cbList,
                                 virConnectDomainEventCallback callback)
{
    int i;
    for (i = 0 ; i < cbList->count ; i++) {
        if(cbList->callbacks[i]->cb == callback &&
           cbList->callbacks[i]->conn == conn) {
76 77 78
            virFreeCallback freecb = cbList->callbacks[i]->freecb;
            if (freecb)
                (*freecb)(cbList->callbacks[i]->opaque);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
            virUnrefConnect(cbList->callbacks[i]->conn);
            VIR_FREE(cbList->callbacks[i]);

            if (i < (cbList->count - 1))
                memmove(cbList->callbacks + i,
                        cbList->callbacks + i + 1,
                        sizeof(*(cbList->callbacks)) *
                                (cbList->count - (i + 1)));

            if (VIR_REALLOC_N(cbList->callbacks,
                              cbList->count - 1) < 0) {
                ; /* Failure to reduce memory allocation isn't fatal */
            }
            cbList->count--;

            return 0;
        }
    }
C
Cole Robinson 已提交
97 98 99

    eventReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s",
                     _("could not find event callback for removal"));
100 101 102
    return -1;
}

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 136 137 138 139 140
/**
 * virDomainEventCallbackListRemoveConn:
 * @conn: pointer to the connection
 * @cbList: the list
 *
 * Internal function to remove all of a given connection's callback
 * from a virDomainEventCallbackListPtr
 */
int
virDomainEventCallbackListRemoveConn(virConnectPtr conn,
                                     virDomainEventCallbackListPtr cbList)
{
    int old_count = cbList->count;
    int i;
    for (i = 0 ; i < cbList->count ; i++) {
        if(cbList->callbacks[i]->conn == conn) {
            virFreeCallback freecb = cbList->callbacks[i]->freecb;
            if (freecb)
                (*freecb)(cbList->callbacks[i]->opaque);
            virUnrefConnect(cbList->callbacks[i]->conn);
            VIR_FREE(cbList->callbacks[i]);

            if (i < (cbList->count - 1))
                memmove(cbList->callbacks + i,
                        cbList->callbacks + i + 1,
                        sizeof(*(cbList->callbacks)) *
                                (cbList->count - (i + 1)));
            cbList->count--;
            i--;
        }
    }
    if (cbList->count < old_count &&
        VIR_REALLOC_N(cbList->callbacks, cbList->count) < 0) {
        ; /* Failure to reduce memory allocation isn't fatal */
    }
    return 0;
}

141 142 143 144 145 146 147 148 149 150 151 152
int virDomainEventCallbackListMarkDelete(virConnectPtr conn,
                                         virDomainEventCallbackListPtr cbList,
                                         virConnectDomainEventCallback callback)
{
    int i;
    for (i = 0 ; i < cbList->count ; i++) {
        if (cbList->callbacks[i]->conn == conn &&
            cbList->callbacks[i]->cb == callback) {
            cbList->callbacks[i]->deleted = 1;
            return 0;
        }
    }
C
Cole Robinson 已提交
153 154 155

    eventReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s",
                     _("could not find event callback for deletion"));
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    return -1;
}

int virDomainEventCallbackListPurgeMarked(virDomainEventCallbackListPtr cbList)
{
    int old_count = cbList->count;
    int i;
    for (i = 0 ; i < cbList->count ; i++) {
        if (cbList->callbacks[i]->deleted) {
            virFreeCallback freecb = cbList->callbacks[i]->freecb;
            if (freecb)
                (*freecb)(cbList->callbacks[i]->opaque);
            virUnrefConnect(cbList->callbacks[i]->conn);
            VIR_FREE(cbList->callbacks[i]);

            if (i < (cbList->count - 1))
                memmove(cbList->callbacks + i,
                        cbList->callbacks + i + 1,
                        sizeof(*(cbList->callbacks)) *
                                (cbList->count - (i + 1)));
            cbList->count--;
            i--;
        }
    }
    if (cbList->count < old_count &&
        VIR_REALLOC_N(cbList->callbacks, cbList->count) < 0) {
        ; /* Failure to reduce memory allocation isn't fatal */
    }
    return 0;
}

187 188 189 190 191 192 193 194 195 196 197 198 199
/**
 * virDomainEventCallbackListAdd:
 * @conn: pointer to the connection
 * @cbList: the list
 * @callback: the callback to add
 * @opaque: opaque data tio pass to callback
 *
 * Internal function to add a callback from a virDomainEventCallbackListPtr
 */
int
virDomainEventCallbackListAdd(virConnectPtr conn,
                              virDomainEventCallbackListPtr cbList,
                              virConnectDomainEventCallback callback,
200 201
                              void *opaque,
                              virFreeCallback freecb)
202 203 204 205 206 207 208 209 210 211 212 213 214
{
    virDomainEventCallbackPtr event;
    int n;

    /* Check incoming */
    if ( !cbList ) {
        return -1;
    }

    /* check if we already have this callback on our list */
    for (n=0; n < cbList->count; n++) {
        if(cbList->callbacks[n]->cb == callback &&
           conn == cbList->callbacks[n]->conn) {
C
Cole Robinson 已提交
215 216
            eventReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s",
                             _("event callback already tracked"));
217 218 219 220 221
            return -1;
        }
    }
    /* Allocate new event */
    if (VIR_ALLOC(event) < 0) {
C
Cole Robinson 已提交
222
        virReportOOMError(conn);
223 224 225 226 227
        return -1;
    }
    event->conn = conn;
    event->cb = callback;
    event->opaque = opaque;
228
    event->freecb = freecb;
229 230 231 232

    /* Make space on list */
    n = cbList->count;
    if (VIR_REALLOC_N(cbList->callbacks, n + 1) < 0) {
C
Cole Robinson 已提交
233
        virReportOOMError(conn);
234 235 236 237 238 239 240 241 242 243 244
        VIR_FREE(event);
        return -1;
    }

    event->conn->refs++;

    cbList->callbacks[n] = event;
    cbList->count++;
    return 0;
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258
void virDomainEventFree(virDomainEventPtr event)
{
    if (!event)
        return;

    VIR_FREE(event->name);
    VIR_FREE(event);
}


virDomainEventQueuePtr virDomainEventQueueNew(void)
{
    virDomainEventQueuePtr ret;

C
Cole Robinson 已提交
259 260
    if (VIR_ALLOC(ret) < 0) {
        virReportOOMError(NULL);
261
        return NULL;
C
Cole Robinson 已提交
262
    }
263 264 265 266 267 268 269 270 271 272

    return ret;
}

virDomainEventPtr virDomainEventNew(int id, const char *name,
                                    const unsigned char *uuid,
                                    int type, int detail)
{
    virDomainEventPtr event;

C
Cole Robinson 已提交
273 274
    if (VIR_ALLOC(event) < 0) {
        virReportOOMError(NULL);
275
        return NULL;
C
Cole Robinson 已提交
276
    }
277 278 279 280

    event->type = type;
    event->detail = detail;
    if (!(event->name = strdup(name))) {
C
Cole Robinson 已提交
281
        virReportOOMError(NULL);
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
        VIR_FREE(event);
        return NULL;
    }
    event->id = id;
    memcpy(event->uuid, uuid, VIR_UUID_BUFLEN);

    return event;
}

virDomainEventPtr virDomainEventNewFromDom(virDomainPtr dom, int type, int detail)
{
    return virDomainEventNew(dom->id, dom->name, dom->uuid, type, detail);
}

virDomainEventPtr virDomainEventNewFromObj(virDomainObjPtr obj, int type, int detail)
{
    return virDomainEventNewFromDef(obj->def, type, detail);
}

virDomainEventPtr virDomainEventNewFromDef(virDomainDefPtr def, int type, int detail)
{
    return virDomainEventNew(def->id, def->name, def->uuid, type, detail);
}

306 307 308 309 310 311 312 313 314 315
/**
 * virDomainEventQueueFree:
 * @queue: pointer to the queue
 *
 * Free the memory in the queue. We process this like a list here
 */
void
virDomainEventQueueFree(virDomainEventQueuePtr queue)
{
    int i;
316 317 318 319 320
    if (!queue)
        return;

    for (i = 0; i < queue->count ; i++) {
        virDomainEventFree(queue->events[i]);
321
    }
322
    VIR_FREE(queue->events);
323 324 325 326
    VIR_FREE(queue);
}

/**
327
 * virDomainEventQueuePop:
328 329 330 331 332 333 334 335
 * @evtQueue: the queue of events
 *
 * Internal function to pop off, and return the front of the queue
 * NOTE: The caller is responsible for freeing the returned object
 *
 * Returns: virDomainEventPtr on success NULL on failure.
 */
virDomainEventPtr
336
virDomainEventQueuePop(virDomainEventQueuePtr evtQueue)
337 338 339
{
    virDomainEventPtr ret;

C
Cole Robinson 已提交
340 341 342
    if (!evtQueue || evtQueue->count == 0 ) {
        eventReportError(NULL, VIR_ERR_INTERNAL_ERROR, "%s",
                         _("event queue is empty, nothing to pop"));
343
        return NULL;
C
Cole Robinson 已提交
344
    }
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

    ret = evtQueue->events[0];

    memmove(evtQueue->events,
            evtQueue->events + 1,
            sizeof(*(evtQueue->events)) *
                    (evtQueue->count - 1));

    if (VIR_REALLOC_N(evtQueue->events,
                        evtQueue->count - 1) < 0) {
        ; /* Failure to reduce memory allocation isn't fatal */
    }
    evtQueue->count--;

    return ret;
}

/**
363
 * virDomainEventQueuePush:
364 365 366 367 368 369 370 371
 * @evtQueue: the dom event queue
 * @event: the event to add
 *
 * Internal function to push onto the back of an virDomainEventQueue
 *
 * Returns: 0 on success, -1 on failure
 */
int
372 373
virDomainEventQueuePush(virDomainEventQueuePtr evtQueue,
                        virDomainEventPtr event)
374
{
375
    if (!evtQueue) {
376 377 378 379 380 381
        return -1;
    }

    /* Make space on queue */
    if (VIR_REALLOC_N(evtQueue->events,
                      evtQueue->count + 1) < 0) {
C
Cole Robinson 已提交
382
        virReportOOMError(NULL);
383 384 385
        return -1;
    }

386
    evtQueue->events[evtQueue->count] = event;
387 388 389 390
    evtQueue->count++;
    return 0;
}

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

void virDomainEventDispatchDefaultFunc(virConnectPtr conn,
                                       virDomainEventPtr event,
                                       virConnectDomainEventCallback cb,
                                       void *cbopaque,
                                       void *opaque ATTRIBUTE_UNUSED)
{
    virDomainPtr dom = virGetDomain(conn, event->name, event->uuid);
    if (dom) {
        dom->id = event->id;
        (*cb)(conn, dom, event->type, event->detail, cbopaque);
        virDomainFree(dom);
    }
}


void virDomainEventDispatch(virDomainEventPtr event,
                            virDomainEventCallbackListPtr callbacks,
                            virDomainEventDispatchFunc dispatch,
                            void *opaque)
{
    int i;
    /* Cache this now, since we may be dropping the lock,
       and have more callbacks added. We're guarenteed not
       to have any removed */
    int cbCount = callbacks->count;

    for (i = 0 ; i < cbCount ; i++) {
        if (callbacks->callbacks[i] &&
            !callbacks->callbacks[i]->deleted) {
            (*dispatch)(callbacks->callbacks[i]->conn,
                        event,
                        callbacks->callbacks[i]->cb,
                        callbacks->callbacks[i]->opaque,
                        opaque);
        }
    }
}


void virDomainEventQueueDispatch(virDomainEventQueuePtr queue,
                                 virDomainEventCallbackListPtr callbacks,
                                 virDomainEventDispatchFunc dispatch,
                                 void *opaque)
{
    int i;

    for (i = 0 ; i < queue->count ; i++) {
        virDomainEventDispatch(queue->events[i], callbacks, dispatch, opaque);
        virDomainEventFree(queue->events[i]);
    }
    VIR_FREE(queue->events);
    queue->count = 0;
}