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 struct _mate *node_to_mate(struct udev_list_node *node)
63 mate -= offsetof(struct _mate, node);
64 return (struct _mate *)mate;
67 static void sig_alrm(int signo)
72 static void usage(void)
74 printf("usage: collect [--add|--remove] [--debug] <checkpoint> <id> <idlist>\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"
88 * Prepares the database file
90 static int prepare(char *dir, char *filename)
96 if (stat(dir, &statbuf) < 0)
99 sprintf(buf, "%s/%s", dir, filename);
101 fd = open(buf,O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
103 fprintf(stderr, "Cannot open %s: %s\n", buf, strerror(errno));
105 if (lockf(fd,F_TLOCK,0) < 0) {
107 fprintf(stderr, "Lock taken, wait for %d seconds\n", UDEV_ALARM_TIMEOUT);
108 if (errno == EAGAIN || errno == EACCES) {
109 alarm(UDEV_ALARM_TIMEOUT);
110 lockf(fd, F_LOCK, 0);
112 fprintf(stderr, "Acquired lock on %s\n", buf);
115 fprintf(stderr, "Could not get lock on %s: %s\n", buf, strerror(errno));
123 * Read checkpoint file
125 * Tricky reading this. We allocate a buffer twice as large
126 * as we're going to read. Then we read into the upper half
127 * of that buffer and start parsing.
128 * Once we do _not_ find end-of-work terminator (whitespace
129 * character) we move the upper half to the lower half,
130 * adjust the read pointer and read the next bit.
131 * Quite clever methinks :-)
132 * I should become a programmer ...
134 * Yes, one could have used fgets() for this. But then we'd
135 * have to use freopen etc which I found quite tedious.
137 static int checkout(int fd)
140 char *buf, *ptr, *word = NULL;
145 buf = calloc(1,bufsize + 1);
147 fprintf(stderr, "Out of memory\n");
150 memset(buf, ' ', bufsize);
152 while ((read(fd, buf + len, len)) > 0) {
153 while (ptr && *ptr) {
155 ptr = strpbrk(word," \n\t\r");
156 if (!ptr && word < (buf + len)) {
157 bufsize = bufsize << 1;
159 fprintf(stderr, "ID overflow, restarting with size %zi\n", bufsize);
161 lseek(fd, 0, SEEK_SET);
171 fprintf(stderr, "Found word %s\n", word);
172 him = malloc(sizeof (struct _mate));
173 him->name = strdup(word);
174 him->state = STATE_OLD;
175 udev_list_node_append(&him->node, &bunch);
179 memcpy(buf, buf + len, len);
180 memset(buf + len, ' ', len);
196 * Adds a new ID 'us' to the internal list,
197 * marks it as confirmed.
199 static void invite(char *us)
201 struct udev_list_node *him_node;
202 struct _mate *who = NULL;
205 fprintf(stderr, "Adding ID '%s'\n", us);
207 udev_list_node_foreach(him_node, &bunch) {
208 struct _mate *him = node_to_mate(him_node);
210 if (!strcmp(him->name, us)) {
211 him->state = STATE_CONFIRMED;
216 fprintf(stderr, "ID '%s' not in database\n", us);
223 * Marks the ID 'us' as invalid,
224 * causing it to be removed when the
225 * list is written out.
227 static void reject(char *us)
229 struct udev_list_node *him_node;
230 struct _mate *who = NULL;
233 fprintf(stderr, "Removing ID '%s'\n", us);
235 udev_list_node_foreach(him_node, &bunch) {
236 struct _mate *him = node_to_mate(him_node);
238 if (!strcmp(him->name, us)) {
239 him->state = STATE_NONE;
244 fprintf(stderr, "ID '%s' not in database\n", us);
250 * Remove all IDs in the internal list which are not part
251 * of the list passed via the commandline.
253 static void kickout(void)
255 struct udev_list_node *him_node;
256 struct udev_list_node *tmp;
258 udev_list_node_foreach_safe(him_node, tmp, &bunch) {
259 struct _mate *him = node_to_mate(him_node);
261 if (him->state == STATE_OLD) {
262 udev_list_node_remove(&him->node);
272 * Counts all missing IDs in the internal list.
274 static int missing(int fd)
278 struct udev_list_node *him_node;
280 buf = malloc(bufsize);
284 udev_list_node_foreach(him_node, &bunch) {
285 struct _mate *him = node_to_mate(him_node);
287 if (him->state == STATE_NONE) {
290 while (strlen(him->name)+1 >= bufsize) {
293 bufsize = bufsize << 1;
294 tmpbuf = realloc(buf, bufsize);
301 snprintf(buf, strlen(him->name)+2, "%s ", him->name);
302 write(fd, buf, strlen(buf));
313 * Prints out the status of the internal list.
315 static void everybody(void)
317 struct udev_list_node *him_node;
318 const char *state = "";
320 udev_list_node_foreach(him_node, &bunch) {
321 struct _mate *him = node_to_mate(him_node);
323 switch (him->state) {
330 case STATE_CONFIRMED:
334 fprintf(stderr, "ID: %s=%s\n", him->name, state);
338 int main(int argc, char **argv)
341 static const struct option options[] = {
342 { "add", no_argument, NULL, 'a' },
343 { "remove", no_argument, NULL, 'r' },
344 { "debug", no_argument, NULL, 'd' },
345 { "help", no_argument, NULL, 'h' },
349 char *checkpoint, *us;
352 int ret = EXIT_SUCCESS;
354 char tmpdir[UTIL_PATH_SIZE];
365 option = getopt_long(argc, argv, "ardh", options, NULL);
389 if (argi + 2 > argc) {
390 printf("Missing parameter(s)\n");
394 checkpoint = argv[argi++];
397 if (signal(SIGALRM, sig_alrm) == SIG_ERR) {
398 fprintf(stderr, "Cannot set SIGALRM: %s\n", strerror(errno));
403 udev_list_node_init(&bunch);
406 fprintf(stderr, "Using checkpoint '%s'\n", checkpoint);
408 util_strscpyl(tmpdir, sizeof(tmpdir), udev_get_run_path(udev), "/collect", NULL);
409 fd = prepare(tmpdir, checkpoint);
415 if (checkout(fd) < 0) {
420 for (i = argi; i < argc; i++) {
421 struct udev_list_node *him_node;
425 udev_list_node_foreach(him_node, &bunch) {
426 struct _mate *him = node_to_mate(him_node);
428 if (!strcmp(him->name, argv[i]))
435 fprintf(stderr, "ID %s: not in database\n", argv[i]);
436 him = malloc(sizeof (struct _mate));
437 him->name = malloc(strlen(argv[i]) + 1);
438 strcpy(him->name, argv[i]);
439 him->state = STATE_NONE;
440 udev_list_node_append(&him->node, &bunch);
443 fprintf(stderr, "ID %s: found in database\n", argv[i]);
444 who->state = STATE_CONFIRMED;
455 fprintf(stderr, "Prune lists\n");
459 lseek(fd, 0, SEEK_SET);
463 lockf(fd, F_ULOCK, 0);
469 printf("COLLECT_%s=%d\n", checkpoint, ret);