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 testNetworkDefine(const void *data) { const objecteventTest *test = data; lifecycleEventCounter counter; virNetworkPtr net; int id; int ret = 0; lifecycleEventCounter_reset(&counter); id = virConnectNetworkEventRegisterAny(test->conn, NULL, VIR_NETWORK_EVENT_ID_LIFECYCLE, VIR_NETWORK_EVENT_CALLBACK(&networkLifecycleCb), &counter, NULL); /* Make sure the define event is triggered */ net = virNetworkDefineXML(test->conn, networkDef); if (virEventRunDefaultImpl() < 0) { ret = -1; goto cleanup; } if (counter.defineEvents != 1 || counter.unexpectedEvents > 0) { ret = -1; goto cleanup; } /* Make sure the undefine event is triggered */ virNetworkUndefine(net); if (virEventRunDefaultImpl() < 0) { ret = -1; goto cleanup; } if (counter.undefineEvents != 1 || counter.unexpectedEvents > 0) { ret = -1; goto cleanup; } cleanup: virConnectNetworkEventDeregisterAny(test->conn, id); virNetworkFree(net); return ret; }
static int testNetworkStartStopEvent(const void *data) { const objecteventTest *test = data; lifecycleEventCounter counter; int id; int ret = 0; lifecycleEventCounter_reset(&counter); id = virConnectNetworkEventRegisterAny(test->conn, test->net, VIR_NETWORK_EVENT_ID_LIFECYCLE, VIR_NETWORK_EVENT_CALLBACK(&networkLifecycleCb), &counter, NULL); virNetworkCreate(test->net); virNetworkDestroy(test->net); if (virEventRunDefaultImpl() < 0) { ret = -1; goto cleanup; } if (counter.startEvents != 1 || counter.stopEvents != 1 || counter.unexpectedEvents > 0) { ret = -1; goto cleanup; } cleanup: virConnectNetworkEventDeregisterAny(test->conn, id); 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 void qemuMonitorTestWorker(void *opaque) { qemuMonitorTestPtr test = opaque; virMutexLock(&test->lock); while (!test->quit) { virMutexUnlock(&test->lock); if (virEventRunDefaultImpl() < 0) { test->quit = true; break; } virMutexLock(&test->lock); } test->running = false; virMutexUnlock(&test->lock); return; }
static int testDomainCreateXMLOld(const void *data) { const objecteventTest *test = data; lifecycleEventCounter counter; virDomainPtr dom = NULL; int ret = -1; bool registered = false; lifecycleEventCounter_reset(&counter); if (virConnectDomainEventRegister(test->conn, domainLifecycleCb, &counter, NULL) != 0) goto cleanup; registered = true; dom = virDomainCreateXML(test->conn, domainDef, 0); if (dom == NULL || virEventRunDefaultImpl() < 0) goto cleanup; if (counter.startEvents != 1 || counter.unexpectedEvents > 0) goto cleanup; if (virConnectDomainEventDeregister(test->conn, domainLifecycleCb) != 0) goto cleanup; registered = false; ret = 0; cleanup: if (registered) virConnectDomainEventDeregister(test->conn, domainLifecycleCb); 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; }
/** * lxcControllerMain * @serverFd: server socket fd to accept client requests * @clientFd: initial client which is the libvirtd daemon * @hostFd: open fd for application facing Pty * @contFd: open fd for container facing Pty * * Processes I/O on consoles and the monitor * * Returns 0 on success or -1 in case of error */ static int lxcControllerMain(int serverFd, int clientFd, int *hostFds, int *contFds, size_t nFds, pid_t container) { struct lxcConsole *consoles; struct lxcMonitor monitor = { .serverFd = serverFd, .clientFd = clientFd, }; virErrorPtr err; int rc = -1; size_t i; if (virMutexInit(&lock) < 0) goto cleanup2; if (pipe2(sigpipe, O_CLOEXEC|O_NONBLOCK) < 0) { virReportSystemError(errno, "%s", _("Cannot create signal pipe")); goto cleanup; } if (virEventAddHandle(sigpipe[0], VIR_EVENT_HANDLE_READABLE, lxcSignalChildIO, &container, NULL) < 0) { lxcError(VIR_ERR_INTERNAL_ERROR, "%s", _("Unable to watch signal pipe")); goto cleanup; } if (signal(SIGCHLD, lxcSignalChildHandler) == SIG_ERR) { virReportSystemError(errno, "%s", _("Cannot install signal handler")); goto cleanup; } VIR_DEBUG("serverFd=%d clientFd=%d", serverFd, clientFd); virResetLastError(); if ((monitor.serverWatch = virEventAddHandle(monitor.serverFd, VIR_EVENT_HANDLE_READABLE, lxcServerAccept, &monitor, NULL)) < 0) { lxcError(VIR_ERR_INTERNAL_ERROR, "%s", _("Unable to watch monitor socket")); goto cleanup; } if (monitor.clientFd != -1 && (monitor.clientWatch = virEventAddHandle(monitor.clientFd, VIR_EVENT_HANDLE_READABLE, lxcClientIO, &monitor, NULL)) < 0) { lxcError(VIR_ERR_INTERNAL_ERROR, "%s", _("Unable to watch client socket")); goto cleanup; } if (VIR_ALLOC_N(consoles, nFds) < 0) { virReportOOMError(); goto cleanup; } for (i = 0 ; i < nFds ; i++) { consoles[i].hostFd = hostFds[i]; consoles[i].contFd = contFds[i]; if ((consoles[i].hostWatch = virEventAddHandle(consoles[i].hostFd, VIR_EVENT_HANDLE_READABLE, lxcConsoleIO, &consoles[i], NULL)) < 0) { lxcError(VIR_ERR_INTERNAL_ERROR, "%s", _("Unable to watch host console PTY")); goto cleanup; } if ((consoles[i].contWatch = virEventAddHandle(consoles[i].contFd, VIR_EVENT_HANDLE_READABLE, lxcConsoleIO, &consoles[i], NULL)) < 0) { lxcError(VIR_ERR_INTERNAL_ERROR, "%s", _("Unable to watch host console PTY")); goto cleanup; } } virMutexLock(&lock); while (!quit) { virMutexUnlock(&lock); if (virEventRunDefaultImpl() < 0) goto cleanup; virMutexLock(&lock); } virMutexUnlock(&lock); err = virGetLastError(); if (!err || err->code == VIR_ERR_OK) rc = 0; cleanup: virMutexDestroy(&lock); signal(SIGCHLD, SIG_DFL); cleanup2: VIR_FORCE_CLOSE(monitor.serverFd); VIR_FORCE_CLOSE(monitor.clientFd); VIR_FREE(consoles); return rc; }