]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Fix error handling of readdir() in virFileLoopDeviceOpen
authorDaniel P. Berrange <berrange@redhat.com>
Fri, 3 May 2013 13:26:56 +0000 (14:26 +0100)
committerDaniel P. Berrange <berrange@redhat.com>
Mon, 13 May 2013 12:15:19 +0000 (13:15 +0100)
To correctly handle errors from readdir() you must set 'errno'
to zero before invoking it & check its value afterwards to
distinguish error from EOF.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
src/util/virfile.c

index a59d67d5e126210cb22618c1960d3c91977d5f46..52e2c2da00e9f1a41cd31ceddfa40b71591e9e4a 100644 (file)
@@ -546,6 +546,7 @@ static int virFileLoopDeviceOpen(char **dev_name)
         goto cleanup;
     }
 
+    errno = 0;
     while ((de = readdir(dh)) != NULL) {
         if (!STRPREFIX(de->d_name, "loop"))
             continue;
@@ -577,10 +578,15 @@ static int virFileLoopDeviceOpen(char **dev_name)
         /* Oh well, try the next device */
         VIR_FORCE_CLOSE(fd);
         VIR_FREE(looppath);
+        errno = 0;
     }
 
-    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                   _("Unable to find a free loop device in /dev"));
+    if (errno != 0)
+        virReportSystemError(errno, "%s",
+                             _("Unable to iterate over loop devices"));
+    else
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Unable to find a free loop device in /dev"));
 
 cleanup:
     if (fd != -1) {