]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/udev-util.c
udev: stop to use LOG_REALM_UDEV
[thirdparty/systemd.git] / src / shared / udev-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <ctype.h>
4 #include <errno.h>
5 #include <sys/inotify.h>
6 #include <unistd.h>
7
8 #include "alloc-util.h"
9 #include "device-nodes.h"
10 #include "device-util.h"
11 #include "env-file.h"
12 #include "escape.h"
13 #include "fd-util.h"
14 #include "log.h"
15 #include "macro.h"
16 #include "parse-util.h"
17 #include "path-util.h"
18 #include "signal-util.h"
19 #include "string-table.h"
20 #include "string-util.h"
21 #include "strxcpyx.h"
22 #include "udev-util.h"
23 #include "utf8.h"
24
25 static const char* const resolve_name_timing_table[_RESOLVE_NAME_TIMING_MAX] = {
26 [RESOLVE_NAME_NEVER] = "never",
27 [RESOLVE_NAME_LATE] = "late",
28 [RESOLVE_NAME_EARLY] = "early",
29 };
30
31 DEFINE_STRING_TABLE_LOOKUP(resolve_name_timing, ResolveNameTiming);
32
33 int udev_parse_config_full(
34 unsigned *ret_children_max,
35 usec_t *ret_exec_delay_usec,
36 usec_t *ret_event_timeout_usec,
37 ResolveNameTiming *ret_resolve_name_timing,
38 int *ret_timeout_signal) {
39
40 _cleanup_free_ char *log_val = NULL, *children_max = NULL, *exec_delay = NULL, *event_timeout = NULL, *resolve_names = NULL, *timeout_signal = NULL;
41 int r;
42
43 r = parse_env_file(NULL, "/etc/udev/udev.conf",
44 "udev_log", &log_val,
45 "children_max", &children_max,
46 "exec_delay", &exec_delay,
47 "event_timeout", &event_timeout,
48 "resolve_names", &resolve_names,
49 "timeout_signal", &timeout_signal);
50 if (r == -ENOENT)
51 return 0;
52 if (r < 0)
53 return r;
54
55 if (log_val) {
56 const char *log;
57 size_t n;
58
59 /* unquote */
60 n = strlen(log_val);
61 if (n >= 2 &&
62 ((log_val[0] == '"' && log_val[n-1] == '"') ||
63 (log_val[0] == '\'' && log_val[n-1] == '\''))) {
64 log_val[n - 1] = '\0';
65 log = log_val + 1;
66 } else
67 log = log_val;
68
69 /* we set the udev log level here explicitly, this is supposed
70 * to regulate the code in libudev/ and udev/. */
71 r = log_set_max_level_from_string(log);
72 if (r < 0)
73 log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
74 "failed to set udev log level '%s', ignoring: %m", log);
75 }
76
77 if (ret_children_max && children_max) {
78 r = safe_atou(children_max, ret_children_max);
79 if (r < 0)
80 log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
81 "failed to parse children_max=%s, ignoring: %m", children_max);
82 }
83
84 if (ret_exec_delay_usec && exec_delay) {
85 r = parse_sec(exec_delay, ret_exec_delay_usec);
86 if (r < 0)
87 log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
88 "failed to parse exec_delay=%s, ignoring: %m", exec_delay);
89 }
90
91 if (ret_event_timeout_usec && event_timeout) {
92 r = parse_sec(event_timeout, ret_event_timeout_usec);
93 if (r < 0)
94 log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
95 "failed to parse event_timeout=%s, ignoring: %m", event_timeout);
96 }
97
98 if (ret_resolve_name_timing && resolve_names) {
99 ResolveNameTiming t;
100
101 t = resolve_name_timing_from_string(resolve_names);
102 if (t < 0)
103 log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
104 "failed to parse resolve_names=%s, ignoring.", resolve_names);
105 else
106 *ret_resolve_name_timing = t;
107 }
108
109 if (ret_timeout_signal && timeout_signal) {
110 r = signal_from_string(timeout_signal);
111 if (r < 0)
112 log_syntax(NULL, LOG_WARNING, "/etc/udev/udev.conf", 0, r,
113 "failed to parse timeout_signal=%s, ignoring: %m", timeout_signal);
114 else
115 *ret_timeout_signal = r;
116 }
117
118 return 0;
119 }
120
121 /* Note that if -ENOENT is returned, it will be logged at debug level rather than error,
122 * because it's an expected, common occurrence that the caller will handle with a fallback */
123 static int device_new_from_dev_path(const char *devlink, sd_device **ret_device) {
124 struct stat st;
125 int r;
126
127 assert(devlink);
128
129 if (stat(devlink, &st) < 0)
130 return log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno,
131 "Failed to stat() %s: %m", devlink);
132
133 if (!S_ISBLK(st.st_mode))
134 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK),
135 "%s does not point to a block device: %m", devlink);
136
137 r = sd_device_new_from_devnum(ret_device, 'b', st.st_rdev);
138 if (r < 0)
139 return log_error_errno(r, "Failed to initialize device from %s: %m", devlink);
140
141 return 0;
142 }
143
144 struct DeviceMonitorData {
145 const char *sysname;
146 const char *devlink;
147 sd_device *device;
148 };
149
150 static void device_monitor_data_free(struct DeviceMonitorData *d) {
151 assert(d);
152
153 sd_device_unref(d->device);
154 }
155
156 static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) {
157 struct DeviceMonitorData *data = userdata;
158 const char *sysname;
159
160 assert(device);
161 assert(data);
162 assert(data->sysname || data->devlink);
163 assert(!data->device);
164
165 /* Ignore REMOVE events here. We are waiting for initialization after all, not de-initialization. We
166 * might see a REMOVE event from an earlier use of the device (devices by the same name are recycled
167 * by the kernel after all), which we should not get confused by. After all we cannot distinguish use
168 * cycles of the devices, as the udev queue is entirely asynchronous.
169 *
170 * If we see a REMOVE event here for the use cycle we actually care about then we won't notice of
171 * course, but that should be OK, given the timeout logic used on the wait loop: this will be noticed
172 * by means of -ETIMEDOUT. Thus we won't notice immediately, but eventually, and that should be
173 * sufficient for an error path that should regularly not happen.
174 *
175 * (And yes, we only need to special case REMOVE. It's the only "negative" event type, where a device
176 * ceases to exist. All other event types are "positive": the device exists and is registered in the
177 * udev database, thus whenever we see the event, we can consider it initialized.) */
178 if (device_for_action(device, DEVICE_ACTION_REMOVE))
179 return 0;
180
181 if (data->sysname && sd_device_get_sysname(device, &sysname) >= 0 && streq(sysname, data->sysname))
182 goto found;
183
184 if (data->devlink) {
185 const char *devlink;
186
187 FOREACH_DEVICE_DEVLINK(device, devlink)
188 if (path_equal(devlink, data->devlink))
189 goto found;
190
191 if (sd_device_get_devname(device, &devlink) >= 0 && path_equal(devlink, data->devlink))
192 goto found;
193 }
194
195 return 0;
196
197 found:
198 data->device = sd_device_ref(device);
199 return sd_event_exit(sd_device_monitor_get_event(monitor), 0);
200 }
201
202 static int device_wait_for_initialization_internal(
203 sd_device *_device,
204 const char *devlink,
205 const char *subsystem,
206 usec_t deadline,
207 sd_device **ret) {
208
209 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
210 _cleanup_(sd_event_source_unrefp) sd_event_source *timeout_source = NULL;
211 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
212 /* Ensure that if !_device && devlink, device gets unrefd on errors since it will be new */
213 _cleanup_(sd_device_unrefp) sd_device *device = sd_device_ref(_device);
214 _cleanup_(device_monitor_data_free) struct DeviceMonitorData data = {
215 .devlink = devlink,
216 };
217 int r;
218
219 assert(device || (subsystem && devlink));
220
221 /* Devlink might already exist, if it does get the device to use the sysname filtering */
222 if (!device && devlink) {
223 r = device_new_from_dev_path(devlink, &device);
224 if (r < 0 && r != -ENOENT)
225 return r;
226 }
227
228 if (device) {
229 if (sd_device_get_is_initialized(device) > 0) {
230 if (ret)
231 *ret = sd_device_ref(device);
232 return 0;
233 }
234 /* We need either the sysname or the devlink for filtering */
235 assert_se(sd_device_get_sysname(device, &data.sysname) >= 0 || devlink);
236 }
237
238 /* Wait until the device is initialized, so that we can get access to the ID_PATH property */
239
240 r = sd_event_new(&event);
241 if (r < 0)
242 return log_error_errno(r, "Failed to get default event: %m");
243
244 r = sd_device_monitor_new(&monitor);
245 if (r < 0)
246 return log_error_errno(r, "Failed to acquire monitor: %m");
247
248 if (device && !subsystem) {
249 r = sd_device_get_subsystem(device, &subsystem);
250 if (r < 0 && r != -ENOENT)
251 return log_device_error_errno(device, r, "Failed to get subsystem: %m");
252 }
253
254 if (subsystem) {
255 r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, subsystem, NULL);
256 if (r < 0)
257 return log_error_errno(r, "Failed to add %s subsystem match to monitor: %m", subsystem);
258 }
259
260 r = sd_device_monitor_attach_event(monitor, event);
261 if (r < 0)
262 return log_error_errno(r, "Failed to attach event to device monitor: %m");
263
264 r = sd_device_monitor_start(monitor, device_monitor_handler, &data);
265 if (r < 0)
266 return log_error_errno(r, "Failed to start device monitor: %m");
267
268 if (deadline != USEC_INFINITY) {
269 r = sd_event_add_time(
270 event, &timeout_source,
271 CLOCK_MONOTONIC, deadline, 0,
272 NULL, INT_TO_PTR(-ETIMEDOUT));
273 if (r < 0)
274 return log_error_errno(r, "Failed to add timeout event source: %m");
275 }
276
277 /* Check again, maybe things changed. Udev will re-read the db if the device wasn't initialized
278 * yet. */
279 if (!device && devlink) {
280 r = device_new_from_dev_path(devlink, &device);
281 if (r < 0 && r != -ENOENT)
282 return r;
283 }
284 if (device && sd_device_get_is_initialized(device) > 0) {
285 if (ret)
286 *ret = sd_device_ref(device);
287 return 0;
288 }
289
290 r = sd_event_loop(event);
291 if (r < 0)
292 return log_error_errno(r, "Failed to wait for device to be initialized: %m");
293
294 if (ret)
295 *ret = TAKE_PTR(data.device);
296 return 0;
297 }
298
299 int device_wait_for_initialization(sd_device *device, const char *subsystem, usec_t deadline, sd_device **ret) {
300 return device_wait_for_initialization_internal(device, NULL, subsystem, deadline, ret);
301 }
302
303 int device_wait_for_devlink(const char *devlink, const char *subsystem, usec_t deadline, sd_device **ret) {
304 return device_wait_for_initialization_internal(NULL, devlink, subsystem, deadline, ret);
305 }
306
307 int device_is_renaming(sd_device *dev) {
308 int r;
309
310 assert(dev);
311
312 r = sd_device_get_property_value(dev, "ID_RENAMING", NULL);
313 if (r == -ENOENT)
314 return false;
315 if (r < 0)
316 return r;
317
318 return true;
319 }
320
321 bool device_for_action(sd_device *dev, DeviceAction action) {
322 DeviceAction a;
323
324 assert(dev);
325
326 if (device_get_action(dev, &a) < 0)
327 return false;
328
329 return a == action;
330 }
331
332 void log_device_uevent(sd_device *device, const char *str) {
333 DeviceAction action = _DEVICE_ACTION_INVALID;
334 uint64_t seqnum = 0;
335
336 if (!DEBUG_LOGGING)
337 return;
338
339 (void) device_get_seqnum(device, &seqnum);
340 (void) device_get_action(device, &action);
341 log_device_debug(device, "%s%s(SEQNUM=%"PRIu64", ACTION=%s)",
342 strempty(str), isempty(str) ? "" : " ",
343 seqnum, strna(device_action_to_string(action)));
344 }
345
346 int udev_rule_parse_value(char *str, char **ret_value, char **ret_endpos) {
347 char *i, *j;
348 int r;
349 bool is_escaped;
350
351 /* value must be double quotated */
352 is_escaped = str[0] == 'e';
353 str += is_escaped;
354 if (str[0] != '"')
355 return -EINVAL;
356 str++;
357
358 if (!is_escaped) {
359 /* unescape double quotation '\"'->'"' */
360 for (i = j = str; *i != '"'; i++, j++) {
361 if (*i == '\0')
362 return -EINVAL;
363 if (i[0] == '\\' && i[1] == '"')
364 i++;
365 *j = *i;
366 }
367 j[0] = '\0';
368 } else {
369 _cleanup_free_ char *unescaped = NULL;
370
371 /* find the end position of value */
372 for (i = str; *i != '"'; i++) {
373 if (i[0] == '\\')
374 i++;
375 if (*i == '\0')
376 return -EINVAL;
377 }
378 i[0] = '\0';
379
380 r = cunescape_length(str, i - str, 0, &unescaped);
381 if (r < 0)
382 return r;
383 assert(r <= i - str);
384 memcpy(str, unescaped, r + 1);
385 }
386
387 *ret_value = str;
388 *ret_endpos = i + 1;
389 return 0;
390 }
391
392 size_t udev_replace_whitespace(const char *str, char *to, size_t len) {
393 bool is_space = false;
394 size_t i, j;
395
396 assert(str);
397 assert(to);
398
399 /* Copy from 'str' to 'to', while removing all leading and trailing whitespace, and replacing
400 * each run of consecutive whitespace with a single underscore. The chars from 'str' are copied
401 * up to the \0 at the end of the string, or at most 'len' chars. This appends \0 to 'to', at
402 * the end of the copied characters.
403 *
404 * If 'len' chars are copied into 'to', the final \0 is placed at len+1 (i.e. 'to[len] = \0'),
405 * so the 'to' buffer must have at least len+1 chars available.
406 *
407 * Note this may be called with 'str' == 'to', i.e. to replace whitespace in-place in a buffer.
408 * This function can handle that situation.
409 *
410 * Note that only 'len' characters are read from 'str'. */
411
412 i = strspn(str, WHITESPACE);
413
414 for (j = 0; j < len && i < len && str[i] != '\0'; i++) {
415 if (isspace(str[i])) {
416 is_space = true;
417 continue;
418 }
419
420 if (is_space) {
421 if (j + 1 >= len)
422 break;
423
424 to[j++] = '_';
425 is_space = false;
426 }
427 to[j++] = str[i];
428 }
429
430 to[j] = '\0';
431 return j;
432 }
433
434 size_t udev_replace_chars(char *str, const char *allow) {
435 size_t i = 0, replaced = 0;
436
437 assert(str);
438
439 /* allow chars in allow list, plain ascii, hex-escaping and valid utf8. */
440
441 while (str[i] != '\0') {
442 int len;
443
444 if (allow_listed_char_for_devnode(str[i], allow)) {
445 i++;
446 continue;
447 }
448
449 /* accept hex encoding */
450 if (str[i] == '\\' && str[i+1] == 'x') {
451 i += 2;
452 continue;
453 }
454
455 /* accept valid utf8 */
456 len = utf8_encoded_valid_unichar(str + i, (size_t) -1);
457 if (len > 1) {
458 i += len;
459 continue;
460 }
461
462 /* if space is allowed, replace whitespace with ordinary space */
463 if (isspace(str[i]) && allow && strchr(allow, ' ')) {
464 str[i] = ' ';
465 i++;
466 replaced++;
467 continue;
468 }
469
470 /* everything else is replaced with '_' */
471 str[i] = '_';
472 i++;
473 replaced++;
474 }
475 return replaced;
476 }
477
478 int udev_resolve_subsys_kernel(const char *string, char *result, size_t maxsize, bool read_value) {
479 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
480 _cleanup_free_ char *temp = NULL;
481 char *subsys, *sysname, *attr;
482 const char *val;
483 int r;
484
485 assert(string);
486 assert(result);
487
488 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
489
490 if (string[0] != '[')
491 return -EINVAL;
492
493 temp = strdup(string);
494 if (!temp)
495 return -ENOMEM;
496
497 subsys = &temp[1];
498
499 sysname = strchr(subsys, '/');
500 if (!sysname)
501 return -EINVAL;
502 sysname[0] = '\0';
503 sysname = &sysname[1];
504
505 attr = strchr(sysname, ']');
506 if (!attr)
507 return -EINVAL;
508 attr[0] = '\0';
509 attr = &attr[1];
510 if (attr[0] == '/')
511 attr = &attr[1];
512 if (attr[0] == '\0')
513 attr = NULL;
514
515 if (read_value && !attr)
516 return -EINVAL;
517
518 r = sd_device_new_from_subsystem_sysname(&dev, subsys, sysname);
519 if (r < 0)
520 return r;
521
522 if (read_value) {
523 r = sd_device_get_sysattr_value(dev, attr, &val);
524 if (r < 0 && r != -ENOENT)
525 return r;
526 if (r == -ENOENT)
527 result[0] = '\0';
528 else
529 strscpy(result, maxsize, val);
530 log_debug("value '[%s/%s]%s' is '%s'", subsys, sysname, attr, result);
531 } else {
532 r = sd_device_get_syspath(dev, &val);
533 if (r < 0)
534 return r;
535
536 strscpyl(result, maxsize, val, attr ? "/" : NULL, attr ?: NULL, NULL);
537 log_debug("path '[%s/%s]%s' is '%s'", subsys, sysname, strempty(attr), result);
538 }
539 return 0;
540 }
541
542 int udev_queue_is_empty(void) {
543 return access("/run/udev/queue", F_OK) < 0 ?
544 (errno == ENOENT ? true : -errno) : false;
545 }
546
547 int udev_queue_init(void) {
548 _cleanup_close_ int fd = -1;
549
550 fd = inotify_init1(IN_CLOEXEC);
551 if (fd < 0)
552 return -errno;
553
554 if (inotify_add_watch(fd, "/run/udev" , IN_DELETE) < 0)
555 return -errno;
556
557 return TAKE_FD(fd);
558 }