]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-trigger.c
Merge pull request #346 from poettering/install-bad-memory
[thirdparty/systemd.git] / src / udev / udevadm-trigger.c
1 /*
2 * Copyright (C) 2008-2009 Kay Sievers <kay@vrfy.org>
3 *
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.
8 *
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/>.
16 */
17
18 #include <stddef.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <errno.h>
24 #include <fcntl.h>
25
26 #include "udev.h"
27 #include "udev-util.h"
28 #include "udevadm-util.h"
29 #include "util.h"
30
31 static int verbose;
32 static int dry_run;
33
34 static void exec_list(struct udev_enumerate *udev_enumerate, const char *action) {
35 struct udev_list_entry *entry;
36
37 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(udev_enumerate)) {
38 char filename[UTIL_PATH_SIZE];
39 int fd;
40
41 if (verbose)
42 printf("%s\n", udev_list_entry_get_name(entry));
43 if (dry_run)
44 continue;
45 strscpyl(filename, sizeof(filename), udev_list_entry_get_name(entry), "/uevent", NULL);
46 fd = open(filename, O_WRONLY|O_CLOEXEC);
47 if (fd < 0)
48 continue;
49 if (write(fd, action, strlen(action)) < 0)
50 log_debug_errno(errno, "error writing '%s' to '%s': %m", action, filename);
51 close(fd);
52 }
53 }
54
55 static const char *keyval(const char *str, const char **val, char *buf, size_t size) {
56 char *pos;
57
58 strscpy(buf, size,str);
59 pos = strchr(buf, '=');
60 if (pos != NULL) {
61 pos[0] = 0;
62 pos++;
63 }
64 *val = pos;
65 return buf;
66 }
67
68 static void help(void) {
69 printf("%s trigger OPTIONS\n\n"
70 "Request events from the kernel.\n\n"
71 " -h --help Show this help\n"
72 " --version Show package version\n"
73 " -v --verbose Print the list of devices while running\n"
74 " -n --dry-run Do not actually trigger the events\n"
75 " -t --type= Type of events to trigger\n"
76 " devices sysfs devices (default)\n"
77 " subsystems sysfs subsystems and drivers\n"
78 " -c --action=ACTION Event action value, default is \"change\"\n"
79 " -s --subsystem-match=SUBSYSTEM Trigger devices from a matching subsystem\n"
80 " -S --subsystem-nomatch=SUBSYSTEM Exclude devices from a matching subsystem\n"
81 " -a --attr-match=FILE[=VALUE] Trigger devices with a matching attribute\n"
82 " -A --attr-nomatch=FILE[=VALUE] Exclude devices with a matching attribute\n"
83 " -p --property-match=KEY=VALUE Trigger devices with a matching property\n"
84 " -g --tag-match=KEY=VALUE Trigger devices with a matching property\n"
85 " -y --sysname-match=NAME Trigger devices with this /sys path\n"
86 " --name-match=NAME Trigger devices with this /dev name\n"
87 " -b --parent-match=NAME Trigger devices with that parent device\n"
88 , program_invocation_short_name);
89 }
90
91 static int adm_trigger(struct udev *udev, int argc, char *argv[]) {
92 enum {
93 ARG_NAME = 0x100,
94 };
95
96 static const struct option options[] = {
97 { "verbose", no_argument, NULL, 'v' },
98 { "dry-run", no_argument, NULL, 'n' },
99 { "type", required_argument, NULL, 't' },
100 { "action", required_argument, NULL, 'c' },
101 { "subsystem-match", required_argument, NULL, 's' },
102 { "subsystem-nomatch", required_argument, NULL, 'S' },
103 { "attr-match", required_argument, NULL, 'a' },
104 { "attr-nomatch", required_argument, NULL, 'A' },
105 { "property-match", required_argument, NULL, 'p' },
106 { "tag-match", required_argument, NULL, 'g' },
107 { "sysname-match", required_argument, NULL, 'y' },
108 { "name-match", required_argument, NULL, ARG_NAME },
109 { "parent-match", required_argument, NULL, 'b' },
110 { "help", no_argument, NULL, 'h' },
111 {}
112 };
113 enum {
114 TYPE_DEVICES,
115 TYPE_SUBSYSTEMS,
116 } device_type = TYPE_DEVICES;
117 const char *action = "change";
118 _cleanup_udev_enumerate_unref_ struct udev_enumerate *udev_enumerate = NULL;
119 int c, r;
120
121 udev_enumerate = udev_enumerate_new(udev);
122 if (udev_enumerate == NULL)
123 return 1;
124
125 while ((c = getopt_long(argc, argv, "vno:t:c:s:S:a:A:p:g:y:b:h", options, NULL)) >= 0) {
126 const char *key;
127 const char *val;
128 char buf[UTIL_PATH_SIZE];
129
130 switch (c) {
131 case 'v':
132 verbose = 1;
133 break;
134 case 'n':
135 dry_run = 1;
136 break;
137 case 't':
138 if (streq(optarg, "devices"))
139 device_type = TYPE_DEVICES;
140 else if (streq(optarg, "subsystems"))
141 device_type = TYPE_SUBSYSTEMS;
142 else {
143 log_error("unknown type --type=%s", optarg);
144 return 2;
145 }
146 break;
147 case 'c':
148 if (!nulstr_contains("add\0" "remove\0" "change\0", optarg)) {
149 log_error("unknown action '%s'", optarg);
150 return 2;
151 } else
152 action = optarg;
153
154 break;
155 case 's':
156 r = udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
157 if (r < 0) {
158 log_error_errno(r, "could not add subsystem match '%s': %m", optarg);
159 return 2;
160 }
161 break;
162 case 'S':
163 r = udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
164 if (r < 0) {
165 log_error_errno(r, "could not add negative subsystem match '%s': %m", optarg);
166 return 2;
167 }
168 break;
169 case 'a':
170 key = keyval(optarg, &val, buf, sizeof(buf));
171 r = udev_enumerate_add_match_sysattr(udev_enumerate, key, val);
172 if (r < 0) {
173 log_error_errno(r, "could not add sysattr match '%s=%s': %m", key, val);
174 return 2;
175 }
176 break;
177 case 'A':
178 key = keyval(optarg, &val, buf, sizeof(buf));
179 r = udev_enumerate_add_nomatch_sysattr(udev_enumerate, key, val);
180 if (r < 0) {
181 log_error_errno(r, "could not add negative sysattr match '%s=%s': %m", key, val);
182 return 2;
183 }
184 break;
185 case 'p':
186 key = keyval(optarg, &val, buf, sizeof(buf));
187 r = udev_enumerate_add_match_property(udev_enumerate, key, val);
188 if (r < 0) {
189 log_error_errno(r, "could not add property match '%s=%s': %m", key, val);
190 return 2;
191 }
192 break;
193 case 'g':
194 r = udev_enumerate_add_match_tag(udev_enumerate, optarg);
195 if (r < 0) {
196 log_error_errno(r, "could not add tag match '%s': %m", optarg);
197 return 2;
198 }
199 break;
200 case 'y':
201 r = udev_enumerate_add_match_sysname(udev_enumerate, optarg);
202 if (r < 0) {
203 log_error_errno(r, "could not add sysname match '%s': %m", optarg);
204 return 2;
205 }
206 break;
207 case 'b': {
208 _cleanup_udev_device_unref_ struct udev_device *dev;
209
210 dev = find_device(udev, optarg, "/sys");
211 if (dev == NULL) {
212 log_error("unable to open the device '%s'", optarg);
213 return 2;
214 }
215
216 r = udev_enumerate_add_match_parent(udev_enumerate, dev);
217 if (r < 0) {
218 log_error_errno(r, "could not add parent match '%s': %m", optarg);
219 return 2;
220 }
221 break;
222 }
223
224 case ARG_NAME: {
225 _cleanup_udev_device_unref_ struct udev_device *dev;
226
227 dev = find_device(udev, optarg, "/dev/");
228 if (dev == NULL) {
229 log_error("unable to open the device '%s'", optarg);
230 return 2;
231 }
232
233 r = udev_enumerate_add_match_parent(udev_enumerate, dev);
234 if (r < 0) {
235 log_error_errno(r, "could not add parent match '%s': %m", optarg);
236 return 2;
237 }
238 break;
239 }
240
241 case 'h':
242 help();
243 return 0;
244 case '?':
245 return 1;
246 default:
247 assert_not_reached("Unknown option");
248 }
249 }
250
251 for (; optind < argc; optind++) {
252 _cleanup_udev_device_unref_ struct udev_device *dev;
253
254 dev = find_device(udev, argv[optind], NULL);
255 if (dev == NULL) {
256 log_error("unable to open the device '%s'", argv[optind]);
257 return 2;
258 }
259
260 r = udev_enumerate_add_match_parent(udev_enumerate, dev);
261 if (r < 0) {
262 log_error_errno(r, "could not add tag match '%s': %m", optarg);
263 return 2;
264 }
265 }
266
267 switch (device_type) {
268 case TYPE_SUBSYSTEMS:
269 udev_enumerate_scan_subsystems(udev_enumerate);
270 exec_list(udev_enumerate, action);
271 return 0;
272 case TYPE_DEVICES:
273 udev_enumerate_scan_devices(udev_enumerate);
274 exec_list(udev_enumerate, action);
275 return 0;
276 default:
277 assert_not_reached("device_type");
278 }
279 }
280
281 const struct udevadm_cmd udevadm_trigger = {
282 .name = "trigger",
283 .cmd = adm_trigger,
284 .help = "Request events from the kernel",
285 };