domain_event.c 65.4 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
#define VIR_OBJECT_EVENT_CALLBACK(cb) ((virConnectObjectEventGenericCallback)(cb))

37
struct _virObjectMeta {
38 39 40 41
    int id;
    char *name;
    unsigned char uuid[VIR_UUID_BUFLEN];
};
42 43
typedef struct _virObjectMeta virObjectMeta;
typedef virObjectMeta *virObjectMetaPtr;
44

45 46 47
typedef struct _virObjectEventQueue virObjectEventQueue;
typedef virObjectEventQueue *virObjectEventQueuePtr;

48
struct _virObjectEventCallbackList {
49 50
    unsigned int nextID;
    unsigned int count;
51
    virObjectEventCallbackPtr *callbacks;
52
};
53 54
typedef struct _virObjectEventCallbackList virObjectEventCallbackList;
typedef virObjectEventCallbackList *virObjectEventCallbackListPtr;
55

56
struct _virObjectEventQueue {
57 58 59 60
    unsigned int count;
    virDomainEventPtr *events;
};

61
struct _virObjectEventState {
62
    /* The list of domain event callbacks */
63
    virObjectEventCallbackListPtr callbacks;
64 65
    /* The queue of object events */
    virObjectEventQueuePtr queue;
66 67 68 69 70 71 72
    /* Timer for flushing events queue */
    int timer;
    /* Flag if we're in process of dispatching */
    bool isDispatching;
    virMutex lock;
};

73
struct _virObjectEventCallback {
74 75
    int callbackID;
    int eventID;
76
    virConnectPtr conn;
77
    virObjectMetaPtr meta;
78
    virConnectObjectEventGenericCallback cb;
79 80 81 82 83
    void *opaque;
    virFreeCallback freecb;
    int deleted;
};

C
Cédric Bosdonnat 已提交
84 85 86 87


static virClassPtr virObjectEventClass;
static virClassPtr virDomainEventClass;
88
static virClassPtr virDomainEventLifecycleClass;
89
static virClassPtr virDomainEventRTCChangeClass;
90
static virClassPtr virDomainEventWatchdogClass;
91
static virClassPtr virDomainEventIOErrorClass;
92
static virClassPtr virDomainEventGraphicsClass;
93
static virClassPtr virDomainEventBlockJobClass;
94
static virClassPtr virDomainEventDiskChangeClass;
95
static virClassPtr virDomainEventTrayChangeClass;
96

C
Cédric Bosdonnat 已提交
97 98
static void virObjectEventDispose(void *obj);
static void virDomainEventDispose(void *obj);
99
static void virDomainEventLifecycleDispose(void *obj);
100
static void virDomainEventRTCChangeDispose(void *obj);
101
static void virDomainEventWatchdogDispose(void *obj);
102
static void virDomainEventIOErrorDispose(void *obj);
103
static void virDomainEventGraphicsDispose(void *obj);
104
static void virDomainEventBlockJobDispose(void *obj);
105
static void virDomainEventDiskChangeDispose(void *obj);
106
static void virDomainEventTrayChangeDispose(void *obj);
C
Cédric Bosdonnat 已提交
107 108 109

struct _virObjectEvent {
    virObject parent;
110
    int eventID;
C
Cédric Bosdonnat 已提交
111 112 113 114
};

struct _virDomainEvent {
    virObjectEvent parent;
115

116
    virObjectMeta meta;
117 118

    union {
119 120 121 122
        struct {
            /* In unit of 1024 bytes */
            unsigned long long actual;
        } balloonChange;
123 124 125
        struct {
            char *devAlias;
        } deviceRemoved;
126
    } data;
127
};
128

129 130 131 132 133 134 135 136 137
struct _virDomainEventLifecycle {
    virDomainEvent parent;

    int type;
    int detail;
};
typedef struct _virDomainEventLifecycle virDomainEventLifecycle;
typedef virDomainEventLifecycle *virDomainEventLifecyclePtr;

138 139 140 141 142 143 144
struct _virDomainEventRTCChange {
    virDomainEvent parent;

    long long offset;
};
typedef struct _virDomainEventRTCChange virDomainEventRTCChange;
typedef virDomainEventRTCChange *virDomainEventRTCChangePtr;
145

146 147 148 149 150 151 152 153
struct _virDomainEventWatchdog {
    virDomainEvent parent;

    int action;
};
typedef struct _virDomainEventWatchdog virDomainEventWatchdog;
typedef virDomainEventWatchdog *virDomainEventWatchdogPtr;

154 155 156 157 158 159 160 161 162 163 164
struct _virDomainEventIOError {
    virDomainEvent parent;

    char *srcPath;
    char *devAlias;
    int action;
    char *reason;
};
typedef struct _virDomainEventIOError virDomainEventIOError;
typedef virDomainEventIOError *virDomainEventIOErrorPtr;

165 166 167 168 169 170 171 172 173 174
struct _virDomainEventBlockJob {
    virDomainEvent parent;

    char *path;
    int type;
    int status;
};
typedef struct _virDomainEventBlockJob virDomainEventBlockJob;
typedef virDomainEventBlockJob *virDomainEventBlockJobPtr;

175 176 177 178 179 180 181 182 183 184 185 186
struct _virDomainEventGraphics {
    virDomainEvent parent;

    int phase;
    virDomainEventGraphicsAddressPtr local;
    virDomainEventGraphicsAddressPtr remote;
    char *authScheme;
    virDomainEventGraphicsSubjectPtr subject;
};
typedef struct _virDomainEventGraphics virDomainEventGraphics;
typedef virDomainEventGraphics *virDomainEventGraphicsPtr;

187 188 189 190 191 192 193 194 195 196 197
struct _virDomainEventDiskChange {
    virDomainEvent parent;

    char *oldSrcPath;
    char *newSrcPath;
    char *devAlias;
    int reason;
};
typedef struct _virDomainEventDiskChange virDomainEventDiskChange;
typedef virDomainEventDiskChange *virDomainEventDiskChangePtr;

198 199 200 201 202 203 204 205 206
struct _virDomainEventTrayChange {
    virDomainEvent parent;

    char *devAlias;
    int reason;
};
typedef struct _virDomainEventTrayChange virDomainEventTrayChange;
typedef virDomainEventTrayChange *virDomainEventTrayChangePtr;

207

C
Cédric Bosdonnat 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221
static int virObjectEventOnceInit(void)
{
    if (!(virObjectEventClass =
          virClassNew(virClassForObject(),
                      "virObjectEvent",
                      sizeof(virObjectEvent),
                      virObjectEventDispose)))
        return -1;
    if (!(virDomainEventClass =
          virClassNew(virObjectEventClass,
                      "virDomainEvent",
                      sizeof(virDomainEvent),
                      virDomainEventDispose)))
        return -1;
222 223 224 225 226 227
    if (!(virDomainEventLifecycleClass =
          virClassNew(virDomainEventClass,
                      "virDomainEventLifecycle",
                      sizeof(virDomainEventLifecycle),
                      virDomainEventLifecycleDispose)))
        return -1;
228 229 230 231 232 233
    if (!(virDomainEventRTCChangeClass =
          virClassNew(virDomainEventClass,
                      "virDomainEventRTCChange",
                      sizeof(virDomainEventRTCChange),
                      virDomainEventRTCChangeDispose)))
        return -1;
234 235 236 237 238 239
    if (!(virDomainEventWatchdogClass =
          virClassNew(virDomainEventClass,
                      "virDomainEventWatchdog",
                      sizeof(virDomainEventWatchdog),
                      virDomainEventWatchdogDispose)))
        return -1;
240 241 242 243 244 245
    if (!(virDomainEventIOErrorClass =
          virClassNew(virDomainEventClass,
                      "virDomainEventIOError",
                      sizeof(virDomainEventIOError),
                      virDomainEventIOErrorDispose)))
        return -1;
246 247 248 249 250 251
    if (!(virDomainEventGraphicsClass =
          virClassNew(virDomainEventClass,
                      "virDomainEventGraphics",
                      sizeof(virDomainEventGraphics),
                      virDomainEventGraphicsDispose)))
        return -1;
252 253 254 255 256 257
    if (!(virDomainEventBlockJobClass =
          virClassNew(virDomainEventClass,
                      "virDomainEventBlockJob",
                      sizeof(virDomainEventBlockJob),
                      virDomainEventBlockJobDispose)))
        return -1;
258 259 260 261 262 263
    if (!(virDomainEventDiskChangeClass =
          virClassNew(virDomainEventClass,
                      "virDomainEventDiskChange",
                      sizeof(virDomainEventDiskChange),
                      virDomainEventDiskChangeDispose)))
        return -1;
264 265 266 267 268 269
    if (!(virDomainEventTrayChangeClass =
          virClassNew(virDomainEventClass,
                      "virDomainEventTrayChange",
                      sizeof(virDomainEventTrayChange),
                      virDomainEventTrayChangeDispose)))
        return -1;
C
Cédric Bosdonnat 已提交
270 271 272 273 274 275 276 277 278 279 280 281 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
    return 0;
}

VIR_ONCE_GLOBAL_INIT(virObjectEvent)

static int virObjectEventGetEventID(void *anyobj)
{
    virObjectEventPtr obj = anyobj;

    if (!virObjectIsClass(obj, virObjectEventClass)) {
        VIR_WARN("Object %p (%s) is not a virObjectEvent instance",
                 obj, obj ? virClassName(obj->parent.klass) : "(unknown)");
        return -1;
    }
    return obj->eventID;
}

static void virObjectEventDispose(void *obj)
{
    virObjectEventPtr event = obj;

    VIR_DEBUG("obj=%p", event);
}

static void virDomainEventDispose(void *obj)
{
    virDomainEventPtr event = obj;

    VIR_DEBUG("obj=%p", event);

    switch (virObjectEventGetEventID(event)) {

    case VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED:
        VIR_FREE(event->data.deviceRemoved.devAlias);
        break;
    }

    VIR_FREE(event->meta.name);
}

310 311 312 313 314 315
static void virDomainEventLifecycleDispose(void *obj)
{
    virDomainEventLifecyclePtr event = obj;
    VIR_DEBUG("obj=%p", event);
}

316 317 318 319 320 321
static void virDomainEventRTCChangeDispose(void *obj)
{
    virDomainEventRTCChangePtr event = obj;
    VIR_DEBUG("obj=%p", event);
}

322 323 324 325 326 327
static void virDomainEventWatchdogDispose(void *obj)
{
    virDomainEventWatchdogPtr event = obj;
    VIR_DEBUG("obj=%p", event);
}

328 329 330 331 332 333 334 335 336 337
static void virDomainEventIOErrorDispose(void *obj)
{
    virDomainEventIOErrorPtr event = obj;
    VIR_DEBUG("obj=%p", event);

    VIR_FREE(event->srcPath);
    VIR_FREE(event->devAlias);
    VIR_FREE(event->reason);
}

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
static void virDomainEventGraphicsDispose(void *obj)
{
    virDomainEventGraphicsPtr event = obj;
    VIR_DEBUG("obj=%p", event);

    if (event->local) {
        VIR_FREE(event->local->node);
        VIR_FREE(event->local->service);
        VIR_FREE(event->local);
    }
    if (event->remote) {
        VIR_FREE(event->remote->node);
        VIR_FREE(event->remote->service);
        VIR_FREE(event->remote);
    }
    VIR_FREE(event->authScheme);
    if (event->subject) {
        size_t i;
        for (i = 0; i < event->subject->nidentity; i++) {
            VIR_FREE(event->subject->identities[i].type);
            VIR_FREE(event->subject->identities[i].name);
        }
        VIR_FREE(event->subject);
    }
}

364 365 366 367 368 369 370 371
static void virDomainEventBlockJobDispose(void *obj)
{
    virDomainEventBlockJobPtr event = obj;
    VIR_DEBUG("obj=%p", event);

    VIR_FREE(event->path);
}

372 373 374 375 376 377 378 379 380 381
static void virDomainEventDiskChangeDispose(void *obj)
{
    virDomainEventDiskChangePtr event = obj;
    VIR_DEBUG("obj=%p", event);

    VIR_FREE(event->oldSrcPath);
    VIR_FREE(event->newSrcPath);
    VIR_FREE(event->devAlias);
}

382 383 384 385 386 387 388 389
static void virDomainEventTrayChangeDispose(void *obj)
{
    virDomainEventTrayChangePtr event = obj;
    VIR_DEBUG("obj=%p", event);

    VIR_FREE(event->devAlias);
}

390
/**
391
 * virObjectEventCallbackListFree:
392 393 394 395
 * @list: event callback list head
 *
 * Free the memory in the domain event callback list
 */
396
static void
397
virObjectEventCallbackListFree(virObjectEventCallbackListPtr list)
398
{
399
    size_t i;
400 401 402
    if (!list)
        return;

403
    for (i=0; i<list->count; i++) {
404 405 406
        virFreeCallback freecb = list->callbacks[i]->freecb;
        if (freecb)
            (*freecb)(list->callbacks[i]->opaque);
407 408
        VIR_FREE(list->callbacks[i]);
    }
409
    VIR_FREE(list->callbacks);
410 411
    VIR_FREE(list);
}
412 413


414 415 416 417 418 419
/**
 * virDomainEventCallbackListRemove:
 * @conn: pointer to the connection
 * @cbList: the list
 * @callback: the callback to remove
 *
420
 * Internal function to remove a callback from a virObjectEventCallbackListPtr
421
 */
422
static int
423
virDomainEventCallbackListRemove(virConnectPtr conn,
424
                                 virObjectEventCallbackListPtr cbList,
425 426
                                 virConnectDomainEventCallback callback)
{
427
    int ret = 0;
428
    size_t i;
429
    for (i = 0; i < cbList->count; i++) {
430
        if (cbList->callbacks[i]->cb == VIR_OBJECT_EVENT_CALLBACK(callback) &&
431 432
            cbList->callbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE &&
            cbList->callbacks[i]->conn == conn) {
433 434 435
            virFreeCallback freecb = cbList->callbacks[i]->freecb;
            if (freecb)
                (*freecb)(cbList->callbacks[i]->opaque);
436
            virObjectUnref(cbList->callbacks[i]->conn);
437 438 439 440 441 442 443 444 445 446 447 448 449 450
            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--;

451
            for (i = 0; i < cbList->count; i++) {
452 453 454 455
                if (!cbList->callbacks[i]->deleted)
                    ret++;
            }
            return ret;
456 457
        }
    }
C
Cole Robinson 已提交
458

459 460
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("could not find event callback for removal"));
461 462 463
    return -1;
}

464

465
/**
466
 * virObjectEventCallbackListRemoveID:
467 468 469 470
 * @conn: pointer to the connection
 * @cbList: the list
 * @callback: the callback to remove
 *
471
 * Internal function to remove a callback from a virObjectEventCallbackListPtr
472
 */
473
static int
474 475
virObjectEventCallbackListRemoveID(virConnectPtr conn,
                                   virObjectEventCallbackListPtr cbList,
476 477
                                   int callbackID)
{
478
    int ret = 0;
479
    size_t i;
480
    for (i = 0; i < cbList->count; i++) {
481 482 483 484 485
        if (cbList->callbacks[i]->callbackID == callbackID &&
            cbList->callbacks[i]->conn == conn) {
            virFreeCallback freecb = cbList->callbacks[i]->freecb;
            if (freecb)
                (*freecb)(cbList->callbacks[i]->opaque);
486
            virObjectUnref(cbList->callbacks[i]->conn);
487 488 489 490 491 492 493 494 495 496 497 498 499 500
            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--;

501
            for (i = 0; i < cbList->count; i++) {
502 503 504 505
                if (!cbList->callbacks[i]->deleted)
                    ret++;
            }
            return ret;
506 507 508
        }
    }

509 510
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("could not find event callback for removal"));
511 512 513 514
    return -1;
}


515 516
static int
virDomainEventCallbackListMarkDelete(virConnectPtr conn,
517
                                     virObjectEventCallbackListPtr cbList,
518
                                     virConnectDomainEventCallback callback)
519
{
520
    int ret = 0;
521
    size_t i;
522
    for (i = 0; i < cbList->count; i++) {
523
        if (cbList->callbacks[i]->cb == VIR_OBJECT_EVENT_CALLBACK(callback) &&
524 525
            cbList->callbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE &&
            cbList->callbacks[i]->conn == conn) {
526
            cbList->callbacks[i]->deleted = 1;
527
            for (i = 0; i < cbList->count; i++) {
528 529 530 531
                if (!cbList->callbacks[i]->deleted)
                    ret++;
            }
            return ret;
532 533
        }
    }
C
Cole Robinson 已提交
534

535 536
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("could not find event callback for deletion"));
537 538 539
    return -1;
}

540

541
static int
542 543
virObjectEventCallbackListMarkDeleteID(virConnectPtr conn,
                                       virObjectEventCallbackListPtr cbList,
544
                                       int callbackID)
545
{
546
    int ret = 0;
547
    size_t i;
548
    for (i = 0; i < cbList->count; i++) {
549 550 551
        if (cbList->callbacks[i]->callbackID == callbackID &&
            cbList->callbacks[i]->conn == conn) {
            cbList->callbacks[i]->deleted = 1;
552
            for (i = 0; i < cbList->count; i++) {
553 554 555 556
                if (!cbList->callbacks[i]->deleted)
                    ret++;
            }
            return ret;
557 558 559
        }
    }

560 561
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("could not find event callback for deletion"));
562 563 564 565
    return -1;
}


566
static int
567
virObjectEventCallbackListPurgeMarked(virObjectEventCallbackListPtr cbList)
568 569
{
    int old_count = cbList->count;
570 571 572 573
    int n;
    for (n = 0; n < cbList->count; n++) {
        if (cbList->callbacks[n]->deleted) {
            virFreeCallback freecb = cbList->callbacks[n]->freecb;
574
            if (freecb)
575 576 577
                (*freecb)(cbList->callbacks[n]->opaque);
            virObjectUnref(cbList->callbacks[n]->conn);
            VIR_FREE(cbList->callbacks[n]);
578

579 580 581
            if (n < (cbList->count - 1))
                memmove(cbList->callbacks + n,
                        cbList->callbacks + n + 1,
582
                        sizeof(*(cbList->callbacks)) *
583
                                (cbList->count - (n + 1)));
584
            cbList->count--;
585
            n--;
586 587 588 589 590 591 592 593 594
        }
    }
    if (cbList->count < old_count &&
        VIR_REALLOC_N(cbList->callbacks, cbList->count) < 0) {
        ; /* Failure to reduce memory allocation isn't fatal */
    }
    return 0;
}

595

596
/**
597
 * virObjectEventCallbackListAddID:
598 599
 * @conn: pointer to the connection
 * @cbList: the list
600 601 602
 * @uuid: the uuid of the object to filter on
 * @name: the name of the object to filter on
 * @id: the ID of the object to filter on
603 604 605
 * @eventID: the event ID
 * @callback: the callback to add
 * @opaque: opaque data tio pass to callback
606
 * @callbackID: filled with callback ID
607
 *
608
 * Internal function to add a callback from a virObjectEventCallbackListPtr
609
 */
610
static int
611 612 613 614 615
virObjectEventCallbackListAddID(virConnectPtr conn,
                                virObjectEventCallbackListPtr cbList,
                                unsigned char uuid[VIR_UUID_BUFLEN],
                                const char *name,
                                int id,
616
                                int eventID,
617
                                virConnectObjectEventGenericCallback callback,
618
                                void *opaque,
619 620
                                virFreeCallback freecb,
                                int *callbackID)
621
{
622
    virObjectEventCallbackPtr event;
623
    size_t i;
624
    int ret = 0;
625 626

    /* Check incoming */
627
    if (!cbList) {
628 629 630 631
        return -1;
    }

    /* check if we already have this callback on our list */
632
    for (i = 0; i < cbList->count; i++) {
633
        if (cbList->callbacks[i]->cb == VIR_OBJECT_EVENT_CALLBACK(callback) &&
634
            cbList->callbacks[i]->eventID == eventID &&
635
            cbList->callbacks[i]->conn == conn &&
636
            ((uuid && cbList->callbacks[i]->meta &&
637
              memcmp(cbList->callbacks[i]->meta->uuid,
638 639
                     uuid, VIR_UUID_BUFLEN) == 0) ||
             (!uuid && !cbList->callbacks[i]->meta))) {
640 641
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("event callback already tracked"));
642 643 644 645
            return -1;
        }
    }
    /* Allocate new event */
646
    if (VIR_ALLOC(event) < 0)
647
        goto error;
648
    event->conn = conn;
649 650
    event->cb = callback;
    event->eventID = eventID;
651
    event->opaque = opaque;
652
    event->freecb = freecb;
653

654
    if (name && uuid && id > 0) {
655
        if (VIR_ALLOC(event->meta) < 0)
656
            goto error;
657
        if (VIR_STRDUP(event->meta->name, name) < 0)
658
            goto error;
659 660
        memcpy(event->meta->uuid, uuid, VIR_UUID_BUFLEN);
        event->meta->id = id;
661 662
    }

663 664
    /* Make space on list */
    if (VIR_REALLOC_N(cbList->callbacks, cbList->count + 1) < 0)
665
        goto error;
666

667
    virObjectRef(event->conn);
668

669
    cbList->callbacks[cbList->count] = event;
670
    cbList->count++;
671 672 673

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

674
    for (i = 0; i < cbList->count; i++) {
675
        if (cbList->callbacks[i]->eventID == eventID &&
676 677 678 679 680 681 682 683 684
            cbList->callbacks[i]->conn == conn &&
            !cbList->callbacks[i]->deleted)
            ret++;
    }

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

    return ret;
685

686
error:
687
    if (event) {
688 689 690
        if (event->meta)
            VIR_FREE(event->meta->name);
        VIR_FREE(event->meta);
691 692 693 694 695 696
    }
    VIR_FREE(event);
    return -1;
}


697 698 699 700 701 702 703
/**
 * virDomainEventCallbackListAdd:
 * @conn: pointer to the connection
 * @cbList: the list
 * @callback: the callback to add
 * @opaque: opaque data tio pass to callback
 *
704
 * Internal function to add a callback from a virObjectEventCallbackListPtr
705 706 707
 */
static int
virDomainEventCallbackListAdd(virConnectPtr conn,
708
                              virObjectEventCallbackListPtr cbList,
709 710 711
                              virConnectDomainEventCallback callback,
                              void *opaque,
                              virFreeCallback freecb)
712
{
713
    return virObjectEventCallbackListAddID(conn, cbList, NULL, NULL, 0,
714
                                           VIR_DOMAIN_EVENT_ID_LIFECYCLE,
715
                                           VIR_OBJECT_EVENT_CALLBACK(callback),
716
                                           opaque, freecb, NULL);
717 718 719
}


720 721

static int
722 723
virObjectEventCallbackListEventID(virConnectPtr conn,
                                  virObjectEventCallbackListPtr cbList,
724
                                  int callbackID)
725
{
726
    size_t i;
727

728
    for (i = 0; i < cbList->count; i++) {
729 730 731 732 733 734 735 736 737
        if (cbList->callbacks[i]->deleted)
            continue;

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

    return -1;
738 739
}

740

741
/**
742
 * virObjectEventQueueClear:
743 744
 * @queue: pointer to the queue
 *
745
 * Removes all elements from the queue
746
 */
747
static void
748
virObjectEventQueueClear(virObjectEventQueuePtr queue)
749
{
750
    size_t i;
751 752 753
    if (!queue)
        return;

754
    for (i = 0; i < queue->count; i++) {
C
Cédric Bosdonnat 已提交
755
        virObjectUnref(queue->events[i]);
756 757
    }
    VIR_FREE(queue->events);
758 759 760 761
    queue->count = 0;
}

/**
762
 * virObjectEventQueueFree:
763 764 765 766 767
 * @queue: pointer to the queue
 *
 * Free the memory in the queue. We process this like a list here
 */
static void
768
virObjectEventQueueFree(virObjectEventQueuePtr queue)
769 770 771 772
{
    if (!queue)
        return;

773
    virObjectEventQueueClear(queue);
774 775
    VIR_FREE(queue);
}
776

777 778
static virObjectEventQueuePtr
virObjectEventQueueNew(void)
779
{
780
    virObjectEventQueuePtr ret;
781

782
    ignore_value(VIR_ALLOC(ret));
783 784 785
    return ret;
}

786
static void
787
virObjectEventStateLock(virObjectEventStatePtr state)
788 789 790 791 792
{
    virMutexLock(&state->lock);
}

static void
793
virObjectEventStateUnlock(virObjectEventStatePtr state)
794 795 796 797
{
    virMutexUnlock(&state->lock);
}

798
/**
799 800
 * virObjectEventStateFree:
 * @list: virObjectEventStatePtr to free
801
 *
802
 * Free a virObjectEventStatePtr and its members, and unregister the timer.
803 804
 */
void
805
virObjectEventStateFree(virObjectEventStatePtr state)
806 807 808 809
{
    if (!state)
        return;

810
    virObjectEventCallbackListFree(state->callbacks);
811
    virObjectEventQueueFree(state->queue);
812 813 814

    if (state->timer != -1)
        virEventRemoveTimeout(state->timer);
815 816

    virMutexDestroy(&state->lock);
E
Eric Blake 已提交
817
    VIR_FREE(state);
818 819
}

820

821
static void virObjectEventStateFlush(virObjectEventStatePtr state);
822 823 824 825

static void
virDomainEventTimer(int timer ATTRIBUTE_UNUSED, void *opaque)
{
826
    virObjectEventStatePtr state = opaque;
827

828
    virObjectEventStateFlush(state);
829 830
}

831
/**
832
 * virObjectEventStateNew:
833
 */
834 835
virObjectEventStatePtr
virObjectEventStateNew(void)
836
{
837
    virObjectEventStatePtr state = NULL;
838

839
    if (VIR_ALLOC(state) < 0)
840 841
        goto error;

842 843 844 845 846 847 848
    if (virMutexInit(&state->lock) < 0) {
        virReportSystemError(errno, "%s",
                             _("unable to initialize state mutex"));
        VIR_FREE(state);
        goto error;
    }

849
    if (VIR_ALLOC(state->callbacks) < 0)
850 851
        goto error;

852
    if (!(state->queue = virObjectEventQueueNew()))
853 854
        goto error;

855
    state->timer = -1;
856 857 858 859

    return state;

error:
860
    virObjectEventStateFree(state);
861 862 863
    return NULL;
}

C
Cédric Bosdonnat 已提交
864 865
static void *virObjectEventNew(virClassPtr klass,
                               int eventID)
866
{
C
Cédric Bosdonnat 已提交
867
    virObjectEventPtr event;
868

C
Cédric Bosdonnat 已提交
869 870 871 872 873 874 875 876 877 878 879
    if (virObjectEventInitialize() < 0)
        return NULL;

    if (!virClassIsDerivedFrom(klass, virObjectEventClass)) {
        virReportInvalidArg(klass,
                            _("Class %s must derive from virObjectEvent"),
                            virClassName(klass));
        return NULL;
    }

    if (!(event = virObjectNew(klass)))
880 881
        return NULL;

882
    event->eventID = eventID;
C
Cédric Bosdonnat 已提交
883 884 885 886 887

    VIR_DEBUG("obj=%p", event);
    return event;
}

888 889 890 891 892
static void *virDomainEventNew(virClassPtr klass,
                               int eventID,
                               int id,
                               const char *name,
                               const unsigned char *uuid)
C
Cédric Bosdonnat 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
{
    virDomainEventPtr event;

    if (virObjectEventInitialize() < 0)
        return NULL;

    if (!virClassIsDerivedFrom(klass, virDomainEventClass)) {
        virReportInvalidArg(klass,
                            _("Class %s must derive from virDomainEvent"),
                            virClassName(klass));
        return NULL;
    }

    if (!(event = virObjectEventNew(klass, eventID)))
        return NULL;

909
    if (VIR_STRDUP(event->meta.name, name) < 0) {
910 911 912
        VIR_FREE(event);
        return NULL;
    }
913 914
    event->meta.id = id;
    memcpy(event->meta.uuid, uuid, VIR_UUID_BUFLEN);
915 916 917 918

    return event;
}

919
virDomainEventPtr virDomainEventLifecycleNew(int id, const char *name,
920 921 922
                                    const unsigned char *uuid,
                                    int type, int detail)
{
923
    virDomainEventLifecyclePtr event;
924

C
Cédric Bosdonnat 已提交
925 926 927
    if (virObjectEventInitialize() < 0)
        return NULL;

928 929 930
    if (!(event = virDomainEventNew(virDomainEventLifecycleClass,
                                    VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                    id, name, uuid)))
C
Cédric Bosdonnat 已提交
931 932
        return NULL;

933 934
    event->type = type;
    event->detail = detail;
935

936
    return (virDomainEventPtr)event;
937 938
}

939
virDomainEventPtr virDomainEventLifecycleNewFromDom(virDomainPtr dom, int type, int detail)
940
{
941 942
    return virDomainEventLifecycleNew(dom->id, dom->name, dom->uuid,
                                      type, detail);
943 944
}

945
virDomainEventPtr virDomainEventLifecycleNewFromObj(virDomainObjPtr obj, int type, int detail)
946
{
947
    return virDomainEventLifecycleNewFromDef(obj->def, type, detail);
948 949
}

950
virDomainEventPtr virDomainEventLifecycleNewFromDef(virDomainDefPtr def, int type, int detail)
951
{
952 953
    return virDomainEventLifecycleNew(def->id, def->name, def->uuid,
                                      type, detail);
954 955
}

M
Matthias Bolte 已提交
956 957 958
virDomainEventPtr virDomainEventRebootNew(int id, const char *name,
                                          const unsigned char *uuid)
{
C
Cédric Bosdonnat 已提交
959 960 961
    if (virObjectEventInitialize() < 0)
        return NULL;

962 963 964
    return virDomainEventNew(virDomainEventClass,
                             VIR_DOMAIN_EVENT_ID_REBOOT,
                             id, name, uuid);
M
Matthias Bolte 已提交
965 966
}

967 968
virDomainEventPtr virDomainEventRebootNewFromDom(virDomainPtr dom)
{
C
Cédric Bosdonnat 已提交
969 970 971
    if (virObjectEventInitialize() < 0)
        return NULL;

972 973 974
    return virDomainEventNew(virDomainEventClass,
                             VIR_DOMAIN_EVENT_ID_REBOOT,
                             dom->id, dom->name, dom->uuid);
975
}
M
Matthias Bolte 已提交
976

977 978
virDomainEventPtr virDomainEventRebootNewFromObj(virDomainObjPtr obj)
{
C
Cédric Bosdonnat 已提交
979 980 981
    if (virObjectEventInitialize() < 0)
        return NULL;

982 983 984
    return virDomainEventNew(virDomainEventClass,
                             VIR_DOMAIN_EVENT_ID_REBOOT,
                             obj->def->id, obj->def->name, obj->def->uuid);
985 986
}

987 988 989
virDomainEventPtr virDomainEventRTCChangeNewFromDom(virDomainPtr dom,
                                                    long long offset)
{
990
    virDomainEventRTCChangePtr ev;
C
Cédric Bosdonnat 已提交
991 992 993 994

    if (virObjectEventInitialize() < 0)
        return NULL;

995
    if (!(ev = virDomainEventNew(virDomainEventRTCChangeClass,
996 997
                                 VIR_DOMAIN_EVENT_ID_RTC_CHANGE,
                                 dom->id, dom->name, dom->uuid)))
C
Cédric Bosdonnat 已提交
998
        return NULL;
999

1000
    ev->offset = offset;
1001

1002
    return (virDomainEventPtr)ev;
1003 1004 1005 1006
}
virDomainEventPtr virDomainEventRTCChangeNewFromObj(virDomainObjPtr obj,
                                                    long long offset)
{
1007
    virDomainEventRTCChangePtr ev;
1008

C
Cédric Bosdonnat 已提交
1009 1010 1011
    if (virObjectEventInitialize() < 0)
        return NULL;

1012
    if (!(ev = virDomainEventNew(virDomainEventRTCChangeClass,
1013 1014 1015
                                 VIR_DOMAIN_EVENT_ID_RTC_CHANGE,
                                 obj->def->id, obj->def->name,
                                 obj->def->uuid)))
C
Cédric Bosdonnat 已提交
1016 1017
        return NULL;

1018
    ev->offset = offset;
1019

1020
    return (virDomainEventPtr)ev;
1021 1022
}

1023
virDomainEventPtr virDomainEventWatchdogNewFromDom(virDomainPtr dom, int action)
1024
{
1025
    virDomainEventWatchdogPtr ev;
1026

C
Cédric Bosdonnat 已提交
1027 1028 1029
    if (virObjectEventInitialize() < 0)
        return NULL;

1030
    if (!(ev = virDomainEventNew(virDomainEventWatchdogClass,
1031 1032
                                 VIR_DOMAIN_EVENT_ID_WATCHDOG,
                                 dom->id, dom->name, dom->uuid)))
C
Cédric Bosdonnat 已提交
1033 1034
        return NULL;

1035
    ev->action = action;
1036

1037
    return (virDomainEventPtr)ev;
1038
}
1039
virDomainEventPtr virDomainEventWatchdogNewFromObj(virDomainObjPtr obj, int action)
1040
{
1041
    virDomainEventWatchdogPtr ev;
C
Cédric Bosdonnat 已提交
1042 1043 1044

    if (virObjectEventInitialize() < 0)
        return NULL;
1045

1046
    if (!(ev = virDomainEventNew(virDomainEventWatchdogClass,
1047 1048 1049
                                 VIR_DOMAIN_EVENT_ID_WATCHDOG,
                                 obj->def->id, obj->def->name,
                                 obj->def->uuid)))
C
Cédric Bosdonnat 已提交
1050 1051
        return NULL;

1052
    ev->action = action;
1053

1054
    return (virDomainEventPtr)ev;
1055 1056
}

1057 1058 1059 1060 1061 1062
static virDomainEventPtr virDomainEventIOErrorNewFromDomImpl(int event,
                                                             virDomainPtr dom,
                                                             const char *srcPath,
                                                             const char *devAlias,
                                                             int action,
                                                             const char *reason)
1063
{
1064
    virDomainEventIOErrorPtr ev;
C
Cédric Bosdonnat 已提交
1065 1066 1067 1068

    if (virObjectEventInitialize() < 0)
        return NULL;

1069
    if (!(ev = virDomainEventNew(virDomainEventIOErrorClass, event,
1070
                                 dom->id, dom->name, dom->uuid)))
C
Cédric Bosdonnat 已提交
1071 1072
        return NULL;

1073 1074 1075 1076
    ev->action = action;
    if (VIR_STRDUP(ev->srcPath, srcPath) < 0 ||
        VIR_STRDUP(ev->devAlias, devAlias) < 0 ||
        VIR_STRDUP(ev->reason, reason) < 0) {
C
Cédric Bosdonnat 已提交
1077 1078
        virObjectUnref(ev);
        ev = NULL;
1079 1080
    }

1081
    return (virDomainEventPtr)ev;
1082
}
1083 1084 1085 1086 1087 1088 1089

static virDomainEventPtr virDomainEventIOErrorNewFromObjImpl(int event,
                                                             virDomainObjPtr obj,
                                                             const char *srcPath,
                                                             const char *devAlias,
                                                             int action,
                                                             const char *reason)
1090
{
1091
    virDomainEventIOErrorPtr ev;
C
Cédric Bosdonnat 已提交
1092 1093 1094 1095

    if (virObjectEventInitialize() < 0)
        return NULL;

1096
    if (!(ev = virDomainEventNew(virDomainEventIOErrorClass, event,
1097 1098
                                 obj->def->id, obj->def->name,
                                 obj->def->uuid)))
C
Cédric Bosdonnat 已提交
1099 1100
        return NULL;

1101 1102 1103 1104
    ev->action = action;
    if (VIR_STRDUP(ev->srcPath, srcPath) < 0 ||
        VIR_STRDUP(ev->devAlias, devAlias) < 0 ||
        VIR_STRDUP(ev->reason, reason) < 0) {
C
Cédric Bosdonnat 已提交
1105 1106
        virObjectUnref(ev);
        ev = NULL;
1107 1108
    }

1109
    return (virDomainEventPtr)ev;
1110 1111
}

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
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);
}

1154 1155

virDomainEventPtr virDomainEventGraphicsNewFromDom(virDomainPtr dom,
1156 1157 1158 1159 1160
                                       int phase,
                                       virDomainEventGraphicsAddressPtr local,
                                       virDomainEventGraphicsAddressPtr remote,
                                       const char *authScheme,
                                       virDomainEventGraphicsSubjectPtr subject)
1161
{
1162
    virDomainEventGraphicsPtr ev;
1163

C
Cédric Bosdonnat 已提交
1164 1165 1166
    if (virObjectEventInitialize() < 0)
        return NULL;

1167
    if (!(ev = virDomainEventNew(virDomainEventGraphicsClass,
1168 1169
                                 VIR_DOMAIN_EVENT_ID_GRAPHICS,
                                 dom->id, dom->name, dom->uuid)))
C
Cédric Bosdonnat 已提交
1170 1171
        return NULL;

1172 1173
    ev->phase = phase;
    if (VIR_STRDUP(ev->authScheme, authScheme) < 0) {
C
Cédric Bosdonnat 已提交
1174 1175
        virObjectUnref(ev);
        return NULL;
1176
    }
1177 1178 1179
    ev->local = local;
    ev->remote = remote;
    ev->subject = subject;
1180

1181
    return (virDomainEventPtr)ev;
1182 1183 1184
}

virDomainEventPtr virDomainEventGraphicsNewFromObj(virDomainObjPtr obj,
1185 1186 1187 1188 1189
                                       int phase,
                                       virDomainEventGraphicsAddressPtr local,
                                       virDomainEventGraphicsAddressPtr remote,
                                       const char *authScheme,
                                       virDomainEventGraphicsSubjectPtr subject)
1190
{
1191
    virDomainEventGraphicsPtr ev;
1192

C
Cédric Bosdonnat 已提交
1193 1194 1195
    if (virObjectEventInitialize() < 0)
        return NULL;

1196
    if (!(ev = virDomainEventNew(virDomainEventGraphicsClass,
1197 1198 1199
                                 VIR_DOMAIN_EVENT_ID_GRAPHICS,
                                 obj->def->id, obj->def->name,
                                 obj->def->uuid)))
C
Cédric Bosdonnat 已提交
1200 1201
        return NULL;

1202 1203
    ev->phase = phase;
    if (VIR_STRDUP(ev->authScheme, authScheme) < 0) {
C
Cédric Bosdonnat 已提交
1204 1205
        virObjectUnref(ev);
        return NULL;
1206
    }
1207 1208 1209
    ev->local = local;
    ev->remote = remote;
    ev->subject = subject;
1210

1211
    return (virDomainEventPtr)ev;
1212 1213
}

1214 1215 1216 1217 1218 1219 1220
static
virDomainEventPtr  virDomainEventBlockJobNew(int id,
                                             const char *name,
                                             unsigned char *uuid,
                                             const char *path,
                                             int type,
                                             int status)
1221
{
1222
    virDomainEventBlockJobPtr ev;
1223

C
Cédric Bosdonnat 已提交
1224 1225 1226
    if (virObjectEventInitialize() < 0)
        return NULL;

1227
    if (!(ev = virDomainEventNew(virDomainEventBlockJobClass,
1228 1229
                                 VIR_DOMAIN_EVENT_ID_BLOCK_JOB,
                                 id, name, uuid)))
C
Cédric Bosdonnat 已提交
1230 1231
        return NULL;

1232
    if (VIR_STRDUP(ev->path, path) < 0) {
C
Cédric Bosdonnat 已提交
1233 1234
        virObjectUnref(ev);
        return NULL;
1235
    }
1236 1237
    ev->type = type;
    ev->status = status;
1238

1239
    return (virDomainEventPtr)ev;
1240 1241 1242
}

virDomainEventPtr virDomainEventBlockJobNewFromObj(virDomainObjPtr obj,
1243 1244 1245
                                       const char *path,
                                       int type,
                                       int status)
1246 1247 1248 1249 1250 1251
{
    return virDomainEventBlockJobNew(obj->def->id, obj->def->name,
                                     obj->def->uuid, path, type, status);
}

virDomainEventPtr virDomainEventBlockJobNewFromDom(virDomainPtr dom,
1252 1253 1254
                                       const char *path,
                                       int type,
                                       int status)
1255 1256 1257 1258
{
    return virDomainEventBlockJobNew(dom->id, dom->name, dom->uuid,
                                     path, type, status);
}
1259

1260 1261
virDomainEventPtr virDomainEventControlErrorNewFromDom(virDomainPtr dom)
{
C
Cédric Bosdonnat 已提交
1262 1263 1264 1265 1266
    virDomainEventPtr ev;

    if (virObjectEventInitialize() < 0)
        return NULL;

1267 1268 1269
    if (!(ev = virDomainEventNew(virDomainEventClass,
                                 VIR_DOMAIN_EVENT_ID_CONTROL_ERROR,
                                 dom->id, dom->name, dom->uuid)))
C
Cédric Bosdonnat 已提交
1270
        return NULL;
1271 1272 1273 1274 1275 1276
    return ev;
}


virDomainEventPtr virDomainEventControlErrorNewFromObj(virDomainObjPtr obj)
{
C
Cédric Bosdonnat 已提交
1277 1278 1279 1280 1281
    virDomainEventPtr ev;

    if (virObjectEventInitialize() < 0)
        return NULL;

1282 1283 1284 1285
    if (!(ev = virDomainEventNew(virDomainEventClass,
                                 VIR_DOMAIN_EVENT_ID_CONTROL_ERROR,
                                 obj->def->id, obj->def->name,
                                 obj->def->uuid)))
C
Cédric Bosdonnat 已提交
1286
        return NULL;
1287 1288 1289
    return ev;
}

1290 1291 1292 1293 1294 1295
static
virDomainEventPtr virDomainEventDiskChangeNew(int id, const char *name,
                                              unsigned char *uuid,
                                              const char *oldSrcPath,
                                              const char *newSrcPath,
                                              const char *devAlias, int reason)
1296
{
1297
    virDomainEventDiskChangePtr ev;
1298

C
Cédric Bosdonnat 已提交
1299 1300
    if (virObjectEventInitialize() < 0)
        return NULL;
1301

1302
    if (!(ev = virDomainEventNew(virDomainEventDiskChangeClass,
1303 1304
                                 VIR_DOMAIN_EVENT_ID_DISK_CHANGE,
                                 id, name, uuid)))
C
Cédric Bosdonnat 已提交
1305
        return NULL;
1306

1307
    if (VIR_STRDUP(ev->devAlias, devAlias) < 0)
C
Cédric Bosdonnat 已提交
1308
        goto error;
1309

1310
    if (VIR_STRDUP(ev->oldSrcPath, oldSrcPath) < 0)
C
Cédric Bosdonnat 已提交
1311 1312
        goto error;

1313
    if (VIR_STRDUP(ev->newSrcPath, newSrcPath) < 0)
C
Cédric Bosdonnat 已提交
1314 1315
        goto error;

1316
    ev->reason = reason;
1317

1318
    return (virDomainEventPtr)ev;
1319 1320

error:
C
Cédric Bosdonnat 已提交
1321
    virObjectUnref(ev);
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
    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);
}
1346

1347 1348 1349 1350 1351 1352
static virDomainEventPtr
virDomainEventTrayChangeNew(int id, const char *name,
                            unsigned char *uuid,
                            const char *devAlias,
                            int reason)
{
1353
    virDomainEventTrayChangePtr ev;
1354

C
Cédric Bosdonnat 已提交
1355 1356
    if (virObjectEventInitialize() < 0)
        return NULL;
1357

1358
    if (!(ev = virDomainEventNew(virDomainEventTrayChangeClass,
1359 1360
                                 VIR_DOMAIN_EVENT_ID_TRAY_CHANGE,
                                 id, name, uuid)))
C
Cédric Bosdonnat 已提交
1361 1362
        return NULL;

1363
    if (VIR_STRDUP(ev->devAlias, devAlias) < 0)
C
Cédric Bosdonnat 已提交
1364 1365
        goto error;

1366
    ev->reason = reason;
1367

1368
    return (virDomainEventPtr)ev;
1369 1370

error:
C
Cédric Bosdonnat 已提交
1371
    virObjectUnref(ev);
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
    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 已提交
1394 1395 1396 1397
static virDomainEventPtr
virDomainEventPMWakeupNew(int id, const char *name,
                          unsigned char *uuid)
{
C
Cédric Bosdonnat 已提交
1398 1399 1400 1401 1402
    virDomainEventPtr ev;

    if (virObjectEventInitialize() < 0)
        return NULL;

1403 1404 1405
    if (!(ev = virDomainEventNew(virDomainEventClass,
                                 VIR_DOMAIN_EVENT_ID_PMWAKEUP,
                                 id, name, uuid)))
C
Cédric Bosdonnat 已提交
1406
        return NULL;
O
Osier Yang 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423

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

O
Osier Yang 已提交
1425 1426 1427 1428
static virDomainEventPtr
virDomainEventPMSuspendNew(int id, const char *name,
                           unsigned char *uuid)
{
C
Cédric Bosdonnat 已提交
1429 1430 1431 1432 1433
    virDomainEventPtr ev;

    if (virObjectEventInitialize() < 0)
        return NULL;

1434 1435 1436
    if (!(ev = virDomainEventNew(virDomainEventClass,
                                 VIR_DOMAIN_EVENT_ID_PMSUSPEND,
                                 id, name, uuid)))
C
Cédric Bosdonnat 已提交
1437
        return NULL;
O
Osier Yang 已提交
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455

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

1456 1457 1458 1459
static virDomainEventPtr
virDomainEventPMSuspendDiskNew(int id, const char *name,
                               unsigned char *uuid)
{
C
Cédric Bosdonnat 已提交
1460 1461 1462 1463 1464
    virDomainEventPtr ev;

    if (virObjectEventInitialize() < 0)
        return NULL;

1465 1466 1467
    if (!(ev = virDomainEventNew(virDomainEventClass,
                                 VIR_DOMAIN_EVENT_ID_PMSUSPEND_DISK,
                                 id, name, uuid)))
C
Cédric Bosdonnat 已提交
1468
        return NULL;
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
    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);
}

1486 1487 1488
virDomainEventPtr virDomainEventBalloonChangeNewFromDom(virDomainPtr dom,
                                                        unsigned long long actual)
{
C
Cédric Bosdonnat 已提交
1489 1490 1491 1492 1493
    virDomainEventPtr ev;

    if (virObjectEventInitialize() < 0)
        return NULL;

1494 1495 1496
    if (!(ev = virDomainEventNew(virDomainEventClass,
                                 VIR_DOMAIN_EVENT_ID_BALLOON_CHANGE,
                                 dom->id, dom->name, dom->uuid)))
C
Cédric Bosdonnat 已提交
1497
        return NULL;
1498

C
Cédric Bosdonnat 已提交
1499
    ev->data.balloonChange.actual = actual;
1500 1501 1502 1503 1504 1505

    return ev;
}
virDomainEventPtr virDomainEventBalloonChangeNewFromObj(virDomainObjPtr obj,
                                                        unsigned long long actual)
{
C
Cédric Bosdonnat 已提交
1506 1507 1508 1509 1510
    virDomainEventPtr ev;

    if (virObjectEventInitialize() < 0)
        return NULL;

1511 1512 1513
    if (!(ev = virDomainEventNew(virDomainEventClass,
                                 VIR_DOMAIN_EVENT_ID_BALLOON_CHANGE,
                                 obj->def->id, obj->def->name, obj->def->uuid)))
C
Cédric Bosdonnat 已提交
1514
        return NULL;
1515

C
Cédric Bosdonnat 已提交
1516
    ev->data.balloonChange.actual = actual;
1517 1518 1519 1520

    return ev;
}

1521 1522 1523 1524 1525 1526
static virDomainEventPtr
virDomainEventDeviceRemovedNew(int id,
                               const char *name,
                               unsigned char *uuid,
                               const char *devAlias)
{
C
Cédric Bosdonnat 已提交
1527
    virDomainEventPtr ev;
1528

C
Cédric Bosdonnat 已提交
1529 1530 1531
    if (virObjectEventInitialize() < 0)
        return NULL;

1532 1533 1534
    if (!(ev = virDomainEventNew(virDomainEventClass,
                                 VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED,
                                 id, name, uuid)))
C
Cédric Bosdonnat 已提交
1535 1536 1537 1538
        return NULL;

    if (VIR_STRDUP(ev->data.deviceRemoved.devAlias, devAlias) < 0)
        goto error;
1539 1540 1541 1542

    return ev;

error:
C
Cédric Bosdonnat 已提交
1543
    virObjectUnref(ev);
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
    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);
}

1563
/**
1564 1565
 * virObjectEventQueuePush:
 * @evtQueue: the object event queue
1566 1567
 * @event: the event to add
 *
1568
 * Internal function to push to the back of a virObjectEventQueue
1569 1570 1571
 *
 * Returns: 0 on success, -1 on failure
 */
1572
static int
1573
virObjectEventQueuePush(virObjectEventQueuePtr evtQueue,
1574
                        virDomainEventPtr event)
1575
{
1576
    if (!evtQueue) {
1577 1578 1579 1580 1581
        return -1;
    }

    /* Make space on queue */
    if (VIR_REALLOC_N(evtQueue->events,
1582
                      evtQueue->count + 1) < 0)
1583 1584
        return -1;

1585
    evtQueue->events[evtQueue->count] = event;
1586 1587 1588 1589
    evtQueue->count++;
    return 0;
}

1590

1591
typedef void (*virObjectEventDispatchFunc)(virConnectPtr conn,
1592
                                           virDomainEventPtr event,
1593
                                           virConnectObjectEventGenericCallback cb,
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
                                           void *cbopaque,
                                           void *opaque);


static void
virDomainEventDispatchDefaultFunc(virConnectPtr conn,
                                  virDomainEventPtr event,
                                  virConnectDomainEventGenericCallback cb,
                                  void *cbopaque,
                                  void *opaque ATTRIBUTE_UNUSED)
1604
{
1605
    virDomainPtr dom = virGetDomain(conn, event->meta.name, event->meta.uuid);
C
Cédric Bosdonnat 已提交
1606
    int eventID = virObjectEventGetEventID(event);
1607 1608
    if (!dom)
        return;
1609
    dom->id = event->meta.id;
1610

C
Cédric Bosdonnat 已提交
1611
    switch ((virDomainEventID) eventID) {
1612
    case VIR_DOMAIN_EVENT_ID_LIFECYCLE:
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
        {
            virDomainEventLifecyclePtr lifecycleEvent;

            lifecycleEvent = (virDomainEventLifecyclePtr)event;
            ((virConnectDomainEventCallback)cb)(conn, dom,
                                                lifecycleEvent->type,
                                                lifecycleEvent->detail,
                                                cbopaque);
            goto cleanup;
        }
1623

1624 1625 1626
    case VIR_DOMAIN_EVENT_ID_REBOOT:
        (cb)(conn, dom,
             cbopaque);
1627
        goto cleanup;
1628

1629
    case VIR_DOMAIN_EVENT_ID_RTC_CHANGE:
1630 1631 1632 1633 1634 1635 1636 1637 1638
        {
            virDomainEventRTCChangePtr rtcChangeEvent;

            rtcChangeEvent = (virDomainEventRTCChangePtr)event;
            ((virConnectDomainEventRTCChangeCallback)cb)(conn, dom,
                                                         rtcChangeEvent->offset,
                                                         cbopaque);
            goto cleanup;
        }
1639

1640
    case VIR_DOMAIN_EVENT_ID_WATCHDOG:
1641 1642 1643 1644 1645 1646 1647 1648 1649
        {
            virDomainEventWatchdogPtr watchdogEvent;

            watchdogEvent = (virDomainEventWatchdogPtr)event;
            ((virConnectDomainEventWatchdogCallback)cb)(conn, dom,
                                                        watchdogEvent->action,
                                                        cbopaque);
            goto cleanup;
        }
1650

1651
    case VIR_DOMAIN_EVENT_ID_IO_ERROR:
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
        {
            virDomainEventIOErrorPtr ioErrorEvent;

            ioErrorEvent = (virDomainEventIOErrorPtr)event;
            ((virConnectDomainEventIOErrorCallback)cb)(conn, dom,
                                                       ioErrorEvent->srcPath,
                                                       ioErrorEvent->devAlias,
                                                       ioErrorEvent->action,
                                                       cbopaque);
            goto cleanup;
        }
1663

1664
    case VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON:
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
        {
            virDomainEventIOErrorPtr ioErrorEvent;

            ioErrorEvent = (virDomainEventIOErrorPtr)event;
            ((virConnectDomainEventIOErrorReasonCallback)cb)(conn, dom,
                                                             ioErrorEvent->srcPath,
                                                             ioErrorEvent->devAlias,
                                                             ioErrorEvent->action,
                                                             ioErrorEvent->reason,
                                                             cbopaque);
            goto cleanup;
        }
1677

1678
    case VIR_DOMAIN_EVENT_ID_GRAPHICS:
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
        {
            virDomainEventGraphicsPtr graphicsEvent;

            graphicsEvent = (virDomainEventGraphicsPtr)event;
            ((virConnectDomainEventGraphicsCallback)cb)(conn, dom,
                                                        graphicsEvent->phase,
                                                        graphicsEvent->local,
                                                        graphicsEvent->remote,
                                                        graphicsEvent->authScheme,
                                                        graphicsEvent->subject,
                                                        cbopaque);
            goto cleanup;
        }
1692

1693 1694 1695
    case VIR_DOMAIN_EVENT_ID_CONTROL_ERROR:
        (cb)(conn, dom,
             cbopaque);
1696
        goto cleanup;
1697

1698
    case VIR_DOMAIN_EVENT_ID_BLOCK_JOB:
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
        {
            virDomainEventBlockJobPtr blockJobEvent;

            blockJobEvent = (virDomainEventBlockJobPtr)event;
            ((virConnectDomainEventBlockJobCallback)cb)(conn, dom,
                                                        blockJobEvent->path,
                                                        blockJobEvent->type,
                                                        blockJobEvent->status,
                                                        cbopaque);
            goto cleanup;
        }
1710

1711
    case VIR_DOMAIN_EVENT_ID_DISK_CHANGE:
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
        {
            virDomainEventDiskChangePtr diskChangeEvent;

            diskChangeEvent = (virDomainEventDiskChangePtr)event;
            ((virConnectDomainEventDiskChangeCallback)cb)(conn, dom,
                                                          diskChangeEvent->oldSrcPath,
                                                          diskChangeEvent->newSrcPath,
                                                          diskChangeEvent->devAlias,
                                                          diskChangeEvent->reason,
                                                          cbopaque);
            goto cleanup;
        }
1724

1725
    case VIR_DOMAIN_EVENT_ID_TRAY_CHANGE:
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
        {
            virDomainEventTrayChangePtr trayChangeEvent;

            trayChangeEvent = (virDomainEventTrayChangePtr)event;
            ((virConnectDomainEventTrayChangeCallback)cb)(conn, dom,
                                                          trayChangeEvent->devAlias,
                                                          trayChangeEvent->reason,
                                                          cbopaque);
            goto cleanup;
        }
1736

O
Osier Yang 已提交
1737 1738
    case VIR_DOMAIN_EVENT_ID_PMWAKEUP:
        ((virConnectDomainEventPMWakeupCallback)cb)(conn, dom, 0, cbopaque);
1739
        goto cleanup;
O
Osier Yang 已提交
1740

O
Osier Yang 已提交
1741 1742
    case VIR_DOMAIN_EVENT_ID_PMSUSPEND:
        ((virConnectDomainEventPMSuspendCallback)cb)(conn, dom, 0, cbopaque);
1743
        goto cleanup;
O
Osier Yang 已提交
1744

1745 1746 1747 1748
    case VIR_DOMAIN_EVENT_ID_BALLOON_CHANGE:
        ((virConnectDomainEventBalloonChangeCallback)cb)(conn, dom,
                                                         event->data.balloonChange.actual,
                                                         cbopaque);
1749
        goto cleanup;
1750

1751 1752
    case VIR_DOMAIN_EVENT_ID_PMSUSPEND_DISK:
        ((virConnectDomainEventPMSuspendDiskCallback)cb)(conn, dom, 0, cbopaque);
1753
        goto cleanup;
1754

1755 1756 1757 1758 1759 1760 1761
    case VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED:
        ((virConnectDomainEventDeviceRemovedCallback)cb)(conn, dom,
                                                         event->data.deviceRemoved.devAlias,
                                                         cbopaque);
        goto cleanup;

    case VIR_DOMAIN_EVENT_ID_LAST:
1762
        break;
1763
    }
1764

C
Cédric Bosdonnat 已提交
1765
    VIR_WARN("Unexpected event ID %d", eventID);
1766 1767

cleanup:
1768
    virDomainFree(dom);
1769 1770 1771
}


1772
static int virDomainEventDispatchMatchCallback(virDomainEventPtr event,
1773
                                               virObjectEventCallbackPtr cb)
1774 1775 1776 1777 1778
{
    if (!cb)
        return 0;
    if (cb->deleted)
        return 0;
C
Cédric Bosdonnat 已提交
1779
    if (cb->eventID != virObjectEventGetEventID(event))
1780 1781
        return 0;

1782
    if (cb->meta) {
E
Eric Blake 已提交
1783
        /* Deliberately ignoring 'id' for matching, since that
1784 1785 1786
         * will cause problems when a domain switches between
         * running & shutoff states & ignoring 'name' since
         * Xen sometimes renames guests during migration, thus
E
Eric Blake 已提交
1787
         * leaving 'uuid' as the only truly reliable ID we can use*/
1788

1789
        if (memcmp(event->meta.uuid, cb->meta->uuid, VIR_UUID_BUFLEN) == 0)
1790 1791 1792 1793 1794 1795 1796 1797
            return 1;

        return 0;
    } else {
        return 1;
    }
}

1798 1799 1800

static void
virDomainEventDispatch(virDomainEventPtr event,
1801
                       virObjectEventCallbackListPtr callbacks,
1802
                       virObjectEventDispatchFunc dispatch,
1803
                       void *opaque)
1804
{
1805
    size_t i;
1806
    /* Cache this now, since we may be dropping the lock,
E
Eric Blake 已提交
1807
       and have more callbacks added. We're guaranteed not
1808 1809 1810
       to have any removed */
    int cbCount = callbacks->count;

1811
    for (i = 0; i < cbCount; i++) {
1812 1813 1814 1815 1816 1817 1818 1819
        if (!virDomainEventDispatchMatchCallback(event, callbacks->callbacks[i]))
            continue;

        (*dispatch)(callbacks->callbacks[i]->conn,
                    event,
                    callbacks->callbacks[i]->cb,
                    callbacks->callbacks[i]->opaque,
                    opaque);
1820 1821 1822 1823
    }
}


1824
static void
1825
virObjectEventQueueDispatch(virObjectEventQueuePtr queue,
1826
                            virObjectEventCallbackListPtr callbacks,
1827
                            virObjectEventDispatchFunc dispatch,
1828
                            void *opaque)
1829
{
1830
    size_t i;
1831

1832
    for (i = 0; i < queue->count; i++) {
1833
        virDomainEventDispatch(queue->events[i], callbacks, dispatch, opaque);
C
Cédric Bosdonnat 已提交
1834
        virObjectUnref(queue->events[i]);
1835 1836 1837 1838
    }
    VIR_FREE(queue->events);
    queue->count = 0;
}
1839 1840

void
1841
virObjectEventStateQueue(virObjectEventStatePtr state,
1842 1843 1844
                         virDomainEventPtr event)
{
    if (state->timer < 0) {
C
Cédric Bosdonnat 已提交
1845
        virObjectUnref(event);
1846 1847 1848
        return;
    }

1849
    virObjectEventStateLock(state);
1850

1851
    if (virObjectEventQueuePush(state->queue, event) < 0) {
1852
        VIR_DEBUG("Error adding event to queue");
C
Cédric Bosdonnat 已提交
1853
        virObjectUnref(event);
1854 1855 1856 1857
    }

    if (state->queue->count == 1)
        virEventUpdateTimeout(state->timer, 0);
1858
    virObjectEventStateUnlock(state);
1859 1860
}

1861 1862

static void
1863
virObjectEventStateDispatchFunc(virConnectPtr conn,
1864
                                virDomainEventPtr event,
1865
                                virConnectObjectEventGenericCallback cb,
1866 1867 1868
                                void *cbopaque,
                                void *opaque)
{
1869
    virObjectEventStatePtr state = opaque;
1870 1871

    /* Drop the lock whle dispatching, for sake of re-entrancy */
1872
    virObjectEventStateUnlock(state);
1873 1874
    virDomainEventDispatchDefaultFunc(conn, event,
            VIR_DOMAIN_EVENT_CALLBACK(cb), cbopaque, NULL);
1875
    virObjectEventStateLock(state);
1876 1877 1878 1879
}


static void
1880
virObjectEventStateFlush(virObjectEventStatePtr state)
1881
{
1882
    virObjectEventQueue tempQueue;
1883

1884
    virObjectEventStateLock(state);
1885 1886 1887 1888 1889 1890 1891 1892 1893
    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);
1894

1895
    virObjectEventQueueDispatch(&tempQueue,
1896
                                state->callbacks,
1897
                                virObjectEventStateDispatchFunc,
1898
                                state);
1899 1900

    /* Purge any deleted callbacks */
1901
    virObjectEventCallbackListPurgeMarked(state->callbacks);
1902 1903

    state->isDispatching = false;
1904
    virObjectEventStateUnlock(state);
1905 1906
}

1907

1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
/**
 * virObjectEventStateRegisterID:
 * @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
 */
int
virObjectEventStateRegisterID(virConnectPtr conn,
                              virObjectEventStatePtr state,
                              unsigned char *uuid,
                              const char *name,
                              int id,
                              int eventID,
                              virConnectObjectEventGenericCallback cb,
                              void *opaque,
                              virFreeCallback freecb,
                              int *callbackID)
{
    int ret = -1;

    virObjectEventStateLock(state);

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

    ret = virObjectEventCallbackListAddID(conn, state->callbacks,
                                          uuid, name, id, eventID, cb, opaque, freecb,
                                          callbackID);

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

cleanup:
    virObjectEventStateUnlock(state);
    return ret;
}


1967
/**
1968 1969
 * virDomainEventStateRegister:
 * @conn: connection to associate with callback
1970
 * @state: object event state
1971 1972 1973 1974 1975 1976 1977 1978 1979
 * @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
 */
1980 1981
int
virDomainEventStateRegister(virConnectPtr conn,
1982
                            virObjectEventStatePtr state,
1983 1984 1985
                            virConnectDomainEventCallback callback,
                            void *opaque,
                            virFreeCallback freecb)
1986
{
1987 1988
    int ret = -1;

1989
    virObjectEventStateLock(state);
1990 1991 1992 1993 1994 1995 1996

    if ((state->callbacks->count == 0) &&
        (state->timer == -1) &&
        (state->timer = virEventAddTimeout(-1,
                                           virDomainEventTimer,
                                           state,
                                           NULL)) < 0) {
1997 1998
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("could not initialize domain event timer"));
1999 2000 2001
        goto cleanup;
    }

2002 2003
    ret = virDomainEventCallbackListAdd(conn, state->callbacks,
                                        callback, opaque, freecb);
2004 2005 2006 2007 2008 2009 2010 2011 2012

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

cleanup:
2013
    virObjectEventStateUnlock(state);
2014 2015 2016 2017 2018 2019 2020
    return ret;
}


/**
 * virDomainEventStateRegisterID:
 * @conn: connection to associate with callback
2021
 * @state: object event state
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
 * @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
 */
2033 2034
int
virDomainEventStateRegisterID(virConnectPtr conn,
2035
                              virObjectEventStatePtr state,
2036 2037 2038 2039 2040 2041
                              virDomainPtr dom,
                              int eventID,
                              virConnectDomainEventGenericCallback cb,
                              void *opaque,
                              virFreeCallback freecb,
                              int *callbackID)
2042
{
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
    if (dom)
        return virObjectEventStateRegisterID(conn, state, dom->uuid, dom->name,
                                             dom->id, eventID,
                                             VIR_OBJECT_EVENT_CALLBACK(cb),
                                             opaque, freecb, callbackID);
     else
        return virObjectEventStateRegisterID(conn, state, NULL, NULL, 0,
                                             eventID,
                                             VIR_OBJECT_EVENT_CALLBACK(cb),
                                             opaque, freecb, callbackID);
2053 2054 2055 2056 2057
}


/**
 * virDomainEventStateDeregister:
2058
 * @conn: connection to associate with callback
2059
 * @state: object event state
2060 2061 2062 2063 2064 2065 2066
 * @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
 */
2067 2068
int
virDomainEventStateDeregister(virConnectPtr conn,
2069
                              virObjectEventStatePtr state,
2070 2071
                              virConnectDomainEventCallback callback)
{
2072 2073
    int ret;

2074
    virObjectEventStateLock(state);
2075
    if (state->isDispatching)
2076 2077
        ret = virDomainEventCallbackListMarkDelete(conn,
                                                   state->callbacks, callback);
2078
    else
2079
        ret = virDomainEventCallbackListRemove(conn, state->callbacks, callback);
2080 2081 2082 2083 2084

    if (state->callbacks->count == 0 &&
        state->timer != -1) {
        virEventRemoveTimeout(state->timer);
        state->timer = -1;
2085
        virObjectEventQueueClear(state->queue);
2086 2087
    }

2088
    virObjectEventStateUnlock(state);
2089
    return ret;
2090 2091
}

2092 2093

/**
2094
 * virObjectEventStateDeregisterID:
2095
 * @conn: connection to associate with callback
2096
 * @state: object event state
2097 2098 2099
 * @callbackID: ID of the function to remove from event
 *
 * Unregister the function @callbackID with connection @conn,
2100
 * from @state, for events.
2101
 *
2102
 * Returns: the number of callbacks still registered, or -1 on error
2103
 */
2104
int
2105 2106
virObjectEventStateDeregisterID(virConnectPtr conn,
                                virObjectEventStatePtr state,
2107
                                int callbackID)
2108
{
2109 2110
    int ret;

2111
    virObjectEventStateLock(state);
2112
    if (state->isDispatching)
2113
        ret = virObjectEventCallbackListMarkDeleteID(conn,
2114
                                                     state->callbacks, callbackID);
2115
    else
2116
        ret = virObjectEventCallbackListRemoveID(conn,
2117
                                                 state->callbacks, callbackID);
2118 2119 2120 2121 2122

    if (state->callbacks->count == 0 &&
        state->timer != -1) {
        virEventRemoveTimeout(state->timer);
        state->timer = -1;
2123
        virObjectEventQueueClear(state->queue);
2124 2125
    }

2126
    virObjectEventStateUnlock(state);
2127
    return ret;
2128
}
2129 2130 2131


/**
2132
 * virObjectEventStateEventID:
2133
 * @conn: connection associated with the callback
2134
 * @state: object event state
2135 2136 2137 2138 2139 2140 2141 2142
 * @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
2143 2144
virObjectEventStateEventID(virConnectPtr conn,
                           virObjectEventStatePtr state,
2145 2146 2147 2148
                           int callbackID)
{
    int ret;

2149
    virObjectEventStateLock(state);
2150
    ret = virObjectEventCallbackListEventID(conn,
2151
                                            state->callbacks, callbackID);
2152
    virObjectEventStateUnlock(state);
2153 2154
    return ret;
}