2 * Copyright (C) 2004-2006 Kay Sievers <kay@vrfy.org>
3 * Copyright (C) 2006 Hannes Reinecke <hare@suse.de>
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.
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.
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.
32 #include <sys/types.h>
39 LIST_HEAD(device_list);
40 LIST_HEAD(filter_subsystem_match_list);
41 LIST_HEAD(filter_subsystem_nomatch_list);
42 LIST_HEAD(filter_attr_match_list);
43 LIST_HEAD(filter_attr_nomatch_list);
45 /* devices that should run last cause of their dependencies */
46 static int delay_device(const char *devpath)
48 static const char *delay_device_list[] = {
55 for (i = 0; delay_device_list[i] != NULL; i++)
56 if (fnmatch(delay_device_list[i], devpath, 0) == 0)
61 static int device_list_insert(const char *path)
63 char filename[PATH_SIZE];
64 char devpath[PATH_SIZE];
67 dbg("add '%s'" , path);
69 /* we only have a device, if we have an uevent file */
70 strlcpy(filename, path, sizeof(filename));
71 strlcat(filename, "/uevent", sizeof(filename));
72 if (stat(filename, &statbuf) < 0)
74 if (!(statbuf.st_mode & S_IWUSR))
77 strlcpy(devpath, &path[strlen(sysfs_path)], sizeof(devpath));
79 /* resolve possible link to real target */
80 if (lstat(path, &statbuf) < 0)
82 if (S_ISLNK(statbuf.st_mode))
83 if (sysfs_resolve_link(devpath, sizeof(devpath)) != 0)
86 name_list_add(&device_list, devpath, 1);
90 static void trigger_uevent(const char *devpath, const char *action)
92 char filename[PATH_SIZE];
95 strlcpy(filename, sysfs_path, sizeof(filename));
96 strlcat(filename, devpath, sizeof(filename));
97 strlcat(filename, "/uevent", sizeof(filename));
100 printf("%s\n", devpath);
105 fd = open(filename, O_WRONLY);
107 dbg("error on opening %s: %s", filename, strerror(errno));
111 if (write(fd, action, strlen(action)) < 0)
112 info("error writing '%s' to '%s': %s", action, filename, strerror(errno));
117 static void exec_list(const char *action)
119 struct name_entry *loop_device;
120 struct name_entry *tmp_device;
122 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
123 if (delay_device(loop_device->name))
126 trigger_uevent(loop_device->name, action);
127 list_del(&loop_device->node);
131 /* trigger remaining delayed devices */
132 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
133 trigger_uevent(loop_device->name, action);
134 list_del(&loop_device->node);
139 static int subsystem_filtered(const char *subsystem)
141 struct name_entry *loop_name;
143 /* skip devices matching the listed subsystems */
144 list_for_each_entry(loop_name, &filter_subsystem_nomatch_list, node)
145 if (fnmatch(loop_name->name, subsystem, 0) == 0)
148 /* skip devices not matching the listed subsystems */
149 if (!list_empty(&filter_subsystem_match_list)) {
150 list_for_each_entry(loop_name, &filter_subsystem_match_list, node)
151 if (fnmatch(loop_name->name, subsystem, 0) == 0)
159 static int attr_match(const char *path, const char *attr_value)
161 char attr[NAME_SIZE];
162 char file[PATH_SIZE];
165 strlcpy(attr, attr_value, sizeof(attr));
167 /* separate attr and match value */
168 match_value = strchr(attr, '=');
169 if (match_value != NULL) {
170 match_value[0] = '\0';
171 match_value = &match_value[1];
174 strlcpy(file, path, sizeof(file));
175 strlcat(file, "/", sizeof(file));
176 strlcat(file, attr, sizeof(file));
178 if (match_value != NULL) {
179 /* match file content */
180 char value[NAME_SIZE];
184 fd = open(file, O_RDONLY);
187 size = read(fd, value, sizeof(value));
192 remove_trailing_chars(value, '\n');
194 /* match if attribute value matches */
195 if (fnmatch(match_value, value, 0) == 0)
198 /* match if attribute exists */
201 if (stat(file, &statbuf) == 0)
207 static int attr_filtered(const char *path)
209 struct name_entry *loop_name;
211 /* skip devices matching the listed sysfs attributes */
212 list_for_each_entry(loop_name, &filter_attr_nomatch_list, node)
213 if (attr_match(path, loop_name->name))
216 /* skip devices not matching the listed sysfs attributes */
217 if (!list_empty(&filter_attr_match_list)) {
218 list_for_each_entry(loop_name, &filter_attr_match_list, node)
219 if (attr_match(path, loop_name->name))
231 static void scan_subsystem(const char *subsys, enum scan_type scan)
233 char base[PATH_SIZE];
238 if (scan == SCAN_DEVICES)
240 else if (scan == SCAN_SUBSYSTEM)
245 strlcpy(base, sysfs_path, sizeof(base));
246 strlcat(base, "/", sizeof(base));
247 strlcat(base, subsys, sizeof(base));
251 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
252 char dirname[PATH_SIZE];
254 struct dirent *dent2;
256 if (dent->d_name[0] == '.')
259 if (scan == SCAN_DEVICES)
260 if (subsystem_filtered(dent->d_name))
263 strlcpy(dirname, base, sizeof(dirname));
264 strlcat(dirname, "/", sizeof(dirname));
265 strlcat(dirname, dent->d_name, sizeof(dirname));
267 if (scan == SCAN_SUBSYSTEM) {
268 if (!subsystem_filtered("subsystem"))
269 device_list_insert(dirname);
270 if (subsystem_filtered("drivers"))
274 strlcat(dirname, subdir, sizeof(dirname));
276 /* look for devices/drivers */
277 dir2 = opendir(dirname);
279 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
280 char dirname2[PATH_SIZE];
282 if (dent2->d_name[0] == '.')
285 strlcpy(dirname2, dirname, sizeof(dirname2));
286 strlcat(dirname2, "/", sizeof(dirname2));
287 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
288 if (attr_filtered(dirname2))
290 device_list_insert(dirname2);
299 static void scan_block(void)
301 char base[PATH_SIZE];
305 if (subsystem_filtered("block"))
308 strlcpy(base, sysfs_path, sizeof(base));
309 strlcat(base, "/block", sizeof(base));
313 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
314 char dirname[PATH_SIZE];
316 struct dirent *dent2;
318 if (dent->d_name[0] == '.')
321 strlcpy(dirname, base, sizeof(dirname));
322 strlcat(dirname, "/", sizeof(dirname));
323 strlcat(dirname, dent->d_name, sizeof(dirname));
324 if (attr_filtered(dirname))
326 if (device_list_insert(dirname) != 0)
329 /* look for partitions */
330 dir2 = opendir(dirname);
332 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
333 char dirname2[PATH_SIZE];
335 if (dent2->d_name[0] == '.')
338 if (!strcmp(dent2->d_name,"device"))
341 strlcpy(dirname2, dirname, sizeof(dirname2));
342 strlcat(dirname2, "/", sizeof(dirname2));
343 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
344 if (attr_filtered(dirname2))
346 device_list_insert(dirname2);
355 static void scan_class(void)
357 char base[PATH_SIZE];
361 strlcpy(base, sysfs_path, sizeof(base));
362 strlcat(base, "/class", sizeof(base));
366 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
367 char dirname[PATH_SIZE];
369 struct dirent *dent2;
371 if (dent->d_name[0] == '.')
374 if (subsystem_filtered(dent->d_name))
377 strlcpy(dirname, base, sizeof(dirname));
378 strlcat(dirname, "/", sizeof(dirname));
379 strlcat(dirname, dent->d_name, sizeof(dirname));
380 dir2 = opendir(dirname);
382 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
383 char dirname2[PATH_SIZE];
385 if (dent2->d_name[0] == '.')
388 if (!strcmp(dent2->d_name, "device"))
391 strlcpy(dirname2, dirname, sizeof(dirname2));
392 strlcat(dirname2, "/", sizeof(dirname2));
393 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
394 if (attr_filtered(dirname2))
396 device_list_insert(dirname2);
405 static void scan_failed(void)
407 char base[PATH_SIZE];
411 strlcpy(base, udev_root, sizeof(base));
412 strlcat(base, "/" EVENT_FAILED_DIR, sizeof(base));
416 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
417 char device[PATH_SIZE];
420 if (dent->d_name[0] == '.')
423 start = strlcpy(device, sysfs_path, sizeof(device));
424 if(start >= sizeof(device))
425 start = sizeof(device) - 1;
426 strlcat(device, dent->d_name, sizeof(device));
427 path_decode(&device[start]);
428 device_list_insert(device);
434 int udevtrigger(int argc, char *argv[], char *envp[])
438 const char *action = "add";
439 static const struct option options[] = {
440 { "verbose", 0, NULL, 'v' },
441 { "dry-run", 0, NULL, 'n' },
442 { "retry-failed", 0, NULL, 'F' },
443 { "help", 0, NULL, 'h' },
444 { "action", 1, NULL, 'c' },
445 { "subsystem-match", 1, NULL, 's' },
446 { "subsystem-nomatch", 1, NULL, 'S' },
447 { "attr-match", 1, NULL, 'a' },
448 { "attr-nomatch", 1, NULL, 'A' },
452 logging_init("udevtrigger");
454 dbg("version %s", UDEV_VERSION);
458 option = getopt_long(argc, argv, "vnFhc:s:S:a:A:", options, NULL);
476 name_list_add(&filter_subsystem_match_list, optarg, 0);
479 name_list_add(&filter_subsystem_nomatch_list, optarg, 0);
482 name_list_add(&filter_attr_match_list, optarg, 0);
485 name_list_add(&filter_attr_nomatch_list, optarg, 0);
488 printf("Usage: udevadm trigger OPTIONS\n"
489 " --verbose print the list of devices while running\n"
490 " --dry-run do not actually trigger the events\n"
491 " --retry-failed trigger only the events which have been\n"
492 " marked as failed during a previous run\n"
493 " --subsystem-match=<subsystem> trigger devices from a matching subystem\n"
494 " --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
495 " --attr-match=<file[=<value>]> trigger devices with a matching sysfs\n"
497 " --attr-nomatch=<file[=<value>]> exclude devices with a matching sysfs\n"
499 " --help print this text\n"
511 char base[PATH_SIZE];
514 /* if we have /sys/subsystem, forget all the old stuff */
515 strlcpy(base, sysfs_path, sizeof(base));
516 strlcat(base, "/subsystem", sizeof(base));
517 if (stat(base, &statbuf) == 0) {
518 scan_subsystem("subsystem", SCAN_SUBSYSTEM);
520 scan_subsystem("subsystem", SCAN_DEVICES);
523 scan_subsystem("bus", SCAN_SUBSYSTEM);
525 scan_subsystem("bus", SCAN_DEVICES);
528 /* scan "block" if it isn't a "class" */
529 strlcpy(base, sysfs_path, sizeof(base));
530 strlcat(base, "/class/block", sizeof(base));
531 if (stat(base, &statbuf) != 0)
538 name_list_cleanup(&filter_subsystem_match_list);
539 name_list_cleanup(&filter_subsystem_nomatch_list);
540 name_list_cleanup(&filter_attr_match_list);
541 name_list_cleanup(&filter_attr_nomatch_list);