chiark / gitweb /
libudev: queue - always rebuild queue file when nothing is queued anymore
[elogind.git] / libudev / libudev-queue-private.c
index d9f7a67..0dcf9b0 100644 (file)
@@ -45,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"
@@ -90,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);
 }
 
@@ -163,7 +164,7 @@ static struct queue_devpaths *build_index(struct udev_queue_export *udev_queue_e
        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;
@@ -221,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;
@@ -255,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;
 
@@ -302,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)
@@ -341,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);
@@ -352,12 +356,9 @@ static int update_queue(struct udev_queue_export *udev_queue_export,
                        return -1;
        }
 
-       /* when the queue file grows too large, garbage-collect and rebuild it */
-       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);
@@ -365,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);
 
@@ -401,7 +403,7 @@ static void update_failed(struct udev_queue_export *udev_queue_export,
                return;
 
        /* location of failed file */
-       util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/failed/",
+       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) {
@@ -409,9 +411,7 @@ static void update_failed(struct udev_queue_export *udev_queue_export,
                /* record event in the failed directory */
                udev_queue_export->failed_count++;
                util_create_path(udev, filename);
-               udev_selinux_setfscreatecon(udev, filename, S_IFLNK);
                symlink(udev_device_get_devpath(udev_device), filename);
-               udev_selinux_resetfscreatecon(udev);
                break;
 
        case DEVICE_QUEUED:
@@ -423,17 +423,6 @@ static void update_failed(struct udev_queue_export *udev_queue_export,
                break;
 
        case DEVICE_FINISHED:
-               if (udev_device_get_sysname_old(udev_device) != NULL &&
-                   strcmp(udev_device_get_sysname_old(udev_device), udev_device_get_sysname(udev_device)) != 0) {
-                       /* "move" event - rename failed file to current name, do not delete failed */
-                       char filename_old[UTIL_PATH_SIZE];
-
-                       util_strscpyl(filename_old, sizeof(filename_old), udev_get_dev_path(udev), "/.udev/failed/",
-                                     udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname_old(udev_device), NULL);
-                       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;
        }