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 [--add|--remove] [--debug] <checkpoint> <id> <idlist>\n"
73 " Adds ID <id> to the list governed by <checkpoint>.\n"
74 " <id> must be part of the list <idlist>.\n"
75 " If all IDs given by <idlist> are listed (ie collect has been\n"
76 " invoked for each ID in <idlist>) collect returns 0, the\n"
77 " number of missing IDs otherwise.\n"
78 " On error a negative number is returned.\n"
85 * Prepares the database file
87 static int prepare(char *dir, char *filename)
93 if (stat(dir, &statbuf) < 0)
96 snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
98 fd = open(buf,O_RDWR|O_CREAT|O_CLOEXEC, S_IRUSR|S_IWUSR);
100 fprintf(stderr, "Cannot open %s: %m\n", buf);
102 if (lockf(fd,F_TLOCK,0) < 0) {
104 fprintf(stderr, "Lock taken, wait for %d seconds\n", UDEV_ALARM_TIMEOUT);
105 if (errno == EAGAIN || errno == EACCES) {
106 alarm(UDEV_ALARM_TIMEOUT);
107 lockf(fd, F_LOCK, 0);
109 fprintf(stderr, "Acquired lock on %s\n", buf);
112 fprintf(stderr, "Could not get lock on %s: %m\n", buf);
120 * Read checkpoint file
122 * Tricky reading this. We allocate a buffer twice as large
123 * as we're going to read. Then we read into the upper half
124 * of that buffer and start parsing.
125 * Once we do _not_ find end-of-work terminator (whitespace
126 * character) we move the upper half to the lower half,
127 * adjust the read pointer and read the next bit.
128 * Quite clever methinks :-)
129 * I should become a programmer ...
131 * Yes, one could have used fgets() for this. But then we'd
132 * have to use freopen etc which I found quite tedious.
134 static int checkout(int fd)
137 char *buf, *ptr, *word = NULL;
142 buf = malloc(bufsize + 1);
145 memset(buf, ' ', bufsize);
149 while ((read(fd, buf + len, len)) > 0) {
150 while (ptr && *ptr) {
152 ptr = strpbrk(word," \n\t\r");
153 if (!ptr && word < (buf + len)) {
154 bufsize = bufsize << 1;
156 fprintf(stderr, "ID overflow, restarting with size %zi\n", bufsize);
158 lseek(fd, 0, SEEK_SET);
168 fprintf(stderr, "Found word %s\n", word);
169 him = malloc(sizeof (struct _mate));
174 him->name = strdup(word);
180 him->state = STATE_OLD;
181 udev_list_node_append(&him->node, &bunch);
185 memcpy(buf, buf + len, len);
186 memset(buf + len, ' ', len);
202 * Adds a new ID 'us' to the internal list,
203 * marks it as confirmed.
205 static void invite(char *us)
207 struct udev_list_node *him_node;
208 struct _mate *who = NULL;
211 fprintf(stderr, "Adding ID '%s'\n", us);
213 udev_list_node_foreach(him_node, &bunch) {
214 struct _mate *him = node_to_mate(him_node);
216 if (streq(him->name, us)) {
217 him->state = STATE_CONFIRMED;
222 fprintf(stderr, "ID '%s' not in database\n", us);
229 * Marks the ID 'us' as invalid,
230 * causing it to be removed when the
231 * list is written out.
233 static void reject(char *us)
235 struct udev_list_node *him_node;
236 struct _mate *who = NULL;
239 fprintf(stderr, "Removing ID '%s'\n", us);
241 udev_list_node_foreach(him_node, &bunch) {
242 struct _mate *him = node_to_mate(him_node);
244 if (streq(him->name, us)) {
245 him->state = STATE_NONE;
250 fprintf(stderr, "ID '%s' not in database\n", us);
256 * Remove all IDs in the internal list which are not part
257 * of the list passed via the commandline.
259 static void kickout(void)
261 struct udev_list_node *him_node;
262 struct udev_list_node *tmp;
264 udev_list_node_foreach_safe(him_node, tmp, &bunch) {
265 struct _mate *him = node_to_mate(him_node);
267 if (him->state == STATE_OLD) {
268 udev_list_node_remove(&him->node);
278 * Counts all missing IDs in the internal list.
280 static int missing(int fd)
284 struct udev_list_node *him_node;
286 buf = malloc(bufsize);
290 udev_list_node_foreach(him_node, &bunch) {
291 struct _mate *him = node_to_mate(him_node);
293 if (him->state == STATE_NONE) {
296 while (strlen(him->name)+1 >= bufsize) {
299 bufsize = bufsize << 1;
300 tmpbuf = realloc(buf, bufsize);
307 snprintf(buf, strlen(him->name)+2, "%s ", him->name);
308 if (write(fd, buf, strlen(buf)) < 0) {
322 * Prints out the status of the internal list.
324 static void everybody(void)
326 struct udev_list_node *him_node;
327 const char *state = "";
329 udev_list_node_foreach(him_node, &bunch) {
330 struct _mate *him = node_to_mate(him_node);
332 switch (him->state) {
339 case STATE_CONFIRMED:
343 fprintf(stderr, "ID: %s=%s\n", him->name, state);
347 int main(int argc, char **argv)
350 static const struct option options[] = {
351 { "add", no_argument, NULL, 'a' },
352 { "remove", no_argument, NULL, 'r' },
353 { "debug", no_argument, NULL, 'd' },
354 { "help", no_argument, NULL, 'h' },
358 char *checkpoint, *us;
361 int ret = EXIT_SUCCESS;
363 char tmpdir[UTIL_PATH_SIZE];
374 option = getopt_long(argc, argv, "ardh", options, NULL);
398 if (argi + 2 > argc) {
399 printf("Missing parameter(s)\n");
403 checkpoint = argv[argi++];
406 if (signal(SIGALRM, sig_alrm) == SIG_ERR) {
407 fprintf(stderr, "Cannot set SIGALRM: %m\n");
412 udev_list_node_init(&bunch);
415 fprintf(stderr, "Using checkpoint '%s'\n", checkpoint);
417 strscpyl(tmpdir, sizeof(tmpdir), "/run/udev/collect", NULL);
418 fd = prepare(tmpdir, checkpoint);
424 if (checkout(fd) < 0) {
429 for (i = argi; i < argc; i++) {
430 struct udev_list_node *him_node;
434 udev_list_node_foreach(him_node, &bunch) {
435 struct _mate *him = node_to_mate(him_node);
437 if (streq(him->name, argv[i]))
444 fprintf(stderr, "ID %s: not in database\n", argv[i]);
445 him = new(struct _mate, 1);
451 him->name = strdup(argv[i]);
458 him->state = STATE_NONE;
459 udev_list_node_append(&him->node, &bunch);
462 fprintf(stderr, "ID %s: found in database\n", argv[i]);
463 who->state = STATE_CONFIRMED;
474 fprintf(stderr, "Prune lists\n");
478 lseek(fd, 0, SEEK_SET);
482 lockf(fd, F_ULOCK, 0);
488 printf("COLLECT_%s=%d\n", checkpoint, ret);