domain_event.c 52.9 KB
Newer Older
1 2 3
/*
 * domain_event.c: domain event queue processing helpers
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2010-2013 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26
 *
 * Author: Ben Guthro
 */

#include <config.h>

#include "domain_event.h"
27
#include "virlog.h"
28
#include "datatypes.h"
29
#include "viralloc.h"
30
#include "virerror.h"
31
#include "virstring.h"
C
Cole Robinson 已提交
32 33 34

#define VIR_FROM_THIS VIR_FROM_NONE

35 36 37 38 39 40 41 42
struct _virDomainMeta {
    int id;
    char *name;
    unsigned char uuid[VIR_UUID_BUFLEN];
};
typedef struct _virDomainMeta virDomainMeta;
typedef virDomainMeta *virDomainMetaPtr;

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
struct _virDomainEventCallbackList {
    unsigned int nextID;
    unsigned int count;
    virDomainEventCallbackPtr *callbacks;
};

struct _virDomainEventQueue {
    unsigned int count;
    virDomainEventPtr *events;
};

struct _virDomainEventState {
    /* The list of domain event callbacks */
    virDomainEventCallbackListPtr callbacks;
    /* The queue of domain events */
    virDomainEventQueuePtr queue;
    /* Timer for flushing events queue */
    int timer;
    /* Flag if we're in process of dispatching */
    bool isDispatching;
    virMutex lock;
};

66
struct _virDomainEventCallback {
67 68
    int callbackID;
    int eventID;
69
    virConnectPtr conn;
70 71
    virDomainMetaPtr dom;
    virConnectDomainEventGenericCallback cb;
72 73 74 75 76 77
    void *opaque;
    virFreeCallback freecb;
    int deleted;
};

struct _virDomainEvent {
78 79 80 81 82 83 84 85 86
    int eventID;

    virDomainMeta dom;

    union {
        struct {
            int type;
            int detail;
        } lifecycle;
87 88 89
        struct {
            long long offset;
        } rtcChange;
90 91 92
        struct {
            int action;
        } watchdog;
93 94 95 96
        struct {
            char *srcPath;
            char *devAlias;
            int action;
97
            char *reason;
98
        } ioError;
99 100 101 102 103 104 105
        struct {
            int phase;
            virDomainEventGraphicsAddressPtr local;
            virDomainEventGraphicsAddressPtr remote;
            char *authScheme;
            virDomainEventGraphicsSubjectPtr subject;
        } graphics;
106 107 108 109 110
        struct {
            char *path;
            int type;
            int status;
        } blockJob;
111 112 113 114 115 116
        struct {
            char *oldSrcPath;
            char *newSrcPath;
            char *devAlias;
            int reason;
        } diskChange;
117 118 119 120
        struct {
            char *devAlias;
            int reason;
        } trayChange;
121 122 123 124
        struct {
            /* In unit of 1024 bytes */
            unsigned long long actual;
        } balloonChange;
125 126 127
        struct {
            char *devAlias;
        } deviceRemoved;
128
    } data;
129
};
130 131 132 133 134 135 136

/**
 * virDomainEventCallbackListFree:
 * @list: event callback list head
 *
 * Free the memory in the domain event callback list
 */
137
static void
138 139
virDomainEventCallbackListFree(virDomainEventCallbackListPtr list)
{
140
    size_t i;
141 142 143
    if (!list)
        return;

144
    for (i=0; i<list->count; i++) {
145 146 147
        virFreeCallback freecb = list->callbacks[i]->freecb;
        if (freecb)
            (*freecb)(list->callbacks[i]->opaque);
148 149 150 151
        VIR_FREE(list->callbacks[i]);
    }
    VIR_FREE(list);
}
152 153


154 155 156 157 158 159 160 161
/**
 * virDomainEventCallbackListRemove:
 * @conn: pointer to the connection
 * @cbList: the list
 * @callback: the callback to remove
 *
 * Internal function to remove a callback from a virDomainEventCallbackListPtr
 */
162
static int
163 164 165 166
virDomainEventCallbackListRemove(virConnectPtr conn,
                                 virDomainEventCallbackListPtr cbList,
                                 virConnectDomainEventCallback callback)
{
167
    int ret = 0;
168
    size_t i;
169
    for (i = 0; i < cbList->count; i++) {
170 171 172
        if (cbList->callbacks[i]->cb == VIR_DOMAIN_EVENT_CALLBACK(callback) &&
            cbList->callbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE &&
            cbList->callbacks[i]->conn == conn) {
173 174 175
            virFreeCallback freecb = cbList->callbacks[i]->freecb;
            if (freecb)
                (*freecb)(cbList->callbacks[i]->opaque);
176
            virObjectUnref(cbList->callbacks[i]->conn);
177 178 179 180 181 182 183 184 185 186 187 188 189 190
            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--;

191
            for (i = 0; i < cbList->count; i++) {
192 193 194 195
                if (!cbList->callbacks[i]->deleted)
                    ret++;
            }
            return ret;
196 197
        }
    }
C
Cole Robinson 已提交
198

199 200
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("could not find event callback for removal"));
201 202 203
    return -1;
}

204

205 206 207 208 209 210 211 212
/**
 * virDomainEventCallbackListRemoveID:
 * @conn: pointer to the connection
 * @cbList: the list
 * @callback: the callback to remove
 *
 * Internal function to remove a callback from a virDomainEventCallbackListPtr
 */
213
static int
214 215 216 217
virDomainEventCallbackListRemoveID(virConnectPtr conn,
                                   virDomainEventCallbackListPtr cbList,
                                   int callbackID)
{
218
    int ret = 0;
219
    size_t i;
220
    for (i = 0; i < cbList->count; i++) {
221 222 223 224 225
        if (cbList->callbacks[i]->callbackID == callbackID &&
            cbList->callbacks[i]->conn == conn) {
            virFreeCallback freecb = cbList->callbacks[i]->freecb;
            if (freecb)
                (*freecb)(cbList->callbacks[i]->opaque);
226
            virObjectUnref(cbList->callbacks[i]->conn);
227 228 229 230 231 232 233 234 235 236 237 238 239 240
            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--;

241
            for (i = 0; i < cbList->count; i++) {
242 243 244 245
                if (!cbList->callbacks[i]->deleted)
                    ret++;
            }
            return ret;
246 247 248
        }
    }

249 250
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("could not find event callback for removal"));
251 252 253 254
    return -1;
}


255 256 257 258
static int
virDomainEventCallbackListMarkDelete(virConnectPtr conn,
                                     virDomainEventCallbackListPtr cbList,
                                     virConnectDomainEventCallback callback)
259
{
260
    int ret = 0;
261
    size_t i;
262
    for (i = 0; i < cbList->count; i++) {
263 264 265
        if (cbList->callbacks[i]->cb == VIR_DOMAIN_EVENT_CALLBACK(callback) &&
            cbList->callbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE &&
            cbList->callbacks[i]->conn == conn) {
266
            cbList->callbacks[i]->deleted = 1;
267
            for (i = 0; i < cbList->count; i++) {
268 269 270 271
                if (!cbList->callbacks[i]->deleted)
                    ret++;
            }
            return ret;
272 273
        }
    }
C
Cole Robinson 已提交
274

275 276
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("could not find event callback for deletion"));
277 278 279
    return -1;
}

280

281 282 283 284
static int
virDomainEventCallbackListMarkDeleteID(virConnectPtr conn,
                                       virDomainEventCallbackListPtr cbList,
                                       int callbackID)
285
{
286
    int ret = 0;
287
    size_t i;
288
    for (i = 0; i < cbList->count; i++) {
289 290 291
        if (cbList->callbacks[i]->callbackID == callbackID &&
            cbList->callbacks[i]->conn == conn) {
            cbList->callbacks[i]->deleted = 1;
292
            for (i = 0; i < cbList->count; i++) {
293 294 295 296
                if (!cbList->callbacks[i]->deleted)
                    ret++;
            }
            return ret;
297 298 299
        }
    }

300 301
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("could not find event callback for deletion"));
302 303 304 305
    return -1;
}


306 307
static int
virDomainEventCallbackListPurgeMarked(virDomainEventCallbackListPtr cbList)
308 309
{
    int old_count = cbList->count;
310 311 312 313
    int n;
    for (n = 0; n < cbList->count; n++) {
        if (cbList->callbacks[n]->deleted) {
            virFreeCallback freecb = cbList->callbacks[n]->freecb;
314
            if (freecb)
315 316 317
                (*freecb)(cbList->callbacks[n]->opaque);
            virObjectUnref(cbList->callbacks[n]->conn);
            VIR_FREE(cbList->callbacks[n]);
318

319 320 321
            if (n < (cbList->count - 1))
                memmove(cbList->callbacks + n,
                        cbList->callbacks + n + 1,
322
                        sizeof(*(cbList->callbacks)) *
323
                                (cbList->count - (n + 1)));
324
            cbList->count--;
325
            n--;
326 327 328 329 330 331 332 333 334
        }
    }
    if (cbList->count < old_count &&
        VIR_REALLOC_N(cbList->callbacks, cbList->count) < 0) {
        ; /* Failure to reduce memory allocation isn't fatal */
    }
    return 0;
}

335

336 337 338 339 340 341 342
/**
 * virDomainEventCallbackListAddID:
 * @conn: pointer to the connection
 * @cbList: the list
 * @eventID: the event ID
 * @callback: the callback to add
 * @opaque: opaque data tio pass to callback
343
 * @callbackID: filled with callback ID
344 345 346
 *
 * Internal function to add a callback from a virDomainEventCallbackListPtr
 */
347
static int
348 349 350 351 352 353
virDomainEventCallbackListAddID(virConnectPtr conn,
                                virDomainEventCallbackListPtr cbList,
                                virDomainPtr dom,
                                int eventID,
                                virConnectDomainEventGenericCallback callback,
                                void *opaque,
354 355
                                virFreeCallback freecb,
                                int *callbackID)
356 357
{
    virDomainEventCallbackPtr event;
358
    size_t i;
359
    int ret = 0;
360 361

    /* Check incoming */
362
    if (!cbList) {
363 364 365 366
        return -1;
    }

    /* check if we already have this callback on our list */
367
    for (i = 0; i < cbList->count; i++) {
368
        if (cbList->callbacks[i]->cb == VIR_DOMAIN_EVENT_CALLBACK(callback) &&
369
            cbList->callbacks[i]->eventID == eventID &&
370 371 372 373 374
            cbList->callbacks[i]->conn == conn &&
            ((dom && cbList->callbacks[i]->dom &&
              memcmp(cbList->callbacks[i]->dom->uuid,
                     dom->uuid, VIR_UUID_BUFLEN) == 0) ||
             (!dom && !cbList->callbacks[i]->dom))) {
375 376
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("event callback already tracked"));
377 378 379 380
            return -1;
        }
    }
    /* Allocate new event */
381
    if (VIR_ALLOC(event) < 0)
382
        goto error;
383
    event->conn = conn;
384 385
    event->cb = callback;
    event->eventID = eventID;
386
    event->opaque = opaque;
387
    event->freecb = freecb;
388

389 390
    if (dom) {
        if (VIR_ALLOC(event->dom) < 0)
391
            goto error;
392 393
        if (VIR_STRDUP(event->dom->name, dom->name) < 0)
            goto error;
394 395
        memcpy(event->dom->uuid, dom->uuid, VIR_UUID_BUFLEN);
        event->dom->id = dom->id;
396 397
    }

398 399
    /* Make space on list */
    if (VIR_REALLOC_N(cbList->callbacks, cbList->count + 1) < 0)
400
        goto error;
401

402
    virObjectRef(event->conn);
403

404
    cbList->callbacks[cbList->count] = event;
405
    cbList->count++;
406 407 408

    event->callbackID = cbList->nextID++;

409
    for (i = 0; i < cbList->count; i++) {
410
        if (cbList->callbacks[i]->eventID == eventID &&
411 412 413 414 415 416 417 418 419
            cbList->callbacks[i]->conn == conn &&
            !cbList->callbacks[i]->deleted)
            ret++;
    }

    if (callbackID)
        *callbackID = event->callbackID;

    return ret;
420

421
error:
422 423 424 425 426 427 428 429 430 431
    if (event) {
        if (event->dom)
            VIR_FREE(event->dom->name);
        VIR_FREE(event->dom);
    }
    VIR_FREE(event);
    return -1;
}


432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
/**
 * 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
 */
static int
virDomainEventCallbackListAdd(virConnectPtr conn,
                              virDomainEventCallbackListPtr cbList,
                              virConnectDomainEventCallback callback,
                              void *opaque,
                              virFreeCallback freecb)
447
{
448 449 450 451
    return virDomainEventCallbackListAddID(conn, cbList, NULL,
                                           VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                           VIR_DOMAIN_EVENT_CALLBACK(callback),
                                           opaque, freecb, NULL);
452 453 454
}


455 456 457 458 459

static int
virDomainEventCallbackListEventID(virConnectPtr conn,
                                  virDomainEventCallbackListPtr cbList,
                                  int callbackID)
460
{
461
    size_t i;
462

463
    for (i = 0; i < cbList->count; i++) {
464 465 466 467 468 469 470 471 472
        if (cbList->callbacks[i]->deleted)
            continue;

        if (cbList->callbacks[i]->callbackID == callbackID &&
            cbList->callbacks[i]->conn == conn)
            return cbList->callbacks[i]->eventID;
    }

    return -1;
473 474
}

475

476 477 478 479 480
void virDomainEventFree(virDomainEventPtr event)
{
    if (!event)
        return;

481
    switch (event->eventID) {
482
    case VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON:
483
    case VIR_DOMAIN_EVENT_ID_IO_ERROR:
484 485
        VIR_FREE(event->data.ioError.srcPath);
        VIR_FREE(event->data.ioError.devAlias);
486
        VIR_FREE(event->data.ioError.reason);
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
        break;

    case VIR_DOMAIN_EVENT_ID_GRAPHICS:
        if (event->data.graphics.local) {
            VIR_FREE(event->data.graphics.local->node);
            VIR_FREE(event->data.graphics.local->service);
            VIR_FREE(event->data.graphics.local);
        }
        if (event->data.graphics.remote) {
            VIR_FREE(event->data.graphics.remote->node);
            VIR_FREE(event->data.graphics.remote->service);
            VIR_FREE(event->data.graphics.remote);
        }
        VIR_FREE(event->data.graphics.authScheme);
        if (event->data.graphics.subject) {
502
            size_t i;
503
            for (i = 0; i < event->data.graphics.subject->nidentity; i++) {
504 505 506 507 508
                VIR_FREE(event->data.graphics.subject->identities[i].type);
                VIR_FREE(event->data.graphics.subject->identities[i].name);
            }
            VIR_FREE(event->data.graphics.subject);
        }
509 510 511 512 513
        break;

    case VIR_DOMAIN_EVENT_ID_BLOCK_JOB:
        VIR_FREE(event->data.blockJob.path);
        break;
514 515 516 517 518 519

    case VIR_DOMAIN_EVENT_ID_DISK_CHANGE:
        VIR_FREE(event->data.diskChange.oldSrcPath);
        VIR_FREE(event->data.diskChange.newSrcPath);
        VIR_FREE(event->data.diskChange.devAlias);
        break;
520 521 522
    case VIR_DOMAIN_EVENT_ID_TRAY_CHANGE:
        VIR_FREE(event->data.trayChange.devAlias);
        break;
523 524 525
    case VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED:
        VIR_FREE(event->data.deviceRemoved.devAlias);
        break;
526 527
    }

528
    VIR_FREE(event->dom.name);
529 530 531
    VIR_FREE(event);
}

532
/**
533
 * virDomainEventQueueClear:
534 535
 * @queue: pointer to the queue
 *
536
 * Removes all elements from the queue
537
 */
538
static void
539
virDomainEventQueueClear(virDomainEventQueuePtr queue)
540
{
541
    size_t i;
542 543 544
    if (!queue)
        return;

545
    for (i = 0; i < queue->count; i++) {
546 547 548
        virDomainEventFree(queue->events[i]);
    }
    VIR_FREE(queue->events);
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
    queue->count = 0;
}

/**
 * virDomainEventQueueFree:
 * @queue: pointer to the queue
 *
 * Free the memory in the queue. We process this like a list here
 */
static void
virDomainEventQueueFree(virDomainEventQueuePtr queue)
{
    if (!queue)
        return;

    virDomainEventQueueClear(queue);
565 566
    VIR_FREE(queue);
}
567

568 569
static virDomainEventQueuePtr
virDomainEventQueueNew(void)
570 571 572
{
    virDomainEventQueuePtr ret;

573
    ignore_value(VIR_ALLOC(ret));
574 575 576
    return ret;
}

577 578 579 580 581 582 583 584 585 586 587 588
static void
virDomainEventStateLock(virDomainEventStatePtr state)
{
    virMutexLock(&state->lock);
}

static void
virDomainEventStateUnlock(virDomainEventStatePtr state)
{
    virMutexUnlock(&state->lock);
}

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
/**
 * virDomainEventStateFree:
 * @list: virDomainEventStatePtr to free
 *
 * Free a virDomainEventStatePtr and its members, and unregister the timer.
 */
void
virDomainEventStateFree(virDomainEventStatePtr state)
{
    if (!state)
        return;

    virDomainEventCallbackListFree(state->callbacks);
    virDomainEventQueueFree(state->queue);

    if (state->timer != -1)
        virEventRemoveTimeout(state->timer);
606 607

    virMutexDestroy(&state->lock);
E
Eric Blake 已提交
608
    VIR_FREE(state);
609 610
}

611 612 613 614 615 616 617 618 619 620 621

static void virDomainEventStateFlush(virDomainEventStatePtr state);

static void
virDomainEventTimer(int timer ATTRIBUTE_UNUSED, void *opaque)
{
    virDomainEventStatePtr state = opaque;

    virDomainEventStateFlush(state);
}

622 623 624
/**
 * virDomainEventStateNew:
 */
625
virDomainEventStatePtr
626
virDomainEventStateNew(void)
627 628 629
{
    virDomainEventStatePtr state = NULL;

630
    if (VIR_ALLOC(state) < 0)
631 632
        goto error;

633 634 635 636 637 638 639
    if (virMutexInit(&state->lock) < 0) {
        virReportSystemError(errno, "%s",
                             _("unable to initialize state mutex"));
        VIR_FREE(state);
        goto error;
    }

640
    if (VIR_ALLOC(state->callbacks) < 0)
641 642
        goto error;

643
    if (!(state->queue = virDomainEventQueueNew()))
644 645
        goto error;

646
    state->timer = -1;
647 648 649 650 651 652 653 654

    return state;

error:
    virDomainEventStateFree(state);
    return NULL;
}

655 656 657 658
static virDomainEventPtr virDomainEventNewInternal(int eventID,
                                                   int id,
                                                   const char *name,
                                                   const unsigned char *uuid)
659 660 661
{
    virDomainEventPtr event;

662
    if (VIR_ALLOC(event) < 0)
663 664
        return NULL;

665
    event->eventID = eventID;
666
    if (VIR_STRDUP(event->dom.name, name) < 0) {
667 668 669
        VIR_FREE(event);
        return NULL;
    }
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
    event->dom.id = id;
    memcpy(event->dom.uuid, uuid, VIR_UUID_BUFLEN);

    return event;
}

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

    if (event) {
        event->data.lifecycle.type = type;
        event->data.lifecycle.detail = detail;
    }
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705

    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);
}

M
Matthias Bolte 已提交
706 707 708 709 710 711 712
virDomainEventPtr virDomainEventRebootNew(int id, const char *name,
                                          const unsigned char *uuid)
{
    return virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_REBOOT,
                                     id, name, uuid);
}

713 714 715 716 717
virDomainEventPtr virDomainEventRebootNewFromDom(virDomainPtr dom)
{
    return virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_REBOOT,
                                     dom->id, dom->name, dom->uuid);
}
M
Matthias Bolte 已提交
718

719 720 721 722 723 724
virDomainEventPtr virDomainEventRebootNewFromObj(virDomainObjPtr obj)
{
    return virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_REBOOT,
                                     obj->def->id, obj->def->name, obj->def->uuid);
}

725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
virDomainEventPtr virDomainEventRTCChangeNewFromDom(virDomainPtr dom,
                                                    long long offset)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_RTC_CHANGE,
                                  dom->id, dom->name, dom->uuid);

    if (ev)
        ev->data.rtcChange.offset = offset;

    return ev;
}
virDomainEventPtr virDomainEventRTCChangeNewFromObj(virDomainObjPtr obj,
                                                    long long offset)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_RTC_CHANGE,
                                  obj->def->id, obj->def->name, obj->def->uuid);

    if (ev)
        ev->data.rtcChange.offset = offset;

    return ev;
}

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
virDomainEventPtr virDomainEventWatchdogNewFromDom(virDomainPtr dom,
                                                   int action)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_WATCHDOG,
                                  dom->id, dom->name, dom->uuid);

    if (ev)
        ev->data.watchdog.action = action;

    return ev;
}
virDomainEventPtr virDomainEventWatchdogNewFromObj(virDomainObjPtr obj,
                                                   int action)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_WATCHDOG,
                                  obj->def->id, obj->def->name, obj->def->uuid);

    if (ev)
        ev->data.watchdog.action = action;

    return ev;
}

775 776 777 778 779 780
static virDomainEventPtr virDomainEventIOErrorNewFromDomImpl(int event,
                                                             virDomainPtr dom,
                                                             const char *srcPath,
                                                             const char *devAlias,
                                                             int action,
                                                             const char *reason)
781 782
{
    virDomainEventPtr ev =
783
        virDomainEventNewInternal(event,
784 785 786 787
                                  dom->id, dom->name, dom->uuid);

    if (ev) {
        ev->data.ioError.action = action;
788 789 790
        if (VIR_STRDUP(ev->data.ioError.srcPath, srcPath) < 0 ||
            VIR_STRDUP(ev->data.ioError.devAlias, devAlias) < 0 ||
            VIR_STRDUP(ev->data.ioError.reason, reason) < 0) {
791 792 793 794 795 796 797
            virDomainEventFree(ev);
            ev = NULL;
        }
    }

    return ev;
}
798 799 800 801 802 803 804

static virDomainEventPtr virDomainEventIOErrorNewFromObjImpl(int event,
                                                             virDomainObjPtr obj,
                                                             const char *srcPath,
                                                             const char *devAlias,
                                                             int action,
                                                             const char *reason)
805 806
{
    virDomainEventPtr ev =
807
        virDomainEventNewInternal(event,
808 809 810
                                  obj->def->id, obj->def->name, obj->def->uuid);

    if (ev) {
D
Daniel P. Berrange 已提交
811
        ev->data.ioError.action = action;
812 813 814
        if (VIR_STRDUP(ev->data.ioError.srcPath, srcPath) < 0 ||
            VIR_STRDUP(ev->data.ioError.devAlias, devAlias) < 0 ||
            VIR_STRDUP(ev->data.ioError.reason, reason) < 0) {
815 816 817 818 819 820 821 822
            virDomainEventFree(ev);
            ev = NULL;
        }
    }

    return ev;
}

823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
virDomainEventPtr virDomainEventIOErrorNewFromDom(virDomainPtr dom,
                                                  const char *srcPath,
                                                  const char *devAlias,
                                                  int action)
{
    return virDomainEventIOErrorNewFromDomImpl(VIR_DOMAIN_EVENT_ID_IO_ERROR,
                                               dom, srcPath, devAlias,
                                               action, NULL);
}

virDomainEventPtr virDomainEventIOErrorNewFromObj(virDomainObjPtr obj,
                                                  const char *srcPath,
                                                  const char *devAlias,
                                                  int action)
{
    return virDomainEventIOErrorNewFromObjImpl(VIR_DOMAIN_EVENT_ID_IO_ERROR,
                                               obj, srcPath, devAlias,
                                               action, NULL);
}

virDomainEventPtr virDomainEventIOErrorReasonNewFromDom(virDomainPtr dom,
                                                        const char *srcPath,
                                                        const char *devAlias,
                                                        int action,
                                                        const char *reason)
{
    return virDomainEventIOErrorNewFromDomImpl(VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON,
                                               dom, srcPath, devAlias,
                                               action, reason);
}

virDomainEventPtr virDomainEventIOErrorReasonNewFromObj(virDomainObjPtr obj,
                                                        const char *srcPath,
                                                        const char *devAlias,
                                                        int action,
                                                        const char *reason)
{
    return virDomainEventIOErrorNewFromObjImpl(VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON,
                                               obj, srcPath, devAlias,
                                               action, reason);
}

865 866 867 868 869 870 871 872 873 874 875 876 877 878

virDomainEventPtr virDomainEventGraphicsNewFromDom(virDomainPtr dom,
                                                   int phase,
                                                   virDomainEventGraphicsAddressPtr local,
                                                   virDomainEventGraphicsAddressPtr remote,
                                                   const char *authScheme,
                                                   virDomainEventGraphicsSubjectPtr subject)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_GRAPHICS,
                                  dom->id, dom->name, dom->uuid);

    if (ev) {
        ev->data.graphics.phase = phase;
879
        if (VIR_STRDUP(ev->data.graphics.authScheme, authScheme) < 0) {
880
            virDomainEventFree(ev);
881
            return NULL;
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
        }
        ev->data.graphics.local = local;
        ev->data.graphics.remote = remote;
        ev->data.graphics.subject = subject;
    }

    return ev;
}

virDomainEventPtr virDomainEventGraphicsNewFromObj(virDomainObjPtr obj,
                                                   int phase,
                                                   virDomainEventGraphicsAddressPtr local,
                                                   virDomainEventGraphicsAddressPtr remote,
                                                   const char *authScheme,
                                                   virDomainEventGraphicsSubjectPtr subject)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_GRAPHICS,
                                  obj->def->id, obj->def->name, obj->def->uuid);

    if (ev) {
        ev->data.graphics.phase = phase;
904
        if (VIR_STRDUP(ev->data.graphics.authScheme, authScheme) < 0) {
905
            virDomainEventFree(ev);
906
            return NULL;
907 908 909 910 911 912 913 914 915
        }
        ev->data.graphics.local = local;
        ev->data.graphics.remote = remote;
        ev->data.graphics.subject = subject;
    }

    return ev;
}

916 917 918 919 920 921 922 923 924
static virDomainEventPtr
virDomainEventBlockJobNew(int id, const char *name, unsigned char *uuid,
                          const char *path, int type, int status)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_BLOCK_JOB,
                                  id, name, uuid);

    if (ev) {
925
        if (VIR_STRDUP(ev->data.blockJob.path, path) < 0) {
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
            virDomainEventFree(ev);
            return NULL;
        }
        ev->data.blockJob.type = type;
        ev->data.blockJob.status = status;
    }

    return ev;
}

virDomainEventPtr virDomainEventBlockJobNewFromObj(virDomainObjPtr obj,
                                                   const char *path,
                                                   int type,
                                                   int status)
{
    return virDomainEventBlockJobNew(obj->def->id, obj->def->name,
                                     obj->def->uuid, path, type, status);
}

virDomainEventPtr virDomainEventBlockJobNewFromDom(virDomainPtr dom,
                                                   const char *path,
                                                   int type,
                                                   int status)
{
    return virDomainEventBlockJobNew(dom->id, dom->name, dom->uuid,
                                     path, type, status);
}
953

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
virDomainEventPtr virDomainEventControlErrorNewFromDom(virDomainPtr dom)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_CONTROL_ERROR,
                                  dom->id, dom->name, dom->uuid);
    return ev;
}


virDomainEventPtr virDomainEventControlErrorNewFromObj(virDomainObjPtr obj)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_CONTROL_ERROR,
                                  obj->def->id, obj->def->name, obj->def->uuid);
    return ev;
}

971 972 973 974 975 976 977 978 979 980 981 982
static virDomainEventPtr
virDomainEventDiskChangeNew(int id, const char *name,
                            unsigned char *uuid,
                            const char *oldSrcPath,
                            const char *newSrcPath,
                            const char *devAlias, int reason)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_DISK_CHANGE,
                                  id, name, uuid);

    if (ev) {
983
        if (VIR_STRDUP(ev->data.diskChange.devAlias, devAlias) < 0)
984 985
            goto error;

986
        if (VIR_STRDUP(ev->data.diskChange.oldSrcPath, oldSrcPath) < 0)
987 988
            goto error;

989
        if (VIR_STRDUP(ev->data.diskChange.newSrcPath, newSrcPath) < 0)
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
            goto error;

        ev->data.diskChange.reason = reason;
    }

    return ev;

error:
    virDomainEventFree(ev);
    return NULL;
}

virDomainEventPtr virDomainEventDiskChangeNewFromObj(virDomainObjPtr obj,
                                                     const char *oldSrcPath,
                                                     const char *newSrcPath,
                                                     const char *devAlias,
                                                     int reason)
{
    return virDomainEventDiskChangeNew(obj->def->id, obj->def->name,
                                       obj->def->uuid, oldSrcPath,
                                       newSrcPath, devAlias, reason);
}

virDomainEventPtr virDomainEventDiskChangeNewFromDom(virDomainPtr dom,
                                                     const char *oldSrcPath,
                                                     const char *newSrcPath,
                                                     const char *devAlias,
                                                     int reason)
{
    return virDomainEventDiskChangeNew(dom->id, dom->name, dom->uuid,
                                       oldSrcPath, newSrcPath,
                                       devAlias, reason);
}
1023

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
static virDomainEventPtr
virDomainEventTrayChangeNew(int id, const char *name,
                            unsigned char *uuid,
                            const char *devAlias,
                            int reason)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_TRAY_CHANGE,
                                  id, name, uuid);

    if (ev) {
1035
        if (VIR_STRDUP(ev->data.trayChange.devAlias, devAlias) < 0)
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
            goto error;

        ev->data.trayChange.reason = reason;
    }

    return ev;

error:
    virDomainEventFree(ev);
    return NULL;
}

virDomainEventPtr virDomainEventTrayChangeNewFromObj(virDomainObjPtr obj,
                                                     const char *devAlias,
                                                     int reason)
{
    return virDomainEventTrayChangeNew(obj->def->id,
                                       obj->def->name,
                                       obj->def->uuid,
                                       devAlias,
                                       reason);
}

virDomainEventPtr virDomainEventTrayChangeNewFromDom(virDomainPtr dom,
                                                     const char *devAlias,
                                                     int reason)
{
    return virDomainEventTrayChangeNew(dom->id, dom->name, dom->uuid,
                                       devAlias, reason);
}

O
Osier Yang 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
static virDomainEventPtr
virDomainEventPMWakeupNew(int id, const char *name,
                          unsigned char *uuid)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_PMWAKEUP,
                                  id, name, uuid);

    return ev;
}

virDomainEventPtr
virDomainEventPMWakeupNewFromObj(virDomainObjPtr obj)
{
    return virDomainEventPMWakeupNew(obj->def->id,
                                     obj->def->name,
                                     obj->def->uuid);
}

virDomainEventPtr
virDomainEventPMWakeupNewFromDom(virDomainPtr dom)
{
    return virDomainEventPMWakeupNew(dom->id, dom->name, dom->uuid);
}
1091

O
Osier Yang 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
static virDomainEventPtr
virDomainEventPMSuspendNew(int id, const char *name,
                           unsigned char *uuid)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_PMSUSPEND,
                                  id, name, uuid);

    return ev;
}

virDomainEventPtr
virDomainEventPMSuspendNewFromObj(virDomainObjPtr obj)
{
    return virDomainEventPMSuspendNew(obj->def->id,
                                      obj->def->name,
                                      obj->def->uuid);
}

virDomainEventPtr
virDomainEventPMSuspendNewFromDom(virDomainPtr dom)
{
    return virDomainEventPMSuspendNew(dom->id, dom->name, dom->uuid);
}

1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
static virDomainEventPtr
virDomainEventPMSuspendDiskNew(int id, const char *name,
                               unsigned char *uuid)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_PMSUSPEND_DISK,
                                  id, name, uuid);
    return ev;
}

virDomainEventPtr
virDomainEventPMSuspendDiskNewFromObj(virDomainObjPtr obj)
{
    return virDomainEventPMSuspendDiskNew(obj->def->id,
                                          obj->def->name,
                                          obj->def->uuid);
}

virDomainEventPtr
virDomainEventPMSuspendDiskNewFromDom(virDomainPtr dom)
{
    return virDomainEventPMSuspendDiskNew(dom->id, dom->name, dom->uuid);
}

1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
virDomainEventPtr virDomainEventBalloonChangeNewFromDom(virDomainPtr dom,
                                                        unsigned long long actual)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_BALLOON_CHANGE,
                                  dom->id, dom->name, dom->uuid);

    if (ev)
        ev->data.balloonChange.actual = actual;

    return ev;
}
virDomainEventPtr virDomainEventBalloonChangeNewFromObj(virDomainObjPtr obj,
                                                        unsigned long long actual)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_BALLOON_CHANGE,
                                  obj->def->id, obj->def->name, obj->def->uuid);

    if (ev)
        ev->data.balloonChange.actual = actual;

    return ev;
}

1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
static virDomainEventPtr
virDomainEventDeviceRemovedNew(int id,
                               const char *name,
                               unsigned char *uuid,
                               const char *devAlias)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED,
                                  id, name, uuid);

    if (ev) {
        if (VIR_STRDUP(ev->data.deviceRemoved.devAlias, devAlias) < 0)
            goto error;
    }

    return ev;

error:
    virDomainEventFree(ev);
    return NULL;
}

virDomainEventPtr
virDomainEventDeviceRemovedNewFromObj(virDomainObjPtr obj,
                                      const char *devAlias)
{
    return virDomainEventDeviceRemovedNew(obj->def->id, obj->def->name,
                                          obj->def->uuid, devAlias);
}

virDomainEventPtr
virDomainEventDeviceRemovedNewFromDom(virDomainPtr dom,
                                      const char *devAlias)
{
    return virDomainEventDeviceRemovedNew(dom->id, dom->name, dom->uuid,
                                          devAlias);
}

1204
/**
1205
 * virDomainEventQueuePush:
1206 1207 1208
 * @evtQueue: the dom event queue
 * @event: the event to add
 *
E
Eric Blake 已提交
1209
 * Internal function to push to the back of a virDomainEventQueue
1210 1211 1212
 *
 * Returns: 0 on success, -1 on failure
 */
1213
static int
1214 1215
virDomainEventQueuePush(virDomainEventQueuePtr evtQueue,
                        virDomainEventPtr event)
1216
{
1217
    if (!evtQueue) {
1218 1219 1220 1221 1222
        return -1;
    }

    /* Make space on queue */
    if (VIR_REALLOC_N(evtQueue->events,
1223
                      evtQueue->count + 1) < 0)
1224 1225
        return -1;

1226
    evtQueue->events[evtQueue->count] = event;
1227 1228 1229 1230
    evtQueue->count++;
    return 0;
}

1231

1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
typedef void (*virDomainEventDispatchFunc)(virConnectPtr conn,
                                           virDomainEventPtr event,
                                           virConnectDomainEventGenericCallback cb,
                                           void *cbopaque,
                                           void *opaque);


static void
virDomainEventDispatchDefaultFunc(virConnectPtr conn,
                                  virDomainEventPtr event,
                                  virConnectDomainEventGenericCallback cb,
                                  void *cbopaque,
                                  void *opaque ATTRIBUTE_UNUSED)
1245
{
1246 1247 1248 1249 1250
    virDomainPtr dom = virGetDomain(conn, event->dom.name, event->dom.uuid);
    if (!dom)
        return;
    dom->id = event->dom.id;

1251
    switch ((virDomainEventID) event->eventID) {
1252 1253 1254 1255 1256
    case VIR_DOMAIN_EVENT_ID_LIFECYCLE:
        ((virConnectDomainEventCallback)cb)(conn, dom,
                                            event->data.lifecycle.type,
                                            event->data.lifecycle.detail,
                                            cbopaque);
1257
        goto cleanup;
1258

1259 1260 1261
    case VIR_DOMAIN_EVENT_ID_REBOOT:
        (cb)(conn, dom,
             cbopaque);
1262
        goto cleanup;
1263

1264 1265 1266 1267
    case VIR_DOMAIN_EVENT_ID_RTC_CHANGE:
        ((virConnectDomainEventRTCChangeCallback)cb)(conn, dom,
                                                     event->data.rtcChange.offset,
                                                     cbopaque);
1268
        goto cleanup;
1269

1270 1271 1272 1273
    case VIR_DOMAIN_EVENT_ID_WATCHDOG:
        ((virConnectDomainEventWatchdogCallback)cb)(conn, dom,
                                                    event->data.watchdog.action,
                                                    cbopaque);
1274
        goto cleanup;
1275

1276 1277 1278 1279 1280 1281
    case VIR_DOMAIN_EVENT_ID_IO_ERROR:
        ((virConnectDomainEventIOErrorCallback)cb)(conn, dom,
                                                   event->data.ioError.srcPath,
                                                   event->data.ioError.devAlias,
                                                   event->data.ioError.action,
                                                   cbopaque);
1282
        goto cleanup;
1283

1284 1285 1286 1287 1288 1289 1290
    case VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON:
        ((virConnectDomainEventIOErrorReasonCallback)cb)(conn, dom,
                                                         event->data.ioError.srcPath,
                                                         event->data.ioError.devAlias,
                                                         event->data.ioError.action,
                                                         event->data.ioError.reason,
                                                         cbopaque);
1291
        goto cleanup;
1292

1293 1294 1295 1296 1297 1298 1299 1300
    case VIR_DOMAIN_EVENT_ID_GRAPHICS:
        ((virConnectDomainEventGraphicsCallback)cb)(conn, dom,
                                                    event->data.graphics.phase,
                                                    event->data.graphics.local,
                                                    event->data.graphics.remote,
                                                    event->data.graphics.authScheme,
                                                    event->data.graphics.subject,
                                                    cbopaque);
1301
        goto cleanup;
1302

1303 1304 1305
    case VIR_DOMAIN_EVENT_ID_CONTROL_ERROR:
        (cb)(conn, dom,
             cbopaque);
1306
        goto cleanup;
1307

1308 1309 1310 1311 1312 1313
    case VIR_DOMAIN_EVENT_ID_BLOCK_JOB:
        ((virConnectDomainEventBlockJobCallback)cb)(conn, dom,
                                                    event->data.blockJob.path,
                                                    event->data.blockJob.type,
                                                    event->data.blockJob.status,
                                                    cbopaque);
1314
        goto cleanup;
1315

1316 1317 1318 1319 1320 1321 1322
    case VIR_DOMAIN_EVENT_ID_DISK_CHANGE:
        ((virConnectDomainEventDiskChangeCallback)cb)(conn, dom,
                                                      event->data.diskChange.oldSrcPath,
                                                      event->data.diskChange.newSrcPath,
                                                      event->data.diskChange.devAlias,
                                                      event->data.diskChange.reason,
                                                      cbopaque);
1323
        goto cleanup;
1324

1325 1326 1327 1328 1329
    case VIR_DOMAIN_EVENT_ID_TRAY_CHANGE:
        ((virConnectDomainEventTrayChangeCallback)cb)(conn, dom,
                                                      event->data.trayChange.devAlias,
                                                      event->data.trayChange.reason,
                                                      cbopaque);
1330
        goto cleanup;
1331

O
Osier Yang 已提交
1332 1333
    case VIR_DOMAIN_EVENT_ID_PMWAKEUP:
        ((virConnectDomainEventPMWakeupCallback)cb)(conn, dom, 0, cbopaque);
1334
        goto cleanup;
O
Osier Yang 已提交
1335

O
Osier Yang 已提交
1336 1337
    case VIR_DOMAIN_EVENT_ID_PMSUSPEND:
        ((virConnectDomainEventPMSuspendCallback)cb)(conn, dom, 0, cbopaque);
1338
        goto cleanup;
O
Osier Yang 已提交
1339

1340 1341 1342 1343
    case VIR_DOMAIN_EVENT_ID_BALLOON_CHANGE:
        ((virConnectDomainEventBalloonChangeCallback)cb)(conn, dom,
                                                         event->data.balloonChange.actual,
                                                         cbopaque);
1344
        goto cleanup;
1345

1346 1347
    case VIR_DOMAIN_EVENT_ID_PMSUSPEND_DISK:
        ((virConnectDomainEventPMSuspendDiskCallback)cb)(conn, dom, 0, cbopaque);
1348
        goto cleanup;
1349

1350 1351 1352 1353 1354 1355 1356
    case VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED:
        ((virConnectDomainEventDeviceRemovedCallback)cb)(conn, dom,
                                                         event->data.deviceRemoved.devAlias,
                                                         cbopaque);
        goto cleanup;

    case VIR_DOMAIN_EVENT_ID_LAST:
1357
        break;
1358
    }
1359

1360 1361 1362
    VIR_WARN("Unexpected event ID %d", event->eventID);

cleanup:
1363
    virDomainFree(dom);
1364 1365 1366
}


1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
static int virDomainEventDispatchMatchCallback(virDomainEventPtr event,
                                               virDomainEventCallbackPtr cb)
{
    if (!cb)
        return 0;
    if (cb->deleted)
        return 0;
    if (cb->eventID != event->eventID)
        return 0;

    if (cb->dom) {
E
Eric Blake 已提交
1378
        /* Deliberately ignoring 'id' for matching, since that
1379 1380 1381
         * will cause problems when a domain switches between
         * running & shutoff states & ignoring 'name' since
         * Xen sometimes renames guests during migration, thus
E
Eric Blake 已提交
1382
         * leaving 'uuid' as the only truly reliable ID we can use*/
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392

        if (memcmp(event->dom.uuid, cb->dom->uuid, VIR_UUID_BUFLEN) == 0)
            return 1;

        return 0;
    } else {
        return 1;
    }
}

1393 1394 1395 1396 1397 1398

static void
virDomainEventDispatch(virDomainEventPtr event,
                       virDomainEventCallbackListPtr callbacks,
                       virDomainEventDispatchFunc dispatch,
                       void *opaque)
1399
{
1400
    size_t i;
1401
    /* Cache this now, since we may be dropping the lock,
E
Eric Blake 已提交
1402
       and have more callbacks added. We're guaranteed not
1403 1404 1405
       to have any removed */
    int cbCount = callbacks->count;

1406
    for (i = 0; i < cbCount; i++) {
1407 1408 1409 1410 1411 1412 1413 1414
        if (!virDomainEventDispatchMatchCallback(event, callbacks->callbacks[i]))
            continue;

        (*dispatch)(callbacks->callbacks[i]->conn,
                    event,
                    callbacks->callbacks[i]->cb,
                    callbacks->callbacks[i]->opaque,
                    opaque);
1415 1416 1417 1418
    }
}


1419 1420 1421 1422 1423
static void
virDomainEventQueueDispatch(virDomainEventQueuePtr queue,
                            virDomainEventCallbackListPtr callbacks,
                            virDomainEventDispatchFunc dispatch,
                            void *opaque)
1424
{
1425
    size_t i;
1426

1427
    for (i = 0; i < queue->count; i++) {
1428 1429 1430 1431 1432 1433
        virDomainEventDispatch(queue->events[i], callbacks, dispatch, opaque);
        virDomainEventFree(queue->events[i]);
    }
    VIR_FREE(queue->events);
    queue->count = 0;
}
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443

void
virDomainEventStateQueue(virDomainEventStatePtr state,
                         virDomainEventPtr event)
{
    if (state->timer < 0) {
        virDomainEventFree(event);
        return;
    }

1444 1445
    virDomainEventStateLock(state);

1446 1447 1448 1449 1450 1451 1452
    if (virDomainEventQueuePush(state->queue, event) < 0) {
        VIR_DEBUG("Error adding event to queue");
        virDomainEventFree(event);
    }

    if (state->queue->count == 1)
        virEventUpdateTimeout(state->timer, 0);
1453
    virDomainEventStateUnlock(state);
1454 1455
}

1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474

static void
virDomainEventStateDispatchFunc(virConnectPtr conn,
                                virDomainEventPtr event,
                                virConnectDomainEventGenericCallback cb,
                                void *cbopaque,
                                void *opaque)
{
    virDomainEventStatePtr state = opaque;

    /* Drop the lock whle dispatching, for sake of re-entrancy */
    virDomainEventStateUnlock(state);
    virDomainEventDispatchDefaultFunc(conn, event, cb, cbopaque, NULL);
    virDomainEventStateLock(state);
}


static void
virDomainEventStateFlush(virDomainEventStatePtr state)
1475 1476 1477
{
    virDomainEventQueue tempQueue;

1478
    virDomainEventStateLock(state);
1479 1480 1481 1482 1483 1484 1485 1486 1487
    state->isDispatching = true;

    /* Copy the queue, so we're reentrant safe when dispatchFunc drops the
     * driver lock */
    tempQueue.count = state->queue->count;
    tempQueue.events = state->queue->events;
    state->queue->count = 0;
    state->queue->events = NULL;
    virEventUpdateTimeout(state->timer, -1);
1488

1489 1490
    virDomainEventQueueDispatch(&tempQueue,
                                state->callbacks,
1491 1492
                                virDomainEventStateDispatchFunc,
                                state);
1493 1494 1495 1496 1497

    /* Purge any deleted callbacks */
    virDomainEventCallbackListPurgeMarked(state->callbacks);

    state->isDispatching = false;
1498
    virDomainEventStateUnlock(state);
1499 1500
}

1501 1502

/**
1503 1504
 * virDomainEventStateRegister:
 * @conn: connection to associate with callback
1505
 * @state: domain event state
1506 1507 1508 1509 1510 1511 1512 1513 1514
 * @callback: function to remove from event
 * @opaque: data blob to pass to callback
 * @freecb: callback to free @opaque
 *
 * Register the function @callback with connection @conn,
 * from @state, for lifecycle events.
 *
 * Returns: the number of lifecycle callbacks now registered, or -1 on error
 */
1515 1516 1517 1518 1519 1520
int
virDomainEventStateRegister(virConnectPtr conn,
                            virDomainEventStatePtr state,
                            virConnectDomainEventCallback callback,
                            void *opaque,
                            virFreeCallback freecb)
1521
{
1522 1523
    int ret = -1;

1524
    virDomainEventStateLock(state);
1525 1526 1527 1528 1529 1530 1531

    if ((state->callbacks->count == 0) &&
        (state->timer == -1) &&
        (state->timer = virEventAddTimeout(-1,
                                           virDomainEventTimer,
                                           state,
                                           NULL)) < 0) {
1532 1533
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("could not initialize domain event timer"));
1534 1535 1536
        goto cleanup;
    }

1537 1538
    ret = virDomainEventCallbackListAdd(conn, state->callbacks,
                                        callback, opaque, freecb);
1539 1540 1541 1542 1543 1544 1545 1546 1547

    if (ret == -1 &&
        state->callbacks->count == 0 &&
        state->timer != -1) {
        virEventRemoveTimeout(state->timer);
        state->timer = -1;
    }

cleanup:
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
    virDomainEventStateUnlock(state);
    return ret;
}


/**
 * virDomainEventStateRegisterID:
 * @conn: connection to associate with callback
 * @state: domain event state
 * @eventID: ID of the event type to register for
 * @cb: function to remove from event
 * @opaque: data blob to pass to callback
 * @freecb: callback to free @opaque
 * @callbackID: filled with callback ID
 *
 * Register the function @callbackID with connection @conn,
 * from @state, for events of type @eventID.
 *
 * Returns: the number of callbacks now registered, or -1 on error
 */
1568 1569 1570 1571 1572 1573 1574 1575 1576
int
virDomainEventStateRegisterID(virConnectPtr conn,
                              virDomainEventStatePtr state,
                              virDomainPtr dom,
                              int eventID,
                              virConnectDomainEventGenericCallback cb,
                              void *opaque,
                              virFreeCallback freecb,
                              int *callbackID)
1577
{
1578 1579
    int ret = -1;

1580
    virDomainEventStateLock(state);
1581 1582 1583 1584 1585 1586 1587

    if ((state->callbacks->count == 0) &&
        (state->timer == -1) &&
        (state->timer = virEventAddTimeout(-1,
                                           virDomainEventTimer,
                                           state,
                                           NULL)) < 0) {
1588 1589
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("could not initialize domain event timer"));
1590 1591 1592
        goto cleanup;
    }

1593 1594 1595
    ret = virDomainEventCallbackListAddID(conn, state->callbacks,
                                          dom, eventID, cb, opaque, freecb,
                                          callbackID);
1596 1597 1598 1599 1600 1601 1602 1603 1604

    if (ret == -1 &&
        state->callbacks->count == 0 &&
        state->timer != -1) {
        virEventRemoveTimeout(state->timer);
        state->timer = -1;
    }

cleanup:
1605 1606 1607 1608 1609 1610 1611
    virDomainEventStateUnlock(state);
    return ret;
}


/**
 * virDomainEventStateDeregister:
1612
 * @conn: connection to associate with callback
1613
 * @state: domain event state
1614 1615 1616 1617 1618 1619 1620
 * @callback: function to remove from event
 *
 * Unregister the function @callback with connection @conn,
 * from @state, for lifecycle events.
 *
 * Returns: the number of lifecycle callbacks still registered, or -1 on error
 */
1621 1622 1623 1624 1625
int
virDomainEventStateDeregister(virConnectPtr conn,
                              virDomainEventStatePtr state,
                              virConnectDomainEventCallback callback)
{
1626 1627 1628
    int ret;

    virDomainEventStateLock(state);
1629
    if (state->isDispatching)
1630 1631
        ret = virDomainEventCallbackListMarkDelete(conn,
                                                   state->callbacks, callback);
1632
    else
1633
        ret = virDomainEventCallbackListRemove(conn, state->callbacks, callback);
1634 1635 1636 1637 1638

    if (state->callbacks->count == 0 &&
        state->timer != -1) {
        virEventRemoveTimeout(state->timer);
        state->timer = -1;
1639
        virDomainEventQueueClear(state->queue);
1640 1641
    }

1642 1643
    virDomainEventStateUnlock(state);
    return ret;
1644 1645
}

1646 1647

/**
1648
 * virDomainEventStateDeregisterID:
1649
 * @conn: connection to associate with callback
1650
 * @state: domain event state
1651 1652 1653
 * @callbackID: ID of the function to remove from event
 *
 * Unregister the function @callbackID with connection @conn,
1654
 * from @state, for events.
1655
 *
1656
 * Returns: the number of callbacks still registered, or -1 on error
1657
 */
1658
int
1659 1660 1661
virDomainEventStateDeregisterID(virConnectPtr conn,
                                virDomainEventStatePtr state,
                                int callbackID)
1662
{
1663 1664 1665
    int ret;

    virDomainEventStateLock(state);
1666
    if (state->isDispatching)
1667 1668
        ret = virDomainEventCallbackListMarkDeleteID(conn,
                                                     state->callbacks, callbackID);
1669
    else
1670 1671
        ret = virDomainEventCallbackListRemoveID(conn,
                                                 state->callbacks, callbackID);
1672 1673 1674 1675 1676

    if (state->callbacks->count == 0 &&
        state->timer != -1) {
        virEventRemoveTimeout(state->timer);
        state->timer = -1;
1677
        virDomainEventQueueClear(state->queue);
1678 1679
    }

1680 1681
    virDomainEventStateUnlock(state);
    return ret;
1682
}
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708


/**
 * virDomainEventStateEventID:
 * @conn: connection associated with the callback
 * @state: domain event state
 * @callbackID: the callback to query
 *
 * Query what event ID type is associated with the
 * callback @callbackID for connection @conn
 *
 * Returns 0 on success, -1 on error
 */
int
virDomainEventStateEventID(virConnectPtr conn,
                           virDomainEventStatePtr state,
                           int callbackID)
{
    int ret;

    virDomainEventStateLock(state);
    ret = virDomainEventCallbackListEventID(conn,
                                            state->callbacks, callbackID);
    virDomainEventStateUnlock(state);
    return ret;
}