]> err.no Git - util-linux/commitdiff
mount: loop device race condition
authorMatthias Koenig <mkoenig@suse.de>
Wed, 20 Jun 2007 13:17:47 +0000 (15:17 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 27 Jun 2007 14:15:50 +0000 (16:15 +0200)
Fix race in losetup

Retry acquiring a loop device if set_loop failed with EBUSY

Signed-Off-By: Matthias Koenig <mkoenig@suse.de>
Signed-off-by: Karel Zak <kzak@redhat.com>
mount/lomount.c

index a5def98041facf906ee0ae1c3ccdf1d3b0d6ec8f..2989717e44eb49ded0c1ca902f548e5f18feb103 100644 (file)
@@ -563,9 +563,9 @@ main(int argc, char **argv) {
                device = find_unused_loop_device();
                if (device == NULL)
                        return -1;
-               if (verbose)
-                       printf("Loop device is %s\n", device);
                if (argc == optind) {
+                       if (verbose)
+                               printf("Loop device is %s\n", device);
                        printf("%s\n", device);
                        return 0;
                }
@@ -587,10 +587,23 @@ main(int argc, char **argv) {
                        usage();
                if (passfd && sscanf(passfd, "%d", &pfd) != 1)
                        usage();
-               res = set_loop(device, file, off, encryption, pfd, &ro);
-               if (res == 0 && showdev && find) {
+               do {
+                       res = set_loop(device, file, off, encryption, pfd, &ro);
+                       if (res == 2 && find) {
+                               if (verbose)
+                                       printf("stolen loop=%s...trying again\n",
+                                               device);
+                               free(device);
+                               if (!(device = find_unused_loop_device()))
+                                       return -1;
+                       }
+               } while (find && res == 2);
+
+               if (verbose && res == 0)
+                       printf("Loop device is %s\n", device);
+
+               if (res == 0 && showdev && find)
                        printf("%s\n", device);
-               }
        }
        return res;
 }