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.
27 #include "libudev-private.h"
29 #include "string-util.h"
32 #define UDEV_ALARM_TIMEOUT 180
41 struct udev_list_node node
;
43 enum collect_state state
;
46 static struct udev_list_node bunch
;
49 /* This can increase dynamically */
50 static size_t bufsize
= BUFSIZE
;
52 static inline struct _mate
*node_to_mate(struct udev_list_node
*node
)
54 return container_of(node
, struct _mate
, node
);
57 noreturn
static void sig_alrm(int signo
)
62 static void usage(void)
64 printf("%s [options] <checkpoint> <id> <idlist>\n\n"
65 "Collect variables across events.\n\n"
66 " -h --help Print this message\n"
67 " -a --add Add ID <id> to the list <idlist>\n"
68 " -r --remove Remove ID <id> from the list <idlist>\n"
69 " -d --debug Debug to stderr\n\n"
70 " Adds ID <id> to the list governed by <checkpoint>.\n"
71 " <id> must be part of the list <idlist>.\n"
72 " If all IDs given by <idlist> are listed (ie collect has been\n"
73 " invoked for each ID in <idlist>) collect returns 0, the\n"
74 " number of missing IDs otherwise.\n"
75 " On error a negative number is returned.\n\n"
76 , program_invocation_short_name
);
82 * Prepares the database file
84 static int prepare(char *dir
, char *filename
)
90 if (r
< 0 && errno
!= EEXIST
)
93 snprintf(buf
, sizeof(buf
), "%s/%s", dir
, filename
);
95 fd
= open(buf
,O_RDWR
|O_CREAT
|O_CLOEXEC
, S_IRUSR
|S_IWUSR
);
97 fprintf(stderr
, "Cannot open %s: %m\n", buf
);
99 if (lockf(fd
,F_TLOCK
,0) < 0) {
101 fprintf(stderr
, "Lock taken, wait for %d seconds\n", UDEV_ALARM_TIMEOUT
);
102 if (errno
== EAGAIN
|| errno
== EACCES
) {
103 alarm(UDEV_ALARM_TIMEOUT
);
104 lockf(fd
, F_LOCK
, 0);
106 fprintf(stderr
, "Acquired lock on %s\n", buf
);
109 fprintf(stderr
, "Could not get lock on %s: %m\n", buf
);
117 * Read checkpoint file
119 * Tricky reading this. We allocate a buffer twice as large
120 * as we're going to read. Then we read into the upper half
121 * of that buffer and start parsing.
122 * Once we do _not_ find end-of-work terminator (whitespace
123 * character) we move the upper half to the lower half,
124 * adjust the read pointer and read the next bit.
125 * Quite clever methinks :-)
126 * I should become a programmer ...
128 * Yes, one could have used fgets() for this. But then we'd
129 * have to use freopen etc which I found quite tedious.
131 static int checkout(int fd
)
134 char *buf
, *ptr
, *word
= NULL
;
139 buf
= malloc(bufsize
+ 1);
142 memset(buf
, ' ', bufsize
);
146 while ((read(fd
, buf
+ len
, len
)) > 0) {
147 while (ptr
&& *ptr
) {
149 ptr
= strpbrk(word
," \n\t\r");
150 if (!ptr
&& word
< (buf
+ len
)) {
151 bufsize
= bufsize
<< 1;
153 fprintf(stderr
, "ID overflow, restarting with size %zu\n", bufsize
);
155 lseek(fd
, 0, SEEK_SET
);
165 fprintf(stderr
, "Found word %s\n", word
);
166 him
= malloc(sizeof (struct _mate
));
171 him
->name
= strdup(word
);
177 him
->state
= STATE_OLD
;
178 udev_list_node_append(&him
->node
, &bunch
);
182 memcpy(buf
, buf
+ len
, len
);
183 memset(buf
+ len
, ' ', len
);
199 * Adds a new ID 'us' to the internal list,
200 * marks it as confirmed.
202 static void invite(char *us
)
204 struct udev_list_node
*him_node
;
205 struct _mate
*who
= NULL
;
208 fprintf(stderr
, "Adding ID '%s'\n", us
);
210 udev_list_node_foreach(him_node
, &bunch
) {
211 struct _mate
*him
= node_to_mate(him_node
);
213 if (streq(him
->name
, us
)) {
214 him
->state
= STATE_CONFIRMED
;
219 fprintf(stderr
, "ID '%s' not in database\n", us
);
226 * Marks the ID 'us' as invalid,
227 * causing it to be removed when the
228 * list is written out.
230 static void reject(char *us
)
232 struct udev_list_node
*him_node
;
233 struct _mate
*who
= NULL
;
236 fprintf(stderr
, "Removing ID '%s'\n", us
);
238 udev_list_node_foreach(him_node
, &bunch
) {
239 struct _mate
*him
= node_to_mate(him_node
);
241 if (streq(him
->name
, us
)) {
242 him
->state
= STATE_NONE
;
247 fprintf(stderr
, "ID '%s' not in database\n", us
);
253 * Remove all IDs in the internal list which are not part
254 * of the list passed via the command line.
256 static void kickout(void)
258 struct udev_list_node
*him_node
;
259 struct udev_list_node
*tmp
;
261 udev_list_node_foreach_safe(him_node
, tmp
, &bunch
) {
262 struct _mate
*him
= node_to_mate(him_node
);
264 if (him
->state
== STATE_OLD
) {
265 udev_list_node_remove(&him
->node
);
275 * Counts all missing IDs in the internal list.
277 static int missing(int fd
)
281 struct udev_list_node
*him_node
;
283 buf
= malloc(bufsize
);
287 udev_list_node_foreach(him_node
, &bunch
) {
288 struct _mate
*him
= node_to_mate(him_node
);
290 if (him
->state
== STATE_NONE
) {
293 while (strlen(him
->name
)+1 >= bufsize
) {
296 bufsize
= bufsize
<< 1;
297 tmpbuf
= realloc(buf
, bufsize
);
304 snprintf(buf
, strlen(him
->name
)+2, "%s ", him
->name
);
305 if (write(fd
, buf
, strlen(buf
)) < 0) {
319 * Prints out the status of the internal list.
321 static void everybody(void)
323 struct udev_list_node
*him_node
;
324 const char *state
= "";
326 udev_list_node_foreach(him_node
, &bunch
) {
327 struct _mate
*him
= node_to_mate(him_node
);
329 switch (him
->state
) {
336 case STATE_CONFIRMED
:
340 fprintf(stderr
, "ID: %s=%s\n", him
->name
, state
);
344 int main(int argc
, char **argv
)
347 static const struct option options
[] = {
348 { "add", no_argument
, NULL
, 'a' },
349 { "remove", no_argument
, NULL
, 'r' },
350 { "debug", no_argument
, NULL
, 'd' },
351 { "help", no_argument
, NULL
, 'h' },
355 char *checkpoint
, *us
;
358 int ret
= EXIT_SUCCESS
;
360 char tmpdir
[UTIL_PATH_SIZE
];
371 option
= getopt_long(argc
, argv
, "ardh", options
, NULL
);
395 if (argi
+ 2 > argc
) {
396 printf("Missing parameter(s)\n");
400 checkpoint
= argv
[argi
++];
403 if (signal(SIGALRM
, sig_alrm
) == SIG_ERR
) {
404 fprintf(stderr
, "Cannot set SIGALRM: %m\n");
409 udev_list_node_init(&bunch
);
412 fprintf(stderr
, "Using checkpoint '%s'\n", checkpoint
);
414 strscpyl(tmpdir
, sizeof(tmpdir
), "/run/udev/collect", NULL
);
415 fd
= prepare(tmpdir
, checkpoint
);
421 if (checkout(fd
) < 0) {
426 for (i
= argi
; i
< argc
; i
++) {
427 struct udev_list_node
*him_node
;
431 udev_list_node_foreach(him_node
, &bunch
) {
432 struct _mate
*him
= node_to_mate(him_node
);
434 if (streq(him
->name
, argv
[i
]))
441 fprintf(stderr
, "ID %s: not in database\n", argv
[i
]);
442 him
= new(struct _mate
, 1);
448 him
->name
= strdup(argv
[i
]);
455 him
->state
= STATE_NONE
;
456 udev_list_node_append(&him
->node
, &bunch
);
459 fprintf(stderr
, "ID %s: found in database\n", argv
[i
]);
460 who
->state
= STATE_CONFIRMED
;
471 fprintf(stderr
, "Prune lists\n");
475 lseek(fd
, 0, SEEK_SET
);
479 lockf(fd
, F_ULOCK
, 0);
485 printf("COLLECT_%s=%d\n", checkpoint
, ret
);