[libvirt] [PATCH 47/75] lxc: Use g_strdup_printf() instead of virAsprintf()

Daniel Henrique Barboza danielhb413 at gmail.com
Thu Oct 24 13:10:45 UTC 2019



On 10/22/19 10:57 AM, Michal Privoznik wrote:
> Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
> ---
>   src/lxc/lxc_container.c  | 41 +++++++++++++++++------------------
>   src/lxc/lxc_controller.c | 46 ++++++++++++++++++++--------------------
>   src/lxc/lxc_driver.c     |  8 +++----
>   src/lxc/lxc_fuse.c       |  6 +++---
>   src/lxc/lxc_monitor.c    |  2 +-
>   src/lxc/lxc_native.c     |  8 +++----
>   src/lxc/lxc_process.c    | 20 ++++++++---------
>   7 files changed, 65 insertions(+), 66 deletions(-)
> 
> diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
> index 090a22571d..0ad872b65b 100644
> --- a/src/lxc/lxc_container.c
> +++ b/src/lxc/lxc_container.c
> @@ -682,7 +682,7 @@ static int lxcContainerPrepareRoot(virDomainDefPtr def,
>       if (lxcContainerResolveSymlinks(root, false) < 0)
>           return -1;
>   
> -    virAsprintf(&dst, "%s/%s.root", LXC_STATE_DIR, def->name);
> +    dst = g_strdup_printf("%s/%s.root", LXC_STATE_DIR, def->name);
>   
>       tmp = root->dst;
>       root->dst = dst;
> @@ -717,7 +717,7 @@ static int lxcContainerPivotRoot(virDomainFSDefPtr root)
>           goto err;
>       }
>   
> -    virAsprintf(&oldroot, "%s/.oldroot", root->src->path);
> +    oldroot = g_strdup_printf("%s/.oldroot", root->src->path);
>   
>       if (virFileMakePath(oldroot) < 0) {
>           virReportSystemError(errno,
> @@ -736,7 +736,7 @@ static int lxcContainerPivotRoot(virDomainFSDefPtr root)
>       }
>   
>       /* Create a directory called 'new' in tmpfs */
> -    virAsprintf(&newroot, "%s/new", oldroot);
> +    newroot = g_strdup_printf("%s/new", oldroot);
>   
>       if (virFileMakePath(newroot) < 0) {
>           virReportSystemError(errno,
> @@ -921,7 +921,7 @@ static int lxcContainerMountBasicFS(bool userns_enabled,
>               char *hostdir;
>               int ret;
>   
> -            virAsprintf(&hostdir, "/.oldroot%s", mnt->dst);
> +            hostdir = g_strdup_printf("/.oldroot%s", mnt->dst);
>   
>               ret = virFileIsMountPoint(hostdir);
>               VIR_FREE(hostdir);
> @@ -1012,10 +1012,9 @@ static int lxcContainerMountProcFuse(virDomainDefPtr def,
>   
>       VIR_DEBUG("Mount /proc/meminfo stateDir=%s", stateDir);
>   
> -    virAsprintf(&meminfo_path,
> -                "/.oldroot/%s/%s.fuse/meminfo",
> -                stateDir,
> -                def->name);
> +    meminfo_path = g_strdup_printf("/.oldroot/%s/%s.fuse/meminfo",
> +                                   stateDir,
> +                                   def->name);
>   
>       if ((ret = mount(meminfo_path, "/proc/meminfo",
>                        NULL, MS_BIND, NULL)) < 0) {
> @@ -1044,7 +1043,7 @@ static int lxcContainerMountFSDev(virDomainDefPtr def,
>   
>       VIR_DEBUG("Mount /dev/ stateDir=%s", stateDir);
>   
> -    virAsprintf(&path, "/.oldroot/%s/%s.dev", stateDir, def->name);
> +    path = g_strdup_printf("/.oldroot/%s/%s.dev", stateDir, def->name);
>   
>       if (virFileMakePath("/dev") < 0) {
>           virReportSystemError(errno, "%s",
> @@ -1078,7 +1077,7 @@ static int lxcContainerMountFSDevPTS(virDomainDefPtr def,
>   
>       VIR_DEBUG("Mount /dev/pts stateDir=%s", stateDir);
>   
> -    virAsprintf(&path, "/.oldroot/%s/%s.devpts", stateDir, def->name);
> +    path = g_strdup_printf("/.oldroot/%s/%s.devpts", stateDir, def->name);
>   
>       if (virFileMakePath("/dev/pts") < 0) {
>           virReportSystemError(errno, "%s",
> @@ -1130,7 +1129,7 @@ static int lxcContainerSetupDevices(char **ttyPaths, size_t nttyPaths)
>   
>       for (i = 0; i < nttyPaths; i++) {
>           char *tty;
> -        virAsprintf(&tty, "/dev/tty%zu", i + 1);
> +        tty = g_strdup_printf("/dev/tty%zu", i + 1);
>   
>           if (virFileBindMountDevice(ttyPaths[i], tty) < 0) {
>               VIR_FREE(tty);
> @@ -1156,7 +1155,7 @@ static int lxcContainerMountFSBind(virDomainFSDefPtr fs,
>   
>       VIR_DEBUG("src=%s dst=%s", fs->src->path, fs->dst);
>   
> -    virAsprintf(&src, "%s%s", srcprefix, fs->src->path);
> +    src = g_strdup_printf("%s%s", srcprefix, fs->src->path);
>   
>       if (stat(fs->dst, &st) < 0) {
>           if (errno != ENOENT) {
> @@ -1326,8 +1325,8 @@ static int lxcContainerMountFSBlockAuto(virDomainFSDefPtr fs,
>   
>       /* First time around we use /etc/filesystems */
>    retry:
> -    virAsprintf(&fslist, "%s%s", srcprefix,
> -                tryProc ? "/proc/filesystems" : "/etc/filesystems");
> +    fslist = g_strdup_printf("%s%s", srcprefix,
> +                             tryProc ? "/proc/filesystems" : "/etc/filesystems");
>   
>       VIR_DEBUG("Open fslist %s", fslist);
>       if (!(fp = fopen(fslist, "r"))) {
> @@ -1488,7 +1487,7 @@ static int lxcContainerMountFSBlock(virDomainFSDefPtr fs,
>   
>       VIR_DEBUG("src=%s dst=%s", fs->src->path, fs->dst);
>   
> -    virAsprintf(&src, "%s%s", srcprefix, fs->src->path);
> +    src = g_strdup_printf("%s%s", srcprefix, fs->src->path);
>   
>       ret = lxcContainerMountFSBlockHelper(fs, src, srcprefix, sec_mount_options);
>   
> @@ -1507,7 +1506,7 @@ static int lxcContainerMountFSTmpfs(virDomainFSDefPtr fs,
>   
>       VIR_DEBUG("usage=%lld sec=%s", fs->usage, sec_mount_options);
>   
> -    virAsprintf(&data, "size=%lld%s", fs->usage, sec_mount_options);
> +    data = g_strdup_printf("size=%lld%s", fs->usage, sec_mount_options);
>   
>       if (virFileMakePath(fs->dst) < 0) {
>           virReportSystemError(errno,
> @@ -1655,20 +1654,20 @@ static int lxcContainerUnmountForSharedRoot(const char *stateDir,
>   
>       /* These filesystems are created by libvirt temporarily, they
>        * shouldn't appear in container. */
> -    virAsprintf(&tmp, "%s/%s.dev", stateDir, domain);
> +    tmp = g_strdup_printf("%s/%s.dev", stateDir, domain);
>   
>       if (lxcContainerUnmountSubtree(tmp, false) < 0)
>           goto cleanup;
>   
>       VIR_FREE(tmp);
> -    virAsprintf(&tmp, "%s/%s.devpts", stateDir, domain);
> +    tmp = g_strdup_printf("%s/%s.devpts", stateDir, domain);
>   
>       if (lxcContainerUnmountSubtree(tmp, false) < 0)
>           goto cleanup;
>   
>   #if WITH_FUSE
>       VIR_FREE(tmp);
> -    virAsprintf(&tmp, "%s/%s.fuse", stateDir, domain);
> +    tmp = g_strdup_printf("%s/%s.fuse", stateDir, domain);
>   
>       if (lxcContainerUnmountSubtree(tmp, false) < 0)
>           goto cleanup;
> @@ -2232,8 +2231,8 @@ static int lxcContainerChild(void *data)
>           const char *tty = argv->ttyPaths[0];
>           if (STRPREFIX(tty, "/dev/pts/"))
>               tty += strlen("/dev/pts/");
> -        virAsprintf(&ttyPath, "%s/%s.devpts/%s", LXC_STATE_DIR, vmDef->name,
> -                    tty);
> +        ttyPath = g_strdup_printf("%s/%s.devpts/%s", LXC_STATE_DIR, vmDef->name,
> +                                  tty);
>       } else {
>           virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
>                          _("At least one tty is required"));
> diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
> index 56e30eeaa9..17c4282be4 100644
> --- a/src/lxc/lxc_controller.c
> +++ b/src/lxc/lxc_controller.c
> @@ -568,7 +568,7 @@ static int virLXCControllerAppendNBDPids(virLXCControllerPtr ctrl,
>       if (!STRPREFIX(dev, "/dev/"))
>           goto cleanup;
>   
> -    virAsprintf(&pidpath, "/sys/devices/virtual/block/%s/pid", dev + 5);
> +    pidpath = g_strdup_printf("/sys/devices/virtual/block/%s/pid", dev + 5);
>   
>       /* Wait for the pid file to appear */
>       while (!virFileExists(pidpath)) {
> @@ -946,7 +946,7 @@ static int virLXCControllerSetupServer(virLXCControllerPtr ctrl)
>       virNetServerServicePtr svc = NULL;
>       char *sockpath;
>   
> -    virAsprintf(&sockpath, "%s/%s.sock", LXC_STATE_DIR, ctrl->name);
> +    sockpath = g_strdup_printf("%s/%s.sock", LXC_STATE_DIR, ctrl->name);
>   
>       if (!(srv = virNetServerNew("LXC", 1,
>                                   0, 0, 0, 1,
> @@ -1434,14 +1434,14 @@ static int virLXCControllerSetupUserns(virLXCControllerPtr ctrl)
>       }
>   
>       VIR_DEBUG("Setting up userns maps");
> -    virAsprintf(&uid_map, "/proc/%d/uid_map", ctrl->initpid);
> +    uid_map = g_strdup_printf("/proc/%d/uid_map", ctrl->initpid);
>   
>       if (virLXCControllerSetupUsernsMap(ctrl->def->idmap.uidmap,
>                                          ctrl->def->idmap.nuidmap,
>                                          uid_map) < 0)
>           goto cleanup;
>   
> -    virAsprintf(&gid_map, "/proc/%d/gid_map", ctrl->initpid);
> +    gid_map = g_strdup_printf("/proc/%d/gid_map", ctrl->initpid);
>   
>       if (virLXCControllerSetupUsernsMap(ctrl->def->idmap.gidmap,
>                                          ctrl->def->idmap.ngidmap,
> @@ -1467,14 +1467,14 @@ static int virLXCControllerSetupDev(virLXCControllerPtr ctrl)
>       mount_options = virSecurityManagerGetMountOptions(ctrl->securityManager,
>                                                         ctrl->def);
>   
> -    virAsprintf(&dev, "/%s/%s.dev", LXC_STATE_DIR, ctrl->def->name);
> +    dev = g_strdup_printf("/%s/%s.dev", LXC_STATE_DIR, ctrl->def->name);
>   
>       /*
>        * tmpfs is limited to 64kb, since we only have device nodes in there
>        * and don't want to DOS the entire OS RAM usage
>        */
>   
> -    virAsprintf(&opts, "mode=755,size=65536%s", mount_options);
> +    opts = g_strdup_printf("mode=755,size=65536%s", mount_options);
>   
>       if (virFileSetupDev(dev, opts) < 0)
>           goto cleanup;
> @@ -1514,8 +1514,8 @@ static int virLXCControllerPopulateDevices(virLXCControllerPtr ctrl)
>   
>       /* Populate /dev/ with a few important bits */
>       for (i = 0; i < G_N_ELEMENTS(devs); i++) {
> -        virAsprintf(&path, "/%s/%s.dev/%s", LXC_STATE_DIR, ctrl->def->name,
> -                    devs[i].path);
> +        path = g_strdup_printf("/%s/%s.dev/%s", LXC_STATE_DIR, ctrl->def->name,
> +                               devs[i].path);
>   
>           dev_t dev = makedev(devs[i].maj, devs[i].min);
>           if (mknod(path, S_IFCHR, dev) < 0 ||
> @@ -1553,13 +1553,13 @@ virLXCControllerSetupHostdevSubsysUSB(virDomainDefPtr vmDef,
>       mode_t mode;
>       virDomainHostdevSubsysUSBPtr usbsrc = &def->source.subsys.u.usb;
>   
> -    virAsprintf(&src, USB_DEVFS "/%03d/%03d", usbsrc->bus, usbsrc->device);
> +    src = g_strdup_printf(USB_DEVFS "/%03d/%03d", usbsrc->bus, usbsrc->device);
>   
> -    virAsprintf(&vroot, "/%s/%s.dev/bus/usb/", LXC_STATE_DIR, vmDef->name);
> +    vroot = g_strdup_printf("/%s/%s.dev/bus/usb/", LXC_STATE_DIR, vmDef->name);
>   
> -    virAsprintf(&dstdir, "%s/%03d/", vroot, usbsrc->bus);
> +    dstdir = g_strdup_printf("%s/%03d/", vroot, usbsrc->bus);
>   
> -    virAsprintf(&dstfile, "%s/%03d", dstdir, usbsrc->device);
> +    dstfile = g_strdup_printf("%s/%03d", dstdir, usbsrc->device);
>   
>       if (stat(src, &sb) < 0) {
>           virReportSystemError(errno,
> @@ -1633,7 +1633,7 @@ virLXCControllerSetupHostdevCapsStorage(virDomainDefPtr vmDef,
>       while (*(path + len) == '/')
>           len++;
>   
> -    virAsprintf(&dst, "/%s/%s.dev/%s", LXC_STATE_DIR, vmDef->name,
> +    dst = g_strdup_printf("/%s/%s.dev/%s", LXC_STATE_DIR, vmDef->name,
>                   strchr(path + len, '/'));
>   


This one got misaligned.



>       if (stat(dev, &sb) < 0) {
> @@ -1709,8 +1709,8 @@ virLXCControllerSetupHostdevCapsMisc(virDomainDefPtr vmDef,
>       while (*(path + len) == '/')
>           len++;
>   
> -    virAsprintf(&dst, "/%s/%s.dev/%s", LXC_STATE_DIR, vmDef->name,
> -                strchr(path + len, '/'));
> +    dst = g_strdup_printf("/%s/%s.dev/%s", LXC_STATE_DIR, vmDef->name,
> +                          strchr(path + len, '/'));
>   
>       if (stat(dev, &sb) < 0) {
>           virReportSystemError(errno,
> @@ -1865,8 +1865,8 @@ static int virLXCControllerSetupDisk(virLXCControllerPtr ctrl,
>           goto cleanup;
>       }
>   
> -    virAsprintf(&dst, "/%s/%s.dev/%s", LXC_STATE_DIR, ctrl->def->name,
> -                def->dst);
> +    dst = g_strdup_printf("/%s/%s.dev/%s", LXC_STATE_DIR, ctrl->def->name,
> +                          def->dst);
>   
>       if (stat(def->src->path, &sb) < 0) {
>           virReportSystemError(errno,
> @@ -2055,8 +2055,8 @@ lxcCreateTty(virLXCControllerPtr ctrl, int *ttymaster,
>        * while glibc has to fstat(), fchmod(), and fchown() for older
>        * kernels, we can skip those steps.  ptyno shouldn't currently be
>        * anything other than 0, but let's play it safe.  */
> -    virAsprintf(ttyName, "/dev/pts/%d", ptyno);
> -    virAsprintf(ttyHostPath, "/%s/%s.devpts/%d", LXC_STATE_DIR, ctrl->def->name, ptyno);
> +    *ttyName = g_strdup_printf("/dev/pts/%d", ptyno);
> +    *ttyHostPath = g_strdup_printf("/%s/%s.devpts/%d", LXC_STATE_DIR, ctrl->def->name, ptyno);
>   
>       ret = 0;
>   
> @@ -2112,8 +2112,8 @@ virLXCControllerSetupDevPTS(virLXCControllerPtr ctrl)
>       mount_options = virSecurityManagerGetMountOptions(ctrl->securityManager,
>                                                         ctrl->def);
>   
> -    virAsprintf(&devpts, "%s/%s.devpts", LXC_STATE_DIR, ctrl->def->name);
> -    virAsprintf(&ctrl->devptmx, "%s/%s.devpts/ptmx", LXC_STATE_DIR, ctrl->def->name);
> +    devpts = g_strdup_printf("%s/%s.devpts", LXC_STATE_DIR, ctrl->def->name);
> +    ctrl->devptmx = g_strdup_printf("%s/%s.devpts/ptmx", LXC_STATE_DIR, ctrl->def->name);
>   
>       if (virFileMakePath(devpts) < 0) {
>           virReportSystemError(errno,
> @@ -2129,8 +2129,8 @@ virLXCControllerSetupDevPTS(virLXCControllerPtr ctrl)
>   
>       /* XXX should we support gid=X for X!=5 for distros which use
>        * a different gid for tty?  */
> -    virAsprintf(&opts, "newinstance,ptmxmode=0666,mode=0620,gid=%u%s", ptsgid,
> -                NULLSTR_EMPTY(mount_options));
> +    opts = g_strdup_printf("newinstance,ptmxmode=0666,mode=0620,gid=%u%s", ptsgid,
> +                           NULLSTR_EMPTY(mount_options));
>   
>       VIR_DEBUG("Mount devpts on %s type=tmpfs flags=0x%x, opts=%s",
>                 devpts, MS_NOSUID, opts);
> diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
> index acaeae9311..2de252ee87 100644
> --- a/src/lxc/lxc_driver.c
> +++ b/src/lxc/lxc_driver.c
> @@ -3788,7 +3788,7 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
>       if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks + 1) < 0)
>           goto cleanup;
>   
> -    virAsprintf(&file, "/dev/%s", def->dst);
> +    file = g_strdup_printf("/dev/%s", def->dst);
>   
>       if (lxcDomainAttachDeviceMknod(driver,
>                                      0700 | S_IFBLK,
> @@ -3973,7 +3973,7 @@ lxcDomainAttachDeviceHostdevSubsysUSBLive(virLXCDriverPtr driver,
>       }
>   
>       usbsrc = &def->source.subsys.u.usb;
> -    virAsprintf(&src, "/dev/bus/usb/%03d/%03d", usbsrc->bus, usbsrc->device);
> +    src = g_strdup_printf("/dev/bus/usb/%03d/%03d", usbsrc->bus, usbsrc->device);
>   
>       if (!(usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device, NULL)))
>           goto cleanup;
> @@ -4309,7 +4309,7 @@ lxcDomainDetachDeviceDiskLive(virDomainObjPtr vm,
>       def = vm->def->disks[idx];
>       src = virDomainDiskGetSource(def);
>   
> -    virAsprintf(&dst, "/dev/%s", def->dst);
> +    dst = g_strdup_printf("/dev/%s", def->dst);
>   
>       if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
>           virReportError(VIR_ERR_OPERATION_INVALID, "%s",
> @@ -4447,7 +4447,7 @@ lxcDomainDetachDeviceHostdevUSBLive(virLXCDriverPtr driver,
>       }
>   
>       usbsrc = &def->source.subsys.u.usb;
> -    virAsprintf(&dst, "/dev/bus/usb/%03d/%03d", usbsrc->bus, usbsrc->device);
> +    dst = g_strdup_printf("/dev/bus/usb/%03d/%03d", usbsrc->bus, usbsrc->device);
>   
>       if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
>           virReportError(VIR_ERR_OPERATION_INVALID, "%s",
> diff --git a/src/lxc/lxc_fuse.c b/src/lxc/lxc_fuse.c
> index 35dbad7986..3e2ad197d3 100644
> --- a/src/lxc/lxc_fuse.c
> +++ b/src/lxc/lxc_fuse.c
> @@ -47,7 +47,7 @@ static int lxcProcGetattr(const char *path, struct stat *stbuf)
>       virDomainDefPtr def = (virDomainDefPtr)context->private_data;
>   
>       memset(stbuf, 0, sizeof(struct stat));
> -    virAsprintf(&mempath, "/proc/%s", path);
> +    mempath = g_strdup_printf("/proc/%s", path);
>   
>       res = 0;
>   
> @@ -250,7 +250,7 @@ static int lxcProcRead(const char *path G_GNUC_UNUSED,
>       struct fuse_context *context = NULL;
>       virDomainDefPtr def = NULL;
>   
> -    virAsprintf(&hostpath, "/proc/%s", path);
> +    hostpath = g_strdup_printf("/proc/%s", path);
>   
>       context = fuse_get_context();
>       def = (virDomainDefPtr)context->private_data;
> @@ -305,7 +305,7 @@ int lxcSetupFuse(virLXCFusePtr *f, virDomainDefPtr def)
>       if (virMutexInit(&fuse->lock) < 0)
>           goto cleanup2;
>   
> -    virAsprintf(&fuse->mountpoint, "%s/%s.fuse/", LXC_STATE_DIR, def->name);
> +    fuse->mountpoint = g_strdup_printf("%s/%s.fuse/", LXC_STATE_DIR, def->name);
>   
>       if (virFileMakePath(fuse->mountpoint) < 0) {
>           virReportSystemError(errno, _("Cannot create %s"),
> diff --git a/src/lxc/lxc_monitor.c b/src/lxc/lxc_monitor.c
> index 1983af135e..479125374b 100644
> --- a/src/lxc/lxc_monitor.c
> +++ b/src/lxc/lxc_monitor.c
> @@ -151,7 +151,7 @@ virLXCMonitorPtr virLXCMonitorNew(virDomainObjPtr vm,
>       if (!(mon = virObjectLockableNew(virLXCMonitorClass)))
>           return NULL;
>   
> -    virAsprintf(&sockpath, "%s/%s.sock", socketdir, vm->def->name);
> +    sockpath = g_strdup_printf("%s/%s.sock", socketdir, vm->def->name);
>   
>       if (!(mon->client = virNetClientNewUNIX(sockpath, false, NULL)))
>           goto error;
> diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c
> index c7cf2c4b0e..b9dffd6de9 100644
> --- a/src/lxc/lxc_native.c
> +++ b/src/lxc/lxc_native.c
> @@ -265,7 +265,7 @@ lxcAddFstabLine(virDomainDefPtr def, lxcFstabPtr fstab)
>           return -1;
>   
>       if (fstab->dst[0] != '/') {
> -        virAsprintf(&dst, "/%s", fstab->dst);
> +        dst = g_strdup_printf("/%s", fstab->dst);
>       } else {
>           dst = g_strdup(fstab->dst);
>       }
> @@ -491,8 +491,8 @@ lxcAddNetworkDefinition(lxcNetworkParseData *data)
>            * on the host */
>           if (isVlan && data->vlanid) {
>               VIR_FREE(hostdev->source.caps.u.net.ifname);
> -            virAsprintf(&hostdev->source.caps.u.net.ifname, "%s.%s",
> -                        data->link, data->vlanid);
> +            hostdev->source.caps.u.net.ifname = g_strdup_printf("%s.%s",
> +                                                                data->link, data->vlanid);


Perhaps put data->vlanid in the next line to make the data->link line a 
bit shorter.


Reviewed-by: Daniel Henrique Barboza <danielhb413 at gmail.com>




More information about the libvir-list mailing list