2 * Copyright (C) 2008 Kay Sievers <kayi.sievers@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/types.h>
31 #include <sys/socket.h>
39 static void exec_list(struct udev_enumerate *udev_enumerate, const char *action)
41 struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
42 struct udev_list_entry *entry;
44 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(udev_enumerate)) {
45 char filename[UTIL_PATH_SIZE];
49 printf("%s\n", udev_list_entry_get_name(entry));
52 util_strlcpy(filename, udev_list_entry_get_name(entry), sizeof(filename));
53 util_strlcat(filename, "/uevent", sizeof(filename));
54 fd = open(filename, O_WRONLY);
56 dbg(udev, "error on opening %s: %m\n", filename);
59 if (write(fd, action, strlen(action)) < 0)
60 info(udev, "error writing '%s' to '%s': %m\n", action, filename);
65 static int scan_failed(struct udev_enumerate *udev_enumerate)
67 struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
68 struct udev_queue *udev_queue;
69 struct udev_list_entry *list_entry;
71 udev_queue = udev_queue_new(udev);
72 if (udev_queue == NULL)
74 udev_list_entry_foreach(list_entry, udev_queue_get_failed_list_entry(udev_queue))
75 udev_enumerate_add_syspath(udev_enumerate, udev_list_entry_get_name(list_entry));
79 int udevadm_trigger(struct udev *udev, int argc, char *argv[])
81 static const struct option options[] = {
82 { "verbose", no_argument, NULL, 'v' },
83 { "dry-run", no_argument, NULL, 'n' },
84 { "type", required_argument, NULL, 't' },
85 { "retry-failed", no_argument, NULL, 'F' },
86 { "action", required_argument, NULL, 'c' },
87 { "subsystem-match", required_argument, NULL, 's' },
88 { "subsystem-nomatch", required_argument, NULL, 'S' },
89 { "attr-match", required_argument, NULL, 'a' },
90 { "attr-nomatch", required_argument, NULL, 'A' },
91 { "help", no_argument, NULL, 'h' },
98 } device_type = TYPE_DEVICES;
99 const char *action = "add";
100 struct udev_enumerate *udev_enumerate;
103 dbg(udev, "version %s\n", VERSION);
104 udev_enumerate = udev_enumerate_new(udev);
105 if (udev_enumerate == NULL) {
112 char attr[UTIL_PATH_SIZE];
115 option = getopt_long(argc, argv, "vnFo:t:hce::s:S:a:A:", options, NULL);
127 if (strcmp(optarg, "devices") == 0) {
128 device_type = TYPE_DEVICES;
129 } else if (strcmp(optarg, "subsystems") == 0) {
130 device_type = TYPE_SUBSYSTEMS;
131 } else if (strcmp(optarg, "failed") == 0) {
132 device_type = TYPE_FAILED;
134 fprintf(stderr, "unknown type --type=%s\n", optarg);
135 err(udev, "unknown type --type=%s\n", optarg);
141 device_type = TYPE_FAILED;
147 udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
150 udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
153 util_strlcpy(attr, optarg, sizeof(attr));
154 val = strchr(attr, '=');
159 udev_enumerate_add_match_sysattr(udev_enumerate, attr, val);
162 util_strlcpy(attr, optarg, sizeof(attr));
163 val = strchr(attr, '=');
168 udev_enumerate_add_nomatch_sysattr(udev_enumerate, attr, val);
171 printf("Usage: udevadm trigger OPTIONS\n"
172 " --verbose print the list of devices while running\n"
173 " --dry-run do not actually trigger the events\n"
174 " --type= type of events to trigger\n"
175 " devices sys devices\n"
176 " subsystems sys subsystems and drivers\n"
177 " failed trigger only the events which have been\n"
178 " marked as failed during a previous run\n"
179 " --subsystem-match=<subsystem> trigger devices from a matching subystem\n"
180 " --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
181 " --attr-match=<file[=<value>]> trigger devices with a matching attribute\n"
182 " --attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n"
183 " --help print this text\n"
191 switch (device_type) {
193 scan_failed(udev_enumerate);
194 exec_list(udev_enumerate, action);
196 case TYPE_SUBSYSTEMS:
197 udev_enumerate_scan_subsystems(udev_enumerate);
198 exec_list(udev_enumerate, action);
201 udev_enumerate_scan_devices(udev_enumerate);
202 exec_list(udev_enumerate, action);
208 udev_enumerate_unref(udev_enumerate);