domain_event.c 38.3 KB
Newer Older
1 2 3
/*
 * domain_event.c: domain event queue processing helpers
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2010-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * Copyright (C) 2008 VirtualIron
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Ben Guthro
 */

#include <config.h>

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

#define VIR_FROM_THIS VIR_FROM_NONE

34
#define eventReportError(code, ...)                                 \
35
    virReportErrorHelper(VIR_FROM_THIS, code, __FILE__,             \
36
                         __FUNCTION__, __LINE__, __VA_ARGS__)
37

38 39 40 41 42 43 44 45
struct _virDomainMeta {
    int id;
    char *name;
    unsigned char uuid[VIR_UUID_BUFLEN];
};
typedef struct _virDomainMeta virDomainMeta;
typedef virDomainMeta *virDomainMetaPtr;

46
struct _virDomainEventCallback {
47 48
    int callbackID;
    int eventID;
49
    virConnectPtr conn;
50 51
    virDomainMetaPtr dom;
    virConnectDomainEventGenericCallback cb;
52 53 54 55 56 57
    void *opaque;
    virFreeCallback freecb;
    int deleted;
};

struct _virDomainEvent {
58 59 60 61 62 63 64 65 66
    int eventID;

    virDomainMeta dom;

    union {
        struct {
            int type;
            int detail;
        } lifecycle;
67 68 69
        struct {
            long long offset;
        } rtcChange;
70 71 72
        struct {
            int action;
        } watchdog;
73 74 75 76
        struct {
            char *srcPath;
            char *devAlias;
            int action;
77
            char *reason;
78
        } ioError;
79 80 81 82 83 84 85
        struct {
            int phase;
            virDomainEventGraphicsAddressPtr local;
            virDomainEventGraphicsAddressPtr remote;
            char *authScheme;
            virDomainEventGraphicsSubjectPtr subject;
        } graphics;
86 87 88 89
        struct {
            char *path;
            int status;
        } blockPull;
90
    } data;
91
};
92 93 94 95 96 97 98 99 100 101 102

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

106
    for (i=0; i<list->count; i++) {
107 108 109
        virFreeCallback freecb = list->callbacks[i]->freecb;
        if (freecb)
            (*freecb)(list->callbacks[i]->opaque);
110 111 112 113
        VIR_FREE(list->callbacks[i]);
    }
    VIR_FREE(list);
}
114 115


116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/**
 * virDomainEventCallbackListRemove:
 * @conn: pointer to the connection
 * @cbList: the list
 * @callback: the callback to remove
 *
 * Internal function to remove a callback from a virDomainEventCallbackListPtr
 */
int
virDomainEventCallbackListRemove(virConnectPtr conn,
                                 virDomainEventCallbackListPtr cbList,
                                 virConnectDomainEventCallback callback)
{
    int i;
    for (i = 0 ; i < cbList->count ; i++) {
131 132 133
        if (cbList->callbacks[i]->cb == VIR_DOMAIN_EVENT_CALLBACK(callback) &&
            cbList->callbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE &&
            cbList->callbacks[i]->conn == conn) {
134 135 136
            virFreeCallback freecb = cbList->callbacks[i]->freecb;
            if (freecb)
                (*freecb)(cbList->callbacks[i]->opaque);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
            virUnrefConnect(cbList->callbacks[i]->conn);
            VIR_FREE(cbList->callbacks[i]);

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

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

            return 0;
        }
    }
C
Cole Robinson 已提交
155

156
    eventReportError(VIR_ERR_INTERNAL_ERROR, "%s",
C
Cole Robinson 已提交
157
                     _("could not find event callback for removal"));
158 159 160
    return -1;
}

161

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
/**
 * virDomainEventCallbackListRemoveID:
 * @conn: pointer to the connection
 * @cbList: the list
 * @callback: the callback to remove
 *
 * Internal function to remove a callback from a virDomainEventCallbackListPtr
 */
int
virDomainEventCallbackListRemoveID(virConnectPtr conn,
                                   virDomainEventCallbackListPtr cbList,
                                   int callbackID)
{
    int i;
    for (i = 0 ; i < cbList->count ; i++) {
        if (cbList->callbacks[i]->callbackID == callbackID &&
            cbList->callbacks[i]->conn == conn) {
            virFreeCallback freecb = cbList->callbacks[i]->freecb;
            if (freecb)
                (*freecb)(cbList->callbacks[i]->opaque);
            virUnrefConnect(cbList->callbacks[i]->conn);
            VIR_FREE(cbList->callbacks[i]);

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

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

            return 0;
        }
    }

201
    eventReportError(VIR_ERR_INTERNAL_ERROR, "%s",
202 203 204 205 206
                     _("could not find event callback for removal"));
    return -1;
}


207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/**
 * virDomainEventCallbackListRemoveConn:
 * @conn: pointer to the connection
 * @cbList: the list
 *
 * Internal function to remove all of a given connection's callback
 * from a virDomainEventCallbackListPtr
 */
int
virDomainEventCallbackListRemoveConn(virConnectPtr conn,
                                     virDomainEventCallbackListPtr cbList)
{
    int old_count = cbList->count;
    int i;
    for (i = 0 ; i < cbList->count ; i++) {
222
        if (cbList->callbacks[i]->conn == conn) {
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
            virFreeCallback freecb = cbList->callbacks[i]->freecb;
            if (freecb)
                (*freecb)(cbList->callbacks[i]->opaque);
            virUnrefConnect(cbList->callbacks[i]->conn);
            VIR_FREE(cbList->callbacks[i]);

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

245

246 247 248 249 250 251
int virDomainEventCallbackListMarkDelete(virConnectPtr conn,
                                         virDomainEventCallbackListPtr cbList,
                                         virConnectDomainEventCallback callback)
{
    int i;
    for (i = 0 ; i < cbList->count ; i++) {
252 253 254
        if (cbList->callbacks[i]->cb == VIR_DOMAIN_EVENT_CALLBACK(callback) &&
            cbList->callbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE &&
            cbList->callbacks[i]->conn == conn) {
255 256 257 258
            cbList->callbacks[i]->deleted = 1;
            return 0;
        }
    }
C
Cole Robinson 已提交
259

260
    eventReportError(VIR_ERR_INTERNAL_ERROR, "%s",
C
Cole Robinson 已提交
261
                     _("could not find event callback for deletion"));
262 263 264
    return -1;
}

265

266 267 268 269 270 271 272 273 274 275 276 277 278
int virDomainEventCallbackListMarkDeleteID(virConnectPtr conn,
                                           virDomainEventCallbackListPtr cbList,
                                           int callbackID)
{
    int i;
    for (i = 0 ; i < cbList->count ; i++) {
        if (cbList->callbacks[i]->callbackID == callbackID &&
            cbList->callbacks[i]->conn == conn) {
            cbList->callbacks[i]->deleted = 1;
            return 0;
        }
    }

279
    eventReportError(VIR_ERR_INTERNAL_ERROR, "%s",
280 281 282 283 284
                     _("could not find event callback for deletion"));
    return -1;
}


285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
int virDomainEventCallbackListPurgeMarked(virDomainEventCallbackListPtr cbList)
{
    int old_count = cbList->count;
    int i;
    for (i = 0 ; i < cbList->count ; i++) {
        if (cbList->callbacks[i]->deleted) {
            virFreeCallback freecb = cbList->callbacks[i]->freecb;
            if (freecb)
                (*freecb)(cbList->callbacks[i]->opaque);
            virUnrefConnect(cbList->callbacks[i]->conn);
            VIR_FREE(cbList->callbacks[i]);

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

313

314 315 316 317 318 319 320 321 322 323 324 325 326
/**
 * virDomainEventCallbackListAdd:
 * @conn: pointer to the connection
 * @cbList: the list
 * @callback: the callback to add
 * @opaque: opaque data tio pass to callback
 *
 * Internal function to add a callback from a virDomainEventCallbackListPtr
 */
int
virDomainEventCallbackListAdd(virConnectPtr conn,
                              virDomainEventCallbackListPtr cbList,
                              virConnectDomainEventCallback callback,
327 328
                              void *opaque,
                              virFreeCallback freecb)
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
{
    return virDomainEventCallbackListAddID(conn, cbList, NULL,
                                           VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                           VIR_DOMAIN_EVENT_CALLBACK(callback),
                                           opaque, freecb);
}


/**
 * 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
 *
 * Internal function to add a callback from a virDomainEventCallbackListPtr
 */
int
virDomainEventCallbackListAddID(virConnectPtr conn,
                                virDomainEventCallbackListPtr cbList,
                                virDomainPtr dom,
                                int eventID,
                                virConnectDomainEventGenericCallback callback,
                                void *opaque,
                                virFreeCallback freecb)
355 356
{
    virDomainEventCallbackPtr event;
357
    int i;
358 359 360 361 362 363 364

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

    /* check if we already have this callback on our list */
365 366 367 368
    for (i = 0 ; i < cbList->count ; i++) {
        if (cbList->callbacks[i]->cb == VIR_DOMAIN_EVENT_CALLBACK(callback) &&
            cbList->callbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE &&
            cbList->callbacks[i]->conn == conn) {
369
            eventReportError(VIR_ERR_INTERNAL_ERROR, "%s",
C
Cole Robinson 已提交
370
                             _("event callback already tracked"));
371 372 373 374
            return -1;
        }
    }
    /* Allocate new event */
375 376
    if (VIR_ALLOC(event) < 0)
        goto no_memory;
377
    event->conn = conn;
378 379
    event->cb = callback;
    event->eventID = eventID;
380
    event->opaque = opaque;
381
    event->freecb = freecb;
382

383 384 385 386 387 388 389
    if (dom) {
        if (VIR_ALLOC(event->dom) < 0)
            goto no_memory;
        if (!(event->dom->name = strdup(dom->name)))
            goto no_memory;
        memcpy(event->dom->uuid, dom->uuid, VIR_UUID_BUFLEN);
        event->dom->id = dom->id;
390 391
    }

392 393 394 395
    /* Make space on list */
    if (VIR_REALLOC_N(cbList->callbacks, cbList->count + 1) < 0)
        goto no_memory;

396 397
    event->conn->refs++;

398
    cbList->callbacks[cbList->count] = event;
399
    cbList->count++;
400 401 402 403

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

    return event->callbackID;
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

no_memory:
    virReportOOMError();

    if (event) {
        if (event->dom)
            VIR_FREE(event->dom->name);
        VIR_FREE(event->dom);
    }
    VIR_FREE(event);
    return -1;
}


int virDomainEventCallbackListCountID(virConnectPtr conn,
                                      virDomainEventCallbackListPtr cbList,
                                      int eventID)
{
    int i;
    int count = 0;

    for (i = 0 ; i < cbList->count ; i++) {
        if (cbList->callbacks[i]->deleted)
            continue;

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

    return count;
}


int virDomainEventCallbackListEventID(virConnectPtr conn,
                                      virDomainEventCallbackListPtr cbList,
                                      int callbackID)
{
    int i;

    for (i = 0 ; i < cbList->count ; i++) {
        if (cbList->callbacks[i]->deleted)
            continue;

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

    return -1;
454 455
}

456 457 458

int virDomainEventCallbackListCount(virDomainEventCallbackListPtr cbList)
{
459 460 461 462 463 464
    int i;
    int count = 0;

    for (i = 0 ; i < cbList->count ; i++) {
        if (cbList->callbacks[i]->deleted)
            continue;
465 466
        if (cbList->callbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE)
            count++;
467 468 469
    }

    return count;
470 471 472
}


473 474 475 476 477
void virDomainEventFree(virDomainEventPtr event)
{
    if (!event)
        return;

478
    switch (event->eventID) {
479
    case VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON:
480
    case VIR_DOMAIN_EVENT_ID_IO_ERROR:
481 482
        VIR_FREE(event->data.ioError.srcPath);
        VIR_FREE(event->data.ioError.devAlias);
483
        VIR_FREE(event->data.ioError.reason);
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
        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) {
            int i;
            for (i = 0 ; i < event->data.graphics.subject->nidentity ; i++) {
                VIR_FREE(event->data.graphics.subject->identities[i].type);
                VIR_FREE(event->data.graphics.subject->identities[i].name);
            }
            VIR_FREE(event->data.graphics.subject);
        }
506 507 508 509 510
        break;

    case VIR_DOMAIN_EVENT_ID_BLOCK_PULL:
        VIR_FREE(event->data.blockPull.path);
        break;
511 512
    }

513
    VIR_FREE(event->dom.name);
514 515 516
    VIR_FREE(event);
}

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
/**
 * virDomainEventQueueFree:
 * @queue: pointer to the queue
 *
 * Free the memory in the queue. We process this like a list here
 */
void
virDomainEventQueueFree(virDomainEventQueuePtr queue)
{
    int i;
    if (!queue)
        return;

    for (i = 0; i < queue->count ; i++) {
        virDomainEventFree(queue->events[i]);
    }
    VIR_FREE(queue->events);
    VIR_FREE(queue);
}
536 537 538 539 540

virDomainEventQueuePtr virDomainEventQueueNew(void)
{
    virDomainEventQueuePtr ret;

C
Cole Robinson 已提交
541
    if (VIR_ALLOC(ret) < 0) {
542
        virReportOOMError();
543
        return NULL;
C
Cole Robinson 已提交
544
    }
545 546 547 548

    return ret;
}

549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
/**
 * 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);
E
Eric Blake 已提交
566
    VIR_FREE(state);
567 568
}

569 570 571 572 573 574 575 576 577 578
/**
 * virDomainEventStateNew:
 * @timeout_cb: virEventTimeoutCallback to call when timer expires
 * @timeout_opaque: Data for timeout_cb
 * @timeout_free: Optional virFreeCallback for freeing timeout_opaque
 * @requireTimer: If true, return an error if registering the timer fails.
 *                This is fatal for drivers that sit behind the daemon
 *                (qemu, lxc), since there should always be a event impl
 *                registered.
 */
579 580 581
virDomainEventStatePtr
virDomainEventStateNew(virEventTimeoutCallback timeout_cb,
                       void *timeout_opaque,
582 583
                       virFreeCallback timeout_free,
                       bool requireTimer)
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
{
    virDomainEventStatePtr state = NULL;

    if (VIR_ALLOC(state) < 0) {
        virReportOOMError();
        goto error;
    }

    if (VIR_ALLOC(state->callbacks) < 0) {
        virReportOOMError();
        goto error;
    }

    if (!(state->queue = virDomainEventQueueNew())) {
        goto error;
    }

    if ((state->timer = virEventAddTimeout(-1,
                                           timeout_cb,
                                           timeout_opaque,
                                           timeout_free)) < 0) {
605 606 607 608 609 610 611 612
        if (requireTimer == false) {
            VIR_DEBUG("virEventAddTimeout failed: No addTimeoutImpl defined. "
                      "continuing without events.");
        } else {
            eventReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                             _("could not initialize domain event timer"));
            goto error;
        }
613 614 615 616 617 618 619 620 621
    }

    return state;

error:
    virDomainEventStateFree(state);
    return NULL;
}

622 623 624 625
static virDomainEventPtr virDomainEventNewInternal(int eventID,
                                                   int id,
                                                   const char *name,
                                                   const unsigned char *uuid)
626 627 628
{
    virDomainEventPtr event;

C
Cole Robinson 已提交
629
    if (VIR_ALLOC(event) < 0) {
630
        virReportOOMError();
631
        return NULL;
C
Cole Robinson 已提交
632
    }
633

634 635
    event->eventID = eventID;
    if (!(event->dom.name = strdup(name))) {
636
        virReportOOMError();
637 638 639
        VIR_FREE(event);
        return NULL;
    }
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
    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;
    }
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675

    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 已提交
676 677 678 679 680 681 682
virDomainEventPtr virDomainEventRebootNew(int id, const char *name,
                                          const unsigned char *uuid)
{
    return virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_REBOOT,
                                     id, name, uuid);
}

683 684 685 686 687
virDomainEventPtr virDomainEventRebootNewFromDom(virDomainPtr dom)
{
    return virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_REBOOT,
                                     dom->id, dom->name, dom->uuid);
}
M
Matthias Bolte 已提交
688

689 690 691 692 693 694
virDomainEventPtr virDomainEventRebootNewFromObj(virDomainObjPtr obj)
{
    return virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_REBOOT,
                                     obj->def->id, obj->def->name, obj->def->uuid);
}

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
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;
}

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
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;
}

745 746 747 748 749 750
static virDomainEventPtr virDomainEventIOErrorNewFromDomImpl(int event,
                                                             virDomainPtr dom,
                                                             const char *srcPath,
                                                             const char *devAlias,
                                                             int action,
                                                             const char *reason)
751 752
{
    virDomainEventPtr ev =
753
        virDomainEventNewInternal(event,
754 755 756 757 758
                                  dom->id, dom->name, dom->uuid);

    if (ev) {
        ev->data.ioError.action = action;
        if (!(ev->data.ioError.srcPath = strdup(srcPath)) ||
759 760
            !(ev->data.ioError.devAlias = strdup(devAlias)) ||
            (reason && !(ev->data.ioError.reason = strdup(reason)))) {
761 762 763 764 765 766 767
            virDomainEventFree(ev);
            ev = NULL;
        }
    }

    return ev;
}
768 769 770 771 772 773 774

static virDomainEventPtr virDomainEventIOErrorNewFromObjImpl(int event,
                                                             virDomainObjPtr obj,
                                                             const char *srcPath,
                                                             const char *devAlias,
                                                             int action,
                                                             const char *reason)
775 776
{
    virDomainEventPtr ev =
777
        virDomainEventNewInternal(event,
778 779 780
                                  obj->def->id, obj->def->name, obj->def->uuid);

    if (ev) {
D
Daniel P. Berrange 已提交
781
        ev->data.ioError.action = action;
782
        if (!(ev->data.ioError.srcPath = strdup(srcPath)) ||
783
            !(ev->data.ioError.devAlias = strdup(devAlias)) ||
784
            (reason && !(ev->data.ioError.reason = strdup(reason)))) {
785 786 787 788 789 790 791 792
            virDomainEventFree(ev);
            ev = NULL;
        }
    }

    return ev;
}

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
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);
}

835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850

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;
        if (!(ev->data.graphics.authScheme = strdup(authScheme))) {
            virDomainEventFree(ev);
851
            return NULL;
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
        }
        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;
        if (!(ev->data.graphics.authScheme = strdup(authScheme))) {
            virDomainEventFree(ev);
876
            return NULL;
877 878 879 880 881 882 883 884 885
        }
        ev->data.graphics.local = local;
        ev->data.graphics.remote = remote;
        ev->data.graphics.subject = subject;
    }

    return ev;
}

886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
static virDomainEventPtr
virDomainEventBlockPullNew(int id, const char *name, unsigned char *uuid,
                           const char *path, int status)
{
    virDomainEventPtr ev =
        virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_BLOCK_PULL,
                                  id, name, uuid);

    if (ev) {
        if (!(ev->data.blockPull.path = strdup(path))) {
            virReportOOMError();
            virDomainEventFree(ev);
            return NULL;
        }
        ev->data.blockPull.status = status;
    }

    return ev;
}

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

virDomainEventPtr virDomainEventBlockPullNewFromDom(virDomainPtr dom,
                                                    const char *path,
                                                    int status)
{
    return virDomainEventBlockPullNew(dom->id, dom->name, dom->uuid,
                                                 path, status);
}
921

922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
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;
}


940
/**
941
 * virDomainEventQueuePop:
942 943 944 945 946 947 948 949
 * @evtQueue: the queue of events
 *
 * Internal function to pop off, and return the front of the queue
 * NOTE: The caller is responsible for freeing the returned object
 *
 * Returns: virDomainEventPtr on success NULL on failure.
 */
virDomainEventPtr
950
virDomainEventQueuePop(virDomainEventQueuePtr evtQueue)
951 952 953
{
    virDomainEventPtr ret;

C
Cole Robinson 已提交
954
    if (!evtQueue || evtQueue->count == 0 ) {
955
        eventReportError(VIR_ERR_INTERNAL_ERROR, "%s",
C
Cole Robinson 已提交
956
                         _("event queue is empty, nothing to pop"));
957
        return NULL;
C
Cole Robinson 已提交
958
    }
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976

    ret = evtQueue->events[0];

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

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

    return ret;
}

/**
977
 * virDomainEventQueuePush:
978 979 980 981 982 983 984 985
 * @evtQueue: the dom event queue
 * @event: the event to add
 *
 * Internal function to push onto the back of an virDomainEventQueue
 *
 * Returns: 0 on success, -1 on failure
 */
int
986 987
virDomainEventQueuePush(virDomainEventQueuePtr evtQueue,
                        virDomainEventPtr event)
988
{
989
    if (!evtQueue) {
990 991 992 993 994 995
        return -1;
    }

    /* Make space on queue */
    if (VIR_REALLOC_N(evtQueue->events,
                      evtQueue->count + 1) < 0) {
996
        virReportOOMError();
997 998 999
        return -1;
    }

1000
    evtQueue->events[evtQueue->count] = event;
1001 1002 1003 1004
    evtQueue->count++;
    return 0;
}

1005 1006 1007

void virDomainEventDispatchDefaultFunc(virConnectPtr conn,
                                       virDomainEventPtr event,
1008
                                       virConnectDomainEventGenericCallback cb,
1009 1010 1011
                                       void *cbopaque,
                                       void *opaque ATTRIBUTE_UNUSED)
{
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
    virDomainPtr dom = virGetDomain(conn, event->dom.name, event->dom.uuid);
    if (!dom)
        return;
    dom->id = event->dom.id;

    switch (event->eventID) {
    case VIR_DOMAIN_EVENT_ID_LIFECYCLE:
        ((virConnectDomainEventCallback)cb)(conn, dom,
                                            event->data.lifecycle.type,
                                            event->data.lifecycle.detail,
                                            cbopaque);
        break;

1025 1026 1027 1028 1029
    case VIR_DOMAIN_EVENT_ID_REBOOT:
        (cb)(conn, dom,
             cbopaque);
        break;

1030 1031 1032 1033 1034 1035
    case VIR_DOMAIN_EVENT_ID_RTC_CHANGE:
        ((virConnectDomainEventRTCChangeCallback)cb)(conn, dom,
                                                     event->data.rtcChange.offset,
                                                     cbopaque);
        break;

1036 1037 1038 1039 1040 1041
    case VIR_DOMAIN_EVENT_ID_WATCHDOG:
        ((virConnectDomainEventWatchdogCallback)cb)(conn, dom,
                                                    event->data.watchdog.action,
                                                    cbopaque);
        break;

1042 1043 1044 1045 1046 1047 1048 1049
    case VIR_DOMAIN_EVENT_ID_IO_ERROR:
        ((virConnectDomainEventIOErrorCallback)cb)(conn, dom,
                                                   event->data.ioError.srcPath,
                                                   event->data.ioError.devAlias,
                                                   event->data.ioError.action,
                                                   cbopaque);
        break;

1050 1051 1052 1053 1054 1055 1056 1057 1058
    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);
        break;

1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    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);
        break;

1069 1070 1071 1072 1073
    case VIR_DOMAIN_EVENT_ID_CONTROL_ERROR:
        (cb)(conn, dom,
             cbopaque);
        break;

1074 1075 1076 1077 1078 1079 1080
    case VIR_DOMAIN_EVENT_ID_BLOCK_PULL:
        ((virConnectDomainEventBlockPullCallback)cb)(conn, dom,
                                                     event->data.blockPull.path,
                                                     event->data.blockPull.status,
                                                     cbopaque);
        break;

1081 1082 1083
    default:
        VIR_WARN("Unexpected event ID %d", event->eventID);
        break;
1084
    }
1085 1086

    virDomainFree(dom);
1087 1088 1089
}


1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
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) {
        /* Delibrately ignoring 'id' for matching, since that
         * will cause problems when a domain switches between
         * running & shutoff states & ignoring 'name' since
         * Xen sometimes renames guests during migration, thus
         * leaving 'uuid' as the only truely reliable ID we can use*/

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

        return 0;
    } else {
        return 1;
    }
}

1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
void virDomainEventDispatch(virDomainEventPtr event,
                            virDomainEventCallbackListPtr callbacks,
                            virDomainEventDispatchFunc dispatch,
                            void *opaque)
{
    int i;
    /* Cache this now, since we may be dropping the lock,
       and have more callbacks added. We're guarenteed not
       to have any removed */
    int cbCount = callbacks->count;

    for (i = 0 ; i < cbCount ; i++) {
1128 1129 1130 1131 1132 1133 1134 1135
        if (!virDomainEventDispatchMatchCallback(event, callbacks->callbacks[i]))
            continue;

        (*dispatch)(callbacks->callbacks[i]->conn,
                    event,
                    callbacks->callbacks[i]->cb,
                    callbacks->callbacks[i]->opaque,
                    opaque);
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    }
}


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

    for (i = 0 ; i < queue->count ; i++) {
        virDomainEventDispatch(queue->events[i], callbacks, dispatch, opaque);
        virDomainEventFree(queue->events[i]);
    }
    VIR_FREE(queue->events);
    queue->count = 0;
}
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 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 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224

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

    if (virDomainEventQueuePush(state->queue, event) < 0) {
        VIR_DEBUG("Error adding event to queue");
        virDomainEventFree(event);
    }

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

void
virDomainEventStateFlush(virDomainEventStatePtr state,
                         virDomainEventDispatchFunc dispatchFunc,
                         void *opaque)
{
    virDomainEventQueue tempQueue;

    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);
    virDomainEventQueueDispatch(&tempQueue,
                                state->callbacks,
                                dispatchFunc,
                                opaque);

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

    state->isDispatching = false;
}

int
virDomainEventStateDeregister(virConnectPtr conn,
                              virDomainEventStatePtr state,
                              virConnectDomainEventCallback callback)
{
    if (state->isDispatching)
        return virDomainEventCallbackListMarkDelete(conn,
                                                    state->callbacks, callback);
    else
        return virDomainEventCallbackListRemove(conn, state->callbacks, callback);
}

int
virDomainEventStateDeregisterAny(virConnectPtr conn,
                                 virDomainEventStatePtr state,
                                 int callbackID)
{
    if (state->isDispatching)
        return virDomainEventCallbackListMarkDeleteID(conn,
                                                      state->callbacks, callbackID);
    else
        return virDomainEventCallbackListRemoveID(conn,
                                                  state->callbacks, callbackID);
}