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);
56 if (write(fd, action, strlen(action)) < 0)
57 log_debug("error writing '%s' to '%s': %m\n", action, filename);
62 static const char *keyval(const char *str, const char **val, char *buf, size_t size)
66 util_strscpy(buf, size,str);
67 pos = strchr(buf, '=');
76 static int adm_trigger(struct udev *udev, int argc, char *argv[])
78 static const struct option options[] = {
79 { "verbose", no_argument, NULL, 'v' },
80 { "dry-run", no_argument, NULL, 'n' },
81 { "type", required_argument, NULL, 't' },
82 { "action", required_argument, NULL, 'c' },
83 { "subsystem-match", required_argument, NULL, 's' },
84 { "subsystem-nomatch", required_argument, NULL, 'S' },
85 { "attr-match", required_argument, NULL, 'a' },
86 { "attr-nomatch", required_argument, NULL, 'A' },
87 { "property-match", required_argument, NULL, 'p' },
88 { "tag-match", required_argument, NULL, 'g' },
89 { "sysname-match", required_argument, NULL, 'y' },
90 { "parent-match", required_argument, NULL, 'b' },
91 { "help", no_argument, NULL, 'h' },
97 } device_type = TYPE_DEVICES;
98 const char *action = "change";
99 struct udev_enumerate *udev_enumerate;
102 udev_enumerate = udev_enumerate_new(udev);
103 if (udev_enumerate == NULL) {
112 char buf[UTIL_PATH_SIZE];
114 option = getopt_long(argc, argv, "vng:o:t:hc:p:s:S:a:A:y:b:", options, NULL);
126 if (strcmp(optarg, "devices") == 0) {
127 device_type = TYPE_DEVICES;
128 } else if (strcmp(optarg, "subsystems") == 0) {
129 device_type = TYPE_SUBSYSTEMS;
131 log_error("unknown type --type=%s\n", optarg);
140 udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
143 udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
146 key = keyval(optarg, &val, buf, sizeof(buf));
147 udev_enumerate_add_match_sysattr(udev_enumerate, key, val);
150 key = keyval(optarg, &val, buf, sizeof(buf));
151 udev_enumerate_add_nomatch_sysattr(udev_enumerate, key, val);
154 key = keyval(optarg, &val, buf, sizeof(buf));
155 udev_enumerate_add_match_property(udev_enumerate, key, val);
158 udev_enumerate_add_match_tag(udev_enumerate, optarg);
161 udev_enumerate_add_match_sysname(udev_enumerate, optarg);
164 char path[UTIL_PATH_SIZE];
165 struct udev_device *dev;
167 /* add sys dir if needed */
168 if (strncmp(optarg, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0)
169 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev), optarg, NULL);
171 util_strscpy(path, sizeof(path), optarg);
172 util_remove_trailing_chars(path, '/');
173 dev = udev_device_new_from_syspath(udev, path);
175 log_error("unable to open the device '%s'\n", optarg);
179 udev_enumerate_add_match_parent(udev_enumerate, dev);
180 /* drop reference immediately, enumerate pins the device as long as needed */
181 udev_device_unref(dev);
185 printf("Usage: udevadm trigger OPTIONS\n"
186 " --verbose print the list of devices while running\n"
187 " --dry-run do not actually trigger the events\n"
188 " --type= type of events to trigger\n"
189 " devices sys devices (default)\n"
190 " subsystems sys subsystems and drivers\n"
191 " --action=<action> event action value, default is \"change\"\n"
192 " --subsystem-match=<subsystem> trigger devices from a matching subsystem\n"
193 " --subsystem-nomatch=<subsystem> exclude devices from a matching subsystem\n"
194 " --attr-match=<file[=<value>]> trigger devices with a matching attribute\n"
195 " --attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n"
196 " --property-match=<key>=<value> trigger devices with a matching property\n"
197 " --tag-match=<key>=<value> trigger devices with a matching property\n"
198 " --sysname-match=<name> trigger devices with a matching name\n"
199 " --parent-match=<name> trigger devices with that parent device\n"
208 switch (device_type) {
209 case TYPE_SUBSYSTEMS:
210 udev_enumerate_scan_subsystems(udev_enumerate);
211 exec_list(udev_enumerate, action);
214 udev_enumerate_scan_devices(udev_enumerate);
215 exec_list(udev_enumerate, action);
221 udev_enumerate_unref(udev_enumerate);
225 const struct udevadm_cmd udevadm_trigger = {
228 .help = "request events from the kernel",