提交 e4b07fd6 编写于 作者: D Daniel P. Berrange

Switch LXC driver to use a private data blob for virDomainObj state

* src/lxc/lxc_driver.c: Introduce lxcDomainObjPrivate for storing
  monitor device state
上级 f248c965
...@@ -53,6 +53,14 @@ ...@@ -53,6 +53,14 @@
#define VIR_FROM_THIS VIR_FROM_LXC #define VIR_FROM_THIS VIR_FROM_LXC
typedef struct _lxcDomainObjPrivate lxcDomainObjPrivate;
typedef lxcDomainObjPrivate *lxcDomainObjPrivatePtr;
struct _lxcDomainObjPrivate {
int monitor;
int monitorWatch;
};
static int lxcStartup(int privileged); static int lxcStartup(int privileged);
static int lxcShutdown(void); static int lxcShutdown(void);
static lxc_driver_t *lxc_driver = NULL; static lxc_driver_t *lxc_driver = NULL;
...@@ -68,6 +76,27 @@ static void lxcDriverUnlock(lxc_driver_t *driver) ...@@ -68,6 +76,27 @@ static void lxcDriverUnlock(lxc_driver_t *driver)
virMutexUnlock(&driver->lock); virMutexUnlock(&driver->lock);
} }
static void *lxcDomainObjPrivateAlloc(void)
{
lxcDomainObjPrivatePtr priv;
if (VIR_ALLOC(priv) < 0)
return NULL;
priv->monitor = -1;
priv->monitorWatch = -1;
return priv;
}
static void lxcDomainObjPrivateFree(void *data)
{
lxcDomainObjPrivatePtr priv = data;
VIR_FREE(priv);
}
static void lxcDomainEventFlush(int timer, void *opaque); static void lxcDomainEventFlush(int timer, void *opaque);
static void lxcDomainEventQueue(lxc_driver_t *driver, static void lxcDomainEventQueue(lxc_driver_t *driver,
virDomainEventPtr event); virDomainEventPtr event);
...@@ -671,6 +700,7 @@ static int lxcVmCleanup(virConnectPtr conn, ...@@ -671,6 +700,7 @@ static int lxcVmCleanup(virConnectPtr conn,
int childStatus = -1; int childStatus = -1;
virCgroupPtr cgroup; virCgroupPtr cgroup;
int i; int i;
lxcDomainObjPrivatePtr priv = vm->privateData;
while (((waitRc = waitpid(vm->pid, &childStatus, 0)) == -1) && while (((waitRc = waitpid(vm->pid, &childStatus, 0)) == -1) &&
errno == EINTR) errno == EINTR)
...@@ -689,8 +719,8 @@ static int lxcVmCleanup(virConnectPtr conn, ...@@ -689,8 +719,8 @@ static int lxcVmCleanup(virConnectPtr conn,
DEBUG("container exited with rc: %d", rc); DEBUG("container exited with rc: %d", rc);
} }
virEventRemoveHandle(vm->monitorWatch); virEventRemoveHandle(priv->monitorWatch);
close(vm->monitor); close(priv->monitor);
virFileDeletePid(driver->stateDir, vm->def->name); virFileDeletePid(driver->stateDir, vm->def->name);
virDomainDeleteConfig(conn, driver->stateDir, NULL, vm); virDomainDeleteConfig(conn, driver->stateDir, NULL, vm);
...@@ -698,7 +728,8 @@ static int lxcVmCleanup(virConnectPtr conn, ...@@ -698,7 +728,8 @@ static int lxcVmCleanup(virConnectPtr conn,
vm->state = VIR_DOMAIN_SHUTOFF; vm->state = VIR_DOMAIN_SHUTOFF;
vm->pid = -1; vm->pid = -1;
vm->def->id = -1; vm->def->id = -1;
vm->monitor = -1; priv->monitor = -1;
priv->monitorWatch = -1;
for (i = 0 ; i < vm->def->nnets ; i++) { for (i = 0 ; i < vm->def->nnets ; i++) {
vethInterfaceUpOrDown(vm->def->nets[i]->ifname, 0); vethInterfaceUpOrDown(vm->def->nets[i]->ifname, 0);
...@@ -919,12 +950,15 @@ static void lxcMonitorEvent(int watch, ...@@ -919,12 +950,15 @@ static void lxcMonitorEvent(int watch,
lxc_driver_t *driver = lxc_driver; lxc_driver_t *driver = lxc_driver;
virDomainObjPtr vm = data; virDomainObjPtr vm = data;
virDomainEventPtr event = NULL; virDomainEventPtr event = NULL;
lxcDomainObjPrivatePtr priv;
lxcDriverLock(driver); lxcDriverLock(driver);
virDomainObjLock(vm); virDomainObjLock(vm);
lxcDriverUnlock(driver); lxcDriverUnlock(driver);
if (vm->monitor != fd || vm->monitorWatch != watch) { priv = vm->privateData;
if (priv->monitor != fd || priv->monitorWatch != watch) {
virEventRemoveHandle(watch); virEventRemoveHandle(watch);
goto cleanup; goto cleanup;
} }
...@@ -1157,6 +1191,7 @@ static int lxcVmStart(virConnectPtr conn, ...@@ -1157,6 +1191,7 @@ static int lxcVmStart(virConnectPtr conn,
int logfd = -1; int logfd = -1;
unsigned int nveths = 0; unsigned int nveths = 0;
char **veths = NULL; char **veths = NULL;
lxcDomainObjPrivatePtr priv = vm->privateData;
if ((r = virFileMakePath(driver->logDir)) < 0) { if ((r = virFileMakePath(driver->logDir)) < 0) {
virReportSystemError(conn, r, virReportSystemError(conn, r,
...@@ -1209,7 +1244,7 @@ static int lxcVmStart(virConnectPtr conn, ...@@ -1209,7 +1244,7 @@ static int lxcVmStart(virConnectPtr conn,
/* Connect to the controller as a client *first* because /* Connect to the controller as a client *first* because
* this will block until the child has written their * this will block until the child has written their
* pid file out to disk */ * pid file out to disk */
if ((vm->monitor = lxcMonitorClient(conn, driver, vm)) < 0) if ((priv->monitor = lxcMonitorClient(conn, driver, vm)) < 0)
goto cleanup; goto cleanup;
/* And get its pid */ /* And get its pid */
...@@ -1223,8 +1258,8 @@ static int lxcVmStart(virConnectPtr conn, ...@@ -1223,8 +1258,8 @@ static int lxcVmStart(virConnectPtr conn,
vm->def->id = vm->pid; vm->def->id = vm->pid;
vm->state = VIR_DOMAIN_RUNNING; vm->state = VIR_DOMAIN_RUNNING;
if ((vm->monitorWatch = virEventAddHandle( if ((priv->monitorWatch = virEventAddHandle(
vm->monitor, priv->monitor,
VIR_EVENT_HANDLE_ERROR | VIR_EVENT_HANDLE_HANGUP, VIR_EVENT_HANDLE_ERROR | VIR_EVENT_HANDLE_HANGUP,
lxcMonitorEvent, lxcMonitorEvent,
vm, NULL)) < 0) { vm, NULL)) < 0) {
...@@ -1240,9 +1275,9 @@ cleanup: ...@@ -1240,9 +1275,9 @@ cleanup:
vethDelete(veths[i]); vethDelete(veths[i]);
VIR_FREE(veths[i]); VIR_FREE(veths[i]);
} }
if (rc != 0 && vm->monitor != -1) { if (rc != 0 && priv->monitor != -1) {
close(vm->monitor); close(priv->monitor);
vm->monitor = -1; priv->monitor = -1;
} }
if (parentTty != -1) if (parentTty != -1)
close(parentTty); close(parentTty);
...@@ -1610,16 +1645,19 @@ lxcReconnectVM(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque) ...@@ -1610,16 +1645,19 @@ lxcReconnectVM(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
lxc_driver_t *driver = opaque; lxc_driver_t *driver = opaque;
char *config = NULL; char *config = NULL;
virDomainDefPtr tmp; virDomainDefPtr tmp;
lxcDomainObjPrivatePtr priv;
virDomainObjLock(vm); virDomainObjLock(vm);
if ((vm->monitor = lxcMonitorClient(NULL, driver, vm)) < 0) {
priv = vm->privateData;
if ((priv->monitor = lxcMonitorClient(NULL, driver, vm)) < 0) {
goto cleanup; goto cleanup;
} }
/* Read pid from controller */ /* Read pid from controller */
if ((virFileReadPid(lxc_driver->stateDir, vm->def->name, &vm->pid)) != 0) { if ((virFileReadPid(lxc_driver->stateDir, vm->def->name, &vm->pid)) != 0) {
close(vm->monitor); close(priv->monitor);
vm->monitor = -1; priv->monitor = -1;
goto cleanup; goto cleanup;
} }
...@@ -1639,10 +1677,19 @@ lxcReconnectVM(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque) ...@@ -1639,10 +1677,19 @@ lxcReconnectVM(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
if (vm->pid != 0) { if (vm->pid != 0) {
vm->def->id = vm->pid; vm->def->id = vm->pid;
vm->state = VIR_DOMAIN_RUNNING; vm->state = VIR_DOMAIN_RUNNING;
if ((priv->monitorWatch = virEventAddHandle(
priv->monitor,
VIR_EVENT_HANDLE_ERROR | VIR_EVENT_HANDLE_HANGUP,
lxcMonitorEvent,
vm, NULL)) < 0) {
lxcVmTerminate(NULL, driver, vm, 0);
goto cleanup;
}
} else { } else {
vm->def->id = -1; vm->def->id = -1;
close(vm->monitor); close(priv->monitor);
vm->monitor = -1; priv->monitor = -1;
} }
cleanup: cleanup:
...@@ -1715,6 +1762,9 @@ static int lxcStartup(int privileged) ...@@ -1715,6 +1762,9 @@ static int lxcStartup(int privileged)
if ((lxc_driver->caps = lxcCapsInit()) == NULL) if ((lxc_driver->caps = lxcCapsInit()) == NULL)
goto cleanup; goto cleanup;
lxc_driver->caps->privateDataAllocFunc = lxcDomainObjPrivateAlloc;
lxc_driver->caps->privateDataFreeFunc = lxcDomainObjPrivateFree;
if (virDomainLoadAllConfigs(NULL, if (virDomainLoadAllConfigs(NULL,
lxc_driver->caps, lxc_driver->caps,
&lxc_driver->domains, &lxc_driver->domains,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册