chiark / gitweb /
libudev: queue - always rebuild queue file when nothing is queued anymore
[elogind.git] / libudev / libudev-queue-private.c
index 9ae680c..0dcf9b0 100644 (file)
  *
  * When a new event is queued, its details are appended to the log.
  * When the event finishes, a second record is appended to the log
- * with the same sequence number but a null devpath.
+ * with the same sequence number but a devpath len of 0.
  *
  * Example:
- *     {1, "/devices/virtual/mem/null" },
- *     {2, "/devices/virtual/mem/zero" },
- *     {1, "" },
- * Event 2 is still queued, but event 1 has been finished
+ *     { 0x0000000000000001 }
+ *     { 0x0000000000000001, 0x0019, "/devices/virtual/mem/null" },
+ *     { 0x0000000000000002, 0x001b, "/devices/virtual/mem/random" },
+ *     { 0x0000000000000001, 0x0000 },
+ *     { 0x0000000000000003, 0x0019, "/devices/virtual/mem/zero" },
  *
- * The queue does not grow indefinitely.  It is periodically re-created
- * to remove finished events.  Atomic rename() makes this transparent to readers.
+ * Events 2 and 3 are still queued, but event 1 has finished.
+ *
+ * The queue does not grow indefinitely. It is periodically re-created
+ * to remove finished events. Atomic rename() makes this transparent to readers.
  *
  * The queue file starts with a single sequence number which specifies the
- * minimum sequence number in the log that follows.  Any events prior to this
+ * minimum sequence number in the log that follows. Any events prior to this
  * sequence number have already finished.
  */
 
@@ -42,9 +45,9 @@
 #include <fcntl.h>
 #include <dirent.h>
 #include <limits.h>
+#include <errno.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <assert.h>
 
 #include "libudev.h"
 #include "libudev-private.h"
@@ -87,23 +90,24 @@ struct udev_queue_export *udev_queue_export_new(struct udev *udev)
        return udev_queue_export;
 }
 
-void udev_queue_export_unref(struct udev_queue_export *udev_queue_export)
+struct udev_queue_export *udev_queue_export_unref(struct udev_queue_export *udev_queue_export)
 {
        if (udev_queue_export == NULL)
-               return;
+               return NULL;
        if (udev_queue_export->queue_file != NULL)
                fclose(udev_queue_export->queue_file);
        free(udev_queue_export);
+       return NULL;
 }
 
 void udev_queue_export_cleanup(struct udev_queue_export *udev_queue_export)
 {
        char filename[UTIL_PATH_SIZE];
 
-       util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_queue_export->udev), "/.udev/queue.tmp", NULL);
+       util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev_queue_export->udev), "/queue.tmp", NULL);
        unlink(filename);
 
-       util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_queue_export->udev), "/.udev/queue.bin", NULL);
+       util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev_queue_export->udev), "/queue.bin", NULL);
        unlink(filename);
 }
 
@@ -155,12 +159,12 @@ static struct queue_devpaths *build_index(struct udev_queue_export *udev_queue_e
                return NULL;
        }
        devpaths = calloc(1, sizeof(struct queue_devpaths) + (range + 1) * sizeof(long));
-       if (index == NULL)
+       if (devpaths == NULL)
                return NULL;
        devpaths->devpaths_size = range + 1;
 
        /* read all records and populate the table */
-       while(1) {
+       for (;;) {
                if (udev_queue_read_seqnum(udev_queue_export->queue_file, &seqnum) < 0)
                        break;
                n = seqnum - udev_queue_export->seqnum_max;
@@ -218,7 +222,7 @@ static int rebuild_queue_file(struct udev_queue_export *udev_queue_export)
        }
 
        /* create new queue file */
-       util_strscpyl(filename_tmp, sizeof(filename_tmp), udev_get_dev_path(udev_queue_export->udev), "/.udev/queue.tmp", NULL);
+       util_strscpyl(filename_tmp, sizeof(filename_tmp), udev_get_run_path(udev_queue_export->udev), "/queue.tmp", NULL);
        new_queue_file = fopen(filename_tmp, "w+");
        if (new_queue_file == NULL)
                goto error;
@@ -252,7 +256,7 @@ static int rebuild_queue_file(struct udev_queue_export *udev_queue_export)
                goto error;
 
        /* rename the new file on top of the old one */
-       util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_queue_export->udev), "/.udev/queue.bin", NULL);
+       util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev_queue_export->udev), "/queue.bin", NULL);
        if (rename(filename_tmp, filename) != 0)
                goto error;
 
@@ -299,8 +303,10 @@ static int write_queue_record(struct udev_queue_export *udev_queue_export,
        len = (devpath_len < USHRT_MAX) ? devpath_len : USHRT_MAX;
        if (fwrite(&len, sizeof(unsigned short), 1, udev_queue_export->queue_file) != 1)
                goto write_error;
-       if (fwrite(devpath, 1, len, udev_queue_export->queue_file) != len)
-               goto write_error;
+       if (len > 0) {
+               if (fwrite(devpath, 1, len, udev_queue_export->queue_file) != len)
+                       goto write_error;
+       }
 
        /* *must* flush output; caller may fork */
        if (fflush(udev_queue_export->queue_file) != 0)
@@ -338,6 +344,7 @@ static int update_queue(struct udev_queue_export *udev_queue_export,
        int bytes;
        int err;
 
+       /* FINISHED records have a zero length devpath */
        if (state == DEVICE_QUEUED) {
                devpath = udev_device_get_devpath(udev_device);
                devpath_len = strlen(devpath);
@@ -349,12 +356,9 @@ static int update_queue(struct udev_queue_export *udev_queue_export,
                        return -1;
        }
 
-       /* when the queue files grow too large, they must be garbage collected and rebuilt */
-       bytes = ftell(udev_queue_export->queue_file) + queue_record_size(devpath_len);
-
        /* if we're removing the last event from the queue, that's the best time to rebuild it */
-       if (state != DEVICE_QUEUED && udev_queue_export->queued_count == 1 && bytes > 2048) {
-               /* because we don't need to read the old queue file */
+       if (state != DEVICE_QUEUED && udev_queue_export->queued_count == 1) {
+               /* we don't need to read the old queue file */
                fclose(udev_queue_export->queue_file);
                udev_queue_export->queue_file = NULL;
                rebuild_queue_file(udev_queue_export);
@@ -362,6 +366,7 @@ static int update_queue(struct udev_queue_export *udev_queue_export,
        }
 
        /* try to rebuild the queue files before they grow larger than one page. */
+       bytes = ftell(udev_queue_export->queue_file) + queue_record_size(devpath_len);
        if ((udev_queue_export->waste_bytes > bytes / 2) && bytes > 4096)
                rebuild_queue_file(udev_queue_export);
 
@@ -393,27 +398,20 @@ static void update_failed(struct udev_queue_export *udev_queue_export,
 {
        struct udev *udev = udev_device_get_udev(udev_device);
        char filename[UTIL_PATH_SIZE];
-       char *s;
-       size_t l;
 
        if (state != DEVICE_FAILED && udev_queue_export->failed_count == 0)
                return;
 
        /* location of failed file */
-       s = filename;
-       l = util_strpcpyl(&s, sizeof(filename), udev_get_dev_path(udev_queue_export->udev), "/.udev/failed/", NULL);
-       util_path_encode(udev_device_get_devpath(udev_device), s, l);
+       util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/failed/",
+                     udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
 
        switch (state) {
        case DEVICE_FAILED:
                /* record event in the failed directory */
-               if (udev_queue_export->failed_count == 0)
-                       util_create_path(udev, filename);
                udev_queue_export->failed_count++;
-
-               udev_selinux_setfscreatecon(udev, filename, S_IFLNK);
+               util_create_path(udev, filename);
                symlink(udev_device_get_devpath(udev_device), filename);
-               udev_selinux_resetfscreatecon(udev);
                break;
 
        case DEVICE_QUEUED:
@@ -425,18 +423,6 @@ static void update_failed(struct udev_queue_export *udev_queue_export,
                break;
 
        case DEVICE_FINISHED:
-               if (udev_device_get_devpath_old(udev_device) != NULL) {
-                       /* "move" event - rename failed file to current name, do not delete failed */
-                       char filename_old[UTIL_PATH_SIZE];
-
-                       s = filename_old;
-                       l = util_strpcpyl(&s, sizeof(filename_old), udev_get_dev_path(udev_queue_export->udev), "/.udev/failed/", NULL);
-                       util_path_encode(udev_device_get_devpath_old(udev_device), s, l);
-
-                       if (rename(filename_old, filename) == 0)
-                               info(udev, "renamed devpath, moved failed state of '%s' to %s'\n",
-                                    udev_device_get_devpath_old(udev_device), udev_device_get_devpath(udev_device));
-               }
                break;
        }