chiark / gitweb /
do not skip /dev/{disk,char}/M:m removal when the device node is already gone
[elogind.git] / extras / collect / collect.c
index 355b85b5cfdb6199c63caa95bb1c5acf3b1d33a9..f78f3b778eaea5c0eb8dc452db69f5651299ea92 100644 (file)
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#include "../../udev/lib/libudev.h"
-#include "../../udev/lib/libudev-private.h"
+#include "libudev.h"
+#include "libudev-private.h"
 
-#define TMPFILE                        UDEV_PREFIX "/dev/.udev/collect"
 #define BUFSIZE                        16
 #define UDEV_ALARM_TIMEOUT     180
 
@@ -124,7 +123,7 @@ static int prepare(char *dir, char *filename)
  * Read checkpoint file
  *
  * Tricky reading this. We allocate a buffer twice as large
- * as we're goint to read. Then we read into the upper half
+ * as we're going to read. Then we read into the upper half
  * of that buffer and start parsing.
  * Once we do _not_ find end-of-work terminator (whitespace
  * character) we move the upper half to the lower half,
@@ -171,8 +170,7 @@ static int checkout(int fd)
                                if (debug)
                                        fprintf(stderr, "Found word %s\n", word);
                                him = malloc(sizeof (struct _mate));
-                               him->name = malloc(strlen(word) + 1);
-                               strcpy(him->name, word);
+                               him->name = strdup(word);
                                him->state = STATE_OLD;
                                udev_list_node_append(&him->node, &bunch);
                                word = NULL;
@@ -332,13 +330,14 @@ static void everybody(void)
                case STATE_CONFIRMED:
                        state = "confirmed";
                        break;
-               fprintf(stderr, "ID: %s=%s\n", him->name, state);
                }
+               fprintf(stderr, "ID: %s=%s\n", him->name, state);
        }
 }
 
 int main(int argc, char **argv)
 {
+       struct udev *udev;
        static const struct option options[] = {
                { "add", no_argument, NULL, 'a' },
                { "remove", no_argument, NULL, 'r' },
@@ -350,8 +349,15 @@ int main(int argc, char **argv)
        char *checkpoint, *us;
        int fd;
        int i;
-       int ret = 0;
+       int ret = EXIT_SUCCESS;
        int prune = 0;
+       char tmpdir[UTIL_PATH_SIZE];
+
+       udev = udev_new();
+       if (udev == NULL) {
+               ret = EXIT_FAILURE;
+               goto exit;
+       }
 
        while (1) {
                int option;
@@ -394,12 +400,13 @@ int main(int argc, char **argv)
                goto exit;
        }
 
-       udev_list_init(&bunch);
+       udev_list_node_init(&bunch);
 
        if (debug)
                fprintf(stderr, "Using checkpoint '%s'\n", checkpoint);
 
-       fd = prepare(TMPFILE, checkpoint);
+       util_strscpyl(tmpdir, sizeof(tmpdir), udev_get_run_path(udev), "/collect", NULL);
+       fd = prepare(tmpdir, checkpoint);
        if (fd < 0) {
                ret = 3;
                goto out;
@@ -455,11 +462,12 @@ int main(int argc, char **argv)
 
        lockf(fd, F_ULOCK, 0);
        close(fd);
- out:
+out:
        if (debug)
                everybody();
        if (ret >= 0)
                printf("COLLECT_%s=%d\n", checkpoint, ret);
- exit:
+exit:
+       udev_unref(udev);
        return ret;
 }