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