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'\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(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\n", filename, strerror(errno));
117 if (write(fd, action, strlen(action)) < 0)
118 info("error writing '%s' to '%s': %s\n", action, filename, strerror(errno));
123 static int pass_to_socket(const char *devpath, const char *action, const char *env)
126 struct name_entry *name_loop;
130 char path[PATH_SIZE];
132 char link_target[PATH_SIZE];
137 printf("%s\n", devpath);
139 udev_device_init(&udev);
140 udev_db_get_device(&udev, devpath);
143 bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
148 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "%s", env);
152 /* add standard keys */
153 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVPATH=%s", devpath);
155 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "ACTION=%s", action);
159 strlcpy(path, sysfs_path, sizeof(path));
160 strlcat(path, devpath, sizeof(path));
161 strlcat(path, "/subsystem", sizeof(path));
162 len = readlink(path, link_target, sizeof(link_target));
166 link_target[len] = '\0';
167 pos = strrchr(link_target, '/');
169 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "SUBSYSTEM=%s", &pos[1]);
174 /* add symlinks and node name */
176 list_for_each_entry(name_loop, &udev.symlink_list, node) {
177 strlcat(path, udev_root, sizeof(path));
178 strlcat(path, "/", sizeof(path));
179 strlcat(path, name_loop->name, sizeof(path));
180 strlcat(path, " ", sizeof(path));
182 remove_trailing_chars(path, ' ');
183 if (path[0] != '\0') {
184 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVLINKS=%s", path);
187 if (udev.name[0] != '\0') {
188 strlcpy(path, udev_root, sizeof(path));
189 strlcat(path, "/", sizeof(path));
190 strlcat(path, udev.name, sizeof(path));
191 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVNAME=%s", path);
195 /* add keys from device "uevent" file */
196 strlcpy(path, sysfs_path, sizeof(path));
197 strlcat(path, devpath, sizeof(path));
198 strlcat(path, "/uevent", sizeof(path));
199 fd = open(path, O_RDONLY);
203 count = read(fd, value, sizeof(value));
210 while (key[0] != '\0') {
213 next = strchr(key, '\n');
217 bufpos += strlcpy(&buf[bufpos], key, sizeof(buf) - bufpos-1);
224 /* add keys from database */
225 list_for_each_entry(name_loop, &udev.env_list, node) {
226 bufpos += strlcpy(&buf[bufpos], name_loop->name, sizeof(buf) - bufpos-1);
229 if (bufpos > sizeof(buf))
230 bufpos = sizeof(buf);
232 count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, saddrlen);
239 static void exec_list(const char *action, const char *env)
241 struct name_entry *loop_device;
242 struct name_entry *tmp_device;
244 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
245 if (delay_device(loop_device->name))
248 pass_to_socket(loop_device->name, action, env);
250 trigger_uevent(loop_device->name, action);
251 list_del(&loop_device->node);
255 /* trigger remaining delayed devices */
256 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
258 pass_to_socket(loop_device->name, action, env);
260 trigger_uevent(loop_device->name, action);
261 list_del(&loop_device->node);
266 static int subsystem_filtered(const char *subsystem)
268 struct name_entry *loop_name;
270 /* skip devices matching the listed subsystems */
271 list_for_each_entry(loop_name, &filter_subsystem_nomatch_list, node)
272 if (fnmatch(loop_name->name, subsystem, 0) == 0)
275 /* skip devices not matching the listed subsystems */
276 if (!list_empty(&filter_subsystem_match_list)) {
277 list_for_each_entry(loop_name, &filter_subsystem_match_list, node)
278 if (fnmatch(loop_name->name, subsystem, 0) == 0)
286 static int attr_match(const char *path, const char *attr_value)
288 char attr[NAME_SIZE];
289 char file[PATH_SIZE];
292 strlcpy(attr, attr_value, sizeof(attr));
294 /* separate attr and match value */
295 match_value = strchr(attr, '=');
296 if (match_value != NULL) {
297 match_value[0] = '\0';
298 match_value = &match_value[1];
301 strlcpy(file, path, sizeof(file));
302 strlcat(file, "/", sizeof(file));
303 strlcat(file, attr, sizeof(file));
305 if (match_value != NULL) {
306 /* match file content */
307 char value[NAME_SIZE];
311 fd = open(file, O_RDONLY);
314 size = read(fd, value, sizeof(value));
319 remove_trailing_chars(value, '\n');
321 /* match if attribute value matches */
322 if (fnmatch(match_value, value, 0) == 0)
325 /* match if attribute exists */
328 if (stat(file, &statbuf) == 0)
334 static int attr_filtered(const char *path)
336 struct name_entry *loop_name;
338 /* skip devices matching the listed sysfs attributes */
339 list_for_each_entry(loop_name, &filter_attr_nomatch_list, node)
340 if (attr_match(path, loop_name->name))
343 /* skip devices not matching the listed sysfs attributes */
344 if (!list_empty(&filter_attr_match_list)) {
345 list_for_each_entry(loop_name, &filter_attr_match_list, node)
346 if (attr_match(path, loop_name->name))
358 static void scan_subsystem(const char *subsys, enum scan_type scan)
360 char base[PATH_SIZE];
365 if (scan == SCAN_DEVICES)
367 else if (scan == SCAN_SUBSYSTEM)
372 strlcpy(base, sysfs_path, sizeof(base));
373 strlcat(base, "/", sizeof(base));
374 strlcat(base, subsys, sizeof(base));
378 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
379 char dirname[PATH_SIZE];
381 struct dirent *dent2;
383 if (dent->d_name[0] == '.')
386 if (scan == SCAN_DEVICES)
387 if (subsystem_filtered(dent->d_name))
390 strlcpy(dirname, base, sizeof(dirname));
391 strlcat(dirname, "/", sizeof(dirname));
392 strlcat(dirname, dent->d_name, sizeof(dirname));
394 if (scan == SCAN_SUBSYSTEM) {
395 if (attr_filtered(dirname))
397 if (!subsystem_filtered("subsystem"))
398 device_list_insert(dirname);
399 if (subsystem_filtered("drivers"))
403 strlcat(dirname, subdir, sizeof(dirname));
405 /* look for devices/drivers */
406 dir2 = opendir(dirname);
408 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
409 char dirname2[PATH_SIZE];
411 if (dent2->d_name[0] == '.')
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 device_list_insert(dirname2);
428 static void scan_block(void)
430 char base[PATH_SIZE];
434 if (subsystem_filtered("block"))
437 strlcpy(base, sysfs_path, sizeof(base));
438 strlcat(base, "/block", sizeof(base));
442 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
443 char dirname[PATH_SIZE];
445 struct dirent *dent2;
447 if (dent->d_name[0] == '.')
450 strlcpy(dirname, base, sizeof(dirname));
451 strlcat(dirname, "/", sizeof(dirname));
452 strlcat(dirname, dent->d_name, sizeof(dirname));
453 if (attr_filtered(dirname))
455 if (device_list_insert(dirname) != 0)
458 /* look for partitions */
459 dir2 = opendir(dirname);
461 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
462 char dirname2[PATH_SIZE];
464 if (dent2->d_name[0] == '.')
467 if (!strcmp(dent2->d_name,"device"))
470 strlcpy(dirname2, dirname, sizeof(dirname2));
471 strlcat(dirname2, "/", sizeof(dirname2));
472 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
473 if (attr_filtered(dirname2))
475 device_list_insert(dirname2);
484 static void scan_class(void)
486 char base[PATH_SIZE];
490 strlcpy(base, sysfs_path, sizeof(base));
491 strlcat(base, "/class", sizeof(base));
495 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
496 char dirname[PATH_SIZE];
498 struct dirent *dent2;
500 if (dent->d_name[0] == '.')
503 if (subsystem_filtered(dent->d_name))
506 strlcpy(dirname, base, sizeof(dirname));
507 strlcat(dirname, "/", sizeof(dirname));
508 strlcat(dirname, dent->d_name, sizeof(dirname));
509 dir2 = opendir(dirname);
511 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
512 char dirname2[PATH_SIZE];
514 if (dent2->d_name[0] == '.')
517 if (!strcmp(dent2->d_name, "device"))
520 strlcpy(dirname2, dirname, sizeof(dirname2));
521 strlcat(dirname2, "/", sizeof(dirname2));
522 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
523 if (attr_filtered(dirname2))
525 device_list_insert(dirname2);
534 static void scan_failed(void)
536 char base[PATH_SIZE];
540 strlcpy(base, udev_root, sizeof(base));
541 strlcat(base, "/.udev/failed", sizeof(base));
545 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
546 char device[PATH_SIZE];
549 if (dent->d_name[0] == '.')
552 start = strlcpy(device, sysfs_path, sizeof(device));
553 if(start >= sizeof(device))
554 start = sizeof(device) - 1;
555 strlcat(device, dent->d_name, sizeof(device));
556 path_decode(&device[start]);
557 device_list_insert(device);
563 int udevtrigger(int argc, char *argv[], char *envp[])
566 const char *sockpath = NULL;
568 const char *action = "add";
569 const char *env = NULL;
570 static const struct option options[] = {
571 { "verbose", 0, NULL, 'v' },
572 { "dry-run", 0, NULL, 'n' },
573 { "retry-failed", 0, NULL, 'F' },
574 { "socket", 1, NULL, 'o' },
575 { "help", 0, NULL, 'h' },
576 { "action", 1, NULL, 'c' },
577 { "subsystem-match", 1, NULL, 's' },
578 { "subsystem-nomatch", 1, NULL, 'S' },
579 { "attr-match", 1, NULL, 'a' },
580 { "attr-nomatch", 1, NULL, 'A' },
581 { "env", 1, NULL, 'e' },
585 logging_init("udevtrigger");
587 dbg("version %s\n", VERSION);
591 option = getopt_long(argc, argv, "vnFo:hce::s:S:a:A:", options, NULL);
612 if (strchr(optarg, '=') != NULL)
616 name_list_add(&filter_subsystem_match_list, optarg, 0);
619 name_list_add(&filter_subsystem_nomatch_list, optarg, 0);
622 name_list_add(&filter_attr_match_list, optarg, 0);
625 name_list_add(&filter_attr_nomatch_list, optarg, 0);
628 printf("Usage: udevadm trigger OPTIONS\n"
629 " --verbose print the list of devices while running\n"
630 " --dry-run do not actually trigger the events\n"
631 " --retry-failed trigger only the events which have been\n"
632 " marked as failed during a previous run\n"
633 " --socket=<socket path> pass events to socket instead of triggering kernel events\n"
634 " --env=<KEY>=<value> pass an additional key (works only with --socket=)\n"
635 " --subsystem-match=<subsystem> trigger devices from a matching subystem\n"
636 " --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
637 " --attr-match=<file[=<value>]> trigger devices with a matching sysfs\n"
639 " --attr-nomatch=<file[=<value>]> exclude devices with a matching sysfs\n"
641 " --help print this text\n"
649 if (sockpath != NULL) {
652 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
653 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
654 saddr.sun_family = AF_LOCAL;
655 if (sockpath[0] == '@') {
656 /* abstract namespace socket requested */
657 strlcpy(&saddr.sun_path[1], &sockpath[1], sizeof(saddr.sun_path)-1);
658 saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
659 } else if (stat(sockpath, &stats) == 0 && S_ISSOCK(stats.st_mode)) {
660 /* existing socket file */
661 strlcpy(saddr.sun_path, sockpath, sizeof(saddr.sun_path));
662 saddrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path);
664 /* no socket file, assume abstract namespace socket */
665 strlcpy(&saddr.sun_path[1], sockpath, sizeof(saddr.sun_path)-1);
666 saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
668 } else if (env != NULL) {
669 fprintf(stderr, "error: --env= only valid with --socket= option\n");
675 exec_list(action, env);
677 char base[PATH_SIZE];
680 /* if we have /sys/subsystem, forget all the old stuff */
681 strlcpy(base, sysfs_path, sizeof(base));
682 strlcat(base, "/subsystem", sizeof(base));
683 if (stat(base, &statbuf) == 0) {
684 scan_subsystem("subsystem", SCAN_SUBSYSTEM);
685 exec_list(action, env);
686 scan_subsystem("subsystem", SCAN_DEVICES);
687 exec_list(action, env);
689 scan_subsystem("bus", SCAN_SUBSYSTEM);
690 exec_list(action, env);
691 scan_subsystem("bus", SCAN_DEVICES);
694 /* scan "block" if it isn't a "class" */
695 strlcpy(base, sysfs_path, sizeof(base));
696 strlcat(base, "/class/block", sizeof(base));
697 if (stat(base, &statbuf) != 0)
699 exec_list(action, env);
704 name_list_cleanup(&filter_subsystem_match_list);
705 name_list_cleanup(&filter_subsystem_nomatch_list);
706 name_list_cleanup(&filter_attr_match_list);
707 name_list_cleanup(&filter_attr_nomatch_list);