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"
31 #define UDEV_ALARM_TIMEOUT 180
40 struct udev_list_node node
;
42 enum collect_state state
;
45 static struct udev_list_node bunch
;
48 /* This can increase dynamically */
49 static size_t bufsize
= BUFSIZE
;
51 static inline struct _mate
*node_to_mate(struct udev_list_node
*node
)
53 return container_of(node
, struct _mate
, node
);
56 noreturn
static void sig_alrm(int signo
)
61 static void usage(void)
63 printf("%s [options] <checkpoint> <id> <idlist>\n\n"
64 "Collect variables across events.\n\n"
65 " -h --help Print this message\n"
66 " -a --add Add ID <id> to the list <idlist>\n"
67 " -r --remove Remove ID <id> from the list <idlist>\n"
68 " -d --debug Debug to stderr\n\n"
69 " Adds ID <id> to the list governed by <checkpoint>.\n"
70 " <id> must be part of the list <idlist>.\n"
71 " If all IDs given by <idlist> are listed (ie collect has been\n"
72 " invoked for each ID in <idlist>) collect returns 0, the\n"
73 " number of missing IDs otherwise.\n"
74 " On error a negative number is returned.\n\n"
75 , program_invocation_short_name
);
81 * Prepares the database file
83 static int prepare(char *dir
, char *filename
)
89 if (r
< 0 && errno
!= EEXIST
)
92 snprintf(buf
, sizeof(buf
), "%s/%s", dir
, filename
);
94 fd
= open(buf
,O_RDWR
|O_CREAT
|O_CLOEXEC
, S_IRUSR
|S_IWUSR
);
96 fprintf(stderr
, "Cannot open %s: %m\n", buf
);
98 if (lockf(fd
,F_TLOCK
,0) < 0) {
100 fprintf(stderr
, "Lock taken, wait for %d seconds\n", UDEV_ALARM_TIMEOUT
);
101 if (errno
== EAGAIN
|| errno
== EACCES
) {
102 alarm(UDEV_ALARM_TIMEOUT
);
103 lockf(fd
, F_LOCK
, 0);
105 fprintf(stderr
, "Acquired lock on %s\n", buf
);
108 fprintf(stderr
, "Could not get lock on %s: %m\n", buf
);
116 * Read checkpoint file
118 * Tricky reading this. We allocate a buffer twice as large
119 * as we're going to read. Then we read into the upper half
120 * of that buffer and start parsing.
121 * Once we do _not_ find end-of-work terminator (whitespace
122 * character) we move the upper half to the lower half,
123 * adjust the read pointer and read the next bit.
124 * Quite clever methinks :-)
125 * I should become a programmer ...
127 * Yes, one could have used fgets() for this. But then we'd
128 * have to use freopen etc which I found quite tedious.
130 static int checkout(int fd
)
133 char *buf
, *ptr
, *word
= NULL
;
138 buf
= malloc(bufsize
+ 1);
141 memset(buf
, ' ', bufsize
);
145 while ((read(fd
, buf
+ len
, len
)) > 0) {
146 while (ptr
&& *ptr
) {
148 ptr
= strpbrk(word
," \n\t\r");
149 if (!ptr
&& word
< (buf
+ len
)) {
150 bufsize
= bufsize
<< 1;
152 fprintf(stderr
, "ID overflow, restarting with size %zu\n", bufsize
);
154 lseek(fd
, 0, SEEK_SET
);
164 fprintf(stderr
, "Found word %s\n", word
);
165 him
= malloc(sizeof (struct _mate
));
170 him
->name
= strdup(word
);
176 him
->state
= STATE_OLD
;
177 udev_list_node_append(&him
->node
, &bunch
);
181 memcpy(buf
, buf
+ len
, len
);
182 memset(buf
+ len
, ' ', len
);
198 * Adds a new ID 'us' to the internal list,
199 * marks it as confirmed.
201 static void invite(char *us
)
203 struct udev_list_node
*him_node
;
204 struct _mate
*who
= NULL
;
207 fprintf(stderr
, "Adding ID '%s'\n", us
);
209 udev_list_node_foreach(him_node
, &bunch
) {
210 struct _mate
*him
= node_to_mate(him_node
);
212 if (streq(him
->name
, us
)) {
213 him
->state
= STATE_CONFIRMED
;
218 fprintf(stderr
, "ID '%s' not in database\n", us
);
225 * Marks the ID 'us' as invalid,
226 * causing it to be removed when the
227 * list is written out.
229 static void reject(char *us
)
231 struct udev_list_node
*him_node
;
232 struct _mate
*who
= NULL
;
235 fprintf(stderr
, "Removing ID '%s'\n", us
);
237 udev_list_node_foreach(him_node
, &bunch
) {
238 struct _mate
*him
= node_to_mate(him_node
);
240 if (streq(him
->name
, us
)) {
241 him
->state
= STATE_NONE
;
246 fprintf(stderr
, "ID '%s' not in database\n", us
);
252 * Remove all IDs in the internal list which are not part
253 * of the list passed via the command line.
255 static void kickout(void)
257 struct udev_list_node
*him_node
;
258 struct udev_list_node
*tmp
;
260 udev_list_node_foreach_safe(him_node
, tmp
, &bunch
) {
261 struct _mate
*him
= node_to_mate(him_node
);
263 if (him
->state
== STATE_OLD
) {
264 udev_list_node_remove(&him
->node
);
274 * Counts all missing IDs in the internal list.
276 static int missing(int fd
)
280 struct udev_list_node
*him_node
;
282 buf
= malloc(bufsize
);
286 udev_list_node_foreach(him_node
, &bunch
) {
287 struct _mate
*him
= node_to_mate(him_node
);
289 if (him
->state
== STATE_NONE
) {
292 while (strlen(him
->name
)+1 >= bufsize
) {
295 bufsize
= bufsize
<< 1;
296 tmpbuf
= realloc(buf
, bufsize
);
303 snprintf(buf
, strlen(him
->name
)+2, "%s ", him
->name
);
304 if (write(fd
, buf
, strlen(buf
)) < 0) {
318 * Prints out the status of the internal list.
320 static void everybody(void)
322 struct udev_list_node
*him_node
;
323 const char *state
= "";
325 udev_list_node_foreach(him_node
, &bunch
) {
326 struct _mate
*him
= node_to_mate(him_node
);
328 switch (him
->state
) {
335 case STATE_CONFIRMED
:
339 fprintf(stderr
, "ID: %s=%s\n", him
->name
, state
);
343 int main(int argc
, char **argv
)
346 static const struct option options
[] = {
347 { "add", no_argument
, NULL
, 'a' },
348 { "remove", no_argument
, NULL
, 'r' },
349 { "debug", no_argument
, NULL
, 'd' },
350 { "help", no_argument
, NULL
, 'h' },
354 char *checkpoint
, *us
;
357 int ret
= EXIT_SUCCESS
;
359 char tmpdir
[UTIL_PATH_SIZE
];
370 option
= getopt_long(argc
, argv
, "ardh", options
, NULL
);
394 if (argi
+ 2 > argc
) {
395 printf("Missing parameter(s)\n");
399 checkpoint
= argv
[argi
++];
402 if (signal(SIGALRM
, sig_alrm
) == SIG_ERR
) {
403 fprintf(stderr
, "Cannot set SIGALRM: %m\n");
408 udev_list_node_init(&bunch
);
411 fprintf(stderr
, "Using checkpoint '%s'\n", checkpoint
);
413 strscpyl(tmpdir
, sizeof(tmpdir
), "/run/udev/collect", NULL
);
414 fd
= prepare(tmpdir
, checkpoint
);
420 if (checkout(fd
) < 0) {
425 for (i
= argi
; i
< argc
; i
++) {
426 struct udev_list_node
*him_node
;
430 udev_list_node_foreach(him_node
, &bunch
) {
431 struct _mate
*him
= node_to_mate(him_node
);
433 if (streq(him
->name
, argv
[i
]))
440 fprintf(stderr
, "ID %s: not in database\n", argv
[i
]);
441 him
= new(struct _mate
, 1);
447 him
->name
= strdup(argv
[i
]);
454 him
->state
= STATE_NONE
;
455 udev_list_node_append(&him
->node
, &bunch
);
458 fprintf(stderr
, "ID %s: found in database\n", argv
[i
]);
459 who
->state
= STATE_CONFIRMED
;
470 fprintf(stderr
, "Prune lists\n");
474 lseek(fd
, 0, SEEK_SET
);
478 lockf(fd
, F_ULOCK
, 0);
484 printf("COLLECT_%s=%d\n", checkpoint
, ret
);