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>
33 #include <sys/socket.h>
38 #include "udev_rules.h"
42 LIST_HEAD(device_list);
43 LIST_HEAD(filter_subsystem_match_list);
44 LIST_HEAD(filter_subsystem_nomatch_list);
45 LIST_HEAD(filter_attr_match_list);
46 LIST_HEAD(filter_attr_nomatch_list);
48 static struct sockaddr_un saddr;
49 static socklen_t saddrlen;
51 /* devices that should run last cause of their dependencies */
52 static int delay_device(const char *devpath)
54 static const char *delay_device_list[] = {
61 for (i = 0; delay_device_list[i] != NULL; i++)
62 if (fnmatch(delay_device_list[i], devpath, 0) == 0)
67 static int device_list_insert(const char *path)
69 char filename[PATH_SIZE];
70 char devpath[PATH_SIZE];
73 dbg("add '%s'" , path);
75 /* we only have a device, if we have an uevent file */
76 strlcpy(filename, path, sizeof(filename));
77 strlcat(filename, "/uevent", sizeof(filename));
78 if (stat(filename, &statbuf) < 0)
80 if (!(statbuf.st_mode & S_IWUSR))
83 strlcpy(devpath, &path[strlen(sysfs_path)], sizeof(devpath));
85 /* resolve possible link to real target */
86 if (lstat(path, &statbuf) < 0)
88 if (S_ISLNK(statbuf.st_mode))
89 if (sysfs_resolve_link(devpath, sizeof(devpath)) != 0)
92 name_list_add(&device_list, devpath, 1);
96 static void trigger_uevent(const char *devpath, const char *action)
98 char filename[PATH_SIZE];
101 strlcpy(filename, sysfs_path, sizeof(filename));
102 strlcat(filename, devpath, sizeof(filename));
103 strlcat(filename, "/uevent", sizeof(filename));
106 printf("%s\n", devpath);
111 fd = open(filename, O_WRONLY);
113 dbg("error on opening %s: %s", filename, strerror(errno));
117 if (write(fd, action, strlen(action)) < 0)
118 info("error writing '%s' to '%s': %s", action, filename, strerror(errno));
123 static int pass_to_socket(const char *devpath, const char *action)
126 struct name_entry *name_loop;
130 char path[PATH_SIZE];
132 char link_target[PATH_SIZE];
136 udev_device_init(&udev);
137 udev_db_get_device(&udev, devpath);
140 bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
143 /* add standard keys */
144 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVPATH=%s", devpath);
146 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "ACTION=%s", action);
150 strlcpy(path, sysfs_path, sizeof(path));
151 strlcat(path, devpath, sizeof(path));
152 strlcat(path, "/subsystem", sizeof(path));
153 len = readlink(path, link_target, sizeof(link_target));
157 link_target[len] = '\0';
158 pos = strrchr(link_target, '/');
160 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "SUBSYSTEM=%s", &pos[1]);
165 /* add symlinks and node name */
167 list_for_each_entry(name_loop, &udev.symlink_list, node) {
168 strlcat(path, udev_root, sizeof(path));
169 strlcat(path, "/", sizeof(path));
170 strlcat(path, name_loop->name, sizeof(path));
171 strlcat(path, " ", sizeof(path));
173 remove_trailing_chars(path, ' ');
174 if (path[0] != '\0') {
175 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVLINKS=%s", path);
178 if (udev.name[0] != '\0') {
179 strlcpy(path, udev_root, sizeof(path));
180 strlcat(path, "/", sizeof(path));
181 strlcat(path, udev.name, sizeof(path));
182 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVNAME=%s", path);
186 /* add keys from device "uevent" file */
187 strlcpy(path, sysfs_path, sizeof(path));
188 strlcat(path, devpath, sizeof(path));
189 strlcat(path, "/uevent", sizeof(path));
190 fd = open(path, O_RDONLY);
194 count = read(fd, value, sizeof(value));
201 while (key[0] != '\0') {
204 next = strchr(key, '\n');
208 bufpos += strlcpy(&buf[bufpos], key, sizeof(buf) - bufpos-1);
215 /* add keys from database */
216 list_for_each_entry(name_loop, &udev.env_list, node) {
217 bufpos += strlcpy(&buf[bufpos], name_loop->name, sizeof(buf) - bufpos-1);
220 if (bufpos > sizeof(buf))
221 bufpos = sizeof(buf);
223 count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, saddrlen);
230 static void exec_list(const char *action)
232 struct name_entry *loop_device;
233 struct name_entry *tmp_device;
235 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
236 if (delay_device(loop_device->name))
239 pass_to_socket(loop_device->name, action);
241 trigger_uevent(loop_device->name, action);
242 list_del(&loop_device->node);
246 /* trigger remaining delayed devices */
247 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
249 pass_to_socket(loop_device->name, action);
251 trigger_uevent(loop_device->name, action);
252 list_del(&loop_device->node);
257 static int subsystem_filtered(const char *subsystem)
259 struct name_entry *loop_name;
261 /* skip devices matching the listed subsystems */
262 list_for_each_entry(loop_name, &filter_subsystem_nomatch_list, node)
263 if (fnmatch(loop_name->name, subsystem, 0) == 0)
266 /* skip devices not matching the listed subsystems */
267 if (!list_empty(&filter_subsystem_match_list)) {
268 list_for_each_entry(loop_name, &filter_subsystem_match_list, node)
269 if (fnmatch(loop_name->name, subsystem, 0) == 0)
277 static int attr_match(const char *path, const char *attr_value)
279 char attr[NAME_SIZE];
280 char file[PATH_SIZE];
283 strlcpy(attr, attr_value, sizeof(attr));
285 /* separate attr and match value */
286 match_value = strchr(attr, '=');
287 if (match_value != NULL) {
288 match_value[0] = '\0';
289 match_value = &match_value[1];
292 strlcpy(file, path, sizeof(file));
293 strlcat(file, "/", sizeof(file));
294 strlcat(file, attr, sizeof(file));
296 if (match_value != NULL) {
297 /* match file content */
298 char value[NAME_SIZE];
302 fd = open(file, O_RDONLY);
305 size = read(fd, value, sizeof(value));
310 remove_trailing_chars(value, '\n');
312 /* match if attribute value matches */
313 if (fnmatch(match_value, value, 0) == 0)
316 /* match if attribute exists */
319 if (stat(file, &statbuf) == 0)
325 static int attr_filtered(const char *path)
327 struct name_entry *loop_name;
329 /* skip devices matching the listed sysfs attributes */
330 list_for_each_entry(loop_name, &filter_attr_nomatch_list, node)
331 if (attr_match(path, loop_name->name))
334 /* skip devices not matching the listed sysfs attributes */
335 if (!list_empty(&filter_attr_match_list)) {
336 list_for_each_entry(loop_name, &filter_attr_match_list, node)
337 if (attr_match(path, loop_name->name))
349 static void scan_subsystem(const char *subsys, enum scan_type scan)
351 char base[PATH_SIZE];
356 if (scan == SCAN_DEVICES)
358 else if (scan == SCAN_SUBSYSTEM)
363 strlcpy(base, sysfs_path, sizeof(base));
364 strlcat(base, "/", sizeof(base));
365 strlcat(base, subsys, sizeof(base));
369 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
370 char dirname[PATH_SIZE];
372 struct dirent *dent2;
374 if (dent->d_name[0] == '.')
377 if (scan == SCAN_DEVICES)
378 if (subsystem_filtered(dent->d_name))
381 strlcpy(dirname, base, sizeof(dirname));
382 strlcat(dirname, "/", sizeof(dirname));
383 strlcat(dirname, dent->d_name, sizeof(dirname));
385 if (scan == SCAN_SUBSYSTEM) {
386 if (!subsystem_filtered("subsystem"))
387 device_list_insert(dirname);
388 if (subsystem_filtered("drivers"))
392 strlcat(dirname, subdir, sizeof(dirname));
394 /* look for devices/drivers */
395 dir2 = opendir(dirname);
397 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
398 char dirname2[PATH_SIZE];
400 if (dent2->d_name[0] == '.')
403 strlcpy(dirname2, dirname, sizeof(dirname2));
404 strlcat(dirname2, "/", sizeof(dirname2));
405 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
406 if (attr_filtered(dirname2))
408 device_list_insert(dirname2);
417 static void scan_block(void)
419 char base[PATH_SIZE];
423 if (subsystem_filtered("block"))
426 strlcpy(base, sysfs_path, sizeof(base));
427 strlcat(base, "/block", sizeof(base));
431 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
432 char dirname[PATH_SIZE];
434 struct dirent *dent2;
436 if (dent->d_name[0] == '.')
439 strlcpy(dirname, base, sizeof(dirname));
440 strlcat(dirname, "/", sizeof(dirname));
441 strlcat(dirname, dent->d_name, sizeof(dirname));
442 if (attr_filtered(dirname))
444 if (device_list_insert(dirname) != 0)
447 /* look for partitions */
448 dir2 = opendir(dirname);
450 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
451 char dirname2[PATH_SIZE];
453 if (dent2->d_name[0] == '.')
456 if (!strcmp(dent2->d_name,"device"))
459 strlcpy(dirname2, dirname, sizeof(dirname2));
460 strlcat(dirname2, "/", sizeof(dirname2));
461 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
462 if (attr_filtered(dirname2))
464 device_list_insert(dirname2);
473 static void scan_class(void)
475 char base[PATH_SIZE];
479 strlcpy(base, sysfs_path, sizeof(base));
480 strlcat(base, "/class", sizeof(base));
484 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
485 char dirname[PATH_SIZE];
487 struct dirent *dent2;
489 if (dent->d_name[0] == '.')
492 if (subsystem_filtered(dent->d_name))
495 strlcpy(dirname, base, sizeof(dirname));
496 strlcat(dirname, "/", sizeof(dirname));
497 strlcat(dirname, dent->d_name, sizeof(dirname));
498 dir2 = opendir(dirname);
500 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
501 char dirname2[PATH_SIZE];
503 if (dent2->d_name[0] == '.')
506 if (!strcmp(dent2->d_name, "device"))
509 strlcpy(dirname2, dirname, sizeof(dirname2));
510 strlcat(dirname2, "/", sizeof(dirname2));
511 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
512 if (attr_filtered(dirname2))
514 device_list_insert(dirname2);
523 static void scan_failed(void)
525 char base[PATH_SIZE];
529 strlcpy(base, udev_root, sizeof(base));
530 strlcat(base, "/" EVENT_FAILED_DIR, sizeof(base));
534 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
535 char device[PATH_SIZE];
538 if (dent->d_name[0] == '.')
541 start = strlcpy(device, sysfs_path, sizeof(device));
542 if(start >= sizeof(device))
543 start = sizeof(device) - 1;
544 strlcat(device, dent->d_name, sizeof(device));
545 path_decode(&device[start]);
546 device_list_insert(device);
552 int udevtrigger(int argc, char *argv[], char *envp[])
555 const char *sockpath = NULL;
557 const char *action = "add";
558 static const struct option options[] = {
559 { "verbose", 0, NULL, 'v' },
560 { "dry-run", 0, NULL, 'n' },
561 { "retry-failed", 0, NULL, 'F' },
562 { "socket", 1, NULL, 'o' },
563 { "help", 0, NULL, 'h' },
564 { "action", 1, NULL, 'c' },
565 { "subsystem-match", 1, NULL, 's' },
566 { "subsystem-nomatch", 1, NULL, 'S' },
567 { "attr-match", 1, NULL, 'a' },
568 { "attr-nomatch", 1, NULL, 'A' },
572 logging_init("udevtrigger");
574 dbg("version %s", UDEV_VERSION);
578 option = getopt_long(argc, argv, "vnFo:hc:s:S:a:A:", options, NULL);
599 name_list_add(&filter_subsystem_match_list, optarg, 0);
602 name_list_add(&filter_subsystem_nomatch_list, optarg, 0);
605 name_list_add(&filter_attr_match_list, optarg, 0);
608 name_list_add(&filter_attr_nomatch_list, optarg, 0);
611 printf("Usage: udevadm trigger OPTIONS\n"
612 " --verbose print the list of devices while running\n"
613 " --dry-run do not actually trigger the events\n"
614 " --retry-failed trigger only the events which have been\n"
615 " marked as failed during a previous run\n"
616 " --subsystem-match=<subsystem> trigger devices from a matching subystem\n"
617 " --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
618 " --attr-match=<file[=<value>]> trigger devices with a matching sysfs\n"
620 " --attr-nomatch=<file[=<value>]> exclude devices with a matching sysfs\n"
622 " --help print this text\n"
630 if (sockpath != NULL) {
631 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
632 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
633 saddr.sun_family = AF_LOCAL;
634 /* abstract namespace only */
635 strlcpy(&saddr.sun_path[1], sockpath, sizeof(saddr.sun_path)-1);
636 saddrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
643 char base[PATH_SIZE];
646 /* if we have /sys/subsystem, forget all the old stuff */
647 strlcpy(base, sysfs_path, sizeof(base));
648 strlcat(base, "/subsystem", sizeof(base));
649 if (stat(base, &statbuf) == 0) {
650 scan_subsystem("subsystem", SCAN_SUBSYSTEM);
652 scan_subsystem("subsystem", SCAN_DEVICES);
655 scan_subsystem("bus", SCAN_SUBSYSTEM);
657 scan_subsystem("bus", SCAN_DEVICES);
660 /* scan "block" if it isn't a "class" */
661 strlcpy(base, sysfs_path, sizeof(base));
662 strlcat(base, "/class/block", sizeof(base));
663 if (stat(base, &statbuf) != 0)
670 name_list_cleanup(&filter_subsystem_match_list);
671 name_list_cleanup(&filter_subsystem_nomatch_list);
672 name_list_cleanup(&filter_attr_match_list);
673 name_list_cleanup(&filter_attr_nomatch_list);