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>
37 #include "udev_rules.h"
41 LIST_HEAD(device_list);
42 LIST_HEAD(filter_subsystem_match_list);
43 LIST_HEAD(filter_subsystem_nomatch_list);
44 LIST_HEAD(filter_attr_match_list);
45 LIST_HEAD(filter_attr_nomatch_list);
47 static struct sockaddr_un saddr;
48 static socklen_t saddrlen;
50 /* devices that should run last cause of their dependencies */
51 static int delay_device(const char *devpath)
53 static const char *delay_device_list[] = {
60 for (i = 0; delay_device_list[i] != NULL; i++)
61 if (fnmatch(delay_device_list[i], devpath, 0) == 0)
66 static int device_list_insert(struct udev *udev, const char *path)
68 char filename[PATH_SIZE];
69 char devpath[PATH_SIZE];
72 dbg(udev, "add '%s'\n" , path);
74 /* we only have a device, if we have an uevent file */
75 strlcpy(filename, path, sizeof(filename));
76 strlcat(filename, "/uevent", sizeof(filename));
77 if (stat(filename, &statbuf) < 0)
79 if (!(statbuf.st_mode & S_IWUSR))
82 strlcpy(devpath, &path[strlen(udev_get_sys_path(udev))], sizeof(devpath));
84 /* resolve possible link to real target */
85 if (lstat(path, &statbuf) < 0)
87 if (S_ISLNK(statbuf.st_mode))
88 if (sysfs_resolve_link(udev, devpath, sizeof(devpath)) != 0)
91 name_list_add(udev, &device_list, devpath, 1);
95 static void trigger_uevent(struct udev *udev, const char *devpath, const char *action)
97 char filename[PATH_SIZE];
100 strlcpy(filename, udev_get_sys_path(udev), sizeof(filename));
101 strlcat(filename, devpath, sizeof(filename));
102 strlcat(filename, "/uevent", sizeof(filename));
105 printf("%s\n", devpath);
110 fd = open(filename, O_WRONLY);
112 dbg(udev, "error on opening %s: %s\n", filename, strerror(errno));
116 if (write(fd, action, strlen(action)) < 0)
117 info(udev, "error writing '%s' to '%s': %s\n", action, filename, strerror(errno));
122 static int pass_to_socket(struct udev *udev, const char *devpath, const char *action, const char *env)
124 struct udevice *udevice;
125 struct name_entry *name_loop;
129 char path[PATH_SIZE];
131 char link_target[PATH_SIZE];
136 printf("%s\n", devpath);
138 udevice = udev_device_init(udev);
141 udev_db_get_device(udevice, devpath);
144 bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
149 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "%s", env);
153 /* add standard keys */
154 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVPATH=%s", devpath);
156 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "ACTION=%s", action);
160 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
161 strlcat(path, devpath, sizeof(path));
162 strlcat(path, "/subsystem", sizeof(path));
163 len = readlink(path, link_target, sizeof(link_target));
167 link_target[len] = '\0';
168 pos = strrchr(link_target, '/');
170 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "SUBSYSTEM=%s", &pos[1]);
175 /* add symlinks and node name */
177 list_for_each_entry(name_loop, &udevice->symlink_list, node) {
178 strlcat(path, udev_get_dev_path(udev), sizeof(path));
179 strlcat(path, "/", sizeof(path));
180 strlcat(path, name_loop->name, sizeof(path));
181 strlcat(path, " ", sizeof(path));
183 remove_trailing_chars(path, ' ');
184 if (path[0] != '\0') {
185 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVLINKS=%s", path);
188 if (udevice->name[0] != '\0') {
189 strlcpy(path, udev_get_dev_path(udev), sizeof(path));
190 strlcat(path, "/", sizeof(path));
191 strlcat(path, udevice->name, sizeof(path));
192 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVNAME=%s", path);
196 /* add keys from device "uevent" file */
197 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
198 strlcat(path, devpath, sizeof(path));
199 strlcat(path, "/uevent", sizeof(path));
200 fd = open(path, O_RDONLY);
204 count = read(fd, value, sizeof(value));
211 while (key[0] != '\0') {
214 next = strchr(key, '\n');
218 bufpos += strlcpy(&buf[bufpos], key, sizeof(buf) - bufpos-1);
225 /* add keys from database */
226 list_for_each_entry(name_loop, &udevice->env_list, node) {
227 bufpos += strlcpy(&buf[bufpos], name_loop->name, sizeof(buf) - bufpos-1);
230 if (bufpos > sizeof(buf))
231 bufpos = sizeof(buf);
233 count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, saddrlen);
240 static void exec_list(struct udev *udev, const char *action, const char *env)
242 struct name_entry *loop_device;
243 struct name_entry *tmp_device;
245 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
246 if (delay_device(loop_device->name))
249 pass_to_socket(udev, loop_device->name, action, env);
251 trigger_uevent(udev, loop_device->name, action);
252 list_del(&loop_device->node);
256 /* trigger remaining delayed devices */
257 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
259 pass_to_socket(udev, loop_device->name, action, env);
261 trigger_uevent(udev, loop_device->name, action);
262 list_del(&loop_device->node);
267 static int subsystem_filtered(const char *subsystem)
269 struct name_entry *loop_name;
271 /* skip devices matching the listed subsystems */
272 list_for_each_entry(loop_name, &filter_subsystem_nomatch_list, node)
273 if (fnmatch(loop_name->name, subsystem, 0) == 0)
276 /* skip devices not matching the listed subsystems */
277 if (!list_empty(&filter_subsystem_match_list)) {
278 list_for_each_entry(loop_name, &filter_subsystem_match_list, node)
279 if (fnmatch(loop_name->name, subsystem, 0) == 0)
287 static int attr_match(const char *path, const char *attr_value)
289 char attr[NAME_SIZE];
290 char file[PATH_SIZE];
293 strlcpy(attr, attr_value, sizeof(attr));
295 /* separate attr and match value */
296 match_value = strchr(attr, '=');
297 if (match_value != NULL) {
298 match_value[0] = '\0';
299 match_value = &match_value[1];
302 strlcpy(file, path, sizeof(file));
303 strlcat(file, "/", sizeof(file));
304 strlcat(file, attr, sizeof(file));
306 if (match_value != NULL) {
307 /* match file content */
308 char value[NAME_SIZE];
312 fd = open(file, O_RDONLY);
315 size = read(fd, value, sizeof(value));
320 remove_trailing_chars(value, '\n');
322 /* match if attribute value matches */
323 if (fnmatch(match_value, value, 0) == 0)
326 /* match if attribute exists */
329 if (stat(file, &statbuf) == 0)
335 static int attr_filtered(const char *path)
337 struct name_entry *loop_name;
339 /* skip devices matching the listed sysfs attributes */
340 list_for_each_entry(loop_name, &filter_attr_nomatch_list, node)
341 if (attr_match(path, loop_name->name))
344 /* skip devices not matching the listed sysfs attributes */
345 if (!list_empty(&filter_attr_match_list)) {
346 list_for_each_entry(loop_name, &filter_attr_match_list, node)
347 if (attr_match(path, loop_name->name))
359 static void scan_subsystem(struct udev *udev, const char *subsys, enum scan_type scan)
361 char base[PATH_SIZE];
366 if (scan == SCAN_DEVICES)
368 else if (scan == SCAN_SUBSYSTEM)
373 strlcpy(base, udev_get_sys_path(udev), sizeof(base));
374 strlcat(base, "/", sizeof(base));
375 strlcat(base, subsys, sizeof(base));
379 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
380 char dirname[PATH_SIZE];
382 struct dirent *dent2;
384 if (dent->d_name[0] == '.')
387 if (scan == SCAN_DEVICES)
388 if (subsystem_filtered(dent->d_name))
391 strlcpy(dirname, base, sizeof(dirname));
392 strlcat(dirname, "/", sizeof(dirname));
393 strlcat(dirname, dent->d_name, sizeof(dirname));
395 if (scan == SCAN_SUBSYSTEM) {
396 if (attr_filtered(dirname))
398 if (!subsystem_filtered("subsystem"))
399 device_list_insert(udev, dirname);
400 if (subsystem_filtered("drivers"))
404 strlcat(dirname, subdir, sizeof(dirname));
406 /* look for devices/drivers */
407 dir2 = opendir(dirname);
409 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
410 char dirname2[PATH_SIZE];
412 if (dent2->d_name[0] == '.')
415 strlcpy(dirname2, dirname, sizeof(dirname2));
416 strlcat(dirname2, "/", sizeof(dirname2));
417 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
418 if (attr_filtered(dirname2))
420 device_list_insert(udev, dirname2);
429 static void scan_block(struct udev *udev)
431 char base[PATH_SIZE];
435 if (subsystem_filtered("block"))
438 strlcpy(base, udev_get_sys_path(udev), sizeof(base));
439 strlcat(base, "/block", sizeof(base));
443 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
444 char dirname[PATH_SIZE];
446 struct dirent *dent2;
448 if (dent->d_name[0] == '.')
451 strlcpy(dirname, base, sizeof(dirname));
452 strlcat(dirname, "/", sizeof(dirname));
453 strlcat(dirname, dent->d_name, sizeof(dirname));
454 if (attr_filtered(dirname))
456 if (device_list_insert(udev, dirname) != 0)
459 /* look for partitions */
460 dir2 = opendir(dirname);
462 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
463 char dirname2[PATH_SIZE];
465 if (dent2->d_name[0] == '.')
468 if (!strcmp(dent2->d_name,"device"))
471 strlcpy(dirname2, dirname, sizeof(dirname2));
472 strlcat(dirname2, "/", sizeof(dirname2));
473 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
474 if (attr_filtered(dirname2))
476 device_list_insert(udev, dirname2);
485 static void scan_class(struct udev *udev)
487 char base[PATH_SIZE];
491 strlcpy(base, udev_get_sys_path(udev), sizeof(base));
492 strlcat(base, "/class", sizeof(base));
496 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
497 char dirname[PATH_SIZE];
499 struct dirent *dent2;
501 if (dent->d_name[0] == '.')
504 if (subsystem_filtered(dent->d_name))
507 strlcpy(dirname, base, sizeof(dirname));
508 strlcat(dirname, "/", sizeof(dirname));
509 strlcat(dirname, dent->d_name, sizeof(dirname));
510 dir2 = opendir(dirname);
512 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
513 char dirname2[PATH_SIZE];
515 if (dent2->d_name[0] == '.')
518 if (!strcmp(dent2->d_name, "device"))
521 strlcpy(dirname2, dirname, sizeof(dirname2));
522 strlcat(dirname2, "/", sizeof(dirname2));
523 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
524 if (attr_filtered(dirname2))
526 device_list_insert(udev, dirname2);
535 static void scan_failed(struct udev *udev)
537 char base[PATH_SIZE];
541 strlcpy(base, udev_get_dev_path(udev), sizeof(base));
542 strlcat(base, "/.udev/failed", sizeof(base));
546 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
547 char device[PATH_SIZE];
550 if (dent->d_name[0] == '.')
553 start = strlcpy(device, udev_get_sys_path(udev), sizeof(device));
554 if(start >= sizeof(device))
555 start = sizeof(device) - 1;
556 strlcat(device, dent->d_name, sizeof(device));
557 path_decode(&device[start]);
558 device_list_insert(udev, device);
564 int udevadm_trigger(struct udev *udev, int argc, char *argv[])
567 const char *sockpath = NULL;
569 const char *action = "add";
570 const char *env = NULL;
571 static const struct option options[] = {
572 { "verbose", 0, NULL, 'v' },
573 { "dry-run", 0, NULL, 'n' },
574 { "retry-failed", 0, NULL, 'F' },
575 { "socket", 1, NULL, 'o' },
576 { "help", 0, NULL, 'h' },
577 { "action", 1, NULL, 'c' },
578 { "subsystem-match", 1, NULL, 's' },
579 { "subsystem-nomatch", 1, NULL, 'S' },
580 { "attr-match", 1, NULL, 'a' },
581 { "attr-nomatch", 1, NULL, 'A' },
582 { "env", 1, NULL, 'e' },
586 dbg(udev, "version %s\n", VERSION);
589 option = getopt_long(argc, argv, "vnFo:hce::s:S:a:A:", options, NULL);
610 if (strchr(optarg, '=') != NULL)
614 name_list_add(udev, &filter_subsystem_match_list, optarg, 0);
617 name_list_add(udev, &filter_subsystem_nomatch_list, optarg, 0);
620 name_list_add(udev, &filter_attr_match_list, optarg, 0);
623 name_list_add(udev, &filter_attr_nomatch_list, optarg, 0);
626 printf("Usage: udevadm trigger OPTIONS\n"
627 " --verbose print the list of devices while running\n"
628 " --dry-run do not actually trigger the events\n"
629 " --retry-failed trigger only the events which have been\n"
630 " marked as failed during a previous run\n"
631 " --socket=<socket path> pass events to socket instead of triggering kernel events\n"
632 " --env=<KEY>=<value> pass an additional key (works only with --socket=)\n"
633 " --subsystem-match=<subsystem> trigger devices from a matching subystem\n"
634 " --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
635 " --attr-match=<file[=<value>]> trigger devices with a matching sysfs\n"
637 " --attr-nomatch=<file[=<value>]> exclude devices with a matching sysfs\n"
639 " --help print this text\n"
647 if (sockpath != NULL) {
650 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
651 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
652 saddr.sun_family = AF_LOCAL;
653 if (sockpath[0] == '@') {
654 /* abstract namespace socket requested */
655 strlcpy(&saddr.sun_path[1], &sockpath[1], sizeof(saddr.sun_path)-1);
656 saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
657 } else if (stat(sockpath, &stats) == 0 && S_ISSOCK(stats.st_mode)) {
658 /* existing socket file */
659 strlcpy(saddr.sun_path, sockpath, sizeof(saddr.sun_path));
660 saddrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path);
662 /* no socket file, assume abstract namespace socket */
663 strlcpy(&saddr.sun_path[1], sockpath, sizeof(saddr.sun_path)-1);
664 saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
666 } else if (env != NULL) {
667 fprintf(stderr, "error: --env= only valid with --socket= option\n");
673 exec_list(udev, action, env);
675 char base[PATH_SIZE];
678 /* if we have /sys/subsystem, forget all the old stuff */
679 strlcpy(base, udev_get_sys_path(udev), sizeof(base));
680 strlcat(base, "/subsystem", sizeof(base));
681 if (stat(base, &statbuf) == 0) {
682 scan_subsystem(udev, "subsystem", SCAN_SUBSYSTEM);
683 exec_list(udev, action, env);
684 scan_subsystem(udev, "subsystem", SCAN_DEVICES);
685 exec_list(udev, action, env);
687 scan_subsystem(udev, "bus", SCAN_SUBSYSTEM);
688 exec_list(udev, action, env);
689 scan_subsystem(udev, "bus", SCAN_DEVICES);
692 /* scan "block" if it isn't a "class" */
693 strlcpy(base, udev_get_sys_path(udev), sizeof(base));
694 strlcat(base, "/class/block", sizeof(base));
695 if (stat(base, &statbuf) != 0)
697 exec_list(udev, action, env);
702 name_list_cleanup(udev, &filter_subsystem_match_list);
703 name_list_cleanup(udev, &filter_subsystem_nomatch_list);
704 name_list_cleanup(udev, &filter_attr_match_list);
705 name_list_cleanup(udev, &filter_attr_nomatch_list);