Debian Codegraph @

src/udev/udevadm-util.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include "sd-bus.h"
4
5 #include "alloc-util.h"
6 #include "bus-error.h"
7 #include "bus-util.h"
8 #include "conf-files.h"
9 #include "constants.h"
10 #include "device-private.h"
11 #include "errno-util.h"
12 #include "extract-word.h"
13 #include "log.h"
14 #include "path-util.h"
15 #include "string-table.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "udev-ctrl.h"
19 #include "udev-rules.h"
20 #include "udev-varlink.h"
21 #include "udevadm-util.h"
22 #include "unit-def.h"
23 #include "unit-name.h"
24 #include "varlink-util.h"
25
26 static int find_device_from_unit(const char *unit_name, sd_device **ret) {
27 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
28 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
29 _cleanup_free_ char *unit_path = NULL, *syspath = NULL;
30 int r;
31
32 if (!unit_name_is_valid(unit_name, UNIT_NAME_PLAIN))
33 return -EINVAL;
34
35 if (unit_name_to_type(unit_name) != UNIT_DEVICE)
36 return -EINVAL;
37
38 r = bus_connect_system_systemd(&bus);
39 if (r < 0) {
40 _cleanup_free_ char *path = NULL;
41
42 log_debug_errno(r, "Failed to open connection to systemd, using unit name as syspath: %m");
43
44 r = unit_name_to_path(unit_name, &path);
45 if (r < 0)
46 return log_debug_errno(r, "Failed to convert \"%s\" to a device path: %m", unit_name);
47
48 return sd_device_new_from_path(ret, path);
49 }
50
51 unit_path = unit_dbus_path_from_name(unit_name);
52 if (!unit_path)
53 return -ENOMEM;
54
55 r = sd_bus_get_property_string(
56 bus,
57 "org.freedesktop.systemd1",
58 unit_path,
59 "org.freedesktop.systemd1.Device",
60 "SysFSPath",
61 &error,
62 &syspath);
63 if (r < 0)
64 return log_debug_errno(r, "Failed to get SysFSPath= dbus property for %s: %s",
65 unit_name, bus_error_message(&error, r));
66
67 return sd_device_new_from_syspath(ret, syspath);
68 }
69
70 int find_device(const char *id, const char *prefix, sd_device **ret) {
71 assert(id);
72 assert(ret);
73
74 if (sd_device_new_from_device_id(ret, id) >= 0)
75 return 0;
76
77 if (sd_device_new_from_path(ret, id) >= 0)
78 return 0;
79
80 if (prefix && !path_startswith(id, prefix)) {
81 _cleanup_free_ char *path = NULL;
82
83 path = path_join(prefix, id);
84 if (!path)
85 return -ENOMEM;
86
87 if (sd_device_new_from_path(ret, path) >= 0)
88 return 0;
89 }
90
91 /* if a path is provided, then it cannot be a unit name. Let's return earlier. */
92 if (is_path(id))
93 return -ENODEV;
94
95 /* Check if the argument looks like a device unit name. */
96 return find_device_from_unit(id, ret);
97 }
98
99 int find_device_with_action(const char *id, sd_device_action_t action, sd_device **ret) {
100 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
101 int r;
102
103 assert(id);
104 assert(ret);
105 assert(action >= 0 && action < _SD_DEVICE_ACTION_MAX);
106
107 r = find_device(id, "/sys", &dev);
108 if (r < 0)
109 return r;
110
111 r = device_read_uevent_file(dev);
112 if (r < 0)
113 return r;
114
115 r = device_set_action(dev, action);
116 if (r < 0)
117 return r;
118
119 *ret = TAKE_PTR(dev);
120 return 0;
121 }
122
123
124 int parse_device_action(const char *str, sd_device_action_t *ret) {
125 assert(str);
126
127 if (streq(str, "help"))
128 return DUMP_STRING_TABLE(device_action, sd_device_action_t, _SD_DEVICE_ACTION_MAX);
129
130 sd_device_action_t a = device_action_from_string(str);
131 if (a < 0)
132 return log_error_errno(a, "Invalid action '%s'.", str);
133
134 if (ret)
135 *ret = a;
136 return 1;
137 }
138
139 int parse_resolve_name_timing(const char *str, ResolveNameTiming *ret) {
140 assert(str);
141
142 if (streq(str, "help"))
143 return DUMP_STRING_TABLE(resolve_name_timing, ResolveNameTiming, _RESOLVE_NAME_TIMING_MAX);
144
145 ResolveNameTiming v = resolve_name_timing_from_string(str);
146 if (v < 0)
147 return log_error_errno(v, "--resolve-names= must be 'early', 'late', or 'never'.");
148
149 if (ret)
150 *ret = v;
151 return 1;
152 }
153
154 int parse_key_value_argument(const char *str, bool require_value, char **key, char **value) {
155 _cleanup_free_ char *k = NULL, *v = NULL;
156 const char *s = str;
157 int r;
158
159 assert(s);
160 assert(key);
161 assert(value);
162
163 r = extract_many_words(&s, "=", EXTRACT_DONT_COALESCE_SEPARATORS, &k, &v);
164 if (r < 0)
165 return log_error_errno(r, "Failed to parse key/value pair %s: %m", str);
166 if (require_value && r < 2)
167 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Missing '=' in key/value pair %s.", str);
168
169 if (!filename_is_valid(k))
170 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "%s is not a valid key name", k);
171
172 free_and_replace(*key, k);
173 free_and_replace(*value, v);
174 return 0;
175 }
176
177 static int udev_ping_via_ctrl(usec_t timeout_usec, bool ignore_connection_failure) {
178 _cleanup_(udev_ctrl_unrefp) UdevCtrl *uctrl = NULL;
179 int r;
180
181 r = udev_ctrl_new(&uctrl);
182 if (r < 0)
183 return log_error_errno(r, "Failed to initialize udev control: %m");
184
185 r = udev_ctrl_send_ping(uctrl);
186 if (r < 0) {
187 bool ignore = ignore_connection_failure && (ERRNO_IS_NEG_DISCONNECT(r) || r == -ENOENT);
188 log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, r,
189 "Failed to connect to udev daemon%s: %m",
190 ignore ? ", ignoring" : "");
191 return ignore ? 0 : r;
192 }
193
194 r = udev_ctrl_wait(uctrl, timeout_usec);
195 if (r < 0)
196 return log_error_errno(r, "Failed to wait for daemon to reply: %m");
197
198 return 1; /* received reply */
199 }
200
201 int udev_ping(usec_t timeout_usec, bool ignore_connection_failure) {
202 _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *link = NULL;
203 int r;
204
205 r = udev_varlink_connect(&link, timeout_usec);
206 if (ERRNO_IS_NEG_DISCONNECT(r) || r == -ENOENT) {
207 log_debug_errno(r, "Failed to connect to udev via varlink, falling back to use legacy control socket, ignoring: %m");
208 return udev_ping_via_ctrl(timeout_usec, ignore_connection_failure);
209 }
210 if (r < 0)
211 return log_error_errno(r, "Failed to connect to udev via varlink: %m");
212
213 r = varlink_call_and_log(link, "io.systemd.service.Ping", /* parameters= */ NULL, /* reply= */ NULL);
214 if (r < 0)
215 return r;
216
217 return 1; /* received reply */
218 }
219
220 static int search_rules_file_in_conf_dirs(const char *s, const char *root, ConfFile ***files, size_t *n_files) {
221 _cleanup_free_ char *with_suffix = NULL;
222 int r;
223
224 assert(s);
225 assert(files);
226 assert(n_files);
227
228 if (isempty(s) || is_path(s))
229 return 0;
230
231 if (!endswith(s, ".rules")) {
232 with_suffix = strjoin(s, ".rules");
233 if (!with_suffix)
234 return log_oom();
235
236 s = with_suffix;
237 }
238
239 if (!filename_is_valid(s))
240 return 0;
241
242 STRV_FOREACH(p, CONF_PATHS_STRV("udev/rules.d")) {
243
244 _cleanup_free_ char *path = path_join(*p, s);
245 if (!path)
246 return log_oom();
247
248 _cleanup_(conf_file_freep) ConfFile *c = NULL;
249 r = conf_file_new(path, root, CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED, &c);
250 if (r == -ERFKILL) {
251 log_warning_errno(r, "File '%s%s' is a mask, ignoring.", empty_to_root(root), skip_leading_slash(path));
252 return 1; /* Found masked file. */
253 }
254 if (r == -ENOENT)
255 continue;
256 if (r < 0)
257 return log_error_errno(r, "Failed to chase '%s%s': %m", empty_to_root(root), skip_leading_slash(path));
258
259 if (!GREEDY_REALLOC_APPEND(*files, *n_files, &c, 1))
260 return log_oom();
261
262 TAKE_PTR(c);
263 return 1; /* found */
264 }
265
266 return 0;
267 }
268
269 static int search_rules_file(const char *s, const char *root, ConfFile ***files, size_t *n_files) {
270 int r;
271
272 assert(s);
273 assert(files);
274 assert(n_files);
275
276 /* If the input is a file name (e.g. 99-systemd.rules), then try to find it in udev/rules.d directories. */
277 r = search_rules_file_in_conf_dirs(s, root, files, n_files);
278 if (r != 0)
279 return r;
280
281 /* If not found, or if it is a path, then chase it. */
282 _cleanup_(conf_file_freep) ConfFile *c = NULL;
283 r = conf_file_new(s, root, CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED, &c);
284 if (r == -ERFKILL) {
285 log_warning_errno(r, "File '%s%s' is a mask, ignoring.", empty_to_root(root), skip_leading_slash(s));
286 return 0; /* Found masked file. */
287 }
288 if (r >= 0) {
289 if (!GREEDY_REALLOC_APPEND(*files, *n_files, &c, 1))
290 return log_oom();
291
292 TAKE_PTR(c);
293 return 0;
294 }
295
296 if (r != -EISDIR)
297 return log_error_errno(r, "Failed to chase '%s%s': %m", empty_to_root(root), skip_leading_slash(s));
298
299 /* If a directory is specified, then find all rules file in the directory. */
300 ConfFile **f = NULL;
301 size_t n = 0;
302
303 CLEANUP_ARRAY(f, n, conf_file_free_array);
304
305 r = conf_files_list_strv_full(".rules", root, CONF_FILES_REGULAR | CONF_FILES_WARN, (const char* const*) STRV_MAKE_CONST(s), &f, &n);
306 if (r < 0)
307 return log_error_errno(r, "Failed to enumerate rules files in '%s%s': %m", empty_to_root(root), skip_leading_slash(s));
308
309 if (!GREEDY_REALLOC_APPEND(*files, *n_files, f, n))
310 return log_oom();
311
312 f = mfree(f); /* The array elements are owned by 'files'. So, conf_file_free_array() must not be called. */
313 n = 0;
314 return 0;
315 }
316
317 int search_rules_files(char * const *a, const char *root, ConfFile ***ret_files, size_t *ret_n_files) {
318 ConfFile **files = NULL;
319 size_t n_files = 0;
320 int r;
321
322 CLEANUP_ARRAY(files, n_files, conf_file_free_array);
323
324 assert(ret_files);
325 assert(ret_n_files);
326
327 if (strv_isempty(a)) {
328 r = conf_files_list_strv_full(".rules", root, CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED | CONF_FILES_WARN,
329 (const char* const*) CONF_PATHS_STRV("udev/rules.d"), &files, &n_files);
330 if (r < 0)
331 return log_error_errno(r, "Failed to enumerate rules files: %m");
332
333 if (root && n_files == 0)
334 return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No rules files found in '%s'.", root);
335
336 } else
337 STRV_FOREACH(s, a) {
338 r = search_rules_file(*s, root, &files, &n_files);
339 if (r < 0)
340 return r;
341 }
342
343 *ret_files = TAKE_PTR(files);
344 *ret_n_files = n_files;
345 return 0;
346 }