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