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