From 7fca37554c2059f13a3de32bcc29c3c1e404fbc6 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Tue, 16 Jul 2013 17:45:05 +0200 Subject: [PATCH] Introduce a virLXCDriverConfigPtr object Currently the virLXCDriverPtr struct contains an wide variety of data with varying access needs. Move all the static config data into a dedicated virLXCDriverConfigPtr object. The only locking requirement is to hold the driver lock, while obtaining an instance of virLXCDriverConfigPtr. Once a reference is held on the config object, it can be used completely lockless since it is immutable. NB, not all APIs correctly hold the driver lock while getting a reference to the config object in this patch. This is safe for now since the config is never updated on the fly. Later patches will address this fully. --- src/lxc/lxc_conf.c | 81 ++++++++++++++++++------ src/lxc/lxc_conf.h | 36 ++++++++--- src/lxc/lxc_driver.c | 142 +++++++++++++++++++++++++++++------------- src/lxc/lxc_process.c | 39 ++++++++---- 4 files changed, 211 insertions(+), 87 deletions(-) diff --git a/src/lxc/lxc_conf.c b/src/lxc/lxc_conf.c index 78b155993a..4e97f10c5c 100644 --- a/src/lxc/lxc_conf.c +++ b/src/lxc/lxc_conf.c @@ -41,6 +41,22 @@ #define VIR_FROM_THIS VIR_FROM_LXC +static virClassPtr virLXCDriverConfigClass; +static void virLXCDriverConfigDispose(void *obj); + +static int virLXCConfigOnceInit(void) +{ + if (!(virLXCDriverConfigClass = virClassNew(virClassForObject(), + "virLXCDriverConfig", + sizeof(virLXCDriverConfig), + virLXCDriverConfigDispose))) + return -1; + + return 0; +} + +VIR_ONCE_GLOBAL_INIT(virLXCConfig) + /* Functions */ virCapsPtr lxcCapsInit(virLXCDriverPtr driver) @@ -146,28 +162,42 @@ lxcDomainXMLConfInit(void) } -int lxcLoadDriverConfig(virLXCDriverPtr driver) +virLXCDriverConfigPtr +virLXCDriverConfigNew(void) { - char *filename; - virConfPtr conf; - virConfValuePtr p; + virLXCDriverConfigPtr cfg; + + if (virLXCConfigInitialize() < 0) + return NULL; - driver->securityDefaultConfined = false; - driver->securityRequireConfined = false; + if (!(cfg = virObjectNew(virLXCDriverConfigClass))) + return NULL; + + cfg->securityDefaultConfined = false; + cfg->securityRequireConfined = false; /* Set the container configuration directory */ - if (VIR_STRDUP(driver->configDir, LXC_CONFIG_DIR) < 0) + if (VIR_STRDUP(cfg->configDir, LXC_CONFIG_DIR) < 0) goto error; - if (VIR_STRDUP(driver->stateDir, LXC_STATE_DIR) < 0) + if (VIR_STRDUP(cfg->stateDir, LXC_STATE_DIR) < 0) goto error; - if (VIR_STRDUP(driver->logDir, LXC_LOG_DIR) < 0) + if (VIR_STRDUP(cfg->logDir, LXC_LOG_DIR) < 0) goto error; - if (VIR_STRDUP(driver->autostartDir, LXC_AUTOSTART_DIR) < 0) + if (VIR_STRDUP(cfg->autostartDir, LXC_AUTOSTART_DIR) < 0) goto error; + return cfg; +error: + virObjectUnref(cfg); + return NULL; +} - if (VIR_STRDUP(filename, SYSCONFDIR "/libvirt/lxc.conf") < 0) - goto error; +int +virLXCLoadDriverConfig(virLXCDriverConfigPtr cfg, + const char *filename) +{ + virConfPtr conf; + virConfValuePtr p; /* Avoid error from non-existant or unreadable file. */ if (access(filename, R_OK) == -1) @@ -186,12 +216,12 @@ int lxcLoadDriverConfig(virLXCDriverPtr driver) p = virConfGetValue(conf, "log_with_libvirtd"); CHECK_TYPE("log_with_libvirtd", VIR_CONF_LONG); - if (p) driver->log_libvirtd = p->l; + if (p) cfg->log_libvirtd = p->l; p = virConfGetValue(conf, "security_driver"); CHECK_TYPE("security_driver", VIR_CONF_STRING); if (p && p->str) { - if (VIR_STRDUP(driver->securityDriverName, p->str) < 0) { + if (VIR_STRDUP(cfg->securityDriverName, p->str) < 0) { virConfFree(conf); return -1; } @@ -199,11 +229,11 @@ int lxcLoadDriverConfig(virLXCDriverPtr driver) p = virConfGetValue(conf, "security_default_confined"); CHECK_TYPE("security_default_confined", VIR_CONF_LONG); - if (p) driver->securityDefaultConfined = p->l; + if (p) cfg->securityDefaultConfined = p->l; p = virConfGetValue(conf, "security_require_confined"); CHECK_TYPE("security_require_confined", VIR_CONF_LONG); - if (p) driver->securityRequireConfined = p->l; + if (p) cfg->securityRequireConfined = p->l; #undef CHECK_TYPE @@ -211,9 +241,22 @@ int lxcLoadDriverConfig(virLXCDriverPtr driver) virConfFree(conf); done: - VIR_FREE(filename); return 0; +} -error: - return -1; +virLXCDriverConfigPtr virLXCDriverGetConfig(virLXCDriverPtr driver) +{ + return virObjectRef(driver->config); +} + +static void +virLXCDriverConfigDispose(void *obj) +{ + virLXCDriverConfigPtr cfg = obj; + + VIR_FREE(cfg->configDir); + VIR_FREE(cfg->autostartDir); + VIR_FREE(cfg->stateDir); + VIR_FREE(cfg->logDir); + VIR_FREE(cfg->securityDriverName); } diff --git a/src/lxc/lxc_conf.h b/src/lxc/lxc_conf.h index bbf6eea03b..8ad925803b 100644 --- a/src/lxc/lxc_conf.h +++ b/src/lxc/lxc_conf.h @@ -46,33 +46,46 @@ typedef struct _virLXCDriver virLXCDriver; typedef virLXCDriver *virLXCDriverPtr; +typedef struct _virLXCDriverConfig virLXCDriverConfig; +typedef virLXCDriverConfig *virLXCDriverConfigPtr; + +struct _virLXCDriverConfig { + virObject parent; + + char *configDir; + char *autostartDir; + char *stateDir; + char *logDir; + int log_libvirtd; + int have_netns; + + char *securityDriverName; + bool securityDefaultConfined; + bool securityRequireConfined; +}; + struct _virLXCDriver { virMutex lock; + virLXCDriverConfigPtr config; + virCapsPtr caps; + virDomainXMLOptionPtr xmlopt; virSysinfoDefPtr hostsysinfo; size_t nactive; + virStateInhibitCallback inhibitCallback; void *inhibitOpaque; virDomainObjListPtr domains; - char *configDir; - char *autostartDir; - char *stateDir; - char *logDir; - int log_libvirtd; - int have_netns; virUSBDeviceListPtr activeUsbHostdevs; virDomainEventStatePtr domainEventState; - char *securityDriverName; - bool securityDefaultConfined; - bool securityRequireConfined; virSecurityManagerPtr securityManager; /* Mapping of 'char *uuidstr' -> virConnectPtr @@ -81,7 +94,10 @@ struct _virLXCDriver { virHashTablePtr autodestroy; }; -int lxcLoadDriverConfig(virLXCDriverPtr driver); +virLXCDriverConfigPtr virLXCDriverConfigNew(void); +virLXCDriverConfigPtr virLXCDriverGetConfig(virLXCDriverPtr driver); +int virLXCLoadDriverConfig(virLXCDriverConfigPtr cfg, + const char *filename); virCapsPtr lxcCapsInit(virLXCDriverPtr driver); virDomainXMLOptionPtr lxcDomainXMLConfInit(void); diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 56fe69b70d..33803887b4 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -456,8 +456,11 @@ static virDomainPtr lxcDomainDefineXML(virConnectPtr conn, const char *xml) virDomainPtr dom = NULL; virDomainEventPtr event = NULL; virDomainDefPtr oldDef = NULL; + virLXCDriverConfigPtr cfg = NULL; lxcDriverLock(driver); + cfg = virLXCDriverGetConfig(driver); + if (!(def = virDomainDefParseString(xml, driver->caps, driver->xmlopt, 1 << VIR_DOMAIN_VIRT_LXC, VIR_DOMAIN_XML_INACTIVE))) @@ -469,7 +472,7 @@ static virDomainPtr lxcDomainDefineXML(virConnectPtr conn, const char *xml) if (virSecurityManagerVerify(driver->securityManager, def) < 0) goto cleanup; - if ((def->nets != NULL) && !(driver->have_netns)) { + if ((def->nets != NULL) && !(cfg->have_netns)) { virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("System lacks NETNS support")); goto cleanup; @@ -482,7 +485,7 @@ static virDomainPtr lxcDomainDefineXML(virConnectPtr conn, const char *xml) def = NULL; vm->persistent = 1; - if (virDomainSaveConfig(driver->configDir, + if (virDomainSaveConfig(cfg->configDir, vm->newDef ? vm->newDef : vm->def) < 0) { virDomainObjListRemove(driver->domains, vm); vm = NULL; @@ -507,6 +510,7 @@ cleanup: if (event) virDomainEventStateQueue(driver->domainEventState, event); lxcDriverUnlock(driver); + virObjectUnref(cfg); return dom; } @@ -517,10 +521,13 @@ static int lxcDomainUndefineFlags(virDomainPtr dom, virDomainObjPtr vm; virDomainEventPtr event = NULL; int ret = -1; + virLXCDriverConfigPtr cfg = NULL; virCheckFlags(0, -1); lxcDriverLock(driver); + cfg = virLXCDriverGetConfig(driver); + vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!vm) { char uuidstr[VIR_UUID_STRING_BUFLEN]; @@ -539,8 +546,8 @@ static int lxcDomainUndefineFlags(virDomainPtr dom, goto cleanup; } - if (virDomainDeleteConfig(driver->configDir, - driver->autostartDir, + if (virDomainDeleteConfig(cfg->configDir, + cfg->autostartDir, vm) < 0) goto cleanup; @@ -563,6 +570,7 @@ cleanup: if (event) virDomainEventStateQueue(driver->domainEventState, event); lxcDriverUnlock(driver); + virObjectUnref(cfg); return ret; } @@ -1030,10 +1038,13 @@ static int lxcDomainCreateWithFiles(virDomainPtr dom, virDomainObjPtr vm; virDomainEventPtr event = NULL; int ret = -1; + virLXCDriverConfigPtr cfg = NULL; virCheckFlags(VIR_DOMAIN_START_AUTODESTROY, -1); lxcDriverLock(driver); + cfg = virLXCDriverGetConfig(driver); + vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!vm) { char uuidstr[VIR_UUID_STRING_BUFLEN]; @@ -1046,7 +1057,7 @@ static int lxcDomainCreateWithFiles(virDomainPtr dom, if (virDomainCreateWithFilesEnsureACL(dom->conn, vm->def) < 0) goto cleanup; - if ((vm->def->nets != NULL) && !(driver->have_netns)) { + if ((vm->def->nets != NULL) && !(cfg->have_netns)) { virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("System lacks NETNS support")); goto cleanup; @@ -1078,6 +1089,7 @@ cleanup: if (event) virDomainEventStateQueue(driver->domainEventState, event); lxcDriverUnlock(driver); + virObjectUnref(cfg); return ret; } @@ -1129,10 +1141,13 @@ lxcDomainCreateXMLWithFiles(virConnectPtr conn, virDomainDefPtr def; virDomainPtr dom = NULL; virDomainEventPtr event = NULL; + virLXCDriverConfigPtr cfg = NULL; virCheckFlags(VIR_DOMAIN_START_AUTODESTROY, NULL); lxcDriverLock(driver); + cfg = virLXCDriverGetConfig(driver); + if (!(def = virDomainDefParseString(xml, driver->caps, driver->xmlopt, 1 << VIR_DOMAIN_VIRT_LXC, VIR_DOMAIN_XML_INACTIVE))) @@ -1144,7 +1159,7 @@ lxcDomainCreateXMLWithFiles(virConnectPtr conn, if (virSecurityManagerVerify(driver->securityManager, def) < 0) goto cleanup; - if ((def->nets != NULL) && !(driver->have_netns)) { + if ((def->nets != NULL) && !(cfg->have_netns)) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("System lacks NETNS support")); goto cleanup; @@ -1184,6 +1199,7 @@ cleanup: if (event) virDomainEventStateQueue(driver->domainEventState, event); lxcDriverUnlock(driver); + virObjectUnref(cfg); return dom; } @@ -1485,26 +1501,24 @@ static int lxcCheckNetNsSupport(void) } -static int -lxcSecurityInit(virLXCDriverPtr driver) +static virSecurityManagerPtr +lxcSecurityInit(virLXCDriverConfigPtr cfg) { - VIR_INFO("lxcSecurityInit %s", driver->securityDriverName); - virSecurityManagerPtr mgr = virSecurityManagerNew(driver->securityDriverName, + VIR_INFO("lxcSecurityInit %s", cfg->securityDriverName); + virSecurityManagerPtr mgr = virSecurityManagerNew(cfg->securityDriverName, LXC_DRIVER_NAME, false, - driver->securityDefaultConfined, - driver->securityRequireConfined); + cfg->securityDefaultConfined, + cfg->securityRequireConfined); if (!mgr) goto error; - driver->securityManager = mgr; - - return 0; + return mgr; error: VIR_ERROR(_("Failed to initialize security drivers")); virObjectUnref(mgr); - return -1; + return NULL; } @@ -1513,6 +1527,7 @@ static int lxcStateInitialize(bool privileged, void *opaque ATTRIBUTE_UNUSED) { char *ld; + virLXCDriverConfigPtr cfg = NULL; /* Valgrind gets very annoyed when we clone containers, so * disable LXC when under valgrind @@ -1554,14 +1569,17 @@ static int lxcStateInitialize(bool privileged, lxc_driver->hostsysinfo = virSysinfoRead(); - lxc_driver->log_libvirtd = 0; /* by default log to container logfile */ - lxc_driver->have_netns = lxcCheckNetNsSupport(); + if (!(lxc_driver->config = cfg = virLXCDriverConfigNew())) + goto cleanup; + + cfg->log_libvirtd = 0; /* by default log to container logfile */ + cfg->have_netns = lxcCheckNetNsSupport(); /* Call function to load lxc driver configuration information */ - if (lxcLoadDriverConfig(lxc_driver) < 0) + if (virLXCLoadDriverConfig(cfg, SYSCONFDIR "/libvirt/lxc.conf") < 0) goto cleanup; - if (lxcSecurityInit(lxc_driver) < 0) + if (!(lxc_driver->securityManager = lxcSecurityInit(cfg))) goto cleanup; if ((lxc_driver->activeUsbHostdevs = virUSBDeviceListNew()) == NULL) @@ -1578,7 +1596,7 @@ static int lxcStateInitialize(bool privileged, /* Get all the running persistent or transient configs first */ if (virDomainObjListLoadAllConfigs(lxc_driver->domains, - lxc_driver->stateDir, + cfg->stateDir, NULL, 1, lxc_driver->caps, lxc_driver->xmlopt, @@ -1590,8 +1608,8 @@ static int lxcStateInitialize(bool privileged, /* Then inactive persistent configs */ if (virDomainObjListLoadAllConfigs(lxc_driver->domains, - lxc_driver->configDir, - lxc_driver->autostartDir, 0, + cfg->configDir, + cfg->autostartDir, 0, lxc_driver->caps, lxc_driver->xmlopt, 1 << VIR_DOMAIN_VIRT_LXC, @@ -1633,19 +1651,23 @@ static void lxcNotifyLoadDomain(virDomainObjPtr vm, int newVM, void *opaque) */ static int lxcStateReload(void) { + virLXCDriverConfigPtr cfg = NULL; + if (!lxc_driver) return 0; lxcDriverLock(lxc_driver); + cfg = virLXCDriverGetConfig(lxc_driver); + virDomainObjListLoadAllConfigs(lxc_driver->domains, - lxc_driver->configDir, - lxc_driver->autostartDir, 0, + cfg->configDir, + cfg->autostartDir, 0, lxc_driver->caps, lxc_driver->xmlopt, 1 << VIR_DOMAIN_VIRT_LXC, lxcNotifyLoadDomain, lxc_driver); lxcDriverUnlock(lxc_driver); - + virObjectUnref(cfg); return 0; } @@ -1667,10 +1689,7 @@ static int lxcStateCleanup(void) virObjectUnref(lxc_driver->caps); virObjectUnref(lxc_driver->securityManager); virObjectUnref(lxc_driver->xmlopt); - VIR_FREE(lxc_driver->configDir); - VIR_FREE(lxc_driver->autostartDir); - VIR_FREE(lxc_driver->stateDir); - VIR_FREE(lxc_driver->logDir); + virObjectUnref(lxc_driver->config); lxcDriverUnlock(lxc_driver); virMutexDestroy(&lxc_driver->lock); VIR_FREE(lxc_driver); @@ -1881,6 +1900,7 @@ lxcDomainSetSchedulerParametersFlags(virDomainPtr dom, int ret = -1; int rc; virLXCDomainObjPrivatePtr priv; + virLXCDriverConfigPtr cfg = NULL; virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1); @@ -1896,6 +1916,8 @@ lxcDomainSetSchedulerParametersFlags(virDomainPtr dom, lxcDriverLock(driver); + cfg = virLXCDriverGetConfig(driver); + vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (vm == NULL) { @@ -1974,12 +1996,12 @@ lxcDomainSetSchedulerParametersFlags(virDomainPtr dom, } } - if (virDomainSaveStatus(driver->xmlopt, driver->stateDir, vm) < 0) + if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0) goto cleanup; if (flags & VIR_DOMAIN_AFFECT_CONFIG) { - rc = virDomainSaveConfig(driver->configDir, vmdef); + rc = virDomainSaveConfig(cfg->configDir, vmdef); if (rc < 0) goto cleanup; @@ -1994,6 +2016,7 @@ cleanup: if (vm) virObjectUnlock(vm); lxcDriverUnlock(driver); + virObjectUnref(cfg); return ret; } @@ -2135,6 +2158,7 @@ lxcDomainSetBlkioParameters(virDomainPtr dom, virDomainDefPtr persistentDef = NULL; int ret = -1; virLXCDomainObjPrivatePtr priv; + virLXCDriverConfigPtr cfg = NULL; virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1); @@ -2146,6 +2170,8 @@ lxcDomainSetBlkioParameters(virDomainPtr dom, lxcDriverLock(driver); + cfg = virLXCDriverGetConfig(driver); + vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (vm == NULL) { @@ -2208,7 +2234,7 @@ lxcDomainSetBlkioParameters(virDomainPtr dom, } } - if (virDomainSaveConfig(driver->configDir, persistentDef) < 0) + if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0) goto cleanup; } @@ -2217,6 +2243,7 @@ cleanup: if (vm) virObjectUnlock(vm); lxcDriverUnlock(driver); + virObjectUnref(cfg); return ret; } @@ -2420,13 +2447,17 @@ cleanup: } static int lxcDomainSetAutostart(virDomainPtr dom, - int autostart) { + int autostart) +{ virLXCDriverPtr driver = dom->conn->privateData; virDomainObjPtr vm; char *configFile = NULL, *autostartLink = NULL; int ret = -1; + virLXCDriverConfigPtr cfg = NULL; lxcDriverLock(driver); + cfg = virLXCDriverGetConfig(driver); + vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!vm) { @@ -2453,20 +2484,20 @@ static int lxcDomainSetAutostart(virDomainPtr dom, goto cleanup; } - configFile = virDomainConfigFile(driver->configDir, + configFile = virDomainConfigFile(cfg->configDir, vm->def->name); if (configFile == NULL) goto cleanup; - autostartLink = virDomainConfigFile(driver->autostartDir, + autostartLink = virDomainConfigFile(cfg->autostartDir, vm->def->name); if (autostartLink == NULL) goto cleanup; if (autostart) { - if (virFileMakePath(driver->autostartDir) < 0) { + if (virFileMakePath(cfg->autostartDir) < 0) { virReportSystemError(errno, _("Cannot create autostart directory %s"), - driver->autostartDir); + cfg->autostartDir); goto cleanup; } @@ -2494,6 +2525,7 @@ cleanup: if (vm) virObjectUnlock(vm); lxcDriverUnlock(driver); + virObjectUnref(cfg); return ret; } @@ -2589,8 +2621,11 @@ static int lxcDomainSuspend(virDomainPtr dom) virDomainObjPtr vm; virDomainEventPtr event = NULL; int ret = -1; + virLXCDriverConfigPtr cfg = NULL; lxcDriverLock(driver); + cfg = virLXCDriverGetConfig(driver); + vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!vm) { @@ -2623,7 +2658,7 @@ static int lxcDomainSuspend(virDomainPtr dom) VIR_DOMAIN_EVENT_SUSPENDED_PAUSED); } - if (virDomainSaveStatus(driver->xmlopt, driver->stateDir, vm) < 0) + if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0) goto cleanup; ret = 0; @@ -2633,6 +2668,7 @@ cleanup: if (vm) virObjectUnlock(vm); lxcDriverUnlock(driver); + virObjectUnref(cfg); return ret; } @@ -2643,8 +2679,11 @@ static int lxcDomainResume(virDomainPtr dom) virDomainEventPtr event = NULL; int ret = -1; virLXCDomainObjPrivatePtr priv; + virLXCDriverConfigPtr cfg = NULL; lxcDriverLock(driver); + cfg = virLXCDriverGetConfig(driver); + vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!vm) { @@ -2680,7 +2719,7 @@ static int lxcDomainResume(virDomainPtr dom) VIR_DOMAIN_EVENT_RESUMED_UNPAUSED); } - if (virDomainSaveStatus(driver->xmlopt, driver->stateDir, vm) < 0) + if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0) goto cleanup; ret = 0; @@ -2690,6 +2729,7 @@ cleanup: if (vm) virObjectUnlock(vm); lxcDriverUnlock(driver); + virObjectUnref(cfg); return ret; } @@ -4313,6 +4353,7 @@ static int lxcDomainAttachDeviceFlags(virDomainPtr dom, virDomainDeviceDefPtr dev = NULL, dev_copy = NULL; int ret = -1; unsigned int affect; + virLXCDriverConfigPtr cfg = NULL; virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1); @@ -4320,6 +4361,8 @@ static int lxcDomainAttachDeviceFlags(virDomainPtr dom, affect = flags & (VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG); lxcDriverLock(driver); + cfg = virLXCDriverGetConfig(driver); + vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!vm) { @@ -4395,7 +4438,7 @@ static int lxcDomainAttachDeviceFlags(virDomainPtr dom, * changed even if we failed to attach the device. For example, * a new controller may be created. */ - if (virDomainSaveStatus(driver->xmlopt, driver->stateDir, vm) < 0) { + if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0) { ret = -1; goto cleanup; } @@ -4403,7 +4446,7 @@ static int lxcDomainAttachDeviceFlags(virDomainPtr dom, /* Finally, if no error until here, we can save config. */ if (flags & VIR_DOMAIN_AFFECT_CONFIG) { - ret = virDomainSaveConfig(driver->configDir, vmdef); + ret = virDomainSaveConfig(cfg->configDir, vmdef); if (!ret) { virDomainObjAssignDef(vm, vmdef, false, NULL); vmdef = NULL; @@ -4418,6 +4461,7 @@ cleanup: if (vm) virObjectUnlock(vm); lxcDriverUnlock(driver); + virObjectUnref(cfg); return ret; } @@ -4440,6 +4484,7 @@ static int lxcDomainUpdateDeviceFlags(virDomainPtr dom, virDomainDeviceDefPtr dev = NULL, dev_copy = NULL; int ret = -1; unsigned int affect; + virLXCDriverConfigPtr cfg = NULL; virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG | @@ -4448,6 +4493,8 @@ static int lxcDomainUpdateDeviceFlags(virDomainPtr dom, affect = flags & (VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG); lxcDriverLock(driver); + cfg = virLXCDriverGetConfig(driver); + vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!vm) { @@ -4524,7 +4571,7 @@ static int lxcDomainUpdateDeviceFlags(virDomainPtr dom, /* Finally, if no error until here, we can save config. */ if (flags & VIR_DOMAIN_AFFECT_CONFIG) { - ret = virDomainSaveConfig(driver->configDir, vmdef); + ret = virDomainSaveConfig(cfg->configDir, vmdef); if (!ret) { virDomainObjAssignDef(vm, vmdef, false, NULL); vmdef = NULL; @@ -4539,6 +4586,7 @@ cleanup: if (vm) virObjectUnlock(vm); lxcDriverUnlock(driver); + virObjectUnref(cfg); return ret; } @@ -4553,6 +4601,7 @@ static int lxcDomainDetachDeviceFlags(virDomainPtr dom, virDomainDeviceDefPtr dev = NULL, dev_copy = NULL; int ret = -1; unsigned int affect; + virLXCDriverConfigPtr cfg = NULL; virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1); @@ -4560,6 +4609,8 @@ static int lxcDomainDetachDeviceFlags(virDomainPtr dom, affect = flags & (VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG); lxcDriverLock(driver); + cfg = virLXCDriverGetConfig(driver); + vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!vm) { @@ -4636,7 +4687,7 @@ static int lxcDomainDetachDeviceFlags(virDomainPtr dom, * changed even if we failed to attach the device. For example, * a new controller may be created. */ - if (virDomainSaveStatus(driver->xmlopt, driver->stateDir, vm) < 0) { + if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0) { ret = -1; goto cleanup; } @@ -4644,7 +4695,7 @@ static int lxcDomainDetachDeviceFlags(virDomainPtr dom, /* Finally, if no error until here, we can save config. */ if (flags & VIR_DOMAIN_AFFECT_CONFIG) { - ret = virDomainSaveConfig(driver->configDir, vmdef); + ret = virDomainSaveConfig(cfg->configDir, vmdef); if (!ret) { virDomainObjAssignDef(vm, vmdef, false, NULL); vmdef = NULL; @@ -4659,6 +4710,7 @@ cleanup: if (vm) virObjectUnlock(vm); lxcDriverUnlock(driver); + virObjectUnref(cfg); return ret; } diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index 54eb728fc3..233ac3c52c 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -225,6 +225,7 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver, size_t i; virLXCDomainObjPrivatePtr priv = vm->privateData; virNetDevVPortProfilePtr vport = NULL; + virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); VIR_DEBUG("Stopping VM name=%s pid=%d reason=%d", vm->def->name, (int)vm->pid, (int)reason); @@ -249,8 +250,8 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver, priv->monitor = NULL; } - virPidFileDelete(driver->stateDir, vm->def->name); - virDomainDeleteConfig(driver->stateDir, NULL, vm); + virPidFileDelete(cfg->stateDir, vm->def->name); + virDomainDeleteConfig(cfg->stateDir, NULL, vm); virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, reason); vm->pid = -1; @@ -301,6 +302,7 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver, vm->def->id = -1; vm->newDef = NULL; } + virObjectUnref(cfg); } @@ -367,6 +369,7 @@ char *virLXCProcessSetupInterfaceDirect(virConnectPtr conn, virLXCDriverPtr driver = conn->privateData; virNetDevBandwidthPtr bw; virNetDevVPortProfilePtr prof; + virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); /* XXX how todo bandwidth controls ? * Since the 'net-ifname' is about to be moved to a different @@ -402,13 +405,14 @@ char *virLXCProcessSetupInterfaceDirect(virConnectPtr conn, virDomainNetGetActualVirtPortProfile(net), &res_ifname, VIR_NETDEV_VPORT_PROFILE_OP_CREATE, - driver->stateDir, + cfg->stateDir, virDomainNetGetActualBandwidth(net)) < 0) goto cleanup; ret = res_ifname; cleanup: + virObjectUnref(cfg); return ret; } @@ -673,10 +677,12 @@ static void virLXCProcessMonitorInitNotify(virLXCMonitorPtr mon ATTRIBUTE_UNUSED { virLXCDriverPtr driver = lxc_driver; virLXCDomainObjPrivatePtr priv; + virLXCDriverConfigPtr cfg; ino_t inode; lxcDriverLock(driver); virObjectLock(vm); + cfg = virLXCDriverGetConfig(driver); lxcDriverUnlock(driver); priv = vm->privateData; @@ -692,10 +698,11 @@ static void virLXCProcessMonitorInitNotify(virLXCMonitorPtr mon ATTRIBUTE_UNUSED } virDomainAuditInit(vm, initpid, inode); - if (virDomainSaveStatus(lxc_driver->xmlopt, lxc_driver->stateDir, vm) < 0) + if (virDomainSaveStatus(lxc_driver->xmlopt, cfg->stateDir, vm) < 0) VIR_WARN("Cannot update XML with PID for LXC %s", vm->def->name); virObjectUnlock(vm); + virObjectUnref(cfg); } static virLXCMonitorCallbacks monitorCallbacks = { @@ -709,6 +716,7 @@ static virLXCMonitorPtr virLXCProcessConnectMonitor(virLXCDriverPtr driver, virDomainObjPtr vm) { virLXCMonitorPtr monitor = NULL; + virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); if (virSecurityManagerSetSocketLabel(driver->securityManager, vm->def) < 0) goto cleanup; @@ -717,7 +725,7 @@ static virLXCMonitorPtr virLXCProcessConnectMonitor(virLXCDriverPtr driver, * deleted while the monitor is active */ virObjectRef(vm); - monitor = virLXCMonitorNew(vm, driver->stateDir, &monitorCallbacks); + monitor = virLXCMonitorNew(vm, cfg->stateDir, &monitorCallbacks); if (monitor == NULL) virObjectUnref(vm); @@ -731,6 +739,7 @@ static virLXCMonitorPtr virLXCProcessConnectMonitor(virLXCDriverPtr driver, } cleanup: + virObjectUnref(cfg); return monitor; } @@ -812,6 +821,7 @@ virLXCProcessBuildControllerCmd(virLXCDriverPtr driver, char *filterstr; char *outputstr; virCommandPtr cmd; + virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); cmd = virCommandNew(vm->def->emulator); @@ -832,7 +842,7 @@ virLXCProcessBuildControllerCmd(virLXCDriverPtr driver, VIR_FREE(filterstr); } - if (driver->log_libvirtd) { + if (cfg->log_libvirtd) { if (virLogGetNbOutputs() > 0) { outputstr = virLogGetOutputs(); if (!outputstr) { @@ -878,6 +888,7 @@ virLXCProcessBuildControllerCmd(virLXCDriverPtr driver, return cmd; cleanup: virCommandFree(cmd); + virObjectUnref(cfg); return NULL; } @@ -1052,6 +1063,7 @@ int virLXCProcessStart(virConnectPtr conn, virCommandPtr cmd = NULL; virLXCDomainObjPrivatePtr priv = vm->privateData; virErrorPtr err = NULL; + virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); virCgroupFree(&priv->cgroup); @@ -1080,15 +1092,15 @@ int virLXCProcessStart(virConnectPtr conn, return -1; } - if (virFileMakePath(driver->logDir) < 0) { + if (virFileMakePath(cfg->logDir) < 0) { virReportSystemError(errno, _("Cannot create log directory '%s'"), - driver->logDir); + cfg->logDir); return -1; } if (virAsprintf(&logfile, "%s/%s.log", - driver->logDir, vm->def->name) < 0) + cfg->logDir, vm->def->name) < 0) return -1; /* Do this up front, so any part of the startup process can add @@ -1178,7 +1190,7 @@ int virLXCProcessStart(virConnectPtr conn, goto cleanup; /* Save the configuration for the controller */ - if (virDomainSaveConfig(driver->stateDir, vm->def) < 0) + if (virDomainSaveConfig(cfg->stateDir, vm->def) < 0) goto cleanup; if ((logfd = open(logfile, O_WRONLY | O_APPEND | O_CREAT, @@ -1253,7 +1265,7 @@ int virLXCProcessStart(virConnectPtr conn, goto cleanup; /* And get its pid */ - if ((r = virPidFileRead(driver->stateDir, vm->def->name, &vm->pid)) < 0) { + if ((r = virPidFileRead(cfg->stateDir, vm->def->name, &vm->pid)) < 0) { char out[1024]; if (virLXCProcessReadLogOutput(vm, logfile, pos, out, 1024) > 0) @@ -1262,7 +1274,7 @@ int virLXCProcessStart(virConnectPtr conn, else virReportSystemError(-r, _("Failed to read pid file %s/%s.pid"), - driver->stateDir, vm->def->name); + cfg->stateDir, vm->def->name); goto cleanup; } @@ -1301,7 +1313,7 @@ int virLXCProcessStart(virConnectPtr conn, * location for the benefit of libvirt_lxc. We're now overwriting * it with the live status XML instead. This is a (currently * harmless) inconsistency we should fix one day */ - if (virDomainSaveStatus(driver->xmlopt, driver->stateDir, vm) < 0) + if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm) < 0) goto error; /* finally we can call the 'started' hook script if any */ @@ -1364,6 +1376,7 @@ cleanup: VIR_FORCE_CLOSE(handshakefds[0]); VIR_FORCE_CLOSE(handshakefds[1]); VIR_FREE(logfile); + virObjectUnref(cfg); if (err) { virSetError(err); -- GitLab