chiark / gitweb /
replace "add|change" with "!remove"
[elogind.git] / extras / cdrom_id / cdrom_id.c
index e485768de41154488a3b4d659dbfb8fd47e629bd..894a8905749e90c203fcbe41d6a06e09a7a868dc 100644 (file)
@@ -30,6 +30,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <getopt.h>
+#include <time.h>
 #include <scsi/sg.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -126,7 +127,6 @@ static int is_mounted(const char *device)
        if (fp == NULL)
                return -ENOSYS;
        while (fscanf(fp, "%*s %*s %i:%i %*[^\n]", &maj, &min) == 2) {
-               printf("got %u %u\n", maj, min);
                if (makedev(maj, min) == statbuf.st_rdev) {
                        mounted = 1;
                        break;
@@ -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,7 +593,17 @@ int main(int argc, char *argv[])
                goto exit;
        }
 
-       fd = open(node, O_RDONLY|O_NONBLOCK|(is_mounted(node) ? 0 : O_EXCL));
+       srand((unsigned int)getpid());
+       for (cnt = 20; cnt > 0; cnt--) {
+               struct timespec duration;
+
+               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);
                fprintf(stderr, "unable to open '%s'\n", node);