Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx> --- src/lxc/lxc_conf.c | 31 +++++++++++++-------------- src/lxc/lxc_conf.h | 10 +++++++-- src/lxc/lxc_driver.c | 50 +++++++++++++++---------------------------- src/lxc/lxc_process.c | 33 +++++++++------------------- 4 files changed, 50 insertions(+), 74 deletions(-) diff --git a/src/lxc/lxc_conf.c b/src/lxc/lxc_conf.c index 6942781731..399e774ffe 100644 --- a/src/lxc/lxc_conf.c +++ b/src/lxc/lxc_conf.c @@ -42,19 +42,20 @@ VIR_LOG_INIT("lxc.lxc_conf"); -static virClassPtr virLXCDriverConfigClass; -static void virLXCDriverConfigDispose(void *obj); +G_DEFINE_TYPE(virLXCDriverConfig, vir_lxc_driver_config, G_TYPE_OBJECT); -static int virLXCConfigOnceInit(void) -{ - if (!VIR_CLASS_NEW(virLXCDriverConfig, virClassForObject())) - return -1; +static void virLXCDriverConfigFinalize(GObject *obj); - return 0; +static void vir_lxc_driver_config_init(virLXCDriverConfig *cfg G_GNUC_UNUSED) +{ } -VIR_ONCE_GLOBAL_INIT(virLXCConfig); +static void vir_lxc_driver_config_class_init(virLXCDriverConfigClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + obj->finalize = virLXCDriverConfigFinalize; +} /* Functions */ virCapsPtr virLXCDriverCapsInit(virLXCDriverPtr driver) @@ -226,11 +227,7 @@ virLXCDriverConfigNew(void) { virLXCDriverConfigPtr cfg; - if (virLXCConfigInitialize() < 0) - return NULL; - - if (!(cfg = virObjectNew(virLXCDriverConfigClass))) - return NULL; + cfg = VIR_LXC_DRIVER_CONFIG(g_object_new(VIR_TYPE_LXC_DRIVER_CONFIG, NULL)); cfg->securityDefaultConfined = false; cfg->securityRequireConfined = false; @@ -277,19 +274,21 @@ virLXCDriverConfigPtr virLXCDriverGetConfig(virLXCDriverPtr driver) { virLXCDriverConfigPtr cfg; lxcDriverLock(driver); - cfg = virObjectRef(driver->config); + cfg = g_object_ref(driver->config); lxcDriverUnlock(driver); return cfg; } static void -virLXCDriverConfigDispose(void *obj) +virLXCDriverConfigFinalize(GObject *obj) { - virLXCDriverConfigPtr cfg = obj; + virLXCDriverConfigPtr cfg = VIR_LXC_DRIVER_CONFIG(obj); VIR_FREE(cfg->configDir); VIR_FREE(cfg->autostartDir); VIR_FREE(cfg->stateDir); VIR_FREE(cfg->logDir); VIR_FREE(cfg->securityDriverName); + + G_OBJECT_CLASS(vir_lxc_driver_config_parent_class)->finalize(obj); } diff --git a/src/lxc/lxc_conf.h b/src/lxc/lxc_conf.h index f2f0e0a570..5ed84f79c7 100644 --- a/src/lxc/lxc_conf.h +++ b/src/lxc/lxc_conf.h @@ -34,6 +34,7 @@ #include "virusb.h" #include "virclosecallbacks.h" #include "virhostdev.h" +#include <glib-object.h> #define LXC_DRIVER_NAME "LXC" @@ -45,11 +46,16 @@ typedef struct _virLXCDriver virLXCDriver; typedef virLXCDriver *virLXCDriverPtr; -typedef struct _virLXCDriverConfig virLXCDriverConfig; +#define VIR_TYPE_LXC_DRIVER_CONFIG vir_lxc_driver_config_get_type() +G_DECLARE_FINAL_TYPE(virLXCDriverConfig, + vir_lxc_driver_config, + VIR, + LXC_DRIVER_CONFIG, + GObject); typedef virLXCDriverConfig *virLXCDriverConfigPtr; struct _virLXCDriverConfig { - virObject parent; + GObject parent; char *configDir; char *autostartDir; diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index c34bcd5397..9c40daaec9 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -400,7 +400,7 @@ lxcDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags) virDomainPtr dom = NULL; virObjectEventPtr event = NULL; virDomainDefPtr oldDef = NULL; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); g_autoptr(virCaps) caps = NULL; unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE; @@ -458,7 +458,6 @@ lxcDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags) virDomainDefFree(oldDef); virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); return dom; } @@ -475,7 +474,7 @@ static int lxcDomainUndefineFlags(virDomainPtr dom, virDomainObjPtr vm; virObjectEventPtr event = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virCheckFlags(0, -1); @@ -510,7 +509,6 @@ static int lxcDomainUndefineFlags(virDomainPtr dom, cleanup: virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); return ret; } @@ -638,7 +636,7 @@ static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, int ret = -1; virLXCDomainObjPrivatePtr priv; virLXCDriverPtr driver = dom->conn->privateData; - virLXCDriverConfigPtr cfg = NULL; + g_autoptr(virLXCDriverConfig) cfg = NULL; virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG | @@ -719,7 +717,6 @@ static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -743,7 +740,7 @@ lxcDomainSetMemoryParameters(virDomainPtr dom, virDomainDefPtr persistentDef = NULL; virDomainObjPtr vm = NULL; virLXCDomainObjPrivatePtr priv = NULL; - virLXCDriverConfigPtr cfg = NULL; + g_autoptr(virLXCDriverConfig) cfg = NULL; virLXCDriverPtr driver = dom->conn->privateData; int ret = -1; @@ -804,7 +801,6 @@ lxcDomainSetMemoryParameters(virDomainPtr dom, cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -976,7 +972,7 @@ static int lxcDomainCreateWithFiles(virDomainPtr dom, virDomainObjPtr vm; virObjectEventPtr event = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virCheckFlags(VIR_DOMAIN_START_AUTODESTROY, -1); @@ -1023,7 +1019,6 @@ static int lxcDomainCreateWithFiles(virDomainPtr dom, cleanup: virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); virNWFilterUnlockFilterUpdates(); return ret; } @@ -1079,7 +1074,7 @@ lxcDomainCreateXMLWithFiles(virConnectPtr conn, virDomainDefPtr def = NULL; virDomainPtr dom = NULL; virObjectEventPtr event = NULL; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); g_autoptr(virCaps) caps = NULL; unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE; @@ -1150,7 +1145,6 @@ lxcDomainCreateXMLWithFiles(virConnectPtr conn, virDomainDefFree(def); virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); virNWFilterUnlockFilterUpdates(); return dom; } @@ -1583,7 +1577,7 @@ static void lxcNotifyLoadDomain(virDomainObjPtr vm, int newVM, void *opaque) static int lxcStateReload(void) { - virLXCDriverConfigPtr cfg = NULL; + g_autoptr(virLXCDriverConfig) cfg = NULL; if (!lxc_driver) return 0; @@ -1595,7 +1589,6 @@ lxcStateReload(void) cfg->autostartDir, false, lxc_driver->xmlopt, lxcNotifyLoadDomain, lxc_driver); - virObjectUnref(cfg); return 0; } @@ -1619,7 +1612,7 @@ static int lxcStateCleanup(void) if (lxc_driver->lockFD != -1) virPidFileRelease(lxc_driver->config->stateDir, "driver", lxc_driver->lockFD); - virObjectUnref(lxc_driver->config); + g_clear_object(&lxc_driver->config); virMutexDestroy(&lxc_driver->lock); VIR_FREE(lxc_driver); @@ -1800,7 +1793,7 @@ lxcDomainSetSchedulerParametersFlags(virDomainPtr dom, int ret = -1; int rc; virLXCDomainObjPrivatePtr priv; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1); @@ -1913,7 +1906,6 @@ lxcDomainSetSchedulerParametersFlags(virDomainPtr dom, cleanup: virDomainDefFree(persistentDefCopy); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -2238,7 +2230,7 @@ lxcDomainSetBlkioParameters(virDomainPtr dom, virDomainDefPtr def = NULL; virDomainDefPtr persistentDef = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = NULL; + g_autoptr(virLXCDriverConfig) cfg = NULL; virLXCDomainObjPrivatePtr priv; virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | @@ -2303,7 +2295,6 @@ lxcDomainSetBlkioParameters(virDomainPtr dom, cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -2462,7 +2453,7 @@ static int lxcDomainSetAutostart(virDomainPtr dom, virDomainObjPtr vm; char *configFile = NULL, *autostartLink = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); if (!(vm = lxcDomObjFromDomain(dom))) goto cleanup; @@ -2528,7 +2519,6 @@ static int lxcDomainSetAutostart(virDomainPtr dom, VIR_FREE(configFile); VIR_FREE(autostartLink); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -2624,7 +2614,7 @@ static int lxcDomainSuspend(virDomainPtr dom) virDomainObjPtr vm; virObjectEventPtr event = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); if (!(vm = lxcDomObjFromDomain(dom))) goto cleanup; @@ -2661,7 +2651,6 @@ static int lxcDomainSuspend(virDomainPtr dom) cleanup: virObjectEventStateQueue(driver->domainEventState, event); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -2673,7 +2662,7 @@ static int lxcDomainResume(virDomainPtr dom) int ret = -1; int state; virLXCDomainObjPrivatePtr priv; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); if (!(vm = lxcDomObjFromDomain(dom))) goto cleanup; @@ -2718,7 +2707,6 @@ static int lxcDomainResume(virDomainPtr dom) cleanup: virObjectEventStateQueue(driver->domainEventState, event); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4296,7 +4284,7 @@ static int lxcDomainAttachDeviceFlags(virDomainPtr dom, virDomainDefPtr vmdef = NULL; virDomainDeviceDefPtr dev = NULL, dev_copy = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1); @@ -4383,7 +4371,6 @@ static int lxcDomainAttachDeviceFlags(virDomainPtr dom, virDomainDeviceDefFree(dev_copy); virDomainDeviceDefFree(dev); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4405,7 +4392,7 @@ static int lxcDomainUpdateDeviceFlags(virDomainPtr dom, virDomainDefPtr vmdef = NULL; virDomainDeviceDefPtr dev = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1); @@ -4455,7 +4442,6 @@ static int lxcDomainUpdateDeviceFlags(virDomainPtr dom, virDomainDefFree(vmdef); virDomainDeviceDefFree(dev); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4470,7 +4456,7 @@ static int lxcDomainDetachDeviceFlags(virDomainPtr dom, virDomainDefPtr vmdef = NULL; virDomainDeviceDefPtr dev = NULL, dev_copy = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1); @@ -4551,7 +4537,6 @@ static int lxcDomainDetachDeviceFlags(virDomainPtr dom, virDomainDeviceDefFree(dev_copy); virDomainDeviceDefFree(dev); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4824,7 +4809,7 @@ lxcDomainSetMetadata(virDomainPtr dom, { virLXCDriverPtr driver = dom->conn->privateData; virDomainObjPtr vm; - virLXCDriverConfigPtr cfg = NULL; + g_autoptr(virLXCDriverConfig) cfg = NULL; int ret = -1; virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | @@ -4855,7 +4840,6 @@ lxcDomainSetMetadata(virDomainPtr dom, cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index 6a3c9b7da3..c83a6c5a8f 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -162,7 +162,7 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver, size_t i; virLXCDomainObjPrivatePtr priv = vm->privateData; const virNetDevVPortProfile *vport = NULL; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virConnectPtr conn = NULL; VIR_DEBUG("Cleanup VM name=%s pid=%d reason=%d", @@ -257,7 +257,6 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver, } virDomainObjRemoveTransientDef(vm); - virObjectUnref(cfg); virObjectUnref(conn); } @@ -340,11 +339,10 @@ virLXCProcessSetupInterfaceDirect(virLXCDriverPtr driver, virDomainDefPtr def, virDomainNetDefPtr net) { - char *ret = NULL; char *res_ifname = NULL; const virNetDevBandwidth *bw; const virNetDevVPortProfile *prof; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); const char *linkdev = virDomainNetGetActualDirectDev(net); unsigned int macvlan_create_flags = VIR_NETDEV_MACVLAN_CREATE_IFUP; @@ -386,13 +384,9 @@ virLXCProcessSetupInterfaceDirect(virLXCDriverPtr driver, cfg->stateDir, NULL, 0, macvlan_create_flags) < 0) - goto cleanup; - - ret = res_ifname; + return NULL; - cleanup: - virObjectUnref(cfg); - return ret; + return res_ifname; } static const char *nsInfoLocal[VIR_LXC_DOMAIN_NAMESPACE_LAST] = { @@ -786,7 +780,7 @@ static void virLXCProcessMonitorInitNotify(virLXCMonitorPtr mon G_GNUC_UNUSED, { virLXCDriverPtr driver = lxc_driver; virLXCDomainObjPrivatePtr priv; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); ino_t inode = 0; virObjectLock(vm); @@ -806,7 +800,6 @@ static void virLXCProcessMonitorInitNotify(virLXCMonitorPtr mon G_GNUC_UNUSED, VIR_WARN("Cannot update XML with PID for LXC %s", vm->def->name); virObjectUnlock(vm); - virObjectUnref(cfg); } static virLXCMonitorCallbacks monitorCallbacks = { @@ -820,10 +813,10 @@ static virLXCMonitorPtr virLXCProcessConnectMonitor(virLXCDriverPtr driver, virDomainObjPtr vm) { virLXCMonitorPtr monitor = NULL; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); if (virSecurityManagerSetSocketLabel(driver->securityManager, vm->def) < 0) - goto cleanup; + return NULL; /* Hold an extra reference because we can't allow 'vm' to be * deleted while the monitor is active. This will be unreffed @@ -840,11 +833,8 @@ static virLXCMonitorPtr virLXCProcessConnectMonitor(virLXCDriverPtr driver, virObjectUnref(monitor); monitor = NULL; } - goto cleanup; } - cleanup: - virObjectUnref(cfg); return monitor; } @@ -932,7 +922,7 @@ virLXCProcessBuildControllerCmd(virLXCDriverPtr driver, char *filterstr; char *outputstr; virCommandPtr cmd; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); cmd = virCommandNew(vm->def->emulator); @@ -1013,7 +1003,6 @@ virLXCProcessBuildControllerCmd(virLXCDriverPtr driver, virCommandRequireHandshake(cmd); cleanup: - virObjectUnref(cfg); return cmd; error: virCommandFree(cmd); @@ -1202,7 +1191,7 @@ int virLXCProcessStart(virConnectPtr conn, virLXCDomainObjPrivatePtr priv = vm->privateData; virCapsPtr caps = NULL; virErrorPtr err = NULL; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virCgroupPtr selfcgroup; int status; char *pidfile = NULL; @@ -1564,7 +1553,6 @@ int virLXCProcessStart(virConnectPtr conn, VIR_FORCE_CLOSE(handshakefds[1]); VIR_FREE(pidfile); VIR_FREE(logfile); - virObjectUnref(cfg); g_clear_object(&caps); virErrorRestore(&err); @@ -1662,7 +1650,7 @@ virLXCProcessReconnectDomain(virDomainObjPtr vm, { virLXCDriverPtr driver = opaque; virLXCDomainObjPrivatePtr priv; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); int ret = -1; virObjectLock(vm); @@ -1728,7 +1716,6 @@ virLXCProcessReconnectDomain(virDomainObjPtr vm, ret = 0; cleanup: - virObjectUnref(cfg); virObjectUnlock(vm); return ret; -- 2.26.2