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"
38 #define UDEV_ALARM_TIMEOUT 180
47 struct udev_list_node node;
49 enum collect_state state;
52 static struct udev_list_node bunch;
55 /* This can increase dynamically */
56 static size_t bufsize = BUFSIZE;
58 static inline struct _mate *node_to_mate(struct udev_list_node *node)
60 return container_of(node, struct _mate, node);
63 static void sig_alrm(int signo)
68 static void usage(void)
70 printf("usage: collect [--add|--remove] [--debug] <checkpoint> <id> <idlist>\n"
72 " Adds ID <id> to the list governed by <checkpoint>.\n"
73 " <id> must be part of the list <idlist>.\n"
74 " If all IDs given by <idlist> are listed (ie collect has been\n"
75 " invoked for each ID in <idlist>) collect returns 0, the\n"
76 " number of missing IDs otherwise.\n"
77 " On error a negative number is returned.\n"
84 * Prepares the database file
86 static int prepare(char *dir, char *filename)
92 if (stat(dir, &statbuf) < 0)
95 sprintf(buf, "%s/%s", dir, filename);
97 fd = open(buf,O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
99 fprintf(stderr, "Cannot open %s: %s\n", buf, strerror(errno));
101 if (lockf(fd,F_TLOCK,0) < 0) {
103 fprintf(stderr, "Lock taken, wait for %d seconds\n", UDEV_ALARM_TIMEOUT);
104 if (errno == EAGAIN || errno == EACCES) {
105 alarm(UDEV_ALARM_TIMEOUT);
106 lockf(fd, F_LOCK, 0);
108 fprintf(stderr, "Acquired lock on %s\n", buf);
111 fprintf(stderr, "Could not get lock on %s: %s\n", buf, strerror(errno));
119 * Read checkpoint file
121 * Tricky reading this. We allocate a buffer twice as large
122 * as we're going to read. Then we read into the upper half
123 * of that buffer and start parsing.
124 * Once we do _not_ find end-of-work terminator (whitespace
125 * character) we move the upper half to the lower half,
126 * adjust the read pointer and read the next bit.
127 * Quite clever methinks :-)
128 * I should become a programmer ...
130 * Yes, one could have used fgets() for this. But then we'd
131 * have to use freopen etc which I found quite tedious.
133 static int checkout(int fd)
136 char *buf, *ptr, *word = NULL;
141 buf = calloc(1,bufsize + 1);
143 fprintf(stderr, "Out of memory\n");
146 memset(buf, ' ', bufsize);
148 while ((read(fd, buf + len, len)) > 0) {
149 while (ptr && *ptr) {
151 ptr = strpbrk(word," \n\t\r");
152 if (!ptr && word < (buf + len)) {
153 bufsize = bufsize << 1;
155 fprintf(stderr, "ID overflow, restarting with size %zi\n", bufsize);
157 lseek(fd, 0, SEEK_SET);
167 fprintf(stderr, "Found word %s\n", word);
168 him = malloc(sizeof (struct _mate));
169 him->name = strdup(word);
170 him->state = STATE_OLD;
171 udev_list_node_append(&him->node, &bunch);
175 memcpy(buf, buf + len, len);
176 memset(buf + len, ' ', len);
192 * Adds a new ID 'us' to the internal list,
193 * marks it as confirmed.
195 static void invite(char *us)
197 struct udev_list_node *him_node;
198 struct _mate *who = NULL;
201 fprintf(stderr, "Adding ID '%s'\n", us);
203 udev_list_node_foreach(him_node, &bunch) {
204 struct _mate *him = node_to_mate(him_node);
206 if (!strcmp(him->name, us)) {
207 him->state = STATE_CONFIRMED;
212 fprintf(stderr, "ID '%s' not in database\n", us);
219 * Marks the ID 'us' as invalid,
220 * causing it to be removed when the
221 * list is written out.
223 static void reject(char *us)
225 struct udev_list_node *him_node;
226 struct _mate *who = NULL;
229 fprintf(stderr, "Removing ID '%s'\n", us);
231 udev_list_node_foreach(him_node, &bunch) {
232 struct _mate *him = node_to_mate(him_node);
234 if (!strcmp(him->name, us)) {
235 him->state = STATE_NONE;
240 fprintf(stderr, "ID '%s' not in database\n", us);
246 * Remove all IDs in the internal list which are not part
247 * of the list passed via the commandline.
249 static void kickout(void)
251 struct udev_list_node *him_node;
252 struct udev_list_node *tmp;
254 udev_list_node_foreach_safe(him_node, tmp, &bunch) {
255 struct _mate *him = node_to_mate(him_node);
257 if (him->state == STATE_OLD) {
258 udev_list_node_remove(&him->node);
268 * Counts all missing IDs in the internal list.
270 static int missing(int fd)
274 struct udev_list_node *him_node;
276 buf = malloc(bufsize);
280 udev_list_node_foreach(him_node, &bunch) {
281 struct _mate *him = node_to_mate(him_node);
283 if (him->state == STATE_NONE) {
286 while (strlen(him->name)+1 >= bufsize) {
289 bufsize = bufsize << 1;
290 tmpbuf = realloc(buf, bufsize);
297 snprintf(buf, strlen(him->name)+2, "%s ", him->name);
298 write(fd, buf, strlen(buf));
309 * Prints out the status of the internal list.
311 static void everybody(void)
313 struct udev_list_node *him_node;
314 const char *state = "";
316 udev_list_node_foreach(him_node, &bunch) {
317 struct _mate *him = node_to_mate(him_node);
319 switch (him->state) {
326 case STATE_CONFIRMED:
330 fprintf(stderr, "ID: %s=%s\n", him->name, state);
334 int main(int argc, char **argv)
337 static const struct option options[] = {
338 { "add", no_argument, NULL, 'a' },
339 { "remove", no_argument, NULL, 'r' },
340 { "debug", no_argument, NULL, 'd' },
341 { "help", no_argument, NULL, 'h' },
345 char *checkpoint, *us;
348 int ret = EXIT_SUCCESS;
350 char tmpdir[UTIL_PATH_SIZE];
361 option = getopt_long(argc, argv, "ardh", options, NULL);
385 if (argi + 2 > argc) {
386 printf("Missing parameter(s)\n");
390 checkpoint = argv[argi++];
393 if (signal(SIGALRM, sig_alrm) == SIG_ERR) {
394 fprintf(stderr, "Cannot set SIGALRM: %s\n", strerror(errno));
399 udev_list_node_init(&bunch);
402 fprintf(stderr, "Using checkpoint '%s'\n", checkpoint);
404 util_strscpyl(tmpdir, sizeof(tmpdir), "/run/udev/collect", NULL);
405 fd = prepare(tmpdir, checkpoint);
411 if (checkout(fd) < 0) {
416 for (i = argi; i < argc; i++) {
417 struct udev_list_node *him_node;
421 udev_list_node_foreach(him_node, &bunch) {
422 struct _mate *him = node_to_mate(him_node);
424 if (!strcmp(him->name, argv[i]))
431 fprintf(stderr, "ID %s: not in database\n", argv[i]);
432 him = malloc(sizeof (struct _mate));
433 him->name = malloc(strlen(argv[i]) + 1);
434 strcpy(him->name, argv[i]);
435 him->state = STATE_NONE;
436 udev_list_node_append(&him->node, &bunch);
439 fprintf(stderr, "ID %s: found in database\n", argv[i]);
440 who->state = STATE_CONFIRMED;
451 fprintf(stderr, "Prune lists\n");
455 lseek(fd, 0, SEEK_SET);
459 lockf(fd, F_ULOCK, 0);
465 printf("COLLECT_%s=%d\n", checkpoint, ret);