chiark / gitweb /
udevd: control - no not delete socket file when --daemon is used
[elogind.git] / extras / collect / collect.c
index ce49b9f410c5630ea6ce70898ab456042b4aa806..f78f3b778eaea5c0eb8dc452db69f5651299ea92 100644 (file)
@@ -31,9 +31,9 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#include "../../list.h"
+#include "libudev.h"
+#include "libudev-private.h"
 
-#define TMPFILE                        "/dev/.udev/collect"
 #define BUFSIZE                        16
 #define UDEV_ALARM_TIMEOUT     180
 
@@ -44,17 +44,26 @@ enum collect_state {
 };
 
 struct _mate {
-       struct list_head node;
+       struct udev_list_node node;
        char *name;
        enum collect_state state;
 };
 
-static LIST_HEAD(bunch);
+static struct udev_list_node bunch;
 static int debug;
 
 /* This can increase dynamically */
 static size_t bufsize = BUFSIZE;
 
+static struct _mate *node_to_mate(struct udev_list_node *node)
+{
+       char *mate;
+
+       mate = (char *)node;
+       mate -= offsetof(struct _mate, node);
+       return (struct _mate *)mate;
+}
+
 static void sig_alrm(int signo)
 {
        exit(4);
@@ -114,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,
@@ -161,10 +170,9 @@ 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;
-                               list_add_tail(&him->node, &bunch);
+                               udev_list_node_append(&him->node, &bunch);
                                word = NULL;
                        }
                }
@@ -190,13 +198,15 @@ static int checkout(int fd)
  */
 static void invite(char *us)
 {
-       struct _mate *him, *who;
+       struct udev_list_node *him_node;
+       struct _mate *who = NULL;
 
        if (debug)
                fprintf(stderr, "Adding ID '%s'\n", us);
 
-       who = NULL;
-       list_for_each_entry(him, &bunch, node) {
+       udev_list_node_foreach(him_node, &bunch) {
+               struct _mate *him = node_to_mate(him_node);
+
                if (!strcmp(him->name, us)) {
                        him->state = STATE_CONFIRMED;
                        who = him;
@@ -216,13 +226,15 @@ static void invite(char *us)
  */
 static void reject(char *us)
 {
-       struct _mate *him, *who;
+       struct udev_list_node *him_node;
+       struct _mate *who = NULL;
 
        if (debug)
                fprintf(stderr, "Removing ID '%s'\n", us);
 
-       who = NULL;
-       list_for_each_entry(him, &bunch, node) {
+       udev_list_node_foreach(him_node, &bunch) {
+               struct _mate *him = node_to_mate(him_node);
+
                if (!strcmp(him->name, us)) {
                        him->state = STATE_NONE;
                        who = him;
@@ -230,7 +242,6 @@ static void reject(char *us)
        }
        if (debug && !who)
                fprintf(stderr, "ID '%s' not in database\n", us);
-
 }
 
 /*
@@ -241,11 +252,14 @@ static void reject(char *us)
  */
 static void kickout(void)
 {
-       struct _mate *him, *them;
+       struct udev_list_node *him_node;
+       struct udev_list_node *tmp;
+
+       udev_list_node_foreach_safe(him_node, tmp, &bunch) {
+               struct _mate *him = node_to_mate(him_node);
 
-       list_for_each_entry_safe(him, them, &bunch, node) {
                if (him->state == STATE_OLD) {
-                       list_del(&him->node);
+                       udev_list_node_remove(&him->node);
                        free(him->name);
                        free(him);
                }
@@ -261,13 +275,15 @@ static int missing(int fd)
 {
        char *buf;
        int ret = 0;
-       struct _mate *him;
+       struct udev_list_node *him_node;
 
        buf = malloc(bufsize);
        if (!buf)
                return -1;
 
-       list_for_each_entry(him, &bunch, node) {
+       udev_list_node_foreach(him_node, &bunch) {
+               struct _mate *him = node_to_mate(him_node);
+
                if (him->state == STATE_NONE) {
                        ret++;
                } else {
@@ -288,7 +304,6 @@ static int missing(int fd)
        }
 
        free(buf);
-
        return ret;
 }
 
@@ -299,10 +314,12 @@ static int missing(int fd)
  */
 static void everybody(void)
 {
-       struct _mate *him;
+       struct udev_list_node *him_node;
        const char *state = "";
 
-       list_for_each_entry(him, &bunch, node) {
+       udev_list_node_foreach(him_node, &bunch) {
+               struct _mate *him = node_to_mate(him_node);
+
                switch (him->state) {
                case STATE_NONE:
                        state = "none";
@@ -313,27 +330,34 @@ 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", 0, NULL, 'a' },
-               { "remove", 0, NULL, 'r' },
-               { "debug", 0, NULL, 'd' },
-               { "help", 0, NULL, 'h' },
+               { "add", no_argument, NULL, 'a' },
+               { "remove", no_argument, NULL, 'r' },
+               { "debug", no_argument, NULL, 'd' },
+               { "help", no_argument, NULL, 'h' },
                {}
        };
        int argi;
        char *checkpoint, *us;
-       struct _mate *him, *who;
        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;
@@ -376,12 +400,13 @@ int main(int argc, char **argv)
                goto exit;
        }
 
-       INIT_LIST_HEAD(&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;
@@ -393,19 +418,26 @@ int main(int argc, char **argv)
        }
 
        for (i = argi; i < argc; i++) {
+               struct udev_list_node *him_node;
+               struct _mate *who;
+
                who = NULL;
-               list_for_each_entry(him, &bunch, node) {
+               udev_list_node_foreach(him_node, &bunch) {
+                       struct _mate *him = node_to_mate(him_node);
+
                        if (!strcmp(him->name, argv[i]))
                                who = him;
                }
                if (!who) {
+                       struct _mate *him;
+
                        if (debug)
                                fprintf(stderr, "ID %s: not in database\n", argv[i]);
                        him = malloc(sizeof (struct _mate));
                        him->name = malloc(strlen(argv[i]) + 1);
                        strcpy(him->name, argv[i]);
                        him->state = STATE_NONE;
-                       list_add_tail(&him->node, &bunch);
+                       udev_list_node_append(&him->node, &bunch);
                } else {
                        if (debug)
                                fprintf(stderr, "ID %s: found in database\n", argv[i]);
@@ -430,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;
 }