chiark / gitweb /
cdrom_id: check mount state in retry loop
authorKay Sievers <kay.sievers@vrfy.org>
Wed, 7 Apr 2010 09:32:22 +0000 (11:32 +0200)
committerKay Sievers <kay.sievers@vrfy.org>
Wed, 7 Apr 2010 09:32:22 +0000 (11:32 +0200)
Based on a patch from Harald Hoyer.

extras/cdrom_id/cdrom_id.c

index 7c9f8cc73eb3fee57322b822bd5ac30cef9dca97..894a8905749e90c203fcbe41d6a06e09a7a868dc 100644 (file)
@@ -547,6 +547,7 @@ int main(int argc, char *argv[])
        const char *node = NULL;
        int export = 0;
        int fd = -1;
+       int cnt;
        int rc = 0;
 
        udev = udev_new();
@@ -592,21 +593,16 @@ int main(int argc, char *argv[])
                goto exit;
        }
 
-       if (is_mounted(node)) {
-               fd = open(node, O_RDONLY|O_NONBLOCK);
-       } else {
-               int cnt;
+       srand((unsigned int)getpid());
+       for (cnt = 20; cnt > 0; cnt--) {
                struct timespec duration;
 
-               srand((unsigned int)getpid());
-               for (cnt = 40; cnt > 0; cnt--) {
-                       fd = open(node, O_RDONLY|O_NONBLOCK|O_EXCL);
-                       if (fd >= 0 || errno != EBUSY)
-                               break;
-                       duration.tv_sec = 0;
-                       duration.tv_nsec = (100 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
-                       nanosleep(&duration, NULL);
-               }
+               fd = open(node, O_RDONLY|O_NONBLOCK|(is_mounted(node) ? 0 : O_EXCL));
+               if (fd >= 0 || errno != EBUSY)
+                       break;
+               duration.tv_sec = 0;
+               duration.tv_nsec = (100 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
+               nanosleep(&duration, NULL);
        }
        if (fd < 0) {
                info(udev, "unable to open '%s'\n", node);