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 "alloc-util.h"
28 #include "libudev-private.h"
30 #include "stdio-util.h"
31 #include "string-util.h"
34 #define UDEV_ALARM_TIMEOUT 180
43 struct udev_list_node node
;
45 enum collect_state state
;
48 static struct udev_list_node bunch
;
51 /* This can increase dynamically */
52 static size_t bufsize
= BUFSIZE
;
54 static inline struct _mate
*node_to_mate(struct udev_list_node
*node
)
56 return container_of(node
, struct _mate
, node
);
59 noreturn
static void sig_alrm(int signo
)
64 static void usage(void)
66 printf("%s [options] <checkpoint> <id> <idlist>\n\n"
67 "Collect variables across events.\n\n"
68 " -h --help Print this message\n"
69 " -a --add Add ID <id> to the list <idlist>\n"
70 " -r --remove Remove ID <id> from the list <idlist>\n"
71 " -d --debug Debug to stderr\n\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\n"
78 , program_invocation_short_name
);
84 * Prepares the database file
86 static int prepare(char *dir
, char *filename
)
92 if (r
< 0 && errno
!= EEXIST
)
95 xsprintf(buf
, "%s/%s", dir
, filename
);
97 fd
= open(buf
,O_RDWR
|O_CREAT
|O_CLOEXEC
, S_IRUSR
|S_IWUSR
);
99 fprintf(stderr
, "Cannot open %s: %m\n", buf
);
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: %m\n", buf
);
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
= malloc(bufsize
+ 1);
144 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 %zu\n", bufsize
);
157 lseek(fd
, 0, SEEK_SET
);
167 fprintf(stderr
, "Found word %s\n", word
);
168 him
= malloc(sizeof (struct _mate
));
173 him
->name
= strdup(word
);
179 him
->state
= STATE_OLD
;
180 udev_list_node_append(&him
->node
, &bunch
);
184 memcpy(buf
, buf
+ len
, len
);
185 memset(buf
+ len
, ' ', len
);
201 * Adds a new ID 'us' to the internal list,
202 * marks it as confirmed.
204 static void invite(char *us
)
206 struct udev_list_node
*him_node
;
207 struct _mate
*who
= NULL
;
210 fprintf(stderr
, "Adding ID '%s'\n", us
);
212 udev_list_node_foreach(him_node
, &bunch
) {
213 struct _mate
*him
= node_to_mate(him_node
);
215 if (streq(him
->name
, us
)) {
216 him
->state
= STATE_CONFIRMED
;
221 fprintf(stderr
, "ID '%s' not in database\n", us
);
228 * Marks the ID 'us' as invalid,
229 * causing it to be removed when the
230 * list is written out.
232 static void reject(char *us
)
234 struct udev_list_node
*him_node
;
235 struct _mate
*who
= NULL
;
238 fprintf(stderr
, "Removing ID '%s'\n", us
);
240 udev_list_node_foreach(him_node
, &bunch
) {
241 struct _mate
*him
= node_to_mate(him_node
);
243 if (streq(him
->name
, us
)) {
244 him
->state
= STATE_NONE
;
249 fprintf(stderr
, "ID '%s' not in database\n", us
);
255 * Remove all IDs in the internal list which are not part
256 * of the list passed via the command line.
258 static void kickout(void)
260 struct udev_list_node
*him_node
;
261 struct udev_list_node
*tmp
;
263 udev_list_node_foreach_safe(him_node
, tmp
, &bunch
) {
264 struct _mate
*him
= node_to_mate(him_node
);
266 if (him
->state
== STATE_OLD
) {
267 udev_list_node_remove(&him
->node
);
277 * Counts all missing IDs in the internal list.
279 static int missing(int fd
)
283 struct udev_list_node
*him_node
;
285 buf
= malloc(bufsize
);
289 udev_list_node_foreach(him_node
, &bunch
) {
290 struct _mate
*him
= node_to_mate(him_node
);
292 if (him
->state
== STATE_NONE
) {
295 while (strlen(him
->name
)+1 >= bufsize
) {
298 bufsize
= bufsize
<< 1;
299 tmpbuf
= realloc(buf
, bufsize
);
306 snprintf(buf
, strlen(him
->name
)+2, "%s ", him
->name
);
307 if (write(fd
, buf
, strlen(buf
)) < 0) {
321 * Prints out the status of the internal list.
323 static void everybody(void)
325 struct udev_list_node
*him_node
;
326 const char *state
= "";
328 udev_list_node_foreach(him_node
, &bunch
) {
329 struct _mate
*him
= node_to_mate(him_node
);
331 switch (him
->state
) {
338 case STATE_CONFIRMED
:
342 fprintf(stderr
, "ID: %s=%s\n", him
->name
, state
);
346 int main(int argc
, char **argv
)
349 static const struct option options
[] = {
350 { "add", no_argument
, NULL
, 'a' },
351 { "remove", no_argument
, NULL
, 'r' },
352 { "debug", no_argument
, NULL
, 'd' },
353 { "help", no_argument
, NULL
, 'h' },
357 char *checkpoint
, *us
;
360 int ret
= EXIT_SUCCESS
;
362 char tmpdir
[UTIL_PATH_SIZE
];
373 option
= getopt_long(argc
, argv
, "ardh", options
, NULL
);
397 if (argi
+ 2 > argc
) {
398 printf("Missing parameter(s)\n");
402 checkpoint
= argv
[argi
++];
405 if (signal(SIGALRM
, sig_alrm
) == SIG_ERR
) {
406 fprintf(stderr
, "Cannot set SIGALRM: %m\n");
411 udev_list_node_init(&bunch
);
414 fprintf(stderr
, "Using checkpoint '%s'\n", checkpoint
);
416 strscpyl(tmpdir
, sizeof(tmpdir
), "/run/udev/collect", NULL
);
417 fd
= prepare(tmpdir
, checkpoint
);
423 if (checkout(fd
) < 0) {
428 for (i
= argi
; i
< argc
; i
++) {
429 struct udev_list_node
*him_node
;
433 udev_list_node_foreach(him_node
, &bunch
) {
434 struct _mate
*him
= node_to_mate(him_node
);
436 if (streq(him
->name
, argv
[i
]))
443 fprintf(stderr
, "ID %s: not in database\n", argv
[i
]);
444 him
= new(struct _mate
, 1);
450 him
->name
= strdup(argv
[i
]);
457 him
->state
= STATE_NONE
;
458 udev_list_node_append(&him
->node
, &bunch
);
461 fprintf(stderr
, "ID %s: found in database\n", argv
[i
]);
462 who
->state
= STATE_CONFIRMED
;
473 fprintf(stderr
, "Prune lists\n");
477 lseek(fd
, 0, SEEK_SET
);
481 lockf(fd
, F_ULOCK
, 0);
487 printf("COLLECT_%s=%d\n", checkpoint
, ret
);