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];
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);
146 /* add standard keys */
147 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVPATH=%s", devpath);
149 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "ACTION=%s", action);
153 strlcpy(path, sysfs_path, sizeof(path));
154 strlcat(path, devpath, sizeof(path));
155 strlcat(path, "/subsystem", sizeof(path));
156 len = readlink(path, link_target, sizeof(link_target));
160 link_target[len] = '\0';
161 pos = strrchr(link_target, '/');
163 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "SUBSYSTEM=%s", &pos[1]);
168 /* add symlinks and node name */
170 list_for_each_entry(name_loop, &udev.symlink_list, node) {
171 strlcat(path, udev_root, sizeof(path));
172 strlcat(path, "/", sizeof(path));
173 strlcat(path, name_loop->name, sizeof(path));
174 strlcat(path, " ", sizeof(path));
176 remove_trailing_chars(path, ' ');
177 if (path[0] != '\0') {
178 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVLINKS=%s", path);
181 if (udev.name[0] != '\0') {
182 strlcpy(path, udev_root, sizeof(path));
183 strlcat(path, "/", sizeof(path));
184 strlcat(path, udev.name, sizeof(path));
185 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVNAME=%s", path);
189 /* add keys from device "uevent" file */
190 strlcpy(path, sysfs_path, sizeof(path));
191 strlcat(path, devpath, sizeof(path));
192 strlcat(path, "/uevent", sizeof(path));
193 fd = open(path, O_RDONLY);
197 count = read(fd, value, sizeof(value));
204 while (key[0] != '\0') {
207 next = strchr(key, '\n');
211 bufpos += strlcpy(&buf[bufpos], key, sizeof(buf) - bufpos-1);
218 /* add keys from database */
219 list_for_each_entry(name_loop, &udev.env_list, node) {
220 bufpos += strlcpy(&buf[bufpos], name_loop->name, sizeof(buf) - bufpos-1);
223 if (bufpos > sizeof(buf))
224 bufpos = sizeof(buf);
226 count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, saddrlen);
233 static void exec_list(const char *action)
235 struct name_entry *loop_device;
236 struct name_entry *tmp_device;
238 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
239 if (delay_device(loop_device->name))
242 pass_to_socket(loop_device->name, action);
244 trigger_uevent(loop_device->name, action);
245 list_del(&loop_device->node);
249 /* trigger remaining delayed devices */
250 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
252 pass_to_socket(loop_device->name, action);
254 trigger_uevent(loop_device->name, action);
255 list_del(&loop_device->node);
260 static int subsystem_filtered(const char *subsystem)
262 struct name_entry *loop_name;
264 /* skip devices matching the listed subsystems */
265 list_for_each_entry(loop_name, &filter_subsystem_nomatch_list, node)
266 if (fnmatch(loop_name->name, subsystem, 0) == 0)
269 /* skip devices not matching the listed subsystems */
270 if (!list_empty(&filter_subsystem_match_list)) {
271 list_for_each_entry(loop_name, &filter_subsystem_match_list, node)
272 if (fnmatch(loop_name->name, subsystem, 0) == 0)
280 static int attr_match(const char *path, const char *attr_value)
282 char attr[NAME_SIZE];
283 char file[PATH_SIZE];
286 strlcpy(attr, attr_value, sizeof(attr));
288 /* separate attr and match value */
289 match_value = strchr(attr, '=');
290 if (match_value != NULL) {
291 match_value[0] = '\0';
292 match_value = &match_value[1];
295 strlcpy(file, path, sizeof(file));
296 strlcat(file, "/", sizeof(file));
297 strlcat(file, attr, sizeof(file));
299 if (match_value != NULL) {
300 /* match file content */
301 char value[NAME_SIZE];
305 fd = open(file, O_RDONLY);
308 size = read(fd, value, sizeof(value));
313 remove_trailing_chars(value, '\n');
315 /* match if attribute value matches */
316 if (fnmatch(match_value, value, 0) == 0)
319 /* match if attribute exists */
322 if (stat(file, &statbuf) == 0)
328 static int attr_filtered(const char *path)
330 struct name_entry *loop_name;
332 /* skip devices matching the listed sysfs attributes */
333 list_for_each_entry(loop_name, &filter_attr_nomatch_list, node)
334 if (attr_match(path, loop_name->name))
337 /* skip devices not matching the listed sysfs attributes */
338 if (!list_empty(&filter_attr_match_list)) {
339 list_for_each_entry(loop_name, &filter_attr_match_list, node)
340 if (attr_match(path, loop_name->name))
352 static void scan_subsystem(const char *subsys, enum scan_type scan)
354 char base[PATH_SIZE];
359 if (scan == SCAN_DEVICES)
361 else if (scan == SCAN_SUBSYSTEM)
366 strlcpy(base, sysfs_path, sizeof(base));
367 strlcat(base, "/", sizeof(base));
368 strlcat(base, subsys, sizeof(base));
372 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
373 char dirname[PATH_SIZE];
375 struct dirent *dent2;
377 if (dent->d_name[0] == '.')
380 if (scan == SCAN_DEVICES)
381 if (subsystem_filtered(dent->d_name))
384 strlcpy(dirname, base, sizeof(dirname));
385 strlcat(dirname, "/", sizeof(dirname));
386 strlcat(dirname, dent->d_name, sizeof(dirname));
388 if (scan == SCAN_SUBSYSTEM) {
389 if (!subsystem_filtered("subsystem"))
390 device_list_insert(dirname);
391 if (subsystem_filtered("drivers"))
395 strlcat(dirname, subdir, sizeof(dirname));
397 /* look for devices/drivers */
398 dir2 = opendir(dirname);
400 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
401 char dirname2[PATH_SIZE];
403 if (dent2->d_name[0] == '.')
406 strlcpy(dirname2, dirname, sizeof(dirname2));
407 strlcat(dirname2, "/", sizeof(dirname2));
408 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
409 if (attr_filtered(dirname2))
411 device_list_insert(dirname2);
420 static void scan_block(void)
422 char base[PATH_SIZE];
426 if (subsystem_filtered("block"))
429 strlcpy(base, sysfs_path, sizeof(base));
430 strlcat(base, "/block", sizeof(base));
434 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
435 char dirname[PATH_SIZE];
437 struct dirent *dent2;
439 if (dent->d_name[0] == '.')
442 strlcpy(dirname, base, sizeof(dirname));
443 strlcat(dirname, "/", sizeof(dirname));
444 strlcat(dirname, dent->d_name, sizeof(dirname));
445 if (attr_filtered(dirname))
447 if (device_list_insert(dirname) != 0)
450 /* look for partitions */
451 dir2 = opendir(dirname);
453 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
454 char dirname2[PATH_SIZE];
456 if (dent2->d_name[0] == '.')
459 if (!strcmp(dent2->d_name,"device"))
462 strlcpy(dirname2, dirname, sizeof(dirname2));
463 strlcat(dirname2, "/", sizeof(dirname2));
464 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
465 if (attr_filtered(dirname2))
467 device_list_insert(dirname2);
476 static void scan_class(void)
478 char base[PATH_SIZE];
482 strlcpy(base, sysfs_path, sizeof(base));
483 strlcat(base, "/class", sizeof(base));
487 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
488 char dirname[PATH_SIZE];
490 struct dirent *dent2;
492 if (dent->d_name[0] == '.')
495 if (subsystem_filtered(dent->d_name))
498 strlcpy(dirname, base, sizeof(dirname));
499 strlcat(dirname, "/", sizeof(dirname));
500 strlcat(dirname, dent->d_name, sizeof(dirname));
501 dir2 = opendir(dirname);
503 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
504 char dirname2[PATH_SIZE];
506 if (dent2->d_name[0] == '.')
509 if (!strcmp(dent2->d_name, "device"))
512 strlcpy(dirname2, dirname, sizeof(dirname2));
513 strlcat(dirname2, "/", sizeof(dirname2));
514 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
515 if (attr_filtered(dirname2))
517 device_list_insert(dirname2);
526 static void scan_failed(void)
528 char base[PATH_SIZE];
532 strlcpy(base, udev_root, sizeof(base));
533 strlcat(base, "/" EVENT_FAILED_DIR, sizeof(base));
537 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
538 char device[PATH_SIZE];
541 if (dent->d_name[0] == '.')
544 start = strlcpy(device, sysfs_path, sizeof(device));
545 if(start >= sizeof(device))
546 start = sizeof(device) - 1;
547 strlcat(device, dent->d_name, sizeof(device));
548 path_decode(&device[start]);
549 device_list_insert(device);
555 int udevtrigger(int argc, char *argv[], char *envp[])
558 const char *sockpath = NULL;
560 const char *action = "add";
561 static const struct option options[] = {
562 { "verbose", 0, NULL, 'v' },
563 { "dry-run", 0, NULL, 'n' },
564 { "retry-failed", 0, NULL, 'F' },
565 { "socket", 1, NULL, 'o' },
566 { "help", 0, NULL, 'h' },
567 { "action", 1, NULL, 'c' },
568 { "subsystem-match", 1, NULL, 's' },
569 { "subsystem-nomatch", 1, NULL, 'S' },
570 { "attr-match", 1, NULL, 'a' },
571 { "attr-nomatch", 1, NULL, 'A' },
575 logging_init("udevtrigger");
577 dbg("version %s", UDEV_VERSION);
581 option = getopt_long(argc, argv, "vnFo:hc:s:S:a:A:", options, NULL);
602 name_list_add(&filter_subsystem_match_list, optarg, 0);
605 name_list_add(&filter_subsystem_nomatch_list, optarg, 0);
608 name_list_add(&filter_attr_match_list, optarg, 0);
611 name_list_add(&filter_attr_nomatch_list, optarg, 0);
614 printf("Usage: udevadm trigger OPTIONS\n"
615 " --verbose print the list of devices while running\n"
616 " --dry-run do not actually trigger the events\n"
617 " --retry-failed trigger only the events which have been\n"
618 " marked as failed during a previous run\n"
619 " --subsystem-match=<subsystem> trigger devices from a matching subystem\n"
620 " --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
621 " --attr-match=<file[=<value>]> trigger devices with a matching sysfs\n"
623 " --attr-nomatch=<file[=<value>]> exclude devices with a matching sysfs\n"
625 " --help print this text\n"
633 if (sockpath != NULL) {
636 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
637 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
638 saddr.sun_family = AF_LOCAL;
639 if (sockpath[0] == '@') {
640 /* abstract namespace socket requested */
641 strlcpy(&saddr.sun_path[1], &sockpath[1], sizeof(saddr.sun_path)-1);
642 saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
643 } else if (stat(sockpath, &stats) == 0 && S_ISSOCK(stats.st_mode)) {
644 /* existing socket file */
645 strlcpy(saddr.sun_path, sockpath, sizeof(saddr.sun_path));
646 saddrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path);
648 /* no socket file, assume abstract namespace socket */
649 strlcpy(&saddr.sun_path[1], sockpath, sizeof(saddr.sun_path)-1);
650 saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
658 char base[PATH_SIZE];
661 /* if we have /sys/subsystem, forget all the old stuff */
662 strlcpy(base, sysfs_path, sizeof(base));
663 strlcat(base, "/subsystem", sizeof(base));
664 if (stat(base, &statbuf) == 0) {
665 scan_subsystem("subsystem", SCAN_SUBSYSTEM);
667 scan_subsystem("subsystem", SCAN_DEVICES);
670 scan_subsystem("bus", SCAN_SUBSYSTEM);
672 scan_subsystem("bus", SCAN_DEVICES);
675 /* scan "block" if it isn't a "class" */
676 strlcpy(base, sysfs_path, sizeof(base));
677 strlcat(base, "/class/block", sizeof(base));
678 if (stat(base, &statbuf) != 0)
685 name_list_cleanup(&filter_subsystem_match_list);
686 name_list_cleanup(&filter_subsystem_nomatch_list);
687 name_list_cleanup(&filter_attr_match_list);
688 name_list_cleanup(&filter_attr_nomatch_list);