[libvirt] [PATCH] command: avoid fd leak on failure

Eric Blake eblake at redhat.com
Tue Jul 12 20:09:34 UTC 2011


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




More information about the libvir-list mailing list