From: Matthias Koenig Date: Wed, 20 Jun 2007 13:17:47 +0000 (+0200) Subject: mount: loop device race condition X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6ffa7c9383a3fca31b5f70eb8d3528d0b09980aa;p=util-linux mount: loop device race condition Fix race in losetup Retry acquiring a loop device if set_loop failed with EBUSY Signed-Off-By: Matthias Koenig Signed-off-by: Karel Zak --- diff --git a/mount/lomount.c b/mount/lomount.c index a5def980..2989717e 100644 --- a/mount/lomount.c +++ b/mount/lomount.c @@ -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; }