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