Fix a possible race condition if two processes try to claim the same loop
authorRob Landley <rob@landley.net>
Sun, 25 Jun 2006 15:29:12 +0000 (15:29 -0000)
committerRob Landley <rob@landley.net>
Sun, 25 Jun 2006 15:29:12 +0000 (15:29 -0000)
device at the same time.  We should only CLR_FD if the set status fails,
not if the SET_FD fails.

libbb/loop.c

index 2f9d029248e8e4dbb0aa6dfc26665a4104693888..e5b21642e487a96e082ab522efb7baff5dc92c8b 100644 (file)
@@ -118,9 +118,10 @@ int set_loop(char **device, const char *file, int offset)
                        safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
                        loopinfo.lo_offset = offset;
                        /* Associate free loop device with file.  */
-                       if(!ioctl(dfd, LOOP_SET_FD, ffd) &&
-                          !ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo)) rc=0;
-                       else ioctl(dfd, LOOP_CLR_FD, 0);
+                       if(!ioctl(dfd, LOOP_SET_FD, ffd)) {
+                          if (!ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo)) rc=0;
+                               else ioctl(dfd, LOOP_CLR_FD, 0);
+                       }
 
                /* If this block device already set up right, re-use it.
                   (Yes this is racy, but associating two loop devices with the same