2 * Copyright (C) 2004-2008 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
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include <sys/types.h>
32 #include <sys/socket.h>
36 #include "udev_rules.h"
40 LIST_HEAD(device_list);
41 LIST_HEAD(filter_subsystem_match_list);
42 LIST_HEAD(filter_subsystem_nomatch_list);
43 LIST_HEAD(filter_attr_match_list);
44 LIST_HEAD(filter_attr_nomatch_list);
46 static struct sockaddr_un saddr;
47 static socklen_t saddrlen;
49 /* devices that should run last cause of their dependencies */
50 static int delay_device(const char *syspath)
52 static const char *delay_device_list[] = {
59 for (i = 0; delay_device_list[i] != NULL; i++)
60 if (fnmatch(delay_device_list[i], syspath, 0) == 0)
65 static int device_list_insert(struct udev *udev, const char *path)
67 char filename[UTIL_PATH_SIZE];
70 dbg(udev, "add '%s'\n" , path);
72 /* we only have a device, if we have an uevent file */
73 util_strlcpy(filename, path, sizeof(filename));
74 util_strlcat(filename, "/uevent", sizeof(filename));
75 if (stat(filename, &statbuf) < 0)
77 if (!(statbuf.st_mode & S_IWUSR))
80 /* resolve possible link to real target */
81 util_strlcpy(filename, path, sizeof(filename));
82 if (lstat(filename, &statbuf) < 0)
84 if (S_ISLNK(statbuf.st_mode))
85 if (util_resolve_sys_link(udev, filename, sizeof(filename)) != 0)
88 name_list_add(udev, &device_list, filename, 1);
92 static void trigger_uevent(struct udev *udev, const char *syspath, const char *action)
94 char filename[UTIL_PATH_SIZE];
97 util_strlcpy(filename, syspath, sizeof(filename));
98 util_strlcat(filename, "/uevent", sizeof(filename));
101 printf("%s\n", syspath);
106 fd = open(filename, O_WRONLY);
108 dbg(udev, "error on opening %s: %s\n", filename, strerror(errno));
112 if (write(fd, action, strlen(action)) < 0)
113 info(udev, "error writing '%s' to '%s': %s\n", action, filename, strerror(errno));
118 static int pass_to_socket(struct udev *udev, const char *syspath, const char *action, const char *env)
120 struct udevice *udevice;
121 struct name_entry *name_loop;
125 char path[UTIL_PATH_SIZE];
127 char link_target[UTIL_PATH_SIZE];
129 const char *devpath = &syspath[strlen(udev_get_sys_path(udev))];
133 printf("%s\n", devpath);
135 udevice = udev_device_init(udev);
138 udev_db_get_device(udevice, devpath);
141 bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
146 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "%s", env);
150 /* add standard keys */
151 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVPATH=%s", devpath);
153 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "ACTION=%s", action);
157 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
158 util_strlcat(path, devpath, sizeof(path));
159 util_strlcat(path, "/subsystem", sizeof(path));
160 len = readlink(path, link_target, sizeof(link_target));
164 link_target[len] = '\0';
165 pos = strrchr(link_target, '/');
167 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "SUBSYSTEM=%s", &pos[1]);
172 /* add symlinks and node name */
174 list_for_each_entry(name_loop, &udevice->symlink_list, node) {
175 util_strlcat(path, udev_get_dev_path(udev), sizeof(path));
176 util_strlcat(path, "/", sizeof(path));
177 util_strlcat(path, name_loop->name, sizeof(path));
178 util_strlcat(path, " ", sizeof(path));
180 util_remove_trailing_chars(path, ' ');
181 if (path[0] != '\0') {
182 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVLINKS=%s", path);
185 if (udevice->name[0] != '\0') {
186 util_strlcpy(path, udev_get_dev_path(udev), sizeof(path));
187 util_strlcat(path, "/", sizeof(path));
188 util_strlcat(path, udevice->name, sizeof(path));
189 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVNAME=%s", path);
193 /* add keys from device "uevent" file */
194 util_strlcpy(path, syspath, sizeof(path));
195 util_strlcat(path, "/uevent", sizeof(path));
196 fd = open(path, O_RDONLY);
200 count = read(fd, value, sizeof(value));
207 while (key[0] != '\0') {
210 next = strchr(key, '\n');
214 bufpos += util_strlcpy(&buf[bufpos], key, sizeof(buf) - bufpos-1);
221 /* add keys from database */
222 list_for_each_entry(name_loop, &udevice->env_list, node) {
223 bufpos += util_strlcpy(&buf[bufpos], name_loop->name, sizeof(buf) - bufpos-1);
226 if (bufpos > sizeof(buf))
227 bufpos = sizeof(buf);
229 count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, saddrlen);
236 static void exec_list(struct udev *udev, const char *action, const char *env)
238 struct name_entry *loop_device;
239 struct name_entry *tmp_device;
241 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
242 if (delay_device(loop_device->name))
245 pass_to_socket(udev, loop_device->name, action, env);
247 trigger_uevent(udev, loop_device->name, action);
248 list_del(&loop_device->node);
252 /* trigger remaining delayed devices */
253 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
255 pass_to_socket(udev, loop_device->name, action, env);
257 trigger_uevent(udev, loop_device->name, action);
258 list_del(&loop_device->node);
263 static int subsystem_filtered(const char *subsystem)
265 struct name_entry *loop_name;
267 /* skip devices matching the listed subsystems */
268 list_for_each_entry(loop_name, &filter_subsystem_nomatch_list, node)
269 if (fnmatch(loop_name->name, subsystem, 0) == 0)
272 /* skip devices not matching the listed subsystems */
273 if (!list_empty(&filter_subsystem_match_list)) {
274 list_for_each_entry(loop_name, &filter_subsystem_match_list, node)
275 if (fnmatch(loop_name->name, subsystem, 0) == 0)
283 static int attr_match(const char *path, const char *attr_value)
285 char attr[UTIL_NAME_SIZE];
286 char file[UTIL_PATH_SIZE];
289 util_strlcpy(attr, attr_value, sizeof(attr));
291 /* separate attr and match value */
292 match_value = strchr(attr, '=');
293 if (match_value != NULL) {
294 match_value[0] = '\0';
295 match_value = &match_value[1];
298 util_strlcpy(file, path, sizeof(file));
299 util_strlcat(file, "/", sizeof(file));
300 util_strlcat(file, attr, sizeof(file));
302 if (match_value != NULL) {
303 /* match file content */
304 char value[UTIL_NAME_SIZE];
308 fd = open(file, O_RDONLY);
311 size = read(fd, value, sizeof(value));
316 util_remove_trailing_chars(value, '\n');
318 /* match if attribute value matches */
319 if (fnmatch(match_value, value, 0) == 0)
322 /* match if attribute exists */
325 if (stat(file, &statbuf) == 0)
331 static int attr_filtered(const char *path)
333 struct name_entry *loop_name;
335 /* skip devices matching the listed sysfs attributes */
336 list_for_each_entry(loop_name, &filter_attr_nomatch_list, node)
337 if (attr_match(path, loop_name->name))
340 /* skip devices not matching the listed sysfs attributes */
341 if (!list_empty(&filter_attr_match_list)) {
342 list_for_each_entry(loop_name, &filter_attr_match_list, node)
343 if (attr_match(path, loop_name->name))
355 static void scan_subsystem(struct udev *udev, const char *subsys, enum scan_type scan)
357 char base[UTIL_PATH_SIZE];
362 if (scan == SCAN_DEVICES)
364 else if (scan == SCAN_SUBSYSTEM)
369 util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
370 util_strlcat(base, "/", sizeof(base));
371 util_strlcat(base, subsys, sizeof(base));
375 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
376 char dirname[UTIL_PATH_SIZE];
378 struct dirent *dent2;
380 if (dent->d_name[0] == '.')
383 if (scan == SCAN_DEVICES)
384 if (subsystem_filtered(dent->d_name))
387 util_strlcpy(dirname, base, sizeof(dirname));
388 util_strlcat(dirname, "/", sizeof(dirname));
389 util_strlcat(dirname, dent->d_name, sizeof(dirname));
391 if (scan == SCAN_SUBSYSTEM) {
392 if (attr_filtered(dirname))
394 if (!subsystem_filtered("subsystem"))
395 device_list_insert(udev, dirname);
396 if (subsystem_filtered("drivers"))
400 util_strlcat(dirname, subdir, sizeof(dirname));
402 /* look for devices/drivers */
403 dir2 = opendir(dirname);
405 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
406 char dirname2[UTIL_PATH_SIZE];
408 if (dent2->d_name[0] == '.')
411 util_strlcpy(dirname2, dirname, sizeof(dirname2));
412 util_strlcat(dirname2, "/", sizeof(dirname2));
413 util_strlcat(dirname2, dent2->d_name, sizeof(dirname2));
414 if (attr_filtered(dirname2))
416 device_list_insert(udev, dirname2);
425 static void scan_block(struct udev *udev)
427 char base[UTIL_PATH_SIZE];
431 if (subsystem_filtered("block"))
434 util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
435 util_strlcat(base, "/block", sizeof(base));
439 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
440 char dirname[UTIL_PATH_SIZE];
442 struct dirent *dent2;
444 if (dent->d_name[0] == '.')
447 util_strlcpy(dirname, base, sizeof(dirname));
448 util_strlcat(dirname, "/", sizeof(dirname));
449 util_strlcat(dirname, dent->d_name, sizeof(dirname));
450 if (attr_filtered(dirname))
452 if (device_list_insert(udev, dirname) != 0)
455 /* look for partitions */
456 dir2 = opendir(dirname);
458 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
459 char dirname2[UTIL_PATH_SIZE];
461 if (dent2->d_name[0] == '.')
464 if (!strcmp(dent2->d_name,"device"))
467 util_strlcpy(dirname2, dirname, sizeof(dirname2));
468 util_strlcat(dirname2, "/", sizeof(dirname2));
469 util_strlcat(dirname2, dent2->d_name, sizeof(dirname2));
470 if (attr_filtered(dirname2))
472 device_list_insert(udev, dirname2);
481 static void scan_class(struct udev *udev)
483 char base[UTIL_PATH_SIZE];
487 util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
488 util_strlcat(base, "/class", sizeof(base));
492 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
493 char dirname[UTIL_PATH_SIZE];
495 struct dirent *dent2;
497 if (dent->d_name[0] == '.')
500 if (subsystem_filtered(dent->d_name))
503 util_strlcpy(dirname, base, sizeof(dirname));
504 util_strlcat(dirname, "/", sizeof(dirname));
505 util_strlcat(dirname, dent->d_name, sizeof(dirname));
506 dir2 = opendir(dirname);
508 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
509 char dirname2[UTIL_PATH_SIZE];
511 if (dent2->d_name[0] == '.')
514 if (!strcmp(dent2->d_name, "device"))
517 util_strlcpy(dirname2, dirname, sizeof(dirname2));
518 util_strlcat(dirname2, "/", sizeof(dirname2));
519 util_strlcat(dirname2, dent2->d_name, sizeof(dirname2));
520 if (attr_filtered(dirname2))
522 device_list_insert(udev, dirname2);
531 static void scan_failed(struct udev *udev)
533 char base[UTIL_PATH_SIZE];
537 util_strlcpy(base, udev_get_dev_path(udev), sizeof(base));
538 util_strlcat(base, "/.udev/failed", sizeof(base));
542 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
543 char device[UTIL_PATH_SIZE];
546 if (dent->d_name[0] == '.')
549 start = util_strlcpy(device, udev_get_sys_path(udev), sizeof(device));
550 if(start >= sizeof(device))
551 start = sizeof(device) - 1;
552 util_strlcat(device, dent->d_name, sizeof(device));
553 util_path_decode(&device[start]);
554 device_list_insert(udev, device);
560 int udevadm_trigger(struct udev *udev, int argc, char *argv[])
563 const char *sockpath = NULL;
565 const char *action = "add";
566 const char *env = NULL;
567 static const struct option options[] = {
568 { "verbose", 0, NULL, 'v' },
569 { "dry-run", 0, NULL, 'n' },
570 { "retry-failed", 0, NULL, 'F' },
571 { "socket", 1, NULL, 'o' },
572 { "help", 0, NULL, 'h' },
573 { "action", 1, NULL, 'c' },
574 { "subsystem-match", 1, NULL, 's' },
575 { "subsystem-nomatch", 1, NULL, 'S' },
576 { "attr-match", 1, NULL, 'a' },
577 { "attr-nomatch", 1, NULL, 'A' },
578 { "env", 1, NULL, 'e' },
582 dbg(udev, "version %s\n", VERSION);
585 option = getopt_long(argc, argv, "vnFo:hce::s:S:a:A:", options, NULL);
606 if (strchr(optarg, '=') != NULL)
610 name_list_add(udev, &filter_subsystem_match_list, optarg, 0);
613 name_list_add(udev, &filter_subsystem_nomatch_list, optarg, 0);
616 name_list_add(udev, &filter_attr_match_list, optarg, 0);
619 name_list_add(udev, &filter_attr_nomatch_list, optarg, 0);
622 printf("Usage: udevadm trigger OPTIONS\n"
623 " --verbose print the list of devices while running\n"
624 " --dry-run do not actually trigger the events\n"
625 " --retry-failed trigger only the events which have been\n"
626 " marked as failed during a previous run\n"
627 " --socket=<socket path> pass events to socket instead of triggering kernel events\n"
628 " --env=<KEY>=<value> pass an additional key (works only with --socket=)\n"
629 " --subsystem-match=<subsystem> trigger devices from a matching subystem\n"
630 " --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
631 " --attr-match=<file[=<value>]> trigger devices with a matching sysfs\n"
633 " --attr-nomatch=<file[=<value>]> exclude devices with a matching sysfs\n"
635 " --help print this text\n"
643 if (sockpath != NULL) {
646 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
647 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
648 saddr.sun_family = AF_LOCAL;
649 if (sockpath[0] == '@') {
650 /* abstract namespace socket requested */
651 util_strlcpy(&saddr.sun_path[1], &sockpath[1], sizeof(saddr.sun_path)-1);
652 saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
653 } else if (stat(sockpath, &stats) == 0 && S_ISSOCK(stats.st_mode)) {
654 /* existing socket file */
655 util_strlcpy(saddr.sun_path, sockpath, sizeof(saddr.sun_path));
656 saddrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path);
658 /* no socket file, assume abstract namespace socket */
659 util_strlcpy(&saddr.sun_path[1], sockpath, sizeof(saddr.sun_path)-1);
660 saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
662 } else if (env != NULL) {
663 fprintf(stderr, "error: --env= only valid with --socket= option\n");
669 exec_list(udev, action, env);
671 char base[UTIL_PATH_SIZE];
674 /* if we have /sys/subsystem, forget all the old stuff */
675 util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
676 util_strlcat(base, "/subsystem", sizeof(base));
677 if (stat(base, &statbuf) == 0) {
678 scan_subsystem(udev, "subsystem", SCAN_SUBSYSTEM);
679 exec_list(udev, action, env);
680 scan_subsystem(udev, "subsystem", SCAN_DEVICES);
681 exec_list(udev, action, env);
683 scan_subsystem(udev, "bus", SCAN_SUBSYSTEM);
684 exec_list(udev, action, env);
685 scan_subsystem(udev, "bus", SCAN_DEVICES);
688 /* scan "block" if it isn't a "class" */
689 util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
690 util_strlcat(base, "/class/block", sizeof(base));
691 if (stat(base, &statbuf) != 0)
693 exec_list(udev, action, env);
698 name_list_cleanup(udev, &filter_subsystem_match_list);
699 name_list_cleanup(udev, &filter_subsystem_nomatch_list);
700 name_list_cleanup(udev, &filter_attr_match_list);
701 name_list_cleanup(udev, &filter_attr_nomatch_list);