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(struct udev *udev, const char *path)
69 char filename[PATH_SIZE];
70 char devpath[PATH_SIZE];
73 dbg(udev, "add '%s'\n" , 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(udev_get_sys_path(udev))], 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(udev, devpath, sizeof(devpath)) != 0)
92 name_list_add(udev, &device_list, devpath, 1);
96 static void trigger_uevent(struct udev *udev, const char *devpath, const char *action)
98 char filename[PATH_SIZE];
101 strlcpy(filename, udev_get_sys_path(udev), 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(udev, "error on opening %s: %s\n", filename, strerror(errno));
117 if (write(fd, action, strlen(action)) < 0)
118 info(udev, "error writing '%s' to '%s': %s\n", action, filename, strerror(errno));
123 static int pass_to_socket(struct udev *udev, const char *devpath, const char *action, const char *env)
125 struct udevice *udevice;
126 struct name_entry *name_loop;
130 char path[PATH_SIZE];
132 char link_target[PATH_SIZE];
137 printf("%s\n", devpath);
139 udevice = udev_device_init(udev);
142 udev_db_get_device(udevice, devpath);
145 bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
150 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "%s", env);
154 /* add standard keys */
155 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVPATH=%s", devpath);
157 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "ACTION=%s", action);
161 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
162 strlcat(path, devpath, sizeof(path));
163 strlcat(path, "/subsystem", sizeof(path));
164 len = readlink(path, link_target, sizeof(link_target));
168 link_target[len] = '\0';
169 pos = strrchr(link_target, '/');
171 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "SUBSYSTEM=%s", &pos[1]);
176 /* add symlinks and node name */
178 list_for_each_entry(name_loop, &udevice->symlink_list, node) {
179 strlcat(path, udev_get_dev_path(udev), sizeof(path));
180 strlcat(path, "/", sizeof(path));
181 strlcat(path, name_loop->name, sizeof(path));
182 strlcat(path, " ", sizeof(path));
184 remove_trailing_chars(path, ' ');
185 if (path[0] != '\0') {
186 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVLINKS=%s", path);
189 if (udevice->name[0] != '\0') {
190 strlcpy(path, udev_get_dev_path(udev), sizeof(path));
191 strlcat(path, "/", sizeof(path));
192 strlcat(path, udevice->name, sizeof(path));
193 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVNAME=%s", path);
197 /* add keys from device "uevent" file */
198 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
199 strlcat(path, devpath, sizeof(path));
200 strlcat(path, "/uevent", sizeof(path));
201 fd = open(path, O_RDONLY);
205 count = read(fd, value, sizeof(value));
212 while (key[0] != '\0') {
215 next = strchr(key, '\n');
219 bufpos += strlcpy(&buf[bufpos], key, sizeof(buf) - bufpos-1);
226 /* add keys from database */
227 list_for_each_entry(name_loop, &udevice->env_list, node) {
228 bufpos += strlcpy(&buf[bufpos], name_loop->name, sizeof(buf) - bufpos-1);
231 if (bufpos > sizeof(buf))
232 bufpos = sizeof(buf);
234 count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, saddrlen);
241 static void exec_list(struct udev *udev, const char *action, const char *env)
243 struct name_entry *loop_device;
244 struct name_entry *tmp_device;
246 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
247 if (delay_device(loop_device->name))
250 pass_to_socket(udev, loop_device->name, action, env);
252 trigger_uevent(udev, loop_device->name, action);
253 list_del(&loop_device->node);
257 /* trigger remaining delayed devices */
258 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
260 pass_to_socket(udev, loop_device->name, action, env);
262 trigger_uevent(udev, loop_device->name, action);
263 list_del(&loop_device->node);
268 static int subsystem_filtered(const char *subsystem)
270 struct name_entry *loop_name;
272 /* skip devices matching the listed subsystems */
273 list_for_each_entry(loop_name, &filter_subsystem_nomatch_list, node)
274 if (fnmatch(loop_name->name, subsystem, 0) == 0)
277 /* skip devices not matching the listed subsystems */
278 if (!list_empty(&filter_subsystem_match_list)) {
279 list_for_each_entry(loop_name, &filter_subsystem_match_list, node)
280 if (fnmatch(loop_name->name, subsystem, 0) == 0)
288 static int attr_match(const char *path, const char *attr_value)
290 char attr[NAME_SIZE];
291 char file[PATH_SIZE];
294 strlcpy(attr, attr_value, sizeof(attr));
296 /* separate attr and match value */
297 match_value = strchr(attr, '=');
298 if (match_value != NULL) {
299 match_value[0] = '\0';
300 match_value = &match_value[1];
303 strlcpy(file, path, sizeof(file));
304 strlcat(file, "/", sizeof(file));
305 strlcat(file, attr, sizeof(file));
307 if (match_value != NULL) {
308 /* match file content */
309 char value[NAME_SIZE];
313 fd = open(file, O_RDONLY);
316 size = read(fd, value, sizeof(value));
321 remove_trailing_chars(value, '\n');
323 /* match if attribute value matches */
324 if (fnmatch(match_value, value, 0) == 0)
327 /* match if attribute exists */
330 if (stat(file, &statbuf) == 0)
336 static int attr_filtered(const char *path)
338 struct name_entry *loop_name;
340 /* skip devices matching the listed sysfs attributes */
341 list_for_each_entry(loop_name, &filter_attr_nomatch_list, node)
342 if (attr_match(path, loop_name->name))
345 /* skip devices not matching the listed sysfs attributes */
346 if (!list_empty(&filter_attr_match_list)) {
347 list_for_each_entry(loop_name, &filter_attr_match_list, node)
348 if (attr_match(path, loop_name->name))
360 static void scan_subsystem(struct udev *udev, const char *subsys, enum scan_type scan)
362 char base[PATH_SIZE];
367 if (scan == SCAN_DEVICES)
369 else if (scan == SCAN_SUBSYSTEM)
374 strlcpy(base, udev_get_sys_path(udev), sizeof(base));
375 strlcat(base, "/", sizeof(base));
376 strlcat(base, subsys, sizeof(base));
380 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
381 char dirname[PATH_SIZE];
383 struct dirent *dent2;
385 if (dent->d_name[0] == '.')
388 if (scan == SCAN_DEVICES)
389 if (subsystem_filtered(dent->d_name))
392 strlcpy(dirname, base, sizeof(dirname));
393 strlcat(dirname, "/", sizeof(dirname));
394 strlcat(dirname, dent->d_name, sizeof(dirname));
396 if (scan == SCAN_SUBSYSTEM) {
397 if (attr_filtered(dirname))
399 if (!subsystem_filtered("subsystem"))
400 device_list_insert(udev, dirname);
401 if (subsystem_filtered("drivers"))
405 strlcat(dirname, subdir, sizeof(dirname));
407 /* look for devices/drivers */
408 dir2 = opendir(dirname);
410 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
411 char dirname2[PATH_SIZE];
413 if (dent2->d_name[0] == '.')
416 strlcpy(dirname2, dirname, sizeof(dirname2));
417 strlcat(dirname2, "/", sizeof(dirname2));
418 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
419 if (attr_filtered(dirname2))
421 device_list_insert(udev, dirname2);
430 static void scan_block(struct udev *udev)
432 char base[PATH_SIZE];
436 if (subsystem_filtered("block"))
439 strlcpy(base, udev_get_sys_path(udev), sizeof(base));
440 strlcat(base, "/block", sizeof(base));
444 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
445 char dirname[PATH_SIZE];
447 struct dirent *dent2;
449 if (dent->d_name[0] == '.')
452 strlcpy(dirname, base, sizeof(dirname));
453 strlcat(dirname, "/", sizeof(dirname));
454 strlcat(dirname, dent->d_name, sizeof(dirname));
455 if (attr_filtered(dirname))
457 if (device_list_insert(udev, dirname) != 0)
460 /* look for partitions */
461 dir2 = opendir(dirname);
463 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
464 char dirname2[PATH_SIZE];
466 if (dent2->d_name[0] == '.')
469 if (!strcmp(dent2->d_name,"device"))
472 strlcpy(dirname2, dirname, sizeof(dirname2));
473 strlcat(dirname2, "/", sizeof(dirname2));
474 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
475 if (attr_filtered(dirname2))
477 device_list_insert(udev, dirname2);
486 static void scan_class(struct udev *udev)
488 char base[PATH_SIZE];
492 strlcpy(base, udev_get_sys_path(udev), sizeof(base));
493 strlcat(base, "/class", sizeof(base));
497 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
498 char dirname[PATH_SIZE];
500 struct dirent *dent2;
502 if (dent->d_name[0] == '.')
505 if (subsystem_filtered(dent->d_name))
508 strlcpy(dirname, base, sizeof(dirname));
509 strlcat(dirname, "/", sizeof(dirname));
510 strlcat(dirname, dent->d_name, sizeof(dirname));
511 dir2 = opendir(dirname);
513 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
514 char dirname2[PATH_SIZE];
516 if (dent2->d_name[0] == '.')
519 if (!strcmp(dent2->d_name, "device"))
522 strlcpy(dirname2, dirname, sizeof(dirname2));
523 strlcat(dirname2, "/", sizeof(dirname2));
524 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
525 if (attr_filtered(dirname2))
527 device_list_insert(udev, dirname2);
536 static void scan_failed(struct udev *udev)
538 char base[PATH_SIZE];
542 strlcpy(base, udev_get_dev_path(udev), sizeof(base));
543 strlcat(base, "/.udev/failed", sizeof(base));
547 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
548 char device[PATH_SIZE];
551 if (dent->d_name[0] == '.')
554 start = strlcpy(device, udev_get_sys_path(udev), sizeof(device));
555 if(start >= sizeof(device))
556 start = sizeof(device) - 1;
557 strlcat(device, dent->d_name, sizeof(device));
558 path_decode(&device[start]);
559 device_list_insert(udev, device);
565 int udevadm_trigger(struct udev *udev, int argc, char *argv[])
568 const char *sockpath = NULL;
570 const char *action = "add";
571 const char *env = NULL;
572 static const struct option options[] = {
573 { "verbose", 0, NULL, 'v' },
574 { "dry-run", 0, NULL, 'n' },
575 { "retry-failed", 0, NULL, 'F' },
576 { "socket", 1, NULL, 'o' },
577 { "help", 0, NULL, 'h' },
578 { "action", 1, NULL, 'c' },
579 { "subsystem-match", 1, NULL, 's' },
580 { "subsystem-nomatch", 1, NULL, 'S' },
581 { "attr-match", 1, NULL, 'a' },
582 { "attr-nomatch", 1, NULL, 'A' },
583 { "env", 1, NULL, 'e' },
587 dbg(udev, "version %s\n", VERSION);
590 option = getopt_long(argc, argv, "vnFo:hce::s:S:a:A:", options, NULL);
611 if (strchr(optarg, '=') != NULL)
615 name_list_add(udev, &filter_subsystem_match_list, optarg, 0);
618 name_list_add(udev, &filter_subsystem_nomatch_list, optarg, 0);
621 name_list_add(udev, &filter_attr_match_list, optarg, 0);
624 name_list_add(udev, &filter_attr_nomatch_list, optarg, 0);
627 printf("Usage: udevadm trigger OPTIONS\n"
628 " --verbose print the list of devices while running\n"
629 " --dry-run do not actually trigger the events\n"
630 " --retry-failed trigger only the events which have been\n"
631 " marked as failed during a previous run\n"
632 " --socket=<socket path> pass events to socket instead of triggering kernel events\n"
633 " --env=<KEY>=<value> pass an additional key (works only with --socket=)\n"
634 " --subsystem-match=<subsystem> trigger devices from a matching subystem\n"
635 " --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
636 " --attr-match=<file[=<value>]> trigger devices with a matching sysfs\n"
638 " --attr-nomatch=<file[=<value>]> exclude devices with a matching sysfs\n"
640 " --help print this text\n"
648 if (sockpath != NULL) {
651 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
652 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
653 saddr.sun_family = AF_LOCAL;
654 if (sockpath[0] == '@') {
655 /* abstract namespace socket requested */
656 strlcpy(&saddr.sun_path[1], &sockpath[1], sizeof(saddr.sun_path)-1);
657 saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
658 } else if (stat(sockpath, &stats) == 0 && S_ISSOCK(stats.st_mode)) {
659 /* existing socket file */
660 strlcpy(saddr.sun_path, sockpath, sizeof(saddr.sun_path));
661 saddrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path);
663 /* no socket file, assume abstract namespace socket */
664 strlcpy(&saddr.sun_path[1], sockpath, sizeof(saddr.sun_path)-1);
665 saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
667 } else if (env != NULL) {
668 fprintf(stderr, "error: --env= only valid with --socket= option\n");
674 exec_list(udev, action, env);
676 char base[PATH_SIZE];
679 /* if we have /sys/subsystem, forget all the old stuff */
680 strlcpy(base, udev_get_sys_path(udev), sizeof(base));
681 strlcat(base, "/subsystem", sizeof(base));
682 if (stat(base, &statbuf) == 0) {
683 scan_subsystem(udev, "subsystem", SCAN_SUBSYSTEM);
684 exec_list(udev, action, env);
685 scan_subsystem(udev, "subsystem", SCAN_DEVICES);
686 exec_list(udev, action, env);
688 scan_subsystem(udev, "bus", SCAN_SUBSYSTEM);
689 exec_list(udev, action, env);
690 scan_subsystem(udev, "bus", SCAN_DEVICES);
693 /* scan "block" if it isn't a "class" */
694 strlcpy(base, udev_get_sys_path(udev), sizeof(base));
695 strlcat(base, "/class/block", sizeof(base));
696 if (stat(base, &statbuf) != 0)
698 exec_list(udev, action, env);
703 name_list_cleanup(udev, &filter_subsystem_match_list);
704 name_list_cleanup(udev, &filter_subsystem_nomatch_list);
705 name_list_cleanup(udev, &filter_attr_match_list);
706 name_list_cleanup(udev, &filter_attr_nomatch_list);