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>
38 void log_message(int priority, const char *format, ...)
42 if (priority > udev_log_priority)
45 va_start(args, format);
46 vsyslog(priority, format, args);
54 /* list of devices that we should run last due to any one of a number of reasons */
55 static char *last_list[] = {
63 /* list of devices that we should run first due to any one of a number of reasons */
64 static char *first_list[] = {
70 LIST_HEAD(device_first_list);
71 LIST_HEAD(device_default_list);
72 LIST_HEAD(device_last_list);
74 LIST_HEAD(filter_subsystem_match_list);
75 LIST_HEAD(filter_subsystem_nomatch_list);
76 LIST_HEAD(filter_attr_match_list);
77 LIST_HEAD(filter_attr_nomatch_list);
79 static int device_list_insert(const char *path)
81 struct list_head *device_list = &device_default_list;
82 const char *devpath = &path[strlen(sysfs_path)];
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;
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;
98 dbg("add '%s'" , path);
99 /* double entries will be ignored */
100 name_list_add(device_list, path, 0);
104 static void trigger_uevent(const char *path)
106 char filename[PATH_SIZE];
109 strlcpy(filename, path, sizeof(filename));
110 strlcat(filename, "/uevent", sizeof(filename));
113 printf("%s\n", path);
118 fd = open(filename, O_WRONLY);
120 dbg("error on opening %s: %s\n", filename, strerror(errno));
124 if (write(fd, "add", 3) < 0)
125 info("error on triggering %s: %s\n", filename, strerror(errno));
130 static void exec_lists(void)
132 struct name_entry *loop_device;
133 struct name_entry *tmp_device;
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);
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);
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);
157 static int is_device(const char *path)
159 char filename[PATH_SIZE];
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)
168 if (!(statbuf.st_mode & S_IWUSR))
174 static int subsystem_filtered(const char *subsystem)
176 struct name_entry *loop_name;
178 /* skip devices matching the listed subsystems */
179 list_for_each_entry(loop_name, &filter_subsystem_nomatch_list, node)
180 if (fnmatch(loop_name->name, subsystem, 0) == 0)
183 /* skip devices not matching the listed subsystems */
184 if (!list_empty(&filter_subsystem_match_list)) {
185 list_for_each_entry(loop_name, &filter_subsystem_match_list, node)
186 if (fnmatch(loop_name->name, subsystem, 0) == 0)
194 static int attr_match(const char *path, const char *attr_value)
196 char attr[NAME_SIZE];
197 char file[PATH_SIZE];
200 strlcpy(attr, attr_value, sizeof(attr));
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];
209 strlcpy(file, path, sizeof(file));
210 strlcat(file, "/", sizeof(file));
211 strlcat(file, attr, sizeof(file));
213 if (match_value != NULL) {
214 /* match file content */
215 char value[NAME_SIZE];
219 fd = open(file, O_RDONLY);
222 size = read(fd, value, sizeof(value));
227 remove_trailing_chars(value, '\n');
229 /* match if attribute value matches */
230 if (fnmatch(match_value, value, 0) == 0)
233 /* match if attribute exists */
236 if (stat(file, &statbuf) == 0)
242 static int attr_filtered(const char *path)
244 struct name_entry *loop_name;
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))
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))
261 static void scan_bus(void)
263 char base[PATH_SIZE];
267 strlcpy(base, sysfs_path, sizeof(base));
268 strlcat(base, "/bus", sizeof(base));
272 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
273 char dirname[PATH_SIZE];
275 struct dirent *dent2;
277 if (dent->d_name[0] == '.')
280 if (subsystem_filtered(dent->d_name))
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));
288 /* look for devices */
289 dir2 = opendir(dirname);
291 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
292 char dirname2[PATH_SIZE];
294 if (dent2->d_name[0] == '.')
297 strlcpy(dirname2, dirname, sizeof(dirname2));
298 strlcat(dirname2, "/", sizeof(dirname2));
299 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
300 if (attr_filtered(dirname2))
302 if (is_device(dirname2))
303 device_list_insert(dirname2);
312 static void scan_block(void)
314 char base[PATH_SIZE];
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)
325 if (subsystem_filtered("block"))
328 strlcpy(base, sysfs_path, sizeof(base));
329 strlcat(base, "/block", sizeof(base));
333 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
334 char dirname[PATH_SIZE];
336 struct dirent *dent2;
338 if (dent->d_name[0] == '.')
341 strlcpy(dirname, base, sizeof(dirname));
342 strlcat(dirname, "/", sizeof(dirname));
343 strlcat(dirname, dent->d_name, sizeof(dirname));
344 if (attr_filtered(dirname))
346 if (is_device(dirname))
347 device_list_insert(dirname);
351 /* look for partitions */
352 dir2 = opendir(dirname);
354 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
355 char dirname2[PATH_SIZE];
357 if (dent2->d_name[0] == '.')
360 if (!strcmp(dent2->d_name,"device"))
363 strlcpy(dirname2, dirname, sizeof(dirname2));
364 strlcat(dirname2, "/", sizeof(dirname2));
365 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
366 if (attr_filtered(dirname2))
368 if (is_device(dirname2))
369 device_list_insert(dirname2);
378 static void scan_class(void)
380 char base[PATH_SIZE];
384 strlcpy(base, sysfs_path, sizeof(base));
385 strlcat(base, "/class", sizeof(base));
389 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
390 char dirname[PATH_SIZE];
392 struct dirent *dent2;
394 if (dent->d_name[0] == '.')
397 if (subsystem_filtered(dent->d_name))
400 strlcpy(dirname, base, sizeof(dirname));
401 strlcat(dirname, "/", sizeof(dirname));
402 strlcat(dirname, dent->d_name, sizeof(dirname));
403 dir2 = opendir(dirname);
405 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
406 char dirname2[PATH_SIZE];
408 if (dent2->d_name[0] == '.')
411 if (!strcmp(dent2->d_name, "device"))
414 strlcpy(dirname2, dirname, sizeof(dirname2));
415 strlcat(dirname2, "/", sizeof(dirname2));
416 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
417 if (attr_filtered(dirname2))
419 if (is_device(dirname2))
420 device_list_insert(dirname2);
429 static void scan_failed(void)
431 char base[PATH_SIZE];
435 strlcpy(base, udev_root, sizeof(base));
436 strlcat(base, "/" EVENT_FAILED_DIR, sizeof(base));
440 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
441 char device[PATH_SIZE];
442 size_t start, end, i;
444 if (dent->d_name[0] == '.')
447 strlcpy(device, sysfs_path, sizeof(device));
448 start = strlcat(device, "/", sizeof(device));
449 end = strlcat(device, dent->d_name, sizeof(device));
450 if (end > sizeof(device))
451 end = sizeof(device);
453 /* replace PATH_TO_NAME_CHAR with '/' */
454 for (i = start; i < end; i++)
455 if (device[i] == PATH_TO_NAME_CHAR)
458 if (is_device(device))
459 device_list_insert(device);
467 int main(int argc, char *argv[], char *envp[])
471 static const struct option options[] = {
472 { "verbose", 0, NULL, 'v' },
473 { "dry-run", 0, NULL, 'n' },
474 { "retry-failed", 0, NULL, 'F' },
475 { "help", 0, NULL, 'h' },
476 { "subsystem-match", 1, NULL, 's' },
477 { "subsystem-nomatch", 1, NULL, 'S' },
478 { "attr-match", 1, NULL, 'a' },
479 { "attr-nomatch", 1, NULL, 'A' },
483 logging_init("udevtrigger");
485 dbg("version %s", UDEV_VERSION);
489 option = getopt_long(argc, argv, "vnFhs:S:a:A:", options, NULL);
504 name_list_add(&filter_subsystem_match_list, optarg, 0);
507 name_list_add(&filter_subsystem_nomatch_list, optarg, 0);
510 name_list_add(&filter_attr_match_list, optarg, 0);
513 name_list_add(&filter_attr_nomatch_list, optarg, 0);
516 printf("Usage: udevtrigger OPTIONS\n"
517 " --verbose print the list of devices while running\n"
518 " --dry-run do not actually trigger the events\n"
519 " --retry-failed trigger only the events which have been\n"
520 " marked as failed during a previous run\n"
521 " --subsystem-match=<subsystem> trigger devices from a matching subystem\n"
522 " --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
523 " --attr-match=<file[=<value>]> trigger devices with a matching sysfs\n"
525 " --attr-nomatch=<file[=<value>]> exclude devices with a matching sysfs\n"
527 " --help print this text\n"
545 name_list_cleanup(&filter_subsystem_match_list);
546 name_list_cleanup(&filter_subsystem_nomatch_list);
547 name_list_cleanup(&filter_attr_match_list);
548 name_list_cleanup(&filter_attr_nomatch_list);