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 struct udev_device *device;
77 device = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
80 info(udev, "add '%s'\n", udev_device_get_syspath(device));
81 udev_enumerate_add_device(udev_enumerate, device);
82 udev_device_unref(device);
87 int udevadm_trigger(struct udev *udev, int argc, char *argv[])
89 static const struct option options[] = {
90 { "verbose", 0, NULL, 'v' },
91 { "dry-run", 0, NULL, 'n' },
92 { "type", 1, NULL, 't' },
93 { "retry-failed", 0, NULL, 'F' },
94 { "action", 1, NULL, 'c' },
95 { "subsystem-match", 1, NULL, 's' },
96 { "subsystem-nomatch", 1, NULL, 'S' },
97 { "attr-match", 1, NULL, 'a' },
98 { "attr-nomatch", 1, NULL, 'A' },
99 { "help", 0, NULL, 'h' },
106 } device_type = TYPE_DEVICES;
107 const char *action = "add";
108 struct udev_enumerate *udev_enumerate;
111 dbg(udev, "version %s\n", VERSION);
112 udev_enumerate = udev_enumerate_new(udev);
113 if (udev_enumerate == NULL) {
120 char attr[UTIL_PATH_SIZE];
123 option = getopt_long(argc, argv, "vnFo:t:hce::s:S:a:A:", options, NULL);
135 if (strcmp(optarg, "devices") == 0) {
136 device_type = TYPE_DEVICES;
137 } else if (strcmp(optarg, "subsystems") == 0) {
138 device_type = TYPE_SUBSYSTEMS;
139 } else if (strcmp(optarg, "failed") == 0) {
140 device_type = TYPE_FAILED;
142 fprintf(stderr, "unknown type --type=%s\n", optarg);
143 err(udev, "unknown type --type=%s\n", optarg);
149 device_type = TYPE_FAILED;
155 udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
158 udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
161 util_strlcpy(attr, optarg, sizeof(attr));
162 val = strchr(attr, '=');
167 udev_enumerate_add_match_attr(udev_enumerate, attr, val);
170 util_strlcpy(attr, optarg, sizeof(attr));
171 val = strchr(attr, '=');
176 udev_enumerate_add_nomatch_attr(udev_enumerate, attr, val);
179 printf("Usage: udevadm trigger OPTIONS\n"
180 " --verbose print the list of devices while running\n"
181 " --dry-run do not actually trigger the events\n"
182 " --type= type of events to trigger\n"
183 " devices sys devices\n"
184 " subsystems sys subsystems and drivers\n"
185 " failed trigger only the events which have been\n"
186 " marked as failed during a previous run\n"
187 " --subsystem-match=<subsystem> trigger devices from a matching subystem\n"
188 " --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
189 " --attr-match=<file[=<value>]> trigger devices with a matching attribute\n"
190 " --attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n"
191 " --help print this text\n"
199 switch (device_type) {
201 scan_failed(udev_enumerate);
202 exec_list(udev_enumerate, action);
204 case TYPE_SUBSYSTEMS:
205 udev_enumerate_scan_subsystems(udev_enumerate);
206 exec_list(udev_enumerate, action);
209 udev_enumerate_scan_devices(udev_enumerate);
210 exec_list(udev_enumerate, action);
216 udev_enumerate_unref(udev_enumerate);