From edea0d98949e8ac9bba917e2a69ca9a3a7903c3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Date: Tue, 12 May 2020 18:31:16 +0100 Subject: [PATCH] lxc: replace VIR_ALLOC/REALLOC with g_new0/renew MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: Laine Stump Signed-off-by: Daniel P. Berrangé --- src/lxc/lxc_cgroup.c | 1 - src/lxc/lxc_conf.c | 4 +--- src/lxc/lxc_container.c | 6 ++---- src/lxc/lxc_controller.c | 22 ++++++---------------- src/lxc/lxc_domain.c | 16 ++++------------ src/lxc/lxc_driver.c | 29 ++++++++++++++++------------- src/lxc/lxc_fuse.c | 6 +----- src/lxc/lxc_fuse.h | 1 + src/lxc/lxc_hostdev.c | 1 - src/lxc/lxc_monitor.c | 2 -- src/lxc/lxc_native.c | 11 ++++------- src/lxc/lxc_process.c | 11 +++-------- 12 files changed, 38 insertions(+), 72 deletions(-) diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c index 4ae34925e6..e71f37d2b1 100644 --- a/src/lxc/lxc_cgroup.c +++ b/src/lxc/lxc_cgroup.c @@ -27,7 +27,6 @@ #include "virfile.h" #include "virerror.h" #include "virlog.h" -#include "viralloc.h" #include "virstring.h" #include "virsystemd.h" #include "virutil.h" diff --git a/src/lxc/lxc_conf.c b/src/lxc/lxc_conf.c index 43e9b70651..13da6c4586 100644 --- a/src/lxc/lxc_conf.c +++ b/src/lxc/lxc_conf.c @@ -29,7 +29,6 @@ #include "lxc_domain.h" #include "virerror.h" #include "virconf.h" -#include "viralloc.h" #include "virlog.h" #include "viruuid.h" #include "configmake.h" @@ -145,8 +144,7 @@ virCapsPtr virLXCDriverCapsInit(virLXCDriverPtr driver) VIR_DOMAIN_VIRT_LXC); type = virDomainVirtTypeToString(VIR_DOMAIN_VIRT_LXC); /* Allocate the primary security driver for LXC. */ - if (VIR_ALLOC(caps->host.secModels) < 0) - goto error; + caps->host.secModels = g_new0(virCapsHostSecModel, 1); caps->host.nsecModels = 1; caps->host.secModels[0].model = g_strdup(model); caps->host.secModels[0].doi = g_strdup(doi); diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index bd78fa7af6..77df49ea75 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -156,8 +156,7 @@ int lxcContainerHasReboot(void) } cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF; - if (VIR_ALLOC_N(stack, stacksize) < 0) - return -1; + stack = g_new0(char, stacksize); childStack = stack + stacksize; @@ -2322,8 +2321,7 @@ int lxcContainerStart(virDomainDefPtr def, }; /* allocate a stack for the container */ - if (VIR_ALLOC_N(stack, stacksize) < 0) - return -1; + stack = g_new0(char, stacksize); stacktop = stack + stacksize; diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index 0438d72538..4672920574 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -189,13 +189,10 @@ virLXCControllerDriverFree(virLXCDriverPtr driver) static virLXCControllerPtr virLXCControllerNew(const char *name) { - virLXCControllerPtr ctrl = NULL; + virLXCControllerPtr ctrl = g_new0(virLXCController, 1); virLXCDriverPtr driver = NULL; g_autofree char *configFile = NULL; - if (VIR_ALLOC(ctrl) < 0) - goto error; - ctrl->timerShutdown = -1; ctrl->firstClient = true; @@ -2333,12 +2330,9 @@ virLXCControllerRun(virLXCControllerPtr ctrl) int rc = -1; int control[2] = { -1, -1}; int containerhandshake[2] = { -1, -1 }; - char **containerTTYPaths = NULL; + char **containerTTYPaths = g_new0(char *, ctrl->nconsoles); size_t i; - if (VIR_ALLOC_N(containerTTYPaths, ctrl->nconsoles) < 0) - goto cleanup; - if (socketpair(PF_UNIX, SOCK_STREAM, 0, control) < 0) { virReportSystemError(errno, "%s", _("sockpair failed")); @@ -2530,14 +2524,12 @@ int main(int argc, char *argv[]) break; case 'v': - if (VIR_REALLOC_N(veths, nveths+1) < 0) - goto cleanup; + veths = g_renew(char *, veths, nveths+1); veths[nveths++] = g_strdup(optarg); break; case 'c': - if (VIR_REALLOC_N(ttyFDs, nttyFDs + 1) < 0) - goto cleanup; + ttyFDs = g_renew(int, ttyFDs, nttyFDs + 1); if (virStrToLong_i(optarg, NULL, 10, &ttyFDs[nttyFDs++]) < 0) { fprintf(stderr, "malformed --console argument '%s'", optarg); goto cleanup; @@ -2545,8 +2537,7 @@ int main(int argc, char *argv[]) break; case 'p': - if (VIR_REALLOC_N(passFDs, npassFDs + 1) < 0) - goto cleanup; + passFDs = g_renew(int, passFDs, npassFDs + 1); if (virStrToLong_i(optarg, NULL, 10, &passFDs[npassFDs++]) < 0) { fprintf(stderr, "malformed --passfd argument '%s'", optarg); goto cleanup; @@ -2661,8 +2652,7 @@ int main(int argc, char *argv[]) if (ns_fd[i] != -1) { if (!ctrl->nsFDs) {/*allocate only once */ size_t j = 0; - if (VIR_ALLOC_N(ctrl->nsFDs, VIR_LXC_DOMAIN_NAMESPACE_LAST) < 0) - goto cleanup; + ctrl->nsFDs = g_new0(int, VIR_LXC_DOMAIN_NAMESPACE_LAST); for (j = 0; j < VIR_LXC_DOMAIN_NAMESPACE_LAST; j++) ctrl->nsFDs[j] = -1; } diff --git a/src/lxc/lxc_domain.c b/src/lxc/lxc_domain.c index b9c3b4eec3..bf82844fd2 100644 --- a/src/lxc/lxc_domain.c +++ b/src/lxc/lxc_domain.c @@ -23,7 +23,6 @@ #include "lxc_domain.h" -#include "viralloc.h" #include "virlog.h" #include "virerror.h" #include "virstring.h" @@ -153,10 +152,7 @@ virLXCDomainObjEndJob(virLXCDriverPtr driver G_GNUC_UNUSED, static void * virLXCDomainObjPrivateAlloc(void *opaque G_GNUC_UNUSED) { - virLXCDomainObjPrivatePtr priv; - - if (VIR_ALLOC(priv) < 0) - return NULL; + virLXCDomainObjPrivatePtr priv = g_new0(virLXCDomainObjPrivate, 1); if (virLXCDomainObjInitJob(priv) < 0) { g_free(priv); @@ -208,7 +204,7 @@ static int lxcDomainDefNamespaceParse(xmlXPathContextPtr ctxt, void **data) { - lxcDomainDefPtr lxcDef = NULL; + lxcDomainDefPtr lxcDef = g_new0(lxcDomainDef, 1); g_autofree xmlNodePtr *nodes = NULL; bool uses_lxc_ns = false; xmlNodePtr node; @@ -216,9 +212,6 @@ lxcDomainDefNamespaceParse(xmlXPathContextPtr ctxt, int n; size_t i; - if (VIR_ALLOC(lxcDef) < 0) - return -1; - node = ctxt->node; if ((n = virXPathNodeSet("./lxc:namespace/*", ctxt, &nodes)) < 0) goto error; @@ -470,9 +463,8 @@ virLXCDomainSetRunlevel(virDomainObjPtr vm, for (nfifos = 0; virInitctlFifos[nfifos]; nfifos++) ; - if (VIR_ALLOC_N(data.st, nfifos) < 0 || - VIR_ALLOC_N(data.st_valid, nfifos) < 0) - goto cleanup; + data.st = g_new0(struct stat, nfifos); + data.st_valid = g_new0(bool, nfifos); for (i = 0; virInitctlFifos[i]; i++) { const char *fifo = virInitctlFifos[i]; diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 2c63b0f3a3..46a182be45 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -44,7 +44,6 @@ #include "lxc_driver.h" #include "lxc_native.h" #include "lxc_process.h" -#include "viralloc.h" #include "virnetdevbridge.h" #include "virnetdevveth.h" #include "virnetdevopenvswitch.h" @@ -1492,8 +1491,7 @@ static int lxcStateInitialize(bool privileged, return VIR_DRV_STATE_INIT_SKIPPED; } - if (VIR_ALLOC(lxc_driver) < 0) - return VIR_DRV_STATE_INIT_ERROR; + lxc_driver = g_new0(virLXCDriver, 1); lxc_driver->lockFD = -1; if (virMutexInit(&lxc_driver->lock) < 0) { g_free(lxc_driver); @@ -3398,8 +3396,9 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver, perms) < 0) goto cleanup; - if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks + 1) < 0) - goto cleanup; + vm->def->disks = g_renew(virDomainDiskDefPtr, + vm->def->disks, + vm->def->ndisks + 1); file = g_strdup_printf("/dev/%s", def->dst); @@ -3451,8 +3450,9 @@ lxcDomainAttachDeviceNetLive(virLXCDriverPtr driver, return -1; /* preallocate new slot for device */ - if (VIR_REALLOC_N(vm->def->nets, vm->def->nnets+1) < 0) - return -1; + vm->def->nets = g_renew(virDomainNetDefPtr, + vm->def->nets, + vm->def->nnets + 1); /* If appropriate, grab a physical device from the configured * network's pool of devices, or resolve bridge device name @@ -3606,8 +3606,9 @@ lxcDomainAttachDeviceHostdevSubsysUSBLive(virLXCDriverPtr driver, goto cleanup; } - if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs + 1) < 0) - goto cleanup; + vm->def->hostdevs = g_renew(virDomainHostdevDefPtr, + vm->def->hostdevs, + vm->def->nhostdevs + 1); if (virUSBDeviceFileIterate(usb, virLXCSetupHostUSBDeviceCgroup, @@ -3675,8 +3676,9 @@ lxcDomainAttachDeviceHostdevStorageLive(virLXCDriverPtr driver, goto cleanup; } - if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs+1) < 0) - goto cleanup; + vm->def->hostdevs = g_renew(virDomainHostdevDefPtr, + vm->def->hostdevs, + vm->def->nhostdevs + 1); if (virCgroupAllowDevice(priv->cgroup, 'b', @@ -3754,8 +3756,9 @@ lxcDomainAttachDeviceHostdevMiscLive(virLXCDriverPtr driver, VIR_CGROUP_DEVICE_RWM) < 0) goto cleanup; - if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs+1) < 0) - goto cleanup; + vm->def->hostdevs = g_renew(virDomainHostdevDefPtr, + vm->def->hostdevs, + vm->def->nhostdevs + 1); if (lxcDomainAttachDeviceMknod(driver, 0700 | S_IFBLK, diff --git a/src/lxc/lxc_fuse.c b/src/lxc/lxc_fuse.c index c4223f4e06..146629f67e 100644 --- a/src/lxc/lxc_fuse.c +++ b/src/lxc/lxc_fuse.c @@ -31,7 +31,6 @@ #include "virfile.h" #include "virbuffer.h" #include "virstring.h" -#include "viralloc.h" #include "virutil.h" #define VIR_FROM_THIS VIR_FROM_LXC @@ -284,10 +283,7 @@ int lxcSetupFuse(virLXCFusePtr *f, virDomainDefPtr def) { int ret = -1; struct fuse_args args = FUSE_ARGS_INIT(0, NULL); - virLXCFusePtr fuse = NULL; - - if (VIR_ALLOC(fuse) < 0) - goto cleanup; + virLXCFusePtr fuse = g_new0(virLXCFuse, 1); fuse->def = def; diff --git a/src/lxc/lxc_fuse.h b/src/lxc/lxc_fuse.h index e9cfd9a20a..6bba5669a8 100644 --- a/src/lxc/lxc_fuse.h +++ b/src/lxc/lxc_fuse.h @@ -50,6 +50,7 @@ struct virLXCFuse { struct fuse_chan *ch; virMutex lock; }; +typedef struct virLXCFuse virLXCFuse; typedef struct virLXCFuse *virLXCFusePtr; int lxcSetupFuse(virLXCFusePtr *f, virDomainDefPtr def); diff --git a/src/lxc/lxc_hostdev.c b/src/lxc/lxc_hostdev.c index 6dbed9b9a4..609fbe5c87 100644 --- a/src/lxc/lxc_hostdev.c +++ b/src/lxc/lxc_hostdev.c @@ -22,7 +22,6 @@ #include #include "lxc_hostdev.h" -#include "viralloc.h" #include "virlog.h" #include "virerror.h" #include "virhostdev.h" diff --git a/src/lxc/lxc_monitor.c b/src/lxc/lxc_monitor.c index e0d03f9d4c..96c325af49 100644 --- a/src/lxc/lxc_monitor.c +++ b/src/lxc/lxc_monitor.c @@ -24,8 +24,6 @@ #include "lxc_conf.h" #include "lxc_monitor_dispatch.h" -#include "viralloc.h" - #include "virerror.h" #include "virlog.h" #include "virthread.h" diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c index 622d506398..9e879e438a 100644 --- a/src/lxc/lxc_native.c +++ b/src/lxc/lxc_native.c @@ -151,9 +151,10 @@ lxcParseFstabLine(char *fstabLine) lxcFstabPtr fstab = NULL; char **parts; - if (!fstabLine || VIR_ALLOC(fstab) < 0) + if (!fstabLine) return NULL; + fstab = g_new0(lxcFstab, 1); if (!(parts = lxcStringSplit(fstabLine))) goto error; @@ -561,10 +562,7 @@ lxcNetworkParseDataIPs(const char *name, { int family = AF_INET; char **ipparts = NULL; - g_autofree virNetDevIPAddrPtr ip = NULL; - - if (VIR_ALLOC(ip) < 0) - return -1; + g_autofree virNetDevIPAddrPtr ip = g_new0(virNetDevIPAddr, 1); if (STREQ(name, "ipv6") || STREQ(name, "ipv6.address")) family = AF_INET6; @@ -820,8 +818,7 @@ lxcCreateConsoles(virDomainDefPtr def, virConfPtr properties) return -1; } - if (VIR_ALLOC_N(def->consoles, nbttys) < 0) - return -1; + def->consoles = g_new0(virDomainChrDefPtr, nbttys); def->nconsoles = nbttys; for (i = 0; i < nbttys; i++) { diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index 998754062c..f3d57875ad 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -550,8 +550,7 @@ virLXCProcessSetupInterfaces(virLXCDriverPtr driver, virConnectPtr netconn = NULL; virErrorPtr save_err = NULL; - if (VIR_ALLOC_N(*veths, def->nnets + 1) < 0) - return -1; + *veths = g_new0(char *, def->nnets + 1); for (i = 0; i < def->nnets; i++) { char *veth = NULL; @@ -1246,10 +1245,7 @@ int virLXCProcessStart(virConnectPtr conn, } if (!vm->def->resource) { - virDomainResourceDefPtr res; - - if (VIR_ALLOC(res) < 0) - goto cleanup; + virDomainResourceDefPtr res = g_new0(virDomainResourceDef, 1); res->partition = g_strdup("/machine"); @@ -1298,8 +1294,7 @@ int virLXCProcessStart(virConnectPtr conn, * and forward I/O between them. */ nttyFDs = vm->def->nconsoles; - if (VIR_ALLOC_N(ttyFDs, nttyFDs) < 0) - goto cleanup; + ttyFDs = g_new0(int, nttyFDs); for (i = 0; i < vm->def->nconsoles; i++) ttyFDs[i] = -1;