Fix file descriptor leak in daemon.
authorRichard Jones <rjones@redhat.com>
Mon, 20 Apr 2009 10:35:57 +0000 (11:35 +0100)
committerRichard Jones <rjones@redhat.com>
Mon, 20 Apr 2009 10:35:57 +0000 (11:35 +0100)
daemon/guestfsd.c
daemon/proto.c

index 2553ea2..a957683 100644 (file)
@@ -384,6 +384,10 @@ commandv (char **stdoutput, char **stderror, char * const* const argv)
   pid = fork ();
   if (pid == -1) {
     perror ("fork");
   pid = fork ();
   if (pid == -1) {
     perror ("fork");
+    close (so_fd[0]);
+    close (so_fd[1]);
+    close (se_fd[0]);
+    close (se_fd[1]);
     return -1;
   }
 
     return -1;
   }
 
@@ -415,6 +419,8 @@ commandv (char **stdoutput, char **stderror, char * const* const argv)
     r = select (MAX (so_fd[0], se_fd[0]) + 1, &rset2, NULL, NULL, NULL);
     if (r == -1) {
       perror ("select");
     r = select (MAX (so_fd[0], se_fd[0]) + 1, &rset2, NULL, NULL, NULL);
     if (r == -1) {
       perror ("select");
+      close (so_fd[0]);
+      close (se_fd[0]);
       waitpid (pid, NULL, 0);
       return -1;
     }
       waitpid (pid, NULL, 0);
       return -1;
     }
@@ -423,6 +429,8 @@ commandv (char **stdoutput, char **stderror, char * const* const argv)
       r = read (so_fd[0], buf, sizeof buf);
       if (r == -1) {
        perror ("read");
       r = read (so_fd[0], buf, sizeof buf);
       if (r == -1) {
        perror ("read");
+       close (so_fd[0]);
+       close (se_fd[0]);
        waitpid (pid, NULL, 0);
        return -1;
       }
        waitpid (pid, NULL, 0);
        return -1;
       }
@@ -444,6 +452,8 @@ commandv (char **stdoutput, char **stderror, char * const* const argv)
       r = read (se_fd[0], buf, sizeof buf);
       if (r == -1) {
        perror ("read");
       r = read (se_fd[0], buf, sizeof buf);
       if (r == -1) {
        perror ("read");
+       close (so_fd[0]);
+       close (se_fd[0]);
        waitpid (pid, NULL, 0);
        return -1;
       }
        waitpid (pid, NULL, 0);
        return -1;
       }
@@ -462,6 +472,9 @@ commandv (char **stdoutput, char **stderror, char * const* const argv)
     }
   }
 
     }
   }
 
+  close (so_fd[0]);
+  close (se_fd[0]);
+
   /* Make sure the output buffers are \0-terminated.  Also remove any
    * trailing \n characters from the error buffer (not from stdout).
    */
   /* Make sure the output buffers are \0-terminated.  Also remove any
    * trailing \n characters from the error buffer (not from stdout).
    */
index cd61f18..becf27c 100644 (file)
@@ -51,6 +51,14 @@ main_loop (int _sock)
   sock = _sock;
 
   for (;;) {
   sock = _sock;
 
   for (;;) {
+#if 0
+    /* Most common errors are leaked memory and leaked file descriptors,
+     * so run this between each command:
+     */
+    if (verbose)
+      system ("ls -l /proc/self/fd");
+#endif
+
     /* Read the length word. */
     xread (sock, lenbuf, 4);
     xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
     /* Read the length word. */
     xread (sock, lenbuf, 4);
     xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);