chiark / gitweb /
udevtrigger: options to filter by subsystem and sysfs attribute
[elogind.git] / udevtrigger.c
1 /*
2  * Copyright (C) 2004-2006 Kay Sievers <kay@vrfy.org>
3  * Copyright (C) 2006 Hannes Reinecke <hare@suse.de>
4  *
5  *      This program is free software; you can redistribute it and/or modify it
6  *      under the terms of the GNU General Public License as published by the
7  *      Free Software Foundation version 2 of the License.
8  * 
9  *      This program is distributed in the hope that it will be useful, but
10  *      WITHOUT ANY WARRANTY; without even the implied warranty of
11  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *      General Public License for more details.
13  * 
14  *      You should have received a copy of the GNU General Public License along
15  *      with this program; if not, write to the Free Software Foundation, Inc.,
16  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <errno.h>
27 #include <dirent.h>
28 #include <fcntl.h>
29 #include <syslog.h>
30 #include <fnmatch.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33
34 #include "udev.h"
35 #include "udevd.h"
36
37 #ifdef USE_LOG
38 void log_message(int priority, const char *format, ...)
39 {
40         va_list args;
41
42         if (priority > udev_log_priority)
43                 return;
44
45         va_start(args, format);
46         vsyslog(priority, format, args);
47         va_end(args);
48 }
49 #endif
50
51 static int verbose;
52 static int dry_run;
53
54 /* list of devices that we should run last due to any one of a number of reasons */
55 static char *last_list[] = {
56         "/class/block/md",
57         "/class/block/dm-",
58         "/block/md",
59         "/block/dm-",
60         NULL
61 };
62
63 /* list of devices that we should run first due to any one of a number of reasons */
64 static char *first_list[] = {
65         "/class/mem",
66         "/class/tty",
67         NULL
68 };
69
70 LIST_HEAD(device_first_list);
71 LIST_HEAD(device_default_list);
72 LIST_HEAD(device_last_list);
73
74 LIST_HEAD(filter_subsytem_match_list);
75 LIST_HEAD(filter_subsytem_nomatch_list);
76 LIST_HEAD(filter_attr_match_list);
77 LIST_HEAD(filter_attr_nomatch_list);
78
79 static int device_list_insert(const char *path)
80 {
81         struct list_head *device_list = &device_default_list;
82         const char *devpath = &path[strlen(sysfs_path)];
83         int i;
84
85         for (i = 0; first_list[i] != NULL; i++) {
86                 if (strncmp(devpath, first_list[i], strlen(first_list[i])) == 0) {
87                         device_list = &device_first_list;
88                         break;
89                 }
90         }
91         for (i = 0; last_list[i] != NULL; i++) {
92                 if (strncmp(devpath, last_list[i], strlen(last_list[i])) == 0) {
93                         device_list = &device_last_list;
94                         break;
95                 }
96         }
97
98         dbg("add '%s'" , path);
99         /* double entries will be ignored */
100         name_list_add(device_list, path, 0);
101         return 0;
102 }
103
104 static void trigger_uevent(const char *path)
105 {
106         char filename[PATH_SIZE];
107         int fd;
108
109         strlcpy(filename, path, sizeof(filename));
110         strlcat(filename, "/uevent", sizeof(filename));
111
112         if (verbose)
113                 printf("%s\n", path);
114
115         if (dry_run)
116                 return;
117
118         fd = open(filename, O_WRONLY);
119         if (fd < 0) {
120                 dbg("error on opening %s: %s\n", filename, strerror(errno));
121                 return;
122         }
123
124         if (write(fd, "add", 3) < 0)
125                 info("error on triggering %s: %s\n", filename, strerror(errno));
126
127         close(fd);
128 }
129
130 static void exec_lists(void)
131 {
132         struct name_entry *loop_device;
133         struct name_entry *tmp_device;
134
135         /* handle the devices on the "first" list first */
136         list_for_each_entry_safe(loop_device, tmp_device, &device_first_list, node) {
137                 trigger_uevent(loop_device->name);
138                 list_del(&loop_device->node);
139                 free(loop_device);
140         }
141
142         /* handle the devices on the "default" list next */
143         list_for_each_entry_safe(loop_device, tmp_device, &device_default_list, node) {
144                 trigger_uevent(loop_device->name);
145                 list_del(&loop_device->node);
146                 free(loop_device);
147         }
148
149         /* handle devices on the "last" list, if any */
150         list_for_each_entry_safe(loop_device, tmp_device, &device_last_list, node) {
151                 trigger_uevent(loop_device->name);
152                 list_del(&loop_device->node);
153                 free(loop_device);
154         }
155 }
156
157 static int is_device(const char *path)
158 {
159         char filename[PATH_SIZE];
160         struct stat statbuf;
161
162         /* look for the uevent file of the kobject */
163         strlcpy(filename, path, sizeof(filename));
164         strlcat(filename, "/uevent", sizeof(filename));
165         if (stat(filename, &statbuf) < 0)
166                 return 0;
167
168         if (!(statbuf.st_mode & S_IWUSR))
169                 return 0;
170
171         return 1;
172 }
173
174 static int subsystem_filtered(const char *subsystem)
175 {
176         struct name_entry *loop_name;
177
178         /* skip devices matching the listed subsystems */
179         list_for_each_entry(loop_name, &filter_subsytem_nomatch_list, node)
180                 if (fnmatch(subsystem, loop_name->name, 0) == 0)
181                         return 1;
182
183         /* skip devices not matching the listed subsystems */
184         if (!list_empty(&filter_subsytem_match_list)) {
185                 list_for_each_entry(loop_name, &filter_subsytem_match_list, node)
186                         if (fnmatch(subsystem, loop_name->name, 0) == 0)
187                                 return 0;
188                 return 1;
189         }
190
191         return 0;
192 }
193
194 static int attr_match(const char *path, const char *attr_value)
195 {
196         char attr[NAME_SIZE];
197         char file[PATH_SIZE];
198         char *match_value;
199
200         strlcpy(attr, attr_value, sizeof(attr));
201
202         /* separate attr and match value */
203         match_value = strchr(attr, '=');
204         if (match_value != NULL) {
205                 match_value[0] = '\0';
206                 match_value = &match_value[1];
207         }
208
209         strlcpy(file, path, sizeof(file));
210         strlcat(file, "/", sizeof(file));
211         strlcat(file, attr, sizeof(file));
212
213         if (match_value != NULL) {
214                 /* match file content */
215                 char value[NAME_SIZE];
216                 int fd;
217                 ssize_t size;
218
219                 fd = open(file, O_RDONLY);
220                 if (fd < 0)
221                         return 0;
222                 size = read(fd, value, sizeof(value));
223                 close(fd);
224                 if (size < 0)
225                         return 0;
226                 value[size] = '\0';
227                 remove_trailing_chars(value, '\n');
228
229                 /* match if attribute value matches */
230                 if (fnmatch(match_value, value, 0) == 0)
231                         return 1;
232         } else {
233                 /* match if attribute exists */
234                 struct stat statbuf;
235
236                 if (stat(file, &statbuf) == 0)
237                         return 1;
238         }
239         return 0;
240 }
241
242 static int attr_filtered(const char *path)
243 {
244         struct name_entry *loop_name;
245
246         /* skip devices matching the listed sysfs attributes */
247         list_for_each_entry(loop_name, &filter_attr_nomatch_list, node)
248                 if (attr_match(path, loop_name->name))
249                         return 1;
250
251         /* skip devices not matching the listed sysfs attributes */
252         if (!list_empty(&filter_attr_match_list)) {
253                 list_for_each_entry(loop_name, &filter_attr_match_list, node)
254                         if (attr_match(path, loop_name->name))
255                                 return 0;
256                 return 1;
257         }
258         return 0;
259 }
260
261 static void scan_bus(void)
262 {
263         char base[PATH_SIZE];
264         DIR *dir;
265         struct dirent *dent;
266
267         strlcpy(base, sysfs_path, sizeof(base));
268         strlcat(base, "/bus", sizeof(base));
269
270         dir = opendir(base);
271         if (dir != NULL) {
272                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
273                         char dirname[PATH_SIZE];
274                         DIR *dir2;
275                         struct dirent *dent2;
276
277                         if (dent->d_name[0] == '.')
278                                 continue;
279
280                         if (subsystem_filtered(dent->d_name))
281                                 continue;
282
283                         strlcpy(dirname, base, sizeof(dirname));
284                         strlcat(dirname, "/", sizeof(dirname));
285                         strlcat(dirname, dent->d_name, sizeof(dirname));
286                         strlcat(dirname, "/devices", sizeof(dirname));
287
288                         /* look for devices */
289                         dir2 = opendir(dirname);
290                         if (dir2 != NULL) {
291                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
292                                         char dirname2[PATH_SIZE];
293
294                                         if (dent2->d_name[0] == '.')
295                                                 continue;
296
297                                         strlcpy(dirname2, dirname, sizeof(dirname2));
298                                         strlcat(dirname2, "/", sizeof(dirname2));
299                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
300                                         if (attr_filtered(dirname2))
301                                                 continue;
302                                         if (is_device(dirname2))
303                                                 device_list_insert(dirname2);
304                                 }
305                                 closedir(dir2);
306                         }
307                 }
308                 closedir(dir);
309         }
310 }
311
312 static void scan_block(void)
313 {
314         char base[PATH_SIZE];
315         DIR *dir;
316         struct dirent *dent;
317         struct stat statbuf;
318
319         /* skip if "block" is already a "class" */
320         strlcpy(base, sysfs_path, sizeof(base));
321         strlcat(base, "/class/block", sizeof(base));
322         if (stat(base, &statbuf) == 0)
323                 return;
324
325         if (subsystem_filtered("block"))
326                 return;
327
328         strlcpy(base, sysfs_path, sizeof(base));
329         strlcat(base, "/block", sizeof(base));
330
331         dir = opendir(base);
332         if (dir != NULL) {
333                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
334                         char dirname[PATH_SIZE];
335                         DIR *dir2;
336                         struct dirent *dent2;
337
338                         if (dent->d_name[0] == '.')
339                                 continue;
340
341                         strlcpy(dirname, base, sizeof(dirname));
342                         strlcat(dirname, "/", sizeof(dirname));
343                         strlcat(dirname, dent->d_name, sizeof(dirname));
344                         if (attr_filtered(dirname))
345                                 continue;
346                         if (is_device(dirname))
347                                 device_list_insert(dirname);
348                         else
349                                 continue;
350
351                         /* look for partitions */
352                         dir2 = opendir(dirname);
353                         if (dir2 != NULL) {
354                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
355                                         char dirname2[PATH_SIZE];
356
357                                         if (dent2->d_name[0] == '.')
358                                                 continue;
359
360                                         if (!strcmp(dent2->d_name,"device"))
361                                                 continue;
362
363                                         strlcpy(dirname2, dirname, sizeof(dirname2));
364                                         strlcat(dirname2, "/", sizeof(dirname2));
365                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
366                                         if (attr_filtered(dirname2))
367                                                 continue;
368                                         if (is_device(dirname2))
369                                                 device_list_insert(dirname2);
370                                 }
371                                 closedir(dir2);
372                         }
373                 }
374                 closedir(dir);
375         }
376 }
377
378 static void scan_class(void)
379 {
380         char base[PATH_SIZE];
381         DIR *dir;
382         struct dirent *dent;
383
384         strlcpy(base, sysfs_path, sizeof(base));
385         strlcat(base, "/class", sizeof(base));
386
387         dir = opendir(base);
388         if (dir != NULL) {
389                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
390                         char dirname[PATH_SIZE];
391                         DIR *dir2;
392                         struct dirent *dent2;
393
394                         if (dent->d_name[0] == '.')
395                                 continue;
396
397                         if (subsystem_filtered(dent->d_name))
398                                 continue;
399
400                         strlcpy(dirname, base, sizeof(dirname));
401                         strlcat(dirname, "/", sizeof(dirname));
402                         strlcat(dirname, dent->d_name, sizeof(dirname));
403                         dir2 = opendir(dirname);
404                         if (dir2 != NULL) {
405                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
406                                         char dirname2[PATH_SIZE];
407
408                                         if (dent2->d_name[0] == '.')
409                                                 continue;
410
411                                         if (!strcmp(dent2->d_name, "device"))
412                                                 continue;
413
414                                         strlcpy(dirname2, dirname, sizeof(dirname2));
415                                         strlcat(dirname2, "/", sizeof(dirname2));
416                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
417                                         if (attr_filtered(dirname2))
418                                                 continue;
419                                         if (is_device(dirname2))
420                                                 device_list_insert(dirname2);
421                                 }
422                                 closedir(dir2);
423                         }
424                 }
425                 closedir(dir);
426         }
427 }
428
429 static void scan_failed(void)
430 {
431         char base[PATH_SIZE];
432         DIR *dir;
433         struct dirent *dent;
434
435         strlcpy(base, udev_root, sizeof(base));
436         strlcat(base, "/", sizeof(base));
437         strlcat(base, EVENT_FAILED_DIR, sizeof(base));
438
439         dir = opendir(base);
440         if (dir != NULL) {
441                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
442                         char linkname[PATH_SIZE];
443                         char target[PATH_SIZE];
444                         int len;
445
446                         if (dent->d_name[0] == '.')
447                                 continue;
448
449                         strlcpy(linkname, base, sizeof(linkname));
450                         strlcat(linkname, "/", sizeof(linkname));
451                         strlcat(linkname, dent->d_name, sizeof(linkname));
452
453                         len = readlink(linkname, target, sizeof(target));
454                         if (len <= 0)
455                                 continue;
456                         target[len] = '\0';
457
458                         if (is_device(target))
459                                 device_list_insert(target);
460                         else
461                                 continue;
462                 }
463                 closedir(dir);
464         }
465 }
466
467 int main(int argc, char *argv[], char *envp[])
468 {
469         int failed = 0;
470         int option;
471         int longindex;
472         struct option options[] = {
473                 { "verbose", 0, NULL, 'v' },
474                 { "dry-run", 0, NULL, 'n' },
475                 { "retry-failed", 0, NULL, 'F' },
476                 { "help", 0, NULL, 'h' },
477                 { "subsystem-match", 1, NULL, 's' },
478                 { "subsystem-nomatch", 1, NULL, 'S' },
479                 { "attr-match", 1, NULL, 'a' },
480                 { "attr-nomatch", 1, NULL, 'A' },
481                 {}
482         };
483
484         logging_init("udevtrigger");
485         udev_config_init();
486         dbg("version %s", UDEV_VERSION);
487         sysfs_init();
488
489         while (1) {
490                 option = getopt_long(argc, argv, "vnFhs:S:a:A:", options, &longindex);
491                 if (option == -1)
492                         break;
493
494                 switch (option) {
495                 case 'v':
496                         verbose = 1;
497                         break;
498                 case 'n':
499                         dry_run = 1;
500                         break;
501                 case 'F':
502                         failed = 1;
503                         break;
504                 case 's':
505                         name_list_add(&filter_subsytem_match_list, optarg, 0);
506                         break;
507                 case 'S':
508                         name_list_add(&filter_subsytem_nomatch_list, optarg, 0);
509                         break;
510                 case 'a':
511                         name_list_add(&filter_attr_match_list, optarg, 0);
512                         break;
513                 case 'A':
514                         name_list_add(&filter_attr_nomatch_list, optarg, 0);
515                         break;
516                 case 'h':
517                         printf("Usage: udevtrigger OPTIONS\n"
518                                "  --verbose                        print the list of devices which will be triggered\n"
519                                "  --dry-run                        do not actually trigger the event\n"
520                                "  --retry-failed                   trigger only the events which are failed during a previous run\n"
521                                "  --subsystem-match                select only devices from the specified subystem\n"
522                                "  --subsystem-nomatch              exclude devices from the specified subystem\n"
523                                "  --attr-match=<file[=<value>]>    select only devices with a matching sysfs attribute\n"
524                                "  --attr-nomatch=<file[=<value>]>  exclude devices with a matching sysfs attribute\n"
525                                "  --help                           print this text\n"
526                                "\n");
527                         goto exit;
528                 default:
529                         goto exit;
530                 }
531         }
532
533         if (failed)
534                 scan_failed();
535         else {
536                 scan_bus();
537                 scan_class();
538                 scan_block();
539         }
540         exec_lists();
541
542 exit:
543         name_list_cleanup(&filter_subsytem_match_list);
544         name_list_cleanup(&filter_subsytem_nomatch_list);
545         name_list_cleanup(&filter_attr_match_list);
546         name_list_cleanup(&filter_attr_nomatch_list);
547
548         sysfs_cleanup();
549         logging_close();
550         return 0;
551 }