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);
46 void log_message(int priority, const char *format, ...)
50 if (priority > udev_log_priority)
53 va_start(args, format);
54 vsyslog(priority, format, args);
59 /* devices that should run last cause of their dependencies */
60 static int delay_device(const char *devpath)
62 static const char *delay_device_list[] = {
69 for (i = 0; delay_device_list[i] != NULL; i++)
70 if (fnmatch(delay_device_list[i], devpath, 0) == 0)
75 static int device_list_insert(const char *path)
77 char filename[PATH_SIZE];
78 char devpath[PATH_SIZE];
81 dbg("add '%s'" , path);
83 /* we only have a device, if we have an uevent file */
84 strlcpy(filename, path, sizeof(filename));
85 strlcat(filename, "/uevent", sizeof(filename));
86 if (stat(filename, &statbuf) < 0)
88 if (!(statbuf.st_mode & S_IWUSR))
91 strlcpy(devpath, &path[strlen(sysfs_path)], sizeof(devpath));
93 /* resolve possible link to real target */
94 if (lstat(path, &statbuf) < 0)
96 if (S_ISLNK(statbuf.st_mode))
97 if (sysfs_resolve_link(devpath, sizeof(devpath)) != 0)
100 name_list_add(&device_list, devpath, 1);
104 static void trigger_uevent(const char *devpath)
106 char filename[PATH_SIZE];
109 strlcpy(filename, sysfs_path, sizeof(filename));
110 strlcat(filename, devpath, sizeof(filename));
111 strlcat(filename, "/uevent", sizeof(filename));
114 printf("%s\n", devpath);
119 fd = open(filename, O_WRONLY);
121 dbg("error on opening %s: %s\n", filename, strerror(errno));
125 if (write(fd, "add", 3) < 0)
126 info("error on triggering %s: %s\n", filename, strerror(errno));
131 static void exec_list(void)
133 struct name_entry *loop_device;
134 struct name_entry *tmp_device;
136 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
137 if (delay_device(loop_device->name))
140 trigger_uevent(loop_device->name);
141 list_del(&loop_device->node);
145 /* trigger remaining delayed devices */
146 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
147 trigger_uevent(loop_device->name);
148 list_del(&loop_device->node);
153 static int subsystem_filtered(const char *subsystem)
155 struct name_entry *loop_name;
157 /* skip devices matching the listed subsystems */
158 list_for_each_entry(loop_name, &filter_subsystem_nomatch_list, node)
159 if (fnmatch(loop_name->name, subsystem, 0) == 0)
162 /* skip devices not matching the listed subsystems */
163 if (!list_empty(&filter_subsystem_match_list)) {
164 list_for_each_entry(loop_name, &filter_subsystem_match_list, node)
165 if (fnmatch(loop_name->name, subsystem, 0) == 0)
173 static int attr_match(const char *path, const char *attr_value)
175 char attr[NAME_SIZE];
176 char file[PATH_SIZE];
179 strlcpy(attr, attr_value, sizeof(attr));
181 /* separate attr and match value */
182 match_value = strchr(attr, '=');
183 if (match_value != NULL) {
184 match_value[0] = '\0';
185 match_value = &match_value[1];
188 strlcpy(file, path, sizeof(file));
189 strlcat(file, "/", sizeof(file));
190 strlcat(file, attr, sizeof(file));
192 if (match_value != NULL) {
193 /* match file content */
194 char value[NAME_SIZE];
198 fd = open(file, O_RDONLY);
201 size = read(fd, value, sizeof(value));
206 remove_trailing_chars(value, '\n');
208 /* match if attribute value matches */
209 if (fnmatch(match_value, value, 0) == 0)
212 /* match if attribute exists */
215 if (stat(file, &statbuf) == 0)
221 static int attr_filtered(const char *path)
223 struct name_entry *loop_name;
225 /* skip devices matching the listed sysfs attributes */
226 list_for_each_entry(loop_name, &filter_attr_nomatch_list, node)
227 if (attr_match(path, loop_name->name))
230 /* skip devices not matching the listed sysfs attributes */
231 if (!list_empty(&filter_attr_match_list)) {
232 list_for_each_entry(loop_name, &filter_attr_match_list, node)
233 if (attr_match(path, loop_name->name))
240 static void scan_bus(void)
242 char base[PATH_SIZE];
246 strlcpy(base, sysfs_path, sizeof(base));
247 strlcat(base, "/bus", 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 (subsystem_filtered(dent->d_name))
262 strlcpy(dirname, base, sizeof(dirname));
263 strlcat(dirname, "/", sizeof(dirname));
264 strlcat(dirname, dent->d_name, sizeof(dirname));
265 strlcat(dirname, "/devices", sizeof(dirname));
267 /* look for devices */
268 dir2 = opendir(dirname);
270 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
271 char dirname2[PATH_SIZE];
273 if (dent2->d_name[0] == '.')
276 strlcpy(dirname2, dirname, sizeof(dirname2));
277 strlcat(dirname2, "/", sizeof(dirname2));
278 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
279 if (attr_filtered(dirname2))
281 device_list_insert(dirname2);
290 static void scan_block(void)
292 char base[PATH_SIZE];
297 /* skip if "block" is already a "class" */
298 strlcpy(base, sysfs_path, sizeof(base));
299 strlcat(base, "/class/block", sizeof(base));
300 if (stat(base, &statbuf) == 0)
303 if (subsystem_filtered("block"))
306 strlcpy(base, sysfs_path, sizeof(base));
307 strlcat(base, "/block", sizeof(base));
311 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
312 char dirname[PATH_SIZE];
314 struct dirent *dent2;
316 if (dent->d_name[0] == '.')
319 strlcpy(dirname, base, sizeof(dirname));
320 strlcat(dirname, "/", sizeof(dirname));
321 strlcat(dirname, dent->d_name, sizeof(dirname));
322 if (attr_filtered(dirname))
324 if (device_list_insert(dirname) != 0)
327 /* look for partitions */
328 dir2 = opendir(dirname);
330 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
331 char dirname2[PATH_SIZE];
333 if (dent2->d_name[0] == '.')
336 if (!strcmp(dent2->d_name,"device"))
339 strlcpy(dirname2, dirname, sizeof(dirname2));
340 strlcat(dirname2, "/", sizeof(dirname2));
341 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
342 if (attr_filtered(dirname2))
344 device_list_insert(dirname2);
353 static void scan_class(void)
355 char base[PATH_SIZE];
359 strlcpy(base, sysfs_path, sizeof(base));
360 strlcat(base, "/class", sizeof(base));
364 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
365 char dirname[PATH_SIZE];
367 struct dirent *dent2;
369 if (dent->d_name[0] == '.')
372 if (subsystem_filtered(dent->d_name))
375 strlcpy(dirname, base, sizeof(dirname));
376 strlcat(dirname, "/", sizeof(dirname));
377 strlcat(dirname, dent->d_name, sizeof(dirname));
378 dir2 = opendir(dirname);
380 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
381 char dirname2[PATH_SIZE];
383 if (dent2->d_name[0] == '.')
386 if (!strcmp(dent2->d_name, "device"))
389 strlcpy(dirname2, dirname, sizeof(dirname2));
390 strlcat(dirname2, "/", sizeof(dirname2));
391 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
392 if (attr_filtered(dirname2))
394 device_list_insert(dirname2);
403 static void scan_failed(void)
405 char base[PATH_SIZE];
409 strlcpy(base, udev_root, sizeof(base));
410 strlcat(base, "/" EVENT_FAILED_DIR, sizeof(base));
414 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
415 char device[PATH_SIZE];
416 size_t start, end, i;
418 if (dent->d_name[0] == '.')
421 strlcpy(device, sysfs_path, sizeof(device));
422 start = strlcat(device, "/", sizeof(device));
423 end = strlcat(device, dent->d_name, sizeof(device));
424 if (end > sizeof(device))
425 end = sizeof(device);
427 /* replace PATH_TO_NAME_CHAR with '/' */
428 for (i = start; i < end; i++)
429 if (device[i] == PATH_TO_NAME_CHAR)
432 device_list_insert(device);
438 int main(int argc, char *argv[], char *envp[])
442 static const struct option options[] = {
443 { "verbose", 0, NULL, 'v' },
444 { "dry-run", 0, NULL, 'n' },
445 { "retry-failed", 0, NULL, 'F' },
446 { "help", 0, NULL, 'h' },
447 { "subsystem-match", 1, NULL, 's' },
448 { "subsystem-nomatch", 1, NULL, 'S' },
449 { "attr-match", 1, NULL, 'a' },
450 { "attr-nomatch", 1, NULL, 'A' },
454 logging_init("udevtrigger");
456 dbg("version %s", UDEV_VERSION);
460 option = getopt_long(argc, argv, "vnFhs:S:a:A:", options, NULL);
475 name_list_add(&filter_subsystem_match_list, optarg, 0);
478 name_list_add(&filter_subsystem_nomatch_list, optarg, 0);
481 name_list_add(&filter_attr_match_list, optarg, 0);
484 name_list_add(&filter_attr_nomatch_list, optarg, 0);
487 printf("Usage: udevtrigger OPTIONS\n"
488 " --verbose print the list of devices while running\n"
489 " --dry-run do not actually trigger the events\n"
490 " --retry-failed trigger only the events which have been\n"
491 " marked as failed during a previous run\n"
492 " --subsystem-match=<subsystem> trigger devices from a matching subystem\n"
493 " --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
494 " --attr-match=<file[=<value>]> trigger devices with a matching sysfs\n"
496 " --attr-nomatch=<file[=<value>]> exclude devices with a matching sysfs\n"
498 " --help print this text\n"
516 name_list_cleanup(&filter_subsystem_match_list);
517 name_list_cleanup(&filter_subsystem_nomatch_list);
518 name_list_cleanup(&filter_attr_match_list);
519 name_list_cleanup(&filter_attr_nomatch_list);