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 sprintf(buf, "%s/%s", dir, filename);
98 fd = open(buf,O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
100 fprintf(stderr, "Cannot open %s: %s\n", buf, strerror(errno));
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: %s\n", buf, strerror(errno));
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 = calloc(1,bufsize + 1);
144 fprintf(stderr, "Out of memory.\n");
147 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));
170 him->name = strdup(word);
171 him->state = STATE_OLD;
172 udev_list_node_append(&him->node, &bunch);
176 memcpy(buf, buf + len, len);
177 memset(buf + len, ' ', len);
193 * Adds a new ID 'us' to the internal list,
194 * marks it as confirmed.
196 static void invite(char *us)
198 struct udev_list_node *him_node;
199 struct _mate *who = NULL;
202 fprintf(stderr, "Adding ID '%s'\n", us);
204 udev_list_node_foreach(him_node, &bunch) {
205 struct _mate *him = node_to_mate(him_node);
207 if (!strcmp(him->name, us)) {
208 him->state = STATE_CONFIRMED;
213 fprintf(stderr, "ID '%s' not in database\n", us);
220 * Marks the ID 'us' as invalid,
221 * causing it to be removed when the
222 * list is written out.
224 static void reject(char *us)
226 struct udev_list_node *him_node;
227 struct _mate *who = NULL;
230 fprintf(stderr, "Removing ID '%s'\n", us);
232 udev_list_node_foreach(him_node, &bunch) {
233 struct _mate *him = node_to_mate(him_node);
235 if (!strcmp(him->name, us)) {
236 him->state = STATE_NONE;
241 fprintf(stderr, "ID '%s' not in database\n", us);
247 * Remove all IDs in the internal list which are not part
248 * of the list passed via the commandline.
250 static void kickout(void)
252 struct udev_list_node *him_node;
253 struct udev_list_node *tmp;
255 udev_list_node_foreach_safe(him_node, tmp, &bunch) {
256 struct _mate *him = node_to_mate(him_node);
258 if (him->state == STATE_OLD) {
259 udev_list_node_remove(&him->node);
269 * Counts all missing IDs in the internal list.
271 static int missing(int fd)
275 struct udev_list_node *him_node;
277 buf = malloc(bufsize);
281 udev_list_node_foreach(him_node, &bunch) {
282 struct _mate *him = node_to_mate(him_node);
284 if (him->state == STATE_NONE) {
287 while (strlen(him->name)+1 >= bufsize) {
290 bufsize = bufsize << 1;
291 tmpbuf = realloc(buf, bufsize);
298 snprintf(buf, strlen(him->name)+2, "%s ", him->name);
299 write(fd, buf, strlen(buf));
310 * Prints out the status of the internal list.
312 static void everybody(void)
314 struct udev_list_node *him_node;
315 const char *state = "";
317 udev_list_node_foreach(him_node, &bunch) {
318 struct _mate *him = node_to_mate(him_node);
320 switch (him->state) {
327 case STATE_CONFIRMED:
331 fprintf(stderr, "ID: %s=%s\n", him->name, state);
335 int main(int argc, char **argv)
338 static const struct option options[] = {
339 { "add", no_argument, NULL, 'a' },
340 { "remove", no_argument, NULL, 'r' },
341 { "debug", no_argument, NULL, 'd' },
342 { "help", no_argument, NULL, 'h' },
346 char *checkpoint, *us;
349 int ret = EXIT_SUCCESS;
351 char tmpdir[UTIL_PATH_SIZE];
362 option = getopt_long(argc, argv, "ardh", options, NULL);
386 if (argi + 2 > argc) {
387 printf("Missing parameter(s)\n");
391 checkpoint = argv[argi++];
394 if (signal(SIGALRM, sig_alrm) == SIG_ERR) {
395 fprintf(stderr, "Cannot set SIGALRM: %s\n", strerror(errno));
400 udev_list_node_init(&bunch);
403 fprintf(stderr, "Using checkpoint '%s'\n", checkpoint);
405 util_strscpyl(tmpdir, sizeof(tmpdir), "/run/udev/collect", NULL);
406 fd = prepare(tmpdir, checkpoint);
412 if (checkout(fd) < 0) {
417 for (i = argi; i < argc; i++) {
418 struct udev_list_node *him_node;
422 udev_list_node_foreach(him_node, &bunch) {
423 struct _mate *him = node_to_mate(him_node);
425 if (!strcmp(him->name, argv[i]))
432 fprintf(stderr, "ID %s: not in database\n", argv[i]);
433 him = malloc(sizeof (struct _mate));
434 him->name = malloc(strlen(argv[i]) + 1);
435 strcpy(him->name, argv[i]);
436 him->state = STATE_NONE;
437 udev_list_node_append(&him->node, &bunch);
440 fprintf(stderr, "ID %s: found in database\n", argv[i]);
441 who->state = STATE_CONFIRMED;
452 fprintf(stderr, "Prune lists\n");
456 lseek(fd, 0, SEEK_SET);
460 lockf(fd, F_ULOCK, 0);
466 printf("COLLECT_%s=%d\n", checkpoint, ret);