2 * Copyright (C) 2008-2009 Kay Sievers <kay.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_strscpyl(filename, sizeof(filename), udev_list_entry_get_name(entry), "/uevent", NULL);
53 fd = open(filename, O_WRONLY);
55 dbg(udev, "error on opening %s: %m\n", filename);
58 if (write(fd, action, strlen(action)) < 0)
59 info(udev, "error writing '%s' to '%s': %m\n", action, filename);
64 static int scan_failed(struct udev_enumerate *udev_enumerate)
66 struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
67 struct udev_queue *udev_queue;
68 struct udev_list_entry *list_entry;
70 udev_queue = udev_queue_new(udev);
71 if (udev_queue == NULL)
73 udev_list_entry_foreach(list_entry, udev_queue_get_failed_list_entry(udev_queue))
74 udev_enumerate_add_syspath(udev_enumerate, udev_list_entry_get_name(list_entry));
78 static const char *keyval(const char *str, const char **val, char *buf, size_t size)
82 util_strscpy(buf, size,str);
83 pos = strchr(buf, '=');
92 int udevadm_trigger(struct udev *udev, int argc, char *argv[])
94 static const struct option options[] = {
95 { "verbose", no_argument, NULL, 'v' },
96 { "dry-run", no_argument, NULL, 'n' },
97 { "type", required_argument, NULL, 't' },
98 { "action", required_argument, NULL, 'c' },
99 { "subsystem-match", required_argument, NULL, 's' },
100 { "subsystem-nomatch", required_argument, NULL, 'S' },
101 { "attr-match", required_argument, NULL, 'a' },
102 { "attr-nomatch", required_argument, NULL, 'A' },
103 { "property-match", required_argument, NULL, 'p' },
104 { "sysname-match", required_argument, NULL, 'y' },
105 { "help", no_argument, NULL, 'h' },
112 } device_type = TYPE_DEVICES;
113 const char *action = "add";
114 struct udev_enumerate *udev_enumerate;
117 dbg(udev, "version %s\n", VERSION);
118 udev_enumerate = udev_enumerate_new(udev);
119 if (udev_enumerate == NULL) {
128 char buf[UTIL_PATH_SIZE];
130 option = getopt_long(argc, argv, "vnFo:t:hcp:s:S:a:A:y:", options, NULL);
142 if (strcmp(optarg, "devices") == 0) {
143 device_type = TYPE_DEVICES;
144 } else if (strcmp(optarg, "subsystems") == 0) {
145 device_type = TYPE_SUBSYSTEMS;
146 } else if (strcmp(optarg, "failed") == 0) {
147 device_type = TYPE_FAILED;
149 fprintf(stderr, "unknown type --type=%s\n", optarg);
150 err(udev, "unknown type --type=%s\n", optarg);
159 udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
162 udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
165 key = keyval(optarg, &val, buf, sizeof(buf));
166 udev_enumerate_add_match_sysattr(udev_enumerate, key, val);
169 key = keyval(optarg, &val, buf, sizeof(buf));
170 udev_enumerate_add_nomatch_sysattr(udev_enumerate, key, val);
173 key = keyval(optarg, &val, buf, sizeof(buf));
174 udev_enumerate_add_match_property(udev_enumerate, key, val);
177 udev_enumerate_add_match_sysname(udev_enumerate, optarg);
180 printf("Usage: udevadm trigger OPTIONS\n"
181 " --verbose print the list of devices while running\n"
182 " --dry-run do not actually trigger the events\n"
183 " --type= type of events to trigger\n"
184 " devices sys devices (default)\n"
185 " subsystems sys subsystems and drivers\n"
186 " failed trigger only the events which have been\n"
187 " marked as failed during a previous run\n"
188 " --action=<action> event action value, default is \"add\"\n"
189 " --subsystem-match=<subsystem> trigger devices from a matching subystem\n"
190 " --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
191 " --attr-match=<file[=<value>]> trigger devices with a matching attribute\n"
192 " --attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n"
193 " --property-match=<key>=<value> trigger devices with a matching property\n"
194 " --sysname-match=<name> trigger devices with a matching name\n"
202 switch (device_type) {
204 scan_failed(udev_enumerate);
205 exec_list(udev_enumerate, action);
207 case TYPE_SUBSYSTEMS:
208 udev_enumerate_scan_subsystems(udev_enumerate);
209 exec_list(udev_enumerate, action);
212 udev_enumerate_scan_devices(udev_enumerate);
213 exec_list(udev_enumerate, action);
219 udev_enumerate_unref(udev_enumerate);