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