]>
Commit | Line | Data |
---|---|---|
0d5be398 | 1 | /* |
fc206fbe | 2 | * Copyright (C) 2008-2009 Kay Sievers <kay.sievers@vrfy.org> |
0d5be398 | 3 | * |
55e9959b KS |
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. | |
0d5be398 | 8 | * |
55e9959b KS |
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. | |
13 | * | |
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/>. | |
0d5be398 KS |
16 | */ |
17 | ||
18 | #include <stdlib.h> | |
19 | #include <stddef.h> | |
20 | #include <string.h> | |
21 | #include <stdio.h> | |
22 | #include <unistd.h> | |
fc89fe7e | 23 | #include <getopt.h> |
0d5be398 KS |
24 | #include <errno.h> |
25 | #include <dirent.h> | |
26 | #include <fcntl.h> | |
27 | #include <syslog.h> | |
fc89fe7e | 28 | #include <fnmatch.h> |
0d5be398 KS |
29 | #include <sys/stat.h> |
30 | #include <sys/types.h> | |
ab815cae KS |
31 | #include <sys/socket.h> |
32 | #include <sys/un.h> | |
0d5be398 KS |
33 | |
34 | #include "udev.h" | |
35 | ||
c48622cc KS |
36 | static int verbose; |
37 | static int dry_run; | |
c48622cc | 38 | |
f5001d24 | 39 | static void exec_list(struct udev_enumerate *udev_enumerate, const char *action) |
c48622cc | 40 | { |
f5001d24 KS |
41 | struct udev *udev = udev_enumerate_get_udev(udev_enumerate); |
42 | struct udev_list_entry *entry; | |
ab815cae | 43 | |
f5001d24 KS |
44 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(udev_enumerate)) { |
45 | char filename[UTIL_PATH_SIZE]; | |
fc89fe7e | 46 | int fd; |
fc89fe7e | 47 | |
f5001d24 KS |
48 | if (verbose) |
49 | printf("%s\n", udev_list_entry_get_name(entry)); | |
50 | if (dry_run) | |
51 | continue; | |
065db052 | 52 | util_strscpyl(filename, sizeof(filename), udev_list_entry_get_name(entry), "/uevent", NULL); |
f5001d24 KS |
53 | fd = open(filename, O_WRONLY); |
54 | if (fd < 0) { | |
55 | dbg(udev, "error on opening %s: %m\n", filename); | |
56 | continue; | |
0d5be398 | 57 | } |
f5001d24 KS |
58 | if (write(fd, action, strlen(action)) < 0) |
59 | info(udev, "error writing '%s' to '%s': %m\n", action, filename); | |
60 | close(fd); | |
0d5be398 KS |
61 | } |
62 | } | |
63 | ||
8a1946ae | 64 | static int scan_failed(struct udev_enumerate *udev_enumerate) |
04cc3a81 | 65 | { |
f5001d24 | 66 | struct udev *udev = udev_enumerate_get_udev(udev_enumerate); |
8a1946ae KS |
67 | struct udev_queue *udev_queue; |
68 | struct udev_list_entry *list_entry; | |
04cc3a81 | 69 | |
8a1946ae KS |
70 | udev_queue = udev_queue_new(udev); |
71 | if (udev_queue == NULL) | |
72 | return -1; | |
13ddea81 KS |
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)); | |
f5001d24 | 75 | return 0; |
04cc3a81 KS |
76 | } |
77 | ||
80381823 KS |
78 | static const char *keyval(const char *str, const char **val, char *buf, size_t size) |
79 | { | |
80 | char *pos; | |
81 | ||
82 | util_strscpy(buf, size,str); | |
83 | pos = strchr(buf, '='); | |
84 | if (pos != NULL) { | |
85 | pos[0] = 0; | |
86 | pos++; | |
87 | } | |
88 | *val = pos; | |
89 | return buf; | |
90 | } | |
91 | ||
7d563a17 | 92 | int udevadm_trigger(struct udev *udev, int argc, char *argv[]) |
0d5be398 | 93 | { |
e97717ba | 94 | static const struct option options[] = { |
033e9f8c KS |
95 | { "verbose", no_argument, NULL, 'v' }, |
96 | { "dry-run", no_argument, NULL, 'n' }, | |
97 | { "type", required_argument, NULL, 't' }, | |
033e9f8c KS |
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' }, | |
80381823 | 103 | { "property-match", required_argument, NULL, 'p' }, |
28460195 | 104 | { "tag-match", required_argument, NULL, 'g' }, |
cf5bd040 | 105 | { "sysname-match", required_argument, NULL, 'y' }, |
b05211fa | 106 | { "parent-match", required_argument, NULL, 'b' }, |
033e9f8c | 107 | { "help", no_argument, NULL, 'h' }, |
fc89fe7e KS |
108 | {} |
109 | }; | |
f5001d24 KS |
110 | enum { |
111 | TYPE_DEVICES, | |
112 | TYPE_SUBSYSTEMS, | |
113 | TYPE_FAILED, | |
114 | } device_type = TYPE_DEVICES; | |
236fae6c | 115 | const char *action = "change"; |
f5001d24 KS |
116 | struct udev_enumerate *udev_enumerate; |
117 | int rc = 0; | |
0d5be398 | 118 | |
7d563a17 | 119 | dbg(udev, "version %s\n", VERSION); |
f5001d24 KS |
120 | udev_enumerate = udev_enumerate_new(udev); |
121 | if (udev_enumerate == NULL) { | |
122 | rc = 1; | |
123 | goto exit; | |
124 | } | |
0d5be398 | 125 | |
88cbfb09 | 126 | for (;;) { |
f5001d24 | 127 | int option; |
80381823 KS |
128 | const char *key; |
129 | const char *val; | |
130 | char buf[UTIL_PATH_SIZE]; | |
f5001d24 | 131 | |
b05211fa | 132 | option = getopt_long(argc, argv, "vng:o:t:hc:p:s:S:a:A:y:b:", options, NULL); |
fc89fe7e KS |
133 | if (option == -1) |
134 | break; | |
0d5be398 | 135 | |
fc89fe7e KS |
136 | switch (option) { |
137 | case 'v': | |
0d5be398 | 138 | verbose = 1; |
fc89fe7e KS |
139 | break; |
140 | case 'n': | |
0d5be398 | 141 | dry_run = 1; |
fc89fe7e | 142 | break; |
f5001d24 KS |
143 | case 't': |
144 | if (strcmp(optarg, "devices") == 0) { | |
145 | device_type = TYPE_DEVICES; | |
146 | } else if (strcmp(optarg, "subsystems") == 0) { | |
147 | device_type = TYPE_SUBSYSTEMS; | |
148 | } else if (strcmp(optarg, "failed") == 0) { | |
149 | device_type = TYPE_FAILED; | |
150 | } else { | |
f5001d24 KS |
151 | err(udev, "unknown type --type=%s\n", optarg); |
152 | rc = 2; | |
153 | goto exit; | |
154 | } | |
fc89fe7e | 155 | break; |
285e2a24 KS |
156 | case 'c': |
157 | action = optarg; | |
158 | break; | |
fc89fe7e | 159 | case 's': |
f5001d24 | 160 | udev_enumerate_add_match_subsystem(udev_enumerate, optarg); |
fc89fe7e KS |
161 | break; |
162 | case 'S': | |
f5001d24 | 163 | udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg); |
fc89fe7e KS |
164 | break; |
165 | case 'a': | |
80381823 KS |
166 | key = keyval(optarg, &val, buf, sizeof(buf)); |
167 | udev_enumerate_add_match_sysattr(udev_enumerate, key, val); | |
fc89fe7e KS |
168 | break; |
169 | case 'A': | |
80381823 KS |
170 | key = keyval(optarg, &val, buf, sizeof(buf)); |
171 | udev_enumerate_add_nomatch_sysattr(udev_enumerate, key, val); | |
172 | break; | |
173 | case 'p': | |
174 | key = keyval(optarg, &val, buf, sizeof(buf)); | |
175 | udev_enumerate_add_match_property(udev_enumerate, key, val); | |
fc89fe7e | 176 | break; |
28460195 KS |
177 | case 'g': |
178 | udev_enumerate_add_match_tag(udev_enumerate, optarg); | |
179 | break; | |
cf5bd040 KS |
180 | case 'y': |
181 | udev_enumerate_add_match_sysname(udev_enumerate, optarg); | |
182 | break; | |
b05211fa KS |
183 | case 'b': { |
184 | char path[UTIL_PATH_SIZE]; | |
185 | struct udev_device *dev; | |
186 | ||
187 | /* add sys dir if needed */ | |
188 | if (strncmp(optarg, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0) | |
189 | util_strscpyl(path, sizeof(path), udev_get_sys_path(udev), optarg, NULL); | |
190 | else | |
191 | util_strscpy(path, sizeof(path), optarg); | |
192 | util_remove_trailing_chars(path, '/'); | |
193 | dev = udev_device_new_from_syspath(udev, path); | |
194 | if (dev == NULL) { | |
195 | err(udev, "unable to open the device '%s'\n", optarg); | |
196 | rc = 2; | |
197 | goto exit; | |
198 | } | |
199 | udev_enumerate_add_match_parent(udev_enumerate, dev); | |
200 | /* drop reference immediately, enumerate pins the device as long as needed */ | |
201 | udev_device_unref(dev); | |
202 | break; | |
203 | } | |
fc89fe7e | 204 | case 'h': |
225cb03b | 205 | printf("Usage: udevadm trigger OPTIONS\n" |
7c27c752 KS |
206 | " --verbose print the list of devices while running\n" |
207 | " --dry-run do not actually trigger the events\n" | |
f5001d24 | 208 | " --type= type of events to trigger\n" |
1d59ba40 | 209 | " devices sys devices (default)\n" |
f5001d24 KS |
210 | " subsystems sys subsystems and drivers\n" |
211 | " failed trigger only the events which have been\n" | |
212 | " marked as failed during a previous run\n" | |
236fae6c | 213 | " --action=<action> event action value, default is \"change\"\n" |
214a6c79 AJ |
214 | " --subsystem-match=<subsystem> trigger devices from a matching subsystem\n" |
215 | " --subsystem-nomatch=<subsystem> exclude devices from a matching subsystem\n" | |
f5001d24 KS |
216 | " --attr-match=<file[=<value>]> trigger devices with a matching attribute\n" |
217 | " --attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n" | |
80381823 | 218 | " --property-match=<key>=<value> trigger devices with a matching property\n" |
28460195 | 219 | " --tag-match=<key>=<value> trigger devices with a matching property\n" |
cf5bd040 | 220 | " --sysname-match=<name> trigger devices with a matching name\n" |
b05211fa | 221 | " --parent-match=<name> trigger devices with that parent device\n" |
f1e7e360 | 222 | " --help\n\n"); |
fc89fe7e KS |
223 | goto exit; |
224 | default: | |
e261c5f6 | 225 | rc = 1; |
04cc3a81 | 226 | goto exit; |
0d5be398 KS |
227 | } |
228 | } | |
229 | ||
f5001d24 KS |
230 | switch (device_type) { |
231 | case TYPE_FAILED: | |
8a1946ae | 232 | scan_failed(udev_enumerate); |
f5001d24 KS |
233 | exec_list(udev_enumerate, action); |
234 | goto exit; | |
235 | case TYPE_SUBSYSTEMS: | |
236 | udev_enumerate_scan_subsystems(udev_enumerate); | |
237 | exec_list(udev_enumerate, action); | |
238 | goto exit; | |
239 | case TYPE_DEVICES: | |
240 | udev_enumerate_scan_devices(udev_enumerate); | |
241 | exec_list(udev_enumerate, action); | |
242 | goto exit; | |
243 | default: | |
7b2aad33 | 244 | goto exit; |
ab815cae | 245 | } |
0d5be398 | 246 | exit: |
f5001d24 KS |
247 | udev_enumerate_unref(udev_enumerate); |
248 | return rc; | |
0d5be398 | 249 | } |