screenshot: don't unlink bogus file

The previous qemu patch could end up calling unlink(tmp) before
tmp was the name of a valid file (unlinking a fileXXXXXX template
instead), or calling unlink(tmp) twice on success (once here,
and once at the end of the stream).  Meanwhile, vbox also suffered
from the same leaked tmp file bug.

* src/qemu/qemu_driver.c (qemuDomainScreenshot): Don't unlink on
success, or on invalid name.
* src/vbox/vbox_tmpl.c (vboxDomainScreenshot): Don't leak temp file.
This commit is contained in:
Eric Blake 2011-08-02 10:58:21 -06:00
parent 6915439794
commit 440d6b6a0b
2 changed files with 9 additions and 4 deletions

View File

@ -2912,18 +2912,21 @@ qemuDomainScreenshot(virDomainPtr dom,
qemuDomainObjEnterMonitor(driver, vm); qemuDomainObjEnterMonitor(driver, vm);
if (qemuMonitorScreendump(priv->mon, tmp) < 0) { if (qemuMonitorScreendump(priv->mon, tmp) < 0) {
qemuDomainObjExitMonitor(driver, vm); qemuDomainObjExitMonitor(driver, vm);
unlink(tmp);
goto endjob; goto endjob;
} }
qemuDomainObjExitMonitor(driver, vm); qemuDomainObjExitMonitor(driver, vm);
if (VIR_CLOSE(tmp_fd) < 0) { if (VIR_CLOSE(tmp_fd) < 0) {
virReportSystemError(errno, _("unable to close %s"), tmp); virReportSystemError(errno, _("unable to close %s"), tmp);
unlink(tmp);
goto endjob; goto endjob;
} }
if (virFDStreamOpenFile(st, tmp, 0, 0, O_RDONLY, true) < 0) { if (virFDStreamOpenFile(st, tmp, 0, 0, O_RDONLY, true) < 0) {
qemuReportError(VIR_ERR_OPERATION_FAILED, "%s", qemuReportError(VIR_ERR_OPERATION_FAILED, "%s",
_("unable to open stream")); _("unable to open stream"));
unlink(tmp);
goto endjob; goto endjob;
} }
@ -2931,10 +2934,7 @@ qemuDomainScreenshot(virDomainPtr dom,
endjob: endjob:
VIR_FORCE_CLOSE(tmp_fd); VIR_FORCE_CLOSE(tmp_fd);
if (tmp) { VIR_FREE(tmp);
unlink(tmp);
VIR_FREE(tmp);
}
if (qemuDomainObjEndJob(driver, vm) == 0) if (qemuDomainObjEndJob(driver, vm) == 0)
vm = NULL; vm = NULL;

View File

@ -8713,6 +8713,7 @@ vboxDomainScreenshot(virDomainPtr dom,
if (NS_FAILED(rc) || !width || !height) { if (NS_FAILED(rc) || !width || !height) {
vboxError(VIR_ERR_OPERATION_FAILED, "%s", vboxError(VIR_ERR_OPERATION_FAILED, "%s",
_("unable to get screen resolution")); _("unable to get screen resolution"));
unlink(tmp);
goto endjob; goto endjob;
} }
@ -8723,6 +8724,7 @@ vboxDomainScreenshot(virDomainPtr dom,
if (NS_FAILED(rc)) { if (NS_FAILED(rc)) {
vboxError(VIR_ERR_OPERATION_FAILED, "%s", vboxError(VIR_ERR_OPERATION_FAILED, "%s",
_("failed to take screenshot")); _("failed to take screenshot"));
unlink(tmp);
goto endjob; goto endjob;
} }
@ -8730,17 +8732,20 @@ vboxDomainScreenshot(virDomainPtr dom,
screenDataSize) < 0) { screenDataSize) < 0) {
virReportSystemError(errno, _("unable to write data " virReportSystemError(errno, _("unable to write data "
"to '%s'"), tmp); "to '%s'"), tmp);
unlink(tmp);
goto endjob; goto endjob;
} }
if (VIR_CLOSE(tmp_fd) < 0) { if (VIR_CLOSE(tmp_fd) < 0) {
virReportSystemError(errno, _("unable to close %s"), tmp); virReportSystemError(errno, _("unable to close %s"), tmp);
unlink(tmp);
goto endjob; goto endjob;
} }
if (virFDStreamOpenFile(st, tmp, 0, 0, O_RDONLY, true) < 0) { if (virFDStreamOpenFile(st, tmp, 0, 0, O_RDONLY, true) < 0) {
vboxError(VIR_ERR_OPERATION_FAILED, "%s", vboxError(VIR_ERR_OPERATION_FAILED, "%s",
_("unable to open stream")); _("unable to open stream"));
unlink(tmp);
goto endjob; goto endjob;
} }