]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-trigger.c
Merge pull request #11916 from yuwata/pid1-id-renaming-handling
[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;
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_debug_errno(r, "Failed to write '%s' to '%s', ignoring: %m", action, filename);
49 continue;
50 }
51
52 if (settle_set) {
53 r = set_put_strdup(settle_set, syspath);
54 if (r < 0)
55 return log_oom();
56 }
57 }
58
59 return 0;
60 }
61
62 static int device_monitor_handler(sd_device_monitor *m, sd_device *dev, void *userdata) {
63 Set *settle_set = userdata;
64 const char *syspath;
65
66 assert(dev);
67 assert(settle_set);
68
69 if (sd_device_get_syspath(dev, &syspath) < 0)
70 return 0;
71
72 if (arg_verbose)
73 printf("settle %s\n", syspath);
74
75 if (!set_remove(settle_set, syspath))
76 log_debug("Got epoll event on syspath %s not present in syspath set", syspath);
77
78 if (set_isempty(settle_set))
79 return sd_event_exit(sd_device_monitor_get_event(m), 0);
80
81 return 0;
82 }
83
84 static char* keyval(const char *str, const char **key, const char **val) {
85 char *buf, *pos;
86
87 buf = strdup(str);
88 if (!buf)
89 return NULL;
90
91 pos = strchr(buf, '=');
92 if (pos) {
93 pos[0] = 0;
94 pos++;
95 }
96
97 *key = buf;
98 *val = pos;
99
100 return buf;
101 }
102
103 static int help(void) {
104 printf("%s trigger [OPTIONS] DEVPATH\n\n"
105 "Request events from the kernel.\n\n"
106 " -h --help Show this help\n"
107 " -V --version Show package version\n"
108 " -v --verbose Print the list of devices while running\n"
109 " -n --dry-run Do not actually trigger the events\n"
110 " -t --type= Type of events to trigger\n"
111 " devices sysfs devices (default)\n"
112 " subsystems sysfs subsystems and drivers\n"
113 " -c --action=ACTION Event action value, default is \"change\"\n"
114 " -s --subsystem-match=SUBSYSTEM Trigger devices from a matching subsystem\n"
115 " -S --subsystem-nomatch=SUBSYSTEM Exclude devices from a matching subsystem\n"
116 " -a --attr-match=FILE[=VALUE] Trigger devices with a matching attribute\n"
117 " -A --attr-nomatch=FILE[=VALUE] Exclude devices with a matching attribute\n"
118 " -p --property-match=KEY=VALUE Trigger devices with a matching property\n"
119 " -g --tag-match=KEY=VALUE Trigger devices with a matching property\n"
120 " -y --sysname-match=NAME Trigger devices with this /sys path\n"
121 " --name-match=NAME Trigger devices with this /dev name\n"
122 " -b --parent-match=NAME Trigger devices with that parent device\n"
123 " -w --settle Wait for the triggered events to complete\n"
124 " --wait-daemon[=SECONDS] Wait for udevd daemon to be initialized\n"
125 " before triggering uevents\n"
126 , program_invocation_short_name);
127
128 return 0;
129 }
130
131 int trigger_main(int argc, char *argv[], void *userdata) {
132 enum {
133 ARG_NAME = 0x100,
134 ARG_PING,
135 };
136
137 static const struct option options[] = {
138 { "verbose", no_argument, NULL, 'v' },
139 { "dry-run", no_argument, NULL, 'n' },
140 { "type", required_argument, NULL, 't' },
141 { "action", required_argument, NULL, 'c' },
142 { "subsystem-match", required_argument, NULL, 's' },
143 { "subsystem-nomatch", required_argument, NULL, 'S' },
144 { "attr-match", required_argument, NULL, 'a' },
145 { "attr-nomatch", required_argument, NULL, 'A' },
146 { "property-match", required_argument, NULL, 'p' },
147 { "tag-match", required_argument, NULL, 'g' },
148 { "sysname-match", required_argument, NULL, 'y' },
149 { "name-match", required_argument, NULL, ARG_NAME },
150 { "parent-match", required_argument, NULL, 'b' },
151 { "settle", no_argument, NULL, 'w' },
152 { "wait-daemon", optional_argument, NULL, ARG_PING },
153 { "version", no_argument, NULL, 'V' },
154 { "help", no_argument, NULL, 'h' },
155 {}
156 };
157 enum {
158 TYPE_DEVICES,
159 TYPE_SUBSYSTEMS,
160 } device_type = TYPE_DEVICES;
161 const char *action = "change";
162 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
163 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *m = NULL;
164 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
165 _cleanup_set_free_free_ Set *settle_set = NULL;
166 usec_t ping_timeout_usec = 5 * USEC_PER_SEC;
167 bool settle = false, ping = false;
168 int c, r;
169
170 if (running_in_chroot() > 0) {
171 log_info("Running in chroot, ignoring request.");
172 return 0;
173 }
174
175 r = sd_device_enumerator_new(&e);
176 if (r < 0)
177 return r;
178
179 r = sd_device_enumerator_allow_uninitialized(e);
180 if (r < 0)
181 return r;
182
183 while ((c = getopt_long(argc, argv, "vnt:c:s:S:a:A:p:g:y:b:wVh", options, NULL)) >= 0) {
184 _cleanup_free_ char *buf = NULL;
185 const char *key, *val;
186
187 switch (c) {
188 case 'v':
189 arg_verbose = true;
190 break;
191 case 'n':
192 arg_dry_run = true;
193 break;
194 case 't':
195 if (streq(optarg, "devices"))
196 device_type = TYPE_DEVICES;
197 else if (streq(optarg, "subsystems"))
198 device_type = TYPE_SUBSYSTEMS;
199 else
200 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown type --type=%s", optarg);
201 break;
202 case 'c':
203 if (device_action_from_string(optarg) < 0)
204 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown action '%s'", optarg);
205
206 action = optarg;
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 = device_enumerator_add_match_parent_incremental(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 = device_enumerator_add_match_parent_incremental(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 r = udev_ctrl_new(&uctrl);
312 if (r < 0)
313 return log_error_errno(r, "Failed to initialize udev control: %m");
314
315 r = udev_ctrl_send_ping(uctrl);
316 if (r < 0)
317 return log_error_errno(r, "Failed to connect to udev daemon: %m");
318
319 r = udev_ctrl_wait(uctrl, ping_timeout_usec);
320 if (r < 0)
321 return log_error_errno(r, "Failed to wait for daemon to reply: %m");
322 }
323
324 for (; optind < argc; optind++) {
325 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
326
327 r = find_device(argv[optind], NULL, &dev);
328 if (r < 0)
329 return log_error_errno(r, "Failed to open the device '%s': %m", argv[optind]);
330
331 r = device_enumerator_add_match_parent_incremental(e, dev);
332 if (r < 0)
333 return log_error_errno(r, "Failed to add parent match '%s': %m", argv[optind]);
334 }
335
336 if (settle) {
337 settle_set = set_new(&string_hash_ops);
338 if (!settle_set)
339 return log_oom();
340
341 r = sd_event_default(&event);
342 if (r < 0)
343 return log_error_errno(r, "Failed to get default event: %m");
344
345 r = sd_device_monitor_new(&m);
346 if (r < 0)
347 return log_error_errno(r, "Failed to create device monitor object: %m");
348
349 r = sd_device_monitor_attach_event(m, event);
350 if (r < 0)
351 return log_error_errno(r, "Failed to attach event to device monitor: %m");
352
353 r = sd_device_monitor_start(m, device_monitor_handler, settle_set);
354 if (r < 0)
355 return log_error_errno(r, "Failed to start device monitor: %m");
356 }
357
358 switch (device_type) {
359 case TYPE_SUBSYSTEMS:
360 r = device_enumerator_scan_subsystems(e);
361 if (r < 0)
362 return log_error_errno(r, "Failed to scan subsystems: %m");
363 break;
364 case TYPE_DEVICES:
365 r = device_enumerator_scan_devices(e);
366 if (r < 0)
367 return log_error_errno(r, "Failed to scan devices: %m");
368 break;
369 default:
370 assert_not_reached("Unknown device type");
371 }
372 r = exec_list(e, action, settle_set);
373 if (r < 0)
374 return r;
375
376 if (event && !set_isempty(settle_set)) {
377 r = sd_event_loop(event);
378 if (r < 0)
379 return log_error_errno(r, "Event loop failed: %m");
380 }
381
382 return 0;
383 }