[libvirt PATCH 00/14] use g_auto for virCommand (Episode I: The End)

A partial cleanup. The rest is in progress (TM) Ján Tomko (14): bhyve: refactor virBhyveProbeGrubCaps bhyve: refactor bhyveProbeCapsDeviceHelper bhyve: refactor bhyveProbeCapsFromHelp bhyve: refactor bhyveConnectDomainXMLToNative bhyve: use g_auto in virBhyveProcessStartImpl bhyve: use g_auto in virBhyveProcessStop bhyve: refactor virBhyveProcessBuildBhyveCmd lxc: use g_auto in lxcContainerChild lxc: refactor virLXCProcessBuildControllerCmd lxc: use g_auto for virCommand in virLXCProcessEnsureRootFS storage: logical: use two cmd vars in GetPoolSources storage: util: steal cmd in CreateQemuImgCmdFromVol security: apparmor: use automatic cleanup in load_profile qemu: use automatic cleanup for virCommand src/bhyve/bhyve_capabilities.c | 57 ++++++++++----------------- src/bhyve/bhyve_command.c | 36 ++++++++--------- src/bhyve/bhyve_driver.c | 28 ++++++------- src/bhyve/bhyve_process.c | 25 ++++-------- src/lxc/lxc_container.c | 3 +- src/lxc/lxc_process.c | 14 ++----- src/qemu/qemu_driver.c | 3 +- src/qemu/qemu_interface.c | 3 +- src/qemu/qemu_tpm.c | 4 +- src/security/security_apparmor.c | 15 ++----- src/storage/storage_backend_logical.c | 18 ++++----- src/storage/storage_util.c | 5 +-- 12 files changed, 78 insertions(+), 133 deletions(-) -- 2.31.1

Use g_auto and remove the 'ret' variable, as well as the cleanup label. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/bhyve/bhyve_capabilities.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/bhyve/bhyve_capabilities.c b/src/bhyve/bhyve_capabilities.c index 5a0c6c2b77..ed0df35518 100644 --- a/src/bhyve/bhyve_capabilities.c +++ b/src/bhyve/bhyve_capabilities.c @@ -168,35 +168,27 @@ virBhyveDomainCapsBuild(struct _bhyveConn *conn, int virBhyveProbeGrubCaps(virBhyveGrubCapsFlags *caps) { - char *binary, *help; - virCommand *cmd; - int ret, exit; + g_autofree char *binary = NULL; + g_autofree char *help = NULL; + g_autoptr(virCommand) cmd = NULL; + int exit; - ret = 0; *caps = 0; - cmd = NULL; - help = NULL; binary = virFindFileInPath("grub-bhyve"); - if (binary == NULL) - goto out; + if (!binary) + return 0; cmd = virCommandNew(binary); virCommandAddArg(cmd, "--help"); virCommandSetOutputBuffer(cmd, &help); - if (virCommandRun(cmd, &exit) < 0) { - ret = -1; - goto out; - } + if (virCommandRun(cmd, &exit) < 0) + return -1; if (strstr(help, "--cons-dev") != NULL) *caps |= BHYVE_GRUB_CAP_CONSDEV; - out: - VIR_FREE(help); - virCommandFree(cmd); - VIR_FREE(binary); - return ret; + return 0; } static int -- 2.31.1

Use g_auto and remove the 'ret' variable, as well as the cleanup label. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/bhyve/bhyve_capabilities.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/bhyve/bhyve_capabilities.c b/src/bhyve/bhyve_capabilities.c index ed0df35518..421d0622cd 100644 --- a/src/bhyve/bhyve_capabilities.c +++ b/src/bhyve/bhyve_capabilities.c @@ -199,24 +199,20 @@ bhyveProbeCapsDeviceHelper(unsigned int *caps, const char *errormsg, unsigned int flag) { - char *error; - virCommand *cmd = NULL; - int ret = -1, exit; + g_autofree char *error = NULL; + g_autoptr(virCommand) cmd = NULL; + int exit; cmd = virCommandNew(binary); virCommandAddArgList(cmd, bus, device, NULL); virCommandSetErrorBuffer(cmd, &error); if (virCommandRun(cmd, &exit) < 0) - goto cleanup; + return -1; if (strstr(error, errormsg) == NULL) *caps |= flag; - ret = 0; - cleanup: - VIR_FREE(error); - virCommandFree(cmd); - return ret; + return 0; } static int -- 2.31.1

Use g_auto and remove the 'ret' variable, as well as the out label. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/bhyve/bhyve_capabilities.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/bhyve/bhyve_capabilities.c b/src/bhyve/bhyve_capabilities.c index 421d0622cd..b065256cf0 100644 --- a/src/bhyve/bhyve_capabilities.c +++ b/src/bhyve/bhyve_capabilities.c @@ -218,17 +218,15 @@ bhyveProbeCapsDeviceHelper(unsigned int *caps, static int bhyveProbeCapsFromHelp(unsigned int *caps, char *binary) { - char *help; - virCommand *cmd = NULL; - int ret = 0, exit; + g_autofree char *help = NULL; + g_autoptr(virCommand) cmd = NULL; + int exit; cmd = virCommandNew(binary); virCommandAddArg(cmd, "-h"); virCommandSetErrorBuffer(cmd, &help); - if (virCommandRun(cmd, &exit) < 0) { - ret = -1; - goto out; - } + if (virCommandRun(cmd, &exit) < 0) + return -1; if (strstr(help, "-u:") != NULL) *caps |= BHYVE_CAP_RTC_UTC; @@ -239,10 +237,7 @@ bhyveProbeCapsFromHelp(unsigned int *caps, char *binary) if (strstr(help, "-c vcpus") == NULL) *caps |= BHYVE_CAP_CPUTOPOLOGY; - out: - VIR_FREE(help); - virCommandFree(cmd); - return ret; + return 0; } static int -- 2.31.1

Use g_auto and remove the ret variable, as well as the cleanup label. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/bhyve/bhyve_driver.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c index 21407a3957..f291f12e50 100644 --- a/src/bhyve/bhyve_driver.c +++ b/src/bhyve/bhyve_driver.c @@ -671,27 +671,26 @@ bhyveConnectDomainXMLToNative(virConnectPtr conn, g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; struct _bhyveConn *privconn = conn->privateData; g_autoptr(virDomainDef) def = NULL; - virCommand *cmd = NULL; - virCommand *loadcmd = NULL; - char *ret = NULL; + g_autoptr(virCommand) cmd = NULL; + g_autoptr(virCommand) loadcmd = NULL; virCheckFlags(0, NULL); if (virConnectDomainXMLToNativeEnsureACL(conn) < 0) - goto cleanup; + return NULL; if (STRNEQ(format, BHYVE_CONFIG_FORMAT_ARGV)) { virReportError(VIR_ERR_INVALID_ARG, _("Unsupported config type %s"), format); - goto cleanup; + return NULL; } if (!(def = virDomainDefParseString(xmlData, privconn->xmlopt, NULL, VIR_DOMAIN_DEF_PARSE_INACTIVE))) - goto cleanup; + return NULL; if (bhyveDomainAssignAddresses(def, NULL) < 0) - goto cleanup; + return NULL; if (def->os.bootloader == NULL && def->os.loader) { @@ -699,35 +698,30 @@ bhyveConnectDomainXMLToNative(virConnectPtr conn, if (!virDomainDefHasOldStyleROUEFI(def)) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Only read-only pflash is supported.")); - goto cleanup; + return NULL; } if ((bhyveDriverGetBhyveCaps(privconn) & BHYVE_CAP_LPC_BOOTROM) == 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Installed bhyve binary does not support " "bootrom")); - goto cleanup; + return NULL; } } else { if (!(loadcmd = virBhyveProcessBuildLoadCmd(privconn, def, "<device.map>", NULL))) - goto cleanup; + return NULL; virCommandToStringBuf(loadcmd, &buf, false, false); virBufferAddChar(&buf, '\n'); } if (!(cmd = virBhyveProcessBuildBhyveCmd(privconn, def, true))) - goto cleanup; + return NULL; virCommandToStringBuf(cmd, &buf, false, false); - ret = virBufferContentAndReset(&buf); - - cleanup: - virCommandFree(loadcmd); - virCommandFree(cmd); - return ret; + return virBufferContentAndReset(&buf); } static int -- 2.31.1

Use g_auto and VIR_AUTOCLOSE where possible. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/bhyve/bhyve_process.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/bhyve/bhyve_process.c b/src/bhyve/bhyve_process.c index 8de5fe4838..ead6b0e522 100644 --- a/src/bhyve/bhyve_process.c +++ b/src/bhyve/bhyve_process.c @@ -116,12 +116,12 @@ virBhyveProcessStartImpl(struct _bhyveConn *driver, virDomainObj *vm, virDomainRunningReason reason) { - char *devmap_file = NULL; - char *devicemap = NULL; - char *logfile = NULL; - int logfd = -1; - virCommand *cmd = NULL; - virCommand *load_cmd = NULL; + g_autofree char *devmap_file = NULL; + g_autofree char *devicemap = NULL; + g_autofree char *logfile = NULL; + VIR_AUTOCLOSE logfd = -1; + g_autoptr(virCommand) cmd = NULL; + g_autoptr(virCommand) load_cmd = NULL; bhyveDomainObjPrivate *priv = vm->privateData; int ret = -1, rc; @@ -227,28 +227,21 @@ virBhyveProcessStartImpl(struct _bhyveConn *driver, if (rc < 0 && errno != ENOENT) virReportSystemError(errno, _("cannot unlink file '%s'"), devmap_file); - VIR_FREE(devicemap); } - VIR_FREE(devmap_file); if (ret < 0) { int exitstatus; /* Needed to avoid logging non-zero status */ - virCommand *destroy_cmd; + g_autoptr(virCommand) destroy_cmd = NULL; if ((destroy_cmd = virBhyveProcessBuildDestroyCmd(driver, vm->def)) != NULL) { virCommandSetOutputFD(load_cmd, &logfd); virCommandSetErrorFD(load_cmd, &logfd); ignore_value(virCommandRun(destroy_cmd, &exitstatus)); - virCommandFree(destroy_cmd); } bhyveNetCleanup(vm); } - virCommandFree(load_cmd); - virCommandFree(cmd); - VIR_FREE(logfile); - VIR_FORCE_CLOSE(logfd); return ret; } -- 2.31.1

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/bhyve/bhyve_process.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/bhyve/bhyve_process.c b/src/bhyve/bhyve_process.c index ead6b0e522..ee692d2ba3 100644 --- a/src/bhyve/bhyve_process.c +++ b/src/bhyve/bhyve_process.c @@ -285,7 +285,7 @@ virBhyveProcessStop(struct _bhyveConn *driver, virDomainShutoffReason reason) { int ret = -1; - virCommand *cmd = NULL; + g_autoptr(virCommand) cmd = NULL; bhyveDomainObjPrivate *priv = vm->privateData; if (!virDomainObjIsActive(vm)) { @@ -335,8 +335,6 @@ virBhyveProcessStop(struct _bhyveConn *driver, bhyveProcessStopHook(vm, VIR_HOOK_BHYVE_OP_RELEASE); cleanup: - virCommandFree(cmd); - virPidFileDelete(BHYVE_STATE_DIR, vm->def->name); virDomainDeleteConfig(BHYVE_STATE_DIR, NULL, vm); -- 2.31.1

Use automatic cleanup for virCommand, steal it on success and remove the error label. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/bhyve/bhyve_command.c | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/bhyve/bhyve_command.c b/src/bhyve/bhyve_command.c index cf858dfcd6..3368d20a04 100644 --- a/src/bhyve/bhyve_command.c +++ b/src/bhyve/bhyve_command.c @@ -649,7 +649,7 @@ virBhyveProcessBuildBhyveCmd(struct _bhyveConn *driver, virDomainDef *def, * -S 31,uart,stdio \ * vm0 */ - virCommand *cmd = virCommandNew(BHYVE); + g_autoptr(virCommand) cmd = virCommandNew(BHYVE); size_t i; unsigned nusbcontrollers = 0; unsigned nisacontrollers = 0; @@ -661,14 +661,14 @@ virBhyveProcessBuildBhyveCmd(struct _bhyveConn *driver, virDomainDef *def, if (def->cpu->dies != 1) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Only 1 die per socket is supported")); - goto error; + return NULL; } if (nvcpus != def->cpu->sockets * def->cpu->cores * def->cpu->threads) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Invalid CPU topology: total number of vCPUs " "must equal the product of sockets, cores, " "and threads")); - goto error; + return NULL; } if ((bhyveDriverGetBhyveCaps(driver) & BHYVE_CAP_CPUTOPOLOGY) != 0) { @@ -681,7 +681,7 @@ virBhyveProcessBuildBhyveCmd(struct _bhyveConn *driver, virDomainDef *def, virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Installed bhyve binary does not support " "defining CPU topology")); - goto error; + return NULL; } } else { virCommandAddArgFormat(cmd, "%d", nvcpus); @@ -716,14 +716,14 @@ virBhyveProcessBuildBhyveCmd(struct _bhyveConn *driver, virDomainDef *def, virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Installed bhyve binary does not support " "UTC clock")); - goto error; + return NULL; } break; default: virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unsupported clock offset '%s'"), virDomainClockOffsetTypeToString(def->clock.offset)); - goto error; + return NULL; } /* Clarification about -H and -P flags from Peter Grehan: @@ -751,7 +751,7 @@ virBhyveProcessBuildBhyveCmd(struct _bhyveConn *driver, virDomainDef *def, virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Installed bhyve binary does not support " "UEFI loader")); - goto error; + return NULL; } } @@ -759,26 +759,26 @@ virBhyveProcessBuildBhyveCmd(struct _bhyveConn *driver, virDomainDef *def, for (i = 0; i < def->ncontrollers; i++) { if (bhyveBuildControllerArgStr(def, def->controllers[i], driver, cmd, &nusbcontrollers, &nisacontrollers) < 0) - goto error; + return NULL; } for (i = 0; i < def->nnets; i++) { if (bhyveBuildNetArgStr(def, def->nets[i], driver, cmd, dryRun) < 0) - goto error; + return NULL; } for (i = 0; i < def->ndisks; i++) { if (bhyveBuildDiskArgStr(def, def->disks[i], cmd) < 0) - goto error; + return NULL; } if (def->ngraphics && def->nvideos) { if (def->ngraphics == 1 && def->nvideos == 1) { if (bhyveBuildGraphicsArgStr(def, def->graphics[0], def->videos[0], driver, cmd, dryRun) < 0) - goto error; + return NULL; } else { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Multiple graphics devices are not supported")); - goto error; + return NULL; } } @@ -786,16 +786,16 @@ virBhyveProcessBuildBhyveCmd(struct _bhyveConn *driver, virDomainDef *def, if (bhyveBuildSoundArgStr(def, def->sounds[i], virDomainDefFindAudioByID(def, def->sounds[i]->audioId), driver, cmd) < 0) - goto error; + return NULL; } for (i = 0; i < def->nfss; i++) { if (bhyveBuildFSArgStr(def, def->fss[i], cmd) < 0) - goto error; + return NULL; } if (bhyveBuildConsoleArgStr(def, cmd) < 0) - goto error; + return NULL; if (def->namespaceData) { bhyveDomainCmdlineDef *bhyvecmd; @@ -811,11 +811,7 @@ virBhyveProcessBuildBhyveCmd(struct _bhyveConn *driver, virDomainDef *def, virCommandAddArg(cmd, def->name); - return cmd; - - error: - virCommandFree(cmd); - return NULL; + return g_steal_pointer(&cmd); } virCommand * -- 2.31.1

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/lxc/lxc_container.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 1d5b9bf429..3f38c55fc6 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -1927,7 +1927,7 @@ static int lxcContainerChild(void *data) int ret = -1; g_autofree char *ttyPath = NULL; virDomainFSDef *root; - virCommand *cmd = NULL; + g_autoptr(virCommand) cmd = NULL; int hasReboot; g_autofree gid_t *groups = NULL; int ngroups; @@ -2075,7 +2075,6 @@ static int lxcContainerChild(void *data) virGetLastErrorMessage()); } - virCommandFree(cmd); return ret; } -- 2.31.1

Use automatic cleanup and remove the labels. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/lxc/lxc_process.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index 625208c86b..118d9cbdcc 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -935,7 +935,7 @@ virLXCProcessBuildControllerCmd(virLXCDriver *driver, size_t i; g_autofree char *filterstr = NULL; g_autofree char *outputstr = NULL; - virCommand *cmd; + g_autoptr(virCommand) cmd = NULL; g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); cmd = virCommandNew(vm->def->emulator); @@ -955,7 +955,7 @@ virLXCProcessBuildControllerCmd(virLXCDriver *driver, if (cfg->log_libvirtd) { if (virLogGetNbOutputs() > 0) { if (!(outputstr = virLogGetOutputs())) - goto error; + return NULL; virCommandAddEnvPair(cmd, "LIBVIRT_LOG_OUTPUTS", outputstr); } @@ -1007,12 +1007,7 @@ virLXCProcessBuildControllerCmd(virLXCDriver *driver, * write the live domain status XML with the PID */ virCommandRequireHandshake(cmd); - cleanup: - return cmd; - error: - virCommandFree(cmd); - cmd = NULL; - goto cleanup; + return g_steal_pointer(&cmd); } -- 2.31.1

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/lxc/lxc_process.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index 118d9cbdcc..db023630bd 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -1187,7 +1187,7 @@ int virLXCProcessStart(virConnectPtr conn, char ebuf[1024]; g_autofree char *timestamp = NULL; int nsInheritFDs[VIR_LXC_DOMAIN_NAMESPACE_LAST]; - virCommand *cmd = NULL; + g_autoptr(virCommand) cmd = NULL; virLXCDomainObjPrivate *priv = vm->privateData; g_autoptr(virCaps) caps = NULL; virErrorPtr err = NULL; @@ -1555,7 +1555,6 @@ int virLXCProcessStart(virConnectPtr conn, virLXCProcessCleanup(driver, vm, VIR_DOMAIN_SHUTOFF_FAILED, stopFlags); } } - virCommandFree(cmd); for (i = 0; i < nttyFDs; i++) VIR_FORCE_CLOSE(ttyFDs[i]); for (i = 0; i < G_N_ELEMENTS(handshakefds); i++) -- 2.31.1

Do not mix manual and automatic freeing. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/storage/storage_backend_logical.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/storage/storage_backend_logical.c b/src/storage/storage_backend_logical.c index ed490b0201..3f27e63aeb 100644 --- a/src/storage/storage_backend_logical.c +++ b/src/storage/storage_backend_logical.c @@ -477,23 +477,23 @@ virStorageBackendLogicalGetPoolSources(virStoragePoolSourceList *sourceList) int vars[] = { 2 }; - g_autoptr(virCommand) cmd = NULL; + g_autoptr(virCommand) vgcmd = NULL; + g_autoptr(virCommand) pvcmd = NULL; /* * NOTE: ignoring errors here; this is just to "touch" any logical volumes * that might be hanging around, so if this fails for some reason, the * worst that happens is that scanning doesn't pick everything up */ - cmd = virCommandNew(VGSCAN); - if (virCommandRun(cmd, NULL) < 0) + vgcmd = virCommandNew(VGSCAN); + if (virCommandRun(vgcmd, NULL) < 0) VIR_WARN("Failure when running vgscan to refresh physical volumes"); - virCommandFree(cmd); - cmd = virCommandNewArgList(PVS, - "--noheadings", - "-o", "pv_name,vg_name", - NULL, NULL); - return virCommandRunRegex(cmd, 1, regexes, vars, + pvcmd = virCommandNewArgList(PVS, + "--noheadings", + "-o", "pv_name,vg_name", + NULL, NULL); + return virCommandRunRegex(pvcmd, 1, regexes, vars, virStorageBackendLogicalFindPoolSourcesFunc, sourceList, "pvs", NULL); } -- 2.31.1

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/storage/storage_util.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c index ce61f37172..bfc3edb1fd 100644 --- a/src/storage/storage_util.c +++ b/src/storage/storage_util.c @@ -1116,7 +1116,7 @@ virStorageBackendCreateQemuImgCmdFromVol(virStoragePoolObj *pool, const char *inputSecretPath, virStorageVolEncryptConvertStep convertStep) { - virCommand *cmd = NULL; + g_autoptr(virCommand) cmd = NULL; struct _virStorageBackendQemuImgInfo info = { .format = vol->target.format, .type = NULL, @@ -1246,11 +1246,10 @@ virStorageBackendCreateQemuImgCmdFromVol(virStoragePoolObj *pool, } VIR_FREE(info.secretAlias); - return cmd; + return g_steal_pointer(&cmd); error: VIR_FREE(info.secretAlias); - virCommandFree(cmd); return NULL; } -- 2.31.1

Use g_auto for virCommand and char * and drop the cleanup label. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/security/security_apparmor.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c index dadcfd67db..d1087aa10c 100644 --- a/src/security/security_apparmor.c +++ b/src/security/security_apparmor.c @@ -163,14 +163,13 @@ load_profile(virSecurityManager *mgr G_GNUC_UNUSED, const char *fn, bool append) { - int rc = -1; bool create = true; - char *xml = NULL; - virCommand *cmd = NULL; + g_autofree char *xml = NULL; + g_autoptr(virCommand) cmd = NULL; xml = virDomainDefFormat(def, NULL, VIR_DOMAIN_DEF_FORMAT_SECURE); if (!xml) - goto cleanup; + return -1; if (profile_status_file(profile) >= 0) create = false; @@ -191,13 +190,7 @@ load_profile(virSecurityManager *mgr G_GNUC_UNUSED, virLogGetDefaultPriority()); virCommandSetInputBuffer(cmd, xml); - rc = virCommandRun(cmd, NULL); - - cleanup: - VIR_FREE(xml); - virCommandFree(cmd); - - return rc; + return virCommandRun(cmd, NULL); } static int -- 2.31.1

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/qemu/qemu_driver.c | 3 +-- src/qemu/qemu_interface.c | 3 +-- src/qemu/qemu_tpm.c | 4 +--- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index e444ad2d45..db2b25adbd 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -6315,7 +6315,7 @@ static char *qemuConnectDomainXMLToNative(virConnectPtr conn, { virQEMUDriver *driver = conn->privateData; virDomainObj *vm = NULL; - virCommand *cmd = NULL; + g_autoptr(virCommand) cmd = NULL; char *ret = NULL; size_t i; @@ -6375,7 +6375,6 @@ static char *qemuConnectDomainXMLToNative(virConnectPtr conn, ret = virCommandToString(cmd, false); cleanup: - virCommandFree(cmd); virObjectUnref(vm); return ret; } diff --git a/src/qemu/qemu_interface.c b/src/qemu/qemu_interface.c index ac0168c80d..676bc896d6 100644 --- a/src/qemu/qemu_interface.c +++ b/src/qemu/qemu_interface.c @@ -320,7 +320,7 @@ qemuCreateInBridgePortWithHelper(virQEMUDriverConfig *cfg, int *tapfd, unsigned int flags) { - virCommand *cmd; + g_autoptr(virCommand) cmd = NULL; char *errbuf = NULL, *cmdstr = NULL; int pair[2] = { -1, -1 }; @@ -387,7 +387,6 @@ qemuCreateInBridgePortWithHelper(virQEMUDriverConfig *cfg, cleanup: VIR_FREE(cmdstr); VIR_FREE(errbuf); - virCommandFree(cmd); VIR_FORCE_CLOSE(pair[0]); return *tapfd < 0 ? -1 : 0; } diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c index 1b923fd68e..7e7b01768e 100644 --- a/src/qemu/qemu_tpm.c +++ b/src/qemu/qemu_tpm.c @@ -793,7 +793,7 @@ static void qemuTPMEmulatorStop(const char *swtpmStateDir, const char *shortName) { - virCommand *cmd; + g_autoptr(virCommand) cmd = NULL; g_autofree char *pathname = NULL; g_autofree char *errbuf = NULL; g_autofree char *swtpm_ioctl = virTPMGetSwtpmIoctl(); @@ -817,8 +817,6 @@ qemuTPMEmulatorStop(const char *swtpmStateDir, ignore_value(virCommandRun(cmd, NULL)); - virCommandFree(cmd); - /* clean up the socket */ unlink(pathname); } -- 2.31.1

On 12/13/21 14:59, Ján Tomko wrote:
A partial cleanup. The rest is in progress (TM)
Ján Tomko (14): bhyve: refactor virBhyveProbeGrubCaps bhyve: refactor bhyveProbeCapsDeviceHelper bhyve: refactor bhyveProbeCapsFromHelp bhyve: refactor bhyveConnectDomainXMLToNative bhyve: use g_auto in virBhyveProcessStartImpl bhyve: use g_auto in virBhyveProcessStop bhyve: refactor virBhyveProcessBuildBhyveCmd lxc: use g_auto in lxcContainerChild lxc: refactor virLXCProcessBuildControllerCmd lxc: use g_auto for virCommand in virLXCProcessEnsureRootFS storage: logical: use two cmd vars in GetPoolSources storage: util: steal cmd in CreateQemuImgCmdFromVol security: apparmor: use automatic cleanup in load_profile qemu: use automatic cleanup for virCommand
src/bhyve/bhyve_capabilities.c | 57 ++++++++++----------------- src/bhyve/bhyve_command.c | 36 ++++++++--------- src/bhyve/bhyve_driver.c | 28 ++++++------- src/bhyve/bhyve_process.c | 25 ++++-------- src/lxc/lxc_container.c | 3 +- src/lxc/lxc_process.c | 14 ++----- src/qemu/qemu_driver.c | 3 +- src/qemu/qemu_interface.c | 3 +- src/qemu/qemu_tpm.c | 4 +- src/security/security_apparmor.c | 15 ++----- src/storage/storage_backend_logical.c | 18 ++++----- src/storage/storage_util.c | 5 +-- 12 files changed, 78 insertions(+), 133 deletions(-)
Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Michal
participants (2)
-
Ján Tomko
-
Michal Prívozník