[PATCH] command: avoid fd leak on failure

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



virCommandTransferFD promises that the fd is no longer owned by
the caller.  Normally, we want the fd to remain open until the
child runs, but in error situations, we must close it earlier.

* src/util/command.c (virCommandTransferFD): Close fd now if we
can't track it to close later.
---
 src/util/command.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/util/command.c b/src/util/command.c
index 3c516ec..83d4e88 100644
--- a/src/util/command.c
+++ b/src/util/command.c
@@ -738,7 +738,7 @@ virCommandKeepFD(virCommandPtr cmd, int fd, bool transfer)
 void
 virCommandPreserveFD(virCommandPtr cmd, int fd)
 {
-    return virCommandKeepFD(cmd, fd, false);
+    virCommandKeepFD(cmd, fd, false);
 }

 /*
@@ -749,7 +749,13 @@ virCommandPreserveFD(virCommandPtr cmd, int fd)
 void
 virCommandTransferFD(virCommandPtr cmd, int fd)
 {
-    return virCommandKeepFD(cmd, fd, true);
+    virCommandKeepFD(cmd, fd, true);
+    if ((!cmd || cmd->has_error) && fd > STDERR_FILENO) {
+        /* We must close the fd now, instead of waiting for
+         * virCommandRun, if there was an error that prevented us from
+         * adding the fd to cmd.  */
+        VIR_FORCE_CLOSE(fd);
+    }
 }


-- 
1.7.4.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]