void ConnAliveThread::registerConnEvents() { int ret = virConnectRegisterCloseCallback( *ptr_ConnPtr, connEventCallBack, this, // don't register freeData, because it remove this thread NULL); closeCallbackRegistered = !(ret<0); if (ret<0) sendConnErrors(); domainsLifeCycleCallback = virConnectDomainEventRegisterAny( *ptr_ConnPtr, NULL, // set domainsLifeCycleCallback signature VIR_DOMAIN_EVENT_ID_LIFECYCLE, VIR_DOMAIN_EVENT_CALLBACK(domEventCallback), this, // don't register freeData, because it remove this thread NULL); if (ret<0) sendConnErrors(); networkLifeCycleCallback = virConnectNetworkEventRegisterAny( *ptr_ConnPtr, NULL, // set networksLifeCycleCallback signature VIR_NETWORK_EVENT_ID_LIFECYCLE, VIR_NETWORK_EVENT_CALLBACK(netEventCallback), this, // don't register freeData, because it remove this thread NULL); if (ret<0) sendConnErrors(); }
/** * virDomainEventCallbackListAdd: * @conn: pointer to the connection * @cbList: the list * @callback: the callback to add * @opaque: opaque data tio pass to callback * * Internal function to add a callback from a virDomainEventCallbackListPtr */ int virDomainEventCallbackListAdd(virConnectPtr conn, virDomainEventCallbackListPtr cbList, virConnectDomainEventCallback callback, void *opaque, virFreeCallback freecb) { return virDomainEventCallbackListAddID(conn, cbList, NULL, VIR_DOMAIN_EVENT_ID_LIFECYCLE, VIR_DOMAIN_EVENT_CALLBACK(callback), opaque, freecb); }
static int testDomainDefine(const void *data) { const objecteventTest *test = data; lifecycleEventCounter counter; int eventId = VIR_DOMAIN_EVENT_ID_LIFECYCLE; virDomainPtr dom = NULL; int id; int ret = 0; lifecycleEventCounter_reset(&counter); id = virConnectDomainEventRegisterAny(test->conn, NULL, eventId, VIR_DOMAIN_EVENT_CALLBACK(&domainLifecycleCb), &counter, NULL); /* Make sure the define event is triggered */ dom = virDomainDefineXML(test->conn, domainDef); if (dom == NULL || virEventRunDefaultImpl() < 0) { ret = -1; goto cleanup; } if (counter.defineEvents != 1 || counter.unexpectedEvents > 0) { ret = -1; goto cleanup; } /* Make sure the undefine event is triggered */ virDomainUndefine(dom); if (virEventRunDefaultImpl() < 0) { ret = -1; goto cleanup; } if (counter.undefineEvents != 1 || counter.unexpectedEvents > 0) { ret = -1; goto cleanup; } cleanup: virConnectDomainEventDeregisterAny(test->conn, id); if (dom != NULL) virDomainFree(dom); return ret; }
int virDomainEventCallbackListMarkDelete(virConnectPtr conn, virDomainEventCallbackListPtr cbList, virConnectDomainEventCallback callback) { int i; for (i = 0 ; i < cbList->count ; i++) { if (cbList->callbacks[i]->cb == VIR_DOMAIN_EVENT_CALLBACK(callback) && cbList->callbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE && cbList->callbacks[i]->conn == conn) { cbList->callbacks[i]->deleted = 1; return 0; } } eventReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("could not find event callback for deletion")); return -1; }
/** * virDomainEventCallbackListRemove: * @conn: pointer to the connection * @cbList: the list * @callback: the callback to remove * * Internal function to remove a callback from a virDomainEventCallbackListPtr */ static int virDomainEventCallbackListRemove(virConnectPtr conn, virDomainEventCallbackListPtr cbList, virConnectDomainEventCallback callback) { int ret = 0; int i; for (i = 0 ; i < cbList->count ; i++) { if (cbList->callbacks[i]->cb == VIR_DOMAIN_EVENT_CALLBACK(callback) && cbList->callbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE && cbList->callbacks[i]->conn == conn) { virFreeCallback freecb = cbList->callbacks[i]->freecb; if (freecb) (*freecb)(cbList->callbacks[i]->opaque); virUnrefConnect(cbList->callbacks[i]->conn); VIR_FREE(cbList->callbacks[i]); if (i < (cbList->count - 1)) memmove(cbList->callbacks + i, cbList->callbacks + i + 1, sizeof(*(cbList->callbacks)) * (cbList->count - (i + 1))); if (VIR_REALLOC_N(cbList->callbacks, cbList->count - 1) < 0) { ; /* Failure to reduce memory allocation isn't fatal */ } cbList->count--; for (i = 0 ; i < cbList->count ; i++) { if (!cbList->callbacks[i]->deleted) ret++; } return ret; } } eventReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("could not find event callback for removal")); return -1; }
static int testDomainCreateXMLNew(const void *data) { const objecteventTest *test = data; lifecycleEventCounter counter; int eventId = VIR_DOMAIN_EVENT_ID_LIFECYCLE; virDomainPtr dom = NULL; int id; int ret = -1; lifecycleEventCounter_reset(&counter); id = virConnectDomainEventRegisterAny(test->conn, NULL, eventId, VIR_DOMAIN_EVENT_CALLBACK(&domainLifecycleCb), &counter, NULL); if (id < 0) goto cleanup; dom = virDomainCreateXML(test->conn, domainDef, 0); if (dom == NULL || virEventRunDefaultImpl() < 0) goto cleanup; if (counter.startEvents != 1 || counter.unexpectedEvents > 0) goto cleanup; if (virConnectDomainEventDeregisterAny(test->conn, id) != 0) goto cleanup; id = -1; ret = 0; cleanup: if (id >= 0) virConnectDomainEventDeregisterAny(test->conn, id); if (dom) { virDomainDestroy(dom); virDomainFree(dom); } return ret; }
/** * virDomainEventCallbackListAddID: * @conn: pointer to the connection * @cbList: the list * @eventID: the event ID * @callback: the callback to add * @opaque: opaque data tio pass to callback * * Internal function to add a callback from a virDomainEventCallbackListPtr */ int virDomainEventCallbackListAddID(virConnectPtr conn, virDomainEventCallbackListPtr cbList, virDomainPtr dom, int eventID, virConnectDomainEventGenericCallback callback, void *opaque, virFreeCallback freecb) { virDomainEventCallbackPtr event; int i; /* Check incoming */ if ( !cbList ) { return -1; } /* check if we already have this callback on our list */ for (i = 0 ; i < cbList->count ; i++) { if (cbList->callbacks[i]->cb == VIR_DOMAIN_EVENT_CALLBACK(callback) && cbList->callbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE && cbList->callbacks[i]->conn == conn) { eventReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("event callback already tracked")); return -1; } } /* Allocate new event */ if (VIR_ALLOC(event) < 0) goto no_memory; event->conn = conn; event->cb = callback; event->eventID = eventID; event->opaque = opaque; event->freecb = freecb; if (dom) { if (VIR_ALLOC(event->dom) < 0) goto no_memory; if (!(event->dom->name = strdup(dom->name))) goto no_memory; memcpy(event->dom->uuid, dom->uuid, VIR_UUID_BUFLEN); event->dom->id = dom->id; } /* Make space on list */ if (VIR_REALLOC_N(cbList->callbacks, cbList->count + 1) < 0) goto no_memory; event->conn->refs++; cbList->callbacks[cbList->count] = event; cbList->count++; event->callbackID = cbList->nextID++; return event->callbackID; no_memory: virReportOOMError(); if (event) { if (event->dom) VIR_FREE(event->dom->name); VIR_FREE(event->dom); } VIR_FREE(event); return -1; }
static int testDomainStartStopEvent(const void *data) { const objecteventTest *test = data; lifecycleEventCounter counter; int eventId = VIR_DOMAIN_EVENT_ID_LIFECYCLE; int id; int ret = -1; virDomainPtr dom; virConnectPtr conn2 = NULL; virDomainPtr dom2 = NULL; lifecycleEventCounter_reset(&counter); dom = virDomainLookupByName(test->conn, "test"); if (dom == NULL) return -1; id = virConnectDomainEventRegisterAny(test->conn, dom, eventId, VIR_DOMAIN_EVENT_CALLBACK(&domainLifecycleCb), &counter, NULL); /* Test domain is started */ virDomainDestroy(dom); virDomainCreate(dom); if (virEventRunDefaultImpl() < 0) goto cleanup; if (counter.startEvents != 1 || counter.stopEvents != 1 || counter.unexpectedEvents > 0) goto cleanup; /* Repeat the test, but this time, trigger the events via an * alternate connection. */ if (!(conn2 = virConnectOpen("test:///default"))) goto cleanup; if (!(dom2 = virDomainLookupByName(conn2, "test"))) goto cleanup; if (virDomainDestroy(dom2) < 0) goto cleanup; if (virDomainCreate(dom2) < 0) goto cleanup; if (virEventRunDefaultImpl() < 0) goto cleanup; if (counter.startEvents != 2 || counter.stopEvents != 2 || counter.unexpectedEvents > 0) goto cleanup; ret = 0; cleanup: virConnectDomainEventDeregisterAny(test->conn, id); virDomainFree(dom); if (dom2) virDomainFree(dom2); if (conn2) virConnectClose(conn2); return ret; }
static int testDomainCreateXMLMixed(const void *data) { const objecteventTest *test = data; lifecycleEventCounter counter; virDomainPtr dom; int ret = -1; int id1 = -1; int id2 = -1; bool registered = false; lifecycleEventCounter_reset(&counter); /* Fun with mixing old and new API, also with global and * per-domain. Handler should be fired three times, once for each * registration. */ dom = virDomainDefineXML(test->conn, domainDef); if (dom == NULL) goto cleanup; id1 = virConnectDomainEventRegisterAny(test->conn, dom, VIR_DOMAIN_EVENT_ID_LIFECYCLE, VIR_DOMAIN_EVENT_CALLBACK(&domainLifecycleCb), &counter, NULL); if (id1 < 0) goto cleanup; if (virConnectDomainEventRegister(test->conn, domainLifecycleCb, &counter, NULL) != 0) goto cleanup; registered = true; id2 = virConnectDomainEventRegisterAny(test->conn, NULL, VIR_DOMAIN_EVENT_ID_LIFECYCLE, VIR_DOMAIN_EVENT_CALLBACK(&domainLifecycleCb), &counter, NULL); if (id2 < 0) goto cleanup; dom = virDomainCreateXML(test->conn, domainDef, 0); if (dom == NULL || virEventRunDefaultImpl() < 0) goto cleanup; if (counter.startEvents != 3 || counter.unexpectedEvents > 0) goto cleanup; if (virConnectDomainEventDeregister(test->conn, domainLifecycleCb) != 0) goto cleanup; registered = false; if (virConnectDomainEventDeregisterAny(test->conn, id1) != 0) goto cleanup; id1 = -1; if (virConnectDomainEventDeregisterAny(test->conn, id2) != 0) goto cleanup; id2 = -1; ret = 0; cleanup: if (id1 >= 0) virConnectDomainEventDeregisterAny(test->conn, id1); if (id2 >= 0) virConnectDomainEventDeregisterAny(test->conn, id2); if (registered) virConnectDomainEventDeregister(test->conn, domainLifecycleCb); if (dom != NULL) { virDomainUndefine(dom); virDomainDestroy(dom); virDomainFree(dom); } return ret; }