Register for, and dispatch domain event callbacks qemu_conf.h | 3 ++ qemu_driver.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 2 deletions(-)
diff --git a/src/qemu_conf.h b/src/qemu_conf.h index cfd7d35..d06c4d7 100644 --- a/src/qemu_conf.h +++ b/src/qemu_conf.h @@ -63,6 +63,9 @@ struct qemud_driver { char *vncListen; virCapsPtr caps; + + /* An array of callbacks */ + virDomainEventCallbackListPtr domainEventCallbacks; }; diff --git a/src/qemu_driver.c b/src/qemu_driver.c index a86b787..9792541 100644 --- a/src/qemu_driver.c +++ b/src/qemu_driver.c @@ -159,6 +159,10 @@ qemudStartup(void) { /* Don't have a dom0 so start from 1 */ qemu_driver->nextvmid = 1; + /* Init callback list */ + if(VIR_ALLOC(qemu_driver->domainEventCallbacks) < 0) + return -1; + if (!uid) { if (asprintf(&qemu_driver->logDir, "%s/log/libvirt/qemu", LOCAL_STATE_DIR) == -1) @@ -301,6 +305,9 @@ qemudShutdown(void) { VIR_FREE(qemu_driver->autostartDir); VIR_FREE(qemu_driver->vncTLSx509certdir); + /* Free domain callback list */ + virDomainEventCallbackListFree(qemu_driver->domainEventCallbacks); + if (qemu_driver->brctl) brShutdown(qemu_driver->brctl); @@ -742,6 +749,8 @@ static int qemudNextFreeVNCPort(struct qemud_driver *driver ATTRIBUTE_UNUSED) { return -1; } +static virDomainPtr qemudDomainLookupByName(virConnectPtr conn, const char *name); + static int qemudStartVMDaemon(virConnectPtr conn, struct qemud_driver *driver, virDomainObjPtr vm, @@ -756,6 +765,7 @@ static int qemudStartVMDaemon(virConnectPtr conn, unsigned int qemuCmdFlags; fd_set keepfd; const char *emulator; + virDomainPtr dom; FD_ZERO(&keepfd); @@ -918,6 +928,11 @@ static int qemudStartVMDaemon(virConnectPtr conn, qemudShutdownVMDaemon(conn, driver, vm); return -1; } + dom = qemudDomainLookupByName(conn,vm->def->name); + if(dom) + virDispatchDomainEvent(dom, VIR_DOMAIN_EVENT_STARTED); + else + DEBUG0("Warning - dom is NULL at domain start"); } return ret; @@ -1503,6 +1518,7 @@ static int qemudDomainSuspend(virDomainPtr dom) { } vm->state = VIR_DOMAIN_PAUSED; qemudDebug("Reply %s", info); + virDispatchDomainEvent(dom, VIR_DOMAIN_EVENT_SUSPENDED); VIR_FREE(info); return 0; } @@ -1531,6 +1547,7 @@ static int qemudDomainResume(virDomainPtr dom) { } vm->state = VIR_DOMAIN_RUNNING; qemudDebug("Reply %s", info); + virDispatchDomainEvent(dom, VIR_DOMAIN_EVENT_RESUMED); VIR_FREE(info); return 0; } @@ -1572,7 +1589,7 @@ static int qemudDomainDestroy(virDomainPtr dom) { if (!vm->persistent) virDomainRemoveInactive(&driver->domains, vm); - + virDispatchDomainEvent(dom, VIR_DOMAIN_EVENT_STOPPED); return 0; } @@ -1903,7 +1920,7 @@ static int qemudDomainSave(virDomainPtr dom, if (!vm->persistent) virDomainRemoveInactive(&driver->domains, vm); - + virDispatchDomainEvent(dom, VIR_DOMAIN_EVENT_SAVED); return 0; } @@ -2104,6 +2121,7 @@ static int qemudDomainRestore(virConnectPtr conn, struct qemud_driver *driver = (struct qemud_driver *)conn->privateData; virDomainDefPtr def; virDomainObjPtr vm; + virDomainPtr dom; int fd; int ret; char *xml; @@ -2210,6 +2228,11 @@ static int qemudDomainRestore(virConnectPtr conn, vm->state = VIR_DOMAIN_RUNNING; } + dom = virDomainLookupByID(conn, def->id); + if(dom) { + virDispatchDomainEvent(dom, VIR_DOMAIN_EVENT_RESTORED); + VIR_FREE(dom); + } return 0; } @@ -3051,6 +3074,52 @@ done: } +static int qemudDomainEventRegister (virConnectPtr conn, + void *callback, + void *opaque) +{ + struct qemud_driver *driver = (struct qemud_driver *)conn->privateData; + + return virDomainEventCallbackListAdd(conn, driver->domainEventCallbacks, callback, opaque); +} + +static int qemudDomainEventDeregister (virConnectPtr conn, + void *callback) +{ + struct qemud_driver *driver = (struct qemud_driver *)conn->privateData; + + return virDomainEventCallbackListRemove(conn, driver->domainEventCallbacks, callback); +} + +static void qemudDomainEventDispatch (virDomainPtr dom, + virDomainEventType evt) +{ + int i; + struct qemud_driver *driver; + virDomainEventCallbackListPtr cbList; + + if(!dom->conn) { + DEBUG0("Invalid conn"); + return; + } + driver = (struct qemud_driver *)dom->conn->privateData; + + if(!driver) { + DEBUG0("Invalid driver"); + return; + } + cbList = driver->domainEventCallbacks; + + for(i=0;i<cbList->count;i++) { + if(cbList->callbacks[i] && cbList->callbacks[i]->cb) + DEBUG("Dispatching callback %p %p event %d", cbList->callbacks[i], cbList->callbacks[i]->cb, evt); + cbList->callbacks[i]->cb(cbList->callbacks[i]->conn, + dom, evt, + cbList->callbacks[i]->opaque); + } + +} + static virDriver qemuDriver = { VIR_DRV_QEMU, "QEMU", @@ -3121,6 +3190,9 @@ static virDriver qemuDriver = { NULL, /* nodeGetCellsFreeMemory */ NULL, /* getFreeMemory */ #endif + qemudDomainEventRegister, /* domainEventRegister */ + qemudDomainEventDeregister, /* domainEventDeregister */ + qemudDomainEventDispatch, /* domainEventDispatch */ };
-- Libvir-list mailing list Libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list