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

#include <config.h>

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

#define VIR_FROM_THIS VIR_FROM_NONE

37
VIR_LOG_INIT("util.domain_event");
38

C
Cédric Bosdonnat 已提交
39
static virClassPtr virDomainEventClass;
40
static virClassPtr virDomainEventLifecycleClass;
41
static virClassPtr virDomainEventRTCChangeClass;
42
static virClassPtr virDomainEventWatchdogClass;
43
static virClassPtr virDomainEventIOErrorClass;
44
static virClassPtr virDomainEventGraphicsClass;
45
static virClassPtr virDomainEventBlockJobClass;
46
static virClassPtr virDomainEventDiskChangeClass;
47
static virClassPtr virDomainEventTrayChangeClass;
48
static virClassPtr virDomainEventBalloonChangeClass;
49
static virClassPtr virDomainEventDeviceRemovedClass;
E
Eric Blake 已提交
50
static virClassPtr virDomainEventPMClass;
51
static virClassPtr virDomainQemuMonitorEventClass;
52
static virClassPtr virDomainEventTunableClass;
53
static virClassPtr virDomainEventAgentLifecycleClass;
54
static virClassPtr virDomainEventDeviceAddedClass;
55
static virClassPtr virDomainEventMigrationIterationClass;
J
Jiri Denemark 已提交
56
static virClassPtr virDomainEventJobCompletedClass;
57
static virClassPtr virDomainEventDeviceRemovalFailedClass;
58
static virClassPtr virDomainEventMetadataChangeClass;
59
static virClassPtr virDomainEventBlockThresholdClass;
60

C
Cédric Bosdonnat 已提交
61
static void virDomainEventDispose(void *obj);
62
static void virDomainEventLifecycleDispose(void *obj);
63
static void virDomainEventRTCChangeDispose(void *obj);
64
static void virDomainEventWatchdogDispose(void *obj);
65
static void virDomainEventIOErrorDispose(void *obj);
66
static void virDomainEventGraphicsDispose(void *obj);
67
static void virDomainEventBlockJobDispose(void *obj);
68
static void virDomainEventDiskChangeDispose(void *obj);
69
static void virDomainEventTrayChangeDispose(void *obj);
70
static void virDomainEventBalloonChangeDispose(void *obj);
71
static void virDomainEventDeviceRemovedDispose(void *obj);
E
Eric Blake 已提交
72
static void virDomainEventPMDispose(void *obj);
73
static void virDomainQemuMonitorEventDispose(void *obj);
74
static void virDomainEventTunableDispose(void *obj);
75
static void virDomainEventAgentLifecycleDispose(void *obj);
76
static void virDomainEventDeviceAddedDispose(void *obj);
77
static void virDomainEventMigrationIterationDispose(void *obj);
J
Jiri Denemark 已提交
78
static void virDomainEventJobCompletedDispose(void *obj);
79
static void virDomainEventDeviceRemovalFailedDispose(void *obj);
80
static void virDomainEventMetadataChangeDispose(void *obj);
81
static void virDomainEventBlockThresholdDispose(void *obj);
C
Cédric Bosdonnat 已提交
82

83 84 85 86 87
static void
virDomainEventDispatchDefaultFunc(virConnectPtr conn,
                                  virObjectEventPtr event,
                                  virConnectObjectEventGenericCallback cb,
                                  void *cbopaque);
C
Cédric Bosdonnat 已提交
88

89 90 91 92 93 94
static void
virDomainQemuMonitorEventDispatchFunc(virConnectPtr conn,
                                      virObjectEventPtr event,
                                      virConnectObjectEventGenericCallback cb,
                                      void *cbopaque);

C
Cédric Bosdonnat 已提交
95 96
struct _virDomainEvent {
    virObjectEvent parent;
97

98
    /* Unused attribute to allow for subclass creation */
99
    bool dummy;
100
};
101 102
typedef struct _virDomainEvent virDomainEvent;
typedef virDomainEvent *virDomainEventPtr;
103

104 105 106 107 108 109 110 111 112
struct _virDomainEventLifecycle {
    virDomainEvent parent;

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

113 114 115 116 117 118 119
struct _virDomainEventRTCChange {
    virDomainEvent parent;

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

121 122 123 124 125 126 127 128
struct _virDomainEventWatchdog {
    virDomainEvent parent;

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

129 130 131 132 133 134 135 136 137 138 139
struct _virDomainEventIOError {
    virDomainEvent parent;

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

140 141 142
struct _virDomainEventBlockJob {
    virDomainEvent parent;

143
    char *disk; /* path or dst, depending on event id */
144 145 146 147 148 149
    int type;
    int status;
};
typedef struct _virDomainEventBlockJob virDomainEventBlockJob;
typedef virDomainEventBlockJob *virDomainEventBlockJobPtr;

150 151 152 153 154 155 156 157 158 159 160 161
struct _virDomainEventGraphics {
    virDomainEvent parent;

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

162 163 164 165 166 167 168 169 170 171 172
struct _virDomainEventDiskChange {
    virDomainEvent parent;

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

173 174 175 176 177 178 179 180 181
struct _virDomainEventTrayChange {
    virDomainEvent parent;

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

182 183 184 185 186 187 188 189 190
struct _virDomainEventBalloonChange {
    virDomainEvent parent;

    /* In unit of 1024 bytes */
    unsigned long long actual;
};
typedef struct _virDomainEventBalloonChange virDomainEventBalloonChange;
typedef virDomainEventBalloonChange *virDomainEventBalloonChangePtr;

191 192 193 194 195 196 197 198
struct _virDomainEventDeviceRemoved {
    virDomainEvent parent;

    char *devAlias;
};
typedef struct _virDomainEventDeviceRemoved virDomainEventDeviceRemoved;
typedef virDomainEventDeviceRemoved *virDomainEventDeviceRemovedPtr;

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

    char *devAlias;
};
typedef struct _virDomainEventDeviceAdded virDomainEventDeviceAdded;
typedef virDomainEventDeviceAdded *virDomainEventDeviceAddedPtr;

E
Eric Blake 已提交
207 208 209 210 211 212 213 214
struct _virDomainEventPM {
    virDomainEvent parent;

    int reason;
};
typedef struct _virDomainEventPM virDomainEventPM;
typedef virDomainEventPM *virDomainEventPMPtr;

215 216 217 218 219 220 221 222 223 224 225
struct _virDomainQemuMonitorEvent {
    virObjectEvent parent;

    char *event;
    long long seconds;
    unsigned int micros;
    char *details;
};
typedef struct _virDomainQemuMonitorEvent virDomainQemuMonitorEvent;
typedef virDomainQemuMonitorEvent *virDomainQemuMonitorEventPtr;

226 227 228 229 230 231 232 233 234
struct _virDomainEventTunable {
    virDomainEvent parent;

    virTypedParameterPtr params;
    int nparams;
};
typedef struct _virDomainEventTunable virDomainEventTunable;
typedef virDomainEventTunable *virDomainEventTunablePtr;

235 236 237 238 239 240 241 242 243
struct _virDomainEventAgentLifecycle {
    virDomainEvent parent;

    int state;
    int reason;
};
typedef struct _virDomainEventAgentLifecycle virDomainEventAgentLifecycle;
typedef virDomainEventAgentLifecycle *virDomainEventAgentLifecyclePtr;

244 245 246 247 248 249 250 251
struct _virDomainEventMigrationIteration {
    virDomainEvent parent;

    int iteration;
};
typedef struct _virDomainEventMigrationIteration virDomainEventMigrationIteration;
typedef virDomainEventMigrationIteration *virDomainEventMigrationIterationPtr;

J
Jiri Denemark 已提交
252 253 254 255 256 257 258 259
struct _virDomainEventJobCompleted {
    virDomainEvent parent;

    virTypedParameterPtr params;
    int nparams;
};
typedef struct _virDomainEventJobCompleted virDomainEventJobCompleted;
typedef virDomainEventJobCompleted *virDomainEventJobCompletedPtr;
260

261 262 263 264 265 266 267 268
struct _virDomainEventDeviceRemovalFailed {
    virDomainEvent parent;

    char *devAlias;
};
typedef struct _virDomainEventDeviceRemovalFailed virDomainEventDeviceRemovalFailed;
typedef virDomainEventDeviceRemovalFailed *virDomainEventDeviceRemovalFailedPtr;

269
struct _virDomainEventMetadataChange {
270 271 272 273 274
    virDomainEvent parent;

    int type;
    char *nsuri;
};
275
typedef struct _virDomainEventMetadataChange virDomainEventMetadataChange;
276 277
typedef virDomainEventMetadataChange *virDomainEventMetadataChangePtr;

278 279 280 281 282 283 284 285 286 287 288
struct _virDomainEventBlockThreshold {
    virDomainEvent parent;

    char *dev;
    char *path;

    unsigned long long threshold;
    unsigned long long excess;
};
typedef struct _virDomainEventBlockThreshold virDomainEventBlockThreshold;
typedef virDomainEventBlockThreshold *virDomainEventBlockThresholdPtr;
289 290


291 292
static int
virDomainEventsOnceInit(void)
293
{
294
    if (!VIR_CLASS_NEW(virDomainEvent, virClassForObjectEvent()))
C
Cédric Bosdonnat 已提交
295
        return -1;
296
    if (!VIR_CLASS_NEW(virDomainEventLifecycle, virDomainEventClass))
297
        return -1;
298
    if (!VIR_CLASS_NEW(virDomainEventRTCChange, virDomainEventClass))
299
        return -1;
300
    if (!VIR_CLASS_NEW(virDomainEventWatchdog, virDomainEventClass))
301
        return -1;
302
    if (!VIR_CLASS_NEW(virDomainEventIOError, virDomainEventClass))
303
        return -1;
304
    if (!VIR_CLASS_NEW(virDomainEventGraphics, virDomainEventClass))
305
        return -1;
306
    if (!VIR_CLASS_NEW(virDomainEventBlockJob, virDomainEventClass))
307
        return -1;
308
    if (!VIR_CLASS_NEW(virDomainEventDiskChange, virDomainEventClass))
309
        return -1;
310
    if (!VIR_CLASS_NEW(virDomainEventTrayChange, virDomainEventClass))
311
        return -1;
312
    if (!VIR_CLASS_NEW(virDomainEventBalloonChange, virDomainEventClass))
313
        return -1;
314
    if (!VIR_CLASS_NEW(virDomainEventDeviceRemoved, virDomainEventClass))
315
        return -1;
316
    if (!VIR_CLASS_NEW(virDomainEventDeviceAdded, virDomainEventClass))
317
        return -1;
318
    if (!VIR_CLASS_NEW(virDomainEventPM, virDomainEventClass))
E
Eric Blake 已提交
319
        return -1;
320
    if (!VIR_CLASS_NEW(virDomainQemuMonitorEvent, virClassForObjectEvent()))
321
        return -1;
322
    if (!VIR_CLASS_NEW(virDomainEventTunable, virDomainEventClass))
323
        return -1;
324
    if (!VIR_CLASS_NEW(virDomainEventAgentLifecycle, virDomainEventClass))
325
        return -1;
326
    if (!VIR_CLASS_NEW(virDomainEventMigrationIteration, virDomainEventClass))
327
        return -1;
328
    if (!VIR_CLASS_NEW(virDomainEventJobCompleted, virDomainEventClass))
J
Jiri Denemark 已提交
329
        return -1;
330
    if (!VIR_CLASS_NEW(virDomainEventDeviceRemovalFailed, virDomainEventClass))
331
        return -1;
332
    if (!VIR_CLASS_NEW(virDomainEventMetadataChange, virDomainEventClass))
333
        return -1;
334
    if (!VIR_CLASS_NEW(virDomainEventBlockThreshold, virDomainEventClass))
335
        return -1;
C
Cédric Bosdonnat 已提交
336 337 338
    return 0;
}

339
VIR_ONCE_GLOBAL_INIT(virDomainEvents);
C
Cédric Bosdonnat 已提交
340 341


342 343
static void
virDomainEventDispose(void *obj)
C
Cédric Bosdonnat 已提交
344 345 346 347 348 349
{
    virDomainEventPtr event = obj;

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

350 351
static void
virDomainEventLifecycleDispose(void *obj)
352 353 354 355 356
{
    virDomainEventLifecyclePtr event = obj;
    VIR_DEBUG("obj=%p", event);
}

357 358
static void
virDomainEventRTCChangeDispose(void *obj)
359 360 361 362 363
{
    virDomainEventRTCChangePtr event = obj;
    VIR_DEBUG("obj=%p", event);
}

364 365
static void
virDomainEventWatchdogDispose(void *obj)
366 367 368 369 370
{
    virDomainEventWatchdogPtr event = obj;
    VIR_DEBUG("obj=%p", event);
}

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

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

382 383
static void
virDomainEventGraphicsDispose(void *obj)
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
{
    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);
    }
}

409 410
static void
virDomainEventBlockJobDispose(void *obj)
411 412 413 414
{
    virDomainEventBlockJobPtr event = obj;
    VIR_DEBUG("obj=%p", event);

415
    VIR_FREE(event->disk);
416 417
}

418 419
static void
virDomainEventDiskChangeDispose(void *obj)
420 421 422 423 424 425 426 427 428
{
    virDomainEventDiskChangePtr event = obj;
    VIR_DEBUG("obj=%p", event);

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

429 430
static void
virDomainEventTrayChangeDispose(void *obj)
431 432 433 434 435 436 437
{
    virDomainEventTrayChangePtr event = obj;
    VIR_DEBUG("obj=%p", event);

    VIR_FREE(event->devAlias);
}

438 439
static void
virDomainEventBalloonChangeDispose(void *obj)
440 441 442 443 444
{
    virDomainEventBalloonChangePtr event = obj;
    VIR_DEBUG("obj=%p", event);
}

445 446
static void
virDomainEventDeviceRemovedDispose(void *obj)
447 448 449 450 451 452 453
{
    virDomainEventDeviceRemovedPtr event = obj;
    VIR_DEBUG("obj=%p", event);

    VIR_FREE(event->devAlias);
}

454 455 456 457 458 459 460 461 462
static void
virDomainEventDeviceAddedDispose(void *obj)
{
    virDomainEventDeviceAddedPtr event = obj;
    VIR_DEBUG("obj=%p", event);

    VIR_FREE(event->devAlias);
}

463 464 465 466 467 468 469 470 471 472 473

static void
virDomainEventDeviceRemovalFailedDispose(void *obj)
{
    virDomainEventDeviceRemovalFailedPtr event = obj;
    VIR_DEBUG("obj=%p", event);

    VIR_FREE(event->devAlias);
}


E
Eric Blake 已提交
474 475 476 477 478 479 480
static void
virDomainEventPMDispose(void *obj)
{
    virDomainEventPMPtr event = obj;
    VIR_DEBUG("obj=%p", event);
}

481 482 483 484 485 486 487 488 489 490
static void
virDomainQemuMonitorEventDispose(void *obj)
{
    virDomainQemuMonitorEventPtr event = obj;
    VIR_DEBUG("obj=%p", event);

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

491 492 493 494 495 496 497 498 499
static void
virDomainEventTunableDispose(void *obj)
{
    virDomainEventTunablePtr event = obj;
    VIR_DEBUG("obj=%p", event);

    virTypedParamsFree(event->params, event->nparams);
}

500 501 502 503 504 505 506
static void
virDomainEventAgentLifecycleDispose(void *obj)
{
    virDomainEventAgentLifecyclePtr event = obj;
    VIR_DEBUG("obj=%p", event);
};

507 508 509 510 511 512 513
static void
virDomainEventMigrationIterationDispose(void *obj)
{
    virDomainEventMigrationIterationPtr event = obj;
    VIR_DEBUG("obj=%p", event);
};

J
Jiri Denemark 已提交
514 515 516 517 518 519 520 521 522
static void
virDomainEventJobCompletedDispose(void *obj)
{
    virDomainEventJobCompletedPtr event = obj;
    VIR_DEBUG("obj=%p", event);

    virTypedParamsFree(event->params, event->nparams);
}

523

524 525 526 527 528 529 530 531 532 533
static void
virDomainEventMetadataChangeDispose(void *obj)
{
    virDomainEventMetadataChangePtr event = obj;
    VIR_DEBUG("obj=%p", event);

    VIR_FREE(event->nsuri);
}


534 535 536 537 538 539 540 541 542 543 544
static void
virDomainEventBlockThresholdDispose(void *obj)
{
    virDomainEventBlockThresholdPtr event = obj;
    VIR_DEBUG("obj=%p", event);

    VIR_FREE(event->dev);
    VIR_FREE(event->path);
}


545 546 547 548 549 550
static void *
virDomainEventNew(virClassPtr klass,
                  int eventID,
                  int id,
                  const char *name,
                  const unsigned char *uuid)
C
Cédric Bosdonnat 已提交
551 552
{
    virDomainEventPtr event;
553
    char uuidstr[VIR_UUID_STRING_BUFLEN];
C
Cédric Bosdonnat 已提交
554

555
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
556 557 558 559 560 561 562 563 564
        return NULL;

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

565 566 567 568
    /* We use uuid for matching key. We ignore 'name' because
     * Xen sometimes renames guests during migration, thus
     * 'uuid' is the only truly reliable key we can use. */
    virUUIDFormat(uuid, uuidstr);
569 570 571
    if (!(event = virObjectEventNew(klass,
                                    virDomainEventDispatchDefaultFunc,
                                    eventID,
572
                                    id, name, uuid, uuidstr)))
573
        return NULL;
574

575
    return (virObjectEventPtr)event;
576 577
}

578 579 580 581 582 583
virObjectEventPtr
virDomainEventLifecycleNew(int id,
                           const char *name,
                           const unsigned char *uuid,
                           int type,
                           int detail)
584
{
585
    virDomainEventLifecyclePtr event;
586

587
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
588 589
        return NULL;

590 591 592
    if (!(event = virDomainEventNew(virDomainEventLifecycleClass,
                                    VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                    id, name, uuid)))
C
Cédric Bosdonnat 已提交
593 594
        return NULL;

595 596
    event->type = type;
    event->detail = detail;
597

598
    return (virObjectEventPtr)event;
599 600
}

601 602 603 604
virObjectEventPtr
virDomainEventLifecycleNewFromDom(virDomainPtr dom,
                                  int type,
                                  int detail)
605
{
606 607
    return virDomainEventLifecycleNew(dom->id, dom->name, dom->uuid,
                                      type, detail);
608 609
}

610 611 612 613
virObjectEventPtr
virDomainEventLifecycleNewFromObj(virDomainObjPtr obj,
                                  int type,
                                  int detail)
614
{
615
    return virDomainEventLifecycleNewFromDef(obj->def, type, detail);
616 617
}

618 619 620 621
virObjectEventPtr
virDomainEventLifecycleNewFromDef(virDomainDefPtr def,
                                  int type,
                                  int detail)
622
{
623 624
    return virDomainEventLifecycleNew(def->id, def->name, def->uuid,
                                      type, detail);
625 626
}

627 628 629 630
virObjectEventPtr
virDomainEventRebootNew(int id,
                        const char *name,
                        const unsigned char *uuid)
M
Matthias Bolte 已提交
631
{
632
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
633 634
        return NULL;

635 636 637
    return virDomainEventNew(virDomainEventClass,
                             VIR_DOMAIN_EVENT_ID_REBOOT,
                             id, name, uuid);
M
Matthias Bolte 已提交
638 639
}

640 641
virObjectEventPtr
virDomainEventRebootNewFromDom(virDomainPtr dom)
642
{
643
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
644 645
        return NULL;

646 647 648
    return virDomainEventNew(virDomainEventClass,
                             VIR_DOMAIN_EVENT_ID_REBOOT,
                             dom->id, dom->name, dom->uuid);
649
}
M
Matthias Bolte 已提交
650

651 652
virObjectEventPtr
virDomainEventRebootNewFromObj(virDomainObjPtr obj)
653
{
654
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
655 656
        return NULL;

657 658 659
    return virDomainEventNew(virDomainEventClass,
                             VIR_DOMAIN_EVENT_ID_REBOOT,
                             obj->def->id, obj->def->name, obj->def->uuid);
660 661
}

662 663 664
virObjectEventPtr
virDomainEventRTCChangeNewFromDom(virDomainPtr dom,
                                  long long offset)
665
{
666
    virDomainEventRTCChangePtr ev;
C
Cédric Bosdonnat 已提交
667

668
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
669 670
        return NULL;

671
    if (!(ev = virDomainEventNew(virDomainEventRTCChangeClass,
672 673
                                 VIR_DOMAIN_EVENT_ID_RTC_CHANGE,
                                 dom->id, dom->name, dom->uuid)))
C
Cédric Bosdonnat 已提交
674
        return NULL;
675

676
    ev->offset = offset;
677

678
    return (virObjectEventPtr)ev;
679
}
680 681 682
virObjectEventPtr
virDomainEventRTCChangeNewFromObj(virDomainObjPtr obj,
                                  long long offset)
683
{
684
    virDomainEventRTCChangePtr ev;
685

686
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
687 688
        return NULL;

689
    if (!(ev = virDomainEventNew(virDomainEventRTCChangeClass,
690 691 692
                                 VIR_DOMAIN_EVENT_ID_RTC_CHANGE,
                                 obj->def->id, obj->def->name,
                                 obj->def->uuid)))
C
Cédric Bosdonnat 已提交
693 694
        return NULL;

695
    ev->offset = offset;
696

697
    return (virObjectEventPtr)ev;
698 699
}

700 701 702
virObjectEventPtr
virDomainEventWatchdogNewFromDom(virDomainPtr dom,
                                 int action)
703
{
704
    virDomainEventWatchdogPtr ev;
705

706
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
707 708
        return NULL;

709
    if (!(ev = virDomainEventNew(virDomainEventWatchdogClass,
710 711
                                 VIR_DOMAIN_EVENT_ID_WATCHDOG,
                                 dom->id, dom->name, dom->uuid)))
C
Cédric Bosdonnat 已提交
712 713
        return NULL;

714
    ev->action = action;
715

716
    return (virObjectEventPtr)ev;
717
}
718 719 720
virObjectEventPtr
virDomainEventWatchdogNewFromObj(virDomainObjPtr obj,
                                 int action)
721
{
722
    virDomainEventWatchdogPtr ev;
C
Cédric Bosdonnat 已提交
723

724
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
725
        return NULL;
726

727
    if (!(ev = virDomainEventNew(virDomainEventWatchdogClass,
728 729 730
                                 VIR_DOMAIN_EVENT_ID_WATCHDOG,
                                 obj->def->id, obj->def->name,
                                 obj->def->uuid)))
C
Cédric Bosdonnat 已提交
731 732
        return NULL;

733
    ev->action = action;
734

735
    return (virObjectEventPtr)ev;
736 737
}

738 739 740 741 742 743 744
static virObjectEventPtr
virDomainEventIOErrorNewFromDomImpl(int event,
                                    virDomainPtr dom,
                                    const char *srcPath,
                                    const char *devAlias,
                                    int action,
                                    const char *reason)
745
{
746
    virDomainEventIOErrorPtr ev;
C
Cédric Bosdonnat 已提交
747

748
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
749 750
        return NULL;

751
    if (!(ev = virDomainEventNew(virDomainEventIOErrorClass, event,
752
                                 dom->id, dom->name, dom->uuid)))
C
Cédric Bosdonnat 已提交
753 754
        return NULL;

755 756 757 758
    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 已提交
759 760
        virObjectUnref(ev);
        ev = NULL;
761 762
    }

763
    return (virObjectEventPtr)ev;
764
}
765

766 767 768 769 770 771 772
static virObjectEventPtr
virDomainEventIOErrorNewFromObjImpl(int event,
                                    virDomainObjPtr obj,
                                    const char *srcPath,
                                    const char *devAlias,
                                    int action,
                                    const char *reason)
773
{
774
    virDomainEventIOErrorPtr ev;
C
Cédric Bosdonnat 已提交
775

776
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
777 778
        return NULL;

779
    if (!(ev = virDomainEventNew(virDomainEventIOErrorClass, event,
780 781
                                 obj->def->id, obj->def->name,
                                 obj->def->uuid)))
C
Cédric Bosdonnat 已提交
782 783
        return NULL;

784 785 786 787
    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 已提交
788 789
        virObjectUnref(ev);
        ev = NULL;
790 791
    }

792
    return (virObjectEventPtr)ev;
793 794
}

795 796 797 798 799
virObjectEventPtr
virDomainEventIOErrorNewFromDom(virDomainPtr dom,
                                const char *srcPath,
                                const char *devAlias,
                                int action)
800 801 802 803 804 805
{
    return virDomainEventIOErrorNewFromDomImpl(VIR_DOMAIN_EVENT_ID_IO_ERROR,
                                               dom, srcPath, devAlias,
                                               action, NULL);
}

806 807 808 809 810
virObjectEventPtr
virDomainEventIOErrorNewFromObj(virDomainObjPtr obj,
                                const char *srcPath,
                                const char *devAlias,
                                int action)
811 812 813 814 815 816
{
    return virDomainEventIOErrorNewFromObjImpl(VIR_DOMAIN_EVENT_ID_IO_ERROR,
                                               obj, srcPath, devAlias,
                                               action, NULL);
}

817 818 819 820 821 822
virObjectEventPtr
virDomainEventIOErrorReasonNewFromDom(virDomainPtr dom,
                                      const char *srcPath,
                                      const char *devAlias,
                                      int action,
                                      const char *reason)
823 824 825 826 827 828
{
    return virDomainEventIOErrorNewFromDomImpl(VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON,
                                               dom, srcPath, devAlias,
                                               action, reason);
}

829 830 831 832 833 834
virObjectEventPtr
virDomainEventIOErrorReasonNewFromObj(virDomainObjPtr obj,
                                      const char *srcPath,
                                      const char *devAlias,
                                      int action,
                                      const char *reason)
835 836 837 838 839 840
{
    return virDomainEventIOErrorNewFromObjImpl(VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON,
                                               obj, srcPath, devAlias,
                                               action, reason);
}

841

842 843 844 845 846 847 848
virObjectEventPtr
virDomainEventGraphicsNewFromDom(virDomainPtr dom,
                                 int phase,
                                 virDomainEventGraphicsAddressPtr local,
                                 virDomainEventGraphicsAddressPtr remote,
                                 const char *authScheme,
                                 virDomainEventGraphicsSubjectPtr subject)
849
{
850
    virDomainEventGraphicsPtr ev;
851

852
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
853 854
        return NULL;

855
    if (!(ev = virDomainEventNew(virDomainEventGraphicsClass,
856 857
                                 VIR_DOMAIN_EVENT_ID_GRAPHICS,
                                 dom->id, dom->name, dom->uuid)))
C
Cédric Bosdonnat 已提交
858 859
        return NULL;

860 861
    ev->phase = phase;
    if (VIR_STRDUP(ev->authScheme, authScheme) < 0) {
C
Cédric Bosdonnat 已提交
862 863
        virObjectUnref(ev);
        return NULL;
864
    }
865 866 867
    ev->local = local;
    ev->remote = remote;
    ev->subject = subject;
868

869
    return (virObjectEventPtr)ev;
870 871
}

872 873 874 875 876 877 878
virObjectEventPtr
virDomainEventGraphicsNewFromObj(virDomainObjPtr obj,
                                 int phase,
                                 virDomainEventGraphicsAddressPtr local,
                                 virDomainEventGraphicsAddressPtr remote,
                                 const char *authScheme,
                                 virDomainEventGraphicsSubjectPtr subject)
879
{
880
    virDomainEventGraphicsPtr ev;
881

882
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
883 884
        return NULL;

885
    if (!(ev = virDomainEventNew(virDomainEventGraphicsClass,
886 887 888
                                 VIR_DOMAIN_EVENT_ID_GRAPHICS,
                                 obj->def->id, obj->def->name,
                                 obj->def->uuid)))
C
Cédric Bosdonnat 已提交
889 890
        return NULL;

891 892
    ev->phase = phase;
    if (VIR_STRDUP(ev->authScheme, authScheme) < 0) {
C
Cédric Bosdonnat 已提交
893 894
        virObjectUnref(ev);
        return NULL;
895
    }
896 897 898
    ev->local = local;
    ev->remote = remote;
    ev->subject = subject;
899

900
    return (virObjectEventPtr)ev;
901 902
}

903
static virObjectEventPtr
904 905
virDomainEventBlockJobNew(int event,
                          int id,
906 907
                          const char *name,
                          unsigned char *uuid,
908
                          const char *disk,
909 910
                          int type,
                          int status)
911
{
912
    virDomainEventBlockJobPtr ev;
913

914
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
915 916
        return NULL;

917
    if (!(ev = virDomainEventNew(virDomainEventBlockJobClass,
918
                                 event,
919
                                 id, name, uuid)))
C
Cédric Bosdonnat 已提交
920 921
        return NULL;

922
    if (VIR_STRDUP(ev->disk, disk) < 0) {
C
Cédric Bosdonnat 已提交
923 924
        virObjectUnref(ev);
        return NULL;
925
    }
926 927
    ev->type = type;
    ev->status = status;
928

929
    return (virObjectEventPtr)ev;
930 931
}

932 933 934 935 936
virObjectEventPtr
virDomainEventBlockJobNewFromObj(virDomainObjPtr obj,
                                 const char *path,
                                 int type,
                                 int status)
937
{
938 939
    return virDomainEventBlockJobNew(VIR_DOMAIN_EVENT_ID_BLOCK_JOB,
                                     obj->def->id, obj->def->name,
940 941 942
                                     obj->def->uuid, path, type, status);
}

943 944 945 946 947
virObjectEventPtr
virDomainEventBlockJobNewFromDom(virDomainPtr dom,
                                 const char *path,
                                 int type,
                                 int status)
948
{
949 950
    return virDomainEventBlockJobNew(VIR_DOMAIN_EVENT_ID_BLOCK_JOB,
                                     dom->id, dom->name, dom->uuid,
951 952
                                     path, type, status);
}
953

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
virObjectEventPtr
virDomainEventBlockJob2NewFromObj(virDomainObjPtr obj,
                                  const char *dst,
                                  int type,
                                  int status)
{
    return virDomainEventBlockJobNew(VIR_DOMAIN_EVENT_ID_BLOCK_JOB_2,
                                     obj->def->id, obj->def->name,
                                     obj->def->uuid, dst, type, status);
}

virObjectEventPtr
virDomainEventBlockJob2NewFromDom(virDomainPtr dom,
                                  const char *dst,
                                  int type,
                                  int status)
{
    return virDomainEventBlockJobNew(VIR_DOMAIN_EVENT_ID_BLOCK_JOB_2,
                                     dom->id, dom->name, dom->uuid,
                                     dst, type, status);
}

976 977
virObjectEventPtr
virDomainEventControlErrorNewFromDom(virDomainPtr dom)
978
{
979
    virObjectEventPtr ev;
C
Cédric Bosdonnat 已提交
980

981
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
982 983
        return NULL;

984 985 986
    if (!(ev = virDomainEventNew(virDomainEventClass,
                                 VIR_DOMAIN_EVENT_ID_CONTROL_ERROR,
                                 dom->id, dom->name, dom->uuid)))
C
Cédric Bosdonnat 已提交
987
        return NULL;
988 989 990 991
    return ev;
}


992 993
virObjectEventPtr
virDomainEventControlErrorNewFromObj(virDomainObjPtr obj)
994
{
995
    virObjectEventPtr ev;
C
Cédric Bosdonnat 已提交
996

997
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
998 999
        return NULL;

1000 1001 1002 1003
    if (!(ev = virDomainEventNew(virDomainEventClass,
                                 VIR_DOMAIN_EVENT_ID_CONTROL_ERROR,
                                 obj->def->id, obj->def->name,
                                 obj->def->uuid)))
C
Cédric Bosdonnat 已提交
1004
        return NULL;
1005 1006 1007
    return ev;
}

1008 1009 1010 1011 1012 1013 1014 1015
static virObjectEventPtr
virDomainEventDiskChangeNew(int id,
                            const char *name,
                            unsigned char *uuid,
                            const char *oldSrcPath,
                            const char *newSrcPath,
                            const char *devAlias,
                            int reason)
1016
{
1017
    virDomainEventDiskChangePtr ev;
1018

1019
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
1020
        return NULL;
1021

1022
    if (!(ev = virDomainEventNew(virDomainEventDiskChangeClass,
1023 1024
                                 VIR_DOMAIN_EVENT_ID_DISK_CHANGE,
                                 id, name, uuid)))
C
Cédric Bosdonnat 已提交
1025
        return NULL;
1026

1027
    if (VIR_STRDUP(ev->devAlias, devAlias) < 0)
C
Cédric Bosdonnat 已提交
1028
        goto error;
1029

1030
    if (VIR_STRDUP(ev->oldSrcPath, oldSrcPath) < 0)
C
Cédric Bosdonnat 已提交
1031 1032
        goto error;

1033
    if (VIR_STRDUP(ev->newSrcPath, newSrcPath) < 0)
C
Cédric Bosdonnat 已提交
1034 1035
        goto error;

1036
    ev->reason = reason;
1037

1038
    return (virObjectEventPtr)ev;
1039

1040
 error:
C
Cédric Bosdonnat 已提交
1041
    virObjectUnref(ev);
1042 1043 1044
    return NULL;
}

1045 1046 1047 1048 1049 1050
virObjectEventPtr
virDomainEventDiskChangeNewFromObj(virDomainObjPtr obj,
                                   const char *oldSrcPath,
                                   const char *newSrcPath,
                                   const char *devAlias,
                                   int reason)
1051 1052 1053 1054 1055 1056
{
    return virDomainEventDiskChangeNew(obj->def->id, obj->def->name,
                                       obj->def->uuid, oldSrcPath,
                                       newSrcPath, devAlias, reason);
}

1057 1058 1059 1060 1061 1062
virObjectEventPtr
virDomainEventDiskChangeNewFromDom(virDomainPtr dom,
                                   const char *oldSrcPath,
                                   const char *newSrcPath,
                                   const char *devAlias,
                                   int reason)
1063 1064 1065 1066 1067
{
    return virDomainEventDiskChangeNew(dom->id, dom->name, dom->uuid,
                                       oldSrcPath, newSrcPath,
                                       devAlias, reason);
}
1068

1069
static virObjectEventPtr
1070 1071
virDomainEventTrayChangeNew(int id,
                            const char *name,
1072 1073 1074 1075
                            unsigned char *uuid,
                            const char *devAlias,
                            int reason)
{
1076
    virDomainEventTrayChangePtr ev;
1077

1078
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
1079
        return NULL;
1080

1081
    if (!(ev = virDomainEventNew(virDomainEventTrayChangeClass,
1082 1083
                                 VIR_DOMAIN_EVENT_ID_TRAY_CHANGE,
                                 id, name, uuid)))
C
Cédric Bosdonnat 已提交
1084 1085
        return NULL;

1086
    if (VIR_STRDUP(ev->devAlias, devAlias) < 0)
C
Cédric Bosdonnat 已提交
1087 1088
        goto error;

1089
    ev->reason = reason;
1090

1091
    return (virObjectEventPtr)ev;
1092

1093
 error:
C
Cédric Bosdonnat 已提交
1094
    virObjectUnref(ev);
1095 1096 1097
    return NULL;
}

1098 1099 1100 1101
virObjectEventPtr
virDomainEventTrayChangeNewFromObj(virDomainObjPtr obj,
                                  const char *devAlias,
                                  int reason)
1102 1103 1104 1105 1106 1107 1108 1109
{
    return virDomainEventTrayChangeNew(obj->def->id,
                                       obj->def->name,
                                       obj->def->uuid,
                                       devAlias,
                                       reason);
}

1110 1111 1112 1113
virObjectEventPtr
virDomainEventTrayChangeNewFromDom(virDomainPtr dom,
                                   const char *devAlias,
                                   int reason)
1114 1115 1116 1117 1118
{
    return virDomainEventTrayChangeNew(dom->id, dom->name, dom->uuid,
                                       devAlias, reason);
}

1119
static virObjectEventPtr
1120 1121
virDomainEventPMWakeupNew(int id,
                          const char *name,
E
Eric Blake 已提交
1122 1123
                          unsigned char *uuid,
                          int reason)
O
Osier Yang 已提交
1124
{
E
Eric Blake 已提交
1125
    virDomainEventPMPtr ev;
C
Cédric Bosdonnat 已提交
1126

1127
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
1128 1129
        return NULL;

E
Eric Blake 已提交
1130
    if (!(ev = virDomainEventNew(virDomainEventPMClass,
1131 1132
                                 VIR_DOMAIN_EVENT_ID_PMWAKEUP,
                                 id, name, uuid)))
C
Cédric Bosdonnat 已提交
1133
        return NULL;
O
Osier Yang 已提交
1134

E
Eric Blake 已提交
1135 1136
    ev->reason = reason;
    return (virObjectEventPtr)ev;
O
Osier Yang 已提交
1137 1138
}

1139
virObjectEventPtr
O
Osier Yang 已提交
1140 1141 1142 1143
virDomainEventPMWakeupNewFromObj(virDomainObjPtr obj)
{
    return virDomainEventPMWakeupNew(obj->def->id,
                                     obj->def->name,
E
Eric Blake 已提交
1144 1145
                                     obj->def->uuid,
                                     0);
O
Osier Yang 已提交
1146 1147
}

1148
virObjectEventPtr
E
Eric Blake 已提交
1149
virDomainEventPMWakeupNewFromDom(virDomainPtr dom, int reason)
O
Osier Yang 已提交
1150
{
E
Eric Blake 已提交
1151
    return virDomainEventPMWakeupNew(dom->id, dom->name, dom->uuid, reason);
O
Osier Yang 已提交
1152
}
1153

1154
static virObjectEventPtr
1155 1156
virDomainEventPMSuspendNew(int id,
                           const char *name,
E
Eric Blake 已提交
1157 1158
                           unsigned char *uuid,
                           int reason)
O
Osier Yang 已提交
1159
{
E
Eric Blake 已提交
1160
    virDomainEventPMPtr ev;
C
Cédric Bosdonnat 已提交
1161

1162
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
1163 1164
        return NULL;

E
Eric Blake 已提交
1165
    if (!(ev = virDomainEventNew(virDomainEventPMClass,
1166 1167
                                 VIR_DOMAIN_EVENT_ID_PMSUSPEND,
                                 id, name, uuid)))
C
Cédric Bosdonnat 已提交
1168
        return NULL;
O
Osier Yang 已提交
1169

E
Eric Blake 已提交
1170 1171
    ev->reason = reason;
    return (virObjectEventPtr)ev;
O
Osier Yang 已提交
1172 1173
}

1174
virObjectEventPtr
O
Osier Yang 已提交
1175 1176 1177 1178
virDomainEventPMSuspendNewFromObj(virDomainObjPtr obj)
{
    return virDomainEventPMSuspendNew(obj->def->id,
                                      obj->def->name,
E
Eric Blake 已提交
1179 1180
                                      obj->def->uuid,
                                      0);
O
Osier Yang 已提交
1181 1182
}

1183
virObjectEventPtr
E
Eric Blake 已提交
1184
virDomainEventPMSuspendNewFromDom(virDomainPtr dom, int reason)
O
Osier Yang 已提交
1185
{
E
Eric Blake 已提交
1186
    return virDomainEventPMSuspendNew(dom->id, dom->name, dom->uuid, reason);
O
Osier Yang 已提交
1187 1188
}

1189
static virObjectEventPtr
1190 1191
virDomainEventPMSuspendDiskNew(int id,
                               const char *name,
E
Eric Blake 已提交
1192 1193
                               unsigned char *uuid,
                               int reason)
1194
{
E
Eric Blake 已提交
1195
    virDomainEventPMPtr ev;
C
Cédric Bosdonnat 已提交
1196

1197
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
1198 1199
        return NULL;

E
Eric Blake 已提交
1200
    if (!(ev = virDomainEventNew(virDomainEventPMClass,
1201 1202
                                 VIR_DOMAIN_EVENT_ID_PMSUSPEND_DISK,
                                 id, name, uuid)))
C
Cédric Bosdonnat 已提交
1203
        return NULL;
E
Eric Blake 已提交
1204 1205 1206

    ev->reason = reason;
    return (virObjectEventPtr)ev;
1207 1208
}

1209
virObjectEventPtr
1210 1211 1212 1213
virDomainEventPMSuspendDiskNewFromObj(virDomainObjPtr obj)
{
    return virDomainEventPMSuspendDiskNew(obj->def->id,
                                          obj->def->name,
E
Eric Blake 已提交
1214 1215
                                          obj->def->uuid,
                                          0);
1216 1217
}

1218
virObjectEventPtr
E
Eric Blake 已提交
1219
virDomainEventPMSuspendDiskNewFromDom(virDomainPtr dom, int reason)
1220
{
E
Eric Blake 已提交
1221 1222
    return virDomainEventPMSuspendDiskNew(dom->id, dom->name, dom->uuid,
                                          reason);
1223 1224
}

1225 1226 1227
virObjectEventPtr
virDomainEventBalloonChangeNewFromDom(virDomainPtr dom,
                                      unsigned long long actual)
1228
{
1229
    virDomainEventBalloonChangePtr ev;
C
Cédric Bosdonnat 已提交
1230

1231
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
1232 1233
        return NULL;

1234
    if (!(ev = virDomainEventNew(virDomainEventBalloonChangeClass,
1235 1236
                                 VIR_DOMAIN_EVENT_ID_BALLOON_CHANGE,
                                 dom->id, dom->name, dom->uuid)))
C
Cédric Bosdonnat 已提交
1237
        return NULL;
1238

1239
    ev->actual = actual;
1240

1241
    return (virObjectEventPtr)ev;
1242
}
1243 1244 1245
virObjectEventPtr
virDomainEventBalloonChangeNewFromObj(virDomainObjPtr obj,
                                      unsigned long long actual)
1246
{
1247
    virDomainEventBalloonChangePtr ev;
C
Cédric Bosdonnat 已提交
1248

1249
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
1250 1251
        return NULL;

1252
    if (!(ev = virDomainEventNew(virDomainEventBalloonChangeClass,
1253 1254
                                 VIR_DOMAIN_EVENT_ID_BALLOON_CHANGE,
                                 obj->def->id, obj->def->name, obj->def->uuid)))
C
Cédric Bosdonnat 已提交
1255
        return NULL;
1256

1257
    ev->actual = actual;
1258

1259
    return (virObjectEventPtr)ev;
1260 1261
}

1262 1263 1264 1265 1266
static virObjectEventPtr
virDomainEventDeviceRemovedNew(int id,
                               const char *name,
                               unsigned char *uuid,
                               const char *devAlias)
1267
{
1268
    virDomainEventDeviceRemovedPtr ev;
1269

1270
    if (virDomainEventsInitialize() < 0)
C
Cédric Bosdonnat 已提交
1271 1272
        return NULL;

1273
    if (!(ev = virDomainEventNew(virDomainEventDeviceRemovedClass,
1274 1275
                                 VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED,
                                 id, name, uuid)))
C
Cédric Bosdonnat 已提交
1276 1277
        return NULL;

1278
    if (VIR_STRDUP(ev->devAlias, devAlias) < 0)
C
Cédric Bosdonnat 已提交
1279
        goto error;
1280

1281
    return (virObjectEventPtr)ev;
1282

1283
 error:
C
Cédric Bosdonnat 已提交
1284
    virObjectUnref(ev);
1285 1286 1287
    return NULL;
}

1288 1289 1290
virObjectEventPtr
virDomainEventDeviceRemovedNewFromObj(virDomainObjPtr obj,
                                      const char *devAlias)
1291 1292 1293 1294 1295
{
    return virDomainEventDeviceRemovedNew(obj->def->id, obj->def->name,
                                          obj->def->uuid, devAlias);
}

1296 1297 1298
virObjectEventPtr
virDomainEventDeviceRemovedNewFromDom(virDomainPtr dom,
                                      const char *devAlias)
1299 1300 1301 1302 1303
{
    return virDomainEventDeviceRemovedNew(dom->id, dom->name, dom->uuid,
                                          devAlias);
}

1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
static virObjectEventPtr
virDomainEventDeviceAddedNew(int id,
                             const char *name,
                             unsigned char *uuid,
                             const char *devAlias)
{
    virDomainEventDeviceAddedPtr ev;

    if (virDomainEventsInitialize() < 0)
        return NULL;

    if (!(ev = virDomainEventNew(virDomainEventDeviceAddedClass,
                                 VIR_DOMAIN_EVENT_ID_DEVICE_ADDED,
                                 id, name, uuid)))
        return NULL;

    if (VIR_STRDUP(ev->devAlias, devAlias) < 0)
        goto error;

    return (virObjectEventPtr)ev;

 error:
    virObjectUnref(ev);
    return NULL;
}

virObjectEventPtr
virDomainEventDeviceAddedNewFromObj(virDomainObjPtr obj,
                                       const char *devAlias)
{
    return virDomainEventDeviceAddedNew(obj->def->id, obj->def->name,
                                           obj->def->uuid, devAlias);
}

virObjectEventPtr
virDomainEventDeviceAddedNewFromDom(virDomainPtr dom,
                                      const char *devAlias)
{
    return virDomainEventDeviceAddedNew(dom->id, dom->name, dom->uuid,
                                          devAlias);
}
1345

1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357

static virObjectEventPtr
virDomainEventDeviceRemovalFailedNew(int id,
                                     const char *name,
                                     unsigned char *uuid,
                                     const char *devAlias)
{
    virDomainEventDeviceRemovalFailedPtr ev;

    if (virDomainEventsInitialize() < 0)
        return NULL;

1358
    if (!(ev = virDomainEventNew(virDomainEventDeviceRemovalFailedClass,
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
                                 VIR_DOMAIN_EVENT_ID_DEVICE_REMOVAL_FAILED,
                                 id, name, uuid)))
        return NULL;

    if (VIR_STRDUP(ev->devAlias, devAlias) < 0)
        goto error;

    return (virObjectEventPtr)ev;

 error:
    virObjectUnref(ev);
    return NULL;
}

virObjectEventPtr
virDomainEventDeviceRemovalFailedNewFromObj(virDomainObjPtr obj,
                                            const char *devAlias)
{
    return virDomainEventDeviceRemovalFailedNew(obj->def->id, obj->def->name,
                                                obj->def->uuid, devAlias);
}

virObjectEventPtr
virDomainEventDeviceRemovalFailedNewFromDom(virDomainPtr dom,
                                            const char *devAlias)
{
    return virDomainEventDeviceRemovalFailedNew(dom->id, dom->name, dom->uuid,
                                                devAlias);
}


1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
static virObjectEventPtr
virDomainEventAgentLifecycleNew(int id,
                                const char *name,
                                const unsigned char *uuid,
                                int state,
                                int reason)
{
    virDomainEventAgentLifecyclePtr ev;

    if (virDomainEventsInitialize() < 0)
        return NULL;

    if (!(ev = virDomainEventNew(virDomainEventAgentLifecycleClass,
                                 VIR_DOMAIN_EVENT_ID_AGENT_LIFECYCLE,
                                 id, name, uuid)))
        return NULL;

    ev->state = state;
    ev->reason = reason;

    return (virObjectEventPtr)ev;
}

virObjectEventPtr
virDomainEventAgentLifecycleNewFromObj(virDomainObjPtr obj,
                                       int state,
                                       int reason)
{
    return virDomainEventAgentLifecycleNew(obj->def->id, obj->def->name,
                                           obj->def->uuid, state, reason);
}

virObjectEventPtr
virDomainEventAgentLifecycleNewFromDom(virDomainPtr dom,
                                       int state,
                                       int reason)
{
    return virDomainEventAgentLifecycleNew(dom->id, dom->name, dom->uuid,
                                           state, reason);
}

1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
static virObjectEventPtr
virDomainEventMigrationIterationNew(int id,
                                    const char *name,
                                    const unsigned char *uuid,
                                    int iteration)
{
    virDomainEventMigrationIterationPtr ev;

    if (virDomainEventsInitialize() < 0)
        return NULL;

    if (!(ev = virDomainEventNew(virDomainEventMigrationIterationClass,
                                 VIR_DOMAIN_EVENT_ID_MIGRATION_ITERATION,
                                 id, name, uuid)))
        return NULL;

    ev->iteration = iteration;

    return (virObjectEventPtr)ev;
}

virObjectEventPtr
virDomainEventMigrationIterationNewFromObj(virDomainObjPtr obj,
                                           int iteration)
{
    return virDomainEventMigrationIterationNew(obj->def->id, obj->def->name,
                                               obj->def->uuid, iteration);
}

virObjectEventPtr
virDomainEventMigrationIterationNewFromDom(virDomainPtr dom,
                                           int iteration)
{
    return virDomainEventMigrationIterationNew(dom->id, dom->name, dom->uuid,
                                               iteration);
}

J
Jiri Denemark 已提交
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
/* This function consumes @params, the caller must not free it.
 */
static virObjectEventPtr
virDomainEventJobCompletedNew(int id,
                              const char *name,
                              const unsigned char *uuid,
                              virTypedParameterPtr params,
                              int nparams)
{
    virDomainEventJobCompletedPtr ev;

    if (virDomainEventsInitialize() < 0)
        goto error;

    if (!(ev = virDomainEventNew(virDomainEventJobCompletedClass,
                                 VIR_DOMAIN_EVENT_ID_JOB_COMPLETED,
                                 id, name, uuid)))
        goto error;

    ev->params = params;
    ev->nparams = nparams;

    return (virObjectEventPtr) ev;

 error:
    virTypedParamsFree(params, nparams);
    return NULL;
}

virObjectEventPtr
virDomainEventJobCompletedNewFromObj(virDomainObjPtr obj,
                                     virTypedParameterPtr params,
                                     int nparams)
{
    return virDomainEventJobCompletedNew(obj->def->id, obj->def->name,
                                         obj->def->uuid, params, nparams);
}

virObjectEventPtr
virDomainEventJobCompletedNewFromDom(virDomainPtr dom,
                                     virTypedParameterPtr params,
                                     int nparams)
{
    return virDomainEventJobCompletedNew(dom->id, dom->name, dom->uuid,
                                         params, nparams);
}

1515

1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
/* This function consumes the params so caller don't have to care about
 * freeing it even if error occurs. The reason is to not have to do deep
 * copy of params.
 */
static virObjectEventPtr
virDomainEventTunableNew(int id,
                         const char *name,
                         unsigned char *uuid,
                         virTypedParameterPtr params,
                         int nparams)
{
    virDomainEventTunablePtr ev;

    if (virDomainEventsInitialize() < 0)
        goto error;

    if (!(ev = virDomainEventNew(virDomainEventTunableClass,
                                 VIR_DOMAIN_EVENT_ID_TUNABLE,
                                 id, name, uuid)))
        goto error;

    ev->params = params;
    ev->nparams = nparams;

    return (virObjectEventPtr)ev;

 error:
    virTypedParamsFree(params, nparams);
    return NULL;
}

virObjectEventPtr
virDomainEventTunableNewFromObj(virDomainObjPtr obj,
                                virTypedParameterPtr params,
                                int nparams)
{
    return virDomainEventTunableNew(obj->def->id,
                                    obj->def->name,
                                    obj->def->uuid,
                                    params,
                                    nparams);
}

virObjectEventPtr
virDomainEventTunableNewFromDom(virDomainPtr dom,
                                virTypedParameterPtr params,
                                int nparams)
{
    return virDomainEventTunableNew(dom->id,
                                    dom->name,
                                    dom->uuid,
                                    params,
                                    nparams);
}

1571

1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
static virObjectEventPtr
virDomainEventMetadataChangeNew(int id,
                                const char *name,
                                unsigned char *uuid,
                                int type,
                                const char *nsuri)
{
    virDomainEventMetadataChangePtr ev;

    if (virDomainEventsInitialize() < 0)
        return NULL;

    if (!(ev = virDomainEventNew(virDomainEventMetadataChangeClass,
                                 VIR_DOMAIN_EVENT_ID_METADATA_CHANGE,
                                 id, name, uuid)))
        return NULL;

    ev->type = type;
    if (nsuri && VIR_STRDUP(ev->nsuri, nsuri) < 0)
        goto error;

    return (virObjectEventPtr)ev;

 error:
    virObjectUnref(ev);
    return NULL;
}

virObjectEventPtr
virDomainEventMetadataChangeNewFromObj(virDomainObjPtr obj,
                                       int type,
                                       const char *nsuri)
{
    return virDomainEventMetadataChangeNew(obj->def->id, obj->def->name,
                                           obj->def->uuid, type, nsuri);
}

virObjectEventPtr
virDomainEventMetadataChangeNewFromDom(virDomainPtr dom,
                                       int type,
                                       const char *nsuri)
{
    return virDomainEventMetadataChangeNew(dom->id, dom->name, dom->uuid,
                                           type, nsuri);
}


1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
static virObjectEventPtr
virDomainEventBlockThresholdNew(int id,
                                const char *name,
                                unsigned char *uuid,
                                const char *dev,
                                const char *path,
                                unsigned long long threshold,
                                unsigned long long excess)
{
    virDomainEventBlockThresholdPtr ev;

    if (virDomainEventsInitialize() < 0)
        return NULL;

    if (!(ev = virDomainEventNew(virDomainEventBlockThresholdClass,
                                 VIR_DOMAIN_EVENT_ID_BLOCK_THRESHOLD,
                                 id, name, uuid)))
        return NULL;

    if (VIR_STRDUP(ev->dev, dev) < 0 ||
        VIR_STRDUP(ev->path, path) < 0) {
        virObjectUnref(ev);
        return NULL;
    }
    ev->threshold = threshold;
    ev->excess = excess;

    return (virObjectEventPtr)ev;
}

virObjectEventPtr
virDomainEventBlockThresholdNewFromObj(virDomainObjPtr obj,
                                       const char *dev,
                                       const char *path,
                                       unsigned long long threshold,
                                       unsigned long long excess)
{
    return virDomainEventBlockThresholdNew(obj->def->id, obj->def->name,
                                           obj->def->uuid, dev, path,
                                           threshold, excess);
}

virObjectEventPtr
virDomainEventBlockThresholdNewFromDom(virDomainPtr dom,
                                       const char *dev,
                                       const char *path,
                                       unsigned long long threshold,
                                       unsigned long long excess)
{
    return virDomainEventBlockThresholdNew(dom->id, dom->name, dom->uuid,
                                           dev, path, threshold, excess);
}


1673
static void
1674
virDomainEventDispatchDefaultFunc(virConnectPtr conn,
1675
                                  virObjectEventPtr event,
1676 1677
                                  virConnectObjectEventGenericCallback cb,
                                  void *cbopaque)
1678
{
1679 1680 1681
    virDomainPtr dom = virGetDomain(conn, event->meta.name,
                                    event->meta.uuid, event->meta.id);

1682 1683 1684
    if (!dom)
        return;

1685
    switch ((virDomainEventID) event->eventID) {
1686
    case VIR_DOMAIN_EVENT_ID_LIFECYCLE:
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
        {
            virDomainEventLifecyclePtr lifecycleEvent;

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

1698 1699 1700
    case VIR_DOMAIN_EVENT_ID_REBOOT:
        (cb)(conn, dom,
             cbopaque);
1701
        goto cleanup;
1702

1703
    case VIR_DOMAIN_EVENT_ID_RTC_CHANGE:
1704 1705 1706 1707 1708 1709 1710 1711 1712
        {
            virDomainEventRTCChangePtr rtcChangeEvent;

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

1714
    case VIR_DOMAIN_EVENT_ID_WATCHDOG:
1715 1716 1717 1718 1719 1720 1721 1722 1723
        {
            virDomainEventWatchdogPtr watchdogEvent;

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

1725
    case VIR_DOMAIN_EVENT_ID_IO_ERROR:
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
        {
            virDomainEventIOErrorPtr ioErrorEvent;

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

1738
    case VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON:
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
        {
            virDomainEventIOErrorPtr ioErrorEvent;

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

1752
    case VIR_DOMAIN_EVENT_ID_GRAPHICS:
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
        {
            virDomainEventGraphicsPtr graphicsEvent;

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

1767 1768 1769
    case VIR_DOMAIN_EVENT_ID_CONTROL_ERROR:
        (cb)(conn, dom,
             cbopaque);
1770
        goto cleanup;
1771

1772
    case VIR_DOMAIN_EVENT_ID_BLOCK_JOB:
1773
    case VIR_DOMAIN_EVENT_ID_BLOCK_JOB_2:
1774 1775 1776 1777 1778
        {
            virDomainEventBlockJobPtr blockJobEvent;

            blockJobEvent = (virDomainEventBlockJobPtr)event;
            ((virConnectDomainEventBlockJobCallback)cb)(conn, dom,
1779
                                                        blockJobEvent->disk,
1780 1781 1782 1783 1784
                                                        blockJobEvent->type,
                                                        blockJobEvent->status,
                                                        cbopaque);
            goto cleanup;
        }
1785

1786
    case VIR_DOMAIN_EVENT_ID_DISK_CHANGE:
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
        {
            virDomainEventDiskChangePtr diskChangeEvent;

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

1800
    case VIR_DOMAIN_EVENT_ID_TRAY_CHANGE:
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
        {
            virDomainEventTrayChangePtr trayChangeEvent;

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

O
Osier Yang 已提交
1812
    case VIR_DOMAIN_EVENT_ID_PMWAKEUP:
E
Eric Blake 已提交
1813 1814 1815 1816 1817 1818 1819 1820
        {
            virDomainEventPMPtr pmEvent = (virDomainEventPMPtr)event;

            ((virConnectDomainEventPMWakeupCallback)cb)(conn, dom,
                                                        pmEvent->reason,
                                                        cbopaque);
            goto cleanup;
        }
O
Osier Yang 已提交
1821

O
Osier Yang 已提交
1822
    case VIR_DOMAIN_EVENT_ID_PMSUSPEND:
E
Eric Blake 已提交
1823 1824 1825 1826 1827 1828 1829 1830
        {
            virDomainEventPMPtr pmEvent = (virDomainEventPMPtr)event;

            ((virConnectDomainEventPMSuspendCallback)cb)(conn, dom,
                                                         pmEvent->reason,
                                                         cbopaque);
            goto cleanup;
        }
O
Osier Yang 已提交
1831

1832
    case VIR_DOMAIN_EVENT_ID_BALLOON_CHANGE:
1833 1834 1835 1836 1837 1838 1839 1840 1841
        {
            virDomainEventBalloonChangePtr balloonChangeEvent;

            balloonChangeEvent = (virDomainEventBalloonChangePtr)event;
            ((virConnectDomainEventBalloonChangeCallback)cb)(conn, dom,
                                                             balloonChangeEvent->actual,
                                                             cbopaque);
            goto cleanup;
        }
1842

1843
    case VIR_DOMAIN_EVENT_ID_PMSUSPEND_DISK:
E
Eric Blake 已提交
1844 1845 1846 1847 1848 1849 1850 1851
        {
            virDomainEventPMPtr pmEvent = (virDomainEventPMPtr)event;

            ((virConnectDomainEventPMSuspendDiskCallback)cb)(conn, dom,
                                                             pmEvent->reason,
                                                             cbopaque);
            goto cleanup;
        }
1852

1853
    case VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED:
1854 1855 1856 1857 1858 1859 1860 1861 1862
        {
            virDomainEventDeviceRemovedPtr deviceRemovedEvent;

            deviceRemovedEvent = (virDomainEventDeviceRemovedPtr)event;
            ((virConnectDomainEventDeviceRemovedCallback)cb)(conn, dom,
                                                             deviceRemovedEvent->devAlias,
                                                             cbopaque);
            goto cleanup;
        }
1863

1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
    case VIR_DOMAIN_EVENT_ID_TUNABLE:
        {
            virDomainEventTunablePtr tunableEvent;
            tunableEvent = (virDomainEventTunablePtr)event;
            ((virConnectDomainEventTunableCallback)cb)(conn, dom,
                                                       tunableEvent->params,
                                                       tunableEvent->nparams,
                                                       cbopaque);
            goto cleanup;
        }

1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
    case VIR_DOMAIN_EVENT_ID_AGENT_LIFECYCLE:
        {
            virDomainEventAgentLifecyclePtr agentLifecycleEvent;
            agentLifecycleEvent = (virDomainEventAgentLifecyclePtr)event;
            ((virConnectDomainEventAgentLifecycleCallback)cb)(conn, dom,
                                                              agentLifecycleEvent->state,
                                                              agentLifecycleEvent->reason,
                                                              cbopaque);
            goto cleanup;
        }

1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
    case VIR_DOMAIN_EVENT_ID_DEVICE_ADDED:
        {
            virDomainEventDeviceAddedPtr deviceAddedEvent;

            deviceAddedEvent = (virDomainEventDeviceAddedPtr)event;
            ((virConnectDomainEventDeviceAddedCallback)cb)(conn, dom,
                                                           deviceAddedEvent->devAlias,
                                                           cbopaque);
            goto cleanup;
        }

1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
    case VIR_DOMAIN_EVENT_ID_MIGRATION_ITERATION:
        {
            virDomainEventMigrationIterationPtr ev;

            ev = (virDomainEventMigrationIterationPtr) event;
            ((virConnectDomainEventMigrationIterationCallback)cb)(conn, dom,
                                                                  ev->iteration,
                                                                  cbopaque);
            goto cleanup;
        }

J
Jiri Denemark 已提交
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
    case VIR_DOMAIN_EVENT_ID_JOB_COMPLETED:
        {
            virDomainEventJobCompletedPtr ev;

            ev = (virDomainEventJobCompletedPtr) event;
            ((virConnectDomainEventJobCompletedCallback) cb)(conn, dom,
                                                             ev->params,
                                                             ev->nparams,
                                                             cbopaque);
            goto cleanup;
        }

1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
    case VIR_DOMAIN_EVENT_ID_DEVICE_REMOVAL_FAILED:
        {
            virDomainEventDeviceRemovalFailedPtr deviceRemovalFailedEvent;

            deviceRemovalFailedEvent = (virDomainEventDeviceRemovalFailedPtr)event;
            ((virConnectDomainEventDeviceRemovalFailedCallback)cb)(conn, dom,
                                                                   deviceRemovalFailedEvent->devAlias,
                                                                   cbopaque);
            goto cleanup;
        }

1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
    case VIR_DOMAIN_EVENT_ID_METADATA_CHANGE:
        {
            virDomainEventMetadataChangePtr metadataChangeEvent;

            metadataChangeEvent = (virDomainEventMetadataChangePtr)event;
            ((virConnectDomainEventMetadataChangeCallback)cb)(conn, dom,
                                                              metadataChangeEvent->type,
                                                              metadataChangeEvent->nsuri,
                                                              cbopaque);
            goto cleanup;
        }

1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
    case VIR_DOMAIN_EVENT_ID_BLOCK_THRESHOLD:
        {
            virDomainEventBlockThresholdPtr blockThresholdEvent;

            blockThresholdEvent = (virDomainEventBlockThresholdPtr)event;
            ((virConnectDomainEventBlockThresholdCallback)cb)(conn, dom,
                                                              blockThresholdEvent->dev,
                                                              blockThresholdEvent->path,
                                                              blockThresholdEvent->threshold,
                                                              blockThresholdEvent->excess,
                                                              cbopaque);
            goto cleanup;
        }
1956
    case VIR_DOMAIN_EVENT_ID_LAST:
1957
        break;
1958
    }
1959

1960
    VIR_WARN("Unexpected event ID %d", event->eventID);
1961

1962
 cleanup:
1963
    virObjectUnref(dom);
1964 1965 1966
}


1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
virObjectEventPtr
virDomainQemuMonitorEventNew(int id,
                             const char *name,
                             const unsigned char *uuid,
                             const char *event,
                             long long seconds,
                             unsigned int micros,
                             const char *details)
{
    virDomainQemuMonitorEventPtr ev;
1977
    char uuidstr[VIR_UUID_STRING_BUFLEN];
1978 1979 1980 1981

    if (virDomainEventsInitialize() < 0)
        return NULL;

1982
    virUUIDFormat(uuid, uuidstr);
1983 1984
    if (!(ev = virObjectEventNew(virDomainQemuMonitorEventClass,
                                 virDomainQemuMonitorEventDispatchFunc,
1985
                                 0, id, name, uuid, uuidstr)))
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
        return NULL;

    /* event is mandatory, details are optional */
    if (VIR_STRDUP(ev->event, event) <= 0)
        goto error;
    ev->seconds = seconds;
    ev->micros = micros;
    if (VIR_STRDUP(ev->details, details) < 0)
        goto error;

    return (virObjectEventPtr)ev;

1998
 error:
1999 2000 2001 2002 2003
    virObjectUnref(ev);
    return NULL;
}


2004 2005 2006 2007 2008 2009
/* In order to filter by event name, we need to store a copy of the
 * name to filter on.  By wrapping the caller's freecb, we can
 * piggyback our cleanup to happen at the same time the caller
 * deregisters.  */
struct virDomainQemuMonitorEventData {
    char *event;
2010
    GRegex *regex;
2011
    unsigned int flags;
2012 2013 2014 2015 2016 2017
    void *opaque;
    virFreeCallback freecb;
};
typedef struct virDomainQemuMonitorEventData virDomainQemuMonitorEventData;


2018 2019 2020 2021 2022 2023
static void
virDomainQemuMonitorEventDispatchFunc(virConnectPtr conn,
                                      virObjectEventPtr event,
                                      virConnectObjectEventGenericCallback cb,
                                      void *cbopaque)
{
2024
    virDomainPtr dom;
2025
    virDomainQemuMonitorEventPtr qemuMonitorEvent;
2026
    virDomainQemuMonitorEventData *data = cbopaque;
2027

2028 2029
    if (!(dom = virGetDomain(conn, event->meta.name,
                             event->meta.uuid, event->meta.id)))
2030 2031 2032 2033 2034 2035 2036 2037
        return;

    qemuMonitorEvent = (virDomainQemuMonitorEventPtr)event;
    ((virConnectDomainQemuMonitorEventCallback)cb)(conn, dom,
                                                   qemuMonitorEvent->event,
                                                   qemuMonitorEvent->seconds,
                                                   qemuMonitorEvent->micros,
                                                   qemuMonitorEvent->details,
2038
                                                   data->opaque);
2039
    virObjectUnref(dom);
2040 2041 2042
}


2043
/**
2044 2045
 * virDomainEventStateRegister:
 * @conn: connection to associate with callback
2046
 * @state: object event state
2047 2048
 * @callback: the callback to add
 * @opaque: data blob to pass to @callback
2049 2050 2051 2052 2053 2054 2055
 * @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
 */
2056 2057
int
virDomainEventStateRegister(virConnectPtr conn,
2058
                            virObjectEventStatePtr state,
2059 2060 2061
                            virConnectDomainEventCallback callback,
                            void *opaque,
                            virFreeCallback freecb)
2062
{
2063 2064
    int callbackID;

2065 2066 2067
    if (virDomainEventsInitialize() < 0)
        return -1;

2068
    return virObjectEventStateRegisterID(conn, state, NULL,
2069
                                         NULL, NULL, virDomainEventClass,
2070 2071
                                         VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                         VIR_OBJECT_EVENT_CALLBACK(callback),
2072 2073
                                         opaque, freecb,
                                         true, &callbackID, false);
2074 2075 2076 2077 2078 2079
}


/**
 * virDomainEventStateRegisterID:
 * @conn: connection to associate with callback
2080
 * @state: object event state
2081
 * @dom: optional domain for filtering the event
2082
 * @eventID: ID of the event type to register for
2083 2084
 * @cb: function to invoke when event fires
 * @opaque: data blob to pass to @callback
2085 2086 2087
 * @freecb: callback to free @opaque
 * @callbackID: filled with callback ID
 *
2088 2089 2090
 * Register the function @cb with connection @conn, from @state, for
 * events of type @eventID, and return the registration handle in
 * @callbackID.
2091 2092 2093
 *
 * Returns: the number of callbacks now registered, or -1 on error
 */
2094 2095
int
virDomainEventStateRegisterID(virConnectPtr conn,
2096
                              virObjectEventStatePtr state,
2097 2098 2099 2100 2101 2102
                              virDomainPtr dom,
                              int eventID,
                              virConnectDomainEventGenericCallback cb,
                              void *opaque,
                              virFreeCallback freecb,
                              int *callbackID)
2103
{
C
Cole Robinson 已提交
2104 2105
    char uuidstr[VIR_UUID_STRING_BUFLEN];

2106 2107 2108
    if (virDomainEventsInitialize() < 0)
        return -1;

C
Cole Robinson 已提交
2109 2110 2111
    if (dom)
        virUUIDFormat(dom->uuid, uuidstr);
    return virObjectEventStateRegisterID(conn, state, dom ? uuidstr : NULL,
2112 2113
                                         NULL, NULL,
                                         virDomainEventClass, eventID,
2114
                                         VIR_OBJECT_EVENT_CALLBACK(cb),
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151
                                         opaque, freecb,
                                         false, callbackID, false);
}


/**
 * virDomainEventStateRegisterClient:
 * @conn: connection to associate with callback
 * @state: object event state
 * @dom: optional domain for filtering the event
 * @eventID: ID of the event type to register for
 * @cb: function to invoke when event fires
 * @opaque: data blob to pass to @callback
 * @freecb: callback to free @opaque
 * @legacy: true if callback is tracked by function instead of callbackID
 * @callbackID: filled with callback ID
 * @remoteID: true if server supports filtering
 *
 * Register the function @cb with connection @conn, from @state, for
 * events of type @eventID, and return the registration handle in
 * @callbackID.  This version is intended for use on the client side
 * of RPC.
 *
 * Returns: the number of callbacks now registered, or -1 on error
 */
int
virDomainEventStateRegisterClient(virConnectPtr conn,
                                  virObjectEventStatePtr state,
                                  virDomainPtr dom,
                                  int eventID,
                                  virConnectDomainEventGenericCallback cb,
                                  void *opaque,
                                  virFreeCallback freecb,
                                  bool legacy,
                                  int *callbackID,
                                  bool remoteID)
{
C
Cole Robinson 已提交
2152 2153
    char uuidstr[VIR_UUID_STRING_BUFLEN];

2154 2155 2156
    if (virDomainEventsInitialize() < 0)
        return -1;

C
Cole Robinson 已提交
2157 2158 2159
    if (dom)
        virUUIDFormat(dom->uuid, uuidstr);
    return virObjectEventStateRegisterID(conn, state, dom ? uuidstr : NULL,
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
                                         NULL, NULL,
                                         virDomainEventClass, eventID,
                                         VIR_OBJECT_EVENT_CALLBACK(cb),
                                         opaque, freecb,
                                         legacy, callbackID, remoteID);
}


/**
 * virDomainEventStateCallbackID:
 * @conn: connection associated with callback
 * @state: object event state
 * @cb: function registered as a callback with virDomainEventStateRegister()
 * @remoteID: associated remote id of the callback
 *
 * Returns the callbackID of @cb, or -1 with an error issued if the
 * function is not currently registered.
 */
int
virDomainEventStateCallbackID(virConnectPtr conn,
                              virObjectEventStatePtr state,
                              virConnectDomainEventCallback cb,
                              int *remoteID)
{
    return virObjectEventStateCallbackID(conn, state, virDomainEventClass,
                                         VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                         VIR_OBJECT_EVENT_CALLBACK(cb),
                                         remoteID);
2188 2189 2190 2191 2192
}


/**
 * virDomainEventStateDeregister:
2193
 * @conn: connection to associate with callback
2194
 * @state: object event state
2195
 * @cb: function to remove from event
2196
 *
2197 2198
 * Unregister the function @cb with connection @conn, from @state, for
 * lifecycle events.
2199 2200 2201
 *
 * Returns: the number of lifecycle callbacks still registered, or -1 on error
 */
2202 2203
int
virDomainEventStateDeregister(virConnectPtr conn,
2204
                              virObjectEventStatePtr state,
2205
                              virConnectDomainEventCallback cb)
2206
{
2207
    int callbackID;
2208

2209 2210 2211
    callbackID = virObjectEventStateCallbackID(conn, state,
                                               virDomainEventClass,
                                               VIR_DOMAIN_EVENT_ID_LIFECYCLE,
2212 2213
                                               VIR_OBJECT_EVENT_CALLBACK(cb),
                                               NULL);
2214 2215
    if (callbackID < 0)
        return -1;
2216
    return virObjectEventStateDeregisterID(conn, state, callbackID, true);
2217
}
2218 2219


2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
/**
 * virDomainQemuMonitorEventFilter:
 * @conn: the connection pointer
 * @event: the event about to be dispatched
 * @opaque: the opaque data registered with the filter
 *
 * Callback for filtering based on event names.  Returns true if the
 * event should be dispatched.
 */
static bool
J
Ján Tomko 已提交
2230
virDomainQemuMonitorEventFilter(virConnectPtr conn G_GNUC_UNUSED,
2231 2232 2233 2234 2235 2236 2237 2238
                                virObjectEventPtr event,
                                void *opaque)
{
    virDomainQemuMonitorEventData *data = opaque;
    virDomainQemuMonitorEventPtr monitorEvent;

    monitorEvent = (virDomainQemuMonitorEventPtr) event;

2239 2240 2241
    if (data->flags == -1)
        return true;
    if (data->flags & VIR_CONNECT_DOMAIN_QEMU_MONITOR_EVENT_REGISTER_REGEX)
2242
        return g_regex_match(data->regex, monitorEvent->event, 0, NULL) == TRUE;
2243 2244
    if (data->flags & VIR_CONNECT_DOMAIN_QEMU_MONITOR_EVENT_REGISTER_NOCASE)
        return STRCASEEQ(monitorEvent->event, data->event);
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
    return STREQ(monitorEvent->event, data->event);
}


static void
virDomainQemuMonitorEventCleanup(void *opaque)
{
    virDomainQemuMonitorEventData *data = opaque;

    VIR_FREE(data->event);
2255
    if (data->flags & VIR_CONNECT_DOMAIN_QEMU_MONITOR_EVENT_REGISTER_REGEX)
2256
        g_regex_unref(data->regex);
2257 2258 2259 2260 2261 2262
    if (data->freecb)
        (data->freecb)(data->opaque);
    VIR_FREE(data);
}


2263 2264 2265 2266 2267 2268 2269 2270 2271
/**
 * virDomainQemuMonitorEventStateRegisterID:
 * @conn: connection to associate with callback
 * @state: object event state
 * @dom: optional domain where event must occur
 * @event: optional name of event to register for
 * @cb: function to invoke when event occurs
 * @opaque: data blob to pass to callback
 * @freecb: callback to free @opaque
2272 2273
 * @flags: -1 for client, valid virConnectDomainQemuMonitorEventRegisterFlags
 *         for server
2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291
 * @callbackID: filled with callback ID
 *
 * Register the function @cb with connection @conn, from @state, for
 * events of type @eventID.
 *
 * Returns: the number of callbacks now registered, or -1 on error
 */
int
virDomainQemuMonitorEventStateRegisterID(virConnectPtr conn,
                                         virObjectEventStatePtr state,
                                         virDomainPtr dom,
                                         const char *event,
                                         virConnectDomainQemuMonitorEventCallback cb,
                                         void *opaque,
                                         virFreeCallback freecb,
                                         unsigned int flags,
                                         int *callbackID)
{
2292 2293
    virDomainQemuMonitorEventData *data = NULL;
    virObjectEventCallbackFilter filter = NULL;
C
Cole Robinson 已提交
2294
    char uuidstr[VIR_UUID_STRING_BUFLEN];
2295

2296 2297 2298 2299
    if (virDomainEventsInitialize() < 0)
        return -1;

    if (flags != -1)
2300 2301 2302
        virCheckFlags(VIR_CONNECT_DOMAIN_QEMU_MONITOR_EVENT_REGISTER_REGEX |
                      VIR_CONNECT_DOMAIN_QEMU_MONITOR_EVENT_REGISTER_NOCASE,
                      -1);
2303 2304
    if (VIR_ALLOC(data) < 0)
        return -1;
2305
    data->flags = flags;
2306
    if (event && flags != -1) {
2307
        if (flags & VIR_CONNECT_DOMAIN_QEMU_MONITOR_EVENT_REGISTER_REGEX) {
2308 2309
            int cflags = G_REGEX_OPTIMIZE;
            g_autoptr(GError) err = NULL;
2310

2311 2312 2313 2314
            if (flags & VIR_CONNECT_DOMAIN_QEMU_MONITOR_EVENT_REGISTER_NOCASE)
                cflags |= G_REGEX_CASELESS;
            data->regex = g_regex_new(event, cflags, 0, &err);
            if (!data->regex) {
2315 2316
                virReportError(VIR_ERR_INVALID_ARG,
                               _("failed to compile regex '%s': %s"),
2317
                               event, err->message);
2318 2319 2320 2321 2322 2323 2324
                VIR_FREE(data);
                return -1;
            }
        } else if (VIR_STRDUP(data->event, event) < 0) {
            VIR_FREE(data);
            return -1;
        }
2325
    }
2326 2327 2328 2329 2330
    data->opaque = opaque;
    data->freecb = freecb;
    if (event)
        filter = virDomainQemuMonitorEventFilter;
    freecb = virDomainQemuMonitorEventCleanup;
2331

C
Cole Robinson 已提交
2332 2333 2334
    if (dom)
        virUUIDFormat(dom->uuid, uuidstr);
    return virObjectEventStateRegisterID(conn, state, dom ? uuidstr : NULL,
2335
                                         filter, data,
2336 2337
                                         virDomainQemuMonitorEventClass, 0,
                                         VIR_OBJECT_EVENT_CALLBACK(cb),
2338
                                         data, freecb,
2339 2340
                                         false, callbackID, false);
}