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