2 * Collect variables across events.
4 * usage: collect [--add|--remove] <checkpoint> <id> <idlist>
6 * Adds ID <id> to the list governed by <checkpoint>.
7 * <id> must be part of the ID list <idlist>.
8 * If all IDs given by <idlist> are listed (ie collect has been
9 * invoked for each ID in <idlist>) collect returns 0, the
10 * number of missing IDs otherwise.
11 * A negative number is returned on error.
13 * Copyright(C) 2007, Hannes Reinecke <hare@suse.de>
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 2 of the License, or
18 * (at your option) any later version.
31 #include <sys/types.h>
35 #include "libudev-private.h"
39 #define UDEV_ALARM_TIMEOUT 180
48 struct udev_list_node node;
50 enum collect_state state;
53 static struct udev_list_node bunch;
56 /* This can increase dynamically */
57 static size_t bufsize = BUFSIZE;
59 static inline struct _mate *node_to_mate(struct udev_list_node *node)
61 return container_of(node, struct _mate, node);
64 noreturn static void sig_alrm(int signo)
69 static void usage(void)
71 printf("Usage: collect [options] <checkpoint> <id> <idlist>\n"
72 " -a,--add add ID <id> to the list <idlist>\n"
73 " -r,--remove remove ID <id> from the list <idlist>\n"
74 " -d,--debug debug to stderr\n"
75 " -h,--help print this help text\n\n"
76 " Adds ID <id> to the list governed by <checkpoint>.\n"
77 " <id> must be part of the list <idlist>.\n"
78 " If all IDs given by <idlist> are listed (ie collect has been\n"
79 " invoked for each ID in <idlist>) collect returns 0, the\n"
80 " number of missing IDs otherwise.\n"
81 " On error a negative number is returned.\n\n");
87 * Prepares the database file
89 static int prepare(char *dir, char *filename)
95 if (r < 0 && errno != EEXIST)
98 snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
100 fd = open(buf,O_RDWR|O_CREAT|O_CLOEXEC, S_IRUSR|S_IWUSR);
102 fprintf(stderr, "Cannot open %s: %m\n", buf);
104 if (lockf(fd,F_TLOCK,0) < 0) {
106 fprintf(stderr, "Lock taken, wait for %d seconds\n", UDEV_ALARM_TIMEOUT);
107 if (errno == EAGAIN || errno == EACCES) {
108 alarm(UDEV_ALARM_TIMEOUT);
109 lockf(fd, F_LOCK, 0);
111 fprintf(stderr, "Acquired lock on %s\n", buf);
114 fprintf(stderr, "Could not get lock on %s: %m\n", buf);
122 * Read checkpoint file
124 * Tricky reading this. We allocate a buffer twice as large
125 * as we're going to read. Then we read into the upper half
126 * of that buffer and start parsing.
127 * Once we do _not_ find end-of-work terminator (whitespace
128 * character) we move the upper half to the lower half,
129 * adjust the read pointer and read the next bit.
130 * Quite clever methinks :-)
131 * I should become a programmer ...
133 * Yes, one could have used fgets() for this. But then we'd
134 * have to use freopen etc which I found quite tedious.
136 static int checkout(int fd)
139 char *buf, *ptr, *word = NULL;
144 buf = malloc(bufsize + 1);
147 memset(buf, ' ', bufsize);
151 while ((read(fd, buf + len, len)) > 0) {
152 while (ptr && *ptr) {
154 ptr = strpbrk(word," \n\t\r");
155 if (!ptr && word < (buf + len)) {
156 bufsize = bufsize << 1;
158 fprintf(stderr, "ID overflow, restarting with size %zu\n", bufsize);
160 lseek(fd, 0, SEEK_SET);
170 fprintf(stderr, "Found word %s\n", word);
171 him = malloc(sizeof (struct _mate));
176 him->name = strdup(word);
182 him->state = STATE_OLD;
183 udev_list_node_append(&him->node, &bunch);
187 memcpy(buf, buf + len, len);
188 memset(buf + len, ' ', len);
204 * Adds a new ID 'us' to the internal list,
205 * marks it as confirmed.
207 static void invite(char *us)
209 struct udev_list_node *him_node;
210 struct _mate *who = NULL;
213 fprintf(stderr, "Adding ID '%s'\n", us);
215 udev_list_node_foreach(him_node, &bunch) {
216 struct _mate *him = node_to_mate(him_node);
218 if (streq(him->name, us)) {
219 him->state = STATE_CONFIRMED;
224 fprintf(stderr, "ID '%s' not in database\n", us);
231 * Marks the ID 'us' as invalid,
232 * causing it to be removed when the
233 * list is written out.
235 static void reject(char *us)
237 struct udev_list_node *him_node;
238 struct _mate *who = NULL;
241 fprintf(stderr, "Removing ID '%s'\n", us);
243 udev_list_node_foreach(him_node, &bunch) {
244 struct _mate *him = node_to_mate(him_node);
246 if (streq(him->name, us)) {
247 him->state = STATE_NONE;
252 fprintf(stderr, "ID '%s' not in database\n", us);
258 * Remove all IDs in the internal list which are not part
259 * of the list passed via the command line.
261 static void kickout(void)
263 struct udev_list_node *him_node;
264 struct udev_list_node *tmp;
266 udev_list_node_foreach_safe(him_node, tmp, &bunch) {
267 struct _mate *him = node_to_mate(him_node);
269 if (him->state == STATE_OLD) {
270 udev_list_node_remove(&him->node);
280 * Counts all missing IDs in the internal list.
282 static int missing(int fd)
286 struct udev_list_node *him_node;
288 buf = malloc(bufsize);
292 udev_list_node_foreach(him_node, &bunch) {
293 struct _mate *him = node_to_mate(him_node);
295 if (him->state == STATE_NONE) {
298 while (strlen(him->name)+1 >= bufsize) {
301 bufsize = bufsize << 1;
302 tmpbuf = realloc(buf, bufsize);
309 snprintf(buf, strlen(him->name)+2, "%s ", him->name);
310 if (write(fd, buf, strlen(buf)) < 0) {
324 * Prints out the status of the internal list.
326 static void everybody(void)
328 struct udev_list_node *him_node;
329 const char *state = "";
331 udev_list_node_foreach(him_node, &bunch) {
332 struct _mate *him = node_to_mate(him_node);
334 switch (him->state) {
341 case STATE_CONFIRMED:
345 fprintf(stderr, "ID: %s=%s\n", him->name, state);
349 int main(int argc, char **argv)
352 static const struct option options[] = {
353 { "add", no_argument, NULL, 'a' },
354 { "remove", no_argument, NULL, 'r' },
355 { "debug", no_argument, NULL, 'd' },
356 { "help", no_argument, NULL, 'h' },
360 char *checkpoint, *us;
363 int ret = EXIT_SUCCESS;
365 char tmpdir[UTIL_PATH_SIZE];
376 option = getopt_long(argc, argv, "ardh", options, NULL);
400 if (argi + 2 > argc) {
401 printf("Missing parameter(s)\n");
405 checkpoint = argv[argi++];
408 if (signal(SIGALRM, sig_alrm) == SIG_ERR) {
409 fprintf(stderr, "Cannot set SIGALRM: %m\n");
414 udev_list_node_init(&bunch);
417 fprintf(stderr, "Using checkpoint '%s'\n", checkpoint);
419 strscpyl(tmpdir, sizeof(tmpdir), "/run/udev/collect", NULL);
420 fd = prepare(tmpdir, checkpoint);
426 if (checkout(fd) < 0) {
431 for (i = argi; i < argc; i++) {
432 struct udev_list_node *him_node;
436 udev_list_node_foreach(him_node, &bunch) {
437 struct _mate *him = node_to_mate(him_node);
439 if (streq(him->name, argv[i]))
446 fprintf(stderr, "ID %s: not in database\n", argv[i]);
447 him = new(struct _mate, 1);
453 him->name = strdup(argv[i]);
460 him->state = STATE_NONE;
461 udev_list_node_append(&him->node, &bunch);
464 fprintf(stderr, "ID %s: found in database\n", argv[i]);
465 who->state = STATE_CONFIRMED;
476 fprintf(stderr, "Prune lists\n");
480 lseek(fd, 0, SEEK_SET);
484 lockf(fd, F_ULOCK, 0);
490 printf("COLLECT_%s=%d\n", checkpoint, ret);