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

[libvirt] [PATCH 1/3] avoid closing fd more than once in virFDStreamOpenFileInternal()



When we assign fd and childfd to elements of fds[], we will
close these fds more than once on error. So we should assign
fds[] to -1 after we assign fd and childfd to elements of
fds[]. We should also close childfd on error.
---
 src/fdstream.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/src/fdstream.c b/src/fdstream.c
index fca0f41..0de3b04 100644
--- a/src/fdstream.c
+++ b/src/fdstream.c
@@ -581,6 +581,7 @@ virFDStreamOpenFileInternal(virStreamPtr st,
     struct stat sb;
     virCommandPtr cmd = NULL;
     int errfd = -1;
+    int childfd = -1;
 
     VIR_DEBUG("st=%p path=%s oflags=%x offset=%llu length=%llu mode=%o",
               st, path, oflags, offset, length, mode);
@@ -619,7 +620,6 @@ virFDStreamOpenFileInternal(virStreamPtr st,
     if ((st->flags & VIR_STREAM_NONBLOCK) &&
         (!S_ISCHR(sb.st_mode) &&
          !S_ISFIFO(sb.st_mode))) {
-        int childfd;
 
         if ((oflags & O_ACCMODE) == O_RDWR) {
             streamsReportError(VIR_ERR_INTERNAL_ERROR,
@@ -650,6 +650,7 @@ virFDStreamOpenFileInternal(virStreamPtr st,
             fd = fds[1];
             virCommandSetInputFD(cmd, childfd);
         }
+        fds[0] = fds[1] = -1;
         virCommandSetErrorFD(cmd, &errfd);
 
         if (virCommandRunAsync(cmd, NULL) < 0)
@@ -668,6 +669,7 @@ error:
     VIR_FORCE_CLOSE(fds[0]);
     VIR_FORCE_CLOSE(fds[1]);
     VIR_FORCE_CLOSE(fd);
+    VIR_FORCE_CLOSE(childfd);
     VIR_FORCE_CLOSE(errfd);
     if (oflags & O_CREAT)
         unlink(path);
-- 
1.7.1


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