From 5c86fbb72d6e90025481db756d7ee04afb9b7f66 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Tue, 21 Jul 2020 15:04:38 +0200 Subject: [PATCH] qemuDomainDetachDeviceUnlink: Unlink paths in one go MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Simirarly to qemuDomainAttachDeviceMknodHelper() which was modified just a couple of commits ago, modify the unlink helper which is called on device detach so that it can unlink multiple files in one go instead of forking off for every single one of them. Signed-off-by: Michal Privoznik Reviewed-by: Ján Tomko --- src/qemu/qemu_namespace.c | 69 ++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/src/qemu/qemu_namespace.c b/src/qemu/qemu_namespace.c index 4b8cc08a1e..0984f50742 100644 --- a/src/qemu/qemu_namespace.c +++ b/src/qemu/qemu_namespace.c @@ -1282,47 +1282,24 @@ qemuNamespaceMknodPaths(virDomainObjPtr vm G_GNUC_UNUSED, static int -qemuDomainDetachDeviceUnlinkHelper(pid_t pid G_GNUC_UNUSED, - void *opaque) -{ - const char *path = opaque; - - VIR_DEBUG("Unlinking %s", path); - if (unlink(path) < 0 && errno != ENOENT) { - virReportSystemError(errno, - _("Unable to remove device %s"), path); - return -1; - } - - return 0; -} - - -static int -qemuDomainDetachDeviceUnlink(virQEMUDriverPtr driver G_GNUC_UNUSED, - virDomainObjPtr vm, - const char *file, - char * const *devMountsPath, - size_t ndevMountsPath) +qemuNamespaceUnlinkHelper(pid_t pid G_GNUC_UNUSED, + void *opaque) { + char **paths = opaque; size_t i; - if (STRPREFIX(file, QEMU_DEVPREFIX)) { - for (i = 0; i < ndevMountsPath; i++) { - if (STREQ(devMountsPath[i], "/dev")) - continue; - if (STRPREFIX(file, devMountsPath[i])) - break; - } + for (i = 0; paths[i]; i++) { + const char *path = paths[i]; - if (i == ndevMountsPath) { - if (virProcessRunInMountNamespace(vm->pid, - qemuDomainDetachDeviceUnlinkHelper, - (void *)file) < 0) - return -1; + VIR_DEBUG("Unlinking %s", path); + if (unlink(path) < 0 && errno != ENOENT) { + virReportSystemError(errno, + _("Unable to remove device %s"), path); + return -1; } } + g_strfreev(paths); return 0; } @@ -1335,6 +1312,7 @@ qemuDomainNamespaceUnlinkPaths(virDomainObjPtr vm, qemuDomainObjPrivatePtr priv = vm->privateData; virQEMUDriverPtr driver = priv->driver; g_autoptr(virQEMUDriverConfig) cfg = NULL; + VIR_AUTOSTRINGLIST unlinkPaths = NULL; char **devMountsPath = NULL; size_t ndevMountsPath = 0; size_t i; @@ -1351,11 +1329,28 @@ qemuDomainNamespaceUnlinkPaths(virDomainObjPtr vm, goto cleanup; for (i = 0; i < npaths; i++) { - if (qemuDomainDetachDeviceUnlink(driver, vm, paths[i], - devMountsPath, ndevMountsPath) < 0) - goto cleanup; + const char *file = paths[i]; + + if (STRPREFIX(file, QEMU_DEVPREFIX)) { + for (i = 0; i < ndevMountsPath; i++) { + if (STREQ(devMountsPath[i], "/dev")) + continue; + if (STRPREFIX(file, devMountsPath[i])) + break; + } + + if (i == ndevMountsPath && + virStringListAdd(&unlinkPaths, file) < 0) + return -1; + } } + if (unlinkPaths && + virProcessRunInMountNamespace(vm->pid, + qemuNamespaceUnlinkHelper, + unlinkPaths) < 0) + return -1; + ret = 0; cleanup: virStringListFreeCount(devMountsPath, ndevMountsPath); -- 2.47.2