chiark / gitweb /
fix compilation with --enable-debug
[elogind.git] / libudev / libudev-queue.c
index 68efba0..d305db6 100644 (file)
@@ -131,7 +131,7 @@ unsigned long long int udev_get_kernel_seqnum(struct udev *udev)
        ssize_t len;
 
        util_strscpyl(filename, sizeof(filename), udev_get_sys_path(udev), "/kernel/uevent_seqnum", NULL);
-       fd = open(filename, O_RDONLY);
+       fd = open(filename, O_RDONLY|O_CLOEXEC);
        if (fd < 0)
                return 0;
        len = read(fd, buf, sizeof(buf));
@@ -215,7 +215,7 @@ static FILE *open_queue_file(struct udev_queue *udev_queue, unsigned long long i
        FILE *queue_file;
 
        util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_queue->udev), "/.udev/queue.bin", NULL);
-       queue_file = fopen(filename, "r");
+       queue_file = fopen(filename, "re");
        if (queue_file == NULL)
                return NULL;
 
@@ -299,7 +299,7 @@ int udev_queue_get_queue_is_empty(struct udev_queue *udev_queue)
        if (queue_file == NULL)
                return 1;
 
-       while (1) {
+       for (;;) {
                unsigned long long int seqnum;
                ssize_t devpath_len;
 
@@ -348,7 +348,7 @@ out:
 int udev_queue_get_seqnum_sequence_is_finished(struct udev_queue *udev_queue,
                                               unsigned long long int start, unsigned long long int end)
 {
-       unsigned long long int seqnum = 0;
+       unsigned long long int seqnum;
        ssize_t devpath_len;
        int unfinished;
        FILE *queue_file;
@@ -360,24 +360,41 @@ int udev_queue_get_seqnum_sequence_is_finished(struct udev_queue *udev_queue,
                return 1;
        if (start < seqnum)
                start = seqnum;
-       if (start > end)
+       if (start > end) {
+               fclose(queue_file);
                return 1;
-       if (end - start > INT_MAX - 1)
+       }
+       if (end - start > INT_MAX - 1) {
+               fclose(queue_file);
                return -EOVERFLOW;
-       unfinished = (end - start) + 1;
+       }
+
+       /*
+        * we might start with 0, and handle the initial seqnum
+        * only when we find an entry in the queue file
+        **/
+       unfinished = end - start;
 
-       while (unfinished > 0) {
+       do {
                if (udev_queue_read_seqnum(queue_file, &seqnum) < 0)
                        break;
                devpath_len = udev_queue_skip_devpath(queue_file);
                if (devpath_len < 0)
                        break;
 
+               /*
+                * we might start with an empty or re-build queue file, where
+                * the initial seqnum is not recorded as finished
+                */
+               if (start == seqnum && devpath_len > 0)
+                       unfinished++;
+
                if (devpath_len == 0) {
                        if (seqnum >= start && seqnum <= end)
                                unfinished--;
                }
-       }
+       } while (unfinished > 0);
+
        fclose(queue_file);
 
        return (unfinished == 0);
@@ -484,7 +501,7 @@ struct udev_list_entry *udev_queue_get_failed_list_entry(struct udev_queue *udev
                s = syspath;
                l = util_strpcpyl(&s, sizeof(syspath), udev_get_sys_path(udev_queue->udev), NULL);
                len = readlinkat(dirfd(dir), dent->d_name, s, l);
-               if (len < 0 || (size_t)len >= l)
+               if (len <= 0 || (size_t)len == l)
                        continue;
                s[len] = '\0';
                dbg(udev_queue->udev, "found '%s' [%s]\n", syspath, dent->d_name);