]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-trigger.c
udevadm: add --wait-daemon option to 'trigger' command
[thirdparty/systemd.git] / src / udev / udevadm-trigger.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2
3 #include <errno.h>
4 #include <getopt.h>
5
6 #include "sd-device.h"
7 #include "sd-event.h"
8
9 #include "device-enumerator-private.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "path-util.h"
13 #include "process-util.h"
14 #include "set.h"
15 #include "string-util.h"
16 #include "strv.h"
17 #include "udevadm.h"
18 #include "udevadm-util.h"
19 #include "udev-ctrl.h"
20 #include "virt.h"
21
22 static bool arg_verbose = false;
23 static bool arg_dry_run = false;
24
25 static int exec_list(sd_device_enumerator *e, const char *action, Set *settle_set) {
26 sd_device *d;
27 int r;
28
29 FOREACH_DEVICE_AND_SUBSYSTEM(e, d) {
30 _cleanup_free_ char *filename = NULL;
31 const char *syspath;
32
33 if (sd_device_get_syspath(d, &syspath) < 0)
34 continue;
35
36 if (arg_verbose)
37 printf("%s\n", syspath);
38 if (arg_dry_run)
39 continue;
40
41 filename = path_join(syspath, "uevent");
42 if (!filename)
43 return log_oom();
44
45 r = write_string_file(filename, action, WRITE_STRING_FILE_DISABLE_BUFFER);
46 if (r < 0) {
47 log_debug_errno(r, "Failed to write '%s' to '%s', ignoring: %m", action, filename);
48 continue;
49 }
50
51 if (settle_set) {
52 r = set_put_strdup(settle_set, syspath);
53 if (r < 0)
54 return log_oom();
55 }
56 }
57
58 return 0;
59 }
60
61 static int device_monitor_handler(sd_device_monitor *m, sd_device *dev, void *userdata) {
62 Set *settle_set = userdata;
63 const char *syspath;
64
65 assert(dev);
66 assert(settle_set);
67
68 if (sd_device_get_syspath(dev, &syspath) < 0)
69 return 0;
70
71 if (arg_verbose)
72 printf("settle %s\n", syspath);
73
74 if (!set_remove(settle_set, syspath))
75 log_debug("Got epoll event on syspath %s not present in syspath set", syspath);
76
77 if (set_isempty(settle_set))
78 return sd_event_exit(sd_device_monitor_get_event(m), 0);
79
80 return 0;
81 }
82
83 static char* keyval(const char *str, const char **key, const char **val) {
84 char *buf, *pos;
85
86 buf = strdup(str);
87 if (!buf)
88 return NULL;
89
90 pos = strchr(buf, '=');
91 if (pos) {
92 pos[0] = 0;
93 pos++;
94 }
95
96 *key = buf;
97 *val = pos;
98
99 return buf;
100 }
101
102 static int help(void) {
103 printf("%s trigger [OPTIONS] DEVPATH\n\n"
104 "Request events from the kernel.\n\n"
105 " -h --help Show this help\n"
106 " -V --version Show package version\n"
107 " -v --verbose Print the list of devices while running\n"
108 " -n --dry-run Do not actually trigger the events\n"
109 " -t --type= Type of events to trigger\n"
110 " devices sysfs devices (default)\n"
111 " subsystems sysfs subsystems and drivers\n"
112 " -c --action=ACTION Event action value, default is \"change\"\n"
113 " -s --subsystem-match=SUBSYSTEM Trigger devices from a matching subsystem\n"
114 " -S --subsystem-nomatch=SUBSYSTEM Exclude devices from a matching subsystem\n"
115 " -a --attr-match=FILE[=VALUE] Trigger devices with a matching attribute\n"
116 " -A --attr-nomatch=FILE[=VALUE] Exclude devices with a matching attribute\n"
117 " -p --property-match=KEY=VALUE Trigger devices with a matching property\n"
118 " -g --tag-match=KEY=VALUE Trigger devices with a matching property\n"
119 " -y --sysname-match=NAME Trigger devices with this /sys path\n"
120 " --name-match=NAME Trigger devices with this /dev name\n"
121 " -b --parent-match=NAME Trigger devices with that parent device\n"
122 " -w --settle Wait for the triggered events to complete\n"
123 " --wait-daemon[=SECONDS] Wait for udevd daemon to be initialized\n"
124 " before triggering uevents\n"
125 , program_invocation_short_name);
126
127 return 0;
128 }
129
130 int trigger_main(int argc, char *argv[], void *userdata) {
131 enum {
132 ARG_NAME = 0x100,
133 ARG_PING,
134 };
135
136 static const struct option options[] = {
137 { "verbose", no_argument, NULL, 'v' },
138 { "dry-run", no_argument, NULL, 'n' },
139 { "type", required_argument, NULL, 't' },
140 { "action", required_argument, NULL, 'c' },
141 { "subsystem-match", required_argument, NULL, 's' },
142 { "subsystem-nomatch", required_argument, NULL, 'S' },
143 { "attr-match", required_argument, NULL, 'a' },
144 { "attr-nomatch", required_argument, NULL, 'A' },
145 { "property-match", required_argument, NULL, 'p' },
146 { "tag-match", required_argument, NULL, 'g' },
147 { "sysname-match", required_argument, NULL, 'y' },
148 { "name-match", required_argument, NULL, ARG_NAME },
149 { "parent-match", required_argument, NULL, 'b' },
150 { "settle", no_argument, NULL, 'w' },
151 { "wait-daemon", optional_argument, NULL, ARG_PING },
152 { "version", no_argument, NULL, 'V' },
153 { "help", no_argument, NULL, 'h' },
154 {}
155 };
156 enum {
157 TYPE_DEVICES,
158 TYPE_SUBSYSTEMS,
159 } device_type = TYPE_DEVICES;
160 const char *action = "change";
161 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
162 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *m = NULL;
163 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
164 _cleanup_set_free_free_ Set *settle_set = NULL;
165 usec_t ping_timeout_usec = 5 * USEC_PER_SEC;
166 bool settle = false, ping = false;
167 int c, r;
168
169 if (running_in_chroot() > 0) {
170 log_info("Running in chroot, ignoring request.");
171 return 0;
172 }
173
174 r = sd_device_enumerator_new(&e);
175 if (r < 0)
176 return r;
177
178 r = sd_device_enumerator_allow_uninitialized(e);
179 if (r < 0)
180 return r;
181
182 while ((c = getopt_long(argc, argv, "vnt:c:s:S:a:A:p:g:y:b:wVh", options, NULL)) >= 0) {
183 _cleanup_free_ char *buf = NULL;
184 const char *key, *val;
185
186 switch (c) {
187 case 'v':
188 arg_verbose = true;
189 break;
190 case 'n':
191 arg_dry_run = true;
192 break;
193 case 't':
194 if (streq(optarg, "devices"))
195 device_type = TYPE_DEVICES;
196 else if (streq(optarg, "subsystems"))
197 device_type = TYPE_SUBSYSTEMS;
198 else
199 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown type --type=%s", optarg);
200 break;
201 case 'c':
202 if (STR_IN_SET(optarg, "add", "remove", "change"))
203 action = optarg;
204 else
205 log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown action '%s'", optarg);
206
207 break;
208 case 's':
209 r = sd_device_enumerator_add_match_subsystem(e, optarg, true);
210 if (r < 0)
211 return log_error_errno(r, "Failed to add subsystem match '%s': %m", optarg);
212 break;
213 case 'S':
214 r = sd_device_enumerator_add_match_subsystem(e, optarg, false);
215 if (r < 0)
216 return log_error_errno(r, "Failed to add negative subsystem match '%s': %m", optarg);
217 break;
218 case 'a':
219 buf = keyval(optarg, &key, &val);
220 if (!buf)
221 return log_oom();
222 r = sd_device_enumerator_add_match_sysattr(e, key, val, true);
223 if (r < 0)
224 return log_error_errno(r, "Failed to add sysattr match '%s=%s': %m", key, val);
225 break;
226 case 'A':
227 buf = keyval(optarg, &key, &val);
228 if (!buf)
229 return log_oom();
230 r = sd_device_enumerator_add_match_sysattr(e, key, val, false);
231 if (r < 0)
232 return log_error_errno(r, "Failed to add negative sysattr match '%s=%s': %m", key, val);
233 break;
234 case 'p':
235 buf = keyval(optarg, &key, &val);
236 if (!buf)
237 return log_oom();
238 r = sd_device_enumerator_add_match_property(e, key, val);
239 if (r < 0)
240 return log_error_errno(r, "Failed to add property match '%s=%s': %m", key, val);
241 break;
242 case 'g':
243 r = sd_device_enumerator_add_match_tag(e, optarg);
244 if (r < 0)
245 return log_error_errno(r, "Failed to add tag match '%s': %m", optarg);
246 break;
247 case 'y':
248 r = sd_device_enumerator_add_match_sysname(e, optarg);
249 if (r < 0)
250 return log_error_errno(r, "Failed to add sysname match '%s': %m", optarg);
251 break;
252 case 'b': {
253 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
254
255 r = find_device(optarg, "/sys", &dev);
256 if (r < 0)
257 return log_error_errno(r, "Failed to open the device '%s': %m", optarg);
258
259 r = sd_device_enumerator_add_match_parent(e, dev);
260 if (r < 0)
261 return log_error_errno(r, "Failed to add parent match '%s': %m", optarg);
262 break;
263 }
264 case 'w':
265 settle = true;
266 break;
267
268 case ARG_NAME: {
269 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
270
271 r = find_device(optarg, "/dev/", &dev);
272 if (r < 0)
273 return log_error_errno(r, "Failed to open the device '%s': %m", optarg);
274
275 r = sd_device_enumerator_add_match_parent(e, dev);
276 if (r < 0)
277 return log_error_errno(r, "Failed to add parent match '%s': %m", optarg);
278 break;
279 }
280
281 case ARG_PING: {
282 ping = true;
283 if (optarg) {
284 r = parse_sec(optarg, &ping_timeout_usec);
285 if (r < 0)
286 log_error_errno(r, "Failed to parse timeout value '%s', ignoring: %m", optarg);
287 }
288 break;
289 }
290
291 case 'V':
292 return print_version();
293 case 'h':
294 return help();
295 case '?':
296 return -EINVAL;
297 default:
298 assert_not_reached("Unknown option");
299 }
300 }
301
302 if (!arg_dry_run || ping) {
303 r = must_be_root();
304 if (r < 0)
305 return r;
306 }
307
308 if (ping) {
309 _cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL;
310
311 uctrl = udev_ctrl_new();
312 if (!uctrl)
313 return log_oom();
314
315 r = udev_ctrl_send_ping(uctrl, ping_timeout_usec);
316 if (r < 0)
317 return log_error_errno(r, "Failed to connect to udev daemon: %m");
318 }
319
320 for (; optind < argc; optind++) {
321 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
322
323 r = find_device(argv[optind], NULL, &dev);
324 if (r < 0)
325 return log_error_errno(r, "Failed to open the device '%s': %m", argv[optind]);
326
327 r = sd_device_enumerator_add_match_parent(e, dev);
328 if (r < 0)
329 return log_error_errno(r, "Failed to add parent match '%s': %m", argv[optind]);
330 }
331
332 if (settle) {
333 settle_set = set_new(&string_hash_ops);
334 if (!settle_set)
335 return log_oom();
336
337 r = sd_event_default(&event);
338 if (r < 0)
339 return log_error_errno(r, "Failed to get default event: %m");
340
341 r = sd_device_monitor_new(&m);
342 if (r < 0)
343 return log_error_errno(r, "Failed to create device monitor object: %m");
344
345 r = sd_device_monitor_attach_event(m, event);
346 if (r < 0)
347 return log_error_errno(r, "Failed to attach event to device monitor: %m");
348
349 r = sd_device_monitor_start(m, device_monitor_handler, settle_set);
350 if (r < 0)
351 return log_error_errno(r, "Failed to start device monitor: %m");
352 }
353
354 switch (device_type) {
355 case TYPE_SUBSYSTEMS:
356 r = device_enumerator_scan_subsystems(e);
357 if (r < 0)
358 return log_error_errno(r, "Failed to scan subsystems: %m");
359 break;
360 case TYPE_DEVICES:
361 r = device_enumerator_scan_devices(e);
362 if (r < 0)
363 return log_error_errno(r, "Failed to scan devices: %m");
364 break;
365 default:
366 assert_not_reached("Unknown device type");
367 }
368 r = exec_list(e, action, settle_set);
369 if (r < 0)
370 return r;
371
372 if (event && !set_isempty(settle_set)) {
373 r = sd_event_loop(event);
374 if (r < 0)
375 return log_error_errno(r, "Event loop failed: %m");
376 }
377
378 return 0;
379 }