1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-26 03:21:44 +03:00

lxc: Replacing default strings definitions by g_autofree statement

There are a lots of strings being handled inside some LXC functions.
They can be moved to g_autofree to avoid declaring a return value to get
proper code cleanups. This commit is changing functions from
lxc_{controller,cgroup,fuse} only.

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
This commit is contained in:
Julio Faracco 2020-02-24 11:24:27 -03:00 committed by Daniel P. Berrangé
parent a97e17c4e2
commit 5b82f7f3af
3 changed files with 44 additions and 90 deletions

View File

@ -55,8 +55,7 @@ static int virLXCCgroupSetupCpusetTune(virDomainDefPtr def,
virCgroupPtr cgroup, virCgroupPtr cgroup,
virBitmapPtr nodemask) virBitmapPtr nodemask)
{ {
int ret = -1; g_autofree char *mask = NULL;
char *mask = NULL;
virDomainNumatuneMemMode mode; virDomainNumatuneMemMode mode;
if (def->placement_mode != VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO && if (def->placement_mode != VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO &&
@ -67,21 +66,17 @@ static int virLXCCgroupSetupCpusetTune(virDomainDefPtr def,
if (virDomainNumatuneGetMode(def->numa, -1, &mode) < 0 || if (virDomainNumatuneGetMode(def->numa, -1, &mode) < 0 ||
mode == VIR_DOMAIN_NUMATUNE_MEM_STRICT) { mode == VIR_DOMAIN_NUMATUNE_MEM_STRICT) {
ret = 0; return 0;
goto cleanup;
} }
if (virDomainNumatuneMaybeFormatNodeset(def->numa, nodemask, if (virDomainNumatuneMaybeFormatNodeset(def->numa, nodemask,
&mask, -1) < 0) &mask, -1) < 0)
goto cleanup; return -1;
if (mask && virCgroupSetCpusetMems(cgroup, mask) < 0) if (mask && virCgroupSetCpusetMems(cgroup, mask) < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
VIR_FREE(mask);
return ret;
} }

View File

@ -803,8 +803,7 @@ static int virLXCControllerGetNumadAdvice(virLXCControllerPtr ctrl,
virBitmapPtr *mask) virBitmapPtr *mask)
{ {
virBitmapPtr nodemask = NULL; virBitmapPtr nodemask = NULL;
char *nodeset = NULL; g_autofree char *nodeset = NULL;
int ret = -1;
/* Get the advisory nodeset from numad if 'placement' of /* Get the advisory nodeset from numad if 'placement' of
* either <vcpu> or <numatune> is 'auto'. * either <vcpu> or <numatune> is 'auto'.
@ -813,20 +812,17 @@ static int virLXCControllerGetNumadAdvice(virLXCControllerPtr ctrl,
nodeset = virNumaGetAutoPlacementAdvice(virDomainDefGetVcpus(ctrl->def), nodeset = virNumaGetAutoPlacementAdvice(virDomainDefGetVcpus(ctrl->def),
ctrl->def->mem.cur_balloon); ctrl->def->mem.cur_balloon);
if (!nodeset) if (!nodeset)
goto cleanup; return -1;
VIR_DEBUG("Nodeset returned from numad: %s", nodeset); VIR_DEBUG("Nodeset returned from numad: %s", nodeset);
if (virBitmapParse(nodeset, &nodemask, VIR_DOMAIN_CPUMASK_LEN) < 0) if (virBitmapParse(nodeset, &nodemask, VIR_DOMAIN_CPUMASK_LEN) < 0)
goto cleanup; return -1;
} }
ret = 0;
*mask = nodemask; *mask = nodemask;
cleanup: return 0;
VIR_FREE(nodeset);
return ret;
} }
@ -1435,9 +1431,8 @@ virLXCControllerSetupUsernsMap(virDomainIdMapEntryPtr map,
*/ */
static int virLXCControllerSetupUserns(virLXCControllerPtr ctrl) static int virLXCControllerSetupUserns(virLXCControllerPtr ctrl)
{ {
char *uid_map = NULL; g_autofree char *uid_map = NULL;
char *gid_map = NULL; g_autofree char *gid_map = NULL;
int ret = -1;
/* User namespace is disabled for container */ /* User namespace is disabled for container */
if (ctrl->def->idmap.nuidmap == 0) { if (ctrl->def->idmap.nuidmap == 0) {
@ -1451,28 +1446,23 @@ static int virLXCControllerSetupUserns(virLXCControllerPtr ctrl)
if (virLXCControllerSetupUsernsMap(ctrl->def->idmap.uidmap, if (virLXCControllerSetupUsernsMap(ctrl->def->idmap.uidmap,
ctrl->def->idmap.nuidmap, ctrl->def->idmap.nuidmap,
uid_map) < 0) uid_map) < 0)
goto cleanup; return -1;
gid_map = g_strdup_printf("/proc/%d/gid_map", ctrl->initpid); gid_map = g_strdup_printf("/proc/%d/gid_map", ctrl->initpid);
if (virLXCControllerSetupUsernsMap(ctrl->def->idmap.gidmap, if (virLXCControllerSetupUsernsMap(ctrl->def->idmap.gidmap,
ctrl->def->idmap.ngidmap, ctrl->def->idmap.ngidmap,
gid_map) < 0) gid_map) < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
VIR_FREE(uid_map);
VIR_FREE(gid_map);
return ret;
} }
static int virLXCControllerSetupDev(virLXCControllerPtr ctrl) static int virLXCControllerSetupDev(virLXCControllerPtr ctrl)
{ {
char *mount_options = NULL; g_autofree char *mount_options = NULL;
char *opts = NULL; g_autofree char *opts = NULL;
char *dev = NULL; g_autofree char *dev = NULL;
int ret = -1;
VIR_DEBUG("Setting up /dev/ for container"); VIR_DEBUG("Setting up /dev/ for container");
@ -1489,24 +1479,18 @@ static int virLXCControllerSetupDev(virLXCControllerPtr ctrl)
opts = g_strdup_printf("mode=755,size=65536%s", mount_options); opts = g_strdup_printf("mode=755,size=65536%s", mount_options);
if (virFileSetupDev(dev, opts) < 0) if (virFileSetupDev(dev, opts) < 0)
goto cleanup; return -1;
if (lxcContainerChown(ctrl->def, dev) < 0) if (lxcContainerChown(ctrl->def, dev) < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
VIR_FREE(opts);
VIR_FREE(mount_options);
VIR_FREE(dev);
return ret;
} }
static int virLXCControllerPopulateDevices(virLXCControllerPtr ctrl) static int virLXCControllerPopulateDevices(virLXCControllerPtr ctrl)
{ {
size_t i; size_t i;
int ret = -1; g_autofree char *path = NULL;
char *path = NULL;
const struct { const struct {
int maj; int maj;
int min; int min;
@ -1522,7 +1506,7 @@ static int virLXCControllerPopulateDevices(virLXCControllerPtr ctrl)
}; };
if (virLXCControllerSetupDev(ctrl) < 0) if (virLXCControllerSetupDev(ctrl) < 0)
goto cleanup; return -1;
/* Populate /dev/ with a few important bits */ /* Populate /dev/ with a few important bits */
for (i = 0; i < G_N_ELEMENTS(devs); i++) { for (i = 0; i < G_N_ELEMENTS(devs); i++) {
@ -1535,19 +1519,14 @@ static int virLXCControllerPopulateDevices(virLXCControllerPtr ctrl)
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to make device %s"), _("Failed to make device %s"),
path); path);
goto cleanup; return -1;
} }
if (lxcContainerChown(ctrl->def, path) < 0) if (lxcContainerChown(ctrl->def, path) < 0)
goto cleanup; return -1;
VIR_FREE(path);
} }
ret = 0; return 0;
cleanup:
VIR_FREE(path);
return ret;
} }
@ -2113,10 +2092,9 @@ virLXCControllerSetupPrivateNS(void)
static int static int
virLXCControllerSetupDevPTS(virLXCControllerPtr ctrl) virLXCControllerSetupDevPTS(virLXCControllerPtr ctrl)
{ {
char *mount_options = NULL; g_autofree char *mount_options = NULL;
char *opts = NULL; g_autofree char *opts = NULL;
char *devpts = NULL; g_autofree char *devpts = NULL;
int ret = -1;
gid_t ptsgid = 5; gid_t ptsgid = 5;
VIR_DEBUG("Setting up private /dev/pts"); VIR_DEBUG("Setting up private /dev/pts");
@ -2131,7 +2109,7 @@ virLXCControllerSetupDevPTS(virLXCControllerPtr ctrl)
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to make path %s"), _("Failed to make path %s"),
devpts); devpts);
goto cleanup; return -1;
} }
if (ctrl->def->idmap.ngidmap) if (ctrl->def->idmap.ngidmap)
@ -2150,26 +2128,20 @@ virLXCControllerSetupDevPTS(virLXCControllerPtr ctrl)
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to mount devpts on %s"), _("Failed to mount devpts on %s"),
devpts); devpts);
goto cleanup; return -1;
} }
if (access(ctrl->devptmx, R_OK) < 0) { if (access(ctrl->devptmx, R_OK) < 0) {
virReportSystemError(ENOSYS, "%s", virReportSystemError(ENOSYS, "%s",
_("Kernel does not support private devpts")); _("Kernel does not support private devpts"));
goto cleanup; return -1;
} }
if ((lxcContainerChown(ctrl->def, ctrl->devptmx) < 0) || if ((lxcContainerChown(ctrl->def, ctrl->devptmx) < 0) ||
(lxcContainerChown(ctrl->def, devpts) < 0)) (lxcContainerChown(ctrl->def, devpts) < 0))
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
VIR_FREE(opts);
VIR_FREE(devpts);
VIR_FREE(mount_options);
return ret;
} }
@ -2190,8 +2162,7 @@ virLXCControllerSetupConsoles(virLXCControllerPtr ctrl,
char **containerTTYPaths) char **containerTTYPaths)
{ {
size_t i; size_t i;
int ret = -1; g_autofree char *ttyHostPath = NULL;
char *ttyHostPath = NULL;
for (i = 0; i < ctrl->nconsoles; i++) { for (i = 0; i < ctrl->nconsoles; i++) {
VIR_DEBUG("Opening tty on private %s", ctrl->devptmx); VIR_DEBUG("Opening tty on private %s", ctrl->devptmx);
@ -2200,20 +2171,17 @@ virLXCControllerSetupConsoles(virLXCControllerPtr ctrl,
&containerTTYPaths[i], &ttyHostPath) < 0) { &containerTTYPaths[i], &ttyHostPath) < 0) {
virReportSystemError(errno, "%s", virReportSystemError(errno, "%s",
_("Failed to allocate tty")); _("Failed to allocate tty"));
goto cleanup; return -1;
} }
/* Change the owner of tty device to the root user of container */ /* Change the owner of tty device to the root user of container */
if (lxcContainerChown(ctrl->def, ttyHostPath) < 0) if (lxcContainerChown(ctrl->def, ttyHostPath) < 0)
goto cleanup; return -1;
VIR_FREE(ttyHostPath); VIR_FREE(ttyHostPath);
} }
ret = 0; return 0;
cleanup:
VIR_FREE(ttyHostPath);
return ret;
} }

View File

@ -42,8 +42,7 @@ static const char *fuse_meminfo_path = "/meminfo";
static int lxcProcGetattr(const char *path, struct stat *stbuf) static int lxcProcGetattr(const char *path, struct stat *stbuf)
{ {
int res; g_autofree char *mempath = NULL;
char *mempath = NULL;
struct stat sb; struct stat sb;
struct fuse_context *context = fuse_get_context(); struct fuse_context *context = fuse_get_context();
virDomainDefPtr def = (virDomainDefPtr)context->private_data; virDomainDefPtr def = (virDomainDefPtr)context->private_data;
@ -51,16 +50,12 @@ static int lxcProcGetattr(const char *path, struct stat *stbuf)
memset(stbuf, 0, sizeof(struct stat)); memset(stbuf, 0, sizeof(struct stat));
mempath = g_strdup_printf("/proc/%s", path); mempath = g_strdup_printf("/proc/%s", path);
res = 0;
if (STREQ(path, "/")) { if (STREQ(path, "/")) {
stbuf->st_mode = S_IFDIR | 0755; stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2; stbuf->st_nlink = 2;
} else if (STREQ(path, fuse_meminfo_path)) { } else if (STREQ(path, fuse_meminfo_path)) {
if (stat(mempath, &sb) < 0) { if (stat(mempath, &sb) < 0)
res = -errno; return -errno;
goto cleanup;
}
stbuf->st_uid = def->idmap.uidmap ? def->idmap.uidmap[0].target : 0; stbuf->st_uid = def->idmap.uidmap ? def->idmap.uidmap[0].target : 0;
stbuf->st_gid = def->idmap.gidmap ? def->idmap.gidmap[0].target : 0; stbuf->st_gid = def->idmap.gidmap ? def->idmap.gidmap[0].target : 0;
@ -73,12 +68,10 @@ static int lxcProcGetattr(const char *path, struct stat *stbuf)
stbuf->st_ctime = sb.st_ctime; stbuf->st_ctime = sb.st_ctime;
stbuf->st_mtime = sb.st_mtime; stbuf->st_mtime = sb.st_mtime;
} else { } else {
res = -ENOENT; return -ENOENT;
} }
cleanup: return 0;
VIR_FREE(mempath);
return res;
} }
static int lxcProcReaddir(const char *path, void *buf, static int lxcProcReaddir(const char *path, void *buf,
@ -129,7 +122,7 @@ static int lxcProcReadMeminfo(char *hostpath, virDomainDefPtr def,
{ {
int res; int res;
FILE *fd = NULL; FILE *fd = NULL;
char *line = NULL; g_autofree char *line = NULL;
size_t n; size_t n;
struct virLXCMeminfo meminfo; struct virLXCMeminfo meminfo;
virBuffer buffer = VIR_BUFFER_INITIALIZER; virBuffer buffer = VIR_BUFFER_INITIALIZER;
@ -231,7 +224,6 @@ static int lxcProcReadMeminfo(char *hostpath, virDomainDefPtr def,
memcpy(buf, virBufferCurrentContent(new_meminfo), res); memcpy(buf, virBufferCurrentContent(new_meminfo), res);
cleanup: cleanup:
VIR_FREE(line);
virBufferFreeAndReset(new_meminfo); virBufferFreeAndReset(new_meminfo);
VIR_FORCE_FCLOSE(fd); VIR_FORCE_FCLOSE(fd);
return res; return res;
@ -244,7 +236,7 @@ static int lxcProcRead(const char *path G_GNUC_UNUSED,
struct fuse_file_info *fi G_GNUC_UNUSED) struct fuse_file_info *fi G_GNUC_UNUSED)
{ {
int res = -ENOENT; int res = -ENOENT;
char *hostpath = NULL; g_autofree char *hostpath = NULL;
struct fuse_context *context = NULL; struct fuse_context *context = NULL;
virDomainDefPtr def = NULL; virDomainDefPtr def = NULL;
@ -258,7 +250,6 @@ static int lxcProcRead(const char *path G_GNUC_UNUSED,
res = lxcProcHostRead(hostpath, buf, size, offset); res = lxcProcHostRead(hostpath, buf, size, offset);
} }
VIR_FREE(hostpath);
return res; return res;
} }